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