Shaka Packager SDK
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/mpd_builder.h"
17 #include "packager/mpd/base/mpd_notifier.h"
18 #include "packager/mpd/base/mpd_utils.h"
19 #include "packager/mpd/base/simple_mpd_notifier.h"
20 
21 DEFINE_bool(generate_dash_if_iop_compliant_mpd,
22  false,
23  "Try to generate DASH-IF IOPv3 compliant MPD. This is best effort "
24  "and does not guarantee compliance. Off by default until players "
25  "support IOP MPDs.");
26 
27 namespace shaka {
28 
29 namespace {
30 
31 // Factory that creates SimpleMpdNotifier instances.
32 class SimpleMpdNotifierFactory : public MpdNotifierFactory {
33  public:
34  SimpleMpdNotifierFactory() {}
35  ~SimpleMpdNotifierFactory() override {}
36 
37  std::unique_ptr<MpdNotifier> Create(const MpdOptions& mpd_options) override {
38  return std::unique_ptr<MpdNotifier>(new SimpleMpdNotifier(mpd_options));
39  }
40 };
41 
42 } // namespace
43 
44 MpdWriter::MpdWriter() : notifier_factory_(new SimpleMpdNotifierFactory()) {}
45 MpdWriter::~MpdWriter() {}
46 
47 bool MpdWriter::AddFile(const std::string& media_info_path) {
48  std::string file_content;
49  if (!File::ReadFileToString(media_info_path.c_str(), &file_content)) {
50  LOG(ERROR) << "Failed to read " << media_info_path << " to string.";
51  return false;
52  }
53 
54  MediaInfo media_info;
55  if (!::google::protobuf::TextFormat::ParseFromString(file_content,
56  &media_info)) {
57  LOG(ERROR) << "Failed to parse " << file_content << " to MediaInfo.";
58  return false;
59  }
60 
61  media_infos_.push_back(media_info);
62  return true;
63 }
64 
65 void MpdWriter::AddBaseUrl(const std::string& base_url) {
66  base_urls_.push_back(base_url);
67 }
68 
69 bool MpdWriter::WriteMpdToFile(const char* file_name) {
70  CHECK(file_name);
71  MpdOptions mpd_options;
72  mpd_options.mpd_params.base_urls = base_urls_;
73  mpd_options.mpd_params.mpd_output = file_name;
74  mpd_options.mpd_params.generate_dash_if_iop_compliant_mpd =
75  FLAGS_generate_dash_if_iop_compliant_mpd;
76  std::unique_ptr<MpdNotifier> notifier =
77  notifier_factory_->Create(mpd_options);
78  if (!notifier->Init()) {
79  LOG(ERROR) << "failed to initialize MpdNotifier.";
80  return false;
81  }
82 
83  for (const MediaInfo& media_info : media_infos_) {
84  uint32_t unused_conatiner_id;
85  if (!notifier->NotifyNewContainer(media_info, &unused_conatiner_id)) {
86  LOG(ERROR) << "Failed to add MediaInfo for media file: "
87  << media_info.media_file_name();
88  return false;
89  }
90  }
91 
92  if (!notifier->Flush()) {
93  LOG(ERROR) << "Failed to flush MPD notifier.";
94  return false;
95  }
96  return true;
97 }
98 
99 void MpdWriter::SetMpdNotifierFactoryForTest(
100  std::unique_ptr<MpdNotifierFactory> factory) {
101  notifier_factory_ = std::move(factory);
102 }
103 
104 } // namespace shaka
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:216
All the methods that are virtual are virtual for mocking.