7 #include "packager/mpd/util/mpd_writer.h"
9 #include <gflags/gflags.h>
10 #include <google/protobuf/text_format.h>
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"
22 DEFINE_bool(generate_dash_if_iop_compliant_mpd,
24 "Try to generate DASH-IF IOPv3 compliant MPD. This is best effort "
25 "and does not guarantee compliance. Off by default until players "
33 class DashIopMpdNotifierFactory :
public MpdNotifierFactory {
35 DashIopMpdNotifierFactory() {}
36 ~DashIopMpdNotifierFactory()
override {}
38 std::unique_ptr<MpdNotifier> Create(
const MpdOptions& mpd_options,
39 const std::vector<std::string>& base_urls,
40 const std::string& output_path)
override {
41 return std::unique_ptr<MpdNotifier>(
42 new DashIopMpdNotifier(mpd_options, base_urls, output_path));
47 class SimpleMpdNotifierFactory :
public MpdNotifierFactory {
49 SimpleMpdNotifierFactory() {}
50 ~SimpleMpdNotifierFactory()
override {}
52 std::unique_ptr<MpdNotifier> Create(
const MpdOptions& mpd_options,
53 const std::vector<std::string>& base_urls,
54 const std::string& output_path)
override {
55 return std::unique_ptr<MpdNotifier>(
56 new SimpleMpdNotifier(mpd_options, base_urls, output_path));
62 MpdWriter::MpdWriter()
63 : notifier_factory_(FLAGS_generate_dash_if_iop_compliant_mpd
64 ? static_cast<MpdNotifierFactory*>(
65 new DashIopMpdNotifierFactory())
66 : static_cast<MpdNotifierFactory*>(
67 new SimpleMpdNotifierFactory())) {}
68 MpdWriter::~MpdWriter() {}
70 bool MpdWriter::AddFile(
const std::string& media_info_path,
71 const std::string& mpd_path) {
72 std::string file_content;
73 if (!File::ReadFileToString(media_info_path.c_str(), &file_content)) {
74 LOG(ERROR) <<
"Failed to read " << media_info_path <<
" to string.";
79 if (!::google::protobuf::TextFormat::ParseFromString(file_content,
81 LOG(ERROR) <<
"Failed to parse " << file_content <<
" to MediaInfo.";
86 media_infos_.push_back(media_info);
90 void MpdWriter::AddBaseUrl(
const std::string& base_url) {
91 base_urls_.push_back(base_url);
94 bool MpdWriter::WriteMpdToFile(
const char* file_name) {
96 std::unique_ptr<MpdNotifier> notifier =
97 notifier_factory_->Create(MpdOptions(), base_urls_, file_name);
98 if (!notifier->Init()) {
99 LOG(ERROR) <<
"failed to initialize MpdNotifier.";
103 for (std::list<MediaInfo>::const_iterator it = media_infos_.begin();
104 it != media_infos_.end();
106 uint32_t unused_conatiner_id;
107 if (!notifier->NotifyNewContainer(*it, &unused_conatiner_id)) {
108 LOG(ERROR) <<
"Failed to add MediaInfo for media file: "
109 << it->media_file_name();
114 if (!notifier->Flush()) {
115 LOG(ERROR) <<
"Failed to flush MPD notifier.";
121 void MpdWriter::SetMpdNotifierFactoryForTest(
122 std::unique_ptr<MpdNotifierFactory> factory) {
123 notifier_factory_ = std::move(factory);
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)