Shaka Packager SDK
Loading...
Searching...
No Matches
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 <cstdint>
16#include <list>
17#include <memory>
18#include <optional>
19#include <string>
20
21#include <libxml/tree.h>
22
23#include <packager/macros/classes.h>
24#include <packager/mpd/base/mpd_options.h>
25#include <packager/mpd/base/xml/xml_node.h>
26#include <packager/utils/clock.h>
27
28// TODO(rkuroiwa): For classes with |id_|, consider removing the field and let
29// the MPD (XML) generation functions take care of assigning an ID to each
30// element.
31namespace shaka {
32
33class AdaptationSet;
34class MediaInfo;
35class Period;
36
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 [[nodiscard]] 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<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 std::optional<xml::XmlNode> GenerateMpd();
91
92 // Set MPD attributes common to all profiles. Uses non-zero |mpd_options_| to
93 // set attributes for the MPD.
94 [[nodiscard]] bool 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 [[nodiscard]] bool AddStaticMpdInfo(xml::XmlNode* mpd_node);
99
100 // Same as AddStaticMpdInfo() but for 'dynamic' MPDs.
101 [[nodiscard]] bool AddDynamicMpdInfo(xml::XmlNode* mpd_node);
102
103 // Add UTCTiming element if utc timing is provided.
104 [[nodiscard]] bool 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 [[nodiscard]] 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 uint32_t period_counter_ = 0;
126 uint32_t representation_counter_ = 0;
127
128 // By default, this returns the current time. This can be injected for
129 // testing.
130 std::unique_ptr<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
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)
void AddBaseUrl(const std::string &base_url)
virtual Period * GetOrCreatePeriod(double start_time_in_seconds)
void InjectClockForTesting(std::unique_ptr< Clock > clock)
This is for testing.
Definition mpd_builder.h:73
virtual bool ToString(std::string *output)
All the methods that are virtual are virtual for mocking.
Defines Mpd Options.
Definition mpd_options.h:25