Shaka Packager SDK
representation.h
1 // Copyright 2017 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 //
8 
9 #ifndef PACKAGER_MPD_BASE_REPRESENTATION_H_
10 #define PACKAGER_MPD_BASE_REPRESENTATION_H_
11 
12 #include "packager/mpd/base/bandwidth_estimator.h"
13 #include "packager/mpd/base/media_info.pb.h"
14 #include "packager/mpd/base/segment_info.h"
15 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
16 
17 #include <stdint.h>
18 
19 #include <list>
20 #include <memory>
21 
22 namespace shaka {
23 
24 struct ContentProtectionElement;
25 struct MpdOptions;
26 
27 namespace xml {
28 class XmlNode;
29 class RepresentationXmlNode;
30 } // namespace xml
31 
33  public:
36 
41  virtual void OnNewSegmentForRepresentation(int64_t start_time,
42  int64_t duration) = 0;
43 
48  virtual void OnSetFrameRateForRepresentation(uint32_t frame_duration,
49  uint32_t timescale) = 0;
50 };
51 
55  public:
56  enum SuppressFlag {
57  kSuppressWidth = 1,
58  kSuppressHeight = 2,
59  kSuppressFrameRate = 4,
60  };
61 
62  virtual ~Representation();
63 
67  bool Init();
68 
79  virtual void AddContentProtectionElement(
80  const ContentProtectionElement& element);
81 
93  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
94  const std::string& pssh);
95 
104  virtual void AddNewSegment(int64_t start_time,
105  int64_t duration,
106  uint64_t size);
107 
113  virtual void SetSampleDuration(uint32_t sample_duration);
114 
116  virtual const MediaInfo& GetMediaInfo() const;
117 
119  xml::scoped_xml_ptr<xmlNode> GetXml();
120 
129  void SuppressOnce(SuppressFlag flag);
130 
132  void SetPresentationTimeOffset(double presentation_time_offset);
133 
142  bool GetStartAndEndTimestamps(double* start_timestamp_seconds,
143  double* end_timestamp_seconds) const;
144 
146  uint32_t id() const { return id_; }
147 
148  void set_media_info(const MediaInfo& media_info) { media_info_ = media_info; }
149 
150  protected:
160  const MediaInfo& media_info,
161  const MpdOptions& mpd_options,
162  uint32_t representation_id,
163  std::unique_ptr<RepresentationStateChangeListener> state_change_listener);
164 
169  const Representation& representation,
170  std::unique_ptr<RepresentationStateChangeListener> state_change_listener);
171 
172  private:
173  Representation(const Representation&) = delete;
174  Representation& operator=(const Representation&) = delete;
175 
176  friend class AdaptationSet;
177  friend class RepresentationTest;
178 
179  // Returns true if |media_info_| has required fields to generate a valid
180  // Representation. Otherwise returns false.
181  bool HasRequiredMediaInfoFields() const;
182 
183  // Add a SegmentInfo. This function may insert an adjusted SegmentInfo if
184  // |allow_approximate_segment_timeline_| is set.
185  void AddSegmentInfo(int64_t start_time, int64_t duration);
186 
187  // Check if two timestamps are approximately equal if
188  // |allow_approximate_segment_timeline_| is set; Otherwise check whether the
189  // two times match.
190  bool ApproximiatelyEqual(int64_t time1, int64_t time2) const;
191 
192  // Return adjusted duration if |allow_aproximate_segment_timeline_or_duration|
193  // is set; otherwise duration is returned without adjustment.
194  int64_t AdjustDuration(int64_t duration) const;
195 
196  // Remove elements from |segment_infos_| for dynamic live profile. Increments
197  // |start_number_| by the number of segments removed.
198  void SlideWindow();
199 
200  // Remove the first segment in |segment_info|.
201  void RemoveOldSegment(SegmentInfo* segment_info);
202 
203  // Note: Because 'mimeType' is a required field for a valid MPD, these return
204  // strings.
205  std::string GetVideoMimeType() const;
206  std::string GetAudioMimeType() const;
207  std::string GetTextMimeType() const;
208 
209  // Get Representation as string. For debugging.
210  std::string RepresentationAsString() const;
211 
212  // Init() checks that only one of VideoInfo, AudioInfo, or TextInfo is set. So
213  // any logic using this can assume only one set.
214  MediaInfo media_info_;
215  std::list<ContentProtectionElement> content_protection_elements_;
216 
217  int64_t current_buffer_depth_ = 0;
218  // TODO(kqyang): Address sliding window issue with multiple periods.
219  std::list<SegmentInfo> segment_infos_;
220  // A list to hold the file names of the segments to be removed temporarily.
221  // Once a file is actually removed, it is removed from the list.
222  std::list<std::string> segments_to_be_removed_;
223 
224  const uint32_t id_;
225  std::string mime_type_;
226  std::string codecs_;
227  BandwidthEstimator bandwidth_estimator_;
228  const MpdOptions& mpd_options_;
229 
230  // startNumber attribute for SegmentTemplate.
231  // Starts from 1.
232  uint32_t start_number_ = 1;
233 
234  // If this is not null, then Representation is responsible for calling the
235  // right methods at right timings.
236  std::unique_ptr<RepresentationStateChangeListener> state_change_listener_;
237 
238  // Bit vector for tracking witch attributes should not be output.
239  int output_suppression_flags_ = 0;
240 
241  // When set to true, allows segments to have slightly different durations (up
242  // to one sample).
243  const bool allow_approximate_segment_timeline_ = false;
244  // Segments with duration difference less than one frame duration are
245  // considered to have the same duration.
246  uint32_t frame_duration_ = 0;
247 };
248 
249 } // namespace shaka
250 
251 #endif // PACKAGER_MPD_BASE_REPRESENTATION_H_
uint32_t id() const
All the methods that are virtual are virtual for mocking.
Defines Mpd Options.
Definition: mpd_options.h:25