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 <filesystem>
11#include <list>
12#include <memory>
13#include <string>
14#include <vector>
15
16#include <packager/hls_params.h>
17#include <packager/macros/classes.h>
18#include <packager/mpd/base/bandwidth_estimator.h>
19#include <packager/mpd/base/media_info.pb.h>
20#include "packager/media/base/fourccs.h"
21
22namespace shaka {
23
24class File;
25
26namespace hls {
27
28class HlsEntry {
29 public:
30 enum class EntryType {
31 kExtInf,
32 kExtKey,
33 kExtDiscontinuity,
34 kExtPlacementOpportunity,
35 };
36 virtual ~HlsEntry();
37
38 EntryType type() const { return type_; }
39 virtual std::string ToString() = 0;
40
41 protected:
42 explicit HlsEntry(EntryType type);
43
44 private:
45 EntryType type_;
46};
47
50 public:
51 enum class MediaPlaylistStreamType {
52 kUnknown,
53 kAudio,
54 kVideo,
55 kVideoIFramesOnly,
56 kSubtitle,
57 };
58 enum class EncryptionMethod {
59 kNone, // No encryption, i.e. clear.
60 kAes128, // Completely encrypted using AES-CBC.
61 kSampleAes, // Encrypted using SAMPLE-AES method.
62 kSampleAesCenc, // 'cenc' encrypted content.
63 };
64
73 MediaPlaylist(const HlsParams& hls_params,
74 const std::string& file_name,
75 const std::string& name,
76 const std::string& group_id);
77 virtual ~MediaPlaylist();
78
79 const std::string& file_name() const { return file_name_; }
80 const std::string& name() const { return name_; }
81 const std::string& group_id() const { return group_id_; }
82 MediaPlaylistStreamType stream_type() const { return stream_type_; }
83 const std::string& codec() const { return codec_; }
84 const std::string& supplemental_codec() const { return supplemental_codec_; }
85 const media::FourCC& compatible_brand() const { return compatible_brand_; }
86 const std::list<std::unique_ptr<HlsEntry>>& entries() const {
87 return entries_;
88 }
89
91 void SetStreamTypeForTesting(MediaPlaylistStreamType stream_type);
92
94 void SetCodecForTesting(const std::string& codec);
95
97 void SetLanguageForTesting(const std::string& language);
98
100 void SetForcedSubtitleForTesting(const bool forced_subtitle);
101
104 const std::vector<std::string>& characteristics);
105
107 void AddEncryptionInfoForTesting(MediaPlaylist::EncryptionMethod method,
108 const std::string& url,
109 const std::string& key_id,
110 const std::string& iv,
111 const std::string& key_format,
112 const std::string& key_format_versions);
113
118 virtual bool SetMediaInfo(const MediaInfo& media_info);
119 MediaInfo GetMediaInfo() const { return media_info_; }
120
125 virtual void SetSampleDuration(int32_t sample_duration);
126
134 virtual void AddSegment(const std::string& file_name,
135 int64_t start_time,
136 int64_t duration,
137 uint64_t start_byte_offset,
138 uint64_t size);
139
146 virtual void AddKeyFrame(int64_t timestamp,
147 uint64_t start_byte_offset,
148 uint64_t size);
149
161 virtual void AddEncryptionInfo(EncryptionMethod method,
162 const std::string& url,
163 const std::string& key_id,
164 const std::string& iv,
165 const std::string& key_format,
166 const std::string& key_format_versions);
167
170 virtual void AddPlacementOpportunity();
171
182 virtual bool WriteToFile(const std::filesystem::path& file_path);
183
187 virtual uint64_t MaxBitrate() const;
188
192 virtual uint64_t AvgBitrate() const;
193
196 virtual double GetLongestSegmentDuration() const;
197
205 virtual void SetTargetDuration(int32_t target_duration);
206
208 virtual int GetNumChannels() const;
209
213 virtual int GetEC3JocComplexity() const;
214
218 virtual bool GetAC4ImsFlag() const;
219
223 virtual bool GetAC4CbiFlag() const;
224
227 virtual bool GetDisplayResolution(uint32_t* width, uint32_t* height) const;
228
230 virtual std::string GetVideoRange() const;
231
233 virtual double GetFrameRate() const;
234
237 const std::string& language() const { return language_; }
238
239 const std::vector<std::string>& characteristics() const {
240 return characteristics_;
241 }
242
243 bool forced_subtitle() const { return forced_subtitle_; }
244
245 bool is_dvs() const {
246 // HLS Authoring Specification for Apple Devices
247 // https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices#overview
248 // Section 2.12.
249 const char DVS_CHARACTERISTICS[] = "public.accessibility.describes-video";
250 return characteristics_.size() == 1 &&
251 characteristics_[0] == DVS_CHARACTERISTICS;
252 }
253
254 private:
255 // Add a SegmentInfoEntry (#EXTINF).
256 void AddSegmentInfoEntry(const std::string& segment_file_name,
257 int64_t start_time,
258 int64_t duration,
259 uint64_t start_byte_offset,
260 uint64_t size);
261 // Adjust the duration of the last SegmentInfoEntry to end on
262 // |next_timestamp|.
263 void AdjustLastSegmentInfoEntryDuration(int64_t next_timestamp);
264 // Remove elements from |entries_| for live profile. Increments
265 // |sequence_number_| by the number of segments removed.
266 void SlideWindow();
267 // Remove the segment specified by |start_time|. The actual deletion can
268 // happen at a later time depending on the value of
269 // |preserved_segment_outside_live_window| in |hls_params_|.
270 void RemoveOldSegment(int64_t start_time);
271
272 const HlsParams& hls_params_;
273 // Mainly for MasterPlaylist to use these values.
274 const std::string file_name_;
275 const std::string name_;
276 const std::string group_id_;
277 MediaInfo media_info_;
278 MediaPlaylistStreamType stream_type_ = MediaPlaylistStreamType::kUnknown;
279 // Whether to use byte range for SegmentInfoEntry.
280 bool use_byte_range_ = false;
281 std::string codec_;
282 std::string supplemental_codec_;
283 media::FourCC compatible_brand_;
284 std::string language_;
285 std::vector<std::string> characteristics_;
286 bool forced_subtitle_ = false;
287 uint32_t media_sequence_number_ = 0;
288 bool inserted_discontinuity_tag_ = false;
289 int discontinuity_sequence_number_ = 0;
290
291 double longest_segment_duration_seconds_ = 0.0;
292 int32_t time_scale_ = 0;
293
294 BandwidthEstimator bandwidth_estimator_;
295
296 // Cache the previous calls AddSegment() end offset. This is used to construct
297 // SegmentInfoEntry.
298 uint64_t previous_segment_end_offset_ = 0;
299
300 // See SetTargetDuration() comments.
301 bool target_duration_set_ = false;
302 int32_t target_duration_ = 0;
303
304 // TODO(kqyang): This could be managed better by a separate class, than having
305 // all them managed in MediaPlaylist.
306 std::list<std::unique_ptr<HlsEntry>> entries_;
307 double current_buffer_depth_ = 0;
308 // A list to hold the file names of the segments to be removed temporarily.
309 // Once a file is actually removed, it is removed from the list.
310 std::list<std::string> segments_to_be_removed_;
311
312 // Used by kVideoIFrameOnly playlists to track the i-frames (key frames).
313 struct KeyFrameInfo {
314 int64_t timestamp;
315 uint64_t start_byte_offset;
316 uint64_t size;
317 std::string segment_file_name;
318 };
319 std::list<KeyFrameInfo> key_frames_;
320
321 DISALLOW_COPY_AND_ASSIGN(MediaPlaylist);
322};
323
325 public:
326 EncryptionInfoEntry(MediaPlaylist::EncryptionMethod method,
327 const std::string& url,
328 const std::string& key_id,
329 const std::string& iv,
330 const std::string& key_format,
331 const std::string& key_format_versions);
332
333 std::string ToString() override;
334 std::string ToString(std::string);
335
336 private:
338 EncryptionInfoEntry& operator=(const EncryptionInfoEntry&) = delete;
339
340 const MediaPlaylist::EncryptionMethod method_;
341 const std::string url_;
342 const std::string key_id_;
343 const std::string iv_;
344 const std::string key_format_;
345 const std::string key_format_versions_;
346};
347
348} // namespace hls
349} // namespace shaka
350
351#endif // PACKAGER_HLS_BASE_MEDIA_PLAYLIST_H_
Methods are virtual for mocking.
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 uint64_t MaxBitrate() const
void SetLanguageForTesting(const std::string &language)
For testing only.
virtual int GetNumChannels() const
virtual bool WriteToFile(const std::filesystem::path &file_path)
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.