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)
override {
39 return std::unique_ptr<MpdNotifier>(
new DashIopMpdNotifier(mpd_options));
44 class SimpleMpdNotifierFactory :
public MpdNotifierFactory {
46 SimpleMpdNotifierFactory() {}
47 ~SimpleMpdNotifierFactory()
override {}
49 std::unique_ptr<MpdNotifier> Create(
const MpdOptions& mpd_options)
override {
50 return std::unique_ptr<MpdNotifier>(
new SimpleMpdNotifier(mpd_options));
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() {}
64 bool MpdWriter::AddFile(
const std::string& media_info_path,
65 const std::string& mpd_path) {
66 std::string file_content;
68 LOG(ERROR) <<
"Failed to read " << media_info_path <<
" to string.";
73 if (!::google::protobuf::TextFormat::ParseFromString(file_content,
75 LOG(ERROR) <<
"Failed to parse " << file_content <<
" to MediaInfo.";
80 media_infos_.push_back(media_info);
84 void MpdWriter::AddBaseUrl(
const std::string& base_url) {
85 base_urls_.push_back(base_url);
88 bool MpdWriter::WriteMpdToFile(
const char* 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.";
100 for (std::list<MediaInfo>::const_iterator it = media_infos_.begin();
101 it != media_infos_.end();
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();
111 if (!notifier->Flush()) {
112 LOG(ERROR) <<
"Failed to flush MPD notifier.";
118 void MpdWriter::SetMpdNotifierFactoryForTest(
119 std::unique_ptr<MpdNotifierFactory> factory) {
120 notifier_factory_ = std::move(factory);
static bool ReadFileToString(const char *file_name, std::string *contents)
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)