[Ffmpeg-devel] FFMPEG RTSP for Windows

Michael A. Kohn mike
Wed Mar 29 23:52:20 CEST 2006


>> I don't know anything about Winsock, but go ahead and send patches for
>> any improvements you make. Send unified diffs: 'diff -Naur' or 'cvs
diff
>> -u'. If they're good changes then the patches ought to be considered.

One important thing is kinda missing from this.  The Winsock library has
to be intialized before it can be used.  At the top of my test program I
have something like this:

WSADATA wsaData;
WORD wVersionRequested=MAKEWORD(1,1);
int Win32isStupid;

  Win32isStupid=WSAStartup(wVersionRequested, &wsaData);
  if (Win32isStupid) return -1;

Really ugly, but this has to be done once before any socket code is
called.  I'm not sure where to put that in libavformat, so I left it out.
Without this ffmpeg won't do rstp.  If you could point me in the right
direction where to put it, I can make a new patch.. or if someone else
wants to.. just copy that code above and make sure it's called once before
making socket calls.

here's the patch:

diff -Naur ffmpeg/configure ffmpeg_diff/configure
--- ffmpeg/configure    2006-03-28 17:36:07.000000000 -0600
+++ ffmpeg_diff/configure       2006-03-29 15:33:31.000000000 -0600
@@ -922,7 +922,8 @@
     dv1394="no"
     dc1394="no"
     ffserver="no"
-    network="no"
+    #network="no"
+    extralibs="$extralibs -lws2_32"
 if test "$mingwce" = "yes"; then
     protocols="no"
 fi
diff -Naur ffmpeg/libavformat/http.c ffmpeg_diff/libavformat/http.c
--- ffmpeg/libavformat/http.c   2006-01-12 16:43:23.000000000 -0600
+++ ffmpeg_diff/libavformat/http.c      2006-03-29 15:34:26.000000000
-0600
@@ -19,14 +19,25 @@
 #include "avformat.h"
 #include <unistd.h>
 #include <sys/types.h>
+
+#ifndef CONFIG_WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
+#endif
+
 #ifndef __BEOS__
+#ifndef CONFIG_WIN32
 # include <arpa/inet.h>
+#endif
 #else
 # include "barpainet.h"
 #endif
+
+#ifndef CONFIG_WIN32
 #include <netdb.h>
+#else
+#include <winsock2.h>
+#endif


 /* XXX: POST protocol is not completly implemented because ffmpeg use
@@ -208,7 +219,11 @@
     s->line_count = 0;
     s->location[0] = '\0';
     if (post) {
+#ifndef CONFIG_WIN32
         sleep(1);
+#else
+        Sleep(1000);
+#endif
         return 0;
     }

diff -Naur ffmpeg/libavformat/rtp.c ffmpeg_diff/libavformat/rtp.c
--- ffmpeg/libavformat/rtp.c    2006-03-01 16:45:27.000000000 -0600
+++ ffmpeg_diff/libavformat/rtp.c       2006-03-29 15:34:37.000000000
-0600
@@ -22,14 +22,29 @@

 #include <unistd.h>
 #include <sys/types.h>
+
+#ifndef CONFIG_WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
+#endif
+
 #ifndef __BEOS__
+#ifndef CONFIG_WIN32
 # include <arpa/inet.h>
+#endif
 #else
 # include "barpainet.h"
 #endif
+
+#ifndef CONFIG_WIN32
 #include <netdb.h>
+#else
+#include <winsock2.h>
+/* I'm sure the next two lines are evil in some way, but I'm not sure
+   how else to do that right now */
+#undef rand
+#define random rand
+#endif

 //#define DEBUG

diff -Naur ffmpeg/libavformat/rtpproto.c
ffmpeg_diff/libavformat/rtpproto.c
--- ffmpeg/libavformat/rtpproto.c       2006-01-12 16:43:25.000000000
-0600
+++ ffmpeg_diff/libavformat/rtpproto.c  2006-03-29 15:34:42.000000000
-0600
@@ -21,14 +21,26 @@
 #include <unistd.h>
 #include <stdarg.h>
 #include <sys/types.h>
