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/stream_descriptor.h" 16 #include "packager/base/at_exit.h" 17 #include "packager/base/files/file_path.h" 18 #include "packager/base/logging.h" 19 #include "packager/base/optional.h" 20 #include "packager/base/path_service.h" 21 #include "packager/base/strings/string_util.h" 22 #include "packager/base/strings/stringprintf.h" 23 #include "packager/base/threading/simple_thread.h" 24 #include "packager/base/time/clock.h" 25 #include "packager/file/file.h" 26 #include "packager/hls/base/hls_notifier.h" 27 #include "packager/hls/base/simple_hls_notifier.h" 28 #include "packager/media/base/container_names.h" 29 #include "packager/media/base/fourccs.h" 30 #include "packager/media/base/key_source.h" 31 #include "packager/media/base/language_utils.h" 32 #include "packager/media/base/muxer.h" 33 #include "packager/media/base/muxer_options.h" 34 #include "packager/media/base/muxer_util.h" 35 #include "packager/media/chunking/chunking_handler.h" 36 #include "packager/media/chunking/cue_alignment_handler.h" 37 #include "packager/media/chunking/text_chunker.h" 38 #include "packager/media/crypto/encryption_handler.h" 39 #include "packager/media/demuxer/demuxer.h" 40 #include "packager/media/event/muxer_listener_factory.h" 41 #include "packager/media/event/vod_media_info_dump_muxer_listener.h" 42 #include "packager/media/formats/webvtt/text_padder.h" 43 #include "packager/media/formats/webvtt/text_readers.h" 44 #include "packager/media/formats/webvtt/webvtt_parser.h" 45 #include "packager/media/formats/webvtt/webvtt_text_output_handler.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::SyncPointQueue;
67 const char kMediaInfoSuffix[] =
".media_info";
69 const int64_t kDefaultTextZeroBiasMs = 10 * 60 * 1000;
71 MuxerOptions CreateMuxerOptions(
const StreamDescriptor& stream,
72 const PackagingParams& params) {
75 options.mp4_params = params.mp4_output_params;
76 options.transport_stream_timestamp_offset_ms =
77 params.transport_stream_timestamp_offset_ms;
78 options.temp_dir = params.temp_dir;
79 options.bandwidth = stream.bandwidth;
80 options.output_file_name = stream.output;
81 options.segment_template = stream.segment_template;
86 MuxerListenerFactory::StreamData ToMuxerListenerData(
87 const StreamDescriptor& stream) {
88 MuxerListenerFactory::StreamData data;
89 data.media_info_output = stream.output;
90 data.hls_group_id = stream.hls_group_id;
91 data.hls_name = stream.hls_name;
92 data.hls_playlist_name = stream.hls_playlist_name;
93 data.hls_iframe_playlist_name = stream.hls_iframe_playlist_name;
94 data.hls_characteristics = stream.hls_characteristics;
101 bool DetermineTextFileCodec(
const std::string& file, std::string* out) {
106 LOG(ERROR) <<
"Failed to open file " << file
107 <<
" to determine file format.";
111 const uint8_t* content_data =
112 reinterpret_cast<const uint8_t*
>(content.data());
113 MediaContainerName container_name =
114 DetermineContainer(content_data, content.size());
116 if (container_name == CONTAINER_WEBVTT) {
121 if (container_name == CONTAINER_TTML) {
129 MediaContainerName GetOutputFormat(
const StreamDescriptor& descriptor) {
130 if (!descriptor.output_format.empty()) {
131 MediaContainerName format =
132 DetermineContainerFromFormatName(descriptor.output_format);
133 if (format == CONTAINER_UNKNOWN) {
134 LOG(ERROR) <<
"Unable to determine output format from '" 135 << descriptor.output_format <<
"'.";
140 base::Optional<MediaContainerName> format_from_output;
141 base::Optional<MediaContainerName> format_from_segment;
142 if (!descriptor.output.empty()) {
143 format_from_output = DetermineContainerFromFileName(descriptor.output);
144 if (format_from_output.value() == CONTAINER_UNKNOWN) {
145 LOG(ERROR) <<
"Unable to determine output format from '" 146 << descriptor.output <<
"'.";
149 if (!descriptor.segment_template.empty()) {
150 format_from_segment =
151 DetermineContainerFromFileName(descriptor.segment_template);
152 if (format_from_segment.value() == CONTAINER_UNKNOWN) {
153 LOG(ERROR) <<
"Unable to determine output format from '" 154 << descriptor.segment_template <<
"'.";
158 if (format_from_output && format_from_segment) {
159 if (format_from_output.value() != format_from_segment.value()) {
160 LOG(ERROR) <<
"Output format determined from '" << descriptor.output
161 <<
"' differs from output format determined from '" 162 << descriptor.segment_template <<
"'.";
163 return CONTAINER_UNKNOWN;
167 if (format_from_output)
168 return format_from_output.value();
169 if (format_from_segment)
170 return format_from_segment.value();
171 return CONTAINER_UNKNOWN;
174 Status ValidateStreamDescriptor(
bool dump_stream_info,
175 const StreamDescriptor& stream) {
176 if (stream.input.empty()) {
177 return Status(error::INVALID_ARGUMENT,
"Stream input not specified.");
182 if (dump_stream_info && stream.output.empty() &&
183 stream.segment_template.empty()) {
187 if (stream.output.empty() && stream.segment_template.empty()) {
188 return Status(error::INVALID_ARGUMENT,
189 "Streams must specify 'output' or 'segment template'.");
193 if (stream.stream_selector.empty()) {
194 return Status(error::INVALID_ARGUMENT,
195 "Stream stream_selector not specified.");
199 if (stream.segment_template.length()) {
200 RETURN_IF_ERROR(ValidateSegmentTemplate(stream.segment_template));
205 const MediaContainerName output_format = GetOutputFormat(stream);
207 if (output_format == CONTAINER_UNKNOWN) {
208 return Status(error::INVALID_ARGUMENT,
"Unsupported output format.");
209 }
else if (output_format == MediaContainerName::CONTAINER_MPEG2TS) {
210 if (stream.segment_template.empty()) {
212 error::INVALID_ARGUMENT,
213 "Please specify 'segment_template'. Single file TS output is " 220 if (stream.output.length()) {
221 return Status(error::INVALID_ARGUMENT,
222 "All TS segments must be self-initializing. Stream " 223 "descriptors 'output' or 'init_segment' are not allowed.");
225 }
else if (output_format == CONTAINER_WEBVTT ||
226 output_format == CONTAINER_AAC || output_format == CONTAINER_AC3 ||
227 output_format == CONTAINER_EAC3) {
230 if (stream.segment_template.length() && stream.output.length()) {
232 error::INVALID_ARGUMENT,
233 "Segmented WebVTT or PackedAudio output cannot have an init segment. " 234 "Do not specify stream descriptors 'output' or 'init_segment' when " 235 "using 'segment_template'.");
240 if (stream.segment_template.length() && stream.output.empty()) {
241 return Status(error::INVALID_ARGUMENT,
242 "Please specify 'init_segment'. All non-TS multi-segment " 243 "content must provide an init segment.");
247 if (stream.output.find(
'$') != std::string::npos) {
248 if (output_format == CONTAINER_WEBVTT) {
250 error::UNIMPLEMENTED,
251 "WebVTT output with one file per Representation per Period " 252 "is not supported yet. Please use fMP4 instead. If that needs to be " 253 "supported, please file a feature request on GitHub.");
258 RETURN_IF_ERROR(ValidateSegmentTemplate(stream.output));
264 Status ValidateParams(
const PackagingParams& packaging_params,
265 const std::vector<StreamDescriptor>& stream_descriptors) {
266 if (!packaging_params.chunking_params.segment_sap_aligned &&
267 packaging_params.chunking_params.subsegment_sap_aligned) {
268 return Status(error::INVALID_ARGUMENT,
269 "Setting segment_sap_aligned to false but " 270 "subsegment_sap_aligned to true is not allowed.");
273 if (stream_descriptors.empty()) {
274 return Status(error::INVALID_ARGUMENT,
275 "Stream descriptors cannot be empty.");
280 const bool on_demand_dash_profile =
281 stream_descriptors.begin()->segment_template.empty();
282 std::set<std::string> outputs;
283 std::set<std::string> segment_templates;
284 for (
const auto& descriptor : stream_descriptors) {
285 if (on_demand_dash_profile != descriptor.segment_template.empty()) {
286 return Status(error::INVALID_ARGUMENT,
287 "Inconsistent stream descriptor specification: " 288 "segment_template should be specified for none or all " 289 "stream descriptors.");
292 RETURN_IF_ERROR(ValidateStreamDescriptor(
293 packaging_params.test_params.dump_stream_info, descriptor));
295 if (base::StartsWith(descriptor.input,
"udp://",
296 base::CompareCase::SENSITIVE)) {
297 const HlsParams& hls_params = packaging_params.hls_params;
298 if (!hls_params.master_playlist_output.empty() &&
299 hls_params.playlist_type == HlsPlaylistType::kVod) {
301 <<
"Seeing UDP input with HLS Playlist Type set to VOD. The " 302 "playlists will only be generated when UDP socket is closed. " 303 "If you want to do live packaging, --hls_playlist_type needs to " 310 if (!descriptor.output.empty()) {
311 if (outputs.find(descriptor.output) != outputs.end()) {
313 error::INVALID_ARGUMENT,
314 "Seeing duplicated outputs '" + descriptor.output +
315 "' in stream descriptors. Every output must be unique.");
317 outputs.insert(descriptor.output);
319 if (!descriptor.segment_template.empty()) {
320 if (segment_templates.find(descriptor.segment_template) !=
321 segment_templates.end()) {
322 return Status(error::INVALID_ARGUMENT,
323 "Seeing duplicated segment templates '" +
324 descriptor.segment_template +
325 "' in stream descriptors. Every segment template " 328 segment_templates.insert(descriptor.segment_template);
332 if (packaging_params.output_media_info && !on_demand_dash_profile) {
334 return Status(error::UNIMPLEMENTED,
335 "--output_media_info is only supported for on-demand profile " 336 "(not using segment_template).");
342 bool StreamDescriptorCompareFn(
const StreamDescriptor& a,
343 const StreamDescriptor& b) {
349 if (a.input == b.input) {
350 if (a.stream_selector == b.stream_selector) {
353 return a.trick_play_factor < b.trick_play_factor;
355 return a.stream_selector < b.stream_selector;
359 return a.input < b.input;
364 class FakeClock :
public base::Clock {
366 base::Time Now()
override {
return base::Time(); }
369 bool StreamInfoToTextMediaInfo(
const StreamDescriptor& stream_descriptor,
370 MediaInfo* text_media_info) {
372 if (!DetermineTextFileCodec(stream_descriptor.input, &codec)) {
373 LOG(ERROR) <<
"Failed to determine the text file format for " 374 << stream_descriptor.input;
378 MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
379 text_info->set_codec(codec);
381 const std::string& language = stream_descriptor.language;
382 if (!language.empty()) {
383 text_info->set_language(language);
386 text_media_info->set_media_file_name(stream_descriptor.output);
387 text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
389 if (stream_descriptor.bandwidth != 0) {
390 text_media_info->set_bandwidth(stream_descriptor.bandwidth);
395 const int kDefaultTextBandwidth = 256;
396 text_media_info->set_bandwidth(kDefaultTextBandwidth);
405 Status CreateDemuxer(
const StreamDescriptor& stream,
406 const PackagingParams& packaging_params,
407 std::shared_ptr<Demuxer>* new_demuxer) {
408 std::shared_ptr<Demuxer> demuxer = std::make_shared<Demuxer>(stream.input);
409 demuxer->set_dump_stream_info(packaging_params.test_params.dump_stream_info);
411 if (packaging_params.decryption_params.key_provider != KeyProvider::kNone) {
412 std::unique_ptr<KeySource> decryption_key_source(
413 CreateDecryptionKeySource(packaging_params.decryption_params));
414 if (!decryption_key_source) {
416 error::INVALID_ARGUMENT,
417 "Must define decryption key source when defining key provider");
419 demuxer->SetKeySource(std::move(decryption_key_source));
422 *new_demuxer = std::move(demuxer);
426 std::shared_ptr<MediaHandler> CreateEncryptionHandler(
427 const PackagingParams& packaging_params,
428 const StreamDescriptor& stream,
429 KeySource* key_source) {
430 if (stream.skip_encryption) {
439 EncryptionParams encryption_params = packaging_params.encryption_params;
444 if (GetOutputFormat(stream) == CONTAINER_MPEG2TS ||
445 GetOutputFormat(stream) == CONTAINER_AAC ||
446 GetOutputFormat(stream) == CONTAINER_AC3 ||
447 GetOutputFormat(stream) == CONTAINER_EAC3) {
448 VLOG(1) <<
"Use Apple Sample AES encryption for MPEG2TS or Packed Audio.";
449 encryption_params.protection_scheme = kAppleSampleAesProtectionScheme;
452 if (!stream.drm_label.empty()) {
453 const std::string& drm_label = stream.drm_label;
454 encryption_params.stream_label_func =
455 [drm_label](
const EncryptionParams::EncryptedStreamAttributes&) {
458 }
else if (!encryption_params.stream_label_func) {
459 const int kDefaultMaxSdPixels = 768 * 576;
460 const int kDefaultMaxHdPixels = 1920 * 1080;
461 const int kDefaultMaxUhd1Pixels = 4096 * 2160;
462 encryption_params.stream_label_func = std::bind(
464 kDefaultMaxHdPixels, kDefaultMaxUhd1Pixels, std::placeholders::_1);
467 return std::make_shared<EncryptionHandler>(encryption_params, key_source);
470 std::unique_ptr<TextChunker> CreateTextChunker(
471 const ChunkingParams& chunking_params) {
472 const float segment_length_in_seconds =
473 chunking_params.segment_duration_in_seconds;
474 return std::unique_ptr<TextChunker>(
475 new TextChunker(segment_length_in_seconds));
478 Status CreateHlsTextJob(
const StreamDescriptor& stream,
479 const PackagingParams& packaging_params,
480 std::unique_ptr<MuxerListener> muxer_listener,
481 SyncPointQueue* sync_points,
482 JobManager* job_manager) {
483 DCHECK(muxer_listener);
486 if (stream.segment_template.empty()) {
487 return Status(error::INVALID_ARGUMENT,
488 "Cannot output text (" + stream.input +
489 ") to HLS with no segment template");
495 MuxerOptions muxer_options = CreateMuxerOptions(stream, packaging_params);
496 muxer_options.bandwidth = stream.bandwidth ? stream.bandwidth : 256;
498 auto output = std::make_shared<WebVttTextOutputHandler>(
499 muxer_options, std::move(muxer_listener));
501 std::unique_ptr<FileReader> reader;
505 std::make_shared<WebVttParser>(std::move(reader), stream.language);
506 auto padder = std::make_shared<TextPadder>(kDefaultTextZeroBiasMs);
507 auto cue_aligner = sync_points
508 ? std::make_shared<CueAlignmentHandler>(sync_points)
510 auto chunker = CreateTextChunker(packaging_params.chunking_params);
512 job_manager->Add(
"Segmented Text Job", parser);
514 return MediaHandler::Chain({std::move(parser), std::move(padder),
515 std::move(cue_aligner), std::move(chunker),
519 Status CreateWebVttToMp4TextJob(
const StreamDescriptor& stream,
520 const PackagingParams& packaging_params,
521 std::unique_ptr<MuxerListener> muxer_listener,
522 SyncPointQueue* sync_points,
523 MuxerFactory* muxer_factory,
524 std::shared_ptr<OriginHandler>* root) {
525 std::unique_ptr<FileReader> reader;
529 std::make_shared<WebVttParser>(std::move(reader), stream.language);
530 auto padder = std::make_shared<TextPadder>(kDefaultTextZeroBiasMs);
532 auto text_to_mp4 = std::make_shared<WebVttToMp4Handler>();
533 auto muxer = muxer_factory->CreateMuxer(GetOutputFormat(stream), stream);
534 muxer->SetMuxerListener(std::move(muxer_listener));
537 std::shared_ptr<MediaHandler> cue_aligner;
539 cue_aligner = std::make_shared<CueAlignmentHandler>(sync_points);
542 std::shared_ptr<MediaHandler> chunker =
543 CreateTextChunker(packaging_params.chunking_params);
547 return MediaHandler::Chain({std::move(parser), std::move(padder),
548 std::move(cue_aligner), std::move(chunker),
549 std::move(text_to_mp4), std::move(muxer)});
552 Status CreateTextJobs(
553 const std::vector<std::reference_wrapper<const StreamDescriptor>>& streams,
554 const PackagingParams& packaging_params,
555 SyncPointQueue* sync_points,
556 MuxerListenerFactory* muxer_listener_factory,
557 MuxerFactory* muxer_factory,
558 MpdNotifier* mpd_notifier,
559 JobManager* job_manager) {
560 DCHECK(muxer_listener_factory);
562 for (
const StreamDescriptor& stream : streams) {
569 const auto input_container = DetermineContainerFromFileName(stream.input);
570 const auto output_container = GetOutputFormat(stream);
572 if (input_container != CONTAINER_WEBVTT &&
573 input_container != CONTAINER_TTML) {
574 return Status(error::INVALID_ARGUMENT,
575 "Text output format is not support for " + stream.input);
578 if (output_container == CONTAINER_MOV) {
579 if (input_container == CONTAINER_TTML) {
580 return Status(error::INVALID_ARGUMENT,
581 "TTML in MP4 is not supported yet. Please follow " 582 "https://github.com/google/shaka-packager/issues/87 for " 586 std::unique_ptr<MuxerListener> muxer_listener =
587 muxer_listener_factory->CreateListener(ToMuxerListenerData(stream));
589 std::shared_ptr<OriginHandler> root;
590 RETURN_IF_ERROR(CreateWebVttToMp4TextJob(
591 stream, packaging_params, std::move(muxer_listener), sync_points,
592 muxer_factory, &root));
594 job_manager->Add(
"MP4 text job", std::move(root));
596 std::unique_ptr<MuxerListener> hls_listener =
597 muxer_listener_factory->CreateHlsListener(
598 ToMuxerListenerData(stream));
602 if (input_container == CONTAINER_TTML) {
603 return Status(error::INVALID_ARGUMENT,
604 "HLS does not support TTML in xml format.");
606 if (stream.segment_template.empty() || !stream.output.empty()) {
607 return Status(error::INVALID_ARGUMENT,
608 "segment_template needs to be specified for HLS text " 609 "output. Single file output is not supported yet.");
613 if (mpd_notifier && !stream.segment_template.empty()) {
614 return Status(error::INVALID_ARGUMENT,
615 "Cannot create text output for MPD with segment output.");
621 RETURN_IF_ERROR(CreateHlsTextJob(stream, packaging_params,
622 std::move(hls_listener), sync_points,
626 if (!stream.output.empty()) {
627 if (!
File::Copy(stream.input.c_str(), stream.output.c_str())) {
630 &error,
"Failed to copy the input file (%s) to output file (%s).",
631 stream.input.c_str(), stream.output.c_str());
632 return Status(error::FILE_FAILURE, error);
635 MediaInfo text_media_info;
636 if (!StreamInfoToTextMediaInfo(stream, &text_media_info)) {
637 return Status(error::INVALID_ARGUMENT,
638 "Could not create media info for stream.");
645 if (mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
646 mpd_notifier->Flush();
648 return Status(error::PARSER_FAILURE,
649 "Failed to process text file " + stream.input);
653 if (packaging_params.output_media_info) {
655 text_media_info, stream.output + kMediaInfoSuffix);
664 Status CreateAudioVideoJobs(
665 const std::vector<std::reference_wrapper<const StreamDescriptor>>& streams,
666 const PackagingParams& packaging_params,
667 KeySource* encryption_key_source,
668 SyncPointQueue* sync_points,
669 MuxerListenerFactory* muxer_listener_factory,
670 MuxerFactory* muxer_factory,
671 JobManager* job_manager) {
672 DCHECK(muxer_listener_factory);
673 DCHECK(muxer_factory);
678 std::map<std::string, std::shared_ptr<Demuxer>> sources;
679 std::map<std::string, std::shared_ptr<MediaHandler>> cue_aligners;
681 for (
const StreamDescriptor& stream : streams) {
682 bool seen_input_before = sources.find(stream.input) != sources.end();
683 if (seen_input_before) {
688 CreateDemuxer(stream, packaging_params, &sources[stream.input]));
689 cue_aligners[stream.input] =
690 sync_points ? std::make_shared<CueAlignmentHandler>(sync_points)
694 for (
auto& source : sources) {
695 job_manager->Add(
"RemuxJob", source.second);
700 std::shared_ptr<MediaHandler> replicator;
702 std::string previous_input;
703 std::string previous_selector;
705 for (
const StreamDescriptor& stream : streams) {
707 auto& demuxer = sources[stream.input];
708 auto& cue_aligner = cue_aligners[stream.input];
710 const bool new_input_file = stream.input != previous_input;
711 const bool new_stream =
712 new_input_file || previous_selector != stream.stream_selector;
713 previous_input = stream.input;
714 previous_selector = stream.stream_selector;
718 if (stream.output.empty() && stream.segment_template.empty()) {
726 if (!stream.language.empty()) {
727 demuxer->SetLanguageOverride(stream.stream_selector, stream.language);
730 replicator = std::make_shared<Replicator>();
732 std::make_shared<ChunkingHandler>(packaging_params.chunking_params);
733 auto encryptor = CreateEncryptionHandler(packaging_params, stream,
734 encryption_key_source);
739 MediaHandler::Chain({cue_aligner, chunker, encryptor, replicator}));
741 demuxer->SetHandler(stream.stream_selector, cue_aligner));
743 RETURN_IF_ERROR(MediaHandler::Chain({chunker, encryptor, replicator}));
744 RETURN_IF_ERROR(demuxer->SetHandler(stream.stream_selector, chunker));
749 std::shared_ptr<Muxer> muxer =
750 muxer_factory->CreateMuxer(GetOutputFormat(stream), stream);
752 return Status(error::INVALID_ARGUMENT,
"Failed to create muxer for " +
754 stream.stream_selector);
757 std::unique_ptr<MuxerListener> muxer_listener =
758 muxer_listener_factory->CreateListener(ToMuxerListenerData(stream));
759 muxer->SetMuxerListener(std::move(muxer_listener));
762 std::shared_ptr<MediaHandler> trick_play =
763 stream.trick_play_factor
764 ? std::make_shared<TrickPlayHandler>(stream.trick_play_factor)
767 RETURN_IF_ERROR(MediaHandler::Chain({replicator, trick_play, muxer}));
773 Status CreateAllJobs(
const std::vector<StreamDescriptor>& stream_descriptors,
774 const PackagingParams& packaging_params,
775 MpdNotifier* mpd_notifier,
776 KeySource* encryption_key_source,
777 SyncPointQueue* sync_points,
778 MuxerListenerFactory* muxer_listener_factory,
779 MuxerFactory* muxer_factory,
780 JobManager* job_manager) {
781 DCHECK(muxer_factory);
782 DCHECK(muxer_listener_factory);
786 std::vector<std::reference_wrapper<const StreamDescriptor>> text_streams;
787 std::vector<std::reference_wrapper<const StreamDescriptor>>
790 bool has_transport_audio_video_streams =
false;
791 bool has_non_transport_audio_video_streams =
false;
793 for (
const StreamDescriptor& stream : stream_descriptors) {
797 if (stream.stream_selector ==
"text") {
798 text_streams.push_back(stream);
800 audio_video_streams.push_back(stream);
802 switch (GetOutputFormat(stream)) {
803 case CONTAINER_MPEG2TS:
807 has_transport_audio_video_streams =
true;
810 has_non_transport_audio_video_streams =
true;
818 std::sort(audio_video_streams.begin(), audio_video_streams.end(),
819 media::StreamDescriptorCompareFn);
821 if (!text_streams.empty()) {
822 PackagingParams text_packaging_params = packaging_params;
823 if (text_packaging_params.transport_stream_timestamp_offset_ms > 0) {
824 if (has_transport_audio_video_streams &&
825 has_non_transport_audio_video_streams) {
826 LOG(WARNING) <<
"There may be problems mixing transport streams and " 827 "non-transport streams. For example, the subtitles may " 828 "be out of sync with non-transport streams.";
829 }
else if (has_non_transport_audio_video_streams) {
832 text_packaging_params.transport_stream_timestamp_offset_ms = 0;
836 RETURN_IF_ERROR(CreateTextJobs(text_streams, text_packaging_params,
837 sync_points, muxer_listener_factory,
838 muxer_factory, mpd_notifier, job_manager));
841 RETURN_IF_ERROR(CreateAudioVideoJobs(
842 audio_video_streams, packaging_params, encryption_key_source, sync_points,
843 muxer_listener_factory, muxer_factory, job_manager));
846 return job_manager->InitializeJobs();
852 struct Packager::PackagerInternal {
853 media::FakeClock fake_clock;
854 std::unique_ptr<KeySource> encryption_key_source;
855 std::unique_ptr<MpdNotifier> mpd_notifier;
856 std::unique_ptr<hls::HlsNotifier> hls_notifier;
857 BufferCallbackParams buffer_callback_params;
858 std::unique_ptr<media::JobManager> job_manager;
861 Packager::Packager() {}
863 Packager::~Packager() {}
867 const std::vector<StreamDescriptor>& stream_descriptors) {
869 static base::AtExitManager exit;
873 return Status(error::INVALID_ARGUMENT,
"Already initialized.");
875 RETURN_IF_ERROR(media::ValidateParams(packaging_params, stream_descriptors));
878 SetPackagerVersionForTesting(
882 std::unique_ptr<PackagerInternal>
internal(
new PackagerInternal);
886 internal->encryption_key_source = CreateEncryptionKeySource(
887 static_cast<media::FourCC>(
890 if (!internal->encryption_key_source)
891 return Status(error::INVALID_ARGUMENT,
"Failed to create key source.");
900 const double target_segment_duration =
909 if (internal->buffer_callback_params.write_func) {
911 internal->buffer_callback_params, mpd_params.
mpd_output);
929 const bool on_demand_dash_profile =
930 stream_descriptors.begin()->segment_template.empty();
932 media::GetMpdOptions(on_demand_dash_profile, mpd_params);
934 if (!internal->mpd_notifier->Init()) {
935 LOG(ERROR) <<
"MpdNotifier failed to initialize.";
936 return Status(error::INVALID_ARGUMENT,
937 "Failed to initialize MpdNotifier.");
945 std::unique_ptr<SyncPointQueue> sync_points;
950 internal->job_manager.reset(
new JobManager(std::move(sync_points)));
952 std::vector<StreamDescriptor> streams_for_jobs;
958 if (internal->buffer_callback_params.read_func) {
963 if (internal->buffer_callback_params.write_func) {
967 internal->buffer_callback_params, descriptor.segment_template);
975 error::INVALID_ARGUMENT,
976 "Unknown/invalid language specified: " + descriptor.language);
980 streams_for_jobs.push_back(copy);
990 internal->hls_notifier.get());
992 RETURN_IF_ERROR(media::CreateAllJobs(
993 streams_for_jobs, packaging_params, internal->mpd_notifier.get(),
994 internal->encryption_key_source.get(),
995 internal->job_manager->sync_points(), &muxer_listener_factory,
996 &muxer_factory,
internal->job_manager.get()));
998 internal_ = std::move(
internal);
1004 return Status(error::INVALID_ARGUMENT,
"Not yet initialized.");
1006 RETURN_IF_ERROR(internal_->job_manager->RunJobs());
1008 if (internal_->hls_notifier) {
1009 if (!internal_->hls_notifier->Flush())
1010 return Status(error::INVALID_ARGUMENT,
"Failed to flush Hls.");
1012 if (internal_->mpd_notifier) {
1013 if (!internal_->mpd_notifier->Flush())
1014 return Status(error::INVALID_ARGUMENT,
"Failed to flush Mpd.");
1021 LOG(INFO) <<
"Not yet initialized. Return directly.";
1024 internal_->job_manager->CancelJobs();
1028 return GetPackagerVersion();
1034 int max_uhd1_pixels,
1036 if (stream_attributes.stream_type ==
1037 EncryptionParams::EncryptedStreamAttributes::kAudio)
1039 if (stream_attributes.stream_type ==
1040 EncryptionParams::EncryptedStreamAttributes::kVideo) {
1041 const int pixels = stream_attributes.oneof.video.width *
1042 stream_attributes.oneof.video.height;
1043 if (pixels <= max_sd_pixels)
1045 if (pixels <= max_hd_pixels)
1047 if (pixels <= max_uhd1_pixels)
BufferCallbackParams buffer_callback_params
Buffer callback params.
std::string master_playlist_output
HLS master playlist output path.
DASH MPD related parameters.
Defines a single input/output stream.
std::string input
Input/source media file path or network stream URL. Required.
HlsParams hls_params
HLS related parameters.
Status Initialize(const PackagingParams &packaging_params, const std::vector< StreamDescriptor > &stream_descriptors)
std::string default_language
static std::string DefaultStreamLabelFunction(int max_sd_pixels, int max_hd_pixels, int max_uhd1_pixels, const EncryptionParams::EncryptedStreamAttributes &stream_attributes)
ChunkingParams chunking_params
Chunking (segmentation) related parameters.
std::string default_text_language
std::vector< Cuepoint > cue_points
List of cuepoints.
std::string LanguageToShortestForm(const std::string &language)
std::string segment_template
Specifies segment template. Can be empty.
static bool Copy(const char *from_file_name, const char *to_file_name)
static bool ReadFileToString(const char *file_name, std::string *contents)
All the methods that are virtual are virtual for mocking.
static std::string GetLibraryVersion()
double target_segment_duration
std::string LanguageToISO_639_2(const std::string &language)
std::string injected_library_version
MpdParams mpd_params
DASH MPD related parameters.
AdCueGeneratorParams ad_cue_generator_params
Out of band cuepoint parameters.
EncryptionParams encryption_params
Encryption and Decryption Parameters.
std::string mpd_output
MPD output file path.
static std::string MakeCallbackFileName(const BufferCallbackParams &callback_params, const std::string &name)
double target_segment_duration
Encrypted stream information that is used to determine stream label.
std::string default_language
double segment_duration_in_seconds
Segment duration in seconds.
void Cancel()
Cancel packaging. Note that it has to be called from another thread.
std::string default_text_language