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