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