7 #include "packager/packager.h"
11 #include "packager/app/job_manager.h"
12 #include "packager/app/libcrypto_threading.h"
13 #include "packager/app/muxer_factory.h"
14 #include "packager/app/packager_util.h"
15 #include "packager/app/single_thread_job_manager.h"
16 #include "packager/app/stream_descriptor.h"
17 #include "packager/base/at_exit.h"
18 #include "packager/base/files/file_path.h"
19 #include "packager/base/logging.h"
20 #include "packager/base/optional.h"
21 #include "packager/base/path_service.h"
22 #include "packager/base/strings/string_util.h"
23 #include "packager/base/strings/stringprintf.h"
24 #include "packager/base/threading/simple_thread.h"
25 #include "packager/base/time/clock.h"
26 #include "packager/file/file.h"
27 #include "packager/hls/base/hls_notifier.h"
28 #include "packager/hls/base/simple_hls_notifier.h"
29 #include "packager/media/base/cc_stream_filter.h"
30 #include "packager/media/base/container_names.h"
31 #include "packager/media/base/fourccs.h"
32 #include "packager/media/base/key_source.h"
33 #include "packager/media/base/language_utils.h"
34 #include "packager/media/base/muxer.h"
35 #include "packager/media/base/muxer_options.h"
36 #include "packager/media/base/muxer_util.h"
37 #include "packager/media/chunking/chunking_handler.h"
38 #include "packager/media/chunking/cue_alignment_handler.h"
39 #include "packager/media/chunking/text_chunker.h"
40 #include "packager/media/crypto/encryption_handler.h"
41 #include "packager/media/demuxer/demuxer.h"
42 #include "packager/media/event/muxer_listener_factory.h"
43 #include "packager/media/event/vod_media_info_dump_muxer_listener.h"
44 #include "packager/media/formats/ttml/ttml_to_mp4_handler.h"
45 #include "packager/media/formats/webvtt/text_padder.h"
46 #include "packager/media/formats/webvtt/webvtt_to_mp4_handler.h"
47 #include "packager/media/replicator/replicator.h"
48 #include "packager/media/trick_play/trick_play_handler.h"
49 #include "packager/mpd/base/media_info.pb.h"
50 #include "packager/mpd/base/mpd_builder.h"
51 #include "packager/mpd/base/simple_mpd_notifier.h"
52 #include "packager/status_macros.h"
53 #include "packager/version/version.h"
59 using media::JobManager;
60 using media::KeySource;
61 using media::MuxerOptions;
62 using media::SingleThreadJobManager;
63 using media::SyncPointQueue;
68 const char kMediaInfoSuffix[] =
".media_info";
70 const int64_t kDefaultTextZeroBiasMs = 10 * 60 * 1000;
72 MuxerListenerFactory::StreamData ToMuxerListenerData(
73 const StreamDescriptor& stream) {
74 MuxerListenerFactory::StreamData data;
75 data.media_info_output = stream.output;
77 data.hls_group_id = stream.hls_group_id;
78 data.hls_name = stream.hls_name;
79 data.hls_playlist_name = stream.hls_playlist_name;
80 data.hls_iframe_playlist_name = stream.hls_iframe_playlist_name;
81 data.hls_characteristics = stream.hls_characteristics;
82 data.hls_only = stream.hls_only;
84 data.dash_accessiblities = stream.dash_accessiblities;
85 data.dash_roles = stream.dash_roles;
86 data.dash_only = stream.dash_only;
93 bool DetermineTextFileCodec(
const std::string& file, std::string* out) {
98 LOG(ERROR) <<
"Failed to open file " << file
99 <<
" to determine file format.";
103 const uint8_t* content_data =
104 reinterpret_cast<const uint8_t*
>(content.data());
105 MediaContainerName container_name =
106 DetermineContainer(content_data, content.size());
108 if (container_name == CONTAINER_WEBVTT) {
113 if (container_name == CONTAINER_TTML) {
121 MediaContainerName GetOutputFormat(
const StreamDescriptor& descriptor) {
122 if (!descriptor.output_format.empty()) {
123 MediaContainerName format =
124 DetermineContainerFromFormatName(descriptor.output_format);
125 if (format == CONTAINER_UNKNOWN) {
126 LOG(ERROR) <<
"Unable to determine output format from '"
127 << descriptor.output_format <<
"'.";
132 base::Optional<MediaContainerName> format_from_output;
133 base::Optional<MediaContainerName> format_from_segment;
134 if (!descriptor.output.empty()) {
135 format_from_output = DetermineContainerFromFileName(descriptor.output);
136 if (format_from_output.value() == CONTAINER_UNKNOWN) {
137 LOG(ERROR) <<
"Unable to determine output format from '"
138 << descriptor.output <<
"'.";
141 if (!descriptor.segment_template.empty()) {
142 format_from_segment =
143 DetermineContainerFromFileName(descriptor.segment_template);
144 if (format_from_segment.value() == CONTAINER_UNKNOWN) {
145 LOG(ERROR) <<
"Unable to determine output format from '"
146 << descriptor.segment_template <<
"'.";
150 if (format_from_output && format_from_segment) {
151 if (format_from_output.value() != format_from_segment.value()) {
152 LOG(ERROR) <<
"Output format determined from '" << descriptor.output
153 <<
"' differs from output format determined from '"
154 << descriptor.segment_template <<
"'.";
155 return CONTAINER_UNKNOWN;
159 if (format_from_output)
160 return format_from_output.value();
161 if (format_from_segment)
162 return format_from_segment.value();
163 return CONTAINER_UNKNOWN;
166 MediaContainerName GetTextOutputCodec(
const StreamDescriptor& descriptor) {
167 const auto output_container = GetOutputFormat(descriptor);
168 if (output_container != CONTAINER_MOV)
169 return output_container;
171 const auto input_container = DetermineContainerFromFileName(descriptor.input);
172 if (base::EqualsCaseInsensitiveASCII(descriptor.output_format,
"vtt+mp4") ||
173 base::EqualsCaseInsensitiveASCII(descriptor.output_format,
175 return CONTAINER_WEBVTT;
176 }
else if (!base::EqualsCaseInsensitiveASCII(descriptor.output_format,
178 input_container == CONTAINER_WEBVTT) {
180 return CONTAINER_WEBVTT;
183 return CONTAINER_TTML;
187 bool IsTextStream(
const StreamDescriptor& stream) {
188 if (stream.stream_selector ==
"text")
190 if (base::EqualsCaseInsensitiveASCII(stream.output_format,
"vtt+mp4") ||
191 base::EqualsCaseInsensitiveASCII(stream.output_format,
"webvtt+mp4") ||
192 base::EqualsCaseInsensitiveASCII(stream.output_format,
"ttml+mp4")) {
196 auto output_format = GetOutputFormat(stream);
197 return output_format == CONTAINER_WEBVTT || output_format == CONTAINER_TTML;
200 Status ValidateStreamDescriptor(
bool dump_stream_info,
201 const StreamDescriptor& stream) {
202 if (stream.input.empty()) {
203 return Status(error::INVALID_ARGUMENT,
"Stream input not specified.");
208 if (dump_stream_info && stream.output.empty() &&
209 stream.segment_template.empty()) {
213 if (stream.output.empty() && stream.segment_template.empty()) {
214 return Status(error::INVALID_ARGUMENT,
215 "Streams must specify 'output' or 'segment template'.");
219 if (stream.stream_selector.empty()) {
220 return Status(error::INVALID_ARGUMENT,
221 "Stream stream_selector not specified.");
225 if (stream.segment_template.length()) {
226 RETURN_IF_ERROR(ValidateSegmentTemplate(stream.segment_template));
231 const MediaContainerName output_format = GetOutputFormat(stream);
233 if (output_format == CONTAINER_UNKNOWN) {
234 return Status(error::INVALID_ARGUMENT,
"Unsupported output format.");
236 if (output_format == MediaContainerName::CONTAINER_MPEG2TS) {
237 if (stream.segment_template.empty()) {
239 error::INVALID_ARGUMENT,
240 "Please specify 'segment_template'. Single file TS output is "
247 if (stream.output.length()) {
248 return Status(error::INVALID_ARGUMENT,
249 "All TS segments must be self-initializing. Stream "
250 "descriptors 'output' or 'init_segment' are not allowed.");
252 }
else if (output_format == CONTAINER_WEBVTT ||
253 output_format == CONTAINER_TTML ||
254 output_format == CONTAINER_AAC || output_format == CONTAINER_MP3 ||
255 output_format == CONTAINER_AC3 ||
256 output_format == CONTAINER_EAC3) {
259 if (stream.segment_template.length() && stream.output.length()) {
261 error::INVALID_ARGUMENT,
262 "Segmented subtitles or PackedAudio output cannot have an init "
263 "segment. Do not specify stream descriptors 'output' or "
264 "'init_segment' when using 'segment_template'.");
269 if (stream.segment_template.length() && stream.output.empty()) {
270 return Status(error::INVALID_ARGUMENT,
271 "Please specify 'init_segment'. All non-TS multi-segment "
272 "content must provide an init segment.");
276 if (stream.output.find(
'$') != std::string::npos) {
277 if (output_format == CONTAINER_WEBVTT) {
279 error::UNIMPLEMENTED,
280 "WebVTT output with one file per Representation per Period "
281 "is not supported yet. Please use fMP4 instead. If that needs to be "
282 "supported, please file a feature request on GitHub.");
287 RETURN_IF_ERROR(ValidateSegmentTemplate(stream.output));
293 Status ValidateParams(
const PackagingParams& packaging_params,
294 const std::vector<StreamDescriptor>& stream_descriptors) {
295 if (!packaging_params.chunking_params.segment_sap_aligned &&
296 packaging_params.chunking_params.subsegment_sap_aligned) {
297 return Status(error::INVALID_ARGUMENT,
298 "Setting segment_sap_aligned to false but "
299 "subsegment_sap_aligned to true is not allowed.");
302 if (stream_descriptors.empty()) {
303 return Status(error::INVALID_ARGUMENT,
304 "Stream descriptors cannot be empty.");
309 const bool on_demand_dash_profile =
310 stream_descriptors.begin()->segment_template.empty();
311 std::set<std::string> outputs;
312 std::set<std::string> segment_templates;
313 for (
const auto& descriptor : stream_descriptors) {
314 if (on_demand_dash_profile != descriptor.segment_template.empty()) {
315 return Status(error::INVALID_ARGUMENT,
316 "Inconsistent stream descriptor specification: "
317 "segment_template should be specified for none or all "
318 "stream descriptors.");
321 RETURN_IF_ERROR(ValidateStreamDescriptor(
322 packaging_params.test_params.dump_stream_info, descriptor));
324 if (base::StartsWith(descriptor.input,
"udp://",
325 base::CompareCase::SENSITIVE)) {
326 const HlsParams& hls_params = packaging_params.hls_params;
327 if (!hls_params.master_playlist_output.empty() &&
328 hls_params.playlist_type == HlsPlaylistType::kVod) {
330 <<
"Seeing UDP input with HLS Playlist Type set to VOD. The "
331 "playlists will only be generated when UDP socket is closed. "
332 "If you want to do live packaging, --hls_playlist_type needs to "
339 if (!descriptor.output.empty()) {
340 if (outputs.find(descriptor.output) != outputs.end()) {
342 error::INVALID_ARGUMENT,
343 "Seeing duplicated outputs '" + descriptor.output +
344 "' in stream descriptors. Every output must be unique.");
346 outputs.insert(descriptor.output);
348 if (!descriptor.segment_template.empty()) {
349 if (segment_templates.find(descriptor.segment_template) !=
350 segment_templates.end()) {
351 return Status(error::INVALID_ARGUMENT,
352 "Seeing duplicated segment templates '" +
353 descriptor.segment_template +
354 "' in stream descriptors. Every segment template "
357 segment_templates.insert(descriptor.segment_template);
361 if (packaging_params.output_media_info && !on_demand_dash_profile) {
363 return Status(error::UNIMPLEMENTED,
364 "--output_media_info is only supported for on-demand profile "
365 "(not using segment_template).");
368 if (on_demand_dash_profile &&
369 !packaging_params.mpd_params.mpd_output.empty() &&
370 !packaging_params.mp4_output_params.generate_sidx_in_media_segments &&
371 !packaging_params.mpd_params.use_segment_list) {
372 return Status(error::UNIMPLEMENTED,
373 "--generate_sidx_in_media_segments is required for DASH "
374 "on-demand profile (not using segment_template or segment list).");
380 bool StreamDescriptorCompareFn(
const StreamDescriptor& a,
381 const StreamDescriptor& b) {
387 if (a.input == b.input) {
388 if (a.stream_selector == b.stream_selector) {
391 return a.trick_play_factor < b.trick_play_factor;
393 return a.stream_selector < b.stream_selector;
396 return a.input < b.input;
401 class FakeClock :
public base::Clock {
403 base::Time Now()
override {
return base::Time(); }
406 bool StreamInfoToTextMediaInfo(
const StreamDescriptor& stream_descriptor,
407 MediaInfo* text_media_info) {
409 if (!DetermineTextFileCodec(stream_descriptor.input, &codec)) {
410 LOG(ERROR) <<
"Failed to determine the text file format for "
411 << stream_descriptor.input;
415 MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
416 text_info->set_codec(codec);
418 const std::string& language = stream_descriptor.language;
419 if (!language.empty()) {
420 text_info->set_language(language);
423 text_media_info->set_media_file_name(stream_descriptor.output);
424 text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
426 if (stream_descriptor.bandwidth != 0) {
427 text_media_info->set_bandwidth(stream_descriptor.bandwidth);
432 const int kDefaultTextBandwidth = 256;
433 text_media_info->set_bandwidth(kDefaultTextBandwidth);
436 if (!stream_descriptor.dash_roles.empty()) {
437 for (
const auto& dash_role : stream_descriptor.dash_roles) {
438 text_media_info->add_dash_roles(dash_role);
448 Status CreateDemuxer(
const StreamDescriptor& stream,
449 const PackagingParams& packaging_params,
450 std::shared_ptr<Demuxer>* new_demuxer) {
451 std::shared_ptr<Demuxer> demuxer = std::make_shared<Demuxer>(stream.input);
452 demuxer->set_dump_stream_info(packaging_params.test_params.dump_stream_info);
454 if (packaging_params.decryption_params.key_provider != KeyProvider::kNone) {
455 std::unique_ptr<KeySource> decryption_key_source(
456 CreateDecryptionKeySource(packaging_params.decryption_params));
457 if (!decryption_key_source) {
459 error::INVALID_ARGUMENT,
460 "Must define decryption key source when defining key provider");
462 demuxer->
SetKeySource(std::move(decryption_key_source));
465 *new_demuxer = std::move(demuxer);
469 std::shared_ptr<MediaHandler> CreateEncryptionHandler(
470 const PackagingParams& packaging_params,
471 const StreamDescriptor& stream,
472 KeySource* key_source) {
473 if (stream.skip_encryption) {
482 EncryptionParams encryption_params = packaging_params.encryption_params;
487 if (GetOutputFormat(stream) == CONTAINER_MPEG2TS ||
488 GetOutputFormat(stream) == CONTAINER_AAC ||
489 GetOutputFormat(stream) == CONTAINER_AC3 ||
490 GetOutputFormat(stream) == CONTAINER_EAC3) {
491 VLOG(1) <<
"Use Apple Sample AES encryption for MPEG2TS or Packed Audio.";
492 encryption_params.protection_scheme = kAppleSampleAesProtectionScheme;
495 if (!stream.drm_label.empty()) {
496 const std::string& drm_label = stream.drm_label;
497 encryption_params.stream_label_func =
498 [drm_label](
const EncryptionParams::EncryptedStreamAttributes&) {
501 }
else if (!encryption_params.stream_label_func) {
502 const int kDefaultMaxSdPixels = 768 * 576;
503 const int kDefaultMaxHdPixels = 1920 * 1080;
504 const int kDefaultMaxUhd1Pixels = 4096 * 2160;
505 encryption_params.stream_label_func = std::bind(
507 kDefaultMaxHdPixels, kDefaultMaxUhd1Pixels, std::placeholders::_1);
510 return std::make_shared<EncryptionHandler>(encryption_params, key_source);
513 std::unique_ptr<MediaHandler> CreateTextChunker(
514 const ChunkingParams& chunking_params) {
515 const float segment_length_in_seconds =
516 chunking_params.segment_duration_in_seconds;
517 return std::unique_ptr<MediaHandler>(
518 new TextChunker(segment_length_in_seconds));
521 Status CreateTtmlJobs(
522 const std::vector<std::reference_wrapper<const StreamDescriptor>>& streams,
523 const PackagingParams& packaging_params,
524 SyncPointQueue* sync_points,
525 MuxerFactory* muxer_factory,
526 MpdNotifier* mpd_notifier,
527 JobManager* job_manager) {
529 for (
const StreamDescriptor& stream : streams) {
531 if (!packaging_params.hls_params.master_playlist_output.empty() &&
533 return Status(error::INVALID_ARGUMENT,
534 "HLS does not support TTML in xml format.");
537 if (!stream.segment_template.empty()) {
538 return Status(error::INVALID_ARGUMENT,
539 "Segmented TTML is not supported.");
542 if (GetOutputFormat(stream) != CONTAINER_TTML) {
543 return Status(error::INVALID_ARGUMENT,
544 "Converting TTML to other formats is not supported");
547 if (!stream.output.empty()) {
548 if (!
File::Copy(stream.input.c_str(), stream.output.c_str())) {
551 &error,
"Failed to copy the input file (%s) to output file (%s).",
552 stream.input.c_str(), stream.output.c_str());
553 return Status(error::FILE_FAILURE, error);
556 MediaInfo text_media_info;
557 if (!StreamInfoToTextMediaInfo(stream, &text_media_info)) {
558 return Status(error::INVALID_ARGUMENT,
559 "Could not create media info for stream.");
566 if (mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
567 mpd_notifier->Flush();
569 return Status(error::PARSER_FAILURE,
570 "Failed to process text file " + stream.input);
574 if (packaging_params.output_media_info) {
576 text_media_info, stream.output + kMediaInfoSuffix);
584 Status CreateAudioVideoJobs(
585 const std::vector<std::reference_wrapper<const StreamDescriptor>>& streams,
586 const PackagingParams& packaging_params,
587 KeySource* encryption_key_source,
588 SyncPointQueue* sync_points,
589 MuxerListenerFactory* muxer_listener_factory,
590 MuxerFactory* muxer_factory,
591 JobManager* job_manager) {
592 DCHECK(muxer_listener_factory);
593 DCHECK(muxer_factory);
598 std::map<std::string, std::shared_ptr<Demuxer>> sources;
599 std::map<std::string, std::shared_ptr<MediaHandler>> cue_aligners;
601 for (
const StreamDescriptor& stream : streams) {
602 bool seen_input_before = sources.find(stream.input) != sources.end();
603 if (seen_input_before) {
608 CreateDemuxer(stream, packaging_params, &sources[stream.input]));
609 cue_aligners[stream.input] =
610 sync_points ? std::make_shared<CueAlignmentHandler>(sync_points)
614 for (
auto& source : sources) {
615 job_manager->Add(
"RemuxJob", source.second);
620 std::shared_ptr<MediaHandler> replicator;
622 std::string previous_input;
623 std::string previous_selector;
625 for (
const StreamDescriptor& stream : streams) {
627 auto& demuxer = sources[stream.input];
628 auto& cue_aligner = cue_aligners[stream.input];
630 const bool new_input_file = stream.input != previous_input;
631 const bool new_stream =
632 new_input_file || previous_selector != stream.stream_selector;
633 const bool is_text = IsTextStream(stream);
634 previous_input = stream.input;
635 previous_selector = stream.stream_selector;
639 if (stream.output.empty() && stream.segment_template.empty()) {
647 if (!stream.language.empty()) {
651 std::vector<std::shared_ptr<MediaHandler>> handlers;
653 handlers.emplace_back(
654 std::make_shared<TextPadder>(kDefaultTextZeroBiasMs));
657 handlers.emplace_back(cue_aligner);
660 handlers.emplace_back(std::make_shared<ChunkingHandler>(
661 packaging_params.chunking_params));
662 handlers.emplace_back(CreateEncryptionHandler(packaging_params, stream,
663 encryption_key_source));
666 replicator = std::make_shared<Replicator>();
667 handlers.emplace_back(replicator);
669 RETURN_IF_ERROR(MediaHandler::Chain(handlers));
670 RETURN_IF_ERROR(demuxer->
SetHandler(stream.stream_selector, handlers[0]));
674 const auto output_format = GetOutputFormat(stream);
675 std::shared_ptr<Muxer> muxer =
676 muxer_factory->CreateMuxer(output_format, stream);
678 return Status(error::INVALID_ARGUMENT,
"Failed to create muxer for " +
680 stream.stream_selector);
683 std::unique_ptr<MuxerListener> muxer_listener =
684 muxer_listener_factory->CreateListener(ToMuxerListenerData(stream));
687 std::vector<std::shared_ptr<MediaHandler>> handlers;
688 handlers.emplace_back(replicator);
691 if (stream.trick_play_factor) {
692 handlers.emplace_back(
693 std::make_shared<TrickPlayHandler>(stream.trick_play_factor));
696 if (stream.cc_index >= 0) {
697 handlers.emplace_back(
698 std::make_shared<CcStreamFilter>(stream.language, stream.cc_index));
702 (!stream.segment_template.empty() || output_format == CONTAINER_MOV)) {
703 handlers.emplace_back(
704 CreateTextChunker(packaging_params.chunking_params));
707 if (is_text && output_format == CONTAINER_MOV) {
708 const auto output_codec = GetTextOutputCodec(stream);
709 if (output_codec == CONTAINER_WEBVTT) {
710 handlers.emplace_back(std::make_shared<WebVttToMp4Handler>());
711 }
else if (output_codec == CONTAINER_TTML) {
712 handlers.emplace_back(std::make_shared<ttml::TtmlToMp4Handler>());
716 handlers.emplace_back(muxer);
717 RETURN_IF_ERROR(MediaHandler::Chain(handlers));
723 Status CreateAllJobs(
const std::vector<StreamDescriptor>& stream_descriptors,
724 const PackagingParams& packaging_params,
725 MpdNotifier* mpd_notifier,
726 KeySource* encryption_key_source,
727 SyncPointQueue* sync_points,
728 MuxerListenerFactory* muxer_listener_factory,
729 MuxerFactory* muxer_factory,
730 JobManager* job_manager) {
731 DCHECK(muxer_factory);
732 DCHECK(muxer_listener_factory);
736 std::vector<std::reference_wrapper<const StreamDescriptor>> ttml_streams;
737 std::vector<std::reference_wrapper<const StreamDescriptor>>
740 bool has_transport_audio_video_streams =
false;
741 bool has_non_transport_audio_video_streams =
false;
743 for (
const StreamDescriptor& stream : stream_descriptors) {
744 const auto input_container = DetermineContainerFromFileName(stream.input);
745 const auto output_format = GetOutputFormat(stream);
746 if (input_container == CONTAINER_TTML) {
747 ttml_streams.push_back(stream);
749 audio_video_streams.push_back(stream);
750 switch (output_format) {
751 case CONTAINER_MPEG2TS:
756 has_transport_audio_video_streams =
true;
759 case CONTAINER_WEBVTT:
762 has_non_transport_audio_video_streams =
true;
770 std::sort(audio_video_streams.begin(), audio_video_streams.end(),
771 media::StreamDescriptorCompareFn);
773 if (packaging_params.transport_stream_timestamp_offset_ms > 0) {
774 if (has_transport_audio_video_streams &&
775 has_non_transport_audio_video_streams) {
776 LOG(WARNING) <<
"There may be problems mixing transport streams and "
777 "non-transport streams. For example, the subtitles may "
778 "be out of sync with non-transport streams.";
779 }
else if (has_non_transport_audio_video_streams) {
782 muxer_factory->SetTsStreamOffset(0);
786 RETURN_IF_ERROR(CreateTtmlJobs(ttml_streams, packaging_params, sync_points,
787 muxer_factory, mpd_notifier, job_manager));
788 RETURN_IF_ERROR(CreateAudioVideoJobs(
789 audio_video_streams, packaging_params, encryption_key_source, sync_points,
790 muxer_listener_factory, muxer_factory, job_manager));
793 return job_manager->InitializeJobs();
799 struct Packager::PackagerInternal {
800 media::FakeClock fake_clock;
801 std::unique_ptr<KeySource> encryption_key_source;
802 std::unique_ptr<MpdNotifier> mpd_notifier;
803 std::unique_ptr<hls::HlsNotifier> hls_notifier;
804 BufferCallbackParams buffer_callback_params;
805 std::unique_ptr<media::JobManager> job_manager;
808 Packager::Packager() {}
810 Packager::~Packager() {}
814 const std::vector<StreamDescriptor>& stream_descriptors) {
816 static base::AtExitManager exit;
820 return Status(error::INVALID_ARGUMENT,
"Already initialized.");
822 RETURN_IF_ERROR(media::ValidateParams(packaging_params, stream_descriptors));
825 SetPackagerVersionForTesting(
829 std::unique_ptr<PackagerInternal>
internal(
new PackagerInternal);
833 internal->encryption_key_source = CreateEncryptionKeySource(
834 static_cast<media::FourCC
>(
837 if (!internal->encryption_key_source)
838 return Status(error::INVALID_ARGUMENT,
"Failed to create key source.");
847 const double target_segment_duration =
854 if (internal->buffer_callback_params.write_func) {
856 internal->buffer_callback_params, mpd_params.
mpd_output);
872 hls_params.is_independent_segments =
876 const bool on_demand_dash_profile =
877 stream_descriptors.begin()->segment_template.empty();
879 media::GetMpdOptions(on_demand_dash_profile, mpd_params);
881 if (!internal->mpd_notifier->Init()) {
882 LOG(ERROR) <<
"MpdNotifier failed to initialize.";
883 return Status(error::INVALID_ARGUMENT,
884 "Failed to initialize MpdNotifier.");
892 std::unique_ptr<SyncPointQueue> sync_points;
898 internal->job_manager.reset(
901 internal->job_manager.reset(
new JobManager(std::move(sync_points)));
904 std::vector<StreamDescriptor> streams_for_jobs;
910 if (internal->buffer_callback_params.read_func) {
915 if (internal->buffer_callback_params.write_func) {
919 internal->buffer_callback_params, descriptor.segment_template);
927 error::INVALID_ARGUMENT,
928 "Unknown/invalid language specified: " + descriptor.language);
932 streams_for_jobs.push_back(copy);
943 internal->mpd_notifier.get(), internal->hls_notifier.get());
945 RETURN_IF_ERROR(media::CreateAllJobs(
946 streams_for_jobs, packaging_params, internal->mpd_notifier.get(),
947 internal->encryption_key_source.get(),
948 internal->job_manager->sync_points(), &muxer_listener_factory,
949 &muxer_factory, internal->job_manager.get()));
951 internal_ = std::move(
internal);
957 return Status(error::INVALID_ARGUMENT,
"Not yet initialized.");
959 RETURN_IF_ERROR(internal_->job_manager->RunJobs());
961 if (internal_->hls_notifier) {
962 if (!internal_->hls_notifier->Flush())
963 return Status(error::INVALID_ARGUMENT,
"Failed to flush Hls.");
965 if (internal_->mpd_notifier) {
966 if (!internal_->mpd_notifier->Flush())
967 return Status(error::INVALID_ARGUMENT,
"Failed to flush Mpd.");
974 LOG(INFO) <<
"Not yet initialized. Return directly.";
977 internal_->job_manager->CancelJobs();
981 return GetPackagerVersion();
989 if (stream_attributes.stream_type ==
990 EncryptionParams::EncryptedStreamAttributes::kAudio)
992 if (stream_attributes.stream_type ==
993 EncryptionParams::EncryptedStreamAttributes::kVideo) {
994 const int pixels = stream_attributes.oneof.video.width *
995 stream_attributes.oneof.video.height;
996 if (pixels <= max_sd_pixels)
998 if (pixels <= max_hd_pixels)
1000 if (pixels <= max_uhd1_pixels)