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