diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2011-05-15 21:18:02 +0400 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2011-05-15 21:18:02 +0400 |
commit | d46aada5c2c71b9b0a259e62699cab25837053b2 (patch) | |
tree | ab474244a6fda04d8a10d25201620cdaee11c3c6 /libavformat/udp.c | |
parent | 66b1f210c024a08ba00e4a730e64940d248b8717 (diff) | |
parent | 5a153604c930792aa7f00c55cbf3c470f582dfb7 (diff) |
Merge branch 'master' into oldabi
* master: (403 commits)
Initial caf muxer.
Support decoding of amr_nb and gsm in caf.
Fix decoding of msrle samples with 1bpp.
udp: remove resource.h inclusion, it breaks mingw compilation.
ffmpeg: Allow seting and cycling through debug modes.
Fix FSF address copy paste error in some license headers.
Add an aac sample which uses LTP to fate-aac.
ffmpeg: Help for interactive keys.
UDP: dont use thread_t as truth value.
swscale: fix compile on mingw32
[PATCH] Update pixdesc_be fate refs after adding 9/10bit YUV420P formats.
arm: properly mark external symbol call
ffmpeg: Interactivity support. Try pressing +-hs.
swscale: 10l forgot git add this change from ronald.
AVFrame: only set parameters from AVCodecContext in decode_video*() when no frame reordering is used.
avcodec_default_get_buffer: init picture parameters.
swscale: properly inline bits/endianness in yuv2yuvX16inC().
swscale: fix clipping of 9/10bit YUV420P.
Add av_clip_uintp2() function
Support more QT 1bpp rawvideo files.
...
Conflicts:
libavcodec/flacenc.c
libavcodec/h261dec.c
libavcodec/h263dec.c
libavcodec/mpeg12.c
libavcodec/msrle.c
libavcodec/options.c
libavcodec/qpeg.c
libavcodec/rv34.c
libavcodec/svq1dec.c
libavcodec/svq3.c
libavcodec/vc1dec.c
libavcodec/version.h
libavfilter/avfilter.h
libavformat/file.c
libavformat/options.c
libavformat/rtpproto.c
libavformat/udp.c
libavutil/avutil.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavformat/udp.c')
-rw-r--r-- | libavformat/udp.c | 119 |
1 files changed, 117 insertions, 2 deletions
diff --git a/libavformat/udp.c b/libavformat/udp.c index 6f22277e23..021f529d64 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -25,15 +25,17 @@ */ #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */ -#define _DARWIN_C_SOURCE /* Needed for using IP_MULTICAST_TTL on OS X */ + #include "avformat.h" #include "avio_internal.h" #include "libavutil/parseutils.h" +#include "libavutil/fifo.h" #include <unistd.h> #include "internal.h" #include "network.h" #include "os_support.h" #include "url.h" +#include <pthread.h> #include <sys/time.h> #ifndef IPV6_ADD_MEMBERSHIP @@ -51,6 +53,13 @@ typedef struct { struct sockaddr_storage dest_addr; int dest_addr_len; int is_connected; + + /* Circular Buffer variables for use in UDP receive code */ + int circular_buffer_size; + AVFifoBuffer *fifo; + int circular_buffer_available_max; + int circular_buffer_error; + pthread_t circular_buffer_thread; } UDPContext; #define UDP_TX_BUF_SIZE 32768 @@ -301,6 +310,66 @@ int udp_get_file_handle(URLContext *h) return s->udp_fd; } +static void *circular_buffer_task( void *_URLContext) +{ + URLContext *h = _URLContext; + UDPContext *s = h->priv_data; + fd_set rfds; + struct timeval tv; + + for(;;) { + int left; + int ret; + int len; + + if (url_interrupt_cb()) { + s->circular_buffer_error = EINTR; + return NULL; + } + + FD_ZERO(&rfds); + FD_SET(s->udp_fd, &rfds); + tv.tv_sec = 1; + tv.tv_usec = 0; + ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv); + if (ret < 0) { + if (ff_neterrno() == AVERROR(EINTR)) + continue; + s->circular_buffer_error = EIO; + return NULL; + } + + if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds))) + continue; + + /* How much do we have left to the end of the buffer */ + /* Whats the minimum we can read so that we dont comletely fill the buffer */ + left = av_fifo_space(s->fifo); + left = FFMIN(left, s->fifo->end - s->fifo->wptr); + + /* No Space left, error, what do we do now */ + if( !left) { + av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n"); + s->circular_buffer_error = EIO; + return NULL; + } + + len = recv(s->udp_fd, s->fifo->wptr, left, 0); + if (len < 0) { + if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) { + s->circular_buffer_error = EIO; + return NULL; + } + } + s->fifo->wptr += len; + if (s->fifo->wptr >= s->fifo->end) + s->fifo->wptr = s->fifo->buffer; + s->fifo->wndx += len; + } + + return NULL; +} + /* put it in UDP context */ /* return non zero if error */ static int udp_open(URLContext *h, const char *uri, int flags) @@ -328,10 +397,12 @@ static int udp_open(URLContext *h, const char *uri, int flags) s->ttl = 16; s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE; + s->circular_buffer_size = 7*188*4096; + p = strchr(uri, '?'); if (p) { if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) { - const char *endptr=NULL; + char *endptr=NULL; s->reuse_socket = strtol(buf, &endptr, 10); /* assume if no digits were found it is a request to enable it */ if (buf == endptr) @@ -353,6 +424,9 @@ static int udp_open(URLContext *h, const char *uri, int flags) if (av_find_info_tag(buf, sizeof(buf), "connect", p)) { s->is_connected = strtol(buf, NULL, 10); } + if (av_find_info_tag(buf, sizeof(buf), "buf_size", p)) { + s->circular_buffer_size = strtol(buf, NULL, 10)*188; + } } /* fill the dest addr */ @@ -434,10 +508,21 @@ static int udp_open(URLContext *h, const char *uri, int flags) } s->udp_fd = udp_fd; + + if (!is_output && s->circular_buffer_size) { + /* start the task going */ + s->fifo = av_fifo_alloc(s->circular_buffer_size); + if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) { + av_log(h, AV_LOG_ERROR, "pthread_create failed\n"); + goto fail; + } + } + return 0; fail: if (udp_fd >= 0) closesocket(udp_fd); + av_fifo_free(s->fifo); av_free(s); return AVERROR(EIO); } @@ -446,6 +531,33 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) { UDPContext *s = h->priv_data; int ret; + int avail; + int left; + fd_set rfds; + struct timeval tv; + + if (s->fifo) { + + do { + avail = av_fifo_size(s->fifo); + if (avail) { // >=size) { + + // Maximum amount available + size = FFMIN( avail, size); + av_fifo_generic_read(s->fifo, buf, size, NULL); + return size; + } + else { + FD_ZERO(&rfds); + FD_SET(s->udp_fd, &rfds); + tv.tv_sec = 1; + tv.tv_usec = 0; + ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv); + if (ret<0) + return ret; + } + } while( 1); + } if (!(h->flags & AVIO_FLAG_NONBLOCK)) { ret = ff_network_wait_fd(s->udp_fd, 0); @@ -453,6 +565,7 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) return ret; } ret = recv(s->udp_fd, buf, size, 0); + return ret < 0 ? ff_neterrno() : ret; } @@ -484,6 +597,8 @@ static int udp_close(URLContext *h) if (s->is_multicast && !(h->flags & AVIO_WRONLY)) udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr); closesocket(s->udp_fd); + av_log( h, AV_LOG_INFO, "circular_buffer_info max:%d%%\r\n", (s->circular_buffer_available_max*100)/s->circular_buffer_size); + av_fifo_free(s->fifo); av_free(s); return 0; } |