7 #include <gflags/gflags.h>
10 #include "packager/app/fixed_key_encryption_flags.h"
11 #include "packager/app/hls_flags.h"
12 #include "packager/app/libcrypto_threading.h"
13 #include "packager/app/mpd_flags.h"
14 #include "packager/app/muxer_flags.h"
15 #include "packager/app/packager_util.h"
16 #include "packager/app/stream_descriptor.h"
17 #include "packager/app/vlog_flags.h"
18 #include "packager/app/widevine_encryption_flags.h"
19 #include "packager/base/at_exit.h"
20 #include "packager/base/command_line.h"
21 #include "packager/base/files/file_path.h"
22 #include "packager/base/logging.h"
23 #include "packager/base/stl_util.h"
24 #include "packager/base/strings/string_split.h"
25 #include "packager/base/strings/stringprintf.h"
26 #include "packager/base/threading/simple_thread.h"
27 #include "packager/base/time/clock.h"
28 #include "packager/hls/base/hls_notifier.h"
29 #include "packager/hls/base/simple_hls_notifier.h"
30 #include "packager/media/base/container_names.h"
31 #include "packager/media/base/demuxer.h"
32 #include "packager/media/base/fourccs.h"
33 #include "packager/media/base/key_source.h"
34 #include "packager/media/base/muxer_options.h"
35 #include "packager/media/base/muxer_util.h"
36 #include "packager/media/event/hls_notify_muxer_listener.h"
37 #include "packager/media/event/mpd_notify_muxer_listener.h"
38 #include "packager/media/event/vod_media_info_dump_muxer_listener.h"
39 #include "packager/media/file/file.h"
40 #include "packager/media/formats/mp2t/ts_muxer.h"
41 #include "packager/media/formats/mp4/mp4_muxer.h"
42 #include "packager/media/formats/webm/webm_muxer.h"
43 #include "packager/mpd/base/dash_iop_mpd_notifier.h"
44 #include "packager/mpd/base/media_info.pb.h"
45 #include "packager/mpd/base/mpd_builder.h"
46 #include "packager/mpd/base/simple_mpd_notifier.h"
47 #include "packager/version/version.h"
49 DEFINE_bool(use_fake_clock_for_muxer,
51 "Set to true to use a fake clock for muxer. With this flag set, "
52 "creation time and modification time in outputs are set to 0. "
53 "Should only be used for testing.");
60 "Packager driver program. Usage:\n\n"
61 "%s [flags] <stream_descriptor> ...\n"
62 "stream_descriptor consists of comma separated field_name/value pairs:\n"
63 "field_name=value,[field_name=value,]...\n"
64 "Supported field names are as follows:\n"
65 " - input (in): Required input/source media file path or network stream\n"
67 " - stream_selector (stream): Required field with value 'audio',\n"
68 " 'video', or stream number (zero based).\n"
69 " - output (out): Required output file (single file) or initialization\n"
70 " file path (multiple file).\n"
71 " - segment_template (segment): Optional value which specifies the\n"
72 " naming pattern for the segment files, and that the stream should be\n"
73 " split into multiple files. Its presence should be consistent across\n"
75 " - bandwidth (bw): Optional value which contains a user-specified\n"
76 " content bit rate for the stream, in bits/sec. If specified, this\n"
77 " value is propagated to the $Bandwidth$ template parameter for\n"
78 " segment names. If not specified, its value may be estimated.\n"
79 " - language (lang): Optional value which contains a user-specified\n"
80 " language tag. If specified, this value overrides any language\n"
81 " metadata in the input track.\n"
82 " - output_format (format): Optional value which specifies the format\n"
83 " of the output files (MP4 or WebM). If not specified, it will be\n"
84 " derived from the file extension of the output file.\n"
85 " - hls_name: Required for audio when outputting HLS.\n"
86 " name of the output stream. This is not (necessarily) the same as\n"
87 " output. This is used as the NAME attribute for EXT-X-MEDIA\n"
88 " - hls_group_id: Required for audio when outputting HLS.\n"
89 " The group ID for the output stream. For HLS this is used as the\n"
90 " GROUP-ID attribute for EXT-X-MEDIA.\n"
91 " - playlist_name: Required for HLS output.\n"
92 " Name of the playlist for the stream. Usually ends with '.m3u8'.\n";
94 const char kMediaInfoSuffix[] =
".media_info";
98 kArgumentValidationFailed,
106 std::string DetermineTextFileFormat(
const std::string& file) {
109 LOG(ERROR) <<
"Failed to open file " << file
110 <<
" to determine file format.";
113 MediaContainerName container_name = DetermineContainer(
114 reinterpret_cast<const uint8_t*>(content.data()), content.size());
115 if (container_name == CONTAINER_WEBVTT) {
117 }
else if (container_name == CONTAINER_TTML) {
124 FourCC GetProtectionScheme(
const std::string& protection_scheme) {
125 if (protection_scheme ==
"cenc") {
127 }
else if (protection_scheme ==
"cens") {
129 }
else if (protection_scheme ==
"cbc1") {
131 }
else if (protection_scheme ==
"cbcs") {
134 LOG(ERROR) <<
"Unknown protection scheme: " << protection_scheme;
143 class FakeClock :
public base::Clock {
145 base::Time Now()
override {
return base::Time(); }
149 class RemuxJob :
public base::SimpleThread {
151 RemuxJob(scoped_ptr<Demuxer> demuxer)
152 : SimpleThread(
"RemuxJob"),
153 demuxer_(demuxer.Pass()) {}
155 ~RemuxJob()
override {
156 STLDeleteElements(&muxers_);
159 void AddMuxer(scoped_ptr<Muxer> mux) {
160 muxers_.push_back(mux.release());
163 Demuxer* demuxer() {
return demuxer_.get(); }
164 Status status() {
return status_; }
167 void Run()
override {
169 status_ = demuxer_->Run();
172 scoped_ptr<Demuxer> demuxer_;
173 std::vector<Muxer*> muxers_;
176 DISALLOW_COPY_AND_ASSIGN(RemuxJob);
179 bool StreamInfoToTextMediaInfo(
const StreamDescriptor& stream_descriptor,
180 const MuxerOptions& stream_muxer_options,
181 MediaInfo* text_media_info) {
182 const std::string& language = stream_descriptor.language;
183 std::string format = DetermineTextFileFormat(stream_descriptor.input);
184 if (format.empty()) {
185 LOG(ERROR) <<
"Failed to determine the text file format for "
186 << stream_descriptor.input;
190 if (!
File::Copy(stream_descriptor.input.c_str(),
191 stream_muxer_options.output_file_name.c_str())) {
192 LOG(ERROR) <<
"Failed to copy the input file (" << stream_descriptor.input
193 <<
") to output file (" << stream_muxer_options.output_file_name
198 text_media_info->set_media_file_name(stream_muxer_options.output_file_name);
199 text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
201 if (stream_muxer_options.bandwidth != 0) {
202 text_media_info->set_bandwidth(stream_muxer_options.bandwidth);
207 text_media_info->set_bandwidth(256);
210 MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
211 text_info->set_format(format);
212 if (!language.empty())
213 text_info->set_language(language);
218 scoped_ptr<Muxer> CreateOutputMuxer(
const MuxerOptions& options,
219 MediaContainerName container) {
220 if (container == CONTAINER_WEBM) {
221 return scoped_ptr<Muxer>(
new webm::WebMMuxer(options));
222 }
else if (container == CONTAINER_MPEG2TS) {
223 return scoped_ptr<Muxer>(
new mp2t::TsMuxer(options));
225 DCHECK_EQ(container, CONTAINER_MOV);
226 return scoped_ptr<Muxer>(
new mp4::MP4Muxer(options));
230 bool CreateRemuxJobs(
const StreamDescriptorList& stream_descriptors,
231 const MuxerOptions& muxer_options,
232 FakeClock* fake_clock,
233 KeySource* key_source,
234 MpdNotifier* mpd_notifier,
235 hls::HlsNotifier* hls_notifier,
236 std::vector<RemuxJob*>* remux_jobs) {
238 DCHECK(!(mpd_notifier && hls_notifier));
241 std::string previous_input;
242 int stream_number = 0;
243 for (StreamDescriptorList::const_iterator
244 stream_iter = stream_descriptors.begin();
245 stream_iter != stream_descriptors.end();
246 ++stream_iter, ++stream_number) {
248 MuxerOptions stream_muxer_options(muxer_options);
249 stream_muxer_options.output_file_name = stream_iter->output;
250 if (!stream_iter->segment_template.empty()) {
251 if (!ValidateSegmentTemplate(stream_iter->segment_template)) {
252 LOG(ERROR) <<
"ERROR: segment template with '"
253 << stream_iter->segment_template <<
"' is invalid.";
256 stream_muxer_options.segment_template = stream_iter->segment_template;
257 if (stream_muxer_options.single_segment) {
258 LOG(WARNING) <<
"Segment template and single segment are incompatible, "
259 "setting single segment to false.";
260 stream_muxer_options.single_segment =
false;
263 stream_muxer_options.bandwidth = stream_iter->bandwidth;
266 if (stream_iter->stream_selector ==
"text") {
267 MediaInfo text_media_info;
268 if (!StreamInfoToTextMediaInfo(*stream_iter, stream_muxer_options,
275 if (!mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
276 LOG(ERROR) <<
"Failed to process text file " << stream_iter->input;
278 mpd_notifier->Flush();
280 }
else if (FLAGS_output_media_info) {
283 stream_muxer_options.output_file_name + kMediaInfoSuffix);
286 <<
"--mpd_output or --output_media_info flags are "
287 "required for text output. Skipping manifest related output for "
288 << stream_iter->input;
293 if (stream_iter->input != previous_input) {
295 scoped_ptr<Demuxer> demuxer(
new Demuxer(stream_iter->input));
296 if (FLAGS_enable_widevine_decryption ||
297 FLAGS_enable_fixed_key_decryption) {
298 scoped_ptr<KeySource> key_source(CreateDecryptionKeySource());
301 demuxer->SetKeySource(key_source.Pass());
303 Status status = demuxer->Initialize();
305 LOG(ERROR) <<
"Demuxer failed to initialize: " << status.ToString();
308 if (FLAGS_dump_stream_info) {
309 printf(
"\nFile \"%s\":\n", stream_iter->input.c_str());
310 DumpStreamInfo(demuxer->streams());
311 if (stream_iter->output.empty())
314 remux_jobs->push_back(
new RemuxJob(demuxer.Pass()));
315 previous_input = stream_iter->input;
317 DCHECK(!remux_jobs->empty());
319 scoped_ptr<Muxer> muxer(
320 CreateOutputMuxer(stream_muxer_options, stream_iter->output_format));
321 if (FLAGS_use_fake_clock_for_muxer) muxer->set_clock(fake_clock);
324 muxer->SetKeySource(key_source,
327 FLAGS_crypto_period_duration,
328 GetProtectionScheme(FLAGS_protection_scheme));
331 scoped_ptr<MuxerListener> muxer_listener;
332 DCHECK(!(FLAGS_output_media_info && mpd_notifier));
333 if (FLAGS_output_media_info) {
334 const std::string output_media_info_file_name =
335 stream_muxer_options.output_file_name + kMediaInfoSuffix;
336 scoped_ptr<VodMediaInfoDumpMuxerListener>
337 vod_media_info_dump_muxer_listener(
338 new VodMediaInfoDumpMuxerListener(output_media_info_file_name));
339 muxer_listener = vod_media_info_dump_muxer_listener.Pass();
342 scoped_ptr<MpdNotifyMuxerListener> mpd_notify_muxer_listener(
343 new MpdNotifyMuxerListener(mpd_notifier));
344 muxer_listener = mpd_notify_muxer_listener.Pass();
350 std::string group_id = stream_iter->hls_group_id;
351 std::string name = stream_iter->hls_name;
352 std::string hls_playlist_name = stream_iter->hls_playlist_name;
353 if (group_id.empty())
356 name = base::StringPrintf(
"stream_%d", stream_number);
357 if (hls_playlist_name.empty())
358 hls_playlist_name = base::StringPrintf(
"stream_%d.m3u8", stream_number);
360 muxer_listener.reset(
new HlsNotifyMuxerListener(hls_playlist_name, name,
361 group_id, hls_notifier));
365 muxer->SetMuxerListener(muxer_listener.Pass());
367 if (!AddStreamToMuxer(remux_jobs->back()->demuxer()->streams(),
368 stream_iter->stream_selector,
369 stream_iter->language,
373 remux_jobs->back()->AddMuxer(muxer.Pass());
379 Status RunRemuxJobs(
const std::vector<RemuxJob*>& remux_jobs) {
381 for (std::vector<RemuxJob*>::const_iterator job_iter = remux_jobs.begin();
382 job_iter != remux_jobs.end();
384 (*job_iter)->Start();
392 for (std::vector<RemuxJob*>::const_iterator job_iter = remux_jobs.begin();
393 job_iter != remux_jobs.end();
395 if ((*job_iter)->HasBeenJoined()) {
396 status = (*job_iter)->status();
404 }
while (!all_joined && status.ok());
409 bool RunPackager(
const StreamDescriptorList& stream_descriptors) {
410 const FourCC protection_scheme = GetProtectionScheme(FLAGS_protection_scheme);
411 if (protection_scheme == FOURCC_NULL)
414 if (!AssignFlagsFromProfile())
417 if (FLAGS_output_media_info && !FLAGS_mpd_output.empty()) {
418 NOTIMPLEMENTED() <<
"ERROR: --output_media_info and --mpd_output do not "
422 if (FLAGS_output_media_info && !FLAGS_single_segment) {
424 NOTIMPLEMENTED() <<
"ERROR: --output_media_info is only supported if "
425 "--single_segment is true.";
431 if (!FLAGS_mpd_output.empty() && !FLAGS_hls_master_playlist_output.empty()) {
432 LOG(ERROR) <<
"Cannot output both MPD and HLS.";
437 MuxerOptions muxer_options;
438 if (!GetMuxerOptions(&muxer_options))
441 MpdOptions mpd_options;
442 if (!GetMpdOptions(&mpd_options))
446 scoped_ptr<KeySource> encryption_key_source;
447 if (FLAGS_enable_widevine_encryption || FLAGS_enable_fixed_key_encryption) {
448 encryption_key_source = CreateEncryptionKeySource();
449 if (!encryption_key_source)
453 scoped_ptr<MpdNotifier> mpd_notifier;
454 if (!FLAGS_mpd_output.empty()) {
455 DashProfile profile =
456 FLAGS_single_segment ? kOnDemandProfile : kLiveProfile;
457 std::vector<std::string> base_urls;
458 base::SplitString(FLAGS_base_urls,
',', &base_urls);
459 if (FLAGS_generate_dash_if_iop_compliant_mpd) {
460 mpd_notifier.reset(
new DashIopMpdNotifier(profile, mpd_options, base_urls,
463 mpd_notifier.reset(
new SimpleMpdNotifier(profile, mpd_options, base_urls,
466 if (!mpd_notifier->Init()) {
467 LOG(ERROR) <<
"MpdNotifier failed to initialize.";
472 scoped_ptr<hls::HlsNotifier> hls_notifier;
473 if (!FLAGS_hls_master_playlist_output.empty()) {
474 base::FilePath master_playlist_path(FLAGS_hls_master_playlist_output);
475 base::FilePath master_playlist_name = master_playlist_path.BaseName();
477 hls_notifier.reset(
new hls::SimpleHlsNotifier(
478 hls::HlsNotifier::HlsProfile::kOnDemandProfile, FLAGS_hls_base_url,
479 master_playlist_path.DirName().AsEndingWithSeparator().value(),
480 master_playlist_name.value()));
483 std::vector<RemuxJob*> remux_jobs;
484 STLElementDeleter<std::vector<RemuxJob*> > scoped_jobs_deleter(&remux_jobs);
485 FakeClock fake_clock;
486 if (!CreateRemuxJobs(stream_descriptors, muxer_options, &fake_clock,
487 encryption_key_source.get(), mpd_notifier.get(),
488 hls_notifier.get(), &remux_jobs)) {
492 Status status = RunRemuxJobs(remux_jobs);
494 LOG(ERROR) <<
"Packaging Error: " << status.ToString();
499 if (!hls_notifier->Flush())
503 if (!mpd_notifier->Flush())
507 printf(
"Packaging completed successfully.\n");
511 int PackagerMain(
int argc,
char** argv) {
512 base::AtExitManager exit;
514 base::CommandLine::Init(argc, argv);
515 CHECK(logging::InitLogging(logging::LoggingSettings()));
517 google::SetUsageMessage(base::StringPrintf(kUsage, argv[0]));
518 google::ParseCommandLineFlags(&argc, &argv,
true);
520 std::string version_string =
521 base::StringPrintf(
"shaka-packager version %s", kPackagerVersion);
522 google::ShowUsageWithFlags(version_string.c_str());
527 return kArgumentValidationFailed;
529 LibcryptoThreading libcrypto_threading;
532 StreamDescriptorList stream_descriptors;
533 for (
int i = 1; i < argc; ++i) {
534 if (!InsertStreamDescriptor(argv[i], &stream_descriptors))
535 return kArgumentValidationFailed;
537 return RunPackager(stream_descriptors) ? kSuccess : kPackagingFailed;
543 int main(
int argc,
char** argv) {
544 return shaka::media::PackagerMain(argc, argv);
bool ValidateWidevineCryptoFlags()
bool ValidateFixedCryptoFlags()