DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
mpd_writer.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/mpd/util/mpd_writer.h"
8 
9 #include <gflags/gflags.h>
10 #include <google/protobuf/text_format.h>
11 #include <stdint.h>
12 
13 #include "packager/base/file_util.h"
14 #include "packager/base/files/file_path.h"
15 #include "packager/media/file/file.h"
16 #include "packager/mpd/base/dash_iop_mpd_notifier.h"
17 #include "packager/mpd/base/mpd_builder.h"
18 #include "packager/mpd/base/mpd_notifier.h"
19 #include "packager/mpd/base/mpd_utils.h"
20 #include "packager/mpd/base/simple_mpd_notifier.h"
21 
22 DEFINE_bool(generate_dash_if_iop_compliant_mpd,
23  false,
24  "Try to generate DASH-IF IOPv3 compliant MPD. This is best effort "
25  "and does not guarantee compliance. Off by default until players "
26  "support IOP MPDs.");
27 
29 
30 namespace edash_packager {
31 
32 namespace {
33 
34 // Factory that creates DashIopMpdNotifier instances.
35 class DashIopMpdNotifierFactory : public MpdNotifierFactory {
36  public:
37  DashIopMpdNotifierFactory() {}
38  virtual ~DashIopMpdNotifierFactory() OVERRIDE {}
39 
40  virtual scoped_ptr<MpdNotifier> Create(
41  DashProfile dash_profile,
42  const MpdOptions& mpd_options,
43  const std::vector<std::string>& base_urls,
44  const std::string& output_path) OVERRIDE {
45  return scoped_ptr<MpdNotifier>(new DashIopMpdNotifier(
46  dash_profile, mpd_options, base_urls, output_path));
47  }
48 };
49 
50 // Factory that creates SimpleMpdNotifier instances.
51 class SimpleMpdNotifierFactory : public MpdNotifierFactory {
52  public:
53  SimpleMpdNotifierFactory() {}
54  virtual ~SimpleMpdNotifierFactory() OVERRIDE {}
55 
56  virtual scoped_ptr<MpdNotifier> Create(
57  DashProfile dash_profile,
58  const MpdOptions& mpd_options,
59  const std::vector<std::string>& base_urls,
60  const std::string& output_path) OVERRIDE {
61  return scoped_ptr<MpdNotifier>(new SimpleMpdNotifier(
62  dash_profile, mpd_options, base_urls, output_path));
63  }
64 };
65 
66 } // namespace
67 
68 MpdWriter::MpdWriter()
69  : notifier_factory_(FLAGS_generate_dash_if_iop_compliant_mpd
70  ? static_cast<MpdNotifierFactory*>(
71  new DashIopMpdNotifierFactory())
72  : static_cast<MpdNotifierFactory*>(
73  new SimpleMpdNotifierFactory())) {}
74 MpdWriter::~MpdWriter() {}
75 
76 bool MpdWriter::AddFile(const std::string& media_info_path,
77  const std::string& mpd_path) {
78  std::string file_content;
79  if (!media::File::ReadFileToString(media_info_path.c_str(),
80  &file_content)) {
81  LOG(ERROR) << "Failed to read " << media_info_path << " to string.";
82  return false;
83  }
84 
85  MediaInfo media_info;
86  if (!::google::protobuf::TextFormat::ParseFromString(file_content,
87  &media_info)) {
88  LOG(ERROR) << "Failed to parse " << file_content << " to MediaInfo.";
89  return false;
90  }
91 
92  MpdBuilder::MakePathsRelativeToMpd(mpd_path, &media_info);
93  media_infos_.push_back(media_info);
94  return true;
95 }
96 
97 void MpdWriter::AddBaseUrl(const std::string& base_url) {
98  base_urls_.push_back(base_url);
99 }
100 
101 bool MpdWriter::WriteMpdToFile(const char* file_name) {
102  CHECK(file_name);
103  scoped_ptr<MpdNotifier> notifier = notifier_factory_->Create(
104  kOnDemandProfile, MpdOptions(), base_urls_, file_name);
105  if (!notifier->Init()) {
106  LOG(ERROR) << "failed to initialize MpdNotifier.";
107  return false;
108  }
109 
110  for (std::list<MediaInfo>::const_iterator it = media_infos_.begin();
111  it != media_infos_.end();
112  ++it) {
113  uint32_t unused_conatiner_id;
114  if (!notifier->NotifyNewContainer(*it, &unused_conatiner_id)) {
115  LOG(ERROR) << "Failed to add MediaInfo for media file: "
116  << it->media_file_name();
117  return false;
118  }
119  }
120 
121  if (!notifier->Flush()) {
122  LOG(ERROR) << "Failed to flush MPD notifier.";
123  return false;
124  }
125  return true;
126 }
127 
128 void MpdWriter::SetMpdNotifierFactoryForTest(
129  scoped_ptr<MpdNotifierFactory> factory) {
130  notifier_factory_ = factory.Pass();
131 }
132 
133 } // namespace edash_packager
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:160
Define an abstract file interface.
Definition: file.h:22
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)
Definition: mpd_builder.cc:608