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/key_source.h"
29 #include "packager/media/base/muxer_options.h"
30 #include "packager/media/base/muxer_util.h"
31 #include "packager/media/event/mpd_notify_muxer_listener.h"
32 #include "packager/media/event/vod_media_info_dump_muxer_listener.h"
33 #include "packager/media/file/file.h"
34 #include "packager/media/formats/mp4/mp4_muxer.h"
35 #include "packager/media/formats/webm/webm_muxer.h"
36 #include "packager/mpd/base/dash_iop_mpd_notifier.h"
37 #include "packager/mpd/base/media_info.pb.h"
38 #include "packager/mpd/base/mpd_builder.h"
39 #include "packager/mpd/base/simple_mpd_notifier.h"
40 #include "packager/version/version.h"
42 DEFINE_bool(use_fake_clock_for_muxer,
44 "Set to true to use a fake clock for muxer. With this flag set, "
45 "creation time and modification time in outputs are set to 0. "
46 "Should only be used for testing.");
50 "Packager driver program. Usage:\n\n"
51 "%s [flags] <stream_descriptor> ...\n"
52 "stream_descriptor consists of comma separated field_name/value pairs:\n"
53 "field_name=value,[field_name=value,]...\n"
54 "Supported field names are as follows:\n"
55 " - input (in): Required input/source media file path or network stream\n"
57 " - stream_selector (stream): Required field with value 'audio',\n"
58 " 'video', or stream number (zero based).\n"
59 " - output (out): Required output file (single file) or initialization\n"
60 " file path (multiple file).\n"
61 " - segment_template (segment): Optional value which specifies the\n"
62 " naming pattern for the segment files, and that the stream should be\n"
63 " split into multiple files. Its presence should be consistent across\n"
65 " - bandwidth (bw): Optional value which contains a user-specified\n"
66 " content bit rate for the stream, in bits/sec. If specified, this\n"
67 " value is propagated to the $Bandwidth$ template parameter for\n"
68 " segment names. If not specified, its value may be estimated.\n"
69 " - language (lang): Optional value which contains a user-specified\n"
70 " language tag. If specified, this value overrides any language\n"
71 " metadata in the input track.\n"
72 " - output_format (format): Optional value which specifies the format\n"
73 " of the output files (MP4 or WebM). If not specified, it will be\n"
74 " derived from the file extension of the output file.\n";
76 const char kMediaInfoSuffix[] =
".media_info";
80 kArgumentValidationFailed,
88 std::string DetermineTextFileFormat(
const std::string& file) {
91 LOG(ERROR) <<
"Failed to open file " << file
92 <<
" to determine file format.";
95 edash_packager::media::MediaContainerName container_name =
96 edash_packager::media::DetermineContainer(
97 reinterpret_cast<const uint8_t*>(content.data()), content.size());
98 if (container_name == edash_packager::media::CONTAINER_WEBVTT) {
100 }
else if (container_name == edash_packager::media::CONTAINER_TTML) {
109 namespace edash_packager {
114 class FakeClock :
public base::Clock {
116 base::Time Now()
override {
return base::Time(); }
120 class RemuxJob :
public base::SimpleThread {
122 RemuxJob(scoped_ptr<Demuxer> demuxer)
123 : SimpleThread(
"RemuxJob"),
124 demuxer_(demuxer.Pass()) {}
126 ~RemuxJob()
override {
127 STLDeleteElements(&muxers_);
130 void AddMuxer(scoped_ptr<Muxer> mux) {
131 muxers_.push_back(mux.release());
134 Demuxer* demuxer() {
return demuxer_.get(); }
135 Status status() {
return status_; }
138 void Run()
override {
140 status_ = demuxer_->Run();
143 scoped_ptr<Demuxer> demuxer_;
144 std::vector<Muxer*> muxers_;
147 DISALLOW_COPY_AND_ASSIGN(RemuxJob);
150 bool StreamInfoToTextMediaInfo(
const StreamDescriptor& stream_descriptor,
151 const MuxerOptions& stream_muxer_options,
152 MediaInfo* text_media_info) {
153 const std::string& language = stream_descriptor.language;
154 std::string format = DetermineTextFileFormat(stream_descriptor.input);
155 if (format.empty()) {
156 LOG(ERROR) <<
"Failed to determine the text file format for "
157 << stream_descriptor.input;
161 if (!
File::Copy(stream_descriptor.input.c_str(),
162 stream_muxer_options.output_file_name.c_str())) {
163 LOG(ERROR) <<
"Failed to copy the input file (" << stream_descriptor.input
164 <<
") to output file (" << stream_muxer_options.output_file_name
169 text_media_info->set_media_file_name(stream_muxer_options.output_file_name);
170 text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
172 if (stream_muxer_options.bandwidth != 0) {
173 text_media_info->set_bandwidth(stream_muxer_options.bandwidth);
178 text_media_info->set_bandwidth(256);
181 MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
182 text_info->set_format(format);
183 if (!language.empty())
184 text_info->set_language(language);
189 scoped_ptr<Muxer> CreateOutputMuxer(
const MuxerOptions& options,
190 MediaContainerName container) {
191 if (container == CONTAINER_WEBM) {
192 return scoped_ptr<Muxer>(
new webm::WebMMuxer(options));
194 DCHECK_EQ(container, CONTAINER_MOV);
195 return scoped_ptr<Muxer>(
new mp4::MP4Muxer(options));
199 bool CreateRemuxJobs(
const StreamDescriptorList& stream_descriptors,
200 const MuxerOptions& muxer_options,
201 FakeClock* fake_clock,
202 KeySource* key_source,
203 MpdNotifier* mpd_notifier,
204 std::vector<RemuxJob*>* remux_jobs) {
207 std::string previous_input;
208 for (StreamDescriptorList::const_iterator stream_iter =
209 stream_descriptors.begin();
210 stream_iter != stream_descriptors.end();
213 MuxerOptions stream_muxer_options(muxer_options);
214 stream_muxer_options.output_file_name = stream_iter->output;
215 if (!stream_iter->segment_template.empty()) {
216 if (!ValidateSegmentTemplate(stream_iter->segment_template)) {
217 LOG(ERROR) <<
"ERROR: segment template with '"
218 << stream_iter->segment_template <<
"' is invalid.";
221 stream_muxer_options.segment_template = stream_iter->segment_template;
223 stream_muxer_options.bandwidth = stream_iter->bandwidth;
226 if (stream_iter->stream_selector ==
"text") {
227 MediaInfo text_media_info;
228 if (!StreamInfoToTextMediaInfo(*stream_iter, stream_muxer_options,
235 if (!mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
236 LOG(ERROR) <<
"Failed to process text file " << stream_iter->input;
238 mpd_notifier->Flush();
240 }
else if (FLAGS_output_media_info) {
243 stream_muxer_options.output_file_name + kMediaInfoSuffix);
246 <<
"--mpd_output or --output_media_info flags are "
247 "required for text output. Skipping manifest related output for "
248 << stream_iter->input;
253 if (stream_iter->input != previous_input) {
255 scoped_ptr<Demuxer> demuxer(
new Demuxer(stream_iter->input));
256 if (FLAGS_enable_widevine_decryption ||
257 FLAGS_enable_fixed_key_decryption) {
258 scoped_ptr<KeySource> key_source(CreateDecryptionKeySource());
261 demuxer->SetKeySource(key_source.Pass());
263 Status status = demuxer->Initialize();
265 LOG(ERROR) <<
"Demuxer failed to initialize: " << status.ToString();
268 if (FLAGS_dump_stream_info) {
269 printf(
"\nFile \"%s\":\n", stream_iter->input.c_str());
270 DumpStreamInfo(demuxer->streams());
271 if (stream_iter->output.empty())
274 remux_jobs->push_back(
new RemuxJob(demuxer.Pass()));
275 previous_input = stream_iter->input;
277 DCHECK(!remux_jobs->empty());
279 MediaContainerName output_format = stream_iter->output_format;
280 if (output_format == CONTAINER_UNKNOWN) {
282 DetermineContainerFromFileName(stream_muxer_options.output_file_name);
284 if (output_format == CONTAINER_UNKNOWN) {
285 LOG(ERROR) <<
"Unable to determine output format for file "
286 << stream_muxer_options.output_file_name;
291 scoped_ptr<Muxer> muxer(
292 CreateOutputMuxer(stream_muxer_options, output_format));
293 if (FLAGS_use_fake_clock_for_muxer) muxer->set_clock(fake_clock);
296 muxer->SetKeySource(key_source,
299 FLAGS_crypto_period_duration);
302 scoped_ptr<MuxerListener> muxer_listener;
303 DCHECK(!(FLAGS_output_media_info && mpd_notifier));
304 if (FLAGS_output_media_info) {
305 const std::string output_media_info_file_name =
306 stream_muxer_options.output_file_name + kMediaInfoSuffix;
307 scoped_ptr<VodMediaInfoDumpMuxerListener>
308 vod_media_info_dump_muxer_listener(
309 new VodMediaInfoDumpMuxerListener(output_media_info_file_name));
310 muxer_listener = vod_media_info_dump_muxer_listener.Pass();
313 scoped_ptr<MpdNotifyMuxerListener> mpd_notify_muxer_listener(
314 new MpdNotifyMuxerListener(mpd_notifier));
315 muxer_listener = mpd_notify_muxer_listener.Pass();
319 muxer->SetMuxerListener(muxer_listener.Pass());
321 if (!AddStreamToMuxer(remux_jobs->back()->demuxer()->streams(),
322 stream_iter->stream_selector,
323 stream_iter->language,
326 remux_jobs->back()->AddMuxer(muxer.Pass());
332 Status RunRemuxJobs(
const std::vector<RemuxJob*>& remux_jobs) {
334 for (std::vector<RemuxJob*>::const_iterator job_iter = remux_jobs.begin();
335 job_iter != remux_jobs.end();
337 (*job_iter)->Start();
345 for (std::vector<RemuxJob*>::const_iterator job_iter = remux_jobs.begin();
346 job_iter != remux_jobs.end();
348 if ((*job_iter)->HasBeenJoined()) {
349 status = (*job_iter)->status();
357 }
while (!all_joined && status.ok());
362 bool RunPackager(
const StreamDescriptorList& stream_descriptors) {
363 if (!AssignFlagsFromProfile())
366 if (FLAGS_output_media_info && !FLAGS_mpd_output.empty()) {
367 NOTIMPLEMENTED() <<
"ERROR: --output_media_info and --mpd_output do not "
371 if (FLAGS_output_media_info && !FLAGS_single_segment) {
373 NOTIMPLEMENTED() <<
"ERROR: --output_media_info is only supported if "
374 "--single_segment is true.";
379 MuxerOptions muxer_options;
380 if (!GetMuxerOptions(&muxer_options))
383 MpdOptions mpd_options;
384 if (!GetMpdOptions(&mpd_options))
388 scoped_ptr<KeySource> encryption_key_source;
389 if (FLAGS_enable_widevine_encryption || FLAGS_enable_fixed_key_encryption) {
390 encryption_key_source = CreateEncryptionKeySource();
391 if (!encryption_key_source)
395 scoped_ptr<MpdNotifier> mpd_notifier;
396 if (!FLAGS_mpd_output.empty()) {
397 DashProfile profile =
398 FLAGS_single_segment ? kOnDemandProfile : kLiveProfile;
399 std::vector<std::string> base_urls;
400 base::SplitString(FLAGS_base_urls,
',', &base_urls);
401 if (FLAGS_generate_dash_if_iop_compliant_mpd) {
402 mpd_notifier.reset(
new DashIopMpdNotifier(profile, mpd_options, base_urls,
405 mpd_notifier.reset(
new SimpleMpdNotifier(profile, mpd_options, base_urls,
408 if (!mpd_notifier->Init()) {
409 LOG(ERROR) <<
"MpdNotifier failed to initialize.";
414 std::vector<RemuxJob*> remux_jobs;
415 STLElementDeleter<std::vector<RemuxJob*> > scoped_jobs_deleter(&remux_jobs);
416 FakeClock fake_clock;
417 if (!CreateRemuxJobs(stream_descriptors, muxer_options, &fake_clock,
418 encryption_key_source.get(), mpd_notifier.get(),
423 Status status = RunRemuxJobs(remux_jobs);
425 LOG(ERROR) <<
"Packaging Error: " << status.ToString();
429 printf(
"Packaging completed successfully.\n");
433 int PackagerMain(
int argc,
char** argv) {
434 base::AtExitManager exit;
436 base::CommandLine::Init(argc, argv);
437 CHECK(logging::InitLogging(logging::LoggingSettings()));
439 google::SetUsageMessage(base::StringPrintf(kUsage, argv[0]));
440 google::ParseCommandLineFlags(&argc, &argv,
true);
442 std::string version_string =
443 base::StringPrintf(
"edash-packager version %s", kPackagerVersion);
444 google::ShowUsageWithFlags(version_string.c_str());
449 return kArgumentValidationFailed;
454 StreamDescriptorList stream_descriptors;
455 for (
int i = 1; i < argc; ++i) {
456 if (!InsertStreamDescriptor(argv[i], &stream_descriptors))
457 return kArgumentValidationFailed;
459 return RunPackager(stream_descriptors) ? kSuccess : kPackagingFailed;
465 int main(
int argc,
char** argv) {
466 return edash_packager::media::PackagerMain(argc, argv);
bool ValidateFixedCryptoFlags()
bool ValidateWidevineCryptoFlags()