Shaka Packager SDK
Loading...
Searching...
No Matches
mpd_builder.h
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//
10
11#ifndef MPD_BASE_MPD_BUILDER_H_
12#define MPD_BASE_MPD_BUILDER_H_
13
14#include <cstdint>
15#include <list>
16#include <memory>
17#include <optional>
18#include <string>
19#include <utility>
20
21#include <libxml/tree.h>
22
23#include <packager/mpd/base/mpd_options.h>
24#include <packager/mpd/base/xml/xml_node.h>
25#include <packager/utils/clock.h>
26
27// TODO(rkuroiwa): For classes with |id_|, consider removing the field and let
28// the MPD (XML) generation functions take care of assigning an ID to each
29// element.
30namespace shaka {
31
32class AdaptationSet;
33class MediaInfo;
34class Period;
35
38 public:
41 explicit MpdBuilder(const MpdOptions& mpd_options);
42 virtual ~MpdBuilder();
43
46 void AddBaseUrl(const std::string& base_url);
47
54 virtual Period* GetOrCreatePeriod(double start_time_in_seconds);
55
58 void FinalizeDynamicMpd();
59
63 // TODO(kqyang): Handle file IO in this class as in HLS media_playlist?
64 [[nodiscard]] virtual bool ToString(std::string* output);
65
71 static void MakePathsRelativeToMpd(const std::string& mpd_path,
72 MediaInfo* media_info);
73
74 // Inject a |clock| that returns the current time.
76 void InjectClockForTesting(std::unique_ptr<Clock> clock) {
77 clock_ = std::move(clock);
78 }
79
80 private:
81 MpdBuilder(const MpdBuilder&) = delete;
82 MpdBuilder& operator=(const MpdBuilder&) = delete;
83
84 // LiveMpdBuilderTest needs to set availabilityStartTime so that the test
85 // doesn't need to depend on current time.
86 friend class LiveMpdBuilderTest;
87 template <DashProfile profile>
88 friend class MpdBuilderTest;
89
90 // Returns the document pointer to the MPD. This must be freed by the caller
91 // using appropriate xmlDocPtr freeing function.
92 // On failure, this returns NULL.
93 std::optional<xml::XmlNode> GenerateMpd();
94
95 // Set MPD attributes common to all profiles. Uses non-zero |mpd_options_| to
96 // set attributes for the MPD.
97 [[nodiscard]] bool AddCommonMpdInfo(xml::XmlNode* mpd_node);
98
99 // Adds 'static' MPD attributes and elements to |mpd_node|. This assumes that
100 // the first child element is a Period element.
101 [[nodiscard]] bool AddStaticMpdInfo(xml::XmlNode* mpd_node);
102
103 // Same as AddStaticMpdInfo() but for 'dynamic' MPDs.
104 [[nodiscard]] bool AddDynamicMpdInfo(xml::XmlNode* mpd_node);
105
106 // Add UTCTiming element if utc timing is provided.
107 [[nodiscard]] bool AddUtcTiming(xml::XmlNode* mpd_node);
108
109 float GetStaticMpdDuration();
110
111 // Set MPD attributes for dynamic profile MPD. Uses non-zero |mpd_options_| as
112 // well as various calculations to set attributes for the MPD.
113 void SetDynamicMpdAttributes(xml::XmlNode* mpd_node);
114
115 // Gets the earliest, normalized segment timestamp. Returns true if
116 // successful, false otherwise.
117 [[nodiscard]] bool GetEarliestTimestamp(double* timestamp_seconds);
118
119 // Update Period durations and presentation timestamps.
120 void UpdatePeriodDurationAndPresentationTimestamp();
121
122 MpdOptions mpd_options_;
123 std::list<std::unique_ptr<Period>> periods_;
124
125 std::list<std::string> base_urls_;
126 std::string availability_start_time_;
127
128 uint32_t period_counter_ = 0;
129 uint32_t representation_counter_ = 0;
130
131 // By default, this returns the current time. This can be injected for
132 // testing.
133 std::unique_ptr<Clock> clock_;
134};
135
136} // namespace shaka
137
138#endif // MPD_BASE_MPD_BUILDER_H_
This class generates DASH MPDs (Media Presentation Descriptions).
Definition mpd_builder.h:37
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)
void AddBaseUrl(const std::string &base_url)
virtual Period * GetOrCreatePeriod(double start_time_in_seconds)
void InjectClockForTesting(std::unique_ptr< Clock > clock)
This is for testing.
Definition mpd_builder.h:76
virtual bool ToString(std::string *output)
All the methods that are virtual are virtual for mocking.
Defines Mpd Options.
Definition mpd_options.h:24