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  kRoleUnknown,
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 AddAccessibility(const std::string& scheme,
103  const std::string& value);
104 
109  virtual void AddRole(Role role);
110 
115  xml::scoped_xml_ptr<xmlNode> GetXml();
116 
122  virtual void ForceSetSegmentAlignment(bool segment_alignment);
123 
126  virtual void AddAdaptationSetSwitching(const AdaptationSet* adaptation_set);
127 
129  bool has_id() const { return static_cast<bool>(id_); }
130 
131  // Must be unique in the Period.
132  uint32_t id() const { return id_.value(); }
133 
136  void set_id(uint32_t id) { id_ = id; }
137 
149  void OnNewSegmentForRepresentation(uint32_t representation_id,
150  uint64_t start_time,
151  uint64_t duration);
152 
165  void OnSetFrameRateForRepresentation(uint32_t representation_id,
166  uint32_t frame_duration,
167  uint32_t timescale);
168 
171  virtual void AddTrickPlayReference(const AdaptationSet* adaptation_set);
172 
173  // Return the list of Representations in this AdaptationSet.
174  const std::list<Representation*> GetRepresentations() const;
175 
177  bool IsVideo() const;
178 
179  protected:
186  AdaptationSet(const std::string& language,
187  const MpdOptions& mpd_options,
188  uint32_t* representation_counter);
189 
190  private:
191  AdaptationSet(const AdaptationSet&) = delete;
192  AdaptationSet& operator=(const AdaptationSet&) = delete;
193 
194  friend class Period;
195  friend class AdaptationSetTest;
196 
197  // kSegmentAlignmentUnknown means that it is uncertain if the
198  // (sub)segments are aligned or not.
199  // kSegmentAlignmentTrue means that it is certain that the all the (current)
200  // segments added to the adaptation set are aligned.
201  // kSegmentAlignmentFalse means that it is it is certain that some segments
202  // are not aligned. This is useful to disable the computation for
203  // segment alignment, once it is certain that some segments are not aligned.
204  enum SegmentAligmentStatus {
205  kSegmentAlignmentUnknown,
206  kSegmentAlignmentTrue,
207  kSegmentAlignmentFalse
208  };
209 
210  // This maps Representations (IDs) to a list of start times of the segments.
211  // e.g.
212  // If Representation 1 has start time 0, 100, 200 and Representation 2 has
213  // start times 0, 200, 400, then the map contains:
214  // 1 -> [0, 100, 200]
215  // 2 -> [0, 200, 400]
216  typedef std::map<uint32_t, std::list<uint64_t>> RepresentationTimeline;
217 
218  // Update AdaptationSet attributes for new MediaInfo.
219  void UpdateFromMediaInfo(const MediaInfo& media_info);
220 
229  void CheckDynamicSegmentAlignment(uint32_t representation_id,
230  uint64_t start_time,
231  uint64_t duration);
232 
233  // Checks representation_segment_start_times_ and sets segments_aligned_.
234  // Use this for static MPD, do not use for dynamic MPD.
235  void CheckStaticSegmentAlignment();
236 
237  // Records the framerate of a Representation.
238  void RecordFrameRate(uint32_t frame_duration, uint32_t timescale);
239 
240  std::list<ContentProtectionElement> content_protection_elements_;
241  // representation_id => Representation map. It also keeps the representations_
242  // sorted by default.
243  std::map<uint32_t, std::unique_ptr<Representation>> representation_map_;
244 
245  uint32_t* const representation_counter_;
246 
247  base::Optional<uint32_t> id_;
248  const std::string language_;
249  const MpdOptions& mpd_options_;
250 
251  // An array of adaptation sets this adaptation set can switch to.
252  std::vector<const AdaptationSet*> switchable_adaptation_sets_;
253 
254  // Video widths and heights of Representations. Note that this is a set; if
255  // there is only 1 resolution, then @width & @height should be set, otherwise
256  // @maxWidth & @maxHeight should be set for DASH IOP.
257  std::set<uint32_t> video_widths_;
258  std::set<uint32_t> video_heights_;
259 
260  // Video representations' frame rates.
261  // The frame rate notation for MPD is <integer>/<integer> (where the
262  // denominator is optional). This means the frame rate could be non-whole
263  // rational value, therefore the key is of type double.
264  // Value is <integer>/<integer> in string form.
265  // So, key == CalculatedValue(value)
266  std::map<double, std::string> video_frame_rates_;
267 
268  // contentType attribute of AdaptationSet.
269  // Determined by examining the MediaInfo passed to AddRepresentation().
270  std::string content_type_;
271 
272  // This does not have to be a set, it could be a list or vector because all we
273  // really care is whether there is more than one entry.
274  // Contains one entry if all the Representations have the same picture aspect
275  // ratio (@par attribute for AdaptationSet).
276  // There will be more than one entry if there are multiple picture aspect
277  // ratios.
278  // The @par attribute should only be set if there is exactly one entry
279  // in this set.
280  std::set<std::string> picture_aspect_ratio_;
281 
282  // accessibilities of this AdaptationSet.
283  struct Accessibility {
284  std::string scheme;
285  std::string value;
286  };
287  std::vector<Accessibility> accessibilities_;
288 
289  // The roles of this AdaptationSet.
290  std::set<Role> roles_;
291 
292  // True iff all the segments are aligned.
293  SegmentAligmentStatus segments_aligned_;
294  bool force_set_segment_alignment_;
295 
296  // Keeps track of segment start times of Representations.
297  // For static MPD, this will not be cleared, all the segment start times are
298  // stored in this. This should not out-of-memory for a reasonable length
299  // video and reasonable subsegment length.
300  // For dynamic MPD, the entries are deleted (see
301  // CheckDynamicSegmentAlignment() implementation comment) because storing the
302  // entire timeline is not reasonable and may cause an out-of-memory problem.
303  RepresentationTimeline representation_segment_start_times_;
304 
305  // Record the original AdaptationSets the trick play stream belongs to. There
306  // can be more than one reference AdaptationSets as multiple streams e.g. SD
307  // and HD videos in different AdaptationSets can share the same trick play
308  // stream.
309  std::vector<const AdaptationSet*> trick_play_references_;
310 };
311 
312 } // namespace shaka
313 
314 #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)