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