Shaka Packager SDK
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 <chrono>
15 #include <list>
16 #include <memory>
17 #include <optional>
18 #include <string>
19 
20 #include <libxml/tree.h>
21 
22 #include <packager/macros/classes.h>
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.
30 namespace shaka {
31 
32 class AdaptationSet;
33 class MediaInfo;
34 class Period;
35 
37 class MpdBuilder {
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 
59  // TODO(kqyang): Handle file IO in this class as in HLS media_playlist?
60  [[nodiscard]] virtual bool ToString(std::string* output);
61 
67  static void MakePathsRelativeToMpd(const std::string& mpd_path,
68  MediaInfo* media_info);
69 
70  // Inject a |clock| that returns the current time.
72  void InjectClockForTesting(std::unique_ptr<Clock> clock) {
73  clock_ = std::move(clock);
74  }
75 
76  private:
77  MpdBuilder(const MpdBuilder&) = delete;
78  MpdBuilder& operator=(const MpdBuilder&) = delete;
79 
80  // LiveMpdBuilderTest needs to set availabilityStartTime so that the test
81  // doesn't need to depend on current time.
82  friend class LiveMpdBuilderTest;
83  template <DashProfile profile>
84  friend class MpdBuilderTest;
85 
86  // Returns the document pointer to the MPD. This must be freed by the caller
87  // using appropriate xmlDocPtr freeing function.
88  // On failure, this returns NULL.
89  std::optional<xml::XmlNode> GenerateMpd();
90 
91  // Set MPD attributes common to all profiles. Uses non-zero |mpd_options_| to
92  // set attributes for the MPD.
93  [[nodiscard]] bool AddCommonMpdInfo(xml::XmlNode* mpd_node);
94 
95  // Adds 'static' MPD attributes and elements to |mpd_node|. This assumes that
96  // the first child element is a Period element.
97  [[nodiscard]] bool AddStaticMpdInfo(xml::XmlNode* mpd_node);
98 
99  // Same as AddStaticMpdInfo() but for 'dynamic' MPDs.
100  [[nodiscard]] bool AddDynamicMpdInfo(xml::XmlNode* mpd_node);
101 
102  // Add UTCTiming element if utc timing is provided.
103  [[nodiscard]] bool AddUtcTiming(xml::XmlNode* mpd_node);
104 
105  float GetStaticMpdDuration();
106 
107  // Set MPD attributes for dynamic profile MPD. Uses non-zero |mpd_options_| as
108  // well as various calculations to set attributes for the MPD.
109  void SetDynamicMpdAttributes(xml::XmlNode* mpd_node);
110 
111  // Gets the earliest, normalized segment timestamp. Returns true if
112  // successful, false otherwise.
113  [[nodiscard]] bool GetEarliestTimestamp(double* timestamp_seconds);
114 
115  // Update Period durations and presentation timestamps.
116  void UpdatePeriodDurationAndPresentationTimestamp();
117 
118  MpdOptions mpd_options_;
119  std::list<std::unique_ptr<Period>> periods_;
120 
121  std::list<std::string> base_urls_;
122  std::string availability_start_time_;
123 
124  uint32_t period_counter_ = 0;
125  uint32_t representation_counter_ = 0;
126 
127  // By default, this returns the current time. This can be injected for
128  // testing.
129  std::unique_ptr<Clock> clock_;
130 };
131 
132 } // namespace shaka
133 
134 #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)
Definition: mpd_builder.cc:409
MpdBuilder(const MpdOptions &mpd_options)
Definition: mpd_builder.cc:124
void AddBaseUrl(const std::string &base_url)
Definition: mpd_builder.cc:129
virtual Period * GetOrCreatePeriod(double start_time_in_seconds)
Definition: mpd_builder.cc:133
void InjectClockForTesting(std::unique_ptr< Clock > clock)
This is for testing.
Definition: mpd_builder.h:72
virtual bool ToString(std::string *output)
Definition: mpd_builder.cc:147
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66
Defines Mpd Options.
Definition: mpd_options.h:25