Shaka Packager SDK
adaptation_set.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_ADAPTATION_SET_H_
10 #define PACKAGER_MPD_BASE_ADAPTATION_SET_H_
11 
12 #include <stdint.h>
13 
14 #include <list>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <vector>
19 
20 #include "packager/base/atomic_sequence_num.h"
21 #include "packager/base/optional.h"
22 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
23 
24 namespace shaka {
25 
26 class MediaInfo;
27 class Representation;
28 
29 struct ContentProtectionElement;
30 struct MpdOptions;
31 
32 namespace xml {
33 class XmlNode;
34 } // namespace xml
35 
39  public:
40  // The role for this AdaptationSet. These values are used to add a Role
41  // element to the AdaptationSet with schemeIdUri=urn:mpeg:dash:role:2011.
42  // See ISO/IEC 23009-1:2012 section 5.8.5.5.
43  enum Role {
44  kRoleCaption,
45  kRoleSubtitle,
46  kRoleMain,
47  kRoleAlternate,
48  kRoleSupplementary,
49  kRoleCommentary,
50  kRoleDub
51  };
52 
53  virtual ~AdaptationSet();
54 
61  virtual Representation* AddRepresentation(const MediaInfo& media_info);
62 
69  virtual Representation* CopyRepresentation(
70  const Representation& representation);
71 
81  virtual void AddContentProtectionElement(
82  const ContentProtectionElement& element);
83 
95  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
96  const std::string& pssh);
97 
102  virtual void AddRole(Role role);
103 
108  xml::scoped_xml_ptr<xmlNode> GetXml();
109 
115  virtual void ForceSetSegmentAlignment(bool segment_alignment);
116 
119  virtual void AddAdaptationSetSwitching(const AdaptationSet* adaptation_set);
120 
122  bool has_id() const { return static_cast<bool>(id_); }
123 
124  // Must be unique in the Period.
125  uint32_t id() const { return id_.value(); }
126 
129  void set_id(uint32_t id) { id_ = id; }
130 
142  void OnNewSegmentForRepresentation(uint32_t representation_id,
143  uint64_t start_time,
144  uint64_t duration);
145 
158  void OnSetFrameRateForRepresentation(uint32_t representation_id,
159  uint32_t frame_duration,
160  uint32_t timescale);
161 
164  virtual void AddTrickPlayReference(const AdaptationSet* adaptation_set);
165 
166  // Return the list of Representations in this AdaptationSet.
167  const std::list<Representation*> GetRepresentations() const;
168 
170  bool IsVideo() const;
171 
172  protected:
179  AdaptationSet(const std::string& language,
180  const MpdOptions& mpd_options,
181  base::AtomicSequenceNumber* representation_counter);
182 
183  private:
184  AdaptationSet(const AdaptationSet&) = delete;
185  AdaptationSet& operator=(const AdaptationSet&) = delete;
186 
187  friend class Period;
188  friend class AdaptationSetTest;
189 
190  // kSegmentAlignmentUnknown means that it is uncertain if the
191  // (sub)segments are aligned or not.
192  // kSegmentAlignmentTrue means that it is certain that the all the (current)
193  // segments added to the adaptation set are aligned.
194  // kSegmentAlignmentFalse means that it is it is certain that some segments
195  // are not aligned. This is useful to disable the computation for
196  // segment alignment, once it is certain that some segments are not aligned.
197  enum SegmentAligmentStatus {
198  kSegmentAlignmentUnknown,
199  kSegmentAlignmentTrue,
200  kSegmentAlignmentFalse
201  };
202 
203  // This maps Representations (IDs) to a list of start times of the segments.
204  // e.g.
205  // If Representation 1 has start time 0, 100, 200 and Representation 2 has
206  // start times 0, 200, 400, then the map contains:
207  // 1 -> [0, 100, 200]
208  // 2 -> [0, 200, 400]
209  typedef std::map<uint32_t, std::list<uint64_t>> RepresentationTimeline;
210 
211  // Update AdaptationSet attributes for new MediaInfo.
212  void UpdateFromMediaInfo(const MediaInfo& media_info);
213 
221  void CheckLiveSegmentAlignment(uint32_t representation_id,
222  uint64_t start_time,
223  uint64_t duration);
224 
225  // Checks representation_segment_start_times_ and sets segments_aligned_.
226  // Use this for VOD, do not use for Live.
227  void CheckVodSegmentAlignment();
228 
229  // Records the framerate of a Representation.
230  void RecordFrameRate(uint32_t frame_duration, uint32_t timescale);
231 
232  std::list<ContentProtectionElement> content_protection_elements_;
233  // representation_id => Representation map. It also keeps the representations_
234  // sorted by default.
235  std::map<uint32_t, std::unique_ptr<Representation>> representation_map_;
236 
237  base::AtomicSequenceNumber* const representation_counter_;
238 
239  base::Optional<uint32_t> id_;
240  const std::string language_;
241  const MpdOptions& mpd_options_;
242 
243  // An array of adaptation sets this adaptation set can switch to.
244  std::vector<const AdaptationSet*> switchable_adaptation_sets_;
245 
246  // Video widths and heights of Representations. Note that this is a set; if
247  // there is only 1 resolution, then @width & @height should be set, otherwise
248  // @maxWidth & @maxHeight should be set for DASH IOP.
249  std::set<uint32_t> video_widths_;
250  std::set<uint32_t> video_heights_;
251 
252  // Video representations' frame rates.
253  // The frame rate notation for MPD is <integer>/<integer> (where the
254  // denominator is optional). This means the frame rate could be non-whole
255  // rational value, therefore the key is of type double.
256  // Value is <integer>/<integer> in string form.
257  // So, key == CalculatedValue(value)
258  std::map<double, std::string> video_frame_rates_;
259 
260  // contentType attribute of AdaptationSet.
261  // Determined by examining the MediaInfo passed to AddRepresentation().
262  std::string content_type_;
263 
264  // This does not have to be a set, it could be a list or vector because all we
265  // really care is whether there is more than one entry.
266  // Contains one entry if all the Representations have the same picture aspect
267  // ratio (@par attribute for AdaptationSet).
268  // There will be more than one entry if there are multiple picture aspect
269  // ratios.
270  // The @par attribute should only be set if there is exactly one entry
271  // in this set.
272  std::set<std::string> picture_aspect_ratio_;
273 
274  // The roles of this AdaptationSet.
275  std::set<Role> roles_;
276 
277  // True iff all the segments are aligned.
278  SegmentAligmentStatus segments_aligned_;
279  bool force_set_segment_alignment_;
280 
281  // Keeps track of segment start times of Representations.
282  // For VOD, this will not be cleared, all the segment start times are
283  // stored in this. This should not out-of-memory for a reasonable length
284  // video and reasonable subsegment length.
285  // For Live, the entries are deleted (see CheckLiveSegmentAlignment()
286  // implementation comment) because storing the entire timeline is not
287  // reasonable and may cause an out-of-memory problem.
288  RepresentationTimeline representation_segment_start_times_;
289 
290  // Record the original AdaptationSets the trick play stream belongs to. There
291  // can be more than one reference AdaptationSets as multiple streams e.g. SD
292  // and HD videos in different AdaptationSets can share the same trick play
293  // stream.
294  std::vector<const AdaptationSet*> trick_play_references_;
295 };
296 
297 } // namespace shaka
298 
299 #endif // PACKAGER_MPD_BASE_ADAPTATION_SET_H_
All the methods that are virtual are virtual for mocking.
Defines Mpd Options.
Definition: mpd_options.h:25
void set_id(uint32_t id)