+
+#ifndef CONFIG_WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
+#endif
+
 #ifndef __BEOS__
+#ifndef CONFIG_WIN32
 # include <arpa/inet.h>
+#endif
 #else
 # include "barpainet.h"
 #endif
+
+#ifndef CONFIG_WIN32
 #include <netdb.h>
+#else
+#include <winsock2.h>
+#endif
+
 #include <fcntl.h>

 #define RTP_TX_BUF_SIZE  (64 * 1024)
diff -Naur ffmpeg/libavformat/rtsp.c ffmpeg_diff/libavformat/rtsp.c
--- ffmpeg/libavformat/rtsp.c   2006-03-10 18:22:21.000000000 -0600
+++ ffmpeg_diff/libavformat/rtsp.c      2006-03-29 15:34:32.000000000
-0600
@@ -20,14 +20,24 @@

 #include <unistd.h> /* for select() prototype */
 #include <sys/time.h>
+
+#ifndef CONFIG_WIN32
 #include <netinet/in.h>
 #include <sys/socket.h>
+#endif
+
 #ifndef __BEOS__
+#ifndef CONFIG_WIN32
 # include <arpa/inet.h>
+#endif
 #else
 # include "barpainet.h"
 #endif

+#ifdef CONFIG_WIN32
+#include <winsock2.h>
+#endif
+
 //#define DEBUG
 //#define DEBUG_RTP_TCP

@@ -342,8 +352,14 @@
         if (strcmp(buf1, "IP4") != 0)
             return;
         get_word_sep(buf1, sizeof(buf1), "/", &p);
+#ifdef CONFIG_WIN32
+        sdp_ip.s_addr=inet_addr(buf1);
+        if (sdp_ip.s_addr == INADDR_NONE)
+            return;
+#else
         if (inet_aton(buf1, &sdp_ip) == 0)
             return;
+#endif
         ttl = 16;
         if (*p == '/') {
             p++;
@@ -590,8 +606,15 @@
                 if (*p == '=') {
                     p++;
                     get_word_sep(buf, sizeof(buf), ";,", &p);
+#ifdef CONFIG_WIN32
+                    ipaddr.s_addr=inet_addr(buf);
+                    if (ipaddr.s_addr == INADDR_NONE)
+                        th->destination = ntohl(ipaddr.s_addr);
+#else
+
                     if (inet_aton(buf, &ipaddr))
                         th->destination = ntohl(ipaddr.s_addr);
+#endif
                 }
             }
             while (*p != ';' && *p != '\0' && *p != ',')
diff -Naur ffmpeg/libavformat/tcp.c ffmpeg_diff/libavformat/tcp.c
--- ffmpeg/libavformat/tcp.c    2006-02-02 07:07:30.000000000 -0600
+++ ffmpeg_diff/libavformat/tcp.c       2006-03-29 15:34:22.000000000
-0600
@@ -19,17 +19,30 @@
 #include "avformat.h"
 #include <unistd.h>
 #include <sys/types.h>
+
+#ifndef CONFIG_WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
+#endif
+
 #if defined(__BEOS__) || defined(__INNOTEK_LIBC__)
 typedef int socklen_t;
 #endif
 #ifndef __BEOS__
+#ifndef CONFIG_WIN32
 # include <arpa/inet.h>
+#endif
 #else
 # include "barpainet.h"
 #endif
+
+#ifndef CONFIG_WIN32
 #include <netdb.h>
+#else
+#include <winsock2.h>
+#define socklen_t long
+#endif
+
 #include <sys/time.h>
 #include <fcntl.h>

