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 <absl/time/time.h>
18
19#include <packager/hls_params.h>
20#include <packager/macros/classes.h>
21#include <packager/media/base/fourccs.h>
22#include <packager/mpd/base/bandwidth_estimator.h>
23#include <packager/mpd/base/media_info.pb.h>
24
25namespace shaka {
26
27class File;
28
29namespace hls {
30
31class HlsEntry {
32 public:
33 enum class EntryType {
34 kExtInf,
35 kExtKey,
36 kExtDiscontinuity,
37 kExtPlacementOpportunity,
38 kProgramDateTime,
39 };
40 virtual ~HlsEntry();
41
42 EntryType type() const { return type_; }
43 virtual std::string ToString() = 0;
44
45 protected:
46 explicit HlsEntry(EntryType type);
47
48 private:
49 EntryType type_;
50};
51
54 public:
55 enum class MediaPlaylistStreamType {
56 kUnknown,
57 kAudio,
58 kVideo,
59 kVideoIFramesOnly,
60 kSubtitle,
61 };
62 enum class EncryptionMethod {
63 kNone, // No encryption, i.e. clear.
64 kAes128, // Completely encrypted using AES-CBC.
65 kSampleAes, // Encrypted using SAMPLE-AES method.
66 kSampleAesCenc, // 'cenc' encrypted content.
67 };
68
77 MediaPlaylist(const HlsParams& hls_params,
78 const std::string& file_name,
79 const std::string& name,
80 const std::string& group_id);
81 virtual ~MediaPlaylist();
82
83 const std::string& file_name() const { return file_name_; }
84 const std::string& name() const { return name_; }
85 const std::string& group_id() const { return group_id_; }
86 MediaPlaylistStreamType stream_type() const { return stream_type_; }
87 const std::string& codec() const { return codec_; }
88 const std::string& supplemental_codec() const { return supplemental_codec_; }
89 const media::FourCC& compatible_brand() const { return compatible_brand_; }
90 const std::list<std::unique_ptr<HlsEntry>>& entries() const {
91 return entries_;
92 }
93
95 void SetStreamTypeForTesting(MediaPlaylistStreamType stream_type);
96
98 void SetCodecForTesting(const std::string& codec);
99
101 void SetLanguageForTesting(const std::string& language);
102
104 void SetForcedSubtitleForTesting(const bool forced_subtitle);
105
108 const std::vector<std::string>& characteristics);
109
112 void SetIndexForTesting(uint32_t index);
113
115 void AddEncryptionInfoForTesting(MediaPlaylist::EncryptionMethod method,
116 const std::string& url,
117 const std::string& key_id,
118 const std::string& iv,
119 const std::string& key_format,
120 const std::string& key_format_versions);
121
126 virtual bool SetMediaInfo(const MediaInfo& media_info);
127 MediaInfo GetMediaInfo() const { return media_info_; }
128
133 virtual void SetSampleDuration(int32_t sample_duration);
134
142 virtual void AddSegment(const std::string& file_name,
143 int64_t start_time,
144 int64_t duration,
145 uint64_t start_byte_offset,
146 uint64_t size);
147
150 virtual void SetReferenceTime(const absl::Time& reference_time);
151
158 virtual void AddKeyFrame(int64_t timestamp,
159 uint64_t start_byte_offset,
160 uint64_t size);
161
173 virtual void AddEncryptionInfo(EncryptionMethod method,
174 const std::string& url,
175 const std::string& key_id,
176 const std::string& iv,
177 const std::string& key_format,
178 const std::string& key_format_versions);
179
182 virtual void AddPlacementOpportunity();
183
198 virtual bool WriteToFile(const std::filesystem::path& file_path,
199 bool event_to_vod_on_end_of_stream,
200 bool end_stream);
201
205 virtual uint64_t MaxBitrate() const;
206
210 virtual uint64_t AvgBitrate() const;
211
214 virtual double GetLongestSegmentDuration() const;
215
223 virtual void SetTargetDuration(int32_t target_duration);
224
226 virtual int GetNumChannels() const;
227
231 virtual int GetEC3JocComplexity() const;
232
236 virtual bool GetAC4ImsFlag() const;
237
241 virtual bool GetAC4CbiFlag() const;
242
245 virtual bool GetDisplayResolution(uint32_t* width, uint32_t* height) const;
246
248 virtual std::string GetVideoRange() const;
249
251 virtual double GetFrameRate() const;
252
255 const std::string& language() const { return language_; }
256
257 const std::vector<std::string>& characteristics() const {
258 return characteristics_;
259 }
260
261 bool forced_subtitle() const { return forced_subtitle_; }
262
263 bool is_dvs() const {
264 // HLS Authoring Specification for Apple Devices
265 // https://developer.apple.com/documentation/http_live_streaming/hls_authoring_specification_for_apple_devices#overview
266 // Section 2.12.
267 const char DVS_CHARACTERISTICS[] = "public.accessibility.describes-video";
268 return characteristics_.size() == 1 &&
269 characteristics_[0] == DVS_CHARACTERISTICS;
270 }
271
272 private:
273 // Add a SegmentInfoEntry (#EXTINF).
274 void AddSegmentInfoEntry(const std::string& segment_file_name,
275 int64_t start_time,
276 int64_t duration,
277 uint64_t start_byte_offset,
278 uint64_t size);
279 // Adjust the duration of the last SegmentInfoEntry to end on
280 // |next_timestamp|.
281 void AdjustLastSegmentInfoEntryDuration(int64_t next_timestamp);
282 // Remove elements from |entries_| for live profile. Increments
283 // |sequence_number_| by the number of segments removed.
284 void SlideWindow();
285 // Remove the segment specified by |start_time|. The actual deletion can
286 // happen at a later time depending on the value of
287 // |preserved_segment_outside_live_window| in |hls_params_|.
288 void RemoveOldSegment(int64_t start_time);
289
290 const HlsParams& hls_params_;
291 // Mainly for MasterPlaylist to use these values.
292 const std::string file_name_;
293 const std::string name_;
294 const std::string group_id_;
295 MediaInfo media_info_;
296 MediaPlaylistStreamType stream_type_ = MediaPlaylistStreamType::kUnknown;
297 // Whether to use byte range for SegmentInfoEntry.
298 bool use_byte_range_ = false;
299 std::string codec_;
300 std::string supplemental_codec_;
301 media::FourCC compatible_brand_;
302 std::string language_;
303 std::vector<std::string> characteristics_;
304 bool forced_subtitle_ = false;
305 uint32_t media_sequence_number_ = 0;
306 bool inserted_discontinuity_tag_ = false;
307 int discontinuity_sequence_number_ = 0;
308
309 double longest_segment_duration_seconds_ = 0.0;
310 int32_t time_scale_ = 0;
311
312 BandwidthEstimator bandwidth_estimator_;
313
314 // Cache the previous calls AddSegment() end offset. This is used to construct
315 // SegmentInfoEntry.
316 uint64_t previous_segment_end_offset_ = 0;
317
318 // See SetTargetDuration() comments.
319 bool target_duration_set_ = false;
320 int32_t target_duration_ = 0;
321
322 // TODO(kqyang): This could be managed better by a separate class, than having
323 // all them managed in MediaPlaylist.
324 std::list<std::unique_ptr<HlsEntry>> entries_;
325 double current_buffer_depth_ = 0;
326 // A list to hold the file names of the segments to be removed temporarily.
327 // Once a file is actually removed, it is removed from the list.
328 std::list<std::string> segments_to_be_removed_;
329
330 // This is the wall clock time when media timestamp is 0.
331 absl::Time reference_time_;
332
333 // Used by kVideoIFrameOnly playlists to track the i-frames (key frames).
334 struct KeyFrameInfo {
335 int64_t timestamp;
336 uint64_t start_byte_offset;
337 uint64_t size;
338 std::string segment_file_name;
339 };
340 std::list<KeyFrameInfo> key_frames_;
341
342 DISALLOW_COPY_AND_ASSIGN(MediaPlaylist);
343};
344
346 public:
347 explicit ProgramDateTimeEntry(const absl::Time& program_time);
348
349 std::string ToString() override;
350
351 private:
353 ProgramDateTimeEntry& operator=(const ProgramDateTimeEntry&) = delete;
354
355 const absl::Time program_time_;
356};
357
359 public:
360 EncryptionInfoEntry(MediaPlaylist::EncryptionMethod method,
361 const std::string& url,
362 const std::string& key_id,
363 const std::string& iv,
364 const std::string& key_format,
365 const std::string& key_format_versions);
366
367 std::string ToString() override;
368 std::string ToString(std::string);
369
370 private:
372 EncryptionInfoEntry& operator=(const EncryptionInfoEntry&) = delete;
373
374 const MediaPlaylist::EncryptionMethod method_;
375 const std::string url_;
376 const std::string key_id_;
377 const std::string iv_;
378 const std::string key_format_;
379 const std::string key_format_versions_;
380};
381
382} // namespace hls
383} // namespace shaka
384
385#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
void SetIndexForTesting(uint32_t index)
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.