Revision a6963f83 libavformat/aviobuf.c

View differences:

libavformat/aviobuf.c
19 19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 20
 */
21 21

  
22
/* needed for usleep() */
23
#define _XOPEN_SOURCE 600
24
#include <unistd.h>
22 25
#include "libavutil/crc.h"
23 26
#include "libavutil/intreadwrite.h"
24 27
#include "avformat.h"
......
88 91
    return s;
89 92
}
90 93

  
94
static inline int retry_transfer_wrapper(void *opaque, unsigned char *buf,
95
                                         int size,
96
                                         int (*transfer_func)(void *, unsigned char *, int)) {
97
    int fast_retries = 5;
98
    while (1) {
99
        int ret = transfer_func(opaque, buf, size);
100
        if (ret == AVERROR(EAGAIN)) {
101
            if (fast_retries)
102
                fast_retries--;
103
            else
104
                usleep(1000);
105
        } else
106
            return ret;
107
    }
108
}
109

  
91 110
static void flush_buffer(ByteIOContext *s)
92 111
{
93 112
    if (s->buf_ptr > s->buffer) {
94 113
        if (s->write_packet && !s->error){
95
            int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
114
            int ret= retry_transfer_wrapper(s->opaque, s->buffer,
115
                                            s->buf_ptr - s->buffer,
116
                                            s->write_packet);
96 117
            if(ret < 0){
97 118
                s->error = ret;
98 119
            }
......
361 382
    }
362 383

  
363 384
    if(s->read_packet)
364
        len = s->read_packet(s->opaque, dst, len);
385
        len = retry_transfer_wrapper(s->opaque, dst, len, s->read_packet);
365 386
    else
366 387
        len = 0;
367 388
    if (len <= 0) {
......
432 453
        if (len == 0) {
433 454
            if(size > s->buffer_size && !s->update_checksum){
434 455
                if(s->read_packet)
435
                    len = s->read_packet(s->opaque, buf, size);
456
                    len = retry_transfer_wrapper(s->opaque, buf, size,
457
                                                 s->read_packet);
436 458
                if (len <= 0) {
437 459
                    /* do not modify buffer if EOF reached so that a seek back can
438 460
                    be done without rereading data */

Also available in: Unified diff