Move AdaptationSet, Representation out of mpd_builder.h
Change-Id: I61fffb4d956f189b44c7537ddcf183bbf4129840
This commit is contained in:
parent
dedbd8b724
commit
86d960bea6
|
@ -0,0 +1,548 @@
|
||||||
|
// Copyright 2017 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
#include "packager/mpd/base/adaptation_set.h"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include "packager/base/logging.h"
|
||||||
|
#include "packager/base/strings/string_number_conversions.h"
|
||||||
|
#include "packager/media/base/language_utils.h"
|
||||||
|
#include "packager/mpd/base/media_info.pb.h"
|
||||||
|
#include "packager/mpd/base/mpd_utils.h"
|
||||||
|
#include "packager/mpd/base/representation.h"
|
||||||
|
#include "packager/mpd/base/xml/xml_node.h"
|
||||||
|
|
||||||
|
namespace shaka {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
AdaptationSet::Role MediaInfoTextTypeToRole(
|
||||||
|
MediaInfo::TextInfo::TextType type) {
|
||||||
|
switch (type) {
|
||||||
|
case MediaInfo::TextInfo::UNKNOWN:
|
||||||
|
LOG(WARNING) << "Unknown text type, assuming subtitle.";
|
||||||
|
return AdaptationSet::kRoleSubtitle;
|
||||||
|
case MediaInfo::TextInfo::CAPTION:
|
||||||
|
return AdaptationSet::kRoleCaption;
|
||||||
|
case MediaInfo::TextInfo::SUBTITLE:
|
||||||
|
return AdaptationSet::kRoleSubtitle;
|
||||||
|
default:
|
||||||
|
NOTREACHED() << "Unknown MediaInfo TextType: " << type
|
||||||
|
<< " assuming subtitle.";
|
||||||
|
return AdaptationSet::kRoleSubtitle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string RoleToText(AdaptationSet::Role role) {
|
||||||
|
// Using switch so that the compiler can detect whether there is a case that's
|
||||||
|
// not being handled.
|
||||||
|
switch (role) {
|
||||||
|
case AdaptationSet::kRoleCaption:
|
||||||
|
return "caption";
|
||||||
|
case AdaptationSet::kRoleSubtitle:
|
||||||
|
return "subtitle";
|
||||||
|
case AdaptationSet::kRoleMain:
|
||||||
|
return "main";
|
||||||
|
case AdaptationSet::kRoleAlternate:
|
||||||
|
return "alternate";
|
||||||
|
case AdaptationSet::kRoleSupplementary:
|
||||||
|
return "supplementary";
|
||||||
|
case AdaptationSet::kRoleCommentary:
|
||||||
|
return "commentary";
|
||||||
|
case AdaptationSet::kRoleDub:
|
||||||
|
return "dub";
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
NOTREACHED();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the picture aspect ratio string e.g. "16:9", "4:3".
|
||||||
|
// "Reducing the quotient to minimal form" does not work well in practice as
|
||||||
|
// there may be some rounding performed in the input, e.g. the resolution of
|
||||||
|
// 480p is 854:480 for 16:9 aspect ratio, can only be reduced to 427:240.
|
||||||
|
// The algorithm finds out the pair of integers, num and den, where num / den is
|
||||||
|
// the closest ratio to scaled_width / scaled_height, by looping den through
|
||||||
|
// common values.
|
||||||
|
std::string GetPictureAspectRatio(uint32_t width,
|
||||||
|
uint32_t height,
|
||||||
|
uint32_t pixel_width,
|
||||||
|
uint32_t pixel_height) {
|
||||||
|
const uint32_t scaled_width = pixel_width * width;
|
||||||
|
const uint32_t scaled_height = pixel_height * height;
|
||||||
|
const double par = static_cast<double>(scaled_width) / scaled_height;
|
||||||
|
|
||||||
|
// Typical aspect ratios have par_y less than or equal to 19:
|
||||||
|
// https://en.wikipedia.org/wiki/List_of_common_resolutions
|
||||||
|
const uint32_t kLargestPossibleParY = 19;
|
||||||
|
|
||||||
|
uint32_t par_num = 0;
|
||||||
|
uint32_t par_den = 0;
|
||||||
|
double min_error = 1.0;
|
||||||
|
for (uint32_t den = 1; den <= kLargestPossibleParY; ++den) {
|
||||||
|
uint32_t num = par * den + 0.5;
|
||||||
|
double error = fabs(par - static_cast<double>(num) / den);
|
||||||
|
if (error < min_error) {
|
||||||
|
min_error = error;
|
||||||
|
par_num = num;
|
||||||
|
par_den = den;
|
||||||
|
if (error == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VLOG(2) << "width*pix_width : height*pixel_height (" << scaled_width << ":"
|
||||||
|
<< scaled_height << ") reduced to " << par_num << ":" << par_den
|
||||||
|
<< " with error " << min_error << ".";
|
||||||
|
|
||||||
|
return base::IntToString(par_num) + ":" + base::IntToString(par_den);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adds an entry to picture_aspect_ratio if the size of picture_aspect_ratio is
|
||||||
|
// less than 2 and video_info has both pixel width and pixel height.
|
||||||
|
void AddPictureAspectRatio(const MediaInfo::VideoInfo& video_info,
|
||||||
|
std::set<std::string>* picture_aspect_ratio) {
|
||||||
|
// If there are more than one entries in picture_aspect_ratio, the @par
|
||||||
|
// attribute cannot be set, so skip.
|
||||||
|
if (picture_aspect_ratio->size() > 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (video_info.width() == 0 || video_info.height() == 0 ||
|
||||||
|
video_info.pixel_width() == 0 || video_info.pixel_height() == 0) {
|
||||||
|
// If there is even one Representation without a @sar attribute, @par cannot
|
||||||
|
// be calculated.
|
||||||
|
// Just populate the set with at least 2 bogus strings so that further call
|
||||||
|
// to this function will bail out immediately.
|
||||||
|
picture_aspect_ratio->insert("bogus");
|
||||||
|
picture_aspect_ratio->insert("entries");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string par = GetPictureAspectRatio(
|
||||||
|
video_info.width(), video_info.height(), video_info.pixel_width(),
|
||||||
|
video_info.pixel_height());
|
||||||
|
DVLOG(1) << "Setting par as: " << par
|
||||||
|
<< " for video with width: " << video_info.width()
|
||||||
|
<< " height: " << video_info.height()
|
||||||
|
<< " pixel_width: " << video_info.pixel_width() << " pixel_height; "
|
||||||
|
<< video_info.pixel_height();
|
||||||
|
picture_aspect_ratio->insert(par);
|
||||||
|
}
|
||||||
|
|
||||||
|
class RepresentationStateChangeListenerImpl
|
||||||
|
: public RepresentationStateChangeListener {
|
||||||
|
public:
|
||||||
|
// |adaptation_set| is not owned by this class.
|
||||||
|
RepresentationStateChangeListenerImpl(uint32_t representation_id,
|
||||||
|
AdaptationSet* adaptation_set)
|
||||||
|
: representation_id_(representation_id), adaptation_set_(adaptation_set) {
|
||||||
|
DCHECK(adaptation_set_);
|
||||||
|
}
|
||||||
|
~RepresentationStateChangeListenerImpl() override {}
|
||||||
|
|
||||||
|
// RepresentationStateChangeListener implementation.
|
||||||
|
void OnNewSegmentForRepresentation(uint64_t start_time,
|
||||||
|
uint64_t duration) override {
|
||||||
|
adaptation_set_->OnNewSegmentForRepresentation(representation_id_,
|
||||||
|
start_time, duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnSetFrameRateForRepresentation(uint32_t frame_duration,
|
||||||
|
uint32_t timescale) override {
|
||||||
|
adaptation_set_->OnSetFrameRateForRepresentation(representation_id_,
|
||||||
|
frame_duration, timescale);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const uint32_t representation_id_;
|
||||||
|
AdaptationSet* const adaptation_set_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(RepresentationStateChangeListenerImpl);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
AdaptationSet::AdaptationSet(uint32_t adaptation_set_id,
|
||||||
|
const std::string& lang,
|
||||||
|
const MpdOptions& mpd_options,
|
||||||
|
base::AtomicSequenceNumber* counter)
|
||||||
|
: representation_counter_(counter),
|
||||||
|
id_(adaptation_set_id),
|
||||||
|
lang_(lang),
|
||||||
|
mpd_options_(mpd_options),
|
||||||
|
segments_aligned_(kSegmentAlignmentUnknown),
|
||||||
|
force_set_segment_alignment_(false) {
|
||||||
|
DCHECK(counter);
|
||||||
|
}
|
||||||
|
|
||||||
|
AdaptationSet::~AdaptationSet() {}
|
||||||
|
|
||||||
|
Representation* AdaptationSet::AddRepresentation(const MediaInfo& media_info) {
|
||||||
|
const uint32_t representation_id = representation_counter_->GetNext();
|
||||||
|
// Note that AdaptationSet outlive Representation, so this object
|
||||||
|
// will die before AdaptationSet.
|
||||||
|
std::unique_ptr<RepresentationStateChangeListener> listener(
|
||||||
|
new RepresentationStateChangeListenerImpl(representation_id, this));
|
||||||
|
std::unique_ptr<Representation> representation(new Representation(
|
||||||
|
media_info, mpd_options_, representation_id, std::move(listener)));
|
||||||
|
|
||||||
|
if (!representation->Init()) {
|
||||||
|
LOG(ERROR) << "Failed to initialize Representation.";
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// For videos, record the width, height, and the frame rate to calculate the
|
||||||
|
// max {width,height,framerate} required for DASH IOP.
|
||||||
|
if (media_info.has_video_info()) {
|
||||||
|
const MediaInfo::VideoInfo& video_info = media_info.video_info();
|
||||||
|
DCHECK(video_info.has_width());
|
||||||
|
DCHECK(video_info.has_height());
|
||||||
|
video_widths_.insert(video_info.width());
|
||||||
|
video_heights_.insert(video_info.height());
|
||||||
|
|
||||||
|
if (video_info.has_time_scale() && video_info.has_frame_duration())
|
||||||
|
RecordFrameRate(video_info.frame_duration(), video_info.time_scale());
|
||||||
|
|
||||||
|
AddPictureAspectRatio(video_info, &picture_aspect_ratio_);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (media_info.has_video_info()) {
|
||||||
|
content_type_ = "video";
|
||||||
|
} else if (media_info.has_audio_info()) {
|
||||||
|
content_type_ = "audio";
|
||||||
|
} else if (media_info.has_text_info()) {
|
||||||
|
content_type_ = "text";
|
||||||
|
|
||||||
|
if (media_info.text_info().has_type() &&
|
||||||
|
(media_info.text_info().type() != MediaInfo::TextInfo::UNKNOWN)) {
|
||||||
|
roles_.insert(MediaInfoTextTypeToRole(media_info.text_info().type()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
representations_.push_back(std::move(representation));
|
||||||
|
return representations_.back().get();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdaptationSet::AddContentProtectionElement(
|
||||||
|
const ContentProtectionElement& content_protection_element) {
|
||||||
|
content_protection_elements_.push_back(content_protection_element);
|
||||||
|
RemoveDuplicateAttributes(&content_protection_elements_.back());
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdaptationSet::UpdateContentProtectionPssh(const std::string& drm_uuid,
|
||||||
|
const std::string& pssh) {
|
||||||
|
UpdateContentProtectionPsshHelper(drm_uuid, pssh,
|
||||||
|
&content_protection_elements_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdaptationSet::AddRole(Role role) {
|
||||||
|
roles_.insert(role);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a copy of <AdaptationSet> xml element, iterate thru all the
|
||||||
|
// <Representation> (child) elements and add them to the copy.
|
||||||
|
// Set all the attributes first and then add the children elements so that flags
|
||||||
|
// can be passed to Representation to avoid setting redundant attributes. For
|
||||||
|
// example, if AdaptationSet@width is set, then Representation@width is
|
||||||
|
// redundant and should not be set.
|
||||||
|
xml::scoped_xml_ptr<xmlNode> AdaptationSet::GetXml() {
|
||||||
|
xml::AdaptationSetXmlNode adaptation_set;
|
||||||
|
|
||||||
|
bool suppress_representation_width = false;
|
||||||
|
bool suppress_representation_height = false;
|
||||||
|
bool suppress_representation_frame_rate = false;
|
||||||
|
|
||||||
|
adaptation_set.SetId(id_);
|
||||||
|
adaptation_set.SetStringAttribute("contentType", content_type_);
|
||||||
|
if (!lang_.empty() && lang_ != "und") {
|
||||||
|
adaptation_set.SetStringAttribute("lang", LanguageToShortestForm(lang_));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note that std::{set,map} are ordered, so the last element is the max value.
|
||||||
|
if (video_widths_.size() == 1) {
|
||||||
|
suppress_representation_width = true;
|
||||||
|
adaptation_set.SetIntegerAttribute("width", *video_widths_.begin());
|
||||||
|
} else if (video_widths_.size() > 1) {
|
||||||
|
adaptation_set.SetIntegerAttribute("maxWidth", *video_widths_.rbegin());
|
||||||
|
}
|
||||||
|
if (video_heights_.size() == 1) {
|
||||||
|
suppress_representation_height = true;
|
||||||
|
adaptation_set.SetIntegerAttribute("height", *video_heights_.begin());
|
||||||
|
} else if (video_heights_.size() > 1) {
|
||||||
|
adaptation_set.SetIntegerAttribute("maxHeight", *video_heights_.rbegin());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (video_frame_rates_.size() == 1) {
|
||||||
|
suppress_representation_frame_rate = true;
|
||||||
|
adaptation_set.SetStringAttribute("frameRate",
|
||||||
|
video_frame_rates_.begin()->second);
|
||||||
|
} else if (video_frame_rates_.size() > 1) {
|
||||||
|
adaptation_set.SetStringAttribute("maxFrameRate",
|
||||||
|
video_frame_rates_.rbegin()->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: must be checked before checking segments_aligned_ (below). So that
|
||||||
|
// segments_aligned_ is set before checking below.
|
||||||
|
if (mpd_options_.dash_profile == DashProfile::kOnDemand) {
|
||||||
|
CheckVodSegmentAlignment();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (segments_aligned_ == kSegmentAlignmentTrue) {
|
||||||
|
adaptation_set.SetStringAttribute(
|
||||||
|
mpd_options_.dash_profile == DashProfile::kOnDemand
|
||||||
|
? "subsegmentAlignment"
|
||||||
|
: "segmentAlignment",
|
||||||
|
"true");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (picture_aspect_ratio_.size() == 1)
|
||||||
|
adaptation_set.SetStringAttribute("par", *picture_aspect_ratio_.begin());
|
||||||
|
|
||||||
|
if (!adaptation_set.AddContentProtectionElements(
|
||||||
|
content_protection_elements_)) {
|
||||||
|
return xml::scoped_xml_ptr<xmlNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!trick_play_reference_ids_.empty()) {
|
||||||
|
std::string id_string;
|
||||||
|
for (uint32_t id : trick_play_reference_ids_) {
|
||||||
|
id_string += std::to_string(id) + ",";
|
||||||
|
}
|
||||||
|
DCHECK(!id_string.empty());
|
||||||
|
id_string.resize(id_string.size() - 1);
|
||||||
|
adaptation_set.AddEssentialProperty(
|
||||||
|
"http://dashif.org/guidelines/trickmode", id_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string switching_ids;
|
||||||
|
for (uint32_t id : adaptation_set_switching_ids_) {
|
||||||
|
if (!switching_ids.empty())
|
||||||
|
switching_ids += ',';
|
||||||
|
switching_ids += base::UintToString(id);
|
||||||
|
}
|
||||||
|
if (!switching_ids.empty()) {
|
||||||
|
adaptation_set.AddSupplementalProperty(
|
||||||
|
"urn:mpeg:dash:adaptation-set-switching:2016", switching_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (AdaptationSet::Role role : roles_)
|
||||||
|
adaptation_set.AddRoleElement("urn:mpeg:dash:role:2011", RoleToText(role));
|
||||||
|
|
||||||
|
for (const std::unique_ptr<Representation>& representation :
|
||||||
|
representations_) {
|
||||||
|
if (suppress_representation_width)
|
||||||
|
representation->SuppressOnce(Representation::kSuppressWidth);
|
||||||
|
if (suppress_representation_height)
|
||||||
|
representation->SuppressOnce(Representation::kSuppressHeight);
|
||||||
|
if (suppress_representation_frame_rate)
|
||||||
|
representation->SuppressOnce(Representation::kSuppressFrameRate);
|
||||||
|
xml::scoped_xml_ptr<xmlNode> child(representation->GetXml());
|
||||||
|
if (!child || !adaptation_set.AddChild(std::move(child)))
|
||||||
|
return xml::scoped_xml_ptr<xmlNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return adaptation_set.PassScopedPtr();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdaptationSet::ForceSetSegmentAlignment(bool segment_alignment) {
|
||||||
|
segments_aligned_ =
|
||||||
|
segment_alignment ? kSegmentAlignmentTrue : kSegmentAlignmentFalse;
|
||||||
|
force_set_segment_alignment_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdaptationSet::AddAdaptationSetSwitching(uint32_t adaptation_set_id) {
|
||||||
|
adaptation_set_switching_ids_.push_back(adaptation_set_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check segmentAlignment for Live here. Storing all start_time and duration
|
||||||
|
// will out-of-memory because there's no way of knowing when it will end.
|
||||||
|
// VOD subsegmentAlignment check is *not* done here because it is possible
|
||||||
|
// that some Representations might not have been added yet (e.g. a thread is
|
||||||
|
// assigned per muxer so one might run faster than others).
|
||||||
|
// To be clear, for Live, all Representations should be added before a
|
||||||
|
// segment is added.
|
||||||
|
void AdaptationSet::OnNewSegmentForRepresentation(uint32_t representation_id,
|
||||||
|
uint64_t start_time,
|
||||||
|
uint64_t duration) {
|
||||||
|
if (mpd_options_.dash_profile == DashProfile::kLive) {
|
||||||
|
CheckLiveSegmentAlignment(representation_id, start_time, duration);
|
||||||
|
} else {
|
||||||
|
representation_segment_start_times_[representation_id].push_back(
|
||||||
|
start_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdaptationSet::OnSetFrameRateForRepresentation(uint32_t representation_id,
|
||||||
|
uint32_t frame_duration,
|
||||||
|
uint32_t timescale) {
|
||||||
|
RecordFrameRate(frame_duration, timescale);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AdaptationSet::AddTrickPlayReferenceId(uint32_t id) {
|
||||||
|
trick_play_reference_ids_.insert(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AdaptationSet::GetEarliestTimestamp(double* timestamp_seconds) {
|
||||||
|
DCHECK(timestamp_seconds);
|
||||||
|
|
||||||
|
double earliest_timestamp(-1);
|
||||||
|
for (const std::unique_ptr<Representation>& representation :
|
||||||
|
representations_) {
|
||||||
|
double timestamp;
|
||||||
|
if (representation->GetEarliestTimestamp(×tamp) &&
|
||||||
|
((earliest_timestamp < 0) || (timestamp < earliest_timestamp))) {
|
||||||
|
earliest_timestamp = timestamp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (earliest_timestamp < 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
*timestamp_seconds = earliest_timestamp;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This implementation assumes that each representations' segments' are
|
||||||
|
// contiguous.
|
||||||
|
// Also assumes that all Representations are added before this is called.
|
||||||
|
// This checks whether the first elements of the lists in
|
||||||
|
// representation_segment_start_times_ are aligned.
|
||||||
|
// For example, suppose this method was just called with args rep_id=2
|
||||||
|
// start_time=1.
|
||||||
|
// 1 -> [1, 100, 200]
|
||||||
|
// 2 -> [1]
|
||||||
|
// The timestamps of the first elements match, so this flags
|
||||||
|
// segments_aligned_=true.
|
||||||
|
// Also since the first segment start times match, the first element of all the
|
||||||
|
// lists are removed, so the map of lists becomes:
|
||||||
|
// 1 -> [100, 200]
|
||||||
|
// 2 -> []
|
||||||
|
// Note that there could be false positives.
|
||||||
|
// e.g. just got rep_id=3 start_time=1 duration=300, and the duration of the
|
||||||
|
// whole AdaptationSet is 300.
|
||||||
|
// 1 -> [1, 100, 200]
|
||||||
|
// 2 -> [1, 90, 100]
|
||||||
|
// 3 -> [1]
|
||||||
|
// They are not aligned but this will be marked as aligned.
|
||||||
|
// But since this is unlikely to happen in the packager (and to save
|
||||||
|
// computation), this isn't handled at the moment.
|
||||||
|
void AdaptationSet::CheckLiveSegmentAlignment(uint32_t representation_id,
|
||||||
|
uint64_t start_time,
|
||||||
|
uint64_t /* duration */) {
|
||||||
|
if (segments_aligned_ == kSegmentAlignmentFalse ||
|
||||||
|
force_set_segment_alignment_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::list<uint64_t>& representation_start_times =
|
||||||
|
representation_segment_start_times_[representation_id];
|
||||||
|
representation_start_times.push_back(start_time);
|
||||||
|
// There's no way to detemine whether the segments are aligned if some
|
||||||
|
// representations do not have any segments.
|
||||||
|
if (representation_segment_start_times_.size() != representations_.size())
|
||||||
|
return;
|
||||||
|
|
||||||
|
DCHECK(!representation_start_times.empty());
|
||||||
|
const uint64_t expected_start_time = representation_start_times.front();
|
||||||
|
for (RepresentationTimeline::const_iterator it =
|
||||||
|
representation_segment_start_times_.begin();
|
||||||
|
it != representation_segment_start_times_.end(); ++it) {
|
||||||
|
// If there are no entries in a list, then there is no way for the
|
||||||
|
// segment alignment status to change.
|
||||||
|
// Note that it can be empty because entries get deleted below.
|
||||||
|
if (it->second.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (expected_start_time != it->second.front()) {
|
||||||
|
// Flag as false and clear the start times data, no need to keep it
|
||||||
|
// around.
|
||||||
|
segments_aligned_ = kSegmentAlignmentFalse;
|
||||||
|
representation_segment_start_times_.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
segments_aligned_ = kSegmentAlignmentTrue;
|
||||||
|
|
||||||
|
for (RepresentationTimeline::iterator it =
|
||||||
|
representation_segment_start_times_.begin();
|
||||||
|
it != representation_segment_start_times_.end(); ++it) {
|
||||||
|
it->second.pop_front();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure all segements start times match for all Representations.
|
||||||
|
// This assumes that the segments are contiguous.
|
||||||
|
void AdaptationSet::CheckVodSegmentAlignment() {
|
||||||
|
if (segments_aligned_ == kSegmentAlignmentFalse ||
|
||||||
|
force_set_segment_alignment_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (representation_segment_start_times_.empty())
|
||||||
|
return;
|
||||||
|
if (representation_segment_start_times_.size() == 1) {
|
||||||
|
segments_aligned_ = kSegmentAlignmentTrue;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is not the most efficient implementation to compare the values
|
||||||
|
// because expected_time_line is compared against all other time lines, but
|
||||||
|
// probably the most readable.
|
||||||
|
const std::list<uint64_t>& expected_time_line =
|
||||||
|
representation_segment_start_times_.begin()->second;
|
||||||
|
|
||||||
|
bool all_segment_time_line_same_length = true;
|
||||||
|
// Note that the first entry is skipped because it is expected_time_line.
|
||||||
|
RepresentationTimeline::const_iterator it =
|
||||||
|
representation_segment_start_times_.begin();
|
||||||
|
for (++it; it != representation_segment_start_times_.end(); ++it) {
|
||||||
|
const std::list<uint64_t>& other_time_line = it->second;
|
||||||
|
if (expected_time_line.size() != other_time_line.size()) {
|
||||||
|
all_segment_time_line_same_length = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::list<uint64_t>* longer_list = &other_time_line;
|
||||||
|
const std::list<uint64_t>* shorter_list = &expected_time_line;
|
||||||
|
if (expected_time_line.size() > other_time_line.size()) {
|
||||||
|
shorter_list = &other_time_line;
|
||||||
|
longer_list = &expected_time_line;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!std::equal(shorter_list->begin(), shorter_list->end(),
|
||||||
|
longer_list->begin())) {
|
||||||
|
// Some segments are definitely unaligned.
|
||||||
|
segments_aligned_ = kSegmentAlignmentFalse;
|
||||||
|
representation_segment_start_times_.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(rkuroiwa): The right way to do this is to also check the durations.
|
||||||
|
// For example:
|
||||||
|
// (a) 3 4 5
|
||||||
|
// (b) 3 4 5 6
|
||||||
|
// could be true or false depending on the length of the third segment of (a).
|
||||||
|
// i.e. if length of the third segment is 2, then this is not aligned.
|
||||||
|
if (!all_segment_time_line_same_length) {
|
||||||
|
segments_aligned_ = kSegmentAlignmentUnknown;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
segments_aligned_ = kSegmentAlignmentTrue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Since all AdaptationSet cares about is the maxFrameRate, representation_id
|
||||||
|
// is not passed to this method.
|
||||||
|
void AdaptationSet::RecordFrameRate(uint32_t frame_duration,
|
||||||
|
uint32_t timescale) {
|
||||||
|
if (frame_duration == 0) {
|
||||||
|
LOG(ERROR) << "Frame duration is 0 and cannot be set.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
video_frame_rates_[static_cast<double>(timescale) / frame_duration] =
|
||||||
|
base::IntToString(timescale) + "/" + base::IntToString(frame_duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace shaka
|
|
@ -0,0 +1,279 @@
|
||||||
|
// Copyright 2017 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
//
|
||||||
|
/// All the methods that are virtual are virtual for mocking.
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <set>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "packager/base/atomic_sequence_num.h"
|
||||||
|
#include "packager/mpd/base/mpd_options.h"
|
||||||
|
#include "packager/mpd/base/xml/scoped_xml_ptr.h"
|
||||||
|
|
||||||
|
namespace shaka {
|
||||||
|
|
||||||
|
class MediaInfo;
|
||||||
|
class Representation;
|
||||||
|
|
||||||
|
struct ContentProtectionElement;
|
||||||
|
|
||||||
|
namespace xml {
|
||||||
|
class XmlNode;
|
||||||
|
} // namespace xml
|
||||||
|
|
||||||
|
/// AdaptationSet class provides methods to add Representations and
|
||||||
|
/// <ContentProtection> elements to the AdaptationSet element.
|
||||||
|
class AdaptationSet {
|
||||||
|
public:
|
||||||
|
// The role for this AdaptationSet. These values are used to add a Role
|
||||||
|
// element to the AdaptationSet with schemeIdUri=urn:mpeg:dash:role:2011.
|
||||||
|
// See ISO/IEC 23009-1:2012 section 5.8.5.5.
|
||||||
|
enum Role {
|
||||||
|
kRoleCaption,
|
||||||
|
kRoleSubtitle,
|
||||||
|
kRoleMain,
|
||||||
|
kRoleAlternate,
|
||||||
|
kRoleSupplementary,
|
||||||
|
kRoleCommentary,
|
||||||
|
kRoleDub
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual ~AdaptationSet();
|
||||||
|
|
||||||
|
/// Create a Representation instance using @a media_info.
|
||||||
|
/// @param media_info is a MediaInfo object used to initialize the returned
|
||||||
|
/// Representation instance. It may contain only one of VideoInfo,
|
||||||
|
/// AudioInfo, or TextInfo, i.e. VideoInfo XOR AudioInfo XOR TextInfo.
|
||||||
|
/// @return On success, returns a pointer to Representation. Otherwise returns
|
||||||
|
/// NULL. The returned pointer is owned by the AdaptationSet instance.
|
||||||
|
virtual Representation* AddRepresentation(const MediaInfo& media_info);
|
||||||
|
|
||||||
|
/// Add a ContenProtection element to the adaptation set.
|
||||||
|
/// AdaptationSet does not add <ContentProtection> elements
|
||||||
|
/// automatically to itself even if @a media_info.protected_content is
|
||||||
|
/// populated. This is because some MPDs should have the elements at
|
||||||
|
/// AdaptationSet level and some at Representation level.
|
||||||
|
/// @param element contains the ContentProtection element contents.
|
||||||
|
/// If @a element has {value, schemeIdUri} set and has
|
||||||
|
/// {“value”, “schemeIdUri”} as key for @a additional_attributes,
|
||||||
|
/// then the former is used.
|
||||||
|
virtual void AddContentProtectionElement(
|
||||||
|
const ContentProtectionElement& element);
|
||||||
|
|
||||||
|
/// Update the 'cenc:pssh' element for @a drm_uuid ContentProtection element.
|
||||||
|
/// If the element does not exist, this will add one.
|
||||||
|
/// @param drm_uuid is the UUID of the DRM for encryption.
|
||||||
|
/// @param pssh is the content of <cenc:pssh> element.
|
||||||
|
/// Note that DASH IF IOP mentions that this should be base64 encoded
|
||||||
|
/// string of the whole pssh box.
|
||||||
|
/// @attention This might get removed once DASH IF IOP specification makes a
|
||||||
|
/// a clear guideline on how to handle key rotation. Also to get
|
||||||
|
/// this working with shaka-player, this method *DOES NOT* update
|
||||||
|
/// the PSSH element. Instead, it removes the element regardless of
|
||||||
|
/// the content of @a pssh.
|
||||||
|
virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
|
||||||
|
const std::string& pssh);
|
||||||
|
|
||||||
|
/// Set the Role element for this AdaptationSet.
|
||||||
|
/// The Role element's is schemeIdUri='urn:mpeg:dash:role:2011'.
|
||||||
|
/// See ISO/IEC 23009-1:2012 section 5.8.5.5.
|
||||||
|
/// @param role of this AdaptationSet.
|
||||||
|
virtual void AddRole(Role role);
|
||||||
|
|
||||||
|
/// Makes a copy of AdaptationSet xml element with its child Representation
|
||||||
|
/// and ContentProtection elements.
|
||||||
|
/// @return On success returns a non-NULL scoped_xml_ptr. Otherwise returns a
|
||||||
|
/// NULL scoped_xml_ptr.
|
||||||
|
xml::scoped_xml_ptr<xmlNode> GetXml();
|
||||||
|
|
||||||
|
/// Forces the (sub)segmentAlignment field to be set to @a segment_alignment.
|
||||||
|
/// Use this if you are certain that the (sub)segments are alinged/unaligned
|
||||||
|
/// for the AdaptationSet.
|
||||||
|
/// @param segment_alignment is the value used for (sub)segmentAlignment
|
||||||
|
/// attribute.
|
||||||
|
virtual void ForceSetSegmentAlignment(bool segment_alignment);
|
||||||
|
|
||||||
|
/// Adds the id of the adaptation set this adaptation set can switch to.
|
||||||
|
/// @param adaptation_set_id is the id of the switchable adaptation set.
|
||||||
|
void AddAdaptationSetSwitching(uint32_t adaptation_set_id);
|
||||||
|
|
||||||
|
/// @return the ids of the adaptation sets this adaptation set can switch to.
|
||||||
|
const std::vector<uint32_t>& adaptation_set_switching_ids() const {
|
||||||
|
return adaptation_set_switching_ids_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Must be unique in the Period.
|
||||||
|
uint32_t id() const { return id_; }
|
||||||
|
|
||||||
|
/// Notifies the AdaptationSet instance that a new (sub)segment was added to
|
||||||
|
/// the Representation with @a representation_id.
|
||||||
|
/// This must be called every time a (sub)segment is added to a
|
||||||
|
/// Representation in this AdaptationSet.
|
||||||
|
/// If a Representation is constructed using AddRepresentation() this
|
||||||
|
/// is called automatically whenever Representation::AddNewSegment() is
|
||||||
|
/// is called.
|
||||||
|
/// @param representation_id is the id of the Representation with a new
|
||||||
|
/// segment.
|
||||||
|
/// @param start_time is the start time of the new segment.
|
||||||
|
/// @param duration is the duration of the new segment.
|
||||||
|
void OnNewSegmentForRepresentation(uint32_t representation_id,
|
||||||
|
uint64_t start_time,
|
||||||
|
uint64_t duration);
|
||||||
|
|
||||||
|
/// Notifies the AdaptationSet instance that the sample duration for the
|
||||||
|
/// Representation was set.
|
||||||
|
/// The frame duration for a video Representation might not be specified when
|
||||||
|
/// a Representation is created (by calling AddRepresentation()).
|
||||||
|
/// This should be used to notify this instance that the frame rate for a
|
||||||
|
/// Represenatation has been set.
|
||||||
|
/// This method is called automatically when
|
||||||
|
/// Represenatation::SetSampleDuration() is called if the Represenatation
|
||||||
|
/// instance was created using AddRepresentation().
|
||||||
|
/// @param representation_id is the id of the Representation.
|
||||||
|
/// @frame_duration is the duration of a frame in the Representation.
|
||||||
|
/// @param timescale is the timescale of the Representation.
|
||||||
|
void OnSetFrameRateForRepresentation(uint32_t representation_id,
|
||||||
|
uint32_t frame_duration,
|
||||||
|
uint32_t timescale);
|
||||||
|
|
||||||
|
/// Add the id of the adaptation set this trick play adaptation set belongs
|
||||||
|
/// to.
|
||||||
|
/// @param id the id of the reference (or main) adapation set.
|
||||||
|
virtual void AddTrickPlayReferenceId(uint32_t id);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// @param adaptation_set_id is an ID number for this AdaptationSet.
|
||||||
|
/// @param lang is the language of this AdaptationSet. Mainly relevant for
|
||||||
|
/// audio.
|
||||||
|
/// @param mpd_options is the options for this MPD.
|
||||||
|
/// @param mpd_type is the type of this MPD.
|
||||||
|
/// @param representation_counter is a Counter for assigning ID numbers to
|
||||||
|
/// Representation. It can not be NULL.
|
||||||
|
AdaptationSet(uint32_t adaptation_set_id,
|
||||||
|
const std::string& lang,
|
||||||
|
const MpdOptions& mpd_options,
|
||||||
|
base::AtomicSequenceNumber* representation_counter);
|
||||||
|
|
||||||
|
private:
|
||||||
|
AdaptationSet(const AdaptationSet&) = delete;
|
||||||
|
AdaptationSet& operator=(const AdaptationSet&) = delete;
|
||||||
|
|
||||||
|
friend class MpdBuilder;
|
||||||
|
template <DashProfile profile>
|
||||||
|
friend class MpdBuilderTest;
|
||||||
|
|
||||||
|
// kSegmentAlignmentUnknown means that it is uncertain if the
|
||||||
|
// (sub)segments are aligned or not.
|
||||||
|
// kSegmentAlignmentTrue means that it is certain that the all the (current)
|
||||||
|
// segments added to the adaptation set are aligned.
|
||||||
|
// kSegmentAlignmentFalse means that it is it is certain that some segments
|
||||||
|
// are not aligned. This is useful to disable the computation for
|
||||||
|
// segment alignment, once it is certain that some segments are not aligned.
|
||||||
|
enum SegmentAligmentStatus {
|
||||||
|
kSegmentAlignmentUnknown,
|
||||||
|
kSegmentAlignmentTrue,
|
||||||
|
kSegmentAlignmentFalse
|
||||||
|
};
|
||||||
|
|
||||||
|
// This maps Representations (IDs) to a list of start times of the segments.
|
||||||
|
// e.g.
|
||||||
|
// If Representation 1 has start time 0, 100, 200 and Representation 2 has
|
||||||
|
// start times 0, 200, 400, then the map contains:
|
||||||
|
// 1 -> [0, 100, 200]
|
||||||
|
// 2 -> [0, 200, 400]
|
||||||
|
typedef std::map<uint32_t, std::list<uint64_t>> RepresentationTimeline;
|
||||||
|
|
||||||
|
// Gets the earliest, normalized segment timestamp. Returns true if
|
||||||
|
// successful, false otherwise.
|
||||||
|
bool GetEarliestTimestamp(double* timestamp_seconds);
|
||||||
|
|
||||||
|
/// Called from OnNewSegmentForRepresentation(). Checks whether the segments
|
||||||
|
/// are aligned. Sets segments_aligned_.
|
||||||
|
/// This is only for Live. For VOD, CheckVodSegmentAlignment() should be used.
|
||||||
|
/// @param representation_id is the id of the Representation with a new
|
||||||
|
/// segment.
|
||||||
|
/// @param start_time is the start time of the new segment.
|
||||||
|
/// @param duration is the duration of the new segment.
|
||||||
|
void CheckLiveSegmentAlignment(uint32_t representation_id,
|
||||||
|
uint64_t start_time,
|
||||||
|
uint64_t duration);
|
||||||
|
|
||||||
|
// Checks representation_segment_start_times_ and sets segments_aligned_.
|
||||||
|
// Use this for VOD, do not use for Live.
|
||||||
|
void CheckVodSegmentAlignment();
|
||||||
|
|
||||||
|
// Records the framerate of a Representation.
|
||||||
|
void RecordFrameRate(uint32_t frame_duration, uint32_t timescale);
|
||||||
|
|
||||||
|
std::list<ContentProtectionElement> content_protection_elements_;
|
||||||
|
std::list<std::unique_ptr<Representation>> representations_;
|
||||||
|
|
||||||
|
base::AtomicSequenceNumber* const representation_counter_;
|
||||||
|
|
||||||
|
const uint32_t id_;
|
||||||
|
const std::string lang_;
|
||||||
|
const MpdOptions& mpd_options_;
|
||||||
|
|
||||||
|
// The ids of the adaptation sets this adaptation set can switch to.
|
||||||
|
std::vector<uint32_t> adaptation_set_switching_ids_;
|
||||||
|
|
||||||
|
// Video widths and heights of Representations. Note that this is a set; if
|
||||||
|
// there is only 1 resolution, then @width & @height should be set, otherwise
|
||||||
|
// @maxWidth & @maxHeight should be set for DASH IOP.
|
||||||
|
std::set<uint32_t> video_widths_;
|
||||||
|
std::set<uint32_t> video_heights_;
|
||||||
|
|
||||||
|
// Video representations' frame rates.
|
||||||
|
// The frame rate notation for MPD is <integer>/<integer> (where the
|
||||||
|
// denominator is optional). This means the frame rate could be non-whole
|
||||||
|
// rational value, therefore the key is of type double.
|
||||||
|
// Value is <integer>/<integer> in string form.
|
||||||
|
// So, key == CalculatedValue(value)
|
||||||
|
std::map<double, std::string> video_frame_rates_;
|
||||||
|
|
||||||
|
// contentType attribute of AdaptationSet.
|
||||||
|
// Determined by examining the MediaInfo passed to AddRepresentation().
|
||||||
|
std::string content_type_;
|
||||||
|
|
||||||
|
// This does not have to be a set, it could be a list or vector because all we
|
||||||
|
// really care is whether there is more than one entry.
|
||||||
|
// Contains one entry if all the Representations have the same picture aspect
|
||||||
|
// ratio (@par attribute for AdaptationSet).
|
||||||
|
// There will be more than one entry if there are multiple picture aspect
|
||||||
|
// ratios.
|
||||||
|
// The @par attribute should only be set if there is exactly one entry
|
||||||
|
// in this set.
|
||||||
|
std::set<std::string> picture_aspect_ratio_;
|
||||||
|
|
||||||
|
// The roles of this AdaptationSet.
|
||||||
|
std::set<Role> roles_;
|
||||||
|
|
||||||
|
// True iff all the segments are aligned.
|
||||||
|
SegmentAligmentStatus segments_aligned_;
|
||||||
|
bool force_set_segment_alignment_;
|
||||||
|
|
||||||
|
// Keeps track of segment start times of Representations.
|
||||||
|
// For VOD, this will not be cleared, all the segment start times are
|
||||||
|
// stored in this. This should not out-of-memory for a reasonable length
|
||||||
|
// video and reasonable subsegment length.
|
||||||
|
// For Live, the entries are deleted (see CheckLiveSegmentAlignment()
|
||||||
|
// implementation comment) because storing the entire timeline is not
|
||||||
|
// reasonable and may cause an out-of-memory problem.
|
||||||
|
RepresentationTimeline representation_segment_start_times_;
|
||||||
|
|
||||||
|
// Record the reference id for the original adaptation sets the trick play
|
||||||
|
// stream belongs to. This is a set because the trick play streams may be for
|
||||||
|
// multiple AdaptationSets (e.g. SD and HD videos in different AdaptationSets
|
||||||
|
// can share the same trick play stream.)
|
||||||
|
std::set<uint32_t> trick_play_reference_ids_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace shaka
|
|
@ -7,9 +7,11 @@
|
||||||
#include "packager/mpd/base/dash_iop_mpd_notifier.h"
|
#include "packager/mpd/base/dash_iop_mpd_notifier.h"
|
||||||
|
|
||||||
#include "packager/base/stl_util.h"
|
#include "packager/base/stl_util.h"
|
||||||
|
#include "packager/mpd/base/adaptation_set.h"
|
||||||
#include "packager/mpd/base/media_info.pb.h"
|
#include "packager/mpd/base/media_info.pb.h"
|
||||||
#include "packager/mpd/base/mpd_notifier_util.h"
|
#include "packager/mpd/base/mpd_notifier_util.h"
|
||||||
#include "packager/mpd/base/mpd_utils.h"
|
#include "packager/mpd/base/mpd_utils.h"
|
||||||
|
#include "packager/mpd/base/representation.h"
|
||||||
|
|
||||||
namespace shaka {
|
namespace shaka {
|
||||||
|
|
||||||
|
|
|
@ -15,12 +15,15 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "packager/base/synchronization/lock.h"
|
#include "packager/base/synchronization/lock.h"
|
||||||
#include "packager/mpd/base/mpd_builder.h"
|
|
||||||
#include "packager/mpd/base/mpd_notifier_util.h"
|
#include "packager/mpd/base/mpd_notifier_util.h"
|
||||||
#include "packager/mpd/base/mpd_options.h"
|
#include "packager/mpd/base/mpd_options.h"
|
||||||
|
|
||||||
namespace shaka {
|
namespace shaka {
|
||||||
|
|
||||||
|
class AdaptationSet;
|
||||||
|
class MpdBuilder;
|
||||||
|
class Representation;
|
||||||
|
|
||||||
/// This class is an MpdNotifier which will try its best to generate a
|
/// This class is an MpdNotifier which will try its best to generate a
|
||||||
/// DASH IF IOPv3 compliant MPD.
|
/// DASH IF IOPv3 compliant MPD.
|
||||||
/// e.g.
|
/// e.g.
|
||||||
|
|
|
@ -11,8 +11,10 @@
|
||||||
|
|
||||||
#include "packager/base/compiler_specific.h"
|
#include "packager/base/compiler_specific.h"
|
||||||
#include "packager/base/synchronization/lock.h"
|
#include "packager/base/synchronization/lock.h"
|
||||||
|
#include "packager/mpd/base/adaptation_set.h"
|
||||||
#include "packager/mpd/base/content_protection_element.h"
|
#include "packager/mpd/base/content_protection_element.h"
|
||||||
#include "packager/mpd/base/mpd_builder.h"
|
#include "packager/mpd/base/mpd_builder.h"
|
||||||
|
#include "packager/mpd/base/representation.h"
|
||||||
|
|
||||||
namespace shaka {
|
namespace shaka {
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,10 +4,6 @@
|
||||||
// license that can be found in the LICENSE file or at
|
// license that can be found in the LICENSE file or at
|
||||||
// https://developers.google.com/open-source/licenses/bsd
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
//
|
//
|
||||||
// This file contains the MpdBuilder, AdaptationSet, and Representation class
|
|
||||||
// declarations.
|
|
||||||
// http://goo.gl/UrsSlF
|
|
||||||
//
|
|
||||||
/// All the methods that are virtual are virtual for mocking.
|
/// All the methods that are virtual are virtual for mocking.
|
||||||
/// NOTE: Inclusion of this module will cause xmlInitParser and xmlCleanupParser
|
/// NOTE: Inclusion of this module will cause xmlInitParser and xmlCleanupParser
|
||||||
/// to be called at static initialization / deinitialization time.
|
/// to be called at static initialization / deinitialization time.
|
||||||
|
@ -15,24 +11,15 @@
|
||||||
#ifndef MPD_BASE_MPD_BUILDER_H_
|
#ifndef MPD_BASE_MPD_BUILDER_H_
|
||||||
#define MPD_BASE_MPD_BUILDER_H_
|
#define MPD_BASE_MPD_BUILDER_H_
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <libxml/tree.h>
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <memory>
|
||||||
#include <set>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "packager/base/atomic_sequence_num.h"
|
#include "packager/base/atomic_sequence_num.h"
|
||||||
#include "packager/base/callback.h"
|
|
||||||
#include "packager/base/gtest_prod_util.h"
|
|
||||||
#include "packager/base/time/clock.h"
|
#include "packager/base/time/clock.h"
|
||||||
#include "packager/base/time/time.h"
|
|
||||||
#include "packager/mpd/base/bandwidth_estimator.h"
|
|
||||||
#include "packager/mpd/base/content_protection_element.h"
|
|
||||||
#include "packager/mpd/base/media_info.pb.h"
|
|
||||||
#include "packager/mpd/base/mpd_options.h"
|
#include "packager/mpd/base/mpd_options.h"
|
||||||
#include "packager/mpd/base/segment_info.h"
|
|
||||||
#include "packager/mpd/base/xml/scoped_xml_ptr.h"
|
|
||||||
|
|
||||||
// TODO(rkuroiwa): For classes with |id_|, consider removing the field and let
|
// TODO(rkuroiwa): For classes with |id_|, consider removing the field and let
|
||||||
// the MPD (XML) generation functions take care of assigning an ID to each
|
// the MPD (XML) generation functions take care of assigning an ID to each
|
||||||
|
@ -40,14 +27,10 @@
|
||||||
namespace shaka {
|
namespace shaka {
|
||||||
|
|
||||||
class AdaptationSet;
|
class AdaptationSet;
|
||||||
class File;
|
class MediaInfo;
|
||||||
class Representation;
|
|
||||||
|
|
||||||
namespace xml {
|
namespace xml {
|
||||||
|
|
||||||
class XmlNode;
|
class XmlNode;
|
||||||
class RepresentationXmlNode;
|
|
||||||
|
|
||||||
} // namespace xml
|
} // namespace xml
|
||||||
|
|
||||||
/// This class generates DASH MPDs (Media Presentation Descriptions).
|
/// This class generates DASH MPDs (Media Presentation Descriptions).
|
||||||
|
@ -89,6 +72,9 @@ class MpdBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
MpdBuilder(const MpdBuilder&) = delete;
|
||||||
|
MpdBuilder& operator=(const MpdBuilder&) = delete;
|
||||||
|
|
||||||
// LiveMpdBuilderTest needs to set availabilityStartTime so that the test
|
// LiveMpdBuilderTest needs to set availabilityStartTime so that the test
|
||||||
// doesn't need to depend on current time.
|
// doesn't need to depend on current time.
|
||||||
friend class LiveMpdBuilderTest;
|
friend class LiveMpdBuilderTest;
|
||||||
|
@ -133,425 +119,6 @@ class MpdBuilder {
|
||||||
// By default, this returns the current time. This can be injected for
|
// By default, this returns the current time. This can be injected for
|
||||||
// testing.
|
// testing.
|
||||||
std::unique_ptr<base::Clock> clock_;
|
std::unique_ptr<base::Clock> clock_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(MpdBuilder);
|
|
||||||
};
|
|
||||||
|
|
||||||
/// AdaptationSet class provides methods to add Representations and
|
|
||||||
/// <ContentProtection> elements to the AdaptationSet element.
|
|
||||||
class AdaptationSet {
|
|
||||||
public:
|
|
||||||
// The role for this AdaptationSet. These values are used to add a Role
|
|
||||||
// element to the AdaptationSet with schemeIdUri=urn:mpeg:dash:role:2011.
|
|
||||||
// See ISO/IEC 23009-1:2012 section 5.8.5.5.
|
|
||||||
enum Role {
|
|
||||||
kRoleCaption,
|
|
||||||
kRoleSubtitle,
|
|
||||||
kRoleMain,
|
|
||||||
kRoleAlternate,
|
|
||||||
kRoleSupplementary,
|
|
||||||
kRoleCommentary,
|
|
||||||
kRoleDub
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual ~AdaptationSet();
|
|
||||||
|
|
||||||
/// Create a Representation instance using @a media_info.
|
|
||||||
/// @param media_info is a MediaInfo object used to initialize the returned
|
|
||||||
/// Representation instance. It may contain only one of VideoInfo,
|
|
||||||
/// AudioInfo, or TextInfo, i.e. VideoInfo XOR AudioInfo XOR TextInfo.
|
|
||||||
/// @return On success, returns a pointer to Representation. Otherwise returns
|
|
||||||
/// NULL. The returned pointer is owned by the AdaptationSet instance.
|
|
||||||
virtual Representation* AddRepresentation(const MediaInfo& media_info);
|
|
||||||
|
|
||||||
/// Add a ContenProtection element to the adaptation set.
|
|
||||||
/// AdaptationSet does not add <ContentProtection> elements
|
|
||||||
/// automatically to itself even if @a media_info.protected_content is
|
|
||||||
/// populated. This is because some MPDs should have the elements at
|
|
||||||
/// AdaptationSet level and some at Representation level.
|
|
||||||
/// @param element contains the ContentProtection element contents.
|
|
||||||
/// If @a element has {value, schemeIdUri} set and has
|
|
||||||
/// {“value”, “schemeIdUri”} as key for @a additional_attributes,
|
|
||||||
/// then the former is used.
|
|
||||||
virtual void AddContentProtectionElement(
|
|
||||||
const ContentProtectionElement& element);
|
|
||||||
|
|
||||||
/// Update the 'cenc:pssh' element for @a drm_uuid ContentProtection element.
|
|
||||||
/// If the element does not exist, this will add one.
|
|
||||||
/// @param drm_uuid is the UUID of the DRM for encryption.
|
|
||||||
/// @param pssh is the content of <cenc:pssh> element.
|
|
||||||
/// Note that DASH IF IOP mentions that this should be base64 encoded
|
|
||||||
/// string of the whole pssh box.
|
|
||||||
/// @attention This might get removed once DASH IF IOP specification makes a
|
|
||||||
/// a clear guideline on how to handle key rotation. Also to get
|
|
||||||
/// this working with shaka-player, this method *DOES NOT* update
|
|
||||||
/// the PSSH element. Instead, it removes the element regardless of
|
|
||||||
/// the content of @a pssh.
|
|
||||||
virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
|
|
||||||
const std::string& pssh);
|
|
||||||
|
|
||||||
/// Set the Role element for this AdaptationSet.
|
|
||||||
/// The Role element's is schemeIdUri='urn:mpeg:dash:role:2011'.
|
|
||||||
/// See ISO/IEC 23009-1:2012 section 5.8.5.5.
|
|
||||||
/// @param role of this AdaptationSet.
|
|
||||||
virtual void AddRole(Role role);
|
|
||||||
|
|
||||||
/// Makes a copy of AdaptationSet xml element with its child Representation
|
|
||||||
/// and ContentProtection elements.
|
|
||||||
/// @return On success returns a non-NULL scoped_xml_ptr. Otherwise returns a
|
|
||||||
/// NULL scoped_xml_ptr.
|
|
||||||
xml::scoped_xml_ptr<xmlNode> GetXml();
|
|
||||||
|
|
||||||
/// Forces the (sub)segmentAlignment field to be set to @a segment_alignment.
|
|
||||||
/// Use this if you are certain that the (sub)segments are alinged/unaligned
|
|
||||||
/// for the AdaptationSet.
|
|
||||||
/// @param segment_alignment is the value used for (sub)segmentAlignment
|
|
||||||
/// attribute.
|
|
||||||
virtual void ForceSetSegmentAlignment(bool segment_alignment);
|
|
||||||
|
|
||||||
/// Adds the id of the adaptation set this adaptation set can switch to.
|
|
||||||
/// @param adaptation_set_id is the id of the switchable adaptation set.
|
|
||||||
void AddAdaptationSetSwitching(uint32_t adaptation_set_id);
|
|
||||||
|
|
||||||
/// @return the ids of the adaptation sets this adaptation set can switch to.
|
|
||||||
const std::vector<uint32_t>& adaptation_set_switching_ids() const {
|
|
||||||
return adaptation_set_switching_ids_;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Must be unique in the Period.
|
|
||||||
uint32_t id() const { return id_; }
|
|
||||||
|
|
||||||
/// Notifies the AdaptationSet instance that a new (sub)segment was added to
|
|
||||||
/// the Representation with @a representation_id.
|
|
||||||
/// This must be called every time a (sub)segment is added to a
|
|
||||||
/// Representation in this AdaptationSet.
|
|
||||||
/// If a Representation is constructed using AddRepresentation() this
|
|
||||||
/// is called automatically whenever Representation::AddNewSegment() is
|
|
||||||
/// is called.
|
|
||||||
/// @param representation_id is the id of the Representation with a new
|
|
||||||
/// segment.
|
|
||||||
/// @param start_time is the start time of the new segment.
|
|
||||||
/// @param duration is the duration of the new segment.
|
|
||||||
void OnNewSegmentForRepresentation(uint32_t representation_id,
|
|
||||||
uint64_t start_time,
|
|
||||||
uint64_t duration);
|
|
||||||
|
|
||||||
/// Notifies the AdaptationSet instance that the sample duration for the
|
|
||||||
/// Representation was set.
|
|
||||||
/// The frame duration for a video Representation might not be specified when
|
|
||||||
/// a Representation is created (by calling AddRepresentation()).
|
|
||||||
/// This should be used to notify this instance that the frame rate for a
|
|
||||||
/// Represenatation has been set.
|
|
||||||
/// This method is called automatically when
|
|
||||||
/// Represenatation::SetSampleDuration() is called if the Represenatation
|
|
||||||
/// instance was created using AddRepresentation().
|
|
||||||
/// @param representation_id is the id of the Representation.
|
|
||||||
/// @frame_duration is the duration of a frame in the Representation.
|
|
||||||
/// @param timescale is the timescale of the Representation.
|
|
||||||
void OnSetFrameRateForRepresentation(uint32_t representation_id,
|
|
||||||
uint32_t frame_duration,
|
|
||||||
uint32_t timescale);
|
|
||||||
|
|
||||||
/// Add the id of the adaptation set this trick play adaptation set belongs
|
|
||||||
/// to.
|
|
||||||
/// @param id the id of the reference (or main) adapation set.
|
|
||||||
virtual void AddTrickPlayReferenceId(uint32_t id);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
/// @param adaptation_set_id is an ID number for this AdaptationSet.
|
|
||||||
/// @param lang is the language of this AdaptationSet. Mainly relevant for
|
|
||||||
/// audio.
|
|
||||||
/// @param mpd_options is the options for this MPD.
|
|
||||||
/// @param mpd_type is the type of this MPD.
|
|
||||||
/// @param representation_counter is a Counter for assigning ID numbers to
|
|
||||||
/// Representation. It can not be NULL.
|
|
||||||
AdaptationSet(uint32_t adaptation_set_id,
|
|
||||||
const std::string& lang,
|
|
||||||
const MpdOptions& mpd_options,
|
|
||||||
base::AtomicSequenceNumber* representation_counter);
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class MpdBuilder;
|
|
||||||
template <DashProfile profile>
|
|
||||||
friend class MpdBuilderTest;
|
|
||||||
|
|
||||||
// kSegmentAlignmentUnknown means that it is uncertain if the
|
|
||||||
// (sub)segments are aligned or not.
|
|
||||||
// kSegmentAlignmentTrue means that it is certain that the all the (current)
|
|
||||||
// segments added to the adaptation set are aligned.
|
|
||||||
// kSegmentAlignmentFalse means that it is it is certain that some segments
|
|
||||||
// are not aligned. This is useful to disable the computation for
|
|
||||||
// segment alignment, once it is certain that some segments are not aligned.
|
|
||||||
enum SegmentAligmentStatus {
|
|
||||||
kSegmentAlignmentUnknown,
|
|
||||||
kSegmentAlignmentTrue,
|
|
||||||
kSegmentAlignmentFalse
|
|
||||||
};
|
|
||||||
|
|
||||||
// This maps Representations (IDs) to a list of start times of the segments.
|
|
||||||
// e.g.
|
|
||||||
// If Representation 1 has start time 0, 100, 200 and Representation 2 has
|
|
||||||
// start times 0, 200, 400, then the map contains:
|
|
||||||
// 1 -> [0, 100, 200]
|
|
||||||
// 2 -> [0, 200, 400]
|
|
||||||
typedef std::map<uint32_t, std::list<uint64_t> > RepresentationTimeline;
|
|
||||||
|
|
||||||
// Gets the earliest, normalized segment timestamp. Returns true if
|
|
||||||
// successful, false otherwise.
|
|
||||||
bool GetEarliestTimestamp(double* timestamp_seconds);
|
|
||||||
|
|
||||||
/// Called from OnNewSegmentForRepresentation(). Checks whether the segments
|
|
||||||
/// are aligned. Sets segments_aligned_.
|
|
||||||
/// This is only for Live. For VOD, CheckVodSegmentAlignment() should be used.
|
|
||||||
/// @param representation_id is the id of the Representation with a new
|
|
||||||
/// segment.
|
|
||||||
/// @param start_time is the start time of the new segment.
|
|
||||||
/// @param duration is the duration of the new segment.
|
|
||||||
void CheckLiveSegmentAlignment(uint32_t representation_id,
|
|
||||||
uint64_t start_time,
|
|
||||||
uint64_t duration);
|
|
||||||
|
|
||||||
// Checks representation_segment_start_times_ and sets segments_aligned_.
|
|
||||||
// Use this for VOD, do not use for Live.
|
|
||||||
void CheckVodSegmentAlignment();
|
|
||||||
|
|
||||||
// Records the framerate of a Representation.
|
|
||||||
void RecordFrameRate(uint32_t frame_duration, uint32_t timescale);
|
|
||||||
|
|
||||||
std::list<ContentProtectionElement> content_protection_elements_;
|
|
||||||
std::list<std::unique_ptr<Representation>> representations_;
|
|
||||||
|
|
||||||
base::AtomicSequenceNumber* const representation_counter_;
|
|
||||||
|
|
||||||
const uint32_t id_;
|
|
||||||
const std::string lang_;
|
|
||||||
const MpdOptions& mpd_options_;
|
|
||||||
|
|
||||||
// The ids of the adaptation sets this adaptation set can switch to.
|
|
||||||
std::vector<uint32_t> adaptation_set_switching_ids_;
|
|
||||||
|
|
||||||
// Video widths and heights of Representations. Note that this is a set; if
|
|
||||||
// there is only 1 resolution, then @width & @height should be set, otherwise
|
|
||||||
// @maxWidth & @maxHeight should be set for DASH IOP.
|
|
||||||
std::set<uint32_t> video_widths_;
|
|
||||||
std::set<uint32_t> video_heights_;
|
|
||||||
|
|
||||||
// Video representations' frame rates.
|
|
||||||
// The frame rate notation for MPD is <integer>/<integer> (where the
|
|
||||||
// denominator is optional). This means the frame rate could be non-whole
|
|
||||||
// rational value, therefore the key is of type double.
|
|
||||||
// Value is <integer>/<integer> in string form.
|
|
||||||
// So, key == CalculatedValue(value)
|
|
||||||
std::map<double, std::string> video_frame_rates_;
|
|
||||||
|
|
||||||
// contentType attribute of AdaptationSet.
|
|
||||||
// Determined by examining the MediaInfo passed to AddRepresentation().
|
|
||||||
std::string content_type_;
|
|
||||||
|
|
||||||
// This does not have to be a set, it could be a list or vector because all we
|
|
||||||
// really care is whether there is more than one entry.
|
|
||||||
// Contains one entry if all the Representations have the same picture aspect
|
|
||||||
// ratio (@par attribute for AdaptationSet).
|
|
||||||
// There will be more than one entry if there are multiple picture aspect
|
|
||||||
// ratios.
|
|
||||||
// The @par attribute should only be set if there is exactly one entry
|
|
||||||
// in this set.
|
|
||||||
std::set<std::string> picture_aspect_ratio_;
|
|
||||||
|
|
||||||
// The roles of this AdaptationSet.
|
|
||||||
std::set<Role> roles_;
|
|
||||||
|
|
||||||
// True iff all the segments are aligned.
|
|
||||||
SegmentAligmentStatus segments_aligned_;
|
|
||||||
bool force_set_segment_alignment_;
|
|
||||||
|
|
||||||
// Keeps track of segment start times of Representations.
|
|
||||||
// For VOD, this will not be cleared, all the segment start times are
|
|
||||||
// stored in this. This should not out-of-memory for a reasonable length
|
|
||||||
// video and reasonable subsegment length.
|
|
||||||
// For Live, the entries are deleted (see CheckLiveSegmentAlignment()
|
|
||||||
// implementation comment) because storing the entire timeline is not
|
|
||||||
// reasonable and may cause an out-of-memory problem.
|
|
||||||
RepresentationTimeline representation_segment_start_times_;
|
|
||||||
|
|
||||||
// Record the reference id for the original adaptation sets the trick play
|
|
||||||
// stream belongs to. This is a set because the trick play streams may be for
|
|
||||||
// multiple AdaptationSets (e.g. SD and HD videos in different AdaptationSets
|
|
||||||
// can share the same trick play stream.)
|
|
||||||
std::set<uint32_t> trick_play_reference_ids_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(AdaptationSet);
|
|
||||||
};
|
|
||||||
|
|
||||||
class RepresentationStateChangeListener {
|
|
||||||
public:
|
|
||||||
RepresentationStateChangeListener() {}
|
|
||||||
virtual ~RepresentationStateChangeListener() {}
|
|
||||||
|
|
||||||
/// Notifies the instance that a new (sub)segment was added to
|
|
||||||
/// the Representation.
|
|
||||||
/// @param start_time is the start time of the new segment.
|
|
||||||
/// @param duration is the duration of the new segment.
|
|
||||||
virtual void OnNewSegmentForRepresentation(uint64_t start_time,
|
|
||||||
uint64_t duration) = 0;
|
|
||||||
|
|
||||||
/// Notifies the instance that the frame rate was set for the
|
|
||||||
/// Representation.
|
|
||||||
/// @param frame_duration is the duration of a frame.
|
|
||||||
/// @param timescale is the timescale of the Representation.
|
|
||||||
virtual void OnSetFrameRateForRepresentation(uint32_t frame_duration,
|
|
||||||
uint32_t timescale) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Representation class contains references to a single media stream, as
|
|
||||||
/// well as optional ContentProtection elements for that stream.
|
|
||||||
class Representation {
|
|
||||||
public:
|
|
||||||
enum SuppressFlag {
|
|
||||||
kSuppressWidth = 1,
|
|
||||||
kSuppressHeight = 2,
|
|
||||||
kSuppressFrameRate = 4,
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual ~Representation();
|
|
||||||
|
|
||||||
/// Tries to initialize the instance. If this does not succeed, the instance
|
|
||||||
/// should not be used.
|
|
||||||
/// @return true on success, false otherwise.
|
|
||||||
bool Init();
|
|
||||||
|
|
||||||
/// Add a ContenProtection element to the representation.
|
|
||||||
/// Representation does not add <ContentProtection> elements
|
|
||||||
/// automatically to itself even if @a media_info passed to
|
|
||||||
/// AdaptationSet::AddRepresentation() has @a media_info.protected_content
|
|
||||||
/// populated. This is because some MPDs should have the elements at
|
|
||||||
/// AdaptationSet level and some at Representation level.
|
|
||||||
/// @param element contains the ContentProtection element contents.
|
|
||||||
/// If @a element has {value, schemeIdUri} set and has
|
|
||||||
/// {“value”, “schemeIdUri”} as key for @a additional_attributes,
|
|
||||||
/// then the former is used.
|
|
||||||
virtual void AddContentProtectionElement(
|
|
||||||
const ContentProtectionElement& element);
|
|
||||||
|
|
||||||
/// Update the 'cenc:pssh' element for @a drm_uuid ContentProtection element.
|
|
||||||
/// If the element does not exist, this will add one.
|
|
||||||
/// @param drm_uuid is the UUID of the DRM for encryption.
|
|
||||||
/// @param pssh is the content of <cenc:pssh> element.
|
|
||||||
/// Note that DASH IF IOP mentions that this should be base64 encoded
|
|
||||||
/// string of the whole pssh box.
|
|
||||||
/// @attention This might get removed once DASH IF IOP specification makes a
|
|
||||||
/// a clear guideline on how to handle key rotation. Also to get
|
|
||||||
/// this working with shaka-player, this method *DOES NOT* update
|
|
||||||
/// the PSSH element. Instead, it removes the element regardless of
|
|
||||||
/// the content of @a pssh.
|
|
||||||
virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
|
|
||||||
const std::string& pssh);
|
|
||||||
|
|
||||||
/// Add a media (sub)segment to the representation.
|
|
||||||
/// AdaptationSet@{subsegmentAlignment,segmentAlignment} cannot be set
|
|
||||||
/// if this is not called for all Representations.
|
|
||||||
/// @param start_time is the start time for the (sub)segment, in units of the
|
|
||||||
/// stream's time scale.
|
|
||||||
/// @param duration is the duration of the segment, in units of the stream's
|
|
||||||
/// time scale.
|
|
||||||
/// @param size of the segment in bytes.
|
|
||||||
virtual void AddNewSegment(uint64_t start_time,
|
|
||||||
uint64_t duration,
|
|
||||||
uint64_t size);
|
|
||||||
|
|
||||||
/// Set the sample duration of this Representation.
|
|
||||||
/// Sample duration is not available right away especially for live. This
|
|
||||||
/// allows setting the sample duration after the Representation has been
|
|
||||||
/// initialized.
|
|
||||||
/// @param sample_duration is the duration of a sample.
|
|
||||||
virtual void SetSampleDuration(uint32_t sample_duration);
|
|
||||||
|
|
||||||
/// @return Copy of <Representation>.
|
|
||||||
xml::scoped_xml_ptr<xmlNode> GetXml();
|
|
||||||
|
|
||||||
/// By calling this methods, the next time GetXml() is
|
|
||||||
/// called, the corresponding attributes will not be set.
|
|
||||||
/// For example, if SuppressOnce(kSuppressWidth) is called, then GetXml() will
|
|
||||||
/// return a <Representation> element without a @width attribute.
|
|
||||||
/// Note that it only applies to the next call to GetXml(), calling GetXml()
|
|
||||||
/// again without calling this methods will return a <Representation> element
|
|
||||||
/// with the attribute.
|
|
||||||
/// This may be called multiple times to set different (or the same) flags.
|
|
||||||
void SuppressOnce(SuppressFlag flag);
|
|
||||||
|
|
||||||
/// @return ID number for <Representation>.
|
|
||||||
uint32_t id() const { return id_; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
/// @param media_info is a MediaInfo containing information on the media.
|
|
||||||
/// @a media_info.bandwidth is required for 'static' profile. If @a
|
|
||||||
/// media_info.bandwidth is not present in 'dynamic' profile, this
|
|
||||||
/// tries to estimate it using the info passed to AddNewSegment().
|
|
||||||
/// @param mpd_options is options for the entire MPD.
|
|
||||||
/// @param representation_id is the numeric ID for the <Representation>.
|
|
||||||
/// @param state_change_listener is an event handler for state changes to
|
|
||||||
/// the representation. If null, no event handler registered.
|
|
||||||
Representation(
|
|
||||||
const MediaInfo& media_info,
|
|
||||||
const MpdOptions& mpd_options,
|
|
||||||
uint32_t representation_id,
|
|
||||||
std::unique_ptr<RepresentationStateChangeListener> state_change_listener);
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class AdaptationSet;
|
|
||||||
template <DashProfile profile>
|
|
||||||
friend class MpdBuilderTest;
|
|
||||||
|
|
||||||
bool AddLiveInfo(xml::RepresentationXmlNode* representation);
|
|
||||||
|
|
||||||
// Returns true if |media_info_| has required fields to generate a valid
|
|
||||||
// Representation. Otherwise returns false.
|
|
||||||
bool HasRequiredMediaInfoFields();
|
|
||||||
|
|
||||||
// Return false if the segment should be considered a new segment. True if the
|
|
||||||
// segment is contiguous.
|
|
||||||
bool IsContiguous(uint64_t start_time,
|
|
||||||
uint64_t duration,
|
|
||||||
uint64_t size) const;
|
|
||||||
|
|
||||||
// Remove elements from |segment_infos_| for dynamic live profile. Increments
|
|
||||||
// |start_number_| by the number of segments removed.
|
|
||||||
void SlideWindow();
|
|
||||||
|
|
||||||
// Note: Because 'mimeType' is a required field for a valid MPD, these return
|
|
||||||
// strings.
|
|
||||||
std::string GetVideoMimeType() const;
|
|
||||||
std::string GetAudioMimeType() const;
|
|
||||||
std::string GetTextMimeType() const;
|
|
||||||
|
|
||||||
// Gets the earliest, normalized segment timestamp. Returns true if
|
|
||||||
// successful, false otherwise.
|
|
||||||
bool GetEarliestTimestamp(double* timestamp_seconds);
|
|
||||||
|
|
||||||
// Init() checks that only one of VideoInfo, AudioInfo, or TextInfo is set. So
|
|
||||||
// any logic using this can assume only one set.
|
|
||||||
MediaInfo media_info_;
|
|
||||||
std::list<ContentProtectionElement> content_protection_elements_;
|
|
||||||
std::list<SegmentInfo> segment_infos_;
|
|
||||||
|
|
||||||
const uint32_t id_;
|
|
||||||
std::string mime_type_;
|
|
||||||
std::string codecs_;
|
|
||||||
BandwidthEstimator bandwidth_estimator_;
|
|
||||||
const MpdOptions& mpd_options_;
|
|
||||||
|
|
||||||
// startNumber attribute for SegmentTemplate.
|
|
||||||
// Starts from 1.
|
|
||||||
uint32_t start_number_;
|
|
||||||
|
|
||||||
// If this is not null, then Representation is responsible for calling the
|
|
||||||
// right methods at right timings.
|
|
||||||
std::unique_ptr<RepresentationStateChangeListener> state_change_listener_;
|
|
||||||
|
|
||||||
// Bit vector for tracking witch attributes should not be output.
|
|
||||||
int output_suppression_flags_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Representation);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace shaka
|
} // namespace shaka
|
||||||
|
|
|
@ -14,8 +14,12 @@
|
||||||
#include "packager/base/strings/string_piece.h"
|
#include "packager/base/strings/string_piece.h"
|
||||||
#include "packager/base/strings/string_util.h"
|
#include "packager/base/strings/string_util.h"
|
||||||
#include "packager/base/strings/stringprintf.h"
|
#include "packager/base/strings/stringprintf.h"
|
||||||
|
#include "packager/mpd/base/adaptation_set.h"
|
||||||
|
#include "packager/mpd/base/content_protection_element.h"
|
||||||
#include "packager/mpd/base/mpd_builder.h"
|
#include "packager/mpd/base/mpd_builder.h"
|
||||||
#include "packager/mpd/base/mpd_utils.h"
|
#include "packager/mpd/base/mpd_utils.h"
|
||||||
|
#include "packager/mpd/base/representation.h"
|
||||||
|
#include "packager/mpd/base/xml/scoped_xml_ptr.h"
|
||||||
#include "packager/mpd/test/mpd_builder_test_helper.h"
|
#include "packager/mpd/test/mpd_builder_test_helper.h"
|
||||||
#include "packager/mpd/test/xml_compare.h"
|
#include "packager/mpd/test/xml_compare.h"
|
||||||
#include "packager/version/version.h"
|
#include "packager/version/version.h"
|
||||||
|
|
|
@ -8,8 +8,13 @@
|
||||||
|
|
||||||
#include <libxml/tree.h>
|
#include <libxml/tree.h>
|
||||||
|
|
||||||
|
#include "packager/base/base64.h"
|
||||||
#include "packager/base/logging.h"
|
#include "packager/base/logging.h"
|
||||||
#include "packager/base/strings/string_number_conversions.h"
|
#include "packager/base/strings/string_number_conversions.h"
|
||||||
|
#include "packager/base/strings/string_util.h"
|
||||||
|
#include "packager/mpd/base/adaptation_set.h"
|
||||||
|
#include "packager/mpd/base/content_protection_element.h"
|
||||||
|
#include "packager/mpd/base/representation.h"
|
||||||
#include "packager/mpd/base/xml/scoped_xml_ptr.h"
|
#include "packager/mpd/base/xml/scoped_xml_ptr.h"
|
||||||
|
|
||||||
namespace shaka {
|
namespace shaka {
|
||||||
|
|
|
@ -14,15 +14,11 @@
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "packager/base/base64.h"
|
|
||||||
#include "packager/base/strings/string_util.h"
|
|
||||||
#include "packager/mpd/base/content_protection_element.h"
|
|
||||||
#include "packager/mpd/base/media_info.pb.h"
|
|
||||||
#include "packager/mpd/base/mpd_builder.h"
|
|
||||||
|
|
||||||
namespace shaka {
|
namespace shaka {
|
||||||
|
|
||||||
|
class AdaptationSet;
|
||||||
class MediaInfo;
|
class MediaInfo;
|
||||||
|
class Representation;
|
||||||
struct ContentProtectionElement;
|
struct ContentProtectionElement;
|
||||||
struct SegmentInfo;
|
struct SegmentInfo;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,466 @@
|
||||||
|
// Copyright 2017 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
|
||||||
|
#include "packager/mpd/base/representation.h"
|
||||||
|
|
||||||
|
#include "packager/base/logging.h"
|
||||||
|
#include "packager/mpd/base/mpd_utils.h"
|
||||||
|
#include "packager/mpd/base/xml/xml_node.h"
|
||||||
|
|
||||||
|
namespace shaka {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
std::string GetMimeType(const std::string& prefix,
|
||||||
|
MediaInfo::ContainerType container_type) {
|
||||||
|
switch (container_type) {
|
||||||
|
case MediaInfo::CONTAINER_MP4:
|
||||||
|
return prefix + "/mp4";
|
||||||
|
case MediaInfo::CONTAINER_MPEG2_TS:
|
||||||
|
// NOTE: DASH MPD spec uses lowercase but RFC3555 says uppercase.
|
||||||
|
return prefix + "/MP2T";
|
||||||
|
case MediaInfo::CONTAINER_WEBM:
|
||||||
|
return prefix + "/webm";
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unsupported container types should be rejected/handled by the caller.
|
||||||
|
LOG(ERROR) << "Unrecognized container type: " << container_type;
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether the video info has width and height.
|
||||||
|
// DASH IOP also requires several other fields for video representations, namely
|
||||||
|
// width, height, framerate, and sar.
|
||||||
|
bool HasRequiredVideoFields(const MediaInfo_VideoInfo& video_info) {
|
||||||
|
if (!video_info.has_height() || !video_info.has_width()) {
|
||||||
|
LOG(ERROR)
|
||||||
|
<< "Width and height are required fields for generating a valid MPD.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// These fields are not required for a valid MPD, but required for DASH IOP
|
||||||
|
// compliant MPD. MpdBuilder can keep generating MPDs without these fields.
|
||||||
|
LOG_IF(WARNING, !video_info.has_time_scale())
|
||||||
|
<< "Video info does not contain timescale required for "
|
||||||
|
"calculating framerate. @frameRate is required for DASH IOP.";
|
||||||
|
LOG_IF(WARNING, !video_info.has_pixel_width())
|
||||||
|
<< "Video info does not contain pixel_width to calculate the sample "
|
||||||
|
"aspect ratio required for DASH IOP.";
|
||||||
|
LOG_IF(WARNING, !video_info.has_pixel_height())
|
||||||
|
<< "Video info does not contain pixel_height to calculate the sample "
|
||||||
|
"aspect ratio required for DASH IOP.";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t GetTimeScale(const MediaInfo& media_info) {
|
||||||
|
if (media_info.has_reference_time_scale()) {
|
||||||
|
return media_info.reference_time_scale();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (media_info.has_video_info()) {
|
||||||
|
return media_info.video_info().time_scale();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (media_info.has_audio_info()) {
|
||||||
|
return media_info.audio_info().time_scale();
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG(WARNING) << "No timescale specified, using 1 as timescale.";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t LastSegmentStartTime(const SegmentInfo& segment_info) {
|
||||||
|
return segment_info.start_time + segment_info.duration * segment_info.repeat;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is equal to |segment_info| end time
|
||||||
|
uint64_t LastSegmentEndTime(const SegmentInfo& segment_info) {
|
||||||
|
return segment_info.start_time +
|
||||||
|
segment_info.duration * (segment_info.repeat + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t LatestSegmentStartTime(const std::list<SegmentInfo>& segments) {
|
||||||
|
DCHECK(!segments.empty());
|
||||||
|
const SegmentInfo& latest_segment = segments.back();
|
||||||
|
return LastSegmentStartTime(latest_segment);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given |timeshift_limit|, finds out the number of segments that are no longer
|
||||||
|
// valid and should be removed from |segment_info|.
|
||||||
|
int SearchTimedOutRepeatIndex(uint64_t timeshift_limit,
|
||||||
|
const SegmentInfo& segment_info) {
|
||||||
|
DCHECK_LE(timeshift_limit, LastSegmentEndTime(segment_info));
|
||||||
|
if (timeshift_limit < segment_info.start_time)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return (timeshift_limit - segment_info.start_time) / segment_info.duration;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Representation::Representation(
|
||||||
|
const MediaInfo& media_info,
|
||||||
|
const MpdOptions& mpd_options,
|
||||||
|
uint32_t id,
|
||||||
|
std::unique_ptr<RepresentationStateChangeListener> state_change_listener)
|
||||||
|
: media_info_(media_info),
|
||||||
|
id_(id),
|
||||||
|
bandwidth_estimator_(BandwidthEstimator::kUseAllBlocks),
|
||||||
|
mpd_options_(mpd_options),
|
||||||
|
start_number_(1),
|
||||||
|
state_change_listener_(std::move(state_change_listener)),
|
||||||
|
output_suppression_flags_(0) {}
|
||||||
|
|
||||||
|
Representation::~Representation() {}
|
||||||
|
|
||||||
|
bool Representation::Init() {
|
||||||
|
if (!AtLeastOneTrue(media_info_.has_video_info(),
|
||||||
|
media_info_.has_audio_info(),
|
||||||
|
media_info_.has_text_info())) {
|
||||||
|
// This is an error. Segment information can be in AdaptationSet, Period, or
|
||||||
|
// MPD but the interface does not provide a way to set them.
|
||||||
|
// See 5.3.9.1 ISO 23009-1:2012 for segment info.
|
||||||
|
LOG(ERROR) << "Representation needs one of video, audio, or text.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (MoreThanOneTrue(media_info_.has_video_info(),
|
||||||
|
media_info_.has_audio_info(),
|
||||||
|
media_info_.has_text_info())) {
|
||||||
|
LOG(ERROR) << "Only one of VideoInfo, AudioInfo, or TextInfo can be set.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (media_info_.container_type() == MediaInfo::CONTAINER_UNKNOWN) {
|
||||||
|
LOG(ERROR) << "'container_type' in MediaInfo cannot be CONTAINER_UNKNOWN.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (media_info_.has_video_info()) {
|
||||||
|
mime_type_ = GetVideoMimeType();
|
||||||
|
if (!HasRequiredVideoFields(media_info_.video_info())) {
|
||||||
|
LOG(ERROR) << "Missing required fields to create a video Representation.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (media_info_.has_audio_info()) {
|
||||||
|
mime_type_ = GetAudioMimeType();
|
||||||
|
} else if (media_info_.has_text_info()) {
|
||||||
|
mime_type_ = GetTextMimeType();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mime_type_.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
codecs_ = GetCodecs(media_info_);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Representation::AddContentProtectionElement(
|
||||||
|
const ContentProtectionElement& content_protection_element) {
|
||||||
|
content_protection_elements_.push_back(content_protection_element);
|
||||||
|
RemoveDuplicateAttributes(&content_protection_elements_.back());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Representation::UpdateContentProtectionPssh(const std::string& drm_uuid,
|
||||||
|
const std::string& pssh) {
|
||||||
|
UpdateContentProtectionPsshHelper(drm_uuid, pssh,
|
||||||
|
&content_protection_elements_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Representation::AddNewSegment(uint64_t start_time,
|
||||||
|
uint64_t duration,
|
||||||
|
uint64_t size) {
|
||||||
|
if (start_time == 0 && duration == 0) {
|
||||||
|
LOG(WARNING) << "Got segment with start_time and duration == 0. Ignoring.";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state_change_listener_)
|
||||||
|
state_change_listener_->OnNewSegmentForRepresentation(start_time, duration);
|
||||||
|
if (IsContiguous(start_time, duration, size)) {
|
||||||
|
++segment_infos_.back().repeat;
|
||||||
|
} else {
|
||||||
|
SegmentInfo s = {start_time, duration, /* Not repeat. */ 0};
|
||||||
|
segment_infos_.push_back(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
bandwidth_estimator_.AddBlock(
|
||||||
|
size, static_cast<double>(duration) / media_info_.reference_time_scale());
|
||||||
|
|
||||||
|
SlideWindow();
|
||||||
|
DCHECK_GE(segment_infos_.size(), 1u);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Representation::SetSampleDuration(uint32_t sample_duration) {
|
||||||
|
if (media_info_.has_video_info()) {
|
||||||
|
media_info_.mutable_video_info()->set_frame_duration(sample_duration);
|
||||||
|
if (state_change_listener_) {
|
||||||
|
state_change_listener_->OnSetFrameRateForRepresentation(
|
||||||
|
sample_duration, media_info_.video_info().time_scale());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uses info in |media_info_| and |content_protection_elements_| to create a
|
||||||
|
// "Representation" node.
|
||||||
|
// MPD schema has strict ordering. The following must be done in order.
|
||||||
|
// AddVideoInfo() (possibly adds FramePacking elements), AddAudioInfo() (Adds
|
||||||
|
// AudioChannelConfig elements), AddContentProtectionElements*(), and
|
||||||
|
// AddVODOnlyInfo() (Adds segment info).
|
||||||
|
xml::scoped_xml_ptr<xmlNode> Representation::GetXml() {
|
||||||
|
if (!HasRequiredMediaInfoFields()) {
|
||||||
|
LOG(ERROR) << "MediaInfo missing required fields.";
|
||||||
|
return xml::scoped_xml_ptr<xmlNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint64_t bandwidth = media_info_.has_bandwidth()
|
||||||
|
? media_info_.bandwidth()
|
||||||
|
: bandwidth_estimator_.Estimate();
|
||||||
|
|
||||||
|
DCHECK(!(HasVODOnlyFields(media_info_) && HasLiveOnlyFields(media_info_)));
|
||||||
|
|
||||||
|
xml::RepresentationXmlNode representation;
|
||||||
|
// Mandatory fields for Representation.
|
||||||
|
representation.SetId(id_);
|
||||||
|
representation.SetIntegerAttribute("bandwidth", bandwidth);
|
||||||
|
if (!codecs_.empty())
|
||||||
|
representation.SetStringAttribute("codecs", codecs_);
|
||||||
|
representation.SetStringAttribute("mimeType", mime_type_);
|
||||||
|
|
||||||
|
const bool has_video_info = media_info_.has_video_info();
|
||||||
|
const bool has_audio_info = media_info_.has_audio_info();
|
||||||
|
|
||||||
|
if (has_video_info &&
|
||||||
|
!representation.AddVideoInfo(
|
||||||
|
media_info_.video_info(),
|
||||||
|
!(output_suppression_flags_ & kSuppressWidth),
|
||||||
|
!(output_suppression_flags_ & kSuppressHeight),
|
||||||
|
!(output_suppression_flags_ & kSuppressFrameRate))) {
|
||||||
|
LOG(ERROR) << "Failed to add video info to Representation XML.";
|
||||||
|
return xml::scoped_xml_ptr<xmlNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (has_audio_info &&
|
||||||
|
!representation.AddAudioInfo(media_info_.audio_info())) {
|
||||||
|
LOG(ERROR) << "Failed to add audio info to Representation XML.";
|
||||||
|
return xml::scoped_xml_ptr<xmlNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!representation.AddContentProtectionElements(
|
||||||
|
content_protection_elements_)) {
|
||||||
|
return xml::scoped_xml_ptr<xmlNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set media duration for static mpd.
|
||||||
|
if (mpd_options_.mpd_type == MpdType::kStatic &&
|
||||||
|
media_info_.has_media_duration_seconds()) {
|
||||||
|
// Adding 'duration' attribute, so that this information can be used when
|
||||||
|
// generating one MPD file. This should be removed from the final MPD.
|
||||||
|
representation.SetFloatingPointAttribute(
|
||||||
|
"duration", media_info_.media_duration_seconds());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HasVODOnlyFields(media_info_) &&
|
||||||
|
!representation.AddVODOnlyInfo(media_info_)) {
|
||||||
|
LOG(ERROR) << "Failed to add VOD segment info.";
|
||||||
|
return xml::scoped_xml_ptr<xmlNode>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HasLiveOnlyFields(media_info_) &&
|
||||||
|
!representation.AddLiveOnlyInfo(media_info_, segment_infos_,
|
||||||
|
start_number_)) {
|
||||||
|
LOG(ERROR) << "Failed to add Live info.";
|
||||||
|
return xml::scoped_xml_ptr<xmlNode>();
|
||||||
|
}
|
||||||
|
// TODO(rkuroiwa): It is likely that all representations have the exact same
|
||||||
|
// SegmentTemplate. Optimize and propagate the tag up to AdaptationSet level.
|
||||||
|
|
||||||
|
output_suppression_flags_ = 0;
|
||||||
|
return representation.PassScopedPtr();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Representation::SuppressOnce(SuppressFlag flag) {
|
||||||
|
output_suppression_flags_ |= flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Representation::HasRequiredMediaInfoFields() {
|
||||||
|
if (HasVODOnlyFields(media_info_) && HasLiveOnlyFields(media_info_)) {
|
||||||
|
LOG(ERROR) << "MediaInfo cannot have both VOD and Live fields.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!media_info_.has_container_type()) {
|
||||||
|
LOG(ERROR) << "MediaInfo missing required field: container_type.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HasVODOnlyFields(media_info_) && !media_info_.has_bandwidth()) {
|
||||||
|
LOG(ERROR) << "Missing 'bandwidth' field. MediaInfo requires bandwidth for "
|
||||||
|
"static profile for generating a valid MPD.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
VLOG_IF(3, HasLiveOnlyFields(media_info_) && !media_info_.has_bandwidth())
|
||||||
|
<< "MediaInfo missing field 'bandwidth'. Using estimated from "
|
||||||
|
"segment size.";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Representation::IsContiguous(uint64_t start_time,
|
||||||
|
uint64_t duration,
|
||||||
|
uint64_t size) const {
|
||||||
|
if (segment_infos_.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Contiguous segment.
|
||||||
|
const SegmentInfo& previous = segment_infos_.back();
|
||||||
|
const uint64_t previous_segment_end_time =
|
||||||
|
previous.start_time + previous.duration * (previous.repeat + 1);
|
||||||
|
if (previous_segment_end_time == start_time &&
|
||||||
|
segment_infos_.back().duration == duration) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No out of order segments.
|
||||||
|
const uint64_t previous_segment_start_time =
|
||||||
|
previous.start_time + previous.duration * previous.repeat;
|
||||||
|
if (previous_segment_start_time >= start_time) {
|
||||||
|
LOG(ERROR) << "Segments should not be out of order segment. Adding segment "
|
||||||
|
"with start_time == "
|
||||||
|
<< start_time << " but the previous segment starts at "
|
||||||
|
<< previous_segment_start_time << ".";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A gap since previous.
|
||||||
|
const uint64_t kRoundingErrorGrace = 5;
|
||||||
|
if (previous_segment_end_time + kRoundingErrorGrace < start_time) {
|
||||||
|
LOG(WARNING) << "Found a gap of size "
|
||||||
|
<< (start_time - previous_segment_end_time)
|
||||||
|
<< " > kRoundingErrorGrace (" << kRoundingErrorGrace
|
||||||
|
<< "). The new segment starts at " << start_time
|
||||||
|
<< " but the previous segment ends at "
|
||||||
|
<< previous_segment_end_time << ".";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No overlapping segments.
|
||||||
|
if (start_time < previous_segment_end_time - kRoundingErrorGrace) {
|
||||||
|
LOG(WARNING)
|
||||||
|
<< "Segments should not be overlapping. The new segment starts at "
|
||||||
|
<< start_time << " but the previous segment ends at "
|
||||||
|
<< previous_segment_end_time << ".";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Within rounding error grace but technically not contiguous in terms of MPD.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Representation::SlideWindow() {
|
||||||
|
DCHECK(!segment_infos_.empty());
|
||||||
|
if (mpd_options_.mpd_params.time_shift_buffer_depth <= 0.0 ||
|
||||||
|
mpd_options_.mpd_type == MpdType::kStatic)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const uint32_t time_scale = GetTimeScale(media_info_);
|
||||||
|
DCHECK_GT(time_scale, 0u);
|
||||||
|
|
||||||
|
uint64_t time_shift_buffer_depth = static_cast<uint64_t>(
|
||||||
|
mpd_options_.mpd_params.time_shift_buffer_depth * time_scale);
|
||||||
|
|
||||||
|
// The start time of the latest segment is considered the current_play_time,
|
||||||
|
// and this should guarantee that the latest segment will stay in the list.
|
||||||
|
const uint64_t current_play_time = LatestSegmentStartTime(segment_infos_);
|
||||||
|
if (current_play_time <= time_shift_buffer_depth)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const uint64_t timeshift_limit = current_play_time - time_shift_buffer_depth;
|
||||||
|
|
||||||
|
// First remove all the SegmentInfos that are completely out of range, by
|
||||||
|
// looking at the very last segment's end time.
|
||||||
|
std::list<SegmentInfo>::iterator first = segment_infos_.begin();
|
||||||
|
std::list<SegmentInfo>::iterator last = first;
|
||||||
|
size_t num_segments_removed = 0;
|
||||||
|
for (; last != segment_infos_.end(); ++last) {
|
||||||
|
const uint64_t last_segment_end_time = LastSegmentEndTime(*last);
|
||||||
|
if (timeshift_limit < last_segment_end_time)
|
||||||
|
break;
|
||||||
|
num_segments_removed += last->repeat + 1;
|
||||||
|
}
|
||||||
|
segment_infos_.erase(first, last);
|
||||||
|
start_number_ += num_segments_removed;
|
||||||
|
|
||||||
|
// Now some segment in the first SegmentInfo should be left in the list.
|
||||||
|
SegmentInfo* first_segment_info = &segment_infos_.front();
|
||||||
|
DCHECK_LE(timeshift_limit, LastSegmentEndTime(*first_segment_info));
|
||||||
|
|
||||||
|
// Identify which segments should still be in the SegmentInfo.
|
||||||
|
const int repeat_index =
|
||||||
|
SearchTimedOutRepeatIndex(timeshift_limit, *first_segment_info);
|
||||||
|
CHECK_GE(repeat_index, 0);
|
||||||
|
if (repeat_index == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
first_segment_info->start_time = first_segment_info->start_time +
|
||||||
|
first_segment_info->duration * repeat_index;
|
||||||
|
|
||||||
|
first_segment_info->repeat = first_segment_info->repeat - repeat_index;
|
||||||
|
start_number_ += repeat_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Representation::GetVideoMimeType() const {
|
||||||
|
return GetMimeType("video", media_info_.container_type());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Representation::GetAudioMimeType() const {
|
||||||
|
return GetMimeType("audio", media_info_.container_type());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Representation::GetTextMimeType() const {
|
||||||
|
CHECK(media_info_.has_text_info());
|
||||||
|
if (media_info_.text_info().format() == "ttml") {
|
||||||
|
switch (media_info_.container_type()) {
|
||||||
|
case MediaInfo::CONTAINER_TEXT:
|
||||||
|
return "application/ttml+xml";
|
||||||
|
case MediaInfo::CONTAINER_MP4:
|
||||||
|
return "application/mp4";
|
||||||
|
default:
|
||||||
|
LOG(ERROR) << "Failed to determine MIME type for TTML container: "
|
||||||
|
<< media_info_.container_type();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (media_info_.text_info().format() == "vtt") {
|
||||||
|
if (media_info_.container_type() == MediaInfo::CONTAINER_TEXT) {
|
||||||
|
return "text/vtt";
|
||||||
|
} else if (media_info_.container_type() == MediaInfo::CONTAINER_MP4) {
|
||||||
|
return "application/mp4";
|
||||||
|
}
|
||||||
|
LOG(ERROR) << "Failed to determine MIME type for VTT container: "
|
||||||
|
<< media_info_.container_type();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG(ERROR) << "Cannot determine MIME type for format: "
|
||||||
|
<< media_info_.text_info().format()
|
||||||
|
<< " container: " << media_info_.container_type();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Representation::GetEarliestTimestamp(double* timestamp_seconds) {
|
||||||
|
DCHECK(timestamp_seconds);
|
||||||
|
|
||||||
|
if (segment_infos_.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
*timestamp_seconds = static_cast<double>(segment_infos_.begin()->start_time) /
|
||||||
|
GetTimeScale(media_info_);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace shaka
|
|
@ -0,0 +1,201 @@
|
||||||
|
// Copyright 2017 Google Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file or at
|
||||||
|
// https://developers.google.com/open-source/licenses/bsd
|
||||||
|
//
|
||||||
|
/// All the methods that are virtual are virtual for mocking.
|
||||||
|
|
||||||
|
#include "packager/mpd/base/bandwidth_estimator.h"
|
||||||
|
#include "packager/mpd/base/media_info.pb.h"
|
||||||
|
#include "packager/mpd/base/mpd_options.h"
|
||||||
|
#include "packager/mpd/base/segment_info.h"
|
||||||
|
#include "packager/mpd/base/xml/scoped_xml_ptr.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace shaka {
|
||||||
|
|
||||||
|
struct ContentProtectionElement;
|
||||||
|
|
||||||
|
namespace xml {
|
||||||
|
class XmlNode;
|
||||||
|
class RepresentationXmlNode;
|
||||||
|
} // namespace xml
|
||||||
|
|
||||||
|
class RepresentationStateChangeListener {
|
||||||
|
public:
|
||||||
|
RepresentationStateChangeListener() {}
|
||||||
|
virtual ~RepresentationStateChangeListener() {}
|
||||||
|
|
||||||
|
/// Notifies the instance that a new (sub)segment was added to
|
||||||
|
/// the Representation.
|
||||||
|
/// @param start_time is the start time of the new segment.
|
||||||
|
/// @param duration is the duration of the new segment.
|
||||||
|
virtual void OnNewSegmentForRepresentation(uint64_t start_time,
|
||||||
|
uint64_t duration) = 0;
|
||||||
|
|
||||||
|
/// Notifies the instance that the frame rate was set for the
|
||||||
|
/// Representation.
|
||||||
|
/// @param frame_duration is the duration of a frame.
|
||||||
|
/// @param timescale is the timescale of the Representation.
|
||||||
|
virtual void OnSetFrameRateForRepresentation(uint32_t frame_duration,
|
||||||
|
uint32_t timescale) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Representation class contains references to a single media stream, as
|
||||||
|
/// well as optional ContentProtection elements for that stream.
|
||||||
|
class Representation {
|
||||||
|
public:
|
||||||
|
enum SuppressFlag {
|
||||||
|
kSuppressWidth = 1,
|
||||||
|
kSuppressHeight = 2,
|
||||||
|
kSuppressFrameRate = 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
virtual ~Representation();
|
||||||
|
|
||||||
|
/// Tries to initialize the instance. If this does not succeed, the instance
|
||||||
|
/// should not be used.
|
||||||
|
/// @return true on success, false otherwise.
|
||||||
|
bool Init();
|
||||||
|
|
||||||
|
/// Add a ContenProtection element to the representation.
|
||||||
|
/// Representation does not add <ContentProtection> elements
|
||||||
|
/// automatically to itself even if @a media_info passed to
|
||||||
|
/// AdaptationSet::AddRepresentation() has @a media_info.protected_content
|
||||||
|
/// populated. This is because some MPDs should have the elements at
|
||||||
|
/// AdaptationSet level and some at Representation level.
|
||||||
|
/// @param element contains the ContentProtection element contents.
|
||||||
|
/// If @a element has {value, schemeIdUri} set and has
|
||||||
|
/// {“value”, “schemeIdUri”} as key for @a additional_attributes,
|
||||||
|
/// then the former is used.
|
||||||
|
virtual void AddContentProtectionElement(
|
||||||
|
const ContentProtectionElement& element);
|
||||||
|
|
||||||
|
/// Update the 'cenc:pssh' element for @a drm_uuid ContentProtection element.
|
||||||
|
/// If the element does not exist, this will add one.
|
||||||
|
/// @param drm_uuid is the UUID of the DRM for encryption.
|
||||||
|
/// @param pssh is the content of <cenc:pssh> element.
|
||||||
|
/// Note that DASH IF IOP mentions that this should be base64 encoded
|
||||||
|
/// string of the whole pssh box.
|
||||||
|
/// @attention This might get removed once DASH IF IOP specification makes a
|
||||||
|
/// a clear guideline on how to handle key rotation. Also to get
|
||||||
|
/// this working with shaka-player, this method *DOES NOT* update
|
||||||
|
/// the PSSH element. Instead, it removes the element regardless of
|
||||||
|
/// the content of @a pssh.
|
||||||
|
virtual void UpdateContentProtectionPssh(const std::string& drm_uuid,
|
||||||
|
const std::string& pssh);
|
||||||
|
|
||||||
|
/// Add a media (sub)segment to the representation.
|
||||||
|
/// AdaptationSet@{subsegmentAlignment,segmentAlignment} cannot be set
|
||||||
|
/// if this is not called for all Representations.
|
||||||
|
/// @param start_time is the start time for the (sub)segment, in units of the
|
||||||
|
/// stream's time scale.
|
||||||
|
/// @param duration is the duration of the segment, in units of the stream's
|
||||||
|
/// time scale.
|
||||||
|
/// @param size of the segment in bytes.
|
||||||
|
virtual void AddNewSegment(uint64_t start_time,
|
||||||
|
uint64_t duration,
|
||||||
|
uint64_t size);
|
||||||
|
|
||||||
|
/// Set the sample duration of this Representation.
|
||||||
|
/// Sample duration is not available right away especially for live. This
|
||||||
|
/// allows setting the sample duration after the Representation has been
|
||||||
|
/// initialized.
|
||||||
|
/// @param sample_duration is the duration of a sample.
|
||||||
|
virtual void SetSampleDuration(uint32_t sample_duration);
|
||||||
|
|
||||||
|
/// @return Copy of <Representation>.
|
||||||
|
xml::scoped_xml_ptr<xmlNode> GetXml();
|
||||||
|
|
||||||
|
/// By calling this methods, the next time GetXml() is
|
||||||
|
/// called, the corresponding attributes will not be set.
|
||||||
|
/// For example, if SuppressOnce(kSuppressWidth) is called, then GetXml() will
|
||||||
|
/// return a <Representation> element without a @width attribute.
|
||||||
|
/// Note that it only applies to the next call to GetXml(), calling GetXml()
|
||||||
|
/// again without calling this methods will return a <Representation> element
|
||||||
|
/// with the attribute.
|
||||||
|
/// This may be called multiple times to set different (or the same) flags.
|
||||||
|
void SuppressOnce(SuppressFlag flag);
|
||||||
|
|
||||||
|
/// @return ID number for <Representation>.
|
||||||
|
uint32_t id() const { return id_; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/// @param media_info is a MediaInfo containing information on the media.
|
||||||
|
/// @a media_info.bandwidth is required for 'static' profile. If @a
|
||||||
|
/// media_info.bandwidth is not present in 'dynamic' profile, this
|
||||||
|
/// tries to estimate it using the info passed to AddNewSegment().
|
||||||
|
/// @param mpd_options is options for the entire MPD.
|
||||||
|
/// @param representation_id is the numeric ID for the <Representation>.
|
||||||
|
/// @param state_change_listener is an event handler for state changes to
|
||||||
|
/// the representation. If null, no event handler registered.
|
||||||
|
Representation(
|
||||||
|
const MediaInfo& media_info,
|
||||||
|
const MpdOptions& mpd_options,
|
||||||
|
uint32_t representation_id,
|
||||||
|
std::unique_ptr<RepresentationStateChangeListener> state_change_listener);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Representation(const Representation&) = delete;
|
||||||
|
Representation& operator=(const Representation&) = delete;
|
||||||
|
|
||||||
|
friend class AdaptationSet;
|
||||||
|
template <DashProfile profile>
|
||||||
|
friend class MpdBuilderTest;
|
||||||
|
|
||||||
|
bool AddLiveInfo(xml::RepresentationXmlNode* representation);
|
||||||
|
|
||||||
|
// Returns true if |media_info_| has required fields to generate a valid
|
||||||
|
// Representation. Otherwise returns false.
|
||||||
|
bool HasRequiredMediaInfoFields();
|
||||||
|
|
||||||
|
// Return false if the segment should be considered a new segment. True if the
|
||||||
|
// segment is contiguous.
|
||||||
|
bool IsContiguous(uint64_t start_time,
|
||||||
|
uint64_t duration,
|
||||||
|
uint64_t size) const;
|
||||||
|
|
||||||
|
// Remove elements from |segment_infos_| for dynamic live profile. Increments
|
||||||
|
// |start_number_| by the number of segments removed.
|
||||||
|
void SlideWindow();
|
||||||
|
|
||||||
|
// Note: Because 'mimeType' is a required field for a valid MPD, these return
|
||||||
|
// strings.
|
||||||
|
std::string GetVideoMimeType() const;
|
||||||
|
std::string GetAudioMimeType() const;
|
||||||
|
std::string GetTextMimeType() const;
|
||||||
|
|
||||||
|
// Gets the earliest, normalized segment timestamp. Returns true if
|
||||||
|
// successful, false otherwise.
|
||||||
|
bool GetEarliestTimestamp(double* timestamp_seconds);
|
||||||
|
|
||||||
|
// Init() checks that only one of VideoInfo, AudioInfo, or TextInfo is set. So
|
||||||
|
// any logic using this can assume only one set.
|
||||||
|
MediaInfo media_info_;
|
||||||
|
std::list<ContentProtectionElement> content_protection_elements_;
|
||||||
|
std::list<SegmentInfo> segment_infos_;
|
||||||
|
|
||||||
|
const uint32_t id_;
|
||||||
|
std::string mime_type_;
|
||||||
|
std::string codecs_;
|
||||||
|
BandwidthEstimator bandwidth_estimator_;
|
||||||
|
const MpdOptions& mpd_options_;
|
||||||
|
|
||||||
|
// startNumber attribute for SegmentTemplate.
|
||||||
|
// Starts from 1.
|
||||||
|
uint32_t start_number_;
|
||||||
|
|
||||||
|
// If this is not null, then Representation is responsible for calling the
|
||||||
|
// right methods at right timings.
|
||||||
|
std::unique_ptr<RepresentationStateChangeListener> state_change_listener_;
|
||||||
|
|
||||||
|
// Bit vector for tracking witch attributes should not be output.
|
||||||
|
int output_suppression_flags_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace shaka
|
|
@ -8,9 +8,11 @@
|
||||||
|
|
||||||
#include "packager/base/logging.h"
|
#include "packager/base/logging.h"
|
||||||
#include "packager/base/stl_util.h"
|
#include "packager/base/stl_util.h"
|
||||||
|
#include "packager/mpd/base/adaptation_set.h"
|
||||||
#include "packager/mpd/base/mpd_builder.h"
|
#include "packager/mpd/base/mpd_builder.h"
|
||||||
#include "packager/mpd/base/mpd_notifier_util.h"
|
#include "packager/mpd/base/mpd_notifier_util.h"
|
||||||
#include "packager/mpd/base/mpd_utils.h"
|
#include "packager/mpd/base/mpd_utils.h"
|
||||||
|
#include "packager/mpd/base/representation.h"
|
||||||
|
|
||||||
namespace shaka {
|
namespace shaka {
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
#include "packager/base/logging.h"
|
#include "packager/base/logging.h"
|
||||||
#include "packager/base/strings/string_util.h"
|
#include "packager/base/strings/string_util.h"
|
||||||
#include "packager/mpd/base/mpd_builder.h"
|
#include "packager/mpd/base/segment_info.h"
|
||||||
#include "packager/mpd/base/xml/xml_node.h"
|
#include "packager/mpd/base/xml/xml_node.h"
|
||||||
#include "packager/mpd/test/xml_compare.h"
|
#include "packager/mpd/test/xml_compare.h"
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,8 @@
|
||||||
'target_name': 'mpd_builder',
|
'target_name': 'mpd_builder',
|
||||||
'type': 'static_library',
|
'type': 'static_library',
|
||||||
'sources': [
|
'sources': [
|
||||||
|
'base/adaptation_set.cc',
|
||||||
|
'base/adaptation_set.h',
|
||||||
'base/bandwidth_estimator.cc',
|
'base/bandwidth_estimator.cc',
|
||||||
'base/bandwidth_estimator.h',
|
'base/bandwidth_estimator.h',
|
||||||
'base/content_protection_element.cc',
|
'base/content_protection_element.cc',
|
||||||
|
@ -41,6 +43,8 @@
|
||||||
'base/mpd_options.h',
|
'base/mpd_options.h',
|
||||||
'base/mpd_utils.cc',
|
'base/mpd_utils.cc',
|
||||||
'base/mpd_utils.h',
|
'base/mpd_utils.h',
|
||||||
|
'base/representation.cc',
|
||||||
|
'base/representation.h',
|
||||||
'base/segment_info.h',
|
'base/segment_info.h',
|
||||||
'base/simple_mpd_notifier.cc',
|
'base/simple_mpd_notifier.cc',
|
||||||
'base/simple_mpd_notifier.h',
|
'base/simple_mpd_notifier.h',
|
||||||
|
|
Loading…
Reference in New Issue