7#include <packager/mpd/base/adaptation_set.h>
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>
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>
27AdaptationSet::Role MediaInfoTextTypeToRole(
28 MediaInfo::TextInfo::TextType 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;
38 NOTIMPLEMENTED() <<
"Unknown MediaInfo TextType: " << type
39 <<
" assuming subtitle.";
40 return AdaptationSet::kRoleSubtitle;
44std::string RoleToText(AdaptationSet::Role role) {
48 case AdaptationSet::kRoleCaption:
50 case AdaptationSet::kRoleSubtitle:
52 case AdaptationSet::kRoleMain:
54 case AdaptationSet::kRoleAlternate:
56 case AdaptationSet::kRoleSupplementary:
57 return "supplementary";
58 case AdaptationSet::kRoleCommentary:
60 case AdaptationSet::kRoleDub:
62 case AdaptationSet::kRoleDescription:
64 case AdaptationSet::kRoleSign:
66 case AdaptationSet::kRoleMetadata:
68 case AdaptationSet::kRoleEnhancedAudioIntelligibility:
69 return "enhanced-audio-intelligibility";
70 case AdaptationSet::kRoleEmergency:
72 case AdaptationSet::kRoleForcedSubtitle:
73 return "forced-subtitle";
74 case AdaptationSet::kRoleEasyreader:
76 case AdaptationSet::kRoleKaraoke:
90std::string GetPictureAspectRatio(uint32_t 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;
100 const uint32_t kLargestPossibleParY = 19;
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) {
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 <<
".";
120 return absl::StrFormat(
"%d:%d", par_num, par_den);
125void AddPictureAspectRatio(
const MediaInfo::VideoInfo& video_info,
126 std::set<std::string>* picture_aspect_ratio) {
129 if (picture_aspect_ratio->size() > 1)
132 if (video_info.width() == 0 || video_info.height() == 0 ||
133 video_info.pixel_width() == 0 || video_info.pixel_height() == 0) {
138 picture_aspect_ratio->insert(
"bogus");
139 picture_aspect_ratio->insert(
"entries");
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);
154class RepresentationStateChangeListenerImpl
155 :
public RepresentationStateChangeListener {
158 RepresentationStateChangeListenerImpl(uint32_t representation_id,
159 AdaptationSet* adaptation_set)
160 : representation_id_(representation_id), adaptation_set_(adaptation_set) {
161 DCHECK(adaptation_set_);
163 ~RepresentationStateChangeListenerImpl()
override {}
166 void OnNewSegmentForRepresentation(int64_t start_time,
167 int64_t duration)
override {
168 adaptation_set_->OnNewSegmentForRepresentation(representation_id_,
169 start_time, duration);
172 void OnSetFrameRateForRepresentation(int32_t frame_duration,
173 int32_t timescale)
override {
174 adaptation_set_->OnSetFrameRateForRepresentation(representation_id_,
175 frame_duration, timescale);
179 const uint32_t representation_id_;
180 AdaptationSet*
const adaptation_set_;
182 DISALLOW_COPY_AND_ASSIGN(RepresentationStateChangeListenerImpl);
190 : representation_counter_(counter),
192 mpd_options_(mpd_options),
193 protected_content_(nullptr) {
197AdaptationSet::~AdaptationSet() {
198 delete protected_content_;
202 DCHECK(!protected_content_);
204 new MediaInfo::ProtectedContent(media_info.protected_content());
209bool ProtectedContentEq(
210 const MediaInfo::ProtectedContent& content_protection1,
211 const MediaInfo::ProtectedContent& content_protection2) {
212 return content_protection1.SerializeAsString() ==
213 content_protection2.SerializeAsString();
217 const MediaInfo& media_info,
218 bool content_protection_in_adaptation_set) {
219 if (codec_ != GetBaseCodec(media_info))
222 if (!content_protection_in_adaptation_set)
225 if (!protected_content_)
226 return !media_info.has_protected_content();
228 if (!media_info.has_protected_content())
231 return ProtectedContentEq(*protected_content_,
232 media_info.protected_content());
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());
252 return GetUUIDs(protected_content_) ==
260 const uint32_t representation_id = media_info.has_index()
262 : (*representation_counter_)++;
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)));
271 if (!new_representation->Init()) {
272 LOG(ERROR) <<
"Failed to initialize Representation.";
275 UpdateFromMediaInfo(media_info);
277 representation_map_[representation_ptr->
id()] = std::move(new_representation);
278 return representation_ptr;
285 std::unique_ptr<RepresentationStateChangeListener> listener(
286 new RepresentationStateChangeListenerImpl(representation.
id(),
this));
287 std::unique_ptr<Representation> new_representation(
290 UpdateFromMediaInfo(new_representation->GetMediaInfo());
292 representation_map_[representation_ptr->
id()] = std::move(new_representation);
293 return representation_ptr;
298 content_protection_elements_.push_back(content_protection_element);
299 RemoveDuplicateAttributes(&content_protection_elements_.back());
303 const std::string& pssh) {
304 UpdateContentProtectionPsshHelper(drm_uuid, pssh,
305 &content_protection_elements_);
309 const std::string& value) {
310 accessibilities_.push_back(Accessibility{scheme, value});
326 bool suppress_representation_width =
false;
327 bool suppress_representation_height =
false;
328 bool suppress_representation_frame_rate =
false;
330 if (id_ && !adaptation_set.
SetId(id_.value()))
334 if (!language_.empty() && language_ !=
"und" &&
340 if (video_widths_.size() == 1) {
341 suppress_representation_width =
true;
344 }
else if (video_widths_.size() > 1) {
346 *video_widths_.rbegin())) {
351 if (video_heights_.size() == 1) {
352 suppress_representation_height =
true;
355 }
else if (video_heights_.size() > 1) {
357 *video_heights_.rbegin())) {
362 if (subsegment_start_with_sap_) {
364 subsegment_start_with_sap_))
366 }
else if (start_with_sap_) {
371 if (video_frame_rates_.size() == 1) {
372 suppress_representation_frame_rate =
true;
374 "frameRate", video_frame_rates_.begin()->second)) {
377 }
else if (video_frame_rates_.size() > 1) {
379 "maxFrameRate", video_frame_rates_.rbegin()->second)) {
385 if (
IsVideo() && matrix_coefficients_ > 0 &&
387 "urn:mpeg:mpegB:cicp:MatrixCoefficients",
388 std::to_string(matrix_coefficients_))) {
393 if (
IsVideo() && color_primaries_ > 0 &&
395 "urn:mpeg:mpegB:cicp:ColourPrimaries",
396 std::to_string(color_primaries_))) {
401 if (
IsVideo() && transfer_characteristics_ > 0 &&
403 "urn:mpeg:mpegB:cicp:TransferCharacteristics",
404 std::to_string(transfer_characteristics_))) {
410 if (mpd_options_.mpd_type == MpdType::kStatic) {
411 CheckStaticSegmentAlignment();
414 if (segments_aligned_ == kSegmentAlignmentTrue) {
416 mpd_options_.dash_profile == DashProfile::kOnDemand
417 ?
"subsegmentAlignment"
418 :
"segmentAlignment",
424 if (picture_aspect_ratio_.size() == 1 &&
426 *picture_aspect_ratio_.begin())) {
430 if (!adaptation_set.AddContentProtectionElements(
431 content_protection_elements_)) {
435 std::string trick_play_reference_ids;
436 for (
const AdaptationSet* tp_adaptation_set : trick_play_references_) {
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());
443 if (!trick_play_reference_ids.empty() &&
445 "http://dashif.org/guidelines/trickmode", trick_play_reference_ids)) {
449 std::string switching_ids;
450 for (
const AdaptationSet* s_adaptation_set : switchable_adaptation_sets_) {
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());
457 if (!switching_ids.empty() &&
459 "urn:mpeg:dash:adaptation-set-switching:2016", switching_ids)) {
463 for (
const AdaptationSet::Accessibility& accessibility : accessibilities_) {
465 accessibility.value)) {
470 for (AdaptationSet::Role role : roles_) {
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)))
493 return adaptation_set;
498 segment_alignment ? kSegmentAlignmentTrue : kSegmentAlignmentFalse;
499 force_set_segment_alignment_ =
true;
504 switchable_adaptation_sets_.push_back(adaptation_set);
508 subsegment_start_with_sap_ = sap_value;
512 start_with_sap_ = sap_value;
524 if (mpd_options_.mpd_type == MpdType::kDynamic) {
525 CheckDynamicSegmentAlignment(representation_id, start_time, duration);
527 representation_segment_start_times_[representation_id].push_back(
533 int32_t frame_duration,
535 RecordFrameRate(frame_duration, timescale);
539 trick_play_references_.push_back(adaptation_set);
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());
547 return representations;
551 return content_type_ ==
"video";
554void AdaptationSet::UpdateFromMediaInfo(
const MediaInfo& media_info) {
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());
564 if (video_info.has_time_scale() && video_info.has_frame_duration())
565 RecordFrameRate(video_info.frame_duration(), video_info.time_scale());
567 AddPictureAspectRatio(video_info, &picture_aspect_ratio_);
572 if (media_info.has_index()) {
573 if (index_.has_value()) {
574 index_ = std::min(index_.value(), media_info.index());
576 index_ = media_info.index();
580 if (media_info.has_dash_label())
581 label_ = media_info.dash_label();
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";
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()));
621void AdaptationSet::CheckDynamicSegmentAlignment(uint32_t representation_id,
624 if (segments_aligned_ == kSegmentAlignmentFalse ||
625 force_set_segment_alignment_) {
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);
634 if (representation_segment_start_times_.size() != representation_map_.size())
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;
645 if (representation_start_time.empty())
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();
654 segments_aligned_ = kSegmentAlignmentFalse;
655 representation_segment_start_times_.clear();
659 segments_aligned_ = kSegmentAlignmentTrue;
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();
669void AdaptationSet::CheckStaticSegmentAlignment() {
670 if (segments_aligned_ == kSegmentAlignmentFalse ||
671 force_set_segment_alignment_) {
674 if (representation_segment_start_times_.empty())
676 if (representation_segment_start_times_.size() == 1) {
677 segments_aligned_ = kSegmentAlignmentTrue;
684 const std::list<int64_t>& expected_time_line =
685 representation_segment_start_times_.begin()->second;
687 bool all_segment_time_line_same_length =
true;
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;
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;
704 if (!std::equal(shorter_list->begin(), shorter_list->end(),
705 longer_list->begin())) {
707 segments_aligned_ = kSegmentAlignmentFalse;
708 representation_segment_start_times_.clear();
719 if (!all_segment_time_line_same_length) {
720 segments_aligned_ = kSegmentAlignmentUnknown;
724 segments_aligned_ = kSegmentAlignmentTrue;
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.";
734 video_frame_rates_[
static_cast<double>(timescale) / frame_duration] =
735 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.