@@ -42,7 +55,12 @@
 {
     struct hostent *hp;

+#ifdef CONFIG_WIN32
+    sin_addr->s_addr=inet_addr(hostname);
+    if (sin_addr->s_addr == INADDR_NONE) {
+#else
     if ((inet_aton(hostname, sin_addr)) == 0) {
+#endif
         hp = gethostbyname(hostname);
         if (!hp)
             return -1;
@@ -85,7 +103,14 @@
     fd = socket(PF_INET, SOCK_STREAM, 0);
     if (fd < 0)
         goto fail;
+#ifndef CONFIG_WIN32
     fcntl(fd, F_SETFL, O_NONBLOCK);
+#else
+{
+    u_long arg=1L;
+    ioctlsocket(fd, FIONBIO, (u_long FAR *)&arg);
+}
+#endif

  redo:
     ret = connect(fd, (struct sockaddr *)&dest_addr,
@@ -93,8 +118,10 @@
     if (ret < 0) {
         if (errno == EINTR)
             goto redo;
+#ifndef CONFIG_WIN32
         if (errno != EINPROGRESS)
             goto fail;
+#endif

         /* wait until we are connected or until abort */
         for(;;) {
@@ -114,7 +141,11 @@

         /* test error */
         optlen = sizeof(ret);
+#ifdef CONFIG_WIN32
+        getsockopt (fd, SOL_SOCKET, SO_ERROR, (char FAR *)&ret, (int FAR
*)&optlen);
+#else
         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
+#endif
         if (ret != 0)
             goto fail;
     }
@@ -147,7 +178,7 @@
         tv.tv_usec = 100 * 1000;
         ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
         if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
-#ifdef __BEOS__
+#if defined(__BEOS__) || defined(CONFIG_WIN32)
             len = recv(s->fd, buf, size, 0);
 #else
             len = read(s->fd, buf, size);
@@ -184,7 +215,7 @@
         tv.tv_usec = 100 * 1000;
         ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
         if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
-#ifdef __BEOS__
+#if defined(__BEOS__) || defined(CONFIG_WIN32)
             len = send(s->fd, buf, size, 0);
 #else
             len = write(s->fd, buf, size);
diff -Naur ffmpeg/libavformat/udp.c ffmpeg_diff/libavformat/udp.c
--- ffmpeg/libavformat/udp.c    2006-01-12 16:43:25.000000000 -0600
+++ ffmpeg_diff/libavformat/udp.c       2006-03-29 15:34:13.000000000
-0600
@@ -19,14 +19,26 @@
 #include "avformat.h"
 #include <unistd.h>
 #include <sys/types.h>
+
+#ifndef CONFIG_WIN32
 #include <sys/socket.h>
 #include <netinet/in.h>
+#endif
+
 #ifndef __BEOS__
+#ifndef CONFIG_WIN32
 # include <arpa/inet.h>
+#endif
 #else
 # include "barpainet.h"
 #endif
+
+#ifndef CONFIG_WIN32
 #include <netdb.h>
+#else
+#include <winsock.h>
+#endif
+

 #ifndef IPV6_ADD_MEMBERSHIP
 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
@@ -211,7 +223,7 @@

  fail:
     if (udp_fd >= 0)
-#ifdef CONFIG_BEOS_NETSERVER
+#if defined(CONFIG_BEOS_NETSERVER) || defined(CONFIG_WIN32)
         closesocket(udp_fd);
 #else
         close(udp_fd);
@@ -357,6 +369,11 @@
     getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len);
     s->local_port = ntohs(my_addr1.sin_port);

+#ifdef CONFIG_WIN32
+    tmp=65536;   /* 64k UDP buffer size.  Should this be bigger? */
+    setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
+#endif
+
 #ifndef CONFIG_BEOS_NETSERVER
     if (s->is_multicast) {
         if (h->flags & URL_WRONLY) {
@@ -411,7 +428,7 @@
     return 0;
  fail:
     if (udp_fd >= 0)
-#ifdef CONFIG_BEOS_NETSERVER
+#if defined(CONFIG_BEOS_NETSERVER) || defined(CONFIG_WIN32)
         closesocket(udp_fd);
 #else
         close(udp_fd);
@@ -483,10 +500,14 @@
     if (s->is_multicast && !(h->flags & URL_WRONLY))
         udp_ipv6_leave_multicast_group(s->udp_fd, (struct sockaddr
*)&s->dest_addr);
 #endif
+#ifndef CONFIG_WIN32
     close(s->udp_fd);
 #else
     closesocket(s->udp_fd);
 #endif
+#else
+    closesocket(s->udp_fd);
+#endif
     av_free(s);
     return 0;
 }






More information about the ffmpeg-devel mailing list