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  float GetStaticMpdDuration();
104 
105  // Set MPD attributes for dynamic profile MPD. Uses non-zero |mpd_options_| as
106  // well as various calculations to set attributes for the MPD.
107  void SetDynamicMpdAttributes(xml::XmlNode* mpd_node);
108 
109  // Gets the earliest, normalized segment timestamp. Returns true if
110  // successful, false otherwise.
111  bool GetEarliestTimestamp(double* timestamp_seconds);
112 
113  MpdOptions mpd_options_;
114  std::list<std::unique_ptr<Period>> periods_;
115 
116  std::list<std::string> base_urls_;
117  std::string availability_start_time_;
118 
119  base::AtomicSequenceNumber period_counter_;
120  base::AtomicSequenceNumber adaptation_set_counter_;
121  base::AtomicSequenceNumber representation_counter_;
122 
123  // By default, this returns the current time. This can be injected for
124  // testing.
125  std::unique_ptr<base::Clock> clock_;
126 };
127 
128 } // namespace shaka
129 
130 #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