Debian Codegraph @

ext/adaptivedemux2/hls/m3u8.h

raw
1 /* GStreamer
2 * Copyright (C) 2010 Marc-Andre Lureau <[email protected]>
3 * Copyright (C) 2010 Andoni Morales Alastruey <[email protected]>
4 * Copyright (C) 2015 Tim-Philipp Müller <[email protected]>
5 *
6 * Copyright (C) 2021-2022 Centricular Ltd
7 * Author: Edward Hervey <[email protected]>
8 * Author: Jan Schmidt <[email protected]>
9 *
10 * m3u8.h:
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
26 */
27
28 #ifndef __M3U8_H__
29 #define __M3U8_H__
30
31 #include <gst/gst.h>
32
33 G_BEGIN_DECLS
34
35 typedef struct _GstHLSMediaPlaylist GstHLSMediaPlaylist;
36 typedef struct _GstHLSTimeMap GstHLSTimeMap;
37 typedef struct _GstM3U8SeekResult GstM3U8SeekResult;
38 typedef struct _GstM3U8MediaSegment GstM3U8MediaSegment;
39 typedef struct _GstM3U8PartialSegment GstM3U8PartialSegment;
40 typedef struct _GstM3U8InitFile GstM3U8InitFile;
41 typedef enum _GstM3U8PreloadHintType GstM3U8PreloadHintType;
42 typedef struct _GstM3U8PreloadHint GstM3U8PreloadHint;
43 typedef struct _GstHLSRenditionStream GstHLSRenditionStream;
44 typedef struct _GstM3U8Client GstM3U8Client;
45 typedef struct _GstHLSVariantStream GstHLSVariantStream;
46 typedef struct _GstHLSMasterPlaylist GstHLSMasterPlaylist;
47
48 #define GST_HLS_MEDIA_PLAYLIST(m) ((GstHLSMediaPlaylist*)m)
49 #define GST_M3U8_MEDIA_SEGMENT(f) ((GstM3U8MediaSegment*)f)
50 #define GST_M3U8_PARTIAL_SEGMENT(p) ((GstM3U8PartialSegment*)p)
51
52 #define GST_HLS_MEDIA_PLAYLIST_LOCK(m) g_mutex_lock (&m->lock);
53 #define GST_HLS_MEDIA_PLAYLIST_UNLOCK(m) g_mutex_unlock (&m->lock);
54
55 #define GST_HLS_MEDIA_PLAYLIST_IS_LIVE(m) ((m)->endlist == FALSE)
56
57 /* hlsdemux must not get closer to the end of a live stream than
58 GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE fragments. Section 6.3.3
59 "Playing the Playlist file" of the HLS draft states that this
60 value is three fragments */
61 #define GST_M3U8_LIVE_MIN_FRAGMENT_DISTANCE 3
62
63 typedef enum {
64 GST_HLS_PLAYLIST_TYPE_UNDEFINED,
65 GST_HLS_PLAYLIST_TYPE_EVENT,
66 GST_HLS_PLAYLIST_TYPE_VOD,
67 } GstHLSPlaylistType;
68
69 /* Extra seek flag extensions for partial segment handling
70 * Values are chosen to avoid collision with the core GST_SEEK_FLAG_*
71 * flags */
72 #define GST_HLS_M3U8_SEEK_FLAG_ALLOW_PARTIAL (1 << 16) /* Allow seeking to a partial segment */
73
74 struct _GstM3U8SeekResult {
75 /* stream time of the segment or partial segment */
76 GstClockTimeDiff stream_time;
77
78 GstM3U8MediaSegment *segment;
79
80 gboolean found_partial_segment;
81 guint part_idx;
82 };
83
84 /**
85 * GstHLSMediaPlaylist:
86 *
87 * Official term in RFC : "Media Playlist". A List of Media Segments.
88 *
89 * It can be used by either a variant stream (GstHLSVariantStream) or an
90 * alternate rendition (GstHLSMedia).
91 *
92 * Note: Was called `GstM3u8` in legacy elements
93 */
94
95 struct _GstHLSMediaPlaylist
96 {
97 gchar *uri; /* actually downloaded URI */
98 gchar *base_uri; /* URI to use as base for resolving relative URIs.
99 * This will be different to uri in case of redirects */
100 GstClockTime playlist_ts; /* Monotonic clock time estimate for this playlist's validity from download time and cached Age */
101 GstClockTime request_time; /* Time at which this playlist was requested in monotonic clock time. */
102
103 /* Base Tag */
104 gint version; /* EXT-X-VERSION (default 1) */
105
106 /* Media Playlist Tags */
107 GstClockTime targetduration; /* EXT-X-TARGETDURATION, default GST_CLOCK_TIME_NONE */
108 GstClockTime partial_targetduration; /* EXT-X-PART-INF, default GST_CLOCK_TIME_NONE */
109
110 gint64 media_sequence; /* EXT-X-MEDIA-SEQUENCE, MSN of the first Media
111 Segment in the playlist. */
112 gint64 discont_sequence; /* EXT-X-DISCONTINUITY-SEQUENCE. Default : 0 */
113 gboolean has_ext_x_dsn; /* EXT-X-DISCONTINUITY-SEQUENCE present and specified */
114 gboolean endlist; /* EXT-X-ENDLIST present */
115 GstHLSPlaylistType type; /* EXT-X-PLAYLIST-TYPE. Default:
116 GST_HLS_PLAYLIST_TYE_UNDEFINED */
117 gboolean i_frame; /* EXT-X-I-FRAMES-ONLY present. */
118
119 gboolean allowcache; /* deprecated EXT-X-ALLOW-CACHE */
120
121 /* Overview of contained media segments */
122 gboolean ext_x_key_present; /* a valid EXT-X-KEY is present on at least one
123 media segment */
124 gboolean ext_x_pdt_present; /* a valid EXT-X-PROGRAM-DATE-TIME is present on
125 at least one media segment */
126
127 GPtrArray *segments; /* Array of GstM3U8MediaSegment */
128
129 GPtrArray *preload_hints; /* Array of GstM3U8PreloadHint */
130
131 /* Generated information */
132 GstClockTime duration; /* The estimated total duration of all segments
133 contained in this playlist */
134
135 gboolean reloaded; /* If TRUE, this indicates that this playlist
136 * was reloaded but had identical content */
137
138 /* Server-Control directive values */
139 GstClockTime skip_boundary; /* Skip Boundary from CAN-SKIP-UNTIL */
140 gboolean can_skip_dateranges; /* TRUE if CAN-SKIP-DATERANGES was YES */
141
142 GstClockTime hold_back; /* Hold-Back value, if provided (or CLOCK_TIME_NONE) */
143 GstClockTime part_hold_back; /* Part-Hold-Back value, if provided (or CLOCK_TIME_NONE */
144 gboolean can_block_reload; /* TRUE if CAN-BLOCK-RELOAD was YES */
145
146 /* Delta playlist info from EXT-X-SKIP tag */
147 gint skipped_segments;
148 gint num_removed_date_ranges;
149 gchar **removed_date_ranges;
150
151 /*< private > */
152 GMutex lock;
153
154 /* Copy of the incoming data that created this media playlist.
155 * See gst_hls_media_playlist_has_same_data() */
156 gchar *last_data;
157
158 gint ref_count; /* ATOMIC */
159 };
160
161 /* gst_hls_media_playlist_new: Internal function : Do not use from demuxer code, only for unit
162 * testing purposes */
163 GstHLSMediaPlaylist * gst_hls_media_playlist_new (const gchar * uri,
164 const gchar * base_uri);
165
166 GstHLSMediaPlaylist * gst_hls_media_playlist_ref (GstHLSMediaPlaylist * m3u8);
167
168 void gst_hls_media_playlist_unref (GstHLSMediaPlaylist * m3u8);
169
170 /**
171 * GstM3U8PartialSegment:
172 *
173 * Official term in RFC : "Partial Segment"
174 *
175 */
176 struct _GstM3U8PartialSegment
177 {
178 gboolean is_gap; /* TRUE if this part is a gap */
179 gboolean independent; /* TRUE if there is an I-frame in the partial segment */
180 gchar *uri;
181 gint64 offset, size;
182
183 GstClockTimeDiff stream_time; /* Computed stream time */
184 GstClockTime duration;
185
186 gint ref_count; /* ATOMIC */
187 };
188
189 GstM3U8PartialSegment *
190 gst_m3u8_partial_segment_ref (GstM3U8PartialSegment *part);
191
192 void
193 gst_m3u8_partial_segment_unref (GstM3U8PartialSegment *part);
194
195 /* Set up as flags, so we can form a bitmask
196 * of seen hint types */
197 enum _GstM3U8PreloadHintType {
198 M3U8_PRELOAD_HINT_NONE = (0 << 0),
199 M3U8_PRELOAD_HINT_MAP = (1 << 0),
200 M3U8_PRELOAD_HINT_PART = (1 << 1),
201 };
202
203 #define M3U8_PRELOAD_HINT_ALL (M3U8_PRELOAD_HINT_PART | M3U8_PRELOAD_HINT_MAP)
204
205 /**
206 * GstM3U8PreloadHint:
207 *
208 * Official term in RFC : "Preload Hint"
209 *
210 */
211 struct _GstM3U8PreloadHint
212 {
213 GstM3U8PreloadHintType hint_type;
214
215 gchar *uri;
216 gint64 offset, size;
217
218 gint ref_count; /* ATOMIC */
219 };
220
221 GstM3U8PreloadHint *
222 gst_m3u8_preload_hint_ref (GstM3U8PreloadHint *hint);
223
224 void
225 gst_m3u8_preload_hint_unref (GstM3U8PreloadHint *hint);
226
227 gboolean
228 gst_m3u8_preload_hint_equal (GstM3U8PreloadHint *hint1, GstM3U8PreloadHint *hint2);
229
230 /**
231 * GstM3U8MediaSegment:
232 *
233 * Official term in RFC : "Media Segment"
234 *
235 * Note : Naming in legacy elements was GstM3U8MediaFile
236 */
237 struct _GstM3U8MediaSegment
238 {
239 gboolean is_gap; /* TRUE if EXT-X-GAP was present for this segment */
240 gboolean partial_only; /* TRUE if this is the last segment in a playlist consisting of only EXT-X-PART and no full URL */
241
242 gchar *title;
243 GstClockTimeDiff stream_time; /* Computed stream time */
244 GstClockTime duration;
245 gchar *uri;
246 gint64 sequence; /* the sequence number of this segment */
247 gint64 discont_sequence; /* The Discontinuity Sequence Number of this segment */
248 gboolean discont; /* this file marks a discontinuity */
249 gchar *key;
250 guint8 iv[16];
251 gint64 offset, size;
252 GstM3U8InitFile *init_file; /* Media Initialization (hold ref) */
253 GDateTime *datetime; /* EXT-X-PROGRAM-DATE-TIME */
254
255 GPtrArray *partial_segments; /* If there are Partial Segments for this Media Segment */
256
257 gint ref_count; /* ATOMIC */
258 };
259
260 struct _GstM3U8InitFile
261 {
262 gchar *uri;
263 gint64 offset, size;
264 guint ref_count; /* ATOMIC */
265 };
266
267 GstM3U8InitFile *gst_m3u8_init_file_ref (GstM3U8InitFile * ifile);
268 void gst_m3u8_init_file_unref (GstM3U8InitFile * ifile);
269 gboolean gst_m3u8_init_file_equal (const GstM3U8InitFile * ifile1, const GstM3U8InitFile *ifile2);
270
271 GstM3U8MediaSegment *
272 gst_m3u8_media_segment_ref (GstM3U8MediaSegment * mfile);
273
274 void
275 gst_m3u8_media_segment_unref (GstM3U8MediaSegment * mfile);
276
277 gboolean
278 gst_hls_media_playlist_has_same_data (GstHLSMediaPlaylist * m3u8,
279 gchar * playlist_data);
280
281 GstHLSMediaPlaylist *
282 gst_hls_media_playlist_parse (gchar * data,
283 GstClockTime playlist_ts,
284 const gchar * uri,
285 const gchar * base_uri);
286
287 gboolean
288 gst_hls_media_playlist_sync_skipped_segments (GstHLSMediaPlaylist * m3u8,
289 GstHLSMediaPlaylist * reference);
290
291 void
292 gst_hls_media_playlist_recalculate_stream_time (GstHLSMediaPlaylist *playlist,
293 GstM3U8MediaSegment *anchor);
294
295 void
296 gst_hls_media_playlist_recalculate_stream_time_from_part (GstHLSMediaPlaylist *playlist,
297 GstM3U8MediaSegment *anchor, guint part_idx);
298
299 GstM3U8MediaSegment *
300 gst_hls_media_playlist_sync_to_segment (GstHLSMediaPlaylist * m3u8,
301 GstM3U8MediaSegment * segment);
302
303 gboolean
304 gst_hls_media_playlist_sync_to_playlist (GstHLSMediaPlaylist * m3u8,
305 GstHLSMediaPlaylist * reference,
306 gboolean *discont);
307
308 gboolean
309 gst_hls_media_playlist_has_next_fragment (GstHLSMediaPlaylist * m3u8,
310 GstM3U8MediaSegment * current,
311 gboolean forward);
312
313 GstM3U8MediaSegment *
314 gst_hls_media_playlist_advance_fragment (GstHLSMediaPlaylist * m3u8,
315 GstM3U8MediaSegment * current,
316 gboolean forward);
317
318 gboolean
319 gst_hls_media_playlist_get_starting_segment (GstHLSMediaPlaylist *self,
320 GstM3U8SeekResult *seek_result);
321
322 GstClockTime
323 gst_hls_media_playlist_get_end_stream_time (GstHLSMediaPlaylist * m3u8);
324
325 GstClockTime
326 gst_hls_media_playlist_get_duration (GstHLSMediaPlaylist * m3u8);
327
328 void
329 gst_hls_media_playlist_get_next_msn_and_part (GstHLSMediaPlaylist * m3u8,
330 gint64 *next_msn,
331 gint64 *next_part);
332
333 gchar *
334 gst_hls_media_playlist_get_uri (GstHLSMediaPlaylist * m3u8);
335
336 gboolean
337 gst_hls_media_playlist_is_live (GstHLSMediaPlaylist * m3u8);
338
339 gboolean
340 gst_hls_media_playlist_get_seek_range (GstHLSMediaPlaylist * m3u8,
341 gint64 * start,
342 gint64 * stop);
343
344 gboolean
345 gst_hls_media_playlist_has_lost_sync (GstHLSMediaPlaylist * m3u8,
346 GstClockTime position);
347
348 gboolean
349 gst_hls_media_playlist_seek (GstHLSMediaPlaylist *playlist,
350 gboolean forward,
351 GstSeekFlags flags,
352 GstClockTimeDiff ts,
353 GstM3U8SeekResult *seek_result);
354
355 gboolean
356 gst_hls_media_playlist_find_position (GstHLSMediaPlaylist *playlist,
357 GstClockTimeDiff ts, gboolean in_partial_segments,
358 GstM3U8SeekResult *seek_result);
359
360 void
361 gst_hls_media_playlist_dump (GstHLSMediaPlaylist* self);
362
363 GstClockTime
364 gst_hls_media_playlist_recommended_buffering_threshold (GstHLSMediaPlaylist * playlist);
365
366 typedef enum
367 {
368 GST_HLS_RENDITION_STREAM_TYPE_INVALID = -1,
369 GST_HLS_RENDITION_STREAM_TYPE_AUDIO,
370 GST_HLS_RENDITION_STREAM_TYPE_VIDEO,
371 GST_HLS_RENDITION_STREAM_TYPE_SUBTITLES,
372 GST_HLS_RENDITION_STREAM_TYPE_CLOSED_CAPTIONS,
373 GST_HLS_N_MEDIA_TYPES
374 } GstHLSRenditionStreamType;
375
376 /**
377 * GstHLSRenditionStream:
378 *
379 * Official term in RFC : "Renditions are alternate versions of the content,
380 * such as audio produced in different languages or video recorded from
381 * different camera angles."
382 *
383 * Note: Was named GstHLSMedia in legacy elements
384 */
385
386 struct _GstHLSRenditionStream {
387 GstHLSRenditionStreamType mtype;
388 gchar *group_id;
389 gchar *name;
390 gchar *lang;
391 gchar *uri;
392 GstCaps *caps;
393 gboolean is_default;
394 gboolean autoselect;
395 gboolean forced;
396
397 gint ref_count; /* ATOMIC */
398 };
399
400 GstHLSRenditionStream *
401 gst_hls_rendition_stream_ref (GstHLSRenditionStream * media);
402
403 void
404 gst_hls_rendition_stream_unref (GstHLSRenditionStream * media);
405
406 const gchar *
407 gst_hls_rendition_stream_type_get_name (GstHLSRenditionStreamType mtype);
408
409
410 /**
411 * GstHLSVariantStream:
412 *
413 * Official term in RFC :
414 * """
415 * A Master Playlist provides a set of Variant Streams, each of which describes
416 * a different version of the same content.
417 *
418 * A Variant Stream includes a Media Playlist that specifies media encoded at a
419 * particular bit rate, in a particular format, and at a particular resolution
420 * for media containing video.
421 * """
422 */
423 struct _GstHLSVariantStream {
424 gchar *name; /* This will be the "name" of the playlist, the original
425 * relative/absolute uri in a variant playlist */
426 gchar *uri;
427 gchar *codecs;
428 GstCaps *caps;
429 GstStreamType codecs_stream_type; /* As defined by codecs */
430 gint bandwidth; /* bits per second */
431 gint program_id;
432 gint width;
433 gint height;
434 gboolean iframe;
435
436 gint refcount; /* ATOMIC */
437
438 /* alternative renditions (names) */
439 gchar *media_groups[GST_HLS_N_MEDIA_TYPES];
440
441 /* List of gchar* fallback uri */
442 GList *fallback;
443 };
444
445 /* Notes: #define are to avoid symbol clashes with legacy hlsdemux */
446
447 #define gst_hls_variant_stream_ref hls_variant_stream_ref
448 GstHLSVariantStream * hls_variant_stream_ref (GstHLSVariantStream * stream);
449
450 #define gst_hls_variant_stream_unref hls_variant_stream_unref
451 void hls_variant_stream_unref (GstHLSVariantStream * stream);
452
453 /**
454 * GstHLSMasterPlaylist:
455 *
456 * Official term in RFC : "A Playlist is either a Media Playlist or a Master
457 * Playlist."
458 *
459 * This is the top-level object, constructed by a manifest provided by external
460 * means.
461 */
462 struct _GstHLSMasterPlaylist
463 {
464 /* Available variant streams, sorted by bitrate (low -> high) */
465 GList *variants; /* GstHLSVariantStream */
466 GList *iframe_variants; /* GstHLSVariantStream */
467
468 /* Default variant, first in the list (originally, before sorting) */
469 GstHLSVariantStream *default_variant;
470
471 /* Full list of Available Alternative Rendition (GstHLSRenditionStream) */
472 GList *renditions;
473
474 /* EXT-X-VERSION. 0 if unspecified */
475 gint version;
476
477 /* TRUE if this playlist is a simple media playlist (and not a master
478 * playlist). Implies that there is only a single variant and no alternate
479 * rendition groups */
480 gboolean is_simple;
481
482 /* TRUE if all variants have codecs specified */
483 gboolean have_codecs;
484
485 /*< private > */
486 gchar *last_data; /* Copy of the incoming data that created this master playlist */
487
488 gint refcount; /* ATOMIC */
489 };
490
491 /* Notes: #define are to avoid symbol clashes with legacy hlsdemux */
492
493 #define gst_hls_master_playlist_new_from_data hls_master_playlist_new_from_data
494 GstHLSMasterPlaylist * hls_master_playlist_new_from_data (gchar * data,
495 const gchar * base_uri);
496
497 #define gst_hls_master_playlist_get_variant_for_bitrate hls_master_playlist_get_variant_for_bitrate
498 GstHLSVariantStream * hls_master_playlist_get_variant_for_bitrate (GstHLSMasterPlaylist * playlist,
499 gboolean iframe_variant,
500 guint bitrate,
501 guint min_bitrate,
502 GList * failed_variants);
503
504 #define gst_hls_master_playlist_get_common_caps hls_master_playlist_get_common_caps
505 GstCaps * hls_master_playlist_get_common_caps (GstHLSMasterPlaylist *playlist);
506
507 #define gst_hls_master_playlist_unref hls_master_playlist_unref
508 void hls_master_playlist_unref (GstHLSMasterPlaylist * playlist);
509
510
511 /* Time Mapping
512 *
513 * Used to map GStreamer times to internal segment timestamps
514 */
515 struct _GstHLSTimeMap {
516 /* DISCONT SEQUENCE NUMBER */
517 gint64 dsn;
518
519 /* The stream time (used for gst timestamps, gst segments, seeking ...) */
520 GstClockTime stream_time;
521
522 /* The optional Program Date Time reference */
523 GDateTime *pdt;
524
525 /* The internal time (ex: mpeg-ts PTS) */
526 GstClockTime internal_time;
527 };
528
529 GstStreamType gst_stream_type_from_hls_type (GstHLSRenditionStreamType stype);
530 GstStreamType gst_hls_get_stream_type_from_structure (GstStructure *structure);
531 GstStreamType gst_hls_get_stream_type_from_caps (GstCaps *caps);
532
533 G_END_DECLS
534
535 #endif /* __M3U8_H__ */