DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
packager_main.cc
1 // Copyright 2014 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 <gflags/gflags.h>
8 #include <iostream>
9 
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/playready_key_encryption_flags.h"
17 #include "packager/app/stream_descriptor.h"
18 #include "packager/app/vlog_flags.h"
19 #include "packager/app/widevine_encryption_flags.h"
20 #include "packager/base/at_exit.h"
21 #include "packager/base/command_line.h"
22 #include "packager/base/files/file_path.h"
23 #include "packager/base/logging.h"
24 #include "packager/base/path_service.h"
25 #include "packager/base/strings/string_split.h"
26 #include "packager/base/strings/stringprintf.h"
27 #include "packager/base/threading/simple_thread.h"
28 #include "packager/base/time/clock.h"
29 #include "packager/hls/base/hls_notifier.h"
30 #include "packager/hls/base/simple_hls_notifier.h"
31 #include "packager/media/base/container_names.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/chunking/chunking_handler.h"
37 #include "packager/media/crypto/encryption_handler.h"
38 #include "packager/media/demuxer/demuxer.h"
39 #include "packager/media/event/hls_notify_muxer_listener.h"
40 #include "packager/media/event/mpd_notify_muxer_listener.h"
41 #include "packager/media/event/vod_media_info_dump_muxer_listener.h"
42 #include "packager/media/file/file.h"
43 #include "packager/media/formats/mp2t/ts_muxer.h"
44 #include "packager/media/formats/mp4/mp4_muxer.h"
45 #include "packager/media/formats/webm/webm_muxer.h"
46 #include "packager/media/trick_play/trick_play_handler.h"
47 #include "packager/mpd/base/dash_iop_mpd_notifier.h"
48 #include "packager/mpd/base/media_info.pb.h"
49 #include "packager/mpd/base/mpd_builder.h"
50 #include "packager/mpd/base/simple_mpd_notifier.h"
51 #include "packager/version/version.h"
52 
53 #if defined(OS_WIN)
54 #include <codecvt>
55 #include <functional>
56 #include <locale>
57 #endif // defined(OS_WIN)
58 
59 DEFINE_bool(use_fake_clock_for_muxer,
60  false,
61  "Set to true to use a fake clock for muxer. With this flag set, "
62  "creation time and modification time in outputs are set to 0. "
63  "Should only be used for testing.");
64 DEFINE_bool(override_version,
65  false,
66  "Override packager version in the generated outputs with "
67  "--test_version if it is set to true. Should be used for "
68  "testing only.");
69 DEFINE_string(test_version,
70  "",
71  "Packager version for testing. Ignored if --override_version is "
72  "false. Should be used for testing only.");
73 
74 namespace shaka {
75 namespace media {
76 namespace {
77 
78 const char kUsage[] =
79  "%s [flags] <stream_descriptor> ...\n\n"
80  " stream_descriptor consists of comma separated field_name/value pairs:\n"
81  " field_name=value,[field_name=value,]...\n"
82  " Supported field names are as follows (names in parenthesis are alias):\n"
83  " - input (in): Required input/source media file path or network stream\n"
84  " URL.\n"
85  " - stream_selector (stream): Required field with value 'audio',\n"
86  " 'video', 'text', or stream number (zero based).\n"
87  " - output (out,init_segment): Required output file (single file) or\n"
88  " initialization file path (multiple file).\n"
89  " - segment_template (segment): Optional value which specifies the\n"
90  " naming pattern for the segment files, and that the stream should be\n"
91  " split into multiple files. Its presence should be consistent across\n"
92  " streams.\n"
93  " - bandwidth (bw): Optional value which contains a user-specified\n"
94  " content bit rate for the stream, in bits/sec. If specified, this\n"
95  " value is propagated to the $Bandwidth$ template parameter for\n"
96  " segment names. If not specified, its value may be estimated.\n"
97  " - language (lang): Optional value which contains a user-specified\n"
98  " language tag. If specified, this value overrides any language\n"
99  " metadata in the input track.\n"
100  " - output_format (format): Optional value which specifies the format\n"
101  " of the output files (MP4 or WebM). If not specified, it will be\n"
102  " derived from the file extension of the output file.\n"
103  " - hls_name: Required for audio when outputting HLS.\n"
104  " name of the output stream. This is not (necessarily) the same as\n"
105  " output. This is used as the NAME attribute for EXT-X-MEDIA\n"
106  " - hls_group_id: Required for audio when outputting HLS.\n"
107  " The group ID for the output stream. For HLS this is used as the\n"
108  " GROUP-ID attribute for EXT-X-MEDIA.\n"
109  " - playlist_name: Required for HLS output.\n"
110  " Name of the playlist for the stream. Usually ends with '.m3u8'.\n"
111  " - skip_encryption=0|1: Optional. Defaults to 0 if not specified. If\n"
112  " it is set to 1, no encryption of the stream will be made.\n";
113 
114 const char kMediaInfoSuffix[] = ".media_info";
115 
116 enum ExitStatus {
117  kSuccess = 0,
118  kArgumentValidationFailed,
119  kPackagingFailed,
120  kInternalError,
121 };
122 
123 // TODO(rkuroiwa): Write TTML and WebVTT parser (demuxing) for a better check
124 // and for supporting live/segmenting (muxing). With a demuxer and a muxer,
125 // CreateRemuxJobs() shouldn't treat text as a special case.
126 std::string DetermineTextFileFormat(const std::string& file) {
127  std::string content;
128  if (!File::ReadFileToString(file.c_str(), &content)) {
129  LOG(ERROR) << "Failed to open file " << file
130  << " to determine file format.";
131  return "";
132  }
133  MediaContainerName container_name = DetermineContainer(
134  reinterpret_cast<const uint8_t*>(content.data()), content.size());
135  if (container_name == CONTAINER_WEBVTT) {
136  return "vtt";
137  } else if (container_name == CONTAINER_TTML) {
138  return "ttml";
139  }
140 
141  return "";
142 }
143 
144 } // namespace
145 
146 // A fake clock that always return time 0 (epoch). Should only be used for
147 // testing.
148 class FakeClock : public base::Clock {
149  public:
150  base::Time Now() override { return base::Time(); }
151 };
152 
153 // Demux, Mux(es) and worker thread used to remux a source file/stream.
154 class RemuxJob : public base::SimpleThread {
155  public:
156  RemuxJob(std::unique_ptr<Demuxer> demuxer)
157  : SimpleThread("RemuxJob"), demuxer_(std::move(demuxer)) {}
158 
159  ~RemuxJob() override {}
160 
161  Demuxer* demuxer() { return demuxer_.get(); }
162  Status status() { return status_; }
163 
164  private:
165  void Run() override {
166  DCHECK(demuxer_);
167  status_ = demuxer_->Run();
168  }
169 
170  std::unique_ptr<Demuxer> demuxer_;
171  Status status_;
172 
173  DISALLOW_COPY_AND_ASSIGN(RemuxJob);
174 };
175 
176 bool StreamInfoToTextMediaInfo(const StreamDescriptor& stream_descriptor,
177  const MuxerOptions& stream_muxer_options,
178  MediaInfo* text_media_info) {
179  const std::string& language = stream_descriptor.language;
180  std::string format = DetermineTextFileFormat(stream_descriptor.input);
181  if (format.empty()) {
182  LOG(ERROR) << "Failed to determine the text file format for "
183  << stream_descriptor.input;
184  return false;
185  }
186 
187  if (!File::Copy(stream_descriptor.input.c_str(),
188  stream_muxer_options.output_file_name.c_str())) {
189  LOG(ERROR) << "Failed to copy the input file (" << stream_descriptor.input
190  << ") to output file (" << stream_muxer_options.output_file_name
191  << ").";
192  return false;
193  }
194 
195  text_media_info->set_media_file_name(stream_muxer_options.output_file_name);
196  text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
197 
198  if (stream_muxer_options.bandwidth != 0) {
199  text_media_info->set_bandwidth(stream_muxer_options.bandwidth);
200  } else {
201  // Text files are usually small and since the input is one file; there's no
202  // way for the player to do ranged requests. So set this value to something
203  // reasonable.
204  text_media_info->set_bandwidth(256);
205  }
206 
207  MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
208  text_info->set_format(format);
209  if (!language.empty())
210  text_info->set_language(language);
211 
212  return true;
213 }
214 
215 std::shared_ptr<Muxer> CreateOutputMuxer(const MuxerOptions& options,
216  MediaContainerName container) {
217  if (container == CONTAINER_WEBM) {
218  return std::shared_ptr<Muxer>(new webm::WebMMuxer(options));
219  } else if (container == CONTAINER_MPEG2TS) {
220  return std::shared_ptr<Muxer>(new mp2t::TsMuxer(options));
221  } else {
222  DCHECK_EQ(container, CONTAINER_MOV);
223  return std::shared_ptr<Muxer>(new mp4::MP4Muxer(options));
224  }
225 }
226 
227 bool CreateRemuxJobs(const StreamDescriptorList& stream_descriptors,
228  const ChunkingOptions& chunking_options,
229  const EncryptionOptions& encryption_options,
230  const MuxerOptions& muxer_options,
231  FakeClock* fake_clock,
232  KeySource* encryption_key_source,
233  MpdNotifier* mpd_notifier,
234  hls::HlsNotifier* hls_notifier,
235  std::vector<std::unique_ptr<RemuxJob>>* remux_jobs) {
236  // No notifiers OR (mpd_notifier XOR hls_notifier); which is NAND.
237  DCHECK(!(mpd_notifier && hls_notifier));
238  DCHECK(remux_jobs);
239 
240  std::shared_ptr<TrickPlayHandler> trick_play_handler;
241 
242  std::string previous_input;
243  std::string previous_stream_selector;
244  int stream_number = 0;
245  for (StreamDescriptorList::const_iterator
246  stream_iter = stream_descriptors.begin();
247  stream_iter != stream_descriptors.end();
248  ++stream_iter, ++stream_number) {
249  // Process stream descriptor.
250  MuxerOptions stream_muxer_options(muxer_options);
251  stream_muxer_options.output_file_name = stream_iter->output;
252  if (!stream_iter->segment_template.empty()) {
253  if (!ValidateSegmentTemplate(stream_iter->segment_template)) {
254  LOG(ERROR) << "ERROR: segment template with '"
255  << stream_iter->segment_template << "' is invalid.";
256  return false;
257  }
258  stream_muxer_options.segment_template = stream_iter->segment_template;
259  }
260  stream_muxer_options.bandwidth = stream_iter->bandwidth;
261 
262  if (stream_iter->stream_selector == "text" &&
263  stream_iter->output_format != CONTAINER_MOV) {
264  MediaInfo text_media_info;
265  if (!StreamInfoToTextMediaInfo(*stream_iter, stream_muxer_options,
266  &text_media_info)) {
267  return false;
268  }
269 
270  if (mpd_notifier) {
271  uint32_t unused;
272  if (!mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
273  LOG(ERROR) << "Failed to process text file " << stream_iter->input;
274  } else {
275  mpd_notifier->Flush();
276  }
277  } else if (FLAGS_output_media_info) {
279  text_media_info,
280  stream_muxer_options.output_file_name + kMediaInfoSuffix);
281  } else {
282  NOTIMPLEMENTED()
283  << "--mpd_output or --output_media_info flags are "
284  "required for text output. Skipping manifest related output for "
285  << stream_iter->input;
286  }
287  continue;
288  }
289 
290  if (stream_iter->input != previous_input) {
291  // New remux job needed. Create demux and job thread.
292  std::unique_ptr<Demuxer> demuxer(new Demuxer(stream_iter->input));
293  demuxer->set_dump_stream_info(FLAGS_dump_stream_info);
294  if (FLAGS_enable_widevine_decryption ||
295  FLAGS_enable_fixed_key_decryption) {
296  std::unique_ptr<KeySource> decryption_key_source(
297  CreateDecryptionKeySource());
298  if (!decryption_key_source)
299  return false;
300  demuxer->SetKeySource(std::move(decryption_key_source));
301  }
302  remux_jobs->emplace_back(new RemuxJob(std::move(demuxer)));
303  trick_play_handler.reset();
304  previous_input = stream_iter->input;
305  // Skip setting up muxers if output is not needed.
306  if (stream_iter->output.empty() && stream_iter->segment_template.empty())
307  continue;
308  }
309  DCHECK(!remux_jobs->empty());
310 
311  // Each stream selector requires an individual trick play handler.
312  // E.g., an input with two video streams needs two trick play handlers.
313  // TODO(hmchen): add a test case in packager_test.py for two video streams
314  // input.
315  if (stream_iter->stream_selector != previous_stream_selector) {
316  previous_stream_selector = stream_iter->stream_selector;
317  trick_play_handler.reset();
318  }
319 
320  std::shared_ptr<Muxer> muxer(
321  CreateOutputMuxer(stream_muxer_options, stream_iter->output_format));
322  if (FLAGS_use_fake_clock_for_muxer) muxer->set_clock(fake_clock);
323 
324  std::unique_ptr<MuxerListener> muxer_listener;
325  DCHECK(!(FLAGS_output_media_info && mpd_notifier));
326  if (FLAGS_output_media_info) {
327  const std::string output_media_info_file_name =
328  stream_muxer_options.output_file_name + kMediaInfoSuffix;
329  std::unique_ptr<VodMediaInfoDumpMuxerListener>
330  vod_media_info_dump_muxer_listener(
331  new VodMediaInfoDumpMuxerListener(output_media_info_file_name));
332  muxer_listener = std::move(vod_media_info_dump_muxer_listener);
333  }
334  if (mpd_notifier) {
335  std::unique_ptr<MpdNotifyMuxerListener> mpd_notify_muxer_listener(
336  new MpdNotifyMuxerListener(mpd_notifier));
337  muxer_listener = std::move(mpd_notify_muxer_listener);
338  }
339 
340  if (hls_notifier) {
341  // TODO(rkuroiwa): Do some smart stuff to group the audios, e.g. detect
342  // languages.
343  std::string group_id = stream_iter->hls_group_id;
344  std::string name = stream_iter->hls_name;
345  std::string hls_playlist_name = stream_iter->hls_playlist_name;
346  if (group_id.empty())
347  group_id = "audio";
348  if (name.empty())
349  name = base::StringPrintf("stream_%d", stream_number);
350  if (hls_playlist_name.empty())
351  hls_playlist_name = base::StringPrintf("stream_%d.m3u8", stream_number);
352 
353  muxer_listener.reset(new HlsNotifyMuxerListener(hls_playlist_name, name,
354  group_id, hls_notifier));
355  }
356 
357  if (muxer_listener)
358  muxer->SetMuxerListener(std::move(muxer_listener));
359 
360  // Create a new trick_play_handler. Note that the stream_decriptors
361  // are sorted so that for the same input and stream_selector, the main
362  // stream is always the last one following the trick play streams.
363  if (stream_iter->trick_play_factor > 0) {
364  if (!trick_play_handler) {
365  trick_play_handler.reset(new TrickPlayHandler());
366  }
367  trick_play_handler->SetHandlerForTrickPlay(stream_iter->trick_play_factor,
368  std::move(muxer));
369  if (trick_play_handler->IsConnected())
370  continue;
371  } else if (trick_play_handler) {
372  trick_play_handler->SetHandlerForMainStream(std::move(muxer));
373  DCHECK(trick_play_handler->IsConnected());
374  continue;
375  }
376 
377  std::vector<std::shared_ptr<MediaHandler>> handlers;
378 
379  auto chunking_handler = std::make_shared<ChunkingHandler>(chunking_options);
380  handlers.push_back(chunking_handler);
381 
382  Status status;
383  if (encryption_key_source && !stream_iter->skip_encryption) {
384  auto new_encryption_options = encryption_options;
385  // Use Sample AES in MPEG2TS.
386  // TODO(kqyang): Consider adding a new flag to enable Sample AES as we
387  // will support CENC in TS in the future.
388  if (stream_iter->output_format == CONTAINER_MPEG2TS) {
389  LOG(INFO) << "Use Apple Sample AES encryption for MPEG2TS.";
390  new_encryption_options.protection_scheme =
391  kAppleSampleAesProtectionScheme;
392  }
393  handlers.emplace_back(
394  new EncryptionHandler(new_encryption_options, encryption_key_source));
395  }
396 
397  // If trick_play_handler is available, muxer should already be connected to
398  // trick_play_handler.
399  if (trick_play_handler) {
400  handlers.push_back(trick_play_handler);
401  } else {
402  handlers.push_back(std::move(muxer));
403  }
404 
405  auto* demuxer = remux_jobs->back()->demuxer();
406  const std::string& stream_selector = stream_iter->stream_selector;
407  status.Update(demuxer->SetHandler(stream_selector, chunking_handler));
408  status.Update(ConnectHandlers(handlers));
409 
410  if (!status.ok()) {
411  LOG(ERROR) << "Failed to setup graph: " << status;
412  return false;
413  }
414  if (!stream_iter->language.empty())
415  demuxer->SetLanguageOverride(stream_selector, stream_iter->language);
416  }
417 
418  // Initialize processing graph.
419  for (const std::unique_ptr<RemuxJob>& job : *remux_jobs) {
420  Status status = job->demuxer()->Initialize();
421  if (!status.ok()) {
422  LOG(ERROR) << "Failed to initialize processing graph " << status;
423  return false;
424  }
425  }
426  return true;
427 }
428 
429 Status RunRemuxJobs(const std::vector<std::unique_ptr<RemuxJob>>& remux_jobs) {
430  // Start the job threads.
431  for (const std::unique_ptr<RemuxJob>& job : remux_jobs)
432  job->Start();
433 
434  // Wait for all jobs to complete or an error occurs.
435  Status status;
436  bool all_joined;
437  do {
438  all_joined = true;
439  for (const std::unique_ptr<RemuxJob>& job : remux_jobs) {
440  if (job->HasBeenJoined()) {
441  status = job->status();
442  if (!status.ok())
443  break;
444  } else {
445  all_joined = false;
446  job->Join();
447  }
448  }
449  } while (!all_joined && status.ok());
450 
451  return status;
452 }
453 
454 bool RunPackager(const StreamDescriptorList& stream_descriptors) {
455  if (FLAGS_output_media_info && !FLAGS_mpd_output.empty()) {
456  NOTIMPLEMENTED() << "ERROR: --output_media_info and --mpd_output do not "
457  "work together.";
458  return false;
459  }
460 
461  // Since there isn't a muxer listener that can output both MPD and HLS,
462  // disallow specifying both MPD and HLS flags.
463  if (!FLAGS_mpd_output.empty() && !FLAGS_hls_master_playlist_output.empty()) {
464  LOG(ERROR) << "Cannot output both MPD and HLS.";
465  return false;
466  }
467 
468  ChunkingOptions chunking_options = GetChunkingOptions();
469  EncryptionOptions encryption_options = GetEncryptionOptions();
470 
471  MuxerOptions muxer_options = GetMuxerOptions();
472 
473  DCHECK(!stream_descriptors.empty());
474  // On demand profile generates single file segment while live profile
475  // generates multiple segments specified using segment template.
476  const bool on_demand_dash_profile =
477  stream_descriptors.begin()->segment_template.empty();
478  for (const auto& stream_descriptor : stream_descriptors) {
479  if (on_demand_dash_profile != stream_descriptor.segment_template.empty()) {
480  LOG(ERROR) << "Inconsistent stream descriptor specification: "
481  "segment_template should be specified for none or all "
482  "stream descriptors.";
483  return false;
484  }
485  }
486  if (FLAGS_output_media_info && !on_demand_dash_profile) {
487  // TODO(rkuroiwa, kqyang): Support partial media info dump for live.
488  NOTIMPLEMENTED() << "ERROR: --output_media_info is only supported for "
489  "on-demand profile (not using segment_template).";
490  return false;
491  }
492 
493  MpdOptions mpd_options = GetMpdOptions(on_demand_dash_profile);
494 
495  // Create encryption key source if needed.
496  std::unique_ptr<KeySource> encryption_key_source;
497  if (FLAGS_enable_widevine_encryption || FLAGS_enable_fixed_key_encryption ||
498  FLAGS_enable_playready_encryption) {
499  if (encryption_options.protection_scheme == FOURCC_NULL)
500  return false;
501  encryption_key_source =
502  CreateEncryptionKeySource(encryption_options.protection_scheme);
503  if (!encryption_key_source)
504  return false;
505  }
506 
507  std::unique_ptr<MpdNotifier> mpd_notifier;
508  if (!FLAGS_mpd_output.empty()) {
509  std::vector<std::string> base_urls = base::SplitString(
510  FLAGS_base_urls, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
511  if (FLAGS_generate_dash_if_iop_compliant_mpd) {
512  mpd_notifier.reset(
513  new DashIopMpdNotifier(mpd_options, base_urls, FLAGS_mpd_output));
514  } else {
515  mpd_notifier.reset(
516  new SimpleMpdNotifier(mpd_options, base_urls, FLAGS_mpd_output));
517  }
518  if (!mpd_notifier->Init()) {
519  LOG(ERROR) << "MpdNotifier failed to initialize.";
520  return false;
521  }
522  }
523 
524  std::unique_ptr<hls::HlsNotifier> hls_notifier;
525  if (!FLAGS_hls_master_playlist_output.empty()) {
526  base::FilePath master_playlist_path(
527  base::FilePath::FromUTF8Unsafe(FLAGS_hls_master_playlist_output));
528  base::FilePath master_playlist_name = master_playlist_path.BaseName();
529 
530  hls_notifier.reset(new hls::SimpleHlsNotifier(
531  hls::HlsNotifier::HlsProfile::kOnDemandProfile, FLAGS_hls_base_url,
532  master_playlist_path.DirName().AsEndingWithSeparator().AsUTF8Unsafe(),
533  master_playlist_name.AsUTF8Unsafe()));
534  }
535 
536  std::vector<std::unique_ptr<RemuxJob>> remux_jobs;
537  FakeClock fake_clock;
538  if (!CreateRemuxJobs(stream_descriptors, chunking_options, encryption_options,
539  muxer_options, &fake_clock, encryption_key_source.get(),
540  mpd_notifier.get(), hls_notifier.get(), &remux_jobs)) {
541  return false;
542  }
543 
544  Status status = RunRemuxJobs(remux_jobs);
545  if (!status.ok()) {
546  LOG(ERROR) << "Packaging Error: " << status.ToString();
547  return false;
548  }
549 
550  if (hls_notifier) {
551  if (!hls_notifier->Flush())
552  return false;
553  }
554  if (mpd_notifier) {
555  if (!mpd_notifier->Flush())
556  return false;
557  }
558 
559  printf("Packaging completed successfully.\n");
560  return true;
561 }
562 
563 int PackagerMain(int argc, char** argv) {
564  base::AtExitManager exit;
565  // Needed to enable VLOG/DVLOG through --vmodule or --v.
566  base::CommandLine::Init(argc, argv);
567 
568  // Set up logging.
569  logging::LoggingSettings log_settings;
570  log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
571  CHECK(logging::InitLogging(log_settings));
572 
573  google::SetVersionString(GetPackagerVersion());
574  google::SetUsageMessage(base::StringPrintf(kUsage, argv[0]));
575  google::ParseCommandLineFlags(&argc, &argv, true);
576  if (argc < 2) {
577  google::ShowUsageWithFlags("Usage");
578  return kSuccess;
579  }
580 
583  return kArgumentValidationFailed;
584  }
585 
586  if (FLAGS_override_version)
587  SetPackagerVersionForTesting(FLAGS_test_version);
588 
589  LibcryptoThreading libcrypto_threading;
590  // TODO(tinskip): Make InsertStreamDescriptor a member of
591  // StreamDescriptorList.
592  StreamDescriptorList stream_descriptors;
593  for (int i = 1; i < argc; ++i) {
594  if (!InsertStreamDescriptor(argv[i], &stream_descriptors))
595  return kArgumentValidationFailed;
596  }
597  return RunPackager(stream_descriptors) ? kSuccess : kPackagingFailed;
598 }
599 
600 } // namespace media
601 } // namespace shaka
602 
603 #if defined(OS_WIN)
604 // Windows wmain, which converts wide character arguments to UTF-8.
605 int wmain(int argc, wchar_t* argv[], wchar_t* envp[]) {
606  std::unique_ptr<char* [], std::function<void(char**)>> utf8_argv(
607  new char*[argc], [argc](char** utf8_args) {
608  // TODO(tinskip): This leaks, but if this code is enabled, it crashes.
609  // Figure out why. I suspect gflags does something funny with the
610  // argument array.
611  // for (int idx = 0; idx < argc; ++idx)
612  // delete[] utf8_args[idx];
613  delete[] utf8_args;
614  });
615  std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
616  for (int idx = 0; idx < argc; ++idx) {
617  std::string utf8_arg(converter.to_bytes(argv[idx]));
618  utf8_arg += '\0';
619  utf8_argv[idx] = new char[utf8_arg.size()];
620  memcpy(utf8_argv[idx], &utf8_arg[0], utf8_arg.size());
621  }
622  return shaka::media::PackagerMain(argc, utf8_argv.get());
623 }
624 #else
625 int main(int argc, char** argv) {
626  return shaka::media::PackagerMain(argc, argv);
627 }
628 #endif // defined(OS_WIN)
static bool Copy(const char *from_file_name, const char *to_file_name)
Definition: file.cc:203
bool ValidateWidevineCryptoFlags()
static bool WriteMediaInfoToFile(const MediaInfo &media_info, const std::string &output_file_path)
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:185
bool ValidateFixedCryptoFlags()