[rtmpdump] r146 - in trunk: Makefile hashswf.c rtmp.c rtmp.h rtmpdump.c rtmpsrv.c rtmpsuck.c streams.c
hyc
subversion at mplayerhq.hu
Thu Dec 31 02:42:08 CET 2009
Author: hyc
Date: Thu Dec 31 02:42:06 2009
New Revision: 146
Log:
Refactor a bit for WIN32 compatibility
Modified:
trunk/Makefile
trunk/hashswf.c
trunk/rtmp.c
trunk/rtmp.h
trunk/rtmpdump.c
trunk/rtmpsrv.c
trunk/rtmpsuck.c
trunk/streams.c
Modified: trunk/Makefile
==============================================================================
--- trunk/Makefile Wed Dec 30 09:19:16 2009 (r145)
+++ trunk/Makefile Thu Dec 31 02:42:06 2009 (r146)
@@ -17,7 +17,7 @@ all:
@echo ' "make cygwin" for a CygWin build, or'
@echo ' "make arm" for a cross-compiled Linux ARM build'
-progs: rtmpdump streams
+progs: rtmpdump streams rtmpsrv rtmpsuck
linux:
@$(MAKE) $(MAKEFLAGS) progs
Modified: trunk/hashswf.c
==============================================================================
--- trunk/hashswf.c Wed Dec 30 09:19:16 2009 (r145)
+++ trunk/hashswf.c Thu Dec 31 02:42:06 2009 (r146)
@@ -21,16 +21,6 @@
#include <stdio.h>
#include <string.h>
-#ifdef WIN32
-#include <winsock.h>
-#else
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <netdb.h>
-#include <arpa/inet.h>
-#endif
-
#include "rtmp.h"
#include <openssl/sha.h>
@@ -104,10 +94,9 @@ http_get(const char *url, struct info *i
int port = 80;
int ssl = 0;
int hlen, flen;
- int s = -1, rc, i, ret = 0;
- FILE *sf = NULL;
+ int rc, i, ret = 0;
struct sockaddr_in sa;
- char buf[4096];
+ RTMPSockBuf sb;
memset(&sa, 0, sizeof(struct sockaddr_in));
sa.sin_family = AF_INET;
@@ -148,74 +137,103 @@ http_get(const char *url, struct info *i
sa.sin_addr = *(struct in_addr *)hp->h_addr;
}
sa.sin_port = htons(port);
- s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (s < 0)
+ sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (sb.sb_socket < 0)
return -1;
- i = sprintf(buf, "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\nReferrer: %.*s\r\n", path, AGENT, host,
- path-url+1, url);
+ i = sprintf(sb.sb_buf, "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\nReferrer: %.*s\r\n",
+ path, AGENT, host, path-url+1, url);
if (in->date[0])
- i += sprintf(buf+i, "If-Modified-Since: %s\r\n", in->date);
- i += sprintf(buf+i, "\r\n");
+ i += sprintf(sb.sb_buf+i, "If-Modified-Since: %s\r\n", in->date);
+ i += sprintf(sb.sb_buf+i, "\r\n");
- if (connect(s, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0)
+ if (connect(sb.sb_socket, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0)
{
ret = -1;
goto leave;
}
- write(s, buf, i);
- sf = fdopen(s, "rb");
+ send(sb.sb_socket, sb.sb_buf, i, 0);
- if (!fgets(buf, sizeof(buf), sf))
+ // set timeout
+ struct timeval tv;
+ memset(&tv, 0, sizeof(tv));
+ tv.tv_sec = 5;
+ if (setsockopt
+ (sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof(tv)))
+ {
+ Log(LOGERROR, "%s, Setting socket timeout to %ds failed!",
+ __FUNCTION__, tv.tv_sec);
+ }
+
+ sb.sb_size = 0;
+ sb.sb_timedout = false;
+ if (RTMPSockBuf_Fill(&sb) < 1)
{
ret = -1;
goto leave;
}
- if (strncmp(buf, "HTTP/1", 6))
+ if (strncmp(sb.sb_buf, "HTTP/1", 6))
{
ret = -1;
goto leave;
}
- p1 = strchr(buf, ' ');
+ p1 = strchr(sb.sb_buf, ' ');
rc = atoi(p1+1);
/* not modified */
if (rc == 304)
goto leave;
- while(fgets(buf, sizeof(buf), sf))
+ p1 = memchr(sb.sb_buf, '\n', sb.sb_size);
+ if (!p1)
{
- if (!strncasecmp(buf, "Content-Length: ", sizeof("Content-Length: ")-1))
+ ret = -1;
+ goto leave;
+ }
+ sb.sb_start = p1+1;
+ sb.sb_size -= sb.sb_start - sb.sb_buf;
+
+ while((p2=memchr(sb.sb_start, '\r', sb.sb_size)))
+ {
+ if (*sb.sb_start == '\r')
{
- flen = atoi(buf+sizeof("Content-Length: ")-1);
+ sb.sb_start += 2;
+ sb.sb_size -= 2;
+ break;
}
- else if (!strncasecmp(buf, "Last-Modified: ", sizeof("Last-Modified: ")-1))
+ else if (!strncasecmp(sb.sb_start, "Content-Length: ", sizeof("Content-Length: ")-1))
+ {
+ flen = atoi(sb.sb_start+sizeof("Content-Length: ")-1);
+ }
+ else if (!strncasecmp(sb.sb_start, "Last-Modified: ", sizeof("Last-Modified: ")-1))
{
- p1 = buf+sizeof("Last-Modified: ")-1;
- p2 = strchr(p1, '\r');
*p2 = '\0';
- strcpy(in->date, p1);
+ strcpy(in->date, sb.sb_start+sizeof("Last-Modified: ")-1);
+ }
+ p2 += 2;
+ sb.sb_size -= p2-sb.sb_start;
+ sb.sb_start = p2;
+ if (sb.sb_size < 1)
+ {
+ if (RTMPSockBuf_Fill(&sb) < 1)
+ {
+ ret = -1;
+ goto leave;
+ }
}
- else if (buf[0] == '\r')
- break;
}
- hlen = sizeof(buf);
- while ((i=fread(buf, 1, hlen, sf))>0)
+ while (sb.sb_size > 0 || RTMPSockBuf_Fill(&sb) > 0)
{
- swfcrunch(buf, 1, i, in);
- flen -= i;
+ swfcrunch(sb.sb_start, 1, sb.sb_size, in);
+ flen -= sb.sb_size;
if (flen < 1)
break;
- if (hlen > flen)
- hlen = flen;
+ sb.sb_size = 0;
}
leave:
- if (sf)
- fclose(sf);
- else if (s >= 0)
- close(s);
+ closesocket(sb.sb_socket);
return ret;
}
Modified: trunk/rtmp.c
==============================================================================
--- trunk/rtmp.c Wed Dec 30 09:19:16 2009 (r145)
+++ trunk/rtmp.c Thu Dec 31 02:42:06 2009 (r146)
@@ -24,22 +24,8 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include <assert.h>
-#ifdef WIN32
-#include <winsock.h>
-#define close(x) closesocket(x)
-#define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
-#define EWOULDBLOCK EAGAIN
-#define sleep(n) Sleep(n*1000)
-#else
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <sys/times.h>
-#endif
-
#include "rtmp.h"
#include "log.h"
@@ -105,8 +91,6 @@ static void HandleClientBW(RTMP * r, con
static int ReadN(RTMP * r, char *buffer, int n);
static bool WriteN(RTMP * r, const char *buffer, int n);
-static bool FillBuffer(RTMP * r);
-
static void DecodeTEA(AVal *key, AVal *text);
uint32_t
@@ -761,7 +745,7 @@ ReadN(RTMP * r, char *buffer, int n)
{
int nBytes = 0, nRead;
if (r->m_nBufferSize == 0)
- if (!FillBuffer(r))
+ if (RTMPSockBuf_Fill(&r->m_sb)<1)
{
if (!r->m_bTimedout)
RTMP_Close(r);
@@ -2359,35 +2343,40 @@ RTMP_Close(RTMP * r)
#endif
}
-static bool
-FillBuffer(RTMP * r)
+int
+RTMPSockBuf_Fill(RTMPSockBuf *sb)
{
- assert(r->m_nBufferSize == 0); // only fill buffer when it's empty
int nBytes;
-again:
- nBytes = recv(r->m_socket, r->m_pBuffer, sizeof(r->m_pBuffer), 0);
- if (nBytes != -1)
- {
- r->m_nBufferSize += nBytes;
- r->m_pBufferStart = r->m_pBuffer;
- }
- else
- {
- int sockerr = GetSockError();
- Log(LOGDEBUG, "%s, recv returned %d. GetSockError(): %d (%s)",
- __FUNCTION__, nBytes, sockerr, strerror(sockerr));
- if (sockerr == EINTR && !RTMP_ctrlC)
- goto again;
+ if (!sb->sb_size)
+ sb->sb_start = sb->sb_buf;
- if (sockerr == EWOULDBLOCK || sockerr == EAGAIN)
- r->m_bTimedout = true;
+ while (1)
+ {
+ nBytes = sizeof(sb->sb_buf) - sb->sb_size - (sb->sb_start - sb->sb_buf);
+ nBytes = recv(sb->sb_socket, sb->sb_start+sb->sb_size, nBytes, 0);
+ if (nBytes != -1)
+ {
+ sb->sb_size += nBytes;
+ }
else
- RTMP_Close(r);
- return false;
+ {
+ int sockerr = GetSockError();
+ Log(LOGDEBUG, "%s, recv returned %d. GetSockError(): %d (%s)",
+ __FUNCTION__, nBytes, sockerr, strerror(sockerr));
+ if (sockerr == EINTR && !RTMP_ctrlC)
+ continue;
+
+ if (sockerr == EWOULDBLOCK || sockerr == EAGAIN)
+ {
+ sb->sb_timedout = true;
+ nBytes = 0;
+ }
+ }
+ break;
}
- return true;
+ return nBytes;
}
#define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf))
Modified: trunk/rtmp.h
==============================================================================
--- trunk/rtmp.h Wed Dec 30 09:19:16 2009 (r145)
+++ trunk/rtmp.h Thu Dec 31 02:42:06 2009 (r146)
@@ -25,18 +25,27 @@
#ifdef WIN32
#include <winsock.h>
-#define GetSockError() WSAGetLastError()
+#define GetSockError() WSAGetLastError()
+#define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
+#define EWOULDBLOCK EAGAIN
+#define sleep(n) Sleep(n*1000)
+#define msleep(n) Sleep(n)
+#define socklen_t int
#else
#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/times.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
-#include <errno.h>
-#define GetSockError() errno
+#include <netinet/tcp.h>
+#define GetSockError() errno
+#define closesocket(s) close(s)
+#define msleep(n) usleep(n*1000)
#endif
+#include <errno.h>
#include <stdint.h>
#include "log.h"
@@ -83,6 +92,15 @@ typedef struct RTMPPacket
char *m_body;
} RTMPPacket;
+typedef struct RTMPSockBuf
+{
+ int sb_socket;
+ int sb_size; /* number of unprocessed bytes in buffer */
+ char *sb_start; /* pointer into sb_pBuffer of next byte to process */
+ char sb_buf[RTMP_BUFFER_CACHE_SIZE]; /* data read from socket */
+ bool sb_timedout;
+} RTMPSockBuf;
+
void RTMPPacket_Reset(RTMPPacket *p);
void RTMPPacket_Dump(RTMPPacket *p);
bool RTMPPacket_Alloc(RTMPPacket *p, int nSize);
@@ -144,14 +162,11 @@ typedef struct RTMP
int m_nClientBW;
uint8_t m_nClientBW2;
bool m_bPlaying;
- bool m_bTimedout;
AVal *m_methodCalls; /* remote method calls queue */
int m_numCalls;
RTMP_LNK Link;
- char *m_pBufferStart; // pointer into m_pBuffer of next byte to process
- int m_nBufferSize; // number of unprocessed bytes in buffer
RTMPPacket *m_vecChannelsIn[RTMP_CHANNELS];
RTMPPacket *m_vecChannelsOut[RTMP_CHANNELS];
int m_channelTimestamp[RTMP_CHANNELS]; // abs timestamp of last packet
@@ -161,7 +176,13 @@ typedef struct RTMP
double m_fEncoding; /* AMF0 or AMF3 */
double m_fDuration; // duration of stream in seconds
- char m_pBuffer[RTMP_BUFFER_CACHE_SIZE]; // data read from socket
+
+ RTMPSockBuf m_sb;
+#define m_socket m_sb.sb_socket
+#define m_nBufferSize m_sb.sb_size
+#define m_pBufferStart m_sb.sb_start
+#define m_pBuffer m_sb.sb_buf
+#define m_bTimedout m_sb.sb_timedout
} RTMP;
void RTMP_SetBufferMS(RTMP *r, int size);
@@ -208,6 +229,8 @@ bool RTMP_SendPause(RTMP *r, bool DoPaus
bool RTMP_FindFirstMatchingProperty(AMFObject *obj, const AVal *name,
AMFObjectProperty *p);
+bool RTMPSockBuf_Fill(RTMPSockBuf *sb);
+
#ifdef CRYPTO
/* hashswf.c */
#define HASHLEN 32
Modified: trunk/rtmpdump.c
==============================================================================
--- trunk/rtmpdump.c Wed Dec 30 09:19:16 2009 (r145)
+++ trunk/rtmpdump.c Thu Dec 31 02:42:06 2009 (r146)
@@ -24,16 +24,18 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
-#include <unistd.h>
+#include <stdio.h>
#include <signal.h> // to catch Ctrl-C
#include <getopt.h>
+#include "rtmp.h"
+#include "log.h"
+#include "parseurl.h"
+
#ifdef WIN32
#define fseeko fseeko64
#define ftello ftello64
-#include <winsock.h>
-#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
@@ -41,10 +43,6 @@
#define SET_BINMODE(f)
#endif
-#include "rtmp.h"
-#include "log.h"
-#include "parseurl.h"
-
#define RTMPDUMP_VERSION "v2.1"
#define RD_SUCCESS 0
Modified: trunk/rtmpsrv.c
==============================================================================
--- trunk/rtmpsrv.c Wed Dec 30 09:19:16 2009 (r145)
+++ trunk/rtmpsrv.c Thu Dec 31 02:42:06 2009 (r146)
@@ -658,7 +658,7 @@ stopStreaming(STREAMING_SERVER * server)
// wait for streaming threads to exit
while (server->state != STREAMING_STOPPED)
- usleep(1 * 1000);
+ msleep(1);
}
if (close(server->socket))
@@ -708,7 +708,9 @@ main(int argc, char **argv)
signal(SIGINT, sigIntHandler);
+#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
+#endif
#ifdef _DEBUG
netstackdump = fopen("netstackdump", "wb");
Modified: trunk/rtmpsuck.c
==============================================================================
--- trunk/rtmpsuck.c Wed Dec 30 09:19:16 2009 (r145)
+++ trunk/rtmpsuck.c Thu Dec 31 02:42:06 2009 (r146)
@@ -979,7 +979,7 @@ startStreaming(const char *address, int
if (listen(sockfd, 10) == -1)
{
Log(LOGERROR, "%s, listen failed", __FUNCTION__);
- close(sockfd);
+ closesocket(sockfd);
return 0;
}
@@ -1004,10 +1004,10 @@ stopStreaming(STREAMING_SERVER * server)
// wait for streaming threads to exit
while (server->state != STREAMING_STOPPED)
- usleep(1 * 1000);
+ msleep(1);
}
- if (close(server->socket))
+ if (closesocket(server->socket))
Log(LOGERROR, "%s: Failed to close listening socket, error %d",
GetSockError());
@@ -1046,7 +1046,9 @@ main(int argc, char **argv)
debuglevel = LOGALL;
signal(SIGINT, sigIntHandler);
+#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
+#endif
#ifdef _DEBUG
netstackdump = fopen("netstackdump", "wb");
Modified: trunk/streams.c
==============================================================================
--- trunk/streams.c Wed Dec 30 09:19:16 2009 (r145)
+++ trunk/streams.c Thu Dec 31 02:42:06 2009 (r146)
@@ -880,7 +880,7 @@ stopStreaming(STREAMING_SERVER * server)
// wait for streaming threads to exit
while (server->state != STREAMING_STOPPED)
- usleep(1 * 1000);
+ msleep(1);
}
if (close(server->socket))
@@ -1141,7 +1141,9 @@ main(int argc, char **argv)
};
signal(SIGINT, sigIntHandler);
+#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
+#endif
InitSockets();
More information about the rtmpdump
mailing list