DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
mpd_generator.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 "packager/app/mpd_generator_flags.h"
8 #include "packager/app/vlog_flags.h"
9 #include "packager/base/at_exit.h"
10 #include "packager/base/command_line.h"
11 #include "packager/base/logging.h"
12 #include "packager/base/strings/string_split.h"
13 #include "packager/base/strings/stringprintf.h"
14 #include "packager/mpd/util/mpd_writer.h"
15 #include "packager/version/version.h"
16 
17 #if defined(OS_WIN)
18 #include <codecvt>
19 #include <functional>
20 #include <locale>
21 #endif // defined(OS_WIN)
22 
23 namespace shaka {
24 namespace {
25 const char kUsage[] =
26  "MPD generation driver program.\n"
27  "This program accepts MediaInfo files in human readable text "
28  "format and outputs an MPD.\n"
29  "The main use case for this is to output MPD for VOD.\n"
30  "Limitations:\n"
31  " Each MediaInfo can only have one of VideoInfo, AudioInfo, or TextInfo.\n"
32  " There will be at most 3 AdaptationSets in the MPD, i.e. 1 video, 1 "
33  "audio, and 1 text.\n"
34  "Sample Usage:\n"
35  "%s --input=\"video1.media_info,video2.media_info,audio1.media_info\" "
36  "--output=\"video_audio.mpd\"";
37 
38 enum ExitStatus {
39  kSuccess = 0,
40  kEmptyInputError,
41  kEmptyOutputError,
42  kFailedToWriteMpdToFileError
43 };
44 
45 ExitStatus CheckRequiredFlags() {
46  if (FLAGS_input.empty()) {
47  LOG(ERROR) << "--input is required.";
48  return kEmptyInputError;
49  }
50 
51  if (FLAGS_output.empty()) {
52  LOG(ERROR) << "--output is required.";
53  return kEmptyOutputError;
54  }
55 
56  return kSuccess;
57 }
58 
59 ExitStatus RunMpdGenerator() {
60  DCHECK_EQ(CheckRequiredFlags(), kSuccess);
61  std::vector<std::string> base_urls;
62  std::vector<std::string> input_files;
63  typedef std::vector<std::string>::const_iterator Iterator;
64 
65  base::SplitString(FLAGS_input, ',', &input_files);
66 
67  if (!FLAGS_base_urls.empty()) {
68  base::SplitString(FLAGS_base_urls, ',', &base_urls);
69  }
70 
71  MpdWriter mpd_writer;
72  for (Iterator it = base_urls.begin(); it != base_urls.end(); ++it)
73  mpd_writer.AddBaseUrl(*it);
74 
75  for (Iterator it = input_files.begin(); it != input_files.end(); ++it) {
76  if (!mpd_writer.AddFile(it->c_str(), FLAGS_output)) {
77  LOG(WARNING) << "MpdWriter failed to read " << *it << ", skipping.";
78  }
79  }
80 
81  if (!mpd_writer.WriteMpdToFile(FLAGS_output.c_str())) {
82  LOG(ERROR) << "Failed to write MPD to " << FLAGS_output;
83  return kFailedToWriteMpdToFileError;
84  }
85 
86  return kSuccess;
87 }
88 
89 int MpdMain(int argc, char** argv) {
90  base::AtExitManager exit;
91  // Needed to enable VLOG/DVLOG through --vmodule or --v.
92  base::CommandLine::Init(argc, argv);
93  CHECK(logging::InitLogging(logging::LoggingSettings()));
94 
95  google::SetUsageMessage(base::StringPrintf(kUsage, argv[0]));
96  google::ParseCommandLineFlags(&argc, &argv, true);
97 
98  ExitStatus status = CheckRequiredFlags();
99  if (status != kSuccess) {
100  const std::string version_string = base::StringPrintf(
101  "mpd_generator version %s", GetPackagerVersion().c_str());
102  google::ShowUsageWithFlags(version_string.c_str());
103  return status;
104  }
105 
106  return RunMpdGenerator();
107 }
108 
109 } // namespace
110 } // namespace shaka
111 
112 #if defined(OS_WIN)
113 // Windows wmain, which converts wide character arguments to UTF-8.
114 int wmain(int argc, wchar_t* argv[], wchar_t* envp[]) {
115  std::unique_ptr<char* [], std::function<void(char**)>> utf8_argv(
116  new char*[argc], [argc](char** utf8_args) {
117  // TODO(tinskip): This leaks, but if this code is enabled, it crashes.
118  // Figure out why. I suspect gflags does something funny with the
119  // argument array.
120  // for (int idx = 0; idx < argc; ++idx)
121  // delete[] utf8_args[idx];
122  delete[] utf8_args;
123  });
124  std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
125  for (int idx = 0; idx < argc; ++idx) {
126  std::string utf8_arg(converter.to_bytes(argv[idx]));
127  utf8_arg += '\0';
128  utf8_argv[idx] = new char[utf8_arg.size()];
129  memcpy(utf8_argv[idx], &utf8_arg[0], utf8_arg.size());
130  }
131  return shaka::MpdMain(argc, utf8_argv.get());
132 }
133 #else
134 int main(int argc, char** argv) {
135  return shaka::MpdMain(argc, argv);
136 }
137 #endif // !defined(OS_WIN)