7#include <packager/mpd/base/adaptation_set.h>
20#include <absl/log/check.h>
21#include <absl/log/log.h>
22#include <absl/strings/str_format.h>
24#include <packager/macros/classes.h>
25#include <packager/macros/logging.h>
26#include <packager/mpd/base/media_info.pb.h>
27#include <packager/mpd/base/mpd_options.h>
28#include <packager/mpd/base/mpd_utils.h>
29#include <packager/mpd/base/representation.h>
30#include <packager/mpd/base/xml/xml_node.h>
35AdaptationSet::Role MediaInfoTextTypeToRole(
36 MediaInfo::TextInfo::TextType type) {
38 case MediaInfo::TextInfo::UNKNOWN:
39 LOG(WARNING) <<
"Unknown text type, assuming subtitle.";
40 return AdaptationSet::kRoleSubtitle;
41 case MediaInfo::TextInfo::CAPTION:
42 return AdaptationSet::kRoleCaption;
43 case MediaInfo::TextInfo::SUBTITLE:
44 return AdaptationSet::kRoleSubtitle;
46 NOTIMPLEMENTED() <<
"Unknown MediaInfo TextType: " << type
47 <<
" assuming subtitle.";
48 return AdaptationSet::kRoleSubtitle;
52std::string RoleToText(AdaptationSet::Role role) {
56 case AdaptationSet::kRoleCaption:
58 case AdaptationSet::kRoleSubtitle:
60 case AdaptationSet::kRoleMain:
62 case AdaptationSet::kRoleAlternate:
64 case AdaptationSet::kRoleSupplementary:
65 return "supplementary";
66 case AdaptationSet::kRoleCommentary:
68 case AdaptationSet::kRoleDub:
70 case AdaptationSet::kRoleDescription:
72 case AdaptationSet::kRoleSign:
74 case AdaptationSet::kRoleMetadata:
76 case AdaptationSet::kRoleEnhancedAudioIntelligibility:
77 return "enhanced-audio-intelligibility";
78 case AdaptationSet::kRoleEmergency:
80 case AdaptationSet::kRoleForcedSubtitle:
81 return "forced-subtitle";
82 case AdaptationSet::kRoleEasyreader:
84 case AdaptationSet::kRoleKaraoke:
98std::string GetPictureAspectRatio(uint32_t width,
100 uint32_t pixel_width,
101 uint32_t pixel_height) {
102 const uint32_t scaled_width = pixel_width * width;
103 const uint32_t scaled_height = pixel_height * height;
104 const double par =
static_cast<double>(scaled_width) / scaled_height;
108 const uint32_t kLargestPossibleParY = 19;
110 uint32_t par_num = 0;
111 uint32_t par_den = 0;
112 double min_error = 1.0;
113 for (uint32_t den = 1; den <= kLargestPossibleParY; ++den) {
114 uint32_t num = par * den + 0.5;
115 double error = fabs(par -
static_cast<double>(num) / den);
116 if (error < min_error) {
124 VLOG(2) <<
"width*pix_width : height*pixel_height (" << scaled_width <<
":"
125 << scaled_height <<
") reduced to " << par_num <<
":" << par_den
126 <<
" with error " << min_error <<
".";
128 return absl::StrFormat(
"%d:%d", par_num, par_den);
133void AddPictureAspectRatio(
const MediaInfo::VideoInfo& video_info,
134 std::set<std::string>* picture_aspect_ratio) {
137 if (picture_aspect_ratio->size() > 1)
140 if (video_info.width() == 0 || video_info.height() == 0 ||
141 video_info.pixel_width() == 0 || video_info.pixel_height() == 0) {
146 picture_aspect_ratio->insert(
"bogus");
147 picture_aspect_ratio->insert(
"entries");
151 const std::string par = GetPictureAspectRatio(
152 video_info.width(), video_info.height(), video_info.pixel_width(),
153 video_info.pixel_height());
154 DVLOG(1) <<
"Setting par as: " << par
155 <<
" for video with width: " << video_info.width()
156 <<
" height: " << video_info.height()
157 <<
" pixel_width: " << video_info.pixel_width() <<
" pixel_height; "
158 << video_info.pixel_height();
159 picture_aspect_ratio->insert(par);
162class RepresentationStateChangeListenerImpl
163 :
public RepresentationStateChangeListener {
166 RepresentationStateChangeListenerImpl(uint32_t representation_id,
167 AdaptationSet* adaptation_set)
168 : representation_id_(representation_id), adaptation_set_(adaptation_set) {
169 DCHECK(adaptation_set_);
171 ~RepresentationStateChangeListenerImpl()
override {}
174 void OnNewSegmentForRepresentation(int64_t start_time,
175 int64_t duration)
override {
176 adaptation_set_->OnNewSegmentForRepresentation(representation_id_,
177 start_time, duration);
180 void OnSetFrameRateForRepresentation(int32_t frame_duration,
181 int32_t timescale)
override {
182 adaptation_set_->OnSetFrameRateForRepresentation(representation_id_,
183 frame_duration, timescale);
187 const uint32_t representation_id_;
188 AdaptationSet*
const adaptation_set_;
190 DISALLOW_COPY_AND_ASSIGN(RepresentationStateChangeListenerImpl);
198 : representation_counter_(counter),
200 mpd_options_(mpd_options),
201 protected_content_(nullptr) {
205AdaptationSet::~AdaptationSet() {
206 delete protected_content_;
210 DCHECK(!protected_content_);
212 new MediaInfo::ProtectedContent(media_info.protected_content());
217bool ProtectedContentEq(
218 const MediaInfo::ProtectedContent& content_protection1,
219 const MediaInfo::ProtectedContent& content_protection2) {
220 return content_protection1.SerializeAsString() ==
221 content_protection2.SerializeAsString();
225 const MediaInfo& media_info,
226 bool content_protection_in_adaptation_set) {
227 if (codec_ != GetBaseCodec(media_info))
230 if (!content_protection_in_adaptation_set)
233 if (!protected_content_)
234 return !media_info.has_protected_content();
236 if (!media_info.has_protected_content())
239 return ProtectedContentEq(*protected_content_,
240 media_info.protected_content());
243std::set<std::string> GetUUIDs(
244 const MediaInfo::ProtectedContent* protected_content) {
245 std::set<std::string> uuids;
246 for (
const auto& entry : protected_content->content_protection_entry())
247 uuids.insert(entry.uuid());
260 return GetUUIDs(protected_content_) ==
268 const uint32_t representation_id = media_info.has_index()
270 : (*representation_counter_)++;
274 std::unique_ptr<RepresentationStateChangeListener> listener(
275 new RepresentationStateChangeListenerImpl(representation_id,
this));
276 std::unique_ptr<Representation> new_representation(
new Representation(
277 media_info, mpd_options_, representation_id, std::move(listener)));
279 if (!new_representation->Init()) {
280 LOG(ERROR) <<
"Failed to initialize Representation.";
283 UpdateFromMediaInfo(media_info);
285 representation_map_[representation_ptr->
id()] = std::move(new_representation);
286 return representation_ptr;
293 std::unique_ptr<RepresentationStateChangeListener> listener(
294 new RepresentationStateChangeListenerImpl(representation.
id(),
this));
295 std::unique_ptr<Representation> new_representation(
298 UpdateFromMediaInfo(new_representation->GetMediaInfo());
300 representation_map_[representation_ptr->
id()] = std::move(new_representation);
301 return representation_ptr;
306 content_protection_elements_.push_back(content_protection_element);
307 RemoveDuplicateAttributes(&content_protection_elements_.back());
311 const std::string& pssh) {
312 UpdateContentProtectionPsshHelper(drm_uuid, pssh,
313 &content_protection_elements_);
317 const std::string& value) {
318 accessibilities_.push_back(Accessibility{scheme, value});
334 bool suppress_representation_width =
false;
335 bool suppress_representation_height =
false;
336 bool suppress_representation_frame_rate =
false;
338 if (id_ && !adaptation_set.
SetId(id_.value()))
342 if (!language_.empty() && language_ !=
"und" &&
348 if (video_widths_.size() == 1) {
349 suppress_representation_width =
true;
352 }
else if (video_widths_.size() > 1) {
354 *video_widths_.rbegin())) {
359 if (video_heights_.size() == 1) {
360 suppress_representation_height =
true;
363 }
else if (video_heights_.size() > 1) {
365 *video_heights_.rbegin())) {
370 if (subsegment_start_with_sap_) {
372 subsegment_start_with_sap_))
374 }
else if (start_with_sap_) {
379 if (video_frame_rates_.size() == 1) {
380 suppress_representation_frame_rate =
true;
382 "frameRate", video_frame_rates_.begin()->second)) {
385 }
else if (video_frame_rates_.size() > 1) {
387 "maxFrameRate", video_frame_rates_.rbegin()->second)) {
393 if (
IsVideo() && matrix_coefficients_ > 0 &&
395 "urn:mpeg:mpegB:cicp:MatrixCoefficients",
396 std::to_string(matrix_coefficients_))) {
401 if (
IsVideo() && color_primaries_ > 0 &&
403 "urn:mpeg:mpegB:cicp:ColourPrimaries",
404 std::to_string(color_primaries_))) {
409 if (
IsVideo() && transfer_characteristics_ > 0 &&
411 "urn:mpeg:mpegB:cicp:TransferCharacteristics",
412 std::to_string(transfer_characteristics_))) {
418 if (mpd_options_.mpd_type == MpdType::kStatic) {
419 CheckStaticSegmentAlignment();
422 if (segments_aligned_ == kSegmentAlignmentTrue) {
424 mpd_options_.dash_profile == DashProfile::kOnDemand
425 ?
"subsegmentAlignment"
426 :
"segmentAlignment",
432 if (picture_aspect_ratio_.size() == 1 &&
434 *picture_aspect_ratio_.begin())) {
438 if (!adaptation_set.AddContentProtectionElements(
439 content_protection_elements_)) {
443 std::string trick_play_reference_ids;
444 for (
const AdaptationSet* tp_adaptation_set : trick_play_references_) {
446 if (!trick_play_reference_ids.empty())
447 trick_play_reference_ids +=
' ';
448 CHECK(tp_adaptation_set->has_id());
449 trick_play_reference_ids += std::to_string(tp_adaptation_set->id());
451 if (!trick_play_reference_ids.empty() &&
453 "http://dashif.org/guidelines/trickmode", trick_play_reference_ids)) {
457 std::string switching_ids;
458 for (
const AdaptationSet* s_adaptation_set : switchable_adaptation_sets_) {
460 if (!switching_ids.empty())
461 switching_ids +=
',';
462 CHECK(s_adaptation_set->has_id());
463 switching_ids += std::to_string(s_adaptation_set->id());
465 if (!switching_ids.empty() &&
467 "urn:mpeg:dash:adaptation-set-switching:2016", switching_ids)) {
471 for (
const AdaptationSet::Accessibility& accessibility : accessibilities_) {
473 accessibility.value)) {
478 for (AdaptationSet::Role role : roles_) {
488 for (
const auto& representation_pair : representation_map_) {
489 const auto& representation = representation_pair.second;
490 if (suppress_representation_width)
491 representation->SuppressOnce(Representation::kSuppressWidth);
492 if (suppress_representation_height)
493 representation->SuppressOnce(Representation::kSuppressHeight);
494 if (suppress_representation_frame_rate)
495 representation->SuppressOnce(Representation::kSuppressFrameRate);
496 auto child = representation->GetXml();
497 if (!child || !adaptation_set.
AddChild(std::move(*child)))
501 return adaptation_set;
506 segment_alignment ? kSegmentAlignmentTrue : kSegmentAlignmentFalse;
507 force_set_segment_alignment_ =
true;
512 switchable_adaptation_sets_.push_back(adaptation_set);
516 subsegment_start_with_sap_ = sap_value;
520 start_with_sap_ = sap_value;
532 if (mpd_options_.mpd_type == MpdType::kDynamic) {
533 CheckDynamicSegmentAlignment(representation_id, start_time, duration);
535 representation_segment_start_times_[representation_id].push_back(
541 int32_t frame_duration,
543 RecordFrameRate(frame_duration, timescale);
547 trick_play_references_.push_back(adaptation_set);
550const std::list<Representation*> AdaptationSet::GetRepresentations()
const {
551 std::list<Representation*> representations;
552 for (
const auto& representation_pair : representation_map_) {
553 representations.push_back(representation_pair.second.get());
555 return representations;
559 return content_type_ ==
"video";
562void AdaptationSet::UpdateFromMediaInfo(
const MediaInfo& media_info) {
565 if (media_info.has_video_info()) {
566 const MediaInfo::VideoInfo& video_info = media_info.video_info();
567 DCHECK(video_info.has_width());
568 DCHECK(video_info.has_height());
569 video_widths_.insert(video_info.width());
570 video_heights_.insert(video_info.height());
572 if (video_info.has_time_scale() && video_info.has_frame_duration())
573 RecordFrameRate(video_info.frame_duration(), video_info.time_scale());
575 AddPictureAspectRatio(video_info, &picture_aspect_ratio_);
580 if (media_info.has_index()) {
581 if (index_.has_value()) {
582 index_ = std::min(index_.value(), media_info.index());
584 index_ = media_info.index();
588 if (media_info.has_dash_label())
589 label_ = media_info.dash_label();
591 if (media_info.has_video_info()) {
592 content_type_ =
"video";
593 }
else if (media_info.has_audio_info()) {
594 content_type_ =
"audio";
595 }
else if (media_info.has_text_info()) {
596 content_type_ =
"text";
598 if (media_info.text_info().has_type() &&
599 (media_info.text_info().type() != MediaInfo::TextInfo::UNKNOWN)) {
600 roles_.insert(MediaInfoTextTypeToRole(media_info.text_info().type()));
629void AdaptationSet::CheckDynamicSegmentAlignment(uint32_t representation_id,
632 if (segments_aligned_ == kSegmentAlignmentFalse ||
633 force_set_segment_alignment_) {
637 std::list<int64_t>& current_representation_start_times =
638 representation_segment_start_times_[representation_id];
639 current_representation_start_times.push_back(start_time);
642 if (representation_segment_start_times_.size() != representation_map_.size())
645 DCHECK(!current_representation_start_times.empty());
646 const int64_t expected_start_time =
647 current_representation_start_times.front();
648 for (
const auto& key_value : representation_segment_start_times_) {
649 const std::list<int64_t>& representation_start_time = key_value.second;
653 if (representation_start_time.empty())
656 if (expected_start_time != representation_start_time.front()) {
657 VLOG(1) <<
"Seeing Misaligned segments with different start_times: "
658 << expected_start_time <<
" vs "
659 << representation_start_time.front();
662 segments_aligned_ = kSegmentAlignmentFalse;
663 representation_segment_start_times_.clear();
667 segments_aligned_ = kSegmentAlignmentTrue;
669 for (
auto& key_value : representation_segment_start_times_) {
670 std::list<int64_t>& representation_start_time = key_value.second;
671 representation_start_time.pop_front();
677void AdaptationSet::CheckStaticSegmentAlignment() {
678 if (segments_aligned_ == kSegmentAlignmentFalse ||
679 force_set_segment_alignment_) {
682 if (representation_segment_start_times_.empty())
684 if (representation_segment_start_times_.size() == 1) {
685 segments_aligned_ = kSegmentAlignmentTrue;
692 const std::list<int64_t>& expected_time_line =
693 representation_segment_start_times_.begin()->second;
695 bool all_segment_time_line_same_length =
true;
697 RepresentationTimeline::const_iterator it =
698 representation_segment_start_times_.begin();
699 for (++it; it != representation_segment_start_times_.end(); ++it) {
700 const std::list<int64_t>& other_time_line = it->second;
701 if (expected_time_line.size() != other_time_line.size()) {
702 all_segment_time_line_same_length =
false;
705 const std::list<int64_t>* longer_list = &other_time_line;
706 const std::list<int64_t>* shorter_list = &expected_time_line;
707 if (expected_time_line.size() > other_time_line.size()) {
708 shorter_list = &other_time_line;
709 longer_list = &expected_time_line;
712 if (!std::equal(shorter_list->begin(), shorter_list->end(),
713 longer_list->begin())) {
715 segments_aligned_ = kSegmentAlignmentFalse;
716 representation_segment_start_times_.clear();
727 if (!all_segment_time_line_same_length) {
728 segments_aligned_ = kSegmentAlignmentUnknown;
732 segments_aligned_ = kSegmentAlignmentTrue;
737void AdaptationSet::RecordFrameRate(int32_t frame_duration, int32_t timescale) {
738 if (frame_duration == 0) {
739 LOG(ERROR) <<
"Frame duration is 0 and cannot be set.";
742 video_frame_rates_[
static_cast<double>(timescale) / frame_duration] =
743 absl::StrFormat(
"%d/%d", timescale, frame_duration);
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)
AdaptationSetType specified in MPD.
bool AddAccessibilityElement(const std::string &scheme_id_uri, const std::string &value)
bool AddLabelElement(const std::string &value)
bool AddRoleElement(const std::string &scheme_id_uri, const std::string &value)
bool AddEssentialProperty(const std::string &scheme_id_uri, const std::string &value)
bool AddSupplementalProperty(const std::string &scheme_id_uri, const std::string &value)
bool AddChild(XmlNode child)
bool SetStringAttribute(const std::string &attribute_name, const std::string &attribute)
bool SetIntegerAttribute(const std::string &attribute_name, uint64_t number)
All the methods that are virtual are virtual for mocking.