7 #include <gflags/gflags.h>
10 #include "packager/app/fixed_key_encryption_flags.h"
11 #include "packager/app/libcrypto_threading.h"
12 #include "packager/app/mpd_flags.h"
13 #include "packager/app/muxer_flags.h"
14 #include "packager/app/packager_util.h"
15 #include "packager/app/stream_descriptor.h"
16 #include "packager/app/vlog_flags.h"
17 #include "packager/app/widevine_encryption_flags.h"
18 #include "packager/base/at_exit.h"
19 #include "packager/base/command_line.h"
20 #include "packager/base/logging.h"
21 #include "packager/base/stl_util.h"
22 #include "packager/base/strings/string_split.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/media/base/container_names.h"
27 #include "packager/media/base/demuxer.h"
28 #include "packager/media/base/fourccs.h"
29 #include "packager/media/base/key_source.h"
30 #include "packager/media/base/muxer_options.h"
31 #include "packager/media/base/muxer_util.h"
32 #include "packager/media/event/mpd_notify_muxer_listener.h"
33 #include "packager/media/event/vod_media_info_dump_muxer_listener.h"
34 #include "packager/media/file/file.h"
35 #include "packager/media/formats/mp2t/ts_muxer.h"
36 #include "packager/media/formats/mp4/mp4_muxer.h"
37 #include "packager/media/formats/webm/webm_muxer.h"
38 #include "packager/mpd/base/dash_iop_mpd_notifier.h"
39 #include "packager/mpd/base/media_info.pb.h"
40 #include "packager/mpd/base/mpd_builder.h"
41 #include "packager/mpd/base/simple_mpd_notifier.h"
42 #include "packager/version/version.h"
44 DEFINE_bool(use_fake_clock_for_muxer,
46 "Set to true to use a fake clock for muxer. With this flag set, "
47 "creation time and modification time in outputs are set to 0. "
48 "Should only be used for testing.");
52 "Packager driver program. Usage:\n\n"
53 "%s [flags] <stream_descriptor> ...\n"
54 "stream_descriptor consists of comma separated field_name/value pairs:\n"
55 "field_name=value,[field_name=value,]...\n"
56 "Supported field names are as follows:\n"
57 " - input (in): Required input/source media file path or network stream\n"
59 " - stream_selector (stream): Required field with value 'audio',\n"
60 " 'video', or stream number (zero based).\n"
61 " - output (out): Required output file (single file) or initialization\n"
62 " file path (multiple file).\n"
63 " - segment_template (segment): Optional value which specifies the\n"
64 " naming pattern for the segment files, and that the stream should be\n"
65 " split into multiple files. Its presence should be consistent across\n"
67 " - bandwidth (bw): Optional value which contains a user-specified\n"
68 " content bit rate for the stream, in bits/sec. If specified, this\n"
69 " value is propagated to the $Bandwidth$ template parameter for\n"
70 " segment names. If not specified, its value may be estimated.\n"
71 " - language (lang): Optional value which contains a user-specified\n"
72 " language tag. If specified, this value overrides any language\n"
73 " metadata in the input track.\n"
74 " - output_format (format): Optional value which specifies the format\n"
75 " of the output files (MP4 or WebM). If not specified, it will be\n"
76 " derived from the file extension of the output file.\n";
78 const char kMediaInfoSuffix[] =
".media_info";
82 kArgumentValidationFailed,
90 std::string DetermineTextFileFormat(
const std::string& file) {
93 LOG(ERROR) <<
"Failed to open file " << file
94 <<
" to determine file format.";
97 edash_packager::media::MediaContainerName container_name =
98 edash_packager::media::DetermineContainer(
99 reinterpret_cast<const uint8_t*>(content.data()), content.size());
100 if (container_name == edash_packager::media::CONTAINER_WEBVTT) {
102 }
else if (container_name == edash_packager::media::CONTAINER_TTML) {
109 edash_packager::media::FourCC GetProtectionScheme(
110 const std::string& protection_scheme) {
111 if (protection_scheme ==
"cenc") {
112 return edash_packager::media::FOURCC_cenc;
113 }
else if (protection_scheme ==
"cens") {
114 return edash_packager::media::FOURCC_cens;
115 }
else if (protection_scheme ==
"cbc1") {
116 return edash_packager::media::FOURCC_cbc1;
117 }
else if (protection_scheme ==
"cbcs") {
118 return edash_packager::media::FOURCC_cbcs;
120 LOG(ERROR) <<
"Unknown protection scheme: " << protection_scheme;
121 return edash_packager::media::FOURCC_NULL;
127 namespace edash_packager {
132 class FakeClock :
public base::Clock {
134 base::Time Now()
override {
return base::Time(); }
138 class RemuxJob :
public base::SimpleThread {
140 RemuxJob(scoped_ptr<Demuxer> demuxer)
141 : SimpleThread(
"RemuxJob"),
142 demuxer_(demuxer.Pass()) {}
144 ~RemuxJob()
override {
145 STLDeleteElements(&muxers_);
148 void AddMuxer(scoped_ptr<Muxer> mux) {
149 muxers_.push_back(mux.release());
152 Demuxer* demuxer() {
return demuxer_.get(); }
153 Status status() {
return status_; }
156 void Run()
override {
158 status_ = demuxer_->Run();
161 scoped_ptr<Demuxer> demuxer_;
162 std::vector<Muxer*> muxers_;
165 DISALLOW_COPY_AND_ASSIGN(RemuxJob);
168 bool StreamInfoToTextMediaInfo(
const StreamDescriptor& stream_descriptor,
169 const MuxerOptions& stream_muxer_options,
170 MediaInfo* text_media_info) {
171 const std::string& language = stream_descriptor.language;
172 std::string format = DetermineTextFileFormat(stream_descriptor.input);
173 if (format.empty()) {
174 LOG(ERROR) <<
"Failed to determine the text file format for "
175 << stream_descriptor.input;
179 if (!
File::Copy(stream_descriptor.input.c_str(),
180 stream_muxer_options.output_file_name.c_str())) {
181 LOG(ERROR) <<
"Failed to copy the input file (" << stream_descriptor.input
182 <<
") to output file (" << stream_muxer_options.output_file_name
187 text_media_info->set_media_file_name(stream_muxer_options.output_file_name);
188 text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
190 if (stream_muxer_options.bandwidth != 0) {
191 text_media_info->set_bandwidth(stream_muxer_options.bandwidth);
196 text_media_info->set_bandwidth(256);
199 MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
200 text_info->set_format(format);
201 if (!language.empty())
202 text_info->set_language(language);
207 scoped_ptr<Muxer> CreateOutputMuxer(
const MuxerOptions& options,
208 MediaContainerName container) {
209 if (container == CONTAINER_WEBM) {
210 return scoped_ptr<Muxer>(
new webm::WebMMuxer(options));
211 }
else if (container == CONTAINER_MPEG2TS) {
212 return scoped_ptr<Muxer>(
new mp2t::TsMuxer(options));
214 DCHECK_EQ(container, CONTAINER_MOV);
215 return scoped_ptr<Muxer>(
new mp4::MP4Muxer(options));
219 bool CreateRemuxJobs(
const StreamDescriptorList& stream_descriptors,
220 const MuxerOptions& muxer_options,
221 FakeClock* fake_clock,
222 KeySource* key_source,
223 MpdNotifier* mpd_notifier,
224 std::vector<RemuxJob*>* remux_jobs) {
227 std::string previous_input;
228 for (StreamDescriptorList::const_iterator stream_iter =
229 stream_descriptors.begin();
230 stream_iter != stream_descriptors.end();
233 MuxerOptions stream_muxer_options(muxer_options);
234 stream_muxer_options.output_file_name = stream_iter->output;
235 if (!stream_iter->segment_template.empty()) {
236 if (!ValidateSegmentTemplate(stream_iter->segment_template)) {
237 LOG(ERROR) <<
"ERROR: segment template with '"
238 << stream_iter->segment_template <<
"' is invalid.";
241 stream_muxer_options.segment_template = stream_iter->segment_template;
243 stream_muxer_options.bandwidth = stream_iter->bandwidth;
246 if (stream_iter->stream_selector ==
"text") {
247 MediaInfo text_media_info;
248 if (!StreamInfoToTextMediaInfo(*stream_iter, stream_muxer_options,
255 if (!mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
256 LOG(ERROR) <<
"Failed to process text file " << stream_iter->input;
258 mpd_notifier->Flush();
260 }
else if (FLAGS_output_media_info) {
263 stream_muxer_options.output_file_name + kMediaInfoSuffix);
266 <<
"--mpd_output or --output_media_info flags are "
267 "required for text output. Skipping manifest related output for "
268 << stream_iter->input;
273 if (stream_iter->input != previous_input) {
275 scoped_ptr<Demuxer> demuxer(
new Demuxer(stream_iter->input));
276 if (FLAGS_enable_widevine_decryption ||
277 FLAGS_enable_fixed_key_decryption) {
278 scoped_ptr<KeySource> key_source(CreateDecryptionKeySource());
281 demuxer->SetKeySource(key_source.Pass());
283 Status status = demuxer->Initialize();
285 LOG(ERROR) <<
"Demuxer failed to initialize: " << status.ToString();
288 if (FLAGS_dump_stream_info) {
289 printf(
"\nFile \"%s\":\n", stream_iter->input.c_str());
290 DumpStreamInfo(demuxer->streams());
291 if (stream_iter->output.empty())
294 remux_jobs->push_back(
new RemuxJob(demuxer.Pass()));
295 previous_input = stream_iter->input;
297 DCHECK(!remux_jobs->empty());
299 MediaContainerName output_format = stream_iter->output_format;
300 if (output_format == CONTAINER_UNKNOWN) {
302 DetermineContainerFromFileName(stream_muxer_options.output_file_name);
304 if (output_format == CONTAINER_UNKNOWN) {
305 LOG(ERROR) <<
"Unable to determine output format for file "
306 << stream_muxer_options.output_file_name;
311 scoped_ptr<Muxer> muxer(
312 CreateOutputMuxer(stream_muxer_options, output_format));
313 if (FLAGS_use_fake_clock_for_muxer) muxer->set_clock(fake_clock);
316 muxer->SetKeySource(key_source,
319 FLAGS_crypto_period_duration,
320 GetProtectionScheme(FLAGS_protection_scheme));
323 scoped_ptr<MuxerListener> muxer_listener;
324 DCHECK(!(FLAGS_output_media_info && mpd_notifier));
325 if (FLAGS_output_media_info) {
326 const std::string output_media_info_file_name =
327 stream_muxer_options.output_file_name + kMediaInfoSuffix;
328 scoped_ptr<VodMediaInfoDumpMuxerListener>
329 vod_media_info_dump_muxer_listener(
330 new VodMediaInfoDumpMuxerListener(output_media_info_file_name));
331 muxer_listener = vod_media_info_dump_muxer_listener.Pass();
334 scoped_ptr<MpdNotifyMuxerListener> mpd_notify_muxer_listener(
335 new MpdNotifyMuxerListener(mpd_notifier));
336 muxer_listener = mpd_notify_muxer_listener.Pass();
340 muxer->SetMuxerListener(muxer_listener.Pass());
342 if (!AddStreamToMuxer(remux_jobs->back()->demuxer()->streams(),
343 stream_iter->stream_selector,
344 stream_iter->language,
347 remux_jobs->back()->AddMuxer(muxer.Pass());
353 Status RunRemuxJobs(
const std::vector<RemuxJob*>& remux_jobs) {
355 for (std::vector<RemuxJob*>::const_iterator job_iter = remux_jobs.begin();
356 job_iter != remux_jobs.end();
358 (*job_iter)->Start();
366 for (std::vector<RemuxJob*>::const_iterator job_iter = remux_jobs.begin();
367 job_iter != remux_jobs.end();
369 if ((*job_iter)->HasBeenJoined()) {
370 status = (*job_iter)->status();
378 }
while (!all_joined && status.ok());
383 bool RunPackager(
const StreamDescriptorList& stream_descriptors) {
384 const FourCC protection_scheme = GetProtectionScheme(FLAGS_protection_scheme);
385 if (protection_scheme == FOURCC_NULL)
388 if (!AssignFlagsFromProfile())
391 if (FLAGS_output_media_info && !FLAGS_mpd_output.empty()) {
392 NOTIMPLEMENTED() <<
"ERROR: --output_media_info and --mpd_output do not "
396 if (FLAGS_output_media_info && !FLAGS_single_segment) {
398 NOTIMPLEMENTED() <<
"ERROR: --output_media_info is only supported if "
399 "--single_segment is true.";
404 MuxerOptions muxer_options;
405 if (!GetMuxerOptions(&muxer_options))
408 MpdOptions mpd_options;
409 if (!GetMpdOptions(&mpd_options))
413 scoped_ptr<KeySource> encryption_key_source;
414 if (FLAGS_enable_widevine_encryption || FLAGS_enable_fixed_key_encryption) {
415 encryption_key_source = CreateEncryptionKeySource();
416 if (!encryption_key_source)
420 scoped_ptr<MpdNotifier> mpd_notifier;
421 if (!FLAGS_mpd_output.empty()) {
422 DashProfile profile =
423 FLAGS_single_segment ? kOnDemandProfile : kLiveProfile;
424 std::vector<std::string> base_urls;
425 base::SplitString(FLAGS_base_urls,
',', &base_urls);
426 if (FLAGS_generate_dash_if_iop_compliant_mpd) {
427 mpd_notifier.reset(
new DashIopMpdNotifier(profile, mpd_options, base_urls,
430 mpd_notifier.reset(
new SimpleMpdNotifier(profile, mpd_options, base_urls,
433 if (!mpd_notifier->Init()) {
434 LOG(ERROR) <<
"MpdNotifier failed to initialize.";
439 std::vector<RemuxJob*> remux_jobs;
440 STLElementDeleter<std::vector<RemuxJob*> > scoped_jobs_deleter(&remux_jobs);
441 FakeClock fake_clock;
442 if (!CreateRemuxJobs(stream_descriptors, muxer_options, &fake_clock,
443 encryption_key_source.get(), mpd_notifier.get(),
448 Status status = RunRemuxJobs(remux_jobs);
450 LOG(ERROR) <<
"Packaging Error: " << status.ToString();
454 printf(
"Packaging completed successfully.\n");
458 int PackagerMain(
int argc,
char** argv) {
459 base::AtExitManager exit;
461 base::CommandLine::Init(argc, argv);
462 CHECK(logging::InitLogging(logging::LoggingSettings()));
464 google::SetUsageMessage(base::StringPrintf(kUsage, argv[0]));
465 google::ParseCommandLineFlags(&argc, &argv,
true);
467 std::string version_string =
468 base::StringPrintf(
"edash-packager version %s", kPackagerVersion);
469 google::ShowUsageWithFlags(version_string.c_str());
474 return kArgumentValidationFailed;
479 StreamDescriptorList stream_descriptors;
480 for (
int i = 1; i < argc; ++i) {
481 if (!InsertStreamDescriptor(argv[i], &stream_descriptors))
482 return kArgumentValidationFailed;
484 return RunPackager(stream_descriptors) ? kSuccess : kPackagingFailed;
490 int main(
int argc,
char** argv) {
491 return edash_packager::media::PackagerMain(argc, argv);
bool ValidateFixedCryptoFlags()
bool ValidateWidevineCryptoFlags()