Shaka Packager SDK
adaptation_set.h
1 // Copyright 2017 Google LLC. 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 <cstdint>
13 #include <list>
14 #include <map>
15 #include <memory>
16 #include <optional>
17 #include <set>
18 #include <vector>
19 
20 #include <packager/mpd/base/xml/xml_node.h>
21 #include "packager/mpd/base/media_info.pb.h"
22 
23 namespace shaka {
24 
25 class MediaInfo;
26 class Representation;
27 
28 struct ContentProtectionElement;
29 struct MpdOptions;
30 
34  public:
35  // The role for this AdaptationSet. These values are used to add a Role
36  // element to the AdaptationSet with schemeIdUri=urn:mpeg:dash:role:2011.
37  // See ISO/IEC 23009-1:2012 section 5.8.5.5.
38  enum Role {
39  kRoleUnknown,
40  kRoleCaption,
41  kRoleSubtitle,
42  kRoleMain,
43  kRoleAlternate,
44  kRoleSupplementary,
45  kRoleCommentary,
46  kRoleDub,
47  kRoleDescription,
48  kRoleSign,
49  kRoleMetadata,
50  kRoleEnhancedAudioIntelligibility,
51  kRoleEmergency,
52  kRoleForcedSubtitle,
53  kRoleEasyreader,
54  kRoleKaraoke,
55  };
56 
57  virtual ~AdaptationSet();
58 
65  virtual Representation* AddRepresentation(const MediaInfo& media_info);
66 
74  const Representation& representation);
75 
85  virtual void AddContentProtectionElement(
86  const ContentProtectionElement& element);
87 
99  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
100  const std::string& pssh);
101 
106  virtual void AddAccessibility(const std::string& scheme,
107  const std::string& value);
108 
113  virtual void AddRole(Role role);
114 
119  std::optional<xml::XmlNode> GetXml();
120 
126  virtual void ForceSetSegmentAlignment(bool segment_alignment);
127 
132  virtual void ForceSubsegmentStartswithSAP(uint32_t sap_value);
133 
137  virtual void ForceStartwithSAP(uint32_t sap_value);
138 
141  virtual void AddAdaptationSetSwitching(const AdaptationSet* adaptation_set);
142 
144  bool has_id() const { return static_cast<bool>(id_); }
145 
147  std::optional<uint32_t> SortIndex() const {
148  return index_.has_value() ? index_ : id_;
149  }
150 
151  // Must be unique in the Period.
152  uint32_t id() const { return id_.value(); }
153 
156  void set_id(uint32_t id) { id_ = id; }
157 
169  void OnNewSegmentForRepresentation(uint32_t representation_id,
170  int64_t start_time,
171  int64_t duration);
172 
185  void OnSetFrameRateForRepresentation(uint32_t representation_id,
186  int32_t frame_duration,
187  int32_t timescale);
188 
191  virtual void AddTrickPlayReference(const AdaptationSet* adaptation_set);
192 
193  // Return the list of Representations in this AdaptationSet.
194  const std::list<Representation*> GetRepresentations() const;
195 
197  bool IsVideo() const;
198 
200  const std::string& codec() const { return codec_; }
201 
204  void set_codec(const std::string& codec) { codec_ = codec; };
205 
207  uint32_t matrix_coefficients() const { return matrix_coefficients_; }
208 
212  matrix_coefficients_ = matrix_coefficients;
213  };
214 
216  uint32_t color_primaries() const { return color_primaries_; }
217 
220  void set_color_primaries(const uint32_t& color_primaries) {
221  color_primaries_ = color_primaries;
222  };
223 
225  uint32_t transfer_characteristics() const {
226  return transfer_characteristics_;
227  }
228 
232  transfer_characteristics_ = transfer_characteristics;
233  };
234 
236  const MediaInfo::ProtectedContent* protected_content() const {
237  return protected_content_;
238  };
239 
242  void set_protected_content(const MediaInfo& media_info);
243 
250  bool MatchAdaptationSet(const MediaInfo& media_info,
251  bool content_protection_in_adaptation_set);
252 
256  bool SwitchableAdaptationSet(const AdaptationSet& adaptation_set);
257 
258  protected:
265  AdaptationSet(const std::string& language,
266  const MpdOptions& mpd_options,
267  uint32_t* representation_counter);
268 
269  private:
270  AdaptationSet(const AdaptationSet&) = delete;
271  AdaptationSet& operator=(const AdaptationSet&) = delete;
272 
273  friend class Period;
274  friend class AdaptationSetTest;
275 
276  // kSegmentAlignmentUnknown means that it is uncertain if the
277  // (sub)segments are aligned or not.
278  // kSegmentAlignmentTrue means that it is certain that the all the (current)
279  // segments added to the adaptation set are aligned.
280  // kSegmentAlignmentFalse means that it is it is certain that some segments
281  // are not aligned. This is useful to disable the computation for
282  // segment alignment, once it is certain that some segments are not aligned.
283  enum SegmentAligmentStatus {
284  kSegmentAlignmentUnknown,
285  kSegmentAlignmentTrue,
286  kSegmentAlignmentFalse
287  };
288 
289  // This maps Representations (IDs) to a list of start times of the segments.
290  // e.g.
291  // If Representation 1 has start time 0, 100, 200 and Representation 2 has
292  // start times 0, 200, 400, then the map contains:
293  // 1 -> [0, 100, 200]
294  // 2 -> [0, 200, 400]
295  typedef std::map<uint32_t, std::list<int64_t>> RepresentationTimeline;
296 
297  // Update AdaptationSet attributes for new MediaInfo.
298  void UpdateFromMediaInfo(const MediaInfo& media_info);
299 
308  void CheckDynamicSegmentAlignment(uint32_t representation_id,
309  int64_t start_time,
310  int64_t duration);
311 
312  // Checks representation_segment_start_times_ and sets segments_aligned_.
313  // Use this for static MPD, do not use for dynamic MPD.
314  void CheckStaticSegmentAlignment();
315 
316  // Records the framerate of a Representation.
317  void RecordFrameRate(int32_t frame_duration, int32_t timescale);
318 
319  std::list<ContentProtectionElement> content_protection_elements_;
320  // representation_id => Representation map. It also keeps the representations_
321  // sorted by default.
322  std::map<uint32_t, std::unique_ptr<Representation>> representation_map_;
323 
324  uint32_t* const representation_counter_;
325 
326  std::optional<uint32_t> id_;
327  const std::string language_;
328  const MpdOptions& mpd_options_;
329 
330  // An array of adaptation sets this adaptation set can switch to.
331  std::vector<const AdaptationSet*> switchable_adaptation_sets_;
332 
333  // Video widths and heights of Representations. Note that this is a set; if
334  // there is only 1 resolution, then @width & @height should be set, otherwise
335  // @maxWidth & @maxHeight should be set for DASH IOP.
336  std::set<uint32_t> video_widths_;
337  std::set<uint32_t> video_heights_;
338 
339  // Video representations' frame rates.
340  // The frame rate notation for MPD is <integer>/<integer> (where the
341  // denominator is optional). This means the frame rate could be non-whole
342  // rational value, therefore the key is of type double.
343  // Value is <integer>/<integer> in string form.
344  // So, key == CalculatedValue(value)
345  std::map<double, std::string> video_frame_rates_;
346 
347  // contentType attribute of AdaptationSet.
348  // Determined by examining the MediaInfo passed to AddRepresentation().
349  std::string content_type_;
350 
351  // Codec of AdaptationSet.
352  std::string codec_;
353 
354  // This does not have to be a set, it could be a list or vector because all we
355  // really care is whether there is more than one entry.
356  // Contains one entry if all the Representations have the same picture aspect
357  // ratio (@par attribute for AdaptationSet).
358  // There will be more than one entry if there are multiple picture aspect
359  // ratios.
360  // The @par attribute should only be set if there is exactly one entry
361  // in this set.
362  std::set<std::string> picture_aspect_ratio_;
363 
364  // accessibilities of this AdaptationSet.
365  struct Accessibility {
366  std::string scheme;
367  std::string value;
368  };
369  std::vector<Accessibility> accessibilities_;
370 
371  // The roles of this AdaptationSet.
372  std::set<Role> roles_;
373 
374  // True iff all the segments are aligned.
375  SegmentAligmentStatus segments_aligned_ = kSegmentAlignmentUnknown;
376  bool force_set_segment_alignment_ = false;
377 
378  // The stream access point for subsegment
379  uint8_t subsegment_start_with_sap_ = 0;
380 
381  // The stream access point for segment
382  uint8_t start_with_sap_ = 0;
383 
384  // Keeps track of segment start times of Representations.
385  // For static MPD, this will not be cleared, all the segment start times are
386  // stored in this. This should not out-of-memory for a reasonable length
387  // video and reasonable subsegment length.
388  // For dynamic MPD, the entries are deleted (see
389  // CheckDynamicSegmentAlignment() implementation comment) because storing the
390  // entire timeline is not reasonable and may cause an out-of-memory problem.
391  RepresentationTimeline representation_segment_start_times_;
392 
393  // Record the original AdaptationSets the trick play stream belongs to. There
394  // can be more than one reference AdaptationSets as multiple streams e.g. SD
395  // and HD videos in different AdaptationSets can share the same trick play
396  // stream.
397  std::vector<const AdaptationSet*> trick_play_references_;
398 
399  // Matrix Coefficients.
400  uint32_t matrix_coefficients_ = 0;
401  // Colour Primaries.
402  uint32_t color_primaries_ = 0;
403 
404  // Transfer characteristics.
405  uint32_t transfer_characteristics_ = 0;
406 
407  // the command-line index for this AdaptationSet
408  std::optional<uint32_t> index_;
409 
410  // The label of this AdaptationSet.
411  std::string label_;
412 
413  // ProtectedContent of this AdaptationSet.
414  MediaInfo::ProtectedContent* protected_content_;
415 };
416 
417 } // namespace shaka
418 
419 #endif // PACKAGER_MPD_BASE_ADAPTATION_SET_H_
uint32_t transfer_characteristics() const
void OnNewSegmentForRepresentation(uint32_t representation_id, int64_t start_time, int64_t duration)
virtual Representation * AddRepresentation(const MediaInfo &media_info)
virtual void AddAccessibility(const std::string &scheme, const std::string &value)
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
virtual void ForceStartwithSAP(uint32_t sap_value)
virtual void ForceSetSegmentAlignment(bool segment_alignment)
const std::string & codec() const
void OnSetFrameRateForRepresentation(uint32_t representation_id, int32_t frame_duration, int32_t timescale)
virtual Representation * CopyRepresentation(const Representation &representation)
void set_matrix_coefficients(const uint32_t &matrix_coefficients)
void set_color_primaries(const uint32_t &color_primaries)
virtual void ForceSubsegmentStartswithSAP(uint32_t sap_value)
bool MatchAdaptationSet(const MediaInfo &media_info, bool content_protection_in_adaptation_set)
virtual void AddTrickPlayReference(const AdaptationSet *adaptation_set)
virtual void AddAdaptationSetSwitching(const AdaptationSet *adaptation_set)
std::optional< uint32_t > SortIndex() const
std::optional< xml::XmlNode > GetXml()
AdaptationSet(const std::string &language, const MpdOptions &mpd_options, uint32_t *representation_counter)
uint32_t color_primaries() const
const MediaInfo::ProtectedContent * protected_content() const
Return ProtectedContent.
void set_protected_content(const MediaInfo &media_info)
uint32_t matrix_coefficients() const
void set_transfer_characteristics(const uint32_t &transfer_characteristics)
void set_codec(const std::string &codec)
bool SwitchableAdaptationSet(const AdaptationSet &adaptation_set)
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
virtual void AddRole(Role role)
void set_id(uint32_t id)
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66
Defines Mpd Options.
Definition: mpd_options.h:25