]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/tcp.c
flush audio encoder buffers at the end
[ffmpeg] / libavformat / tcp.c
index d4b81c88e99310d3ac97434d742b26d46724b5f0..6475c1277b107d9ae4290d2d91ccf6db700d6539 100644 (file)
  */
 #include "avformat.h"
 #include <unistd.h>
-#include <ctype.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#if defined(__APPLE__) || defined(__BEOS__)
+typedef int socklen_t;
+#endif
 #ifndef __BEOS__
 # include <arpa/inet.h>
 #else
@@ -128,7 +130,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
     return 0;
 
  fail:
-    ret = -EIO;
+    ret = AVERROR_IO;
  fail1:
     if (fd >= 0)
         close(fd);
@@ -139,12 +141,11 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
 static int tcp_read(URLContext *h, uint8_t *buf, int size)
 {
     TCPContext *s = h->priv_data;
-    int size1, len, fd_max;
+    int len, fd_max, ret;
     fd_set rfds;
     struct timeval tv;
 
-    size1 = size;
-    while (size > 0) {
+    for (;;) {
         if (url_interrupt_cb())
             return -EINTR;
         fd_max = s->fd;
@@ -152,34 +153,31 @@ static int tcp_read(URLContext *h, uint8_t *buf, int size)
         FD_SET(s->fd, &rfds);
         tv.tv_sec = 0;
         tv.tv_usec = 100 * 1000;
-        select(fd_max + 1, &rfds, NULL, NULL, &tv);
+        ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
+        if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
 #ifdef __BEOS__
-        len = recv(s->fd, buf, size, 0);
+            len = recv(s->fd, buf, size, 0);
 #else
-        len = read(s->fd, buf, size);
+            len = read(s->fd, buf, size);
 #endif
-        if (len < 0) {
-            if (errno != EINTR && errno != EAGAIN)
+            if (len < 0) {
+                if (errno != EINTR && errno != EAGAIN)
 #ifdef __BEOS__
-                return errno;
+                    return errno;
 #else
-                return -errno;
+                    return -errno;
 #endif
-            else
-                continue;
-        } else if (len == 0) {
-            break;
+            } else return len;
+        } else if (ret < 0) {
+            return -1;
         }
-        size -= len;
-        buf += len;
     }
-    return size1 - size;
 }
 
 static int tcp_write(URLContext *h, uint8_t *buf, int size)
 {
     TCPContext *s = h->priv_data;
-    int ret, size1, fd_max;
+    int ret, size1, fd_max, len;
     fd_set wfds;
     struct timeval tv;
 
@@ -192,20 +190,28 @@ static int tcp_write(URLContext *h, uint8_t *buf, int size)
         FD_SET(s->fd, &wfds);
         tv.tv_sec = 0;
         tv.tv_usec = 100 * 1000;
-        select(fd_max + 1, NULL, &wfds, NULL, &tv);
+        ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
+        if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
 #ifdef __BEOS__
-        ret = send(s->fd, buf, size, 0);
+            len = send(s->fd, buf, size, 0);
 #else
-        ret = write(s->fd, buf, size);
+            len = write(s->fd, buf, size);
 #endif
-        if (ret < 0 && errno != EINTR && errno != EAGAIN)
+            if (len < 0) {
+                if (errno != EINTR && errno != EAGAIN) {
 #ifdef __BEOS__
-            return errno;
+                    return errno;
 #else
-            return -errno;
+                    return -errno;
 #endif
-        size -= ret;
-        buf += ret;
+                }
+                continue;
+            }
+            size -= len;
+            buf += len;
+        } else if (ret < 0) {
+            return -1;
+        }
     }
     return size1 - size;
 }