2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2016 Google LLC. All rights reserved.
|
2016-03-25 08:35:44 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/hls/base/media_playlist.h>
|
2016-03-25 08:35:44 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <cinttypes>
|
2016-03-25 08:35:44 +00:00
|
|
|
#include <cmath>
|
2016-08-30 23:01:19 +00:00
|
|
|
#include <memory>
|
2016-03-25 08:35:44 +00:00
|
|
|
|
2023-08-22 01:41:18 +00:00
|
|
|
#include <absl/strings/numbers.h>
|
|
|
|
#include <absl/strings/str_format.h>
|
|
|
|
#include <glog/logging.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/file/file.h>
|
|
|
|
#include <packager/hls/base/tag.h>
|
|
|
|
#include <packager/media/base/language_utils.h>
|
|
|
|
#include <packager/media/base/muxer_util.h>
|
|
|
|
#include <packager/version/version.h>
|
2016-03-25 08:35:44 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-25 08:35:44 +00:00
|
|
|
namespace hls {
|
|
|
|
|
|
|
|
namespace {
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t GetTimeScale(const MediaInfo& media_info) {
|
2016-03-25 08:35:44 +00:00
|
|
|
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();
|
2021-08-04 18:56:44 +00:00
|
|
|
return 0;
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 05:54:28 +00:00
|
|
|
std::string AdjustVideoCodec(const std::string& codec) {
|
|
|
|
// Apple does not like video formats with the parameter sets stored in the
|
|
|
|
// samples. It also fails mediastreamvalidator checks and some Apple devices /
|
|
|
|
// platforms refused to play.
|
|
|
|
// See https://apple.co/30n90DC 1.10 and
|
2022-03-07 19:56:34 +00:00
|
|
|
// https://github.com/shaka-project/shaka-packager/issues/587#issuecomment-489182182.
|
2019-05-15 05:54:28 +00:00
|
|
|
// Replaced with the corresponding formats with the parameter sets stored in
|
|
|
|
// the sample descriptions instead.
|
|
|
|
std::string adjusted_codec = codec;
|
|
|
|
std::string fourcc = codec.substr(0, 4);
|
|
|
|
if (fourcc == "avc3")
|
|
|
|
adjusted_codec = "avc1" + codec.substr(4);
|
|
|
|
else if (fourcc == "hev1")
|
|
|
|
adjusted_codec = "hvc1" + codec.substr(4);
|
|
|
|
else if (fourcc == "dvhe")
|
|
|
|
adjusted_codec = "dvh1" + codec.substr(4);
|
|
|
|
if (adjusted_codec != codec) {
|
|
|
|
VLOG(1) << "Adusting video codec string from " << codec << " to "
|
|
|
|
<< adjusted_codec;
|
|
|
|
}
|
|
|
|
return adjusted_codec;
|
|
|
|
}
|
|
|
|
|
2018-04-05 01:27:57 +00:00
|
|
|
// Duplicated from MpdUtils because:
|
|
|
|
// 1. MpdUtils header depends on libxml header, which is not in the deps here
|
|
|
|
// 2. GetLanguage depends on MediaInfo from packager/mpd/
|
|
|
|
// 3. Moving GetLanguage to LanguageUtils would create a a media => mpd dep.
|
2022-03-07 19:56:34 +00:00
|
|
|
// TODO(https://github.com/shaka-project/shaka-packager/issues/373): Fix this
|
2018-04-05 01:27:57 +00:00
|
|
|
// dependency situation and factor this out to a common location.
|
|
|
|
std::string GetLanguage(const MediaInfo& media_info) {
|
|
|
|
std::string lang;
|
|
|
|
if (media_info.has_audio_info()) {
|
|
|
|
lang = media_info.audio_info().language();
|
|
|
|
} else if (media_info.has_text_info()) {
|
|
|
|
lang = media_info.text_info().language();
|
|
|
|
}
|
|
|
|
return LanguageToShortestForm(lang);
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:51:57 +00:00
|
|
|
void AppendExtXMap(const MediaInfo& media_info, std::string* out) {
|
2018-04-17 17:42:45 +00:00
|
|
|
if (media_info.has_init_segment_url()) {
|
2018-01-31 21:51:57 +00:00
|
|
|
Tag tag("#EXT-X-MAP", out);
|
2018-04-17 17:42:45 +00:00
|
|
|
tag.AddQuotedString("URI", media_info.init_segment_url().data());
|
2018-01-31 21:51:57 +00:00
|
|
|
out->append("\n");
|
2018-04-17 17:42:45 +00:00
|
|
|
} else if (media_info.has_media_file_url() && media_info.has_init_range()) {
|
2017-05-01 20:38:58 +00:00
|
|
|
// It only makes sense for single segment media to have EXT-X-MAP if
|
|
|
|
// there is init_range.
|
2018-01-31 21:51:57 +00:00
|
|
|
Tag tag("#EXT-X-MAP", out);
|
2018-04-17 17:42:45 +00:00
|
|
|
tag.AddQuotedString("URI", media_info.media_file_url().data());
|
2018-01-31 21:51:57 +00:00
|
|
|
|
|
|
|
if (media_info.has_init_range()) {
|
|
|
|
const uint64_t begin = media_info.init_range().begin();
|
|
|
|
const uint64_t end = media_info.init_range().end();
|
|
|
|
const uint64_t length = end - begin + 1;
|
|
|
|
|
|
|
|
tag.AddQuotedNumberPair("BYTERANGE", length, '@', begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
out->append("\n");
|
2017-05-01 20:38:58 +00:00
|
|
|
} else {
|
2018-01-31 21:51:57 +00:00
|
|
|
// This media info does not need an ext-x-map tag.
|
2017-05-01 20:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:17:21 +00:00
|
|
|
std::string CreatePlaylistHeader(
|
|
|
|
const MediaInfo& media_info,
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t target_duration,
|
2018-01-30 03:17:21 +00:00
|
|
|
HlsPlaylistType type,
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType stream_type,
|
2020-01-31 18:25:19 +00:00
|
|
|
uint32_t media_sequence_number,
|
2018-01-30 03:17:21 +00:00
|
|
|
int discontinuity_sequence_number) {
|
2017-03-14 00:02:23 +00:00
|
|
|
const std::string version = GetPackagerVersion();
|
|
|
|
std::string version_line;
|
|
|
|
if (!version.empty()) {
|
|
|
|
version_line =
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::StrFormat("## Generated with %s version %s\n",
|
|
|
|
GetPackagerProjectUrl().c_str(), version.c_str());
|
2017-03-14 00:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 6 is required for EXT-X-MAP without EXT-X-I-FRAMES-ONLY.
|
2023-08-22 01:41:18 +00:00
|
|
|
std::string header = absl::StrFormat(
|
2017-03-14 00:02:23 +00:00
|
|
|
"#EXTM3U\n"
|
|
|
|
"#EXT-X-VERSION:6\n"
|
|
|
|
"%s"
|
|
|
|
"#EXT-X-TARGETDURATION:%d\n",
|
|
|
|
version_line.c_str(), target_duration);
|
|
|
|
|
2017-06-03 00:05:47 +00:00
|
|
|
switch (type) {
|
2017-06-30 20:42:46 +00:00
|
|
|
case HlsPlaylistType::kVod:
|
2017-06-03 00:05:47 +00:00
|
|
|
header += "#EXT-X-PLAYLIST-TYPE:VOD\n";
|
|
|
|
break;
|
2017-06-30 20:42:46 +00:00
|
|
|
case HlsPlaylistType::kEvent:
|
2017-06-03 00:05:47 +00:00
|
|
|
header += "#EXT-X-PLAYLIST-TYPE:EVENT\n";
|
|
|
|
break;
|
2017-06-30 20:42:46 +00:00
|
|
|
case HlsPlaylistType::kLive:
|
2017-06-20 23:30:03 +00:00
|
|
|
if (media_sequence_number > 0) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::StrAppendFormat(&header, "#EXT-X-MEDIA-SEQUENCE:%d\n",
|
|
|
|
media_sequence_number);
|
2017-06-03 00:05:47 +00:00
|
|
|
}
|
|
|
|
if (discontinuity_sequence_number > 0) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::StrAppendFormat(&header, "#EXT-X-DISCONTINUITY-SEQUENCE:%d\n",
|
|
|
|
discontinuity_sequence_number);
|
2017-06-03 00:05:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2023-08-22 01:41:18 +00:00
|
|
|
NOTIMPLEMENTED() << "Unexpected MediaPlaylistType "
|
|
|
|
<< static_cast<int>(type);
|
2017-03-14 00:02:23 +00:00
|
|
|
}
|
2018-01-30 03:17:21 +00:00
|
|
|
if (stream_type ==
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kVideoIFramesOnly) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::StrAppendFormat(&header, "#EXT-X-I-FRAMES-ONLY\n");
|
2018-01-30 03:17:21 +00:00
|
|
|
}
|
2017-03-14 00:02:23 +00:00
|
|
|
|
|
|
|
// Put EXT-X-MAP at the end since the rest of the playlist is about the
|
|
|
|
// segment and key info.
|
2018-01-31 21:51:57 +00:00
|
|
|
AppendExtXMap(media_info, &header);
|
|
|
|
|
2017-03-14 00:02:23 +00:00
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
class SegmentInfoEntry : public HlsEntry {
|
|
|
|
public:
|
2017-05-01 20:38:58 +00:00
|
|
|
// If |use_byte_range| true then this will append EXT-X-BYTERANGE
|
|
|
|
// after EXTINF.
|
|
|
|
// It uses |previous_segment_end_offset| to determine if it has to also
|
|
|
|
// specify the start byte offset in the tag.
|
2019-08-07 23:14:45 +00:00
|
|
|
// |start_time| is in timescale.
|
|
|
|
// |duration_seconds| is duration in seconds.
|
2017-06-03 00:05:47 +00:00
|
|
|
SegmentInfoEntry(const std::string& file_name,
|
2019-08-07 23:14:45 +00:00
|
|
|
int64_t start_time,
|
|
|
|
double duration_seconds,
|
2017-05-01 20:38:58 +00:00
|
|
|
bool use_byte_range,
|
|
|
|
uint64_t start_byte_offset,
|
|
|
|
uint64_t segment_file_size,
|
|
|
|
uint64_t previous_segment_end_offset);
|
2016-03-25 08:35:44 +00:00
|
|
|
|
|
|
|
std::string ToString() override;
|
2019-08-07 23:14:45 +00:00
|
|
|
int64_t start_time() const { return start_time_; }
|
|
|
|
double duration_seconds() const { return duration_seconds_; }
|
|
|
|
void set_duration_seconds(double duration_seconds) {
|
|
|
|
duration_seconds_ = duration_seconds;
|
|
|
|
}
|
2016-03-25 08:35:44 +00:00
|
|
|
|
|
|
|
private:
|
2018-01-12 01:33:37 +00:00
|
|
|
SegmentInfoEntry(const SegmentInfoEntry&) = delete;
|
|
|
|
SegmentInfoEntry& operator=(const SegmentInfoEntry&) = delete;
|
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
const std::string file_name_;
|
2019-08-07 23:14:45 +00:00
|
|
|
const int64_t start_time_;
|
|
|
|
double duration_seconds_;
|
2017-05-01 20:38:58 +00:00
|
|
|
const bool use_byte_range_;
|
|
|
|
const uint64_t start_byte_offset_;
|
|
|
|
const uint64_t segment_file_size_;
|
|
|
|
const uint64_t previous_segment_end_offset_;
|
2016-03-25 08:35:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SegmentInfoEntry::SegmentInfoEntry(const std::string& file_name,
|
2019-08-07 23:14:45 +00:00
|
|
|
int64_t start_time,
|
|
|
|
double duration_seconds,
|
2017-05-01 20:38:58 +00:00
|
|
|
bool use_byte_range,
|
|
|
|
uint64_t start_byte_offset,
|
|
|
|
uint64_t segment_file_size,
|
|
|
|
uint64_t previous_segment_end_offset)
|
2016-03-25 08:35:44 +00:00
|
|
|
: HlsEntry(HlsEntry::EntryType::kExtInf),
|
|
|
|
file_name_(file_name),
|
2017-06-03 00:05:47 +00:00
|
|
|
start_time_(start_time),
|
2019-08-07 23:14:45 +00:00
|
|
|
duration_seconds_(duration_seconds),
|
2017-05-01 20:38:58 +00:00
|
|
|
use_byte_range_(use_byte_range),
|
|
|
|
start_byte_offset_(start_byte_offset),
|
|
|
|
segment_file_size_(segment_file_size),
|
|
|
|
previous_segment_end_offset_(previous_segment_end_offset) {}
|
2016-03-25 08:35:44 +00:00
|
|
|
|
|
|
|
std::string SegmentInfoEntry::ToString() {
|
2023-08-22 01:41:18 +00:00
|
|
|
std::string result = absl::StrFormat("#EXTINF:%.3f,", duration_seconds_);
|
2018-01-31 21:51:57 +00:00
|
|
|
|
2017-05-01 20:38:58 +00:00
|
|
|
if (use_byte_range_) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::StrAppendFormat(&result, "\n#EXT-X-BYTERANGE:%" PRIu64,
|
|
|
|
segment_file_size_);
|
2017-05-01 20:38:58 +00:00
|
|
|
if (previous_segment_end_offset_ + 1 != start_byte_offset_) {
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::StrAppendFormat(&result, "@%" PRIu64, start_byte_offset_);
|
2017-05-01 20:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-31 21:51:57 +00:00
|
|
|
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::StrAppendFormat(&result, "\n%s", file_name_.c_str());
|
2018-01-31 21:51:57 +00:00
|
|
|
|
2017-05-01 20:38:58 +00:00
|
|
|
return result;
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class EncryptionInfoEntry : public HlsEntry {
|
|
|
|
public:
|
|
|
|
EncryptionInfoEntry(MediaPlaylist::EncryptionMethod method,
|
|
|
|
const std::string& url,
|
2017-04-04 20:57:50 +00:00
|
|
|
const std::string& key_id,
|
2016-03-25 08:35:44 +00:00
|
|
|
const std::string& iv,
|
|
|
|
const std::string& key_format,
|
|
|
|
const std::string& key_format_versions);
|
|
|
|
|
|
|
|
std::string ToString() override;
|
|
|
|
|
|
|
|
private:
|
2018-01-12 01:33:37 +00:00
|
|
|
EncryptionInfoEntry(const EncryptionInfoEntry&) = delete;
|
|
|
|
EncryptionInfoEntry& operator=(const EncryptionInfoEntry&) = delete;
|
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
const MediaPlaylist::EncryptionMethod method_;
|
|
|
|
const std::string url_;
|
2017-04-04 20:57:50 +00:00
|
|
|
const std::string key_id_;
|
2016-03-25 08:35:44 +00:00
|
|
|
const std::string iv_;
|
|
|
|
const std::string key_format_;
|
|
|
|
const std::string key_format_versions_;
|
|
|
|
};
|
|
|
|
|
|
|
|
EncryptionInfoEntry::EncryptionInfoEntry(MediaPlaylist::EncryptionMethod method,
|
|
|
|
const std::string& url,
|
2017-04-04 20:57:50 +00:00
|
|
|
const std::string& key_id,
|
2016-03-25 08:35:44 +00:00
|
|
|
const std::string& iv,
|
|
|
|
const std::string& key_format,
|
|
|
|
const std::string& key_format_versions)
|
|
|
|
: HlsEntry(HlsEntry::EntryType::kExtKey),
|
|
|
|
method_(method),
|
|
|
|
url_(url),
|
2017-04-04 20:57:50 +00:00
|
|
|
key_id_(key_id),
|
2016-03-25 08:35:44 +00:00
|
|
|
iv_(iv),
|
|
|
|
key_format_(key_format),
|
|
|
|
key_format_versions_(key_format_versions) {}
|
|
|
|
|
|
|
|
std::string EncryptionInfoEntry::ToString() {
|
2018-01-31 21:51:57 +00:00
|
|
|
std::string tag_string;
|
|
|
|
Tag tag("#EXT-X-KEY", &tag_string);
|
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
if (method_ == MediaPlaylist::EncryptionMethod::kSampleAes) {
|
2018-01-31 21:51:57 +00:00
|
|
|
tag.AddString("METHOD", "SAMPLE-AES");
|
2016-03-25 08:35:44 +00:00
|
|
|
} else if (method_ == MediaPlaylist::EncryptionMethod::kAes128) {
|
2018-01-31 21:51:57 +00:00
|
|
|
tag.AddString("METHOD", "AES-128");
|
2017-04-04 20:57:50 +00:00
|
|
|
} else if (method_ == MediaPlaylist::EncryptionMethod::kSampleAesCenc) {
|
2018-01-31 21:51:57 +00:00
|
|
|
tag.AddString("METHOD", "SAMPLE-AES-CTR");
|
2016-03-25 08:35:44 +00:00
|
|
|
} else {
|
|
|
|
DCHECK(method_ == MediaPlaylist::EncryptionMethod::kNone);
|
2018-01-31 21:51:57 +00:00
|
|
|
tag.AddString("METHOD", "NONE");
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
2018-01-31 21:51:57 +00:00
|
|
|
|
|
|
|
tag.AddQuotedString("URI", url_);
|
|
|
|
|
2017-04-04 20:57:50 +00:00
|
|
|
if (!key_id_.empty()) {
|
2018-01-31 21:51:57 +00:00
|
|
|
tag.AddString("KEYID", key_id_);
|
2017-04-04 20:57:50 +00:00
|
|
|
}
|
2016-03-25 08:35:44 +00:00
|
|
|
if (!iv_.empty()) {
|
2018-01-31 21:51:57 +00:00
|
|
|
tag.AddString("IV", iv_);
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
if (!key_format_versions_.empty()) {
|
2018-01-31 21:51:57 +00:00
|
|
|
tag.AddQuotedString("KEYFORMATVERSIONS", key_format_versions_);
|
|
|
|
}
|
|
|
|
if (!key_format_.empty()) {
|
|
|
|
tag.AddQuotedString("KEYFORMAT", key_format_);
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 21:51:57 +00:00
|
|
|
return tag_string;
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-20 23:30:03 +00:00
|
|
|
class DiscontinuityEntry : public HlsEntry {
|
|
|
|
public:
|
|
|
|
DiscontinuityEntry();
|
|
|
|
|
|
|
|
std::string ToString() override;
|
|
|
|
|
|
|
|
private:
|
2018-01-12 01:33:37 +00:00
|
|
|
DiscontinuityEntry(const DiscontinuityEntry&) = delete;
|
|
|
|
DiscontinuityEntry& operator=(const DiscontinuityEntry&) = delete;
|
2017-06-20 23:30:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DiscontinuityEntry::DiscontinuityEntry()
|
|
|
|
: HlsEntry(HlsEntry::EntryType::kExtDiscontinuity) {}
|
|
|
|
|
|
|
|
std::string DiscontinuityEntry::ToString() {
|
2018-01-31 21:51:57 +00:00
|
|
|
return "#EXT-X-DISCONTINUITY";
|
2017-06-20 23:30:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 01:33:37 +00:00
|
|
|
class PlacementOpportunityEntry : public HlsEntry {
|
|
|
|
public:
|
|
|
|
PlacementOpportunityEntry();
|
|
|
|
|
|
|
|
std::string ToString() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
PlacementOpportunityEntry(const PlacementOpportunityEntry&) = delete;
|
|
|
|
PlacementOpportunityEntry& operator=(const PlacementOpportunityEntry&) =
|
|
|
|
delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
PlacementOpportunityEntry::PlacementOpportunityEntry()
|
|
|
|
: HlsEntry(HlsEntry::EntryType::kExtPlacementOpportunity) {}
|
|
|
|
|
|
|
|
std::string PlacementOpportunityEntry::ToString() {
|
2018-01-31 21:51:57 +00:00
|
|
|
return "#EXT-X-PLACEMENT-OPPORTUNITY";
|
2018-01-12 01:33:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
HlsEntry::HlsEntry(HlsEntry::EntryType type) : type_(type) {}
|
|
|
|
HlsEntry::~HlsEntry() {}
|
|
|
|
|
2018-04-14 01:26:02 +00:00
|
|
|
MediaPlaylist::MediaPlaylist(const HlsParams& hls_params,
|
2016-04-20 22:23:19 +00:00
|
|
|
const std::string& file_name,
|
2016-03-25 08:35:44 +00:00
|
|
|
const std::string& name,
|
|
|
|
const std::string& group_id)
|
2018-04-14 01:26:02 +00:00
|
|
|
: hls_params_(hls_params),
|
2017-06-03 00:05:47 +00:00
|
|
|
file_name_(file_name),
|
|
|
|
name_(name),
|
2020-01-31 18:25:19 +00:00
|
|
|
group_id_(group_id),
|
|
|
|
media_sequence_number_(hls_params_.media_sequence_number) {
|
|
|
|
// When there's a forced media_sequence_number, start with discontinuity
|
|
|
|
if (media_sequence_number_ > 0)
|
|
|
|
entries_.emplace_back(new DiscontinuityEntry());
|
|
|
|
}
|
2016-08-30 23:01:19 +00:00
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
MediaPlaylist::~MediaPlaylist() {}
|
|
|
|
|
2016-04-20 22:23:19 +00:00
|
|
|
void MediaPlaylist::SetStreamTypeForTesting(
|
|
|
|
MediaPlaylistStreamType stream_type) {
|
|
|
|
stream_type_ = stream_type;
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaPlaylist::SetCodecForTesting(const std::string& codec) {
|
|
|
|
codec_ = codec;
|
|
|
|
}
|
|
|
|
|
2018-04-05 01:27:57 +00:00
|
|
|
void MediaPlaylist::SetLanguageForTesting(const std::string& language) {
|
|
|
|
language_ = language;
|
|
|
|
}
|
|
|
|
|
2018-10-10 22:30:28 +00:00
|
|
|
void MediaPlaylist::SetCharacteristicsForTesting(
|
|
|
|
const std::vector<std::string>& characteristics) {
|
|
|
|
characteristics_ = characteristics;
|
|
|
|
}
|
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
bool MediaPlaylist::SetMediaInfo(const MediaInfo& media_info) {
|
2021-08-04 18:56:44 +00:00
|
|
|
const int32_t time_scale = GetTimeScale(media_info);
|
2016-03-25 08:35:44 +00:00
|
|
|
if (time_scale == 0) {
|
|
|
|
LOG(ERROR) << "MediaInfo does not contain a valid timescale.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (media_info.has_video_info()) {
|
2018-01-30 03:17:21 +00:00
|
|
|
stream_type_ = MediaPlaylistStreamType::kVideo;
|
2019-05-15 05:54:28 +00:00
|
|
|
codec_ = AdjustVideoCodec(media_info.video_info().codec());
|
2016-03-25 08:35:44 +00:00
|
|
|
} else if (media_info.has_audio_info()) {
|
2018-01-30 03:17:21 +00:00
|
|
|
stream_type_ = MediaPlaylistStreamType::kAudio;
|
2016-03-25 08:35:44 +00:00
|
|
|
codec_ = media_info.audio_info().codec();
|
|
|
|
} else {
|
2018-01-31 19:14:15 +00:00
|
|
|
stream_type_ = MediaPlaylistStreamType::kSubtitle;
|
2018-02-26 20:13:28 +00:00
|
|
|
codec_ = media_info.text_info().codec();
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time_scale_ = time_scale;
|
|
|
|
media_info_ = media_info;
|
2018-04-05 01:27:57 +00:00
|
|
|
language_ = GetLanguage(media_info);
|
2020-07-30 21:26:45 +00:00
|
|
|
use_byte_range_ = !media_info_.has_segment_template_url() &&
|
|
|
|
media_info_.container_type() != MediaInfo::CONTAINER_TEXT;
|
2018-10-10 22:30:28 +00:00
|
|
|
characteristics_ =
|
|
|
|
std::vector<std::string>(media_info_.hls_characteristics().begin(),
|
|
|
|
media_info_.hls_characteristics().end());
|
2020-01-31 18:25:19 +00:00
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
void MediaPlaylist::SetSampleDuration(int32_t sample_duration) {
|
2019-09-23 06:47:07 +00:00
|
|
|
if (media_info_.has_video_info())
|
|
|
|
media_info_.mutable_video_info()->set_frame_duration(sample_duration);
|
|
|
|
}
|
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
void MediaPlaylist::AddSegment(const std::string& file_name,
|
2018-06-22 01:14:34 +00:00
|
|
|
int64_t start_time,
|
|
|
|
int64_t duration,
|
2017-05-01 20:38:58 +00:00
|
|
|
uint64_t start_byte_offset,
|
2016-03-25 08:35:44 +00:00
|
|
|
uint64_t size) {
|
2018-01-30 03:17:21 +00:00
|
|
|
if (stream_type_ == MediaPlaylistStreamType::kVideoIFramesOnly) {
|
|
|
|
if (key_frames_.empty())
|
|
|
|
return;
|
2018-05-16 17:56:22 +00:00
|
|
|
|
|
|
|
AdjustLastSegmentInfoEntryDuration(key_frames_.front().timestamp);
|
|
|
|
|
|
|
|
for (auto iter = key_frames_.begin(); iter != key_frames_.end(); ++iter) {
|
|
|
|
// Last entry duration may be adjusted later when the next iframe becomes
|
|
|
|
// available.
|
2018-06-22 01:14:34 +00:00
|
|
|
const int64_t next_timestamp = std::next(iter) == key_frames_.end()
|
|
|
|
? (start_time + duration)
|
|
|
|
: std::next(iter)->timestamp;
|
2018-05-16 17:56:22 +00:00
|
|
|
AddSegmentInfoEntry(file_name, iter->timestamp,
|
|
|
|
next_timestamp - iter->timestamp,
|
2018-01-30 03:17:21 +00:00
|
|
|
iter->start_byte_offset, iter->size);
|
|
|
|
}
|
2018-05-16 17:56:22 +00:00
|
|
|
key_frames_.clear();
|
2016-03-25 08:35:44 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-01-30 03:17:21 +00:00
|
|
|
return AddSegmentInfoEntry(file_name, start_time, duration, start_byte_offset,
|
|
|
|
size);
|
|
|
|
}
|
2016-03-25 08:35:44 +00:00
|
|
|
|
2018-06-22 01:14:34 +00:00
|
|
|
void MediaPlaylist::AddKeyFrame(int64_t timestamp,
|
2018-01-30 03:17:21 +00:00
|
|
|
uint64_t start_byte_offset,
|
|
|
|
uint64_t size) {
|
|
|
|
if (stream_type_ != MediaPlaylistStreamType::kVideoIFramesOnly) {
|
|
|
|
if (stream_type_ != MediaPlaylistStreamType::kVideo) {
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "I-Frames Only playlist applies to video renditions only.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stream_type_ = MediaPlaylistStreamType::kVideoIFramesOnly;
|
|
|
|
use_byte_range_ = true;
|
|
|
|
}
|
2023-08-22 01:41:18 +00:00
|
|
|
key_frames_.push_back({timestamp, start_byte_offset, size, std::string("")});
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaPlaylist::AddEncryptionInfo(MediaPlaylist::EncryptionMethod method,
|
|
|
|
const std::string& url,
|
2017-04-04 20:57:50 +00:00
|
|
|
const std::string& key_id,
|
2016-03-25 08:35:44 +00:00
|
|
|
const std::string& iv,
|
|
|
|
const std::string& key_format,
|
|
|
|
const std::string& key_format_versions) {
|
2017-06-20 23:30:03 +00:00
|
|
|
if (!inserted_discontinuity_tag_) {
|
|
|
|
// Insert discontinuity tag only for the first EXT-X-KEY, only if there
|
|
|
|
// are non-encrypted media segments.
|
|
|
|
if (!entries_.empty())
|
|
|
|
entries_.emplace_back(new DiscontinuityEntry());
|
|
|
|
inserted_discontinuity_tag_ = true;
|
|
|
|
}
|
2017-04-04 20:57:50 +00:00
|
|
|
entries_.emplace_back(new EncryptionInfoEntry(
|
|
|
|
method, url, key_id, iv, key_format, key_format_versions));
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 01:33:37 +00:00
|
|
|
void MediaPlaylist::AddPlacementOpportunity() {
|
|
|
|
entries_.emplace_back(new PlacementOpportunityEntry());
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:41:18 +00:00
|
|
|
bool MediaPlaylist::WriteToFile(const std::filesystem::path& file_path) {
|
2016-03-25 08:35:44 +00:00
|
|
|
if (!target_duration_set_) {
|
|
|
|
SetTargetDuration(ceil(GetLongestSegmentDuration()));
|
|
|
|
}
|
|
|
|
|
2018-01-31 21:51:57 +00:00
|
|
|
std::string content = CreatePlaylistHeader(
|
2018-04-14 01:26:02 +00:00
|
|
|
media_info_, target_duration_, hls_params_.playlist_type, stream_type_,
|
2018-01-30 03:17:21 +00:00
|
|
|
media_sequence_number_, discontinuity_sequence_number_);
|
2016-06-29 21:24:40 +00:00
|
|
|
|
2017-06-20 23:30:03 +00:00
|
|
|
for (const auto& entry : entries_)
|
2023-08-22 01:41:18 +00:00
|
|
|
absl::StrAppendFormat(&content, "%s\n", entry->ToString().c_str());
|
2016-04-20 22:23:19 +00:00
|
|
|
|
2018-04-14 01:26:02 +00:00
|
|
|
if (hls_params_.playlist_type == HlsPlaylistType::kVod) {
|
2016-04-20 22:23:19 +00:00
|
|
|
content += "#EXT-X-ENDLIST\n";
|
|
|
|
}
|
|
|
|
|
2023-08-22 01:41:18 +00:00
|
|
|
if (!File::WriteFileAtomically(file_path.string().c_str(), content)) {
|
|
|
|
LOG(ERROR) << "Failed to write playlist to: " << file_path.string();
|
2017-06-03 00:05:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-25 08:35:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-31 21:27:21 +00:00
|
|
|
uint64_t MediaPlaylist::MaxBitrate() const {
|
2016-03-25 08:35:44 +00:00
|
|
|
if (media_info_.has_bandwidth())
|
|
|
|
return media_info_.bandwidth();
|
2018-05-31 01:28:16 +00:00
|
|
|
return bandwidth_estimator_.Max();
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:27:21 +00:00
|
|
|
uint64_t MediaPlaylist::AvgBitrate() const {
|
|
|
|
return bandwidth_estimator_.Estimate();
|
|
|
|
}
|
|
|
|
|
2016-03-25 08:39:07 +00:00
|
|
|
double MediaPlaylist::GetLongestSegmentDuration() const {
|
2019-08-07 23:14:45 +00:00
|
|
|
return longest_segment_duration_seconds_;
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 18:56:44 +00:00
|
|
|
void MediaPlaylist::SetTargetDuration(int32_t target_duration) {
|
2016-03-25 08:35:44 +00:00
|
|
|
if (target_duration_set_) {
|
2017-06-03 00:05:47 +00:00
|
|
|
if (target_duration_ == target_duration)
|
|
|
|
return;
|
|
|
|
VLOG(1) << "Updating target duration from " << target_duration << " to "
|
|
|
|
<< target_duration_;
|
2016-03-25 08:35:44 +00:00
|
|
|
}
|
|
|
|
target_duration_ = target_duration;
|
|
|
|
target_duration_set_ = true;
|
|
|
|
}
|
|
|
|
|
2017-12-09 01:53:17 +00:00
|
|
|
int MediaPlaylist::GetNumChannels() const {
|
|
|
|
return media_info_.audio_info().num_channels();
|
|
|
|
}
|
|
|
|
|
2020-06-04 05:02:49 +00:00
|
|
|
int MediaPlaylist::GetEC3JocComplexity() const {
|
|
|
|
return media_info_.audio_info().codec_specific_data().ec3_joc_complexity();
|
|
|
|
}
|
|
|
|
|
2020-07-04 21:55:28 +00:00
|
|
|
bool MediaPlaylist::GetAC4ImsFlag() const {
|
|
|
|
return media_info_.audio_info().codec_specific_data().ac4_ims_flag();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MediaPlaylist::GetAC4CbiFlag() const {
|
|
|
|
return media_info_.audio_info().codec_specific_data().ac4_cbi_flag();
|
|
|
|
}
|
|
|
|
|
2017-07-13 20:25:36 +00:00
|
|
|
bool MediaPlaylist::GetDisplayResolution(uint32_t* width,
|
|
|
|
uint32_t* height) const {
|
2017-06-14 19:25:17 +00:00
|
|
|
DCHECK(width);
|
|
|
|
DCHECK(height);
|
|
|
|
if (media_info_.has_video_info()) {
|
2017-07-13 20:25:36 +00:00
|
|
|
const double pixel_aspect_ratio =
|
|
|
|
media_info_.video_info().pixel_height() > 0
|
|
|
|
? static_cast<double>(media_info_.video_info().pixel_width()) /
|
|
|
|
media_info_.video_info().pixel_height()
|
|
|
|
: 1.0;
|
|
|
|
*width = static_cast<uint32_t>(media_info_.video_info().width() *
|
|
|
|
pixel_aspect_ratio);
|
2017-06-14 19:25:17 +00:00
|
|
|
*height = media_info_.video_info().height();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-23 06:24:33 +00:00
|
|
|
std::string MediaPlaylist::GetVideoRange() const {
|
2019-10-01 01:22:21 +00:00
|
|
|
// Dolby Vision (dvh1 or dvhe) is always HDR.
|
|
|
|
if (codec_.find("dvh") == 0)
|
|
|
|
return "PQ";
|
|
|
|
|
2019-09-23 06:24:33 +00:00
|
|
|
// HLS specification:
|
|
|
|
// https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-02#section-4.4.4.2
|
|
|
|
switch (media_info_.video_info().transfer_characteristics()) {
|
|
|
|
case 1:
|
|
|
|
return "SDR";
|
|
|
|
case 16:
|
|
|
|
case 18:
|
|
|
|
return "PQ";
|
|
|
|
default:
|
|
|
|
// Leave it empty if we do not have the transfer characteristics
|
|
|
|
// information.
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 06:47:07 +00:00
|
|
|
double MediaPlaylist::GetFrameRate() const {
|
|
|
|
if (media_info_.video_info().frame_duration() == 0)
|
|
|
|
return 0;
|
|
|
|
return static_cast<double>(time_scale_) /
|
|
|
|
media_info_.video_info().frame_duration();
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:17:21 +00:00
|
|
|
void MediaPlaylist::AddSegmentInfoEntry(const std::string& segment_file_name,
|
2018-06-22 01:14:34 +00:00
|
|
|
int64_t start_time,
|
|
|
|
int64_t duration,
|
2018-01-30 03:17:21 +00:00
|
|
|
uint64_t start_byte_offset,
|
|
|
|
uint64_t size) {
|
|
|
|
if (time_scale_ == 0) {
|
|
|
|
LOG(WARNING) << "Timescale is not set and the duration for " << duration
|
|
|
|
<< " cannot be calculated. The output will be wrong.";
|
|
|
|
|
|
|
|
entries_.emplace_back(new SegmentInfoEntry(
|
|
|
|
segment_file_name, 0.0, 0.0, use_byte_range_, start_byte_offset, size,
|
|
|
|
previous_segment_end_offset_));
|
|
|
|
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();
|
|
|
|
|
2018-01-30 03:17:21 +00:00
|
|
|
const double segment_duration_seconds =
|
|
|
|
static_cast<double>(duration) / time_scale_;
|
2019-08-07 23:14:45 +00:00
|
|
|
longest_segment_duration_seconds_ =
|
|
|
|
std::max(longest_segment_duration_seconds_, segment_duration_seconds);
|
2018-05-31 01:28:16 +00:00
|
|
|
bandwidth_estimator_.AddBlock(size, segment_duration_seconds);
|
2019-03-22 00:12:30 +00:00
|
|
|
current_buffer_depth_ += segment_duration_seconds;
|
|
|
|
|
|
|
|
if (!entries_.empty() &&
|
|
|
|
entries_.back()->type() == HlsEntry::EntryType::kExtInf) {
|
|
|
|
const SegmentInfoEntry* segment_info =
|
|
|
|
static_cast<SegmentInfoEntry*>(entries_.back().get());
|
2019-08-07 23:14:45 +00:00
|
|
|
if (segment_info->start_time() > start_time) {
|
2019-03-22 00:12:30 +00:00
|
|
|
LOG(WARNING)
|
|
|
|
<< "Insert a discontinuity tag after the segment with start time "
|
|
|
|
<< segment_info->start_time() << " as the next segment starts at "
|
2019-08-07 23:14:45 +00:00
|
|
|
<< start_time << ".";
|
2019-03-22 00:12:30 +00:00
|
|
|
entries_.emplace_back(new DiscontinuityEntry());
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 03:17:21 +00:00
|
|
|
|
|
|
|
entries_.emplace_back(new SegmentInfoEntry(
|
2019-08-07 23:14:45 +00:00
|
|
|
segment_file_name, start_time, segment_duration_seconds, use_byte_range_,
|
|
|
|
start_byte_offset, size, previous_segment_end_offset_));
|
2018-01-30 03:17:21 +00:00
|
|
|
previous_segment_end_offset_ = start_byte_offset + size - 1;
|
|
|
|
}
|
|
|
|
|
2018-06-22 01:14:34 +00:00
|
|
|
void MediaPlaylist::AdjustLastSegmentInfoEntryDuration(int64_t next_timestamp) {
|
2018-05-16 17:56:22 +00:00
|
|
|
if (time_scale_ == 0)
|
|
|
|
return;
|
|
|
|
|
2018-05-23 17:24:57 +00:00
|
|
|
const double next_timestamp_seconds =
|
2018-05-16 17:56:22 +00:00
|
|
|
static_cast<double>(next_timestamp) / time_scale_;
|
|
|
|
|
|
|
|
for (auto iter = entries_.rbegin(); iter != entries_.rend(); ++iter) {
|
|
|
|
if (iter->get()->type() == HlsEntry::EntryType::kExtInf) {
|
|
|
|
SegmentInfoEntry* segment_info =
|
|
|
|
reinterpret_cast<SegmentInfoEntry*>(iter->get());
|
2018-05-23 17:24:57 +00:00
|
|
|
|
|
|
|
const double segment_duration_seconds =
|
2019-08-07 23:14:45 +00:00
|
|
|
next_timestamp_seconds -
|
|
|
|
static_cast<double>(segment_info->start_time()) / time_scale_;
|
2019-03-22 00:12:30 +00:00
|
|
|
// It could be negative if timestamp messed up.
|
|
|
|
if (segment_duration_seconds > 0)
|
2019-08-07 23:14:45 +00:00
|
|
|
segment_info->set_duration_seconds(segment_duration_seconds);
|
|
|
|
longest_segment_duration_seconds_ =
|
|
|
|
std::max(longest_segment_duration_seconds_, segment_duration_seconds);
|
2018-05-16 17:56:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 23:14:45 +00:00
|
|
|
// TODO(kqyang): Right now this class manages the segments including the
|
|
|
|
// deletion of segments when it is no longer needed. However, this class does
|
|
|
|
// not have access to the segment file paths, which is already translated to
|
|
|
|
// segment URLs by HlsNotifier. We have to re-generate segment file paths from
|
|
|
|
// segment template here in order to delete the old segments.
|
|
|
|
// To make the pipeline cleaner, we should move all file manipulations including
|
|
|
|
// segment management to an intermediate layer between HlsNotifier and
|
|
|
|
// MediaPlaylist.
|
2017-06-03 00:05:47 +00:00
|
|
|
void MediaPlaylist::SlideWindow() {
|
2018-04-14 01:26:02 +00:00
|
|
|
if (hls_params_.time_shift_buffer_depth <= 0.0 ||
|
|
|
|
hls_params_.playlist_type != HlsPlaylistType::kLive) {
|
2017-06-03 00:05:47 +00:00
|
|
|
return;
|
2017-05-01 20:38:58 +00:00
|
|
|
}
|
2021-08-04 18:56:44 +00:00
|
|
|
DCHECK_GT(time_scale_, 0);
|
2017-06-03 00:05:47 +00:00
|
|
|
|
2019-03-22 00:12:30 +00:00
|
|
|
if (current_buffer_depth_ <= hls_params_.time_shift_buffer_depth)
|
2017-06-03 00:05:47 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Temporary list to hold the EXT-X-KEYs. For example, this allows us to
|
|
|
|
// remove <3> without removing <1> and <2> below (<1> and <2> are moved to the
|
|
|
|
// temporary list and added back later).
|
|
|
|
// #EXT-X-KEY <1>
|
|
|
|
// #EXT-X-KEY <2>
|
|
|
|
// #EXTINF <3>
|
|
|
|
// #EXTINF <4>
|
|
|
|
std::list<std::unique_ptr<HlsEntry>> ext_x_keys;
|
|
|
|
// Consecutive key entries are either fully removed or not removed at all.
|
|
|
|
// Keep track of entry types so we know if it is consecutive key entries.
|
|
|
|
HlsEntry::EntryType prev_entry_type = HlsEntry::EntryType::kExtInf;
|
|
|
|
|
|
|
|
std::list<std::unique_ptr<HlsEntry>>::iterator last = entries_.begin();
|
|
|
|
for (; last != entries_.end(); ++last) {
|
|
|
|
HlsEntry::EntryType entry_type = last->get()->type();
|
|
|
|
if (entry_type == HlsEntry::EntryType::kExtKey) {
|
2017-06-20 23:30:03 +00:00
|
|
|
if (prev_entry_type != HlsEntry::EntryType::kExtKey)
|
|
|
|
ext_x_keys.clear();
|
2017-06-03 00:05:47 +00:00
|
|
|
ext_x_keys.push_back(std::move(*last));
|
2017-06-20 23:30:03 +00:00
|
|
|
} else if (entry_type == HlsEntry::EntryType::kExtDiscontinuity) {
|
|
|
|
++discontinuity_sequence_number_;
|
2017-06-03 00:05:47 +00:00
|
|
|
} else {
|
2023-08-22 01:41:18 +00:00
|
|
|
DCHECK_EQ(static_cast<int>(entry_type),
|
|
|
|
static_cast<int>(HlsEntry::EntryType::kExtInf));
|
2019-03-22 00:12:30 +00:00
|
|
|
|
2018-04-17 22:03:02 +00:00
|
|
|
const SegmentInfoEntry& segment_info =
|
|
|
|
*reinterpret_cast<SegmentInfoEntry*>(last->get());
|
2019-03-22 00:12:30 +00:00
|
|
|
// Remove the current segment only if it falls completely out of time
|
|
|
|
// shift buffer range.
|
|
|
|
const bool segment_within_time_shift_buffer =
|
2019-08-07 23:14:45 +00:00
|
|
|
current_buffer_depth_ - segment_info.duration_seconds() <
|
2019-03-22 00:12:30 +00:00
|
|
|
hls_params_.time_shift_buffer_depth;
|
|
|
|
if (segment_within_time_shift_buffer)
|
2017-06-03 00:05:47 +00:00
|
|
|
break;
|
2019-08-07 23:14:45 +00:00
|
|
|
current_buffer_depth_ -= segment_info.duration_seconds();
|
2018-04-17 22:03:02 +00:00
|
|
|
RemoveOldSegment(segment_info.start_time());
|
|
|
|
media_sequence_number_++;
|
2017-06-03 00:05:47 +00:00
|
|
|
}
|
|
|
|
prev_entry_type = entry_type;
|
|
|
|
}
|
|
|
|
entries_.erase(entries_.begin(), last);
|
|
|
|
// Add key entries back.
|
|
|
|
entries_.insert(entries_.begin(), std::make_move_iterator(ext_x_keys.begin()),
|
|
|
|
std::make_move_iterator(ext_x_keys.end()));
|
2018-04-17 22:03:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 01:14:34 +00:00
|
|
|
void MediaPlaylist::RemoveOldSegment(int64_t start_time) {
|
2018-04-17 22:03:02 +00:00
|
|
|
if (hls_params_.preserved_segments_outside_live_window == 0)
|
|
|
|
return;
|
|
|
|
if (stream_type_ == MediaPlaylistStreamType::kVideoIFramesOnly)
|
|
|
|
return;
|
|
|
|
|
|
|
|
segments_to_be_removed_.push_back(
|
|
|
|
media::GetSegmentName(media_info_.segment_template(), start_time,
|
|
|
|
media_sequence_number_, media_info_.bandwidth()));
|
|
|
|
while (segments_to_be_removed_.size() >
|
|
|
|
hls_params_.preserved_segments_outside_live_window) {
|
|
|
|
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-17 22:03:02 +00:00
|
|
|
segments_to_be_removed_.pop_front();
|
|
|
|
}
|
2017-06-03 00:05:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-25 08:35:44 +00:00
|
|
|
} // namespace hls
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|