Shaka Packager SDK
packager.cc
1 // Copyright 2017 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/packager.h"
8 
9 #include <algorithm>
10 
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"
54 
55 namespace shaka {
56 
57 // TODO(kqyang): Clean up namespaces.
58 using media::Demuxer;
59 using media::JobManager;
60 using media::KeySource;
61 using media::MuxerOptions;
62 using media::SyncPointQueue;
63 
64 namespace media {
65 namespace {
66 
67 const char kMediaInfoSuffix[] = ".media_info";
68 
69 const int64_t kDefaultTextZeroBiasMs = 10 * 60 * 1000; // 10 minutes
70 
71 MuxerOptions CreateMuxerOptions(const StreamDescriptor& stream,
72  const PackagingParams& params) {
73  MuxerOptions options;
74 
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;
82 
83  return options;
84 }
85 
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;
95  return data;
96 };
97 
98 // TODO(rkuroiwa): Write TTML and WebVTT parser (demuxing) for a better check
99 // and for supporting live/segmenting (muxing). With a demuxer and a muxer,
100 // CreateAllJobs() shouldn't treat text as a special case.
101 bool DetermineTextFileCodec(const std::string& file, std::string* out) {
102  CHECK(out);
103 
104  std::string content;
105  if (!File::ReadFileToString(file.c_str(), &content)) {
106  LOG(ERROR) << "Failed to open file " << file
107  << " to determine file format.";
108  return false;
109  }
110 
111  const uint8_t* content_data =
112  reinterpret_cast<const uint8_t*>(content.data());
113  MediaContainerName container_name =
114  DetermineContainer(content_data, content.size());
115 
116  if (container_name == CONTAINER_WEBVTT) {
117  *out = "wvtt";
118  return true;
119  }
120 
121  if (container_name == CONTAINER_TTML) {
122  *out = "ttml";
123  return true;
124  }
125 
126  return false;
127 }
128 
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 << "'.";
136  }
137  return format;
138  }
139 
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 << "'.";
147  }
148  }
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 << "'.";
155  }
156  }
157 
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;
164  }
165  }
166 
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;
172 }
173 
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.");
178  }
179 
180  // The only time a stream can have no outputs, is when dump stream info is
181  // set.
182  if (dump_stream_info && stream.output.empty() &&
183  stream.segment_template.empty()) {
184  return Status::OK;
185  }
186 
187  if (stream.output.empty() && stream.segment_template.empty()) {
188  return Status(error::INVALID_ARGUMENT,
189  "Streams must specify 'output' or 'segment template'.");
190  }
191 
192  // Whenever there is output, a stream must be selected.
193  if (stream.stream_selector.empty()) {
194  return Status(error::INVALID_ARGUMENT,
195  "Stream stream_selector not specified.");
196  }
197 
198  // If a segment template is provided, it must be valid.
199  if (stream.segment_template.length()) {
200  RETURN_IF_ERROR(ValidateSegmentTemplate(stream.segment_template));
201  }
202 
203  // There are some specifics that must be checked based on which format
204  // we are writing to.
205  const MediaContainerName output_format = GetOutputFormat(stream);
206 
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()) {
211  return Status(
212  error::INVALID_ARGUMENT,
213  "Please specify 'segment_template'. Single file TS output is "
214  "not supported.");
215  }
216 
217  // Right now the init segment is saved in |output| for multi-segment
218  // content. However, for TS all segments must be self-initializing so
219  // there cannot be an init segment.
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.");
224  }
225  } else if (output_format == CONTAINER_WEBVTT ||
226  output_format == CONTAINER_AAC || output_format == CONTAINER_AC3 ||
227  output_format == CONTAINER_EAC3) {
228  // There is no need for an init segment when outputting because there is no
229  // initialization data.
230  if (stream.segment_template.length() && stream.output.length()) {
231  return Status(
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'.");
236  }
237  } else {
238  // For any other format, if there is a segment template, there must be an
239  // init segment provided.
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.");
244  }
245  }
246 
247  if (stream.output.find('$') != std::string::npos) {
248  if (output_format == CONTAINER_WEBVTT) {
249  return Status(
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.");
254  }
255  // "$" is only allowed if the output file name is a template, which is
256  // used to support one file per Representation per Period when there are
257  // Ad Cues.
258  RETURN_IF_ERROR(ValidateSegmentTemplate(stream.output));
259  }
260 
261  return Status::OK;
262 }
263 
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.");
271  }
272 
273  if (stream_descriptors.empty()) {
274  return Status(error::INVALID_ARGUMENT,
275  "Stream descriptors cannot be empty.");
276  }
277 
278  // On demand profile generates single file segment while live profile
279  // generates multiple segments specified using segment template.
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.");
290  }
291 
292  RETURN_IF_ERROR(ValidateStreamDescriptor(
293  packaging_params.test_params.dump_stream_info, descriptor));
294 
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) {
300  LOG(WARNING)
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 "
304  "be set to LIVE.";
305  }
306  // Skip the check for DASH as DASH defaults to 'dynamic' MPD when segment
307  // template is provided.
308  }
309 
310  if (!descriptor.output.empty()) {
311  if (outputs.find(descriptor.output) != outputs.end()) {
312  return Status(
313  error::INVALID_ARGUMENT,
314  "Seeing duplicated outputs '" + descriptor.output +
315  "' in stream descriptors. Every output must be unique.");
316  }
317  outputs.insert(descriptor.output);
318  }
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 "
326  "must be unique.");
327  }
328  segment_templates.insert(descriptor.segment_template);
329  }
330  }
331 
332  if (packaging_params.output_media_info && !on_demand_dash_profile) {
333  // TODO(rkuroiwa, kqyang): Support partial media info dump for live.
334  return Status(error::UNIMPLEMENTED,
335  "--output_media_info is only supported for on-demand profile "
336  "(not using segment_template).");
337  }
338 
339  return Status::OK;
340 }
341 
342 bool StreamDescriptorCompareFn(const StreamDescriptor& a,
343  const StreamDescriptor& b) {
344  // This function is used by std::sort() to sort the stream descriptors.
345  // Note that std::sort() need a comparator that return true iff the first
346  // argument is strictly lower than the second one. That is: must return false
347  // when they are equal. The requirement is enforced in gcc/g++ but not in
348  // clang.
349  if (a.input == b.input) {
350  if (a.stream_selector == b.stream_selector) {
351  // The MPD notifier requires that the main track comes first, so make
352  // sure that happens.
353  return a.trick_play_factor < b.trick_play_factor;
354  } else {
355  return a.stream_selector < b.stream_selector;
356  }
357  }
358 
359  return a.input < b.input;
360 }
361 
362 // A fake clock that always return time 0 (epoch). Should only be used for
363 // testing.
364 class FakeClock : public base::Clock {
365  public:
366  base::Time Now() override { return base::Time(); }
367 };
368 
369 bool StreamInfoToTextMediaInfo(const StreamDescriptor& stream_descriptor,
370  MediaInfo* text_media_info) {
371  std::string codec;
372  if (!DetermineTextFileCodec(stream_descriptor.input, &codec)) {
373  LOG(ERROR) << "Failed to determine the text file format for "
374  << stream_descriptor.input;
375  return false;
376  }
377 
378  MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
379  text_info->set_codec(codec);
380 
381  const std::string& language = stream_descriptor.language;
382  if (!language.empty()) {
383  text_info->set_language(language);
384  }
385 
386  text_media_info->set_media_file_name(stream_descriptor.output);
387  text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
388 
389  if (stream_descriptor.bandwidth != 0) {
390  text_media_info->set_bandwidth(stream_descriptor.bandwidth);
391  } else {
392  // Text files are usually small and since the input is one file; there's no
393  // way for the player to do ranged requests. So set this value to something
394  // reasonable.
395  const int kDefaultTextBandwidth = 256;
396  text_media_info->set_bandwidth(kDefaultTextBandwidth);
397  }
398 
399  return true;
400 }
401 
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);
410 
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) {
415  return Status(
416  error::INVALID_ARGUMENT,
417  "Must define decryption key source when defining key provider");
418  }
419  demuxer->SetKeySource(std::move(decryption_key_source));
420  }
421 
422  *new_demuxer = std::move(demuxer);
423  return Status::OK;
424 }
425 
426 std::shared_ptr<MediaHandler> CreateEncryptionHandler(
427  const PackagingParams& packaging_params,
428  const StreamDescriptor& stream,
429  KeySource* key_source) {
430  if (stream.skip_encryption) {
431  return nullptr;
432  }
433 
434  if (!key_source) {
435  return nullptr;
436  }
437 
438  // Make a copy so that we can modify it for this specific stream.
439  EncryptionParams encryption_params = packaging_params.encryption_params;
440 
441  // Use Sample AES in MPEG2TS.
442  // TODO(kqyang): Consider adding a new flag to enable Sample AES as we
443  // will support CENC in TS in the future.
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;
450  }
451 
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&) {
456  return drm_label;
457  };
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(
463  &Packager::DefaultStreamLabelFunction, kDefaultMaxSdPixels,
464  kDefaultMaxHdPixels, kDefaultMaxUhd1Pixels, std::placeholders::_1);
465  }
466 
467  return std::make_shared<EncryptionHandler>(encryption_params, key_source);
468 }
469 
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));
476 }
477 
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);
484  DCHECK(job_manager);
485 
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");
490  }
491 
492  // Text files are usually small and since the input is one file;
493  // there's no way for the player to do ranged requests. So set this
494  // value to something reasonable if it is missing.
495  MuxerOptions muxer_options = CreateMuxerOptions(stream, packaging_params);
496  muxer_options.bandwidth = stream.bandwidth ? stream.bandwidth : 256;
497 
498  auto output = std::make_shared<WebVttTextOutputHandler>(
499  muxer_options, std::move(muxer_listener));
500 
501  std::unique_ptr<FileReader> reader;
502  RETURN_IF_ERROR(FileReader::Open(stream.input, &reader));
503 
504  auto parser =
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)
509  : nullptr;
510  auto chunker = CreateTextChunker(packaging_params.chunking_params);
511 
512  job_manager->Add("Segmented Text Job", parser);
513 
514  return MediaHandler::Chain({std::move(parser), std::move(padder),
515  std::move(cue_aligner), std::move(chunker),
516  std::move(output)});
517 }
518 
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;
526  RETURN_IF_ERROR(FileReader::Open(stream.input, &reader));
527 
528  auto parser =
529  std::make_shared<WebVttParser>(std::move(reader), stream.language);
530  auto padder = std::make_shared<TextPadder>(kDefaultTextZeroBiasMs);
531 
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));
535 
536  // Optional Cue Alignment Handler
537  std::shared_ptr<MediaHandler> cue_aligner;
538  if (sync_points) {
539  cue_aligner = std::make_shared<CueAlignmentHandler>(sync_points);
540  }
541 
542  std::shared_ptr<MediaHandler> chunker =
543  CreateTextChunker(packaging_params.chunking_params);
544 
545  *root = parser;
546 
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)});
550 }
551 
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);
561  DCHECK(job_manager);
562  for (const StreamDescriptor& stream : streams) {
563  // There are currently options:
564  // TEXT TTML --> TEXT TTML [ supported ], for DASH only.
565  // TEXT WEBVTT --> TEXT WEBVTT [ supported ]
566  // TEXT WEBVTT --> MP4 WEBVTT [ supported ]
567  // MP4 WEBVTT --> MP4 WEBVTT [ unsupported ]
568  // MP4 WEBVTT --> TEXT WEBVTT [ unsupported ]
569  const auto input_container = DetermineContainerFromFileName(stream.input);
570  const auto output_container = GetOutputFormat(stream);
571 
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);
576  }
577 
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 "
583  "the updates.");
584  }
585 
586  std::unique_ptr<MuxerListener> muxer_listener =
587  muxer_listener_factory->CreateListener(ToMuxerListenerData(stream));
588 
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));
593 
594  job_manager->Add("MP4 text job", std::move(root));
595  } else {
596  std::unique_ptr<MuxerListener> hls_listener =
597  muxer_listener_factory->CreateHlsListener(
598  ToMuxerListenerData(stream));
599 
600  // Check input to ensure that output is possible.
601  if (hls_listener) {
602  if (input_container == CONTAINER_TTML) {
603  return Status(error::INVALID_ARGUMENT,
604  "HLS does not support TTML in xml format.");
605  }
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.");
610  }
611  }
612 
613  if (mpd_notifier && !stream.segment_template.empty()) {
614  return Status(error::INVALID_ARGUMENT,
615  "Cannot create text output for MPD with segment output.");
616  }
617 
618  // If we are outputting to HLS, then create the HLS test pipeline that
619  // will create segmented text output.
620  if (hls_listener) {
621  RETURN_IF_ERROR(CreateHlsTextJob(stream, packaging_params,
622  std::move(hls_listener), sync_points,
623  job_manager));
624  }
625 
626  if (!stream.output.empty()) {
627  if (!File::Copy(stream.input.c_str(), stream.output.c_str())) {
628  std::string error;
629  base::StringAppendF(
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);
633  }
634 
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.");
639  }
640 
641  // If we are outputting to MPD, just add the input to the outputted
642  // manifest.
643  if (mpd_notifier) {
644  uint32_t unused;
645  if (mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
646  mpd_notifier->Flush();
647  } else {
648  return Status(error::PARSER_FAILURE,
649  "Failed to process text file " + stream.input);
650  }
651  }
652 
653  if (packaging_params.output_media_info) {
655  text_media_info, stream.output + kMediaInfoSuffix);
656  }
657  }
658  }
659  }
660 
661  return Status::OK;
662 }
663 
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);
674  DCHECK(job_manager);
675  // Store all the demuxers in a map so that we can look up a stream's demuxer.
676  // This is step one in making this part of the pipeline less dependant on
677  // order.
678  std::map<std::string, std::shared_ptr<Demuxer>> sources;
679  std::map<std::string, std::shared_ptr<MediaHandler>> cue_aligners;
680 
681  for (const StreamDescriptor& stream : streams) {
682  bool seen_input_before = sources.find(stream.input) != sources.end();
683  if (seen_input_before) {
684  continue;
685  }
686 
687  RETURN_IF_ERROR(
688  CreateDemuxer(stream, packaging_params, &sources[stream.input]));
689  cue_aligners[stream.input] =
690  sync_points ? std::make_shared<CueAlignmentHandler>(sync_points)
691  : nullptr;
692  }
693 
694  for (auto& source : sources) {
695  job_manager->Add("RemuxJob", source.second);
696  }
697 
698  // Replicators are shared among all streams with the same input and stream
699  // selector.
700  std::shared_ptr<MediaHandler> replicator;
701 
702  std::string previous_input;
703  std::string previous_selector;
704 
705  for (const StreamDescriptor& stream : streams) {
706  // Get the demuxer for this stream.
707  auto& demuxer = sources[stream.input];
708  auto& cue_aligner = cue_aligners[stream.input];
709 
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;
715 
716  // If the stream has no output, then there is no reason setting-up the rest
717  // of the pipeline.
718  if (stream.output.empty() && stream.segment_template.empty()) {
719  continue;
720  }
721 
722  // Just because it is a different stream descriptor does not mean it is a
723  // new stream. Multiple stream descriptors may have the same stream but
724  // only differ by trick play factor.
725  if (new_stream) {
726  if (!stream.language.empty()) {
727  demuxer->SetLanguageOverride(stream.stream_selector, stream.language);
728  }
729 
730  replicator = std::make_shared<Replicator>();
731  auto chunker =
732  std::make_shared<ChunkingHandler>(packaging_params.chunking_params);
733  auto encryptor = CreateEncryptionHandler(packaging_params, stream,
734  encryption_key_source);
735 
736  // TODO(vaage) : Create a nicer way to connect handlers to demuxers.
737  if (sync_points) {
738  RETURN_IF_ERROR(
739  MediaHandler::Chain({cue_aligner, chunker, encryptor, replicator}));
740  RETURN_IF_ERROR(
741  demuxer->SetHandler(stream.stream_selector, cue_aligner));
742  } else {
743  RETURN_IF_ERROR(MediaHandler::Chain({chunker, encryptor, replicator}));
744  RETURN_IF_ERROR(demuxer->SetHandler(stream.stream_selector, chunker));
745  }
746  }
747 
748  // Create the muxer (output) for this track.
749  std::shared_ptr<Muxer> muxer =
750  muxer_factory->CreateMuxer(GetOutputFormat(stream), stream);
751  if (!muxer) {
752  return Status(error::INVALID_ARGUMENT, "Failed to create muxer for " +
753  stream.input + ":" +
754  stream.stream_selector);
755  }
756 
757  std::unique_ptr<MuxerListener> muxer_listener =
758  muxer_listener_factory->CreateListener(ToMuxerListenerData(stream));
759  muxer->SetMuxerListener(std::move(muxer_listener));
760 
761  // Trick play is optional.
762  std::shared_ptr<MediaHandler> trick_play =
763  stream.trick_play_factor
764  ? std::make_shared<TrickPlayHandler>(stream.trick_play_factor)
765  : nullptr;
766 
767  RETURN_IF_ERROR(MediaHandler::Chain({replicator, trick_play, muxer}));
768  }
769 
770  return Status::OK;
771 }
772 
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);
783  DCHECK(job_manager);
784 
785  // Group all streams based on which pipeline they will use.
786  std::vector<std::reference_wrapper<const StreamDescriptor>> text_streams;
787  std::vector<std::reference_wrapper<const StreamDescriptor>>
788  audio_video_streams;
789 
790  bool has_transport_audio_video_streams = false;
791  bool has_non_transport_audio_video_streams = false;
792 
793  for (const StreamDescriptor& stream : stream_descriptors) {
794  // TODO: Find a better way to determine what stream type a stream
795  // descriptor is as |stream_selector| may use an index. This would
796  // also allow us to use a simpler audio pipeline.
797  if (stream.stream_selector == "text") {
798  text_streams.push_back(stream);
799  } else {
800  audio_video_streams.push_back(stream);
801 
802  switch (GetOutputFormat(stream)) {
803  case CONTAINER_MPEG2TS:
804  case CONTAINER_AAC:
805  case CONTAINER_AC3:
806  case CONTAINER_EAC3:
807  has_transport_audio_video_streams = true;
808  break;
809  default:
810  has_non_transport_audio_video_streams = true;
811  break;
812  }
813  }
814  }
815 
816  // Audio/Video streams need to be in sorted order so that demuxers and trick
817  // play handlers get setup correctly.
818  std::sort(audio_video_streams.begin(), audio_video_streams.end(),
819  media::StreamDescriptorCompareFn);
820 
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) {
830  // Don't insert the X-TIMESTAMP-MAP in WebVTT if there is no transport
831  // stream.
832  text_packaging_params.transport_stream_timestamp_offset_ms = 0;
833  }
834  }
835 
836  RETURN_IF_ERROR(CreateTextJobs(text_streams, text_packaging_params,
837  sync_points, muxer_listener_factory,
838  muxer_factory, mpd_notifier, job_manager));
839  }
840 
841  RETURN_IF_ERROR(CreateAudioVideoJobs(
842  audio_video_streams, packaging_params, encryption_key_source, sync_points,
843  muxer_listener_factory, muxer_factory, job_manager));
844 
845  // Initialize processing graph.
846  return job_manager->InitializeJobs();
847 }
848 
849 } // namespace
850 } // namespace media
851 
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;
859 };
860 
861 Packager::Packager() {}
862 
863 Packager::~Packager() {}
864 
866  const PackagingParams& packaging_params,
867  const std::vector<StreamDescriptor>& stream_descriptors) {
868  // Needed by base::WorkedPool used in ThreadedIoFile.
869  static base::AtExitManager exit;
870  static media::LibcryptoThreading libcrypto_threading;
871 
872  if (internal_)
873  return Status(error::INVALID_ARGUMENT, "Already initialized.");
874 
875  RETURN_IF_ERROR(media::ValidateParams(packaging_params, stream_descriptors));
876 
877  if (!packaging_params.test_params.injected_library_version.empty()) {
878  SetPackagerVersionForTesting(
879  packaging_params.test_params.injected_library_version);
880  }
881 
882  std::unique_ptr<PackagerInternal> internal(new PackagerInternal);
883 
884  // Create encryption key source if needed.
885  if (packaging_params.encryption_params.key_provider != KeyProvider::kNone) {
886  internal->encryption_key_source = CreateEncryptionKeySource(
887  static_cast<media::FourCC>(
888  packaging_params.encryption_params.protection_scheme),
889  packaging_params.encryption_params);
890  if (!internal->encryption_key_source)
891  return Status(error::INVALID_ARGUMENT, "Failed to create key source.");
892  }
893 
894  // Update MPD output and HLS output if needed.
895  MpdParams mpd_params = packaging_params.mpd_params;
896  HlsParams hls_params = packaging_params.hls_params;
897 
898  // |target_segment_duration| is needed for bandwidth estimation and also for
899  // DASH approximate segment timeline.
900  const double target_segment_duration =
902  if (mpd_params.target_segment_duration != 0)
903  mpd_params.target_segment_duration = target_segment_duration;
904  if (hls_params.target_segment_duration != 0)
905  hls_params.target_segment_duration = target_segment_duration;
906 
907  // Store callback params to make it available during packaging.
908  internal->buffer_callback_params = packaging_params.buffer_callback_params;
909  if (internal->buffer_callback_params.write_func) {
911  internal->buffer_callback_params, mpd_params.mpd_output);
913  internal->buffer_callback_params, hls_params.master_playlist_output);
914  }
915 
916  // Both DASH and HLS require language to follow RFC5646
917  // (https://tools.ietf.org/html/rfc5646), which requires the language to be
918  // in the shortest form.
919  mpd_params.default_language =
921  mpd_params.default_text_language =
923  hls_params.default_language =
925  hls_params.default_text_language =
927 
928  if (!mpd_params.mpd_output.empty()) {
929  const bool on_demand_dash_profile =
930  stream_descriptors.begin()->segment_template.empty();
931  const MpdOptions mpd_options =
932  media::GetMpdOptions(on_demand_dash_profile, mpd_params);
933  internal->mpd_notifier.reset(new SimpleMpdNotifier(mpd_options));
934  if (!internal->mpd_notifier->Init()) {
935  LOG(ERROR) << "MpdNotifier failed to initialize.";
936  return Status(error::INVALID_ARGUMENT,
937  "Failed to initialize MpdNotifier.");
938  }
939  }
940 
941  if (!hls_params.master_playlist_output.empty()) {
942  internal->hls_notifier.reset(new hls::SimpleHlsNotifier(hls_params));
943  }
944 
945  std::unique_ptr<SyncPointQueue> sync_points;
946  if (!packaging_params.ad_cue_generator_params.cue_points.empty()) {
947  sync_points.reset(
948  new SyncPointQueue(packaging_params.ad_cue_generator_params));
949  }
950  internal->job_manager.reset(new JobManager(std::move(sync_points)));
951 
952  std::vector<StreamDescriptor> streams_for_jobs;
953 
954  for (const StreamDescriptor& descriptor : stream_descriptors) {
955  // We may need to overwrite some values, so make a copy first.
956  StreamDescriptor copy = descriptor;
957 
958  if (internal->buffer_callback_params.read_func) {
959  copy.input = File::MakeCallbackFileName(internal->buffer_callback_params,
960  descriptor.input);
961  }
962 
963  if (internal->buffer_callback_params.write_func) {
964  copy.output = File::MakeCallbackFileName(internal->buffer_callback_params,
965  descriptor.output);
967  internal->buffer_callback_params, descriptor.segment_template);
968  }
969 
970  // Update language to ISO_639_2 code if set.
971  if (!copy.language.empty()) {
972  copy.language = LanguageToISO_639_2(descriptor.language);
973  if (copy.language == "und") {
974  return Status(
975  error::INVALID_ARGUMENT,
976  "Unknown/invalid language specified: " + descriptor.language);
977  }
978  }
979 
980  streams_for_jobs.push_back(copy);
981  }
982 
983  media::MuxerFactory muxer_factory(packaging_params);
984  if (packaging_params.test_params.inject_fake_clock) {
985  muxer_factory.OverrideClock(&internal->fake_clock);
986  }
987 
988  media::MuxerListenerFactory muxer_listener_factory(
989  packaging_params.output_media_info, internal->mpd_notifier.get(),
990  internal->hls_notifier.get());
991 
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()));
997 
998  internal_ = std::move(internal);
999  return Status::OK;
1000 }
1001 
1003  if (!internal_)
1004  return Status(error::INVALID_ARGUMENT, "Not yet initialized.");
1005 
1006  RETURN_IF_ERROR(internal_->job_manager->RunJobs());
1007 
1008  if (internal_->hls_notifier) {
1009  if (!internal_->hls_notifier->Flush())
1010  return Status(error::INVALID_ARGUMENT, "Failed to flush Hls.");
1011  }
1012  if (internal_->mpd_notifier) {
1013  if (!internal_->mpd_notifier->Flush())
1014  return Status(error::INVALID_ARGUMENT, "Failed to flush Mpd.");
1015  }
1016  return Status::OK;
1017 }
1018 
1020  if (!internal_) {
1021  LOG(INFO) << "Not yet initialized. Return directly.";
1022  return;
1023  }
1024  internal_->job_manager->CancelJobs();
1025 }
1026 
1028  return GetPackagerVersion();
1029 }
1030 
1032  int max_sd_pixels,
1033  int max_hd_pixels,
1034  int max_uhd1_pixels,
1035  const EncryptionParams::EncryptedStreamAttributes& stream_attributes) {
1036  if (stream_attributes.stream_type ==
1037  EncryptionParams::EncryptedStreamAttributes::kAudio)
1038  return "AUDIO";
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)
1044  return "SD";
1045  if (pixels <= max_hd_pixels)
1046  return "HD";
1047  if (pixels <= max_uhd1_pixels)
1048  return "UHD1";
1049  return "UHD2";
1050  }
1051  return "";
1052 }
1053 
1054 } // namespace shaka
BufferCallbackParams buffer_callback_params
Buffer callback params.
Definition: packager.h:66
std::string master_playlist_output
HLS master playlist output path.
Definition: hls_params.h:27
DASH MPD related parameters.
Definition: mpd_params.h:16
Defines a single input/output stream.
Definition: packager.h:73
std::string input
Input/source media file path or network stream URL. Required.
Definition: packager.h:75
HlsParams hls_params
HLS related parameters.
Definition: packager.h:59
Status Initialize(const PackagingParams &packaging_params, const std::vector< StreamDescriptor > &stream_descriptors)
Definition: packager.cc:865
std::string default_language
Definition: mpd_params.h:58
static std::string DefaultStreamLabelFunction(int max_sd_pixels, int max_hd_pixels, int max_uhd1_pixels, const EncryptionParams::EncryptedStreamAttributes &stream_attributes)
Definition: packager.cc:1031
ChunkingParams chunking_params
Chunking (segmentation) related parameters.
Definition: packager.h:48
std::string default_text_language
Definition: hls_params.h:53
std::vector< Cuepoint > cue_points
List of cuepoints.
HLS related parameters.
Definition: hls_params.h:23
std::string LanguageToShortestForm(const std::string &language)
std::string segment_template
Specifies segment template. Can be empty.
Definition: packager.h:85
static bool Copy(const char *from_file_name, const char *to_file_name)
Definition: file.cc:281
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:216
bool inject_fake_clock
Definition: packager.h:31
Convenience class which initializes and terminates libcrypto threading.
All the methods that are virtual are virtual for mocking.
static std::string GetLibraryVersion()
Definition: packager.cc:1027
double target_segment_duration
Definition: hls_params.h:62
std::string LanguageToISO_639_2(const std::string &language)
std::string injected_library_version
Definition: packager.h:34
MpdParams mpd_params
DASH MPD related parameters.
Definition: packager.h:57
AdCueGeneratorParams ad_cue_generator_params
Out of band cuepoint parameters.
Definition: packager.h:51
static bool WriteMediaInfoToFile(const MediaInfo &media_info, const std::string &output_file_path)
EncryptionParams encryption_params
Encryption and Decryption Parameters.
Definition: packager.h:62
std::string mpd_output
MPD output file path.
Definition: mpd_params.h:18
A synchronized queue for cue points.
Status Run()
Definition: packager.cc:1002
static std::string MakeCallbackFileName(const BufferCallbackParams &callback_params, const std::string &name)
Definition: file.cc:354
std::string output
Definition: packager.h:83
double target_segment_duration
Definition: mpd_params.h:84
Encrypted stream information that is used to determine stream label.
void OverrideClock(base::Clock *clock)
std::string default_language
Definition: hls_params.h:50
double segment_duration_in_seconds
Segment duration in seconds.
Defines Mpd Options.
Definition: mpd_options.h:25
void Cancel()
Cancel packaging. Note that it has to be called from another thread.
Definition: packager.cc:1019
Packaging parameters.
Definition: packager.h:38
static Status Open(const std::string &filename, std::unique_ptr< FileReader > *out)
Definition: text_readers.cc:15
std::string language
Definition: packager.h:108
std::string default_text_language
Definition: mpd_params.h:61