Shaka Packager SDK
mpd_builder.h
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 //
10 
11 #ifndef MPD_BASE_MPD_BUILDER_H_
12 #define MPD_BASE_MPD_BUILDER_H_
13 
14 #include <libxml/tree.h>
15 
16 #include <list>
17 #include <memory>
18 #include <string>
19 
20 #include "packager/base/time/clock.h"
21 #include "packager/mpd/base/mpd_options.h"
22 
23 // TODO(rkuroiwa): For classes with |id_|, consider removing the field and let
24 // the MPD (XML) generation functions take care of assigning an ID to each
25 // element.
26 namespace shaka {
27 
28 class AdaptationSet;
29 class MediaInfo;
30 class Period;
31 
32 namespace xml {
33 class XmlNode;
34 } // namespace xml
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  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<base::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  xmlDocPtr GenerateMpd();
90 
91  // Set MPD attributes common to all profiles. Uses non-zero |mpd_options_| to
92  // set attributes for the MPD.
93  void 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  void AddStaticMpdInfo(xml::XmlNode* mpd_node);
98 
99  // Same as AddStaticMpdInfo() but for 'dynamic' MPDs.
100  void AddDynamicMpdInfo(xml::XmlNode* mpd_node);
101 
102  // Add UTCTiming element if utc timing is provided.
103  void 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  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<base::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
All the methods that are virtual are virtual for mocking.
Defines Mpd Options.
Definition: mpd_options.h:25
void InjectClockForTesting(std::unique_ptr< base::Clock > clock)
This is for testing.
Definition: mpd_builder.h:72