From: hyc Date: Thu, 31 Dec 2009 02:58:04 +0000 (+0000) Subject: Move thread support to its own files X-Git-Tag: v2.4~349 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fa70b7551e590f94dbd1238721c788a9fd69d6b2;p=rtmpdump Move thread support to its own files git-svn-id: svn://svn.mplayerhq.hu/rtmpdump/trunk@150 400ebc74-4327-4243-bc38-086b20814532 --- diff --git a/Makefile b/Makefile index d1a142d..90c280b 100644 --- a/Makefile +++ b/Makefile @@ -37,16 +37,16 @@ arm: clean: rm -f *.o rtmpdump$(EXT) streams$(EXT) rtmpsrv$(EXT) rtmpsuck$(EXT) -streams: log.o rtmp.o amf.o streams.o parseurl.o hashswf.o - $(CC) $(LDFLAGS) $^ -o $@$(EXT) $(SLIBS) - rtmpdump: log.o rtmp.o amf.o rtmpdump.o parseurl.o hashswf.o $(CC) $(LDFLAGS) $^ -o $@$(EXT) $(LIBS) -rtmpsrv: log.o rtmp.o amf.o rtmpsrv.o +rtmpsrv: log.o rtmp.o amf.o rtmpsrv.o thread.o + $(CC) $(LDFLAGS) $^ -o $@$(EXT) $(SLIBS) + +rtmpsuck: log.o rtmp.o amf.o rtmpsuck.o hashswf.o thread.o $(CC) $(LDFLAGS) $^ -o $@$(EXT) $(SLIBS) -rtmpsuck: log.o rtmp.o amf.o rtmpsuck.o hashswf.o +streams: log.o rtmp.o amf.o streams.o parseurl.o hashswf.o thread.o $(CC) $(LDFLAGS) $^ -o $@$(EXT) $(SLIBS) log.o: log.c log.h Makefile @@ -57,3 +57,4 @@ amf.o: amf.c amf.h bytes.h log.h Makefile rtmpdump.o: rtmpdump.c rtmp.h log.h amf.h Makefile rtmpsrv.o: rtmpsrv.c rtmp.h log.h amf.h Makefile hashswf.o: hashswf.c +thread.o: thread.c thread.h diff --git a/rtmpsrv.c b/rtmpsrv.c index 1e36e13..9451373 100644 --- a/rtmpsrv.c +++ b/rtmpsrv.c @@ -36,14 +36,11 @@ #include "rtmp.h" #include "parseurl.h" -#ifdef WIN32 -#include -#else +#include "thread.h" + #ifdef linux #include #endif -#include -#endif #define RTMPDUMP_SERVER_VERSION "v2.1" @@ -448,40 +445,7 @@ ServePacket(STREAMING_SERVER *server, RTMP *r, RTMPPacket *packet) return ret; } -#ifdef WIN32 -HANDLE -ThreadCreate(void *(*routine) (void *), void *args) -{ - HANDLE thd; - - thd = (HANDLE) _beginthread(routine, 0, args); - if (thd == -1L) - LogPrintf("%s, _beginthread failed with %d\n", __FUNCTION__, errno); - - return thd; -} -#else -pthread_t -ThreadCreate(void *(*routine) (void *), void *args) -{ - pthread_t id = 0; - pthread_attr_t attributes; - int ret; - - pthread_attr_init(&attributes); - pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); - - ret = - pthread_create(&id, &attributes, (void *(*)(void *)) routine, - (void *) args); - if (ret != 0) - LogPrintf("%s, pthread_create failed with %d\n", __FUNCTION__, ret); - - return id; -} -#endif - -void * +TFTYPE controlServerThread(void *unused) { char ich; @@ -499,7 +463,7 @@ controlServerThread(void *unused) LogPrintf("Unknown command \'%c\', ignoring\n", ich); } } - return 0; + TFRET(); } @@ -565,7 +529,7 @@ quit: return; } -void * +TFTYPE serverThread(STREAMING_SERVER * server) { server->state = STREAMING_ACCEPTING; @@ -601,7 +565,7 @@ serverThread(STREAMING_SERVER * server) } } server->state = STREAMING_STOPPED; - return 0; + TFRET(); } STREAMING_SERVER * @@ -640,7 +604,7 @@ startStreaming(const char *address, int port) server = (STREAMING_SERVER *) calloc(1, sizeof(STREAMING_SERVER)); server->socket = sockfd; - ThreadCreate((void *(*)(void *)) serverThread, server); + ThreadCreate((thrfunc *)serverThread, server); return server; } diff --git a/rtmpsuck.c b/rtmpsuck.c index 07a4863..3b87e32 100644 --- a/rtmpsuck.c +++ b/rtmpsuck.c @@ -36,14 +36,11 @@ #include "rtmp.h" #include "parseurl.h" -#ifdef WIN32 -#include -#else +#include "thread.h" + #ifdef linux #include #endif -#include -#endif #define RTMPDUMP_PROXY_VERSION "v2.1" @@ -610,40 +607,7 @@ WriteStream(char **buf, // target pointer, maybe preallocated return ret; // no more media packets } -#ifdef WIN32 -HANDLE -ThreadCreate(void *(*routine) (void *), void *args) -{ - HANDLE thd; - - thd = (HANDLE) _beginthread(routine, 0, args); - if (thd == -1L) - LogPrintf("%s, _beginthread failed with %d\n", __FUNCTION__, errno); - - return thd; -} -#else -pthread_t -ThreadCreate(void *(*routine) (void *), void *args) -{ - pthread_t id = 0; - pthread_attr_t attributes; - int ret; - - pthread_attr_init(&attributes); - pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); - - ret = - pthread_create(&id, &attributes, (void *(*)(void *)) routine, - (void *) args); - if (ret != 0) - LogPrintf("%s, pthread_create failed with %d\n", __FUNCTION__, ret); - - return id; -} -#endif - -void * +TFTYPE controlServerThread(void *unused) { char ich; @@ -661,7 +625,7 @@ controlServerThread(void *unused) LogPrintf("Unknown command \'%c\', ignoring\n", ich); } } - return 0; + TFRET(); } void doServe(STREAMING_SERVER * server, // server socket and state (our listening socket) @@ -907,7 +871,7 @@ quit: return; } -void * +TFTYPE serverThread(STREAMING_SERVER * server) { server->state = STREAMING_ACCEPTING; @@ -943,7 +907,7 @@ serverThread(STREAMING_SERVER * server) } } server->state = STREAMING_STOPPED; - return 0; + TFRET(); } STREAMING_SERVER * @@ -986,7 +950,7 @@ startStreaming(const char *address, int port) server = (STREAMING_SERVER *) calloc(1, sizeof(STREAMING_SERVER)); server->socket = sockfd; - ThreadCreate((void *(*)(void *)) serverThread, server); + ThreadCreate((thrfunc *)serverThread, server); return server; } diff --git a/streams.c b/streams.c index abc0d57..bcff29c 100644 --- a/streams.c +++ b/streams.c @@ -31,11 +31,7 @@ #include "rtmp.h" #include "parseurl.h" -#ifdef WIN32 -#include -#else -#include -#endif +#include "thread.h" #define RTMPDUMP_STREAMS_VERSION "v2.1" @@ -374,40 +370,7 @@ WriteStream(RTMP * rtmp, char **buf, // target pointer, maybe preallocated return ret; // no more media packets } -#ifdef WIN32 -HANDLE -ThreadCreate(void *(*routine) (void *), void *args) -{ - HANDLE thd; - - thd = (HANDLE) _beginthread(routine, 0, args); - if (thd == -1L) - LogPrintf("%s, _beginthread failed with %d\n", __FUNCTION__, errno); - - return thd; -} -#else -pthread_t -ThreadCreate(void *(*routine) (void *), void *args) -{ - pthread_t id = 0; - pthread_attr_t attributes; - int ret; - - pthread_attr_init(&attributes); - pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); - - ret = - pthread_create(&id, &attributes, (void *(*)(void *)) routine, - (void *) args); - if (ret != 0) - LogPrintf("%s, pthread_create failed with %d\n", __FUNCTION__, ret); - - return id; -} -#endif - -void * +TFTYPE controlServerThread(void *unused) { char ich; @@ -425,7 +388,7 @@ controlServerThread(void *unused) LogPrintf("Unknown command \'%c\', ignoring\n", ich); } } - return 0; + TFRET(); } /* @@ -797,7 +760,7 @@ filenotfound: goto quit; } -void * +TFTYPE serverThread(STREAMING_SERVER * server) { server->state = STREAMING_ACCEPTING; @@ -823,7 +786,7 @@ serverThread(STREAMING_SERVER * server) } } server->state = STREAMING_STOPPED; - return 0; + TFRET(); } STREAMING_SERVER * diff --git a/thread.c b/thread.c new file mode 100644 index 0000000..61e55fa --- /dev/null +++ b/thread.c @@ -0,0 +1,54 @@ +/* Thread compatibility glue + * Copyright (C) 2009 Howard Chu + * + * This Program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This Program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with RTMPDump; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#include "thread.h" +#include "log.h" + +#ifdef WIN32 +HANDLE +ThreadCreate(thrfunc *routine, void *args) +{ + HANDLE thd; + + thd = (HANDLE) _beginthread(routine, 0, args); + if (thd == -1L) + LogPrintf("%s, _beginthread failed with %d\n", __FUNCTION__, errno); + + return thd; +} +#else +pthread_t +ThreadCreate(thrfunc *routine, void *args) +{ + pthread_t id = 0; + pthread_attr_t attributes; + int ret; + + pthread_attr_init(&attributes); + pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); + + ret = + pthread_create(&id, &attributes, routine, args); + if (ret != 0) + LogPrintf("%s, pthread_create failed with %d\n", __FUNCTION__, ret); + + return id; +} +#endif diff --git a/thread.h b/thread.h new file mode 100644 index 0000000..f638661 --- /dev/null +++ b/thread.h @@ -0,0 +1,38 @@ +/* Thread compatibility glue + * Copyright (C) 2009 Howard Chu + * + * This Program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This Program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with RTMPDump; see the file COPYING. If not, write to + * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. + * http://www.gnu.org/copyleft/gpl.html + * + */ + +#ifndef __THREAD_H__ +#define __THREAD_H__ 1 + +#ifdef WIN32 +#include +#define TFTYPE void +#define TFRET() +#define THANDLE HANDLE +#else +#include +#define TFTYPE void * +#define TFRET() return 0 +#define THANDLE pthread_t +#endif +typedef TFTYPE (thrfunc)(void *arg); + +THANDLE ThreadCreate(thrfunc *routine, void *args); +#endif /* __THREAD_H__ */