DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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/command_line.h"
10 #include "packager/base/logging.h"
11 #include "packager/base/strings/string_split.h"
12 #include "packager/base/strings/stringprintf.h"
13 #include "packager/mpd/util/mpd_writer.h"
14 #include "packager/version/version.h"
15 
16 namespace edash_packager {
17 namespace {
18 const char kUsage[] =
19  "MPD generation driver program.\n"
20  "This program accepts MediaInfo files in human readable text "
21  "format and outputs an MPD.\n"
22  "The main use case for this is to output MPD for VOD.\n"
23  "Limitations:\n"
24  " Each MediaInfo can only have one of VideoInfo, AudioInfo, or TextInfo.\n"
25  " There will be at most 3 AdaptationSets in the MPD, i.e. 1 video, 1 "
26  "audio, and 1 text.\n"
27  "Sample Usage:\n"
28  "%s --input=\"video1.media_info,video2.media_info,audio1.media_info\" "
29  "--output=\"video_audio.mpd\"";
30 
31 enum ExitStatus {
32  kSuccess = 0,
33  kEmptyInputError,
34  kEmptyOutputError,
35  kFailedToWriteMpdToFileError
36 };
37 
38 ExitStatus CheckRequiredFlags() {
39  if (FLAGS_input.empty()) {
40  LOG(ERROR) << "--input is required.";
41  return kEmptyInputError;
42  }
43 
44  if (FLAGS_output.empty()) {
45  LOG(ERROR) << "--output is required.";
46  return kEmptyOutputError;
47  }
48 
49  return kSuccess;
50 }
51 
52 ExitStatus RunMpdGenerator() {
53  DCHECK_EQ(CheckRequiredFlags(), kSuccess);
54  std::vector<std::string> base_urls;
55  std::vector<std::string> input_files;
56  typedef std::vector<std::string>::const_iterator Iterator;
57 
58  base::SplitString(FLAGS_input, ',', &input_files);
59 
60  if (!FLAGS_base_urls.empty()) {
61  base::SplitString(FLAGS_base_urls, ',', &base_urls);
62  }
63 
64  edash_packager::MpdWriter mpd_writer;
65  for (Iterator it = base_urls.begin(); it != base_urls.end(); ++it)
66  mpd_writer.AddBaseUrl(*it);
67 
68  for (Iterator it = input_files.begin(); it != input_files.end(); ++it) {
69  if (!mpd_writer.AddFile(it->c_str(), FLAGS_output)) {
70  LOG(WARNING) << "MpdWriter failed to read " << *it << ", skipping.";
71  }
72  }
73 
74  if (!mpd_writer.WriteMpdToFile(FLAGS_output.c_str())) {
75  LOG(ERROR) << "Failed to write MPD to " << FLAGS_output;
76  return kFailedToWriteMpdToFileError;
77  }
78 
79  return kSuccess;
80 }
81 
82 int MpdMain(int argc, char** argv) {
83  // Needed to enable VLOG/DVLOG through --vmodule or --v.
84  base::CommandLine::Init(argc, argv);
85  CHECK(logging::InitLogging(logging::LoggingSettings()));
86 
87  google::SetUsageMessage(base::StringPrintf(kUsage, argv[0]));
88  google::ParseCommandLineFlags(&argc, &argv, true);
89 
90  ExitStatus status = CheckRequiredFlags();
91  if (status != kSuccess) {
92  std::string version_string =
93  base::StringPrintf("mpd_generator version %s", kPackagerVersion);
94  google::ShowUsageWithFlags(version_string.c_str());
95  return status;
96  }
97 
98  return RunMpdGenerator();
99 }
100 
101 } // namespace
102 } // namespace edash_packager
103 
104 int main(int argc, char** argv) {
105  return edash_packager::MpdMain(argc, argv);
106 }