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