7#include <packager/hls/base/media_playlist.h>
15#include <absl/log/check.h>
16#include <absl/log/log.h>
17#include <absl/strings/numbers.h>
18#include <absl/strings/str_format.h>
20#include <packager/file.h>
21#include <packager/hls/base/tag.h>
22#include <packager/macros/logging.h>
23#include <packager/media/base/language_utils.h>
24#include <packager/media/base/muxer_util.h>
25#include <packager/version/version.h>
31int32_t GetTimeScale(
const MediaInfo& media_info) {
32 if (media_info.has_reference_time_scale())
33 return media_info.reference_time_scale();
35 if (media_info.has_video_info())
36 return media_info.video_info().time_scale();
38 if (media_info.has_audio_info())
39 return media_info.audio_info().time_scale();
43std::string AdjustVideoCodec(
const std::string& codec) {
51 std::string adjusted_codec = codec;
52 std::string fourcc = codec.substr(0, 4);
54 adjusted_codec =
"avc1" + codec.substr(4);
55 else if (fourcc ==
"hev1")
56 adjusted_codec =
"hvc1" + codec.substr(4);
57 else if (fourcc ==
"dvhe")
58 adjusted_codec =
"dvh1" + codec.substr(4);
59 if (adjusted_codec != codec) {
60 VLOG(1) <<
"Adusting video codec string from " << codec <<
" to "
63 return adjusted_codec;
72std::string GetLanguage(
const MediaInfo& media_info) {
74 if (media_info.has_audio_info()) {
75 lang = media_info.audio_info().language();
76 }
else if (media_info.has_text_info()) {
77 lang = media_info.text_info().language();
82void AppendExtXMap(
const MediaInfo& media_info, std::string* out) {
83 if (media_info.has_init_segment_url()) {
84 Tag tag(
"#EXT-X-MAP", out);
85 tag.AddQuotedString(
"URI", media_info.init_segment_url().data());
87 }
else if (media_info.has_media_file_url() && media_info.has_init_range()) {
90 Tag tag(
"#EXT-X-MAP", out);
91 tag.AddQuotedString(
"URI", media_info.media_file_url().data());
93 if (media_info.has_init_range()) {
94 const uint64_t begin = media_info.init_range().begin();
95 const uint64_t end = media_info.init_range().end();
96 const uint64_t length = end - begin + 1;
98 tag.AddQuotedNumberPair(
"BYTERANGE", length,
'@', begin);
107std::string CreatePlaylistHeader(
108 const MediaInfo& media_info,
109 int32_t target_duration,
110 HlsPlaylistType type,
111 MediaPlaylist::MediaPlaylistStreamType stream_type,
112 uint32_t media_sequence_number,
113 int discontinuity_sequence_number,
114 std::optional<double> start_time_offset) {
115 const std::string version = GetPackagerVersion();
116 std::string version_line;
117 if (!version.empty()) {
119 absl::StrFormat(
"## Generated with %s version %s\n",
120 GetPackagerProjectUrl().c_str(), version.c_str());
124 std::string header = absl::StrFormat(
128 "#EXT-X-TARGETDURATION:%d\n",
129 version_line.c_str(), target_duration);
132 case HlsPlaylistType::kVod:
133 header +=
"#EXT-X-PLAYLIST-TYPE:VOD\n";
135 case HlsPlaylistType::kEvent:
136 header +=
"#EXT-X-PLAYLIST-TYPE:EVENT\n";
138 case HlsPlaylistType::kLive:
139 if (media_sequence_number > 0) {
140 absl::StrAppendFormat(&header,
"#EXT-X-MEDIA-SEQUENCE:%d\n",
141 media_sequence_number);
143 if (discontinuity_sequence_number > 0) {
144 absl::StrAppendFormat(&header,
"#EXT-X-DISCONTINUITY-SEQUENCE:%d\n",
145 discontinuity_sequence_number);
149 NOTIMPLEMENTED() <<
"Unexpected MediaPlaylistType "
150 <<
static_cast<int>(type);
153 MediaPlaylist::MediaPlaylistStreamType::kVideoIFramesOnly) {
154 absl::StrAppendFormat(&header,
"#EXT-X-I-FRAMES-ONLY\n");
156 if (start_time_offset.has_value()) {
157 absl::StrAppendFormat(&header,
"#EXT-X-START:TIME-OFFSET=%f\n",
158 start_time_offset.value());
163 AppendExtXMap(media_info, &header);
170HlsEntry::HlsEntry(HlsEntry::EntryType type) : type_(type) {}
171HlsEntry::~HlsEntry() {}
173class SegmentInfoEntry :
public HlsEntry {
181 SegmentInfoEntry(
const std::string& file_name,
183 double duration_seconds,
185 uint64_t start_byte_offset,
186 uint64_t segment_file_size,
187 uint64_t previous_segment_end_offset);
189 std::string ToString()
override;
190 int64_t start_time()
const {
return start_time_; }
191 double duration_seconds()
const {
return duration_seconds_; }
192 void set_duration_seconds(
double duration_seconds) {
193 duration_seconds_ = duration_seconds;
197 SegmentInfoEntry(
const SegmentInfoEntry&) =
delete;
198 SegmentInfoEntry& operator=(
const SegmentInfoEntry&) =
delete;
200 const std::string file_name_;
201 const int64_t start_time_;
202 double duration_seconds_;
203 const bool use_byte_range_;
204 const uint64_t start_byte_offset_;
205 const uint64_t segment_file_size_;
206 const uint64_t previous_segment_end_offset_;
209SegmentInfoEntry::SegmentInfoEntry(
const std::string& file_name,
211 double duration_seconds,
213 uint64_t start_byte_offset,
214 uint64_t segment_file_size,
215 uint64_t previous_segment_end_offset)
216 : HlsEntry(HlsEntry::EntryType::kExtInf),
217 file_name_(file_name),
218 start_time_(start_time),
219 duration_seconds_(duration_seconds),
220 use_byte_range_(use_byte_range),
221 start_byte_offset_(start_byte_offset),
222 segment_file_size_(segment_file_size),
223 previous_segment_end_offset_(previous_segment_end_offset) {}
225std::string SegmentInfoEntry::ToString() {
226 std::string result = absl::StrFormat(
"#EXTINF:%.3f,", duration_seconds_);
228 if (use_byte_range_) {
229 absl::StrAppendFormat(&result,
"\n#EXT-X-BYTERANGE:%" PRIu64,
231 if (previous_segment_end_offset_ + 1 != start_byte_offset_) {
232 absl::StrAppendFormat(&result,
"@%" PRIu64, start_byte_offset_);
236 absl::StrAppendFormat(&result,
"\n%s", file_name_.c_str());
241class DiscontinuityEntry :
public HlsEntry {
243 DiscontinuityEntry();
245 std::string ToString()
override;
248 DiscontinuityEntry(
const DiscontinuityEntry&) =
delete;
249 DiscontinuityEntry& operator=(
const DiscontinuityEntry&) =
delete;
252DiscontinuityEntry::DiscontinuityEntry()
253 : HlsEntry(HlsEntry::EntryType::kExtDiscontinuity) {}
255std::string DiscontinuityEntry::ToString() {
256 return "#EXT-X-DISCONTINUITY";
259ProgramDateTimeEntry::ProgramDateTimeEntry(
const absl::Time& program_time)
260 : HlsEntry(HlsEntry::EntryType::kProgramDateTime),
261 program_time_(program_time) {}
263std::string ProgramDateTimeEntry::ToString() {
264 absl::CivilSecond cs =
265 absl::ToCivilSecond(program_time_, absl::UTCTimeZone());
267 int64_t total_ms = absl::ToUnixMillis(program_time_);
268 int ms =
static_cast<int>(total_ms % 1000);
272 return absl::StrFormat(
273 "#EXT-X-PROGRAM-DATE-TIME:%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", cs.year(),
274 cs.month(), cs.day(), cs.hour(), cs.minute(), cs.second(), ms);
277class PlacementOpportunityEntry :
public HlsEntry {
279 PlacementOpportunityEntry();
281 std::string ToString()
override;
284 PlacementOpportunityEntry(
const PlacementOpportunityEntry&) =
delete;
285 PlacementOpportunityEntry& operator=(
const PlacementOpportunityEntry&) =
289PlacementOpportunityEntry::PlacementOpportunityEntry()
290 : HlsEntry(HlsEntry::EntryType::kExtPlacementOpportunity) {}
292std::string PlacementOpportunityEntry::ToString() {
293 return "#EXT-X-PLACEMENT-OPPORTUNITY";
296EncryptionInfoEntry::EncryptionInfoEntry(MediaPlaylist::EncryptionMethod method,
297 const std::string& url,
298 const std::string& key_id,
299 const std::string& iv,
300 const std::string& key_format,
301 const std::string& key_format_versions)
302 : HlsEntry(HlsEntry::EntryType::kExtKey),
307 key_format_(key_format),
308 key_format_versions_(key_format_versions) {}
310std::string EncryptionInfoEntry::ToString() {
314std::string EncryptionInfoEntry::ToString(std::string tag_name) {
315 std::string tag_string;
316 if (tag_name.empty())
317 tag_name =
"#EXT-X-KEY";
318 Tag tag(tag_name, &tag_string);
320 if (method_ == MediaPlaylist::EncryptionMethod::kSampleAes) {
321 tag.AddString(
"METHOD",
"SAMPLE-AES");
322 }
else if (method_ == MediaPlaylist::EncryptionMethod::kAes128) {
323 tag.AddString(
"METHOD",
"AES-128");
324 }
else if (method_ == MediaPlaylist::EncryptionMethod::kSampleAesCenc) {
325 tag.AddString(
"METHOD",
"SAMPLE-AES-CTR");
327 DCHECK(method_ == MediaPlaylist::EncryptionMethod::kNone);
328 tag.AddString(
"METHOD",
"NONE");
331 tag.AddQuotedString(
"URI", url_);
333 if (!key_id_.empty()) {
334 tag.AddString(
"KEYID", key_id_);
337 tag.AddString(
"IV", iv_);
339 if (!key_format_versions_.empty()) {
340 tag.AddQuotedString(
"KEYFORMATVERSIONS", key_format_versions_);
342 if (!key_format_.empty()) {
343 tag.AddQuotedString(
"KEYFORMAT", key_format_);
349MediaPlaylist::MediaPlaylist(
const HlsParams& hls_params,
350 const std::string& file_name,
351 const std::string& name,
352 const std::string& group_id)
353 : hls_params_(hls_params),
354 file_name_(file_name),
357 media_sequence_number_(hls_params_.media_sequence_number),
358 reference_time_(absl::InfinitePast()) {
360 if (media_sequence_number_ > 0)
361 entries_.emplace_back(
new DiscontinuityEntry());
364MediaPlaylist::~MediaPlaylist() {}
367 MediaPlaylistStreamType stream_type) {
368 stream_type_ = stream_type;
380 const std::vector<std::string>& characteristics) {
381 characteristics_ = characteristics;
385 forced_subtitle_ = forced_subtitle;
389 MediaPlaylist::EncryptionMethod method,
390 const std::string& url,
391 const std::string& key_id,
392 const std::string& iv,
393 const std::string& key_format,
394 const std::string& key_format_versions) {
396 method, url, key_id, iv, key_format, key_format_versions));
400 const int32_t time_scale = GetTimeScale(media_info);
401 if (time_scale == 0) {
402 LOG(ERROR) <<
"MediaInfo does not contain a valid timescale.";
406 if (media_info.has_video_info()) {
407 stream_type_ = MediaPlaylistStreamType::kVideo;
408 codec_ = AdjustVideoCodec(media_info.video_info().codec());
409 if (media_info.video_info().has_supplemental_codec() &&
410 media_info.video_info().has_compatible_brand()) {
411 supplemental_codec_ =
412 AdjustVideoCodec(media_info.video_info().supplemental_codec());
413 compatible_brand_ =
static_cast<media::FourCC
>(
414 media_info.video_info().compatible_brand());
416 }
else if (media_info.has_audio_info()) {
417 stream_type_ = MediaPlaylistStreamType::kAudio;
418 codec_ = media_info.audio_info().codec();
420 stream_type_ = MediaPlaylistStreamType::kSubtitle;
421 codec_ = media_info.text_info().codec();
424 time_scale_ = time_scale;
425 media_info_ = media_info;
426 language_ = GetLanguage(media_info);
427 use_byte_range_ = !media_info_.has_segment_template_url() &&
428 media_info_.container_type() != MediaInfo::CONTAINER_TEXT;
430 std::vector<std::string>(media_info_.hls_characteristics().begin(),
431 media_info_.hls_characteristics().end());
433 forced_subtitle_ = media_info_.forced_subtitle();
439 if (media_info_.has_video_info())
440 media_info_.mutable_video_info()->set_frame_duration(sample_duration);
446 uint64_t start_byte_offset,
448 if (stream_type_ == MediaPlaylistStreamType::kVideoIFramesOnly) {
449 if (key_frames_.empty())
452 AdjustLastSegmentInfoEntryDuration(key_frames_.front().timestamp);
454 for (
auto iter = key_frames_.begin(); iter != key_frames_.end(); ++iter) {
457 const int64_t next_timestamp = std::next(iter) == key_frames_.end()
458 ? (start_time + duration)
459 : std::next(iter)->timestamp;
460 AddSegmentInfoEntry(file_name, iter->timestamp,
461 next_timestamp - iter->timestamp,
462 iter->start_byte_offset, iter->size);
467 return AddSegmentInfoEntry(file_name, start_time, duration, start_byte_offset,
472 reference_time_ = reference_time;
476 uint64_t start_byte_offset,
478 if (stream_type_ != MediaPlaylistStreamType::kVideoIFramesOnly) {
479 if (stream_type_ != MediaPlaylistStreamType::kVideo) {
481 <<
"I-Frames Only playlist applies to video renditions only.";
484 stream_type_ = MediaPlaylistStreamType::kVideoIFramesOnly;
485 use_byte_range_ =
true;
487 key_frames_.push_back({timestamp, start_byte_offset, size, std::string(
"")});
491 const std::string& url,
492 const std::string& key_id,
493 const std::string& iv,
494 const std::string& key_format,
495 const std::string& key_format_versions) {
496 if (!inserted_discontinuity_tag_) {
499 if (!entries_.empty())
500 entries_.emplace_back(
new DiscontinuityEntry());
501 inserted_discontinuity_tag_ =
true;
504 method, url, key_id, iv, key_format, key_format_versions));
508 entries_.emplace_back(
new PlacementOpportunityEntry());
512 bool event_to_vod_on_end_of_stream,
514 if (!target_duration_set_) {
518 HlsPlaylistType playlist_type = hls_params_.playlist_type;
519 if (event_to_vod_on_end_of_stream && end_stream &&
520 playlist_type == HlsPlaylistType::kEvent) {
521 playlist_type = HlsPlaylistType::kVod;
524 std::string content = CreatePlaylistHeader(
525 media_info_, target_duration_, playlist_type, stream_type_,
526 media_sequence_number_, discontinuity_sequence_number_,
527 hls_params_.start_time_offset);
529 for (
const auto& entry : entries_)
530 absl::StrAppendFormat(&content,
"%s\n", entry->ToString().c_str());
532 if (playlist_type == HlsPlaylistType::kVod) {
533 content +=
"#EXT-X-ENDLIST\n";
536 if (!File::WriteFileAtomically(file_path.string().c_str(), content)) {
537 LOG(ERROR) <<
"Failed to write playlist to: " << file_path.string();
544 if (media_info_.has_bandwidth())
545 return media_info_.bandwidth();
546 return bandwidth_estimator_.
Max();
550 return bandwidth_estimator_.
Estimate();
554 return longest_segment_duration_seconds_;
558 if (target_duration_set_) {
559 if (target_duration_ == target_duration)
561 VLOG(1) <<
"Updating target duration from " << target_duration_ <<
" to "
564 target_duration_ = target_duration;
565 target_duration_set_ =
true;
569 return media_info_.audio_info().num_channels();
573 return media_info_.audio_info().codec_specific_data().ec3_joc_complexity();
577 return media_info_.audio_info().codec_specific_data().ac4_ims_flag();
581 return media_info_.audio_info().codec_specific_data().ac4_cbi_flag();
585 uint32_t* height)
const {
588 if (media_info_.has_video_info()) {
589 const double pixel_aspect_ratio =
590 media_info_.video_info().pixel_height() > 0
591 ?
static_cast<double>(media_info_.video_info().pixel_width()) /
592 media_info_.video_info().pixel_height()
594 *width =
static_cast<uint32_t
>(media_info_.video_info().width() *
596 *height = media_info_.video_info().height();
604 if (codec_.find(
"dvh") == 0)
609 switch (media_info_.video_info().transfer_characteristics()) {
617 if (!supplemental_codec_.empty() &&
618 compatible_brand_ == media::FOURCC_db4g)
636 if (media_info_.video_info().frame_duration() == 0)
638 return static_cast<double>(time_scale_) /
639 media_info_.video_info().frame_duration();
642void MediaPlaylist::AddSegmentInfoEntry(
const std::string& segment_file_name,
645 uint64_t start_byte_offset,
647 if (time_scale_ == 0) {
648 LOG(WARNING) <<
"Timescale is not set and the duration for " << duration
649 <<
" cannot be calculated. The output will be wrong.";
651 entries_.emplace_back(
new SegmentInfoEntry(
652 segment_file_name, 0.0, 0.0, use_byte_range_, start_byte_offset, size,
653 previous_segment_end_offset_));
664 const double segment_duration_seconds =
665 static_cast<double>(duration) / time_scale_;
666 longest_segment_duration_seconds_ =
667 std::max(longest_segment_duration_seconds_, segment_duration_seconds);
668 bandwidth_estimator_.
AddBlock(size, segment_duration_seconds);
669 current_buffer_depth_ += segment_duration_seconds;
671 if (!entries_.empty() &&
672 entries_.back()->type() == HlsEntry::EntryType::kExtInf) {
673 const SegmentInfoEntry* segment_info =
674 static_cast<SegmentInfoEntry*
>(entries_.back().get());
675 if (segment_info->start_time() > start_time) {
677 <<
"Insert a discontinuity tag after the segment with start time "
678 << segment_info->start_time() <<
" as the next segment starts at "
679 << start_time <<
".";
680 entries_.emplace_back(
new DiscontinuityEntry());
684 if (hls_params_.add_program_date_time &&
685 reference_time_ != absl::InfinitePast()) {
688 bool is_first_segment =
true;
689 bool is_discontinuity =
false;
690 if (!entries_.empty()) {
691 for (
auto it = entries_.rbegin(); it != entries_.rend(); ++it) {
692 if ((*it)->type() == HlsEntry::EntryType::kExtInf) {
693 is_first_segment =
false;
698 const auto& last = *entries_.back();
699 if (last.type() == HlsEntry::EntryType::kExtDiscontinuity) {
700 is_discontinuity =
true;
701 }
else if (entries_.size() >= 2) {
702 const auto& second_last = **std::prev(entries_.cend(), 2);
703 if (last.type() == HlsEntry::EntryType::kExtKey &&
704 second_last.type() == HlsEntry::EntryType::kExtDiscontinuity) {
705 is_discontinuity =
true;
710 if (is_first_segment || is_discontinuity) {
711 const absl::Time program_time =
713 absl::Seconds(
static_cast<double>(start_time) / time_scale_);
714 entries_.emplace_back(
new ProgramDateTimeEntry(program_time));
718 entries_.emplace_back(
new SegmentInfoEntry(
719 segment_file_name, start_time, segment_duration_seconds, use_byte_range_,
720 start_byte_offset, size, previous_segment_end_offset_));
721 previous_segment_end_offset_ = start_byte_offset + size - 1;
724void MediaPlaylist::AdjustLastSegmentInfoEntryDuration(int64_t next_timestamp) {
725 if (time_scale_ == 0)
728 const double next_timestamp_seconds =
729 static_cast<double>(next_timestamp) / time_scale_;
731 for (
auto iter = entries_.rbegin(); iter != entries_.rend(); ++iter) {
732 if (iter->get()->type() == HlsEntry::EntryType::kExtInf) {
733 SegmentInfoEntry* segment_info =
734 reinterpret_cast<SegmentInfoEntry*
>(iter->get());
736 const double segment_duration_seconds =
737 next_timestamp_seconds -
738 static_cast<double>(segment_info->start_time()) / time_scale_;
740 if (segment_duration_seconds > 0)
741 segment_info->set_duration_seconds(segment_duration_seconds);
742 longest_segment_duration_seconds_ =
743 std::max(longest_segment_duration_seconds_, segment_duration_seconds);
757void MediaPlaylist::SlideWindow() {
758 if (hls_params_.time_shift_buffer_depth <= 0.0 ||
759 hls_params_.playlist_type != HlsPlaylistType::kLive) {
762 DCHECK_GT(time_scale_, 0);
764 if (current_buffer_depth_ <= hls_params_.time_shift_buffer_depth)
774 std::list<std::unique_ptr<HlsEntry>> ext_x_keys;
777 HlsEntry::EntryType prev_entry_type = HlsEntry::EntryType::kExtInf;
779 std::list<std::unique_ptr<HlsEntry>>::iterator last = entries_.begin();
780 for (; last != entries_.end(); ++last) {
781 HlsEntry::EntryType entry_type = last->get()->type();
782 if (entry_type == HlsEntry::EntryType::kExtKey) {
783 if (prev_entry_type != HlsEntry::EntryType::kExtKey)
785 ext_x_keys.push_back(std::move(*last));
786 }
else if (entry_type == HlsEntry::EntryType::kExtDiscontinuity) {
787 ++discontinuity_sequence_number_;
789 DCHECK_EQ(
static_cast<int>(entry_type),
790 static_cast<int>(HlsEntry::EntryType::kExtInf));
792 const SegmentInfoEntry& segment_info =
793 *
reinterpret_cast<SegmentInfoEntry*
>(last->get());
796 const bool segment_within_time_shift_buffer =
797 current_buffer_depth_ - segment_info.duration_seconds() <
798 hls_params_.time_shift_buffer_depth;
799 if (segment_within_time_shift_buffer)
801 current_buffer_depth_ -= segment_info.duration_seconds();
802 RemoveOldSegment(segment_info.start_time());
803 media_sequence_number_++;
805 prev_entry_type = entry_type;
807 entries_.erase(entries_.begin(), last);
809 entries_.insert(entries_.begin(), std::make_move_iterator(ext_x_keys.begin()),
810 std::make_move_iterator(ext_x_keys.end()));
813void MediaPlaylist::RemoveOldSegment(int64_t start_time) {
814 if (hls_params_.preserved_segments_outside_live_window == 0)
816 if (stream_type_ == MediaPlaylistStreamType::kVideoIFramesOnly)
819 segments_to_be_removed_.push_back(media::GetSegmentName(
820 media_info_.segment_template(), start_time, media_sequence_number_ + 1,
821 media_info_.bandwidth()));
822 while (segments_to_be_removed_.size() >
823 hls_params_.preserved_segments_outside_live_window) {
824 VLOG(2) <<
"Deleting " << segments_to_be_removed_.front();
825 if (!File::Delete(segments_to_be_removed_.front().c_str())) {
826 LOG(WARNING) <<
"Failed to delete " << segments_to_be_removed_.front()
827 <<
"; Will retry later.";
830 segments_to_be_removed_.pop_front();
void AddBlock(uint64_t size_in_bytes, double duration)
uint64_t Estimate() const
All the methods that are virtual are virtual for mocking.
std::string LanguageToShortestForm(const std::string &language)