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