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 Context
{
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_alloc(capacity
+ read_back_capacity
);
88 return AVERROR(ENOMEM
);
90 ring
->read_back_capacity
= read_back_capacity
;
94 static void ring_destroy(RingBuffer
*ring
)
96 av_fifo_freep(&ring
->fifo
);
99 static void ring_reset(RingBuffer
*ring
)
101 av_fifo_reset(ring
->fifo
);
105 static int ring_size(RingBuffer
*ring
)
107 return av_fifo_size(ring
->fifo
) - ring
->read_pos
;
110 static int ring_space(RingBuffer
*ring
)
112 return av_fifo_space(ring
->fifo
);
115 static int ring_generic_read(RingBuffer
*ring
, void *dest
, int buf_size
, void (*func
)(void*, void*, int))
119 av_assert2(buf_size
<= ring_size(ring
));
120 ret
= av_fifo_generic_peek_at(ring
->fifo
, dest
, ring
->read_pos
, buf_size
, func
);
121 ring
->read_pos
+= buf_size
;
123 if (ring
->read_pos
> ring
->read_back_capacity
) {
124 av_fifo_drain(ring
->fifo
, ring
->read_pos
- ring
->read_back_capacity
);
125 ring
->read_pos
= ring
->read_back_capacity
;
131 static int ring_generic_write(RingBuffer
*ring
, void *src
, int size
, int (*func
)(void*, void*, int))
133 av_assert2(size
<= ring_space(ring
));
134 return av_fifo_generic_write(ring
->fifo
, src
, size
, func
);
137 static int ring_size_of_read_back(RingBuffer
*ring
)
139 return ring
->read_pos
;
142 static int ring_drain(RingBuffer
*ring
, int offset
)
144 av_assert2(offset
>= -ring_size_of_read_back(ring
));
145 av_assert2(offset
<= ring_size(ring
));
146 ring
->read_pos
+= offset
;
150 static int async_check_interrupt(void *arg
)
153 Context
*c
= h
->priv_data
;
155 if (c
->abort_request
)
158 if (ff_check_interrupt(&c
->interrupt_callback
))
159 c
->abort_request
= 1;
161 return c
->abort_request
;
164 static int wrapped_url_read(void *src
, void *dst
, int size
)
167 Context
*c
= h
->priv_data
;
170 ret
= ffurl_read(c
->inner
, dst
, size
);
171 c
->inner_io_error
= ret
< 0 ? ret
: 0;
176 static void *async_buffer_task(void *arg
)
179 Context
*c
= h
->priv_data
;
180 RingBuffer
*ring
= &c
->ring
;
185 int fifo_space
, to_copy
;
187 pthread_mutex_lock(&c
->mutex
);
188 if (async_check_interrupt(h
)) {
189 c
->io_eof_reached
= 1;
190 c
->io_error
= AVERROR_EXIT
;
191 pthread_cond_signal(&c
->cond_wakeup_main
);
192 pthread_mutex_unlock(&c
->mutex
);
196 if (c
->seek_request
) {
197 seek_ret
= ffurl_seek(c
->inner
, c
->seek_pos
, c
->seek_whence
);
199 c
->io_eof_reached
= 0;
204 c
->seek_completed
= 1;
205 c
->seek_ret
= seek_ret
;
209 pthread_cond_signal(&c
->cond_wakeup_main
);
210 pthread_mutex_unlock(&c
->mutex
);
214 fifo_space
= ring_space(ring
);
215 if (c
->io_eof_reached
|| fifo_space
<= 0) {
216 pthread_cond_signal(&c
->cond_wakeup_main
);
217 pthread_cond_wait(&c
->cond_wakeup_background
, &c
->mutex
);
218 pthread_mutex_unlock(&c
->mutex
);
221 pthread_mutex_unlock(&c
->mutex
);
223 to_copy
= FFMIN(4096, fifo_space
);
224 ret
= ring_generic_write(ring
, (void *)h
, to_copy
, wrapped_url_read
);
226 pthread_mutex_lock(&c
->mutex
);
228 c
->io_eof_reached
= 1;
229 if (c
->inner_io_error
< 0)
230 c
->io_error
= c
->inner_io_error
;
233 pthread_cond_signal(&c
->cond_wakeup_main
);
234 pthread_mutex_unlock(&c
->mutex
);
240 static int async_open(URLContext
*h
, const char *arg
, int flags
, AVDictionary
**options
)
242 Context
*c
= h
->priv_data
;
244 AVIOInterruptCB interrupt_callback
= {.callback
= async_check_interrupt
, .opaque
= h
};
246 av_strstart(arg
, "async:", &arg
);
248 ret
= ring_init(&c
->ring
, BUFFER_CAPACITY
, READ_BACK_CAPACITY
);
252 /* wrap interrupt callback */
253 c
->interrupt_callback
= h
->interrupt_callback
;
254 ret
= ffurl_open_whitelist(&c
->inner
, arg
, flags
, &interrupt_callback
, options
, h
->protocol_whitelist
, h
->protocol_blacklist
, h
);
256 av_log(h
, AV_LOG_ERROR
, "ffurl_open failed : %s, %s\n", av_err2str(ret
), arg
);
260 c
->logical_size
= ffurl_size(c
->inner
);
261 h
->is_streamed
= c
->inner
->is_streamed
;
263 ret
= pthread_mutex_init(&c
->mutex
, NULL
);
266 av_log(h
, AV_LOG_ERROR
, "pthread_mutex_init failed : %s\n", av_err2str(ret
));
270 ret
= pthread_cond_init(&c
->cond_wakeup_main
, NULL
);
273 av_log(h
, AV_LOG_ERROR
, "pthread_cond_init failed : %s\n", av_err2str(ret
));
274 goto cond_wakeup_main_fail
;
277 ret
= pthread_cond_init(&c
->cond_wakeup_background
, NULL
);
280 av_log(h
, AV_LOG_ERROR
, "pthread_cond_init failed : %s\n", av_err2str(ret
));
281 goto cond_wakeup_background_fail
;
284 ret
= pthread_create(&c
->async_buffer_thread
, NULL
, async_buffer_task
, h
);
287 av_log(h
, AV_LOG_ERROR
, "pthread_create failed : %s\n", av_err2str(ret
));
294 pthread_cond_destroy(&c
->cond_wakeup_background
);
295 cond_wakeup_background_fail
:
296 pthread_cond_destroy(&c
->cond_wakeup_main
);
297 cond_wakeup_main_fail
:
298 pthread_mutex_destroy(&c
->mutex
);
300 ffurl_closep(&c
->inner
);
302 ring_destroy(&c
->ring
);
307 static int async_close(URLContext
*h
)
309 Context
*c
= h
->priv_data
;
312 pthread_mutex_lock(&c
->mutex
);
313 c
->abort_request
= 1;
314 pthread_cond_signal(&c
->cond_wakeup_background
);
315 pthread_mutex_unlock(&c
->mutex
);
317 ret
= pthread_join(c
->async_buffer_thread
, NULL
);
319 av_log(h
, AV_LOG_ERROR
, "pthread_join(): %s\n", av_err2str(ret
));
321 pthread_cond_destroy(&c
->cond_wakeup_background
);
322 pthread_cond_destroy(&c
->cond_wakeup_main
);
323 pthread_mutex_destroy(&c
->mutex
);
324 ffurl_closep(&c
->inner
);
325 ring_destroy(&c
->ring
);
330 static int async_read_internal(URLContext
*h
, void *dest
, int size
, int read_complete
,
331 void (*func
)(void*, void*, int))
333 Context
*c
= h
->priv_data
;
334 RingBuffer
*ring
= &c
->ring
;
338 pthread_mutex_lock(&c
->mutex
);
340 while (to_read
> 0) {
341 int fifo_size
, to_copy
;
342 if (async_check_interrupt(h
)) {
346 fifo_size
= ring_size(ring
);
347 to_copy
= FFMIN(to_read
, fifo_size
);
349 ring_generic_read(ring
, dest
, to_copy
, func
);
351 dest
= (uint8_t *)dest
+ to_copy
;
352 c
->logical_pos
+= to_copy
;
354 ret
= size
- to_read
;
356 if (to_read
<= 0 || !read_complete
)
358 } else if (c
->io_eof_reached
) {
367 pthread_cond_signal(&c
->cond_wakeup_background
);
368 pthread_cond_wait(&c
->cond_wakeup_main
, &c
->mutex
);
371 pthread_cond_signal(&c
->cond_wakeup_background
);
372 pthread_mutex_unlock(&c
->mutex
);
377 static int async_read(URLContext
*h
, unsigned char *buf
, int size
)
379 return async_read_internal(h
, buf
, size
, 0, NULL
);
382 static void fifo_do_not_copy_func(void* dest
, void* src
, int size
) {
386 static int64_t async_seek(URLContext
*h
, int64_t pos
, int whence
)
388 Context
*c
= h
->priv_data
;
389 RingBuffer
*ring
= &c
->ring
;
391 int64_t new_logical_pos
;
393 int fifo_size_of_read_back
;
395 if (whence
== AVSEEK_SIZE
) {
396 av_log(h
, AV_LOG_TRACE
, "async_seek: AVSEEK_SIZE: %"PRId64
"\n", (int64_t)c
->logical_size
);
397 return c
->logical_size
;
398 } else if (whence
== SEEK_CUR
) {
399 av_log(h
, AV_LOG_TRACE
, "async_seek: %"PRId64
"\n", pos
);
400 new_logical_pos
= pos
+ c
->logical_pos
;
401 } else if (whence
== SEEK_SET
){
402 av_log(h
, AV_LOG_TRACE
, "async_seek: %"PRId64
"\n", pos
);
403 new_logical_pos
= pos
;
405 return AVERROR(EINVAL
);
407 if (new_logical_pos
< 0)
408 return AVERROR(EINVAL
);
410 fifo_size
= ring_size(ring
);
411 fifo_size_of_read_back
= ring_size_of_read_back(ring
);
412 if (new_logical_pos
== c
->logical_pos
) {
413 /* current position */
414 return c
->logical_pos
;
415 } else if ((new_logical_pos
>= (c
->logical_pos
- fifo_size_of_read_back
)) &&
416 (new_logical_pos
< (c
->logical_pos
+ fifo_size
+ SHORT_SEEK_THRESHOLD
))) {
417 int pos_delta
= (int)(new_logical_pos
- c
->logical_pos
);
419 av_log(h
, AV_LOG_TRACE
, "async_seek: fask_seek %"PRId64
" from %d dist:%d/%d\n",
420 new_logical_pos
, (int)c
->logical_pos
,
421 (int)(new_logical_pos
- c
->logical_pos
), fifo_size
);
424 // fast seek forwards
425 async_read_internal(h
, NULL
, pos_delta
, 1, fifo_do_not_copy_func
);
427 // fast seek backwards
428 ring_drain(ring
, pos_delta
);
429 c
->logical_pos
= new_logical_pos
;
432 return c
->logical_pos
;
433 } else if (c
->logical_size
<= 0) {
435 return AVERROR(EINVAL
);
436 } else if (new_logical_pos
> c
->logical_size
) {
438 return AVERROR(EINVAL
);
441 pthread_mutex_lock(&c
->mutex
);
444 c
->seek_pos
= new_logical_pos
;
445 c
->seek_whence
= SEEK_SET
;
446 c
->seek_completed
= 0;
450 if (async_check_interrupt(h
)) {
454 if (c
->seek_completed
) {
455 if (c
->seek_ret
>= 0)
456 c
->logical_pos
= c
->seek_ret
;
460 pthread_cond_signal(&c
->cond_wakeup_background
);
461 pthread_cond_wait(&c
->cond_wakeup_main
, &c
->mutex
);
464 pthread_mutex_unlock(&c
->mutex
);
469 #define OFFSET(x) offsetof(Context, x)
470 #define D AV_OPT_FLAG_DECODING_PARAM
472 static const AVOption options
[] = {
479 static const AVClass async_context_class
= {
480 .class_name
= "Async",
481 .item_name
= av_default_item_name
,
483 .version
= LIBAVUTIL_VERSION_INT
,
486 const URLProtocol ff_async_protocol
= {
488 .url_open2
= async_open
,
489 .url_read
= async_read
,
490 .url_seek
= async_seek
,
491 .url_close
= async_close
,
492 .priv_data_size
= sizeof(Context
),
493 .priv_data_class
= &async_context_class
,
498 #define TEST_SEEK_POS (1536)
499 #define TEST_STREAM_SIZE (2048)
501 typedef struct TestContext
{
504 int64_t logical_size
;
510 static int async_test_open(URLContext
*h
, const char *arg
, int flags
, AVDictionary
**options
)
512 TestContext
*c
= h
->priv_data
;
514 c
->logical_size
= TEST_STREAM_SIZE
;
518 static int async_test_close(URLContext
*h
)
523 static int async_test_read(URLContext
*h
, unsigned char *buf
, int size
)
525 TestContext
*c
= h
->priv_data
;
529 if (c
->opt_read_error
)
530 return c
->opt_read_error
;
532 if (c
->logical_pos
>= c
->logical_size
)
535 for (i
= 0; i
< size
; ++i
) {
536 buf
[i
] = c
->logical_pos
& 0xFF;
541 if (c
->logical_pos
>= c
->logical_size
)
548 static int64_t async_test_seek(URLContext
*h
, int64_t pos
, int whence
)
550 TestContext
*c
= h
->priv_data
;
551 int64_t new_logical_pos
;
553 if (whence
== AVSEEK_SIZE
) {
554 return c
->logical_size
;
555 } else if (whence
== SEEK_CUR
) {
556 new_logical_pos
= pos
+ c
->logical_pos
;
557 } else if (whence
== SEEK_SET
){
558 new_logical_pos
= pos
;
560 return AVERROR(EINVAL
);
562 if (new_logical_pos
< 0)
563 return AVERROR(EINVAL
);
565 c
->logical_pos
= new_logical_pos
;
566 return new_logical_pos
;
569 #define OFFSET(x) offsetof(TestContext, x)
570 #define D AV_OPT_FLAG_DECODING_PARAM
572 static const AVOption async_test_options
[] = {
573 { "async-test-read-error", "cause read fail",
574 OFFSET(opt_read_error
), AV_OPT_TYPE_INT
, { .i64
= 0 }, INT_MIN
, INT_MAX
, .flags
= D
},
581 static const AVClass async_test_context_class
= {
582 .class_name
= "Async-Test",
583 .item_name
= av_default_item_name
,
584 .option
= async_test_options
,
585 .version
= LIBAVUTIL_VERSION_INT
,
588 const URLProtocol ff_async_test_protocol
= {
589 .name
= "async-test",
590 .url_open2
= async_test_open
,
591 .url_read
= async_test_read
,
592 .url_seek
= async_test_seek
,
593 .url_close
= async_test_close
,
594 .priv_data_size
= sizeof(TestContext
),
595 .priv_data_class
= &async_test_context_class
,
600 URLContext
*h
= NULL
;
606 unsigned char buf
[4096];
607 AVDictionary
*opts
= NULL
;
609 ffurl_register_protocol(&ff_async_protocol
);
610 ffurl_register_protocol(&ff_async_test_protocol
);
615 ret
= ffurl_open_whitelist(&h
, "async:async-test:", AVIO_FLAG_READ
,
616 NULL
, NULL
, NULL
, NULL
, NULL
);
617 printf("open: %d\n", ret
);
619 size
= ffurl_size(h
);
620 printf("size: %"PRId64
"\n", size
);
622 pos
= ffurl_seek(h
, 0, SEEK_CUR
);
625 ret
= ffurl_read(h
, buf
, sizeof(buf
));
626 if (ret
== AVERROR_EOF
) {
627 printf("read-error: AVERROR_EOF at %"PRId64
"\n", ffurl_seek(h
, 0, SEEK_CUR
));
633 printf("read-error: %d at %"PRId64
"\n", ret
, ffurl_seek(h
, 0, SEEK_CUR
));
636 for (i
= 0; i
< ret
; ++i
) {
637 if (buf
[i
] != (pos
& 0xFF)) {
638 printf("read-mismatch: actual %d, expecting %d, at %"PRId64
"\n",
639 (int)buf
[i
], (int)(pos
& 0xFF), pos
);
648 printf("read: %"PRId64
"\n", read_len
);
653 ret
= ffurl_read(h
, buf
, 1);
654 printf("read: %d\n", ret
);
656 pos
= ffurl_seek(h
, TEST_SEEK_POS
, SEEK_SET
);
657 printf("seek: %"PRId64
"\n", pos
);
661 ret
= ffurl_read(h
, buf
, sizeof(buf
));
662 if (ret
== AVERROR_EOF
)
667 printf("read-error: %d at %"PRId64
"\n", ret
, ffurl_seek(h
, 0, SEEK_CUR
));
670 for (i
= 0; i
< ret
; ++i
) {
671 if (buf
[i
] != (pos
& 0xFF)) {
672 printf("read-mismatch: actual %d, expecting %d, at %"PRId64
"\n",
673 (int)buf
[i
], (int)(pos
& 0xFF), pos
);
682 printf("read: %"PRId64
"\n", read_len
);
684 ret
= ffurl_read(h
, buf
, 1);
685 printf("read: %d\n", ret
);
691 av_dict_set_int(&opts
, "async-test-read-error", -10000, 0);
692 ret
= ffurl_open_whitelist(&h
, "async:async-test:", AVIO_FLAG_READ
,
693 NULL
, &opts
, NULL
, NULL
, NULL
);
694 printf("open: %d\n", ret
);
696 ret
= ffurl_read(h
, buf
, 1);
697 printf("read: %d\n", ret
);