1 /*
2  * Input async protocol.
3  * Copyright (c) 2015 Zhang Rui <bbcallen@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  *
21  * Based on libavformat/cache.c by Michael Niedermayer
22  */
23 
24  /**
25  * @TODO
26  *      support timeout
27  *      support work with concatdec, hls
28  */
29 
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"
37 #include "url.h"
38 #include <stdint.h>
39 
40 #if HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 
44 #define BUFFER_CAPACITY         (4 * 1024 * 1024)
45 #define READ_BACK_CAPACITY      (4 * 1024 * 1024)
46 #define SHORT_SEEK_THRESHOLD    (256 * 1024)
47 
48 typedef struct RingBuffer
49 {
50     AVFifo       *fifo;
51     int           read_back_capacity;
52 
53     int           read_pos;
54 } RingBuffer;
55 
56 typedef struct Context {
57     AVClass        *class;
58     URLContext     *inner;
59 
60     int             seek_request;
61     int64_t         seek_pos;
62     int             seek_whence;
63     int             seek_completed;
64     int64_t         seek_ret;
65 
66     int             inner_io_error;
67     int             io_error;
68     int             io_eof_reached;
69 
70     int64_t         logical_pos;
71     int64_t         logical_size;
72     RingBuffer      ring;
73 
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;
78 
79     int             abort_request;
80     AVIOInterruptCB interrupt_callback;
81 } Context;
82 
ring_init(RingBuffer *ring, unsigned int capacity, int read_back_capacity)83 static int ring_init(RingBuffer *ring, unsigned int capacity, int read_back_capacity)
84 {
85     memset(ring, 0, sizeof(RingBuffer));
86     ring->fifo = av_fifo_alloc2(capacity + read_back_capacity, 1, 0);
87     if (!ring->fifo)
88         return AVERROR(ENOMEM);
89 
90     ring->read_back_capacity = read_back_capacity;
91     return 0;
92 }
93 
ring_destroy(RingBuffer *ring)94 static void ring_destroy(RingBuffer *ring)
95 {
96     av_fifo_freep2(&ring->fifo);
97 }
98 
ring_reset(RingBuffer *ring)99 static void ring_reset(RingBuffer *ring)
100 {
101     av_fifo_reset2(ring->fifo);
102     ring->read_pos = 0;
103 }
104 
ring_size(RingBuffer *ring)105 static int ring_size(RingBuffer *ring)
106 {
107     return av_fifo_can_read(ring->fifo) - ring->read_pos;
108 }
109 
ring_space(RingBuffer *ring)110 static int ring_space(RingBuffer *ring)
111 {
112     return av_fifo_can_write(ring->fifo);
113 }
114 
ring_read(RingBuffer *ring, void *dest, int buf_size)115 static int ring_read(RingBuffer *ring, void *dest, int buf_size)
116 {
117     int ret = 0;
118 
119     av_assert2(buf_size <= ring_size(ring));
120     if (dest)
121         ret = av_fifo_peek(ring->fifo, dest, buf_size, ring->read_pos);
122     ring->read_pos += buf_size;
123 
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;
127     }
128 
129     return ret;
130 }
131 
wrapped_url_read(void *src, void *dst, size_t *size)132 static int wrapped_url_read(void *src, void *dst, size_t *size)
133 {
134     URLContext *h   = src;
135     Context    *c   = h->priv_data;
136     int         ret;
137 
138     ret = ffurl_read(c->inner, dst, *size);
139     *size             = ret > 0 ? ret : 0;
140     c->inner_io_error = ret < 0 ? ret : 0;
141 
142     return c->inner_io_error;
143 }
144 
ring_write(RingBuffer *ring, URLContext *h, size_t size)145 static int ring_write(RingBuffer *ring, URLContext *h, size_t size)
146 {
147     int ret;
148 
149     av_assert2(size <= ring_space(ring));
150     ret = av_fifo_write_from_cb(ring->fifo, wrapped_url_read, h, &size);
151     if (ret < 0)
152         return ret;
153 
154     return size;
155 }
156 
ring_size_of_read_back(RingBuffer *ring)157 static int ring_size_of_read_back(RingBuffer *ring)
158 {
159     return ring->read_pos;
160 }
161 
ring_drain(RingBuffer *ring, int offset)162 static int ring_drain(RingBuffer *ring, int offset)
163 {
164     av_assert2(offset >= -ring_size_of_read_back(ring));
165     av_assert2(offset <= ring_size(ring));
166     ring->read_pos += offset;
167     return 0;
168 }
169 
async_check_interrupt(void *arg)170 static int async_check_interrupt(void *arg)
171 {
172     URLContext *h   = arg;
173     Context    *c   = h->priv_data;
174 
175     if (c->abort_request)
176         return 1;
177 
178     if (ff_check_interrupt(&c->interrupt_callback))
179         c->abort_request = 1;
180 
181     return c->abort_request;
182 }
183 
async_buffer_task(void *arg)184 static void *async_buffer_task(void *arg)
185 {
186     URLContext   *h    = arg;
187     Context      *c    = h->priv_data;
188     RingBuffer   *ring = &c->ring;
189     int           ret  = 0;
190     int64_t       seek_ret;
191 
192     while (1) {
193         int fifo_space, to_copy;
194 
195         pthread_mutex_lock(&c->mutex);
196         if (async_check_interrupt(h)) {
197             c->io_eof_reached = 1;
198             c->io_error       = AVERROR_EXIT;
199             pthread_cond_signal(&c->cond_wakeup_main);
200             pthread_mutex_unlock(&c->mutex);
201             break;
202         }
203 
204         if (c->seek_request) {
205             seek_ret = ffurl_seek(c->inner, c->seek_pos, c->seek_whence);
206             if (seek_ret >= 0) {
207                 c->io_eof_reached = 0;
208                 c->io_error       = 0;
209                 ring_reset(ring);
210             }
211 
212             c->seek_completed = 1;
213             c->seek_ret       = seek_ret;
214             c->seek_request   = 0;
215 
216 
217             pthread_cond_signal(&c->cond_wakeup_main);
218             pthread_mutex_unlock(&c->mutex);
219             continue;
220         }
221 
222         fifo_space = ring_space(ring);
223         if (c->io_eof_reached || fifo_space <= 0) {
224             pthread_cond_signal(&c->cond_wakeup_main);
225             pthread_cond_wait(&c->cond_wakeup_background, &c->mutex);
226             pthread_mutex_unlock(&c->mutex);
227             continue;
228         }
229         pthread_mutex_unlock(&c->mutex);
230 
231         to_copy = FFMIN(4096, fifo_space);
232         ret = ring_write(ring, h, to_copy);
233 
234         pthread_mutex_lock(&c->mutex);
235         if (ret <= 0) {
236             c->io_eof_reached = 1;
237             if (c->inner_io_error < 0)
238                 c->io_error = c->inner_io_error;
239         }
240 
241         pthread_cond_signal(&c->cond_wakeup_main);
242         pthread_mutex_unlock(&c->mutex);
243     }
244 
245     return NULL;
246 }
247 
async_open(URLContext *h, const char *arg, int flags, AVDictionary **options)248 static int async_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
249 {
250     Context         *c = h->priv_data;
251     int              ret;
252     AVIOInterruptCB  interrupt_callback = {.callback = async_check_interrupt, .opaque = h};
253 
254     av_strstart(arg, "async:", &arg);
255 
256     ret = ring_init(&c->ring, BUFFER_CAPACITY, READ_BACK_CAPACITY);
257     if (ret < 0)
258         goto fifo_fail;
259 
260     /* wrap interrupt callback */
261     c->interrupt_callback = h->interrupt_callback;
262     ret = ffurl_open_whitelist(&c->inner, arg, flags, &interrupt_callback, options, h->protocol_whitelist, h->protocol_blacklist, h);
263     if (ret != 0) {
264         av_log(h, AV_LOG_ERROR, "ffurl_open failed : %s, %s\n", av_err2str(ret), arg);
265         goto url_fail;
266     }
267 
268     c->logical_size = ffurl_size(c->inner);
269     h->is_streamed  = c->inner->is_streamed;
270 
271     ret = pthread_mutex_init(&c->mutex, NULL);
272     if (ret != 0) {
273         ret = AVERROR(ret);
274         av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", av_err2str(ret));
275         goto mutex_fail;
276     }
277 
278     ret = pthread_cond_init(&c->cond_wakeup_main, NULL);
279     if (ret != 0) {
280         ret = AVERROR(ret);
281         av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", av_err2str(ret));
282         goto cond_wakeup_main_fail;
283     }
284 
285     ret = pthread_cond_init(&c->cond_wakeup_background, NULL);
286     if (ret != 0) {
287         ret = AVERROR(ret);
288         av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", av_err2str(ret));
289         goto cond_wakeup_background_fail;
290     }
291 
292     ret = pthread_create(&c->async_buffer_thread, NULL, async_buffer_task, h);
293     if (ret) {
294         ret = AVERROR(ret);
295         av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", av_err2str(ret));
296         goto thread_fail;
297     }
298 
299     return 0;
300 
301 thread_fail:
302     pthread_cond_destroy(&c->cond_wakeup_background);
303 cond_wakeup_background_fail:
304     pthread_cond_destroy(&c->cond_wakeup_main);
305 cond_wakeup_main_fail:
306     pthread_mutex_destroy(&c->mutex);
307 mutex_fail:
308     ffurl_closep(&c->inner);
309 url_fail:
310     ring_destroy(&c->ring);
311 fifo_fail:
312     return ret;
313 }
314 
async_close(URLContext *h)315 static int async_close(URLContext *h)
316 {
317     Context *c = h->priv_data;
318     int      ret;
319 
320     pthread_mutex_lock(&c->mutex);
321     c->abort_request = 1;
322     pthread_cond_signal(&c->cond_wakeup_background);
323     pthread_mutex_unlock(&c->mutex);
324 
325     ret = pthread_join(c->async_buffer_thread, NULL);
326     if (ret != 0)
327         av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", av_err2str(ret));
328 
329     pthread_cond_destroy(&c->cond_wakeup_background);
330     pthread_cond_destroy(&c->cond_wakeup_main);
331     pthread_mutex_destroy(&c->mutex);
332     ffurl_closep(&c->inner);
333     ring_destroy(&c->ring);
334 
335     return 0;
336 }
337 
async_read_internal(URLContext *h, void *dest, int size)338 static int async_read_internal(URLContext *h, void *dest, int size)
339 {
340     Context      *c       = h->priv_data;
341     RingBuffer   *ring    = &c->ring;
342     int     read_complete = !dest;
343     int           to_read = size;
344     int           ret     = 0;
345 
346     pthread_mutex_lock(&c->mutex);
347 
348     while (to_read > 0) {
349         int fifo_size, to_copy;
350         if (async_check_interrupt(h)) {
351             ret = AVERROR_EXIT;
352             break;
353         }
354         fifo_size = ring_size(ring);
355         to_copy   = FFMIN(to_read, fifo_size);
356         if (to_copy > 0) {
357             ring_read(ring, dest, to_copy);
358             if (dest)
359                 dest = (uint8_t *)dest + to_copy;
360             c->logical_pos += to_copy;
361             to_read        -= to_copy;
362             ret             = size - to_read;
363 
364             if (to_read <= 0 || !read_complete)
365                 break;
366         } else if (c->io_eof_reached) {
367             if (ret <= 0) {
368                 if (c->io_error)
369                     ret = c->io_error;
370                 else
371                     ret = AVERROR_EOF;
372             }
373             break;
374         }
375         pthread_cond_signal(&c->cond_wakeup_background);
376         pthread_cond_wait(&c->cond_wakeup_main, &c->mutex);
377     }
378 
379     pthread_cond_signal(&c->cond_wakeup_background);
380     pthread_mutex_unlock(&c->mutex);
381 
382     return ret;
383 }
384 
async_read(URLContext *h, unsigned char *buf, int size)385 static int async_read(URLContext *h, unsigned char *buf, int size)
386 {
387     return async_read_internal(h, buf, size);
388 }
389 
async_seek(URLContext *h, int64_t pos, int whence)390 static int64_t async_seek(URLContext *h, int64_t pos, int whence)
391 {
392     Context      *c    = h->priv_data;
393     RingBuffer   *ring = &c->ring;
394     int64_t       ret;
395     int64_t       new_logical_pos;
396     int fifo_size;
397     int fifo_size_of_read_back;
398 
399     if (whence == AVSEEK_SIZE) {
400         av_log(h, AV_LOG_TRACE, "async_seek: AVSEEK_SIZE: %"PRId64"\n", (int64_t)c->logical_size);
401         return c->logical_size;
402     } else if (whence == SEEK_CUR) {
403         av_log(h, AV_LOG_TRACE, "async_seek: %"PRId64"\n", pos);
404         new_logical_pos = pos + c->logical_pos;
405     } else if (whence == SEEK_SET){
406         av_log(h, AV_LOG_TRACE, "async_seek: %"PRId64"\n", pos);
407         new_logical_pos = pos;
408     } else {
409         return AVERROR(EINVAL);
410     }
411     if (new_logical_pos < 0)
412         return AVERROR(EINVAL);
413 
414     fifo_size = ring_size(ring);
415     fifo_size_of_read_back = ring_size_of_read_back(ring);
416     if (new_logical_pos == c->logical_pos) {
417         /* current position */
418         return c->logical_pos;
419     } else if ((new_logical_pos >= (c->logical_pos - fifo_size_of_read_back)) &&
420                (new_logical_pos < (c->logical_pos + fifo_size + SHORT_SEEK_THRESHOLD))) {
421         int pos_delta = (int)(new_logical_pos - c->logical_pos);
422         /* fast seek */
423         av_log(h, AV_LOG_TRACE, "async_seek: fask_seek %"PRId64" from %d dist:%d/%d\n",
424                 new_logical_pos, (int)c->logical_pos,
425                 (int)(new_logical_pos - c->logical_pos), fifo_size);
426 
427         if (pos_delta > 0) {
428             // fast seek forwards
429             async_read_internal(h, NULL, pos_delta);
430         } else {
431             // fast seek backwards
432             ring_drain(ring, pos_delta);
433             c->logical_pos = new_logical_pos;
434         }
435 
436         return c->logical_pos;
437     } else if (c->logical_size <= 0) {
438         /* can not seek */
439         return AVERROR(EINVAL);
440     } else if (new_logical_pos > c->logical_size) {
441         /* beyond end */
442         return AVERROR(EINVAL);
443     }
444 
445     pthread_mutex_lock(&c->mutex);
446 
447     c->seek_request   = 1;
448     c->seek_pos       = new_logical_pos;
449     c->seek_whence    = SEEK_SET;
450     c->seek_completed = 0;
451     c->seek_ret       = 0;
452 
453     while (1) {
454         if (async_check_interrupt(h)) {
455             ret = AVERROR_EXIT;
456             break;
457         }
458         if (c->seek_completed) {
459             if (c->seek_ret >= 0)
460                 c->logical_pos  = c->seek_ret;
461             ret = c->seek_ret;
462             break;
463         }
464         pthread_cond_signal(&c->cond_wakeup_background);
465         pthread_cond_wait(&c->cond_wakeup_main, &c->mutex);
466     }
467 
468     pthread_mutex_unlock(&c->mutex);
469 
470     return ret;
471 }
472 
473 #define OFFSET(x) offsetof(Context, x)
474 #define D AV_OPT_FLAG_DECODING_PARAM
475 
476 static const AVOption options[] = {
477     {NULL},
478 };
479 
480 #undef D
481 #undef OFFSET
482 
483 static const AVClass async_context_class = {
484     .class_name = "Async",
485     .item_name  = av_default_item_name,
486     .option     = options,
487     .version    = LIBAVUTIL_VERSION_INT,
488 };
489 
490 const URLProtocol ff_async_protocol = {
491     .name                = "async",
492     .url_open2           = async_open,
493     .url_read            = async_read,
494     .url_seek            = async_seek,
495     .url_close           = async_close,
496     .priv_data_size      = sizeof(Context),
497     .priv_data_class     = &async_context_class,
498 };
499 
500 #if 0
501 
502 #define TEST_SEEK_POS    (1536)
503 #define TEST_STREAM_SIZE (2048)
504 
505 typedef struct TestContext {
506     AVClass        *class;
507     int64_t         logical_pos;
508     int64_t         logical_size;
509 
510     /* options */
511     int             opt_read_error;
512 } TestContext;
513 
514 static int async_test_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
515 {
516     TestContext *c = h->priv_data;
517     c->logical_pos  = 0;
518     c->logical_size = TEST_STREAM_SIZE;
519     return 0;
520 }
521 
522 static int async_test_close(URLContext *h)
523 {
524     return 0;
525 }
526 
527 static int async_test_read(URLContext *h, unsigned char *buf, int size)
528 {
529     TestContext *c = h->priv_data;
530     int          i;
531     int          read_len = 0;
532 
533     if (c->opt_read_error)
534         return c->opt_read_error;
535 
536     if (c->logical_pos >= c->logical_size)
537         return AVERROR_EOF;
538 
539     for (i = 0; i < size; ++i) {
540         buf[i] = c->logical_pos & 0xFF;
541 
542         c->logical_pos++;
543         read_len++;
544 
545         if (c->logical_pos >= c->logical_size)
546             break;
547     }
548 
549     return read_len;
550 }
551 
552 static int64_t async_test_seek(URLContext *h, int64_t pos, int whence)
553 {
554     TestContext *c = h->priv_data;
555     int64_t      new_logical_pos;
556 
557     if (whence == AVSEEK_SIZE) {
558         return c->logical_size;
559     } else if (whence == SEEK_CUR) {
560         new_logical_pos = pos + c->logical_pos;
561     } else if (whence == SEEK_SET){
562         new_logical_pos = pos;
563     } else {
564         return AVERROR(EINVAL);
565     }
566     if (new_logical_pos < 0)
567         return AVERROR(EINVAL);
568 
569     c->logical_pos = new_logical_pos;
570     return new_logical_pos;
571 }
572 
573 #define OFFSET(x) offsetof(TestContext, x)
574 #define D AV_OPT_FLAG_DECODING_PARAM
575 
576 static const AVOption async_test_options[] = {
577     { "async-test-read-error",      "cause read fail",
578         OFFSET(opt_read_error),     AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, .flags = D },
579     {NULL},
580 };
581 
582 #undef D
583 #undef OFFSET
584 
585 static const AVClass async_test_context_class = {
586     .class_name = "Async-Test",
587     .item_name  = av_default_item_name,
588     .option     = async_test_options,
589     .version    = LIBAVUTIL_VERSION_INT,
590 };
591 
592 const URLProtocol ff_async_test_protocol = {
593     .name                = "async-test",
594     .url_open2           = async_test_open,
595     .url_read            = async_test_read,
596     .url_seek            = async_test_seek,
597     .url_close           = async_test_close,
598     .priv_data_size      = sizeof(TestContext),
599     .priv_data_class     = &async_test_context_class,
600 };
601 
602 int main(void)
603 {
604     URLContext   *h = NULL;
605     int           i;
606     int           ret;
607     int64_t       size;
608     int64_t       pos;
609     int64_t       read_len;
610     unsigned char buf[4096];
611     AVDictionary *opts = NULL;
612 
613     ffurl_register_protocol(&ff_async_protocol);
614     ffurl_register_protocol(&ff_async_test_protocol);
615 
616     /*
617      * test normal read
618      */
619     ret = ffurl_open_whitelist(&h, "async:async-test:", AVIO_FLAG_READ,
620                                NULL, NULL, NULL, NULL, NULL);
621     printf("open: %d\n", ret);
622 
623     size = ffurl_size(h);
624     printf("size: %"PRId64"\n", size);
625 
626     pos = ffurl_seek(h, 0, SEEK_CUR);
627     read_len = 0;
628     while (1) {
629         ret = ffurl_read(h, buf, sizeof(buf));
630         if (ret == AVERROR_EOF) {
631             printf("read-error: AVERROR_EOF at %"PRId64"\n", ffurl_seek(h, 0, SEEK_CUR));
632             break;
633         }
634         else if (ret == 0)
635             break;
636         else if (ret < 0) {
637             printf("read-error: %d at %"PRId64"\n", ret, ffurl_seek(h, 0, SEEK_CUR));
638             goto fail;
639         } else {
640             for (i = 0; i < ret; ++i) {
641                 if (buf[i] != (pos & 0xFF)) {
642                     printf("read-mismatch: actual %d, expecting %d, at %"PRId64"\n",
643                            (int)buf[i], (int)(pos & 0xFF), pos);
644                     break;
645                 }
646                 pos++;
647             }
648         }
649 
650         read_len += ret;
651     }
652     printf("read: %"PRId64"\n", read_len);
653 
654     /*
655      * test normal seek
656      */
657     ret = ffurl_read(h, buf, 1);
658     printf("read: %d\n", ret);
659 
660     pos = ffurl_seek(h, TEST_SEEK_POS, SEEK_SET);
661     printf("seek: %"PRId64"\n", pos);
662 
663     read_len = 0;
664     while (1) {
665         ret = ffurl_read(h, buf, sizeof(buf));
666         if (ret == AVERROR_EOF)
667             break;
668         else if (ret == 0)
669             break;
670         else if (ret < 0) {
671             printf("read-error: %d at %"PRId64"\n", ret, ffurl_seek(h, 0, SEEK_CUR));
672             goto fail;
673         } else {
674             for (i = 0; i < ret; ++i) {
675                 if (buf[i] != (pos & 0xFF)) {
676                     printf("read-mismatch: actual %d, expecting %d, at %"PRId64"\n",
677                            (int)buf[i], (int)(pos & 0xFF), pos);
678                     break;
679                 }
680                 pos++;
681             }
682         }
683 
684         read_len += ret;
685     }
686     printf("read: %"PRId64"\n", read_len);
687 
688     ret = ffurl_read(h, buf, 1);
689     printf("read: %d\n", ret);
690 
691     /*
692      * test read error
693      */
694     ffurl_close(h);
695     av_dict_set_int(&opts, "async-test-read-error", -10000, 0);
696     ret = ffurl_open_whitelist(&h, "async:async-test:", AVIO_FLAG_READ,
697                                NULL, &opts, NULL, NULL, NULL);
698     printf("open: %d\n", ret);
699 
700     ret = ffurl_read(h, buf, 1);
701     printf("read: %d\n", ret);
702 
703 fail:
704     av_dict_free(&opts);
705     ffurl_close(h);
706     return 0;
707 }
708 
709 #endif
710