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/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 
70  virtual Representation* CopyRepresentationWithTimeOffset(
71  const Representation& representation,
72  uint64_t presentation_time_offset);
73 
83  virtual void AddContentProtectionElement(
84  const ContentProtectionElement& element);
85 
97  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
98  const std::string& pssh);
99 
104  virtual void AddRole(Role role);
105 
110  xml::scoped_xml_ptr<xmlNode> GetXml();
111 
117  virtual void ForceSetSegmentAlignment(bool segment_alignment);
118 
121  virtual void AddAdaptationSetSwitching(uint32_t adaptation_set_id);
122 
123  // Must be unique in the Period.
124  uint32_t id() const { return id_; }
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 
164  virtual void AddTrickPlayReferenceId(uint32_t id);
165 
166  // Return the list of Representations in this AdaptationSet.
167  const std::list<Representation*> GetRepresentations() const;
168 
169  protected:
177  AdaptationSet(uint32_t adaptation_set_id,
178  const std::string& lang,
179  const MpdOptions& mpd_options,
180  base::AtomicSequenceNumber* 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 
220  void CheckLiveSegmentAlignment(uint32_t representation_id,
221  uint64_t start_time,
222  uint64_t duration);
223 
224  // Checks representation_segment_start_times_ and sets segments_aligned_.
225  // Use this for VOD, do not use for Live.
226  void CheckVodSegmentAlignment();
227 
228  // Records the framerate of a Representation.
229  void RecordFrameRate(uint32_t frame_duration, uint32_t timescale);
230 
231  std::list<ContentProtectionElement> content_protection_elements_;
232  // representation_id => Representation map. It also keeps the representations_
233  // sorted by default.
234  std::map<uint32_t, std::unique_ptr<Representation>> representation_map_;
235 
236  base::AtomicSequenceNumber* const representation_counter_;
237 
238  uint32_t id_;
239  const std::string lang_;
240  const MpdOptions& mpd_options_;
241 
242  // The ids of the adaptation sets this adaptation set can switch to.
243  std::vector<uint32_t> adaptation_set_switching_ids_;
244 
245  // Video widths and heights of Representations. Note that this is a set; if
246  // there is only 1 resolution, then @width & @height should be set, otherwise
247  // @maxWidth & @maxHeight should be set for DASH IOP.
248  std::set<uint32_t> video_widths_;
249  std::set<uint32_t> video_heights_;
250 
251  // Video representations' frame rates.
252  // The frame rate notation for MPD is <integer>/<integer> (where the
253  // denominator is optional). This means the frame rate could be non-whole
254  // rational value, therefore the key is of type double.
255  // Value is <integer>/<integer> in string form.
256  // So, key == CalculatedValue(value)
257  std::map<double, std::string> video_frame_rates_;
258 
259  // contentType attribute of AdaptationSet.
260  // Determined by examining the MediaInfo passed to AddRepresentation().
261  std::string content_type_;
262 
263  // This does not have to be a set, it could be a list or vector because all we
264  // really care is whether there is more than one entry.
265  // Contains one entry if all the Representations have the same picture aspect
266  // ratio (@par attribute for AdaptationSet).
267  // There will be more than one entry if there are multiple picture aspect
268  // ratios.
269  // The @par attribute should only be set if there is exactly one entry
270  // in this set.
271  std::set<std::string> picture_aspect_ratio_;
272 
273  // The roles of this AdaptationSet.
274  std::set<Role> roles_;
275 
276  // True iff all the segments are aligned.
277  SegmentAligmentStatus segments_aligned_;
278  bool force_set_segment_alignment_;
279 
280  // Keeps track of segment start times of Representations.
281  // For VOD, this will not be cleared, all the segment start times are
282  // stored in this. This should not out-of-memory for a reasonable length
283  // video and reasonable subsegment length.
284  // For Live, the entries are deleted (see CheckLiveSegmentAlignment()
285  // implementation comment) because storing the entire timeline is not
286  // reasonable and may cause an out-of-memory problem.
287  RepresentationTimeline representation_segment_start_times_;
288 
289  // Record the reference id for the original adaptation sets the trick play
290  // stream belongs to. This is a set because the trick play streams may be for
291  // multiple AdaptationSets (e.g. SD and HD videos in different AdaptationSets
292  // can share the same trick play stream.)
293  std::set<uint32_t> trick_play_reference_ids_;
294 };
295 
296 } // namespace shaka
297 
298 #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)