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
188 virtual bool WriteToFile(const std::filesystem::path& file_path);
189
193 virtual uint64_t MaxBitrate() const;
194
198 virtual uint64_t AvgBitrate() const;
199
202 virtual double GetLongestSegmentDuration() const;
203
211 virtual void SetTargetDuration(int32_t target_duration);
212
214 virtual int GetNumChannels() const;
215
219 virtual int GetEC3JocComplexity() const;
220
224 virtual bool GetAC4ImsFlag() const;
225
229 virtual bool GetAC4CbiFlag() const;
230
233 virtual bool GetDisplayResolution(uint32_t* width, uint32_t* height) const;
234
236 virtual std::string GetVideoRange() const;
237
239 virtual double GetFrameRate() const;
240
243 const std::string& language() const { return language_; }
244
245 const std::vector<std::string>& characteristics() const {
246 return characteristics_;
247 }
248
249 bool forced_subtitle() const { return forced_subtitle_; }
250
251 bool is_dvs() const {
252 // HLS Authoring Specification for Apple Devices
253 // https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices#overview
254 // Section 2.12.
255 const char DVS_CHARACTERISTICS[] = "public.accessibility.describes-video";
256 return characteristics_.size() == 1 &&
257 characteristics_[0] == DVS_CHARACTERISTICS;
258 }
259
260 private:
261 // Add a SegmentInfoEntry (#EXTINF).
262 void AddSegmentInfoEntry(const std::string& segment_file_name,
263 int64_t start_time,
264 int64_t duration,
265 uint64_t start_byte_offset,
266 uint64_t size);
267 // Adjust the duration of the last SegmentInfoEntry to end on
268 // |next_timestamp|.
269 void AdjustLastSegmentInfoEntryDuration(int64_t next_timestamp);
270 // Remove elements from |entries_| for live profile. Increments
271 // |sequence_number_| by the number of segments removed.
272 void SlideWindow();
273 // Remove the segment specified by |start_time|. The actual deletion can
274 // happen at a later time depending on the value of
275 // |preserved_segment_outside_live_window| in |hls_params_|.
276 void RemoveOldSegment(int64_t start_time);
277
278 const HlsParams& hls_params_;
279 // Mainly for MasterPlaylist to use these values.
280 const std::string file_name_;
281 const std::string name_;
282 const std::string group_id_;
283 MediaInfo media_info_;
284 MediaPlaylistStreamType stream_type_ = MediaPlaylistStreamType::kUnknown;
285 // Whether to use byte range for SegmentInfoEntry.
286 bool use_byte_range_ = false;
287 std::string codec_;
288 std::string supplemental_codec_;
289 media::FourCC compatible_brand_;
290 std::string language_;
291 std::vector<std::string> characteristics_;
292 bool forced_subtitle_ = false;
293 uint32_t media_sequence_number_ = 0;
294 bool inserted_discontinuity_tag_ = false;
295 int discontinuity_sequence_number_ = 0;
296
297 double longest_segment_duration_seconds_ = 0.0;
298 int32_t time_scale_ = 0;
299
300 BandwidthEstimator bandwidth_estimator_;
301
302 // Cache the previous calls AddSegment() end offset. This is used to construct
303 // SegmentInfoEntry.
304 uint64_t previous_segment_end_offset_ = 0;
305
306 // See SetTargetDuration() comments.
307 bool target_duration_set_ = false;
308 int32_t target_duration_ = 0;
309
310 // TODO(kqyang): This could be managed better by a separate class, than having
311 // all them managed in MediaPlaylist.
312 std::list<std::unique_ptr<HlsEntry>> entries_;
313 double current_buffer_depth_ = 0;
314 // A list to hold the file names of the segments to be removed temporarily.
315 // Once a file is actually removed, it is removed from the list.
316 std::list<std::string> segments_to_be_removed_;
317
318 // This is the wall clock time when media timestamp is 0.
319 absl::Time reference_time_;
320
321 // Used by kVideoIFrameOnly playlists to track the i-frames (key frames).
322 struct KeyFrameInfo {
323 int64_t timestamp;
324 uint64_t start_byte_offset;
325 uint64_t size;
326 std::string segment_file_name;
327 };
328 std::list<KeyFrameInfo> key_frames_;
329
330 DISALLOW_COPY_AND_ASSIGN(MediaPlaylist);
331};
332
334 public:
335 explicit ProgramDateTimeEntry(const absl::Time& program_time);
336
337 std::string ToString() override;
338
339 private:
341 ProgramDateTimeEntry& operator=(const ProgramDateTimeEntry&) = delete;
342
343 const absl::Time program_time_;
344};
345
347 public:
348 EncryptionInfoEntry(MediaPlaylist::EncryptionMethod method,
349 const std::string& url,
350 const std::string& key_id,
351 const std::string& iv,
352 const std::string& key_format,
353 const std::string& key_format_versions);
354
355 std::string ToString() override;
356 std::string ToString(std::string);
357
358 private:
360 EncryptionInfoEntry& operator=(const EncryptionInfoEntry&) = delete;
361
362 const MediaPlaylist::EncryptionMethod method_;
363 const std::string url_;
364 const std::string key_id_;
365 const std::string iv_;
366 const std::string key_format_;
367 const std::string key_format_versions_;
368};
369
370} // namespace hls
371} // namespace shaka
372
373#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 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 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.