2017-12-13 01:26:37 +00:00
|
|
|
// 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"
|
|
|
|
|
2018-01-29 18:37:50 +00:00
|
|
|
#include <gflags/gflags.h>
|
|
|
|
|
2017-09-25 19:18:50 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-12-13 01:26:37 +00:00
|
|
|
#include "packager/base/logging.h"
|
2019-03-26 22:16:30 +00:00
|
|
|
#include "packager/base/strings/stringprintf.h"
|
2018-04-04 23:17:59 +00:00
|
|
|
#include "packager/file/file.h"
|
|
|
|
#include "packager/media/base/muxer_util.h"
|
2017-12-14 01:00:11 +00:00
|
|
|
#include "packager/mpd/base/mpd_options.h"
|
2017-12-13 01:26:37 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // 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),
|
|
|
|
mpd_options_(mpd_options),
|
|
|
|
state_change_listener_(std::move(state_change_listener)),
|
2017-09-25 19:18:50 +00:00
|
|
|
allow_approximate_segment_timeline_(
|
|
|
|
// TODO(kqyang): Need a better check. $Time is legitimate but not a
|
|
|
|
// template.
|
|
|
|
media_info.segment_template().find("$Time") == std::string::npos &&
|
|
|
|
mpd_options_.mpd_params.allow_approximate_segment_timeline) {}
|
2017-12-13 01:26:37 +00:00
|
|
|
|
2018-01-03 00:10:54 +00:00
|
|
|
Representation::Representation(
|
|
|
|
const Representation& representation,
|
|
|
|
std::unique_ptr<RepresentationStateChangeListener> state_change_listener)
|
|
|
|
: Representation(representation.media_info_,
|
|
|
|
representation.mpd_options_,
|
|
|
|
representation.id_,
|
|
|
|
std::move(state_change_listener)) {
|
|
|
|
mime_type_ = representation.mime_type_;
|
|
|
|
codecs_ = representation.codecs_;
|
|
|
|
|
|
|
|
start_number_ = representation.start_number_;
|
|
|
|
for (const SegmentInfo& segment_info : representation.segment_infos_)
|
|
|
|
start_number_ += segment_info.repeat + 1;
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:26:37 +00:00
|
|
|
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_);
|
|
|
|
}
|
|
|
|
|
2018-06-22 01:14:34 +00:00
|
|
|
void Representation::AddNewSegment(int64_t start_time,
|
|
|
|
int64_t duration,
|
2017-12-13 01:26:37 +00:00
|
|
|
uint64_t size) {
|
|
|
|
if (start_time == 0 && duration == 0) {
|
|
|
|
LOG(WARNING) << "Got segment with start_time and duration == 0. Ignoring.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-22 00:12:30 +00:00
|
|
|
// In order for the oldest segment to be accessible for at least
|
|
|
|
// |time_shift_buffer_depth| seconds, the latest segment should not be in the
|
|
|
|
// sliding window since the player could be playing any part of the latest
|
|
|
|
// segment. So the current segment duration is added to the sum of segment
|
|
|
|
// durations (in the manifest/playlist) after sliding the window.
|
|
|
|
SlideWindow();
|
|
|
|
|
2017-12-13 01:26:37 +00:00
|
|
|
if (state_change_listener_)
|
|
|
|
state_change_listener_->OnNewSegmentForRepresentation(start_time, duration);
|
2017-09-25 19:18:50 +00:00
|
|
|
|
|
|
|
AddSegmentInfo(start_time, duration);
|
2019-03-22 00:12:30 +00:00
|
|
|
current_buffer_depth_ += segment_infos_.back().duration;
|
2017-12-13 01:26:37 +00:00
|
|
|
|
|
|
|
bandwidth_estimator_.AddBlock(
|
|
|
|
size, static_cast<double>(duration) / media_info_.reference_time_scale());
|
|
|
|
}
|
|
|
|
|
2017-09-25 19:18:50 +00:00
|
|
|
void Representation::SetSampleDuration(uint32_t frame_duration) {
|
|
|
|
// Sample duration is used to generate approximate SegmentTimeline.
|
|
|
|
// Text is required to have exactly the same segment duration.
|
|
|
|
if (media_info_.has_audio_info() || media_info_.has_video_info())
|
|
|
|
frame_duration_ = frame_duration;
|
|
|
|
|
2017-12-13 01:26:37 +00:00
|
|
|
if (media_info_.has_video_info()) {
|
2017-09-25 19:18:50 +00:00
|
|
|
media_info_.mutable_video_info()->set_frame_duration(frame_duration);
|
2017-12-13 01:26:37 +00:00
|
|
|
if (state_change_listener_) {
|
|
|
|
state_change_listener_->OnSetFrameRateForRepresentation(
|
2017-09-25 19:18:50 +00:00
|
|
|
frame_duration, media_info_.video_info().time_scale());
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 00:10:54 +00:00
|
|
|
const MediaInfo& Representation::GetMediaInfo() const {
|
|
|
|
return media_info_;
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:26:37 +00:00
|
|
|
// 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).
|
2020-11-10 00:32:58 +00:00
|
|
|
base::Optional<xml::XmlNode> Representation::GetXml() {
|
2017-12-13 01:26:37 +00:00
|
|
|
if (!HasRequiredMediaInfoFields()) {
|
|
|
|
LOG(ERROR) << "MediaInfo missing required fields.";
|
2020-11-10 00:32:58 +00:00
|
|
|
return base::nullopt;
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint64_t bandwidth = media_info_.has_bandwidth()
|
|
|
|
? media_info_.bandwidth()
|
2018-04-13 18:48:11 +00:00
|
|
|
: bandwidth_estimator_.Max();
|
2017-12-13 01:26:37 +00:00
|
|
|
|
|
|
|
DCHECK(!(HasVODOnlyFields(media_info_) && HasLiveOnlyFields(media_info_)));
|
|
|
|
|
|
|
|
xml::RepresentationXmlNode representation;
|
|
|
|
// Mandatory fields for Representation.
|
2020-11-10 00:32:58 +00:00
|
|
|
if (!representation.SetId(id_) ||
|
|
|
|
!representation.SetIntegerAttribute("bandwidth", bandwidth) ||
|
|
|
|
!(codecs_.empty() ||
|
|
|
|
representation.SetStringAttribute("codecs", codecs_)) ||
|
|
|
|
!representation.SetStringAttribute("mimeType", mime_type_)) {
|
|
|
|
return base::nullopt;
|
|
|
|
}
|
2017-12-13 01:26:37 +00:00
|
|
|
|
|
|
|
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.";
|
2020-11-10 00:32:58 +00:00
|
|
|
return base::nullopt;
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (has_audio_info &&
|
|
|
|
!representation.AddAudioInfo(media_info_.audio_info())) {
|
|
|
|
LOG(ERROR) << "Failed to add audio info to Representation XML.";
|
2020-11-10 00:32:58 +00:00
|
|
|
return base::nullopt;
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!representation.AddContentProtectionElements(
|
|
|
|
content_protection_elements_)) {
|
2020-11-10 00:32:58 +00:00
|
|
|
return base::nullopt;
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (HasVODOnlyFields(media_info_) &&
|
2021-05-25 19:08:58 +00:00
|
|
|
!representation.AddVODOnlyInfo(
|
|
|
|
media_info_, mpd_options_.mpd_params.use_segment_list,
|
|
|
|
mpd_options_.mpd_params.target_segment_duration)) {
|
2018-05-22 00:39:21 +00:00
|
|
|
LOG(ERROR) << "Failed to add VOD info.";
|
2020-11-10 00:32:58 +00:00
|
|
|
return base::nullopt;
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (HasLiveOnlyFields(media_info_) &&
|
|
|
|
!representation.AddLiveOnlyInfo(media_info_, segment_infos_,
|
|
|
|
start_number_)) {
|
|
|
|
LOG(ERROR) << "Failed to add Live info.";
|
2020-11-10 00:32:58 +00:00
|
|
|
return base::nullopt;
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
// 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;
|
2020-11-10 00:32:58 +00:00
|
|
|
return std::move(representation);
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Representation::SuppressOnce(SuppressFlag flag) {
|
|
|
|
output_suppression_flags_ |= flag;
|
|
|
|
}
|
|
|
|
|
2018-01-29 18:37:50 +00:00
|
|
|
void Representation::SetPresentationTimeOffset(
|
|
|
|
double presentation_time_offset) {
|
2018-06-22 01:14:34 +00:00
|
|
|
int64_t pto = presentation_time_offset * media_info_.reference_time_scale();
|
2018-01-29 18:37:50 +00:00
|
|
|
if (pto <= 0)
|
|
|
|
return;
|
|
|
|
media_info_.set_presentation_time_offset(pto);
|
|
|
|
}
|
2018-01-05 02:31:27 +00:00
|
|
|
|
2018-01-29 18:37:50 +00:00
|
|
|
bool Representation::GetStartAndEndTimestamps(
|
|
|
|
double* start_timestamp_seconds,
|
|
|
|
double* end_timestamp_seconds) const {
|
2018-01-05 02:31:27 +00:00
|
|
|
if (segment_infos_.empty())
|
|
|
|
return false;
|
|
|
|
|
2018-01-29 18:37:50 +00:00
|
|
|
if (start_timestamp_seconds) {
|
|
|
|
*start_timestamp_seconds =
|
|
|
|
static_cast<double>(segment_infos_.begin()->start_time) /
|
|
|
|
GetTimeScale(media_info_);
|
|
|
|
}
|
|
|
|
if (end_timestamp_seconds) {
|
|
|
|
*end_timestamp_seconds =
|
|
|
|
static_cast<double>(segment_infos_.rbegin()->start_time +
|
|
|
|
segment_infos_.rbegin()->duration *
|
|
|
|
(segment_infos_.rbegin()->repeat + 1)) /
|
|
|
|
GetTimeScale(media_info_);
|
|
|
|
}
|
2018-01-05 02:31:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-24 23:26:37 +00:00
|
|
|
bool Representation::HasRequiredMediaInfoFields() const {
|
2017-12-13 01:26:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-22 01:14:34 +00:00
|
|
|
void Representation::AddSegmentInfo(int64_t start_time, int64_t duration) {
|
2017-09-25 19:18:50 +00:00
|
|
|
const uint64_t kNoRepeat = 0;
|
2018-06-22 01:14:34 +00:00
|
|
|
const int64_t adjusted_duration = AdjustDuration(duration);
|
2017-09-25 19:18:50 +00:00
|
|
|
|
|
|
|
if (!segment_infos_.empty()) {
|
|
|
|
// Contiguous segment.
|
|
|
|
const SegmentInfo& previous = segment_infos_.back();
|
2018-06-22 01:14:34 +00:00
|
|
|
const int64_t previous_segment_end_time =
|
2017-09-25 19:18:50 +00:00
|
|
|
previous.start_time + previous.duration * (previous.repeat + 1);
|
|
|
|
// Make it continuous if the segment start time is close to previous segment
|
|
|
|
// end time.
|
|
|
|
if (ApproximiatelyEqual(previous_segment_end_time, start_time)) {
|
2018-06-22 01:14:34 +00:00
|
|
|
const int64_t segment_end_time_for_same_duration =
|
2017-09-25 19:18:50 +00:00
|
|
|
previous_segment_end_time + previous.duration;
|
2018-06-22 01:14:34 +00:00
|
|
|
const int64_t actual_segment_end_time = start_time + duration;
|
2017-09-25 19:18:50 +00:00
|
|
|
// Consider the segments having identical duration if the segment end time
|
|
|
|
// is close to calculated segment end time by assuming identical duration.
|
|
|
|
if (ApproximiatelyEqual(segment_end_time_for_same_duration,
|
|
|
|
actual_segment_end_time)) {
|
|
|
|
++segment_infos_.back().repeat;
|
|
|
|
} else {
|
|
|
|
segment_infos_.push_back(
|
|
|
|
{previous_segment_end_time,
|
|
|
|
actual_segment_end_time - previous_segment_end_time, kNoRepeat});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-12-13 01:26:37 +00:00
|
|
|
|
2017-09-25 19:18:50 +00:00
|
|
|
// A gap since previous.
|
2018-06-22 01:14:34 +00:00
|
|
|
const int64_t kRoundingErrorGrace = 5;
|
2017-09-25 19:18:50 +00:00
|
|
|
if (previous_segment_end_time + kRoundingErrorGrace < start_time) {
|
2019-03-26 22:16:30 +00:00
|
|
|
LOG(WARNING) << RepresentationAsString() << " Found a gap of size "
|
2017-09-25 19:18:50 +00:00
|
|
|
<< (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 << ".";
|
|
|
|
}
|
2017-12-13 01:26:37 +00:00
|
|
|
|
2017-09-25 19:18:50 +00:00
|
|
|
// No overlapping segments.
|
|
|
|
if (start_time < previous_segment_end_time - kRoundingErrorGrace) {
|
|
|
|
LOG(WARNING)
|
2019-03-26 22:16:30 +00:00
|
|
|
<< RepresentationAsString()
|
|
|
|
<< " Segments should not be overlapping. The new segment starts at "
|
2017-09-25 19:18:50 +00:00
|
|
|
<< start_time << " but the previous segment ends at "
|
|
|
|
<< previous_segment_end_time << ".";
|
|
|
|
}
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 19:18:50 +00:00
|
|
|
segment_infos_.push_back({start_time, adjusted_duration, kNoRepeat});
|
|
|
|
}
|
2017-12-13 01:26:37 +00:00
|
|
|
|
2018-06-22 01:14:34 +00:00
|
|
|
bool Representation::ApproximiatelyEqual(int64_t time1, int64_t time2) const {
|
2017-09-25 19:18:50 +00:00
|
|
|
if (!allow_approximate_segment_timeline_)
|
|
|
|
return time1 == time2;
|
|
|
|
|
|
|
|
// It is not always possible to align segment duration to target duration
|
|
|
|
// exactly. For example, for AAC with sampling rate of 44100, there are always
|
|
|
|
// 1024 audio samples per frame, so the frame duration is 1024/44100. For a
|
|
|
|
// target duration of 2 seconds, the closest segment duration would be 1.984
|
|
|
|
// or 2.00533.
|
|
|
|
|
|
|
|
// An arbitrary error threshold cap. This makes sure that the error is not too
|
|
|
|
// large for large samples.
|
|
|
|
const double kErrorThresholdSeconds = 0.05;
|
|
|
|
|
|
|
|
// So we consider two times equal if they differ by less than one sample.
|
|
|
|
const uint32_t error_threshold =
|
|
|
|
std::min(frame_duration_,
|
|
|
|
static_cast<uint32_t>(kErrorThresholdSeconds *
|
|
|
|
media_info_.reference_time_scale()));
|
2018-06-22 01:37:56 +00:00
|
|
|
return std::abs(time1 - time2) <= error_threshold;
|
2017-09-25 19:18:50 +00:00
|
|
|
}
|
2017-12-13 01:26:37 +00:00
|
|
|
|
2018-06-22 01:14:34 +00:00
|
|
|
int64_t Representation::AdjustDuration(int64_t duration) const {
|
2017-09-25 19:18:50 +00:00
|
|
|
if (!allow_approximate_segment_timeline_)
|
|
|
|
return duration;
|
2018-06-22 01:14:34 +00:00
|
|
|
const int64_t scaled_target_duration =
|
2018-11-14 20:47:48 +00:00
|
|
|
mpd_options_.mpd_params.target_segment_duration *
|
|
|
|
media_info_.reference_time_scale();
|
2017-09-25 19:18:50 +00:00
|
|
|
return ApproximiatelyEqual(scaled_target_duration, duration)
|
|
|
|
? scaled_target_duration
|
|
|
|
: duration;
|
2017-12-13 01:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Representation::SlideWindow() {
|
|
|
|
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);
|
|
|
|
|
2019-03-22 00:12:30 +00:00
|
|
|
const int64_t time_shift_buffer_depth = static_cast<int64_t>(
|
2017-12-13 01:26:37 +00:00
|
|
|
mpd_options_.mpd_params.time_shift_buffer_depth * time_scale);
|
|
|
|
|
2019-03-22 00:12:30 +00:00
|
|
|
if (current_buffer_depth_ <= time_shift_buffer_depth)
|
2017-12-13 01:26:37 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<SegmentInfo>::iterator first = segment_infos_.begin();
|
|
|
|
std::list<SegmentInfo>::iterator last = first;
|
|
|
|
for (; last != segment_infos_.end(); ++last) {
|
2019-03-22 00:12:30 +00:00
|
|
|
// Remove the current segment only if it falls completely out of time shift
|
|
|
|
// buffer range.
|
|
|
|
while (last->repeat >= 0 &&
|
|
|
|
current_buffer_depth_ - last->duration >= time_shift_buffer_depth) {
|
|
|
|
current_buffer_depth_ -= last->duration;
|
|
|
|
RemoveOldSegment(&*last);
|
|
|
|
start_number_++;
|
|
|
|
}
|
|
|
|
if (last->repeat >= 0)
|
2017-12-13 01:26:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
segment_infos_.erase(first, last);
|
|
|
|
}
|
|
|
|
|
2019-03-22 00:12:30 +00:00
|
|
|
void Representation::RemoveOldSegment(SegmentInfo* segment_info) {
|
|
|
|
int64_t segment_start_time = segment_info->start_time;
|
|
|
|
segment_info->start_time += segment_info->duration;
|
|
|
|
segment_info->repeat--;
|
|
|
|
|
2018-04-04 23:17:59 +00:00
|
|
|
if (mpd_options_.mpd_params.preserved_segments_outside_live_window == 0)
|
|
|
|
return;
|
|
|
|
|
2019-03-22 00:12:30 +00:00
|
|
|
segments_to_be_removed_.push_back(
|
|
|
|
media::GetSegmentName(media_info_.segment_template(), segment_start_time,
|
|
|
|
start_number_ - 1, media_info_.bandwidth()));
|
2018-04-04 23:17:59 +00:00
|
|
|
while (segments_to_be_removed_.size() >
|
|
|
|
mpd_options_.mpd_params.preserved_segments_outside_live_window) {
|
2018-04-17 22:03:02 +00:00
|
|
|
VLOG(2) << "Deleting " << segments_to_be_removed_.front();
|
2018-12-15 01:02:13 +00:00
|
|
|
if (!File::Delete(segments_to_be_removed_.front().c_str())) {
|
|
|
|
LOG(WARNING) << "Failed to delete " << segments_to_be_removed_.front()
|
|
|
|
<< "; Will retry later.";
|
|
|
|
break;
|
|
|
|
}
|
2018-04-04 23:17:59 +00:00
|
|
|
segments_to_be_removed_.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:26:37 +00:00
|
|
|
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());
|
2018-02-26 20:13:28 +00:00
|
|
|
if (media_info_.text_info().codec() == "ttml") {
|
2017-12-13 01:26:37 +00:00
|
|
|
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 "";
|
|
|
|
}
|
|
|
|
}
|
2018-02-26 20:13:28 +00:00
|
|
|
if (media_info_.text_info().codec() == "wvtt") {
|
2017-12-13 01:26:37 +00:00
|
|
|
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: "
|
2018-02-26 20:13:28 +00:00
|
|
|
<< media_info_.text_info().codec()
|
2017-12-13 01:26:37 +00:00
|
|
|
<< " container: " << media_info_.container_type();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-03-26 22:16:30 +00:00
|
|
|
std::string Representation::RepresentationAsString() const {
|
|
|
|
std::string s = base::StringPrintf("Representation (id=%d,", id_);
|
|
|
|
if (media_info_.has_video_info()) {
|
|
|
|
const MediaInfo_VideoInfo& video_info = media_info_.video_info();
|
|
|
|
base::StringAppendF(&s, "codec='%s',width=%d,height=%d",
|
|
|
|
video_info.codec().c_str(), video_info.width(),
|
|
|
|
video_info.height());
|
|
|
|
} else if (media_info_.has_audio_info()) {
|
|
|
|
const MediaInfo_AudioInfo& audio_info = media_info_.audio_info();
|
|
|
|
base::StringAppendF(
|
|
|
|
&s, "codec='%s',frequency=%d,language='%s'", audio_info.codec().c_str(),
|
|
|
|
audio_info.sampling_frequency(), audio_info.language().c_str());
|
|
|
|
} else if (media_info_.has_text_info()) {
|
|
|
|
const MediaInfo_TextInfo& text_info = media_info_.text_info();
|
|
|
|
base::StringAppendF(&s, "codec='%s',language='%s'",
|
|
|
|
text_info.codec().c_str(),
|
|
|
|
text_info.language().c_str());
|
|
|
|
}
|
|
|
|
base::StringAppendF(&s, ")");
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:26:37 +00:00
|
|
|
} // namespace shaka
|