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