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