[FFmpeg-devel] [PATCH] aviobuf: Retry if the read/write function returns AVERROR(EAGAIN)

Reimar Döffinger Reimar.Doeffinger
Mon Jan 3 15:34:18 CET 2011


On 3 jan 2011, at 13:27, Martin Storsjo <martin at martin.st> wrote:
> ---
> libavformat/aviobuf.c |   28 +++++++++++++++++++++++++---
> 1 files changed, 25 insertions(+), 3 deletions(-)
> 
> diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
> index df76507..5aff31c 100644
> --- a/libavformat/aviobuf.c
> +++ b/libavformat/aviobuf.c
> @@ -19,6 +19,9 @@
>  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>  */
> 
> +/* needed for usleep() */
> +#define _XOPEN_SOURCE 600
> +#include <unistd.h>
> #include "libavutil/crc.h"
> #include "libavutil/intreadwrite.h"
> #include "avformat.h"
> @@ -88,11 +91,29 @@ ByteIOContext *av_alloc_put_byte(
>     return s;
> }
> 
> +static inline int retry_transfer_wrapper(void *opaque, unsigned char *buf,
> +                                         int size,
> +                                         int (*transfer_func)(void *, unsigned char *, int)) {
> +    int fast_retries = 5;
> +    while (1) {
> +        int ret = transfer_func(opaque, buf, size);
> +        if (ret == AVERROR(EAGAIN)) {
> +            if (fast_retries)
> +                fast_retries--;
> +            else
> +                usleep(1000);
> +        } else
> +            return ret;
> +    }
> +}
> +
> static void flush_buffer(ByteIOContext *s)
> {
>     if (s->buf_ptr > s->buffer) {
>         if (s->write_packet && !s->error){
> -            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
> +            int ret= retry_transfer_wrapper(s->opaque, s->buffer,
> +                                            s->buf_ptr - s->buffer,
> +                                            s->write_packet);

This duplicates code, also the question is why the read_packet and write_packet functions should be allowed to return EAGAIN and if this is sufficiently documented.



More information about the ffmpeg-devel mailing list