Shaka Packager SDK
Loading...
Searching...
No Matches
media_playlist.h
1// Copyright 2016 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#ifndef PACKAGER_HLS_BASE_MEDIA_PLAYLIST_H_
8#define PACKAGER_HLS_BASE_MEDIA_PLAYLIST_H_
9
10#include <cstdint>
11#include <filesystem>
12#include <list>
13#include <memory>
14#include <string>
15#include <vector>
16
17#include <packager/hls_params.h>
18#include <packager/macros/classes.h>
19#include <packager/mpd/base/bandwidth_estimator.h>
20#include <packager/mpd/base/media_info.pb.h>
21#include "packager/media/base/fourccs.h"
22
23namespace shaka {
24
25class File;
26
27namespace hls {
28
29class HlsEntry {
30 public:
31 enum class EntryType {
32 kExtInf,
33 kExtKey,
34 kExtDiscontinuity,
35 kExtPlacementOpportunity,
36 kProgramDateTime,
37 };
38 virtual ~HlsEntry();
39
40 EntryType type() const { return type_; }
41 virtual std::string ToString() = 0;
42
43 protected:
44 explicit HlsEntry(EntryType type);
45
46 private:
47 EntryType type_;
48};
49
52 public:
53 enum class MediaPlaylistStreamType {
54 kUnknown,
55 kAudio,
56 kVideo,
57 kVideoIFramesOnly,
58 kSubtitle,
59 };
60 enum class EncryptionMethod {
61 kNone, // No encryption, i.e. clear.
62 kAes128, // Completely encrypted using AES-CBC.
63 kSampleAes, // Encrypted using SAMPLE-AES method.
64 kSampleAesCenc, // 'cenc' encrypted content.
65 };
66
75 MediaPlaylist(const HlsParams& hls_params,
76 const std::string& file_name,
77 const std::string& name,
78 const std::string& group_id);
79 virtual ~MediaPlaylist();
80
81 const std::string& file_name() const { return file_name_; }
82 const std::string& name() const { return name_; }
83 const std::string& group_id() const { return group_id_; }
84 MediaPlaylistStreamType stream_type() const { return stream_type_; }
85 const std::string& codec() const { return codec_; }
86 const std::string& supplemental_codec() const { return supplemental_codec_; }
87 const media::FourCC& compatible_brand() const { return compatible_brand_; }
88 const std::list<std::unique_ptr<HlsEntry>>& entries() const {
89 return entries_;
90 }
91
93 void SetStreamTypeForTesting(MediaPlaylistStreamType stream_type);
94
96 void SetCodecForTesting(const std::string& codec);
97
99 void SetLanguageForTesting(const std::string& language);
100
102 void SetForcedSubtitleForTesting(const bool forced_subtitle);
103
106 const std::vector<std::string>& characteristics);
107
109 void AddEncryptionInfoForTesting(MediaPlaylist::EncryptionMethod method,
110 const std::string& url,
111 const std::string& key_id,
112 const std::string& iv,
113 const std::string& key_format,
114 const std::string& key_format_versions);
115
120 virtual bool SetMediaInfo(const MediaInfo& media_info);
121 MediaInfo GetMediaInfo() const { return media_info_; }
122
127 virtual void SetSampleDuration(int32_t sample_duration);
128
136 virtual void AddSegment(const std::string& file_name,
137 int64_t start_time,
138 int64_t duration,
139 uint64_t start_byte_offset,
140 uint64_t size);
141
144 virtual void SetReferenceTime(const absl::Time& reference_time);
145
152 virtual void AddKeyFrame(int64_t timestamp,
153 uint64_t start_byte_offset,
154 uint64_t size);
155
167 virtual void AddEncryptionInfo(EncryptionMethod method,
168 const std::string& url,
169 const std::string& key_id,
170 const std::string& iv,
171 const std::string& key_format,
172 const std::string& key_format_versions);
173
176 virtual void AddPlacementOpportunity();
177
192 virtual bool WriteToFile(const std::filesystem::path& file_path,
193 bool event_to_vod_on_end_of_stream,
194 bool end_stream);
195
199 virtual uint64_t MaxBitrate() const;
200
204 virtual uint64_t AvgBitrate() const;
205
208 virtual double GetLongestSegmentDuration() const;
209
217 virtual void SetTargetDuration(int32_t target_duration);
218
220 virtual int GetNumChannels() const;
221
225 virtual int GetEC3JocComplexity() const;
226
230 virtual bool GetAC4ImsFlag() const;
231
235 virtual bool GetAC4CbiFlag() const;
236
239 virtual bool GetDisplayResolution(uint32_t* width, uint32_t* height) const;
240
242 virtual std::string GetVideoRange() const;
243
245 virtual double GetFrameRate() const;
246
249 const std::string& language() const { return language_; }
250
251 const std::vector<std::string>& characteristics() const {
252 return characteristics_;
253 }
254
255 bool forced_subtitle() const { return forced_subtitle_; }
256
257 bool is_dvs() const {
258 // HLS Authoring Specification for Apple Devices
259 // https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices#overview
260 // Section 2.12.
261 const char DVS_CHARACTERISTICS[] = "public.accessibility.describes-video";
262 return characteristics_.size() == 1 &&
263 characteristics_[0] == DVS_CHARACTERISTICS;
264 }
265
266 private:
267 // Add a SegmentInfoEntry (#EXTINF).
268 void AddSegmentInfoEntry(const std::string& segment_file_name,
269 int64_t start_time,
270 int64_t duration,
271 uint64_t start_byte_offset,
272 uint64_t size);
273 // Adjust the duration of the last SegmentInfoEntry to end on
274 // |next_timestamp|.
275 void AdjustLastSegmentInfoEntryDuration(int64_t next_timestamp);
276 // Remove elements from |entries_| for live profile. Increments
277 // |sequence_number_| by the number of segments removed.
278 void SlideWindow();
279 // Remove the segment specified by |start_time|. The actual deletion can
280 // happen at a later time depending on the value of
281 // |preserved_segment_outside_live_window| in |hls_params_|.
282 void RemoveOldSegment(int64_t start_time);
283
284 const HlsParams& hls_params_;
285 // Mainly for MasterPlaylist to use these values.
286 const std::string file_name_;
287 const std::string name_;
288 const std::string group_id_;
289 MediaInfo media_info_;
290 MediaPlaylistStreamType stream_type_ = MediaPlaylistStreamType::kUnknown;
291 // Whether to use byte range for SegmentInfoEntry.
292 bool use_byte_range_ = false;
293 std::string codec_;
294 std::string supplemental_codec_;
295 media::FourCC compatible_brand_;
296 std::string language_;
297 std::vector<std::string> characteristics_;
298 bool forced_subtitle_ = false;
299 uint32_t media_sequence_number_ = 0;
300 bool inserted_discontinuity_tag_ = false;
301 int discontinuity_sequence_number_ = 0;
302
303 double longest_segment_duration_seconds_ = 0.0;
304 int32_t time_scale_ = 0;
305
306 BandwidthEstimator bandwidth_estimator_;
307
308 // Cache the previous calls AddSegment() end offset. This is used to construct
309 // SegmentInfoEntry.
310 uint64_t previous_segment_end_offset_ = 0;
311
312 // See SetTargetDuration() comments.
313 bool target_duration_set_ = false;
314 int32_t target_duration_ = 0;
315
316 // TODO(kqyang): This could be managed better by a separate class, than having
317 // all them managed in MediaPlaylist.
318 std::list<std::unique_ptr<HlsEntry>> entries_;
319 double current_buffer_depth_ = 0;
320 // A list to hold the file names of the segments to be removed temporarily.
321 // Once a file is actually removed, it is removed from the list.
322 std::list<std::string> segments_to_be_removed_;
323
324 // This is the wall clock time when media timestamp is 0.
325 absl::Time reference_time_;
326
327 // Used by kVideoIFrameOnly playlists to track the i-frames (key frames).
328 struct KeyFrameInfo {
329 int64_t timestamp;
330 uint64_t start_byte_offset;
331 uint64_t size;
332 std::string segment_file_name;
333 };
334 std::list<KeyFrameInfo> key_frames_;
335
336 DISALLOW_COPY_AND_ASSIGN(MediaPlaylist);
337};
338
340 public:
341 explicit ProgramDateTimeEntry(const absl::Time& program_time);
342
343 std::string ToString() override;
344
345 private:
347 ProgramDateTimeEntry& operator=(const ProgramDateTimeEntry&) = delete;
348
349 const absl::Time program_time_;
350};
351
353 public:
354 EncryptionInfoEntry(MediaPlaylist::EncryptionMethod method,
355 const std::string& url,
356 const std::string& key_id,
357 const std::string& iv,
358 const std::string& key_format,
359 const std::string& key_format_versions);
360
361 std::string ToString() override;
362 std::string ToString(std::string);
363
364 private:
366 EncryptionInfoEntry& operator=(const EncryptionInfoEntry&) = delete;
367
368 const MediaPlaylist::EncryptionMethod method_;
369 const std::string url_;
370 const std::string key_id_;
371 const std::string iv_;
372 const std::string key_format_;
373 const std::string key_format_versions_;
374};
375
376} // namespace hls
377} // namespace shaka
378
379#endif // PACKAGER_HLS_BASE_MEDIA_PLAYLIST_H_
Methods are virtual for mocking.
virtual bool WriteToFile(const std::filesystem::path &file_path, bool event_to_vod_on_end_of_stream, bool end_stream)
virtual bool GetAC4ImsFlag() const
void SetStreamTypeForTesting(MediaPlaylistStreamType stream_type)
For testing only.
virtual void AddEncryptionInfo(EncryptionMethod method, const std::string &url, const std::string &key_id, const std::string &iv, const std::string &key_format, const std::string &key_format_versions)
void SetCharacteristicsForTesting(const std::vector< std::string > &characteristics)
For testing only.
virtual void AddKeyFrame(int64_t timestamp, uint64_t start_byte_offset, uint64_t size)
virtual double GetFrameRate() const
virtual uint64_t AvgBitrate() const
virtual bool GetDisplayResolution(uint32_t *width, uint32_t *height) const
virtual double GetLongestSegmentDuration() const
virtual int GetEC3JocComplexity() const
virtual void SetReferenceTime(const absl::Time &reference_time)
virtual uint64_t MaxBitrate() const
void SetLanguageForTesting(const std::string &language)
For testing only.
virtual int GetNumChannels() const
virtual std::string GetVideoRange() const
void SetCodecForTesting(const std::string &codec)
For testing only.
virtual void AddPlacementOpportunity()
const std::string & language() const
void AddEncryptionInfoForTesting(MediaPlaylist::EncryptionMethod method, const std::string &url, const std::string &key_id, const std::string &iv, const std::string &key_format, const std::string &key_format_versions)
For testing only.
virtual bool SetMediaInfo(const MediaInfo &media_info)
virtual void SetTargetDuration(int32_t target_duration)
virtual bool GetAC4CbiFlag() const
void SetForcedSubtitleForTesting(const bool forced_subtitle)
For testing only.
virtual void SetSampleDuration(int32_t sample_duration)
virtual void AddSegment(const std::string &file_name, int64_t start_time, int64_t duration, uint64_t start_byte_offset, uint64_t size)
All the methods that are virtual are virtual for mocking.