2 * Input async protocol.
3 * Copyright (c) 2015 Zhang Rui <bbcallen@gmail.com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Based on libavformat/cache.c by Michael Niedermayer
27 * support work with concatdec, hls
30 #include "libavutil/avassert.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/error.h"
33 #include "libavutil/fifo.h"
34 #include "libavutil/log.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/thread.h"
44 #define BUFFER_CAPACITY (4 * 1024 * 1024)
45 #define READ_BACK_CAPACITY (4 * 1024 * 1024)
46 #define SHORT_SEEK_THRESHOLD (256 * 1024)
48 typedef struct RingBuffer
51 int read_back_capacity
;
56 typedef struct AsyncContext
{
74 pthread_cond_t cond_wakeup_main
;
75 pthread_cond_t cond_wakeup_background
;
76 pthread_mutex_t mutex
;
77 pthread_t async_buffer_thread
;
80 AVIOInterruptCB interrupt_callback
;
83 static int ring_init(RingBuffer
*ring
, unsigned int capacity
, int read_back_capacity
)
85 memset(ring
, 0, sizeof(RingBuffer
));
86 ring
->fifo
= av_fifo_alloc2(capacity
+ read_back_capacity
, 1, 0);
88 return AVERROR(ENOMEM
);
90 ring
->read_back_capacity
= read_back_capacity
;
94 static void ring_destroy(RingBuffer
*ring
)
96 av_fifo_freep2(&ring
->fifo
);
99 static void ring_reset(RingBuffer
*ring
)
101 av_fifo_reset2(ring
->fifo
);
105 static int ring_size(RingBuffer
*ring
)
107 return av_fifo_can_read(ring
->fifo
) - ring
->read_pos
;
110 static int ring_space(RingBuffer
*ring
)
112 return av_fifo_can_write(ring
->fifo
);
115 static int ring_read(RingBuffer
*ring
, void *dest
, int buf_size
)
119 av_assert2(buf_size
<= ring_size(ring
));
121 ret
= av_fifo_peek(ring
->fifo
, dest
, buf_size
, ring
->read_pos
);
122 ring
->read_pos
+= buf_size
;
124 if (ring
->read_pos
> ring
->read_back_capacity
) {
125 av_fifo_drain2(ring
->fifo
, ring
->read_pos
- ring
->read_back_capacity
);
126 ring
->read_pos
= ring
->read_back_capacity
;
132 static int wrapped_url_read(void *src
, void *dst
, size_t *size
)
135 AsyncContext
*c
= h
->priv_data
;
138 ret
= ffurl_read(c
->inner
, dst
, *size
);
139 *size
= ret
> 0 ? ret
: 0;
140 c
->inner_io_error
= ret
< 0 ? ret
: 0;
142 return c
->inner_io_error
;
145 static int ring_write(RingBuffer
*ring
, URLContext
*h
, size_t size
)
149 av_assert2(size
<= ring_space(ring
));
150 ret
= av_fifo_write_from_cb(ring
->fifo
, wrapped_url_read
, h
, &size
);
157 static int ring_size_of_read_back(RingBuffer
*ring
)
159 return ring
->read_pos
;
162 static int ring_drain(RingBuffer
*ring
, int offset
)
164 av_assert2(offset
>= -ring_size_of_read_back(ring
));
165 av_assert2(offset
<= ring_size(ring
));
166 ring
->read_pos
+= offset
;
170 static int async_check_interrupt(void *arg
)
173 AsyncContext
*c
= h
->priv_data
;
175 if (c
->abort_request
)
178 if (ff_check_interrupt(&c
->interrupt_callback
))
179 c
->abort_request
= 1;
181 return c
->abort_request
;
184 static void *async_buffer_task(void *arg
)
187 AsyncContext
*c
= h
->priv_data
;
188 RingBuffer
*ring
= &c
->ring
;
192 ff_thread_setname("async");
195 int fifo_space
, to_copy
;
197 pthread_mutex_lock(&c
->mutex
);
198 if (async_check_interrupt(h
)) {
199 c
->io_eof_reached
= 1;
200 c
->io_error
= AVERROR_EXIT
;
201 pthread_cond_signal(&c
->cond_wakeup_main
);
202 pthread_mutex_unlock(&c
->mutex
);
206 if (c
->seek_request
) {
207 seek_ret
= ffurl_seek(c
->inner
, c
->seek_pos
, c
->seek_whence
);
209 c
->io_eof_reached
= 0;
214 c
->seek_completed
= 1;
215 c
->seek_ret
= seek_ret
;
219 pthread_cond_signal(&c
->cond_wakeup_main
);
220 pthread_mutex_unlock(&c
->mutex
);
224 fifo_space
= ring_space(ring
);
225 if (c
->io_eof_reached
|| fifo_space
<= 0) {
226 pthread_cond_signal(&c
->cond_wakeup_main
);
227 pthread_cond_wait(&c
->cond_wakeup_background
, &c
->mutex
);
228 pthread_mutex_unlock(&c
->mutex
);
231 pthread_mutex_unlock(&c
->mutex
);
233 to_copy
= FFMIN(4096, fifo_space
);
234 ret
= ring_write(ring
, h
, to_copy
);
236 pthread_mutex_lock(&c
->mutex
);
238 c
->io_eof_reached
= 1;
239 if (c
->inner_io_error
< 0)
240 c
->io_error
= c
->inner_io_error
;
243 pthread_cond_signal(&c
->cond_wakeup_main
);
244 pthread_mutex_unlock(&c
->mutex
);
250 static int async_open(URLContext
*h
, const char *arg
, int flags
, AVDictionary
**options
)
252 AsyncContext
*c
= h
->priv_data
;
254 AVIOInterruptCB interrupt_callback
= {.callback
= async_check_interrupt
, .opaque
= h
};
256 av_strstart(arg
, "async:", &arg
);
258 ret
= ring_init(&c
->ring
, BUFFER_CAPACITY
, READ_BACK_CAPACITY
);
262 /* wrap interrupt callback */
263 c
->interrupt_callback
= h
->interrupt_callback
;
264 ret
= ffurl_open_whitelist(&c
->inner
, arg
, flags
, &interrupt_callback
, options
, h
->protocol_whitelist
, h
->protocol_blacklist
, h
);
266 av_log(h
, AV_LOG_ERROR
, "ffurl_open failed : %s, %s\n", av_err2str(ret
), arg
);
270 c
->logical_size
= ffurl_size(c
->inner
);
271 h
->is_streamed
= c
->inner
->is_streamed
;
273 ret
= pthread_mutex_init(&c
->mutex
, NULL
);
276 av_log(h
, AV_LOG_ERROR
, "pthread_mutex_init failed : %s\n", av_err2str(ret
));
280 ret
= pthread_cond_init(&c
->cond_wakeup_main
, NULL
);
283 av_log(h
, AV_LOG_ERROR
, "pthread_cond_init failed : %s\n", av_err2str(ret
));
284 goto cond_wakeup_main_fail
;
287 ret
= pthread_cond_init(&c
->cond_wakeup_background
, NULL
);
290 av_log(h
, AV_LOG_ERROR
, "pthread_cond_init failed : %s\n", av_err2str(ret
));
291 goto cond_wakeup_background_fail
;
294 ret
= pthread_create(&c
->async_buffer_thread
, NULL
, async_buffer_task
, h
);
297 av_log(h
, AV_LOG_ERROR
, "pthread_create failed : %s\n", av_err2str(ret
));
304 pthread_cond_destroy(&c
->cond_wakeup_background
);
305 cond_wakeup_background_fail
:
306 pthread_cond_destroy(&c
->cond_wakeup_main
);
307 cond_wakeup_main_fail
:
308 pthread_mutex_destroy(&c
->mutex
);
310 ffurl_closep(&c
->inner
);
312 ring_destroy(&c
->ring
);
317 static int async_close(URLContext
*h
)
319 AsyncContext
*c
= h
->priv_data
;
322 pthread_mutex_lock(&c
->mutex
);
323 c
->abort_request
= 1;
324 pthread_cond_signal(&c
->cond_wakeup_background
);
325 pthread_mutex_unlock(&c
->mutex
);
327 ret
= pthread_join(c
->async_buffer_thread
, NULL
);
329 av_log(h
, AV_LOG_ERROR
, "pthread_join(): %s\n", av_err2str(ret
));
331 pthread_cond_destroy(&c
->cond_wakeup_background
);
332 pthread_cond_destroy(&c
->cond_wakeup_main
);
333 pthread_mutex_destroy(&c
->mutex
);
334 ffurl_closep(&c
->inner
);
335 ring_destroy(&c
->ring
);
340 static int async_read_internal(URLContext
*h
, void *dest
, int size
)
342 AsyncContext
*c
= h
->priv_data
;
343 RingBuffer
*ring
= &c
->ring
;
344 int read_complete
= !dest
;
348 pthread_mutex_lock(&c
->mutex
);
350 while (to_read
> 0) {
351 int fifo_size
, to_copy
;
352 if (async_check_interrupt(h
)) {
356 fifo_size
= ring_size(ring
);
357 to_copy
= FFMIN(to_read
, fifo_size
);
359 ring_read(ring
, dest
, to_copy
);
361 dest
= (uint8_t *)dest
+ to_copy
;
362 c
->logical_pos
+= to_copy
;
364 ret
= size
- to_read
;
366 if (to_read
<= 0 || !read_complete
)
368 } else if (c
->io_eof_reached
) {
377 pthread_cond_signal(&c
->cond_wakeup_background
);
378 pthread_cond_wait(&c
->cond_wakeup_main
, &c
->mutex
);
381 pthread_cond_signal(&c
->cond_wakeup_background
);
382 pthread_mutex_unlock(&c
->mutex
);
387 static int async_read(URLContext
*h
, unsigned char *buf
, int size
)
389 return async_read_internal(h
, buf
, size
);
392 static int64_t async_seek(URLContext
*h
, int64_t pos
, int whence
)
394 AsyncContext
*c
= h
->priv_data
;
395 RingBuffer
*ring
= &c
->ring
;
397 int64_t new_logical_pos
;
399 int fifo_size_of_read_back
;
401 if (whence
== AVSEEK_SIZE
) {
402 av_log(h
, AV_LOG_TRACE
, "async_seek: AVSEEK_SIZE: %"PRId64
"\n", (int64_t)c
->logical_size
);
403 return c
->logical_size
;
404 } else if (whence
== SEEK_CUR
) {
405 av_log(h
, AV_LOG_TRACE
, "async_seek: %"PRId64
"\n", pos
);
406 new_logical_pos
= pos
+ c
->logical_pos
;
407 } else if (whence
== SEEK_SET
){
408 av_log(h
, AV_LOG_TRACE
, "async_seek: %"PRId64
"\n", pos
);
409 new_logical_pos
= pos
;
411 return AVERROR(EINVAL
);
413 if (new_logical_pos
< 0)
414 return AVERROR(EINVAL
);
416 fifo_size
= ring_size(ring
);
417 fifo_size_of_read_back
= ring_size_of_read_back(ring
);
418 if (new_logical_pos
== c
->logical_pos
) {
419 /* current position */
420 return c
->logical_pos
;
421 } else if ((new_logical_pos
>= (c
->logical_pos
- fifo_size_of_read_back
)) &&
422 (new_logical_pos
< (c
->logical_pos
+ fifo_size
+ SHORT_SEEK_THRESHOLD
))) {
423 int pos_delta
= (int)(new_logical_pos
- c
->logical_pos
);
425 av_log(h
, AV_LOG_TRACE
, "async_seek: fask_seek %"PRId64
" from %d dist:%d/%d\n",
426 new_logical_pos
, (int)c
->logical_pos
,
427 (int)(new_logical_pos
- c
->logical_pos
), fifo_size
);
430 // fast seek forwards
431 async_read_internal(h
, NULL
, pos_delta
);
433 // fast seek backwards
434 ring_drain(ring
, pos_delta
);
435 c
->logical_pos
= new_logical_pos
;
438 return c
->logical_pos
;
439 } else if (c
->logical_size
<= 0) {
441 return AVERROR(EINVAL
);
442 } else if (new_logical_pos
> c
->logical_size
) {
444 return AVERROR(EINVAL
);
447 pthread_mutex_lock(&c
->mutex
);
450 c
->seek_pos
= new_logical_pos
;
451 c
->seek_whence
= SEEK_SET
;
452 c
->seek_completed
= 0;
456 if (async_check_interrupt(h
)) {
460 if (c
->seek_completed
) {
461 if (c
->seek_ret
>= 0)
462 c
->logical_pos
= c
->seek_ret
;
466 pthread_cond_signal(&c
->cond_wakeup_background
);
467 pthread_cond_wait(&c
->cond_wakeup_main
, &c
->mutex
);
470 pthread_mutex_unlock(&c
->mutex
);
475 #define OFFSET(x) offsetof(AsyncContext, x)
476 #define D AV_OPT_FLAG_DECODING_PARAM
478 static const AVOption options
[] = {
485 static const AVClass async_context_class
= {
486 .class_name
= "Async",
487 .item_name
= av_default_item_name
,
489 .version
= LIBAVUTIL_VERSION_INT
,
492 const URLProtocol ff_async_protocol
= {
494 .url_open2
= async_open
,
495 .url_read
= async_read
,
496 .url_seek
= async_seek
,
497 .url_close
= async_close
,
498 .priv_data_size
= sizeof(AsyncContext
),
499 .priv_data_class
= &async_context_class
,
504 #define TEST_SEEK_POS (1536)
505 #define TEST_STREAM_SIZE (2048)
507 typedef struct TestContext
{
510 int64_t logical_size
;
516 static int async_test_open(URLContext
*h
, const char *arg
, int flags
, AVDictionary
**options
)
518 TestContext
*c
= h
->priv_data
;
520 c
->logical_size
= TEST_STREAM_SIZE
;
524 static int async_test_close(URLContext
*h
)
529 static int async_test_read(URLContext
*h
, unsigned char *buf
, int size
)
531 TestContext
*c
= h
->priv_data
;
535 if (c
->opt_read_error
)
536 return c
->opt_read_error
;
538 if (c
->logical_pos
>= c
->logical_size
)
541 for (i
= 0; i
< size
; ++i
) {
542 buf
[i
] = c
->logical_pos
& 0xFF;
547 if (c
->logical_pos
>= c
->logical_size
)
554 static int64_t async_test_seek(URLContext
*h
, int64_t pos
, int whence
)
556 TestContext
*c
= h
->priv_data
;
557 int64_t new_logical_pos
;
559 if (whence
== AVSEEK_SIZE
) {
560 return c
->logical_size
;
561 } else if (whence
== SEEK_CUR
) {
562 new_logical_pos
= pos
+ c
->logical_pos
;
563 } else if (whence
== SEEK_SET
){
564 new_logical_pos
= pos
;
566 return AVERROR(EINVAL
);
568 if (new_logical_pos
< 0)
569 return AVERROR(EINVAL
);
571 c
->logical_pos
= new_logical_pos
;
572 return new_logical_pos
;
575 #define OFFSET(x) offsetof(TestContext, x)
576 #define D AV_OPT_FLAG_DECODING_PARAM
578 static const AVOption async_test_options
[] = {
579 { "async-test-read-error", "cause read fail",
580 OFFSET(opt_read_error
), AV_OPT_TYPE_INT
, { .i64
= 0 }, INT_MIN
, INT_MAX
, .flags
= D
},
587 static const AVClass async_test_context_class
= {
588 .class_name
= "Async-Test",
589 .item_name
= av_default_item_name
,
590 .option
= async_test_options
,
591 .version
= LIBAVUTIL_VERSION_INT
,
594 const URLProtocol ff_async_test_protocol
= {
595 .name
= "async-test",
596 .url_open2
= async_test_open
,
597 .url_read
= async_test_read
,
598 .url_seek
= async_test_seek
,
599 .url_close
= async_test_close
,
600 .priv_data_size
= sizeof(TestContext
),
601 .priv_data_class
= &async_test_context_class
,
606 URLContext
*h
= NULL
;
612 unsigned char buf
[4096];
613 AVDictionary
*opts
= NULL
;
615 ffurl_register_protocol(&ff_async_protocol
);
616 ffurl_register_protocol(&ff_async_test_protocol
);
621 ret
= ffurl_open_whitelist(&h
, "async:async-test:", AVIO_FLAG_READ
,
622 NULL
, NULL
, NULL
, NULL
, NULL
);
623 printf("open: %d\n", ret
);
625 size
= ffurl_size(h
);
626 printf("size: %"PRId64
"\n", size
);
628 pos
= ffurl_seek(h
, 0, SEEK_CUR
);
631 ret
= ffurl_read(h
, buf
, sizeof(buf
));
632 if (ret
== AVERROR_EOF
) {
633 printf("read-error: AVERROR_EOF at %"PRId64
"\n", ffurl_seek(h
, 0, SEEK_CUR
));
639 printf("read-error: %d at %"PRId64
"\n", ret
, ffurl_seek(h
, 0, SEEK_CUR
));
642 for (i
= 0; i
< ret
; ++i
) {
643 if (buf
[i
] != (pos
& 0xFF)) {
644 printf("read-mismatch: actual %d, expecting %d, at %"PRId64
"\n",
645 (int)buf
[i
], (int)(pos
& 0xFF), pos
);
654 printf("read: %"PRId64
"\n", read_len
);
659 ret
= ffurl_read(h
, buf
, 1);
660 printf("read: %d\n", ret
);
662 pos
= ffurl_seek(h
, TEST_SEEK_POS
, SEEK_SET
);
663 printf("seek: %"PRId64
"\n", pos
);
667 ret
= ffurl_read(h
, buf
, sizeof(buf
));
668 if (ret
== AVERROR_EOF
)
673 printf("read-error: %d at %"PRId64
"\n", ret
, ffurl_seek(h
, 0, SEEK_CUR
));
676 for (i
= 0; i
< ret
; ++i
) {
677 if (buf
[i
] != (pos
& 0xFF)) {
678 printf("read-mismatch: actual %d, expecting %d, at %"PRId64
"\n",
679 (int)buf
[i
], (int)(pos
& 0xFF), pos
);
688 printf("read: %"PRId64
"\n", read_len
);
690 ret
= ffurl_read(h
, buf
, 1);
691 printf("read: %d\n", ret
);
697 av_dict_set_int(&opts
, "async-test-read-error", -10000, 0);
698 ret
= ffurl_open_whitelist(&h
, "async:async-test:", AVIO_FLAG_READ
,
699 NULL
, &opts
, NULL
, NULL
, NULL
);
700 printf("open: %d\n", ret
);
702 ret
= ffurl_read(h
, buf
, 1);
703 printf("read: %d\n", ret
);