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 |num_segments| starting from |start_time| with |duration|.
201  void RemoveSegments(int64_t start_time,
202  int64_t duration,
203  uint64_t num_segments);
204 
205  // Note: Because 'mimeType' is a required field for a valid MPD, these return
206  // strings.
207  std::string GetVideoMimeType() const;
208  std::string GetAudioMimeType() const;
209  std::string GetTextMimeType() const;
210 
211  // Init() checks that only one of VideoInfo, AudioInfo, or TextInfo is set. So
212  // any logic using this can assume only one set.
213  MediaInfo media_info_;
214  std::list<ContentProtectionElement> content_protection_elements_;
215  // TODO(kqyang): Address sliding window issue with multiple periods.
216  std::list<SegmentInfo> segment_infos_;
217  // A list to hold the file names of the segments to be removed temporarily.
218  // Once a file is actually removed, it is removed from the list.
219  std::list<std::string> segments_to_be_removed_;
220 
221  const uint32_t id_;
222  std::string mime_type_;
223  std::string codecs_;
224  BandwidthEstimator bandwidth_estimator_;
225  const MpdOptions& mpd_options_;
226 
227  // startNumber attribute for SegmentTemplate.
228  // Starts from 1.
229  uint32_t start_number_ = 1;
230 
231  // If this is not null, then Representation is responsible for calling the
232  // right methods at right timings.
233  std::unique_ptr<RepresentationStateChangeListener> state_change_listener_;
234 
235  // Bit vector for tracking witch attributes should not be output.
236  int output_suppression_flags_ = 0;
237 
238  // When set to true, allows segments to have slightly different durations (up
239  // to one sample).
240  const bool allow_approximate_segment_timeline_ = false;
241  // Segments with duration difference less than one frame duration are
242  // considered to have the same duration.
243  uint32_t frame_duration_ = 0;
244 };
245 
246 } // namespace shaka
247 
248 #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