]> git.sesse.net Git - ffmpeg/blob - libavformat/async.c
aacenc_tns: readjust values for new TNS decision making
[ffmpeg] / libavformat / async.c
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 backward short seek
28  *      support work with concatdec, hls
29  */
30
31 #include "libavutil/avassert.h"
32 #include "libavutil/avstring.h"
33 #include "libavutil/error.h"
34 #include "libavutil/fifo.h"
35 #include "libavutil/log.h"
36 #include "libavutil/opt.h"
37 #include "url.h"
38 #include <stdint.h>
39 #include <pthread.h>
40
41 #if HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #define BUFFER_CAPACITY         (4 * 1024 * 1024)
46 #define SHORT_SEEK_THRESHOLD    (256 * 1024)
47
48 typedef struct Context {
49     AVClass        *class;
50     URLContext     *inner;
51
52     int             seek_request;
53     int64_t         seek_pos;
54     int             seek_whence;
55     int             seek_completed;
56     int64_t         seek_ret;
57
58     int             io_error;
59     int             io_eof_reached;
60
61     int64_t         logical_pos;
62     int64_t         logical_size;
63     AVFifoBuffer   *fifo;
64
65     pthread_cond_t  cond_wakeup_main;
66     pthread_cond_t  cond_wakeup_background;
67     pthread_mutex_t mutex;
68     pthread_t       async_buffer_thread;
69
70     int             abort_request;
71     AVIOInterruptCB interrupt_callback;
72 } Context;
73
74 static int async_check_interrupt(void *arg)
75 {
76     URLContext *h   = arg;
77     Context    *c   = h->priv_data;
78
79     if (c->abort_request)
80         return 1;
81
82     if (ff_check_interrupt(&c->interrupt_callback))
83         c->abort_request = 1;
84
85     return c->abort_request;
86 }
87
88 static void *async_buffer_task(void *arg)
89 {
90     URLContext   *h    = arg;
91     Context      *c    = h->priv_data;
92     AVFifoBuffer *fifo = c->fifo;
93     int           ret  = 0;
94     int64_t       seek_ret;
95
96     while (1) {
97         int fifo_space, to_copy;
98
99         pthread_mutex_lock(&c->mutex);
100         if (async_check_interrupt(h)) {
101             c->io_eof_reached = 1;
102             c->io_error       = AVERROR_EXIT;
103             pthread_cond_signal(&c->cond_wakeup_main);
104             pthread_mutex_unlock(&c->mutex);
105             break;
106         }
107
108         if (c->seek_request) {
109             seek_ret = ffurl_seek(c->inner, c->seek_pos, c->seek_whence);
110             if (seek_ret < 0) {
111                 c->io_eof_reached = 1;
112                 c->io_error       = (int)seek_ret;
113             } else {
114                 c->io_eof_reached = 0;
115                 c->io_error       = 0;
116             }
117
118             c->seek_completed = 1;
119             c->seek_ret       = seek_ret;
120             c->seek_request   = 0;
121
122             av_fifo_reset(fifo);
123
124             pthread_cond_signal(&c->cond_wakeup_main);
125             pthread_mutex_unlock(&c->mutex);
126             continue;
127         }
128
129         fifo_space = av_fifo_space(fifo);
130         if (c->io_eof_reached || fifo_space <= 0) {
131             pthread_cond_signal(&c->cond_wakeup_main);
132             pthread_cond_wait(&c->cond_wakeup_background, &c->mutex);
133             pthread_mutex_unlock(&c->mutex);
134             continue;
135         }
136         pthread_mutex_unlock(&c->mutex);
137
138         to_copy = FFMIN(4096, fifo_space);
139         ret = av_fifo_generic_write(fifo, c->inner, to_copy, (void *)ffurl_read);
140
141         pthread_mutex_lock(&c->mutex);
142         if (ret <= 0) {
143             c->io_eof_reached = 1;
144             if (ret < 0) {
145                 c->io_error = ret;
146             }
147         }
148
149         pthread_cond_signal(&c->cond_wakeup_main);
150         pthread_mutex_unlock(&c->mutex);
151     }
152
153     return NULL;
154 }
155
156 static int async_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
157 {
158     Context         *c = h->priv_data;
159     int              ret;
160     AVIOInterruptCB  interrupt_callback = {.callback = async_check_interrupt, .opaque = h};
161
162     av_strstart(arg, "async:", &arg);
163
164     c->fifo = av_fifo_alloc(BUFFER_CAPACITY);
165     if (!c->fifo) {
166         ret = AVERROR(ENOMEM);
167         goto fifo_fail;
168     }
169
170     /* wrap interrupt callback */
171     c->interrupt_callback = h->interrupt_callback;
172     ret = ffurl_open(&c->inner, arg, flags, &interrupt_callback, options);
173     if (ret != 0) {
174         av_log(h, AV_LOG_ERROR, "ffurl_open failed : %s, %s\n", av_err2str(ret), arg);
175         goto url_fail;
176     }
177
178     c->logical_size = ffurl_size(c->inner);
179     h->is_streamed  = c->inner->is_streamed;
180
181     ret = pthread_mutex_init(&c->mutex, NULL);
182     if (ret != 0) {
183         av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", av_err2str(ret));
184         goto mutex_fail;
185     }
186
187     ret = pthread_cond_init(&c->cond_wakeup_main, NULL);
188     if (ret != 0) {
189         av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", av_err2str(ret));
190         goto cond_wakeup_main_fail;
191     }
192
193     ret = pthread_cond_init(&c->cond_wakeup_background, NULL);
194     if (ret != 0) {
195         av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", av_err2str(ret));
196         goto cond_wakeup_background_fail;
197     }
198
199     ret = pthread_create(&c->async_buffer_thread, NULL, async_buffer_task, h);
200     if (ret) {
201         av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", av_err2str(ret));
202         goto thread_fail;
203     }
204
205     return 0;
206
207 thread_fail:
208     pthread_cond_destroy(&c->cond_wakeup_background);
209 cond_wakeup_background_fail:
210     pthread_cond_destroy(&c->cond_wakeup_main);
211 cond_wakeup_main_fail:
212     pthread_mutex_destroy(&c->mutex);
213 mutex_fail:
214     ffurl_close(c->inner);
215 url_fail:
216     av_fifo_freep(&c->fifo);
217 fifo_fail:
218     return ret;
219 }
220
221 static int async_close(URLContext *h)
222 {
223     Context *c = h->priv_data;
224     int      ret;
225
226     pthread_mutex_lock(&c->mutex);
227     c->abort_request = 1;
228     pthread_cond_signal(&c->cond_wakeup_background);
229     pthread_mutex_unlock(&c->mutex);
230
231     ret = pthread_join(c->async_buffer_thread, NULL);
232     if (ret != 0)
233         av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", av_err2str(ret));
234
235     pthread_cond_destroy(&c->cond_wakeup_background);
236     pthread_cond_destroy(&c->cond_wakeup_main);
237     pthread_mutex_destroy(&c->mutex);
238     ffurl_close(c->inner);
239     av_fifo_freep(&c->fifo);
240
241     return 0;
242 }
243
244 static int async_read_internal(URLContext *h, void *dest, int size, int read_complete,
245                                void (*func)(void*, void*, int))
246 {
247     Context      *c       = h->priv_data;
248     AVFifoBuffer *fifo    = c->fifo;
249     int           to_read = size;
250     int           ret     = 0;
251
252     pthread_mutex_lock(&c->mutex);
253
254     while (to_read > 0) {
255         int fifo_size, to_copy;
256         if (async_check_interrupt(h)) {
257             ret = AVERROR_EXIT;
258             break;
259         }
260         fifo_size = av_fifo_size(fifo);
261         to_copy   = FFMIN(to_read, fifo_size);
262         if (to_copy > 0) {
263             av_fifo_generic_read(fifo, dest, to_copy, func);
264             if (!func)
265                 dest = (uint8_t *)dest + to_copy;
266             c->logical_pos += to_copy;
267             to_read        -= to_copy;
268             ret             = size - to_read;
269
270             if (to_read <= 0 || !read_complete)
271                 break;
272         } else if (c->io_eof_reached) {
273             if (ret <= 0)
274                 ret = AVERROR_EOF;
275             break;
276         }
277         pthread_cond_signal(&c->cond_wakeup_background);
278         pthread_cond_wait(&c->cond_wakeup_main, &c->mutex);
279     }
280
281     pthread_cond_signal(&c->cond_wakeup_background);
282     pthread_mutex_unlock(&c->mutex);
283
284     return ret;
285 }
286
287 static int async_read(URLContext *h, unsigned char *buf, int size)
288 {
289     return async_read_internal(h, buf, size, 0, NULL);
290 }
291
292 static void fifo_do_not_copy_func(void* dest, void* src, int size) {
293     // do not copy
294 }
295
296 static int64_t async_seek(URLContext *h, int64_t pos, int whence)
297 {
298     Context      *c    = h->priv_data;
299     AVFifoBuffer *fifo = c->fifo;
300     int64_t       ret;
301     int64_t       new_logical_pos;
302     int fifo_size;
303
304     if (whence == AVSEEK_SIZE) {
305         av_log(h, AV_LOG_TRACE, "async_seek: AVSEEK_SIZE: %"PRId64"\n", (int64_t)c->logical_size);
306         return c->logical_size;
307     } else if (whence == SEEK_CUR) {
308         av_log(h, AV_LOG_TRACE, "async_seek: %"PRId64"\n", pos);
309         new_logical_pos = pos + c->logical_pos;
310     } else if (whence == SEEK_SET){
311         av_log(h, AV_LOG_TRACE, "async_seek: %"PRId64"\n", pos);
312         new_logical_pos = pos;
313     } else {
314         return AVERROR(EINVAL);
315     }
316     if (new_logical_pos < 0)
317         return AVERROR(EINVAL);
318
319     fifo_size = av_fifo_size(fifo);
320     if (new_logical_pos == c->logical_pos) {
321         /* current position */
322         return c->logical_pos;
323     } else if ((new_logical_pos > c->logical_pos) &&
324                (new_logical_pos < (c->logical_pos + fifo_size + SHORT_SEEK_THRESHOLD))) {
325         /* fast seek */
326         av_log(h, AV_LOG_TRACE, "async_seek: fask_seek %"PRId64" from %d dist:%d/%d\n",
327                 new_logical_pos, (int)c->logical_pos,
328                 (int)(new_logical_pos - c->logical_pos), fifo_size);
329         async_read_internal(h, NULL, (int)(new_logical_pos - c->logical_pos), 1, fifo_do_not_copy_func);
330         return c->logical_pos;
331     } else if (c->logical_size <= 0) {
332         /* can not seek */
333         return AVERROR(EINVAL);
334     } else if (new_logical_pos > c->logical_size) {
335         /* beyond end */
336         return AVERROR(EINVAL);
337     }
338
339     pthread_mutex_lock(&c->mutex);
340
341     c->seek_request   = 1;
342     c->seek_pos       = new_logical_pos;
343     c->seek_whence    = SEEK_SET;
344     c->seek_completed = 0;
345     c->seek_ret       = 0;
346
347     while (1) {
348         if (async_check_interrupt(h)) {
349             ret = AVERROR_EXIT;
350             break;
351         }
352         if (c->seek_completed) {
353             if (c->seek_ret >= 0)
354                 c->logical_pos  = c->seek_ret;
355             ret = c->seek_ret;
356             break;
357         }
358         pthread_cond_signal(&c->cond_wakeup_background);
359         pthread_cond_wait(&c->cond_wakeup_main, &c->mutex);
360     }
361
362     pthread_mutex_unlock(&c->mutex);
363
364     return ret;
365 }
366
367 #define OFFSET(x) offsetof(Context, x)
368 #define D AV_OPT_FLAG_DECODING_PARAM
369
370 static const AVOption options[] = {
371     {NULL},
372 };
373
374 static const AVClass async_context_class = {
375     .class_name = "Async",
376     .item_name  = av_default_item_name,
377     .option     = options,
378     .version    = LIBAVUTIL_VERSION_INT,
379 };
380
381 URLProtocol ff_async_protocol = {
382     .name                = "async",
383     .url_open2           = async_open,
384     .url_read            = async_read,
385     .url_seek            = async_seek,
386     .url_close           = async_close,
387     .priv_data_size      = sizeof(Context),
388     .priv_data_class     = &async_context_class,
389 };
390
391 #ifdef TEST
392
393 #define TEST_SEEK_POS    (1536)
394 #define TEST_STREAM_SIZE (2048)
395
396 typedef struct TestContext {
397     AVClass        *class;
398     int64_t         logical_pos;
399     int64_t         logical_size;
400 } TestContext;
401
402 static int async_test_open(URLContext *h, const char *arg, int flags, AVDictionary **options)
403 {
404     TestContext *c = h->priv_data;
405     c->logical_pos  = 0;
406     c->logical_size = TEST_STREAM_SIZE;
407     return 0;
408 }
409
410 static int async_test_close(URLContext *h)
411 {
412     return 0;
413 }
414
415 static int async_test_read(URLContext *h, unsigned char *buf, int size)
416 {
417     TestContext *c = h->priv_data;
418     int          i;
419     int          read_len = 0;
420
421     if (c->logical_pos >= c->logical_size)
422         return AVERROR_EOF;
423
424     for (i = 0; i < size; ++i) {
425         buf[i] = c->logical_pos & 0xFF;
426
427         c->logical_pos++;
428         read_len++;
429
430         if (c->logical_pos >= c->logical_size)
431             break;
432     }
433
434     return read_len;
435 }
436
437 static int64_t async_test_seek(URLContext *h, int64_t pos, int whence)
438 {
439     TestContext *c = h->priv_data;
440     int64_t      new_logical_pos;
441
442     if (whence == AVSEEK_SIZE) {
443         return c->logical_size;
444     } else if (whence == SEEK_CUR) {
445         new_logical_pos = pos + c->logical_pos;
446     } else if (whence == SEEK_SET){
447         new_logical_pos = pos;
448     } else {
449         return AVERROR(EINVAL);
450     }
451     if (new_logical_pos < 0)
452         return AVERROR(EINVAL);
453
454     c->logical_pos = new_logical_pos;
455     return new_logical_pos;
456 }
457
458 static const AVClass async_test_context_class = {
459     .class_name = "Async-Test",
460     .item_name  = av_default_item_name,
461     .version    = LIBAVUTIL_VERSION_INT,
462 };
463
464 URLProtocol ff_async_test_protocol = {
465     .name                = "async-test",
466     .url_open2           = async_test_open,
467     .url_read            = async_test_read,
468     .url_seek            = async_test_seek,
469     .url_close           = async_test_close,
470     .priv_data_size      = sizeof(TestContext),
471     .priv_data_class     = &async_test_context_class,
472 };
473
474 int main(void)
475 {
476     URLContext   *h = NULL;
477     int           i;
478     int           ret;
479     int64_t       size;
480     int64_t       pos;
481     int64_t       read_len;
482     unsigned char buf[4096];
483
484     ffurl_register_protocol(&ff_async_protocol);
485     ffurl_register_protocol(&ff_async_test_protocol);
486
487     ret = ffurl_open(&h, "async:async-test:", AVIO_FLAG_READ, NULL, NULL);
488     printf("open: %d\n", ret);
489
490     size = ffurl_size(h);
491     printf("size: %"PRId64"\n", size);
492
493     pos = ffurl_seek(h, 0, SEEK_CUR);
494     read_len = 0;
495     while (1) {
496         ret = ffurl_read(h, buf, sizeof(buf));
497         if (ret == AVERROR_EOF) {
498             printf("read-error: AVERROR_EOF at %"PRId64"\n", ffurl_seek(h, 0, SEEK_CUR));
499             break;
500         }
501         else if (ret == 0)
502             break;
503         else if (ret < 0) {
504             printf("read-error: %d at %"PRId64"\n", ret, ffurl_seek(h, 0, SEEK_CUR));
505             goto fail;
506         } else {
507             for (i = 0; i < ret; ++i) {
508                 if (buf[i] != (pos & 0xFF)) {
509                     printf("read-mismatch: actual %d, expecting %d, at %"PRId64"\n",
510                            (int)buf[i], (int)(pos & 0xFF), pos);
511                     break;
512                 }
513                 pos++;
514             }
515         }
516
517         read_len += ret;
518     }
519     printf("read: %"PRId64"\n", read_len);
520
521     ret = ffurl_read(h, buf, 1);
522     printf("read: %d\n", ret);
523
524     pos = ffurl_seek(h, TEST_SEEK_POS, SEEK_SET);
525     printf("seek: %"PRId64"\n", pos);
526
527     read_len = 0;
528     while (1) {
529         ret = ffurl_read(h, buf, sizeof(buf));
530         if (ret == AVERROR_EOF)
531             break;
532         else if (ret == 0)
533             break;
534         else if (ret < 0) {
535             printf("read-error: %d at %"PRId64"\n", ret, ffurl_seek(h, 0, SEEK_CUR));
536             goto fail;
537         } else {
538             for (i = 0; i < ret; ++i) {
539                 if (buf[i] != (pos & 0xFF)) {
540                     printf("read-mismatch: actual %d, expecting %d, at %"PRId64"\n",
541                            (int)buf[i], (int)(pos & 0xFF), pos);
542                     break;
543                 }
544                 pos++;
545             }
546         }
547
548         read_len += ret;
549     }
550     printf("read: %"PRId64"\n", read_len);
551
552     ret = ffurl_read(h, buf, 1);
553     printf("read: %d\n", ret);
554
555 fail:
556     ffurl_close(h);
557     return 0;
558 }
559
560 #endif