DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
mpd_builder.cc
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 #include "packager/mpd/base/mpd_builder.h"
8 
9 #include <libxml/tree.h>
10 #include <libxml/xmlstring.h>
11 
12 #include <cmath>
13 #include <iterator>
14 #include <list>
15 #include <string>
16 
17 #include "packager/base/base64.h"
18 #include "packager/base/bind.h"
19 #include "packager/base/files/file_path.h"
20 #include "packager/base/logging.h"
21 #include "packager/base/memory/scoped_ptr.h"
22 #include "packager/base/strings/string_number_conversions.h"
23 #include "packager/base/strings/stringprintf.h"
24 #include "packager/base/synchronization/lock.h"
25 #include "packager/base/time/default_clock.h"
26 #include "packager/base/time/time.h"
27 #include "packager/media/file/file.h"
28 #include "packager/mpd/base/content_protection_element.h"
29 #include "packager/mpd/base/language_utils.h"
30 #include "packager/mpd/base/mpd_utils.h"
31 #include "packager/mpd/base/xml/xml_node.h"
32 
33 namespace shaka {
34 
35 using base::FilePath;
36 using xml::XmlNode;
37 using xml::RepresentationXmlNode;
38 using xml::AdaptationSetXmlNode;
39 
40 namespace {
41 
42 const int kAdaptationSetGroupNotSet = -1;
43 
44 AdaptationSet::Role MediaInfoTextTypeToRole(
45  MediaInfo::TextInfo::TextType type) {
46  switch (type) {
47  case MediaInfo::TextInfo::UNKNOWN:
48  LOG(WARNING) << "Unknown text type, assuming subtitle.";
49  return AdaptationSet::kRoleSubtitle;
50  case MediaInfo::TextInfo::CAPTION:
51  return AdaptationSet::kRoleCaption;
52  case MediaInfo::TextInfo::SUBTITLE:
53  return AdaptationSet::kRoleSubtitle;
54  default:
55  NOTREACHED() << "Unknown MediaInfo TextType: " << type
56  << " assuming subtitle.";
57  return AdaptationSet::kRoleSubtitle;
58  }
59 }
60 
61 std::string GetMimeType(const std::string& prefix,
62  MediaInfo::ContainerType container_type) {
63  switch (container_type) {
64  case MediaInfo::CONTAINER_MP4:
65  return prefix + "/mp4";
66  case MediaInfo::CONTAINER_MPEG2_TS:
67  // NOTE: DASH MPD spec uses lowercase but RFC3555 says uppercase.
68  return prefix + "/MP2T";
69  case MediaInfo::CONTAINER_WEBM:
70  return prefix + "/webm";
71  default:
72  break;
73  }
74 
75  // Unsupported container types should be rejected/handled by the caller.
76  LOG(ERROR) << "Unrecognized container type: " << container_type;
77  return std::string();
78 }
79 
80 void AddMpdNameSpaceInfo(XmlNode* mpd) {
81  DCHECK(mpd);
82 
83  static const char kXmlNamespace[] = "urn:mpeg:dash:schema:mpd:2011";
84  static const char kXmlNamespaceXsi[] =
85  "http://www.w3.org/2001/XMLSchema-instance";
86  static const char kXmlNamespaceXlink[] = "http://www.w3.org/1999/xlink";
87  static const char kDashSchemaMpd2011[] =
88  "urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd";
89  static const char kCencNamespace[] = "urn:mpeg:cenc:2013";
90 
91  mpd->SetStringAttribute("xmlns", kXmlNamespace);
92  mpd->SetStringAttribute("xmlns:xsi", kXmlNamespaceXsi);
93  mpd->SetStringAttribute("xmlns:xlink", kXmlNamespaceXlink);
94  mpd->SetStringAttribute("xsi:schemaLocation", kDashSchemaMpd2011);
95  mpd->SetStringAttribute("xmlns:cenc", kCencNamespace);
96 }
97 
98 bool IsPeriodNode(xmlNodePtr node) {
99  DCHECK(node);
100  int kEqual = 0;
101  return xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("Period")) ==
102  kEqual;
103 }
104 
105 // Find the first <Period> element. This does not recurse down the tree,
106 // only checks direct children. Returns the pointer to Period element on
107 // success, otherwise returns false.
108 // As noted here, we must traverse.
109 // http://www.xmlsoft.org/tutorial/ar01s04.html
110 xmlNodePtr FindPeriodNode(XmlNode* xml_node) {
111  for (xmlNodePtr node = xml_node->GetRawPtr()->xmlChildrenNode; node != NULL;
112  node = node->next) {
113  if (IsPeriodNode(node))
114  return node;
115  }
116 
117  return NULL;
118 }
119 
120 bool Positive(double d) {
121  return d > 0.0;
122 }
123 
124 // Return current time in XML DateTime format. The value is in UTC, so the
125 // string ends with a 'Z'.
126 std::string XmlDateTimeNowWithOffset(
127  int32_t offset_seconds,
128  base::Clock* clock) {
129  base::Time time = clock->Now();
130  time += base::TimeDelta::FromSeconds(offset_seconds);
131  base::Time::Exploded time_exploded;
132  time.UTCExplode(&time_exploded);
133 
134  return base::StringPrintf("%4d-%02d-%02dT%02d:%02d:%02dZ", time_exploded.year,
135  time_exploded.month, time_exploded.day_of_month,
136  time_exploded.hour, time_exploded.minute,
137  time_exploded.second);
138 }
139 
140 void SetIfPositive(const char* attr_name, double value, XmlNode* mpd) {
141  if (Positive(value)) {
142  mpd->SetStringAttribute(attr_name, SecondsToXmlDuration(value));
143  }
144 }
145 
146 uint32_t GetTimeScale(const MediaInfo& media_info) {
147  if (media_info.has_reference_time_scale()) {
148  return media_info.reference_time_scale();
149  }
150 
151  if (media_info.has_video_info()) {
152  return media_info.video_info().time_scale();
153  }
154 
155  if (media_info.has_audio_info()) {
156  return media_info.audio_info().time_scale();
157  }
158 
159  LOG(WARNING) << "No timescale specified, using 1 as timescale.";
160  return 1;
161 }
162 
163 uint64_t LastSegmentStartTime(const SegmentInfo& segment_info) {
164  return segment_info.start_time + segment_info.duration * segment_info.repeat;
165 }
166 
167 // This is equal to |segment_info| end time
168 uint64_t LastSegmentEndTime(const SegmentInfo& segment_info) {
169  return segment_info.start_time +
170  segment_info.duration * (segment_info.repeat + 1);
171 }
172 
173 uint64_t LatestSegmentStartTime(const std::list<SegmentInfo>& segments) {
174  DCHECK(!segments.empty());
175  const SegmentInfo& latest_segment = segments.back();
176  return LastSegmentStartTime(latest_segment);
177 }
178 
179 // Given |timeshift_limit|, finds out the number of segments that are no longer
180 // valid and should be removed from |segment_info|.
181 int SearchTimedOutRepeatIndex(uint64_t timeshift_limit,
182  const SegmentInfo& segment_info) {
183  DCHECK_LE(timeshift_limit, LastSegmentEndTime(segment_info));
184  if (timeshift_limit < segment_info.start_time)
185  return 0;
186 
187  return (timeshift_limit - segment_info.start_time) / segment_info.duration;
188 }
189 
190 // Overload this function to support different types of |output|.
191 // Note that this could be done by call MpdBuilder::ToString() and use the
192 // result to write to a file, it requires an extra copy.
193 bool WriteXmlCharArrayToOutput(xmlChar* doc,
194  int doc_size,
195  std::string* output) {
196  DCHECK(doc);
197  DCHECK(output);
198  output->assign(doc, doc + doc_size);
199  return true;
200 }
201 
202 bool WriteXmlCharArrayToOutput(xmlChar* doc,
203  int doc_size,
204  media::File* output) {
205  DCHECK(doc);
206  DCHECK(output);
207  if (output->Write(doc, doc_size) < doc_size)
208  return false;
209 
210  return output->Flush();
211 }
212 
213 std::string MakePathRelative(const std::string& path,
214  const std::string& mpd_dir) {
215  return (path.find(mpd_dir) == 0) ? path.substr(mpd_dir.size()) : path;
216 }
217 
218 // Check whether the video info has width and height.
219 // DASH IOP also requires several other fields for video representations, namely
220 // width, height, framerate, and sar.
221 bool HasRequiredVideoFields(const MediaInfo_VideoInfo& video_info) {
222  if (!video_info.has_height() || !video_info.has_width()) {
223  LOG(ERROR)
224  << "Width and height are required fields for generating a valid MPD.";
225  return false;
226  }
227  // These fields are not required for a valid MPD, but required for DASH IOP
228  // compliant MPD. MpdBuilder can keep generating MPDs without these fields.
229  LOG_IF(WARNING, !video_info.has_time_scale())
230  << "Video info does not contain timescale required for "
231  "calculating framerate. @frameRate is required for DASH IOP.";
232  LOG_IF(WARNING, !video_info.has_frame_duration())
233  << "Video info does not contain frame duration required "
234  "for calculating framerate. @frameRate is required for DASH IOP.";
235  LOG_IF(WARNING, !video_info.has_pixel_width())
236  << "Video info does not contain pixel_width to calculate the sample "
237  "aspect ratio required for DASH IOP.";
238  LOG_IF(WARNING, !video_info.has_pixel_height())
239  << "Video info does not contain pixel_height to calculate the sample "
240  "aspect ratio required for DASH IOP.";
241  return true;
242 }
243 
244 // Returns the picture aspect ratio string e.g. "16:9", "4:3".
245 // "Reducing the quotient to minimal form" does not work well in practice as
246 // there may be some rounding performed in the input, e.g. the resolution of
247 // 480p is 854:480 for 16:9 aspect ratio, can only be reduced to 427:240.
248 // The algorithm finds out the pair of integers, num and den, where num / den is
249 // the closest ratio to scaled_width / scaled_height, by looping den through
250 // common values.
251 std::string GetPictureAspectRatio(uint32_t width,
252  uint32_t height,
253  uint32_t pixel_width,
254  uint32_t pixel_height) {
255  const uint32_t scaled_width = pixel_width * width;
256  const uint32_t scaled_height = pixel_height * height;
257  const double par = static_cast<double>(scaled_width) / scaled_height;
258 
259  // Typical aspect ratios have par_y less than or equal to 19:
260  // https://en.wikipedia.org/wiki/List_of_common_resolutions
261  const uint32_t kLargestPossibleParY = 19;
262 
263  uint32_t par_num = 0;
264  uint32_t par_den = 0;
265  double min_error = 1.0;
266  for (uint32_t den = 1; den <= kLargestPossibleParY; ++den) {
267  uint32_t num = par * den + 0.5;
268  double error = fabs(par - static_cast<double>(num) / den);
269  if (error < min_error) {
270  min_error = error;
271  par_num = num;
272  par_den = den;
273  if (error == 0) break;
274  }
275  }
276  VLOG(2) << "width*pix_width : height*pixel_height (" << scaled_width << ":"
277  << scaled_height << ") reduced to " << par_num << ":" << par_den
278  << " with error " << min_error << ".";
279 
280  return base::IntToString(par_num) + ":" + base::IntToString(par_den);
281 }
282 
283 // Adds an entry to picture_aspect_ratio if the size of picture_aspect_ratio is
284 // less than 2 and video_info has both pixel width and pixel height.
285 void AddPictureAspectRatio(
286  const MediaInfo::VideoInfo& video_info,
287  std::set<std::string>* picture_aspect_ratio) {
288  // If there are more than one entries in picture_aspect_ratio, the @par
289  // attribute cannot be set, so skip.
290  if (picture_aspect_ratio->size() > 1)
291  return;
292 
293  if (video_info.width() == 0 || video_info.height() == 0 ||
294  video_info.pixel_width() == 0 || video_info.pixel_height() == 0) {
295  // If there is even one Representation without a @sar attribute, @par cannot
296  // be calculated.
297  // Just populate the set with at least 2 bogus strings so that further call
298  // to this function will bail out immediately.
299  picture_aspect_ratio->insert("bogus");
300  picture_aspect_ratio->insert("entries");
301  return;
302  }
303 
304  const std::string par = GetPictureAspectRatio(
305  video_info.width(), video_info.height(),
306  video_info.pixel_width(), video_info.pixel_height());
307  DVLOG(1) << "Setting par as: " << par
308  << " for video with width: " << video_info.width()
309  << " height: " << video_info.height()
310  << " pixel_width: " << video_info.pixel_width() << " pixel_height; "
311  << video_info.pixel_height();
312  picture_aspect_ratio->insert(par);
313 }
314 
315 std::string RoleToText(AdaptationSet::Role role) {
316  // Using switch so that the compiler can detect whether there is a case that's
317  // not being handled.
318  switch (role) {
319  case AdaptationSet::kRoleCaption:
320  return "caption";
321  case AdaptationSet::kRoleSubtitle:
322  return "subtitle";
323  case AdaptationSet::kRoleMain:
324  return "main";
325  case AdaptationSet::kRoleAlternate:
326  return "alternate";
327  case AdaptationSet::kRoleSupplementary:
328  return "supplementary";
329  case AdaptationSet::kRoleCommentary:
330  return "commentary";
331  case AdaptationSet::kRoleDub:
332  return "dub";
333  default:
334  NOTREACHED();
335  return "";
336  }
337 
338  NOTREACHED();
339  return "";
340 }
341 
342 // Spooky static initialization/cleanup of libxml.
343 class LibXmlInitializer {
344  public:
345  LibXmlInitializer() : initialized_(false) {
346  base::AutoLock lock(lock_);
347  if (!initialized_) {
348  xmlInitParser();
349  initialized_ = true;
350  }
351  }
352 
353  ~LibXmlInitializer() {
354  base::AutoLock lock(lock_);
355  if (initialized_) {
356  xmlCleanupParser();
357  initialized_ = false;
358  }
359  }
360 
361  private:
362  base::Lock lock_;
363  bool initialized_;
364 
365  DISALLOW_COPY_AND_ASSIGN(LibXmlInitializer);
366 };
367 
368 class RepresentationStateChangeListenerImpl
369  : public RepresentationStateChangeListener {
370  public:
371  // |adaptation_set| is not owned by this class.
372  RepresentationStateChangeListenerImpl(uint32_t representation_id,
373  AdaptationSet* adaptation_set)
374  : representation_id_(representation_id), adaptation_set_(adaptation_set) {
375  DCHECK(adaptation_set_);
376  }
377  ~RepresentationStateChangeListenerImpl() override {}
378 
379  // RepresentationStateChangeListener implementation.
380  void OnNewSegmentForRepresentation(uint64_t start_time,
381  uint64_t duration) override {
382  adaptation_set_->OnNewSegmentForRepresentation(representation_id_,
383  start_time, duration);
384  }
385 
386  void OnSetFrameRateForRepresentation(uint32_t frame_duration,
387  uint32_t timescale) override {
388  adaptation_set_->OnSetFrameRateForRepresentation(representation_id_,
389  frame_duration, timescale);
390  }
391 
392  private:
393  const uint32_t representation_id_;
394  AdaptationSet* const adaptation_set_;
395 
396  DISALLOW_COPY_AND_ASSIGN(RepresentationStateChangeListenerImpl);
397 };
398 
399 } // namespace
400 
401 MpdBuilder::MpdBuilder(MpdType type, const MpdOptions& mpd_options)
402  : type_(type),
403  mpd_options_(mpd_options),
404  adaptation_sets_deleter_(&adaptation_sets_),
405  clock_(new base::DefaultClock()) {}
406 
407 MpdBuilder::~MpdBuilder() {}
408 
409 void MpdBuilder::AddBaseUrl(const std::string& base_url) {
410  base_urls_.push_back(base_url);
411 }
412 
413 AdaptationSet* MpdBuilder::AddAdaptationSet(const std::string& lang) {
414  scoped_ptr<AdaptationSet> adaptation_set(
415  new AdaptationSet(adaptation_set_counter_.GetNext(), lang, mpd_options_,
416  type_, &representation_counter_));
417 
418  DCHECK(adaptation_set);
419  adaptation_sets_.push_back(adaptation_set.get());
420  return adaptation_set.release();
421 }
422 
424  DCHECK(output_file);
425  return WriteMpdToOutput(output_file);
426 }
427 
428 bool MpdBuilder::ToString(std::string* output) {
429  DCHECK(output);
430  return WriteMpdToOutput(output);
431 }
432 template <typename OutputType>
433 bool MpdBuilder::WriteMpdToOutput(OutputType* output) {
434  static LibXmlInitializer lib_xml_initializer;
435 
436  xml::scoped_xml_ptr<xmlDoc> doc(GenerateMpd());
437  if (!doc.get())
438  return false;
439 
440  static const int kNiceFormat = 1;
441  int doc_str_size = 0;
442  xmlChar* doc_str = NULL;
443  xmlDocDumpFormatMemoryEnc(doc.get(), &doc_str, &doc_str_size, "UTF-8",
444  kNiceFormat);
445 
446  bool result = WriteXmlCharArrayToOutput(doc_str, doc_str_size, output);
447  xmlFree(doc_str);
448 
449  // Cleanup, free the doc.
450  doc.reset();
451  return result;
452 }
453 
454 xmlDocPtr MpdBuilder::GenerateMpd() {
455  // Setup nodes.
456  static const char kXmlVersion[] = "1.0";
457  xml::scoped_xml_ptr<xmlDoc> doc(xmlNewDoc(BAD_CAST kXmlVersion));
458  XmlNode mpd("MPD");
459 
460  // Iterate thru AdaptationSets and add them to one big Period element.
461  XmlNode period("Period");
462 
463  // Always set id=0 for now. Since this class can only generate one Period
464  // at the moment, just use a constant.
465  // Required for 'dynamic' MPDs.
466  period.SetId(0);
467  std::list<AdaptationSet*>::iterator adaptation_sets_it =
468  adaptation_sets_.begin();
469  for (; adaptation_sets_it != adaptation_sets_.end(); ++adaptation_sets_it) {
470  xml::scoped_xml_ptr<xmlNode> child((*adaptation_sets_it)->GetXml());
471  if (!child.get() || !period.AddChild(child.Pass()))
472  return NULL;
473  }
474 
475  // Add baseurls to MPD.
476  std::list<std::string>::const_iterator base_urls_it = base_urls_.begin();
477  for (; base_urls_it != base_urls_.end(); ++base_urls_it) {
478  XmlNode base_url("BaseURL");
479  base_url.SetContent(*base_urls_it);
480 
481  if (!mpd.AddChild(base_url.PassScopedPtr()))
482  return NULL;
483  }
484 
485  if (type_ == kDynamic) {
486  // This is the only Period and it is a regular period.
487  period.SetStringAttribute("start", "PT0S");
488  }
489 
490  if (!mpd.AddChild(period.PassScopedPtr()))
491  return NULL;
492 
493  AddMpdNameSpaceInfo(&mpd);
494  AddCommonMpdInfo(&mpd);
495  switch (type_) {
496  case kStatic:
497  AddStaticMpdInfo(&mpd);
498  break;
499  case kDynamic:
500  AddDynamicMpdInfo(&mpd);
501  break;
502  default:
503  NOTREACHED() << "Unknown MPD type: " << type_;
504  break;
505  }
506 
507  DCHECK(doc);
508  std::string version_string =
509  "Generated with https://github.com/google/shaka-packager version " +
510  mpd_options_.packager_version_string;
511  xml::scoped_xml_ptr<xmlNode> comment(
512  xmlNewDocComment(doc.get(), BAD_CAST version_string.c_str()));
513  xmlDocSetRootElement(doc.get(), comment.get());
514  xmlAddSibling(comment.release(), mpd.Release());
515  return doc.release();
516 }
517 
518 void MpdBuilder::AddCommonMpdInfo(XmlNode* mpd_node) {
519  if (Positive(mpd_options_.min_buffer_time)) {
520  mpd_node->SetStringAttribute(
521  "minBufferTime", SecondsToXmlDuration(mpd_options_.min_buffer_time));
522  } else {
523  LOG(ERROR) << "minBufferTime value not specified.";
524  // TODO(tinskip): Propagate error.
525  }
526 }
527 
528 void MpdBuilder::AddStaticMpdInfo(XmlNode* mpd_node) {
529  DCHECK(mpd_node);
530  DCHECK_EQ(MpdBuilder::kStatic, type_);
531 
532  static const char kStaticMpdType[] = "static";
533  static const char kStaticMpdProfile[] =
534  "urn:mpeg:dash:profile:isoff-on-demand:2011";
535  mpd_node->SetStringAttribute("type", kStaticMpdType);
536  mpd_node->SetStringAttribute("profiles", kStaticMpdProfile);
537  mpd_node->SetStringAttribute(
538  "mediaPresentationDuration",
539  SecondsToXmlDuration(GetStaticMpdDuration(mpd_node)));
540 }
541 
542 void MpdBuilder::AddDynamicMpdInfo(XmlNode* mpd_node) {
543  DCHECK(mpd_node);
544  DCHECK_EQ(MpdBuilder::kDynamic, type_);
545 
546  static const char kDynamicMpdType[] = "dynamic";
547  static const char kDynamicMpdProfile[] =
548  "urn:mpeg:dash:profile:isoff-live:2011";
549  mpd_node->SetStringAttribute("type", kDynamicMpdType);
550  mpd_node->SetStringAttribute("profiles", kDynamicMpdProfile);
551 
552  // No offset from NOW.
553  mpd_node->SetStringAttribute("publishTime",
554  XmlDateTimeNowWithOffset(0, clock_.get()));
555 
556  // 'availabilityStartTime' is required for dynamic profile. Calculate if
557  // not already calculated.
558  if (availability_start_time_.empty()) {
559  double earliest_presentation_time;
560  if (GetEarliestTimestamp(&earliest_presentation_time)) {
561  availability_start_time_ =
562  XmlDateTimeNowWithOffset(mpd_options_.availability_time_offset -
563  std::ceil(earliest_presentation_time),
564  clock_.get());
565  } else {
566  LOG(ERROR) << "Could not determine the earliest segment presentation "
567  "time for availabilityStartTime calculation.";
568  // TODO(tinskip). Propagate an error.
569  }
570  }
571  if (!availability_start_time_.empty())
572  mpd_node->SetStringAttribute("availabilityStartTime",
573  availability_start_time_);
574 
575  if (Positive(mpd_options_.minimum_update_period)) {
576  mpd_node->SetStringAttribute(
577  "minimumUpdatePeriod",
578  SecondsToXmlDuration(mpd_options_.minimum_update_period));
579  } else {
580  LOG(WARNING) << "The profile is dynamic but no minimumUpdatePeriod "
581  "specified.";
582  }
583 
584  SetIfPositive("timeShiftBufferDepth", mpd_options_.time_shift_buffer_depth,
585  mpd_node);
586  SetIfPositive("suggestedPresentationDelay",
587  mpd_options_.suggested_presentation_delay, mpd_node);
588 }
589 
590 float MpdBuilder::GetStaticMpdDuration(XmlNode* mpd_node) {
591  DCHECK(mpd_node);
592  DCHECK_EQ(MpdBuilder::kStatic, type_);
593 
594  xmlNodePtr period_node = FindPeriodNode(mpd_node);
595  DCHECK(period_node) << "Period element must be a child of mpd_node.";
596  DCHECK(IsPeriodNode(period_node));
597 
598  // Attribute mediaPresentationDuration must be present for 'static' MPD. So
599  // setting "PT0S" is required even if none of the representaions have duration
600  // attribute.
601  float max_duration = 0.0f;
602  for (xmlNodePtr adaptation_set = xmlFirstElementChild(period_node);
603  adaptation_set; adaptation_set = xmlNextElementSibling(adaptation_set)) {
604  for (xmlNodePtr representation = xmlFirstElementChild(adaptation_set);
605  representation;
606  representation = xmlNextElementSibling(representation)) {
607  float duration = 0.0f;
608  if (GetDurationAttribute(representation, &duration)) {
609  max_duration = max_duration > duration ? max_duration : duration;
610 
611  // 'duration' attribute is there only to help generate MPD, not
612  // necessary for MPD, remove the attribute.
613  xmlUnsetProp(representation, BAD_CAST "duration");
614  }
615  }
616  }
617 
618  return max_duration;
619 }
620 
621 bool MpdBuilder::GetEarliestTimestamp(double* timestamp_seconds) {
622  DCHECK(timestamp_seconds);
623 
624  double earliest_timestamp(-1);
625  for (std::list<AdaptationSet*>::const_iterator iter =
626  adaptation_sets_.begin();
627  iter != adaptation_sets_.end(); ++iter) {
628  double timestamp;
629  if ((*iter)->GetEarliestTimestamp(&timestamp) &&
630  ((earliest_timestamp < 0) || (timestamp < earliest_timestamp))) {
631  earliest_timestamp = timestamp;
632  }
633  }
634  if (earliest_timestamp < 0)
635  return false;
636 
637  *timestamp_seconds = earliest_timestamp;
638  return true;
639 }
640 
641 void MpdBuilder::MakePathsRelativeToMpd(const std::string& mpd_path,
642  MediaInfo* media_info) {
643  DCHECK(media_info);
644  const std::string kFileProtocol("file://");
645  std::string mpd_file_path = (mpd_path.find(kFileProtocol) == 0)
646  ? mpd_path.substr(kFileProtocol.size())
647  : mpd_path;
648 
649  if (!mpd_file_path.empty()) {
650  std::string mpd_dir(
651  FilePath(mpd_file_path).DirName().AsEndingWithSeparator().value());
652  if (!mpd_dir.empty()) {
653  if (media_info->has_media_file_name()) {
654  media_info->set_media_file_name(
655  MakePathRelative(media_info->media_file_name(), mpd_dir));
656  }
657  if (media_info->has_init_segment_name()) {
658  media_info->set_init_segment_name(
659  MakePathRelative(media_info->init_segment_name(), mpd_dir));
660  }
661  if (media_info->has_segment_template()) {
662  media_info->set_segment_template(
663  MakePathRelative(media_info->segment_template(), mpd_dir));
664  }
665  }
666  }
667 }
668 
669 AdaptationSet::AdaptationSet(uint32_t adaptation_set_id,
670  const std::string& lang,
671  const MpdOptions& mpd_options,
672  MpdBuilder::MpdType mpd_type,
673  base::AtomicSequenceNumber* counter)
674  : representations_deleter_(&representations_),
675  representation_counter_(counter),
676  id_(adaptation_set_id),
677  lang_(lang),
678  mpd_options_(mpd_options),
679  mpd_type_(mpd_type),
680  group_(kAdaptationSetGroupNotSet),
681  segments_aligned_(kSegmentAlignmentUnknown),
682  force_set_segment_alignment_(false) {
683  DCHECK(counter);
684 }
685 
686 AdaptationSet::~AdaptationSet() {}
687 
688 Representation* AdaptationSet::AddRepresentation(const MediaInfo& media_info) {
689  const uint32_t representation_id = representation_counter_->GetNext();
690  // Note that AdaptationSet outlive Representation, so this object
691  // will die before AdaptationSet.
692  scoped_ptr<RepresentationStateChangeListener> listener(
693  new RepresentationStateChangeListenerImpl(representation_id, this));
694  scoped_ptr<Representation> representation(new Representation(
695  media_info, mpd_options_, representation_id, listener.Pass()));
696 
697  if (!representation->Init())
698  return NULL;
699 
700  // For videos, record the width, height, and the frame rate to calculate the
701  // max {width,height,framerate} required for DASH IOP.
702  if (media_info.has_video_info()) {
703  const MediaInfo::VideoInfo& video_info = media_info.video_info();
704  DCHECK(video_info.has_width());
705  DCHECK(video_info.has_height());
706  video_widths_.insert(video_info.width());
707  video_heights_.insert(video_info.height());
708 
709  if (video_info.has_time_scale() && video_info.has_frame_duration())
710  RecordFrameRate(video_info.frame_duration(), video_info.time_scale());
711 
712  AddPictureAspectRatio(video_info, &picture_aspect_ratio_);
713  }
714 
715  if (media_info.has_video_info()) {
716  content_type_ = "video";
717  } else if (media_info.has_audio_info()) {
718  content_type_ = "audio";
719  } else if (media_info.has_text_info()) {
720  content_type_ = "text";
721 
722  if (media_info.text_info().has_type() &&
723  (media_info.text_info().type() != MediaInfo::TextInfo::UNKNOWN)) {
724  roles_.insert(MediaInfoTextTypeToRole(media_info.text_info().type()));
725  }
726  }
727 
728  representations_.push_back(representation.get());
729  return representation.release();
730 }
731 
733  const ContentProtectionElement& content_protection_element) {
734  content_protection_elements_.push_back(content_protection_element);
735  RemoveDuplicateAttributes(&content_protection_elements_.back());
736 }
737 
738 void AdaptationSet::UpdateContentProtectionPssh(const std::string& drm_uuid,
739  const std::string& pssh) {
740  UpdateContentProtectionPsshHelper(drm_uuid, pssh,
741  &content_protection_elements_);
742 }
743 
744 void AdaptationSet::AddRole(Role role) {
745  roles_.insert(role);
746 }
747 
748 // Creates a copy of <AdaptationSet> xml element, iterate thru all the
749 // <Representation> (child) elements and add them to the copy.
750 // Set all the attributes first and then add the children elements so that flags
751 // can be passed to Representation to avoid setting redundant attributes. For
752 // example, if AdaptationSet@width is set, then Representation@width is
753 // redundant and should not be set.
754 xml::scoped_xml_ptr<xmlNode> AdaptationSet::GetXml() {
755  AdaptationSetXmlNode adaptation_set;
756 
757  bool suppress_representation_width = false;
758  bool suppress_representation_height = false;
759  bool suppress_representation_frame_rate = false;
760 
761  adaptation_set.SetId(id_);
762  adaptation_set.SetStringAttribute("contentType", content_type_);
763  if (!lang_.empty() && lang_ != "und") {
764  adaptation_set.SetStringAttribute("lang", LanguageToShortestForm(lang_));
765  }
766 
767  // Note that std::{set,map} are ordered, so the last element is the max value.
768  if (video_widths_.size() == 1) {
769  suppress_representation_width = true;
770  adaptation_set.SetIntegerAttribute("width", *video_widths_.begin());
771  } else if (video_widths_.size() > 1) {
772  adaptation_set.SetIntegerAttribute("maxWidth", *video_widths_.rbegin());
773  }
774  if (video_heights_.size() == 1) {
775  suppress_representation_height = true;
776  adaptation_set.SetIntegerAttribute("height", *video_heights_.begin());
777  } else if (video_heights_.size() > 1) {
778  adaptation_set.SetIntegerAttribute("maxHeight", *video_heights_.rbegin());
779  }
780 
781  if (video_frame_rates_.size() == 1) {
782  suppress_representation_frame_rate = true;
783  adaptation_set.SetStringAttribute("frameRate",
784  video_frame_rates_.begin()->second);
785  } else if (video_frame_rates_.size() > 1) {
786  adaptation_set.SetStringAttribute("maxFrameRate",
787  video_frame_rates_.rbegin()->second);
788  }
789 
790  // Note: must be checked before checking segments_aligned_ (below). So that
791  // segments_aligned_ is set before checking below.
792  if (mpd_type_ == MpdBuilder::kStatic) {
793  CheckVodSegmentAlignment();
794  }
795 
796  if (segments_aligned_ == kSegmentAlignmentTrue) {
797  adaptation_set.SetStringAttribute(mpd_type_ == MpdBuilder::kStatic
798  ? "subsegmentAlignment"
799  : "segmentAlignment",
800  "true");
801  }
802 
803  if (picture_aspect_ratio_.size() == 1)
804  adaptation_set.SetStringAttribute("par", *picture_aspect_ratio_.begin());
805 
806  if (group_ >= 0)
807  adaptation_set.SetIntegerAttribute("group", group_);
808 
809  if (!adaptation_set.AddContentProtectionElements(
810  content_protection_elements_)) {
811  return xml::scoped_xml_ptr<xmlNode>();
812  }
813  for (AdaptationSet::Role role : roles_)
814  adaptation_set.AddRoleElement("urn:mpeg:dash:role:2011", RoleToText(role));
815 
816  for (Representation* representation : representations_) {
817  if (suppress_representation_width)
818  representation->SuppressOnce(Representation::kSuppressWidth);
819  if (suppress_representation_height)
820  representation->SuppressOnce(Representation::kSuppressHeight);
821  if (suppress_representation_frame_rate)
822  representation->SuppressOnce(Representation::kSuppressFrameRate);
823  xml::scoped_xml_ptr<xmlNode> child(representation->GetXml());
824  if (!child || !adaptation_set.AddChild(child.Pass()))
825  return xml::scoped_xml_ptr<xmlNode>();
826  }
827 
828  return adaptation_set.PassScopedPtr();
829 }
830 
831 void AdaptationSet::ForceSetSegmentAlignment(bool segment_alignment) {
832  segments_aligned_ =
833  segment_alignment ? kSegmentAlignmentTrue : kSegmentAlignmentFalse;
834  force_set_segment_alignment_ = true;
835 }
836 
837 void AdaptationSet::SetGroup(int group_number) {
838  group_ = group_number;
839 }
840 
841 int AdaptationSet::Group() const {
842  return group_;
843 }
844 
845 // Check segmentAlignment for Live here. Storing all start_time and duration
846 // will out-of-memory because there's no way of knowing when it will end.
847 // VOD subsegmentAlignment check is *not* done here because it is possible
848 // that some Representations might not have been added yet (e.g. a thread is
849 // assigned per muxer so one might run faster than others).
850 // To be clear, for Live, all Representations should be added before a
851 // segment is added.
852 void AdaptationSet::OnNewSegmentForRepresentation(uint32_t representation_id,
853  uint64_t start_time,
854  uint64_t duration) {
855  if (mpd_type_ == MpdBuilder::kDynamic) {
856  CheckLiveSegmentAlignment(representation_id, start_time, duration);
857  } else {
858  representation_segment_start_times_[representation_id].push_back(
859  start_time);
860  }
861 }
862 
864  uint32_t representation_id,
865  uint32_t frame_duration,
866  uint32_t timescale) {
867  RecordFrameRate(frame_duration, timescale);
868 }
869 
870 bool AdaptationSet::GetEarliestTimestamp(double* timestamp_seconds) {
871  DCHECK(timestamp_seconds);
872 
873  double earliest_timestamp(-1);
874  for (std::list<Representation*>::const_iterator iter =
875  representations_.begin();
876  iter != representations_.end(); ++iter) {
877  double timestamp;
878  if ((*iter)->GetEarliestTimestamp(&timestamp) &&
879  ((earliest_timestamp < 0) || (timestamp < earliest_timestamp))) {
880  earliest_timestamp = timestamp;
881  }
882  }
883  if (earliest_timestamp < 0)
884  return false;
885 
886  *timestamp_seconds = earliest_timestamp;
887  return true;
888 }
889 
890 // This implementation assumes that each representations' segments' are
891 // contiguous.
892 // Also assumes that all Representations are added before this is called.
893 // This checks whether the first elements of the lists in
894 // representation_segment_start_times_ are aligned.
895 // For example, suppose this method was just called with args rep_id=2
896 // start_time=1.
897 // 1 -> [1, 100, 200]
898 // 2 -> [1]
899 // The timestamps of the first elements match, so this flags
900 // segments_aligned_=true.
901 // Also since the first segment start times match, the first element of all the
902 // lists are removed, so the map of lists becomes:
903 // 1 -> [100, 200]
904 // 2 -> []
905 // Note that there could be false positives.
906 // e.g. just got rep_id=3 start_time=1 duration=300, and the duration of the
907 // whole AdaptationSet is 300.
908 // 1 -> [1, 100, 200]
909 // 2 -> [1, 90, 100]
910 // 3 -> [1]
911 // They are not aligned but this will be marked as aligned.
912 // But since this is unlikely to happen in the packager (and to save
913 // computation), this isn't handled at the moment.
914 void AdaptationSet::CheckLiveSegmentAlignment(uint32_t representation_id,
915  uint64_t start_time,
916  uint64_t /* duration */) {
917  if (segments_aligned_ == kSegmentAlignmentFalse ||
918  force_set_segment_alignment_) {
919  return;
920  }
921 
922  std::list<uint64_t>& representation_start_times =
923  representation_segment_start_times_[representation_id];
924  representation_start_times.push_back(start_time);
925  // There's no way to detemine whether the segments are aligned if some
926  // representations do not have any segments.
927  if (representation_segment_start_times_.size() != representations_.size())
928  return;
929 
930  DCHECK(!representation_start_times.empty());
931  const uint64_t expected_start_time = representation_start_times.front();
932  for (RepresentationTimeline::const_iterator it =
933  representation_segment_start_times_.begin();
934  it != representation_segment_start_times_.end(); ++it) {
935  // If there are no entries in a list, then there is no way for the
936  // segment alignment status to change.
937  // Note that it can be empty because entries get deleted below.
938  if (it->second.empty())
939  return;
940 
941  if (expected_start_time != it->second.front()) {
942  // Flag as false and clear the start times data, no need to keep it
943  // around.
944  segments_aligned_ = kSegmentAlignmentFalse;
945  representation_segment_start_times_.clear();
946  return;
947  }
948  }
949  segments_aligned_ = kSegmentAlignmentTrue;
950 
951  for (RepresentationTimeline::iterator it =
952  representation_segment_start_times_.begin();
953  it != representation_segment_start_times_.end(); ++it) {
954  it->second.pop_front();
955  }
956 }
957 
958 // Make sure all segements start times match for all Representations.
959 // This assumes that the segments are contiguous.
960 void AdaptationSet::CheckVodSegmentAlignment() {
961  if (segments_aligned_ == kSegmentAlignmentFalse ||
962  force_set_segment_alignment_) {
963  return;
964  }
965  if (representation_segment_start_times_.empty())
966  return;
967  if (representation_segment_start_times_.size() == 1) {
968  segments_aligned_ = kSegmentAlignmentTrue;
969  return;
970  }
971 
972  // This is not the most efficient implementation to compare the values
973  // because expected_time_line is compared against all other time lines, but
974  // probably the most readable.
975  const std::list<uint64_t>& expected_time_line =
976  representation_segment_start_times_.begin()->second;
977 
978  bool all_segment_time_line_same_length = true;
979  // Note that the first entry is skipped because it is expected_time_line.
980  RepresentationTimeline::const_iterator it =
981  representation_segment_start_times_.begin();
982  for (++it; it != representation_segment_start_times_.end(); ++it) {
983  const std::list<uint64_t>& other_time_line = it->second;
984  if (expected_time_line.size() != other_time_line.size()) {
985  all_segment_time_line_same_length = false;
986  }
987 
988  const std::list<uint64_t>* longer_list = &other_time_line;
989  const std::list<uint64_t>* shorter_list = &expected_time_line;
990  if (expected_time_line.size() > other_time_line.size()) {
991  shorter_list = &other_time_line;
992  longer_list = &expected_time_line;
993  }
994 
995  if (!std::equal(shorter_list->begin(), shorter_list->end(),
996  longer_list->begin())) {
997  // Some segments are definitely unaligned.
998  segments_aligned_ = kSegmentAlignmentFalse;
999  representation_segment_start_times_.clear();
1000  return;
1001  }
1002  }
1003 
1004  // TODO(rkuroiwa): The right way to do this is to also check the durations.
1005  // For example:
1006  // (a) 3 4 5
1007  // (b) 3 4 5 6
1008  // could be true or false depending on the length of the third segment of (a).
1009  // i.e. if length of the third segment is 2, then this is not aligned.
1010  if (!all_segment_time_line_same_length) {
1011  segments_aligned_ = kSegmentAlignmentUnknown;
1012  return;
1013  }
1014 
1015  segments_aligned_ = kSegmentAlignmentTrue;
1016 }
1017 
1018 // Since all AdaptationSet cares about is the maxFrameRate, representation_id
1019 // is not passed to this method.
1020 void AdaptationSet::RecordFrameRate(uint32_t frame_duration,
1021  uint32_t timescale) {
1022  if (frame_duration == 0) {
1023  LOG(ERROR) << "Frame duration is 0 and cannot be set.";
1024  return;
1025  }
1026  video_frame_rates_[static_cast<double>(timescale) / frame_duration] =
1027  base::IntToString(timescale) + "/" + base::IntToString(frame_duration);
1028 }
1029 
1031  const MediaInfo& media_info,
1032  const MpdOptions& mpd_options,
1033  uint32_t id,
1034  scoped_ptr<RepresentationStateChangeListener> state_change_listener)
1035  : media_info_(media_info),
1036  id_(id),
1037  bandwidth_estimator_(BandwidthEstimator::kUseAllBlocks),
1038  mpd_options_(mpd_options),
1039  start_number_(1),
1040  state_change_listener_(state_change_listener.Pass()),
1041  output_suppression_flags_(0) {}
1042 
1043 Representation::~Representation() {}
1044 
1046  if (!AtLeastOneTrue(media_info_.has_video_info(),
1047  media_info_.has_audio_info(),
1048  media_info_.has_text_info())) {
1049  // This is an error. Segment information can be in AdaptationSet, Period, or
1050  // MPD but the interface does not provide a way to set them.
1051  // See 5.3.9.1 ISO 23009-1:2012 for segment info.
1052  LOG(ERROR) << "Representation needs one of video, audio, or text.";
1053  return false;
1054  }
1055 
1056  if (MoreThanOneTrue(media_info_.has_video_info(),
1057  media_info_.has_audio_info(),
1058  media_info_.has_text_info())) {
1059  LOG(ERROR) << "Only one of VideoInfo, AudioInfo, or TextInfo can be set.";
1060  return false;
1061  }
1062 
1063  if (media_info_.container_type() == MediaInfo::CONTAINER_UNKNOWN) {
1064  LOG(ERROR) << "'container_type' in MediaInfo cannot be CONTAINER_UNKNOWN.";
1065  return false;
1066  }
1067 
1068  if (media_info_.has_video_info()) {
1069  mime_type_ = GetVideoMimeType();
1070  if (!HasRequiredVideoFields(media_info_.video_info())) {
1071  LOG(ERROR) << "Missing required fields to create a video Representation.";
1072  return false;
1073  }
1074  } else if (media_info_.has_audio_info()) {
1075  mime_type_ = GetAudioMimeType();
1076  } else if (media_info_.has_text_info()) {
1077  mime_type_ = GetTextMimeType();
1078  }
1079 
1080  if (mime_type_.empty())
1081  return false;
1082 
1083  codecs_ = GetCodecs(media_info_);
1084  return true;
1085 }
1086 
1088  const ContentProtectionElement& content_protection_element) {
1089  content_protection_elements_.push_back(content_protection_element);
1090  RemoveDuplicateAttributes(&content_protection_elements_.back());
1091 }
1092 
1093 void Representation::UpdateContentProtectionPssh(const std::string& drm_uuid,
1094  const std::string& pssh) {
1095  UpdateContentProtectionPsshHelper(drm_uuid, pssh,
1096  &content_protection_elements_);
1097 }
1098 
1099 void Representation::AddNewSegment(uint64_t start_time,
1100  uint64_t duration,
1101  uint64_t size) {
1102  if (start_time == 0 && duration == 0) {
1103  LOG(WARNING) << "Got segment with start_time and duration == 0. Ignoring.";
1104  return;
1105  }
1106 
1107  if (state_change_listener_)
1108  state_change_listener_->OnNewSegmentForRepresentation(start_time, duration);
1109  if (IsContiguous(start_time, duration, size)) {
1110  ++segment_infos_.back().repeat;
1111  } else {
1112  SegmentInfo s = {start_time, duration, /* Not repeat. */ 0};
1113  segment_infos_.push_back(s);
1114  }
1115 
1116  bandwidth_estimator_.AddBlock(
1117  size, static_cast<double>(duration) / media_info_.reference_time_scale());
1118 
1119  SlideWindow();
1120  DCHECK_GE(segment_infos_.size(), 1u);
1121 }
1122 
1123 void Representation::SetSampleDuration(uint32_t sample_duration) {
1124  if (media_info_.has_video_info()) {
1125  media_info_.mutable_video_info()->set_frame_duration(sample_duration);
1126  if (state_change_listener_) {
1127  state_change_listener_->OnSetFrameRateForRepresentation(
1128  sample_duration, media_info_.video_info().time_scale());
1129  }
1130  }
1131 }
1132 
1133 // Uses info in |media_info_| and |content_protection_elements_| to create a
1134 // "Representation" node.
1135 // MPD schema has strict ordering. The following must be done in order.
1136 // AddVideoInfo() (possibly adds FramePacking elements), AddAudioInfo() (Adds
1137 // AudioChannelConfig elements), AddContentProtectionElements*(), and
1138 // AddVODOnlyInfo() (Adds segment info).
1139 xml::scoped_xml_ptr<xmlNode> Representation::GetXml() {
1140  if (!HasRequiredMediaInfoFields()) {
1141  LOG(ERROR) << "MediaInfo missing required fields.";
1142  return xml::scoped_xml_ptr<xmlNode>();
1143  }
1144 
1145  const uint64_t bandwidth = media_info_.has_bandwidth()
1146  ? media_info_.bandwidth()
1147  : bandwidth_estimator_.Estimate();
1148 
1149  DCHECK(!(HasVODOnlyFields(media_info_) && HasLiveOnlyFields(media_info_)));
1150 
1151  RepresentationXmlNode representation;
1152  // Mandatory fields for Representation.
1153  representation.SetId(id_);
1154  representation.SetIntegerAttribute("bandwidth", bandwidth);
1155  if (!codecs_.empty())
1156  representation.SetStringAttribute("codecs", codecs_);
1157  representation.SetStringAttribute("mimeType", mime_type_);
1158 
1159  const bool has_video_info = media_info_.has_video_info();
1160  const bool has_audio_info = media_info_.has_audio_info();
1161 
1162  if (has_video_info &&
1163  !representation.AddVideoInfo(
1164  media_info_.video_info(),
1165  !(output_suppression_flags_ & kSuppressWidth),
1166  !(output_suppression_flags_ & kSuppressHeight),
1167  !(output_suppression_flags_ & kSuppressFrameRate))) {
1168  LOG(ERROR) << "Failed to add video info to Representation XML.";
1169  return xml::scoped_xml_ptr<xmlNode>();
1170  }
1171 
1172  if (has_audio_info &&
1173  !representation.AddAudioInfo(media_info_.audio_info())) {
1174  LOG(ERROR) << "Failed to add audio info to Representation XML.";
1175  return xml::scoped_xml_ptr<xmlNode>();
1176  }
1177 
1178  if (!representation.AddContentProtectionElements(
1179  content_protection_elements_)) {
1180  return xml::scoped_xml_ptr<xmlNode>();
1181  }
1182 
1183  if (HasVODOnlyFields(media_info_) &&
1184  !representation.AddVODOnlyInfo(media_info_)) {
1185  LOG(ERROR) << "Failed to add VOD segment info.";
1186  return xml::scoped_xml_ptr<xmlNode>();
1187  }
1188 
1189  if (HasLiveOnlyFields(media_info_) &&
1190  !representation.AddLiveOnlyInfo(media_info_, segment_infos_,
1191  start_number_)) {
1192  LOG(ERROR) << "Failed to add Live info.";
1193  return xml::scoped_xml_ptr<xmlNode>();
1194  }
1195  // TODO(rkuroiwa): It is likely that all representations have the exact same
1196  // SegmentTemplate. Optimize and propagate the tag up to AdaptationSet level.
1197 
1198  output_suppression_flags_ = 0;
1199  return representation.PassScopedPtr();
1200 }
1201 
1202 void Representation::SuppressOnce(SuppressFlag flag) {
1203  output_suppression_flags_ |= flag;
1204 }
1205 
1206 bool Representation::HasRequiredMediaInfoFields() {
1207  if (HasVODOnlyFields(media_info_) && HasLiveOnlyFields(media_info_)) {
1208  LOG(ERROR) << "MediaInfo cannot have both VOD and Live fields.";
1209  return false;
1210  }
1211 
1212  if (!media_info_.has_container_type()) {
1213  LOG(ERROR) << "MediaInfo missing required field: container_type.";
1214  return false;
1215  }
1216 
1217  if (HasVODOnlyFields(media_info_) && !media_info_.has_bandwidth()) {
1218  LOG(ERROR) << "Missing 'bandwidth' field. MediaInfo requires bandwidth for "
1219  "static profile for generating a valid MPD.";
1220  return false;
1221  }
1222 
1223  VLOG_IF(3, HasLiveOnlyFields(media_info_) && !media_info_.has_bandwidth())
1224  << "MediaInfo missing field 'bandwidth'. Using estimated from "
1225  "segment size.";
1226 
1227  return true;
1228 }
1229 
1230 bool Representation::IsContiguous(uint64_t start_time,
1231  uint64_t duration,
1232  uint64_t size) const {
1233  if (segment_infos_.empty())
1234  return false;
1235 
1236  // Contiguous segment.
1237  const SegmentInfo& previous = segment_infos_.back();
1238  const uint64_t previous_segment_end_time =
1239  previous.start_time + previous.duration * (previous.repeat + 1);
1240  if (previous_segment_end_time == start_time &&
1241  segment_infos_.back().duration == duration) {
1242  return true;
1243  }
1244 
1245  // No out of order segments.
1246  const uint64_t previous_segment_start_time =
1247  previous.start_time + previous.duration * previous.repeat;
1248  if (previous_segment_start_time >= start_time) {
1249  LOG(ERROR) << "Segments should not be out of order segment. Adding segment "
1250  "with start_time == "
1251  << start_time << " but the previous segment starts at "
1252  << previous.start_time << ".";
1253  return false;
1254  }
1255 
1256  // A gap since previous.
1257  const uint64_t kRoundingErrorGrace = 5;
1258  if (previous_segment_end_time + kRoundingErrorGrace < start_time) {
1259  LOG(WARNING) << "Found a gap of size "
1260  << (start_time - previous_segment_end_time)
1261  << " > kRoundingErrorGrace (" << kRoundingErrorGrace
1262  << "). The new segment starts at " << start_time
1263  << " but the previous segment ends at "
1264  << previous_segment_end_time << ".";
1265  return false;
1266  }
1267 
1268  // No overlapping segments.
1269  if (start_time < previous_segment_end_time - kRoundingErrorGrace) {
1270  LOG(WARNING)
1271  << "Segments should not be overlapping. The new segment starts at "
1272  << start_time << " but the previous segment ends at "
1273  << previous_segment_end_time << ".";
1274  return false;
1275  }
1276 
1277  // Within rounding error grace but technically not contiguous in terms of MPD.
1278  return false;
1279 }
1280 
1281 void Representation::SlideWindow() {
1282  DCHECK(!segment_infos_.empty());
1283  if (mpd_options_.time_shift_buffer_depth <= 0.0)
1284  return;
1285 
1286  const uint32_t time_scale = GetTimeScale(media_info_);
1287  DCHECK_GT(time_scale, 0u);
1288 
1289  uint64_t time_shift_buffer_depth =
1290  static_cast<uint64_t>(mpd_options_.time_shift_buffer_depth * time_scale);
1291 
1292  // The start time of the latest segment is considered the current_play_time,
1293  // and this should guarantee that the latest segment will stay in the list.
1294  const uint64_t current_play_time = LatestSegmentStartTime(segment_infos_);
1295  if (current_play_time <= time_shift_buffer_depth)
1296  return;
1297 
1298  const uint64_t timeshift_limit = current_play_time - time_shift_buffer_depth;
1299 
1300  // First remove all the SegmentInfos that are completely out of range, by
1301  // looking at the very last segment's end time.
1302  std::list<SegmentInfo>::iterator first = segment_infos_.begin();
1303  std::list<SegmentInfo>::iterator last = first;
1304  size_t num_segments_removed = 0;
1305  for (; last != segment_infos_.end(); ++last) {
1306  const uint64_t last_segment_end_time = LastSegmentEndTime(*last);
1307  if (timeshift_limit < last_segment_end_time)
1308  break;
1309  num_segments_removed += last->repeat + 1;
1310  }
1311  segment_infos_.erase(first, last);
1312  start_number_ += num_segments_removed;
1313 
1314  // Now some segment in the first SegmentInfo should be left in the list.
1315  SegmentInfo* first_segment_info = &segment_infos_.front();
1316  DCHECK_LE(timeshift_limit, LastSegmentEndTime(*first_segment_info));
1317 
1318  // Identify which segments should still be in the SegmentInfo.
1319  const int repeat_index =
1320  SearchTimedOutRepeatIndex(timeshift_limit, *first_segment_info);
1321  CHECK_GE(repeat_index, 0);
1322  if (repeat_index == 0)
1323  return;
1324 
1325  first_segment_info->start_time = first_segment_info->start_time +
1326  first_segment_info->duration * repeat_index;
1327 
1328  first_segment_info->repeat = first_segment_info->repeat - repeat_index;
1329  start_number_ += repeat_index;
1330 }
1331 
1332 std::string Representation::GetVideoMimeType() const {
1333  return GetMimeType("video", media_info_.container_type());
1334 }
1335 
1336 std::string Representation::GetAudioMimeType() const {
1337  return GetMimeType("audio", media_info_.container_type());
1338 }
1339 
1340 std::string Representation::GetTextMimeType() const {
1341  CHECK(media_info_.has_text_info());
1342  if (media_info_.text_info().format() == "ttml") {
1343  switch (media_info_.container_type()) {
1344  case MediaInfo::CONTAINER_TEXT:
1345  return "application/ttml+xml";
1346  case MediaInfo::CONTAINER_MP4:
1347  return "application/mp4";
1348  default:
1349  LOG(ERROR) << "Failed to determine MIME type for TTML container: "
1350  << media_info_.container_type();
1351  return "";
1352  }
1353  }
1354  if (media_info_.text_info().format() == "vtt") {
1355  if (media_info_.container_type() == MediaInfo::CONTAINER_TEXT) {
1356  return "text/vtt";
1357  }
1358  LOG(ERROR) << "Failed to determine MIME type for VTT container: "
1359  << media_info_.container_type();
1360  return "";
1361  }
1362 
1363  LOG(ERROR) << "Cannot determine MIME type for format: "
1364  << media_info_.text_info().format()
1365  << " container: " << media_info_.container_type();
1366  return "";
1367 }
1368 
1369 bool Representation::GetEarliestTimestamp(double* timestamp_seconds) {
1370  DCHECK(timestamp_seconds);
1371 
1372  if (segment_infos_.empty())
1373  return false;
1374 
1375  *timestamp_seconds = static_cast<double>(segment_infos_.begin()->start_time) /
1376  GetTimeScale(media_info_);
1377  return true;
1378 }
1379 
1380 } // namespace shaka
void OnSetFrameRateForRepresentation(uint32_t representation_id, uint32_t frame_duration, uint32_t timescale)
Definition: mpd_builder.cc:863
virtual void AddNewSegment(uint64_t start_time, uint64_t duration, uint64_t size)
bool WriteMpdToFile(media::File *output_file)
Definition: mpd_builder.cc:423
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:669
virtual void SetSampleDuration(uint32_t sample_duration)
virtual Representation * AddRepresentation(const MediaInfo &media_info)
Definition: mpd_builder.cc:688
std::string LanguageToShortestForm(const std::string &language)
Define an abstract file interface.
Definition: file.h:24
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
Definition: mpd_builder.cc:732
Representation(const MediaInfo &media_info, const MpdOptions &mpd_options, uint32_t representation_id, scoped_ptr< RepresentationStateChangeListener > state_change_listener)
virtual void AddRole(Role role)
Definition: mpd_builder.cc:744
void AddBaseUrl(const std::string &base_url)
Definition: mpd_builder.cc:409
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
Definition: mpd_builder.cc:738
virtual void SetGroup(int group_number)
Definition: mpd_builder.cc:837
xml::scoped_xml_ptr< xmlNode > GetXml()
virtual bool ToString(std::string *output)
Definition: mpd_builder.cc:428
virtual void ForceSetSegmentAlignment(bool segment_alignment)
Definition: mpd_builder.cc:831
static void MakePathsRelativeToMpd(const std::string &mpd_path, MediaInfo *media_info)
Definition: mpd_builder.cc:641
MpdBuilder(MpdType type, const MpdOptions &mpd_options)
Definition: mpd_builder.cc:401
xml::scoped_xml_ptr< xmlNode > GetXml()
Definition: mpd_builder.cc:754
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
virtual AdaptationSet * AddAdaptationSet(const std::string &lang)
Definition: mpd_builder.cc:413
virtual int Group() const
Definition: mpd_builder.cc:841
Defines Mpd Options.
Definition: mpd_options.h:17
void OnNewSegmentForRepresentation(uint32_t representation_id, uint64_t start_time, uint64_t duration)
Definition: mpd_builder.cc:852
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
void SuppressOnce(SuppressFlag flag)