DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
mpd_builder.h
1 // Copyright 2014 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 //
7 // This file contains the MpdBuilder, AdaptationSet, and Representation class
8 // declarations.
9 // http://goo.gl/UrsSlF
10 //
14 
15 #ifndef MPD_BASE_MPD_BUILDER_H_
16 #define MPD_BASE_MPD_BUILDER_H_
17 
18 #include <stdint.h>
19 
20 #include <list>
21 #include <map>
22 #include <set>
23 #include <string>
24 
25 #include "packager/base/atomic_sequence_num.h"
26 #include "packager/base/callback.h"
27 #include "packager/base/gtest_prod_util.h"
28 #include "packager/base/time/clock.h"
29 #include "packager/base/time/time.h"
30 #include "packager/mpd/base/bandwidth_estimator.h"
31 #include "packager/mpd/base/content_protection_element.h"
32 #include "packager/mpd/base/media_info.pb.h"
33 #include "packager/mpd/base/mpd_options.h"
34 #include "packager/mpd/base/segment_info.h"
35 #include "packager/mpd/base/xml/scoped_xml_ptr.h"
36 
37 // TODO(rkuroiwa): For classes with |id_|, consider removing the field and let
38 // the MPD (XML) generation functions take care of assigning an ID to each
39 // element.
40 namespace shaka {
41 
42 namespace media {
43 class File;
44 } // namespace media
45 
46 class AdaptationSet;
47 class Representation;
48 
49 namespace xml {
50 
51 class XmlNode;
52 class RepresentationXmlNode;
53 
54 } // namespace xml
55 
57 class MpdBuilder {
58  public:
59  enum MpdType {
60  kStatic = 0,
61  kDynamic
62  };
63 
67  MpdBuilder(MpdType type, const MpdOptions& mpd_options);
68  virtual ~MpdBuilder();
69 
72  void AddBaseUrl(const std::string& base_url);
73 
78  virtual AdaptationSet* AddAdaptationSet(const std::string& lang);
79 
84  bool WriteMpdToFile(media::File* output_file);
85 
89  virtual bool ToString(std::string* output);
90 
92  MpdType type() const { return type_; }
93 
99  static void MakePathsRelativeToMpd(const std::string& mpd_path,
100  MediaInfo* media_info);
101 
102  // Inject a |clock| that returns the current time.
104  void InjectClockForTesting(std::unique_ptr<base::Clock> clock) {
105  clock_ = std::move(clock);
106  }
107 
108  private:
109  // DynamicMpdBuilderTest needs to set availabilityStartTime so that the test
110  // doesn't need to depend on current time.
111  friend class DynamicMpdBuilderTest;
112 
113  bool ToStringImpl(std::string* output);
114 
115  // This is a helper method for writing out MPDs, called from WriteMpdToFile()
116  // and ToString().
117  template <typename OutputType>
118  bool WriteMpdToOutput(OutputType* output);
119 
120  // Returns the document pointer to the MPD. This must be freed by the caller
121  // using appropriate xmlDocPtr freeing function.
122  // On failure, this returns NULL.
123  xmlDocPtr GenerateMpd();
124 
125  // Set MPD attributes common to all profiles. Uses non-zero |mpd_options_| to
126  // set attributes for the MPD.
127  void AddCommonMpdInfo(xml::XmlNode* mpd_node);
128 
129  // Adds 'static' MPD attributes and elements to |mpd_node|. This assumes that
130  // the first child element is a Period element.
131  void AddStaticMpdInfo(xml::XmlNode* mpd_node);
132 
133  // Same as AddStaticMpdInfo() but for 'dynamic' MPDs.
134  void AddDynamicMpdInfo(xml::XmlNode* mpd_node);
135 
136  float GetStaticMpdDuration(xml::XmlNode* mpd_node);
137 
138  // Set MPD attributes for dynamic profile MPD. Uses non-zero |mpd_options_| as
139  // well as various calculations to set attributes for the MPD.
140  void SetDynamicMpdAttributes(xml::XmlNode* mpd_node);
141 
142  // Gets the earliest, normalized segment timestamp. Returns true if
143  // successful, false otherwise.
144  bool GetEarliestTimestamp(double* timestamp_seconds);
145 
146  MpdType type_;
147  MpdOptions mpd_options_;
148  std::list<std::unique_ptr<AdaptationSet>> adaptation_sets_;
149 
150  std::list<std::string> base_urls_;
151  std::string availability_start_time_;
152 
153  base::AtomicSequenceNumber adaptation_set_counter_;
154  base::AtomicSequenceNumber representation_counter_;
155 
156  // By default, this returns the current time. This can be injected for
157  // testing.
158  std::unique_ptr<base::Clock> clock_;
159 
160  DISALLOW_COPY_AND_ASSIGN(MpdBuilder);
161 };
162 
166  public:
167  // The role for this AdaptationSet. These values are used to add a Role
168  // element to the AdaptationSet with schemeIdUri=urn:mpeg:dash:role:2011.
169  // See ISO/IEC 23009-1:2012 section 5.8.5.5.
170  enum Role {
171  kRoleCaption,
172  kRoleSubtitle,
173  kRoleMain,
174  kRoleAlternate,
175  kRoleSupplementary,
176  kRoleCommentary,
177  kRoleDub
178  };
179 
180  virtual ~AdaptationSet();
181 
188  virtual Representation* AddRepresentation(const MediaInfo& media_info);
189 
199  virtual void AddContentProtectionElement(
200  const ContentProtectionElement& element);
201 
213  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
214  const std::string& pssh);
215 
220  virtual void AddRole(Role role);
221 
226  xml::scoped_xml_ptr<xmlNode> GetXml();
227 
233  virtual void ForceSetSegmentAlignment(bool segment_alignment);
234 
237  void AddAdaptationSetSwitching(uint32_t adaptation_set_id);
238 
240  const std::vector<uint32_t>& adaptation_set_switching_ids() const {
241  return adaptation_set_switching_ids_;
242  }
243 
244  // Must be unique in the Period.
245  uint32_t id() const { return id_; }
246 
258  void OnNewSegmentForRepresentation(uint32_t representation_id,
259  uint64_t start_time,
260  uint64_t duration);
261 
274  void OnSetFrameRateForRepresentation(uint32_t representation_id,
275  uint32_t frame_duration,
276  uint32_t timescale);
277 
278  protected:
286  AdaptationSet(uint32_t adaptation_set_id,
287  const std::string& lang,
288  const MpdOptions& mpd_options,
289  MpdBuilder::MpdType mpd_type,
290  base::AtomicSequenceNumber* representation_counter);
291 
292  private:
293  friend class MpdBuilder;
294  template <MpdBuilder::MpdType type>
295  friend class MpdBuilderTest;
296 
297  // kSegmentAlignmentUnknown means that it is uncertain if the
298  // (sub)segments are aligned or not.
299  // kSegmentAlignmentTrue means that it is certain that the all the (current)
300  // segments added to the adaptation set are aligned.
301  // kSegmentAlignmentFalse means that it is it is certain that some segments
302  // are not aligned. This is useful to disable the computation for
303  // segment alignment, once it is certain that some segments are not aligned.
304  enum SegmentAligmentStatus {
305  kSegmentAlignmentUnknown,
306  kSegmentAlignmentTrue,
307  kSegmentAlignmentFalse
308  };
309 
310  // This maps Representations (IDs) to a list of start times of the segments.
311  // e.g.
312  // If Representation 1 has start time 0, 100, 200 and Representation 2 has
313  // start times 0, 200, 400, then the map contains:
314  // 1 -> [0, 100, 200]
315  // 2 -> [0, 200, 400]
316  typedef std::map<uint32_t, std::list<uint64_t> > RepresentationTimeline;
317 
318  // Gets the earliest, normalized segment timestamp. Returns true if
319  // successful, false otherwise.
320  bool GetEarliestTimestamp(double* timestamp_seconds);
321 
329  void CheckLiveSegmentAlignment(uint32_t representation_id,
330  uint64_t start_time,
331  uint64_t duration);
332 
333  // Checks representation_segment_start_times_ and sets segments_aligned_.
334  // Use this for VOD, do not use for Live.
335  void CheckVodSegmentAlignment();
336 
337  // Records the framerate of a Representation.
338  void RecordFrameRate(uint32_t frame_duration, uint32_t timescale);
339 
340  std::list<ContentProtectionElement> content_protection_elements_;
341  std::list<std::unique_ptr<Representation>> representations_;
342 
343  base::AtomicSequenceNumber* const representation_counter_;
344 
345  const uint32_t id_;
346  const std::string lang_;
347  const MpdOptions& mpd_options_;
348  const MpdBuilder::MpdType mpd_type_;
349 
350  // The ids of the adaptation sets this adaptation set can switch to.
351  std::vector<uint32_t> adaptation_set_switching_ids_;
352 
353  // Video widths and heights of Representations. Note that this is a set; if
354  // there is only 1 resolution, then @width & @height should be set, otherwise
355  // @maxWidth & @maxHeight should be set for DASH IOP.
356  std::set<uint32_t> video_widths_;
357  std::set<uint32_t> video_heights_;
358 
359  // Video representations' frame rates.
360  // The frame rate notation for MPD is <integer>/<integer> (where the
361  // denominator is optional). This means the frame rate could be non-whole
362  // rational value, therefore the key is of type double.
363  // Value is <integer>/<integer> in string form.
364  // So, key == CalculatedValue(value)
365  std::map<double, std::string> video_frame_rates_;
366 
367  // contentType attribute of AdaptationSet.
368  // Determined by examining the MediaInfo passed to AddRepresentation().
369  std::string content_type_;
370 
371  // This does not have to be a set, it could be a list or vector because all we
372  // really care is whether there is more than one entry.
373  // Contains one entry if all the Representations have the same picture aspect
374  // ratio (@par attribute for AdaptationSet).
375  // There will be more than one entry if there are multiple picture aspect
376  // ratios.
377  // The @par attribute should only be set if there is exactly one entry
378  // in this set.
379  std::set<std::string> picture_aspect_ratio_;
380 
381  // The roles of this AdaptationSet.
382  std::set<Role> roles_;
383 
384  // True iff all the segments are aligned.
385  SegmentAligmentStatus segments_aligned_;
386  bool force_set_segment_alignment_;
387 
388  // Keeps track of segment start times of Representations.
389  // For VOD, this will not be cleared, all the segment start times are
390  // stored in this. This should not out-of-memory for a reasonable length
391  // video and reasonable subsegment length.
392  // For Live, the entries are deleted (see CheckLiveSegmentAlignment()
393  // implementation comment) because storing the entire timeline is not
394  // reasonable and may cause an out-of-memory problem.
395  RepresentationTimeline representation_segment_start_times_;
396 
397  DISALLOW_COPY_AND_ASSIGN(AdaptationSet);
398 };
399 
401  public:
404 
409  virtual void OnNewSegmentForRepresentation(uint64_t start_time,
410  uint64_t duration) = 0;
411 
416  virtual void OnSetFrameRateForRepresentation(uint32_t frame_duration,
417  uint32_t timescale) = 0;
418 };
419 
423  public:
424  enum SuppressFlag {
425  kSuppressWidth = 1,
426  kSuppressHeight = 2,
427  kSuppressFrameRate = 4,
428  };
429 
430  virtual ~Representation();
431 
435  bool Init();
436 
447  virtual void AddContentProtectionElement(
448  const ContentProtectionElement& element);
449 
461  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
462  const std::string& pssh);
463 
472  virtual void AddNewSegment(uint64_t start_time,
473  uint64_t duration,
474  uint64_t size);
475 
481  virtual void SetSampleDuration(uint32_t sample_duration);
482 
484  xml::scoped_xml_ptr<xmlNode> GetXml();
485 
494  void SuppressOnce(SuppressFlag flag);
495 
497  uint32_t id() const { return id_; }
498 
499  protected:
509  const MediaInfo& media_info,
510  const MpdOptions& mpd_options,
511  uint32_t representation_id,
512  std::unique_ptr<RepresentationStateChangeListener> state_change_listener);
513 
514  private:
515  friend class AdaptationSet;
516  template <MpdBuilder::MpdType type>
517  friend class MpdBuilderTest;
518 
519  bool AddLiveInfo(xml::RepresentationXmlNode* representation);
520 
521  // Returns true if |media_info_| has required fields to generate a valid
522  // Representation. Otherwise returns false.
523  bool HasRequiredMediaInfoFields();
524 
525  // Return false if the segment should be considered a new segment. True if the
526  // segment is contiguous.
527  bool IsContiguous(uint64_t start_time,
528  uint64_t duration,
529  uint64_t size) const;
530 
531  // Remove elements from |segment_infos_| if
532  // mpd_options_.time_shift_buffer_depth is specified. Increments
533  // |start_number_| by the number of segments removed.
534  void SlideWindow();
535 
536  // Note: Because 'mimeType' is a required field for a valid MPD, these return
537  // strings.
538  std::string GetVideoMimeType() const;
539  std::string GetAudioMimeType() const;
540  std::string GetTextMimeType() const;
541 
542  // Gets the earliest, normalized segment timestamp. Returns true if
543  // successful, false otherwise.
544  bool GetEarliestTimestamp(double* timestamp_seconds);
545 
546  // Init() checks that only one of VideoInfo, AudioInfo, or TextInfo is set. So
547  // any logic using this can assume only one set.
548  MediaInfo media_info_;
549  std::list<ContentProtectionElement> content_protection_elements_;
550  std::list<SegmentInfo> segment_infos_;
551 
552  const uint32_t id_;
553  std::string mime_type_;
554  std::string codecs_;
555  BandwidthEstimator bandwidth_estimator_;
556  const MpdOptions& mpd_options_;
557 
558  // startNumber attribute for SegmentTemplate.
559  // Starts from 1.
560  uint32_t start_number_;
561 
562  // If this is not null, then Representation is responsible for calling the
563  // right methods at right timings.
564  std::unique_ptr<RepresentationStateChangeListener> state_change_listener_;
565 
566  // Bit vector for tracking witch attributes should not be output.
567  int output_suppression_flags_;
568 
569  DISALLOW_COPY_AND_ASSIGN(Representation);
570 };
571 
572 } // namespace shaka
573 
574 #endif // MPD_BASE_MPD_BUILDER_H_
void OnSetFrameRateForRepresentation(uint32_t representation_id, uint32_t frame_duration, uint32_t timescale)
Definition: mpd_builder.cc:871
RepresentationType in MPD.
Definition: xml_node.h:134
virtual void AddNewSegment(uint64_t start_time, uint64_t duration, uint64_t size)
virtual void OnSetFrameRateForRepresentation(uint32_t frame_duration, uint32_t timescale)=0
bool WriteMpdToFile(media::File *output_file)
Definition: mpd_builder.cc:424
Representation(const MediaInfo &media_info, const MpdOptions &mpd_options, uint32_t representation_id, std::unique_ptr< RepresentationStateChangeListener > state_change_listener)
AdaptationSet(uint32_t adaptation_set_id, const std::string &lang, const MpdOptions &mpd_options, MpdBuilder::MpdType mpd_type, base::AtomicSequenceNumber *representation_counter)
Definition: mpd_builder.cc:673
virtual void SetSampleDuration(uint32_t sample_duration)
virtual Representation * AddRepresentation(const MediaInfo &media_info)
Definition: mpd_builder.cc:690
This class generates DASH MPDs (Media Presentation Descriptions).
Definition: mpd_builder.h:57
uint32_t id() const
Definition: mpd_builder.h:497
Define an abstract file interface.
Definition: file.h:24
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
Definition: mpd_builder.cc:734
virtual void AddRole(Role role)
Definition: mpd_builder.cc:746
void AddBaseUrl(const std::string &base_url)
Definition: mpd_builder.cc:406
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
Definition: mpd_builder.cc:740
MpdType type() const
Definition: mpd_builder.h:92
xml::scoped_xml_ptr< xmlNode > GetXml()
virtual bool ToString(std::string *output)
Definition: mpd_builder.cc:429
void AddAdaptationSetSwitching(uint32_t adaptation_set_id)
Definition: mpd_builder.cc:849
const std::vector< uint32_t > & adaptation_set_switching_ids() const
Definition: mpd_builder.h:240
virtual void ForceSetSegmentAlignment(bool segment_alignment)
Definition: mpd_builder.cc:843
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)
Definition: mpd_builder.cc:645
MpdBuilder(MpdType type, const MpdOptions &mpd_options)
Definition: mpd_builder.cc:399
xml::scoped_xml_ptr< xmlNode > GetXml()
Definition: mpd_builder.cc:756
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
virtual AdaptationSet * AddAdaptationSet(const std::string &lang)
Definition: mpd_builder.cc:410
Defines Mpd Options.
Definition: mpd_options.h:15
void OnNewSegmentForRepresentation(uint32_t representation_id, uint64_t start_time, uint64_t duration)
Definition: mpd_builder.cc:860
void InjectClockForTesting(std::unique_ptr< base::Clock > clock)
This is for testing.
Definition: mpd_builder.h:104
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
virtual void OnNewSegmentForRepresentation(uint64_t start_time, uint64_t duration)=0
void SuppressOnce(SuppressFlag flag)