Debian Codegraph @

ext/adaptivedemux2/hls/gsthlsdemux-preloader.c

raw
1 /* GStreamer
2 Copyright (C) 2022 Jan Schmidt <[email protected]>
3 *
4 * gsthlsdemux-preloader.c:
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include "gsthlsdemux-preloader.h"
27
28 /* Everything is called from the scheduler thread, including
29 * download handling callbacks */
30
31 GST_DEBUG_CATEGORY_EXTERN (gst_hls_demux2_debug);
32 #define GST_CAT_DEFAULT gst_hls_demux2_debug
33
34 typedef struct _GstHLSDemuxPreloadRequest GstHLSDemuxPreloadRequest;
35 struct _GstHLSDemuxPreloadRequest
36 {
37 GstHLSDemuxPreloader *preloader; /* Parent preloader */
38 GstM3U8PreloadHint *hint;
39
40 /* Incoming download tracking for the resource */
41 DownloadRequest *download_request;
42 gboolean download_is_finished; /* TRUE if the input download request completed / failed */
43 guint64 download_cur_offset; /* offset of the next expected received data */
44 guint64 download_content_length; /* Content length (filled in when response headers arrive) */
45 GstClockTime request_latency; /* original HTTP request to data latency */
46 GstClockTime download_first_data_time; /* Arrival timestamp of the first data in the download chunk */
47 guint64 download_first_data_offset; /* First data byte offset of the download chunk */
48
49 /* Target tracking for the stream download to deliver data blocks to */
50 /* Each active preload only needs one target to output to at a time,
51 * since we only download one segment at a time, and MAP requests are distinct from PART requests,
52 * so 1 preload = 1 download request by the stream */
53 guint64 target_cur_offset; /* offset of the next delivered target data */
54 DownloadRequest *target_request;
55 };
56
57 static GstHLSDemuxPreloadRequest *
58 gst_hls_demux_preload_request_new (GstHLSDemuxPreloader * preloader,
59 GstM3U8PreloadHint * hint)
60 {
61 GstHLSDemuxPreloadRequest *req = g_new0 (GstHLSDemuxPreloadRequest, 1);
62 req->preloader = preloader;
63 req->hint = gst_m3u8_preload_hint_ref (hint);
64 req->request_latency = GST_CLOCK_TIME_NONE;
65 req->download_first_data_time = GST_CLOCK_TIME_NONE;
66 req->download_first_data_offset = GST_BUFFER_OFFSET_NONE;
67
68 return req;
69 };
70
71 static void
72 gst_hls_demux_preload_request_free (GstHLSDemuxPreloadRequest * req)
73 {
74 gst_m3u8_preload_hint_unref (req->hint);
75
76 if (req->download_request != NULL) {
77 /* The download request must have been cancelled by the preload helper,
78 * but cancellation is async, so we can't verify */
79 download_request_unref (req->download_request);
80 }
81
82 if (req->target_request != NULL) {
83 download_request_unref (req->target_request);
84 }
85
86 g_free (req);
87 };
88
89 static gboolean
90 gst_hls_demux_preloader_submit (GstHLSDemuxPreloader * preloader,
91 GstHLSDemuxPreloadRequest * preload_req, const gchar * referrer_uri);
92 static void gst_hls_demux_preloader_release_request (GstHLSDemuxPreloader *
93 preloader, GstHLSDemuxPreloadRequest * preload_req,
94 gboolean cancel_download);
95
96 GstHLSDemuxPreloader *
97 gst_hls_demux_preloader_new (DownloadHelper * download_helper)
98 {
99 GstHLSDemuxPreloader *preloader = g_new0 (GstHLSDemuxPreloader, 1);
100
101 preloader->download_helper = download_helper;
102 preloader->active_preloads = g_ptr_array_new ();
103
104 return preloader;
105 }
106
107 void
108 gst_hls_demux_preloader_free (GstHLSDemuxPreloader * preloader)
109 {
110 gst_hls_demux_preloader_cancel (preloader, M3U8_PRELOAD_HINT_ALL);
111 g_ptr_array_free (preloader->active_preloads, TRUE);
112 g_free (preloader);
113 }
114
115 void
116 gst_hls_demux_preloader_load (GstHLSDemuxPreloader * preloader,
117 GstM3U8PreloadHint * hint, const gchar * referrer_uri)
118 {
119 /* Check if we have an active preload already for this hint */
120 guint idx;
121 for (idx = 0; idx < preloader->active_preloads->len; idx++) {
122 GstHLSDemuxPreloadRequest *req =
123 g_ptr_array_index (preloader->active_preloads, idx);
124 if (hint->hint_type == req->hint->hint_type) {
125 /* We already have an active hint of this type. If this new one is different, cancel
126 * the active preload before starting this one */
127 if (gst_m3u8_preload_hint_equal (hint, req->hint)) {
128 GST_LOG ("Ignoring pre-existing preload of type %d uri: %s, range:%"
129 G_GINT64_FORMAT " size %" G_GINT64_FORMAT, hint->hint_type,
130 hint->uri, hint->offset, hint->size);
131 return; /* Nothing to do */
132 }
133
134 gst_hls_demux_preloader_release_request (preloader, req, TRUE);
135 g_ptr_array_remove_index_fast (preloader->active_preloads, idx);
136 break;
137 }
138 }
139
140 /* If we get here, then there's no preload of this type. Create one */
141 GstHLSDemuxPreloadRequest *req =
142 gst_hls_demux_preload_request_new (preloader, hint);
143 /* Submit the request */
144
145 if (gst_hls_demux_preloader_submit (preloader, req, referrer_uri)) {
146 g_ptr_array_add (preloader->active_preloads, req);
147 } else {
148 /* Discard failed request */
149 gst_hls_demux_preloader_release_request (preloader, req, TRUE);
150 }
151 }
152
153 void
154 gst_hls_demux_preloader_cancel (GstHLSDemuxPreloader * preloader,
155 GstM3U8PreloadHintType hint_types)
156 {
157 /* Go through the active downloads and remove/cancel any with the matching type */
158 guint idx;
159 for (idx = 0; idx < preloader->active_preloads->len;) {
160 GstHLSDemuxPreloadRequest *req =
161 g_ptr_array_index (preloader->active_preloads, idx);
162 if (hint_types & req->hint->hint_type) {
163 gst_hls_demux_preloader_release_request (preloader, req, TRUE);
164 g_ptr_array_remove_index_fast (preloader->active_preloads, idx);
165 continue; /* Don't increment idx++, as we just removed an item */
166 }
167
168 idx++;
169 }
170 }
171
172 /* This function transfers any available data to the target request, and possibly
173 * completes it and removes it from the preload */
174 static void
175 gst_hls_demux_preloader_despatch (GstHLSDemuxPreloadRequest * preload_req,
176 gboolean input_is_finished)
177 {
178 GstHLSDemuxPreloader *preloader = preload_req->preloader;
179 DownloadRequest *download_req = preload_req->download_request;
180
181 if (input_is_finished)
182 preload_req->download_is_finished = TRUE;
183 else
184 input_is_finished = preload_req->download_is_finished;
185
186 download_request_lock (download_req);
187
188 /* Update timestamp tracking */
189 if (preload_req->request_latency == GST_CLOCK_TIME_NONE) {
190 if (GST_CLOCK_TIME_IS_VALID (download_req->download_request_time) &&
191 GST_CLOCK_TIME_IS_VALID (download_req->download_start_time)) {
192
193 preload_req->request_latency =
194 download_req->download_start_time -
195 download_req->download_request_time;
196 }
197 }
198
199 if (preload_req->download_first_data_time == GST_CLOCK_TIME_NONE &&
200 download_request_get_bytes_available (download_req) > 0) {
201 /* Got the first data of this download burst */
202 preload_req->download_first_data_time = download_req->download_start_time;
203 preload_req->download_first_data_offset =
204 download_request_get_cur_offset (download_req);
205 }
206
207 download_request_unlock (download_req);
208
209 /* If there is a target request, see if any of our data should be
210 * transferred to it, and if it should be despatched as complete */
211 if (preload_req->target_request != NULL) {
212 gboolean output_is_finished = input_is_finished;
213 gboolean despatch_progress = FALSE;
214 DownloadRequest *target_req = preload_req->target_request;
215
216 download_request_lock (target_req);
217 download_request_lock (download_req);
218
219 DownloadRequestState target_state = download_req->state;
220
221 /* Transfer the http status code */
222 target_req->status_code = download_req->status_code;
223
224 GstBuffer *target_buf = download_request_take_buffer_range (download_req,
225 preload_req->target_cur_offset,
226 target_req->range_end);
227
228 if (target_buf != NULL) {
229 /* Deliver data to the target, and update our tracked output position */
230 preload_req->target_cur_offset =
231 GST_BUFFER_OFFSET (target_buf) + gst_buffer_get_size (target_buf);
232
233 GST_LOG ("Adding %" G_GSIZE_FORMAT " bytes at offset %" G_GUINT64_FORMAT
234 " to target download request uri %s range %" G_GINT64_FORMAT " - %"
235 G_GINT64_FORMAT, gst_buffer_get_size (target_buf),
236 GST_BUFFER_OFFSET (target_buf), target_req->uri,
237 target_req->range_start, target_req->range_end);
238
239 download_request_add_buffer (target_req, target_buf);
240 despatch_progress = TRUE; /* Added a buffer, despatch progress callback */
241
242 /* Transfer timing from the input download as best we can, so the receiver can
243 * calculate bitrates. If all preload requests filled one target download,
244 * we could just transfer the timestamps, but to handle the case of an
245 * ongoing chunked connection needs fancier accounting based on the
246 * arrival times of each data burst */
247 if (target_req->download_start_time == GST_CLOCK_TIME_NONE) {
248 if (preload_req->download_first_data_time >
249 preload_req->request_latency) {
250 target_req->download_request_time =
251 preload_req->download_first_data_time -
252 preload_req->request_latency;
253 } else {
254 target_req->download_request_time = 0;
255 }
256
257 target_req->download_start_time = preload_req->download_first_data_time;
258 target_req->download_newest_data_time =
259 download_req->download_newest_data_time;
260 }
261
262 if (target_req->range_end != -1
263 && preload_req->target_cur_offset > target_req->range_end) {
264 /* We've delivered all data to satisfy the requested byte range - the target request is complete */
265 if (target_state == DOWNLOAD_REQUEST_STATE_LOADING) {
266 target_state = DOWNLOAD_REQUEST_STATE_COMPLETE;
267 GST_LOG ("target download request uri %s range %" G_GINT64_FORMAT
268 " - %" G_GINT64_FORMAT " is fully satisfied. Completing",
269 target_req->uri, target_req->range_start, target_req->range_end);
270 }
271
272 output_is_finished = TRUE;
273
274 /* If there's unconsumed data left in the input download, then update
275 * our variable that tracks the first data arrival time in in a prorata
276 * fashion (because there's more partial segment data already downloaded
277 * and we need to preserve a reasonable bitrate estimate. If there's no
278 * data, but the connection is continuing, then it's returned to a blocking
279 * read state that'll send more data in the future when
280 * a new live segment becomes available, so reset our variable as if that
281 * download was starting again */
282 guint64 data_avail =
283 download_request_get_bytes_available (download_req);
284 if (data_avail > 0) {
285 /* burst first data offset must have been set by now */
286 g_assert (preload_req->download_first_data_offset !=
287 GST_BUFFER_OFFSET_NONE);
288
289 /* Calculate how long it took to download the data we have output/discarded
290 * based on the average bitrate so far.
291 * time_to_download = total_download_time * consumed_bytes / total_download_bytes */
292 guint64 new_cur_offset =
293 download_request_get_cur_offset (download_req);
294 GstClockTime data_time_offset =
295 gst_util_uint64_scale (download_req->download_newest_data_time -
296 preload_req->download_first_data_time,
297 new_cur_offset - preload_req->download_first_data_offset,
298 new_cur_offset + data_avail -
299 preload_req->download_first_data_offset);
300
301 preload_req->download_first_data_time += data_time_offset;
302 preload_req->download_first_data_offset = new_cur_offset;
303
304 GST_LOG ("Advancing request timing tracking by %" GST_TIMEP_FORMAT
305 " to time %" GST_TIMEP_FORMAT " @ offset %" G_GUINT64_FORMAT,
306 &data_time_offset,
307 &preload_req->download_first_data_time,
308 preload_req->download_first_data_offset);
309
310 /* Say that this target download finished when the first
311 * byte of the remaining data arrived */
312 target_req->download_end_time = preload_req->download_first_data_time;
313 } else {
314 /* Reset the download start time */
315 preload_req->download_first_data_time = GST_CLOCK_TIME_NONE;
316 preload_req->download_first_data_offset = GST_BUFFER_OFFSET_NONE;
317
318 /* Say that this request finished when the most recent data arrived */
319 target_req->download_end_time =
320 download_req->download_newest_data_time;
321 }
322 }
323 }
324
325 if (input_is_finished
326 && target_req->download_end_time == GST_CLOCK_TIME_NONE) {
327 /* No download end time was set yet - use the input download end time */
328 target_req->download_end_time = download_req->download_end_time;
329 }
330
331 /* Update the target request's state, which may have been adjusted from the
332 * input request's state */
333 target_req->state = target_state;
334
335 if (target_req->headers == NULL && download_req->headers != NULL) {
336 target_req->headers = gst_structure_copy (download_req->headers);
337 }
338
339 if (target_req->redirect_uri == NULL && download_req->redirect_uri != NULL) {
340 target_req->redirect_uri = g_strdup (download_req->redirect_uri);
341 target_req->redirect_permanent = download_req->redirect_permanent;
342 }
343
344 /* We're done with the input download request . */
345 download_request_unlock (download_req);
346
347 if (output_is_finished) {
348 GST_DEBUG ("Finishing target preload request uri: %s, start: %"
349 G_GINT64_FORMAT " end: %" G_GINT64_FORMAT, target_req->uri,
350 target_req->range_start, target_req->range_end);
351
352 download_request_despatch_completion (target_req);
353 download_request_unlock (target_req);
354
355 download_request_unref (target_req);
356 preload_req->target_request = NULL;
357 } else if (despatch_progress) {
358 download_request_despatch_progress (target_req);
359 }
360
361 /* Unlock if the target request didn't get released above */
362 if (preload_req->target_request != NULL) {
363 download_request_unlock (preload_req->target_request);
364 }
365 }
366
367 if (input_is_finished) {
368 if (download_req == NULL
369 || download_request_get_bytes_available (download_req)
370 == 0) {
371 GstM3U8PreloadHint *hint = preload_req->hint;
372 GST_DEBUG ("Removing finished+drained preload type %d uri: %s, start: %"
373 G_GINT64_FORMAT " size: %" G_GINT64_FORMAT, hint->hint_type,
374 hint->uri, hint->offset, hint->size);
375
376 /* The incoming request is complete and the data is drained. Remove this preload request from the list */
377 g_ptr_array_remove_fast (preloader->active_preloads, preload_req);
378 gst_hls_demux_preloader_release_request (preloader, preload_req, FALSE);
379 }
380 }
381 }
382
383 static void
384 on_download_cancellation (DownloadRequest * request, DownloadRequestState state,
385 GstHLSDemuxPreloadRequest * preload_req)
386 {
387 gst_hls_demux_preloader_despatch (preload_req, TRUE);
388 }
389
390 static void
391 on_download_error (DownloadRequest * request, DownloadRequestState state,
392 GstHLSDemuxPreloadRequest * preload_req)
393 {
394 GstM3U8PreloadHint *hint = preload_req->hint;
395 GST_DEBUG ("preload type %d uri: %s download error", hint->hint_type,
396 hint->uri);
397
398 /* FIXME: Should we attempt to re-request a preload? Should we check if
399 * any part was transferred to the target request already? Should we
400 * attempt to request a byte range with a new start position if we
401 * already despatched data to other requests?
402 */
403 gst_hls_demux_preloader_despatch (preload_req, TRUE);
404 }
405
406 static void
407 on_download_progress (DownloadRequest * request, DownloadRequestState state,
408 GstHLSDemuxPreloadRequest * preload_req)
409 {
410 GstM3U8PreloadHint *hint = preload_req->hint;
411
412 GST_DEBUG ("preload type %d uri: %s download progress. position %"
413 G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT " bytes", hint->hint_type,
414 hint->uri,
415 preload_req->download_cur_offset +
416 download_request_get_bytes_available (request), request->content_length);
417 preload_req->download_content_length = request->content_length;
418
419 gst_hls_demux_preloader_despatch (preload_req, FALSE);
420 }
421
422 static void
423 on_download_complete (DownloadRequest * request, DownloadRequestState state,
424 GstHLSDemuxPreloadRequest * preload_req)
425 {
426 GstM3U8PreloadHint *hint = preload_req->hint;
427 GST_DEBUG ("preload type %d uri: %s download complete. position %"
428 G_GUINT64_FORMAT " of %" G_GUINT64_FORMAT " bytes", hint->hint_type,
429 hint->uri,
430 preload_req->download_cur_offset +
431 download_request_get_bytes_available (request), request->content_length);
432 preload_req->download_content_length = request->content_length;
433
434 gst_hls_demux_preloader_despatch (preload_req, TRUE);
435 }
436
437 static gboolean
438 gst_hls_demux_preloader_submit (GstHLSDemuxPreloader * preloader,
439 GstHLSDemuxPreloadRequest * preload_req, const gchar * referrer_uri)
440 {
441 g_assert (preload_req->download_request == NULL);
442
443 DownloadRequest *download_req = download_request_new ();
444 GstM3U8PreloadHint *hint = preload_req->hint;
445
446 /* Configure our download request */
447 gint64 end = RFC8673_LAST_BYTE_POS;
448 if (hint->size > 0) {
449 end = hint->offset + hint->size - 1;
450 }
451
452 download_request_set_uri (download_req, hint->uri, hint->offset, end);
453 download_request_set_callbacks (download_req,
454 (DownloadRequestEventCallback) on_download_complete,
455 (DownloadRequestEventCallback) on_download_error,
456 (DownloadRequestEventCallback) on_download_cancellation,
457 (DownloadRequestEventCallback) on_download_progress, preload_req);
458
459 GST_DEBUG ("Submitting preload type %d uri: %s, range:%" G_GINT64_FORMAT
460 " - %" G_GINT64_FORMAT, hint->hint_type, hint->uri, hint->offset, end);
461
462 if (!downloadhelper_submit_request (preloader->download_helper,
463 referrer_uri, DOWNLOAD_FLAG_NONE, download_req, NULL)) {
464 /* Abandon the request */
465 download_request_unref (download_req);
466 return FALSE;
467 }
468
469 /* Store the current read offset */
470 preload_req->download_cur_offset = hint->offset;
471 preload_req->download_request = download_req;
472 preload_req->download_is_finished = FALSE;
473 return TRUE;
474 }
475
476 static void
477 gst_hls_demux_preloader_release_request (GstHLSDemuxPreloader * preloader,
478 GstHLSDemuxPreloadRequest * preload_req, gboolean cancel_download)
479 {
480 if (preload_req->download_request) {
481 if (cancel_download) {
482 GstM3U8PreloadHint *hint = preload_req->hint;
483
484 GST_DEBUG ("Cancelling preload type %d uri: %s, range start:%"
485 G_GINT64_FORMAT " size %" G_GINT64_FORMAT, hint->hint_type, hint->uri,
486 hint->offset, hint->size);
487
488 /* We don't want any callbacks to happen after we cancel here */
489 download_request_set_callbacks (preload_req->download_request,
490 NULL, NULL, NULL, NULL, NULL);
491 downloadhelper_cancel_request (preloader->download_helper,
492 preload_req->download_request);
493 }
494 }
495
496 gst_hls_demux_preload_request_free (preload_req);
497 }
498
499 /* See if we can satisfy a download request from a preload, and fulfil it if so.
500 * There are several cases:
501 * * The URI and range exactly match one of our preloads -> OK
502 * * The URI matches, and the requested range is a subset of the preload -> OK
503 * * The URI matches, but the requested range is outside what's available in the preload
504 * and can't be provided.
505 *
506 * Within those options, there are sub-possibilities:
507 * * The preload request is ongoing. It might have enough data already to completely provide
508 * the requested range.
509 * * The preload request is ongoing, but has already moved past the requested range (no longer available)
510 * * The preload request is ongoing, will feed data to the target req as it arrives
511 * * The preload request is complete already, so can either provide the requested range or not, but
512 * also needs to mark the target_req as completed once it has passed the required data.
513 */
514 gboolean
515 gst_hls_demux_preloader_provide_request (GstHLSDemuxPreloader * preloader,
516 DownloadRequest * target_req)
517 {
518 guint idx;
519 for (idx = 0; idx < preloader->active_preloads->len; idx++) {
520 GstHLSDemuxPreloadRequest *preload_req =
521 g_ptr_array_index (preloader->active_preloads, idx);
522 GstM3U8PreloadHint *hint = preload_req->hint;
523
524 if (g_strcmp0 (hint->uri, target_req->uri))
525 continue;
526
527 GST_LOG ("Possible matching preload type %d uri: %s, range start:%"
528 G_GINT64_FORMAT " size %" G_GINT64_FORMAT " (download position %"
529 G_GUINT64_FORMAT ") for req with range %" G_GINT64_FORMAT " to %"
530 G_GINT64_FORMAT, hint->hint_type, hint->uri, hint->offset, hint->size,
531 preload_req->download_cur_offset, target_req->range_start,
532 target_req->range_end);
533
534 if (target_req->range_start > preload_req->download_cur_offset) {
535 /* This preload request is for a byte range beyond the desired
536 * position (or something already consumed the target data) */
537 GST_LOG ("Range start didn't match");
538 continue;
539 }
540
541 if (target_req->range_end != -1) {
542 /* The target request does not want the entire rest of the preload
543 * stream, so check that the end is satisfiable */
544 gint64 content_length = preload_req->download_content_length;
545 if (content_length == 0) {
546 /* We don't have information from the preload download's response headers yet,
547 * so check against the requested length and error out later if the server
548 * doesn't provide all the desired response */
549 if (hint->size != -1)
550 content_length = hint->size;
551 }
552
553 if (content_length != 0) {
554 /* We have some idea of the content length. Check if it will provide the requested
555 * range */
556 if (target_req->range_end > hint->offset + content_length - 1) {
557 GST_LOG ("Range end %" G_GINT64_FORMAT " is beyond the end (%"
558 G_GINT64_FORMAT ") of this preload", target_req->range_end,
559 hint->offset + content_length - 1);
560 continue;
561 }
562 }
563 }
564
565 GST_DEBUG ("Found a matching preload type %d uri: %s, range start:%"
566 G_GINT64_FORMAT " size %" G_GINT64_FORMAT, hint->hint_type, hint->uri,
567 hint->offset, hint->size);
568
569 if (preload_req->target_request != NULL) {
570 DownloadRequest *old_request = preload_req->target_request;
571
572 /* Detach the existing target request */
573 if (old_request != target_req) {
574 download_request_lock (old_request);
575 old_request->state = DOWNLOAD_REQUEST_STATE_UNSENT;
576 download_request_despatch_completion (old_request);
577 download_request_unlock (old_request);
578 }
579
580 download_request_unref (old_request);
581 preload_req->target_request = NULL;
582 }
583
584 /* Attach the new target request and despatch any available data */
585 preload_req->target_cur_offset = target_req->range_start;
586 preload_req->target_request = download_request_ref (target_req);
587
588 download_request_lock (target_req);
589 target_req->state = DOWNLOAD_REQUEST_STATE_UNSENT;
590 download_request_begin_download (target_req);
591 download_request_unlock (target_req);
592
593 gst_hls_demux_preloader_despatch (preload_req, FALSE);
594 return TRUE;
595 }
596
597 return FALSE;
598 }