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 <stdint.h>
13 
14 #include <list>
15 #include <memory>
16 
17 #include "packager/base/optional.h"
18 #include "packager/mpd/base/bandwidth_estimator.h"
19 #include "packager/mpd/base/media_info.pb.h"
20 #include "packager/mpd/base/segment_info.h"
21 #include "packager/mpd/base/xml/xml_node.h"
22 
23 namespace shaka {
24 
25 struct ContentProtectionElement;
26 struct MpdOptions;
27 
29  public:
32 
37  virtual void OnNewSegmentForRepresentation(int64_t start_time,
38  int64_t duration) = 0;
39 
44  virtual void OnSetFrameRateForRepresentation(uint32_t frame_duration,
45  uint32_t timescale) = 0;
46 };
47 
51  public:
52  enum SuppressFlag {
53  kSuppressWidth = 1,
54  kSuppressHeight = 2,
55  kSuppressFrameRate = 4,
56  };
57 
58  virtual ~Representation();
59 
63  bool Init();
64 
75  virtual void AddContentProtectionElement(
76  const ContentProtectionElement& element);
77 
89  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
90  const std::string& pssh);
91 
100  virtual void AddNewSegment(int64_t start_time,
101  int64_t duration,
102  uint64_t size);
103 
109  virtual void SetSampleDuration(uint32_t sample_duration);
110 
112  virtual const MediaInfo& GetMediaInfo() const;
113 
115  base::Optional<xml::XmlNode> GetXml();
116 
125  void SuppressOnce(SuppressFlag flag);
126 
128  void SetPresentationTimeOffset(double presentation_time_offset);
129 
138  bool GetStartAndEndTimestamps(double* start_timestamp_seconds,
139  double* end_timestamp_seconds) const;
140 
142  uint32_t id() const { return id_; }
143 
144  void set_media_info(const MediaInfo& media_info) { media_info_ = media_info; }
145 
146  protected:
156  const MediaInfo& media_info,
157  const MpdOptions& mpd_options,
158  uint32_t representation_id,
159  std::unique_ptr<RepresentationStateChangeListener> state_change_listener);
160 
165  const Representation& representation,
166  std::unique_ptr<RepresentationStateChangeListener> state_change_listener);
167 
168  private:
169  Representation(const Representation&) = delete;
170  Representation& operator=(const Representation&) = delete;
171 
172  friend class AdaptationSet;
173  friend class RepresentationTest;
174 
175  // Returns true if |media_info_| has required fields to generate a valid
176  // Representation. Otherwise returns false.
177  bool HasRequiredMediaInfoFields() const;
178 
179  // Add a SegmentInfo. This function may insert an adjusted SegmentInfo if
180  // |allow_approximate_segment_timeline_| is set.
181  void AddSegmentInfo(int64_t start_time, int64_t duration);
182 
183  // Check if two timestamps are approximately equal if
184  // |allow_approximate_segment_timeline_| is set; Otherwise check whether the
185  // two times match.
186  bool ApproximiatelyEqual(int64_t time1, int64_t time2) const;
187 
188  // Return adjusted duration if |allow_aproximate_segment_timeline_or_duration|
189  // is set; otherwise duration is returned without adjustment.
190  int64_t AdjustDuration(int64_t duration) const;
191 
192  // Remove elements from |segment_infos_| for dynamic live profile. Increments
193  // |start_number_| by the number of segments removed.
194  void SlideWindow();
195 
196  // Remove the first segment in |segment_info|.
197  void RemoveOldSegment(SegmentInfo* segment_info);
198 
199  // Note: Because 'mimeType' is a required field for a valid MPD, these return
200  // strings.
201  std::string GetVideoMimeType() const;
202  std::string GetAudioMimeType() const;
203  std::string GetTextMimeType() const;
204 
205  // Get Representation as string. For debugging.
206  std::string RepresentationAsString() const;
207 
208  // Init() checks that only one of VideoInfo, AudioInfo, or TextInfo is set. So
209  // any logic using this can assume only one set.
210  MediaInfo media_info_;
211  std::list<ContentProtectionElement> content_protection_elements_;
212 
213  int64_t current_buffer_depth_ = 0;
214  // TODO(kqyang): Address sliding window issue with multiple periods.
215  std::list<SegmentInfo> segment_infos_;
216  // A list to hold the file names of the segments to be removed temporarily.
217  // Once a file is actually removed, it is removed from the list.
218  std::list<std::string> segments_to_be_removed_;
219 
220  const uint32_t id_;
221  std::string mime_type_;
222  std::string codecs_;
223  BandwidthEstimator bandwidth_estimator_;
224  const MpdOptions& mpd_options_;
225 
226  // startNumber attribute for SegmentTemplate.
227  // Starts from 1.
228  uint32_t start_number_ = 1;
229 
230  // If this is not null, then Representation is responsible for calling the
231  // right methods at right timings.
232  std::unique_ptr<RepresentationStateChangeListener> state_change_listener_;
233 
234  // Bit vector for tracking witch attributes should not be output.
235  int output_suppression_flags_ = 0;
236 
237  // When set to true, allows segments to have slightly different durations (up
238  // to one sample).
239  const bool allow_approximate_segment_timeline_ = false;
240  // Segments with duration difference less than one frame duration are
241  // considered to have the same duration.
242  uint32_t frame_duration_ = 0;
243 };
244 
245 } // namespace shaka
246 
247 #endif // PACKAGER_MPD_BASE_REPRESENTATION_H_
virtual void OnSetFrameRateForRepresentation(uint32_t frame_duration, uint32_t timescale)=0
virtual void OnNewSegmentForRepresentation(int64_t start_time, int64_t duration)=0
virtual void SetSampleDuration(uint32_t sample_duration)
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
void SuppressOnce(SuppressFlag flag)
virtual const MediaInfo & GetMediaInfo() const
bool GetStartAndEndTimestamps(double *start_timestamp_seconds, double *end_timestamp_seconds) const
Representation(const MediaInfo &media_info, const MpdOptions &mpd_options, uint32_t representation_id, std::unique_ptr< RepresentationStateChangeListener > state_change_listener)
uint32_t id() const
void SetPresentationTimeOffset(double presentation_time_offset)
Set @presentationTimeOffset in SegmentBase / SegmentTemplate.
base::Optional< xml::XmlNode > GetXml()
virtual void AddNewSegment(int64_t start_time, int64_t duration, uint64_t size)
All the methods that are virtual are virtual for mocking.