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/xml_node.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  };
48 
49  virtual ~AdaptationSet();
50 
57  virtual Representation* AddRepresentation(const MediaInfo& media_info);
58 
66  const Representation& representation);
67 
77  virtual void AddContentProtectionElement(
78  const ContentProtectionElement& element);
79 
91  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
92  const std::string& pssh);
93 
98  virtual void AddAccessibility(const std::string& scheme,
99  const std::string& value);
100 
105  virtual void AddRole(Role role);
106 
111  base::Optional<xml::XmlNode> GetXml();
112 
118  virtual void ForceSetSegmentAlignment(bool segment_alignment);
119 
122  virtual void AddAdaptationSetSwitching(const AdaptationSet* adaptation_set);
123 
125  bool has_id() const { return static_cast<bool>(id_); }
126 
127  // Must be unique in the Period.
128  uint32_t id() const { return id_.value(); }
129 
132  void set_id(uint32_t id) { id_ = id; }
133 
145  void OnNewSegmentForRepresentation(uint32_t representation_id,
146  uint64_t start_time,
147  uint64_t duration);
148 
161  void OnSetFrameRateForRepresentation(uint32_t representation_id,
162  uint32_t frame_duration,
163  uint32_t timescale);
164 
167  virtual void AddTrickPlayReference(const AdaptationSet* adaptation_set);
168 
169  // Return the list of Representations in this AdaptationSet.
170  const std::list<Representation*> GetRepresentations() const;
171 
173  bool IsVideo() const;
174 
176  const std::string& codec() const { return codec_; }
177 
180  void set_codec(const std::string& codec) { codec_ = codec; };
181 
182  protected:
189  AdaptationSet(const std::string& language,
190  const MpdOptions& mpd_options,
191  uint32_t* representation_counter);
192 
193  private:
194  AdaptationSet(const AdaptationSet&) = delete;
195  AdaptationSet& operator=(const AdaptationSet&) = delete;
196 
197  friend class Period;
198  friend class AdaptationSetTest;
199 
200  // kSegmentAlignmentUnknown means that it is uncertain if the
201  // (sub)segments are aligned or not.
202  // kSegmentAlignmentTrue means that it is certain that the all the (current)
203  // segments added to the adaptation set are aligned.
204  // kSegmentAlignmentFalse means that it is it is certain that some segments
205  // are not aligned. This is useful to disable the computation for
206  // segment alignment, once it is certain that some segments are not aligned.
207  enum SegmentAligmentStatus {
208  kSegmentAlignmentUnknown,
209  kSegmentAlignmentTrue,
210  kSegmentAlignmentFalse
211  };
212 
213  // This maps Representations (IDs) to a list of start times of the segments.
214  // e.g.
215  // If Representation 1 has start time 0, 100, 200 and Representation 2 has
216  // start times 0, 200, 400, then the map contains:
217  // 1 -> [0, 100, 200]
218  // 2 -> [0, 200, 400]
219  typedef std::map<uint32_t, std::list<uint64_t>> RepresentationTimeline;
220 
221  // Update AdaptationSet attributes for new MediaInfo.
222  void UpdateFromMediaInfo(const MediaInfo& media_info);
223 
232  void CheckDynamicSegmentAlignment(uint32_t representation_id,
233  uint64_t start_time,
234  uint64_t duration);
235 
236  // Checks representation_segment_start_times_ and sets segments_aligned_.
237  // Use this for static MPD, do not use for dynamic MPD.
238  void CheckStaticSegmentAlignment();
239 
240  // Records the framerate of a Representation.
241  void RecordFrameRate(uint32_t frame_duration, uint32_t timescale);
242 
243  std::list<ContentProtectionElement> content_protection_elements_;
244  // representation_id => Representation map. It also keeps the representations_
245  // sorted by default.
246  std::map<uint32_t, std::unique_ptr<Representation>> representation_map_;
247 
248  uint32_t* const representation_counter_;
249 
250  base::Optional<uint32_t> id_;
251  const std::string language_;
252  const MpdOptions& mpd_options_;
253 
254  // An array of adaptation sets this adaptation set can switch to.
255  std::vector<const AdaptationSet*> switchable_adaptation_sets_;
256 
257  // Video widths and heights of Representations. Note that this is a set; if
258  // there is only 1 resolution, then @width & @height should be set, otherwise
259  // @maxWidth & @maxHeight should be set for DASH IOP.
260  std::set<uint32_t> video_widths_;
261  std::set<uint32_t> video_heights_;
262 
263  // Video representations' frame rates.
264  // The frame rate notation for MPD is <integer>/<integer> (where the
265  // denominator is optional). This means the frame rate could be non-whole
266  // rational value, therefore the key is of type double.
267  // Value is <integer>/<integer> in string form.
268  // So, key == CalculatedValue(value)
269  std::map<double, std::string> video_frame_rates_;
270 
271  // contentType attribute of AdaptationSet.
272  // Determined by examining the MediaInfo passed to AddRepresentation().
273  std::string content_type_;
274 
275  // Codec of AdaptationSet.
276  std::string codec_;
277 
278  // This does not have to be a set, it could be a list or vector because all we
279  // really care is whether there is more than one entry.
280  // Contains one entry if all the Representations have the same picture aspect
281  // ratio (@par attribute for AdaptationSet).
282  // There will be more than one entry if there are multiple picture aspect
283  // ratios.
284  // The @par attribute should only be set if there is exactly one entry
285  // in this set.
286  std::set<std::string> picture_aspect_ratio_;
287 
288  // accessibilities of this AdaptationSet.
289  struct Accessibility {
290  std::string scheme;
291  std::string value;
292  };
293  std::vector<Accessibility> accessibilities_;
294 
295  // The roles of this AdaptationSet.
296  std::set<Role> roles_;
297 
298  // True iff all the segments are aligned.
299  SegmentAligmentStatus segments_aligned_;
300  bool force_set_segment_alignment_;
301 
302  // Keeps track of segment start times of Representations.
303  // For static MPD, this will not be cleared, all the segment start times are
304  // stored in this. This should not out-of-memory for a reasonable length
305  // video and reasonable subsegment length.
306  // For dynamic MPD, the entries are deleted (see
307  // CheckDynamicSegmentAlignment() implementation comment) because storing the
308  // entire timeline is not reasonable and may cause an out-of-memory problem.
309  RepresentationTimeline representation_segment_start_times_;
310 
311  // Record the original AdaptationSets the trick play stream belongs to. There
312  // can be more than one reference AdaptationSets as multiple streams e.g. SD
313  // and HD videos in different AdaptationSets can share the same trick play
314  // stream.
315  std::vector<const AdaptationSet*> trick_play_references_;
316 };
317 
318 } // namespace shaka
319 
320 #endif // PACKAGER_MPD_BASE_ADAPTATION_SET_H_
shaka::MpdOptions
Defines Mpd Options.
Definition: mpd_options.h:25
shaka::AdaptationSet::codec
const std::string & codec() const
Definition: adaptation_set.h:176
shaka::AdaptationSet::OnSetFrameRateForRepresentation
void OnSetFrameRateForRepresentation(uint32_t representation_id, uint32_t frame_duration, uint32_t timescale)
Definition: adaptation_set.cc:404
shaka::Period
Definition: period.h:26
shaka::AdaptationSet::ForceSetSegmentAlignment
virtual void ForceSetSegmentAlignment(bool segment_alignment)
Definition: adaptation_set.cc:376
shaka::AdaptationSet::has_id
bool has_id() const
Definition: adaptation_set.h:125
shaka::AdaptationSet::IsVideo
bool IsVideo() const
Definition: adaptation_set.cc:422
shaka::AdaptationSet
Definition: adaptation_set.h:33
shaka::AdaptationSet::OnNewSegmentForRepresentation
void OnNewSegmentForRepresentation(uint32_t representation_id, uint64_t start_time, uint64_t duration)
Definition: adaptation_set.cc:393
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::AdaptationSet::AdaptationSet
AdaptationSet(const std::string &language, const MpdOptions &mpd_options, uint32_t *representation_counter)
Definition: adaptation_set.cc:166
shaka::AdaptationSet::GetXml
base::Optional< xml::XmlNode > GetXml()
Definition: adaptation_set.cc:240
shaka::AdaptationSet::UpdateContentProtectionPssh
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
Definition: adaptation_set.cc:219
shaka::Representation
Definition: representation.h:50
shaka::AdaptationSet::AddAccessibility
virtual void AddAccessibility(const std::string &scheme, const std::string &value)
Definition: adaptation_set.cc:225
shaka::AdaptationSet::set_id
void set_id(uint32_t id)
Definition: adaptation_set.h:132
shaka::AdaptationSet::AddRepresentation
virtual Representation * AddRepresentation(const MediaInfo &media_info)
Definition: adaptation_set.cc:179
shaka::AdaptationSet::AddContentProtectionElement
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
Definition: adaptation_set.cc:213
shaka::AdaptationSet::CopyRepresentation
virtual Representation * CopyRepresentation(const Representation &representation)
Definition: adaptation_set.cc:198
shaka::AdaptationSet::AddAdaptationSetSwitching
virtual void AddAdaptationSetSwitching(const AdaptationSet *adaptation_set)
Definition: adaptation_set.cc:382
shaka::AdaptationSet::AddRole
virtual void AddRole(Role role)
Definition: adaptation_set.cc:230
shaka::AdaptationSet::set_codec
void set_codec(const std::string &codec)
Definition: adaptation_set.h:180
shaka::AdaptationSet::AddTrickPlayReference
virtual void AddTrickPlayReference(const AdaptationSet *adaptation_set)
Definition: adaptation_set.cc:410
shaka::ContentProtectionElement
Definition: content_protection_element.h:36