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:
61  explicit MpdBuilder(const MpdOptions& mpd_options);
62  virtual ~MpdBuilder();
63 
66  void AddBaseUrl(const std::string& base_url);
67 
72  virtual AdaptationSet* AddAdaptationSet(const std::string& lang);
73 
78  bool WriteMpdToFile(media::File* output_file);
79 
83  virtual bool ToString(std::string* output);
84 
90  static void MakePathsRelativeToMpd(const std::string& mpd_path,
91  MediaInfo* media_info);
92 
93  // Inject a |clock| that returns the current time.
95  void InjectClockForTesting(std::unique_ptr<base::Clock> clock) {
96  clock_ = std::move(clock);
97  }
98 
99  private:
100  // LiveMpdBuilderTest needs to set availabilityStartTime so that the test
101  // doesn't need to depend on current time.
102  friend class LiveMpdBuilderTest;
103  template <DashProfile profile>
104  friend class MpdBuilderTest;
105 
106  bool ToStringImpl(std::string* output);
107 
108  // This is a helper method for writing out MPDs, called from WriteMpdToFile()
109  // and ToString().
110  template <typename OutputType>
111  bool WriteMpdToOutput(OutputType* output);
112 
113  // Returns the document pointer to the MPD. This must be freed by the caller
114  // using appropriate xmlDocPtr freeing function.
115  // On failure, this returns NULL.
116  xmlDocPtr GenerateMpd();
117 
118  // Set MPD attributes common to all profiles. Uses non-zero |mpd_options_| to
119  // set attributes for the MPD.
120  void AddCommonMpdInfo(xml::XmlNode* mpd_node);
121 
122  // Adds 'static' MPD attributes and elements to |mpd_node|. This assumes that
123  // the first child element is a Period element.
124  void AddStaticMpdInfo(xml::XmlNode* mpd_node);
125 
126  // Same as AddStaticMpdInfo() but for 'dynamic' MPDs.
127  void AddDynamicMpdInfo(xml::XmlNode* mpd_node);
128 
129  float GetStaticMpdDuration(xml::XmlNode* mpd_node);
130 
131  // Set MPD attributes for dynamic profile MPD. Uses non-zero |mpd_options_| as
132  // well as various calculations to set attributes for the MPD.
133  void SetDynamicMpdAttributes(xml::XmlNode* mpd_node);
134 
135  // Gets the earliest, normalized segment timestamp. Returns true if
136  // successful, false otherwise.
137  bool GetEarliestTimestamp(double* timestamp_seconds);
138 
139  MpdOptions mpd_options_;
140  std::list<std::unique_ptr<AdaptationSet>> adaptation_sets_;
141 
142  std::list<std::string> base_urls_;
143  std::string availability_start_time_;
144 
145  base::AtomicSequenceNumber adaptation_set_counter_;
146  base::AtomicSequenceNumber representation_counter_;
147 
148  // By default, this returns the current time. This can be injected for
149  // testing.
150  std::unique_ptr<base::Clock> clock_;
151 
152  DISALLOW_COPY_AND_ASSIGN(MpdBuilder);
153 };
154 
158  public:
159  // The role for this AdaptationSet. These values are used to add a Role
160  // element to the AdaptationSet with schemeIdUri=urn:mpeg:dash:role:2011.
161  // See ISO/IEC 23009-1:2012 section 5.8.5.5.
162  enum Role {
163  kRoleCaption,
164  kRoleSubtitle,
165  kRoleMain,
166  kRoleAlternate,
167  kRoleSupplementary,
168  kRoleCommentary,
169  kRoleDub
170  };
171 
172  virtual ~AdaptationSet();
173 
180  virtual Representation* AddRepresentation(const MediaInfo& media_info);
181 
191  virtual void AddContentProtectionElement(
192  const ContentProtectionElement& element);
193 
205  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
206  const std::string& pssh);
207 
212  virtual void AddRole(Role role);
213 
218  xml::scoped_xml_ptr<xmlNode> GetXml();
219 
225  virtual void ForceSetSegmentAlignment(bool segment_alignment);
226 
229  void AddAdaptationSetSwitching(uint32_t adaptation_set_id);
230 
232  const std::vector<uint32_t>& adaptation_set_switching_ids() const {
233  return adaptation_set_switching_ids_;
234  }
235 
236  // Must be unique in the Period.
237  uint32_t id() const { return id_; }
238 
250  void OnNewSegmentForRepresentation(uint32_t representation_id,
251  uint64_t start_time,
252  uint64_t duration);
253 
266  void OnSetFrameRateForRepresentation(uint32_t representation_id,
267  uint32_t frame_duration,
268  uint32_t timescale);
269 
270  protected:
278  AdaptationSet(uint32_t adaptation_set_id,
279  const std::string& lang,
280  const MpdOptions& mpd_options,
281  base::AtomicSequenceNumber* representation_counter);
282 
283  private:
284  friend class MpdBuilder;
285  template <DashProfile profile>
286  friend class MpdBuilderTest;
287 
288  // kSegmentAlignmentUnknown means that it is uncertain if the
289  // (sub)segments are aligned or not.
290  // kSegmentAlignmentTrue means that it is certain that the all the (current)
291  // segments added to the adaptation set are aligned.
292  // kSegmentAlignmentFalse means that it is it is certain that some segments
293  // are not aligned. This is useful to disable the computation for
294  // segment alignment, once it is certain that some segments are not aligned.
295  enum SegmentAligmentStatus {
296  kSegmentAlignmentUnknown,
297  kSegmentAlignmentTrue,
298  kSegmentAlignmentFalse
299  };
300 
301  // This maps Representations (IDs) to a list of start times of the segments.
302  // e.g.
303  // If Representation 1 has start time 0, 100, 200 and Representation 2 has
304  // start times 0, 200, 400, then the map contains:
305  // 1 -> [0, 100, 200]
306  // 2 -> [0, 200, 400]
307  typedef std::map<uint32_t, std::list<uint64_t> > RepresentationTimeline;
308 
309  // Gets the earliest, normalized segment timestamp. Returns true if
310  // successful, false otherwise.
311  bool GetEarliestTimestamp(double* timestamp_seconds);
312 
320  void CheckLiveSegmentAlignment(uint32_t representation_id,
321  uint64_t start_time,
322  uint64_t duration);
323 
324  // Checks representation_segment_start_times_ and sets segments_aligned_.
325  // Use this for VOD, do not use for Live.
326  void CheckVodSegmentAlignment();
327 
328  // Records the framerate of a Representation.
329  void RecordFrameRate(uint32_t frame_duration, uint32_t timescale);
330 
331  std::list<ContentProtectionElement> content_protection_elements_;
332  std::list<std::unique_ptr<Representation>> representations_;
333 
334  base::AtomicSequenceNumber* const representation_counter_;
335 
336  const uint32_t id_;
337  const std::string lang_;
338  const MpdOptions& mpd_options_;
339 
340  // The ids of the adaptation sets this adaptation set can switch to.
341  std::vector<uint32_t> adaptation_set_switching_ids_;
342 
343  // Video widths and heights of Representations. Note that this is a set; if
344  // there is only 1 resolution, then @width & @height should be set, otherwise
345  // @maxWidth & @maxHeight should be set for DASH IOP.
346  std::set<uint32_t> video_widths_;
347  std::set<uint32_t> video_heights_;
348 
349  // Video representations' frame rates.
350  // The frame rate notation for MPD is <integer>/<integer> (where the
351  // denominator is optional). This means the frame rate could be non-whole
352  // rational value, therefore the key is of type double.
353  // Value is <integer>/<integer> in string form.
354  // So, key == CalculatedValue(value)
355  std::map<double, std::string> video_frame_rates_;
356 
357  // contentType attribute of AdaptationSet.
358  // Determined by examining the MediaInfo passed to AddRepresentation().
359  std::string content_type_;
360 
361  // This does not have to be a set, it could be a list or vector because all we
362  // really care is whether there is more than one entry.
363  // Contains one entry if all the Representations have the same picture aspect
364  // ratio (@par attribute for AdaptationSet).
365  // There will be more than one entry if there are multiple picture aspect
366  // ratios.
367  // The @par attribute should only be set if there is exactly one entry
368  // in this set.
369  std::set<std::string> picture_aspect_ratio_;
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_;
376  bool force_set_segment_alignment_;
377 
378  // Keeps track of segment start times of Representations.
379  // For VOD, this will not be cleared, all the segment start times are
380  // stored in this. This should not out-of-memory for a reasonable length
381  // video and reasonable subsegment length.
382  // For Live, the entries are deleted (see CheckLiveSegmentAlignment()
383  // implementation comment) because storing the entire timeline is not
384  // reasonable and may cause an out-of-memory problem.
385  RepresentationTimeline representation_segment_start_times_;
386 
387  DISALLOW_COPY_AND_ASSIGN(AdaptationSet);
388 };
389 
391  public:
394 
399  virtual void OnNewSegmentForRepresentation(uint64_t start_time,
400  uint64_t duration) = 0;
401 
406  virtual void OnSetFrameRateForRepresentation(uint32_t frame_duration,
407  uint32_t timescale) = 0;
408 };
409 
413  public:
414  enum SuppressFlag {
415  kSuppressWidth = 1,
416  kSuppressHeight = 2,
417  kSuppressFrameRate = 4,
418  };
419 
420  virtual ~Representation();
421 
425  bool Init();
426 
437  virtual void AddContentProtectionElement(
438  const ContentProtectionElement& element);
439 
451  virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
452  const std::string& pssh);
453 
462  virtual void AddNewSegment(uint64_t start_time,
463  uint64_t duration,
464  uint64_t size);
465 
471  virtual void SetSampleDuration(uint32_t sample_duration);
472 
474  xml::scoped_xml_ptr<xmlNode> GetXml();
475 
484  void SuppressOnce(SuppressFlag flag);
485 
487  uint32_t id() const { return id_; }
488 
489  protected:
499  const MediaInfo& media_info,
500  const MpdOptions& mpd_options,
501  uint32_t representation_id,
502  std::unique_ptr<RepresentationStateChangeListener> state_change_listener);
503 
504  private:
505  friend class AdaptationSet;
506  template <DashProfile profile>
507  friend class MpdBuilderTest;
508 
509  bool AddLiveInfo(xml::RepresentationXmlNode* representation);
510 
511  // Returns true if |media_info_| has required fields to generate a valid
512  // Representation. Otherwise returns false.
513  bool HasRequiredMediaInfoFields();
514 
515  // Return false if the segment should be considered a new segment. True if the
516  // segment is contiguous.
517  bool IsContiguous(uint64_t start_time,
518  uint64_t duration,
519  uint64_t size) const;
520 
521  // Remove elements from |segment_infos_| if
522  // mpd_options_.time_shift_buffer_depth is specified. Increments
523  // |start_number_| by the number of segments removed.
524  void SlideWindow();
525 
526  // Note: Because 'mimeType' is a required field for a valid MPD, these return
527  // strings.
528  std::string GetVideoMimeType() const;
529  std::string GetAudioMimeType() const;
530  std::string GetTextMimeType() const;
531 
532  // Gets the earliest, normalized segment timestamp. Returns true if
533  // successful, false otherwise.
534  bool GetEarliestTimestamp(double* timestamp_seconds);
535 
536  // Init() checks that only one of VideoInfo, AudioInfo, or TextInfo is set. So
537  // any logic using this can assume only one set.
538  MediaInfo media_info_;
539  std::list<ContentProtectionElement> content_protection_elements_;
540  std::list<SegmentInfo> segment_infos_;
541 
542  const uint32_t id_;
543  std::string mime_type_;
544  std::string codecs_;
545  BandwidthEstimator bandwidth_estimator_;
546  const MpdOptions& mpd_options_;
547 
548  // startNumber attribute for SegmentTemplate.
549  // Starts from 1.
550  uint32_t start_number_;
551 
552  // If this is not null, then Representation is responsible for calling the
553  // right methods at right timings.
554  std::unique_ptr<RepresentationStateChangeListener> state_change_listener_;
555 
556  // Bit vector for tracking witch attributes should not be output.
557  int output_suppression_flags_;
558 
559  DISALLOW_COPY_AND_ASSIGN(Representation);
560 };
561 
562 } // namespace shaka
563 
564 #endif // MPD_BASE_MPD_BUILDER_H_
void OnSetFrameRateForRepresentation(uint32_t representation_id, uint32_t frame_duration, uint32_t timescale)
Definition: mpd_builder.cc:885
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:422
Representation(const MediaInfo &media_info, const MpdOptions &mpd_options, uint32_t representation_id, std::unique_ptr< RepresentationStateChangeListener > state_change_listener)
virtual void SetSampleDuration(uint32_t sample_duration)
virtual Representation * AddRepresentation(const MediaInfo &media_info)
Definition: mpd_builder.cc:701
This class generates DASH MPDs (Media Presentation Descriptions).
Definition: mpd_builder.h:57
uint32_t id() const
Definition: mpd_builder.h:487
Define an abstract file interface.
Definition: file.h:24
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
Definition: mpd_builder.cc:747
MpdBuilder(const MpdOptions &mpd_options)
Definition: mpd_builder.cc:399
virtual void AddRole(Role role)
Definition: mpd_builder.cc:759
void AddBaseUrl(const std::string &base_url)
Definition: mpd_builder.cc:404
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
Definition: mpd_builder.cc:753
AdaptationSet(uint32_t adaptation_set_id, const std::string &lang, const MpdOptions &mpd_options, base::AtomicSequenceNumber *representation_counter)
Definition: mpd_builder.cc:686
xml::scoped_xml_ptr< xmlNode > GetXml()
virtual bool ToString(std::string *output)
Definition: mpd_builder.cc:427
void AddAdaptationSetSwitching(uint32_t adaptation_set_id)
Definition: mpd_builder.cc:863
const std::vector< uint32_t > & adaptation_set_switching_ids() const
Definition: mpd_builder.h:232
virtual void ForceSetSegmentAlignment(bool segment_alignment)
Definition: mpd_builder.cc:857
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)
Definition: mpd_builder.cc:658
xml::scoped_xml_ptr< xmlNode > GetXml()
Definition: mpd_builder.cc:769
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
virtual AdaptationSet * AddAdaptationSet(const std::string &lang)
Definition: mpd_builder.cc:408
Defines Mpd Options.
Definition: mpd_options.h:23
void OnNewSegmentForRepresentation(uint32_t representation_id, uint64_t start_time, uint64_t duration)
Definition: mpd_builder.cc:874
void InjectClockForTesting(std::unique_ptr< base::Clock > clock)
This is for testing.
Definition: mpd_builder.h:95
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)