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