Shaka Packager SDK
Loading...
Searching...
No Matches
adaptation_set.cc
1// Copyright 2017 Google LLC. 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/adaptation_set.h>
8
9#include <cmath>
10
11#include <absl/log/check.h>
12#include <absl/log/log.h>
13#include <absl/strings/numbers.h>
14#include <absl/strings/str_format.h>
15
16#include <packager/macros/classes.h>
17#include <packager/macros/logging.h>
18#include <packager/mpd/base/media_info.pb.h>
19#include <packager/mpd/base/mpd_options.h>
20#include <packager/mpd/base/mpd_utils.h>
21#include <packager/mpd/base/representation.h>
22#include <packager/mpd/base/xml/xml_node.h>
23
24namespace shaka {
25namespace {
26
27AdaptationSet::Role MediaInfoTextTypeToRole(
28 MediaInfo::TextInfo::TextType type) {
29 switch (type) {
30 case MediaInfo::TextInfo::UNKNOWN:
31 LOG(WARNING) << "Unknown text type, assuming subtitle.";
32 return AdaptationSet::kRoleSubtitle;
33 case MediaInfo::TextInfo::CAPTION:
34 return AdaptationSet::kRoleCaption;
35 case MediaInfo::TextInfo::SUBTITLE:
36 return AdaptationSet::kRoleSubtitle;
37 default:
38 NOTIMPLEMENTED() << "Unknown MediaInfo TextType: " << type
39 << " assuming subtitle.";
40 return AdaptationSet::kRoleSubtitle;
41 }
42}
43
44std::string RoleToText(AdaptationSet::Role role) {
45 // Using switch so that the compiler can detect whether there is a case that's
46 // not being handled.
47 switch (role) {
48 case AdaptationSet::kRoleCaption:
49 return "caption";
50 case AdaptationSet::kRoleSubtitle:
51 return "subtitle";
52 case AdaptationSet::kRoleMain:
53 return "main";
54 case AdaptationSet::kRoleAlternate:
55 return "alternate";
56 case AdaptationSet::kRoleSupplementary:
57 return "supplementary";
58 case AdaptationSet::kRoleCommentary:
59 return "commentary";
60 case AdaptationSet::kRoleDub:
61 return "dub";
62 case AdaptationSet::kRoleDescription:
63 return "description";
64 case AdaptationSet::kRoleSign:
65 return "sign";
66 case AdaptationSet::kRoleMetadata:
67 return "metadata";
68 case AdaptationSet::kRoleEnhancedAudioIntelligibility:
69 return "enhanced-audio-intelligibility";
70 case AdaptationSet::kRoleEmergency:
71 return "emergency";
72 case AdaptationSet::kRoleForcedSubtitle:
73 return "forced-subtitle";
74 case AdaptationSet::kRoleEasyreader:
75 return "easyreader";
76 case AdaptationSet::kRoleKaraoke:
77 return "karaoke";
78 default:
79 return "unknown";
80 }
81}
82
83// Returns the picture aspect ratio string e.g. "16:9", "4:3".
84// "Reducing the quotient to minimal form" does not work well in practice as
85// there may be some rounding performed in the input, e.g. the resolution of
86// 480p is 854:480 for 16:9 aspect ratio, can only be reduced to 427:240.
87// The algorithm finds out the pair of integers, num and den, where num / den is
88// the closest ratio to scaled_width / scaled_height, by looping den through
89// common values.
90std::string GetPictureAspectRatio(uint32_t width,
91 uint32_t height,
92 uint32_t pixel_width,
93 uint32_t pixel_height) {
94 const uint32_t scaled_width = pixel_width * width;
95 const uint32_t scaled_height = pixel_height * height;
96 const double par = static_cast<double>(scaled_width) / scaled_height;
97
98 // Typical aspect ratios have par_y less than or equal to 19:
99 // https://en.wikipedia.org/wiki/List_of_common_resolutions
100 const uint32_t kLargestPossibleParY = 19;
101
102 uint32_t par_num = 0;
103 uint32_t par_den = 0;
104 double min_error = 1.0;
105 for (uint32_t den = 1; den <= kLargestPossibleParY; ++den) {
106 uint32_t num = par * den + 0.5;
107 double error = fabs(par - static_cast<double>(num) / den);
108 if (error < min_error) {
109 min_error = error;
110 par_num = num;
111 par_den = den;
112 if (error == 0)
113 break;
114 }
115 }
116 VLOG(2) << "width*pix_width : height*pixel_height (" << scaled_width << ":"
117 << scaled_height << ") reduced to " << par_num << ":" << par_den
118 << " with error " << min_error << ".";
119
120 return absl::StrFormat("%d:%d", par_num, par_den);
121}
122
123// Adds an entry to picture_aspect_ratio if the size of picture_aspect_ratio is
124// less than 2 and video_info has both pixel width and pixel height.
125void AddPictureAspectRatio(const MediaInfo::VideoInfo& video_info,
126 std::set<std::string>* picture_aspect_ratio) {
127 // If there are more than one entries in picture_aspect_ratio, the @par
128 // attribute cannot be set, so skip.
129 if (picture_aspect_ratio->size() > 1)
130 return;
131
132 if (video_info.width() == 0 || video_info.height() == 0 ||
133 video_info.pixel_width() == 0 || video_info.pixel_height() == 0) {
134 // If there is even one Representation without a @sar attribute, @par cannot
135 // be calculated.
136 // Just populate the set with at least 2 bogus strings so that further call
137 // to this function will bail out immediately.
138 picture_aspect_ratio->insert("bogus");
139 picture_aspect_ratio->insert("entries");
140 return;
141 }
142
143 const std::string par = GetPictureAspectRatio(
144 video_info.width(), video_info.height(), video_info.pixel_width(),
145 video_info.pixel_height());
146 DVLOG(1) << "Setting par as: " << par
147 << " for video with width: " << video_info.width()
148 << " height: " << video_info.height()
149 << " pixel_width: " << video_info.pixel_width() << " pixel_height; "
150 << video_info.pixel_height();
151 picture_aspect_ratio->insert(par);
152}
153
154class RepresentationStateChangeListenerImpl
155 : public RepresentationStateChangeListener {
156 public:
157 // |adaptation_set| is not owned by this class.
158 RepresentationStateChangeListenerImpl(uint32_t representation_id,
159 AdaptationSet* adaptation_set)
160 : representation_id_(representation_id), adaptation_set_(adaptation_set) {
161 DCHECK(adaptation_set_);
162 }
163 ~RepresentationStateChangeListenerImpl() override {}
164
165 // RepresentationStateChangeListener implementation.
166 void OnNewSegmentForRepresentation(int64_t start_time,
167 int64_t duration) override {
168 adaptation_set_->OnNewSegmentForRepresentation(representation_id_,
169 start_time, duration);
170 }
171
172 void OnSetFrameRateForRepresentation(int32_t frame_duration,
173 int32_t timescale) override {
174 adaptation_set_->OnSetFrameRateForRepresentation(representation_id_,
175 frame_duration, timescale);
176 }
177
178 private:
179 const uint32_t representation_id_;
180 AdaptationSet* const adaptation_set_;
181
182 DISALLOW_COPY_AND_ASSIGN(RepresentationStateChangeListenerImpl);
183};
184
185} // namespace
186
187AdaptationSet::AdaptationSet(const std::string& language,
188 const MpdOptions& mpd_options,
189 uint32_t* counter)
190 : representation_counter_(counter),
191 language_(language),
192 mpd_options_(mpd_options),
193 protected_content_(nullptr) {
194 DCHECK(counter);
195}
196
197AdaptationSet::~AdaptationSet() {
198 delete protected_content_;
199}
200
201void AdaptationSet::set_protected_content(const MediaInfo& media_info) {
202 DCHECK(!protected_content_);
203 protected_content_ =
204 new MediaInfo::ProtectedContent(media_info.protected_content());
205}
206
207// The easiest way to check whether two protobufs are equal, is to compare the
208// serialized version.
209bool ProtectedContentEq(
210 const MediaInfo::ProtectedContent& content_protection1,
211 const MediaInfo::ProtectedContent& content_protection2) {
212 return content_protection1.SerializeAsString() ==
213 content_protection2.SerializeAsString();
214}
215
217 const MediaInfo& media_info,
218 bool content_protection_in_adaptation_set) {
219 if (codec_ != GetBaseCodec(media_info))
220 return false;
221
222 if (!content_protection_in_adaptation_set)
223 return true;
224
225 if (!protected_content_)
226 return !media_info.has_protected_content();
227
228 if (!media_info.has_protected_content())
229 return false;
230
231 return ProtectedContentEq(*protected_content_,
232 media_info.protected_content());
233}
234
235std::set<std::string> GetUUIDs(
236 const MediaInfo::ProtectedContent* protected_content) {
237 std::set<std::string> uuids;
238 for (const auto& entry : protected_content->content_protection_entry())
239 uuids.insert(entry.uuid());
240 return uuids;
241}
242
244 const AdaptationSet& adaptation_set) {
245 // adaptation sets are switchable if both are not protected
246 if (!protected_content_ && !adaptation_set.protected_content()) {
247 return true;
248 }
249
250 // or if both are protected and have the same UUID
251 if (protected_content_ && adaptation_set.protected_content()) {
252 return GetUUIDs(protected_content_) ==
253 GetUUIDs(adaptation_set.protected_content());
254 }
255
256 return false;
257}
258
260 const uint32_t representation_id = media_info.has_index()
261 ? media_info.index()
262 : (*representation_counter_)++;
263
264 // Note that AdaptationSet outlive Representation, so this object
265 // will die before AdaptationSet.
266 std::unique_ptr<RepresentationStateChangeListener> listener(
267 new RepresentationStateChangeListenerImpl(representation_id, this));
268 std::unique_ptr<Representation> new_representation(new Representation(
269 media_info, mpd_options_, representation_id, std::move(listener)));
270
271 if (!new_representation->Init()) {
272 LOG(ERROR) << "Failed to initialize Representation.";
273 return NULL;
274 }
275 UpdateFromMediaInfo(media_info);
276 Representation* representation_ptr = new_representation.get();
277 representation_map_[representation_ptr->id()] = std::move(new_representation);
278 return representation_ptr;
279}
280
282 const Representation& representation) {
283 // Note that AdaptationSet outlive Representation, so this object
284 // will die before AdaptationSet.
285 std::unique_ptr<RepresentationStateChangeListener> listener(
286 new RepresentationStateChangeListenerImpl(representation.id(), this));
287 std::unique_ptr<Representation> new_representation(
288 new Representation(representation, std::move(listener)));
289
290 UpdateFromMediaInfo(new_representation->GetMediaInfo());
291 Representation* representation_ptr = new_representation.get();
292 representation_map_[representation_ptr->id()] = std::move(new_representation);
293 return representation_ptr;
294}
295
297 const ContentProtectionElement& content_protection_element) {
298 content_protection_elements_.push_back(content_protection_element);
299 RemoveDuplicateAttributes(&content_protection_elements_.back());
300}
301
302void AdaptationSet::UpdateContentProtectionPssh(const std::string& drm_uuid,
303 const std::string& pssh) {
304 UpdateContentProtectionPsshHelper(drm_uuid, pssh,
305 &content_protection_elements_);
306}
307
308void AdaptationSet::AddAccessibility(const std::string& scheme,
309 const std::string& value) {
310 accessibilities_.push_back(Accessibility{scheme, value});
311}
312
313void AdaptationSet::AddRole(Role role) {
314 roles_.insert(role);
315}
316
317// Creates a copy of <AdaptationSet> xml element, iterate thru all the
318// <Representation> (child) elements and add them to the copy.
319// Set all the attributes first and then add the children elements so that flags
320// can be passed to Representation to avoid setting redundant attributes. For
321// example, if AdaptationSet@width is set, then Representation@width is
322// redundant and should not be set.
323std::optional<xml::XmlNode> AdaptationSet::GetXml() {
324 xml::AdaptationSetXmlNode adaptation_set;
325
326 bool suppress_representation_width = false;
327 bool suppress_representation_height = false;
328 bool suppress_representation_frame_rate = false;
329
330 if (id_ && !adaptation_set.SetId(id_.value()))
331 return std::nullopt;
332 if (!adaptation_set.SetStringAttribute("contentType", content_type_))
333 return std::nullopt;
334 if (!language_.empty() && language_ != "und" &&
335 !adaptation_set.SetStringAttribute("lang", language_)) {
336 return std::nullopt;
337 }
338
339 // Note that std::{set,map} are ordered, so the last element is the max value.
340 if (video_widths_.size() == 1) {
341 suppress_representation_width = true;
342 if (!adaptation_set.SetIntegerAttribute("width", *video_widths_.begin()))
343 return std::nullopt;
344 } else if (video_widths_.size() > 1) {
345 if (!adaptation_set.SetIntegerAttribute("maxWidth",
346 *video_widths_.rbegin())) {
347 return std::nullopt;
348 }
349 }
350
351 if (video_heights_.size() == 1) {
352 suppress_representation_height = true;
353 if (!adaptation_set.SetIntegerAttribute("height", *video_heights_.begin()))
354 return std::nullopt;
355 } else if (video_heights_.size() > 1) {
356 if (!adaptation_set.SetIntegerAttribute("maxHeight",
357 *video_heights_.rbegin())) {
358 return std::nullopt;
359 }
360 }
361
362 if (subsegment_start_with_sap_) {
363 if (!adaptation_set.SetIntegerAttribute("subsegmentStartsWithSAP",
364 subsegment_start_with_sap_))
365 return std::nullopt;
366 } else if (start_with_sap_) {
367 if (!adaptation_set.SetIntegerAttribute("startWithSAP", start_with_sap_))
368 return std::nullopt;
369 }
370
371 if (video_frame_rates_.size() == 1) {
372 suppress_representation_frame_rate = true;
373 if (!adaptation_set.SetStringAttribute(
374 "frameRate", video_frame_rates_.begin()->second)) {
375 return std::nullopt;
376 }
377 } else if (video_frame_rates_.size() > 1) {
378 if (!adaptation_set.SetStringAttribute(
379 "maxFrameRate", video_frame_rates_.rbegin()->second)) {
380 return std::nullopt;
381 }
382 }
383
384 // https://dashif.org/docs/DASH-IF-IOP-v4.3.pdf - 4.2.5.1
385 if (IsVideo() && matrix_coefficients_ > 0 &&
386 !adaptation_set.AddSupplementalProperty(
387 "urn:mpeg:mpegB:cicp:MatrixCoefficients",
388 std::to_string(matrix_coefficients_))) {
389 return std::nullopt;
390 }
391
392 // https://dashif.org/docs/DASH-IF-IOP-v4.3.pdf - 4.2.5.1
393 if (IsVideo() && color_primaries_ > 0 &&
394 !adaptation_set.AddSupplementalProperty(
395 "urn:mpeg:mpegB:cicp:ColourPrimaries",
396 std::to_string(color_primaries_))) {
397 return std::nullopt;
398 }
399
400 // https://dashif.org/docs/DASH-IF-IOP-v4.3.pdf - 4.2.5.1
401 if (IsVideo() && transfer_characteristics_ > 0 &&
402 !adaptation_set.AddSupplementalProperty(
403 "urn:mpeg:mpegB:cicp:TransferCharacteristics",
404 std::to_string(transfer_characteristics_))) {
405 return std::nullopt;
406 }
407
408 // Note: must be checked before checking segments_aligned_ (below). So that
409 // segments_aligned_ is set before checking below.
410 if (mpd_options_.mpd_type == MpdType::kStatic) {
411 CheckStaticSegmentAlignment();
412 }
413
414 if (segments_aligned_ == kSegmentAlignmentTrue) {
415 if (!adaptation_set.SetStringAttribute(
416 mpd_options_.dash_profile == DashProfile::kOnDemand
417 ? "subsegmentAlignment"
418 : "segmentAlignment",
419 "true")) {
420 return std::nullopt;
421 }
422 }
423
424 if (picture_aspect_ratio_.size() == 1 &&
425 !adaptation_set.SetStringAttribute("par",
426 *picture_aspect_ratio_.begin())) {
427 return std::nullopt;
428 }
429
430 if (!adaptation_set.AddContentProtectionElements(
431 content_protection_elements_)) {
432 return std::nullopt;
433 }
434
435 std::string trick_play_reference_ids;
436 for (const AdaptationSet* tp_adaptation_set : trick_play_references_) {
437 // Should be a whitespace-separated list, see DASH-IOP 3.2.9.
438 if (!trick_play_reference_ids.empty())
439 trick_play_reference_ids += ' ';
440 CHECK(tp_adaptation_set->has_id());
441 trick_play_reference_ids += std::to_string(tp_adaptation_set->id());
442 }
443 if (!trick_play_reference_ids.empty() &&
444 !adaptation_set.AddEssentialProperty(
445 "http://dashif.org/guidelines/trickmode", trick_play_reference_ids)) {
446 return std::nullopt;
447 }
448
449 std::string switching_ids;
450 for (const AdaptationSet* s_adaptation_set : switchable_adaptation_sets_) {
451 // Should be a comma-separated list, see DASH-IOP 3.8.
452 if (!switching_ids.empty())
453 switching_ids += ',';
454 CHECK(s_adaptation_set->has_id());
455 switching_ids += std::to_string(s_adaptation_set->id());
456 }
457 if (!switching_ids.empty() &&
458 !adaptation_set.AddSupplementalProperty(
459 "urn:mpeg:dash:adaptation-set-switching:2016", switching_ids)) {
460 return std::nullopt;
461 }
462
463 for (const AdaptationSet::Accessibility& accessibility : accessibilities_) {
464 if (!adaptation_set.AddAccessibilityElement(accessibility.scheme,
465 accessibility.value)) {
466 return std::nullopt;
467 }
468 }
469
470 for (AdaptationSet::Role role : roles_) {
471 if (!adaptation_set.AddRoleElement("urn:mpeg:dash:role:2011",
472 RoleToText(role))) {
473 return std::nullopt;
474 }
475 }
476
477 if (!label_.empty() && !adaptation_set.AddLabelElement(label_))
478 return std::nullopt;
479
480 for (const auto& representation_pair : representation_map_) {
481 const auto& representation = representation_pair.second;
482 if (suppress_representation_width)
483 representation->SuppressOnce(Representation::kSuppressWidth);
484 if (suppress_representation_height)
485 representation->SuppressOnce(Representation::kSuppressHeight);
486 if (suppress_representation_frame_rate)
487 representation->SuppressOnce(Representation::kSuppressFrameRate);
488 auto child = representation->GetXml();
489 if (!child || !adaptation_set.AddChild(std::move(*child)))
490 return std::nullopt;
491 }
492
493 return adaptation_set;
494}
495
496void AdaptationSet::ForceSetSegmentAlignment(bool segment_alignment) {
497 segments_aligned_ =
498 segment_alignment ? kSegmentAlignmentTrue : kSegmentAlignmentFalse;
499 force_set_segment_alignment_ = true;
500}
501
503 const AdaptationSet* adaptation_set) {
504 switchable_adaptation_sets_.push_back(adaptation_set);
505}
506
508 subsegment_start_with_sap_ = sap_value;
509}
510
511void AdaptationSet::ForceStartwithSAP(uint32_t sap_value) {
512 start_with_sap_ = sap_value;
513}
514
515// For dynamic MPD, storing all start_time and duration will out-of-memory
516// because there's no way of knowing when it will end. Static MPD
517// subsegmentAlignment check is *not* done here because it is possible that some
518// Representations might not have been added yet (e.g. a thread is assigned per
519// muxer so one might run faster than others). To be clear, for dynamic MPD, all
520// Representations should be added before a segment is added.
521void AdaptationSet::OnNewSegmentForRepresentation(uint32_t representation_id,
522 int64_t start_time,
523 int64_t duration) {
524 if (mpd_options_.mpd_type == MpdType::kDynamic) {
525 CheckDynamicSegmentAlignment(representation_id, start_time, duration);
526 } else {
527 representation_segment_start_times_[representation_id].push_back(
528 start_time);
529 }
530}
531
533 int32_t frame_duration,
534 int32_t timescale) {
535 RecordFrameRate(frame_duration, timescale);
536}
537
539 trick_play_references_.push_back(adaptation_set);
540}
541
542const std::list<Representation*> AdaptationSet::GetRepresentations() const {
543 std::list<Representation*> representations;
544 for (const auto& representation_pair : representation_map_) {
545 representations.push_back(representation_pair.second.get());
546 }
547 return representations;
548}
549
551 return content_type_ == "video";
552}
553
554void AdaptationSet::UpdateFromMediaInfo(const MediaInfo& media_info) {
555 // For videos, record the width, height, and the frame rate to calculate the
556 // max {width,height,framerate} required for DASH IOP.
557 if (media_info.has_video_info()) {
558 const MediaInfo::VideoInfo& video_info = media_info.video_info();
559 DCHECK(video_info.has_width());
560 DCHECK(video_info.has_height());
561 video_widths_.insert(video_info.width());
562 video_heights_.insert(video_info.height());
563
564 if (video_info.has_time_scale() && video_info.has_frame_duration())
565 RecordFrameRate(video_info.frame_duration(), video_info.time_scale());
566
567 AddPictureAspectRatio(video_info, &picture_aspect_ratio_);
568 }
569
570 // the command-line index for this AdaptationSet will be the
571 // minimum of the Representations in the set
572 if (media_info.has_index()) {
573 if (index_.has_value()) {
574 index_ = std::min(index_.value(), media_info.index());
575 } else {
576 index_ = media_info.index();
577 }
578 }
579
580 if (media_info.has_dash_label())
581 label_ = media_info.dash_label();
582
583 if (media_info.has_video_info()) {
584 content_type_ = "video";
585 } else if (media_info.has_audio_info()) {
586 content_type_ = "audio";
587 } else if (media_info.has_text_info()) {
588 content_type_ = "text";
589
590 if (media_info.text_info().has_type() &&
591 (media_info.text_info().type() != MediaInfo::TextInfo::UNKNOWN)) {
592 roles_.insert(MediaInfoTextTypeToRole(media_info.text_info().type()));
593 }
594 }
595}
596
597// This implementation assumes that each representations' segments' are
598// contiguous.
599// Also assumes that all Representations are added before this is called.
600// This checks whether the first elements of the lists in
601// representation_segment_start_times_ are aligned.
602// For example, suppose this method was just called with args rep_id=2
603// start_time=1.
604// 1 -> [1, 100, 200]
605// 2 -> [1]
606// The timestamps of the first elements match, so this flags
607// segments_aligned_=true.
608// Also since the first segment start times match, the first element of all the
609// lists are removed, so the map of lists becomes:
610// 1 -> [100, 200]
611// 2 -> []
612// Note that there could be false positives.
613// e.g. just got rep_id=3 start_time=1 duration=300, and the duration of the
614// whole AdaptationSet is 300.
615// 1 -> [1, 100, 200]
616// 2 -> [1, 90, 100]
617// 3 -> [1]
618// They are not aligned but this will be marked as aligned.
619// But since this is unlikely to happen in the packager (and to save
620// computation), this isn't handled at the moment.
621void AdaptationSet::CheckDynamicSegmentAlignment(uint32_t representation_id,
622 int64_t start_time,
623 int64_t /* duration */) {
624 if (segments_aligned_ == kSegmentAlignmentFalse ||
625 force_set_segment_alignment_) {
626 return;
627 }
628
629 std::list<int64_t>& current_representation_start_times =
630 representation_segment_start_times_[representation_id];
631 current_representation_start_times.push_back(start_time);
632 // There's no way to detemine whether the segments are aligned if some
633 // representations do not have any segments.
634 if (representation_segment_start_times_.size() != representation_map_.size())
635 return;
636
637 DCHECK(!current_representation_start_times.empty());
638 const int64_t expected_start_time =
639 current_representation_start_times.front();
640 for (const auto& key_value : representation_segment_start_times_) {
641 const std::list<int64_t>& representation_start_time = key_value.second;
642 // If there are no entries in a list, then there is no way for the
643 // segment alignment status to change.
644 // Note that it can be empty because entries get deleted below.
645 if (representation_start_time.empty())
646 return;
647
648 if (expected_start_time != representation_start_time.front()) {
649 VLOG(1) << "Seeing Misaligned segments with different start_times: "
650 << expected_start_time << " vs "
651 << representation_start_time.front();
652 // Flag as false and clear the start times data, no need to keep it
653 // around.
654 segments_aligned_ = kSegmentAlignmentFalse;
655 representation_segment_start_times_.clear();
656 return;
657 }
658 }
659 segments_aligned_ = kSegmentAlignmentTrue;
660
661 for (auto& key_value : representation_segment_start_times_) {
662 std::list<int64_t>& representation_start_time = key_value.second;
663 representation_start_time.pop_front();
664 }
665}
666
667// Make sure all segements start times match for all Representations.
668// This assumes that the segments are contiguous.
669void AdaptationSet::CheckStaticSegmentAlignment() {
670 if (segments_aligned_ == kSegmentAlignmentFalse ||
671 force_set_segment_alignment_) {
672 return;
673 }
674 if (representation_segment_start_times_.empty())
675 return;
676 if (representation_segment_start_times_.size() == 1) {
677 segments_aligned_ = kSegmentAlignmentTrue;
678 return;
679 }
680
681 // This is not the most efficient implementation to compare the values
682 // because expected_time_line is compared against all other time lines, but
683 // probably the most readable.
684 const std::list<int64_t>& expected_time_line =
685 representation_segment_start_times_.begin()->second;
686
687 bool all_segment_time_line_same_length = true;
688 // Note that the first entry is skipped because it is expected_time_line.
689 RepresentationTimeline::const_iterator it =
690 representation_segment_start_times_.begin();
691 for (++it; it != representation_segment_start_times_.end(); ++it) {
692 const std::list<int64_t>& other_time_line = it->second;
693 if (expected_time_line.size() != other_time_line.size()) {
694 all_segment_time_line_same_length = false;
695 }
696
697 const std::list<int64_t>* longer_list = &other_time_line;
698 const std::list<int64_t>* shorter_list = &expected_time_line;
699 if (expected_time_line.size() > other_time_line.size()) {
700 shorter_list = &other_time_line;
701 longer_list = &expected_time_line;
702 }
703
704 if (!std::equal(shorter_list->begin(), shorter_list->end(),
705 longer_list->begin())) {
706 // Some segments are definitely unaligned.
707 segments_aligned_ = kSegmentAlignmentFalse;
708 representation_segment_start_times_.clear();
709 return;
710 }
711 }
712
713 // TODO(rkuroiwa): The right way to do this is to also check the durations.
714 // For example:
715 // (a) 3 4 5
716 // (b) 3 4 5 6
717 // could be true or false depending on the length of the third segment of (a).
718 // i.e. if length of the third segment is 2, then this is not aligned.
719 if (!all_segment_time_line_same_length) {
720 segments_aligned_ = kSegmentAlignmentUnknown;
721 return;
722 }
723
724 segments_aligned_ = kSegmentAlignmentTrue;
725}
726
727// Since all AdaptationSet cares about is the maxFrameRate, representation_id
728// is not passed to this method.
729void AdaptationSet::RecordFrameRate(int32_t frame_duration, int32_t timescale) {
730 if (frame_duration == 0) {
731 LOG(ERROR) << "Frame duration is 0 and cannot be set.";
732 return;
733 }
734 video_frame_rates_[static_cast<double>(timescale) / frame_duration] =
735 absl::StrFormat("%d/%d", timescale, frame_duration);
736}
737
738} // namespace shaka
void OnNewSegmentForRepresentation(uint32_t representation_id, int64_t start_time, int64_t duration)
virtual Representation * AddRepresentation(const MediaInfo &media_info)
virtual void AddAccessibility(const std::string &scheme, const std::string &value)
virtual void AddContentProtectionElement(const ContentProtectionElement &element)
virtual void ForceStartwithSAP(uint32_t sap_value)
const MediaInfo::ProtectedContent * protected_content() const
Return ProtectedContent.
virtual void ForceSetSegmentAlignment(bool segment_alignment)
void OnSetFrameRateForRepresentation(uint32_t representation_id, int32_t frame_duration, int32_t timescale)
virtual Representation * CopyRepresentation(const Representation &representation)
virtual void ForceSubsegmentStartswithSAP(uint32_t sap_value)
bool MatchAdaptationSet(const MediaInfo &media_info, bool content_protection_in_adaptation_set)
virtual void AddTrickPlayReference(const AdaptationSet *adaptation_set)
virtual void AddAdaptationSetSwitching(const AdaptationSet *adaptation_set)
std::optional< xml::XmlNode > GetXml()
AdaptationSet(const std::string &language, const MpdOptions &mpd_options, uint32_t *representation_counter)
void set_protected_content(const MediaInfo &media_info)
bool SwitchableAdaptationSet(const AdaptationSet &adaptation_set)
virtual void UpdateContentProtectionPssh(const std::string &drm_uuid, const std::string &pssh)
virtual void AddRole(Role role)
uint32_t id() const
AdaptationSetType specified in MPD.
Definition xml_node.h:163
bool AddAccessibilityElement(const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:353
bool AddLabelElement(const std::string &value)
Definition xml_node.cc:364
bool AddRoleElement(const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:359
bool AddEssentialProperty(const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:311
bool AddSupplementalProperty(const std::string &scheme_id_uri, const std::string &value)
Definition xml_node.cc:305
bool AddChild(XmlNode child)
Definition xml_node.cc:162
bool SetStringAttribute(const std::string &attribute_name, const std::string &attribute)
Definition xml_node.cc:205
bool SetId(uint32_t id)
Definition xml_node.cc:227
bool SetIntegerAttribute(const std::string &attribute_name, uint64_t number)
Definition xml_node.cc:212
All the methods that are virtual are virtual for mocking.
Defines Mpd Options.
Definition mpd_options.h:25