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/optional.h"
21 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
22 
23 namespace shaka {
24 
25 class MediaInfo;
26 class Representation;
27 
28 struct ContentProtectionElement;
29 struct MpdOptions;
30 
31 namespace xml {
32 class XmlNode;
33 } // namespace xml
34 
38  public:
39  // The role for this AdaptationSet. These values are used to add a Role
40  // element to the AdaptationSet with schemeIdUri=urn:mpeg:dash:role:2011.
41  // See ISO/IEC 23009-1:2012 section 5.8.5.5.
42  enum Role {
43  kRoleCaption,
44  kRoleSubtitle,
45  kRoleMain,
46  kRoleAlternate,
47  kRoleSupplementary,
48  kRoleCommentary,
49  kRoleDub
50  };
51 
52  virtual ~AdaptationSet();
53 
60  virtual Representation* AddRepresentation(const MediaInfo& media_info);
61 
68  virtual Representation* CopyRepresentation(
69  const Representation& representation);
70 
80  virtual void AddContentProtectionElement(
81  const ContentProtectionElement& element);
82 
94  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
95  const std::string& pssh);
96 
101  virtual void AddRole(Role role);
102 
107  xml::scoped_xml_ptr<xmlNode> GetXml();
108 
114  virtual void ForceSetSegmentAlignment(bool segment_alignment);
115 
118  virtual void AddAdaptationSetSwitching(const AdaptationSet* adaptation_set);
119 
121  bool has_id() const { return static_cast<bool>(id_); }
122 
123  // Must be unique in the Period.
124  uint32_t id() const { return id_.value(); }
125 
128  void set_id(uint32_t id) { id_ = id; }
129 
141  void OnNewSegmentForRepresentation(uint32_t representation_id,
142  uint64_t start_time,
143  uint64_t duration);
144 
157  void OnSetFrameRateForRepresentation(uint32_t representation_id,
158  uint32_t frame_duration,
159  uint32_t timescale);
160 
163  virtual void AddTrickPlayReference(const AdaptationSet* adaptation_set);
164 
165  // Return the list of Representations in this AdaptationSet.
166  const std::list<Representation*> GetRepresentations() const;
167 
169  bool IsVideo() const;
170 
171  protected:
178  AdaptationSet(const std::string& language,
179  const MpdOptions& mpd_options,
180  uint32_t* representation_counter);
181 
182  private:
183  AdaptationSet(const AdaptationSet&) = delete;
184  AdaptationSet& operator=(const AdaptationSet&) = delete;
185 
186  friend class Period;
187  friend class AdaptationSetTest;
188 
189  // kSegmentAlignmentUnknown means that it is uncertain if the
190  // (sub)segments are aligned or not.
191  // kSegmentAlignmentTrue means that it is certain that the all the (current)
192  // segments added to the adaptation set are aligned.
193  // kSegmentAlignmentFalse means that it is it is certain that some segments
194  // are not aligned. This is useful to disable the computation for
195  // segment alignment, once it is certain that some segments are not aligned.
196  enum SegmentAligmentStatus {
197  kSegmentAlignmentUnknown,
198  kSegmentAlignmentTrue,
199  kSegmentAlignmentFalse
200  };
201 
202  // This maps Representations (IDs) to a list of start times of the segments.
203  // e.g.
204  // If Representation 1 has start time 0, 100, 200 and Representation 2 has
205  // start times 0, 200, 400, then the map contains:
206  // 1 -> [0, 100, 200]
207  // 2 -> [0, 200, 400]
208  typedef std::map<uint32_t, std::list<uint64_t>> RepresentationTimeline;
209 
210  // Update AdaptationSet attributes for new MediaInfo.
211  void UpdateFromMediaInfo(const MediaInfo& media_info);
212 
221  void CheckDynamicSegmentAlignment(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 static MPD, do not use for dynamic MPD.
227  void CheckStaticSegmentAlignment();
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  uint32_t* 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 static MPD, 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 dynamic MPD, the entries are deleted (see
286  // CheckDynamicSegmentAlignment() implementation comment) because storing the
287  // entire timeline is not 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)