DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
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/files/file_path.h"
14 #include "packager/base/files/file_util.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  ~DashIopMpdNotifierFactory() override {}
39 
40  scoped_ptr<MpdNotifier> Create(DashProfile dash_profile,
41  const MpdOptions& mpd_options,
42  const std::vector<std::string>& base_urls,
43  const std::string& output_path) override {
44  return scoped_ptr<MpdNotifier>(new DashIopMpdNotifier(
45  dash_profile, mpd_options, base_urls, output_path));
46  }
47 };
48 
49 // Factory that creates SimpleMpdNotifier instances.
50 class SimpleMpdNotifierFactory : public MpdNotifierFactory {
51  public:
52  SimpleMpdNotifierFactory() {}
53  ~SimpleMpdNotifierFactory() override {}
54 
55  scoped_ptr<MpdNotifier> Create(DashProfile dash_profile,
56  const MpdOptions& mpd_options,
57  const std::vector<std::string>& base_urls,
58  const std::string& output_path) override {
59  return scoped_ptr<MpdNotifier>(new SimpleMpdNotifier(
60  dash_profile, mpd_options, base_urls, output_path));
61  }
62 };
63 
64 } // namespace
65 
66 MpdWriter::MpdWriter()
67  : notifier_factory_(FLAGS_generate_dash_if_iop_compliant_mpd
68  ? static_cast<MpdNotifierFactory*>(
69  new DashIopMpdNotifierFactory())
70  : static_cast<MpdNotifierFactory*>(
71  new SimpleMpdNotifierFactory())) {}
72 MpdWriter::~MpdWriter() {}
73 
74 bool MpdWriter::AddFile(const std::string& media_info_path,
75  const std::string& mpd_path) {
76  std::string file_content;
77  if (!media::File::ReadFileToString(media_info_path.c_str(),
78  &file_content)) {
79  LOG(ERROR) << "Failed to read " << media_info_path << " to string.";
80  return false;
81  }
82 
83  MediaInfo media_info;
84  if (!::google::protobuf::TextFormat::ParseFromString(file_content,
85  &media_info)) {
86  LOG(ERROR) << "Failed to parse " << file_content << " to MediaInfo.";
87  return false;
88  }
89 
90  MpdBuilder::MakePathsRelativeToMpd(mpd_path, &media_info);
91  media_infos_.push_back(media_info);
92  return true;
93 }
94 
95 void MpdWriter::AddBaseUrl(const std::string& base_url) {
96  base_urls_.push_back(base_url);
97 }
98 
99 bool MpdWriter::WriteMpdToFile(const char* file_name) {
100  CHECK(file_name);
101  scoped_ptr<MpdNotifier> notifier = notifier_factory_->Create(
102  kOnDemandProfile, MpdOptions(), base_urls_, file_name);
103  if (!notifier->Init()) {
104  LOG(ERROR) << "failed to initialize MpdNotifier.";
105  return false;
106  }
107 
108  for (std::list<MediaInfo>::const_iterator it = media_infos_.begin();
109  it != media_infos_.end();
110  ++it) {
111  uint32_t unused_conatiner_id;
112  if (!notifier->NotifyNewContainer(*it, &unused_conatiner_id)) {
113  LOG(ERROR) << "Failed to add MediaInfo for media file: "
114  << it->media_file_name();
115  return false;
116  }
117  }
118 
119  if (!notifier->Flush()) {
120  LOG(ERROR) << "Failed to flush MPD notifier.";
121  return false;
122  }
123  return true;
124 }
125 
126 void MpdWriter::SetMpdNotifierFactoryForTest(
127  scoped_ptr<MpdNotifierFactory> factory) {
128  notifier_factory_ = factory.Pass();
129 }
130 
131 } // namespace edash_packager
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:184
Define an abstract file interface.
Definition: file.h:23
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)
Definition: mpd_builder.cc:641