diff --git a/Makefile b/Makefile index 4745aff7..81126522 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,2 @@ all: cd cmd/revid-cli; go build - -deps: - @cd rtmp; $(MAKE) all - @echo "\n\nNow run sudo make install-deps\n" - -install-deps: - @cd rtmp; $(MAKE) install - -clean: - @cd rtmp; $(MAKE) clean diff --git a/rtmp/Makefile b/rtmp/Makefile deleted file mode 100644 index d1e3f4cc..00000000 --- a/rtmp/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -all: - @cd rtmp_c/librtmp; $(MAKE) all - -install: - @cd rtmp_c/librtmp; $(MAKE) install - -clean: - @cd rtmp_c; $(MAKE) clean diff --git a/rtmp/rtmp_c/Makefile b/rtmp/rtmp_c/Makefile deleted file mode 100644 index a1595a89..00000000 --- a/rtmp/rtmp_c/Makefile +++ /dev/null @@ -1,94 +0,0 @@ -VERSION=v2.4 - -prefix=/usr/local - -CC=$(CROSS_COMPILE)gcc -LD=$(CROSS_COMPILE)ld - -SYS=posix -#SYS=mingw - -CRYPTO=OPENSSL -#CRYPTO=POLARSSL -#CRYPTO=GNUTLS -LIBZ=-lz -LIB_GNUTLS=-lgnutls -lhogweed -lnettle -lgmp $(LIBZ) -LIB_OPENSSL=-lssl -lcrypto $(LIBZ) -LIB_POLARSSL=-lpolarssl $(LIBZ) -CRYPTO_LIB=$(LIB_$(CRYPTO)) -DEF_=-DNO_CRYPTO -CRYPTO_DEF=$(DEF_$(CRYPTO)) - -DEF=-DRTMPDUMP_VERSION=\"$(VERSION)\" $(CRYPTO_DEF) $(XDEF) -OPT=-O2 -CFLAGS=-Wall $(XCFLAGS) $(INC) $(DEF) $(OPT) -LDFLAGS=-Wall $(XLDFLAGS) - -bindir=$(prefix)/bin -sbindir=$(prefix)/sbin -mandir=$(prefix)/man - -BINDIR=$(DESTDIR)$(bindir) -SBINDIR=$(DESTDIR)$(sbindir) -MANDIR=$(DESTDIR)$(mandir) - -LIBS_posix= -LIBS_darwin= -LIBS_mingw=-lws2_32 -lwinmm -lgdi32 -LIB_RTMP=-Llibrtmp -lrtmp -LIBS=$(LIB_RTMP) $(CRYPTO_LIB) $(LIBS_$(SYS)) $(XLIBS) - -THREADLIB_posix=-lpthread -THREADLIB_darwin=-lpthread -THREADLIB_mingw= -THREADLIB=$(THREADLIB_$(SYS)) -SLIBS=$(THREADLIB) $(LIBS) - -LIBRTMP=librtmp/librtmp.a -INCRTMP=librtmp/rtmp_sys.h librtmp/rtmp.h librtmp/log.h librtmp/amf.h - -EXT_posix= -EXT_darwin= -EXT_mingw=.exe -EXT=$(EXT_$(SYS)) - -PROGS=rtmpdump rtmpgw rtmpsrv rtmpsuck - -all: $(LIBRTMP) $(PROGS) - -$(PROGS): $(LIBRTMP) - -install: $(PROGS) - -mkdir -p $(BINDIR) $(SBINDIR) $(MANDIR)/man1 $(MANDIR)/man8 - cp rtmpdump$(EXT) $(BINDIR) - cp rtmpgw$(EXT) rtmpsrv$(EXT) rtmpsuck$(EXT) $(SBINDIR) - cp rtmpdump.1 $(MANDIR)/man1 - cp rtmpgw.8 $(MANDIR)/man8 - @cd librtmp; $(MAKE) install - -clean: - rm -f *.o rtmpdump$(EXT) rtmpgw$(EXT) rtmpsrv$(EXT) rtmpsuck$(EXT) - @cd librtmp; $(MAKE) clean - -FORCE: - -$(LIBRTMP): FORCE - @cd librtmp; $(MAKE) all - -rtmpdump: rtmpdump.o - $(CC) $(LDFLAGS) -o $@$(EXT) $@.o $(LIBS) - -rtmpsrv: rtmpsrv.o thread.o - $(CC) $(LDFLAGS) -o $@$(EXT) $@.o thread.o $(SLIBS) - -rtmpsuck: rtmpsuck.o thread.o - $(CC) $(LDFLAGS) -o $@$(EXT) $@.o thread.o $(SLIBS) - -rtmpgw: rtmpgw.o thread.o - $(CC) $(LDFLAGS) -o $@$(EXT) $@.o thread.o $(SLIBS) - -rtmpgw.o: rtmpgw.c $(INCRTMP) Makefile -rtmpdump.o: rtmpdump.c $(INCRTMP) Makefile -rtmpsrv.o: rtmpsrv.c $(INCRTMP) Makefile -rtmpsuck.o: rtmpsuck.c $(INCRTMP) Makefile -thread.o: thread.c thread.h diff --git a/rtmp/rtmp_c/README b/rtmp/rtmp_c/README deleted file mode 100644 index dcf5f52c..00000000 --- a/rtmp/rtmp_c/README +++ /dev/null @@ -1,220 +0,0 @@ -RTMP Dump v2.4 -(C) 2009 Andrej Stepanchuk -(C) 2009-2011 Howard Chu -(C) 2010 2a665470ced7adb7156fcef47f8199a6371c117b8a79e399a2771e0b36384090 -(C) 2011 33ae1ce77301f4b4494faaa5f609f3c48b9dcf82 -License: GPLv2 -librtmp license: LGPLv2.1 -http://rtmpdump.mplayerhq.hu/ - -To compile type "make" with SYS=, e.g. - - $ make SYS=posix - -for Linux, Unix, etc. or - - $ make SYS=darwin - -for MacOSX or - - $ make SYS=mingw - -for Windows. - -You can cross-compile for other platforms using the CROSS_COMPILE variable: - - $ make CROSS_COMPILE=arm-none-linux- INC=-I/my/cross/includes - -Please read the Makefile to see what other make variables are used. - -This code also requires you to have OpenSSL and zlib installed. You may -optionally use GnuTLS or polarssl instead of OpenSSL if desired. You may -also build with just rtmpe support, and no rtmps/https support, by -specifying -DNO_SSL in the XDEF macro, e.g. - - $ make XDEF=-DNO_SSL - -or - - $ make CRYPTO=POLARSSL XDEF=-DNO_SSL - -You may also turn off all crypto support if desired - - $ make CRYPTO= - -A shared library is now built by default, in addition to the static -library. You can also turn it off if desired - - $ make SHARED= - -The rtmpdump programs still link to the static library, regardless. - -Note that if using OpenSSL, you must have version 0.9.8 or newer. -For Polar SSL you must have version 1.0.0 or newer. - -Credit goes to team boxee for the XBMC RTMP code originally used in RTMPDumper. -The current code is based on the XBMC code but rewritten in C by Howard Chu. - - -SWF Verification ----------------- - -Note: these instructions for manually generating the SWFVerification -info are provided only for historical documentation. The software can now -generate this info automatically, so it is no longer necessary to -run the commands described here. Just use the -W (--swfVfy) option -to perform automatic SWFVerification. - -Download the swf player you want to use for SWFVerification, unzip it using - - $ flasm -x file.swf - -It will show the decompressed filesize, use it for --swfsize - -Now generate the hash - - $ openssl sha -sha256 -hmac "Genuine Adobe Flash Player 001" file.swf - -and use the --swfhash "01234..." option to pass it. - -e.g. $ ./rtmpdump --swfhash "123456..." --swfsize 987... - - -Connect Parameters ------------------- - -Some servers expect additional custom parameters to be attached to the -RTMP connect request. The "--auth" option handles a specific case, where -a boolean TRUE followed by the given string are added to the request. -Other servers may require completely different parameters, so the new -"--conn" option has been added. This option can be set multiple times -on the command line, adding one parameter each time. - -The argument to the option must take the form : where -type can be B for boolean, S for string, N for number, and O for object. -For booleans the value must be 0 or 1. Also, for objects the value must -be 1 to start a new object, or 0 to end the current object. - -Examples: - --conn B:0 --conn S:hello --conn N:3.14159 - -Named parameters can be specified by prefixing 'N' to the type. Then the -name should come next, and finally the value: - --conn NB:myflag:1 --conn NS:category:something --conn NN:pi:3.14159 - -Objects may be added sequentially: - -C O:1 -C NB:flag:1 -C NS:status:success -C O:0 -C O:1 -C NN:time:12.30 -C O:0 -or nested: - -C O:1 -C NS:code:hello -C NO:extra:1 -C NS:data:stuff -C O:0 -C O:0 - - -Building OpenSSL 0.9.8k ------------------------ -arm: -./Configure -DL_ENDIAN --prefix=`pwd`/armlibs linux-generic32 - -Then replace gcc, cc, ar, ranlib in Makefile and crypto/Makefile by arm-linux-* variants and use make && make install_sw - -win32: -Try ./Configure mingw --prefix=`pwd`/win32libs -DL_ENDIAN -DOPENSSL_NO_HW -Replace gcc, cc, ... by mingw32-* variants in Makefile and crypto/Makefile -make && make install_sw - -OpenSSL cross-compiling can be a difficult beast. - -Precompiled OpenSSL binaries for Windows are available on -http://www.slproweb.com/products/Win32OpenSSL.html - -If you're just running a pre-built Windows rtmpdump binary, then all you -need is the "Light" installer. If you want to compile rtmpdump yourself, -you'll need the full installer. - - -Example Servers ---------------- -Three different types of servers are also present in this distribution: - rtmpsrv - a stub server - rtmpsuck - a transparent proxy - rtmpgw - an RTMP to HTTP gateway - -rtmpsrv - Note that this is very incomplete code, and I haven't yet decided -whether or not to finish it. It is useful for obtaining all the parameters -that a real Flash client would send to an RTMP server, so that they can be -used with rtmpdump. The current version now invokes rtmpdump automatically -after parsing a client request. - -rtmpsuck - proxy server. See below... - -All you need to do is redirect your Flash clients to the machine running this -server and it will dump out all the connect / play parameters that the Flash -client sent. The simplest way to cause the redirect is by editing /etc/hosts -when you know the hostname of the RTMP server, and point it to localhost while -running rtmpsrv on your machine. (This approach should work on any OS; on -Windows you would edit %SystemRoot%\system32\drivers\etc\hosts.) - -On Linux you can also use iptables to redirect all outbound RTMP traffic. You -need to be running as root in order to use the iptables command. - -In my original plan I would have the transparent proxy running as a special -user (e.g. user "proxy"), and regular Flash clients running as any other user. -In that case the proxy would make the connection to the real RTMP server. The -iptables rule would look like this: - -iptables -t nat -A OUTPUT -p tcp --dport 1935 -m owner \! --uid-owner proxy \ - -j REDIRECT - -A rule like the above will be needed to use rtmpsuck. Note that you should -replace "proxy" in the above command with an account that actually exists -on your machine. - -Using it in this mode takes advantage of the Linux support for IP redirects; -in particular it uses a special getsockopt() call to retrieve the original -destination address of the connection. That way the proxy can create the -real outbound connection without any other help from the user. The equivalent -functionality may exist on other OSs but needs more investigation. - -(Based on reading the BSD ipfw manpage, this rule ought to work on BSD: - -ipfw add 40 fwd 127.0.0.1,1935 tcp from any to any 1935 not uid proxy - -Some confirmation from any BSD users would be nice.) - -(We have a solution for Windows based on a TDI driver; this is known to -work on Win2K and WinXP but is assumed to not work on Vista or Win7 as the -TDI is no longer used on those OS versions. Also, none of the known -solutions are available as freeware.) - -The rtmpsuck command has only one option: "-z" to turn on debug logging. -It listens on port 1935 for RTMP sessions, but you can also redirect other -ports to it as needed (read the iptables docs). It first performs an RTMP -handshake with the client, then waits for the client to send a connect -request. It parses and prints the connect parameters, then makes an -outbound connection to the real RTMP server. It performs an RTMP handshake -with that server, forwards the connect request, and from that point on it -just relays packets back and forth between the two endpoints. - -It also checks for a few packets that it treats specially: a play packet -from the client will get parsed so that the playpath can be displayed. It -also handles SWF Verification requests from the server, without forwarding -them to the client. (There would be no point, since the response is tied to -each session's handshake.) - -Once the play command is processed, all subsequent audio/video data received -from the server will be written to a file, as well as being delivered back -to the client. - -The point of all this, instead of just using a sniffer, is that since rtmpsuck -has performed real handshakes with both the client and the server, it can -negotiate whatever encryption keys are needed and so record the unencrypted -data. - -rtmpgw - HTTP gateway: this is an HTTP server that accepts requests that -consist of rtmpdump parameters. It then connects to the specified RTMP -server and returns the retrieved data in the HTTP response. The only valid -HTTP request is "GET /" but additional options can be provided in normal -URL-encoded fashion. E.g. - GET /?r=rtmp:%2f%2fserver%2fmyapp&y=somefile HTTP/1.0 - -is equivalent the rtmpdump parameters "-r rtmp://server/myapp -y somefile". - -Note that only the shortform (single letter) rtmpdump options are supported. diff --git a/rtmp/rtmp_c/README.md b/rtmp/rtmp_c/README.md deleted file mode 100644 index 04b4f297..00000000 --- a/rtmp/rtmp_c/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# librtmp -Fork from http://git.ffmpeg.org/rtmpdump integrated with srs. diff --git a/rtmp/rtmp_c/librtmp/Makefile b/rtmp/rtmp_c/librtmp/Makefile deleted file mode 100644 index 43ed9b12..00000000 --- a/rtmp/rtmp_c/librtmp/Makefile +++ /dev/null @@ -1,121 +0,0 @@ -VERSION=v2.4 - -prefix=/usr/local - -incdir=$(prefix)/include/librtmp -bindir=$(prefix)/bin -libdir=$(prefix)/lib -mandir=$(prefix)/man -BINDIR=$(DESTDIR)$(bindir) -INCDIR=$(DESTDIR)$(incdir) -LIBDIR=$(DESTDIR)$(libdir) -MANDIR=$(DESTDIR)$(mandir) - -CC=$(CROSS_COMPILE)gcc -LD=$(CROSS_COMPILE)ld -AR=$(CROSS_COMPILE)ar - -SYS=posix -#CRYPTO=OPENSSL -#CRYPTO=GNUTLS -#DEF_POLARSSL=-DUSE_POLARSSL -#DEF_OPENSSL=-DUSE_OPENSSL -#DEF_GNUTLS=-DUSE_GNUTLS -DEF_=-DNO_CRYPTO -REQ_GNUTLS=gnutls,hogweed,nettle -#REQ_OPENSSL=libssl,libcrypto -PUB_GNUTLS=-lgmp -LIBZ=-lz -LIBS_posix= -LIBS_darwin= -LIBS_mingw=-lws2_32 -lwinmm -lgdi32 -LIB_GNUTLS=-lgnutls -lhogweed -lnettle -lgmp $(LIBZ) -LIB_OPENSSL=-lssl -lcrypto $(LIBZ) -LIB_POLARSSL=-lpolarssl $(LIBZ) -PRIVATE_LIBS=$(LIBS_$(SYS)) -CRYPTO_LIB=$(LIB_$(CRYPTO)) $(PRIVATE_LIBS) -CRYPTO_REQ=$(REQ_$(CRYPTO)) -CRYPTO_DEF=$(DEF_$(CRYPTO)) -PUBLIC_LIBS=$(PUB_$(CRYPTO)) - -SO_VERSION=1 -SOX_posix=so -SOX_darwin=dylib -SOX_mingw=dll -SOX=$(SOX_$(SYS)) -SO_posix=.$(SOX).$(SO_VERSION) -SO_darwin=.$(SO_VERSION).$(SOX) -SO_mingw=-$(SO_VERSION).$(SOX) -SO_EXT=$(SO_$(SYS)) - -SODIR_posix=$(LIBDIR) -SODIR_darwin=$(LIBDIR) -SODIR_mingw=$(BINDIR) -SODIR=$(SODIR_$(SYS)) - -SO_LDFLAGS_posix=-shared -Wl,-soname,$@ -SO_LDFLAGS_darwin=-dynamiclib -twolevel_namespace -undefined dynamic_lookup \ - -fno-common -headerpad_max_install_names -install_name $(libdir)/$@ -SO_LDFLAGS_mingw=-shared -Wl,--out-implib,librtmp.dll.a -SO_LDFLAGS=$(SO_LDFLAGS_$(SYS)) - -INSTALL_IMPLIB_posix= -INSTALL_IMPLIB_darwin= -INSTALL_IMPLIB_mingw=cp librtmp.dll.a $(LIBDIR) -INSTALL_IMPLIB=$(INSTALL_IMPLIB_$(SYS)) - -SHARED=yes -SODEF_yes=-fPIC -SOLIB_yes=librtmp$(SO_EXT) -SOINST_yes=install_so -SO_DEF=$(SODEF_$(SHARED)) -SO_LIB=$(SOLIB_$(SHARED)) -SO_INST=$(SOINST_$(SHARED)) - -DEF=-DRTMPDUMP_VERSION=\"$(VERSION)\" $(CRYPTO_DEF) $(XDEF) -OPT=-O2 -CFLAGS=-Wall $(XCFLAGS) $(INC) $(DEF) $(OPT) $(SO_DEF) -LDFLAGS=$(XLDFLAGS) - - -OBJS=rtmp.o log.o amf.o hashswf.o parseurl.o - -all: librtmp.a $(SO_LIB) - -clean: - rm -f *.o *.a *.$(SOX) *$(SO_EXT) librtmp.pc - -librtmp.a: $(OBJS) - $(AR) rs $@ $? - -librtmp$(SO_EXT): $(OBJS) - $(CC) $(SO_LDFLAGS) $(LDFLAGS) -o $@ $^ $> $(CRYPTO_LIB) - ln -sf $@ librtmp.$(SOX) - -log.o: log.c log.h Makefile -rtmp.o: rtmp.c rtmp.h rtmp_sys.h handshake.h dh.h log.h amf.h Makefile -amf.o: amf.c amf.h bytes.h log.h Makefile -hashswf.o: hashswf.c http.h rtmp.h rtmp_sys.h Makefile -parseurl.o: parseurl.c rtmp.h rtmp_sys.h log.h Makefile - -librtmp.pc: librtmp.pc.in Makefile - sed -e "s;@prefix@;$(prefix);" -e "s;@libdir@;$(libdir);" \ - -e "s;@VERSION@;$(VERSION);" \ - -e "s;@CRYPTO_REQ@;$(CRYPTO_REQ);" \ - -e "s;@PUBLIC_LIBS@;$(PUBLIC_LIBS);" \ - -e "s;@PRIVATE_LIBS@;$(PRIVATE_LIBS);" librtmp.pc.in > $@ - -install: install_base $(SO_INST) - -install_base: librtmp.a librtmp.pc - -mkdir -p $(INCDIR) $(LIBDIR)/pkgconfig $(MANDIR)/man3 $(SODIR) - cp amf.h http.h log.h rtmp.h rtmp_sys.h $(INCDIR) - cp librtmp.a $(LIBDIR) - cp librtmp.pc $(LIBDIR)/pkgconfig - cp librtmp.3 $(MANDIR)/man3 - -install_so: librtmp$(SO_EXT) - cp librtmp$(SO_EXT) $(SODIR) - $(INSTALL_IMPLIB) - cd $(SODIR); ln -sf librtmp$(SO_EXT) librtmp.$(SOX) - diff --git a/rtmp/rtmp_c/librtmp/amf.c b/rtmp/rtmp_c/librtmp/amf.c deleted file mode 100644 index 79541447..00000000 --- a/rtmp/rtmp_c/librtmp/amf.c +++ /dev/null @@ -1,1311 +0,0 @@ -/* - * Copyright (C) 2005-2008 Team XBMC - * http://www.xbmc.org - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#include -#include -#include - -#include "rtmp_sys.h" -#include "amf.h" -#include "log.h" -#include "bytes.h" - -static const AMFObjectProperty AMFProp_Invalid = { {0, 0}, AMF_INVALID }; -static const AMFObject AMFObj_Invalid = { 0, 0 }; -static const AVal AV_empty = { 0, 0 }; - -/* Data is Big-Endian */ -unsigned short -AMF_DecodeInt16(const char *data) -{ - unsigned char *c = (unsigned char *) data; - unsigned short val; - val = (c[0] << 8) | c[1]; - return val; -} - -unsigned int -AMF_DecodeInt24(const char *data) -{ - unsigned char *c = (unsigned char *) data; - unsigned int val; - val = (c[0] << 16) | (c[1] << 8) | c[2]; - return val; -} - -unsigned int -AMF_DecodeInt32(const char *data) -{ - unsigned char *c = (unsigned char *)data; - unsigned int val; - val = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; - return val; -} - -void -AMF_DecodeString(const char *data, AVal *bv) -{ - bv->av_len = AMF_DecodeInt16(data); - bv->av_val = (bv->av_len > 0) ? (char *)data + 2 : NULL; -} - -void -AMF_DecodeLongString(const char *data, AVal *bv) -{ - bv->av_len = AMF_DecodeInt32(data); - bv->av_val = (bv->av_len > 0) ? (char *)data + 4 : NULL; -} - -double -AMF_DecodeNumber(const char *data) -{ - double dVal; -#if __FLOAT_WORD_ORDER == __BYTE_ORDER -#if __BYTE_ORDER == __BIG_ENDIAN - memcpy(&dVal, data, 8); -#elif __BYTE_ORDER == __LITTLE_ENDIAN - unsigned char *ci, *co; - ci = (unsigned char *)data; - co = (unsigned char *)&dVal; - co[0] = ci[7]; - co[1] = ci[6]; - co[2] = ci[5]; - co[3] = ci[4]; - co[4] = ci[3]; - co[5] = ci[2]; - co[6] = ci[1]; - co[7] = ci[0]; -#endif -#else -#if __BYTE_ORDER == __LITTLE_ENDIAN /* __FLOAT_WORD_ORER == __BIG_ENDIAN */ - unsigned char *ci, *co; - ci = (unsigned char *)data; - co = (unsigned char *)&dVal; - co[0] = ci[3]; - co[1] = ci[2]; - co[2] = ci[1]; - co[3] = ci[0]; - co[4] = ci[7]; - co[5] = ci[6]; - co[6] = ci[5]; - co[7] = ci[4]; -#else /* __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN */ - unsigned char *ci, *co; - ci = (unsigned char *)data; - co = (unsigned char *)&dVal; - co[0] = ci[4]; - co[1] = ci[5]; - co[2] = ci[6]; - co[3] = ci[7]; - co[4] = ci[0]; - co[5] = ci[1]; - co[6] = ci[2]; - co[7] = ci[3]; -#endif -#endif - return dVal; -} - -int -AMF_DecodeBoolean(const char *data) -{ - return *data != 0; -} - -char * -AMF_EncodeInt16(char *output, char *outend, short nVal) -{ - if (output+2 > outend) - return NULL; - - output[1] = nVal & 0xff; - output[0] = nVal >> 8; - return output+2; -} - -char * -AMF_EncodeInt24(char *output, char *outend, int nVal) -{ - if (output+3 > outend) - return NULL; - - output[2] = nVal & 0xff; - output[1] = nVal >> 8; - output[0] = nVal >> 16; - return output+3; -} - -char * -AMF_EncodeInt32(char *output, char *outend, int nVal) -{ - if (output+4 > outend) - return NULL; - - output[3] = nVal & 0xff; - output[2] = nVal >> 8; - output[1] = nVal >> 16; - output[0] = nVal >> 24; - return output+4; -} - -char * -AMF_EncodeString(char *output, char *outend, const AVal *bv) -{ - if ((bv->av_len < 65536 && output + 1 + 2 + bv->av_len > outend) || - output + 1 + 4 + bv->av_len > outend) - return NULL; - - if (bv->av_len < 65536) - { - *output++ = AMF_STRING; - - output = AMF_EncodeInt16(output, outend, bv->av_len); - } - else - { - *output++ = AMF_LONG_STRING; - - output = AMF_EncodeInt32(output, outend, bv->av_len); - } - memcpy(output, bv->av_val, bv->av_len); - output += bv->av_len; - - return output; -} - -char * -AMF_EncodeNumber(char *output, char *outend, double dVal) -{ - if (output+1+8 > outend) - return NULL; - - *output++ = AMF_NUMBER; /* type: Number */ - -#if __FLOAT_WORD_ORDER == __BYTE_ORDER -#if __BYTE_ORDER == __BIG_ENDIAN - memcpy(output, &dVal, 8); -#elif __BYTE_ORDER == __LITTLE_ENDIAN - { - unsigned char *ci, *co; - ci = (unsigned char *)&dVal; - co = (unsigned char *)output; - co[0] = ci[7]; - co[1] = ci[6]; - co[2] = ci[5]; - co[3] = ci[4]; - co[4] = ci[3]; - co[5] = ci[2]; - co[6] = ci[1]; - co[7] = ci[0]; - } -#endif -#else -#if __BYTE_ORDER == __LITTLE_ENDIAN /* __FLOAT_WORD_ORER == __BIG_ENDIAN */ - { - unsigned char *ci, *co; - ci = (unsigned char *)&dVal; - co = (unsigned char *)output; - co[0] = ci[3]; - co[1] = ci[2]; - co[2] = ci[1]; - co[3] = ci[0]; - co[4] = ci[7]; - co[5] = ci[6]; - co[6] = ci[5]; - co[7] = ci[4]; - } -#else /* __BYTE_ORDER == __BIG_ENDIAN && __FLOAT_WORD_ORER == __LITTLE_ENDIAN */ - { - unsigned char *ci, *co; - ci = (unsigned char *)&dVal; - co = (unsigned char *)output; - co[0] = ci[4]; - co[1] = ci[5]; - co[2] = ci[6]; - co[3] = ci[7]; - co[4] = ci[0]; - co[5] = ci[1]; - co[6] = ci[2]; - co[7] = ci[3]; - } -#endif -#endif - - return output+8; -} - -char * -AMF_EncodeBoolean(char *output, char *outend, int bVal) -{ - if (output+2 > outend) - return NULL; - - *output++ = AMF_BOOLEAN; - - *output++ = bVal ? 0x01 : 0x00; - - return output; -} - -char * -AMF_EncodeNamedString(char *output, char *outend, const AVal *strName, const AVal *strValue) -{ - if (output+2+strName->av_len > outend) - return NULL; - output = AMF_EncodeInt16(output, outend, strName->av_len); - - memcpy(output, strName->av_val, strName->av_len); - output += strName->av_len; - - return AMF_EncodeString(output, outend, strValue); -} - -char * -AMF_EncodeNamedNumber(char *output, char *outend, const AVal *strName, double dVal) -{ - if (output+2+strName->av_len > outend) - return NULL; - output = AMF_EncodeInt16(output, outend, strName->av_len); - - memcpy(output, strName->av_val, strName->av_len); - output += strName->av_len; - - return AMF_EncodeNumber(output, outend, dVal); -} - -char * -AMF_EncodeNamedBoolean(char *output, char *outend, const AVal *strName, int bVal) -{ - if (output+2+strName->av_len > outend) - return NULL; - output = AMF_EncodeInt16(output, outend, strName->av_len); - - memcpy(output, strName->av_val, strName->av_len); - output += strName->av_len; - - return AMF_EncodeBoolean(output, outend, bVal); -} - -void -AMFProp_GetName(AMFObjectProperty *prop, AVal *name) -{ - *name = prop->p_name; -} - -void -AMFProp_SetName(AMFObjectProperty *prop, AVal *name) -{ - prop->p_name = *name; -} - -AMFDataType -AMFProp_GetType(AMFObjectProperty *prop) -{ - return prop->p_type; -} - -double -AMFProp_GetNumber(AMFObjectProperty *prop) -{ - return prop->p_vu.p_number; -} - -int -AMFProp_GetBoolean(AMFObjectProperty *prop) -{ - return prop->p_vu.p_number != 0; -} - -void -AMFProp_GetString(AMFObjectProperty *prop, AVal *str) -{ - if (prop->p_type == AMF_STRING) - *str = prop->p_vu.p_aval; - else - *str = AV_empty; -} - -void -AMFProp_GetObject(AMFObjectProperty *prop, AMFObject *obj) -{ - if (prop->p_type == AMF_OBJECT) - *obj = prop->p_vu.p_object; - else - *obj = AMFObj_Invalid; -} - -int -AMFProp_IsValid(AMFObjectProperty *prop) -{ - return prop->p_type != AMF_INVALID; -} - -char * -AMFProp_Encode(AMFObjectProperty *prop, char *pBuffer, char *pBufEnd) -{ - if (prop->p_type == AMF_INVALID) - return NULL; - - if (prop->p_type != AMF_NULL && pBuffer + prop->p_name.av_len + 2 + 1 >= pBufEnd) - return NULL; - - if (prop->p_type != AMF_NULL && prop->p_name.av_len) - { - *pBuffer++ = prop->p_name.av_len >> 8; - *pBuffer++ = prop->p_name.av_len & 0xff; - memcpy(pBuffer, prop->p_name.av_val, prop->p_name.av_len); - pBuffer += prop->p_name.av_len; - } - - switch (prop->p_type) - { - case AMF_NUMBER: - pBuffer = AMF_EncodeNumber(pBuffer, pBufEnd, prop->p_vu.p_number); - break; - - case AMF_BOOLEAN: - pBuffer = AMF_EncodeBoolean(pBuffer, pBufEnd, prop->p_vu.p_number != 0); - break; - - case AMF_STRING: - pBuffer = AMF_EncodeString(pBuffer, pBufEnd, &prop->p_vu.p_aval); - break; - - case AMF_NULL: - if (pBuffer+1 >= pBufEnd) - return NULL; - *pBuffer++ = AMF_NULL; - break; - - case AMF_OBJECT: - pBuffer = AMF_Encode(&prop->p_vu.p_object, pBuffer, pBufEnd); - break; - - case AMF_ECMA_ARRAY: - pBuffer = AMF_EncodeEcmaArray(&prop->p_vu.p_object, pBuffer, pBufEnd); - break; - - case AMF_STRICT_ARRAY: - pBuffer = AMF_EncodeArray(&prop->p_vu.p_object, pBuffer, pBufEnd); - break; - - default: - RTMP_Log(RTMP_LOGERROR, "%s, invalid type. %d", __FUNCTION__, prop->p_type); - pBuffer = NULL; - }; - - return pBuffer; -} - -#define AMF3_INTEGER_MAX 268435455 -#define AMF3_INTEGER_MIN -268435456 - -int -AMF3ReadInteger(const char *data, int32_t *valp) -{ - int i = 0; - int32_t val = 0; - - while (i <= 2) - { /* handle first 3 bytes */ - if (data[i] & 0x80) - { /* byte used */ - val <<= 7; /* shift up */ - val |= (data[i] & 0x7f); /* add bits */ - i++; - } - else - { - break; - } - } - - if (i > 2) - { /* use 4th byte, all 8bits */ - val <<= 8; - val |= data[3]; - - /* range check */ - if (val > AMF3_INTEGER_MAX) - val -= (1 << 29); - } - else - { /* use 7bits of last unparsed byte (0xxxxxxx) */ - val <<= 7; - val |= data[i]; - } - - *valp = val; - - return i > 2 ? 4 : i + 1; -} - -int -AMF3ReadString(const char *data, AVal *str) -{ - int32_t ref = 0; - int len; - assert(str != 0); - - len = AMF3ReadInteger(data, &ref); - data += len; - - if ((ref & 0x1) == 0) - { /* reference: 0xxx */ - uint32_t refIndex = (ref >> 1); - RTMP_Log(RTMP_LOGDEBUG, - "%s, string reference, index: %d, not supported, ignoring!", - __FUNCTION__, refIndex); - str->av_val = NULL; - str->av_len = 0; - return len; - } - else - { - uint32_t nSize = (ref >> 1); - - str->av_val = (char *)data; - str->av_len = nSize; - - return len + nSize; - } - return len; -} - -int -AMF3Prop_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, - int bDecodeName) -{ - int nOriginalSize = nSize; - AMF3DataType type; - - prop->p_name.av_len = 0; - prop->p_name.av_val = NULL; - - if (nSize == 0 || !pBuffer) - { - RTMP_Log(RTMP_LOGDEBUG, "empty buffer/no buffer pointer!"); - return -1; - } - - /* decode name */ - if (bDecodeName) - { - AVal name; - int nRes = AMF3ReadString(pBuffer, &name); - - if (name.av_len <= 0) - return nRes; - - nSize -= nRes; - if (nSize <= 0) - return -1; - prop->p_name = name; - pBuffer += nRes; - } - - /* decode */ - type = *pBuffer++; - nSize--; - - switch (type) - { - case AMF3_UNDEFINED: - case AMF3_NULL: - prop->p_type = AMF_NULL; - break; - case AMF3_FALSE: - prop->p_type = AMF_BOOLEAN; - prop->p_vu.p_number = 0.0; - break; - case AMF3_TRUE: - prop->p_type = AMF_BOOLEAN; - prop->p_vu.p_number = 1.0; - break; - case AMF3_INTEGER: - { - int32_t res = 0; - int len = AMF3ReadInteger(pBuffer, &res); - prop->p_vu.p_number = (double)res; - prop->p_type = AMF_NUMBER; - nSize -= len; - break; - } - case AMF3_DOUBLE: - if (nSize < 8) - return -1; - prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); - prop->p_type = AMF_NUMBER; - nSize -= 8; - break; - case AMF3_STRING: - case AMF3_XML_DOC: - case AMF3_XML: - { - int len = AMF3ReadString(pBuffer, &prop->p_vu.p_aval); - prop->p_type = AMF_STRING; - nSize -= len; - break; - } - case AMF3_DATE: - { - int32_t res = 0; - int len = AMF3ReadInteger(pBuffer, &res); - - nSize -= len; - pBuffer += len; - - if ((res & 0x1) == 0) - { /* reference */ - uint32_t nIndex = (res >> 1); - RTMP_Log(RTMP_LOGDEBUG, "AMF3_DATE reference: %d, not supported!", nIndex); - } - else - { - if (nSize < 8) - return -1; - - prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); - nSize -= 8; - prop->p_type = AMF_NUMBER; - } - break; - } - case AMF3_OBJECT: - { - int nRes = AMF3_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE); - if (nRes == -1) - return -1; - nSize -= nRes; - prop->p_type = AMF_OBJECT; - break; - } - case AMF3_ARRAY: - case AMF3_BYTE_ARRAY: - default: - RTMP_Log(RTMP_LOGDEBUG, "%s - AMF3 unknown/unsupported datatype 0x%02x, @%p", - __FUNCTION__, (unsigned char)(*pBuffer), pBuffer); - return -1; - } - if (nSize < 0) - return -1; - - return nOriginalSize - nSize; -} - -int -AMFProp_Decode(AMFObjectProperty *prop, const char *pBuffer, int nSize, - int bDecodeName) -{ - int nOriginalSize = nSize; - int nRes; - - prop->p_name.av_len = 0; - prop->p_name.av_val = NULL; - - if (nSize == 0 || !pBuffer) - { - RTMP_Log(RTMP_LOGDEBUG, "%s: Empty buffer/no buffer pointer!", __FUNCTION__); - return -1; - } - - if (bDecodeName && nSize < 4) - { /* at least name (length + at least 1 byte) and 1 byte of data */ - RTMP_Log(RTMP_LOGDEBUG, - "%s: Not enough data for decoding with name, less than 4 bytes!", - __FUNCTION__); - return -1; - } - - if (bDecodeName) - { - unsigned short nNameSize = AMF_DecodeInt16(pBuffer); - if (nNameSize > nSize - 2) - { - RTMP_Log(RTMP_LOGDEBUG, - "%s: Name size out of range: namesize (%d) > len (%d) - 2", - __FUNCTION__, nNameSize, nSize); - return -1; - } - - AMF_DecodeString(pBuffer, &prop->p_name); - nSize -= 2 + nNameSize; - pBuffer += 2 + nNameSize; - } - - if (nSize == 0) - { - return -1; - } - - nSize--; - - prop->p_type = *pBuffer++; - switch (prop->p_type) - { - case AMF_NUMBER: - if (nSize < 8) - return -1; - prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); - nSize -= 8; - break; - case AMF_BOOLEAN: - if (nSize < 1) - return -1; - prop->p_vu.p_number = (double)AMF_DecodeBoolean(pBuffer); - nSize--; - break; - case AMF_STRING: - { - unsigned short nStringSize = AMF_DecodeInt16(pBuffer); - - if (nSize < (long)nStringSize + 2) - return -1; - AMF_DecodeString(pBuffer, &prop->p_vu.p_aval); - nSize -= (2 + nStringSize); - break; - } - case AMF_OBJECT: - { - int nRes = AMF_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE); - if (nRes == -1) - return -1; - nSize -= nRes; - break; - } - case AMF_MOVIECLIP: - { - RTMP_Log(RTMP_LOGERROR, "AMF_MOVIECLIP reserved!"); - return -1; - break; - } - case AMF_NULL: - case AMF_UNDEFINED: - case AMF_UNSUPPORTED: - prop->p_type = AMF_NULL; - break; - case AMF_REFERENCE: - { - RTMP_Log(RTMP_LOGERROR, "AMF_REFERENCE not supported!"); - return -1; - break; - } - case AMF_ECMA_ARRAY: - { - nSize -= 4; - - /* next comes the rest, mixed array has a final 0x000009 mark and names, so its an object */ - nRes = AMF_Decode(&prop->p_vu.p_object, pBuffer + 4, nSize, TRUE); - if (nRes == -1) - return -1; - nSize -= nRes; - break; - } - case AMF_OBJECT_END: - { - return -1; - break; - } - case AMF_STRICT_ARRAY: - { - unsigned int nArrayLen = AMF_DecodeInt32(pBuffer); - nSize -= 4; - - nRes = AMF_DecodeArray(&prop->p_vu.p_object, pBuffer + 4, nSize, - nArrayLen, FALSE); - if (nRes == -1) - return -1; - nSize -= nRes; - break; - } - case AMF_DATE: - { - RTMP_Log(RTMP_LOGDEBUG, "AMF_DATE"); - - if (nSize < 10) - return -1; - - prop->p_vu.p_number = AMF_DecodeNumber(pBuffer); - prop->p_UTCoffset = AMF_DecodeInt16(pBuffer + 8); - - nSize -= 10; - break; - } - case AMF_LONG_STRING: - case AMF_XML_DOC: - { - unsigned int nStringSize = AMF_DecodeInt32(pBuffer); - if (nSize < (long)nStringSize + 4) - return -1; - AMF_DecodeLongString(pBuffer, &prop->p_vu.p_aval); - nSize -= (4 + nStringSize); - if (prop->p_type == AMF_LONG_STRING) - prop->p_type = AMF_STRING; - break; - } - case AMF_RECORDSET: - { - RTMP_Log(RTMP_LOGERROR, "AMF_RECORDSET reserved!"); - return -1; - break; - } - case AMF_TYPED_OBJECT: - { - RTMP_Log(RTMP_LOGERROR, "AMF_TYPED_OBJECT not supported!"); - return -1; - break; - } - case AMF_AVMPLUS: - { - int nRes = AMF3_Decode(&prop->p_vu.p_object, pBuffer, nSize, TRUE); - if (nRes == -1) - return -1; - nSize -= nRes; - prop->p_type = AMF_OBJECT; - break; - } - default: - RTMP_Log(RTMP_LOGDEBUG, "%s - unknown datatype 0x%02x, @%p", __FUNCTION__, - prop->p_type, pBuffer - 1); - return -1; - } - - return nOriginalSize - nSize; -} - -void -AMFProp_Dump(AMFObjectProperty *prop) -{ - char strRes[256]; - char str[256]; - AVal name; - - if (prop->p_type == AMF_INVALID) - { - RTMP_Log(RTMP_LOGDEBUG, "Property: INVALID"); - return; - } - - if (prop->p_type == AMF_NULL) - { - RTMP_Log(RTMP_LOGDEBUG, "Property: NULL"); - return; - } - - if (prop->p_name.av_len) - { - name = prop->p_name; - } - else - { - name.av_val = "no-name."; - name.av_len = sizeof("no-name.") - 1; - } - if (name.av_len > 18) - name.av_len = 18; - - snprintf(strRes, 255, "Name: %18.*s, ", name.av_len, name.av_val); - - if (prop->p_type == AMF_OBJECT) - { - RTMP_Log(RTMP_LOGDEBUG, "Property: <%sOBJECT>", strRes); - AMF_Dump(&prop->p_vu.p_object); - return; - } - else if (prop->p_type == AMF_ECMA_ARRAY) - { - RTMP_Log(RTMP_LOGDEBUG, "Property: <%sECMA_ARRAY>", strRes); - AMF_Dump(&prop->p_vu.p_object); - return; - } - else if (prop->p_type == AMF_STRICT_ARRAY) - { - RTMP_Log(RTMP_LOGDEBUG, "Property: <%sSTRICT_ARRAY>", strRes); - AMF_Dump(&prop->p_vu.p_object); - return; - } - - switch (prop->p_type) - { - case AMF_NUMBER: - snprintf(str, 255, "NUMBER:\t%.2f", prop->p_vu.p_number); - break; - case AMF_BOOLEAN: - snprintf(str, 255, "BOOLEAN:\t%s", - prop->p_vu.p_number != 0.0 ? "TRUE" : "FALSE"); - break; - case AMF_STRING: - snprintf(str, 255, "STRING:\t%.*s", prop->p_vu.p_aval.av_len, - prop->p_vu.p_aval.av_val); - break; - case AMF_DATE: - snprintf(str, 255, "DATE:\ttimestamp: %.2f, UTC offset: %d", - prop->p_vu.p_number, prop->p_UTCoffset); - break; - default: - snprintf(str, 255, "INVALID TYPE 0x%02x", (unsigned char)prop->p_type); - } - - RTMP_Log(RTMP_LOGDEBUG, "Property: <%s%s>", strRes, str); -} - -void -AMFProp_Reset(AMFObjectProperty *prop) -{ - if (prop->p_type == AMF_OBJECT || prop->p_type == AMF_ECMA_ARRAY || - prop->p_type == AMF_STRICT_ARRAY) - AMF_Reset(&prop->p_vu.p_object); - else - { - prop->p_vu.p_aval.av_len = 0; - prop->p_vu.p_aval.av_val = NULL; - } - prop->p_type = AMF_INVALID; -} - -/* AMFObject */ - -char * -AMF_Encode(AMFObject *obj, char *pBuffer, char *pBufEnd) -{ - int i; - - if (pBuffer+4 >= pBufEnd) - return NULL; - - *pBuffer++ = AMF_OBJECT; - - for (i = 0; i < obj->o_num; i++) - { - char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd); - if (res == NULL) - { - RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d", - i); - break; - } - else - { - pBuffer = res; - } - } - - if (pBuffer + 3 >= pBufEnd) - return NULL; /* no room for the end marker */ - - pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END); - - return pBuffer; -} - -char * -AMF_EncodeEcmaArray(AMFObject *obj, char *pBuffer, char *pBufEnd) -{ - int i; - - if (pBuffer+4 >= pBufEnd) - return NULL; - - *pBuffer++ = AMF_ECMA_ARRAY; - - pBuffer = AMF_EncodeInt32(pBuffer, pBufEnd, obj->o_num); - - for (i = 0; i < obj->o_num; i++) - { - char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd); - if (res == NULL) - { - RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d", - i); - break; - } - else - { - pBuffer = res; - } - } - - if (pBuffer + 3 >= pBufEnd) - return NULL; /* no room for the end marker */ - - pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END); - - return pBuffer; -} - -char * -AMF_EncodeArray(AMFObject *obj, char *pBuffer, char *pBufEnd) -{ - int i; - - if (pBuffer+4 >= pBufEnd) - return NULL; - - *pBuffer++ = AMF_STRICT_ARRAY; - - pBuffer = AMF_EncodeInt32(pBuffer, pBufEnd, obj->o_num); - - for (i = 0; i < obj->o_num; i++) - { - char *res = AMFProp_Encode(&obj->o_props[i], pBuffer, pBufEnd); - if (res == NULL) - { - RTMP_Log(RTMP_LOGERROR, "AMF_Encode - failed to encode property in index %d", - i); - break; - } - else - { - pBuffer = res; - } - } - - //if (pBuffer + 3 >= pBufEnd) - // return NULL; /* no room for the end marker */ - - //pBuffer = AMF_EncodeInt24(pBuffer, pBufEnd, AMF_OBJECT_END); - - return pBuffer; -} - -int -AMF_DecodeArray(AMFObject *obj, const char *pBuffer, int nSize, - int nArrayLen, int bDecodeName) -{ - int nOriginalSize = nSize; - int bError = FALSE; - - obj->o_num = 0; - obj->o_props = NULL; - while (nArrayLen > 0) - { - AMFObjectProperty prop; - int nRes; - nArrayLen--; - - if (nSize <= 0) - { - bError = TRUE; - break; - } - nRes = AMFProp_Decode(&prop, pBuffer, nSize, bDecodeName); - if (nRes == -1) - { - bError = TRUE; - break; - } - else - { - nSize -= nRes; - pBuffer += nRes; - AMF_AddProp(obj, &prop); - } - } - if (bError) - return -1; - - return nOriginalSize - nSize; -} - -int -AMF3_Decode(AMFObject *obj, const char *pBuffer, int nSize, int bAMFData) -{ - int nOriginalSize = nSize; - int32_t ref; - int len; - - obj->o_num = 0; - obj->o_props = NULL; - if (bAMFData) - { - if (*pBuffer != AMF3_OBJECT) - RTMP_Log(RTMP_LOGERROR, - "AMF3 Object encapsulated in AMF stream does not start with AMF3_OBJECT!"); - pBuffer++; - nSize--; - } - - ref = 0; - len = AMF3ReadInteger(pBuffer, &ref); - pBuffer += len; - nSize -= len; - - if ((ref & 1) == 0) - { /* object reference, 0xxx */ - uint32_t objectIndex = (ref >> 1); - - RTMP_Log(RTMP_LOGDEBUG, "Object reference, index: %d", objectIndex); - } - else /* object instance */ - { - int32_t classRef = (ref >> 1); - - AMF3ClassDef cd = { {0, 0} - }; - AMFObjectProperty prop; - - if ((classRef & 0x1) == 0) - { /* class reference */ - uint32_t classIndex = (classRef >> 1); - RTMP_Log(RTMP_LOGDEBUG, "Class reference: %d", classIndex); - } - else - { - int32_t classExtRef = (classRef >> 1); - int i, cdnum; - - cd.cd_externalizable = (classExtRef & 0x1) == 1; - cd.cd_dynamic = ((classExtRef >> 1) & 0x1) == 1; - - cdnum = classExtRef >> 2; - - /* class name */ - - len = AMF3ReadString(pBuffer, &cd.cd_name); - nSize -= len; - pBuffer += len; - - /*std::string str = className; */ - - RTMP_Log(RTMP_LOGDEBUG, - "Class name: %s, externalizable: %d, dynamic: %d, classMembers: %d", - cd.cd_name.av_val, cd.cd_externalizable, cd.cd_dynamic, - cd.cd_num); - - for (i = 0; i < cdnum; i++) - { - AVal memberName; - if (nSize <=0) - { -invalid: - RTMP_Log(RTMP_LOGDEBUG, "%s, invalid class encoding!", - __FUNCTION__); - return nOriginalSize; - } - len = AMF3ReadString(pBuffer, &memberName); - RTMP_Log(RTMP_LOGDEBUG, "Member: %s", memberName.av_val); - AMF3CD_AddProp(&cd, &memberName); - nSize -= len; - pBuffer += len; - } - } - - /* add as referencable object */ - - if (cd.cd_externalizable) - { - int nRes; - AVal name = AVC("DEFAULT_ATTRIBUTE"); - - RTMP_Log(RTMP_LOGDEBUG, "Externalizable, TODO check"); - - nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, FALSE); - if (nRes == -1) - RTMP_Log(RTMP_LOGDEBUG, "%s, failed to decode AMF3 property!", - __FUNCTION__); - else - { - nSize -= nRes; - pBuffer += nRes; - } - - AMFProp_SetName(&prop, &name); - AMF_AddProp(obj, &prop); - } - else - { - int nRes, i; - for (i = 0; i < cd.cd_num; i++) /* non-dynamic */ - { - if (nSize <=0) - goto invalid; - nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, FALSE); - if (nRes == -1) - RTMP_Log(RTMP_LOGDEBUG, "%s, failed to decode AMF3 property!", - __FUNCTION__); - - AMFProp_SetName(&prop, AMF3CD_GetProp(&cd, i)); - AMF_AddProp(obj, &prop); - - pBuffer += nRes; - nSize -= nRes; - } - if (cd.cd_dynamic) - { - int len = 0; - - do - { - if (nSize <=0) - goto invalid; - nRes = AMF3Prop_Decode(&prop, pBuffer, nSize, TRUE); - AMF_AddProp(obj, &prop); - - pBuffer += nRes; - nSize -= nRes; - - len = prop.p_name.av_len; - } - while (len > 0); - } - } - RTMP_Log(RTMP_LOGDEBUG, "class object!"); - } - return nOriginalSize - nSize; -} - -int -AMF_Decode(AMFObject *obj, const char *pBuffer, int nSize, int bDecodeName) -{ - int nOriginalSize = nSize; - int bError = FALSE; /* if there is an error while decoding - try to at least find the end mark AMF_OBJECT_END */ - - obj->o_num = 0; - obj->o_props = NULL; - while (nSize > 0) - { - AMFObjectProperty prop; - int nRes; - - if (nSize >=3 && AMF_DecodeInt24(pBuffer) == AMF_OBJECT_END) - { - nSize -= 3; - bError = FALSE; - break; - } - - if (bError) - { - RTMP_Log(RTMP_LOGERROR, - "DECODING ERROR, IGNORING BYTES UNTIL NEXT KNOWN PATTERN!"); - nSize--; - pBuffer++; - continue; - } - - nRes = AMFProp_Decode(&prop, pBuffer, nSize, bDecodeName); - if (nRes == -1) - { - bError = TRUE; - break; - } - else - { - nSize -= nRes; - if (nSize < 0) - { - bError = TRUE; - break; - } - pBuffer += nRes; - AMF_AddProp(obj, &prop); - } - } - - if (bError) - return -1; - - return nOriginalSize - nSize; -} - -void -AMF_AddProp(AMFObject *obj, const AMFObjectProperty *prop) -{ - if (!(obj->o_num & 0x0f)) - obj->o_props = - realloc(obj->o_props, (obj->o_num + 16) * sizeof(AMFObjectProperty)); - memcpy(&obj->o_props[obj->o_num++], prop, sizeof(AMFObjectProperty)); -} - -int -AMF_CountProp(AMFObject *obj) -{ - return obj->o_num; -} - -AMFObjectProperty * -AMF_GetProp(AMFObject *obj, const AVal *name, int nIndex) -{ - if (nIndex >= 0) - { - if (nIndex < obj->o_num) - return &obj->o_props[nIndex]; - } - else - { - int n; - for (n = 0; n < obj->o_num; n++) - { - if (AVMATCH(&obj->o_props[n].p_name, name)) - return &obj->o_props[n]; - } - } - - return (AMFObjectProperty *)&AMFProp_Invalid; -} - -void -AMF_Dump(AMFObject *obj) -{ - int n; - RTMP_Log(RTMP_LOGDEBUG, "(object begin)"); - for (n = 0; n < obj->o_num; n++) - { - AMFProp_Dump(&obj->o_props[n]); - } - RTMP_Log(RTMP_LOGDEBUG, "(object end)"); -} - -void -AMF_Reset(AMFObject *obj) -{ - int n; - for (n = 0; n < obj->o_num; n++) - { - AMFProp_Reset(&obj->o_props[n]); - } - free(obj->o_props); - obj->o_props = NULL; - obj->o_num = 0; -} - - -/* AMF3ClassDefinition */ - -void -AMF3CD_AddProp(AMF3ClassDef *cd, AVal *prop) -{ - if (!(cd->cd_num & 0x0f)) - cd->cd_props = realloc(cd->cd_props, (cd->cd_num + 16) * sizeof(AVal)); - cd->cd_props[cd->cd_num++] = *prop; -} - -AVal * -AMF3CD_GetProp(AMF3ClassDef *cd, int nIndex) -{ - if (nIndex >= cd->cd_num) - return (AVal *)&AV_empty; - return &cd->cd_props[nIndex]; -} diff --git a/rtmp/rtmp_c/librtmp/amf.h b/rtmp/rtmp_c/librtmp/amf.h deleted file mode 100644 index b061edb9..00000000 --- a/rtmp/rtmp_c/librtmp/amf.h +++ /dev/null @@ -1,165 +0,0 @@ -#ifndef __AMF_H__ -#define __AMF_H__ -/* - * Copyright (C) 2005-2008 Team XBMC - * http://www.xbmc.org - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#include - -#ifndef TRUE -#define TRUE 1 -#define FALSE 0 -#endif - -#ifdef __cplusplus -extern "C" -{ -#endif - - typedef enum - { AMF_NUMBER = 0, AMF_BOOLEAN, AMF_STRING, AMF_OBJECT, - AMF_MOVIECLIP, /* reserved, not used */ - AMF_NULL, AMF_UNDEFINED, AMF_REFERENCE, AMF_ECMA_ARRAY, AMF_OBJECT_END, - AMF_STRICT_ARRAY, AMF_DATE, AMF_LONG_STRING, AMF_UNSUPPORTED, - AMF_RECORDSET, /* reserved, not used */ - AMF_XML_DOC, AMF_TYPED_OBJECT, - AMF_AVMPLUS, /* switch to AMF3 */ - AMF_INVALID = 0xff - } AMFDataType; - - typedef enum - { AMF3_UNDEFINED = 0, AMF3_NULL, AMF3_FALSE, AMF3_TRUE, - AMF3_INTEGER, AMF3_DOUBLE, AMF3_STRING, AMF3_XML_DOC, AMF3_DATE, - AMF3_ARRAY, AMF3_OBJECT, AMF3_XML, AMF3_BYTE_ARRAY - } AMF3DataType; - - typedef struct AVal - { - char *av_val; - int av_len; - } AVal; -#define AVC(str) {str,sizeof(str)-1} -#define AVMATCH(a1,a2) ((a1)->av_len == (a2)->av_len && !memcmp((a1)->av_val,(a2)->av_val,(a1)->av_len)) - - struct AMFObjectProperty; - - typedef struct AMFObject - { - int o_num; - struct AMFObjectProperty *o_props; - } AMFObject; - - typedef struct P_vu { - double p_number; - AVal p_aval; - AMFObject p_object; - } P_vu; - - typedef struct AMFObjectProperty - { - AVal p_name; - AMFDataType p_type; - P_vu p_vu; - int16_t p_UTCoffset; - } AMFObjectProperty; - - char *AMF_EncodeString(char *output, char *outend, const AVal * str); - char *AMF_EncodeNumber(char *output, char *outend, double dVal); - char *AMF_EncodeInt16(char *output, char *outend, short nVal); - char *AMF_EncodeInt24(char *output, char *outend, int nVal); - char *AMF_EncodeInt32(char *output, char *outend, int nVal); - char *AMF_EncodeBoolean(char *output, char *outend, int bVal); - - /* Shortcuts for AMFProp_Encode */ - char *AMF_EncodeNamedString(char *output, char *outend, const AVal * name, const AVal * value); - char *AMF_EncodeNamedNumber(char *output, char *outend, const AVal * name, double dVal); - char *AMF_EncodeNamedBoolean(char *output, char *outend, const AVal * name, int bVal); - - unsigned short AMF_DecodeInt16(const char *data); - unsigned int AMF_DecodeInt24(const char *data); - unsigned int AMF_DecodeInt32(const char *data); - void AMF_DecodeString(const char *data, AVal * str); - void AMF_DecodeLongString(const char *data, AVal * str); - int AMF_DecodeBoolean(const char *data); - double AMF_DecodeNumber(const char *data); - - char *AMF_Encode(AMFObject * obj, char *pBuffer, char *pBufEnd); - char *AMF_EncodeEcmaArray(AMFObject *obj, char *pBuffer, char *pBufEnd); - char *AMF_EncodeArray(AMFObject *obj, char *pBuffer, char *pBufEnd); - - int AMF_Decode(AMFObject * obj, const char *pBuffer, int nSize, - int bDecodeName); - int AMF_DecodeArray(AMFObject * obj, const char *pBuffer, int nSize, - int nArrayLen, int bDecodeName); - int AMF3_Decode(AMFObject * obj, const char *pBuffer, int nSize, - int bDecodeName); - void AMF_Dump(AMFObject * obj); - void AMF_Reset(AMFObject * obj); - - void AMF_AddProp(AMFObject * obj, const AMFObjectProperty * prop); - int AMF_CountProp(AMFObject * obj); - AMFObjectProperty *AMF_GetProp(AMFObject * obj, const AVal * name, - int nIndex); - - AMFDataType AMFProp_GetType(AMFObjectProperty * prop); - void AMFProp_SetNumber(AMFObjectProperty * prop, double dval); - void AMFProp_SetBoolean(AMFObjectProperty * prop, int bflag); - void AMFProp_SetString(AMFObjectProperty * prop, AVal * str); - void AMFProp_SetObject(AMFObjectProperty * prop, AMFObject * obj); - - void AMFProp_GetName(AMFObjectProperty * prop, AVal * name); - void AMFProp_SetName(AMFObjectProperty * prop, AVal * name); - double AMFProp_GetNumber(AMFObjectProperty * prop); - int AMFProp_GetBoolean(AMFObjectProperty * prop); - void AMFProp_GetString(AMFObjectProperty * prop, AVal * str); - void AMFProp_GetObject(AMFObjectProperty * prop, AMFObject * obj); - - int AMFProp_IsValid(AMFObjectProperty * prop); - - char *AMFProp_Encode(AMFObjectProperty * prop, char *pBuffer, char *pBufEnd); - int AMF3Prop_Decode(AMFObjectProperty * prop, const char *pBuffer, - int nSize, int bDecodeName); - int AMFProp_Decode(AMFObjectProperty * prop, const char *pBuffer, - int nSize, int bDecodeName); - - void AMFProp_Dump(AMFObjectProperty * prop); - void AMFProp_Reset(AMFObjectProperty * prop); - - typedef struct AMF3ClassDef - { - AVal cd_name; - char cd_externalizable; - char cd_dynamic; - int cd_num; - AVal *cd_props; - } AMF3ClassDef; - - void AMF3CD_AddProp(AMF3ClassDef * cd, AVal * prop); - AVal *AMF3CD_GetProp(AMF3ClassDef * cd, int idx); - -#ifdef __cplusplus -} -#endif - -#endif /* __AMF_H__ */ diff --git a/rtmp/rtmp_c/librtmp/bytes.h b/rtmp/rtmp_c/librtmp/bytes.h deleted file mode 100644 index 8c6e80d0..00000000 --- a/rtmp/rtmp_c/librtmp/bytes.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright (C) 2005-2008 Team XBMC - * http://www.xbmc.org - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#ifndef __BYTES_H__ -#define __BYTES_H__ - -#include - -#ifdef _WIN32 -/* Windows is little endian only */ -#define __LITTLE_ENDIAN 1234 -#define __BIG_ENDIAN 4321 -#define __BYTE_ORDER __LITTLE_ENDIAN -#define __FLOAT_WORD_ORDER __BYTE_ORDER - -typedef unsigned char uint8_t; - -#else /* !_WIN32 */ - -#include - -#if defined(BYTE_ORDER) && !defined(__BYTE_ORDER) -#define __BYTE_ORDER BYTE_ORDER -#endif - -#if defined(BIG_ENDIAN) && !defined(__BIG_ENDIAN) -#define __BIG_ENDIAN BIG_ENDIAN -#endif - -#if defined(LITTLE_ENDIAN) && !defined(__LITTLE_ENDIAN) -#define __LITTLE_ENDIAN LITTLE_ENDIAN -#endif - -#endif /* !_WIN32 */ - -/* define default endianness */ -#ifndef __LITTLE_ENDIAN -#define __LITTLE_ENDIAN 1234 -#endif - -#ifndef __BIG_ENDIAN -#define __BIG_ENDIAN 4321 -#endif - -#ifndef __BYTE_ORDER -#warning "Byte order not defined on your system, assuming little endian!" -#define __BYTE_ORDER __LITTLE_ENDIAN -#endif - -/* ok, we assume to have the same float word order and byte order if float word order is not defined */ -#ifndef __FLOAT_WORD_ORDER -#warning "Float word order not defined, assuming the same as byte order!" -#define __FLOAT_WORD_ORDER __BYTE_ORDER -#endif - -#if !defined(__BYTE_ORDER) || !defined(__FLOAT_WORD_ORDER) -#error "Undefined byte or float word order!" -#endif - -#if __FLOAT_WORD_ORDER != __BIG_ENDIAN && __FLOAT_WORD_ORDER != __LITTLE_ENDIAN -#error "Unknown/unsupported float word order!" -#endif - -#if __BYTE_ORDER != __BIG_ENDIAN && __BYTE_ORDER != __LITTLE_ENDIAN -#error "Unknown/unsupported byte order!" -#endif - -#endif - diff --git a/rtmp/rtmp_c/librtmp/dh.h b/rtmp/rtmp_c/librtmp/dh.h deleted file mode 100644 index 5fc3f32e..00000000 --- a/rtmp/rtmp_c/librtmp/dh.h +++ /dev/null @@ -1,376 +0,0 @@ -/* RTMPDump - Diffie-Hellmann Key Exchange - * Copyright (C) 2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#include -#include -#include -#include -#include - -#ifdef USE_POLARSSL -#include -typedef mpi * MP_t; -#define MP_new(m) m = malloc(sizeof(mpi)); mpi_init(m) -#define MP_set_w(mpi, w) mpi_lset(mpi, w) -#define MP_cmp(u, v) mpi_cmp_mpi(u, v) -#define MP_set(u, v) mpi_copy(u, v) -#define MP_sub_w(mpi, w) mpi_sub_int(mpi, mpi, w) -#define MP_cmp_1(mpi) mpi_cmp_int(mpi, 1) -#define MP_modexp(r, y, q, p) mpi_exp_mod(r, y, q, p, NULL) -#define MP_free(mpi) mpi_free(mpi); free(mpi) -#define MP_gethex(u, hex, res) MP_new(u); res = mpi_read_string(u, 16, hex) == 0 -#define MP_bytes(u) mpi_size(u) -#define MP_setbin(u,buf,len) mpi_write_binary(u,buf,len) -#define MP_getbin(u,buf,len) MP_new(u); mpi_read_binary(u,buf,len) - -typedef struct MDH { - MP_t p; - MP_t g; - MP_t pub_key; - MP_t priv_key; - long length; - dhm_context ctx; -} MDH; - -#define MDH_new() calloc(1,sizeof(MDH)) -#define MDH_free(vp) {MDH *_dh = vp; dhm_free(&_dh->ctx); MP_free(_dh->p); MP_free(_dh->g); MP_free(_dh->pub_key); MP_free(_dh->priv_key); free(_dh);} - -static int MDH_generate_key(MDH *dh) -{ - unsigned char out[2]; - MP_set(&dh->ctx.P, dh->p); - MP_set(&dh->ctx.G, dh->g); - dh->ctx.len = 128; - dhm_make_public(&dh->ctx, 1024, out, 1, havege_random, &RTMP_TLS_ctx->hs); - MP_new(dh->pub_key); - MP_new(dh->priv_key); - MP_set(dh->pub_key, &dh->ctx.GX); - MP_set(dh->priv_key, &dh->ctx.X); - return 1; -} - -static int MDH_compute_key(uint8_t *secret, size_t len, MP_t pub, MDH *dh) -{ - MP_set(&dh->ctx.GY, pub); - dhm_calc_secret(&dh->ctx, secret, &len); - return 0; -} - -#elif defined(USE_GNUTLS) -#include -#include -#include -typedef mpz_ptr MP_t; -#define MP_new(m) m = malloc(sizeof(*m)); mpz_init2(m, 1) -#define MP_set_w(mpi, w) mpz_set_ui(mpi, w) -#define MP_cmp(u, v) mpz_cmp(u, v) -#define MP_set(u, v) mpz_set(u, v) -#define MP_sub_w(mpi, w) mpz_sub_ui(mpi, mpi, w) -#define MP_cmp_1(mpi) mpz_cmp_ui(mpi, 1) -#define MP_modexp(r, y, q, p) mpz_powm(r, y, q, p) -#define MP_free(mpi) mpz_clear(mpi); free(mpi) -#define MP_gethex(u, hex, res) u = malloc(sizeof(*u)); mpz_init2(u, 1); res = (mpz_set_str(u, hex, 16) == 0) -#define MP_bytes(u) (mpz_sizeinbase(u, 2) + 7) / 8 -#define MP_setbin(u,buf,len) nettle_mpz_get_str_256(len,buf,u) -#define MP_getbin(u,buf,len) u = malloc(sizeof(*u)); mpz_init2(u, 1); nettle_mpz_set_str_256_u(u,len,buf) - -typedef struct MDH { - MP_t p; - MP_t g; - MP_t pub_key; - MP_t priv_key; - long length; -} MDH; - -#define MDH_new() calloc(1,sizeof(MDH)) -#define MDH_free(dh) do {MP_free(((MDH*)(dh))->p); MP_free(((MDH*)(dh))->g); MP_free(((MDH*)(dh))->pub_key); MP_free(((MDH*)(dh))->priv_key); free(dh);} while(0) - -static int MDH_generate_key(MDH *dh) -{ - int num_bytes; - uint32_t seed; - gmp_randstate_t rs; - - num_bytes = (mpz_sizeinbase(dh->p, 2) + 7) / 8 - 1; - if (num_bytes <= 0 || num_bytes > 18000) - return 0; - - dh->priv_key = calloc(1, sizeof(*dh->priv_key)); - if (!dh->priv_key) - return 0; - mpz_init2(dh->priv_key, 1); - gnutls_rnd(GNUTLS_RND_RANDOM, &seed, sizeof(seed)); - gmp_randinit_mt(rs); - gmp_randseed_ui(rs, seed); - mpz_urandomb(dh->priv_key, rs, num_bytes); - gmp_randclear(rs); - - dh->pub_key = calloc(1, sizeof(*dh->pub_key)); - if (!dh->pub_key) - return 0; - mpz_init2(dh->pub_key, 1); - if (!dh->pub_key) { - mpz_clear(dh->priv_key); - free(dh->priv_key); - return 0; - } - - mpz_powm(dh->pub_key, dh->g, dh->priv_key, dh->p); - - return 1; -} - -static int MDH_compute_key(uint8_t *secret, size_t len, MP_t pub, MDH *dh) -{ - mpz_ptr k; - int num_bytes; - - num_bytes = (mpz_sizeinbase(dh->p, 2) + 7) / 8; - if (num_bytes <= 0 || num_bytes > 18000) - return -1; - - k = calloc(1, sizeof(*k)); - if (!k) - return -1; - mpz_init2(k, 1); - - mpz_powm(k, pub, dh->priv_key, dh->p); - nettle_mpz_get_str_256(len, secret, k); - mpz_clear(k); - free(k); - - /* return the length of the shared secret key like DH_compute_key */ - return len; -} - -#else /* USE_OPENSSL */ -#include -#include - -typedef BIGNUM * MP_t; -#define MP_new(m) m = BN_new() -#define MP_set_w(mpi, w) BN_set_word(mpi, w) -#define MP_cmp(u, v) BN_cmp(u, v) -#define MP_set(u, v) BN_copy(u, v) -#define MP_sub_w(mpi, w) BN_sub_word(mpi, w) -#define MP_cmp_1(mpi) BN_cmp(mpi, BN_value_one()) -#define MP_modexp(r, y, q, p) do {BN_CTX *ctx = BN_CTX_new(); BN_mod_exp(r, y, q, p, ctx); BN_CTX_free(ctx);} while(0) -#define MP_free(mpi) BN_free(mpi) -#define MP_gethex(u, hex, res) res = BN_hex2bn(&u, hex) -#define MP_bytes(u) BN_num_bytes(u) -#define MP_setbin(u,buf,len) BN_bn2bin(u,buf) -#define MP_getbin(u,buf,len) u = BN_bin2bn(buf,len,0) - -#define MDH DH -#define MDH_new() DH_new() -#define MDH_free(dh) DH_free(dh) -#define MDH_generate_key(dh) DH_generate_key(dh) -#define MDH_compute_key(secret, seclen, pub, dh) DH_compute_key(secret, pub, dh) - -#endif - -#include "log.h" -#include "dhgroups.h" - -/* RFC 2631, Section 2.1.5, http://www.ietf.org/rfc/rfc2631.txt */ -static int -isValidPublicKey(MP_t y, MP_t p, MP_t q) -{ - int ret = TRUE; - MP_t bn; - assert(y); - - MP_new(bn); - assert(bn); - - /* y must lie in [2,p-1] */ - MP_set_w(bn, 1); - if (MP_cmp(y, bn) < 0) - { - RTMP_Log(RTMP_LOGERROR, "DH public key must be at least 2"); - ret = FALSE; - goto failed; - } - - /* bn = p-2 */ - MP_set(bn, p); - MP_sub_w(bn, 1); - if (MP_cmp(y, bn) > 0) - { - RTMP_Log(RTMP_LOGERROR, "DH public key must be at most p-2"); - ret = FALSE; - goto failed; - } - - /* Verify with Sophie-Germain prime - * - * This is a nice test to make sure the public key position is calculated - * correctly. This test will fail in about 50% of the cases if applied to - * random data. - */ - if (q) - { - /* y must fulfill y^q mod p = 1 */ - MP_modexp(bn, y, q, p); - - if (MP_cmp_1(bn) != 0) - { - RTMP_Log(RTMP_LOGWARNING, "DH public key does not fulfill y^q mod p = 1"); - } - } - -failed: - MP_free(bn); - return ret; -} - -static MDH * -DHInit(int nKeyBits) -{ - size_t res; - MDH *dh = MDH_new(); - - if (!dh) - goto failed; - - MP_new(dh->g); - - if (!dh->g) - goto failed; - - MP_gethex(dh->p, P1024, res); /* prime P1024, see dhgroups.h */ - if (!res) - { - goto failed; - } - - MP_set_w(dh->g, 2); /* base 2 */ - - dh->length = nKeyBits; - return dh; - -failed: - if (dh) - MDH_free(dh); - - return 0; -} - -static int -DHGenerateKey(MDH *dh) -{ - size_t res = 0; - if (!dh) - return 0; - - while (!res) - { - MP_t q1 = NULL; - - if (!MDH_generate_key(dh)) - return 0; - - MP_gethex(q1, Q1024, res); - assert(res); - - res = isValidPublicKey(dh->pub_key, dh->p, q1); - if (!res) - { - MP_free(dh->pub_key); - MP_free(dh->priv_key); - dh->pub_key = dh->priv_key = 0; - } - - MP_free(q1); - } - return 1; -} - -/* fill pubkey with the public key in BIG ENDIAN order - * 00 00 00 00 00 x1 x2 x3 ..... - */ - -static int -DHGetPublicKey(MDH *dh, uint8_t *pubkey, size_t nPubkeyLen) -{ - int len; - if (!dh || !dh->pub_key) - return 0; - - len = MP_bytes(dh->pub_key); - if (len <= 0 || len > (int) nPubkeyLen) - return 0; - - memset(pubkey, 0, nPubkeyLen); - MP_setbin(dh->pub_key, pubkey + (nPubkeyLen - len), len); - return 1; -} - -#if 0 /* unused */ -static int -DHGetPrivateKey(MDH *dh, uint8_t *privkey, size_t nPrivkeyLen) -{ - if (!dh || !dh->priv_key) - return 0; - - int len = MP_bytes(dh->priv_key); - if (len <= 0 || len > (int) nPrivkeyLen) - return 0; - - memset(privkey, 0, nPrivkeyLen); - MP_setbin(dh->priv_key, privkey + (nPrivkeyLen - len), len); - return 1; -} -#endif - -/* computes the shared secret key from the private MDH value and the - * other party's public key (pubkey) - */ -static int -DHComputeSharedSecretKey(MDH *dh, uint8_t *pubkey, size_t nPubkeyLen, - uint8_t *secret) -{ - MP_t q1 = NULL, pubkeyBn = NULL; - size_t len; - int res; - - if (!dh || !secret || nPubkeyLen >= INT_MAX) - return -1; - - MP_getbin(pubkeyBn, pubkey, nPubkeyLen); - if (!pubkeyBn) - return -1; - - MP_gethex(q1, Q1024, len); - assert(len); - - if (isValidPublicKey(pubkeyBn, dh->p, q1)) - res = MDH_compute_key(secret, nPubkeyLen, pubkeyBn, dh); - else - res = -1; - - MP_free(q1); - MP_free(pubkeyBn); - - return res; -} diff --git a/rtmp/rtmp_c/librtmp/dhgroups.h b/rtmp/rtmp_c/librtmp/dhgroups.h deleted file mode 100644 index 2db3989c..00000000 --- a/rtmp/rtmp_c/librtmp/dhgroups.h +++ /dev/null @@ -1,199 +0,0 @@ -/* librtmp - Diffie-Hellmann Key Exchange - * Copyright (C) 2009 Andrej Stepanchuk - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -/* from RFC 3526, see http://www.ietf.org/rfc/rfc3526.txt */ - -/* 2^768 - 2 ^704 - 1 + 2^64 * { [2^638 pi] + 149686 } */ -#define P768 \ - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ - "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ - "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ - "E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF" - -/* 2^1024 - 2^960 - 1 + 2^64 * { [2^894 pi] + 129093 } */ -#define P1024 \ - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ - "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ - "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ - "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ - "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" \ - "FFFFFFFFFFFFFFFF" - -/* Group morder largest prime factor: */ -#define Q1024 \ - "7FFFFFFFFFFFFFFFE487ED5110B4611A62633145C06E0E68" \ - "948127044533E63A0105DF531D89CD9128A5043CC71A026E" \ - "F7CA8CD9E69D218D98158536F92F8A1BA7F09AB6B6A8E122" \ - "F242DABB312F3F637A262174D31BF6B585FFAE5B7A035BF6" \ - "F71C35FDAD44CFD2D74F9208BE258FF324943328F67329C0" \ - "FFFFFFFFFFFFFFFF" - -/* 2^1536 - 2^1472 - 1 + 2^64 * { [2^1406 pi] + 741804 } */ -#define P1536 \ - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ - "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ - "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ - "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ - "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ - "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ - "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ - "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF" - -/* 2^2048 - 2^1984 - 1 + 2^64 * { [2^1918 pi] + 124476 } */ -#define P2048 \ - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ - "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ - "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ - "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ - "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ - "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ - "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ - "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ - "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ - "15728E5A8AACAA68FFFFFFFFFFFFFFFF" - -/* 2^3072 - 2^3008 - 1 + 2^64 * { [2^2942 pi] + 1690314 } */ -#define P3072 \ - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ - "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ - "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ - "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ - "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ - "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ - "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ - "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ - "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ - "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ - "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ - "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ - "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ - "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ - "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF" - -/* 2^4096 - 2^4032 - 1 + 2^64 * { [2^3966 pi] + 240904 } */ -#define P4096 \ - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ - "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ - "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ - "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ - "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ - "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ - "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ - "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ - "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ - "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ - "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ - "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ - "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ - "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ - "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" \ - "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" \ - "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" \ - "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" \ - "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" \ - "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" \ - "FFFFFFFFFFFFFFFF" - -/* 2^6144 - 2^6080 - 1 + 2^64 * { [2^6014 pi] + 929484 } */ -#define P6144 \ - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ - "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ - "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ - "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ - "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ - "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ - "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ - "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ - "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ - "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ - "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ - "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ - "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ - "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ - "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" \ - "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" \ - "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" \ - "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" \ - "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" \ - "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" \ - "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" \ - "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" \ - "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" \ - "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" \ - "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" \ - "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" \ - "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" \ - "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" \ - "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" \ - "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" \ - "12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF" - -/* 2^8192 - 2^8128 - 1 + 2^64 * { [2^8062 pi] + 4743158 } */ -#define P8192 \ - "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ - "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ - "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ - "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ - "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ - "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ - "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ - "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ - "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ - "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ - "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ - "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ - "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ - "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ - "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ - "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" \ - "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" \ - "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" \ - "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" \ - "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" \ - "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492" \ - "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BD" \ - "F8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831" \ - "179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1B" \ - "DB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF" \ - "5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6" \ - "D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F3" \ - "23A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA" \ - "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE328" \ - "06A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55C" \ - "DA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE" \ - "12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E4" \ - "38777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300" \ - "741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F568" \ - "3423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9" \ - "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B" \ - "4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A" \ - "062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A36" \ - "4597E899A0255DC164F31CC50846851DF9AB48195DED7EA1" \ - "B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F92" \ - "4009438B481C6CD7889A002ED5EE382BC9190DA6FC026E47" \ - "9558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" \ - "60C980DD98EDD3DFFFFFFFFFFFFFFFFF" - diff --git a/rtmp/rtmp_c/librtmp/handshake.h b/rtmp/rtmp_c/librtmp/handshake.h deleted file mode 100644 index 0438486d..00000000 --- a/rtmp/rtmp_c/librtmp/handshake.h +++ /dev/null @@ -1,1419 +0,0 @@ -/* - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * Copyright (C) 2010 2a665470ced7adb7156fcef47f8199a6371c117b8a79e399a2771e0b36384090 - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -/* This file is #included in rtmp.c, it is not meant to be compiled alone */ - -#ifdef USE_POLARSSL -#include -#include -#ifndef SHA256_DIGEST_LENGTH -#define SHA256_DIGEST_LENGTH 32 -#endif -#define HMAC_CTX sha2_context -#define HMAC_setup(ctx, key, len) sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0) -#define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len) -#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; sha2_hmac_finish(&ctx, dig) - -typedef arc4_context * RC4_handle; -#define RC4_alloc(h) *h = malloc(sizeof(arc4_context)) -#define RC4_setkey(h,l,k) arc4_setup(h,k,l) -#define RC4_encrypt(h,l,d) arc4_crypt(h,l,(unsigned char *)d,(unsigned char *)d) -#define RC4_encrypt2(h,l,s,d) arc4_crypt(h,l,(unsigned char *)s,(unsigned char *)d) -#define RC4_free(h) free(h) - -#elif defined(USE_GNUTLS) -#include -#include -#ifndef SHA256_DIGEST_LENGTH -#define SHA256_DIGEST_LENGTH 32 -#endif -#undef HMAC_CTX -#define HMAC_CTX struct hmac_sha256_ctx -#define HMAC_setup(ctx, key, len) hmac_sha256_set_key(&ctx, len, key) -#define HMAC_crunch(ctx, buf, len) hmac_sha256_update(&ctx, len, buf) -#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; hmac_sha256_digest(&ctx, SHA256_DIGEST_LENGTH, dig) -#define HMAC_close(ctx) - -typedef struct arcfour_ctx* RC4_handle; -#define RC4_alloc(h) *h = malloc(sizeof(struct arcfour_ctx)) -#define RC4_setkey(h,l,k) arcfour_set_key(h, l, k) -#define RC4_encrypt(h,l,d) arcfour_crypt(h,l,(uint8_t *)d,(uint8_t *)d) -#define RC4_encrypt2(h,l,s,d) arcfour_crypt(h,l,(uint8_t *)d,(uint8_t *)s) -#define RC4_free(h) free(h) - -#else /* USE_OPENSSL */ -#include -#include -#include -#if OPENSSL_VERSION_NUMBER < 0x0090800 || !defined(SHA256_DIGEST_LENGTH) -#error Your OpenSSL is too old, need 0.9.8 or newer with SHA256 -#endif -#define HMAC_setup(ctx, key, len) HMAC_CTX_init(&ctx); HMAC_Init_ex(&ctx, key, len, EVP_sha256(), 0) -#define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, buf, len) -#define HMAC_finish(ctx, dig, dlen) HMAC_Final(&ctx, dig, &dlen); HMAC_CTX_cleanup(&ctx) - -typedef RC4_KEY * RC4_handle; -#define RC4_alloc(h) *h = malloc(sizeof(RC4_KEY)) -#define RC4_setkey(h,l,k) RC4_set_key(h,l,k) -#define RC4_encrypt(h,l,d) RC4(h,l,(uint8_t *)d,(uint8_t *)d) -#define RC4_encrypt2(h,l,s,d) RC4(h,l,(uint8_t *)s,(uint8_t *)d) -#define RC4_free(h) free(h) -#endif - -#define FP10 - -#include "dh.h" - -static const uint8_t GenuineFMSKey[] = { - 0x47, 0x65, 0x6e, 0x75, 0x69, 0x6e, 0x65, 0x20, 0x41, 0x64, 0x6f, 0x62, - 0x65, 0x20, 0x46, 0x6c, - 0x61, 0x73, 0x68, 0x20, 0x4d, 0x65, 0x64, 0x69, 0x61, 0x20, 0x53, 0x65, - 0x72, 0x76, 0x65, 0x72, - 0x20, 0x30, 0x30, 0x31, /* Genuine Adobe Flash Media Server 001 */ - - 0xf0, 0xee, 0xc2, 0x4a, 0x80, 0x68, 0xbe, 0xe8, 0x2e, 0x00, 0xd0, 0xd1, - 0x02, 0x9e, 0x7e, 0x57, 0x6e, 0xec, 0x5d, 0x2d, 0x29, 0x80, 0x6f, 0xab, - 0x93, 0xb8, 0xe6, 0x36, - 0xcf, 0xeb, 0x31, 0xae -}; /* 68 */ - -static const uint8_t GenuineFPKey[] = { - 0x47, 0x65, 0x6E, 0x75, 0x69, 0x6E, 0x65, 0x20, 0x41, 0x64, 0x6F, 0x62, - 0x65, 0x20, 0x46, 0x6C, - 0x61, 0x73, 0x68, 0x20, 0x50, 0x6C, 0x61, 0x79, 0x65, 0x72, 0x20, 0x30, - 0x30, 0x31, /* Genuine Adobe Flash Player 001 */ - 0xF0, 0xEE, - 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 0x02, 0x9E, - 0x7E, 0x57, 0x6E, 0xEC, - 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, - 0x31, 0xAE -}; /* 62 */ - -static void InitRC4Encryption - (uint8_t * secretKey, - uint8_t * pubKeyIn, - uint8_t * pubKeyOut, RC4_handle *rc4keyIn, RC4_handle *rc4keyOut) -{ - uint8_t digest[SHA256_DIGEST_LENGTH]; - unsigned int digestLen = 0; - HMAC_CTX ctx; - - RC4_alloc(rc4keyIn); - RC4_alloc(rc4keyOut); - - HMAC_setup(ctx, secretKey, 128); - HMAC_crunch(ctx, pubKeyIn, 128); - HMAC_finish(ctx, digest, digestLen); - - RTMP_Log(RTMP_LOGDEBUG, "RC4 Out Key: "); - RTMP_LogHex(RTMP_LOGDEBUG, digest, 16); - - RC4_setkey(*rc4keyOut, 16, digest); - - HMAC_setup(ctx, secretKey, 128); - HMAC_crunch(ctx, pubKeyOut, 128); - HMAC_finish(ctx, digest, digestLen); - - RTMP_Log(RTMP_LOGDEBUG, "RC4 In Key: "); - RTMP_LogHex(RTMP_LOGDEBUG, digest, 16); - - RC4_setkey(*rc4keyIn, 16, digest); -} - -typedef unsigned int (getoff)(uint8_t *buf, unsigned int len); - -static unsigned int -GetDHOffset2(uint8_t *handshake, unsigned int len) -{ - unsigned int offset = 0; - uint8_t *ptr = handshake + 768; - unsigned int res; - - assert(RTMP_SIG_SIZE <= len); - - offset += (*ptr); - ptr++; - offset += (*ptr); - ptr++; - offset += (*ptr); - ptr++; - offset += (*ptr); - - res = (offset % 632) + 8; - - if (res + 128 > 767) - { - RTMP_Log(RTMP_LOGERROR, - "%s: Couldn't calculate correct DH offset (got %d), exiting!", - __FUNCTION__, res); - exit(1); - } - return res; -} - -static unsigned int -GetDigestOffset2(uint8_t *handshake, unsigned int len) -{ - unsigned int offset = 0; - uint8_t *ptr = handshake + 772; - unsigned int res; - - offset += (*ptr); - ptr++; - offset += (*ptr); - ptr++; - offset += (*ptr); - ptr++; - offset += (*ptr); - - res = (offset % 728) + 776; - - if (res + 32 > 1535) - { - RTMP_Log(RTMP_LOGERROR, - "%s: Couldn't calculate correct digest offset (got %d), exiting", - __FUNCTION__, res); - exit(1); - } - return res; -} - -static unsigned int -GetDHOffset1(uint8_t *handshake, unsigned int len) -{ - unsigned int offset = 0; - uint8_t *ptr = handshake + 1532; - unsigned int res; - - assert(RTMP_SIG_SIZE <= len); - - offset += (*ptr); - ptr++; - offset += (*ptr); - ptr++; - offset += (*ptr); - ptr++; - offset += (*ptr); - - res = (offset % 632) + 772; - - if (res + 128 > 1531) - { - RTMP_Log(RTMP_LOGERROR, "%s: Couldn't calculate DH offset (got %d), exiting!", - __FUNCTION__, res); - exit(1); - } - - return res; -} - -static unsigned int -GetDigestOffset1(uint8_t *handshake, unsigned int len) -{ - unsigned int offset = 0; - uint8_t *ptr = handshake + 8; - unsigned int res; - - assert(12 <= len); - - offset += (*ptr); - ptr++; - offset += (*ptr); - ptr++; - offset += (*ptr); - ptr++; - offset += (*ptr); - - res = (offset % 728) + 12; - - if (res + 32 > 771) - { - RTMP_Log(RTMP_LOGERROR, - "%s: Couldn't calculate digest offset (got %d), exiting!", - __FUNCTION__, res); - exit(1); - } - - return res; -} - -static getoff *digoff[] = {GetDigestOffset1, GetDigestOffset2}; -static getoff *dhoff[] = {GetDHOffset1, GetDHOffset2}; - -static void -HMACsha256(const uint8_t *message, size_t messageLen, const uint8_t *key, - size_t keylen, uint8_t *digest) -{ - unsigned int digestLen; - HMAC_CTX ctx; - - HMAC_setup(ctx, key, keylen); - HMAC_crunch(ctx, message, messageLen); - HMAC_finish(ctx, digest, digestLen); - - assert(digestLen == 32); -} - -static void -CalculateDigest(unsigned int digestPos, uint8_t *handshakeMessage, - const uint8_t *key, size_t keyLen, uint8_t *digest) -{ - const int messageLen = RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH; - uint8_t message[RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH]; - - memcpy(message, handshakeMessage, digestPos); - memcpy(message + digestPos, - &handshakeMessage[digestPos + SHA256_DIGEST_LENGTH], - messageLen - digestPos); - - HMACsha256(message, messageLen, key, keyLen, digest); -} - -static int -VerifyDigest(unsigned int digestPos, uint8_t *handshakeMessage, const uint8_t *key, - size_t keyLen) -{ - uint8_t calcDigest[SHA256_DIGEST_LENGTH]; - - CalculateDigest(digestPos, handshakeMessage, key, keyLen, calcDigest); - - return memcmp(&handshakeMessage[digestPos], calcDigest, - SHA256_DIGEST_LENGTH) == 0; -} - -/* handshake - * - * Type = [1 bytes] plain: 0x03, encrypted: 0x06, 0x08, 0x09 - * -------------------------------------------------------------------- [1536 bytes] - * Uptime = [4 bytes] big endian unsigned number, uptime - * Version = [4 bytes] each byte represents a version number, e.g. 9.0.124.0 - * ... - * - */ - -static const uint32_t rtmpe8_keys[16][4] = { - {0xbff034b2, 0x11d9081f, 0xccdfb795, 0x748de732}, - {0x086a5eb6, 0x1743090e, 0x6ef05ab8, 0xfe5a39e2}, - {0x7b10956f, 0x76ce0521, 0x2388a73a, 0x440149a1}, - {0xa943f317, 0xebf11bb2, 0xa691a5ee, 0x17f36339}, - {0x7a30e00a, 0xb529e22c, 0xa087aea5, 0xc0cb79ac}, - {0xbdce0c23, 0x2febdeff, 0x1cfaae16, 0x1123239d}, - {0x55dd3f7b, 0x77e7e62e, 0x9bb8c499, 0xc9481ee4}, - {0x407bb6b4, 0x71e89136, 0xa7aebf55, 0xca33b839}, - {0xfcf6bdc3, 0xb63c3697, 0x7ce4f825, 0x04d959b2}, - {0x28e091fd, 0x41954c4c, 0x7fb7db00, 0xe3a066f8}, - {0x57845b76, 0x4f251b03, 0x46d45bcd, 0xa2c30d29}, - {0x0acceef8, 0xda55b546, 0x03473452, 0x5863713b}, - {0xb82075dc, 0xa75f1fee, 0xd84268e8, 0xa72a44cc}, - {0x07cf6e9e, 0xa16d7b25, 0x9fa7ae6c, 0xd92f5629}, - {0xfeb1eae4, 0x8c8c3ce1, 0x4e0064a7, 0x6a387c2a}, - {0x893a9427, 0xcc3013a2, 0xf106385b, 0xa829f927} -}; - -/* RTMPE type 8 uses XTEA on the regular signature - * http://en.wikipedia.org/wiki/XTEA - */ -static void rtmpe8_sig(uint8_t *in, uint8_t *out, int keyid) -{ - unsigned int i, num_rounds = 32; - uint32_t v0, v1, sum=0, delta=0x9E3779B9; - uint32_t const *k; - - v0 = in[0] | (in[1] << 8) | (in[2] << 16) | (in[3] << 24); - v1 = in[4] | (in[5] << 8) | (in[6] << 16) | (in[7] << 24); - k = rtmpe8_keys[keyid]; - - for (i=0; i < num_rounds; i++) { - v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); - sum += delta; - v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); - } - - out[0] = v0; v0 >>= 8; - out[1] = v0; v0 >>= 8; - out[2] = v0; v0 >>= 8; - out[3] = v0; - - out[4] = v1; v1 >>= 8; - out[5] = v1; v1 >>= 8; - out[6] = v1; v1 >>= 8; - out[7] = v1; -} - -/* RTMPE type 9 uses Blowfish on the regular signature - * http://en.wikipedia.org/wiki/Blowfish_(cipher) - */ -#define BF_ROUNDS 16 -typedef struct bf_key { - uint32_t s[4][256]; - uint32_t p[BF_ROUNDS+2]; -} bf_key; - -static const uint32_t bf_sinit[][256] = { - - /* S-Box 0 */ - { 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, - 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, - 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658, - 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, - 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, - 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, - 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6, - 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, - 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c, - 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, - 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1, - 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, - 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a, - 0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, - 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176, - 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, - 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, - 0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, - 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b, - 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, - 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c, - 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, - 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a, - 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, - 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760, - 0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, - 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8, - 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, - 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, - 0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, - 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0, - 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, - 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, - 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, - 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705, - 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, - 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e, - 0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, - 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9, - 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, - 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, - 0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, - 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a, }, - - /* S-Box 1 */ - { 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, - 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, - 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65, - 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, - 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9, - 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, - 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d, - 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, - 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, - 0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, - 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908, - 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, - 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, - 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, - 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908, - 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, - 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b, - 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, - 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa, - 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, - 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, - 0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, - 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5, - 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, - 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, - 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, - 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca, - 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, - 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77, - 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, - 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054, - 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, - 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, - 0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, - 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646, - 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, - 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, - 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, - 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e, - 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, - 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd, - 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, - 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7, }, - - /* S-Box 2 */ - { 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, - 0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, - 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af, - 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, - 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, - 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, - 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, - 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, - 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, - 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, - 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, - 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, - 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, - 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, - 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60, - 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, - 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, - 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, - 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, - 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, - 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, - 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, - 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, - 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, - 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, - 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, - 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086, - 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, - 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, - 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, - 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, - 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, - 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, - 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, - 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, - 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, - 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, - 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, - 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188, - 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, - 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, - 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, - 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0, }, - - /* S-Box 3 */ - { 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, - 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, - 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, - 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, - 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, - 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, - 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1, - 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, - 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, - 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, - 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6, - 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, - 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, - 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, - 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, - 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, - 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, - 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, - 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd, - 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, - 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, - 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, - 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, - 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, - 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, - 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, - 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, - 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, - 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, - 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, - 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, - 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, - 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, - 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, - 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, - 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, - 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, - 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, - 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, - 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, - 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, - 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, - 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6, }, -}; - -static const uint32_t bf_pinit[] = { - /* P-Box */ - 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, - 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, - 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b, -}; - -#define KEYBYTES 24 - -static const unsigned char rtmpe9_keys[16][KEYBYTES] = { - { 0x79, 0x34, 0x77, 0x4c, 0x67, 0xd1, 0x38, 0x3a, 0xdf, 0xb3, 0x56, 0xbe, - 0x8b, 0x7b, 0xd0, 0x24, 0x38, 0xe0, 0x73, 0x58, 0x41, 0x5d, 0x69, 0x67, }, - { 0x46, 0xf6, 0xb4, 0xcc, 0x01, 0x93, 0xe3, 0xa1, 0x9e, 0x7d, 0x3c, 0x65, - 0x55, 0x86, 0xfd, 0x09, 0x8f, 0xf7, 0xb3, 0xc4, 0x6f, 0x41, 0xca, 0x5c, }, - { 0x1a, 0xe7, 0xe2, 0xf3, 0xf9, 0x14, 0x79, 0x94, 0xc0, 0xd3, 0x97, 0x43, - 0x08, 0x7b, 0xb3, 0x84, 0x43, 0x2f, 0x9d, 0x84, 0x3f, 0x21, 0x01, 0x9b, }, - { 0xd3, 0xe3, 0x54, 0xb0, 0xf7, 0x1d, 0xf6, 0x2b, 0x5a, 0x43, 0x4d, 0x04, - 0x83, 0x64, 0x3e, 0x0d, 0x59, 0x2f, 0x61, 0xcb, 0xb1, 0x6a, 0x59, 0x0d, }, - { 0xc8, 0xc1, 0xe9, 0xb8, 0x16, 0x56, 0x99, 0x21, 0x7b, 0x5b, 0x36, 0xb7, - 0xb5, 0x9b, 0xdf, 0x06, 0x49, 0x2c, 0x97, 0xf5, 0x95, 0x48, 0x85, 0x7e, }, - { 0xeb, 0xe5, 0xe6, 0x2e, 0xa4, 0xba, 0xd4, 0x2c, 0xf2, 0x16, 0xe0, 0x8f, - 0x66, 0x23, 0xa9, 0x43, 0x41, 0xce, 0x38, 0x14, 0x84, 0x95, 0x00, 0x53, }, - { 0x66, 0xdb, 0x90, 0xf0, 0x3b, 0x4f, 0xf5, 0x6f, 0xe4, 0x9c, 0x20, 0x89, - 0x35, 0x5e, 0xd2, 0xb2, 0xc3, 0x9e, 0x9f, 0x7f, 0x63, 0xb2, 0x28, 0x81, }, - { 0xbb, 0x20, 0xac, 0xed, 0x2a, 0x04, 0x6a, 0x19, 0x94, 0x98, 0x9b, 0xc8, - 0xff, 0xcd, 0x93, 0xef, 0xc6, 0x0d, 0x56, 0xa7, 0xeb, 0x13, 0xd9, 0x30, }, - { 0xbc, 0xf2, 0x43, 0x82, 0x09, 0x40, 0x8a, 0x87, 0x25, 0x43, 0x6d, 0xe6, - 0xbb, 0xa4, 0xb9, 0x44, 0x58, 0x3f, 0x21, 0x7c, 0x99, 0xbb, 0x3f, 0x24, }, - { 0xec, 0x1a, 0xaa, 0xcd, 0xce, 0xbd, 0x53, 0x11, 0xd2, 0xfb, 0x83, 0xb6, - 0xc3, 0xba, 0xab, 0x4f, 0x62, 0x79, 0xe8, 0x65, 0xa9, 0x92, 0x28, 0x76, }, - { 0xc6, 0x0c, 0x30, 0x03, 0x91, 0x18, 0x2d, 0x7b, 0x79, 0xda, 0xe1, 0xd5, - 0x64, 0x77, 0x9a, 0x12, 0xc5, 0xb1, 0xd7, 0x91, 0x4f, 0x96, 0x4c, 0xa3, }, - { 0xd7, 0x7c, 0x2a, 0xbf, 0xa6, 0xe7, 0x85, 0x7c, 0x45, 0xad, 0xff, 0x12, - 0x94, 0xd8, 0xde, 0xa4, 0x5c, 0x3d, 0x79, 0xa4, 0x44, 0x02, 0x5d, 0x22, }, - { 0x16, 0x19, 0x0d, 0x81, 0x6a, 0x4c, 0xc7, 0xf8, 0xb8, 0xf9, 0x4e, 0xcd, - 0x2c, 0x9e, 0x90, 0x84, 0xb2, 0x08, 0x25, 0x60, 0xe1, 0x1e, 0xae, 0x18, }, - { 0xe9, 0x7c, 0x58, 0x26, 0x1b, 0x51, 0x9e, 0x49, 0x82, 0x60, 0x61, 0xfc, - 0xa0, 0xa0, 0x1b, 0xcd, 0xf5, 0x05, 0xd6, 0xa6, 0x6d, 0x07, 0x88, 0xa3, }, - { 0x2b, 0x97, 0x11, 0x8b, 0xd9, 0x4e, 0xd9, 0xdf, 0x20, 0xe3, 0x9c, 0x10, - 0xe6, 0xa1, 0x35, 0x21, 0x11, 0xf9, 0x13, 0x0d, 0x0b, 0x24, 0x65, 0xb2, }, - { 0x53, 0x6a, 0x4c, 0x54, 0xac, 0x8b, 0x9b, 0xb8, 0x97, 0x29, 0xfc, 0x60, - 0x2c, 0x5b, 0x3a, 0x85, 0x68, 0xb5, 0xaa, 0x6a, 0x44, 0xcd, 0x3f, 0xa7, }, -}; - -#define BF_ENC(X,S) \ - (((S[0][X>>24] + S[1][X>>16 & 0xff]) ^ S[2][(X>>8) & 0xff]) + S[3][X & 0xff]) - -static void bf_enc(uint32_t *x, bf_key *key) -{ - uint32_t Xl; - uint32_t Xr; - uint32_t temp; - int i; - - Xl = x[0]; - Xr = x[1]; - - for (i = 0; i < BF_ROUNDS; ++i) { - Xl ^= key->p[i]; - Xr ^= BF_ENC(Xl,key->s); - - temp = Xl; - Xl = Xr; - Xr = temp; - } - - Xl ^= key->p[BF_ROUNDS]; - Xr ^= key->p[BF_ROUNDS + 1]; - - x[0] = Xr; - x[1] = Xl; -} - -static void bf_setkey(const unsigned char *kp, int keybytes, bf_key *key) -{ - int i; - int j; - int k; - uint32_t data; - uint32_t d[2]; - - memcpy(key->p, bf_pinit, sizeof(key->p)); - memcpy(key->s, bf_sinit, sizeof(key->s)); - - j = 0; - for (i = 0; i < BF_ROUNDS + 2; ++i) { - data = 0x00000000; - for (k = 0; k < 4; ++k) { - data = (data << 8) | kp[j]; - j = j + 1; - if (j >= keybytes) { - j = 0; - } - } - key->p[i] ^= data; - } - - d[0] = 0x00000000; - d[1] = 0x00000000; - - for (i = 0; i < BF_ROUNDS + 2; i += 2) { - bf_enc(d, key); - - key->p[i] = d[0]; - key->p[i + 1] = d[1]; - } - - for (i = 0; i < 4; ++i) { - for (j = 0; j < 256; j += 2) { - - bf_enc(d, key); - - key->s[i][j] = d[0]; - key->s[i][j + 1] = d[1]; - } - } -} - -static void rtmpe9_sig(uint8_t *in, uint8_t *out, int keyid) -{ - uint32_t d[2]; - bf_key key; - - bf_setkey(rtmpe9_keys[keyid], KEYBYTES, &key); - - /* input is little-endian */ - d[0] = in[0] | (in[1] << 8) | (in[2] << 16) | (in[3] << 24); - d[1] = in[4] | (in[5] << 8) | (in[6] << 16) | (in[7] << 24); - bf_enc(d, &key); - out[0] = d[0] & 0xff; - out[1] = (d[0] >> 8) & 0xff; - out[2] = (d[0] >> 16) & 0xff; - out[3] = (d[0] >> 24) & 0xff; - out[4] = d[1] & 0xff; - out[5] = (d[1] >> 8) & 0xff; - out[6] = (d[1] >> 16) & 0xff; - out[7] = (d[1] >> 24) & 0xff; -} - -static int -HandShake(RTMP * r, int FP9HandShake) -{ - int i, offalg = 0; - int dhposClient = 0; - int digestPosClient = 0; - int encrypted = r->Link.protocol & RTMP_FEATURE_ENC; - - RC4_handle keyIn = 0; - RC4_handle keyOut = 0; - - int32_t *ip; - uint32_t uptime; - - uint8_t clientbuf[RTMP_SIG_SIZE + 4], *clientsig=clientbuf+4; - uint8_t serversig[RTMP_SIG_SIZE], client2[RTMP_SIG_SIZE], *reply; - uint8_t type; - getoff *getdh = NULL, *getdig = NULL; - - if (encrypted || r->Link.SWFSize) - FP9HandShake = TRUE; - else - FP9HandShake = FALSE; - - r->Link.rc4keyIn = r->Link.rc4keyOut = 0; - - if (encrypted) - { - clientsig[-1] = 0x06; /* 0x08 is RTMPE as well */ - offalg = 1; - } - else - clientsig[-1] = 0x03; - - uptime = htonl(RTMP_GetTime()); - memcpy(clientsig, &uptime, 4); - - if (FP9HandShake) - { - /* set version to at least 9.0.115.0 */ - if (encrypted) - { - clientsig[4] = 128; - clientsig[6] = 3; - } - else - { - clientsig[4] = 10; - clientsig[6] = 45; - } - clientsig[5] = 0; - clientsig[7] = 2; - - RTMP_Log(RTMP_LOGDEBUG, "%s: Client type: %02X", __FUNCTION__, clientsig[-1]); - getdig = digoff[offalg]; - getdh = dhoff[offalg]; - } - else - { - memset(&clientsig[4], 0, 4); - } - - /* generate random data */ -#ifdef _DEBUG - memset(clientsig+8, 0, RTMP_SIG_SIZE-8); -#else - ip = (int32_t *)(clientsig+8); - for (i = 2; i < RTMP_SIG_SIZE/4; i++) - *ip++ = rand(); -#endif - - /* set handshake digest */ - if (FP9HandShake) - { - if (encrypted) - { - /* generate Diffie-Hellmann parameters */ - r->Link.dh = DHInit(1024); - if (!r->Link.dh) - { - RTMP_Log(RTMP_LOGERROR, "%s: Couldn't initialize Diffie-Hellmann!", - __FUNCTION__); - return FALSE; - } - - dhposClient = getdh(clientsig, RTMP_SIG_SIZE); - RTMP_Log(RTMP_LOGDEBUG, "%s: DH pubkey position: %d", __FUNCTION__, dhposClient); - - if (!DHGenerateKey(r->Link.dh)) - { - RTMP_Log(RTMP_LOGERROR, "%s: Couldn't generate Diffie-Hellmann public key!", - __FUNCTION__); - return FALSE; - } - - if (!DHGetPublicKey(r->Link.dh, &clientsig[dhposClient], 128)) - { - RTMP_Log(RTMP_LOGERROR, "%s: Couldn't write public key!", __FUNCTION__); - return FALSE; - } - } - - digestPosClient = getdig(clientsig, RTMP_SIG_SIZE); /* reuse this value in verification */ - RTMP_Log(RTMP_LOGDEBUG, "%s: Client digest offset: %d", __FUNCTION__, - digestPosClient); - - CalculateDigest(digestPosClient, clientsig, GenuineFPKey, 30, - &clientsig[digestPosClient]); - - RTMP_Log(RTMP_LOGDEBUG, "%s: Initial client digest: ", __FUNCTION__); - RTMP_LogHex(RTMP_LOGDEBUG, clientsig + digestPosClient, - SHA256_DIGEST_LENGTH); - } - -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, "Clientsig: "); - RTMP_LogHex(RTMP_LOGDEBUG, clientsig, RTMP_SIG_SIZE); -#endif - - if (!WriteN(r, (char *)clientsig-1, RTMP_SIG_SIZE + 1)) - return FALSE; - - if (ReadN(r, (char *)&type, 1) != 1) /* 0x03 or 0x06 */ - return FALSE; - - RTMP_Log(RTMP_LOGDEBUG, "%s: Type Answer : %02X", __FUNCTION__, type); - - if (type != clientsig[-1]) - RTMP_Log(RTMP_LOGWARNING, "%s: Type mismatch: client sent %d, server answered %d", - __FUNCTION__, clientsig[-1], type); - - if (ReadN(r, (char *)serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) - return FALSE; - - /* decode server response */ - memcpy(&uptime, serversig, 4); - uptime = ntohl(uptime); - - RTMP_Log(RTMP_LOGDEBUG, "%s: Server Uptime : %d", __FUNCTION__, uptime); - RTMP_Log(RTMP_LOGDEBUG, "%s: FMS Version : %d.%d.%d.%d", __FUNCTION__, serversig[4], - serversig[5], serversig[6], serversig[7]); - - if (FP9HandShake && type == 3 && !serversig[4]) - FP9HandShake = FALSE; - -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, "Server signature:"); - RTMP_LogHex(RTMP_LOGDEBUG, serversig, RTMP_SIG_SIZE); -#endif - - if (FP9HandShake) - { - uint8_t digestResp[SHA256_DIGEST_LENGTH]; - uint8_t *signatureResp = NULL; - - /* we have to use this signature now to find the correct algorithms for getting the digest and DH positions */ - int digestPosServer = getdig(serversig, RTMP_SIG_SIZE); - - if (!VerifyDigest(digestPosServer, serversig, GenuineFMSKey, 36)) - { - RTMP_Log(RTMP_LOGWARNING, "Trying different position for server digest!"); - offalg ^= 1; - getdig = digoff[offalg]; - getdh = dhoff[offalg]; - digestPosServer = getdig(serversig, RTMP_SIG_SIZE); - - if (!VerifyDigest(digestPosServer, serversig, GenuineFMSKey, 36)) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't verify the server digest"); /* continuing anyway will probably fail */ - return FALSE; - } - } - - /* generate SWFVerification token (SHA256 HMAC hash of decompressed SWF, key are the last 32 bytes of the server handshake) */ - if (r->Link.SWFSize) - { - const char swfVerify[] = { 0x01, 0x01 }; - char *vend = r->Link.SWFVerificationResponse+sizeof(r->Link.SWFVerificationResponse); - - memcpy(r->Link.SWFVerificationResponse, swfVerify, 2); - AMF_EncodeInt32(&r->Link.SWFVerificationResponse[2], vend, r->Link.SWFSize); - AMF_EncodeInt32(&r->Link.SWFVerificationResponse[6], vend, r->Link.SWFSize); - HMACsha256(r->Link.SWFHash, SHA256_DIGEST_LENGTH, - &serversig[RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH], - SHA256_DIGEST_LENGTH, - (uint8_t *)&r->Link.SWFVerificationResponse[10]); - } - - /* do Diffie-Hellmann Key exchange for encrypted RTMP */ - if (encrypted) - { - /* compute secret key */ - uint8_t secretKey[128] = { 0 }; - int len, dhposServer; - - dhposServer = getdh(serversig, RTMP_SIG_SIZE); - RTMP_Log(RTMP_LOGDEBUG, "%s: Server DH public key offset: %d", __FUNCTION__, - dhposServer); - len = DHComputeSharedSecretKey(r->Link.dh, &serversig[dhposServer], - 128, secretKey); - if (len < 0) - { - RTMP_Log(RTMP_LOGDEBUG, "%s: Wrong secret key position!", __FUNCTION__); - return FALSE; - } - - RTMP_Log(RTMP_LOGDEBUG, "%s: Secret key: ", __FUNCTION__); - RTMP_LogHex(RTMP_LOGDEBUG, secretKey, 128); - - InitRC4Encryption(secretKey, - (uint8_t *) & serversig[dhposServer], - (uint8_t *) & clientsig[dhposClient], - &keyIn, &keyOut); - } - - - reply = client2; -#ifdef _DEBUG - memset(reply, 0xff, RTMP_SIG_SIZE); -#else - ip = (int32_t *)reply; - for (i = 0; i < RTMP_SIG_SIZE/4; i++) - *ip++ = rand(); -#endif - /* calculate response now */ - signatureResp = reply+RTMP_SIG_SIZE-SHA256_DIGEST_LENGTH; - - HMACsha256(&serversig[digestPosServer], SHA256_DIGEST_LENGTH, - GenuineFPKey, sizeof(GenuineFPKey), digestResp); - HMACsha256(reply, RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH, digestResp, - SHA256_DIGEST_LENGTH, signatureResp); - - /* some info output */ - RTMP_Log(RTMP_LOGDEBUG, - "%s: Calculated digest key from secure key and server digest: ", - __FUNCTION__); - RTMP_LogHex(RTMP_LOGDEBUG, digestResp, SHA256_DIGEST_LENGTH); - -#ifdef FP10 - if (type == 8 ) - { - uint8_t *dptr = digestResp; - uint8_t *sig = signatureResp; - /* encrypt signatureResp */ - for (i=0; iLink.rc4keyIn = keyIn; - r->Link.rc4keyOut = keyOut; - - - /* update the keystreams */ - if (r->Link.rc4keyIn) - { - RC4_encrypt(r->Link.rc4keyIn, RTMP_SIG_SIZE, (uint8_t *) buff); - } - - if (r->Link.rc4keyOut) - { - RC4_encrypt(r->Link.rc4keyOut, RTMP_SIG_SIZE, (uint8_t *) buff); - } - } - } - else - { - if (memcmp(serversig, clientsig, RTMP_SIG_SIZE) != 0) - { - RTMP_Log(RTMP_LOGWARNING, "%s: client signature does not match!", - __FUNCTION__); - } - } - - RTMP_Log(RTMP_LOGDEBUG, "%s: Handshaking finished....", __FUNCTION__); - return TRUE; -} - -static int -SHandShake(RTMP * r) -{ - int i, offalg = 0; - int dhposServer = 0; - int digestPosServer = 0; - RC4_handle keyIn = 0; - RC4_handle keyOut = 0; - int FP9HandShake = FALSE; - int encrypted; - int32_t *ip; - - uint8_t clientsig[RTMP_SIG_SIZE]; - uint8_t serverbuf[RTMP_SIG_SIZE + 4], *serversig = serverbuf+4; - uint8_t type; - uint32_t uptime; - getoff *getdh = NULL, *getdig = NULL; - - if (ReadN(r, (char *)&type, 1) != 1) /* 0x03 or 0x06 */ - return FALSE; - - if (ReadN(r, (char *)clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) - return FALSE; - - RTMP_Log(RTMP_LOGDEBUG, "%s: Type Requested : %02X", __FUNCTION__, type); - RTMP_LogHex(RTMP_LOGDEBUG2, clientsig, RTMP_SIG_SIZE); - - if (type == 3) - { - encrypted = FALSE; - } - else if (type == 6 || type == 8) - { - offalg = 1; - encrypted = TRUE; - FP9HandShake = TRUE; - r->Link.protocol |= RTMP_FEATURE_ENC; - /* use FP10 if client is capable */ - if (clientsig[4] == 128) - type = 8; - } - else - { - RTMP_Log(RTMP_LOGERROR, "%s: Unknown version %02x", - __FUNCTION__, type); - return FALSE; - } - - if (!FP9HandShake && clientsig[4]) - FP9HandShake = TRUE; - - serversig[-1] = type; - - r->Link.rc4keyIn = r->Link.rc4keyOut = 0; - - uptime = htonl(RTMP_GetTime()); - memcpy(serversig, &uptime, 4); - - if (FP9HandShake) - { - /* Server version */ - serversig[4] = 3; - serversig[5] = 5; - serversig[6] = 1; - serversig[7] = 1; - - getdig = digoff[offalg]; - getdh = dhoff[offalg]; - } - else - { - memset(&serversig[4], 0, 4); - } - - /* generate random data */ -#ifdef _DEBUG - memset(serversig+8, 0, RTMP_SIG_SIZE-8); -#else - ip = (int32_t *)(serversig+8); - for (i = 2; i < RTMP_SIG_SIZE/4; i++) - *ip++ = rand(); -#endif - - /* set handshake digest */ - if (FP9HandShake) - { - if (encrypted) - { - /* generate Diffie-Hellmann parameters */ - r->Link.dh = DHInit(1024); - if (!r->Link.dh) - { - RTMP_Log(RTMP_LOGERROR, "%s: Couldn't initialize Diffie-Hellmann!", - __FUNCTION__); - return FALSE; - } - - dhposServer = getdh(serversig, RTMP_SIG_SIZE); - RTMP_Log(RTMP_LOGDEBUG, "%s: DH pubkey position: %d", __FUNCTION__, dhposServer); - - if (!DHGenerateKey(r->Link.dh)) - { - RTMP_Log(RTMP_LOGERROR, "%s: Couldn't generate Diffie-Hellmann public key!", - __FUNCTION__); - return FALSE; - } - - if (!DHGetPublicKey - (r->Link.dh, (uint8_t *) &serversig[dhposServer], 128)) - { - RTMP_Log(RTMP_LOGERROR, "%s: Couldn't write public key!", __FUNCTION__); - return FALSE; - } - } - - digestPosServer = getdig(serversig, RTMP_SIG_SIZE); /* reuse this value in verification */ - RTMP_Log(RTMP_LOGDEBUG, "%s: Server digest offset: %d", __FUNCTION__, - digestPosServer); - - CalculateDigest(digestPosServer, serversig, GenuineFMSKey, 36, - &serversig[digestPosServer]); - - RTMP_Log(RTMP_LOGDEBUG, "%s: Initial server digest: ", __FUNCTION__); - RTMP_LogHex(RTMP_LOGDEBUG, serversig + digestPosServer, - SHA256_DIGEST_LENGTH); - } - - RTMP_Log(RTMP_LOGDEBUG2, "Serversig: "); - RTMP_LogHex(RTMP_LOGDEBUG2, serversig, RTMP_SIG_SIZE); - - if (!WriteN(r, (char *)serversig-1, RTMP_SIG_SIZE + 1)) - return FALSE; - - /* decode client response */ - memcpy(&uptime, clientsig, 4); - uptime = ntohl(uptime); - - RTMP_Log(RTMP_LOGDEBUG, "%s: Client Uptime : %d", __FUNCTION__, uptime); - RTMP_Log(RTMP_LOGDEBUG, "%s: Player Version: %d.%d.%d.%d", __FUNCTION__, clientsig[4], - clientsig[5], clientsig[6], clientsig[7]); - - if (FP9HandShake) - { - uint8_t digestResp[SHA256_DIGEST_LENGTH]; - uint8_t *signatureResp = NULL; - - /* we have to use this signature now to find the correct algorithms for getting the digest and DH positions */ - int digestPosClient = getdig(clientsig, RTMP_SIG_SIZE); - - if (!VerifyDigest(digestPosClient, clientsig, GenuineFPKey, 30)) - { - RTMP_Log(RTMP_LOGWARNING, "Trying different position for client digest!"); - offalg ^= 1; - getdig = digoff[offalg]; - getdh = dhoff[offalg]; - - digestPosClient = getdig(clientsig, RTMP_SIG_SIZE); - - if (!VerifyDigest(digestPosClient, clientsig, GenuineFPKey, 30)) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't verify the client digest"); /* continuing anyway will probably fail */ - return FALSE; - } - } - - /* generate SWFVerification token (SHA256 HMAC hash of decompressed SWF, key are the last 32 bytes of the server handshake) */ - if (r->Link.SWFSize) - { - const char swfVerify[] = { 0x01, 0x01 }; - char *vend = r->Link.SWFVerificationResponse+sizeof(r->Link.SWFVerificationResponse); - - memcpy(r->Link.SWFVerificationResponse, swfVerify, 2); - AMF_EncodeInt32(&r->Link.SWFVerificationResponse[2], vend, r->Link.SWFSize); - AMF_EncodeInt32(&r->Link.SWFVerificationResponse[6], vend, r->Link.SWFSize); - HMACsha256(r->Link.SWFHash, SHA256_DIGEST_LENGTH, - &serversig[RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH], - SHA256_DIGEST_LENGTH, - (uint8_t *)&r->Link.SWFVerificationResponse[10]); - } - - /* do Diffie-Hellmann Key exchange for encrypted RTMP */ - if (encrypted) - { - int dhposClient, len; - /* compute secret key */ - uint8_t secretKey[128] = { 0 }; - - dhposClient = getdh(clientsig, RTMP_SIG_SIZE); - RTMP_Log(RTMP_LOGDEBUG, "%s: Client DH public key offset: %d", __FUNCTION__, - dhposClient); - len = - DHComputeSharedSecretKey(r->Link.dh, - (uint8_t *) &clientsig[dhposClient], 128, - secretKey); - if (len < 0) - { - RTMP_Log(RTMP_LOGDEBUG, "%s: Wrong secret key position!", __FUNCTION__); - return FALSE; - } - - RTMP_Log(RTMP_LOGDEBUG, "%s: Secret key: ", __FUNCTION__); - RTMP_LogHex(RTMP_LOGDEBUG, secretKey, 128); - - InitRC4Encryption(secretKey, - (uint8_t *) &clientsig[dhposClient], - (uint8_t *) &serversig[dhposServer], - &keyIn, &keyOut); - } - - - /* calculate response now */ - signatureResp = clientsig+RTMP_SIG_SIZE-SHA256_DIGEST_LENGTH; - - HMACsha256(&clientsig[digestPosClient], SHA256_DIGEST_LENGTH, - GenuineFMSKey, sizeof(GenuineFMSKey), digestResp); - HMACsha256(clientsig, RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH, digestResp, - SHA256_DIGEST_LENGTH, signatureResp); -#ifdef FP10 - if (type == 8 ) - { - uint8_t *dptr = digestResp; - uint8_t *sig = signatureResp; - /* encrypt signatureResp */ - for (i=0; iLink.rc4keyIn = keyIn; - r->Link.rc4keyOut = keyOut; - - /* update the keystreams */ - if (r->Link.rc4keyIn) - { - RC4_encrypt(r->Link.rc4keyIn, RTMP_SIG_SIZE, (uint8_t *) buff); - } - - if (r->Link.rc4keyOut) - { - RC4_encrypt(r->Link.rc4keyOut, RTMP_SIG_SIZE, (uint8_t *) buff); - } - } - } - else - { - if (memcmp(serversig, clientsig, RTMP_SIG_SIZE) != 0) - { - RTMP_Log(RTMP_LOGWARNING, "%s: client signature does not match!", - __FUNCTION__); - } - } - - RTMP_Log(RTMP_LOGDEBUG, "%s: Handshaking finished....", __FUNCTION__); - return TRUE; -} diff --git a/rtmp/rtmp_c/librtmp/hashswf.c b/rtmp/rtmp_c/librtmp/hashswf.c deleted file mode 100644 index 9f4e2c02..00000000 --- a/rtmp/rtmp_c/librtmp/hashswf.c +++ /dev/null @@ -1,665 +0,0 @@ -/* - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#include -#include -#include -#include -#include - -#include "rtmp_sys.h" -#include "log.h" -#include "http.h" - -#ifdef CRYPTO -#ifdef USE_POLARSSL -#include -#ifndef SHA256_DIGEST_LENGTH -#define SHA256_DIGEST_LENGTH 32 -#endif -#define HMAC_CTX sha2_context -#define HMAC_setup(ctx, key, len) sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0) -#define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len) -#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; sha2_hmac_finish(&ctx, dig) -#define HMAC_close(ctx) -#elif defined(USE_GNUTLS) -#include -#ifndef SHA256_DIGEST_LENGTH -#define SHA256_DIGEST_LENGTH 32 -#endif -#undef HMAC_CTX -#define HMAC_CTX struct hmac_sha256_ctx -#define HMAC_setup(ctx, key, len) hmac_sha256_set_key(&ctx, len, key) -#define HMAC_crunch(ctx, buf, len) hmac_sha256_update(&ctx, len, buf) -#define HMAC_finish(ctx, dig, dlen) dlen = SHA256_DIGEST_LENGTH; hmac_sha256_digest(&ctx, SHA256_DIGEST_LENGTH, dig) -#define HMAC_close(ctx) -#else /* USE_OPENSSL */ -#include -#include -#include -#include -#define HMAC_setup(ctx, key, len) HMAC_CTX_init(&ctx); HMAC_Init_ex(&ctx, (unsigned char *)key, len, EVP_sha256(), 0) -#define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, (unsigned char *)buf, len) -#define HMAC_finish(ctx, dig, dlen) HMAC_Final(&ctx, (unsigned char *)dig, &dlen); -#define HMAC_close(ctx) HMAC_CTX_cleanup(&ctx) -#endif - -extern void RTMP_TLS_Init(); -extern TLS_CTX RTMP_TLS_ctx; - -#include - -#endif /* CRYPTO */ - -#define AGENT "Mozilla/5.0" - -HTTPResult -HTTP_get(struct HTTP_ctx *http, const char *url, HTTP_read_callback *cb) -{ - char *host, *path; - char *p1, *p2; - char hbuf[256]; - int port = 80; -#ifdef CRYPTO - int ssl = 0; -#endif - int hlen, flen = 0; - int rc, i; - int len_known; - HTTPResult ret = HTTPRES_OK; - struct sockaddr_in sa; - RTMPSockBuf sb = {0}; - - http->status = -1; - - memset(&sa, 0, sizeof(struct sockaddr_in)); - sa.sin_family = AF_INET; - - /* we only handle http here */ - if (strncasecmp(url, "http", 4)) - return HTTPRES_BAD_REQUEST; - - if (url[4] == 's') - { -#ifdef CRYPTO - ssl = 1; - port = 443; - if (!RTMP_TLS_ctx) - RTMP_TLS_Init(); -#else - return HTTPRES_BAD_REQUEST; -#endif - } - - p1 = strchr(url + 4, ':'); - if (!p1 || strncmp(p1, "://", 3)) - return HTTPRES_BAD_REQUEST; - - host = p1 + 3; - path = strchr(host, '/'); - hlen = path - host; - strncpy(hbuf, host, hlen); - hbuf[hlen] = '\0'; - host = hbuf; - p1 = strrchr(host, ':'); - if (p1) - { - *p1++ = '\0'; - port = atoi(p1); - } - - sa.sin_addr.s_addr = inet_addr(host); - if (sa.sin_addr.s_addr == INADDR_NONE) - { - struct hostent *hp = gethostbyname(host); - if (!hp || !hp->h_addr) - return HTTPRES_LOST_CONNECTION; - sa.sin_addr = *(struct in_addr *)hp->h_addr; - } - sa.sin_port = htons(port); - sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sb.sb_socket == -1) - return HTTPRES_LOST_CONNECTION; - i = - sprintf(sb.sb_buf, - "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s\r\nReferer: %.*s\r\n", - path, AGENT, host, (int)(path - url + 1), url); - if (http->date[0]) - i += sprintf(sb.sb_buf + i, "If-Modified-Since: %s\r\n", http->date); - i += sprintf(sb.sb_buf + i, "\r\n"); - - if (connect - (sb.sb_socket, (struct sockaddr *)&sa, sizeof(struct sockaddr)) < 0) - { - ret = HTTPRES_LOST_CONNECTION; - goto leave; - } -#ifdef CRYPTO - if (ssl) - { -#ifdef NO_SSL - RTMP_Log(RTMP_LOGERROR, "%s, No SSL/TLS support", __FUNCTION__); - ret = HTTPRES_BAD_REQUEST; - goto leave; -#else - TLS_client(RTMP_TLS_ctx, sb.sb_ssl); - TLS_setfd(sb.sb_ssl, sb.sb_socket); - if (TLS_connect(sb.sb_ssl) < 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__); - ret = HTTPRES_LOST_CONNECTION; - goto leave; - } -#endif - } -#endif - RTMPSockBuf_Send(&sb, sb.sb_buf, i); - - /* set timeout */ -#define HTTP_TIMEOUT 5 - { - SET_RCVTIMEO(tv, HTTP_TIMEOUT); - if (setsockopt - (sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv))) - { - RTMP_Log(RTMP_LOGERROR, "%s, Setting socket timeout to %ds failed!", - __FUNCTION__, HTTP_TIMEOUT); - } - } - - sb.sb_size = 0; - sb.sb_timedout = FALSE; - if (RTMPSockBuf_Fill(&sb) < 1) - { - ret = HTTPRES_LOST_CONNECTION; - goto leave; - } - if (strncmp(sb.sb_buf, "HTTP/1", 6)) - { - ret = HTTPRES_BAD_REQUEST; - goto leave; - } - - p1 = strchr(sb.sb_buf, ' '); - rc = atoi(p1 + 1); - http->status = rc; - - if (rc >= 300) - { - if (rc == 304) - { - ret = HTTPRES_OK_NOT_MODIFIED; - goto leave; - } - else if (rc == 404) - ret = HTTPRES_NOT_FOUND; - else if (rc >= 500) - ret = HTTPRES_SERVER_ERROR; - else if (rc >= 400) - ret = HTTPRES_BAD_REQUEST; - else - ret = HTTPRES_REDIRECTED; - } - - p1 = memchr(sb.sb_buf, '\n', sb.sb_size); - if (!p1) - { - ret = HTTPRES_BAD_REQUEST; - 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') - { - sb.sb_start += 2; - sb.sb_size -= 2; - break; - } - 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)) - { - *p2 = '\0'; - strcpy(http->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 = HTTPRES_LOST_CONNECTION; - goto leave; - } - } - } - - len_known = flen > 0; - while ((!len_known || flen > 0) && - (sb.sb_size > 0 || RTMPSockBuf_Fill(&sb) > 0)) - { - cb(sb.sb_start, 1, sb.sb_size, http->data); - if (len_known) - flen -= sb.sb_size; - http->size += sb.sb_size; - sb.sb_size = 0; - } - - if (flen > 0) - ret = HTTPRES_LOST_CONNECTION; - -leave: - RTMPSockBuf_Close(&sb); - return ret; -} - -#ifdef CRYPTO - -#define CHUNK 16384 - -struct info -{ - z_stream *zs; - HMAC_CTX ctx; - int first; - int zlib; - int size; -}; - -static size_t -swfcrunch(void *ptr, size_t size, size_t nmemb, void *stream) -{ - struct info *i = stream; - char *p = ptr; - size_t len = size * nmemb; - - if (i->first) - { - i->first = 0; - /* compressed? */ - if (!strncmp(p, "CWS", 3)) - { - *p = 'F'; - i->zlib = 1; - } - HMAC_crunch(i->ctx, (unsigned char *)p, 8); - p += 8; - len -= 8; - i->size = 8; - } - - if (i->zlib) - { - unsigned char out[CHUNK]; - i->zs->next_in = (unsigned char *)p; - i->zs->avail_in = len; - do - { - i->zs->avail_out = CHUNK; - i->zs->next_out = out; - inflate(i->zs, Z_NO_FLUSH); - len = CHUNK - i->zs->avail_out; - i->size += len; - HMAC_crunch(i->ctx, out, len); - } - while (i->zs->avail_out == 0); - } - else - { - i->size += len; - HMAC_crunch(i->ctx, (unsigned char *)p, len); - } - return size * nmemb; -} - -static int tzoff; -static int tzchecked; - -#define JAN02_1980 318340800 - -static const char *monthtab[12] = { "Jan", "Feb", "Mar", - "Apr", "May", "Jun", - "Jul", "Aug", "Sep", - "Oct", "Nov", "Dec" -}; -static const char *days[] = - { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; - -/* Parse an HTTP datestamp into Unix time */ -static time_t -make_unix_time(char *s) -{ - struct tm time; - int i, ysub = 1900, fmt = 0; - char *month; - char *n; - time_t res; - - if (s[3] != ' ') - { - fmt = 1; - if (s[3] != ',') - ysub = 0; - } - for (n = s; *n; ++n) - if (*n == '-' || *n == ':') - *n = ' '; - - time.tm_mon = 0; - n = strchr(s, ' '); - if (fmt) - { - /* Day, DD-MMM-YYYY HH:MM:SS GMT */ - time.tm_mday = strtol(n + 1, &n, 0); - month = n + 1; - n = strchr(month, ' '); - time.tm_year = strtol(n + 1, &n, 0); - time.tm_hour = strtol(n + 1, &n, 0); - time.tm_min = strtol(n + 1, &n, 0); - time.tm_sec = strtol(n + 1, NULL, 0); - } - else - { - /* Unix ctime() format. Does not conform to HTTP spec. */ - /* Day MMM DD HH:MM:SS YYYY */ - month = n + 1; - n = strchr(month, ' '); - while (isspace(*n)) - n++; - time.tm_mday = strtol(n, &n, 0); - time.tm_hour = strtol(n + 1, &n, 0); - time.tm_min = strtol(n + 1, &n, 0); - time.tm_sec = strtol(n + 1, &n, 0); - time.tm_year = strtol(n + 1, NULL, 0); - } - if (time.tm_year > 100) - time.tm_year -= ysub; - - for (i = 0; i < 12; i++) - if (!strncasecmp(month, monthtab[i], 3)) - { - time.tm_mon = i; - break; - } - time.tm_isdst = 0; /* daylight saving is never in effect in GMT */ - - /* this is normally the value of extern int timezone, but some - * braindead C libraries don't provide it. - */ - if (!tzchecked) - { - struct tm *tc; - time_t then = JAN02_1980; - tc = localtime(&then); - tzoff = (12 - tc->tm_hour) * 3600 + tc->tm_min * 60 + tc->tm_sec; - tzchecked = 1; - } - res = mktime(&time); - /* Unfortunately, mktime() assumes the input is in local time, - * not GMT, so we have to correct it here. - */ - if (res != -1) - res += tzoff; - return res; -} - -/* Convert a Unix time to a network time string - * Weekday, DD-MMM-YYYY HH:MM:SS GMT - */ -static void -strtime(time_t * t, char *s) -{ - struct tm *tm; - - tm = gmtime((time_t *) t); - sprintf(s, "%s, %02d %s %d %02d:%02d:%02d GMT", - days[tm->tm_wday], tm->tm_mday, monthtab[tm->tm_mon], - tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec); -} - -#define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf)) - -int -RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, - int age) -{ - FILE *f = NULL; - char *path, date[64], cctim[64]; - long pos = 0; - time_t ctim = -1, cnow; - int i, got = 0, ret = 0; - unsigned int hlen; - struct info in = { 0 }; - struct HTTP_ctx http = { 0 }; - HTTPResult httpres; - z_stream zs = { 0 }; - AVal home, hpre; - - date[0] = '\0'; -#ifdef _WIN32 -#ifdef XBMC4XBOX - hpre.av_val = "Q:"; - hpre.av_len = 2; - home.av_val = "\\UserData"; -#else - hpre.av_val = getenv("HOMEDRIVE"); - hpre.av_len = strlen(hpre.av_val); - home.av_val = getenv("HOMEPATH"); -#endif -#define DIRSEP "\\" - -#else /* !_WIN32 */ - hpre.av_val = ""; - hpre.av_len = 0; - home.av_val = getenv("HOME"); -#define DIRSEP "/" -#endif - if (!home.av_val) - home.av_val = "."; - home.av_len = strlen(home.av_val); - - /* SWF hash info is cached in a fixed-format file. - * url: - * ctim: HTTP datestamp of when we last checked it. - * date: HTTP datestamp of the SWF's last modification. - * size: SWF size in hex - * hash: SWF hash in hex - * - * These fields must be present in this order. All fields - * besides URL are fixed size. - */ - path = malloc(hpre.av_len + home.av_len + sizeof(DIRSEP ".swfinfo")); - sprintf(path, "%s%s" DIRSEP ".swfinfo", hpre.av_val, home.av_val); - - f = fopen(path, "r+"); - while (f) - { - char buf[4096], *file, *p; - - file = strchr(url, '/'); - if (!file) - break; - file += 2; - file = strchr(file, '/'); - if (!file) - break; - file++; - hlen = file - url; - p = strrchr(file, '/'); - if (p) - file = p; - else - file--; - - while (fgets(buf, sizeof(buf), f)) - { - char *r1; - - got = 0; - - if (strncmp(buf, "url: ", 5)) - continue; - if (strncmp(buf + 5, url, hlen)) - continue; - r1 = strrchr(buf, '/'); - i = strlen(r1); - r1[--i] = '\0'; - if (strncmp(r1, file, i)) - continue; - pos = ftell(f); - while (got < 4 && fgets(buf, sizeof(buf), f)) - { - if (!strncmp(buf, "size: ", 6)) - { - *size = strtol(buf + 6, NULL, 16); - got++; - } - else if (!strncmp(buf, "hash: ", 6)) - { - unsigned char *ptr = hash, *in = (unsigned char *)buf + 6; - int l = strlen((char *)in) - 1; - for (i = 0; i < l; i += 2) - *ptr++ = (HEX2BIN(in[i]) << 4) | HEX2BIN(in[i + 1]); - got++; - } - else if (!strncmp(buf, "date: ", 6)) - { - buf[strlen(buf) - 1] = '\0'; - strncpy(date, buf + 6, sizeof(date)); - got++; - } - else if (!strncmp(buf, "ctim: ", 6)) - { - buf[strlen(buf) - 1] = '\0'; - ctim = make_unix_time(buf + 6); - got++; - } - else if (!strncmp(buf, "url: ", 5)) - break; - } - break; - } - break; - } - - cnow = time(NULL); - /* If we got a cache time, see if it's young enough to use directly */ - if (age && ctim > 0) - { - ctim = cnow - ctim; - ctim /= 3600 * 24; /* seconds to days */ - if (ctim < age) /* ok, it's new enough */ - goto out; - } - - in.first = 1; - HMAC_setup(in.ctx, "Genuine Adobe Flash Player 001", 30); - inflateInit(&zs); - in.zs = &zs; - - http.date = date; - http.data = ∈ - - httpres = HTTP_get(&http, url, swfcrunch); - - inflateEnd(&zs); - - if (httpres != HTTPRES_OK && httpres != HTTPRES_OK_NOT_MODIFIED) - { - ret = -1; - if (httpres == HTTPRES_LOST_CONNECTION) - RTMP_Log(RTMP_LOGERROR, "%s: connection lost while downloading swfurl %s", - __FUNCTION__, url); - else if (httpres == HTTPRES_NOT_FOUND) - RTMP_Log(RTMP_LOGERROR, "%s: swfurl %s not found", __FUNCTION__, url); - else - RTMP_Log(RTMP_LOGERROR, "%s: couldn't contact swfurl %s (HTTP error %d)", - __FUNCTION__, url, http.status); - } - else - { - if (got && pos) - fseek(f, pos, SEEK_SET); - else - { - char *q; - if (!f) - f = fopen(path, "w"); - if (!f) - { - int err = errno; - RTMP_Log(RTMP_LOGERROR, - "%s: couldn't open %s for writing, errno %d (%s)", - __FUNCTION__, path, err, strerror(err)); - ret = -1; - goto out; - } - fseek(f, 0, SEEK_END); - q = strchr(url, '?'); - if (q) - i = q - url; - else - i = strlen(url); - - fprintf(f, "url: %.*s\n", i, url); - } - strtime(&cnow, cctim); - fprintf(f, "ctim: %s\n", cctim); - - if (!in.first) - { - HMAC_finish(in.ctx, hash, hlen); - *size = in.size; - - fprintf(f, "date: %s\n", date); - fprintf(f, "size: %08x\n", in.size); - fprintf(f, "hash: "); - for (i = 0; i < SHA256_DIGEST_LENGTH; i++) - fprintf(f, "%02x", hash[i]); - fprintf(f, "\n"); - } - } - HMAC_close(in.ctx); -out: - free(path); - if (f) - fclose(f); - return ret; -} -#else -int -RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, - int age) -{ - return -1; -} -#endif diff --git a/rtmp/rtmp_c/librtmp/http.h b/rtmp/rtmp_c/librtmp/http.h deleted file mode 100644 index cf3d9039..00000000 --- a/rtmp/rtmp_c/librtmp/http.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef __RTMP_HTTP_H__ -#define __RTMP_HTTP_H__ -/* - * Copyright (C) 2010 Howard Chu - * Copyright (C) 2010 Antti Ajanki - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -typedef enum { - HTTPRES_OK, /* result OK */ - HTTPRES_OK_NOT_MODIFIED, /* not modified since last request */ - HTTPRES_NOT_FOUND, /* not found */ - HTTPRES_BAD_REQUEST, /* client error */ - HTTPRES_SERVER_ERROR, /* server reported an error */ - HTTPRES_REDIRECTED, /* resource has been moved */ - HTTPRES_LOST_CONNECTION /* connection lost while waiting for data */ -} HTTPResult; - -struct HTTP_ctx { - char *date; - int size; - int status; - void *data; -}; - -typedef size_t (HTTP_read_callback)(void *ptr, size_t size, size_t nmemb, void *stream); - -HTTPResult HTTP_get(struct HTTP_ctx *http, const char *url, HTTP_read_callback *cb); - -#endif diff --git a/rtmp/rtmp_c/librtmp/librtmp.3 b/rtmp/rtmp_c/librtmp/librtmp.3 deleted file mode 100644 index 7c424aa7..00000000 --- a/rtmp/rtmp_c/librtmp/librtmp.3 +++ /dev/null @@ -1,210 +0,0 @@ -.TH LIBRTMP 3 "2011-07-20" "RTMPDump v2.4" -.\" Copyright 2011 Howard Chu. -.\" Copying permitted according to the GNU General Public License V2. -.SH NAME -librtmp \- RTMPDump Real-Time Messaging Protocol API -.SH LIBRARY -RTMPDump RTMP (librtmp, -lrtmp) -.SH SYNOPSIS -.B #include -.SH DESCRIPTION -The Real-Time Messaging Protocol (RTMP) is used for streaming -multimedia content across a TCP/IP network. This API provides most client -functions and a few server functions needed to support RTMP, RTMP tunneled -in HTTP (RTMPT), encrypted RTMP (RTMPE), RTMP over SSL/TLS (RTMPS) and -tunneled variants of these encrypted types (RTMPTE, RTMPTS). The basic -RTMP specification has been published by Adobe but this API was -reverse-engineered without use of the Adobe specification. As such, it may -deviate from any published specifications but it usually duplicates the -actual behavior of the original Adobe clients. - -The RTMPDump software package includes a basic client utility program -in -.BR rtmpdump (1), -some sample servers, and a library used to provide programmatic access -to the RTMP protocol. This man page gives an overview of the RTMP -library routines. These routines are found in the -lrtmp library. Many -other routines are also available, but they are not documented yet. - -The basic interaction is as follows. A session handle is created using -.BR RTMP_Alloc () -and initialized using -.BR RTMP_Init (). -All session parameters are provided using -.BR RTMP_SetupURL (). -The network connection is established using -.BR RTMP_Connect (), -and then the RTMP session is established using -.BR RTMP_ConnectStream (). -The stream is read using -.BR RTMP_Read (). -A client can publish a stream by calling -.BR RTMP_EnableWrite () -before the -.BR RTMP_Connect () -call, and then using -.BR RTMP_Write () -after the session is established. -While a stream is playing it may be paused and unpaused using -.BR RTMP_Pause (). -The stream playback position can be moved using -.BR RTMP_Seek (). -When -.BR RTMP_Read () -returns 0 bytes, the stream is complete and may be closed using -.BR RTMP_Close (). -The session handle is freed using -.BR RTMP_Free (). - -All data is transferred using FLV format. The basic session requires -an RTMP URL. The RTMP URL format is of the form -.nf - rtmp[t][e|s]://hostname[:port][/app[/playpath]] -.fi - -Plain rtmp, as well as tunneled and encrypted sessions are supported. - -Additional options may be specified by appending space-separated -key=value pairs to the URL. Special characters in values may need -to be escaped to prevent misinterpretation by the option parser. -The escape encoding uses a backslash followed by two hexadecimal digits -representing the ASCII value of the character. E.g., spaces must -be escaped as \fB\\20\fP and backslashes must be escaped as \fB\\5c\fP. -.SH OPTIONS -.SS "Network Parameters" -These options define how to connect to the media server. -.TP -.BI socks= host:port -Use the specified SOCKS4 proxy. -.SS "Connection Parameters" -These options define the content of the RTMP Connect request packet. -If correct values are not provided, the media server will reject the -connection attempt. -.TP -.BI app= name -Name of application to connect to on the RTMP server. Overrides -the app in the RTMP URL. Sometimes the librtmp URL parser cannot -determine the app name automatically, so it must be given explicitly -using this option. -.TP -.BI tcUrl= url -URL of the target stream. Defaults to rtmp[t][e|s]://host[:port]/app. -.TP -.BI pageUrl= url -URL of the web page in which the media was embedded. By default no -value will be sent. -.TP -.BI swfUrl= url -URL of the SWF player for the media. By default no value will be sent. -.TP -.BI flashVer= version -Version of the Flash plugin used to run the SWF player. The -default is "LNX 10,0,32,18". -.TP -.BI conn= type:data -Append arbitrary AMF data to the Connect message. The type -must be B for Boolean, N for number, S for string, O for object, or Z -for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, -respectively. Likewise for Objects the data must be 0 or 1 to end or -begin an object, respectively. Data items in subobjects may be named, by -prefixing the type with 'N' and specifying the name before the value, e.g. -NB:myFlag:1. This option may be used multiple times to construct arbitrary -AMF sequences. E.g. -.nf - conn=B:1 conn=S:authMe conn=O:1 conn=NN:code:1.23 conn=NS:flag:ok conn=O:0 -.fi -.SS "Session Parameters" -These options take effect after the Connect request has succeeded. -.TP -.BI playpath= path -Overrides the playpath parsed from the RTMP URL. Sometimes the -rtmpdump URL parser cannot determine the correct playpath -automatically, so it must be given explicitly using this option. -.TP -.BI playlist= 0|1 -If the value is 1 or TRUE, issue a set_playlist command before sending the -play command. The playlist will just contain the current playpath. If the -value is 0 or FALSE, the set_playlist command will not be sent. The -default is FALSE. -.TP -.BI live= 0|1 -Specify that the media is a live stream. No resuming or seeking in -live streams is possible. -.TP -.BI subscribe= path -Name of live stream to subscribe to. Defaults to -.IR playpath . -.TP -.BI start= num -Start at -.I num -seconds into the stream. Not valid for live streams. -.TP -.BI stop= num -Stop at -.I num -seconds into the stream. -.TP -.BI buffer= num -Set buffer time to -.I num -milliseconds. The default is 30000. -.TP -.BI timeout= num -Timeout the session after -.I num -seconds without receiving any data from the server. The default is 120. -.SS "Security Parameters" -These options handle additional authentication requests from the server. -.TP -.BI token= key -Key for SecureToken response, used if the server requires SecureToken -authentication. -.TP -.BI jtv= JSON -JSON token used by legacy Justin.tv servers. Invokes NetStream.Authenticate.UsherToken -.TP -.BI swfVfy= 0|1 -If the value is 1 or TRUE, the SWF player is retrieved from the -specified -.I swfUrl -for performing SWF Verification. The SWF hash and size (used in the -verification step) are computed automatically. Also the SWF information is -cached in a -.I .swfinfo -file in the user's home directory, so that it doesn't need to be retrieved -and recalculated every time. The .swfinfo file records -the SWF URL, the time it was fetched, the modification timestamp of the SWF -file, its size, and its hash. By default, the cached info will be used -for 30 days before re-checking. -.TP -.BI swfAge= days -Specify how many days to use the cached SWF info before re-checking. Use -0 to always check the SWF URL. Note that if the check shows that the -SWF file has the same modification timestamp as before, it will not be -retrieved again. -.SH EXAMPLES -An example character string suitable for use with -.BR RTMP_SetupURL (): -.nf - "rtmp://flashserver:1935/ondemand/thefile swfUrl=http://flashserver/player.swf swfVfy=1" -.fi -.SH ENVIRONMENT -.TP -.B HOME -The value of -.RB $ HOME -is used as the location for the -.I .swfinfo -file. -.SH FILES -.TP -.I $HOME/.swfinfo -Cache of SWF Verification information -.SH "SEE ALSO" -.BR rtmpdump (1), -.BR rtmpgw (8) -.SH AUTHORS -Andrej Stepanchuk, Howard Chu, The Flvstreamer Team -.br - diff --git a/rtmp/rtmp_c/librtmp/librtmp.3.html b/rtmp/rtmp_c/librtmp/librtmp.3.html deleted file mode 100644 index 6f598518..00000000 --- a/rtmp/rtmp_c/librtmp/librtmp.3.html +++ /dev/null @@ -1,312 +0,0 @@ - - -LIBRTMP(3): - - - - - -
LIBRTMP(3)LIBRTMP(3) -
RTMPDump v2.42011-07-20LIBRTMP(3) -


    - -
- -

NAME

    -librtmp − RTMPDump Real-Time Messaging Protocol API -
- -

LIBRARY

    -RTMPDump RTMP (librtmp, -lrtmp) -
- -

SYNOPSIS

    -#include <librtmp/rtmp.h> -
- -

DESCRIPTION

    -The Real-Time Messaging Protocol (RTMP) is used for streaming -multimedia content across a TCP/IP network. This API provides most client -functions and a few server functions needed to support RTMP, RTMP tunneled -in HTTP (RTMPT), encrypted RTMP (RTMPE), RTMP over SSL/TLS (RTMPS) and -tunneled variants of these encrypted types (RTMPTE, RTMPTS). The basic -RTMP specification has been published by Adobe but this API was -reverse-engineered without use of the Adobe specification. As such, it may -deviate from any published specifications but it usually duplicates the -actual behavior of the original Adobe clients. -

    -The RTMPDump software package includes a basic client utility program -in -rtmpdump(1), -some sample servers, and a library used to provide programmatic access -to the RTMP protocol. This man page gives an overview of the RTMP -library routines. These routines are found in the -lrtmp library. Many -other routines are also available, but they are not documented yet. -

    -The basic interaction is as follows. A session handle is created using -RTMP_Alloc() -and initialized using -RTMP_Init(). -All session parameters are provided using -RTMP_SetupURL(). -The network connection is established using -RTMP_Connect(), -and then the RTMP session is established using -RTMP_ConnectStream(). -The stream is read using -RTMP_Read(). -A client can publish a stream by calling -RTMP_EnableWrite() -before the -RTMP_Connect() -call, and then using -RTMP_Write() -after the session is established. -While a stream is playing it may be paused and unpaused using -RTMP_Pause(). -The stream playback position can be moved using -RTMP_Seek(). -When -RTMP_Read() -returns 0 bytes, the stream is complete and may be closed using -RTMP_Close(). -The session handle is freed using -RTMP_Free(). -

    -All data is transferred using FLV format. The basic session requires -an RTMP URL. The RTMP URL format is of the form -

    -  rtmp[t][e|s]://hostname[:port][/app[/playpath]]
    -
    -

    -Plain rtmp, as well as tunneled and encrypted sessions are supported. -

    -Additional options may be specified by appending space-separated -key=value pairs to the URL. Special characters in values may need -to be escaped to prevent misinterpretation by the option parser. -The escape encoding uses a backslash followed by two hexadecimal digits -representing the ASCII value of the character. E.g., spaces must -be escaped as \20 and backslashes must be escaped as \5c. -

- -

OPTIONS

    -
- -

Network Parameters

    -These options define how to connect to the media server. -

    -

    -socks=host:port -
    -Use the specified SOCKS4 proxy. -
    -
- -

Connection Parameters

    -These options define the content of the RTMP Connect request packet. -If correct values are not provided, the media server will reject the -connection attempt. -

    -

    -app=name -
    -Name of application to connect to on the RTMP server. Overrides -the app in the RTMP URL. Sometimes the librtmp URL parser cannot -determine the app name automatically, so it must be given explicitly -using this option. -
    -

    -

    -tcUrl=url -
    -URL of the target stream. Defaults to rtmp[t][e|s]://host[:port]/app. -
    -

    -

    -pageUrl=url -
    -URL of the web page in which the media was embedded. By default no -value will be sent. -
    -

    -

    -swfUrl=url -
    -URL of the SWF player for the media. By default no value will be sent. -
    -

    -

    -flashVer=version -
    -Version of the Flash plugin used to run the SWF player. The -default is "LNX 10,0,32,18". -
    -

    -

    -conn=type:data -
    -Append arbitrary AMF data to the Connect message. The type -must be B for Boolean, N for number, S for string, O for object, or Z -for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, -respectively. Likewise for Objects the data must be 0 or 1 to end or -begin an object, respectively. Data items in subobjects may be named, by -prefixing the type with 'N' and specifying the name before the value, e.g. -NB:myFlag:1. This option may be used multiple times to construct arbitrary -AMF sequences. E.g. -
    -  conn=B:1 conn=S:authMe conn=O:1 conn=NN:code:1.23 conn=NS:flag:ok conn=O:0
    -
    -
    -
- -

Session Parameters

    -These options take effect after the Connect request has succeeded. -

    -

    -playpath=path -
    -Overrides the playpath parsed from the RTMP URL. Sometimes the -rtmpdump URL parser cannot determine the correct playpath -automatically, so it must be given explicitly using this option. -
    -

    -

    -playlist=0|1 -
    -If the value is 1 or TRUE, issue a set_playlist command before sending the -play command. The playlist will just contain the current playpath. If the -value is 0 or FALSE, the set_playlist command will not be sent. The -default is FALSE. -
    -

    -

    -live=0|1 -
    -Specify that the media is a live stream. No resuming or seeking in -live streams is possible. -
    -

    -

    -subscribe=path -
    -Name of live stream to subscribe to. Defaults to -playpath. -
    -

    -

    -start=num -
    -Start at -num -seconds into the stream. Not valid for live streams. -
    -

    -

    -stop=num -
    -Stop at -num -seconds into the stream. -
    -

    -

    -buffer=num -
    -Set buffer time to -num -milliseconds. The default is 30000. -
    -

    -

    -timeout=num -
    -Timeout the session after -num -seconds without receiving any data from the server. The default is 120. -
    -
- -

Security Parameters

    -These options handle additional authentication requests from the server. -

    -

    -token=key -
    -Key for SecureToken response, used if the server requires SecureToken -authentication. -
    -

    -

    -jtv=JSON -
    -JSON token used by legacy Justin.tv servers. Invokes NetStream.Authenticate.UsherToken -
    -

    -

    -swfVfy=0|1 -
    -If the value is 1 or TRUE, the SWF player is retrieved from the -specified -swfUrl -for performing SWF Verification. The SWF hash and size (used in the -verification step) are computed automatically. Also the SWF information is -cached in a -.swfinfo -file in the user's home directory, so that it doesn't need to be retrieved -and recalculated every time. The .swfinfo file records -the SWF URL, the time it was fetched, the modification timestamp of the SWF -file, its size, and its hash. By default, the cached info will be used -for 30 days before re-checking. -
    -

    -

    -swfAge=days -
    -Specify how many days to use the cached SWF info before re-checking. Use -0 to always check the SWF URL. Note that if the check shows that the -SWF file has the same modification timestamp as before, it will not be -retrieved again. -
    -
- -

EXAMPLES

    -An example character string suitable for use with -RTMP_SetupURL(): -
    -  "rtmp://flashserver:1935/ondemand/thefile swfUrl=http://flashserver/player.swf swfVfy=1"
    -
    -
- -

ENVIRONMENT

    -

    -

    -HOME -
    -The value of -$HOME -is used as the location for the -.swfinfo -file. -
    -
- -

FILES

    -

    -

    -$HOME/.swfinfo -
    -Cache of SWF Verification information -
    -
- -

SEE ALSO

- -

AUTHORS

diff --git a/rtmp/rtmp_c/librtmp/librtmp.pc.in b/rtmp/rtmp_c/librtmp/librtmp.pc.in deleted file mode 100644 index 551b4d96..00000000 --- a/rtmp/rtmp_c/librtmp/librtmp.pc.in +++ /dev/null @@ -1,13 +0,0 @@ -prefix=@prefix@ -exec_prefix=${prefix} -libdir=@libdir@ -incdir=${prefix}/include - -Name: librtmp -Description: RTMP implementation -Version: @VERSION@ -Requires: @CRYPTO_REQ@ -URL: http://rtmpdump.mplayerhq.hu -Libs: -L${libdir} -lrtmp -lz @PUBLIC_LIBS@ -Libs.private: @PRIVATE_LIBS@ -Cflags: -I${incdir} diff --git a/rtmp/rtmp_c/librtmp/log.c b/rtmp/rtmp_c/librtmp/log.c deleted file mode 100644 index 1b520000..00000000 --- a/rtmp/rtmp_c/librtmp/log.c +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#include -#include -#include -#include -#include - -#include "rtmp_sys.h" -#include "log.h" - -#define MAX_PRINT_LEN 2048 - -RTMP_LogLevel RTMP_debuglevel = RTMP_LOGERROR; - -static int neednl; - -static FILE *fmsg; - -static RTMP_LogCallback rtmp_log_default, *cb = rtmp_log_default; - -static const char *levels[] = { - "CRIT", "ERROR", "WARNING", "INFO", - "DEBUG", "DEBUG2" -}; - -static void rtmp_log_default(int level, const char *format, va_list vl) -{ - char str[MAX_PRINT_LEN]=""; - - vsnprintf(str, MAX_PRINT_LEN-1, format, vl); - - /* Filter out 'no-name' */ - if ( RTMP_debuglevel RTMP_debuglevel ) - return; - - va_start(args, format); - cb(level, format, args); - va_end(args); -} - -static const char hexdig[] = "0123456789abcdef"; - -void RTMP_LogHex(int level, const uint8_t *data, unsigned long len) -{ - unsigned long i; - char line[50], *ptr; - - if ( level > RTMP_debuglevel ) - return; - - ptr = line; - - for(i=0; i> 4)]; - *ptr++ = hexdig[0x0f & data[i]]; - if ((i & 0x0f) == 0x0f) { - *ptr = '\0'; - ptr = line; - RTMP_Log(level, "%s", line); - } else { - *ptr++ = ' '; - } - } - if (i & 0x0f) { - *ptr = '\0'; - RTMP_Log(level, "%s", line); - } -} - -void RTMP_LogHexString(int level, const uint8_t *data, unsigned long len) -{ -#define BP_OFFSET 9 -#define BP_GRAPH 60 -#define BP_LEN 80 - char line[BP_LEN]; - unsigned long i; - - if ( !data || level > RTMP_debuglevel ) - return; - - /* in case len is zero */ - line[0] = '\0'; - - for ( i = 0 ; i < len ; i++ ) { - int n = i % 16; - unsigned off; - - if( !n ) { - if( i ) RTMP_Log( level, "%s", line ); - memset( line, ' ', sizeof(line)-2 ); - line[sizeof(line)-2] = '\0'; - - off = i % 0x0ffffU; - - line[2] = hexdig[0x0f & (off >> 12)]; - line[3] = hexdig[0x0f & (off >> 8)]; - line[4] = hexdig[0x0f & (off >> 4)]; - line[5] = hexdig[0x0f & off]; - line[6] = ':'; - } - - off = BP_OFFSET + n*3 + ((n >= 8)?1:0); - line[off] = hexdig[0x0f & ( data[i] >> 4 )]; - line[off+1] = hexdig[0x0f & data[i]]; - - off = BP_GRAPH + n + ((n >= 8)?1:0); - - if ( isprint( data[i] )) { - line[BP_GRAPH + n] = data[i]; - } else { - line[BP_GRAPH + n] = '.'; - } - } - - RTMP_Log( level, "%s", line ); -} - -/* These should only be used by apps, never by the library itself */ -void RTMP_LogPrintf(const char *format, ...) -{ - char str[MAX_PRINT_LEN]=""; - int len; - va_list args; - va_start(args, format); - len = vsnprintf(str, MAX_PRINT_LEN-1, format, args); - va_end(args); - - if ( RTMP_debuglevel==RTMP_LOGCRIT ) - return; - - if ( !fmsg ) fmsg = stderr; - - if (neednl) { - putc('\n', fmsg); - neednl = 0; - } - - if (len > MAX_PRINT_LEN-1) - len = MAX_PRINT_LEN-1; - fprintf(fmsg, "%s", str); - if (str[len-1] == '\n') - fflush(fmsg); -} - -void RTMP_LogStatus(const char *format, ...) -{ - char str[MAX_PRINT_LEN]=""; - va_list args; - va_start(args, format); - vsnprintf(str, MAX_PRINT_LEN-1, format, args); - va_end(args); - - if ( RTMP_debuglevel==RTMP_LOGCRIT ) - return; - - if ( !fmsg ) fmsg = stderr; - - fprintf(fmsg, "%s", str); - fflush(fmsg); - neednl = 1; -} diff --git a/rtmp/rtmp_c/librtmp/log.h b/rtmp/rtmp_c/librtmp/log.h deleted file mode 100644 index 2adb1114..00000000 --- a/rtmp/rtmp_c/librtmp/log.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#ifndef __RTMP_LOG_H__ -#define __RTMP_LOG_H__ - -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif -/* Enable this to get full debugging output */ -/* #define _DEBUG */ - -#ifdef _DEBUG -#undef NODEBUG -#endif - -typedef enum -{ RTMP_LOGCRIT=0, RTMP_LOGERROR, RTMP_LOGWARNING, RTMP_LOGINFO, - RTMP_LOGDEBUG, RTMP_LOGDEBUG2, RTMP_LOGALL -} RTMP_LogLevel; - -extern RTMP_LogLevel RTMP_debuglevel; - -typedef void (RTMP_LogCallback)(int level, const char *fmt, va_list); -void RTMP_LogSetCallback(RTMP_LogCallback *cb); -void RTMP_LogSetOutput(FILE *file); -#ifdef __GNUC__ -void RTMP_LogPrintf(const char *format, ...) __attribute__ ((__format__ (__printf__, 1, 2))); -void RTMP_LogStatus(const char *format, ...) __attribute__ ((__format__ (__printf__, 1, 2))); -void RTMP_Log(int level, const char *format, ...) __attribute__ ((__format__ (__printf__, 2, 3))); -#else -void RTMP_LogPrintf(const char *format, ...); -void RTMP_LogStatus(const char *format, ...); -void RTMP_Log(int level, const char *format, ...); -#endif -void RTMP_LogHex(int level, const uint8_t *data, unsigned long len); -void RTMP_LogHexString(int level, const uint8_t *data, unsigned long len); -void RTMP_LogSetLevel(RTMP_LogLevel lvl); -RTMP_LogLevel RTMP_LogGetLevel(void); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/rtmp/rtmp_c/librtmp/parseurl.c b/rtmp/rtmp_c/librtmp/parseurl.c deleted file mode 100644 index 646c70c1..00000000 --- a/rtmp/rtmp_c/librtmp/parseurl.c +++ /dev/null @@ -1,289 +0,0 @@ -/* - * Copyright (C) 2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#include -#include - -#include -#include - -#include "rtmp_sys.h" -#include "log.h" - -int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, - AVal *playpath, AVal *app) -{ - char *p, *end, *col, *ques, *slash; - - RTMP_Log(RTMP_LOGDEBUG, "Parsing..."); - - *protocol = RTMP_PROTOCOL_RTMP; - *port = 0; - playpath->av_len = 0; - playpath->av_val = NULL; - app->av_len = 0; - app->av_val = NULL; - - /* Old School Parsing */ - - /* look for usual :// pattern */ - p = strstr(url, "://"); - if(!p) { - RTMP_Log(RTMP_LOGERROR, "RTMP URL: No :// in url!"); - return FALSE; - } - { - int len = (int)(p-url); - - if(len == 4 && strncasecmp(url, "rtmp", 4)==0) - *protocol = RTMP_PROTOCOL_RTMP; - else if(len == 5 && strncasecmp(url, "rtmpt", 5)==0) - *protocol = RTMP_PROTOCOL_RTMPT; - else if(len == 5 && strncasecmp(url, "rtmps", 5)==0) - *protocol = RTMP_PROTOCOL_RTMPS; - else if(len == 5 && strncasecmp(url, "rtmpe", 5)==0) - *protocol = RTMP_PROTOCOL_RTMPE; - else if(len == 5 && strncasecmp(url, "rtmfp", 5)==0) - *protocol = RTMP_PROTOCOL_RTMFP; - else if(len == 6 && strncasecmp(url, "rtmpte", 6)==0) - *protocol = RTMP_PROTOCOL_RTMPTE; - else if(len == 6 && strncasecmp(url, "rtmpts", 6)==0) - *protocol = RTMP_PROTOCOL_RTMPTS; - else { - RTMP_Log(RTMP_LOGWARNING, "Unknown protocol!\n"); - goto parsehost; - } - } - - RTMP_Log(RTMP_LOGDEBUG, "Parsed protocol: %d", *protocol); - -parsehost: - /* let's get the hostname */ - p+=3; - - /* check for sudden death */ - if(*p==0) { - RTMP_Log(RTMP_LOGWARNING, "No hostname in URL!"); - return FALSE; - } - - end = p + strlen(p); - col = strchr(p, ':'); - ques = strchr(p, '?'); - slash = strchr(p, '/'); - - { - int hostlen; - if(slash) - hostlen = slash - p; - else - hostlen = end - p; - if(col && col -p < hostlen) - hostlen = col - p; - - if(hostlen < 256) { - host->av_val = p; - host->av_len = hostlen; - RTMP_Log(RTMP_LOGDEBUG, "Parsed host : %.*s", hostlen, host->av_val); - } else { - RTMP_Log(RTMP_LOGWARNING, "Hostname exceeds 255 characters!"); - } - - p+=hostlen; - } - - /* get the port number if available */ - if(*p == ':') { - unsigned int p2; - p++; - p2 = atoi(p); - if(p2 > 65535) { - RTMP_Log(RTMP_LOGWARNING, "Invalid port number!"); - } else { - *port = p2; - } - } - - if(!slash) { - RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!"); - return TRUE; - } - p = slash+1; - - { - /* parse application - * - * rtmp://host[:port]/app[/appinstance][/...] - * application = app[/appinstance] - */ - - char *slash2, *slash3 = NULL, *slash4 = NULL; - int applen, appnamelen; - - slash2 = strchr(p, '/'); - if(slash2) - slash3 = strchr(slash2+1, '/'); - if(slash3) - slash4 = strchr(slash3+1, '/'); - - applen = end-p; /* ondemand, pass all parameters as app */ - appnamelen = applen; /* ondemand length */ - - if(ques && strstr(p, "slist=")) { /* whatever it is, the '?' and slist= means we need to use everything as app and parse plapath from slist= */ - appnamelen = ques-p; - } - else if(strncmp(p, "ondemand/", 9)==0) { - /* app = ondemand/foobar, only pass app=ondemand */ - applen = 8; - appnamelen = 8; - } - else { /* app!=ondemand, so app is app[/appinstance] */ - if(slash4) - appnamelen = slash4-p; - else if(slash3) - appnamelen = slash3-p; - else if(slash2) - appnamelen = slash2-p; - - applen = appnamelen; - } - - app->av_val = p; - app->av_len = applen; - RTMP_Log(RTMP_LOGDEBUG, "Parsed app : %.*s", applen, p); - - p += appnamelen; - } - - if (*p == '/') - p++; - - if (end-p) { - AVal av = {p, end-p}; - RTMP_ParsePlaypath(&av, playpath); - } - - return TRUE; -} - -/* - * Extracts playpath from RTMP URL. playpath is the file part of the - * URL, i.e. the part that comes after rtmp://host:port/app/ - * - * Returns the stream name in a format understood by FMS. The name is - * the playpath part of the URL with formatting depending on the stream - * type: - * - * mp4 streams: prepend "mp4:", remove extension - * mp3 streams: prepend "mp3:", remove extension - * flv streams: remove extension - */ -void RTMP_ParsePlaypath(AVal *in, AVal *out) { - int addMP4 = 0; - int addMP3 = 0; - int subExt = 0; - const char *playpath = in->av_val; - const char *temp, *q, *ext = NULL; - const char *ppstart = playpath; - char *streamname, *destptr, *p; - - int pplen = in->av_len; - - out->av_val = NULL; - out->av_len = 0; - - if ((*ppstart == '?') && - (temp=strstr(ppstart, "slist=")) != 0) { - ppstart = temp+6; - pplen = strlen(ppstart); - - temp = strchr(ppstart, '&'); - if (temp) { - pplen = temp-ppstart; - } - } - - q = strchr(ppstart, '?'); - if (pplen >= 4) { - if (q) - ext = q-4; - else - ext = &ppstart[pplen-4]; - if ((strncmp(ext, ".f4v", 4) == 0) || - (strncmp(ext, ".mp4", 4) == 0)) { - addMP4 = 1; - subExt = 1; - /* Only remove .flv from rtmp URL, not slist params */ - } else if ((ppstart == playpath) && - (strncmp(ext, ".flv", 4) == 0)) { - subExt = 1; - } else if (strncmp(ext, ".mp3", 4) == 0) { - addMP3 = 1; - subExt = 1; - } - } - - streamname = (char *)malloc((pplen+4+1)*sizeof(char)); - if (!streamname) - return; - - destptr = streamname; - if (addMP4) { - if (strncmp(ppstart, "mp4:", 4)) { - strcpy(destptr, "mp4:"); - destptr += 4; - } else { - subExt = 0; - } - } else if (addMP3) { - if (strncmp(ppstart, "mp3:", 4)) { - strcpy(destptr, "mp3:"); - destptr += 4; - } else { - subExt = 0; - } - } - - for (p=(char *)ppstart; pplen >0;) { - /* skip extension */ - if (subExt && p == ext) { - p += 4; - pplen -= 4; - continue; - } - if (*p == '%') { - unsigned int c; - sscanf(p+1, "%02x", &c); - *destptr++ = c; - pplen -= 3; - p += 3; - } else { - *destptr++ = *p++; - pplen--; - } - } - *destptr = '\0'; - - out->av_val = streamname; - out->av_len = destptr - streamname; -} diff --git a/rtmp/rtmp_c/librtmp/rtmp.c b/rtmp/rtmp_c/librtmp/rtmp.c deleted file mode 100644 index 379e195c..00000000 --- a/rtmp/rtmp_c/librtmp/rtmp.c +++ /dev/null @@ -1,5222 +0,0 @@ -/* - * Copyright (C) 2005-2008 Team XBMC - * http://www.xbmc.org - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#include -#include -#include -#include -#include - -#include "rtmp_sys.h" -#include "log.h" - -#ifdef CRYPTO -#ifdef USE_POLARSSL -#include -#include -#include -#define MD5_DIGEST_LENGTH 16 - -static const char *my_dhm_P = - "E4004C1F94182000103D883A448B3F80" \ - "2CE4B44A83301270002C20D0321CFD00" \ - "11CCEF784C26A400F43DFB901BCA7538" \ - "F2C6B176001CF5A0FD16D2C48B1D0C1C" \ - "F6AC8E1DA6BCC3B4E1F96B0564965300" \ - "FFA1D0B601EB2800F489AA512C4B248C" \ - "01F76949A60BB7F00A40B1EAB64BDD48" \ - "E8A700D60B7F1200FA8E77B0A979DABF"; - -static const char *my_dhm_G = "4"; - -#elif defined(USE_GNUTLS) -#include -#define MD5_DIGEST_LENGTH 16 -#include -#include -#else /* USE_OPENSSL */ -#include -#include -#include -#include -#include -#endif -TLS_CTX RTMP_TLS_ctx; -#endif - -#define RTMP_SIG_SIZE 1536 -#define RTMP_LARGE_HEADER_SIZE 12 - -static const int packetSize[] = { 12, 8, 4, 1 }; - -int RTMP_ctrlC; - -const char RTMPProtocolStrings[][7] = { - "RTMP", - "RTMPT", - "RTMPE", - "RTMPTE", - "RTMPS", - "RTMPTS", - "", - "", - "RTMFP" -}; - -const char RTMPProtocolStringsLower[][7] = { - "rtmp", - "rtmpt", - "rtmpe", - "rtmpte", - "rtmps", - "rtmpts", - "", - "", - "rtmfp" -}; - -static const char *RTMPT_cmds[] = { - "open", - "send", - "idle", - "close" -}; - -typedef enum { - RTMPT_OPEN=0, RTMPT_SEND, RTMPT_IDLE, RTMPT_CLOSE -} RTMPTCmd; - -static int DumpMetaData(AMFObject *obj); -int HandShake(RTMP *r, int FP9HandShake); -int SocksNegotiate(RTMP *r); - -int SendConnectPacket(RTMP *r, RTMPPacket *cp); -int SendCheckBW(RTMP *r); -int SendCheckBWResult(RTMP *r, double txn); -int SendDeleteStream(RTMP *r, double dStreamId); -int SendFCSubscribe(RTMP *r, AVal *subscribepath); -int SendPlay(RTMP *r); -int SendBytesReceived(RTMP *r); -int SendUsherToken(RTMP *r, AVal *usherToken); - -#if 0 /* unused */ -static int SendBGHasStream(RTMP *r, double dId, AVal *playpath); -#endif - -int HandleInvoke(RTMP *r, const char *body, unsigned int nBodySize); -int HandleMetadata(RTMP *r, char *body, unsigned int len); -void HandleChangeChunkSize(RTMP *r, const RTMPPacket *packet); -void HandleAudio(RTMP *r, const RTMPPacket *packet); -void HandleVideo(RTMP *r, const RTMPPacket *packet); -void HandleCtrl(RTMP *r, const RTMPPacket *packet); -void HandleServerBW(RTMP *r, const RTMPPacket *packet); -void HandleClientBW(RTMP *r, const RTMPPacket *packet); - -int ReadN(RTMP *r, char *buffer, int n); -int WriteN(RTMP *r, const char *buffer, int n); - -void DecodeTEA(AVal *key, AVal *text); - -int HTTP_Post(RTMP *r, RTMPTCmd cmd, const char *buf, int len); -static int HTTP_read(RTMP *r, int fill); - -static void CloseInternal(RTMP *r, int reconnect); - -#ifndef _WIN32 -static int clk_tck; -#endif - -#ifdef CRYPTO -#include "handshake.h" -#endif - -uint32_t -RTMP_GetTime() -{ -#ifdef _DEBUG - return 0; -#elif defined(_WIN32) - return timeGetTime(); -#else - struct tms t; - if (!clk_tck) clk_tck = sysconf(_SC_CLK_TCK); - return times(&t) * 1000 / clk_tck; -#endif -} - -void -RTMP_UserInterrupt() -{ - RTMP_ctrlC = TRUE; -} - -void -RTMPPacket_Reset(RTMPPacket *p) -{ - p->m_headerType = 0; - p->m_packetType = 0; - p->m_nChannel = 0; - p->m_nTimeStamp = 0; - p->m_nInfoField2 = 0; - p->m_hasAbsTimestamp = FALSE; - p->m_nBodySize = 0; - p->m_nBytesRead = 0; -} - -int -RTMPPacket_Alloc(RTMPPacket *p, uint32_t nSize) -{ - char *ptr; - if (nSize > SIZE_MAX - RTMP_MAX_HEADER_SIZE) - return FALSE; - ptr = calloc(1, nSize + RTMP_MAX_HEADER_SIZE); - if (!ptr) - return FALSE; - p->m_body = ptr + RTMP_MAX_HEADER_SIZE; - p->m_nBytesRead = 0; - return TRUE; -} - -void -RTMPPacket_Free(RTMPPacket *p) -{ - if (p->m_body) - { - free(p->m_body - RTMP_MAX_HEADER_SIZE); - p->m_body = NULL; - } -} - -void -RTMPPacket_Dump(RTMPPacket *p) -{ - RTMP_Log(RTMP_LOGDEBUG, - "RTMP PACKET: packet type: 0x%02x. channel: 0x%02x. info 1: %d info 2: %d. Body size: %u. body: 0x%02x", - p->m_packetType, p->m_nChannel, p->m_nTimeStamp, p->m_nInfoField2, - p->m_nBodySize, p->m_body ? (unsigned char)p->m_body[0] : 0); -} - -int -RTMP_LibVersion() -{ - return RTMP_LIB_VERSION; -} - -void -RTMP_TLS_Init() -{ -#ifdef CRYPTO -#ifdef USE_POLARSSL - /* Do this regardless of NO_SSL, we use havege for rtmpe too */ - RTMP_TLS_ctx = calloc(1,sizeof(struct tls_ctx)); - havege_init(&RTMP_TLS_ctx->hs); -#elif defined(USE_GNUTLS) && !defined(NO_SSL) - /* Technically we need to initialize libgcrypt ourselves if - * we're not going to call gnutls_global_init(). Ignoring this - * for now. - */ - gnutls_global_init(); - RTMP_TLS_ctx = malloc(sizeof(struct tls_ctx)); - gnutls_certificate_allocate_credentials(&RTMP_TLS_ctx->cred); - gnutls_priority_init(&RTMP_TLS_ctx->prios, "NORMAL", NULL); - gnutls_certificate_set_x509_trust_file(RTMP_TLS_ctx->cred, - "ca.pem", GNUTLS_X509_FMT_PEM); -#elif !defined(NO_SSL) /* USE_OPENSSL */ - /* libcrypto doesn't need anything special */ - SSL_load_error_strings(); - SSL_library_init(); - OpenSSL_add_all_digests(); - RTMP_TLS_ctx = SSL_CTX_new(SSLv23_method()); - SSL_CTX_set_options(RTMP_TLS_ctx, SSL_OP_ALL); - SSL_CTX_set_default_verify_paths(RTMP_TLS_ctx); -#endif -#endif -} - -void * -RTMP_TLS_AllocServerContext(const char* cert, const char* key) -{ - void *ctx = NULL; -#ifdef CRYPTO - if (!RTMP_TLS_ctx) - RTMP_TLS_Init(); -#ifdef USE_POLARSSL - tls_server_ctx *tc = ctx = calloc(1, sizeof(struct tls_server_ctx)); - tc->dhm_P = my_dhm_P; - tc->dhm_G = my_dhm_G; - tc->hs = &RTMP_TLS_ctx->hs; - if (x509parse_crtfile(&tc->cert, cert)) { - free(tc); - return NULL; - } - if (x509parse_keyfile(&tc->key, key, NULL)) { - x509_free(&tc->cert); - free(tc); - return NULL; - } -#elif defined(USE_GNUTLS) && !defined(NO_SSL) - gnutls_certificate_allocate_credentials((gnutls_certificate_credentials*) &ctx); - if (gnutls_certificate_set_x509_key_file(ctx, cert, key, GNUTLS_X509_FMT_PEM) != 0) { - gnutls_certificate_free_credentials(ctx); - return NULL; - } -#elif !defined(NO_SSL) /* USE_OPENSSL */ - ctx = SSL_CTX_new(SSLv23_server_method()); - if (!SSL_CTX_use_certificate_chain_file(ctx, cert)) { - SSL_CTX_free(ctx); - return NULL; - } - if (!SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM)) { - SSL_CTX_free(ctx); - return NULL; - } -#endif -#endif - return ctx; -} - -void -RTMP_TLS_FreeServerContext(void *ctx) -{ -#ifdef CRYPTO -#ifdef USE_POLARSSL - x509_free(&((tls_server_ctx*)ctx)->cert); - rsa_free(&((tls_server_ctx*)ctx)->key); - free(ctx); -#elif defined(USE_GNUTLS) && !defined(NO_SSL) - gnutls_certificate_free_credentials(ctx); -#elif !defined(NO_SSL) /* USE_OPENSSL */ - SSL_CTX_free(ctx); -#endif -#endif -} - -RTMP * -RTMP_Alloc() -{ - return calloc(1, sizeof(RTMP)); -} - -void -RTMP_Free(RTMP *r) -{ - free(r); -} - -void -RTMP_Init(RTMP *r) -{ -#ifdef CRYPTO - if (!RTMP_TLS_ctx) - RTMP_TLS_Init(); -#endif - - memset(r, 0, sizeof(RTMP)); - r->m_sb.sb_socket = -1; - r->m_inChunkSize = RTMP_DEFAULT_CHUNKSIZE; - r->m_outChunkSize = RTMP_DEFAULT_CHUNKSIZE; - r->m_nBufferMS = 30000; - r->m_nClientBW = 2500000; - r->m_nClientBW2 = 2; - r->m_nServerBW = 2500000; - r->m_fAudioCodecs = 3191.0; - r->m_fVideoCodecs = 252.0; - r->Link.timeout = 30; - r->Link.swfAge = 30; -} - -void -RTMP_EnableWrite(RTMP *r) -{ - r->Link.protocol |= RTMP_FEATURE_WRITE; -} - -double -RTMP_GetDuration(RTMP *r) -{ - return r->m_fDuration; -} - -int -RTMP_IsConnected(RTMP *r) -{ - return r->m_sb.sb_socket != -1; -} - -int -RTMP_Socket(RTMP *r) -{ - return r->m_sb.sb_socket; -} - -int -RTMP_IsTimedout(RTMP *r) -{ - return r->m_sb.sb_timedout; -} - -void -RTMP_SetBufferMS(RTMP *r, int size) -{ - r->m_nBufferMS = size; -} - -void -RTMP_UpdateBufferMS(RTMP *r) -{ - RTMP_SendCtrl(r, 3, r->m_stream_id, r->m_nBufferMS); -} - -#undef OSS -#ifdef _WIN32 -#define OSS "WIN" -#elif defined(__sun__) -#define OSS "SOL" -#elif defined(__APPLE__) -#define OSS "MAC" -#elif defined(__linux__) -#define OSS "LNX" -#else -#define OSS "GNU" -#endif -#define DEF_VERSTR OSS " 10,0,32,18" -static const char DEFAULT_FLASH_VER[] = DEF_VERSTR; -const AVal RTMP_DefaultFlashVer = - { (char *)DEFAULT_FLASH_VER, sizeof(DEFAULT_FLASH_VER) - 1 }; - -void -SocksSetup(RTMP *r, AVal *sockshost) -{ - if (sockshost->av_len) - { - const char *socksport = strchr(sockshost->av_val, ':'); - char *hostname = strdup(sockshost->av_val); - - if (socksport) - hostname[socksport - sockshost->av_val] = '\0'; - r->Link.sockshost.av_val = hostname; - r->Link.sockshost.av_len = strlen(hostname); - - r->Link.socksport = socksport ? atoi(socksport + 1) : 1080; - RTMP_Log(RTMP_LOGDEBUG, "Connecting via SOCKS proxy: %s:%d", r->Link.sockshost.av_val, - r->Link.socksport); - } - else - { - r->Link.sockshost.av_val = NULL; - r->Link.sockshost.av_len = 0; - r->Link.socksport = 0; - } -} - -void -RTMP_SetupStream(RTMP *r, - int protocol, - AVal *host, - unsigned int port, - AVal *sockshost, - AVal *playpath, - AVal *tcUrl, - AVal *swfUrl, - AVal *pageUrl, - AVal *app, - AVal *auth, - AVal *swfSHA256Hash, - uint32_t swfSize, - AVal *flashVer, - AVal *subscribepath, - AVal *usherToken, - int dStart, - int dStop, int bLiveStream, long int timeout) -{ - RTMP_Log(RTMP_LOGDEBUG, "Protocol : %s", RTMPProtocolStrings[protocol&7]); - RTMP_Log(RTMP_LOGDEBUG, "Hostname : %.*s", host->av_len, host->av_val); - RTMP_Log(RTMP_LOGDEBUG, "Port : %d", port); - RTMP_Log(RTMP_LOGDEBUG, "Playpath : %s", playpath->av_val); - - if (tcUrl && tcUrl->av_val) - RTMP_Log(RTMP_LOGDEBUG, "tcUrl : %s", tcUrl->av_val); - if (swfUrl && swfUrl->av_val) - RTMP_Log(RTMP_LOGDEBUG, "swfUrl : %s", swfUrl->av_val); - if (pageUrl && pageUrl->av_val) - RTMP_Log(RTMP_LOGDEBUG, "pageUrl : %s", pageUrl->av_val); - if (app && app->av_val) - RTMP_Log(RTMP_LOGDEBUG, "app : %.*s", app->av_len, app->av_val); - if (auth && auth->av_val) - RTMP_Log(RTMP_LOGDEBUG, "auth : %s", auth->av_val); - if (subscribepath && subscribepath->av_val) - RTMP_Log(RTMP_LOGDEBUG, "subscribepath : %s", subscribepath->av_val); - if (usherToken && usherToken->av_val) - RTMP_Log(RTMP_LOGDEBUG, "NetStream.Authenticate.UsherToken : %s", usherToken->av_val); - if (flashVer && flashVer->av_val) - RTMP_Log(RTMP_LOGDEBUG, "flashVer : %s", flashVer->av_val); - if (dStart > 0) - RTMP_Log(RTMP_LOGDEBUG, "StartTime : %d msec", dStart); - if (dStop > 0) - RTMP_Log(RTMP_LOGDEBUG, "StopTime : %d msec", dStop); - - RTMP_Log(RTMP_LOGDEBUG, "live : %s", bLiveStream ? "yes" : "no"); - RTMP_Log(RTMP_LOGDEBUG, "timeout : %ld sec", timeout); - -#ifdef CRYPTO - if (swfSHA256Hash != NULL && swfSize > 0) - { - memcpy(r->Link.SWFHash, swfSHA256Hash->av_val, sizeof(r->Link.SWFHash)); - r->Link.SWFSize = swfSize; - RTMP_Log(RTMP_LOGDEBUG, "SWFSHA256:"); - RTMP_LogHex(RTMP_LOGDEBUG, r->Link.SWFHash, sizeof(r->Link.SWFHash)); - RTMP_Log(RTMP_LOGDEBUG, "SWFSize : %u", r->Link.SWFSize); - } - else - { - r->Link.SWFSize = 0; - } -#endif - - SocksSetup(r, sockshost); - - if (tcUrl && tcUrl->av_len) - r->Link.tcUrl = *tcUrl; - if (swfUrl && swfUrl->av_len) - r->Link.swfUrl = *swfUrl; - if (pageUrl && pageUrl->av_len) - r->Link.pageUrl = *pageUrl; - if (app && app->av_len) - r->Link.app = *app; - if (auth && auth->av_len) - { - r->Link.auth = *auth; - r->Link.lFlags |= RTMP_LF_AUTH; - } - if (flashVer && flashVer->av_len) - r->Link.flashVer = *flashVer; - else - r->Link.flashVer = RTMP_DefaultFlashVer; - if (subscribepath && subscribepath->av_len) - r->Link.subscribepath = *subscribepath; - if (usherToken && usherToken->av_len) - r->Link.usherToken = *usherToken; - r->Link.seekTime = dStart; - r->Link.stopTime = dStop; - if (bLiveStream) - r->Link.lFlags |= RTMP_LF_LIVE; - r->Link.timeout = timeout; - - r->Link.protocol = protocol; - r->Link.hostname = *host; - r->Link.port = port; - r->Link.playpath = *playpath; - - if (r->Link.port == 0) - { - if (protocol & RTMP_FEATURE_SSL) - r->Link.port = 443; - else if (protocol & RTMP_FEATURE_HTTP) - r->Link.port = 80; - else - r->Link.port = 1935; - } -} - -enum { OPT_STR=0, OPT_INT, OPT_BOOL, OPT_CONN }; -static const char *optinfo[] = { - "string", "integer", "boolean", "AMF" }; - -#define OFF(x) offsetof(struct RTMP,x) - -static struct urlopt { - AVal name; - off_t off; - int otype; - int omisc; - char *use; -} options[] = { - { AVC("socks"), OFF(Link.sockshost), OPT_STR, 0, - "Use the specified SOCKS proxy" }, - { AVC("app"), OFF(Link.app), OPT_STR, 0, - "Name of target app on server" }, - { AVC("tcUrl"), OFF(Link.tcUrl), OPT_STR, 0, - "URL to played stream" }, - { AVC("pageUrl"), OFF(Link.pageUrl), OPT_STR, 0, - "URL of played media's web page" }, - { AVC("swfUrl"), OFF(Link.swfUrl), OPT_STR, 0, - "URL to player SWF file" }, - { AVC("flashver"), OFF(Link.flashVer), OPT_STR, 0, - "Flash version string (default " DEF_VERSTR ")" }, - { AVC("conn"), OFF(Link.extras), OPT_CONN, 0, - "Append arbitrary AMF data to Connect message" }, - { AVC("playpath"), OFF(Link.playpath), OPT_STR, 0, - "Path to target media on server" }, - { AVC("playlist"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_PLST, - "Set playlist before play command" }, - { AVC("live"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_LIVE, - "Stream is live, no seeking possible" }, - { AVC("subscribe"), OFF(Link.subscribepath), OPT_STR, 0, - "Stream to subscribe to" }, - { AVC("jtv"), OFF(Link.usherToken), OPT_STR, 0, - "Justin.tv authentication token" }, - { AVC("token"), OFF(Link.token), OPT_STR, 0, - "Key for SecureToken response" }, - { AVC("swfVfy"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_SWFV, - "Perform SWF Verification" }, - { AVC("swfAge"), OFF(Link.swfAge), OPT_INT, 0, - "Number of days to use cached SWF hash" }, - { AVC("start"), OFF(Link.seekTime), OPT_INT, 0, - "Stream start position in milliseconds" }, - { AVC("stop"), OFF(Link.stopTime), OPT_INT, 0, - "Stream stop position in milliseconds" }, - { AVC("buffer"), OFF(m_nBufferMS), OPT_INT, 0, - "Buffer time in milliseconds" }, - { AVC("timeout"), OFF(Link.timeout), OPT_INT, 0, - "Session timeout in seconds" }, - { AVC("pubUser"), OFF(Link.pubUser), OPT_STR, 0, - "Publisher username" }, - { AVC("pubPasswd"), OFF(Link.pubPasswd), OPT_STR, 0, - "Publisher password" }, - { {NULL,0}, 0, 0} -}; - -static const AVal truth[] = { - AVC("1"), - AVC("on"), - AVC("yes"), - AVC("true"), - {0,0} -}; - -static void RTMP_OptUsage() -{ - int i; - - RTMP_Log(RTMP_LOGERROR, "Valid RTMP options are:\n"); - for (i=0; options[i].name.av_len; i++) { - RTMP_Log(RTMP_LOGERROR, "%10s %-7s %s\n", options[i].name.av_val, - optinfo[options[i].otype], options[i].use); - } -} - -static int -parseAMF(AMFObject *obj, AVal *av, int *depth) -{ - AMFObjectProperty prop = {{0,0}}; - int i; - char *p, *arg = av->av_val; - - if (arg[1] == ':') - { - p = (char *)arg+2; - switch(arg[0]) - { - case 'B': - prop.p_type = AMF_BOOLEAN; - prop.p_vu.p_number = atoi(p); - break; - case 'S': - prop.p_type = AMF_STRING; - prop.p_vu.p_aval.av_val = p; - prop.p_vu.p_aval.av_len = av->av_len - (p-arg); - break; - case 'N': - prop.p_type = AMF_NUMBER; - prop.p_vu.p_number = strtod(p, NULL); - break; - case 'Z': - prop.p_type = AMF_NULL; - break; - case 'O': - i = atoi(p); - if (i) - { - prop.p_type = AMF_OBJECT; - } - else - { - (*depth)--; - return 0; - } - break; - default: - return -1; - } - } - else if (arg[2] == ':' && arg[0] == 'N') - { - p = strchr(arg+3, ':'); - if (!p || !*depth) - return -1; - prop.p_name.av_val = (char *)arg+3; - prop.p_name.av_len = p - (arg+3); - - p++; - switch(arg[1]) - { - case 'B': - prop.p_type = AMF_BOOLEAN; - prop.p_vu.p_number = atoi(p); - break; - case 'S': - prop.p_type = AMF_STRING; - prop.p_vu.p_aval.av_val = p; - prop.p_vu.p_aval.av_len = av->av_len - (p-arg); - break; - case 'N': - prop.p_type = AMF_NUMBER; - prop.p_vu.p_number = strtod(p, NULL); - break; - case 'O': - prop.p_type = AMF_OBJECT; - break; - default: - return -1; - } - } - else - return -1; - - if (*depth) - { - AMFObject *o2; - for (i=0; i<*depth; i++) - { - o2 = &obj->o_props[obj->o_num-1].p_vu.p_object; - obj = o2; - } - } - AMF_AddProp(obj, &prop); - if (prop.p_type == AMF_OBJECT) - (*depth)++; - return 0; -} - -int RTMP_SetOpt(RTMP *r, const AVal *opt, AVal *arg) -{ - int i; - void *v; - - for (i=0; options[i].name.av_len; i++) { - if (opt->av_len != options[i].name.av_len) continue; - if (strcasecmp(opt->av_val, options[i].name.av_val)) continue; - v = (char *)r + options[i].off; - switch(options[i].otype) { - case OPT_STR: { - AVal *aptr = v; - *aptr = *arg; } - break; - case OPT_INT: { - long l = strtol(arg->av_val, NULL, 0); - *(int *)v = l; } - break; - case OPT_BOOL: { - int j, fl; - fl = *(int *)v; - for (j=0; truth[j].av_len; j++) { - if (arg->av_len != truth[j].av_len) continue; - if (strcasecmp(arg->av_val, truth[j].av_val)) continue; - fl |= options[i].omisc; break; } - *(int *)v = fl; - } - break; - case OPT_CONN: - if (parseAMF(&r->Link.extras, arg, &r->Link.edepth)) - return FALSE; - break; - } - break; - } - if (!options[i].name.av_len) { - RTMP_Log(RTMP_LOGERROR, "Unknown option %s", opt->av_val); - RTMP_OptUsage(); - return FALSE; - } - - return TRUE; -} - -int RTMP_SetupURL(RTMP *r, char *url) -{ - AVal opt, arg; - char *p1, *p2, *ptr = strchr(url, ' '); - int ret, len; - unsigned int port = 0; - - if (ptr) - *ptr = '\0'; - - len = strlen(url); - ret = RTMP_ParseURL(url, &r->Link.protocol, &r->Link.hostname, - &port, &r->Link.playpath0, &r->Link.app); - if (!ret) - return ret; - r->Link.port = port; - r->Link.playpath = r->Link.playpath0; - - while (ptr) { - *ptr++ = '\0'; - p1 = ptr; - p2 = strchr(p1, '='); - if (!p2) - break; - opt.av_val = p1; - opt.av_len = p2 - p1; - *p2++ = '\0'; - arg.av_val = p2; - ptr = strchr(p2, ' '); - if (ptr) { - *ptr = '\0'; - arg.av_len = ptr - p2; - /* skip repeated spaces */ - while(ptr[1] == ' ') - *ptr++ = '\0'; - } else { - arg.av_len = strlen(p2); - } - - /* unescape */ - port = arg.av_len; - for (p1=p2; port >0;) { - if (*p1 == '\\') { - unsigned int c; - if (port < 3) - return FALSE; - sscanf(p1+1, "%02x", &c); - *p2++ = c; - port -= 3; - p1 += 3; - } else { - *p2++ = *p1++; - port--; - } - } - arg.av_len = p2 - arg.av_val; - - ret = RTMP_SetOpt(r, &opt, &arg); - if (!ret) - return ret; - } - - if (!r->Link.tcUrl.av_len) - { - r->Link.tcUrl.av_val = url; - if (r->Link.app.av_len) - { - if (r->Link.app.av_val < url + len) - { - /* if app is part of original url, just use it */ - r->Link.tcUrl.av_len = r->Link.app.av_len + (r->Link.app.av_val - url); - } - else - { - len = r->Link.hostname.av_len + r->Link.app.av_len + - sizeof("rtmpte://:65535/"); - r->Link.tcUrl.av_val = malloc(len); - r->Link.tcUrl.av_len = snprintf(r->Link.tcUrl.av_val, len, - "%s://%.*s:%d/%.*s", - RTMPProtocolStringsLower[r->Link.protocol], - r->Link.hostname.av_len, r->Link.hostname.av_val, - r->Link.port, - r->Link.app.av_len, r->Link.app.av_val); - r->Link.lFlags |= RTMP_LF_FTCU; - } - } - else - { - r->Link.tcUrl.av_len = strlen(url); - } - } - -#ifdef CRYPTO - if ((r->Link.lFlags & RTMP_LF_SWFV) && r->Link.swfUrl.av_len) - RTMP_HashSWF(r->Link.swfUrl.av_val, &r->Link.SWFSize, - (unsigned char *)r->Link.SWFHash, r->Link.swfAge); -#endif - - SocksSetup(r, &r->Link.sockshost); - - if (r->Link.port == 0) - { - if (r->Link.protocol & RTMP_FEATURE_SSL) - r->Link.port = 443; - else if (r->Link.protocol & RTMP_FEATURE_HTTP) - r->Link.port = 80; - else - r->Link.port = 1935; - } - return TRUE; -} - -int -add_addr_info(struct sockaddr_in *service, AVal *host, int port) -{ - char *hostname; - int ret = TRUE; - if (host->av_val[host->av_len]) - { - hostname = malloc(host->av_len+1); - memcpy(hostname, host->av_val, host->av_len); - hostname[host->av_len] = '\0'; - } - else - { - hostname = host->av_val; - } - - service->sin_addr.s_addr = inet_addr(hostname); - if (service->sin_addr.s_addr == INADDR_NONE) - { - struct hostent *host = gethostbyname(hostname); - if (host == NULL || host->h_addr == NULL) - { - RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname); - ret = FALSE; - goto finish; - } - service->sin_addr = *(struct in_addr *)host->h_addr; - } - - service->sin_port = htons(port); -finish: - if (hostname != host->av_val) - free(hostname); - return ret; -} - -int -RTMP_Connect0(RTMP *r, struct sockaddr * service) -{ - int on = 1; - r->m_sb.sb_timedout = FALSE; - r->m_pausing = 0; - r->m_fDuration = 0.0; - - r->m_sb.sb_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (r->m_sb.sb_socket != -1) - { - if (connect(r->m_sb.sb_socket, service, sizeof(struct sockaddr)) < 0) - { - int err = GetSockError(); - RTMP_Log(RTMP_LOGERROR, "%s, failed to connect socket. %d (%s)", - __FUNCTION__, err, strerror(err)); - RTMP_Close(r); - return FALSE; - } - - if (r->Link.socksport) - { - RTMP_Log(RTMP_LOGDEBUG, "%s ... SOCKS negotiation", __FUNCTION__); - if (!SocksNegotiate(r)) - { - RTMP_Log(RTMP_LOGERROR, "%s, SOCKS negotiation failed.", __FUNCTION__); - RTMP_Close(r); - return FALSE; - } - } - } - else - { - RTMP_Log(RTMP_LOGERROR, "%s, failed to create socket. Error: %d", __FUNCTION__, - GetSockError()); - return FALSE; - } - - /* set timeout */ - { - SET_RCVTIMEO(tv, r->Link.timeout); - if (setsockopt - (r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv))) - { - printf("int here\n"); fflush(stdout); - RTMP_Log(RTMP_LOGERROR, "%s, Setting socket timeout to %ds failed!", - __FUNCTION__, r->Link.timeout); - } - } - - setsockopt(r->m_sb.sb_socket, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)); - - return TRUE; -} - -int -RTMP_TLS_Accept(RTMP *r, void *ctx) -{ -#if defined(CRYPTO) && !defined(NO_SSL) - TLS_server(ctx, r->m_sb.sb_ssl); - TLS_setfd(r->m_sb.sb_ssl, r->m_sb.sb_socket); - if (TLS_accept(r->m_sb.sb_ssl) < 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__); - return FALSE; - } - return TRUE; -#else - return FALSE; -#endif -} - -int -RTMP_Connect1(RTMP *r, RTMPPacket *cp) -{ - if (r->Link.protocol & RTMP_FEATURE_SSL) - { -#if defined(CRYPTO) && !defined(NO_SSL) - TLS_client(RTMP_TLS_ctx, r->m_sb.sb_ssl); - TLS_setfd(r->m_sb.sb_ssl, r->m_sb.sb_socket); - if (TLS_connect(r->m_sb.sb_ssl) < 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__); - RTMP_Close(r); - return FALSE; - } -#else - RTMP_Log(RTMP_LOGERROR, "%s, no SSL/TLS support", __FUNCTION__); - RTMP_Close(r); - return FALSE; - -#endif - } - if (r->Link.protocol & RTMP_FEATURE_HTTP) - { - r->m_msgCounter = 1; - r->m_clientID.av_val = NULL; - r->m_clientID.av_len = 0; - HTTP_Post(r, RTMPT_OPEN, "", 1); - if (HTTP_read(r, 1) != 0) - { - r->m_msgCounter = 0; - RTMP_Log(RTMP_LOGDEBUG, "%s, Could not connect for handshake", __FUNCTION__); - RTMP_Close(r); - return 0; - } - r->m_msgCounter = 0; - } - RTMP_Log(RTMP_LOGDEBUG, "%s, ... connected, handshaking", __FUNCTION__); - if (!HandShake(r, TRUE)) - { - RTMP_Log(RTMP_LOGERROR, "%s, handshake failed.", __FUNCTION__); - RTMP_Close(r); - return FALSE; - } - RTMP_Log(RTMP_LOGDEBUG, "%s, handshaked", __FUNCTION__); - - if (!SendConnectPacket(r, cp)) - { - RTMP_Log(RTMP_LOGERROR, "%s, RTMP connect failed.", __FUNCTION__); - RTMP_Close(r); - return FALSE; - } - return TRUE; -} - -int -RTMP_Connect(RTMP *r, RTMPPacket *cp) -{ - struct sockaddr_in service; - if (!r->Link.hostname.av_len) - return FALSE; - - memset(&service, 0, sizeof(struct sockaddr_in)); - service.sin_family = AF_INET; - - if (r->Link.socksport) - { - /* Connect via SOCKS */ - if (!add_addr_info(&service, &r->Link.sockshost, r->Link.socksport)) - return FALSE; - } - else - { - /* Connect directly */ - if (!add_addr_info(&service, &r->Link.hostname, r->Link.port)) - return FALSE; - } - - if (!RTMP_Connect0(r, (struct sockaddr *)&service)) - return FALSE; - - r->m_bSendCounter = TRUE; - - return RTMP_Connect1(r, cp); -} - -int SocksNegotiate(RTMP *r) -{ - unsigned long addr; - struct sockaddr_in service; - memset(&service, 0, sizeof(struct sockaddr_in)); - - add_addr_info(&service, &r->Link.hostname, r->Link.port); - addr = htonl(service.sin_addr.s_addr); - - { - char packet[] = { - 4, 1, /* SOCKS 4, connect */ - (r->Link.port >> 8) & 0xFF, - (r->Link.port) & 0xFF, - (char)(addr >> 24) & 0xFF, (char)(addr >> 16) & 0xFF, - (char)(addr >> 8) & 0xFF, (char)addr & 0xFF, - 0 - }; /* NULL terminate */ - - WriteN(r, packet, sizeof packet); - - if (ReadN(r, packet, 8) != 8) - return FALSE; - - if (packet[0] == 0 && packet[1] == 90) - { - return TRUE; - } - else - { - RTMP_Log(RTMP_LOGERROR, "%s, SOCKS returned error code %d", __FUNCTION__, packet[1]); - return FALSE; - } - } -} - -int -RTMP_ConnectStream(RTMP *r, int seekTime) -{ - RTMPPacket packet = { 0 }; - - /* seekTime was already set by SetupStream / SetupURL. - * This is only needed by ReconnectStream. - */ - if (seekTime > 0) - r->Link.seekTime = seekTime; - - r->m_mediaChannel = 0; - - while (!r->m_bPlaying && RTMP_IsConnected(r) && RTMP_ReadPacket(r, &packet)) - { - if (RTMPPacket_IsReady(&packet)) - { - if (!packet.m_nBodySize) - continue; - if ((packet.m_packetType == RTMP_PACKET_TYPE_AUDIO) || - (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) || - (packet.m_packetType == RTMP_PACKET_TYPE_INFO)) - { - RTMP_Log(RTMP_LOGWARNING, "Received FLV packet before play()! Ignoring."); - RTMPPacket_Free(&packet); - continue; - } - - RTMP_ClientPacket(r, &packet); - RTMPPacket_Free(&packet); - } - } - - return r->m_bPlaying; -} - -int -RTMP_ReconnectStream(RTMP *r, int seekTime) -{ - RTMP_DeleteStream(r); - - RTMP_SendCreateStream(r); - - return RTMP_ConnectStream(r, seekTime); -} - -int -RTMP_ToggleStream(RTMP *r) -{ - int res; - - if (!r->m_pausing) - { - if (RTMP_IsTimedout(r) && r->m_read.status == RTMP_READ_EOF) - r->m_read.status = 0; - - res = RTMP_SendPause(r, TRUE, r->m_pauseStamp); - if (!res) - return res; - - r->m_pausing = 1; - sleep(1); - } - res = RTMP_SendPause(r, FALSE, r->m_pauseStamp); - r->m_pausing = 3; - return res; -} - -void -RTMP_DeleteStream(RTMP *r) -{ - if (r->m_stream_id < 0) - return; - - r->m_bPlaying = FALSE; - - SendDeleteStream(r, r->m_stream_id); - r->m_stream_id = -1; -} - -int -RTMP_GetNextMediaPacket(RTMP *r, RTMPPacket *packet) -{ - int bHasMediaPacket = 0; - - while (!bHasMediaPacket && RTMP_IsConnected(r) - && RTMP_ReadPacket(r, packet)) - { - if (!RTMPPacket_IsReady(packet) || !packet->m_nBodySize) - { - continue; - } - - bHasMediaPacket = RTMP_ClientPacket(r, packet); - - if (!bHasMediaPacket) - { - RTMPPacket_Free(packet); - } - else if (r->m_pausing == 3) - { - if (packet->m_nTimeStamp <= r->m_mediaStamp) - { - bHasMediaPacket = 0; -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, - "Skipped type: %02X, size: %d, TS: %d ms, abs TS: %d, pause: %d ms", - packet->m_packetType, packet->m_nBodySize, - packet->m_nTimeStamp, packet->m_hasAbsTimestamp, - r->m_mediaStamp); -#endif - RTMPPacket_Free(packet); - continue; - } - r->m_pausing = 0; - } - } - - if (bHasMediaPacket) - r->m_bPlaying = TRUE; - else if (r->m_sb.sb_timedout && !r->m_pausing) - r->m_pauseStamp = r->m_mediaChannel < r->m_channelsAllocatedIn ? - r->m_channelTimestamp[r->m_mediaChannel] : 0; - - return bHasMediaPacket; -} - -int -RTMP_ClientPacket(RTMP *r, RTMPPacket *packet) -{ - int bHasMediaPacket = 0; - switch (packet->m_packetType) - { - case RTMP_PACKET_TYPE_CHUNK_SIZE: - /* chunk size */ - HandleChangeChunkSize(r, packet); - break; - - case RTMP_PACKET_TYPE_BYTES_READ_REPORT: - /* bytes read report */ - RTMP_Log(RTMP_LOGDEBUG, "%s, received: bytes read report", __FUNCTION__); - break; - - case RTMP_PACKET_TYPE_CONTROL: - /* ctrl */ - HandleCtrl(r, packet); - break; - - case RTMP_PACKET_TYPE_SERVER_BW: - /* server bw */ - HandleServerBW(r, packet); - break; - - case RTMP_PACKET_TYPE_CLIENT_BW: - /* client bw */ - HandleClientBW(r, packet); - break; - - case RTMP_PACKET_TYPE_AUDIO: - /* audio data */ - /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: audio %lu bytes", __FUNCTION__, packet.m_nBodySize); */ - HandleAudio(r, packet); - bHasMediaPacket = 1; - if (!r->m_mediaChannel) - r->m_mediaChannel = packet->m_nChannel; - if (!r->m_pausing) - r->m_mediaStamp = packet->m_nTimeStamp; - break; - - case RTMP_PACKET_TYPE_VIDEO: - /* video data */ - /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: video %lu bytes", __FUNCTION__, packet.m_nBodySize); */ - HandleVideo(r, packet); - bHasMediaPacket = 1; - if (!r->m_mediaChannel) - r->m_mediaChannel = packet->m_nChannel; - if (!r->m_pausing) - r->m_mediaStamp = packet->m_nTimeStamp; - break; - - case RTMP_PACKET_TYPE_FLEX_STREAM_SEND: - /* flex stream send */ - RTMP_Log(RTMP_LOGDEBUG, - "%s, flex stream send, size %u bytes, not supported, ignoring", - __FUNCTION__, packet->m_nBodySize); - break; - - case RTMP_PACKET_TYPE_FLEX_SHARED_OBJECT: - /* flex shared object */ - RTMP_Log(RTMP_LOGDEBUG, - "%s, flex shared object, size %u bytes, not supported, ignoring", - __FUNCTION__, packet->m_nBodySize); - break; - - case RTMP_PACKET_TYPE_FLEX_MESSAGE: - /* flex message */ - { - RTMP_Log(RTMP_LOGDEBUG, - "%s, flex message, size %u bytes, not fully supported", - __FUNCTION__, packet->m_nBodySize); - /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ - - /* some DEBUG code */ -#if 0 - RTMP_LIB_AMFObject obj; - int nRes = obj.Decode(packet.m_body+1, packet.m_nBodySize-1); - if(nRes < 0) { - RTMP_Log(RTMP_LOGERROR, "%s, error decoding AMF3 packet", __FUNCTION__); - /*return; */ - } - - obj.Dump(); -#endif - - if (HandleInvoke(r, packet->m_body + 1, packet->m_nBodySize - 1) == 1) - bHasMediaPacket = 2; - break; - } - case RTMP_PACKET_TYPE_INFO: - /* metadata (notify) */ - RTMP_Log(RTMP_LOGDEBUG, "%s, received: notify %u bytes", __FUNCTION__, - packet->m_nBodySize); - if (HandleMetadata(r, packet->m_body, packet->m_nBodySize)) - bHasMediaPacket = 1; - break; - - case RTMP_PACKET_TYPE_SHARED_OBJECT: - RTMP_Log(RTMP_LOGDEBUG, "%s, shared object, not supported, ignoring", - __FUNCTION__); - break; - - case RTMP_PACKET_TYPE_INVOKE: - /* invoke */ - RTMP_Log(RTMP_LOGDEBUG, "%s, received: invoke %u bytes", __FUNCTION__, - packet->m_nBodySize); - /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ - - if (HandleInvoke(r, packet->m_body, packet->m_nBodySize) == 1) - bHasMediaPacket = 2; - break; - - case RTMP_PACKET_TYPE_FLASH_VIDEO: - { - /* go through FLV packets and handle metadata packets */ - unsigned int pos = 0; - uint32_t nTimeStamp = packet->m_nTimeStamp; - - while (pos + 11 < packet->m_nBodySize) - { - uint32_t dataSize = AMF_DecodeInt24(packet->m_body + pos + 1); /* size without header (11) and prevTagSize (4) */ - - if (pos + 11 + dataSize + 4 > packet->m_nBodySize) - { - RTMP_Log(RTMP_LOGWARNING, "Stream corrupt?!"); - break; - } - if (packet->m_body[pos] == 0x12) - { - HandleMetadata(r, packet->m_body + pos + 11, dataSize); - } - else if (packet->m_body[pos] == 8 || packet->m_body[pos] == 9) - { - nTimeStamp = AMF_DecodeInt24(packet->m_body + pos + 4); - nTimeStamp |= (packet->m_body[pos + 7] << 24); - } - pos += (11 + dataSize + 4); - } - if (!r->m_pausing) - r->m_mediaStamp = nTimeStamp; - - /* FLV tag(s) */ - /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: FLV tag(s) %lu bytes", __FUNCTION__, packet.m_nBodySize); */ - bHasMediaPacket = 1; - break; - } - default: - RTMP_Log(RTMP_LOGDEBUG, "%s, unknown packet type received: 0x%02x", __FUNCTION__, - packet->m_packetType); -#ifdef _DEBUG - RTMP_LogHex(RTMP_LOGDEBUG, packet->m_body, packet->m_nBodySize); -#endif - } - - return bHasMediaPacket; -} - -#ifdef _DEBUG -extern FILE *netstackdump; -extern FILE *netstackdump_read; -#endif - -int -ReadN(RTMP *r, char *buffer, int n) -{ - int nOriginalSize = n; - int avail; - char *ptr; - - r->m_sb.sb_timedout = FALSE; - -#ifdef _DEBUG - memset(buffer, 0, n); -#endif - - ptr = buffer; - while (n > 0) - { - int nBytes = 0, nRead; - if (r->Link.protocol & RTMP_FEATURE_HTTP) - { - int refill = 0; - while (!r->m_resplen) - { - int ret; - if (r->m_sb.sb_size < 13 || refill) - { - if (!r->m_unackd) - HTTP_Post(r, RTMPT_IDLE, "", 1); - if (RTMPSockBuf_Fill(&r->m_sb) < 1) - { - if (!r->m_sb.sb_timedout) - RTMP_Close(r); - return 0; - } - } - if ((ret = HTTP_read(r, 0)) == -1) - { - RTMP_Log(RTMP_LOGDEBUG, "%s, No valid HTTP response found", __FUNCTION__); - RTMP_Close(r); - return 0; - } - else if (ret == -2) - { - refill = 1; - } - else - { - refill = 0; - } - } - if (r->m_resplen && !r->m_sb.sb_size) - RTMPSockBuf_Fill(&r->m_sb); - avail = r->m_sb.sb_size; - if (avail > r->m_resplen) - avail = r->m_resplen; - } - else - { - avail = r->m_sb.sb_size; - if (avail == 0) - { - if (RTMPSockBuf_Fill(&r->m_sb) < 1) - { - if (!r->m_sb.sb_timedout) - RTMP_Close(r); - return 0; - } - avail = r->m_sb.sb_size; - } - } - nRead = ((n < avail) ? n : avail); - if (nRead > 0) - { - memcpy(ptr, r->m_sb.sb_start, nRead); - r->m_sb.sb_start += nRead; - r->m_sb.sb_size -= nRead; - nBytes = nRead; - r->m_nBytesIn += nRead; - if (r->m_bSendCounter - && r->m_nBytesIn > ( r->m_nBytesInSent + r->m_nClientBW / 10)) - if (!SendBytesReceived(r)) - return FALSE; - } - /*RTMP_Log(RTMP_LOGDEBUG, "%s: %d bytes\n", __FUNCTION__, nBytes); */ -#ifdef _DEBUG - fwrite(ptr, 1, nBytes, netstackdump_read); -#endif - - if (nBytes == 0) - { - RTMP_Log(RTMP_LOGDEBUG, "%s, RTMP socket closed by peer", __FUNCTION__); - /*goto again; */ - RTMP_Close(r); - break; - } - - if (r->Link.protocol & RTMP_FEATURE_HTTP) - r->m_resplen -= nBytes; - -#ifdef CRYPTO - if (r->Link.rc4keyIn) - { - RC4_encrypt(r->Link.rc4keyIn, nBytes, ptr); - } -#endif - - n -= nBytes; - ptr += nBytes; - } - - return nOriginalSize - n; -} - -int -WriteN(RTMP *r, const char *buffer, int n) -{ - const char *ptr = buffer; -#ifdef CRYPTO - char *encrypted = 0; - char buf[RTMP_BUFFER_CACHE_SIZE]; - - if (r->Link.rc4keyOut) - { - if (n > sizeof(buf)) - encrypted = (char *)malloc(n); - else - encrypted = (char *)buf; - ptr = encrypted; - RC4_encrypt2(r->Link.rc4keyOut, n, buffer, ptr); - } -#endif - - while (n > 0) - { - int nBytes; - - if (r->Link.protocol & RTMP_FEATURE_HTTP) - nBytes = HTTP_Post(r, RTMPT_SEND, ptr, n); - else - nBytes = RTMPSockBuf_Send(&r->m_sb, ptr, n); - /*RTMP_Log(RTMP_LOGDEBUG, "%s: %d\n", __FUNCTION__, nBytes); */ - - if (nBytes < 0) - { - int sockerr = GetSockError(); - RTMP_Log(RTMP_LOGERROR, "%s, RTMP send error %d (%d bytes)", __FUNCTION__, - sockerr, n); - - if (sockerr == EINTR && !RTMP_ctrlC) - continue; - - RTMP_Close(r); - n = 1; - break; - } - - if (nBytes == 0) - break; - - n -= nBytes; - ptr += nBytes; - } - -#ifdef CRYPTO - if (encrypted && encrypted != buf) - free(encrypted); -#endif - - return n == 0; -} - -#define SAVC(x) static const AVal av_##x = AVC(#x) - -SAVC(app); -SAVC(connect); -SAVC(flashVer); -SAVC(swfUrl); -SAVC(pageUrl); -SAVC(tcUrl); -SAVC(fpad); -SAVC(capabilities); -SAVC(audioCodecs); -SAVC(videoCodecs); -SAVC(videoFunction); -SAVC(objectEncoding); -SAVC(secureToken); -SAVC(secureTokenResponse); -SAVC(type); -SAVC(nonprivate); - -int -SendConnectPacket(RTMP *r, RTMPPacket *cp) -{ - RTMPPacket packet; - char pbuf[4096], *pend = pbuf + sizeof(pbuf); - char *enc; - - if (cp) - return RTMP_SendPacket(r, cp, TRUE); - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_LARGE; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_connect); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_OBJECT; - - enc = AMF_EncodeNamedString(enc, pend, &av_app, &r->Link.app); - if (!enc) - return FALSE; - if (r->Link.protocol & RTMP_FEATURE_WRITE) - { - enc = AMF_EncodeNamedString(enc, pend, &av_type, &av_nonprivate); - if (!enc) - return FALSE; - } - if (r->Link.flashVer.av_len) - { - enc = AMF_EncodeNamedString(enc, pend, &av_flashVer, &r->Link.flashVer); - if (!enc) - return FALSE; - } - if (r->Link.swfUrl.av_len) - { - enc = AMF_EncodeNamedString(enc, pend, &av_swfUrl, &r->Link.swfUrl); - if (!enc) - return FALSE; - } - if (r->Link.tcUrl.av_len) - { - enc = AMF_EncodeNamedString(enc, pend, &av_tcUrl, &r->Link.tcUrl); - if (!enc) - return FALSE; - } - if (!(r->Link.protocol & RTMP_FEATURE_WRITE)) - { - enc = AMF_EncodeNamedBoolean(enc, pend, &av_fpad, FALSE); - if (!enc) - return FALSE; - enc = AMF_EncodeNamedNumber(enc, pend, &av_capabilities, 15.0); - if (!enc) - return FALSE; - enc = AMF_EncodeNamedNumber(enc, pend, &av_audioCodecs, r->m_fAudioCodecs); - if (!enc) - return FALSE; - enc = AMF_EncodeNamedNumber(enc, pend, &av_videoCodecs, r->m_fVideoCodecs); - if (!enc) - return FALSE; - enc = AMF_EncodeNamedNumber(enc, pend, &av_videoFunction, 1.0); - if (!enc) - return FALSE; - if (r->Link.pageUrl.av_len) - { - enc = AMF_EncodeNamedString(enc, pend, &av_pageUrl, &r->Link.pageUrl); - if (!enc) - return FALSE; - } - } - if (r->m_fEncoding != 0.0 || r->m_bSendEncoding) - { /* AMF0, AMF3 not fully supported yet */ - enc = AMF_EncodeNamedNumber(enc, pend, &av_objectEncoding, r->m_fEncoding); - if (!enc) - return FALSE; - } - if (enc + 3 >= pend) - return FALSE; - *enc++ = 0; - *enc++ = 0; /* end of object - 0x00 0x00 0x09 */ - *enc++ = AMF_OBJECT_END; - - /* add auth string */ - if (r->Link.auth.av_len) - { - enc = AMF_EncodeBoolean(enc, pend, r->Link.lFlags & RTMP_LF_AUTH); - if (!enc) - return FALSE; - enc = AMF_EncodeString(enc, pend, &r->Link.auth); - if (!enc) - return FALSE; - } - if (r->Link.extras.o_num) - { - int i; - for (i = 0; i < r->Link.extras.o_num; i++) - { - enc = AMFProp_Encode(&r->Link.extras.o_props[i], enc, pend); - if (!enc) - return FALSE; - } - } - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, TRUE); -} - -#if 0 /* unused */ -SAVC(bgHasStream); - -static int -SendBGHasStream(RTMP *r, double dId, AVal *playpath) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_bgHasStream); - enc = AMF_EncodeNumber(enc, pend, dId); - *enc++ = AMF_NULL; - - enc = AMF_EncodeString(enc, pend, playpath); - if (enc == NULL) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, TRUE); -} -#endif - -SAVC(createStream); - -int -RTMP_SendCreateStream(RTMP *r) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_createStream); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; /* NULL */ - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, TRUE); -} - -SAVC(FCSubscribe); - -int -SendFCSubscribe(RTMP *r, AVal *subscribepath) -{ - RTMPPacket packet; - char pbuf[512], *pend = pbuf + sizeof(pbuf); - char *enc; - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - RTMP_Log(RTMP_LOGDEBUG, "FCSubscribe: %s", subscribepath->av_val); - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_FCSubscribe); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeString(enc, pend, subscribepath); - - if (!enc) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, TRUE); -} - -/* Justin.tv specific authentication */ -static const AVal av_NetStream_Authenticate_UsherToken = AVC("NetStream.Authenticate.UsherToken"); - -int -SendUsherToken(RTMP *r, AVal *usherToken) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - RTMP_Log(RTMP_LOGDEBUG, "UsherToken: %s", usherToken->av_val); - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_NetStream_Authenticate_UsherToken); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeString(enc, pend, usherToken); - - if (!enc) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} -/******************************************/ - -SAVC(releaseStream); - -int -SendReleaseStream(RTMP *r) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_releaseStream); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeString(enc, pend, &r->Link.playpath); - if (!enc) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(FCPublish); - -int -SendFCPublish(RTMP *r) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_FCPublish); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeString(enc, pend, &r->Link.playpath); - if (!enc) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(FCUnpublish); - -int -SendFCUnpublish(RTMP *r) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_FCUnpublish); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeString(enc, pend, &r->Link.playpath); - if (!enc) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(publish); -SAVC(live); -SAVC(record); - -int -SendPublish(RTMP *r) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x04; /* source channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_LARGE; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = r->m_stream_id; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_publish); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeString(enc, pend, &r->Link.playpath); - if (!enc) - return FALSE; - - /* FIXME: should we choose live based on Link.lFlags & RTMP_LF_LIVE? */ - enc = AMF_EncodeString(enc, pend, &av_live); - if (!enc) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, TRUE); -} - -SAVC(deleteStream); - -int -SendDeleteStream(RTMP *r, double dStreamId) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_deleteStream); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeNumber(enc, pend, dStreamId); - - packet.m_nBodySize = enc - packet.m_body; - - /* no response expected */ - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(pause); - -int -RTMP_SendPause(RTMP *r, int DoPause, int iTime) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x08; /* video channel */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_pause); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeBoolean(enc, pend, DoPause); - enc = AMF_EncodeNumber(enc, pend, (double)iTime); - - packet.m_nBodySize = enc - packet.m_body; - - RTMP_Log(RTMP_LOGDEBUG, "%s, %d, pauseTime=%d", __FUNCTION__, DoPause, iTime); - return RTMP_SendPacket(r, &packet, TRUE); -} - -int RTMP_Pause(RTMP *r, int DoPause) -{ - if (DoPause) - r->m_pauseStamp = r->m_mediaChannel < r->m_channelsAllocatedIn ? - r->m_channelTimestamp[r->m_mediaChannel] : 0; - return RTMP_SendPause(r, DoPause, r->m_pauseStamp); -} - -SAVC(seek); - -int -RTMP_SendSeek(RTMP *r, int iTime) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x08; /* video channel */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_seek); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeNumber(enc, pend, (double)iTime); - - packet.m_nBodySize = enc - packet.m_body; - - r->m_read.flags |= RTMP_READ_SEEKING; - r->m_read.nResumeTS = 0; - - return RTMP_SendPacket(r, &packet, TRUE); -} - -int -RTMP_SendServerBW(RTMP *r) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - - packet.m_nChannel = 0x02; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_LARGE; - packet.m_packetType = RTMP_PACKET_TYPE_SERVER_BW; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - packet.m_nBodySize = 4; - - AMF_EncodeInt32(packet.m_body, pend, r->m_nServerBW); - return RTMP_SendPacket(r, &packet, FALSE); -} - -int -RTMP_SendClientBW(RTMP *r) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - - packet.m_nChannel = 0x02; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_LARGE; - packet.m_packetType = RTMP_PACKET_TYPE_CLIENT_BW; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - packet.m_nBodySize = 5; - - AMF_EncodeInt32(packet.m_body, pend, r->m_nClientBW); - packet.m_body[4] = r->m_nClientBW2; - return RTMP_SendPacket(r, &packet, FALSE); -} - -int -SendBytesReceived(RTMP *r) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - - packet.m_nChannel = 0x02; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_BYTES_READ_REPORT; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - packet.m_nBodySize = 4; - - AMF_EncodeInt32(packet.m_body, pend, r->m_nBytesIn); /* hard coded for now */ - r->m_nBytesInSent = r->m_nBytesIn; - - /*RTMP_Log(RTMP_LOGDEBUG, "Send bytes report. 0x%x (%d bytes)", (unsigned int)m_nBytesIn, m_nBytesIn); */ - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(_checkbw); - -int -SendCheckBW(RTMP *r) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_LARGE; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; /* RTMP_GetTime(); */ - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av__checkbw); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - - packet.m_nBodySize = enc - packet.m_body; - - /* triggers _onbwcheck and eventually results in _onbwdone */ - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(_result); - -int -SendCheckBWResult(RTMP *r, double txn) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0x16 * r->m_nBWCheckCounter; /* temp inc value. till we figure it out. */ - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av__result); - enc = AMF_EncodeNumber(enc, pend, txn); - *enc++ = AMF_NULL; - enc = AMF_EncodeNumber(enc, pend, (double)r->m_nBWCheckCounter++); - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(ping); -SAVC(pong); - -int -SendPong(RTMP *r, double txn) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0x16 * r->m_nBWCheckCounter; /* temp inc value. till we figure it out. */ - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_pong); - enc = AMF_EncodeNumber(enc, pend, txn); - *enc++ = AMF_NULL; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(play); - -int -SendPlay(RTMP *r) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x08; /* we make 8 our stream channel */ - packet.m_headerType = RTMP_PACKET_SIZE_LARGE; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = r->m_stream_id; /*0x01000000; */ - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_play); - enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); - *enc++ = AMF_NULL; - - RTMP_Log(RTMP_LOGDEBUG, "%s, seekTime=%d, stopTime=%d, sending play: %s", - __FUNCTION__, r->Link.seekTime, r->Link.stopTime, - r->Link.playpath.av_val); - enc = AMF_EncodeString(enc, pend, &r->Link.playpath); - if (!enc) - return FALSE; - - /* Optional parameters start and len. - * - * start: -2, -1, 0, positive number - * -2: looks for a live stream, then a recorded stream, - * if not found any open a live stream - * -1: plays a live stream - * >=0: plays a recorded streams from 'start' milliseconds - */ - if (r->Link.lFlags & RTMP_LF_LIVE) - enc = AMF_EncodeNumber(enc, pend, -1000.0); - else - { - if (r->Link.seekTime > 0.0) - enc = AMF_EncodeNumber(enc, pend, r->Link.seekTime); /* resume from here */ - else - enc = AMF_EncodeNumber(enc, pend, 0.0); /*-2000.0);*/ /* recorded as default, -2000.0 is not reliable since that freezes the player if the stream is not found */ - } - if (!enc) - return FALSE; - - /* len: -1, 0, positive number - * -1: plays live or recorded stream to the end (default) - * 0: plays a frame 'start' ms away from the beginning - * >0: plays a live or recoded stream for 'len' milliseconds - */ - /*enc += EncodeNumber(enc, -1.0); */ /* len */ - if (r->Link.stopTime) - { - enc = AMF_EncodeNumber(enc, pend, r->Link.stopTime - r->Link.seekTime); - if (!enc) - return FALSE; - } - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, TRUE); -} - -SAVC(set_playlist); -SAVC(0); - -int -SendPlaylist(RTMP *r) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x08; /* we make 8 our stream channel */ - packet.m_headerType = RTMP_PACKET_SIZE_LARGE; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = r->m_stream_id; /*0x01000000; */ - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_set_playlist); - enc = AMF_EncodeNumber(enc, pend, 0); - *enc++ = AMF_NULL; - *enc++ = AMF_ECMA_ARRAY; - *enc++ = 0; - *enc++ = 0; - *enc++ = 0; - *enc++ = AMF_OBJECT; - enc = AMF_EncodeNamedString(enc, pend, &av_0, &r->Link.playpath); - if (!enc) - return FALSE; - if (enc + 3 >= pend) - return FALSE; - *enc++ = 0; - *enc++ = 0; - *enc++ = AMF_OBJECT_END; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, TRUE); -} - -int -SendSecureTokenResponse(RTMP *r, AVal *resp) -{ - RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_secureTokenResponse); - enc = AMF_EncodeNumber(enc, pend, 0.0); - *enc++ = AMF_NULL; - enc = AMF_EncodeString(enc, pend, resp); - if (!enc) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} - -/* -from http://jira.red5.org/confluence/display/docs/Ping: - -Ping is the most mysterious message in RTMP and till now we haven't fully interpreted it yet. In summary, Ping message is used as a special command that are exchanged between client and server. This page aims to document all known Ping messages. Expect the list to grow. - -The type of Ping packet is 0x4 and contains two mandatory parameters and two optional parameters. The first parameter is the type of Ping and in short integer. The second parameter is the target of the ping. As Ping is always sent in Channel 2 (control channel) and the target object in RTMP header is always 0 which means the Connection object, it's necessary to put an extra parameter to indicate the exact target object the Ping is sent to. The second parameter takes this responsibility. The value has the same meaning as the target object field in RTMP header. (The second value could also be used as other purposes, like RTT Ping/Pong. It is used as the timestamp.) The third and fourth parameters are optional and could be looked upon as the parameter of the Ping packet. Below is an unexhausted list of Ping messages. - - * type 0: Clear the stream. No third and fourth parameters. The second parameter could be 0. After the connection is established, a Ping 0,0 will be sent from server to client. The message will also be sent to client on the start of Play and in response of a Seek or Pause/Resume request. This Ping tells client to re-calibrate the clock with the timestamp of the next packet server sends. - * type 1: Tell the stream to clear the playing buffer. - * type 3: Buffer time of the client. The third parameter is the buffer time in millisecond. - * type 4: Reset a stream. Used together with type 0 in the case of VOD. Often sent before type 0. - * type 6: Ping the client from server. The second parameter is the current time. - * type 7: Pong reply from client. The second parameter is the time the server sent with his ping request. - * type 26: SWFVerification request - * type 27: SWFVerification response -*/ -int -RTMP_SendCtrl(RTMP *r, short nType, unsigned int nObject, unsigned int nTime) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf + sizeof(pbuf); - int nSize; - char *buf; - - RTMP_Log(RTMP_LOGDEBUG, "sending ctrl. type: 0x%04x", (unsigned short)nType); - - packet.m_nChannel = 0x02; /* control channel (ping) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = RTMP_PACKET_TYPE_CONTROL; - packet.m_nTimeStamp = 0; /* RTMP_GetTime(); */ - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - switch(nType) { - case 0x03: nSize = 10; break; /* buffer time */ - case 0x1A: nSize = 3; break; /* SWF verify request */ - case 0x1B: nSize = 44; break; /* SWF verify response */ - default: nSize = 6; break; - } - - packet.m_nBodySize = nSize; - - buf = packet.m_body; - buf = AMF_EncodeInt16(buf, pend, nType); - - if (nType == 0x1B) - { -#ifdef CRYPTO - memcpy(buf, r->Link.SWFVerificationResponse, 42); - RTMP_Log(RTMP_LOGDEBUG, "Sending SWFVerification response: "); - RTMP_LogHex(RTMP_LOGDEBUG, (uint8_t *)packet.m_body, packet.m_nBodySize); -#endif - } - else if (nType == 0x1A) - { - *buf = nObject & 0xff; - } - else - { - if (nSize > 2) - buf = AMF_EncodeInt32(buf, pend, nObject); - - if (nSize > 6) - buf = AMF_EncodeInt32(buf, pend, nTime); - } - - return RTMP_SendPacket(r, &packet, FALSE); -} - -void -AV_erase(RTMP_METHOD *vals, int *num, int i, int freeit) -{ - if (freeit) - free(vals[i].name.av_val); - (*num)--; - for (; i < *num; i++) - { - vals[i] = vals[i + 1]; - } - vals[i].name.av_val = NULL; - vals[i].name.av_len = 0; - vals[i].num = 0; -} - -void -RTMP_DropRequest(RTMP *r, int i, int freeit) -{ - AV_erase(r->m_methodCalls, &r->m_numCalls, i, freeit); -} - -void -AV_queue(RTMP_METHOD **vals, int *num, AVal *av, int txn) -{ - char *tmp; - if (!(*num & 0x0f)) - *vals = realloc(*vals, (*num + 16) * sizeof(RTMP_METHOD)); - tmp = malloc(av->av_len + 1); - memcpy(tmp, av->av_val, av->av_len); - tmp[av->av_len] = '\0'; - (*vals)[*num].num = txn; - (*vals)[*num].name.av_len = av->av_len; - (*vals)[(*num)++].name.av_val = tmp; -} - -void -AV_clear(RTMP_METHOD *vals, int num) -{ - int i; - for (i = 0; i < num; i++) - free(vals[i].name.av_val); - free(vals); -} - - -#ifdef CRYPTO -static int -b64enc(const unsigned char *input, int length, char *output, int maxsize) -{ -#ifdef USE_POLARSSL - size_t buf_size = maxsize; - if(base64_encode((unsigned char *) output, &buf_size, input, length) == 0) - { - output[buf_size] = '\0'; - return 1; - } - else - { - RTMP_Log(RTMP_LOGDEBUG, "%s, error", __FUNCTION__); - return 0; - } -#elif defined(USE_GNUTLS) - if (BASE64_ENCODE_RAW_LENGTH(length) <= maxsize) - base64_encode_raw((uint8_t*) output, length, input); - else - { - RTMP_Log(RTMP_LOGDEBUG, "%s, error", __FUNCTION__); - return 0; - } -#else /* USE_OPENSSL */ - BIO *bmem, *b64; - BUF_MEM *bptr; - - b64 = BIO_new(BIO_f_base64()); - bmem = BIO_new(BIO_s_mem()); - b64 = BIO_push(b64, bmem); - BIO_write(b64, input, length); - if (BIO_flush(b64) == 1) - { - BIO_get_mem_ptr(b64, &bptr); - memcpy(output, bptr->data, bptr->length-1); - output[bptr->length-1] = '\0'; - } - else - { - RTMP_Log(RTMP_LOGDEBUG, "%s, error", __FUNCTION__); - return 0; - } - BIO_free_all(b64); -#endif - return 1; -} - -#ifdef USE_POLARSSL -#define MD5_CTX md5_context -#define MD5_Init(ctx) md5_starts(ctx) -#define MD5_Update(ctx,data,len) md5_update(ctx,(unsigned char *)data,len) -#define MD5_Final(dig,ctx) md5_finish(ctx,dig) -#elif defined(USE_GNUTLS) -typedef struct md5_ctx MD5_CTX; -#define MD5_Init(ctx) md5_init(ctx) -#define MD5_Update(ctx,data,len) md5_update(ctx,len,data) -#define MD5_Final(dig,ctx) md5_digest(ctx,MD5_DIGEST_LENGTH,dig) -#else -#endif - -static const AVal av_authmod_adobe = AVC("authmod=adobe"); -static const AVal av_authmod_llnw = AVC("authmod=llnw"); - -static void hexenc(unsigned char *inbuf, int len, char *dst) -{ - char *ptr = dst; - while(len--) { - sprintf(ptr, "%02x", *inbuf++); - ptr += 2; - } - *ptr = '\0'; -} - -static char * -AValChr(AVal *av, char c) -{ - int i; - for (i = 0; i < av->av_len; i++) - if (av->av_val[i] == c) - return &av->av_val[i]; - return NULL; -} - -static int -PublisherAuth(RTMP *r, AVal *description) -{ - char *token_in = NULL; - char *ptr; - unsigned char md5sum_val[MD5_DIGEST_LENGTH+1]; - MD5_CTX md5ctx; - int challenge2_data; -#define RESPONSE_LEN 32 -#define CHALLENGE2_LEN 16 -#define SALTED2_LEN (32+8+8+8) -#define B64DIGEST_LEN 24 /* 16 byte digest => 22 b64 chars + 2 chars padding */ -#define B64INT_LEN 8 /* 4 byte int => 6 b64 chars + 2 chars padding */ -#define HEXHASH_LEN (2*MD5_DIGEST_LENGTH) - char response[RESPONSE_LEN]; - char challenge2[CHALLENGE2_LEN]; - char salted2[SALTED2_LEN]; - AVal pubToken; - - if (strstr(description->av_val, av_authmod_adobe.av_val) != NULL) - { - if(strstr(description->av_val, "code=403 need auth") != NULL) - { - if (strstr(r->Link.app.av_val, av_authmod_adobe.av_val) != NULL) { - RTMP_Log(RTMP_LOGERROR, "%s, wrong pubUser & pubPasswd for publisher auth", __FUNCTION__); - return 0; - } else if(r->Link.pubUser.av_len && r->Link.pubPasswd.av_len) { - pubToken.av_val = malloc(r->Link.pubUser.av_len + av_authmod_adobe.av_len + 8); - pubToken.av_len = sprintf(pubToken.av_val, "?%s&user=%s", - av_authmod_adobe.av_val, - r->Link.pubUser.av_val); - RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken1: %s", __FUNCTION__, pubToken.av_val); - } else { - RTMP_Log(RTMP_LOGERROR, "%s, need to set pubUser & pubPasswd for publisher auth", __FUNCTION__); - return 0; - } - } - else if((token_in = strstr(description->av_val, "?reason=needauth")) != NULL) - { - char *par, *val = NULL, *orig_ptr; - AVal user, salt, opaque, challenge, *aptr = NULL; - opaque.av_len = 0; - challenge.av_len = 0; - - ptr = orig_ptr = strdup(token_in); - while (ptr) - { - par = ptr; - ptr = strchr(par, '&'); - if(ptr) - *ptr++ = '\0'; - - val = strchr(par, '='); - if(val) - *val++ = '\0'; - - if (aptr) { - aptr->av_len = par - aptr->av_val - 1; - aptr = NULL; - } - if (strcmp(par, "user") == 0){ - user.av_val = val; - aptr = &user; - } else if (strcmp(par, "salt") == 0){ - salt.av_val = val; - aptr = &salt; - } else if (strcmp(par, "opaque") == 0){ - opaque.av_val = val; - aptr = &opaque; - } else if (strcmp(par, "challenge") == 0){ - challenge.av_val = val; - aptr = &challenge; - } - - RTMP_Log(RTMP_LOGDEBUG, "%s, par:\"%s\" = val:\"%s\"", __FUNCTION__, par, val); - } - if (aptr) - aptr->av_len = strlen(aptr->av_val); - - /* hash1 = base64enc(md5(user + _aodbeAuthSalt + password)) */ - MD5_Init(&md5ctx); - MD5_Update(&md5ctx, user.av_val, user.av_len); - MD5_Update(&md5ctx, salt.av_val, salt.av_len); - MD5_Update(&md5ctx, r->Link.pubPasswd.av_val, r->Link.pubPasswd.av_len); - MD5_Final(md5sum_val, &md5ctx); - RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s%s%s) =>", __FUNCTION__, - user.av_val, salt.av_val, r->Link.pubPasswd.av_val); - RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); - - b64enc(md5sum_val, MD5_DIGEST_LENGTH, salted2, SALTED2_LEN); - RTMP_Log(RTMP_LOGDEBUG, "%s, b64(md5_1) = %s", __FUNCTION__, salted2); - - challenge2_data = rand(); - - b64enc((unsigned char *) &challenge2_data, sizeof(int), challenge2, CHALLENGE2_LEN); - RTMP_Log(RTMP_LOGDEBUG, "%s, b64(%d) = %s", __FUNCTION__, challenge2_data, challenge2); - - MD5_Init(&md5ctx); - MD5_Update(&md5ctx, salted2, B64DIGEST_LEN); - /* response = base64enc(md5(hash1 + opaque + challenge2)) */ - if (opaque.av_len) - MD5_Update(&md5ctx, opaque.av_val, opaque.av_len); - else if (challenge.av_len) - MD5_Update(&md5ctx, challenge.av_val, challenge.av_len); - MD5_Update(&md5ctx, challenge2, B64INT_LEN); - MD5_Final(md5sum_val, &md5ctx); - - RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s%s%s) =>", __FUNCTION__, - salted2, opaque.av_len ? opaque.av_val : "", challenge2); - RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); - - b64enc(md5sum_val, MD5_DIGEST_LENGTH, response, RESPONSE_LEN); - RTMP_Log(RTMP_LOGDEBUG, "%s, b64(md5_2) = %s", __FUNCTION__, response); - - /* have all hashes, create auth token for the end of app */ - pubToken.av_val = malloc(32 + B64INT_LEN + B64DIGEST_LEN + opaque.av_len); - pubToken.av_len = sprintf(pubToken.av_val, - "&challenge=%s&response=%s&opaque=%s", - challenge2, - response, - opaque.av_len ? opaque.av_val : ""); - RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken2: %s", __FUNCTION__, pubToken.av_val); - free(orig_ptr); - } - else if(strstr(description->av_val, "?reason=authfailed") != NULL) - { - RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: wrong password", __FUNCTION__); - return 0; - } - else if(strstr(description->av_val, "?reason=nosuchuser") != NULL) - { - RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: no such user", __FUNCTION__); - return 0; - } - else - { - RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: unknown auth mode: %s", - __FUNCTION__, description->av_val); - return 0; - } - - ptr = malloc(r->Link.app.av_len + pubToken.av_len); - strncpy(ptr, r->Link.app.av_val, r->Link.app.av_len); - strncpy(ptr + r->Link.app.av_len, pubToken.av_val, pubToken.av_len); - r->Link.app.av_len += pubToken.av_len; - if(r->Link.lFlags & RTMP_LF_FAPU) - free(r->Link.app.av_val); - r->Link.app.av_val = ptr; - - ptr = malloc(r->Link.tcUrl.av_len + pubToken.av_len); - strncpy(ptr, r->Link.tcUrl.av_val, r->Link.tcUrl.av_len); - strncpy(ptr + r->Link.tcUrl.av_len, pubToken.av_val, pubToken.av_len); - r->Link.tcUrl.av_len += pubToken.av_len; - if(r->Link.lFlags & RTMP_LF_FTCU) - free(r->Link.tcUrl.av_val); - r->Link.tcUrl.av_val = ptr; - - free(pubToken.av_val); - r->Link.lFlags |= RTMP_LF_FTCU | RTMP_LF_FAPU; - - RTMP_Log(RTMP_LOGDEBUG, "%s, new app: %.*s tcUrl: %.*s playpath: %s", __FUNCTION__, - r->Link.app.av_len, r->Link.app.av_val, - r->Link.tcUrl.av_len, r->Link.tcUrl.av_val, - r->Link.playpath.av_val); - } - else if (strstr(description->av_val, av_authmod_llnw.av_val) != NULL) - { - if(strstr(description->av_val, "code=403 need auth") != NULL) - { - /* This part seems to be the same for llnw and adobe */ - - if (strstr(r->Link.app.av_val, av_authmod_llnw.av_val) != NULL) { - RTMP_Log(RTMP_LOGERROR, "%s, wrong pubUser & pubPasswd for publisher auth", __FUNCTION__); - return 0; - } else if(r->Link.pubUser.av_len && r->Link.pubPasswd.av_len) { - pubToken.av_val = malloc(r->Link.pubUser.av_len + av_authmod_llnw.av_len + 8); - pubToken.av_len = sprintf(pubToken.av_val, "?%s&user=%s", - av_authmod_llnw.av_val, - r->Link.pubUser.av_val); - RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken1: %s", __FUNCTION__, pubToken.av_val); - } else { - RTMP_Log(RTMP_LOGERROR, "%s, need to set pubUser & pubPasswd for publisher auth", __FUNCTION__); - return 0; - } - } - else if((token_in = strstr(description->av_val, "?reason=needauth")) != NULL) - { - char *orig_ptr; - char *par, *val = NULL; - char hash1[HEXHASH_LEN+1], hash2[HEXHASH_LEN+1], hash3[HEXHASH_LEN+1]; - AVal user, nonce, *aptr = NULL; - AVal apptmp; - - /* llnw auth method - * Seems to be closely based on HTTP Digest Auth: - * http://tools.ietf.org/html/rfc2617 - * http://en.wikipedia.org/wiki/Digest_access_authentication - */ - - const char authmod[] = "llnw"; - const char realm[] = "live"; - const char method[] = "publish"; - const char qop[] = "auth"; - /* nc = 1..connection count (or rather, number of times cnonce has been reused) */ - int nc = 1; - /* nchex = hexenc(nc) (8 hex digits according to RFC 2617) */ - char nchex[9]; - /* cnonce = hexenc(4 random bytes) (initialized on first connection) */ - char cnonce[9]; - - ptr = orig_ptr = strdup(token_in); - /* Extract parameters (we need user and nonce) */ - while (ptr) - { - par = ptr; - ptr = strchr(par, '&'); - if(ptr) - *ptr++ = '\0'; - - val = strchr(par, '='); - if(val) - *val++ = '\0'; - - if (aptr) { - aptr->av_len = par - aptr->av_val - 1; - aptr = NULL; - } - if (strcmp(par, "user") == 0){ - user.av_val = val; - aptr = &user; - } else if (strcmp(par, "nonce") == 0){ - nonce.av_val = val; - aptr = &nonce; - } - - RTMP_Log(RTMP_LOGDEBUG, "%s, par:\"%s\" = val:\"%s\"", __FUNCTION__, par, val); - } - if (aptr) - aptr->av_len = strlen(aptr->av_val); - - /* FIXME: handle case where user==NULL or nonce==NULL */ - - sprintf(nchex, "%08x", nc); - sprintf(cnonce, "%08x", rand()); - - /* hash1 = hexenc(md5(user + ":" + realm + ":" + password)) */ - MD5_Init(&md5ctx); - MD5_Update(&md5ctx, user.av_val, user.av_len); - MD5_Update(&md5ctx, ":", 1); - MD5_Update(&md5ctx, realm, sizeof(realm)-1); - MD5_Update(&md5ctx, ":", 1); - MD5_Update(&md5ctx, r->Link.pubPasswd.av_val, r->Link.pubPasswd.av_len); - MD5_Final(md5sum_val, &md5ctx); - RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s:%s:%s) =>", __FUNCTION__, - user.av_val, realm, r->Link.pubPasswd.av_val); - RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); - hexenc(md5sum_val, MD5_DIGEST_LENGTH, hash1); - - /* hash2 = hexenc(md5(method + ":/" + app + "/" + appInstance)) */ - /* Extract appname + appinstance without query parameters */ - apptmp = r->Link.app; - ptr = AValChr(&apptmp, '?'); - if (ptr) - apptmp.av_len = ptr - apptmp.av_val; - - MD5_Init(&md5ctx); - MD5_Update(&md5ctx, method, sizeof(method)-1); - MD5_Update(&md5ctx, ":/", 2); - MD5_Update(&md5ctx, apptmp.av_val, apptmp.av_len); - if (!AValChr(&apptmp, '/')) - MD5_Update(&md5ctx, "/_definst_", sizeof("/_definst_") - 1); - MD5_Final(md5sum_val, &md5ctx); - RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s:/%.*s) =>", __FUNCTION__, - method, apptmp.av_len, apptmp.av_val); - RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); - hexenc(md5sum_val, MD5_DIGEST_LENGTH, hash2); - - /* hash3 = hexenc(md5(hash1 + ":" + nonce + ":" + nchex + ":" + cnonce + ":" + qop + ":" + hash2)) */ - MD5_Init(&md5ctx); - MD5_Update(&md5ctx, hash1, HEXHASH_LEN); - MD5_Update(&md5ctx, ":", 1); - MD5_Update(&md5ctx, nonce.av_val, nonce.av_len); - MD5_Update(&md5ctx, ":", 1); - MD5_Update(&md5ctx, nchex, sizeof(nchex)-1); - MD5_Update(&md5ctx, ":", 1); - MD5_Update(&md5ctx, cnonce, sizeof(cnonce)-1); - MD5_Update(&md5ctx, ":", 1); - MD5_Update(&md5ctx, qop, sizeof(qop)-1); - MD5_Update(&md5ctx, ":", 1); - MD5_Update(&md5ctx, hash2, HEXHASH_LEN); - MD5_Final(md5sum_val, &md5ctx); - RTMP_Log(RTMP_LOGDEBUG, "%s, md5(%s:%s:%s:%s:%s:%s) =>", __FUNCTION__, - hash1, nonce.av_val, nchex, cnonce, qop, hash2); - RTMP_LogHexString(RTMP_LOGDEBUG, md5sum_val, MD5_DIGEST_LENGTH); - hexenc(md5sum_val, MD5_DIGEST_LENGTH, hash3); - - /* pubToken = &authmod=&user=&nonce=&cnonce=&nc=&response= */ - /* Append nonces and response to query string which already contains - * user + authmod */ - pubToken.av_val = malloc(64 + sizeof(authmod)-1 + user.av_len + nonce.av_len + sizeof(cnonce)-1 + sizeof(nchex)-1 + HEXHASH_LEN); - sprintf(pubToken.av_val, - "&nonce=%s&cnonce=%s&nc=%s&response=%s", - nonce.av_val, cnonce, nchex, hash3); - pubToken.av_len = strlen(pubToken.av_val); - RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken2: %s", __FUNCTION__, pubToken.av_val); - - free(orig_ptr); - } - else if(strstr(description->av_val, "?reason=authfail") != NULL) - { - RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed", __FUNCTION__); - return 0; - } - else if(strstr(description->av_val, "?reason=nosuchuser") != NULL) - { - RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: no such user", __FUNCTION__); - return 0; - } - else - { - RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: unknown auth mode: %s", - __FUNCTION__, description->av_val); - return 0; - } - - ptr = malloc(r->Link.app.av_len + pubToken.av_len); - strncpy(ptr, r->Link.app.av_val, r->Link.app.av_len); - strncpy(ptr + r->Link.app.av_len, pubToken.av_val, pubToken.av_len); - r->Link.app.av_len += pubToken.av_len; - if(r->Link.lFlags & RTMP_LF_FAPU) - free(r->Link.app.av_val); - r->Link.app.av_val = ptr; - - ptr = malloc(r->Link.tcUrl.av_len + pubToken.av_len); - strncpy(ptr, r->Link.tcUrl.av_val, r->Link.tcUrl.av_len); - strncpy(ptr + r->Link.tcUrl.av_len, pubToken.av_val, pubToken.av_len); - r->Link.tcUrl.av_len += pubToken.av_len; - if(r->Link.lFlags & RTMP_LF_FTCU) - free(r->Link.tcUrl.av_val); - r->Link.tcUrl.av_val = ptr; - - free(pubToken.av_val); - r->Link.lFlags |= RTMP_LF_FTCU | RTMP_LF_FAPU; - - RTMP_Log(RTMP_LOGDEBUG, "%s, new app: %.*s tcUrl: %.*s playpath: %s", __FUNCTION__, - r->Link.app.av_len, r->Link.app.av_val, - r->Link.tcUrl.av_len, r->Link.tcUrl.av_val, - r->Link.playpath.av_val); - } - else - { - return 0; - } - return 1; -} -#endif - - -SAVC(onBWDone); -SAVC(onFCSubscribe); -SAVC(onFCUnsubscribe); -SAVC(_onbwcheck); -SAVC(_onbwdone); -SAVC(_error); -SAVC(close); -SAVC(code); -SAVC(level); -SAVC(description); -SAVC(onStatus); -SAVC(playlist_ready); -static const AVal av_NetStream_Failed = AVC("NetStream.Failed"); -static const AVal av_NetStream_Play_Failed = AVC("NetStream.Play.Failed"); -static const AVal av_NetStream_Play_StreamNotFound = -AVC("NetStream.Play.StreamNotFound"); -static const AVal av_NetConnection_Connect_InvalidApp = -AVC("NetConnection.Connect.InvalidApp"); -static const AVal av_NetStream_Play_Start = AVC("NetStream.Play.Start"); -static const AVal av_NetStream_Play_Complete = AVC("NetStream.Play.Complete"); -static const AVal av_NetStream_Play_Stop = AVC("NetStream.Play.Stop"); -static const AVal av_NetStream_Seek_Notify = AVC("NetStream.Seek.Notify"); -static const AVal av_NetStream_Pause_Notify = AVC("NetStream.Pause.Notify"); -static const AVal av_NetStream_Play_PublishNotify = -AVC("NetStream.Play.PublishNotify"); -static const AVal av_NetStream_Play_UnpublishNotify = -AVC("NetStream.Play.UnpublishNotify"); -static const AVal av_NetStream_Publish_Start = AVC("NetStream.Publish.Start"); -static const AVal av_NetConnection_Connect_Rejected = -AVC("NetConnection.Connect.Rejected"); - -void sp(int n){ - printf("%d\n",n);fflush(stdout); -} -/* Returns 0 for OK/Failed/error, 1 for 'Stop or Complete' */ -int -HandleInvoke(RTMP *r, const char *body, unsigned int nBodySize) -{ - AMFObject obj; - AVal method; - double txn; - int ret = 0, nRes; - if (body[0] != 0x02) /* make sure it is a string method name we start with */ - { - sp(1); - RTMP_Log(RTMP_LOGWARNING, "%s, Sanity failed. no string method in invoke packet", - __FUNCTION__); - return 0; - } - - nRes = AMF_Decode(&obj, body, nBodySize, FALSE); - if (nRes < 0) - { - sp(2); - RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__); - return 0; - } - - AMF_Dump(&obj); - AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &method); - txn = AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 1)); - RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val); - - if (AVMATCH(&method, &av__result)) - { - sp(3); - AVal methodInvoked = {0}; - int i; - - for (i=0; im_numCalls; i++) { - if (r->m_methodCalls[i].num == (int)txn) { - sp(4); - methodInvoked = r->m_methodCalls[i].name; - AV_erase(r->m_methodCalls, &r->m_numCalls, i, FALSE); - break; - } - } - if (!methodInvoked.av_val) { - sp(5); - RTMP_Log(RTMP_LOGDEBUG, "%s, received result id %f without matching request", - __FUNCTION__, txn); - goto leave; - } - - RTMP_Log(RTMP_LOGDEBUG, "%s, received result for method call <%s>", __FUNCTION__, - methodInvoked.av_val); - - if (AVMATCH(&methodInvoked, &av_connect)) - { - sp(6); - if (r->Link.token.av_len) - { - sp(7); - AMFObjectProperty p; - if (RTMP_FindFirstMatchingProperty(&obj, &av_secureToken, &p)) - { - sp(8); - DecodeTEA(&r->Link.token, &p.p_vu.p_aval); - SendSecureTokenResponse(r, &p.p_vu.p_aval); - } - } - if (r->Link.protocol & RTMP_FEATURE_WRITE) - { - sp(9); - SendReleaseStream(r); - SendFCPublish(r); - } - else - { - sp(10); - RTMP_SendServerBW(r); - RTMP_SendCtrl(r, 3, 0, 300); - } - RTMP_SendCreateStream(r); - - if (!(r->Link.protocol & RTMP_FEATURE_WRITE)) - { - sp(11); - /* Authenticate on Justin.tv legacy servers before sending FCSubscribe */ - if (r->Link.usherToken.av_len) - SendUsherToken(r, &r->Link.usherToken); - /* Send the FCSubscribe if live stream or if subscribepath is set */ - if (r->Link.subscribepath.av_len) - SendFCSubscribe(r, &r->Link.subscribepath); - else if (r->Link.lFlags & RTMP_LF_LIVE) - SendFCSubscribe(r, &r->Link.playpath); - } - } - else if (AVMATCH(&methodInvoked, &av_createStream)) - { - sp(12); - r->m_stream_id = (int)AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 3)); - - if (r->Link.protocol & RTMP_FEATURE_WRITE) - { - sp(13); - SendPublish(r); - } - else - { - sp(14); - if (r->Link.lFlags & RTMP_LF_PLST) - SendPlaylist(r); - SendPlay(r); - RTMP_SendCtrl(r, 3, r->m_stream_id, r->m_nBufferMS); - } - } - else if (AVMATCH(&methodInvoked, &av_play) || - AVMATCH(&methodInvoked, &av_publish)) - { - sp(15); - r->m_bPlaying = TRUE; - } - free(methodInvoked.av_val); - } - else if (AVMATCH(&method, &av_onBWDone)) - { - sp(16); - if (!r->m_nBWCheckCounter) - SendCheckBW(r); - } - else if (AVMATCH(&method, &av_onFCSubscribe)) - { - sp(17); - /* SendOnFCSubscribe(); */ - } - else if (AVMATCH(&method, &av_onFCUnsubscribe)) - { - sp(18); - RTMP_Close(r); - ret = 1; - } - else if (AVMATCH(&method, &av_ping)) - { - sp(19); - SendPong(r, txn); - } - else if (AVMATCH(&method, &av__onbwcheck)) - { - sp(20); - SendCheckBWResult(r, txn); - } - else if (AVMATCH(&method, &av__onbwdone)) - { - sp(21); - int i; - for (i = 0; i < r->m_numCalls; i++) - if (AVMATCH(&r->m_methodCalls[i].name, &av__checkbw)) - { - sp(22); - AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE); - break; - } - } - else if (AVMATCH(&method, &av__error)) - { - sp(23); -#ifdef CRYPTO - AVal methodInvoked = {0}; - int i; - - if (r->Link.protocol & RTMP_FEATURE_WRITE) - { - for (i=0; im_numCalls; i++) - { - if (r->m_methodCalls[i].num == txn) - { - methodInvoked = r->m_methodCalls[i].name; - AV_erase(r->m_methodCalls, &r->m_numCalls, i, FALSE); - break; - } - } - if (!methodInvoked.av_val) - { - RTMP_Log(RTMP_LOGDEBUG, "%s, received result id %f without matching request", - __FUNCTION__, txn); - goto leave; - } - - RTMP_Log(RTMP_LOGDEBUG, "%s, received error for method call <%s>", __FUNCTION__, - methodInvoked.av_val); - - if (AVMATCH(&methodInvoked, &av_connect)) - { - AMFObject obj2; - AVal code, level, description; - AMFProp_GetObject(AMF_GetProp(&obj, NULL, 3), &obj2); - AMFProp_GetString(AMF_GetProp(&obj2, &av_code, -1), &code); - AMFProp_GetString(AMF_GetProp(&obj2, &av_level, -1), &level); - AMFProp_GetString(AMF_GetProp(&obj2, &av_description, -1), &description); - RTMP_Log(RTMP_LOGDEBUG, "%s, error description: %s", __FUNCTION__, description.av_val); - /* if PublisherAuth returns 1, then reconnect */ - if (PublisherAuth(r, &description) == 1) - { - CloseInternal(r, 1); - if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) - goto leave; - } - } - } - else - { - RTMP_Log(RTMP_LOGERROR, "rtmp server sent error"); - } - free(methodInvoked.av_val); -#else -sp(24); - RTMP_Log(RTMP_LOGERROR, "rtmp server sent error"); -#endif - } - else if (AVMATCH(&method, &av_close)) - { - sp(25); - RTMP_Log(RTMP_LOGERROR, "rtmp server requested close"); - RTMP_Close(r); - } - else if (AVMATCH(&method, &av_onStatus)) - { - sp(26); - AMFObject obj2; - AVal code, level; - AMFProp_GetObject(AMF_GetProp(&obj, NULL, 3), &obj2); - AMFProp_GetString(AMF_GetProp(&obj2, &av_code, -1), &code); - AMFProp_GetString(AMF_GetProp(&obj2, &av_level, -1), &level); - - RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val); - if (AVMATCH(&code, &av_NetStream_Failed) - || AVMATCH(&code, &av_NetStream_Play_Failed) - || AVMATCH(&code, &av_NetStream_Play_StreamNotFound) - || AVMATCH(&code, &av_NetConnection_Connect_InvalidApp)) - { - sp(27); - r->m_stream_id = -1; - RTMP_Close(r); - RTMP_Log(RTMP_LOGERROR, "Closing connection: %s", code.av_val); - } - - else if (AVMATCH(&code, &av_NetStream_Play_Start) - || AVMATCH(&code, &av_NetStream_Play_PublishNotify)) - { - sp(28); - int i; - r->m_bPlaying = TRUE; - for (i = 0; i < r->m_numCalls; i++) - { - if (AVMATCH(&r->m_methodCalls[i].name, &av_play)) - { - sp(29); - AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE); - break; - } - } - } - - else if (AVMATCH(&code, &av_NetStream_Publish_Start)) - { - sp(30); - int i; - r->m_bPlaying = TRUE; - for (i = 0; i < r->m_numCalls; i++) - { - if (AVMATCH(&r->m_methodCalls[i].name, &av_publish)) - { - sp(31); - AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE); - break; - } - } - } - - /* Return 1 if this is a Play.Complete or Play.Stop */ - else if (AVMATCH(&code, &av_NetStream_Play_Complete) - || AVMATCH(&code, &av_NetStream_Play_Stop) - || AVMATCH(&code, &av_NetStream_Play_UnpublishNotify)) - { - sp(32); - RTMP_Close(r); - ret = 1; - } - - else if (AVMATCH(&code, &av_NetStream_Seek_Notify)) - { - sp(33); - r->m_read.flags &= ~RTMP_READ_SEEKING; - } - - else if (AVMATCH(&code, &av_NetStream_Pause_Notify)) - { - sp(34); - if (r->m_pausing == 1 || r->m_pausing == 2) - { - sp(35); - RTMP_SendPause(r, FALSE, r->m_pauseStamp); - r->m_pausing = 3; - } - } - } - else if (AVMATCH(&method, &av_playlist_ready)) - { - sp(36); - int i; - for (i = 0; i < r->m_numCalls; i++) - { - if (AVMATCH(&r->m_methodCalls[i].name, &av_set_playlist)) - { - sp(37); - AV_erase(r->m_methodCalls, &r->m_numCalls, i, TRUE); - break; - } - } - } - else - { - sp(38); - } - sp(39); -leave: -sp(40); - AMF_Reset(&obj); - return ret; -} - -int -RTMP_FindFirstMatchingProperty(AMFObject *obj, const AVal *name, - AMFObjectProperty * p) -{ - int n; - /* this is a small object search to locate the "duration" property */ - for (n = 0; n < obj->o_num; n++) - { - AMFObjectProperty *prop = AMF_GetProp(obj, NULL, n); - - if (AVMATCH(&prop->p_name, name)) - { - memcpy(p, prop, sizeof(*prop)); - return TRUE; - } - - if (prop->p_type == AMF_OBJECT || prop->p_type == AMF_ECMA_ARRAY) - { - if (RTMP_FindFirstMatchingProperty(&prop->p_vu.p_object, name, p)) - return TRUE; - } - } - return FALSE; -} - -/* Like above, but only check if name is a prefix of property */ -int -RTMP_FindPrefixProperty(AMFObject *obj, const AVal *name, - AMFObjectProperty * p) -{ - int n; - for (n = 0; n < obj->o_num; n++) - { - AMFObjectProperty *prop = AMF_GetProp(obj, NULL, n); - - if (prop->p_name.av_len > name->av_len && - !memcmp(prop->p_name.av_val, name->av_val, name->av_len)) - { - memcpy(p, prop, sizeof(*prop)); - return TRUE; - } - - if (prop->p_type == AMF_OBJECT) - { - if (RTMP_FindPrefixProperty(&prop->p_vu.p_object, name, p)) - return TRUE; - } - } - return FALSE; -} - -static int -DumpMetaData(AMFObject *obj) -{ - AMFObjectProperty *prop; - int n, len; - for (n = 0; n < obj->o_num; n++) - { - char str[256] = ""; - prop = AMF_GetProp(obj, NULL, n); - switch (prop->p_type) - { - case AMF_OBJECT: - case AMF_ECMA_ARRAY: - case AMF_STRICT_ARRAY: - if (prop->p_name.av_len) - RTMP_Log(RTMP_LOGINFO, "%.*s:", prop->p_name.av_len, prop->p_name.av_val); - DumpMetaData(&prop->p_vu.p_object); - break; - case AMF_NUMBER: - snprintf(str, 255, "%.2f", prop->p_vu.p_number); - break; - case AMF_BOOLEAN: - snprintf(str, 255, "%s", - prop->p_vu.p_number != 0. ? "TRUE" : "FALSE"); - break; - case AMF_STRING: - len = snprintf(str, 255, "%.*s", prop->p_vu.p_aval.av_len, - prop->p_vu.p_aval.av_val); - if (len >= 1 && str[len-1] == '\n') - str[len-1] = '\0'; - break; - case AMF_DATE: - snprintf(str, 255, "timestamp:%.2f", prop->p_vu.p_number); - break; - default: - snprintf(str, 255, "INVALID TYPE 0x%02x", - (unsigned char)prop->p_type); - } - if (str[0] && prop->p_name.av_len) - { - RTMP_Log(RTMP_LOGINFO, " %-22.*s%s", prop->p_name.av_len, - prop->p_name.av_val, str); - } - } - return FALSE; -} - -SAVC(onMetaData); -SAVC(duration); -SAVC(video); -SAVC(audio); - -int -HandleMetadata(RTMP *r, char *body, unsigned int len) -{ - /* allright we get some info here, so parse it and print it */ - /* also keep duration or filesize to make a nice progress bar */ - - AMFObject obj; - AVal metastring; - int ret = FALSE; - - int nRes = AMF_Decode(&obj, body, len, FALSE); - if (nRes < 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, error decoding meta data packet", __FUNCTION__); - return FALSE; - } - - AMF_Dump(&obj); - AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &metastring); - - if (AVMATCH(&metastring, &av_onMetaData)) - { - AMFObjectProperty prop; - /* Show metadata */ - RTMP_Log(RTMP_LOGINFO, "Metadata:"); - DumpMetaData(&obj); - if (RTMP_FindFirstMatchingProperty(&obj, &av_duration, &prop)) - { - r->m_fDuration = prop.p_vu.p_number; - /*RTMP_Log(RTMP_LOGDEBUG, "Set duration: %.2f", m_fDuration); */ - } - /* Search for audio or video tags */ - if (RTMP_FindPrefixProperty(&obj, &av_video, &prop)) - r->m_read.dataType |= 1; - if (RTMP_FindPrefixProperty(&obj, &av_audio, &prop)) - r->m_read.dataType |= 4; - ret = TRUE; - } - AMF_Reset(&obj); - return ret; -} - -void -HandleChangeChunkSize(RTMP *r, const RTMPPacket *packet) -{ - if (packet->m_nBodySize >= 4) - { - r->m_inChunkSize = AMF_DecodeInt32(packet->m_body); - RTMP_Log(RTMP_LOGDEBUG, "%s, received: chunk size change to %d", __FUNCTION__, - r->m_inChunkSize); - } -} - -void -HandleAudio(RTMP *r, const RTMPPacket *packet) -{ -} - -void -HandleVideo(RTMP *r, const RTMPPacket *packet) -{ -} - -void -HandleCtrl(RTMP *r, const RTMPPacket *packet) -{ - short nType = -1; - unsigned int tmp; - if (packet->m_body && packet->m_nBodySize >= 2) - nType = AMF_DecodeInt16(packet->m_body); - RTMP_Log(RTMP_LOGDEBUG, "%s, received ctrl. type: %d, len: %d", __FUNCTION__, nType, - packet->m_nBodySize); - /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ - - if (packet->m_nBodySize >= 6) - { - switch (nType) - { - case 0: - tmp = AMF_DecodeInt32(packet->m_body + 2); - RTMP_Log(RTMP_LOGDEBUG, "%s, Stream Begin %d", __FUNCTION__, tmp); - break; - - case 1: - tmp = AMF_DecodeInt32(packet->m_body + 2); - RTMP_Log(RTMP_LOGDEBUG, "%s, Stream EOF %d", __FUNCTION__, tmp); - if (r->m_pausing == 1) - r->m_pausing = 2; - break; - - case 2: - tmp = AMF_DecodeInt32(packet->m_body + 2); - RTMP_Log(RTMP_LOGDEBUG, "%s, Stream Dry %d", __FUNCTION__, tmp); - break; - - case 4: - tmp = AMF_DecodeInt32(packet->m_body + 2); - RTMP_Log(RTMP_LOGDEBUG, "%s, Stream IsRecorded %d", __FUNCTION__, tmp); - break; - - case 6: /* server ping. reply with pong. */ - tmp = AMF_DecodeInt32(packet->m_body + 2); - RTMP_Log(RTMP_LOGDEBUG, "%s, Ping %d", __FUNCTION__, tmp); - RTMP_SendCtrl(r, 0x07, tmp, 0); - break; - - /* FMS 3.5 servers send the following two controls to let the client - * know when the server has sent a complete buffer. I.e., when the - * server has sent an amount of data equal to m_nBufferMS in duration. - * The server meters its output so that data arrives at the client - * in realtime and no faster. - * - * The rtmpdump program tries to set m_nBufferMS as large as - * possible, to force the server to send data as fast as possible. - * In practice, the server appears to cap this at about 1 hour's - * worth of data. After the server has sent a complete buffer, and - * sends this BufferEmpty message, it will wait until the play - * duration of that buffer has passed before sending a new buffer. - * The BufferReady message will be sent when the new buffer starts. - * (There is no BufferReady message for the very first buffer; - * presumably the Stream Begin message is sufficient for that - * purpose.) - * - * If the network speed is much faster than the data bitrate, then - * there may be long delays between the end of one buffer and the - * start of the next. - * - * Since usually the network allows data to be sent at - * faster than realtime, and rtmpdump wants to download the data - * as fast as possible, we use this RTMP_LF_BUFX hack: when we - * get the BufferEmpty message, we send a Pause followed by an - * Unpause. This causes the server to send the next buffer immediately - * instead of waiting for the full duration to elapse. (That's - * also the purpose of the ToggleStream function, which rtmpdump - * calls if we get a read timeout.) - * - * Media player apps don't need this hack since they are just - * going to play the data in realtime anyway. It also doesn't work - * for live streams since they obviously can only be sent in - * realtime. And it's all moot if the network speed is actually - * slower than the media bitrate. - */ - case 31: - tmp = AMF_DecodeInt32(packet->m_body + 2); - RTMP_Log(RTMP_LOGDEBUG, "%s, Stream BufferEmpty %d", __FUNCTION__, tmp); - if (!(r->Link.lFlags & RTMP_LF_BUFX)) - break; - if (!r->m_pausing) - { - r->m_pauseStamp = r->m_mediaChannel < r->m_channelsAllocatedIn ? - r->m_channelTimestamp[r->m_mediaChannel] : 0; - RTMP_SendPause(r, TRUE, r->m_pauseStamp); - r->m_pausing = 1; - } - else if (r->m_pausing == 2) - { - RTMP_SendPause(r, FALSE, r->m_pauseStamp); - r->m_pausing = 3; - } - break; - - case 32: - tmp = AMF_DecodeInt32(packet->m_body + 2); - RTMP_Log(RTMP_LOGDEBUG, "%s, Stream BufferReady %d", __FUNCTION__, tmp); - break; - - default: - tmp = AMF_DecodeInt32(packet->m_body + 2); - RTMP_Log(RTMP_LOGDEBUG, "%s, Stream xx %d", __FUNCTION__, tmp); - break; - } - - } - - if (nType == 0x1A) - { - RTMP_Log(RTMP_LOGDEBUG, "%s, SWFVerification ping received: ", __FUNCTION__); - if (packet->m_nBodySize > 2 && packet->m_body[2] > 0x01) - { - RTMP_Log(RTMP_LOGERROR, - "%s: SWFVerification Type %d request not supported! Patches welcome...", - __FUNCTION__, packet->m_body[2]); - } -#ifdef CRYPTO - /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ - - /* respond with HMAC SHA256 of decompressed SWF, key is the 30byte player key, also the last 30 bytes of the server handshake are applied */ - else if (r->Link.SWFSize) - { - RTMP_SendCtrl(r, 0x1B, 0, 0); - } - else - { - RTMP_Log(RTMP_LOGERROR, - "%s: Ignoring SWFVerification request, use --swfVfy!", - __FUNCTION__); - } -#else - RTMP_Log(RTMP_LOGERROR, - "%s: Ignoring SWFVerification request, no CRYPTO support!", - __FUNCTION__); -#endif - } -} - -void -HandleServerBW(RTMP *r, const RTMPPacket *packet) -{ - r->m_nServerBW = AMF_DecodeInt32(packet->m_body); - RTMP_Log(RTMP_LOGDEBUG, "%s: server BW = %d", __FUNCTION__, r->m_nServerBW); -} - -void -HandleClientBW(RTMP *r, const RTMPPacket *packet) -{ - r->m_nClientBW = AMF_DecodeInt32(packet->m_body); - if (packet->m_nBodySize > 4) - r->m_nClientBW2 = packet->m_body[4]; - else - r->m_nClientBW2 = -1; - RTMP_Log(RTMP_LOGDEBUG, "%s: client BW = %d %d", __FUNCTION__, r->m_nClientBW, - r->m_nClientBW2); -} - -int -DecodeInt32LE(const char *data) -{ - unsigned char *c = (unsigned char *)data; - unsigned int val; - - val = (c[3] << 24) | (c[2] << 16) | (c[1] << 8) | c[0]; - return val; -} - -int -EncodeInt32LE(char *output, int nVal) -{ - output[0] = nVal; - nVal >>= 8; - output[1] = nVal; - nVal >>= 8; - output[2] = nVal; - nVal >>= 8; - output[3] = nVal; - return 4; -} - -int -RTMP_ReadPacket(RTMP *r, RTMPPacket *packet) -{ - uint8_t hbuf[RTMP_MAX_HEADER_SIZE] = { 0 }; - char *header = (char *)hbuf; - int nSize, hSize, nToRead, nChunk; - int didAlloc = FALSE; - int extendedTimestamp; - - RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d", __FUNCTION__, r->m_sb.sb_socket); - - if (ReadN(r, (char *)hbuf, 1) == 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header", __FUNCTION__); - return FALSE; - } - - packet->m_headerType = (hbuf[0] & 0xc0) >> 6; - packet->m_nChannel = (hbuf[0] & 0x3f); - header++; - if (packet->m_nChannel == 0) - { - if (ReadN(r, (char *)&hbuf[1], 1) != 1) - { - RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header 2nd byte", - __FUNCTION__); - return FALSE; - } - packet->m_nChannel = hbuf[1]; - packet->m_nChannel += 64; - header++; - } - else if (packet->m_nChannel == 1) - { - int tmp; - if (ReadN(r, (char *)&hbuf[1], 2) != 2) - { - RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header 3nd byte", - __FUNCTION__); - return FALSE; - } - tmp = (hbuf[2] << 8) + hbuf[1]; - packet->m_nChannel = tmp + 64; - RTMP_Log(RTMP_LOGDEBUG, "%s, m_nChannel: %0x", __FUNCTION__, packet->m_nChannel); - header += 2; - } - - nSize = packetSize[packet->m_headerType]; - - if (packet->m_nChannel >= r->m_channelsAllocatedIn) - { - int n = packet->m_nChannel + 10; - int *timestamp = realloc(r->m_channelTimestamp, sizeof(int) * n); - RTMPPacket **packets = realloc(r->m_vecChannelsIn, sizeof(RTMPPacket*) * n); - if (!timestamp) - free(r->m_channelTimestamp); - if (!packets) - free(r->m_vecChannelsIn); - r->m_channelTimestamp = timestamp; - r->m_vecChannelsIn = packets; - if (!timestamp || !packets) { - r->m_channelsAllocatedIn = 0; - return FALSE; - } - memset(r->m_channelTimestamp + r->m_channelsAllocatedIn, 0, sizeof(int) * (n - r->m_channelsAllocatedIn)); - memset(r->m_vecChannelsIn + r->m_channelsAllocatedIn, 0, sizeof(RTMPPacket*) * (n - r->m_channelsAllocatedIn)); - r->m_channelsAllocatedIn = n; - } - - if (nSize == RTMP_LARGE_HEADER_SIZE) /* if we get a full header the timestamp is absolute */ - packet->m_hasAbsTimestamp = TRUE; - - else if (nSize < RTMP_LARGE_HEADER_SIZE) - { /* using values from the last message of this channel */ - if (r->m_vecChannelsIn[packet->m_nChannel]) - memcpy(packet, r->m_vecChannelsIn[packet->m_nChannel], - sizeof(RTMPPacket)); - } - - nSize--; - - if (nSize > 0 && ReadN(r, header, nSize) != nSize) - { - RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet header. type: %x", - __FUNCTION__, (unsigned int)hbuf[0]); - return FALSE; - } - - hSize = nSize + (header - (char *)hbuf); - - if (nSize >= 3) - { - packet->m_nTimeStamp = AMF_DecodeInt24(header); - - /*RTMP_Log(RTMP_LOGDEBUG, "%s, reading RTMP packet chunk on channel %x, headersz %i, timestamp %i, abs timestamp %i", __FUNCTION__, packet.m_nChannel, nSize, packet.m_nTimeStamp, packet.m_hasAbsTimestamp); */ - - if (nSize >= 6) - { - packet->m_nBodySize = AMF_DecodeInt24(header + 3); - packet->m_nBytesRead = 0; - - if (nSize > 6) - { - packet->m_packetType = header[6]; - - if (nSize == 11) - packet->m_nInfoField2 = DecodeInt32LE(header + 7); - } - } - } - - extendedTimestamp = packet->m_nTimeStamp == 0xffffff; - if (extendedTimestamp) - { - if (ReadN(r, header + nSize, 4) != 4) - { - RTMP_Log(RTMP_LOGERROR, "%s, failed to read extended timestamp", - __FUNCTION__); - return FALSE; - } - packet->m_nTimeStamp = AMF_DecodeInt32(header + nSize); - hSize += 4; - } - - RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)hbuf, hSize); - - if (packet->m_nBodySize > 0 && packet->m_body == NULL) - { - if (!RTMPPacket_Alloc(packet, packet->m_nBodySize)) - { - RTMP_Log(RTMP_LOGDEBUG, "%s, failed to allocate packet", __FUNCTION__); - return FALSE; - } - didAlloc = TRUE; - packet->m_headerType = (hbuf[0] & 0xc0) >> 6; - } - - nToRead = packet->m_nBodySize - packet->m_nBytesRead; - nChunk = r->m_inChunkSize; - if (nToRead < nChunk) - nChunk = nToRead; - - /* Does the caller want the raw chunk? */ - if (packet->m_chunk) - { - packet->m_chunk->c_headerSize = hSize; - memcpy(packet->m_chunk->c_header, hbuf, hSize); - packet->m_chunk->c_chunk = packet->m_body + packet->m_nBytesRead; - packet->m_chunk->c_chunkSize = nChunk; - } - - if (ReadN(r, packet->m_body + packet->m_nBytesRead, nChunk) != nChunk) - { - RTMP_Log(RTMP_LOGERROR, "%s, failed to read RTMP packet body. len: %u", - __FUNCTION__, packet->m_nBodySize); - return FALSE; - } - - RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)packet->m_body + packet->m_nBytesRead, nChunk); - - packet->m_nBytesRead += nChunk; - - /* keep the packet as ref for other packets on this channel */ - if (!r->m_vecChannelsIn[packet->m_nChannel]) - r->m_vecChannelsIn[packet->m_nChannel] = malloc(sizeof(RTMPPacket)); - memcpy(r->m_vecChannelsIn[packet->m_nChannel], packet, sizeof(RTMPPacket)); - if (extendedTimestamp) - { - r->m_vecChannelsIn[packet->m_nChannel]->m_nTimeStamp = 0xffffff; - } - - if (RTMPPacket_IsReady(packet)) - { - /* make packet's timestamp absolute */ - if (!packet->m_hasAbsTimestamp) - packet->m_nTimeStamp += r->m_channelTimestamp[packet->m_nChannel]; /* timestamps seem to be always relative!! */ - - r->m_channelTimestamp[packet->m_nChannel] = packet->m_nTimeStamp; - - /* reset the data from the stored packet. we keep the header since we may use it later if a new packet for this channel */ - /* arrives and requests to re-use some info (small packet header) */ - r->m_vecChannelsIn[packet->m_nChannel]->m_body = NULL; - r->m_vecChannelsIn[packet->m_nChannel]->m_nBytesRead = 0; - r->m_vecChannelsIn[packet->m_nChannel]->m_hasAbsTimestamp = FALSE; /* can only be false if we reuse header */ - } - else - { - packet->m_body = NULL; /* so it won't be erased on free */ - } - - return TRUE; -} - -#ifndef CRYPTO -int -HandShake(RTMP *r, int FP9HandShake) -{ - int i; - uint32_t uptime, suptime; - int bMatch; - char type; - char clientbuf[RTMP_SIG_SIZE + 1], *clientsig = clientbuf + 1; - char serversig[RTMP_SIG_SIZE]; - - clientbuf[0] = 0x03; /* not encrypted */ - - uptime = htonl(RTMP_GetTime()); - memcpy(clientsig, &uptime, 4); - - memset(&clientsig[4], 0, 4); - -#ifdef _DEBUG - for (i = 8; i < RTMP_SIG_SIZE; i++) - clientsig[i] = 0xff; -#else - for (i = 8; i < RTMP_SIG_SIZE; i++) - clientsig[i] = (char)(rand() % 256); -#endif - - if (!WriteN(r, clientbuf, RTMP_SIG_SIZE + 1)) - return FALSE; - - if (ReadN(r, &type, 1) != 1) /* 0x03 or 0x06 */ - return FALSE; - - RTMP_Log(RTMP_LOGDEBUG, "%s: Type Answer : %02X", __FUNCTION__, type); - - if (type != clientbuf[0]) - RTMP_Log(RTMP_LOGWARNING, "%s: Type mismatch: client sent %d, server answered %d", - __FUNCTION__, clientbuf[0], type); - - if (ReadN(r, serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) - return FALSE; - - /* decode server response */ - - memcpy(&suptime, serversig, 4); - suptime = ntohl(suptime); - - RTMP_Log(RTMP_LOGDEBUG, "%s: Server Uptime : %d", __FUNCTION__, suptime); - RTMP_Log(RTMP_LOGDEBUG, "%s: FMS Version : %d.%d.%d.%d", __FUNCTION__, - serversig[4], serversig[5], serversig[6], serversig[7]); - - /* 2nd part of handshake */ - if (!WriteN(r, serversig, RTMP_SIG_SIZE)) - return FALSE; - - if (ReadN(r, serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) - return FALSE; - - bMatch = (memcmp(serversig, clientsig, RTMP_SIG_SIZE) == 0); - if (!bMatch) - { - RTMP_Log(RTMP_LOGWARNING, "%s, client signature does not match!", __FUNCTION__); - } - return TRUE; -} - -static int -SHandShake(RTMP *r) -{ - int i; - char serverbuf[RTMP_SIG_SIZE + 1], *serversig = serverbuf + 1; - char clientsig[RTMP_SIG_SIZE]; - uint32_t uptime; - int bMatch; - - if (ReadN(r, serverbuf, 1) != 1) /* 0x03 or 0x06 */ - return FALSE; - - RTMP_Log(RTMP_LOGDEBUG, "%s: Type Request : %02X", __FUNCTION__, serverbuf[0]); - - if (serverbuf[0] != 3) - { - RTMP_Log(RTMP_LOGERROR, "%s: Type unknown: client sent %02X", - __FUNCTION__, serverbuf[0]); - return FALSE; - } - - uptime = htonl(RTMP_GetTime()); - memcpy(serversig, &uptime, 4); - - memset(&serversig[4], 0, 4); -#ifdef _DEBUG - for (i = 8; i < RTMP_SIG_SIZE; i++) - serversig[i] = 0xff; -#else - for (i = 8; i < RTMP_SIG_SIZE; i++) - serversig[i] = (char)(rand() % 256); -#endif - - if (!WriteN(r, serverbuf, RTMP_SIG_SIZE + 1)) - return FALSE; - - if (ReadN(r, clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) - return FALSE; - - /* decode client response */ - - memcpy(&uptime, clientsig, 4); - uptime = ntohl(uptime); - - RTMP_Log(RTMP_LOGDEBUG, "%s: Client Uptime : %d", __FUNCTION__, uptime); - RTMP_Log(RTMP_LOGDEBUG, "%s: Player Version: %d.%d.%d.%d", __FUNCTION__, - clientsig[4], clientsig[5], clientsig[6], clientsig[7]); - - /* 2nd part of handshake */ - if (!WriteN(r, clientsig, RTMP_SIG_SIZE)) - return FALSE; - - if (ReadN(r, clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) - return FALSE; - - bMatch = (memcmp(serversig, clientsig, RTMP_SIG_SIZE) == 0); - if (!bMatch) - { - RTMP_Log(RTMP_LOGWARNING, "%s, client signature does not match!", __FUNCTION__); - } - return TRUE; -} -#endif - -int -RTMP_SendChunk(RTMP *r, RTMPChunk *chunk) -{ - int wrote; - char hbuf[RTMP_MAX_HEADER_SIZE]; - - RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d, size=%d", __FUNCTION__, r->m_sb.sb_socket, - chunk->c_chunkSize); - RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)chunk->c_header, chunk->c_headerSize); - if (chunk->c_chunkSize) - { - char *ptr = chunk->c_chunk - chunk->c_headerSize; - RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)chunk->c_chunk, chunk->c_chunkSize); - /* save header bytes we're about to overwrite */ - memcpy(hbuf, ptr, chunk->c_headerSize); - memcpy(ptr, chunk->c_header, chunk->c_headerSize); - wrote = WriteN(r, ptr, chunk->c_headerSize + chunk->c_chunkSize); - memcpy(ptr, hbuf, chunk->c_headerSize); - } - else - wrote = WriteN(r, chunk->c_header, chunk->c_headerSize); - return wrote; -} - -int -RTMP_SendPacket(RTMP *r, RTMPPacket *packet, int queue) -{ - const RTMPPacket *prevPacket; - uint32_t last = 0; - int nSize; - int hSize, cSize; - char *header, *hptr, *hend, hbuf[RTMP_MAX_HEADER_SIZE], c; - uint32_t t; - char *buffer, *tbuf = NULL, *toff = NULL; - int nChunkSize; - int tlen; - - if (packet->m_nChannel >= r->m_channelsAllocatedOut) - { - int n = packet->m_nChannel + 10; - RTMPPacket **packets = realloc(r->m_vecChannelsOut, sizeof(RTMPPacket*) * n); - if (!packets) { - free(r->m_vecChannelsOut); - r->m_vecChannelsOut = NULL; - r->m_channelsAllocatedOut = 0; - return FALSE; - } - r->m_vecChannelsOut = packets; - memset(r->m_vecChannelsOut + r->m_channelsAllocatedOut, 0, sizeof(RTMPPacket*) * (n - r->m_channelsAllocatedOut)); - r->m_channelsAllocatedOut = n; - } - - prevPacket = r->m_vecChannelsOut[packet->m_nChannel]; - if (prevPacket && packet->m_headerType != RTMP_PACKET_SIZE_LARGE) - { - /* compress a bit by using the prev packet's attributes */ - if (prevPacket->m_nBodySize == packet->m_nBodySize - && prevPacket->m_packetType == packet->m_packetType - && packet->m_headerType == RTMP_PACKET_SIZE_MEDIUM) - packet->m_headerType = RTMP_PACKET_SIZE_SMALL; - - if (prevPacket->m_nTimeStamp == packet->m_nTimeStamp - && packet->m_headerType == RTMP_PACKET_SIZE_SMALL) - packet->m_headerType = RTMP_PACKET_SIZE_MINIMUM; - last = prevPacket->m_nTimeStamp; - } - - if (packet->m_headerType > 3) /* sanity */ - { - RTMP_Log(RTMP_LOGERROR, "sanity failed!! trying to send header of type: 0x%02x.", - (unsigned char)packet->m_headerType); - return FALSE; - } - - nSize = packetSize[packet->m_headerType]; - hSize = nSize; cSize = 0; - t = packet->m_nTimeStamp - last; - - if (packet->m_body) - { - header = packet->m_body - nSize; - hend = packet->m_body; - } - else - { - header = hbuf + 6; - hend = hbuf + sizeof(hbuf); - } - - if (packet->m_nChannel > 319) - cSize = 2; - else if (packet->m_nChannel > 63) - cSize = 1; - if (cSize) - { - header -= cSize; - hSize += cSize; - } - - if (t >= 0xffffff) - { - header -= 4; - hSize += 4; - RTMP_Log(RTMP_LOGWARNING, "Larger timestamp than 24-bit: 0x%x", t); - } - - hptr = header; - c = packet->m_headerType << 6; - switch (cSize) - { - case 0: - c |= packet->m_nChannel; - break; - case 1: - break; - case 2: - c |= 1; - break; - } - *hptr++ = c; - if (cSize) - { - int tmp = packet->m_nChannel - 64; - *hptr++ = tmp & 0xff; - if (cSize == 2) - *hptr++ = tmp >> 8; - } - - if (nSize > 1) - { - hptr = AMF_EncodeInt24(hptr, hend, t > 0xffffff ? 0xffffff : t); - } - - if (nSize > 4) - { - hptr = AMF_EncodeInt24(hptr, hend, packet->m_nBodySize); - *hptr++ = packet->m_packetType; - } - - if (nSize > 8) - hptr += EncodeInt32LE(hptr, packet->m_nInfoField2); - - if (t >= 0xffffff) - hptr = AMF_EncodeInt32(hptr, hend, t); - - nSize = packet->m_nBodySize; - buffer = packet->m_body; - nChunkSize = r->m_outChunkSize; - - RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d, size=%d", __FUNCTION__, r->m_sb.sb_socket, - nSize); - /* send all chunks in one HTTP request */ - if (r->Link.protocol & RTMP_FEATURE_HTTP) - { - int chunks = (nSize+nChunkSize-1) / nChunkSize; - if (chunks > 1) - { - tlen = chunks * (cSize + 1) + nSize + hSize; - tbuf = malloc(tlen); - if (!tbuf) - return FALSE; - toff = tbuf; - } - } - while (nSize + hSize) - { - int wrote; - - if (nSize < nChunkSize) - nChunkSize = nSize; - - RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)header, hSize); - RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)buffer, nChunkSize); - if (tbuf) - { - memcpy(toff, header, nChunkSize + hSize); - toff += nChunkSize + hSize; - } - else - { - wrote = WriteN(r, header, nChunkSize + hSize); - if (!wrote) - return FALSE; - } - nSize -= nChunkSize; - buffer += nChunkSize; - hSize = 0; - - if (nSize > 0) - { - header = buffer - 1; - hSize = 1; - if (cSize) - { - header -= cSize; - hSize += cSize; - } - if (t >= 0xffffff) - { - header -= 4; - hSize += 4; - } - *header = (0xc0 | c); - if (cSize) - { - int tmp = packet->m_nChannel - 64; - header[1] = tmp & 0xff; - if (cSize == 2) - header[2] = tmp >> 8; - } - if (t >= 0xffffff) - { - char* extendedTimestamp = header + 1 + cSize; - AMF_EncodeInt32(extendedTimestamp, extendedTimestamp + 4, t); - } - } - } - if (tbuf) - { - int wrote = WriteN(r, tbuf, toff-tbuf); - free(tbuf); - tbuf = NULL; - if (!wrote) - return FALSE; - } - - /* we invoked a remote method */ - if (packet->m_packetType == RTMP_PACKET_TYPE_INVOKE) - { - AVal method; - char *ptr; - ptr = packet->m_body + 1; - AMF_DecodeString(ptr, &method); - RTMP_Log(RTMP_LOGDEBUG, "Invoking %s", method.av_val); - /* keep it in call queue till result arrives */ - if (queue) { - int txn; - ptr += 3 + method.av_len; - txn = (int)AMF_DecodeNumber(ptr); - AV_queue(&r->m_methodCalls, &r->m_numCalls, &method, txn); - } - } - - if (!r->m_vecChannelsOut[packet->m_nChannel]) - r->m_vecChannelsOut[packet->m_nChannel] = malloc(sizeof(RTMPPacket)); - memcpy(r->m_vecChannelsOut[packet->m_nChannel], packet, sizeof(RTMPPacket)); - return TRUE; -} - -int -RTMP_Serve(RTMP *r) -{ - return SHandShake(r); -} - -void -RTMP_Close(RTMP *r) -{ - CloseInternal(r, 0); -} - -static void -CloseInternal(RTMP *r, int reconnect) -{ - int i; - - if (RTMP_IsConnected(r)) - { - if (r->m_stream_id > 0) - { - i = r->m_stream_id; - r->m_stream_id = 0; - if ((r->Link.protocol & RTMP_FEATURE_WRITE)) - SendFCUnpublish(r); - SendDeleteStream(r, i); - } - if (r->m_clientID.av_val) - { - HTTP_Post(r, RTMPT_CLOSE, "", 1); - free(r->m_clientID.av_val); - r->m_clientID.av_val = NULL; - r->m_clientID.av_len = 0; - } - RTMPSockBuf_Close(&r->m_sb); - } - - r->m_stream_id = -1; - r->m_sb.sb_socket = -1; - r->m_nBWCheckCounter = 0; - r->m_nBytesIn = 0; - r->m_nBytesInSent = 0; - - if (r->m_read.flags & RTMP_READ_HEADER) { - free(r->m_read.buf); - r->m_read.buf = NULL; - } - r->m_read.dataType = 0; - r->m_read.flags = 0; - r->m_read.status = 0; - r->m_read.nResumeTS = 0; - r->m_read.nIgnoredFrameCounter = 0; - r->m_read.nIgnoredFlvFrameCounter = 0; - - r->m_write.m_nBytesRead = 0; - RTMPPacket_Free(&r->m_write); - - for (i = 0; i < r->m_channelsAllocatedIn; i++) - { - if (r->m_vecChannelsIn[i]) - { - RTMPPacket_Free(r->m_vecChannelsIn[i]); - free(r->m_vecChannelsIn[i]); - r->m_vecChannelsIn[i] = NULL; - } - } - free(r->m_vecChannelsIn); - r->m_vecChannelsIn = NULL; - free(r->m_channelTimestamp); - r->m_channelTimestamp = NULL; - r->m_channelsAllocatedIn = 0; - for (i = 0; i < r->m_channelsAllocatedOut; i++) - { - if (r->m_vecChannelsOut[i]) - { - free(r->m_vecChannelsOut[i]); - r->m_vecChannelsOut[i] = NULL; - } - } - free(r->m_vecChannelsOut); - r->m_vecChannelsOut = NULL; - r->m_channelsAllocatedOut = 0; - AV_clear(r->m_methodCalls, r->m_numCalls); - r->m_methodCalls = NULL; - r->m_numCalls = 0; - r->m_numInvokes = 0; - - r->m_bPlaying = FALSE; - r->m_sb.sb_size = 0; - - r->m_msgCounter = 0; - r->m_resplen = 0; - r->m_unackd = 0; - - if (r->Link.lFlags & RTMP_LF_FTCU && !reconnect) - { - free(r->Link.tcUrl.av_val); - r->Link.tcUrl.av_val = NULL; - r->Link.lFlags ^= RTMP_LF_FTCU; - } - if (r->Link.lFlags & RTMP_LF_FAPU && !reconnect) - { - free(r->Link.app.av_val); - r->Link.app.av_val = NULL; - r->Link.lFlags ^= RTMP_LF_FAPU; - } - - if (!reconnect) - { - free(r->Link.playpath0.av_val); - r->Link.playpath0.av_val = NULL; - } -#ifdef CRYPTO - if (r->Link.dh) - { - MDH_free(r->Link.dh); - r->Link.dh = NULL; - } - if (r->Link.rc4keyIn) - { - RC4_free(r->Link.rc4keyIn); - r->Link.rc4keyIn = NULL; - } - if (r->Link.rc4keyOut) - { - RC4_free(r->Link.rc4keyOut); - r->Link.rc4keyOut = NULL; - } -#endif -} - -int -RTMPSockBuf_Fill(RTMPSockBuf *sb) -{ - int nBytes; - - if (!sb->sb_size) - sb->sb_start = sb->sb_buf; - - while (1) - { - nBytes = sizeof(sb->sb_buf) - 1 - sb->sb_size - (sb->sb_start - sb->sb_buf); -#if defined(CRYPTO) && !defined(NO_SSL) - if (sb->sb_ssl) - { - nBytes = TLS_read(sb->sb_ssl, sb->sb_start + sb->sb_size, nBytes); - } - else -#endif - { - nBytes = recv(sb->sb_socket, sb->sb_start + sb->sb_size, nBytes, 0); - } - if (nBytes != -1) - { - sb->sb_size += nBytes; - } - else - { - int sockerr = GetSockError(); - RTMP_Log(RTMP_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 nBytes; -} - -int -RTMPSockBuf_Send(RTMPSockBuf *sb, const char *buf, int len) -{ - int rc; - -#ifdef _DEBUG - fwrite(buf, 1, len, netstackdump); -#endif - -#if defined(CRYPTO) && !defined(NO_SSL) - if (sb->sb_ssl) - { - rc = TLS_write(sb->sb_ssl, buf, len); - } - else -#endif - { - rc = send(sb->sb_socket, buf, len, 0); - } - return rc; -} - -int -RTMPSockBuf_Close(RTMPSockBuf *sb) -{ -#if defined(CRYPTO) && !defined(NO_SSL) - if (sb->sb_ssl) - { - TLS_shutdown(sb->sb_ssl); - TLS_close(sb->sb_ssl); - sb->sb_ssl = NULL; - } -#endif - if (sb->sb_socket != -1) - return closesocket(sb->sb_socket); - return 0; -} - -#define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf)) - -void -DecodeTEA(AVal *key, AVal *text) -{ - uint32_t *v, k[4] = { 0 }, u; - uint32_t z, y, sum = 0, e, DELTA = 0x9e3779b9; - int32_t p, q; - int i, n; - unsigned char *ptr, *out; - - /* prep key: pack 1st 16 chars into 4 LittleEndian ints */ - ptr = (unsigned char *)key->av_val; - u = 0; - n = 0; - v = k; - p = key->av_len > 16 ? 16 : key->av_len; - for (i = 0; i < p; i++) - { - u |= ptr[i] << (n * 8); - if (n == 3) - { - *v++ = u; - u = 0; - n = 0; - } - else - { - n++; - } - } - /* any trailing chars */ - if (u) - *v = u; - - /* prep text: hex2bin, multiples of 4 */ - n = (text->av_len + 7) / 8; - out = malloc(n * 8); - ptr = (unsigned char *)text->av_val; - v = (uint32_t *) out; - for (i = 0; i < n; i++) - { - u = (HEX2BIN(ptr[0]) << 4) + HEX2BIN(ptr[1]); - u |= ((HEX2BIN(ptr[2]) << 4) + HEX2BIN(ptr[3])) << 8; - u |= ((HEX2BIN(ptr[4]) << 4) + HEX2BIN(ptr[5])) << 16; - u |= ((HEX2BIN(ptr[6]) << 4) + HEX2BIN(ptr[7])) << 24; - *v++ = u; - ptr += 8; - } - v = (uint32_t *) out; - - /* http://www.movable-type.co.uk/scripts/tea-block.html */ -#define MX (((z>>5)^(y<<2)) + ((y>>3)^(z<<4))) ^ ((sum^y) + (k[(p&3)^e]^z)); - z = v[n - 1]; - y = v[0]; - q = 6 + 52 / n; - sum = q * DELTA; - while (sum != 0) - { - e = sum >> 2 & 3; - for (p = n - 1; p > 0; p--) - z = v[p - 1], y = v[p] -= MX; - z = v[n - 1]; - y = v[0] -= MX; - sum -= DELTA; - } - - text->av_len /= 2; - memcpy(text->av_val, out, text->av_len); - free(out); -} - -int -HTTP_Post(RTMP *r, RTMPTCmd cmd, const char *buf, int len) -{ - char hbuf[512]; - int hlen = snprintf(hbuf, sizeof(hbuf), "POST /%s%s/%d HTTP/1.1\r\n" - "Host: %.*s:%d\r\n" - "Accept: */*\r\n" - "User-Agent: Shockwave Flash\r\n" - "Connection: Keep-Alive\r\n" - "Cache-Control: no-cache\r\n" - "Content-type: application/x-fcs\r\n" - "Content-length: %d\r\n\r\n", RTMPT_cmds[cmd], - r->m_clientID.av_val ? r->m_clientID.av_val : "", - r->m_msgCounter, r->Link.hostname.av_len, r->Link.hostname.av_val, - r->Link.port, len); - RTMPSockBuf_Send(&r->m_sb, hbuf, hlen); - hlen = RTMPSockBuf_Send(&r->m_sb, buf, len); - r->m_msgCounter++; - r->m_unackd++; - return hlen; -} - -static int -HTTP_read(RTMP *r, int fill) -{ - char *ptr; - int hlen; - -restart: - if (fill) - RTMPSockBuf_Fill(&r->m_sb); - if (r->m_sb.sb_size < 13) { - if (fill) - goto restart; - return -2; - } - if (strncmp(r->m_sb.sb_start, "HTTP/1.1 200 ", 13)) - return -1; - r->m_sb.sb_start[r->m_sb.sb_size] = '\0'; - if (!strstr(r->m_sb.sb_start, "\r\n\r\n")) { - if (fill) - goto restart; - return -2; - } - - ptr = r->m_sb.sb_start + sizeof("HTTP/1.1 200"); - while ((ptr = strstr(ptr, "Content-"))) { - if (!strncasecmp(ptr+8, "length:", 7)) break; - ptr += 8; - } - if (!ptr) - return -1; - hlen = atoi(ptr+16); - ptr = strstr(ptr+16, "\r\n\r\n"); - if (!ptr) - return -1; - ptr += 4; - if (ptr + (r->m_clientID.av_val ? 1 : hlen) > r->m_sb.sb_start + r->m_sb.sb_size) - { - if (fill) - goto restart; - return -2; - } - r->m_sb.sb_size -= ptr - r->m_sb.sb_start; - r->m_sb.sb_start = ptr; - r->m_unackd--; - - if (!r->m_clientID.av_val) - { - r->m_clientID.av_len = hlen; - r->m_clientID.av_val = malloc(hlen+1); - if (!r->m_clientID.av_val) - return -1; - r->m_clientID.av_val[0] = '/'; - memcpy(r->m_clientID.av_val+1, ptr, hlen-1); - r->m_clientID.av_val[hlen] = 0; - r->m_sb.sb_size = 0; - } - else - { - r->m_polling = *ptr++; - r->m_resplen = hlen - 1; - r->m_sb.sb_start++; - r->m_sb.sb_size--; - } - return 0; -} - -#define MAX_IGNORED_FRAMES 50 - -/* Read from the stream until we get a media packet. - * Returns -3 if Play.Close/Stop, -2 if fatal error, -1 if no more media - * packets, 0 if ignorable error, >0 if there is a media packet - */ -static int -Read_1_Packet(RTMP *r, char *buf, unsigned int buflen) -{ - uint32_t prevTagSize = 0; - int rtnGetNextMediaPacket = 0, ret = RTMP_READ_EOF; - RTMPPacket packet = { 0 }; - int recopy = FALSE; - unsigned int size; - char *ptr, *pend; - uint32_t nTimeStamp = 0; - unsigned int len; - - rtnGetNextMediaPacket = RTMP_GetNextMediaPacket(r, &packet); - while (rtnGetNextMediaPacket) - { - char *packetBody = packet.m_body; - unsigned int nPacketLen = packet.m_nBodySize; - - /* Return RTMP_READ_COMPLETE if this was completed nicely with - * invoke message Play.Stop or Play.Complete - */ - if (rtnGetNextMediaPacket == 2) - { - RTMP_Log(RTMP_LOGDEBUG, - "Got Play.Complete or Play.Stop from server. " - "Assuming stream is complete"); - ret = RTMP_READ_COMPLETE; - break; - } - - r->m_read.dataType |= (((packet.m_packetType == RTMP_PACKET_TYPE_AUDIO) << 2) | - (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO)); - - if (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO && nPacketLen <= 5) - { - RTMP_Log(RTMP_LOGDEBUG, "ignoring too small video packet: size: %d", - nPacketLen); - ret = RTMP_READ_IGNORE; - break; - } - if (packet.m_packetType == RTMP_PACKET_TYPE_AUDIO && nPacketLen <= 1) - { - RTMP_Log(RTMP_LOGDEBUG, "ignoring too small audio packet: size: %d", - nPacketLen); - ret = RTMP_READ_IGNORE; - break; - } - - if (r->m_read.flags & RTMP_READ_SEEKING) - { - ret = RTMP_READ_IGNORE; - break; - } -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, "type: %02X, size: %d, TS: %d ms, abs TS: %d", - packet.m_packetType, nPacketLen, packet.m_nTimeStamp, - packet.m_hasAbsTimestamp); - if (packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) - RTMP_Log(RTMP_LOGDEBUG, "frametype: %02X", (*packetBody & 0xf0)); -#endif - - if (r->m_read.flags & RTMP_READ_RESUME) - { - /* check the header if we get one */ - if (packet.m_nTimeStamp == 0) - { - if (r->m_read.nMetaHeaderSize > 0 - && packet.m_packetType == RTMP_PACKET_TYPE_INFO) - { - AMFObject metaObj; - int nRes = - AMF_Decode(&metaObj, packetBody, nPacketLen, FALSE); - if (nRes >= 0) - { - AVal metastring; - AMFProp_GetString(AMF_GetProp(&metaObj, NULL, 0), - &metastring); - - if (AVMATCH(&metastring, &av_onMetaData)) - { - /* compare */ - if ((r->m_read.nMetaHeaderSize != nPacketLen) || - (memcmp - (r->m_read.metaHeader, packetBody, - r->m_read.nMetaHeaderSize) != 0)) - { - ret = RTMP_READ_ERROR; - } - } - AMF_Reset(&metaObj); - if (ret == RTMP_READ_ERROR) - break; - } - } - - /* check first keyframe to make sure we got the right position - * in the stream! (the first non ignored frame) - */ - if (r->m_read.nInitialFrameSize > 0) - { - /* video or audio data */ - if (packet.m_packetType == r->m_read.initialFrameType - && r->m_read.nInitialFrameSize == nPacketLen) - { - /* we don't compare the sizes since the packet can - * contain several FLV packets, just make sure the - * first frame is our keyframe (which we are going - * to rewrite) - */ - if (memcmp - (r->m_read.initialFrame, packetBody, - r->m_read.nInitialFrameSize) == 0) - { - RTMP_Log(RTMP_LOGDEBUG, "Checked keyframe successfully!"); - r->m_read.flags |= RTMP_READ_GOTKF; - /* ignore it! (what about audio data after it? it is - * handled by ignoring all 0ms frames, see below) - */ - ret = RTMP_READ_IGNORE; - break; - } - } - - /* hande FLV streams, even though the server resends the - * keyframe as an extra video packet it is also included - * in the first FLV stream chunk and we have to compare - * it and filter it out !! - */ - if (packet.m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) - { - /* basically we have to find the keyframe with the - * correct TS being nResumeTS - */ - unsigned int pos = 0; - uint32_t ts = 0; - - while (pos + 11 < nPacketLen) - { - /* size without header (11) and prevTagSize (4) */ - uint32_t dataSize = - AMF_DecodeInt24(packetBody + pos + 1); - ts = AMF_DecodeInt24(packetBody + pos + 4); - ts |= (packetBody[pos + 7] << 24); - -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, - "keyframe search: FLV Packet: type %02X, dataSize: %d, timeStamp: %d ms", - packetBody[pos], dataSize, ts); -#endif - /* ok, is it a keyframe?: - * well doesn't work for audio! - */ - if (packetBody[pos /*6928, test 0 */ ] == - r->m_read.initialFrameType - /* && (packetBody[11]&0xf0) == 0x10 */ ) - { - if (ts == r->m_read.nResumeTS) - { - RTMP_Log(RTMP_LOGDEBUG, - "Found keyframe with resume-keyframe timestamp!"); - if (r->m_read.nInitialFrameSize != dataSize - || memcmp(r->m_read.initialFrame, - packetBody + pos + 11, - r->m_read. - nInitialFrameSize) != 0) - { - RTMP_Log(RTMP_LOGERROR, - "FLV Stream: Keyframe doesn't match!"); - ret = RTMP_READ_ERROR; - break; - } - r->m_read.flags |= RTMP_READ_GOTFLVK; - - /* skip this packet? - * check whether skippable: - */ - if (pos + 11 + dataSize + 4 > nPacketLen) - { - RTMP_Log(RTMP_LOGWARNING, - "Non skipable packet since it doesn't end with chunk, stream corrupt!"); - ret = RTMP_READ_ERROR; - break; - } - packetBody += (pos + 11 + dataSize + 4); - nPacketLen -= (pos + 11 + dataSize + 4); - - goto stopKeyframeSearch; - - } - else if (r->m_read.nResumeTS < ts) - { - /* the timestamp ts will only increase with - * further packets, wait for seek - */ - goto stopKeyframeSearch; - } - } - pos += (11 + dataSize + 4); - } - if (ts < r->m_read.nResumeTS) - { - RTMP_Log(RTMP_LOGERROR, - "First packet does not contain keyframe, all " - "timestamps are smaller than the keyframe " - "timestamp; probably the resume seek failed?"); - } - stopKeyframeSearch: - ; - if (!(r->m_read.flags & RTMP_READ_GOTFLVK)) - { - RTMP_Log(RTMP_LOGERROR, - "Couldn't find the seeked keyframe in this chunk!"); - ret = RTMP_READ_IGNORE; - break; - } - } - } - } - - if (packet.m_nTimeStamp > 0 - && (r->m_read.flags & (RTMP_READ_GOTKF|RTMP_READ_GOTFLVK))) - { - /* another problem is that the server can actually change from - * 09/08 video/audio packets to an FLV stream or vice versa and - * our keyframe check will prevent us from going along with the - * new stream if we resumed. - * - * in this case set the 'found keyframe' variables to true. - * We assume that if we found one keyframe somewhere and were - * already beyond TS > 0 we have written data to the output - * which means we can accept all forthcoming data including the - * change between 08/09 <-> FLV packets - */ - r->m_read.flags |= (RTMP_READ_GOTKF|RTMP_READ_GOTFLVK); - } - - /* skip till we find our keyframe - * (seeking might put us somewhere before it) - */ - if (!(r->m_read.flags & RTMP_READ_GOTKF) && - packet.m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO) - { - RTMP_Log(RTMP_LOGWARNING, - "Stream does not start with requested frame, ignoring data... "); - r->m_read.nIgnoredFrameCounter++; - if (r->m_read.nIgnoredFrameCounter > MAX_IGNORED_FRAMES) - ret = RTMP_READ_ERROR; /* fatal error, couldn't continue stream */ - else - ret = RTMP_READ_IGNORE; - break; - } - /* ok, do the same for FLV streams */ - if (!(r->m_read.flags & RTMP_READ_GOTFLVK) && - packet.m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) - { - RTMP_Log(RTMP_LOGWARNING, - "Stream does not start with requested FLV frame, ignoring data... "); - r->m_read.nIgnoredFlvFrameCounter++; - if (r->m_read.nIgnoredFlvFrameCounter > MAX_IGNORED_FRAMES) - ret = RTMP_READ_ERROR; - else - ret = RTMP_READ_IGNORE; - break; - } - - /* we have to ignore the 0ms frames since these are the first - * keyframes; we've got these so don't mess around with multiple - * copies sent by the server to us! (if the keyframe is found at a - * later position there is only one copy and it will be ignored by - * the preceding if clause) - */ - if (!(r->m_read.flags & RTMP_READ_NO_IGNORE) && - packet.m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO) - { - /* exclude type RTMP_PACKET_TYPE_FLASH_VIDEO since it can - * contain several FLV packets - */ - if (packet.m_nTimeStamp == 0) - { - ret = RTMP_READ_IGNORE; - break; - } - else - { - /* stop ignoring packets */ - r->m_read.flags |= RTMP_READ_NO_IGNORE; - } - } - } - - /* calculate packet size and allocate slop buffer if necessary */ - size = nPacketLen + - ((packet.m_packetType == RTMP_PACKET_TYPE_AUDIO - || packet.m_packetType == RTMP_PACKET_TYPE_VIDEO - || packet.m_packetType == RTMP_PACKET_TYPE_INFO) ? 11 : 0) + - (packet.m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO ? 4 : 0); - - if (size + 4 > buflen) - { - /* the extra 4 is for the case of an FLV stream without a last - * prevTagSize (we need extra 4 bytes to append it) */ - r->m_read.buf = malloc(size + 4); - if (r->m_read.buf == 0) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't allocate memory!"); - ret = RTMP_READ_ERROR; /* fatal error */ - break; - } - recopy = TRUE; - ptr = r->m_read.buf; - } - else - { - ptr = buf; - } - pend = ptr + size + 4; - - /* use to return timestamp of last processed packet */ - - /* audio (0x08), video (0x09) or metadata (0x12) packets : - * construct 11 byte header then add rtmp packet's data */ - if (packet.m_packetType == RTMP_PACKET_TYPE_AUDIO - || packet.m_packetType == RTMP_PACKET_TYPE_VIDEO - || packet.m_packetType == RTMP_PACKET_TYPE_INFO) - { - nTimeStamp = r->m_read.nResumeTS + packet.m_nTimeStamp; - prevTagSize = 11 + nPacketLen; - - *ptr = packet.m_packetType; - ptr++; - ptr = AMF_EncodeInt24(ptr, pend, nPacketLen); - -#if 0 - if(packet.m_packetType == RTMP_PACKET_TYPE_VIDEO) { - - /* H264 fix: */ - if((packetBody[0] & 0x0f) == 7) { /* CodecId = H264 */ - uint8_t packetType = *(packetBody+1); - - uint32_t ts = AMF_DecodeInt24(packetBody+2); /* composition time */ - int32_t cts = (ts+0xff800000)^0xff800000; - RTMP_Log(RTMP_LOGDEBUG, "cts : %d\n", cts); - - nTimeStamp -= cts; - /* get rid of the composition time */ - CRTMP::EncodeInt24(packetBody+2, 0); - } - RTMP_Log(RTMP_LOGDEBUG, "VIDEO: nTimeStamp: 0x%08X (%d)\n", nTimeStamp, nTimeStamp); - } -#endif - - ptr = AMF_EncodeInt24(ptr, pend, nTimeStamp); - *ptr = (char)((nTimeStamp & 0xFF000000) >> 24); - ptr++; - - /* stream id */ - ptr = AMF_EncodeInt24(ptr, pend, 0); - } - - memcpy(ptr, packetBody, nPacketLen); - len = nPacketLen; - - /* correct tagSize and obtain timestamp if we have an FLV stream */ - if (packet.m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) - { - unsigned int pos = 0; - int delta; - - /* grab first timestamp and see if it needs fixing */ - nTimeStamp = AMF_DecodeInt24(packetBody + 4); - nTimeStamp |= (packetBody[7] << 24); - delta = packet.m_nTimeStamp - nTimeStamp + r->m_read.nResumeTS; - - while (pos + 11 < nPacketLen) - { - /* size without header (11) and without prevTagSize (4) */ - uint32_t dataSize = AMF_DecodeInt24(packetBody + pos + 1); - nTimeStamp = AMF_DecodeInt24(packetBody + pos + 4); - nTimeStamp |= (packetBody[pos + 7] << 24); - - if (delta) - { - nTimeStamp += delta; - AMF_EncodeInt24(ptr+pos+4, pend, nTimeStamp); - ptr[pos+7] = nTimeStamp>>24; - } - - /* set data type */ - r->m_read.dataType |= (((*(packetBody + pos) == 0x08) << 2) | - (*(packetBody + pos) == 0x09)); - - if (pos + 11 + dataSize + 4 > nPacketLen) - { - if (pos + 11 + dataSize > nPacketLen) - { - RTMP_Log(RTMP_LOGERROR, - "Wrong data size (%u), stream corrupted, aborting!", - dataSize); - ret = RTMP_READ_ERROR; - break; - } - RTMP_Log(RTMP_LOGWARNING, "No tagSize found, appending!"); - - /* we have to append a last tagSize! */ - prevTagSize = dataSize + 11; - AMF_EncodeInt32(ptr + pos + 11 + dataSize, pend, - prevTagSize); - size += 4; - len += 4; - } - else - { - prevTagSize = - AMF_DecodeInt32(packetBody + pos + 11 + dataSize); - -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, - "FLV Packet: type %02X, dataSize: %lu, tagSize: %lu, timeStamp: %lu ms", - (unsigned char)packetBody[pos], dataSize, prevTagSize, - nTimeStamp); -#endif - - if (prevTagSize != (dataSize + 11)) - { -#ifdef _DEBUG - RTMP_Log(RTMP_LOGWARNING, - "Tag and data size are not consitent, writing tag size according to dataSize+11: %d", - dataSize + 11); -#endif - - prevTagSize = dataSize + 11; - AMF_EncodeInt32(ptr + pos + 11 + dataSize, pend, - prevTagSize); - } - } - - pos += prevTagSize + 4; /*(11+dataSize+4); */ - } - } - ptr += len; - - if (packet.m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO) - { - /* FLV tag packets contain their own prevTagSize */ - AMF_EncodeInt32(ptr, pend, prevTagSize); - } - - /* In non-live this nTimeStamp can contain an absolute TS. - * Update ext timestamp with this absolute offset in non-live mode - * otherwise report the relative one - */ - /* RTMP_Log(RTMP_LOGDEBUG, "type: %02X, size: %d, pktTS: %dms, TS: %dms, bLiveStream: %d", packet.m_packetType, nPacketLen, packet.m_nTimeStamp, nTimeStamp, r->Link.lFlags & RTMP_LF_LIVE); */ - r->m_read.timestamp = (r->Link.lFlags & RTMP_LF_LIVE) ? packet.m_nTimeStamp : nTimeStamp; - - ret = size; - break; - } - - if (rtnGetNextMediaPacket) - RTMPPacket_Free(&packet); - - if (recopy) - { - len = ret > buflen ? buflen : ret; - memcpy(buf, r->m_read.buf, len); - r->m_read.bufpos = r->m_read.buf + len; - r->m_read.buflen = ret - len; - } - return ret; -} - -static const char flvHeader[] = { 'F', 'L', 'V', 0x01, - 0x00, /* 0x04 == audio, 0x01 == video */ - 0x00, 0x00, 0x00, 0x09, - 0x00, 0x00, 0x00, 0x00 -}; - -#define HEADERBUF (128*1024) -int -RTMP_Read(RTMP *r, char *buf, int size) -{ - int nRead = 0, total = 0; - - /* can't continue */ -fail: - switch (r->m_read.status) { - case RTMP_READ_EOF: - case RTMP_READ_COMPLETE: - return 0; - case RTMP_READ_ERROR: /* corrupted stream, resume failed */ - SetSockError(EINVAL); - return -1; - default: - break; - } - - /* first time thru */ - if (!(r->m_read.flags & RTMP_READ_HEADER)) - { - if (!(r->m_read.flags & RTMP_READ_RESUME)) - { - char *mybuf = malloc(HEADERBUF), *end = mybuf + HEADERBUF; - int cnt = 0; - r->m_read.buf = mybuf; - r->m_read.buflen = HEADERBUF; - - memcpy(mybuf, flvHeader, sizeof(flvHeader)); - r->m_read.buf += sizeof(flvHeader); - r->m_read.buflen -= sizeof(flvHeader); - cnt += sizeof(flvHeader); - - while (r->m_read.timestamp == 0) - { - nRead = Read_1_Packet(r, r->m_read.buf, r->m_read.buflen); - if (nRead < 0) - { - free(mybuf); - r->m_read.buf = NULL; - r->m_read.buflen = 0; - r->m_read.status = nRead; - goto fail; - } - /* buffer overflow, fix buffer and give up */ - if (r->m_read.buf < mybuf || r->m_read.buf > end) { - mybuf = realloc(mybuf, cnt + nRead); - memcpy(mybuf+cnt, r->m_read.buf, nRead); - free(r->m_read.buf); - r->m_read.buf = mybuf+cnt+nRead; - break; - } - cnt += nRead; - r->m_read.buf += nRead; - r->m_read.buflen -= nRead; - if (r->m_read.dataType == 5) - break; - } - mybuf[4] = r->m_read.dataType; - r->m_read.buflen = r->m_read.buf - mybuf; - r->m_read.buf = mybuf; - r->m_read.bufpos = mybuf; - } - r->m_read.flags |= RTMP_READ_HEADER; - } - - if ((r->m_read.flags & RTMP_READ_SEEKING) && r->m_read.buf) - { - /* drop whatever's here */ - free(r->m_read.buf); - r->m_read.buf = NULL; - r->m_read.bufpos = NULL; - r->m_read.buflen = 0; - } - - /* If there's leftover data buffered, use it up */ - if (r->m_read.buf) - { - nRead = r->m_read.buflen; - if (nRead > size) - nRead = size; - memcpy(buf, r->m_read.bufpos, nRead); - r->m_read.buflen -= nRead; - if (!r->m_read.buflen) - { - free(r->m_read.buf); - r->m_read.buf = NULL; - r->m_read.bufpos = NULL; - } - else - { - r->m_read.bufpos += nRead; - } - buf += nRead; - total += nRead; - size -= nRead; - } - - while (size > 0 && (nRead = Read_1_Packet(r, buf, size)) >= 0) - { - if (!nRead) continue; - buf += nRead; - total += nRead; - size -= nRead; - break; - } - if (nRead < 0) - r->m_read.status = nRead; - - if (size < 0) - total += size; - return total; -} - -static const AVal av_setDataFrame = AVC("@setDataFrame"); - -int -RTMP_Write(RTMP *r, const char *buf, int size) -{ - RTMPPacket *pkt = &r->m_write; - char *pend, *enc; - int s2 = size, ret, num; - - pkt->m_nChannel = 0x04; /* source channel */ - pkt->m_nInfoField2 = r->m_stream_id; - - while (s2) - { - if (!pkt->m_nBytesRead) - { - if (size < 11) { - printf("size: %d\n", size); - printf("too small\n"); - return 0; - } - - if (buf[0] == 'F' && buf[1] == 'L' && buf[2] == 'V') - { - buf += 13; - s2 -= 13; - } - - pkt->m_packetType = *buf++; - pkt->m_nBodySize = AMF_DecodeInt24(buf); - buf += 3; - pkt->m_nTimeStamp = AMF_DecodeInt24(buf); - buf += 3; - pkt->m_nTimeStamp |= *buf++ << 24; - buf += 3; - s2 -= 11; - - if (((pkt->m_packetType == RTMP_PACKET_TYPE_AUDIO - || pkt->m_packetType == RTMP_PACKET_TYPE_VIDEO) && - !pkt->m_nTimeStamp) || pkt->m_packetType == RTMP_PACKET_TYPE_INFO) - { - pkt->m_headerType = RTMP_PACKET_SIZE_LARGE; - if (pkt->m_packetType == RTMP_PACKET_TYPE_INFO) - pkt->m_nBodySize += 16; - } - else - { - pkt->m_headerType = RTMP_PACKET_SIZE_MEDIUM; - } - - if (!RTMPPacket_Alloc(pkt, pkt->m_nBodySize)) - { - RTMP_Log(RTMP_LOGDEBUG, "%s, failed to allocate packet", __FUNCTION__); - return FALSE; - } - enc = pkt->m_body; - pend = enc + pkt->m_nBodySize; - if (pkt->m_packetType == RTMP_PACKET_TYPE_INFO) - { - enc = AMF_EncodeString(enc, pend, &av_setDataFrame); - pkt->m_nBytesRead = enc - pkt->m_body; - } - } - else - { - enc = pkt->m_body + pkt->m_nBytesRead; - } - num = pkt->m_nBodySize - pkt->m_nBytesRead; - if (num > s2) - num = s2; - memcpy(enc, buf, num); - pkt->m_nBytesRead += num; - s2 -= num; - buf += num; - if (pkt->m_nBytesRead == pkt->m_nBodySize) - { - ret = RTMP_SendPacket(r, pkt, FALSE); - RTMPPacket_Free(pkt); - pkt->m_nBytesRead = 0; - if (!ret) - return -1; - buf += 4; - s2 -= 4; - if (s2 < 0) - break; - } - } - return size+s2; -} diff --git a/rtmp/rtmp_c/librtmp/rtmp.h b/rtmp/rtmp_c/librtmp/rtmp.h deleted file mode 100644 index 58abefc7..00000000 --- a/rtmp/rtmp_c/librtmp/rtmp.h +++ /dev/null @@ -1,401 +0,0 @@ -#ifndef __RTMP_H__ -#define __RTMP_H__ -/* - * Copyright (C) 2005-2008 Team XBMC - * http://www.xbmc.org - * Copyright (C) 2008-2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#if !defined(NO_CRYPTO) && !defined(CRYPTO) -#define CRYPTO -#endif - -#include -#include -#include - -#include "amf.h" - -#ifdef __cplusplus -extern "C" -{ -#endif - -#define RTMP_LIB_VERSION 0x020300 /* 2.3 */ - -#define RTMP_FEATURE_HTTP 0x01 -#define RTMP_FEATURE_ENC 0x02 -#define RTMP_FEATURE_SSL 0x04 -#define RTMP_FEATURE_MFP 0x08 /* not yet supported */ -#define RTMP_FEATURE_WRITE 0x10 /* publish, not play */ -#define RTMP_FEATURE_HTTP2 0x20 /* server-side rtmpt */ - -#define RTMP_PROTOCOL_UNDEFINED -1 -#define RTMP_PROTOCOL_RTMP 0 -#define RTMP_PROTOCOL_RTMPE RTMP_FEATURE_ENC -#define RTMP_PROTOCOL_RTMPT RTMP_FEATURE_HTTP -#define RTMP_PROTOCOL_RTMPS RTMP_FEATURE_SSL -#define RTMP_PROTOCOL_RTMPTE (RTMP_FEATURE_HTTP|RTMP_FEATURE_ENC) -#define RTMP_PROTOCOL_RTMPTS (RTMP_FEATURE_HTTP|RTMP_FEATURE_SSL) -#define RTMP_PROTOCOL_RTMFP RTMP_FEATURE_MFP - -#define RTMP_DEFAULT_CHUNKSIZE 128 - -/* needs to fit largest number of bytes recv() may return */ -#define RTMP_BUFFER_CACHE_SIZE (16*1024) - -#define RTMP_CHANNELS 65600 - - extern const char RTMPProtocolStringsLower[][7]; - extern const AVal RTMP_DefaultFlashVer; - extern int RTMP_ctrlC; - - uint32_t RTMP_GetTime(void); - -/* RTMP_PACKET_TYPE_... 0x00 */ -#define RTMP_PACKET_TYPE_CHUNK_SIZE 0x01 -/* RTMP_PACKET_TYPE_... 0x02 */ -#define RTMP_PACKET_TYPE_BYTES_READ_REPORT 0x03 -#define RTMP_PACKET_TYPE_CONTROL 0x04 -#define RTMP_PACKET_TYPE_SERVER_BW 0x05 -#define RTMP_PACKET_TYPE_CLIENT_BW 0x06 -/* RTMP_PACKET_TYPE_... 0x07 */ -#define RTMP_PACKET_TYPE_AUDIO 0x08 -#define RTMP_PACKET_TYPE_VIDEO 0x09 -/* RTMP_PACKET_TYPE_... 0x0A */ -/* RTMP_PACKET_TYPE_... 0x0B */ -/* RTMP_PACKET_TYPE_... 0x0C */ -/* RTMP_PACKET_TYPE_... 0x0D */ -/* RTMP_PACKET_TYPE_... 0x0E */ -#define RTMP_PACKET_TYPE_FLEX_STREAM_SEND 0x0F -#define RTMP_PACKET_TYPE_FLEX_SHARED_OBJECT 0x10 -#define RTMP_PACKET_TYPE_FLEX_MESSAGE 0x11 -#define RTMP_PACKET_TYPE_INFO 0x12 -#define RTMP_PACKET_TYPE_SHARED_OBJECT 0x13 -#define RTMP_PACKET_TYPE_INVOKE 0x14 -/* RTMP_PACKET_TYPE_... 0x15 */ -#define RTMP_PACKET_TYPE_FLASH_VIDEO 0x16 - -#define RTMP_MAX_HEADER_SIZE 18 - -#define RTMP_PACKET_SIZE_LARGE 0 -#define RTMP_PACKET_SIZE_MEDIUM 1 -#define RTMP_PACKET_SIZE_SMALL 2 -#define RTMP_PACKET_SIZE_MINIMUM 3 - - typedef struct RTMPChunk - { - int c_headerSize; - int c_chunkSize; - char *c_chunk; - char c_header[RTMP_MAX_HEADER_SIZE]; - } RTMPChunk; - - typedef struct RTMPPacket - { - uint8_t m_headerType; - uint8_t m_packetType; - uint8_t m_hasAbsTimestamp; /* timestamp absolute or relative? */ - int m_nChannel; - uint32_t m_nTimeStamp; /* timestamp */ - int32_t m_nInfoField2; /* last 4 bytes in a long header */ - uint32_t m_nBodySize; - uint32_t m_nBytesRead; - RTMPChunk *m_chunk; - 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 */ - int sb_timedout; - void *sb_ssl; - } RTMPSockBuf; - - void RTMPPacket_Reset(RTMPPacket *p); - void RTMPPacket_Dump(RTMPPacket *p); - int RTMPPacket_Alloc(RTMPPacket *p, uint32_t nSize); - void RTMPPacket_Free(RTMPPacket *p); - -#define RTMPPacket_IsReady(a) ((a)->m_nBytesRead == (a)->m_nBodySize) - - typedef struct RTMP_LNK - { - AVal hostname; - AVal sockshost; - - AVal playpath0; /* parsed from URL */ - AVal playpath; /* passed in explicitly */ - AVal tcUrl; - AVal swfUrl; - AVal pageUrl; - AVal app; - AVal auth; - AVal flashVer; - AVal subscribepath; - AVal usherToken; - AVal token; - AVal pubUser; - AVal pubPasswd; - AMFObject extras; - int edepth; - - int seekTime; - int stopTime; - -#define RTMP_LF_AUTH 0x0001 /* using auth param */ -#define RTMP_LF_LIVE 0x0002 /* stream is live */ -#define RTMP_LF_SWFV 0x0004 /* do SWF verification */ -#define RTMP_LF_PLST 0x0008 /* send playlist before play */ -#define RTMP_LF_BUFX 0x0010 /* toggle stream on BufferEmpty msg */ -#define RTMP_LF_FTCU 0x0020 /* free tcUrl on close */ -#define RTMP_LF_FAPU 0x0040 /* free app on close */ - int lFlags; - - int swfAge; - - int protocol; - int timeout; /* connection timeout in seconds */ - - int pFlags; /* unused, but kept to avoid breaking ABI */ - - unsigned short socksport; - unsigned short port; - -#ifdef CRYPTO -#define RTMP_SWF_HASHLEN 32 - void *dh; /* for encryption */ - void *rc4keyIn; - void *rc4keyOut; - - uint32_t SWFSize; - uint8_t SWFHash[RTMP_SWF_HASHLEN]; - char SWFVerificationResponse[RTMP_SWF_HASHLEN+10]; -#endif - } RTMP_LNK; - - /* state for read() wrapper */ - typedef struct RTMP_READ - { - char *buf; - char *bufpos; - unsigned int buflen; - uint32_t timestamp; - uint8_t dataType; - uint8_t flags; -#define RTMP_READ_HEADER 0x01 -#define RTMP_READ_RESUME 0x02 -#define RTMP_READ_NO_IGNORE 0x04 -#define RTMP_READ_GOTKF 0x08 -#define RTMP_READ_GOTFLVK 0x10 -#define RTMP_READ_SEEKING 0x20 - int8_t status; -#define RTMP_READ_COMPLETE -3 -#define RTMP_READ_ERROR -2 -#define RTMP_READ_EOF -1 -#define RTMP_READ_IGNORE 0 - - /* if bResume == TRUE */ - uint8_t initialFrameType; - uint32_t nResumeTS; - char *metaHeader; - char *initialFrame; - uint32_t nMetaHeaderSize; - uint32_t nInitialFrameSize; - uint32_t nIgnoredFrameCounter; - uint32_t nIgnoredFlvFrameCounter; - } RTMP_READ; - - typedef struct RTMP_METHOD - { - AVal name; - int num; - } RTMP_METHOD; - - typedef struct RTMP - { - int m_inChunkSize; - int m_outChunkSize; - int m_nBWCheckCounter; - int m_nBytesIn; - int m_nBytesInSent; - int m_nBufferMS; - int m_stream_id; /* returned in _result from createStream */ - int m_mediaChannel; - uint32_t m_mediaStamp; - uint32_t m_pauseStamp; - int m_pausing; - int m_nServerBW; - int m_nClientBW; - uint8_t m_nClientBW2; - uint8_t m_bPlaying; - uint8_t m_bSendEncoding; - uint8_t m_bSendCounter; - - int m_numInvokes; - int m_numCalls; - RTMP_METHOD *m_methodCalls; /* remote method calls queue */ - - int m_channelsAllocatedIn; - int m_channelsAllocatedOut; - RTMPPacket **m_vecChannelsIn; - RTMPPacket **m_vecChannelsOut; - int *m_channelTimestamp; /* abs timestamp of last packet */ - - double m_fAudioCodecs; /* audioCodecs for the connect packet */ - double m_fVideoCodecs; /* videoCodecs for the connect packet */ - double m_fEncoding; /* AMF0 or AMF3 */ - - double m_fDuration; /* duration of stream in seconds */ - - int m_msgCounter; /* RTMPT stuff */ - int m_polling; - int m_resplen; - int m_unackd; - AVal m_clientID; - - RTMP_READ m_read; - RTMPPacket m_write; - RTMPSockBuf m_sb; - RTMP_LNK Link; - } RTMP; - - int RTMP_ParseURL(const char *url, int *protocol, AVal *host, - unsigned int *port, AVal *playpath, AVal *app); - - void RTMP_ParsePlaypath(AVal *in, AVal *out); - void RTMP_SetBufferMS(RTMP *r, int size); - void RTMP_UpdateBufferMS(RTMP *r); - - void AV_clear(RTMP_METHOD *vals, int num); - - void SocksSetup(RTMP *r, AVal *sockshost); - int SocksNegotiate(RTMP *r); - int SendFCUnpublish(RTMP *r); - int HandShake(RTMP *r, int FP9HandShake); - int DecodeInt32LE(const char *data); - int SendBytesReceived(RTMP *r); - int SendConnectPacket(RTMP *r, RTMPPacket *cp); - int SendDeleteStream(RTMP *r, double dStreamId); - int ReadN(RTMP *r, char *buffer, int n); - int HandleInvoke(RTMP *r, const char *body, unsigned int nBodySize); - int HandleMetadata(RTMP *r, char *body, unsigned int len); - void HandleChangeChunkSize(RTMP *r, const RTMPPacket *packet); - void HandleAudio(RTMP *r, const RTMPPacket *packet); - void HandleVideo(RTMP *r, const RTMPPacket *packet); - void HandleCtrl(RTMP *r, const RTMPPacket *packet); - void HandleServerBW(RTMP *r, const RTMPPacket *packet); - void HandleClientBW(RTMP *r, const RTMPPacket *packet); - int RTMP_SetOpt(RTMP *r, const AVal *opt, AVal *arg); - int RTMP_SetupURL(RTMP *r, char *url); - void RTMP_SetupStream(RTMP *r, int protocol, - AVal *hostname, - unsigned int port, - AVal *sockshost, - AVal *playpath, - AVal *tcUrl, - AVal *swfUrl, - AVal *pageUrl, - AVal *app, - AVal *auth, - AVal *swfSHA256Hash, - uint32_t swfSize, - AVal *flashVer, - AVal *subscribepath, - AVal *usherToken, - int dStart, - int dStop, int bLiveStream, long int timeout); - int RTMP_Connect(RTMP *r, RTMPPacket *cp); - struct sockaddr; - int RTMP_Connect0(RTMP *r, struct sockaddr *svc); - int RTMP_Connect1(RTMP *r, RTMPPacket *cp); - int RTMP_Serve(RTMP *r); - int RTMP_TLS_Accept(RTMP *r, void *ctx); - int SendCheckBW(RTMP *r); - int SendCheckBWResult(RTMP *r, double txn); - int SendDeleteStream(RTMP *r, double dStreamId); - int SendFCSubscribe(RTMP *r, AVal *subscribepath); - int SendPlay(RTMP *r); - int SendBytesReceived(RTMP *r); - int SendUsherToken(RTMP *r, AVal *usherToken); - int RTMP_ReadPacket(RTMP *r, RTMPPacket *packet); - int RTMP_SendPacket(RTMP *r, RTMPPacket *packet, int queue); - int RTMP_SendChunk(RTMP *r, RTMPChunk *chunk); - int RTMP_IsConnected(RTMP *r); - int RTMP_Socket(RTMP *r); - int RTMP_IsTimedout(RTMP *r); - double RTMP_GetDuration(RTMP *r); - int RTMP_ToggleStream(RTMP *r); - int RTMP_ConnectStream(RTMP *r, int seekTime); - int RTMP_ReconnectStream(RTMP *r, int seekTime); - void RTMP_DeleteStream(RTMP *r); - int RTMP_GetNextMediaPacket(RTMP *r, RTMPPacket *packet); - int RTMP_ClientPacket(RTMP *r, RTMPPacket *packet); - void RTMP_Init(RTMP *r); - void RTMP_Close(RTMP *r); - RTMP *RTMP_Alloc(void); - void RTMP_Free(RTMP *r); - void RTMP_EnableWrite(RTMP *r); - void *RTMP_TLS_AllocServerContext(const char* cert, const char* key); - void RTMP_TLS_FreeServerContext(void *ctx); - int RTMP_LibVersion(void); - void RTMP_UserInterrupt(void); /* user typed Ctrl-C */ - int RTMP_SendCtrl(RTMP *r, short nType, unsigned int nObject, - unsigned int nTime); - - /* caller probably doesn't know current timestamp, should - * just use RTMP_Pause instead - */ - int RTMP_SendPause(RTMP *r, int DoPause, int dTime); - int RTMP_Pause(RTMP *r, int DoPause); - int RTMP_FindFirstMatchingProperty(AMFObject *obj, const AVal *name, - AMFObjectProperty * p); - int RTMPSockBuf_Fill(RTMPSockBuf *sb); - int RTMPSockBuf_Send(RTMPSockBuf *sb, const char *buf, int len); - int RTMPSockBuf_Close(RTMPSockBuf *sb); - int RTMP_SendCreateStream(RTMP *r); - int RTMP_SendSeek(RTMP *r, int dTime); - int RTMP_SendServerBW(RTMP *r); - int RTMP_SendClientBW(RTMP *r); - void RTMP_DropRequest(RTMP *r, int i, int freeit); - int RTMP_Read(RTMP *r, char *buf, int size); - int RTMP_Write(RTMP *r, const char *buf, int size); - void AV_erase(RTMP_METHOD *vals, int *num, int i, int freeit); - void DecodeTEA(AVal *key, AVal *text); - int SendFCPublish(RTMP *r); - int SendPlaylist(RTMP *r); - int SendPong(RTMP *r, double txn); - int SendPublish(RTMP *r); - int SendReleaseStream(RTMP *r); - int SendSecureTokenResponse(RTMP *r, AVal *resp); -/* hashswf.c */ - int RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, - int age); - -#ifdef __cplusplus -}; -#endif - -#endif diff --git a/rtmp/rtmp_c/librtmp/rtmp_sys.h b/rtmp/rtmp_c/librtmp/rtmp_sys.h deleted file mode 100644 index 85d7e53b..00000000 --- a/rtmp/rtmp_c/librtmp/rtmp_sys.h +++ /dev/null @@ -1,139 +0,0 @@ -#ifndef __RTMP_SYS_H__ -#define __RTMP_SYS_H__ -/* - * Copyright (C) 2010 Howard Chu - * - * This file is part of librtmp. - * - * librtmp is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1, - * or (at your option) any later version. - * - * librtmp 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 Lesser General Public License - * along with librtmp see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/lgpl.html - */ - -#ifdef _WIN32 - -#include -#include - -#ifdef _MSC_VER /* MSVC */ -#define snprintf _snprintf -#define strcasecmp stricmp -#define strncasecmp strnicmp -#define vsnprintf _vsnprintf -#endif - -#define GetSockError() WSAGetLastError() -#define SetSockError(e) WSASetLastError(e) -#define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e) -#define EWOULDBLOCK WSAETIMEDOUT /* we don't use nonblocking, but we do use timeouts */ -#define sleep(n) Sleep(n*1000) -#define msleep(n) Sleep(n) -#define SET_RCVTIMEO(tv,s) int tv = s*1000 -#else /* !_WIN32 */ -#include -#include -#include -#include -#include -#include -#include -#include -#define GetSockError() errno -#define SetSockError(e) errno = e -#undef closesocket -#define closesocket(s) close(s) -#define msleep(n) usleep(n*1000) -#define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0} -#endif - -#include "rtmp.h" - -#ifdef USE_POLARSSL -#include -#include -#include -#include -#if POLARSSL_VERSION_NUMBER < 0x01010000 -#define havege_random havege_rand -#endif -#if POLARSSL_VERSION_NUMBER >= 0x01020000 -#define SSL_SET_SESSION(S,resume,timeout,ctx) ssl_set_session(S,ctx) -#else -#define SSL_SET_SESSION(S,resume,timeout,ctx) ssl_set_session(S,resume,timeout,ctx) -#endif -typedef struct tls_ctx { - havege_state hs; - ssl_session ssn; -} tls_ctx; -typedef struct tls_server_ctx { - havege_state *hs; - x509_cert cert; - rsa_context key; - ssl_session ssn; - const char *dhm_P, *dhm_G; -} tls_server_ctx; - -#define TLS_CTX tls_ctx * -#define TLS_client(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\ - ssl_set_endpoint(s, SSL_IS_CLIENT); ssl_set_authmode(s, SSL_VERIFY_NONE);\ - ssl_set_rng(s, havege_random, &ctx->hs);\ - ssl_set_ciphersuites(s, ssl_default_ciphersuites);\ - SSL_SET_SESSION(s, 1, 600, &ctx->ssn) -#define TLS_server(ctx,s) s = malloc(sizeof(ssl_context)); ssl_init(s);\ - ssl_set_endpoint(s, SSL_IS_SERVER); ssl_set_authmode(s, SSL_VERIFY_NONE);\ - ssl_set_rng(s, havege_random, ((tls_server_ctx*)ctx)->hs);\ - ssl_set_ciphersuites(s, ssl_default_ciphersuites);\ - SSL_SET_SESSION(s, 1, 600, &((tls_server_ctx*)ctx)->ssn);\ - ssl_set_own_cert(s, &((tls_server_ctx*)ctx)->cert, &((tls_server_ctx*)ctx)->key);\ - ssl_set_dh_param(s, ((tls_server_ctx*)ctx)->dhm_P, ((tls_server_ctx*)ctx)->dhm_G) -#define TLS_setfd(s,fd) ssl_set_bio(s, net_recv, &fd, net_send, &fd) -#define TLS_connect(s) ssl_handshake(s) -#define TLS_accept(s) ssl_handshake(s) -#define TLS_read(s,b,l) ssl_read(s,(unsigned char *)b,l) -#define TLS_write(s,b,l) ssl_write(s,(unsigned char *)b,l) -#define TLS_shutdown(s) ssl_close_notify(s) -#define TLS_close(s) ssl_free(s); free(s) - -#elif defined(USE_GNUTLS) -#include -typedef struct tls_ctx { - gnutls_certificate_credentials_t cred; - gnutls_priority_t prios; -} tls_ctx; -#define TLS_CTX tls_ctx * -#define TLS_client(ctx,s) gnutls_init((gnutls_session_t *)(&s), GNUTLS_CLIENT); gnutls_priority_set(s, ctx->prios); gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, ctx->cred) -#define TLS_server(ctx,s) gnutls_init((gnutls_session_t *)(&s), GNUTLS_SERVER); gnutls_priority_set_direct(s, "NORMAL", NULL); gnutls_credentials_set(s, GNUTLS_CRD_CERTIFICATE, ctx) -#define TLS_setfd(s,fd) gnutls_transport_set_ptr(s, (gnutls_transport_ptr_t)(long)fd) -#define TLS_connect(s) gnutls_handshake(s) -#define TLS_accept(s) gnutls_handshake(s) -#define TLS_read(s,b,l) gnutls_record_recv(s,b,l) -#define TLS_write(s,b,l) gnutls_record_send(s,b,l) -#define TLS_shutdown(s) gnutls_bye(s, GNUTLS_SHUT_RDWR) -#define TLS_close(s) gnutls_deinit(s) - -#else /* USE_OPENSSL */ -#define TLS_CTX SSL_CTX * -#define TLS_client(ctx,s) s = SSL_new(ctx) -#define TLS_server(ctx,s) s = SSL_new(ctx) -#define TLS_setfd(s,fd) SSL_set_fd(s,fd) -#define TLS_connect(s) SSL_connect(s) -#define TLS_accept(s) SSL_accept(s) -#define TLS_read(s,b,l) SSL_read(s,b,l) -#define TLS_write(s,b,l) SSL_write(s,b,l) -#define TLS_shutdown(s) SSL_shutdown(s) -#define TLS_close(s) SSL_free(s) - -#endif -#endif diff --git a/rtmp/rtmp_c/openssl-1.0.1g.tar.gz.md5 b/rtmp/rtmp_c/openssl-1.0.1g.tar.gz.md5 deleted file mode 100644 index ab27bf7f..00000000 --- a/rtmp/rtmp_c/openssl-1.0.1g.tar.gz.md5 +++ /dev/null @@ -1 +0,0 @@ -de62b43dfcd858e66a74bee1c834e959 diff --git a/rtmp/rtmp_c/rtmpdump.1 b/rtmp/rtmp_c/rtmpdump.1 deleted file mode 100644 index 7bb53282..00000000 --- a/rtmp/rtmp_c/rtmpdump.1 +++ /dev/null @@ -1,311 +0,0 @@ -.TH RTMPDUMP 1 "2012-07-24" "RTMPDump v2.4" -.\" Copyright 2011 Howard Chu. -.\" Copying permitted according to the GNU General Public License V2. -.SH NAME -rtmpdump \- RTMP streaming media client -.SH SYNOPSIS -.B rtmpdump -.BI \-r \ url -[\c -.BI \-n \ hostname\fR] -[\c -.BI \-c \ port\fR] -[\c -.BI \-l \ protocol\fR] -[\c -.BI \-S \ host:port\fR] -[\c -.BI \-a \ app\fR] -[\c -.BI \-t \ tcUrl\fR] -[\c -.BI \-p \ pageUrl\fR] -[\c -.BI \-s \ swfUrl\fR] -[\c -.BI \-f \ flashVer\fR] -[\c -.BI \-u \ auth\fR] -[\c -.BI \-C \ conndata\fR] -[\c -.BI \-y \ playpath\fR] -[\c -.BR \-Y ] -[\c -.BR \-v ] -[\c -.BI \-d \ subscription\fR] -[\c -.BR \-e ] -[\c -.BI \-k \ skip\fR] -[\c -.BI \-A \ start\fR] -[\c -.BI \-B \ stop\fR] -[\c -.BI \-b \ buffer\fR] -[\c -.BI \-m \ timeout\fR] -[\c -.BI \-T \ key\fR] -[\c -.BI \-j \ JSON\fR] -[\c -.BI \-w \ swfHash\fR] -[\c -.BI \-x \ swfSize\fR] -[\c -.BI \-W \ swfUrl\fR] -[\c -.BI \-X \ swfAge\fR] -[\c -.BI \-o \ output\fR] -[\c -.BR \-# ] -[\c -.BR \-q ] -[\c -.BR \-V ] -[\c -.BR \-z ] -.br -.B rtmpdump \-h -.SH DESCRIPTION -.B rtmpdump -is a tool for dumping media content streamed over RTMP. -.LP -.B rtmpdump -makes a connection to the specified RTMP server and plays the media -specified by the given -.IR url . -The url should be of the form -.nf - rtmp[t][e]://hostname[:port][/app[/playpath]] -.fi - -Plain rtmp, as well as tunneled and encrypted sessions are supported. -.SH OPTIONS -.SS "Network Parameters" -These options define how to connect to the media server. -.TP -\fB\-\-rtmp \-r\fP\ \fIurl\fP -URL of the server and media content. -.TP -\fB\-\-host \-n\fP\ \fIhostname\fP -Overrides the hostname in the RTMP URL. -.TP -\fB\-\-port \-c\fP\ \fIport\fP -Overrides the port number in the RTMP URL. -.TP -\fB\-\-protocol \-l\fP\ \fInumber\fP -Overrides the protocol in the RTMP URL. -.nf - 0 = rtmp - 1 = rtmpt - 2 = rtmpe - 3 = rtmpte - 4 = rtmps - 5 = rtmpts -.fi -.TP -\fB\-\-socks \-S\fP\ \fIhost:port\fP -Use the specified SOCKS4 proxy. -.SS "Connection Parameters" -These options define the content of the RTMP Connect request packet. -If correct values are not provided, the media server will reject the -connection attempt. -.TP -\fB\-\-app \-a\fP\ \fIapp\fP -Name of application to connect to on the RTMP server. Overrides -the app in the RTMP URL. Sometimes the rtmpdump URL parser cannot -determine the app name automatically, so it must be given explicitly -using this option. -.TP -\fB\-\-tcUrl \-t\fP\ \fIurl\fP -URL of the target stream. Defaults to rtmp[e]://host[:port]/app/playpath. -.TP -\fB\-\-pageUrl \-p\fP\ \fIurl\fP -URL of the web page in which the media was embedded. By default no -value will be sent. -.TP -\fB\-\-swfUrl \-s\fP\ \fIurl\fP -URL of the SWF player for the media. By default no value will be sent. -.TP -\fB\-\-flashVer \-f\fP\ \fIversion\fP -Version of the Flash plugin used to run the SWF player. The -default is "LNX 10,0,32,18". -.TP -\fB\-\-auth \-u\fP\ \fIstring\fP -An authentication string to be appended to the Connect message. Using -this option will append a Boolean TRUE and then the specified string. -This option is only used by some particular servers and is -deprecated. The more general -.B \-\-conn -option should be used instead. -.TP -\fB\-\-conn \-C\fP\ \fItype:data\fP -Append arbitrary AMF data to the Connect message. The type -must be B for Boolean, N for number, S for string, O for object, or Z -for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, -respectively. Likewise for Objects the data must be 0 or 1 to end or -begin an object, respectively. Data items in subobjects may be named, by -prefixing the type with 'N' and specifying the name before the value, e.g. -NB:myFlag:1. This option may be used multiple times to construct arbitrary -AMF sequences. E.g. -.nf - \-C B:1 \-C S:authMe \-C O:1 \-C NN:code:1.23 \-C NS:flag:ok \-C O:0 -.fi -.SS "Session Parameters" -These options take effect after the Connect request has succeeded. -.TP -\fB\-\-playpath \-y\fP\ \fIpath\fP -Overrides the playpath parsed from the RTMP URL. Sometimes the -rtmpdump URL parser cannot determine the correct playpath -automatically, so it must be given explicitly using this option. -.TP -.B \-\-playlist \-Y -Issue a set_playlist command before sending the play command. The -playlist will just contain the current playpath. -.TP -.B \-\-live \-v -Specify that the media is a live stream. No resuming or seeking in -live streams is possible. -.TP -\fB\-\-subscribe \-d\fP\ \fIstream\fP -Name of live stream to subscribe to. Defaults to -.IR playpath . -.TP -.B \-\-realtime \-R -Download approximately in realtime, without attempting to speed up via -Pause/Unpause commands ("the BUFX hack"). -Useful for servers that jump backwards in time at the Unpause command. -Resuming and seeking in realtime streams is still possible. -.TP -.B \-\-resume \-e -Resume an incomplete RTMP download. -.TP -\fB\-\-skip \-k\fP\ \fInum\fP -Skip -.I num -keyframes when looking for the last keyframe from which to resume. This -may be useful if a regular attempt to resume fails. The default is 0. -.TP -\fB\-\-start \-A\fP\ \fInum\fP -Start at -.I num -seconds into the stream. Not valid for live streams. -.TP -\fB\-\-stop \-B\fP\ \fInum\fP -Stop at -.I num -seconds into the stream. -.TP -\fB\-\-buffer \-b\fP\ \fInum\fP -Set buffer time to -.I num -milliseconds. The default is 36000000. -.TP -\fB\-\-timeout \-m\fP\ \fInum\fP -Timeout the session after -.I num -seconds without receiving any data from the server. The default is 120. -.SS "Security Parameters" -These options handle additional authentication requests from the server. -.TP -\fB\-\-token \-T\fP\ \fIkey\fP -Key for SecureToken response, used if the server requires SecureToken -authentication. -.TP -\fB\-\-jtv \-j\fP\ \fIJSON\fP -JSON token used by legacy Justin.tv servers. Invokes NetStream.Authenticate.UsherToken -.TP -\fB\-\-swfhash \-w\fP\ \fIhexstring\fP -SHA256 hash of the decompressed SWF file. This option may be needed if -the server uses SWF Verification, but see the -.B \-\-swfVfy -option below. The hash is 32 bytes, and must be -given in hexadecimal. The -.B \-\-swfsize -option must always be used with this option. -.TP -\fB\-\-swfsize \-x\fP\ \fInum\fP -Size of the decompressed SWF file. This option may be needed if the -server uses SWF Verification, but see the -.B \-\-swfVfy -option below. The -.B \-\-swfhash -option must always be used with this option. -.TP -\fB\-\-swfVfy \-W\fP\ \fIurl\fP -URL of the SWF player for this media. This option replaces all three -of the -.BR \-\-swfUrl , -.BR \-\-swfhash , -and -.B \-\-swfsize -options. When this option is used, the SWF player is retrieved from the -specified URL and the hash and size are computed automatically. Also -the information is cached in a -.I .swfinfo -file in the user's home directory, so that it doesn't need to be retrieved -and recalculated every time rtmpdump is run. The .swfinfo file records -the URL, the time it was fetched, the modification timestamp of the SWF -file, its size, and its hash. By default, the cached info will be used -for 30 days before re-checking. -.TP -\fB\-\-swfAge \-X\fP\ \fIdays\fP -Specify how many days to use the cached SWF info before re-checking. Use -0 to always check the SWF URL. Note that if the check shows that the -SWF file has the same modification timestamp as before, it will not be -retrieved again. -.SS Miscellaneous -.TP -\fB\-\-flv \-o\fP\ \fIoutput\fP -Specify the output file name. If the name is \- or is omitted, the -stream is written to stdout. -.TP -.B \-\-hashes \-# -Display streaming progress with a hash mark for each 1% of progress, instead -of a byte counter. -.TP -.B \-\-quiet \-q -Suppress all command output. -.TP -.B \-\-verbose \-V -Verbose command output. -.TP -.B \-\-debug \-z -Debug level output. Extremely verbose, including hex dumps of all packet data. -.TP -.B \-\-help \-h -Print a summary of command options. -.SH EXIT STATUS -.TP -.B 0 -Successful program execution. -.TP -.B 1 -Unrecoverable error. -.TP -.B 2 -Incomplete transfer, resuming may get further. -.SH ENVIRONMENT -.TP -.B HOME -The value of -.RB $ HOME -is used as the location for the -.I .swfinfo -file. -.SH FILES -.TP -.I $HOME/.swfinfo -Cache of SWF Verification information -.SH "SEE ALSO" -.BR rtmpgw (8) -.SH AUTHORS -Andrej Stepanchuk, Howard Chu, The Flvstreamer Team -.br - diff --git a/rtmp/rtmp_c/rtmpdump.1.html b/rtmp/rtmp_c/rtmpdump.1.html deleted file mode 100644 index 4c39b359..00000000 --- a/rtmp/rtmp_c/rtmpdump.1.html +++ /dev/null @@ -1,439 +0,0 @@ - - -RTMPDUMP(1): - - - - - -
RTMPDUMP(1)RTMPDUMP(1) -
RTMPDump v2.42012-07-24RTMPDUMP(1) -


    - -
- -

NAME

    -rtmpdump − RTMP streaming media client -
- -

SYNOPSIS

    -rtmpdump -−r url -[−n hostname] -[−c port] -[−l protocol] -[−S host:port] -[−a app] -[−t tcUrl] -[−p pageUrl] -[−s swfUrl] -[−f flashVer] -[−u auth] -[−C conndata] -[−y playpath] -[−Y] -[−v] -[−R] -[−d subscription] -[−e] -[−k skip] -[−A start] -[−B stop] -[−b buffer] -[−m timeout] -[−T key] -[−j JSON] -[−w swfHash] -[−x swfSize] -[−W swfUrl] -[−X swfAge] -[−o output] -[−#] -[−q] -[−V] -[−z] -
    -rtmpdump −h -
- -

DESCRIPTION

    -rtmpdump -is a tool for dumping media content streamed over RTMP. -

    -rtmpdump -makes a connection to the specified RTMP server and plays the media -specified by the given -url. -The url should be of the form -

    -  rtmp[t][e]://hostname[:port][/app[/playpath]]
    -
    -

    -Plain rtmp, as well as tunneled and encrypted sessions are supported. -

- -

OPTIONS

    -
- -

Network Parameters

    -These options define how to connect to the media server. -

    -

    -−−rtmp −r url -
    -URL of the server and media content. -
    -

    -

    -−−host −n hostname -
    -Overrides the hostname in the RTMP URL. -
    -

    -

    -−−port −c port -
    -Overrides the port number in the RTMP URL. -
    -

    -

    -−−protocol −l number -
    -Overrides the protocol in the RTMP URL. -
    -  0 = rtmp
    -  1 = rtmpt
    -  2 = rtmpe
    -  3 = rtmpte
    -  4 = rtmps
    -  5 = rtmpts
    -
    -
    -

    -

    -−−socks −S host:port -
    -Use the specified SOCKS4 proxy. -
    -
- -

Connection Parameters

    -These options define the content of the RTMP Connect request packet. -If correct values are not provided, the media server will reject the -connection attempt. -

    -

    -−−app −a app -
    -Name of application to connect to on the RTMP server. Overrides -the app in the RTMP URL. Sometimes the rtmpdump URL parser cannot -determine the app name automatically, so it must be given explicitly -using this option. -
    -

    -

    -−−tcUrl −t url -
    -URL of the target stream. Defaults to rtmp[e]://host[:port]/app/playpath. -
    -

    -

    -−−pageUrl −p url -
    -URL of the web page in which the media was embedded. By default no -value will be sent. -
    -

    -

    -−−swfUrl −s url -
    -URL of the SWF player for the media. By default no value will be sent. -
    -

    -

    -−−flashVer −f version -
    -Version of the Flash plugin used to run the SWF player. The -default is "LNX 10,0,32,18". -
    -

    -

    -−−auth −u string -
    -An authentication string to be appended to the Connect message. Using -this option will append a Boolean TRUE and then the specified string. -This option is only used by some particular servers and is -deprecated. The more general -−−conn -option should be used instead. -
    -

    -

    -−−conn −C type:data -
    -Append arbitrary AMF data to the Connect message. The type -must be B for Boolean, N for number, S for string, O for object, or Z -for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, -respectively. Likewise for Objects the data must be 0 or 1 to end or -begin an object, respectively. Data items in subobjects may be named, by -prefixing the type with 'N' and specifying the name before the value, e.g. -NB:myFlag:1. This option may be used multiple times to construct arbitrary -AMF sequences. E.g. -
    -  −C B:1 −C S:authMe −C O:1 −C NN:code:1.23 −C NS:flag:ok −C O:0
    -
    -
    -
- -

Session Parameters

    -These options take effect after the Connect request has succeeded. -

    -

    -−−playpath −y path -
    -Overrides the playpath parsed from the RTMP URL. Sometimes the -rtmpdump URL parser cannot determine the correct playpath -automatically, so it must be given explicitly using this option. -
    -

    -

    -−−playlist −Y -
    -Issue a set_playlist command before sending the play command. The -playlist will just contain the current playpath. -
    -

    -

    -−−live −v -
    -Specify that the media is a live stream. No resuming or seeking in -live streams is possible. -
    -

    -

    -−−subscribe −d stream -
    -Name of live stream to subscribe to. Defaults to -playpath. -
    -

    -

    -−−realtime −R -
    -Download approximately in realtime, without attempting to speed up via -Pause/Unpause commands ("the BUFX hack"). -Useful for servers that jump backwards in time at the Unpause command. -Resuming and seeking in realtime streams is still possible. -
    -

    -

    -−−resume −e -
    -Resume an incomplete RTMP download. -
    -

    -

    -−−skip −k num -
    -Skip -num -keyframes when looking for the last keyframe from which to resume. This -may be useful if a regular attempt to resume fails. The default is 0. -
    -

    -

    -−−start −A num -
    -Start at -num -seconds into the stream. Not valid for live streams. -
    -

    -

    -−−stop −B num -
    -Stop at -num -seconds into the stream. -
    -

    -

    -−−buffer −b num -
    -Set buffer time to -num -milliseconds. The default is 36000000. -
    -

    -

    -−−timeout −m num -
    -Timeout the session after -num -seconds without receiving any data from the server. The default is 120. -
    -
- -

Security Parameters

    -These options handle additional authentication requests from the server. -

    -

    -−−token −T key -
    -Key for SecureToken response, used if the server requires SecureToken -authentication. -
    -

    -

    -−−jtv −j JSON -
    -JSON token used by legacy Justin.tv servers. Invokes NetStream.Authenticate.UsherToken -
    -

    -

    -−−swfhash −w hexstring -
    -SHA256 hash of the decompressed SWF file. This option may be needed if -the server uses SWF Verification, but see the -−−swfVfy -option below. The hash is 32 bytes, and must be -given in hexadecimal. The -−−swfsize -option must always be used with this option. -
    -

    -

    -−−swfsize −x num -
    -Size of the decompressed SWF file. This option may be needed if the -server uses SWF Verification, but see the -−−swfVfy -option below. The -−−swfhash -option must always be used with this option. -
    -

    -

    -−−swfVfy −W url -
    -URL of the SWF player for this media. This option replaces all three -of the -−−swfUrl, -−−swfhash, -and -−−swfsize -options. When this option is used, the SWF player is retrieved from the -specified URL and the hash and size are computed automatically. Also -the information is cached in a -.swfinfo -file in the user's home directory, so that it doesn't need to be retrieved -and recalculated every time rtmpdump is run. The .swfinfo file records -the URL, the time it was fetched, the modification timestamp of the SWF -file, its size, and its hash. By default, the cached info will be used -for 30 days before re-checking. -
    -

    -

    -−−swfAge −X days -
    -Specify how many days to use the cached SWF info before re-checking. Use -0 to always check the SWF URL. Note that if the check shows that the -SWF file has the same modification timestamp as before, it will not be -retrieved again. -
    -
- -

Miscellaneous

    -

    -

    -−−flv −o output -
    -Specify the output file name. If the name is − or is omitted, the -stream is written to stdout. -
    -

    -

    -−−hashes −# -
    -Display streaming progress with a hash mark for each 1% of progress, instead -of a byte counter. -
    -

    -

    -−−quiet −q -
    -Suppress all command output. -
    -

    -

    -−−verbose −V -
    -Verbose command output. -
    -

    -

    -−−debug −z -
    -Debug level output. Extremely verbose, including hex dumps of all packet data. -
    -

    -

    -−−help −h -
    -Print a summary of command options. -
    -
- -

EXIT STATUS

    -

    -

    -0 -
    -Successful program execution. -
    -

    -

    -1 -
    -Unrecoverable error. -
    -

    -

    -2 -
    -Incomplete transfer, resuming may get further. -
    -
- -

ENVIRONMENT

    -

    -

    -HOME -
    -The value of -$HOME -is used as the location for the -.swfinfo -file. -
    -
- -

FILES

    -

    -

    -$HOME/.swfinfo -
    -Cache of SWF Verification information -
    -
- -

SEE ALSO

- -

AUTHORS

diff --git a/rtmp/rtmp_c/rtmpdump.c b/rtmp/rtmp_c/rtmpdump.c deleted file mode 100644 index 13741a7d..00000000 --- a/rtmp/rtmp_c/rtmpdump.c +++ /dev/null @@ -1,1419 +0,0 @@ -/* RTMPDump - * Copyright (C) 2009 Andrej Stepanchuk - * 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, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - */ - -#define _FILE_OFFSET_BITS 64 - -#include -#include -#include -#include - -#include // to catch Ctrl-C -#include - -#include "librtmp/rtmp_sys.h" -#include "librtmp/log.h" - -#ifdef WIN32 -#define fseeko fseeko64 -#define ftello ftello64 -#include -#include -#define SET_BINMODE(f) setmode(fileno(f), O_BINARY) -#else -#define SET_BINMODE(f) -#endif - -#define RD_SUCCESS 0 -#define RD_FAILED 1 -#define RD_INCOMPLETE 2 -#define RD_NO_CONNECT 3 - -#define DEF_TIMEOUT 30 /* seconds */ -#define DEF_BUFTIME (10 * 60 * 60 * 1000) /* 10 hours default */ -#define DEF_SKIPFRM 0 - -// starts sockets -int -InitSockets() -{ -#ifdef WIN32 - WORD version; - WSADATA wsaData; - - version = MAKEWORD(1, 1); - return (WSAStartup(version, &wsaData) == 0); -#else - return TRUE; -#endif -} - -inline void -CleanupSockets() -{ -#ifdef WIN32 - WSACleanup(); -#endif -} - -#ifdef _DEBUG -uint32_t debugTS = 0; -int pnum = 0; - -FILE *netstackdump = 0; -FILE *netstackdump_read = 0; -#endif - -uint32_t nIgnoredFlvFrameCounter = 0; -uint32_t nIgnoredFrameCounter = 0; -#define MAX_IGNORED_FRAMES 50 - -FILE *file = 0; - -void -sigIntHandler(int sig) -{ - RTMP_ctrlC = TRUE; - RTMP_LogPrintf("Caught signal: %d, cleaning up, just a second...\n", sig); - // ignore all these signals now and let the connection close - signal(SIGINT, SIG_IGN); - signal(SIGTERM, SIG_IGN); -#ifndef WIN32 - signal(SIGHUP, SIG_IGN); - signal(SIGPIPE, SIG_IGN); - signal(SIGQUIT, SIG_IGN); -#endif -} - -#define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf)) -int hex2bin(char *str, char **hex) -{ - char *ptr; - int i, l = strlen(str); - - if (l & 1) - return 0; - - *hex = malloc(l/2); - ptr = *hex; - if (!ptr) - return 0; - - for (i=0; i 0) - { - // verify FLV format and read header - uint32_t prevTagSize = 0; - - // check we've got a valid FLV file to continue! - if (fread(hbuf, 1, 13, *file) != 13) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't read FLV file header!"); - return RD_FAILED; - } - if (hbuf[0] != 'F' || hbuf[1] != 'L' || hbuf[2] != 'V' - || hbuf[3] != 0x01) - { - RTMP_Log(RTMP_LOGERROR, "Invalid FLV file!"); - return RD_FAILED; - } - - if ((hbuf[4] & 0x05) == 0) - { - RTMP_Log(RTMP_LOGERROR, - "FLV file contains neither video nor audio, aborting!"); - return RD_FAILED; - } - - uint32_t dataOffset = AMF_DecodeInt32(hbuf + 5); - fseek(*file, dataOffset, SEEK_SET); - - if (fread(hbuf, 1, 4, *file) != 4) - { - RTMP_Log(RTMP_LOGERROR, "Invalid FLV file: missing first prevTagSize!"); - return RD_FAILED; - } - prevTagSize = AMF_DecodeInt32(hbuf); - if (prevTagSize != 0) - { - RTMP_Log(RTMP_LOGWARNING, - "First prevTagSize is not zero: prevTagSize = 0x%08X", - prevTagSize); - } - - // go through the file to find the meta data! - off_t pos = dataOffset + 4; - int bFoundMetaHeader = FALSE; - - while (pos < *size - 4 && !bFoundMetaHeader) - { - fseeko(*file, pos, SEEK_SET); - if (fread(hbuf, 1, 4, *file) != 4) - break; - - uint32_t dataSize = AMF_DecodeInt24(hbuf + 1); - - if (hbuf[0] == 0x12) - { - if (dataSize > bufferSize) - { - /* round up to next page boundary */ - bufferSize = dataSize + 4095; - bufferSize ^= (bufferSize & 4095); - free(buffer); - buffer = malloc(bufferSize); - if (!buffer) - return RD_FAILED; - } - - fseeko(*file, pos + 11, SEEK_SET); - if (fread(buffer, 1, dataSize, *file) != dataSize) - break; - - AMFObject metaObj; - int nRes = AMF_Decode(&metaObj, buffer, dataSize, FALSE); - if (nRes < 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, error decoding meta data packet", - __FUNCTION__); - break; - } - - AVal metastring; - AMFProp_GetString(AMF_GetProp(&metaObj, NULL, 0), &metastring); - - if (AVMATCH(&metastring, &av_onMetaData)) - { - AMF_Dump(&metaObj); - - *nMetaHeaderSize = dataSize; - if (*metaHeader) - free(*metaHeader); - *metaHeader = (char *) malloc(*nMetaHeaderSize); - memcpy(*metaHeader, buffer, *nMetaHeaderSize); - - // get duration - AMFObjectProperty prop; - if (RTMP_FindFirstMatchingProperty - (&metaObj, &av_duration, &prop)) - { - *duration = AMFProp_GetNumber(&prop); - RTMP_Log(RTMP_LOGDEBUG, "File has duration: %f", *duration); - } - - bFoundMetaHeader = TRUE; - break; - } - //metaObj.Reset(); - //delete obj; - } - pos += (dataSize + 11 + 4); - } - - free(buffer); - if (!bFoundMetaHeader) - RTMP_Log(RTMP_LOGWARNING, "Couldn't locate meta data!"); - } - - return RD_SUCCESS; -} - -int -GetLastKeyframe(FILE * file, // output file [in] - int nSkipKeyFrames, // max number of frames to skip when searching for key frame [in] - uint32_t * dSeek, // offset of the last key frame [out] - char **initialFrame, // content of the last keyframe [out] - int *initialFrameType, // initial frame type (audio/video) [out] - uint32_t * nInitialFrameSize) // length of initialFrame [out] -{ - const size_t bufferSize = 16; - char buffer[bufferSize]; - uint8_t dataType; - int bAudioOnly; - off_t size; - - fseek(file, 0, SEEK_END); - size = ftello(file); - - fseek(file, 4, SEEK_SET); - if (fread(&dataType, sizeof(uint8_t), 1, file) != 1) - return RD_FAILED; - - bAudioOnly = (dataType & 0x4) && !(dataType & 0x1); - - RTMP_Log(RTMP_LOGDEBUG, "bAudioOnly: %d, size: %llu", bAudioOnly, - (unsigned long long) size); - - // ok, we have to get the timestamp of the last keyframe (only keyframes are seekable) / last audio frame (audio only streams) - - //if(!bAudioOnly) // we have to handle video/video+audio different since we have non-seekable frames - //{ - // find the last seekable frame - off_t tsize = 0; - uint32_t prevTagSize = 0; - - // go through the file and find the last video keyframe - do - { - int xread; - skipkeyframe: - if (size - tsize < 13) - { - RTMP_Log(RTMP_LOGERROR, - "Unexpected start of file, error in tag sizes, couldn't arrive at prevTagSize=0"); - return RD_FAILED; - } - fseeko(file, size - tsize - 4, SEEK_SET); - xread = fread(buffer, 1, 4, file); - if (xread != 4) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't read prevTagSize from file!"); - return RD_FAILED; - } - - prevTagSize = AMF_DecodeInt32(buffer); - //RTMP_Log(RTMP_LOGDEBUG, "Last packet: prevTagSize: %d", prevTagSize); - - if (prevTagSize == 0) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't find keyframe to resume from!"); - return RD_FAILED; - } - - if (prevTagSize < 0 || prevTagSize > size - 4 - 13) - { - RTMP_Log(RTMP_LOGERROR, - "Last tag size must be greater/equal zero (prevTagSize=%d) and smaller then filesize, corrupt file!", - prevTagSize); - return RD_FAILED; - } - tsize += prevTagSize + 4; - - // read header - fseeko(file, size - tsize, SEEK_SET); - if (fread(buffer, 1, 12, file) != 12) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't read header!"); - return RD_FAILED; - } - //* -#ifdef _DEBUG - uint32_t ts = AMF_DecodeInt24(buffer + 4); - ts |= (buffer[7] << 24); - RTMP_Log(RTMP_LOGDEBUG, "%02X: TS: %d ms", buffer[0], ts); -#endif //*/ - - // this just continues the loop whenever the number of skipped frames is > 0, - // so we look for the next keyframe to continue with - // - // this helps if resuming from the last keyframe fails and one doesn't want to start - // the download from the beginning - // - if (nSkipKeyFrames > 0 - && !(!bAudioOnly - && (buffer[0] != 0x09 || (buffer[11] & 0xf0) != 0x10))) - { -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, - "xxxxxxxxxxxxxxxxxxxxxxxx Well, lets go one more back!"); -#endif - nSkipKeyFrames--; - goto skipkeyframe; - } - - } - while ((bAudioOnly && buffer[0] != 0x08) || (!bAudioOnly && (buffer[0] != 0x09 || (buffer[11] & 0xf0) != 0x10))); // as long as we don't have a keyframe / last audio frame - - // save keyframe to compare/find position in stream - *initialFrameType = buffer[0]; - *nInitialFrameSize = prevTagSize - 11; - *initialFrame = (char *) malloc(*nInitialFrameSize); - - fseeko(file, size - tsize + 11, SEEK_SET); - if (fread(*initialFrame, 1, *nInitialFrameSize, file) != *nInitialFrameSize) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't read last keyframe, aborting!"); - return RD_FAILED; - } - - *dSeek = AMF_DecodeInt24(buffer + 4); // set seek position to keyframe tmestamp - *dSeek |= (buffer[7] << 24); - //} - //else // handle audio only, we can seek anywhere we'd like - //{ - //} - - if (*dSeek < 0) - { - RTMP_Log(RTMP_LOGERROR, - "Last keyframe timestamp is negative, aborting, your file is corrupt!"); - return RD_FAILED; - } - RTMP_Log(RTMP_LOGDEBUG, "Last keyframe found at: %d ms, size: %d, type: %02X", *dSeek, - *nInitialFrameSize, *initialFrameType); - - /* - // now read the timestamp of the frame before the seekable keyframe: - fseeko(file, size-tsize-4, SEEK_SET); - if(fread(buffer, 1, 4, file) != 4) { - RTMP_Log(RTMP_LOGERROR, "Couldn't read prevTagSize from file!"); - goto start; - } - uint32_t prevTagSize = RTMP_LIB::AMF_DecodeInt32(buffer); - fseeko(file, size-tsize-4-prevTagSize+4, SEEK_SET); - if(fread(buffer, 1, 4, file) != 4) { - RTMP_Log(RTMP_LOGERROR, "Couldn't read previous timestamp!"); - goto start; - } - uint32_t timestamp = RTMP_LIB::AMF_DecodeInt24(buffer); - timestamp |= (buffer[3]<<24); - - RTMP_Log(RTMP_LOGDEBUG, "Previous timestamp: %d ms", timestamp); - */ - - if (*dSeek != 0) - { - // seek to position after keyframe in our file (we will ignore the keyframes resent by the server - // since they are sent a couple of times and handling this would be a mess) - fseeko(file, size - tsize + prevTagSize + 4, SEEK_SET); - - // make sure the WriteStream doesn't write headers and ignores all the 0ms TS packets - // (including several meta data headers and the keyframe we seeked to) - //bNoHeader = TRUE; if bResume==true this is true anyway - } - - //} - - return RD_SUCCESS; -} - -int -Download(RTMP * rtmp, // connected RTMP object - FILE * file, uint32_t dSeek, uint32_t dStopOffset, double duration, int bResume, char *metaHeader, uint32_t nMetaHeaderSize, char *initialFrame, int initialFrameType, uint32_t nInitialFrameSize, int nSkipKeyFrames, int bStdoutMode, int bLiveStream, int bRealtimeStream, int bHashes, int bOverrideBufferTime, uint32_t bufferTime, double *percent) // percentage downloaded [out] -{ - int32_t now, lastUpdate; - int bufferSize = 64 * 1024; - char *buffer; - int nRead = 0; - off_t size = ftello(file); - unsigned long lastPercent = 0; - - rtmp->m_read.timestamp = dSeek; - - *percent = 0.0; - - if (rtmp->m_read.timestamp) - { - RTMP_Log(RTMP_LOGDEBUG, "Continuing at TS: %d ms\n", rtmp->m_read.timestamp); - } - - if (bLiveStream) - { - RTMP_LogPrintf("Starting Live Stream\n"); - } - else - { - // print initial status - // Workaround to exit with 0 if the file is fully (> 99.9%) downloaded - if (duration > 0) - { - if ((double) rtmp->m_read.timestamp >= (double) duration * 999.0) - { - RTMP_LogPrintf("Already Completed at: %.3f sec Duration=%.3f sec\n", - (double) rtmp->m_read.timestamp / 1000.0, - (double) duration / 1000.0); - return RD_SUCCESS; - } - else - { - *percent = ((double) rtmp->m_read.timestamp) / (duration * 1000.0) * 100.0; - *percent = ((double) (int) (*percent * 10.0)) / 10.0; - RTMP_LogPrintf("%s download at: %.3f kB / %.3f sec (%.1f%%)\n", - bResume ? "Resuming" : "Starting", - (double) size / 1024.0, (double) rtmp->m_read.timestamp / 1000.0, - *percent); - } - } - else - { - RTMP_LogPrintf("%s download at: %.3f kB\n", - bResume ? "Resuming" : "Starting", - (double) size / 1024.0); - } - if (bRealtimeStream) - RTMP_LogPrintf(" in approximately realtime (disabled BUFX speedup hack)\n"); - } - - if (dStopOffset > 0) - RTMP_LogPrintf("For duration: %.3f sec\n", (double) (dStopOffset - dSeek) / 1000.0); - - if (bResume && nInitialFrameSize > 0) - rtmp->m_read.flags |= RTMP_READ_RESUME; - rtmp->m_read.initialFrameType = initialFrameType; - rtmp->m_read.nResumeTS = dSeek; - rtmp->m_read.metaHeader = metaHeader; - rtmp->m_read.initialFrame = initialFrame; - rtmp->m_read.nMetaHeaderSize = nMetaHeaderSize; - rtmp->m_read.nInitialFrameSize = nInitialFrameSize; - - buffer = (char *) malloc(bufferSize); - - now = RTMP_GetTime(); - lastUpdate = now - 1000; - do - { - nRead = RTMP_Read(rtmp, buffer, bufferSize); - //RTMP_LogPrintf("nRead: %d\n", nRead); - if (nRead > 0) - { - if (fwrite(buffer, sizeof(unsigned char), nRead, file) != - (size_t) nRead) - { - RTMP_Log(RTMP_LOGERROR, "%s: Failed writing, exiting!", __FUNCTION__); - free(buffer); - return RD_FAILED; - } - size += nRead; - - //RTMP_LogPrintf("write %dbytes (%.1f kB)\n", nRead, nRead/1024.0); - if (duration <= 0) // if duration unknown try to get it from the stream (onMetaData) - duration = RTMP_GetDuration(rtmp); - - if (duration > 0) - { - // make sure we claim to have enough buffer time! - if (!bOverrideBufferTime && bufferTime < (duration * 1000.0)) - { - bufferTime = (uint32_t) (duration * 1000.0) + 5000; // extra 5sec to make sure we've got enough - - RTMP_Log(RTMP_LOGDEBUG, - "Detected that buffer time is less than duration, resetting to: %dms", - bufferTime); - RTMP_SetBufferMS(rtmp, bufferTime); - RTMP_UpdateBufferMS(rtmp); - } - *percent = ((double) rtmp->m_read.timestamp) / (duration * 1000.0) * 100.0; - *percent = ((double) (int) (*percent * 10.0)) / 10.0; - if (bHashes) - { - if (lastPercent + 1 <= *percent) - { - RTMP_LogStatus("#"); - lastPercent = (unsigned long) *percent; - } - } - else - { - now = RTMP_GetTime(); - if (abs(now - lastUpdate) > 200) - { - RTMP_LogStatus("\r%.3f kB / %.2f sec (%.1f%%)", - (double) size / 1024.0, - (double) (rtmp->m_read.timestamp) / 1000.0, *percent); - lastUpdate = now; - } - } - } - else - { - now = RTMP_GetTime(); - if (abs(now - lastUpdate) > 200) - { - if (bHashes) - RTMP_LogStatus("#"); - else - RTMP_LogStatus("\r%.3f kB / %.2f sec", (double) size / 1024.0, - (double) (rtmp->m_read.timestamp) / 1000.0); - lastUpdate = now; - } - } - } - else - { -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, "zero read!"); -#endif - if (rtmp->m_read.status == RTMP_READ_EOF) - break; - } - - } - while (!RTMP_ctrlC && nRead > -1 && RTMP_IsConnected(rtmp) && !RTMP_IsTimedout(rtmp)); - free(buffer); - if (nRead < 0) - nRead = rtmp->m_read.status; - - /* Final status update */ - if (!bHashes) - { - if (duration > 0) - { - *percent = ((double) rtmp->m_read.timestamp) / (duration * 1000.0) * 100.0; - *percent = ((double) (int) (*percent * 10.0)) / 10.0; - RTMP_LogStatus("\r%.3f kB / %.2f sec (%.1f%%)", - (double) size / 1024.0, - (double) (rtmp->m_read.timestamp) / 1000.0, *percent); - } - else - { - RTMP_LogStatus("\r%.3f kB / %.2f sec", (double) size / 1024.0, - (double) (rtmp->m_read.timestamp) / 1000.0); - } - } - - RTMP_Log(RTMP_LOGDEBUG, "RTMP_Read returned: %d", nRead); - - if (bResume && nRead == -2) - { - RTMP_LogPrintf("Couldn't resume FLV file, try --skip %d\n\n", - nSkipKeyFrames + 1); - return RD_FAILED; - } - - if (nRead == -3) - return RD_SUCCESS; - - if ((duration > 0 && *percent < 99.9) || RTMP_ctrlC || nRead < 0 - || RTMP_IsTimedout(rtmp)) - { - return RD_INCOMPLETE; - } - - return RD_SUCCESS; -} - -#define STR2AVAL(av,str) av.av_val = str; av.av_len = strlen(av.av_val) - -void usage(char *prog) -{ - RTMP_LogPrintf - ("\n%s: This program dumps the media content streamed over RTMP.\n\n", prog); - RTMP_LogPrintf("--help|-h Prints this help screen.\n"); - RTMP_LogPrintf - ("--url|-i url URL with options included (e.g. rtmp://host[:port]/path swfUrl=url tcUrl=url)\n"); - RTMP_LogPrintf - ("--rtmp|-r url URL (e.g. rtmp://host[:port]/path)\n"); - RTMP_LogPrintf - ("--host|-n hostname Overrides the hostname in the rtmp url\n"); - RTMP_LogPrintf - ("--port|-c port Overrides the port in the rtmp url\n"); - RTMP_LogPrintf - ("--socks|-S host:port Use the specified SOCKS proxy\n"); - RTMP_LogPrintf - ("--protocol|-l num Overrides the protocol in the rtmp url (0 - RTMP, 2 - RTMPE)\n"); - RTMP_LogPrintf - ("--playpath|-y path Overrides the playpath parsed from rtmp url\n"); - RTMP_LogPrintf - ("--playlist|-Y Set playlist before playing\n"); - RTMP_LogPrintf("--swfUrl|-s url URL to player swf file\n"); - RTMP_LogPrintf - ("--tcUrl|-t url URL to played stream (default: \"rtmp://host[:port]/app\")\n"); - RTMP_LogPrintf("--pageUrl|-p url Web URL of played programme\n"); - RTMP_LogPrintf("--app|-a app Name of target app on server\n"); -#ifdef CRYPTO - RTMP_LogPrintf - ("--swfhash|-w hexstring SHA256 hash of the decompressed SWF file (32 bytes)\n"); - RTMP_LogPrintf - ("--swfsize|-x num Size of the decompressed SWF file, required for SWFVerification\n"); - RTMP_LogPrintf - ("--swfVfy|-W url URL to player swf file, compute hash/size automatically\n"); - RTMP_LogPrintf - ("--swfAge|-X days Number of days to use cached SWF hash before refreshing\n"); -#endif - RTMP_LogPrintf - ("--auth|-u string Authentication string to be appended to the connect string\n"); - RTMP_LogPrintf - ("--conn|-C type:data Arbitrary AMF data to be appended to the connect string\n"); - RTMP_LogPrintf - (" B:boolean(0|1), S:string, N:number, O:object-flag(0|1),\n"); - RTMP_LogPrintf - (" Z:(null), NB:name:boolean, NS:name:string, NN:name:number\n"); - RTMP_LogPrintf - ("--flashVer|-f string Flash version string (default: \"%s\")\n", - RTMP_DefaultFlashVer.av_val); - RTMP_LogPrintf - ("--live|-v Save a live stream, no --resume (seeking) of live streams possible\n"); - RTMP_LogPrintf - ("--subscribe|-d string Stream name to subscribe to (otherwise defaults to playpath if live is specifed)\n"); - RTMP_LogPrintf - ("--realtime|-R Don't attempt to speed up download via the Pause/Unpause BUFX hack\n"); - RTMP_LogPrintf - ("--flv|-o string FLV output file name, if the file name is - print stream to stdout\n"); - RTMP_LogPrintf - ("--resume|-e Resume a partial RTMP download\n"); - RTMP_LogPrintf - ("--timeout|-m num Timeout connection num seconds (default: %u)\n", - DEF_TIMEOUT); - RTMP_LogPrintf - ("--start|-A num Start at num seconds into stream (not valid when using --live)\n"); - RTMP_LogPrintf - ("--stop|-B num Stop at num seconds into stream\n"); - RTMP_LogPrintf - ("--token|-T key Key for SecureToken response\n"); - RTMP_LogPrintf - ("--jtv|-j JSON Authentication token for Justin.tv legacy servers\n"); - RTMP_LogPrintf - ("--hashes|-# Display progress with hashes, not with the byte counter\n"); - RTMP_LogPrintf - ("--buffer|-b Buffer time in milliseconds (default: %u)\n", - DEF_BUFTIME); - RTMP_LogPrintf - ("--skip|-k num Skip num keyframes when looking for last keyframe to resume from. Useful if resume fails (default: %d)\n\n", - DEF_SKIPFRM); - RTMP_LogPrintf - ("--quiet|-q Suppresses all command output.\n"); - RTMP_LogPrintf("--verbose|-V Verbose command output.\n"); - RTMP_LogPrintf("--debug|-z Debug level command output.\n"); - RTMP_LogPrintf - ("If you don't pass parameters for swfUrl, pageUrl, or auth these properties will not be included in the connect "); - RTMP_LogPrintf("packet.\n\n"); -} - -int -main(int argc, char **argv) -{ - extern char *optarg; - - int nStatus = RD_SUCCESS; - double percent = 0; - double duration = 0.0; - - int nSkipKeyFrames = DEF_SKIPFRM; // skip this number of keyframes when resuming - - int bOverrideBufferTime = FALSE; // if the user specifies a buffer time override this is true - int bStdoutMode = TRUE; // if true print the stream directly to stdout, messages go to stderr - int bResume = FALSE; // true in resume mode - uint32_t dSeek = 0; // seek position in resume mode, 0 otherwise - uint32_t bufferTime = DEF_BUFTIME; - - // meta header and initial frame for the resume mode (they are read from the file and compared with - // the stream we are trying to continue - char *metaHeader = 0; - uint32_t nMetaHeaderSize = 0; - - // video keyframe for matching - char *initialFrame = 0; - uint32_t nInitialFrameSize = 0; - int initialFrameType = 0; // tye: audio or video - - AVal hostname = { 0, 0 }; - AVal playpath = { 0, 0 }; - AVal subscribepath = { 0, 0 }; - AVal usherToken = { 0, 0 }; //Justin.tv auth token - int port = -1; - int protocol = RTMP_PROTOCOL_UNDEFINED; - int retries = 0; - int bLiveStream = FALSE; // is it a live stream? then we can't seek/resume - int bRealtimeStream = FALSE; // If true, disable the BUFX hack (be patient) - int bHashes = FALSE; // display byte counters not hashes by default - - long int timeout = DEF_TIMEOUT; // timeout connection after 120 seconds - uint32_t dStartOffset = 0; // seek position in non-live mode - uint32_t dStopOffset = 0; - RTMP rtmp = { 0 }; - - AVal fullUrl = { 0, 0 }; - AVal swfUrl = { 0, 0 }; - AVal tcUrl = { 0, 0 }; - AVal pageUrl = { 0, 0 }; - AVal app = { 0, 0 }; - AVal auth = { 0, 0 }; - AVal swfHash = { 0, 0 }; - uint32_t swfSize = 0; - AVal flashVer = { 0, 0 }; - AVal sockshost = { 0, 0 }; - -#ifdef CRYPTO - int swfAge = 30; /* 30 days for SWF cache by default */ - int swfVfy = 0; - unsigned char hash[RTMP_SWF_HASHLEN]; -#endif - - char *flvFile = 0; - - signal(SIGINT, sigIntHandler); - signal(SIGTERM, sigIntHandler); -#ifndef WIN32 - signal(SIGHUP, sigIntHandler); - signal(SIGPIPE, sigIntHandler); - signal(SIGQUIT, sigIntHandler); -#endif - - RTMP_debuglevel = RTMP_LOGINFO; - - // Check for --quiet option before printing any output - int index = 0; - while (index < argc) - { - if (strcmp(argv[index], "--quiet") == 0 - || strcmp(argv[index], "-q") == 0) - RTMP_debuglevel = RTMP_LOGCRIT; - index++; - } - - RTMP_LogPrintf("RTMPDump %s\n", RTMPDUMP_VERSION); - RTMP_LogPrintf - ("(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL\n"); - - if (!InitSockets()) - { - RTMP_Log(RTMP_LOGERROR, - "Couldn't load sockets support on your platform, exiting!"); - return RD_FAILED; - } - - /* sleep(30); */ - - RTMP_Init(&rtmp); - - int opt; - struct option longopts[] = { - {"help", 0, NULL, 'h'}, - {"host", 1, NULL, 'n'}, - {"port", 1, NULL, 'c'}, - {"socks", 1, NULL, 'S'}, - {"protocol", 1, NULL, 'l'}, - {"playpath", 1, NULL, 'y'}, - {"playlist", 0, NULL, 'Y'}, - {"url", 1, NULL, 'i'}, - {"rtmp", 1, NULL, 'r'}, - {"swfUrl", 1, NULL, 's'}, - {"tcUrl", 1, NULL, 't'}, - {"pageUrl", 1, NULL, 'p'}, - {"app", 1, NULL, 'a'}, - {"auth", 1, NULL, 'u'}, - {"conn", 1, NULL, 'C'}, -#ifdef CRYPTO - {"swfhash", 1, NULL, 'w'}, - {"swfsize", 1, NULL, 'x'}, - {"swfVfy", 1, NULL, 'W'}, - {"swfAge", 1, NULL, 'X'}, -#endif - {"flashVer", 1, NULL, 'f'}, - {"live", 0, NULL, 'v'}, - {"realtime", 0, NULL, 'R'}, - {"flv", 1, NULL, 'o'}, - {"resume", 0, NULL, 'e'}, - {"timeout", 1, NULL, 'm'}, - {"buffer", 1, NULL, 'b'}, - {"skip", 1, NULL, 'k'}, - {"subscribe", 1, NULL, 'd'}, - {"start", 1, NULL, 'A'}, - {"stop", 1, NULL, 'B'}, - {"token", 1, NULL, 'T'}, - {"hashes", 0, NULL, '#'}, - {"debug", 0, NULL, 'z'}, - {"quiet", 0, NULL, 'q'}, - {"verbose", 0, NULL, 'V'}, - {"jtv", 1, NULL, 'j'}, - {0, 0, 0, 0} - }; - - while ((opt = - getopt_long(argc, argv, - "hVveqzRr:s:t:i:p:a:b:f:o:u:C:n:c:l:y:Ym:k:d:A:B:T:w:x:W:X:S:#j:", - longopts, NULL)) != -1) - { - switch (opt) - { - case 'h': - usage(argv[0]); - return RD_SUCCESS; -#ifdef CRYPTO - case 'w': - { - int res = hex2bin(optarg, &swfHash.av_val); - if (res != RTMP_SWF_HASHLEN) - { - swfHash.av_val = NULL; - RTMP_Log(RTMP_LOGWARNING, - "Couldn't parse swf hash hex string, not hexstring or not %d bytes, ignoring!", RTMP_SWF_HASHLEN); - } - swfHash.av_len = RTMP_SWF_HASHLEN; - break; - } - case 'x': - { - int size = atoi(optarg); - if (size <= 0) - { - RTMP_Log(RTMP_LOGERROR, "SWF Size must be at least 1, ignoring\n"); - } - else - { - swfSize = size; - } - break; - } - case 'W': - STR2AVAL(swfUrl, optarg); - swfVfy = 1; - break; - case 'X': - { - int num = atoi(optarg); - if (num < 0) - { - RTMP_Log(RTMP_LOGERROR, "SWF Age must be non-negative, ignoring\n"); - } - else - { - swfAge = num; - } - } - break; -#endif - case 'k': - nSkipKeyFrames = atoi(optarg); - if (nSkipKeyFrames < 0) - { - RTMP_Log(RTMP_LOGERROR, - "Number of keyframes skipped must be greater or equal zero, using zero!"); - nSkipKeyFrames = 0; - } - else - { - RTMP_Log(RTMP_LOGDEBUG, "Number of skipped key frames for resume: %d", - nSkipKeyFrames); - } - break; - case 'b': - { - int32_t bt = atol(optarg); - if (bt < 0) - { - RTMP_Log(RTMP_LOGERROR, - "Buffer time must be greater than zero, ignoring the specified value %d!", - bt); - } - else - { - bufferTime = bt; - bOverrideBufferTime = TRUE; - } - break; - } - case 'v': - bLiveStream = TRUE; // no seeking or resuming possible! - break; - case 'R': - bRealtimeStream = TRUE; // seeking and resuming is still possible - break; - case 'd': - STR2AVAL(subscribepath, optarg); - break; - case 'n': - STR2AVAL(hostname, optarg); - break; - case 'c': - port = atoi(optarg); - break; - case 'l': - protocol = atoi(optarg); - if (protocol < RTMP_PROTOCOL_RTMP || protocol > RTMP_PROTOCOL_RTMPTS) - { - RTMP_Log(RTMP_LOGERROR, "Unknown protocol specified: %d", protocol); - return RD_FAILED; - } - break; - case 'y': - STR2AVAL(playpath, optarg); - break; - case 'Y': - RTMP_SetOpt(&rtmp, &av_playlist, (AVal *)&av_true); - break; - case 'r': - { - AVal parsedHost, parsedApp, parsedPlaypath; - unsigned int parsedPort = 0; - int parsedProtocol = RTMP_PROTOCOL_UNDEFINED; - - if (!RTMP_ParseURL - (optarg, &parsedProtocol, &parsedHost, &parsedPort, - &parsedPlaypath, &parsedApp)) - { - RTMP_Log(RTMP_LOGWARNING, "Couldn't parse the specified url (%s)!", - optarg); - } - else - { - if (!hostname.av_len) - hostname = parsedHost; - if (port == -1) - port = parsedPort; - if (playpath.av_len == 0 && parsedPlaypath.av_len) - { - playpath = parsedPlaypath; - } - if (protocol == RTMP_PROTOCOL_UNDEFINED) - protocol = parsedProtocol; - if (app.av_len == 0 && parsedApp.av_len) - { - app = parsedApp; - } - } - break; - } - case 'i': - STR2AVAL(fullUrl, optarg); - break; - case 's': - STR2AVAL(swfUrl, optarg); - break; - case 't': - STR2AVAL(tcUrl, optarg); - break; - case 'p': - STR2AVAL(pageUrl, optarg); - break; - case 'a': - STR2AVAL(app, optarg); - break; - case 'f': - STR2AVAL(flashVer, optarg); - break; - case 'o': - flvFile = optarg; - if (strcmp(flvFile, "-")) - bStdoutMode = FALSE; - - break; - case 'e': - bResume = TRUE; - break; - case 'u': - STR2AVAL(auth, optarg); - break; - case 'C': { - AVal av; - STR2AVAL(av, optarg); - if (!RTMP_SetOpt(&rtmp, &av_conn, &av)) - { - RTMP_Log(RTMP_LOGERROR, "Invalid AMF parameter: %s", optarg); - return RD_FAILED; - } - } - break; - case 'm': - timeout = atoi(optarg); - break; - case 'A': - dStartOffset = (int) (atof(optarg) * 1000.0); - break; - case 'B': - dStopOffset = (int) (atof(optarg) * 1000.0); - break; - case 'T': { - AVal token; - STR2AVAL(token, optarg); - RTMP_SetOpt(&rtmp, &av_token, &token); - } - break; - case '#': - bHashes = TRUE; - break; - case 'q': - RTMP_debuglevel = RTMP_LOGCRIT; - break; - case 'V': - RTMP_debuglevel = RTMP_LOGDEBUG; - break; - case 'z': - RTMP_debuglevel = RTMP_LOGALL; - break; - case 'S': - STR2AVAL(sockshost, optarg); - break; - case 'j': - STR2AVAL(usherToken, optarg); - break; - default: - RTMP_LogPrintf("unknown option: %c\n", opt); - usage(argv[0]); - return RD_FAILED; - break; - } - } - - if (!hostname.av_len && !fullUrl.av_len) - { - RTMP_Log(RTMP_LOGERROR, - "You must specify a hostname (--host) or url (-r \"rtmp://host[:port]/playpath\") containing a hostname"); - return RD_FAILED; - } - if (playpath.av_len == 0 && !fullUrl.av_len) - { - RTMP_Log(RTMP_LOGERROR, - "You must specify a playpath (--playpath) or url (-r \"rtmp://host[:port]/playpath\") containing a playpath"); - return RD_FAILED; - } - - if (protocol == RTMP_PROTOCOL_UNDEFINED && !fullUrl.av_len) - { - RTMP_Log(RTMP_LOGWARNING, - "You haven't specified a protocol (--protocol) or rtmp url (-r), using default protocol RTMP"); - protocol = RTMP_PROTOCOL_RTMP; - } - if (port == -1 && !fullUrl.av_len) - { - RTMP_Log(RTMP_LOGWARNING, - "You haven't specified a port (--port) or rtmp url (-r), using default port 1935"); - port = 0; - } - if (port == 0 && !fullUrl.av_len) - { - if (protocol & RTMP_FEATURE_SSL) - port = 443; - else if (protocol & RTMP_FEATURE_HTTP) - port = 80; - else - port = 1935; - } - - if (flvFile == 0) - { - RTMP_Log(RTMP_LOGWARNING, - "You haven't specified an output file (-o filename), using stdout"); - bStdoutMode = TRUE; - } - - if (bStdoutMode && bResume) - { - RTMP_Log(RTMP_LOGWARNING, - "Can't resume in stdout mode, ignoring --resume option"); - bResume = FALSE; - } - - if (bLiveStream && bResume) - { - RTMP_Log(RTMP_LOGWARNING, "Can't resume live stream, ignoring --resume option"); - bResume = FALSE; - } - -#ifdef CRYPTO - if (swfVfy) - { - if (RTMP_HashSWF(swfUrl.av_val, &swfSize, hash, swfAge) == 0) - { - swfHash.av_val = (char *)hash; - swfHash.av_len = RTMP_SWF_HASHLEN; - } - } - - if (swfHash.av_len == 0 && swfSize > 0) - { - RTMP_Log(RTMP_LOGWARNING, - "Ignoring SWF size, supply also the hash with --swfhash"); - swfSize = 0; - } - - if (swfHash.av_len != 0 && swfSize == 0) - { - RTMP_Log(RTMP_LOGWARNING, - "Ignoring SWF hash, supply also the swf size with --swfsize"); - swfHash.av_len = 0; - swfHash.av_val = NULL; - } -#endif - - if (tcUrl.av_len == 0) - { - tcUrl.av_len = strlen(RTMPProtocolStringsLower[protocol]) + - hostname.av_len + app.av_len + sizeof("://:65535/"); - tcUrl.av_val = (char *) malloc(tcUrl.av_len); - if (!tcUrl.av_val) - return RD_FAILED; - tcUrl.av_len = snprintf(tcUrl.av_val, tcUrl.av_len, "%s://%.*s:%d/%.*s", - RTMPProtocolStringsLower[protocol], hostname.av_len, - hostname.av_val, port, app.av_len, app.av_val); - } - - int first = 1; - - // User defined seek offset - if (dStartOffset > 0) - { - // Live stream - if (bLiveStream) - { - RTMP_Log(RTMP_LOGWARNING, - "Can't seek in a live stream, ignoring --start option"); - dStartOffset = 0; - } - } - - if (!fullUrl.av_len) - { - RTMP_SetupStream(&rtmp, protocol, &hostname, port, &sockshost, &playpath, - &tcUrl, &swfUrl, &pageUrl, &app, &auth, &swfHash, swfSize, - &flashVer, &subscribepath, &usherToken, dSeek, dStopOffset, bLiveStream, timeout); - } - else - { - if (RTMP_SetupURL(&rtmp, fullUrl.av_val) == FALSE) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't parse URL: %s", fullUrl.av_val); - return RD_FAILED; - } - } - - /* Try to keep the stream moving if it pauses on us */ - if (!bLiveStream && !bRealtimeStream && !(protocol & RTMP_FEATURE_HTTP)) - rtmp.Link.lFlags |= RTMP_LF_BUFX; - - off_t size = 0; - - // ok, we have to get the timestamp of the last keyframe (only keyframes are seekable) / last audio frame (audio only streams) - if (bResume) - { - nStatus = - OpenResumeFile(flvFile, &file, &size, &metaHeader, &nMetaHeaderSize, - &duration); - if (nStatus == RD_FAILED) - goto clean; - - if (!file) - { - // file does not exist, so go back into normal mode - bResume = FALSE; // we are back in fresh file mode (otherwise finalizing file won't be done) - } - else - { - nStatus = GetLastKeyframe(file, nSkipKeyFrames, - &dSeek, &initialFrame, - &initialFrameType, &nInitialFrameSize); - if (nStatus == RD_FAILED) - { - RTMP_Log(RTMP_LOGDEBUG, "Failed to get last keyframe."); - goto clean; - } - - if (dSeek == 0) - { - RTMP_Log(RTMP_LOGDEBUG, - "Last keyframe is first frame in stream, switching from resume to normal mode!"); - bResume = FALSE; - } - } - } - - if (!file) - { - if (bStdoutMode) - { - file = stdout; - SET_BINMODE(file); - } - else - { - file = fopen(flvFile, "w+b"); - if (file == 0) - { - RTMP_LogPrintf("Failed to open file! %s\n", flvFile); - return RD_FAILED; - } - } - } - -#ifdef _DEBUG - netstackdump = fopen("netstackdump", "wb"); - netstackdump_read = fopen("netstackdump_read", "wb"); -#endif - - while (!RTMP_ctrlC) - { - RTMP_Log(RTMP_LOGDEBUG, "Setting buffer time to: %dms", bufferTime); - RTMP_SetBufferMS(&rtmp, bufferTime); - - if (first) - { - first = 0; - RTMP_LogPrintf("Connecting ...\n"); - - if (!RTMP_Connect(&rtmp, NULL)) - { - nStatus = RD_NO_CONNECT; - break; - } - - RTMP_Log(RTMP_LOGINFO, "Connected..."); - - // User defined seek offset - if (dStartOffset > 0) - { - // Don't need the start offset if resuming an existing file - if (bResume) - { - RTMP_Log(RTMP_LOGWARNING, - "Can't seek a resumed stream, ignoring --start option"); - dStartOffset = 0; - } - else - { - dSeek = dStartOffset; - } - } - - // Calculate the length of the stream to still play - if (dStopOffset > 0) - { - // Quit if start seek is past required stop offset - if (dStopOffset <= dSeek) - { - RTMP_LogPrintf("Already Completed\n"); - nStatus = RD_SUCCESS; - break; - } - } - - if (!RTMP_ConnectStream(&rtmp, dSeek)) - { - nStatus = RD_FAILED; - break; - } - } - else - { - nInitialFrameSize = 0; - - if (retries) - { - RTMP_Log(RTMP_LOGERROR, "Failed to resume the stream\n\n"); - if (!RTMP_IsTimedout(&rtmp)) - nStatus = RD_FAILED; - else - nStatus = RD_INCOMPLETE; - break; - } - RTMP_Log(RTMP_LOGINFO, "Connection timed out, trying to resume.\n\n"); - /* Did we already try pausing, and it still didn't work? */ - if (rtmp.m_pausing == 3) - { - /* Only one try at reconnecting... */ - retries = 1; - dSeek = rtmp.m_pauseStamp; - if (dStopOffset > 0) - { - if (dStopOffset <= dSeek) - { - RTMP_LogPrintf("Already Completed\n"); - nStatus = RD_SUCCESS; - break; - } - } - if (!RTMP_ReconnectStream(&rtmp, dSeek)) - { - RTMP_Log(RTMP_LOGERROR, "Failed to resume the stream\n\n"); - if (!RTMP_IsTimedout(&rtmp)) - nStatus = RD_FAILED; - else - nStatus = RD_INCOMPLETE; - break; - } - } - else if (!RTMP_ToggleStream(&rtmp)) - { - RTMP_Log(RTMP_LOGERROR, "Failed to resume the stream\n\n"); - if (!RTMP_IsTimedout(&rtmp)) - nStatus = RD_FAILED; - else - nStatus = RD_INCOMPLETE; - break; - } - bResume = TRUE; - } - - nStatus = Download(&rtmp, file, dSeek, dStopOffset, duration, bResume, - metaHeader, nMetaHeaderSize, initialFrame, - initialFrameType, nInitialFrameSize, nSkipKeyFrames, - bStdoutMode, bLiveStream, bRealtimeStream, bHashes, - bOverrideBufferTime, bufferTime, &percent); - free(initialFrame); - initialFrame = NULL; - - /* If we succeeded, we're done. - */ - if (nStatus != RD_INCOMPLETE || !RTMP_IsTimedout(&rtmp) || bLiveStream) - break; - } - - if (nStatus == RD_SUCCESS) - { - RTMP_LogPrintf("Download complete\n"); - } - else if (nStatus == RD_INCOMPLETE) - { - RTMP_LogPrintf - ("Download may be incomplete (downloaded about %.2f%%), try resuming\n", - percent); - } - -clean: - RTMP_Log(RTMP_LOGDEBUG, "Closing connection.\n"); - RTMP_Close(&rtmp); - - if (file != 0) - fclose(file); - - CleanupSockets(); - -#ifdef _DEBUG - if (netstackdump != 0) - fclose(netstackdump); - if (netstackdump_read != 0) - fclose(netstackdump_read); -#endif - return nStatus; -} diff --git a/rtmp/rtmp_c/rtmpgw.8 b/rtmp/rtmp_c/rtmpgw.8 deleted file mode 100644 index 0a231b41..00000000 --- a/rtmp/rtmp_c/rtmpgw.8 +++ /dev/null @@ -1,289 +0,0 @@ -.TH RTMPGW 8 "2011-07-20" "RTMPDump v2.4" -.\" Copyright 2011 Howard Chu. -.\" Copying permitted according to the GNU General Public License V2. -.SH NAME -rtmpgw \- RTMP streaming media gateway -.SH SYNOPSIS -.B rtmpgw -[\c -.BI \-r \ url\fR] -[\c -.BI \-n \ hostname\fR] -[\c -.BI \-c \ port\fR] -[\c -.BI \-l \ protocol\fR] -[\c -.BI \-S \ host:port\fR] -[\c -.BI \-a \ app\fR] -[\c -.BI \-t \ tcUrl\fR] -[\c -.BI \-p \ pageUrl\fR] -[\c -.BI \-s \ swfUrl\fR] -[\c -.BI \-f \ flashVer\fR] -[\c -.BI \-u \ auth\fR] -[\c -.BI \-C \ conndata\fR] -[\c -.BI \-y \ playpath\fR] -[\c -.BR \-v ] -[\c -.BI \-d \ subscription\fR] -[\c -.BR \-e ] -[\c -.BI \-k \ skip\fR] -[\c -.BI \-A \ start\fR] -[\c -.BI \-B \ stop\fR] -[\c -.BI \-b \ buffer\fR] -[\c -.BI \-m \ timeout\fR] -[\c -.BI \-T \ key\fR] -[\c -.BI \-j \ JSON\fR] -[\c -.BI \-w \ swfHash\fR] -[\c -.BI \-x \ swfSize\fR] -[\c -.BI \-W \ swfUrl\fR] -[\c -.BI \-X \ swfAge\fR] -[\c -.BI \-D \ address\fR] -[\c -.BI \-g \ port\fR] -[\c -.BR \-q ] -[\c -.BR \-V ] -[\c -.BR \-z ] -.br -.B rtmpgw \-h -.SH DESCRIPTION -.B rtmpgw -is a server for streaming media content from RTMP out to HTTP. -.LP -.B rtmpgw -listens for HTTP requests that specify RTMP stream parameters and -then returns the RTMP data in the HTTP response. The only valid -HTTP request is "GET /" but additional options can be provided -in URL-encoded fashion. Options specified on the command line will -be used as defaults, which can be overridden by options in the HTTP -request. -.SH OPTIONS -.SS "Network Parameters" -These options define how to connect to the media server. -.TP -\fB\-\-rtmp \-r\fP\ \fIurl\fP -URL of the server and media content. -.TP -\fB\-\-host \-n\fP\ \fIhostname\fP -Overrides the hostname in the RTMP URL. -.TP -\fB\-\-port \-c\fP\ \fIport\fP -Overrides the port number in the RTMP URL. -.TP -\fB\-\-protocol \-l\fP\ \fInumber\fP -Overrides the protocol in the RTMP URL. -.nf - 0 = rtmp - 1 = rtmpt - 2 = rtmpe - 3 = rtmpte - 4 = rtmps - 5 = rtmpts -.fi -.TP -\fB\-\-socks \-S\fP\ \fIhost:port\fP -Use the specified SOCKS4 proxy. -.SS "Connection Parameters" -These options define the content of the RTMP Connect request packet. -If correct values are not provided, the media server will reject the -connection attempt. -.TP -\fB\-\-app \-a\fP\ \fIapp\fP -Name of application to connect to on the RTMP server. Overrides -the app in the RTMP URL. Sometimes the rtmpdump URL parser cannot -determine the app name automatically, so it must be given explicitly -using this option. -.TP -\fB\-\-tcUrl \-t\fP\ \fIurl\fP -URL of the target stream. Defaults to rtmp[e]://host[:port]/app/playpath. -.TP -\fB\-\-pageUrl \-p\fP\ \fIurl\fP -URL of the web page in which the media was embedded. By default no -value will be sent. -.TP -\fB\-\-swfUrl \-s\fP\ \fIurl\fP -URL of the SWF player for the media. By default no value will be sent. -.TP -\fB\-\-flashVer \-f\fP\ \fIversion\fP -Version of the Flash plugin used to run the SWF player. The -default is "LNX 10,0,32,18". -.TP -\fB\-\-auth \-u\fP\ \fIstring\fP -An authentication string to be appended to the Connect message. Using -this option will append a Boolean TRUE and then the specified string. -This option is only used by some particular servers and is -deprecated. The more general -.B \-\-conn -option should be used instead. -.TP -\fB\-\-conn \-C\fP\ \fItype:data\fP -Append arbitrary AMF data to the Connect message. The type -must be B for Boolean, N for number, S for string, O for object, or Z -for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, -respectively. Likewise for Objects the data must be 0 or 1 to end or -begin an object, respectively. Data items in subobjects may be named, by -prefixing the type with 'N' and specifying the name before the value, e.g. -NB:myFlag:1. This option may be used multiple times to construct arbitrary -AMF sequences. E.g. -.nf - \-C B:1 \-C S:authMe \-C O:1 \-C NN:code:1.23 \-C NS:flag:ok \-C O:0 -.fi -.SS "Session Parameters" -These options take effect after the Connect request has succeeded. -.TP -\fB\-\-playpath \-y\fP\ \fIpath\fP -Overrides the playpath parsed from the RTMP URL. Sometimes the -rtmpdump URL parser cannot determine the correct playpath -automatically, so it must be given explicitly using this option. -.TP -.B \-\-live \-v -Specify that the media is a live stream. No resuming or seeking in -live streams is possible. -.TP -\fB\-\-subscribe \-d\fP\ \fIstream\fP -Name of live stream to subscribe to. Defaults to -.IR playpath . -.TP -\fB\-\-start \-A\fP\ \fInum\fP -Start at -.I num -seconds into the stream. Not valid for live streams. -.TP -\fB\-\-stop \-B\fP\ \fInum\fP -Stop at -.I num -seconds into the stream. -.TP -\fB\-\-buffer \-b\fP\ \fInum\fP -Set buffer time to -.I num -milliseconds. The default is 20000. -.TP -\fB\-\-timeout \-m\fP\ \fInum\fP -Timeout the session after -.I num -seconds without receiving any data from the server. The default is 120. -.SS "Security Parameters" -These options handle additional authentication requests from the server. -.TP -\fB\-\-token \-T\fP\ \fIkey\fP -Key for SecureToken response, used if the server requires SecureToken -authentication. -.TP -\fB\-\-jtv \-j\fP\ \fIJSON\fP -JSON token used by legacy Justin.tv servers. Invokes NetStream.Authenticate.UsherToken -.TP -\fB\-\-swfhash \-w\fP\ \fIhexstring\fP -SHA256 hash of the decompressed SWF file. This option may be needed if -the server uses SWF Verification, but see the -.B \-\-swfVfy -option below. The hash is 32 bytes, and must be -given in hexadecimal. The -.B \-\-swfsize -option must always be used with this option. -.TP -\fB\-\-swfsize \-x\fP\ \fInum\fP -Size of the decompressed SWF file. This option may be needed if the -server uses SWF Verification, but see the -.B \-\-swfVfy -option below. The -.B \-\-swfhash -option must always be used with this option. -.TP -\fB\-\-swfVfy \-W\fP\ \fIurl\fP -URL of the SWF player for this media. This option replaces all three -of the -.BR \-\-swfUrl , -.BR \-\-swfhash , -and -.B \-\-swfsize -options. When this option is used, the SWF player is retrieved from the -specified URL and the hash and size are computed automatically. Also -the information is cached in a -.I .swfinfo -file in the user's home directory, so that it doesn't need to be retrieved -and recalculated every time rtmpdump is run. The .swfinfo file records -the URL, the time it was fetched, the modification timestamp of the SWF -file, its size, and its hash. By default, the cached info will be used -for 30 days before re-checking. -.TP -\fB\-\-swfAge \-X\fP\ \fIdays\fP -Specify how many days to use the cached SWF info before re-checking. Use -0 to always check the SWF URL. Note that if the check shows that the -SWF file has the same modification timestamp as before, it will not be -retrieved again. -.SS Miscellaneous -.TP -\fB\-\-device \-D\fP\ \fIaddress\fP -Listener IP address. The default is 0.0.0.0, i.e., any IP address. -.TP -\fB\-\-sport \-g\fP\ \fIport\fP -Listener port. The default is 80. -.TP -.B \-\-quiet \-q -Suppress all command output. -.TP -.B \-\-verbose \-V -Verbose command output. -.TP -.B \-\-debug \-z -Debug level output. Extremely verbose, including hex dumps of all packet data. -.TP -.B \-\-help \-h -Print a summary of command options. -.SH EXAMPLES -The HTTP request -.nf - GET /?r=rtmp:%2f%2fserver%2fmyapp&y=somefile HTTP/1.0 -.fi -is equivalent to the -.BR rtrmpdump (1) -invocation -.nf - rtmpdump \-r rtmp://server/myapp \-y somefile -.fi - -Note that only the shortform (single letter) options are supported. -.SH ENVIRONMENT -.TP -.B HOME -The value of -.RB $ HOME -is used as the location for the -.I .swfinfo -file. -.SH FILES -.TP -.I $HOME/.swfinfo -Cache of SWF Verification information -.SH "SEE ALSO" -.BR rtmpdump (1) -.SH AUTHORS -Andrej Stepanchuk, Howard Chu, The Flvstreamer Team -.br - diff --git a/rtmp/rtmp_c/rtmpgw.8.html b/rtmp/rtmp_c/rtmpgw.8.html deleted file mode 100644 index 68d6734a..00000000 --- a/rtmp/rtmp_c/rtmpgw.8.html +++ /dev/null @@ -1,395 +0,0 @@ - - -RTMPGW(8): - - - - - -
RTMPGW(8)RTMPGW(8) -
RTMPDump v2.42011-07-20RTMPGW(8) -


    - -
- -

NAME

    -rtmpgw − RTMP streaming media gateway -
- -

SYNOPSIS

    -rtmpgw -[−r url] -[−n hostname] -[−c port] -[−l protocol] -[−S host:port] -[−a app] -[−t tcUrl] -[−p pageUrl] -[−s swfUrl] -[−f flashVer] -[−u auth] -[−C conndata] -[−y playpath] -[−v] -[−d subscription] -[−e] -[−k skip] -[−A start] -[−B stop] -[−b buffer] -[−m timeout] -[−T key] -[−j JSON] -[−w swfHash] -[−x swfSize] -[−W swfUrl] -[−X swfAge] -[−D address] -[−g port] -[−q] -[−V] -[−z] -
    -rtmpgw −h -
- -

DESCRIPTION

    -rtmpgw -is a server for streaming media content from RTMP out to HTTP. -

    -rtmpgw -listens for HTTP requests that specify RTMP stream parameters and -then returns the RTMP data in the HTTP response. The only valid -HTTP request is "GET /" but additional options can be provided -in URL-encoded fashion. Options specified on the command line will -be used as defaults, which can be overridden by options in the HTTP -request. -

- -

OPTIONS

    -
- -

Network Parameters

    -These options define how to connect to the media server. -

    -

    -−−rtmp −r url -
    -URL of the server and media content. -
    -

    -

    -−−host −n hostname -
    -Overrides the hostname in the RTMP URL. -
    -

    -

    -−−port −c port -
    -Overrides the port number in the RTMP URL. -
    -

    -

    -−−protocol −l number -
    -Overrides the protocol in the RTMP URL. -
    -  0 = rtmp
    -  1 = rtmpt
    -  2 = rtmpe
    -  3 = rtmpte
    -  4 = rtmps
    -  5 = rtmpts
    -
    -
    -

    -

    -−−socks −S host:port -
    -Use the specified SOCKS4 proxy. -
    -
- -

Connection Parameters

    -These options define the content of the RTMP Connect request packet. -If correct values are not provided, the media server will reject the -connection attempt. -

    -

    -−−app −a app -
    -Name of application to connect to on the RTMP server. Overrides -the app in the RTMP URL. Sometimes the rtmpdump URL parser cannot -determine the app name automatically, so it must be given explicitly -using this option. -
    -

    -

    -−−tcUrl −t url -
    -URL of the target stream. Defaults to rtmp[e]://host[:port]/app/playpath. -
    -

    -

    -−−pageUrl −p url -
    -URL of the web page in which the media was embedded. By default no -value will be sent. -
    -

    -

    -−−swfUrl −s url -
    -URL of the SWF player for the media. By default no value will be sent. -
    -

    -

    -−−flashVer −f version -
    -Version of the Flash plugin used to run the SWF player. The -default is "LNX 10,0,32,18". -
    -

    -

    -−−auth −u string -
    -An authentication string to be appended to the Connect message. Using -this option will append a Boolean TRUE and then the specified string. -This option is only used by some particular servers and is -deprecated. The more general -−−conn -option should be used instead. -
    -

    -

    -−−conn −C type:data -
    -Append arbitrary AMF data to the Connect message. The type -must be B for Boolean, N for number, S for string, O for object, or Z -for null. For Booleans the data must be either 0 or 1 for FALSE or TRUE, -respectively. Likewise for Objects the data must be 0 or 1 to end or -begin an object, respectively. Data items in subobjects may be named, by -prefixing the type with 'N' and specifying the name before the value, e.g. -NB:myFlag:1. This option may be used multiple times to construct arbitrary -AMF sequences. E.g. -
    -  −C B:1 −C S:authMe −C O:1 −C NN:code:1.23 −C NS:flag:ok −C O:0
    -
    -
    -
- -

Session Parameters

    -These options take effect after the Connect request has succeeded. -

    -

    -−−playpath −y path -
    -Overrides the playpath parsed from the RTMP URL. Sometimes the -rtmpdump URL parser cannot determine the correct playpath -automatically, so it must be given explicitly using this option. -
    -

    -

    -−−live −v -
    -Specify that the media is a live stream. No resuming or seeking in -live streams is possible. -
    -

    -

    -−−subscribe −d stream -
    -Name of live stream to subscribe to. Defaults to -playpath. -
    -

    -

    -−−start −A num -
    -Start at -num -seconds into the stream. Not valid for live streams. -
    -

    -

    -−−stop −B num -
    -Stop at -num -seconds into the stream. -
    -

    -

    -−−buffer −b num -
    -Set buffer time to -num -milliseconds. The default is 20000. -
    -

    -

    -−−timeout −m num -
    -Timeout the session after -num -seconds without receiving any data from the server. The default is 120. -
    -
- -

Security Parameters

    -These options handle additional authentication requests from the server. -

    -

    -−−token −T key -
    -Key for SecureToken response, used if the server requires SecureToken -authentication. -
    -

    -

    -−−jtv −j JSON -
    -JSON token used by legacy Justin.tv servers. Invokes NetStream.Authenticate.UsherToken -
    -

    -

    -−−swfhash −w hexstring -
    -SHA256 hash of the decompressed SWF file. This option may be needed if -the server uses SWF Verification, but see the -−−swfVfy -option below. The hash is 32 bytes, and must be -given in hexadecimal. The -−−swfsize -option must always be used with this option. -
    -

    -

    -−−swfsize −x num -
    -Size of the decompressed SWF file. This option may be needed if the -server uses SWF Verification, but see the -−−swfVfy -option below. The -−−swfhash -option must always be used with this option. -
    -

    -

    -−−swfVfy −W url -
    -URL of the SWF player for this media. This option replaces all three -of the -−−swfUrl, -−−swfhash, -and -−−swfsize -options. When this option is used, the SWF player is retrieved from the -specified URL and the hash and size are computed automatically. Also -the information is cached in a -.swfinfo -file in the user's home directory, so that it doesn't need to be retrieved -and recalculated every time rtmpdump is run. The .swfinfo file records -the URL, the time it was fetched, the modification timestamp of the SWF -file, its size, and its hash. By default, the cached info will be used -for 30 days before re-checking. -
    -

    -

    -−−swfAge −X days -
    -Specify how many days to use the cached SWF info before re-checking. Use -0 to always check the SWF URL. Note that if the check shows that the -SWF file has the same modification timestamp as before, it will not be -retrieved again. -
    -
- -

Miscellaneous

    -

    -

    -−−device −D address -
    -Listener IP address. The default is 0.0.0.0, i.e., any IP address. -
    -

    -

    -−−sport −g port -
    -Listener port. The default is 80. -
    -

    -

    -−−quiet −q -
    -Suppress all command output. -
    -

    -

    -−−verbose −V -
    -Verbose command output. -
    -

    -

    -−−debug −z -
    -Debug level output. Extremely verbose, including hex dumps of all packet data. -
    -

    -

    -−−help −h -
    -Print a summary of command options. -
    -
- -

EXAMPLES

    -The HTTP request -
    -	GET /?r=rtmp:%2f%2fserver%2fmyapp&y=somefile HTTP/1.0
    -
    -is equivalent to the -rtrmpdump(1) -invocation -
    -	rtmpdump −r rtmp://server/myapp −y somefile
    -
    -

    -Note that only the shortform (single letter) options are supported. -

- -

ENVIRONMENT

    -

    -

    -HOME -
    -The value of -$HOME -is used as the location for the -.swfinfo -file. -
    -
- -

FILES

    -

    -

    -$HOME/.swfinfo -
    -Cache of SWF Verification information -
    -
- -

SEE ALSO

- -

AUTHORS

diff --git a/rtmp/rtmp_c/rtmpgw.c b/rtmp/rtmp_c/rtmpgw.c deleted file mode 100644 index 3e47602f..00000000 --- a/rtmp/rtmp_c/rtmpgw.c +++ /dev/null @@ -1,1211 +0,0 @@ -/* HTTP-RTMP Stream Gateway - * Copyright (C) 2009 Andrej Stepanchuk - * Copyright (C) 2009-2010 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, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - */ - -#include -#include -#include - -#include -#include - -#include - -#include "librtmp/rtmp_sys.h" -#include "librtmp/log.h" - -#include "thread.h" - -#define RD_SUCCESS 0 -#define RD_FAILED 1 -#define RD_INCOMPLETE 2 - -#define PACKET_SIZE 1024*1024 - -#ifdef WIN32 -#define InitSockets() {\ - WORD version; \ - WSADATA wsaData; \ - \ - version = MAKEWORD(1,1); \ - WSAStartup(version, &wsaData); } - -#define CleanupSockets() WSACleanup() -#else -#define InitSockets() -#define CleanupSockets() -#endif - -enum -{ - STREAMING_ACCEPTING, - STREAMING_IN_PROGRESS, - STREAMING_STOPPING, - STREAMING_STOPPED -}; - -typedef struct -{ - int socket; - int state; - -} STREAMING_SERVER; - -STREAMING_SERVER *httpServer = 0; // server structure pointer - -STREAMING_SERVER *startStreaming(const char *address, int port); -void stopStreaming(STREAMING_SERVER * server); - -typedef struct -{ - AVal hostname; - int rtmpport; - int protocol; - int bLiveStream; // is it a live stream? then we can't seek/resume - - long int timeout; // timeout connection after 120 seconds - uint32_t bufferTime; - - char *rtmpurl; - AVal fullUrl; - AVal playpath; - AVal swfUrl; - AVal tcUrl; - AVal pageUrl; - AVal app; - AVal auth; - AVal swfHash; - AVal flashVer; - AVal token; - AVal subscribepath; - AVal usherToken; //Justin.tv auth token - AVal sockshost; - AMFObject extras; - int edepth; - uint32_t swfSize; - int swfAge; - int swfVfy; - - uint32_t dStartOffset; - uint32_t dStopOffset; - -#ifdef CRYPTO - unsigned char hash[RTMP_SWF_HASHLEN]; -#endif -} RTMP_REQUEST; - -#define STR2AVAL(av,str) av.av_val = str; av.av_len = strlen(av.av_val) - -int -parseAMF(AMFObject *obj, const char *arg, int *depth) -{ - AMFObjectProperty prop = {{0,0}}; - int i; - char *p; - - if (arg[1] == ':') - { - p = (char *)arg+2; - switch(arg[0]) - { - case 'B': - prop.p_type = AMF_BOOLEAN; - prop.p_vu.p_number = atoi(p); - break; - case 'S': - prop.p_type = AMF_STRING; - STR2AVAL(prop.p_vu.p_aval,p); - break; - case 'N': - prop.p_type = AMF_NUMBER; - prop.p_vu.p_number = strtod(p, NULL); - break; - case 'Z': - prop.p_type = AMF_NULL; - break; - case 'O': - i = atoi(p); - if (i) - { - prop.p_type = AMF_OBJECT; - } - else - { - (*depth)--; - return 0; - } - break; - default: - return -1; - } - } - else if (arg[2] == ':' && arg[0] == 'N') - { - p = strchr(arg+3, ':'); - if (!p || !*depth) - return -1; - prop.p_name.av_val = (char *)arg+3; - prop.p_name.av_len = p - (arg+3); - - p++; - switch(arg[1]) - { - case 'B': - prop.p_type = AMF_BOOLEAN; - prop.p_vu.p_number = atoi(p); - break; - case 'S': - prop.p_type = AMF_STRING; - STR2AVAL(prop.p_vu.p_aval,p); - break; - case 'N': - prop.p_type = AMF_NUMBER; - prop.p_vu.p_number = strtod(p, NULL); - break; - case 'O': - prop.p_type = AMF_OBJECT; - break; - default: - return -1; - } - } - else - return -1; - - if (*depth) - { - AMFObject *o2; - for (i=0; i<*depth; i++) - { - o2 = &obj->o_props[obj->o_num-1].p_vu.p_object; - obj = o2; - } - } - AMF_AddProp(obj, &prop); - if (prop.p_type == AMF_OBJECT) - (*depth)++; - return 0; -} - -/* this request is formed from the parameters and used to initialize a new request, - * thus it is a default settings list. All settings can be overriden by specifying the - * parameters in the GET request. */ -RTMP_REQUEST defaultRTMPRequest; - -int ParseOption(char opt, char *arg, RTMP_REQUEST * req); - -#ifdef _DEBUG -uint32_t debugTS = 0; - -int pnum = 0; - -FILE *netstackdump = NULL; -FILE *netstackdump_read = NULL; -#endif - -/* inplace http unescape. This is possible .. strlen(unescaped_string) <= strlen(esacped_string) */ -void -http_unescape(char *data) -{ - char hex[3]; - char *stp; - int src_x = 0; - int dst_x = 0; - - int length = (int) strlen(data); - hex[2] = 0; - - while (src_x < length) - { - if (strncmp(data + src_x, "%", 1) == 0 && src_x + 2 < length) - { - // - // Since we encountered a '%' we know this is an escaped character - // - hex[0] = data[src_x + 1]; - hex[1] = data[src_x + 2]; - data[dst_x] = (char) strtol(hex, &stp, 16); - dst_x += 1; - src_x += 3; - } - else if (src_x != dst_x) - { - // - // This doesn't need to be unescaped. If we didn't unescape anything previously - // there is no need to copy the string either - // - data[dst_x] = data[src_x]; - src_x += 1; - dst_x += 1; - } - else - { - // - // This doesn't need to be unescaped, however we need to copy the string - // - src_x += 1; - dst_x += 1; - } - } - data[dst_x] = '\0'; -} - -TFTYPE -controlServerThread(void *unused) -{ - char ich; - while (1) - { - ich = getchar(); - switch (ich) - { - case 'q': - RTMP_LogPrintf("Exiting\n"); - stopStreaming(httpServer); - exit(0); - break; - default: - RTMP_LogPrintf("Unknown command \'%c\', ignoring\n", ich); - } - } - TFRET(); -} - -/* -ssize_t readHTTPLine(int sockfd, char *buffer, size_t length) -{ - size_t i=0; - - while(i < length-1) { - char c; - int n = read(sockfd, &c, 1); - - if(n == 0) - break; - - buffer[i] = c; - i++; - - if(c == '\n') - break; - } - buffer[i]='\0'; - i++; - - return i; -} - -int isHTTPRequestEOF(char *line, size_t length) -{ - if(length < 2) - return TRUE; - - if(line[0]=='\r' && line[1]=='\n') - return TRUE; - - return FALSE; -} -*/ - -void processTCPrequest(STREAMING_SERVER * server, // server socket and state (our listening socket) - int sockfd // client connection socket - ) -{ - char buf[512] = { 0 }; // answer buffer - char header[2048] = { 0 }; // request header - char *filename = NULL; // GET request: file name //512 not enuf - char *buffer = NULL; // stream buffer - char *ptr = NULL; // header pointer - int len; - - size_t nRead = 0; - - char srvhead[] = "\r\nServer: HTTP-RTMP Stream Server " RTMPDUMP_VERSION "\r\n"; - - char *status = "404 Not Found"; - - server->state = STREAMING_IN_PROGRESS; - - RTMP rtmp = { 0 }; - uint32_t dSeek = 0; // can be used to start from a later point in the stream - - // reset RTMP options to defaults specified upon invokation of streams - RTMP_REQUEST req; - memcpy(&req, &defaultRTMPRequest, sizeof(RTMP_REQUEST)); - - // timeout for http requests - fd_set fds; - struct timeval tv; - - memset(&tv, 0, sizeof(struct timeval)); - tv.tv_sec = 5; - - // go through request lines - //do { - FD_ZERO(&fds); - FD_SET(sockfd, &fds); - - if (select(sockfd + 1, &fds, NULL, NULL, &tv) <= 0) - { - RTMP_Log(RTMP_LOGERROR, "Request timeout/select failed, ignoring request"); - goto quit; - } - else - { - nRead = recv(sockfd, header, 2047, 0); - header[2047] = '\0'; - - RTMP_Log(RTMP_LOGDEBUG, "%s: header: %s", __FUNCTION__, header); - - if (strstr(header, "Range: bytes=") != 0) - { - // TODO check range starts from 0 and asking till the end. - RTMP_LogPrintf("%s, Range request not supported\n", __FUNCTION__); - len = sprintf(buf, "HTTP/1.0 416 Requested Range Not Satisfiable%s\r\n", - srvhead); - send(sockfd, buf, len, 0); - goto quit; - } - - if (strncmp(header, "GET", 3) == 0 && nRead > 4) - { - filename = header + 4; - - // filter " HTTP/..." from end of request - char *p = filename; - while (*p != '\0') - { - if (*p == ' ') - { - *p = '\0'; - break; - } - p++; - } - } - } - //} while(!isHTTPRequestEOF(header, nRead)); - - // if we got a filename from the GET method - if (filename != NULL) - { - RTMP_Log(RTMP_LOGDEBUG, "%s: Request header: %s", __FUNCTION__, filename); - if (filename[0] == '/') - { // if its not empty, is it /? - ptr = filename + 1; - - // parse parameters - if (*ptr == '?') - { - ptr++; - int len = strlen(ptr); - - while (len >= 2) - { - char ich = *ptr; - ptr++; - if (*ptr != '=') - goto filenotfound; // long parameters not (yet) supported - - ptr++; - len -= 2; - - // get position of the next '&' - char *temp; - - unsigned int nArgLen = len; - if ((temp = strstr(ptr, "&")) != 0) - { - nArgLen = temp - ptr; - } - - char *arg = (char *) malloc((nArgLen + 1) * sizeof(char)); - memcpy(arg, ptr, nArgLen * sizeof(char)); - arg[nArgLen] = '\0'; - - //RTMP_Log(RTMP_LOGDEBUG, "%s: unescaping parameter: %s", __FUNCTION__, arg); - http_unescape(arg); - - RTMP_Log(RTMP_LOGDEBUG, "%s: parameter: %c, arg: %s", __FUNCTION__, - ich, arg); - - ptr += nArgLen + 1; - len -= nArgLen + 1; - - if (!ParseOption(ich, arg, &req)) - { - status = "400 unknown option"; - goto filenotfound; - } - } - } - } - else - { - goto filenotfound; - } - } - else - { - RTMP_LogPrintf("%s: No request header received/unsupported method\n", - __FUNCTION__); - } - - // do necessary checks right here to make sure the combined request of default values and GET parameters is correct - if (!req.hostname.av_len && !req.fullUrl.av_len) - { - RTMP_Log(RTMP_LOGERROR, - "You must specify a hostname (--host) or url (-r \"rtmp://host[:port]/playpath\") containing a hostname"); - status = "400 Missing Hostname"; - goto filenotfound; - } - if (req.playpath.av_len == 0 && !req.fullUrl.av_len) - { - RTMP_Log(RTMP_LOGERROR, - "You must specify a playpath (--playpath) or url (-r \"rtmp://host[:port]/playpath\") containing a playpath"); - status = "400 Missing Playpath"; - goto filenotfound;; - } - - if (req.protocol == RTMP_PROTOCOL_UNDEFINED && !req.fullUrl.av_len) - { - RTMP_Log(RTMP_LOGWARNING, - "You haven't specified a protocol (--protocol) or rtmp url (-r), using default protocol RTMP"); - req.protocol = RTMP_PROTOCOL_RTMP; - } - if (req.rtmpport == -1 && !req.fullUrl.av_len) - { - RTMP_Log(RTMP_LOGWARNING, - "You haven't specified a port (--port) or rtmp url (-r), using default port"); - req.rtmpport = 0; - } - if (req.rtmpport == 0 && !req.fullUrl.av_len) - { - if (req.protocol & RTMP_FEATURE_SSL) - req.rtmpport = 443; - else if (req.protocol & RTMP_FEATURE_HTTP) - req.rtmpport = 80; - else - req.rtmpport = 1935; - } - - if (req.tcUrl.av_len == 0) - { - char str[512] = { 0 }; - req.tcUrl.av_len = snprintf(str, 511, "%s://%.*s:%d/%.*s", - RTMPProtocolStringsLower[req.protocol], req.hostname.av_len, - req.hostname.av_val, req.rtmpport, req.app.av_len, req.app.av_val); - req.tcUrl.av_val = (char *) malloc(req.tcUrl.av_len + 1); - strcpy(req.tcUrl.av_val, str); - } - - if (req.swfVfy) - { -#ifdef CRYPTO - if (RTMP_HashSWF(req.swfUrl.av_val, &req.swfSize, req.hash, req.swfAge) == 0) - { - req.swfHash.av_val = (char *)req.hash; - req.swfHash.av_len = RTMP_SWF_HASHLEN; - } -#endif - } - - // after validation of the http request send response header - len = sprintf(buf, "HTTP/1.0 200 OK%sContent-Type: video/flv\r\n\r\n", srvhead); - send(sockfd, buf, len, 0); - - // send the packets - buffer = (char *) calloc(PACKET_SIZE, 1); - - // User defined seek offset - if (req.dStartOffset > 0) - { - if (req.bLiveStream) - RTMP_Log(RTMP_LOGWARNING, - "Can't seek in a live stream, ignoring --seek option"); - else - dSeek += req.dStartOffset; - } - - if (dSeek != 0) - { - RTMP_LogPrintf("Starting at TS: %d ms\n", dSeek); - } - - RTMP_Log(RTMP_LOGDEBUG, "Setting buffer time to: %dms", req.bufferTime); - RTMP_Init(&rtmp); - RTMP_SetBufferMS(&rtmp, req.bufferTime); - if (!req.fullUrl.av_len) - { - RTMP_SetupStream(&rtmp, req.protocol, &req.hostname, req.rtmpport, &req.sockshost, - &req.playpath, &req.tcUrl, &req.swfUrl, &req.pageUrl, &req.app, &req.auth, &req.swfHash, req.swfSize, &req.flashVer, &req.subscribepath, &req.usherToken, dSeek, req.dStopOffset, - req.bLiveStream, req.timeout); - } - else - { - if (RTMP_SetupURL(&rtmp, req.fullUrl.av_val) == FALSE) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't parse URL: %s", req.fullUrl.av_val); - return; - } - } - /* backward compatibility, we always sent this as true before */ - if (req.auth.av_len) - rtmp.Link.lFlags |= RTMP_LF_AUTH; - - rtmp.Link.extras = req.extras; - rtmp.Link.token = req.token; - rtmp.m_read.timestamp = dSeek; - - RTMP_LogPrintf("Connecting ... port: %d, app: %s\n", req.rtmpport, req.app.av_val); - if (!RTMP_Connect(&rtmp, NULL)) - { - RTMP_LogPrintf("%s, failed to connect!\n", __FUNCTION__); - } - else - { - unsigned long size = 0; - double percent = 0; - double duration = 0.0; - - int nWritten = 0; - int nRead = 0; - - do - { - nRead = RTMP_Read(&rtmp, buffer, PACKET_SIZE); - - if (nRead > 0) - { - if ((nWritten = send(sockfd, buffer, nRead, 0)) < 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, sending failed, error: %d", __FUNCTION__, - GetSockError()); - goto cleanup; // we are in STREAMING_IN_PROGRESS, so we'll go to STREAMING_ACCEPTING - } - - size += nRead; - - //RTMP_LogPrintf("write %dbytes (%.1f KB)\n", nRead, nRead/1024.0); - if (duration <= 0) // if duration unknown try to get it from the stream (onMetaData) - duration = RTMP_GetDuration(&rtmp); - - if (duration > 0) - { - percent = - ((double) (dSeek + rtmp.m_read.timestamp)) / (duration * - 1000.0) * 100.0; - percent = ((double) (int) (percent * 10.0)) / 10.0; - RTMP_LogStatus("\r%.3f KB / %.2f sec (%.1f%%)", - (double) size / 1024.0, - (double) (rtmp.m_read.timestamp) / 1000.0, percent); - } - else - { - RTMP_LogStatus("\r%.3f KB / %.2f sec", (double) size / 1024.0, - (double) (rtmp.m_read.timestamp) / 1000.0); - } - } -#ifdef _DEBUG - else - { - RTMP_Log(RTMP_LOGDEBUG, "zero read!"); - } -#endif - } - while (server->state == STREAMING_IN_PROGRESS && nRead > -1 - && RTMP_IsConnected(&rtmp) && nWritten >= 0); - } -cleanup: - RTMP_LogPrintf("Closing connection... "); - RTMP_Close(&rtmp); - RTMP_LogPrintf("done!\n\n"); - -quit: - if (buffer) - { - free(buffer); - buffer = NULL; - } - - if (sockfd) - closesocket(sockfd); - - if (server->state == STREAMING_IN_PROGRESS) - server->state = STREAMING_ACCEPTING; - - return; - -filenotfound: - RTMP_LogPrintf("%s, %s, %s\n", __FUNCTION__, status, filename); - len = sprintf(buf, "HTTP/1.0 %s%s\r\n", status, srvhead); - send(sockfd, buf, len, 0); - goto quit; -} - -TFTYPE -serverThread(void *arg) -{ - STREAMING_SERVER *server = arg; - server->state = STREAMING_ACCEPTING; - - while (server->state == STREAMING_ACCEPTING) - { - struct sockaddr_in addr; - socklen_t addrlen = sizeof(struct sockaddr_in); - int sockfd = - accept(server->socket, (struct sockaddr *) &addr, &addrlen); - - if (sockfd > 0) - { - // Create a new process and transfer the control to that - RTMP_Log(RTMP_LOGDEBUG, "%s: accepted connection from %s\n", __FUNCTION__, - inet_ntoa(addr.sin_addr)); - processTCPrequest(server, sockfd); - RTMP_Log(RTMP_LOGDEBUG, "%s: processed request\n", __FUNCTION__); - } - else - { - RTMP_Log(RTMP_LOGERROR, "%s: accept failed", __FUNCTION__); - } - } - server->state = STREAMING_STOPPED; - TFRET(); -} - -STREAMING_SERVER * -startStreaming(const char *address, int port) -{ - struct sockaddr_in addr; - int sockfd; - STREAMING_SERVER *server; - - sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, couldn't create socket", __FUNCTION__); - return 0; - } - - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(address); //htonl(INADDR_ANY); - addr.sin_port = htons(port); - - if (bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == - -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, TCP bind failed for port number: %d", __FUNCTION__, - port); - return 0; - } - - if (listen(sockfd, 10) == -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, listen failed", __FUNCTION__); - closesocket(sockfd); - return 0; - } - - server = (STREAMING_SERVER *) calloc(1, sizeof(STREAMING_SERVER)); - server->socket = sockfd; - - ThreadCreate(serverThread, server); - - return server; -} - -void -stopStreaming(STREAMING_SERVER * server) -{ - assert(server); - - if (server->state != STREAMING_STOPPED) - { - if (server->state == STREAMING_IN_PROGRESS) - { - server->state = STREAMING_STOPPING; - - // wait for streaming threads to exit - while (server->state != STREAMING_STOPPED) - msleep(1); - } - - if (closesocket(server->socket)) - RTMP_Log(RTMP_LOGERROR, "%s: Failed to close listening socket, error %d", - __FUNCTION__, GetSockError()); - - server->state = STREAMING_STOPPED; - } -} - - -void -sigIntHandler(int sig) -{ - RTMP_ctrlC = TRUE; - RTMP_LogPrintf("Caught signal: %d, cleaning up, just a second...\n", sig); - if (httpServer) - stopStreaming(httpServer); - signal(SIGINT, SIG_DFL); -} - -#define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf)) -int hex2bin(char *str, char **hex) -{ - char *ptr; - int i, l = strlen(str); - - if (l & 1) - return 0; - - *hex = malloc(l/2); - ptr = *hex; - if (!ptr) - return 0; - - for (i=0; iswfHash.av_val); - if (!res || res != RTMP_SWF_HASHLEN) - { - req->swfHash.av_val = NULL; - RTMP_Log(RTMP_LOGWARNING, - "Couldn't parse swf hash hex string, not hexstring or not %d bytes, ignoring!", RTMP_SWF_HASHLEN); - } - req->swfHash.av_len = RTMP_SWF_HASHLEN; - break; - } - case 'x': - { - int size = atoi(arg); - if (size <= 0) - { - RTMP_Log(RTMP_LOGERROR, "SWF Size must be at least 1, ignoring\n"); - } - else - { - req->swfSize = size; - } - break; - } - case 'W': - { - STR2AVAL(req->swfUrl, arg); - req->swfVfy = 1; - } - break; - case 'X': - { - int num = atoi(arg); - if (num < 0) - { - RTMP_Log(RTMP_LOGERROR, "SWF Age must be non-negative, ignoring\n"); - } - else - { - req->swfAge = num; - } - break; - } -#endif - case 'b': - { - int32_t bt = atol(arg); - if (bt < 0) - { - RTMP_Log(RTMP_LOGERROR, - "Buffer time must be greater than zero, ignoring the specified value %d!", - bt); - } - else - { - req->bufferTime = bt; - } - break; - } - case 'v': - req->bLiveStream = TRUE; // no seeking or resuming possible! - break; - case 'd': - STR2AVAL(req->subscribepath, arg); - break; - case 'n': - STR2AVAL(req->hostname, arg); - break; - case 'c': - req->rtmpport = atoi(arg); - break; - case 'l': - { - int protocol = atoi(arg); - if (protocol < RTMP_PROTOCOL_RTMP || protocol > RTMP_PROTOCOL_RTMPTS) - { - RTMP_Log(RTMP_LOGERROR, "Unknown protocol specified: %d, using default", - protocol); - return FALSE; - } - else - { - req->protocol = protocol; - } - break; - } - case 'y': - STR2AVAL(req->playpath, arg); - break; - case 'r': - { - req->rtmpurl = arg; - - AVal parsedHost, parsedPlaypath, parsedApp; - unsigned int parsedPort = 0; - int parsedProtocol = RTMP_PROTOCOL_UNDEFINED; - - if (!RTMP_ParseURL - (req->rtmpurl, &parsedProtocol, &parsedHost, &parsedPort, - &parsedPlaypath, &parsedApp)) - { - RTMP_Log(RTMP_LOGWARNING, "Couldn't parse the specified url (%s)!", arg); - } - else - { - if (!req->hostname.av_len) - req->hostname = parsedHost; - if (req->rtmpport == -1) - req->rtmpport = parsedPort; - if (req->playpath.av_len == 0 && parsedPlaypath.av_len) - { - req->playpath = parsedPlaypath; - } - if (req->protocol == RTMP_PROTOCOL_UNDEFINED) - req->protocol = parsedProtocol; - if (req->app.av_len == 0 && parsedApp.av_len) - { - req->app = parsedApp; - } - } - break; - } - case 'i': - STR2AVAL(req->fullUrl, arg); - break; - case 's': - STR2AVAL(req->swfUrl, arg); - break; - case 't': - STR2AVAL(req->tcUrl, arg); - break; - case 'p': - STR2AVAL(req->pageUrl, arg); - break; - case 'a': - STR2AVAL(req->app, arg); - break; - case 'f': - STR2AVAL(req->flashVer, arg); - break; - case 'u': - STR2AVAL(req->auth, arg); - break; - case 'C': - parseAMF(&req->extras, arg, &req->edepth); - break; - case 'm': - req->timeout = atoi(arg); - break; - case 'A': - req->dStartOffset = (int)(atof(arg) * 1000.0); - //printf("dStartOffset = %d\n", dStartOffset); - break; - case 'B': - req->dStopOffset = (int)(atof(arg) * 1000.0); - //printf("dStartOffset = %d\n", dStartOffset); - break; - case 'T': - STR2AVAL(req->token, arg); - break; - case 'S': - STR2AVAL(req->sockshost, arg); - case 'q': - RTMP_debuglevel = RTMP_LOGCRIT; - break; - case 'V': - RTMP_debuglevel = RTMP_LOGDEBUG; - break; - case 'z': - RTMP_debuglevel = RTMP_LOGALL; - break; - case 'j': - STR2AVAL(req->usherToken, arg); - break; - default: - RTMP_LogPrintf("unknown option: %c, arg: %s\n", opt, arg); - return FALSE; - } - return TRUE; -} - -int -main(int argc, char **argv) -{ - int nStatus = RD_SUCCESS; - - // http streaming server - char DEFAULT_HTTP_STREAMING_DEVICE[] = "0.0.0.0"; // 0.0.0.0 is any device - - char *httpStreamingDevice = DEFAULT_HTTP_STREAMING_DEVICE; // streaming device, default 0.0.0.0 - int nHttpStreamingPort = 80; // port - - RTMP_LogPrintf("HTTP-RTMP Stream Gateway %s\n", RTMPDUMP_VERSION); - RTMP_LogPrintf("(c) 2010 Andrej Stepanchuk, Howard Chu; license: GPL\n\n"); - - // init request - memset(&defaultRTMPRequest, 0, sizeof(RTMP_REQUEST)); - - defaultRTMPRequest.rtmpport = -1; - defaultRTMPRequest.protocol = RTMP_PROTOCOL_UNDEFINED; - defaultRTMPRequest.bLiveStream = FALSE; // is it a live stream? then we can't seek/resume - - defaultRTMPRequest.timeout = 120; // timeout connection after 120 seconds - defaultRTMPRequest.bufferTime = 20 * 1000; - - defaultRTMPRequest.swfAge = 30; - - int opt; - struct option longopts[] = { - {"help", 0, NULL, 'h'}, - {"url", 1, NULL, 'i'}, - {"host", 1, NULL, 'n'}, - {"port", 1, NULL, 'c'}, - {"socks", 1, NULL, 'S'}, - {"protocol", 1, NULL, 'l'}, - {"playpath", 1, NULL, 'y'}, - {"rtmp", 1, NULL, 'r'}, - {"swfUrl", 1, NULL, 's'}, - {"tcUrl", 1, NULL, 't'}, - {"pageUrl", 1, NULL, 'p'}, - {"app", 1, NULL, 'a'}, -#ifdef CRYPTO - {"swfhash", 1, NULL, 'w'}, - {"swfsize", 1, NULL, 'x'}, - {"swfVfy", 1, NULL, 'W'}, - {"swfAge", 1, NULL, 'X'}, -#endif - {"auth", 1, NULL, 'u'}, - {"conn", 1, NULL, 'C'}, - {"flashVer", 1, NULL, 'f'}, - {"live", 0, NULL, 'v'}, - //{"flv", 1, NULL, 'o'}, - //{"resume", 0, NULL, 'e'}, - {"timeout", 1, NULL, 'm'}, - {"buffer", 1, NULL, 'b'}, - //{"skip", 1, NULL, 'k'}, - {"device", 1, NULL, 'D'}, - {"sport", 1, NULL, 'g'}, - {"subscribe", 1, NULL, 'd'}, - {"start", 1, NULL, 'A'}, - {"stop", 1, NULL, 'B'}, - {"token", 1, NULL, 'T'}, - {"debug", 0, NULL, 'z'}, - {"quiet", 0, NULL, 'q'}, - {"verbose", 0, NULL, 'V'}, - {"jtv", 1, NULL, 'j'}, - {0, 0, 0, 0} - }; - - signal(SIGINT, sigIntHandler); -#ifndef WIN32 - signal(SIGPIPE, SIG_IGN); -#endif - - InitSockets(); - - while ((opt = - getopt_long(argc, argv, - "hvqVzr:s:t:i:p:a:f:u:n:c:l:y:m:d:D:A:B:T:g:w:x:W:X:S:j:", longopts, - NULL)) != -1) - { - switch (opt) - { - case 'h': - RTMP_LogPrintf - ("\nThis program serves media content streamed from RTMP onto HTTP.\n\n"); - RTMP_LogPrintf("--help|-h Prints this help screen.\n"); - RTMP_LogPrintf - ("--url|-i url URL with options included (e.g. rtmp://host[:port]/path swfUrl=url tcUrl=url)\n"); - RTMP_LogPrintf - ("--rtmp|-r url URL (e.g. rtmp://host[:port]/path)\n"); - RTMP_LogPrintf - ("--host|-n hostname Overrides the hostname in the rtmp url\n"); - RTMP_LogPrintf - ("--port|-c port Overrides the port in the rtmp url\n"); - RTMP_LogPrintf - ("--socks|-S host:port Use the specified SOCKS proxy\n"); - RTMP_LogPrintf - ("--protocol|-l Overrides the protocol in the rtmp url (0 - RTMP, 2 - RTMPE)\n"); - RTMP_LogPrintf - ("--playpath|-y Overrides the playpath parsed from rtmp url\n"); - RTMP_LogPrintf("--swfUrl|-s url URL to player swf file\n"); - RTMP_LogPrintf - ("--tcUrl|-t url URL to played stream (default: \"rtmp://host[:port]/app\")\n"); - RTMP_LogPrintf("--pageUrl|-p url Web URL of played programme\n"); - RTMP_LogPrintf("--app|-a app Name of target app in server\n"); -#ifdef CRYPTO - RTMP_LogPrintf - ("--swfhash|-w hexstring SHA256 hash of the decompressed SWF file (32 bytes)\n"); - RTMP_LogPrintf - ("--swfsize|-x num Size of the decompressed SWF file, required for SWFVerification\n"); - RTMP_LogPrintf - ("--swfVfy|-W url URL to player swf file, compute hash/size automatically\n"); - RTMP_LogPrintf - ("--swfAge|-X days Number of days to use cached SWF hash before refreshing\n"); -#endif - RTMP_LogPrintf - ("--auth|-u string Authentication string to be appended to the connect string\n"); - RTMP_LogPrintf - ("--conn|-C type:data Arbitrary AMF data to be appended to the connect string\n"); - RTMP_LogPrintf - (" B:boolean(0|1), S:string, N:number, O:object-flag(0|1),\n"); - RTMP_LogPrintf - (" Z:(null), NB:name:boolean, NS:name:string, NN:name:number\n"); - RTMP_LogPrintf - ("--flashVer|-f string Flash version string (default: \"%s\")\n", - RTMP_DefaultFlashVer.av_val); - RTMP_LogPrintf - ("--live|-v Get a live stream, no --resume (seeking) of live streams possible\n"); - RTMP_LogPrintf - ("--subscribe|-d string Stream name to subscribe to (otherwise defaults to playpath if live is specified)\n"); - RTMP_LogPrintf - ("--timeout|-m num Timeout connection num seconds (default: %lu)\n", - defaultRTMPRequest.timeout); - RTMP_LogPrintf - ("--start|-A num Start at num seconds into stream (not valid when using --live)\n"); - RTMP_LogPrintf - ("--stop|-B num Stop at num seconds into stream\n"); - RTMP_LogPrintf - ("--token|-T key Key for SecureToken response\n"); - RTMP_LogPrintf - ("--jtv|-j JSON Authentication token for Justin.tv legacy servers\n"); - RTMP_LogPrintf - ("--buffer|-b Buffer time in milliseconds (default: %u)\n\n", - defaultRTMPRequest.bufferTime); - - RTMP_LogPrintf - ("--device|-D Streaming device ip address (default: %s)\n", - DEFAULT_HTTP_STREAMING_DEVICE); - RTMP_LogPrintf - ("--sport|-g Streaming port (default: %d)\n\n", - nHttpStreamingPort); - RTMP_LogPrintf - ("--quiet|-q Suppresses all command output.\n"); - RTMP_LogPrintf("--verbose|-V Verbose command output.\n"); - RTMP_LogPrintf("--debug|-z Debug level command output.\n"); - RTMP_LogPrintf - ("If you don't pass parameters for swfUrl, pageUrl, or auth these properties will not be included in the connect "); - RTMP_LogPrintf("packet.\n\n"); - return RD_SUCCESS; - break; - // streaming server specific options - case 'D': - if (inet_addr(optarg) == INADDR_NONE) - { - RTMP_Log(RTMP_LOGERROR, - "Invalid binding address (requested address %s), ignoring", - optarg); - } - else - { - httpStreamingDevice = optarg; - } - break; - case 'g': - { - int port = atoi(optarg); - if (port < 0 || port > 65535) - { - RTMP_Log(RTMP_LOGERROR, - "Streaming port out of range (requested port %d), ignoring\n", - port); - } - else - { - nHttpStreamingPort = port; - } - break; - } - default: - //RTMP_LogPrintf("unknown option: %c\n", opt); - if (!ParseOption(opt, optarg, &defaultRTMPRequest)) - return RD_FAILED; - break; - } - } - -#ifdef _DEBUG - netstackdump = fopen("netstackdump", "wb"); - netstackdump_read = fopen("netstackdump_read", "wb"); -#endif - - // start text UI - ThreadCreate(controlServerThread, 0); - - // start http streaming - if ((httpServer = - startStreaming(httpStreamingDevice, nHttpStreamingPort)) == 0) - { - RTMP_Log(RTMP_LOGERROR, "Failed to start HTTP server, exiting!"); - return RD_FAILED; - } - RTMP_LogPrintf("Streaming on http://%s:%d\n", httpStreamingDevice, - nHttpStreamingPort); - - while (httpServer->state != STREAMING_STOPPED) - { - sleep(1); - } - RTMP_Log(RTMP_LOGDEBUG, "Done, exiting..."); - - CleanupSockets(); - -#ifdef _DEBUG - if (netstackdump != 0) - fclose(netstackdump); - if (netstackdump_read != 0) - fclose(netstackdump_read); -#endif - return nStatus; -} diff --git a/rtmp/rtmp_c/rtmpsrv.c b/rtmp/rtmp_c/rtmpsrv.c deleted file mode 100644 index 5df4d3a7..00000000 --- a/rtmp/rtmp_c/rtmpsrv.c +++ /dev/null @@ -1,1193 +0,0 @@ -/* Simple RTMP Server - * Copyright (C) 2009 Andrej Stepanchuk - * Copyright (C) 2009-2011 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, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - */ - -/* This is just a stub for an RTMP server. It doesn't do anything - * beyond obtaining the connection parameters from the client. - */ - -#include -#include -#include -#include - -#include -#include - -#include - -#include "librtmp/rtmp_sys.h" -#include "librtmp/log.h" - -#include "thread.h" - -#ifdef linux -#include -#endif - -#ifndef WIN32 -#include -#include -#endif - -#define RD_SUCCESS 0 -#define RD_FAILED 1 -#define RD_INCOMPLETE 2 - -#define PACKET_SIZE 1024*1024 - -#ifdef WIN32 -#define InitSockets() {\ - WORD version; \ - WSADATA wsaData; \ - \ - version = MAKEWORD(1,1); \ - WSAStartup(version, &wsaData); } - -#define CleanupSockets() WSACleanup() -#else -#define InitSockets() -#define CleanupSockets() -#endif - -#define DUPTIME 5000 /* interval we disallow duplicate requests, in msec */ - -enum -{ - STREAMING_ACCEPTING, - STREAMING_IN_PROGRESS, - STREAMING_STOPPING, - STREAMING_STOPPED -}; - -typedef struct -{ - int socket; - int state; - int streamID; - int arglen; - int argc; - uint32_t filetime; /* time of last download we started */ - AVal filename; /* name of last download */ - char *connect; - -} STREAMING_SERVER; - -STREAMING_SERVER *rtmpServer = 0; // server structure pointer -void *sslCtx = NULL; - -STREAMING_SERVER *startStreaming(const char *address, int port); -void stopStreaming(STREAMING_SERVER * server); -void AVreplace(AVal *src, const AVal *orig, const AVal *repl); - -static const AVal av_dquote = AVC("\""); -static const AVal av_escdquote = AVC("\\\""); - -typedef struct -{ - char *hostname; - int rtmpport; - int protocol; - int bLiveStream; // is it a live stream? then we can't seek/resume - - long int timeout; // timeout connection afte 300 seconds - uint32_t bufferTime; - - char *rtmpurl; - AVal playpath; - AVal swfUrl; - AVal tcUrl; - AVal pageUrl; - AVal app; - AVal auth; - AVal swfHash; - AVal flashVer; - AVal subscribepath; - uint32_t swfSize; - - uint32_t dStartOffset; - uint32_t dStopOffset; - uint32_t nTimeStamp; -} RTMP_REQUEST; - -#define STR2AVAL(av,str) av.av_val = str; av.av_len = strlen(av.av_val) - -/* this request is formed from the parameters and used to initialize a new request, - * thus it is a default settings list. All settings can be overriden by specifying the - * parameters in the GET request. */ -RTMP_REQUEST defaultRTMPRequest; - -#ifdef _DEBUG -uint32_t debugTS = 0; - -int pnum = 0; - -FILE *netstackdump = NULL; -FILE *netstackdump_read = NULL; -#endif - -#define SAVC(x) static const AVal av_##x = AVC(#x) - -SAVC(app); -SAVC(connect); -SAVC(flashVer); -SAVC(swfUrl); -SAVC(pageUrl); -SAVC(tcUrl); -SAVC(fpad); -SAVC(capabilities); -SAVC(audioCodecs); -SAVC(videoCodecs); -SAVC(videoFunction); -SAVC(objectEncoding); -SAVC(_result); -SAVC(createStream); -SAVC(getStreamLength); -SAVC(play); -SAVC(fmsVer); -SAVC(mode); -SAVC(level); -SAVC(code); -SAVC(description); -SAVC(secureToken); - -static int -SendConnectResult(RTMP *r, double txn) -{ - RTMPPacket packet; - char pbuf[384], *pend = pbuf+sizeof(pbuf); - AMFObject obj; - AMFObjectProperty p, op; - AVal av; - - packet.m_nChannel = 0x03; // control channel (invoke) - packet.m_headerType = 1; /* RTMP_PACKET_SIZE_MEDIUM; */ - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - char *enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av__result); - enc = AMF_EncodeNumber(enc, pend, txn); - *enc++ = AMF_OBJECT; - - STR2AVAL(av, "FMS/3,5,1,525"); - enc = AMF_EncodeNamedString(enc, pend, &av_fmsVer, &av); - enc = AMF_EncodeNamedNumber(enc, pend, &av_capabilities, 31.0); - enc = AMF_EncodeNamedNumber(enc, pend, &av_mode, 1.0); - *enc++ = 0; - *enc++ = 0; - *enc++ = AMF_OBJECT_END; - - *enc++ = AMF_OBJECT; - - STR2AVAL(av, "status"); - enc = AMF_EncodeNamedString(enc, pend, &av_level, &av); - STR2AVAL(av, "NetConnection.Connect.Success"); - enc = AMF_EncodeNamedString(enc, pend, &av_code, &av); - STR2AVAL(av, "Connection succeeded."); - enc = AMF_EncodeNamedString(enc, pend, &av_description, &av); - enc = AMF_EncodeNamedNumber(enc, pend, &av_objectEncoding, r->m_fEncoding); -#if 0 - STR2AVAL(av, "58656322c972d6cdf2d776167575045f8484ea888e31c086f7b5ffbd0baec55ce442c2fb"); - enc = AMF_EncodeNamedString(enc, pend, &av_secureToken, &av); -#endif - STR2AVAL(p.p_name, "version"); - STR2AVAL(p.p_vu.p_aval, "3,5,1,525"); - p.p_type = AMF_STRING; - obj.o_num = 1; - obj.o_props = &p; - op.p_type = AMF_OBJECT; - STR2AVAL(op.p_name, "data"); - op.p_vu.p_object = obj; - enc = AMFProp_Encode(&op, enc, pend); - *enc++ = 0; - *enc++ = 0; - *enc++ = AMF_OBJECT_END; - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} - -static int -SendResultNumber(RTMP *r, double txn, double ID) -{ - RTMPPacket packet; - char pbuf[256], *pend = pbuf+sizeof(pbuf); - - packet.m_nChannel = 0x03; // control channel (invoke) - packet.m_headerType = 1; /* RTMP_PACKET_SIZE_MEDIUM; */ - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - char *enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av__result); - enc = AMF_EncodeNumber(enc, pend, txn); - *enc++ = AMF_NULL; - enc = AMF_EncodeNumber(enc, pend, ID); - - packet.m_nBodySize = enc - packet.m_body; - - return RTMP_SendPacket(r, &packet, FALSE); -} - -SAVC(onStatus); -SAVC(status); -static const AVal av_NetStream_Play_Start = AVC("NetStream.Play.Start"); -static const AVal av_Started_playing = AVC("Started playing"); -static const AVal av_NetStream_Play_Stop = AVC("NetStream.Play.Stop"); -static const AVal av_Stopped_playing = AVC("Stopped playing"); -SAVC(details); -SAVC(clientid); -static const AVal av_NetStream_Authenticate_UsherToken = AVC("NetStream.Authenticate.UsherToken"); - -static int -SendPlayStart(RTMP *r) -{ - RTMPPacket packet; - char pbuf[512], *pend = pbuf+sizeof(pbuf); - - packet.m_nChannel = 0x03; // control channel (invoke) - packet.m_headerType = 1; /* RTMP_PACKET_SIZE_MEDIUM; */ - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - char *enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_onStatus); - enc = AMF_EncodeNumber(enc, pend, 0); - *enc++ = AMF_OBJECT; - - enc = AMF_EncodeNamedString(enc, pend, &av_level, &av_status); - enc = AMF_EncodeNamedString(enc, pend, &av_code, &av_NetStream_Play_Start); - enc = AMF_EncodeNamedString(enc, pend, &av_description, &av_Started_playing); - enc = AMF_EncodeNamedString(enc, pend, &av_details, &r->Link.playpath); - enc = AMF_EncodeNamedString(enc, pend, &av_clientid, &av_clientid); - *enc++ = 0; - *enc++ = 0; - *enc++ = AMF_OBJECT_END; - - packet.m_nBodySize = enc - packet.m_body; - return RTMP_SendPacket(r, &packet, FALSE); -} - -static int -SendPlayStop(RTMP *r) -{ - RTMPPacket packet; - char pbuf[512], *pend = pbuf+sizeof(pbuf); - - packet.m_nChannel = 0x03; // control channel (invoke) - packet.m_headerType = 1; /* RTMP_PACKET_SIZE_MEDIUM; */ - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE; - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - char *enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_onStatus); - enc = AMF_EncodeNumber(enc, pend, 0); - *enc++ = AMF_OBJECT; - - enc = AMF_EncodeNamedString(enc, pend, &av_level, &av_status); - enc = AMF_EncodeNamedString(enc, pend, &av_code, &av_NetStream_Play_Stop); - enc = AMF_EncodeNamedString(enc, pend, &av_description, &av_Stopped_playing); - enc = AMF_EncodeNamedString(enc, pend, &av_details, &r->Link.playpath); - enc = AMF_EncodeNamedString(enc, pend, &av_clientid, &av_clientid); - *enc++ = 0; - *enc++ = 0; - *enc++ = AMF_OBJECT_END; - - packet.m_nBodySize = enc - packet.m_body; - return RTMP_SendPacket(r, &packet, FALSE); -} - -static void -spawn_dumper(int argc, AVal *av, char *cmd) -{ -#ifdef WIN32 - STARTUPINFO si = {0}; - PROCESS_INFORMATION pi = {0}; - - si.cb = sizeof(si); - if (CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, - &si, &pi)) - { - CloseHandle(pi.hThread); - CloseHandle(pi.hProcess); - } -#else - /* reap any dead children */ - while (waitpid(-1, NULL, WNOHANG) > 0); - - if (fork() == 0) { - char **argv = malloc((argc+1) * sizeof(char *)); - int i; - - for (i=0; io_num; i++) - { - AMFObjectProperty *p = &obj->o_props[i]; - len += 4; - (*argc)+= 2; - if (p->p_name.av_val) - len += 1; - len += 2; - if (p->p_name.av_val) - len += p->p_name.av_len + 1; - switch(p->p_type) - { - case AMF_BOOLEAN: - len += 1; - break; - case AMF_STRING: - len += p->p_vu.p_aval.av_len; - break; - case AMF_NUMBER: - len += 40; - break; - case AMF_OBJECT: - len += 9; - len += countAMF(&p->p_vu.p_object, argc); - (*argc) += 2; - break; - case AMF_NULL: - default: - break; - } - } - return len; -} - -static char * -dumpAMF(AMFObject *obj, char *ptr, AVal *argv, int *argc) -{ - int i, ac = *argc; - const char opt[] = "NBSO Z"; - - for (i=0; i < obj->o_num; i++) - { - AMFObjectProperty *p = &obj->o_props[i]; - argv[ac].av_val = ptr+1; - argv[ac++].av_len = 2; - ptr += sprintf(ptr, " -C "); - argv[ac].av_val = ptr; - if (p->p_name.av_val) - *ptr++ = 'N'; - *ptr++ = opt[p->p_type]; - *ptr++ = ':'; - if (p->p_name.av_val) - ptr += sprintf(ptr, "%.*s:", p->p_name.av_len, p->p_name.av_val); - switch(p->p_type) - { - case AMF_BOOLEAN: - *ptr++ = p->p_vu.p_number != 0 ? '1' : '0'; - argv[ac].av_len = ptr - argv[ac].av_val; - break; - case AMF_STRING: - memcpy(ptr, p->p_vu.p_aval.av_val, p->p_vu.p_aval.av_len); - ptr += p->p_vu.p_aval.av_len; - argv[ac].av_len = ptr - argv[ac].av_val; - break; - case AMF_NUMBER: - ptr += sprintf(ptr, "%f", p->p_vu.p_number); - argv[ac].av_len = ptr - argv[ac].av_val; - break; - case AMF_OBJECT: - *ptr++ = '1'; - argv[ac].av_len = ptr - argv[ac].av_val; - ac++; - *argc = ac; - ptr = dumpAMF(&p->p_vu.p_object, ptr, argv, argc); - ac = *argc; - argv[ac].av_val = ptr+1; - argv[ac++].av_len = 2; - argv[ac].av_val = ptr+4; - argv[ac].av_len = 3; - ptr += sprintf(ptr, " -C O:0"); - break; - case AMF_NULL: - default: - argv[ac].av_len = ptr - argv[ac].av_val; - break; - } - ac++; - } - *argc = ac; - return ptr; -} - -// Returns 0 for OK/Failed/error, 1 for 'Stop or Complete' -int -ServeInvoke(STREAMING_SERVER *server, RTMP * r, RTMPPacket *packet, unsigned int offset) -{ - const char *body; - unsigned int nBodySize; - int ret = 0, nRes; - - body = packet->m_body + offset; - nBodySize = packet->m_nBodySize - offset; - - if (body[0] != 0x02) // make sure it is a string method name we start with - { - RTMP_Log(RTMP_LOGWARNING, "%s, Sanity failed. no string method in invoke packet", - __FUNCTION__); - return 0; - } - - AMFObject obj; - nRes = AMF_Decode(&obj, body, nBodySize, FALSE); - if (nRes < 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__); - return 0; - } - - AMF_Dump(&obj); - AVal method; - AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &method); - double txn = AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 1)); - RTMP_Log(RTMP_LOGDEBUG, "%s, client invoking <%s>", __FUNCTION__, method.av_val); - - if (AVMATCH(&method, &av_connect)) - { - AMFObject cobj; - AVal pname, pval; - int i; - - server->connect = packet->m_body; - packet->m_body = NULL; - - AMFProp_GetObject(AMF_GetProp(&obj, NULL, 2), &cobj); - for (i=0; iLink.app = pval; - pval.av_val = NULL; - if (!r->Link.app.av_val) - r->Link.app.av_val = ""; - server->arglen += 6 + pval.av_len; - server->argc += 2; - } - else if (AVMATCH(&pname, &av_flashVer)) - { - r->Link.flashVer = pval; - pval.av_val = NULL; - server->arglen += 6 + pval.av_len; - server->argc += 2; - } - else if (AVMATCH(&pname, &av_swfUrl)) - { - r->Link.swfUrl = pval; - pval.av_val = NULL; - server->arglen += 6 + pval.av_len; - server->argc += 2; - } - else if (AVMATCH(&pname, &av_tcUrl)) - { - r->Link.tcUrl = pval; - pval.av_val = NULL; - server->arglen += 6 + pval.av_len; - server->argc += 2; - } - else if (AVMATCH(&pname, &av_pageUrl)) - { - r->Link.pageUrl = pval; - pval.av_val = NULL; - server->arglen += 6 + pval.av_len; - server->argc += 2; - } - else if (AVMATCH(&pname, &av_audioCodecs)) - { - r->m_fAudioCodecs = cobj.o_props[i].p_vu.p_number; - } - else if (AVMATCH(&pname, &av_videoCodecs)) - { - r->m_fVideoCodecs = cobj.o_props[i].p_vu.p_number; - } - else if (AVMATCH(&pname, &av_objectEncoding)) - { - r->m_fEncoding = cobj.o_props[i].p_vu.p_number; - } - } - /* Still have more parameters? Copy them */ - if (obj.o_num > 3) - { - int i = obj.o_num - 3; - r->Link.extras.o_num = i; - r->Link.extras.o_props = malloc(i*sizeof(AMFObjectProperty)); - memcpy(r->Link.extras.o_props, obj.o_props+3, i*sizeof(AMFObjectProperty)); - obj.o_num = 3; - server->arglen += countAMF(&r->Link.extras, &server->argc); - } - SendConnectResult(r, txn); - } - else if (AVMATCH(&method, &av_createStream)) - { - SendResultNumber(r, txn, ++server->streamID); - } - else if (AVMATCH(&method, &av_getStreamLength)) - { - SendResultNumber(r, txn, 10.0); - } - else if (AVMATCH(&method, &av_NetStream_Authenticate_UsherToken)) - { - AVal usherToken; - AMFProp_GetString(AMF_GetProp(&obj, NULL, 3), &usherToken); - AVreplace(&usherToken, &av_dquote, &av_escdquote); - server->arglen += 6 + usherToken.av_len; - server->argc += 2; - r->Link.usherToken = usherToken; - } - else if (AVMATCH(&method, &av_play)) - { - char *file, *p, *q, *cmd, *ptr; - AVal *argv, av; - int len, argc; - uint32_t now; - RTMPPacket pc = {0}; - AMFProp_GetString(AMF_GetProp(&obj, NULL, 3), &r->Link.playpath); - if (!r->Link.playpath.av_len) - return 0; - /* - r->Link.seekTime = AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 4)); - if (obj.o_num > 5) - r->Link.length = AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 5)); - */ - if (r->Link.tcUrl.av_len) - { - len = server->arglen + r->Link.playpath.av_len + 4 + - sizeof("rtmpdump") + r->Link.playpath.av_len + 12; - server->argc += 5; - - cmd = malloc(len + server->argc * sizeof(AVal)); - ptr = cmd; - argv = (AVal *)(cmd + len); - argv[0].av_val = cmd; - argv[0].av_len = sizeof("rtmpdump")-1; - ptr += sprintf(ptr, "rtmpdump"); - argc = 1; - - argv[argc].av_val = ptr + 1; - argv[argc++].av_len = 2; - argv[argc].av_val = ptr + 5; - ptr += sprintf(ptr," -r \"%s\"", r->Link.tcUrl.av_val); - argv[argc++].av_len = r->Link.tcUrl.av_len; - - if (r->Link.app.av_val) - { - argv[argc].av_val = ptr + 1; - argv[argc++].av_len = 2; - argv[argc].av_val = ptr + 5; - ptr += sprintf(ptr, " -a \"%s\"", r->Link.app.av_val); - argv[argc++].av_len = r->Link.app.av_len; - } - if (r->Link.flashVer.av_val) - { - argv[argc].av_val = ptr + 1; - argv[argc++].av_len = 2; - argv[argc].av_val = ptr + 5; - ptr += sprintf(ptr, " -f \"%s\"", r->Link.flashVer.av_val); - argv[argc++].av_len = r->Link.flashVer.av_len; - } - if (r->Link.swfUrl.av_val) - { - argv[argc].av_val = ptr + 1; - argv[argc++].av_len = 2; - argv[argc].av_val = ptr + 5; - ptr += sprintf(ptr, " -W \"%s\"", r->Link.swfUrl.av_val); - argv[argc++].av_len = r->Link.swfUrl.av_len; - } - if (r->Link.pageUrl.av_val) - { - argv[argc].av_val = ptr + 1; - argv[argc++].av_len = 2; - argv[argc].av_val = ptr + 5; - ptr += sprintf(ptr, " -p \"%s\"", r->Link.pageUrl.av_val); - argv[argc++].av_len = r->Link.pageUrl.av_len; - } - if (r->Link.usherToken.av_val) - { - argv[argc].av_val = ptr + 1; - argv[argc++].av_len = 2; - argv[argc].av_val = ptr + 5; - ptr += sprintf(ptr, " -j \"%s\"", r->Link.usherToken.av_val); - argv[argc++].av_len = r->Link.usherToken.av_len; - free(r->Link.usherToken.av_val); - r->Link.usherToken.av_val = NULL; - r->Link.usherToken.av_len = 0; - } - if (r->Link.extras.o_num) { - ptr = dumpAMF(&r->Link.extras, ptr, argv, &argc); - AMF_Reset(&r->Link.extras); - } - argv[argc].av_val = ptr + 1; - argv[argc++].av_len = 2; - argv[argc].av_val = ptr + 5; - ptr += sprintf(ptr, " -y \"%.*s\"", - r->Link.playpath.av_len, r->Link.playpath.av_val); - argv[argc++].av_len = r->Link.playpath.av_len; - - av = r->Link.playpath; - /* strip trailing URL parameters */ - q = memchr(av.av_val, '?', av.av_len); - if (q) - { - if (q == av.av_val) - { - av.av_val++; - av.av_len--; - } - else - { - av.av_len = q - av.av_val; - } - } - /* strip leading slash components */ - for (p=av.av_val+av.av_len-1; p>=av.av_val; p--) - if (*p == '/') - { - p++; - av.av_len -= p - av.av_val; - av.av_val = p; - break; - } - /* skip leading dot */ - if (av.av_val[0] == '.') - { - av.av_val++; - av.av_len--; - } - file = malloc(av.av_len+5); - - memcpy(file, av.av_val, av.av_len); - file[av.av_len] = '\0'; - for (p=file; *p; p++) - if (*p == ':') - *p = '_'; - - /* Add extension if none present */ - if (file[av.av_len - 4] != '.') - { - av.av_len += 4; - } - /* Always use flv extension, regardless of original */ - if (strcmp(file+av.av_len-4, ".flv")) - { - strcpy(file+av.av_len-4, ".flv"); - } - argv[argc].av_val = ptr + 1; - argv[argc++].av_len = 2; - argv[argc].av_val = file; - argv[argc].av_len = av.av_len; - ptr += sprintf(ptr, " -o %s", file); - now = RTMP_GetTime(); - if (now - server->filetime < DUPTIME && AVMATCH(&argv[argc], &server->filename)) - { - printf("Duplicate request, skipping.\n"); - free(file); - } - else - { - printf("\n%s\n\n", cmd); - fflush(stdout); - server->filetime = now; - free(server->filename.av_val); - server->filename = argv[argc++]; - spawn_dumper(argc, argv, cmd); - } - - free(cmd); - } - pc.m_body = server->connect; - server->connect = NULL; - RTMPPacket_Free(&pc); - ret = 1; - RTMP_SendCtrl(r, 0, 1, 0); - SendPlayStart(r); - RTMP_SendCtrl(r, 1, 1, 0); - SendPlayStop(r); - } - AMF_Reset(&obj); - return ret; -} - -int -ServePacket(STREAMING_SERVER *server, RTMP *r, RTMPPacket *packet) -{ - int ret = 0; - - RTMP_Log(RTMP_LOGDEBUG, "%s, received packet type %02X, size %u bytes", __FUNCTION__, - packet->m_packetType, packet->m_nBodySize); - - switch (packet->m_packetType) - { - case RTMP_PACKET_TYPE_CHUNK_SIZE: -// HandleChangeChunkSize(r, packet); - break; - - case RTMP_PACKET_TYPE_BYTES_READ_REPORT: - break; - - case RTMP_PACKET_TYPE_CONTROL: -// HandleCtrl(r, packet); - break; - - case RTMP_PACKET_TYPE_SERVER_BW: -// HandleServerBW(r, packet); - break; - - case RTMP_PACKET_TYPE_CLIENT_BW: - // HandleClientBW(r, packet); - break; - - case RTMP_PACKET_TYPE_AUDIO: - //RTMP_Log(RTMP_LOGDEBUG, "%s, received: audio %lu bytes", __FUNCTION__, packet.m_nBodySize); - break; - - case RTMP_PACKET_TYPE_VIDEO: - //RTMP_Log(RTMP_LOGDEBUG, "%s, received: video %lu bytes", __FUNCTION__, packet.m_nBodySize); - break; - - case RTMP_PACKET_TYPE_FLEX_STREAM_SEND: - break; - - case RTMP_PACKET_TYPE_FLEX_SHARED_OBJECT: - break; - - case RTMP_PACKET_TYPE_FLEX_MESSAGE: - { - RTMP_Log(RTMP_LOGDEBUG, "%s, flex message, size %u bytes, not fully supported", - __FUNCTION__, packet->m_nBodySize); - //RTMP_LogHex(packet.m_body, packet.m_nBodySize); - - // some DEBUG code - /*RTMP_LIB_AMFObject obj; - int nRes = obj.Decode(packet.m_body+1, packet.m_nBodySize-1); - if(nRes < 0) { - RTMP_Log(RTMP_LOGERROR, "%s, error decoding AMF3 packet", __FUNCTION__); - //return; - } - - obj.Dump(); */ - - if (ServeInvoke(server, r, packet, 1)) - RTMP_Close(r); - break; - } - case RTMP_PACKET_TYPE_INFO: - break; - - case RTMP_PACKET_TYPE_SHARED_OBJECT: - break; - - case RTMP_PACKET_TYPE_INVOKE: - RTMP_Log(RTMP_LOGDEBUG, "%s, received: invoke %u bytes", __FUNCTION__, - packet->m_nBodySize); - //RTMP_LogHex(packet.m_body, packet.m_nBodySize); - - if (ServeInvoke(server, r, packet, 0)) - RTMP_Close(r); - break; - - case RTMP_PACKET_TYPE_FLASH_VIDEO: - break; - default: - RTMP_Log(RTMP_LOGDEBUG, "%s, unknown packet type received: 0x%02x", __FUNCTION__, - packet->m_packetType); -#ifdef _DEBUG - RTMP_LogHex(RTMP_LOGDEBUG, packet->m_body, packet->m_nBodySize); -#endif - } - return ret; -} - -TFTYPE -controlServerThread(void *unused) -{ - char ich; - while (1) - { - ich = getchar(); - switch (ich) - { - case 'q': - RTMP_LogPrintf("Exiting\n"); - stopStreaming(rtmpServer); - exit(0); - break; - default: - RTMP_LogPrintf("Unknown command \'%c\', ignoring\n", ich); - } - } - TFRET(); -} - - -void doServe(STREAMING_SERVER * server, // server socket and state (our listening socket) - int sockfd // client connection socket - ) -{ - server->state = STREAMING_IN_PROGRESS; - - RTMP *rtmp = RTMP_Alloc(); /* our session with the real client */ - RTMPPacket packet = { 0 }; - - // timeout for http requests - fd_set fds; - struct timeval tv; - - memset(&tv, 0, sizeof(struct timeval)); - tv.tv_sec = 5; - - FD_ZERO(&fds); - FD_SET(sockfd, &fds); - - if (select(sockfd + 1, &fds, NULL, NULL, &tv) <= 0) - { - RTMP_Log(RTMP_LOGERROR, "Request timeout/select failed, ignoring request"); - goto quit; - } - else - { - RTMP_Init(rtmp); - rtmp->m_sb.sb_socket = sockfd; - if (sslCtx && !RTMP_TLS_Accept(rtmp, sslCtx)) - { - RTMP_Log(RTMP_LOGERROR, "TLS handshake failed"); - goto cleanup; - } - if (!RTMP_Serve(rtmp)) - { - RTMP_Log(RTMP_LOGERROR, "Handshake failed"); - goto cleanup; - } - } - server->arglen = 0; - while (RTMP_IsConnected(rtmp) && RTMP_ReadPacket(rtmp, &packet)) - { - if (!RTMPPacket_IsReady(&packet)) - continue; - ServePacket(server, rtmp, &packet); - RTMPPacket_Free(&packet); - } - -cleanup: - RTMP_LogPrintf("Closing connection... "); - RTMP_Close(rtmp); - /* Should probably be done by RTMP_Close() ... */ - rtmp->Link.playpath.av_val = NULL; - rtmp->Link.tcUrl.av_val = NULL; - rtmp->Link.swfUrl.av_val = NULL; - rtmp->Link.pageUrl.av_val = NULL; - rtmp->Link.app.av_val = NULL; - rtmp->Link.flashVer.av_val = NULL; - if (rtmp->Link.usherToken.av_val) - { - free(rtmp->Link.usherToken.av_val); - rtmp->Link.usherToken.av_val = NULL; - } - RTMP_Free(rtmp); - RTMP_LogPrintf("done!\n\n"); - -quit: - if (server->state == STREAMING_IN_PROGRESS) - server->state = STREAMING_ACCEPTING; - - return; -} - -TFTYPE -serverThread(void *arg) -{ - STREAMING_SERVER *server = arg; - server->state = STREAMING_ACCEPTING; - - while (server->state == STREAMING_ACCEPTING) - { - struct sockaddr_in addr; - socklen_t addrlen = sizeof(struct sockaddr_in); - int sockfd = - accept(server->socket, (struct sockaddr *) &addr, &addrlen); - - if (sockfd > 0) - { -#ifdef linux - struct sockaddr_in dest; - char destch[16]; - socklen_t destlen = sizeof(struct sockaddr_in); - getsockopt(sockfd, SOL_IP, SO_ORIGINAL_DST, &dest, &destlen); - strcpy(destch, inet_ntoa(dest.sin_addr)); - RTMP_Log(RTMP_LOGDEBUG, "%s: accepted connection from %s to %s\n", __FUNCTION__, - inet_ntoa(addr.sin_addr), destch); -#else - RTMP_Log(RTMP_LOGDEBUG, "%s: accepted connection from %s\n", __FUNCTION__, - inet_ntoa(addr.sin_addr)); -#endif - /* Create a new thread and transfer the control to that */ - doServe(server, sockfd); - RTMP_Log(RTMP_LOGDEBUG, "%s: processed request\n", __FUNCTION__); - } - else - { - RTMP_Log(RTMP_LOGERROR, "%s: accept failed", __FUNCTION__); - } - } - server->state = STREAMING_STOPPED; - TFRET(); -} - -STREAMING_SERVER * -startStreaming(const char *address, int port) -{ - struct sockaddr_in addr; - int sockfd, tmp; - STREAMING_SERVER *server; - - sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, couldn't create socket", __FUNCTION__); - return 0; - } - - tmp = 1; - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, - (char *) &tmp, sizeof(tmp) ); - - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(address); //htonl(INADDR_ANY); - addr.sin_port = htons(port); - - if (bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == - -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, TCP bind failed for port number: %d", __FUNCTION__, - port); - return 0; - } - - if (listen(sockfd, 10) == -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, listen failed", __FUNCTION__); - closesocket(sockfd); - return 0; - } - - server = (STREAMING_SERVER *) calloc(1, sizeof(STREAMING_SERVER)); - server->socket = sockfd; - - ThreadCreate(serverThread, server); - - return server; -} - -void -stopStreaming(STREAMING_SERVER * server) -{ - assert(server); - - if (server->state != STREAMING_STOPPED) - { - if (server->state == STREAMING_IN_PROGRESS) - { - server->state = STREAMING_STOPPING; - - // wait for streaming threads to exit - while (server->state != STREAMING_STOPPED) - msleep(1); - } - - if (closesocket(server->socket)) - RTMP_Log(RTMP_LOGERROR, "%s: Failed to close listening socket, error %d", - __FUNCTION__, GetSockError()); - - server->state = STREAMING_STOPPED; - } -} - - -void -sigIntHandler(int sig) -{ - RTMP_ctrlC = TRUE; - RTMP_LogPrintf("Caught signal: %d, cleaning up, just a second...\n", sig); - if (rtmpServer) - stopStreaming(rtmpServer); - signal(SIGINT, SIG_DFL); -} - -int -main(int argc, char **argv) -{ - int nStatus = RD_SUCCESS; - int i; - - // http streaming server - char DEFAULT_HTTP_STREAMING_DEVICE[] = "0.0.0.0"; // 0.0.0.0 is any device - - char *rtmpStreamingDevice = DEFAULT_HTTP_STREAMING_DEVICE; // streaming device, default 0.0.0.0 - int nRtmpStreamingPort = 1935; // port - char *cert = NULL, *key = NULL; - - RTMP_LogPrintf("RTMP Server %s\n", RTMPDUMP_VERSION); - RTMP_LogPrintf("(c) 2010 Andrej Stepanchuk, Howard Chu; license: GPL\n\n"); - - RTMP_debuglevel = RTMP_LOGINFO; - - for (i = 1; i < argc; i++) - { - if (!strcmp(argv[i], "-z")) - RTMP_debuglevel = RTMP_LOGALL; - else if (!strcmp(argv[i], "-c") && i + 1 < argc) - cert = argv[++i]; - else if (!strcmp(argv[i], "-k") && i + 1 < argc) - key = argv[++i]; - } - - if (cert && key) - sslCtx = RTMP_TLS_AllocServerContext(cert, key); - - // init request - memset(&defaultRTMPRequest, 0, sizeof(RTMP_REQUEST)); - - defaultRTMPRequest.rtmpport = -1; - defaultRTMPRequest.protocol = RTMP_PROTOCOL_UNDEFINED; - defaultRTMPRequest.bLiveStream = FALSE; // is it a live stream? then we can't seek/resume - - defaultRTMPRequest.timeout = 300; // timeout connection afte 300 seconds - defaultRTMPRequest.bufferTime = 20 * 1000; - - - signal(SIGINT, sigIntHandler); -#ifndef WIN32 - signal(SIGPIPE, SIG_IGN); -#endif - -#ifdef _DEBUG - netstackdump = fopen("netstackdump", "wb"); - netstackdump_read = fopen("netstackdump_read", "wb"); -#endif - - InitSockets(); - - // start text UI - ThreadCreate(controlServerThread, 0); - - // start http streaming - if ((rtmpServer = - startStreaming(rtmpStreamingDevice, nRtmpStreamingPort)) == 0) - { - RTMP_Log(RTMP_LOGERROR, "Failed to start RTMP server, exiting!"); - return RD_FAILED; - } - RTMP_LogPrintf("Streaming on rtmp://%s:%d\n", rtmpStreamingDevice, - nRtmpStreamingPort); - - while (rtmpServer->state != STREAMING_STOPPED) - { - sleep(1); - } - RTMP_Log(RTMP_LOGDEBUG, "Done, exiting..."); - - if (sslCtx) - RTMP_TLS_FreeServerContext(sslCtx); - - CleanupSockets(); - -#ifdef _DEBUG - if (netstackdump != 0) - fclose(netstackdump); - if (netstackdump_read != 0) - fclose(netstackdump_read); -#endif - return nStatus; -} - -void -AVreplace(AVal *src, const AVal *orig, const AVal *repl) -{ - char *srcbeg = src->av_val; - char *srcend = src->av_val + src->av_len; - char *dest, *sptr, *dptr; - int n = 0; - - /* count occurrences of orig in src */ - sptr = src->av_val; - while (sptr < srcend && (sptr = strstr(sptr, orig->av_val))) - { - n++; - sptr += orig->av_len; - } - if (!n) - return; - - dest = malloc(src->av_len + 1 + (repl->av_len - orig->av_len) * n); - - sptr = src->av_val; - dptr = dest; - while (sptr < srcend && (sptr = strstr(sptr, orig->av_val))) - { - n = sptr - srcbeg; - memcpy(dptr, srcbeg, n); - dptr += n; - memcpy(dptr, repl->av_val, repl->av_len); - dptr += repl->av_len; - sptr += orig->av_len; - srcbeg = sptr; - } - n = srcend - srcbeg; - memcpy(dptr, srcbeg, n); - dptr += n; - *dptr = '\0'; - src->av_val = dest; - src->av_len = dptr - dest; -} diff --git a/rtmp/rtmp_c/rtmpsuck.c b/rtmp/rtmp_c/rtmpsuck.c deleted file mode 100644 index e886179e..00000000 --- a/rtmp/rtmp_c/rtmpsuck.c +++ /dev/null @@ -1,1198 +0,0 @@ -/* RTMP Proxy Server - * Copyright (C) 2009 Andrej Stepanchuk - * 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, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - */ - -/* This is a Proxy Server that displays the connection parameters from a - * client and then saves any data streamed to the client. - */ - -#include -#include -#include -#include - -#include -#include - -#include - -#include "librtmp/rtmp_sys.h" -#include "librtmp/log.h" - -#include "thread.h" - -#ifdef linux -#include -#endif - -#define RD_SUCCESS 0 -#define RD_FAILED 1 -#define RD_INCOMPLETE 2 - -#define PACKET_SIZE 1024*1024 - -#ifdef WIN32 -#define InitSockets() {\ - WORD version; \ - WSADATA wsaData; \ - \ - version = MAKEWORD(1,1); \ - WSAStartup(version, &wsaData); } - -#define CleanupSockets() WSACleanup() -#else -#define InitSockets() -#define CleanupSockets() -#endif - -enum -{ - STREAMING_ACCEPTING, - STREAMING_IN_PROGRESS, - STREAMING_STOPPING, - STREAMING_STOPPED -}; - -typedef struct Flist -{ - struct Flist *f_next; - FILE *f_file; - AVal f_path; -} Flist; - -typedef struct Plist -{ - struct Plist *p_next; - RTMPPacket p_pkt; -} Plist; - -typedef struct -{ - int socket; - int state; - uint32_t stamp; - RTMP rs; - RTMP rc; - Plist *rs_pkt[2]; /* head, tail */ - Plist *rc_pkt[2]; /* head, tail */ - Flist *f_head, *f_tail; - Flist *f_cur; - -} STREAMING_SERVER; - -STREAMING_SERVER *rtmpServer = 0; // server structure pointer - -STREAMING_SERVER *startStreaming(const char *address, int port); -void stopStreaming(STREAMING_SERVER * server); - -#define STR2AVAL(av,str) av.av_val = str; av.av_len = strlen(av.av_val) - -#ifdef _DEBUG -uint32_t debugTS = 0; - -int pnum = 0; - -FILE *netstackdump = NULL; -FILE *netstackdump_read = NULL; -#endif - -#define BUFFERTIME (4*60*60*1000) /* 4 hours */ - -#define SAVC(x) static const AVal av_##x = AVC(#x) - -SAVC(app); -SAVC(connect); -SAVC(flashVer); -SAVC(swfUrl); -SAVC(pageUrl); -SAVC(tcUrl); -SAVC(fpad); -SAVC(capabilities); -SAVC(audioCodecs); -SAVC(videoCodecs); -SAVC(videoFunction); -SAVC(objectEncoding); -SAVC(_result); -SAVC(createStream); -SAVC(play); -SAVC(closeStream); -SAVC(fmsVer); -SAVC(mode); -SAVC(level); -SAVC(code); -SAVC(secureToken); -SAVC(onStatus); -SAVC(close); -static const AVal av_NetStream_Failed = AVC("NetStream.Failed"); -static const AVal av_NetStream_Play_Failed = AVC("NetStream.Play.Failed"); -static const AVal av_NetStream_Play_StreamNotFound = -AVC("NetStream.Play.StreamNotFound"); -static const AVal av_NetConnection_Connect_InvalidApp = -AVC("NetConnection.Connect.InvalidApp"); -static const AVal av_NetStream_Play_Start = AVC("NetStream.Play.Start"); -static const AVal av_NetStream_Play_Complete = AVC("NetStream.Play.Complete"); -static const AVal av_NetStream_Play_Stop = AVC("NetStream.Play.Stop"); - -static const char *cst[] = { "client", "server" }; - -// Returns 0 for OK/Failed/error, 1 for 'Stop or Complete' -int -ServeInvoke(STREAMING_SERVER *server, int which, RTMPPacket *pack, const char *body) -{ - int ret = 0, nRes; - int nBodySize = pack->m_nBodySize; - - if (body > pack->m_body) - nBodySize--; - - if (body[0] != 0x02) // make sure it is a string method name we start with - { - RTMP_Log(RTMP_LOGWARNING, "%s, Sanity failed. no string method in invoke packet", - __FUNCTION__); - return 0; - } - - AMFObject obj; - nRes = AMF_Decode(&obj, body, nBodySize, FALSE); - if (nRes < 0) - { - RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__); - return 0; - } - - AMF_Dump(&obj); - AVal method; - AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &method); - RTMP_Log(RTMP_LOGDEBUG, "%s, %s invoking <%s>", __FUNCTION__, cst[which], method.av_val); - - if (AVMATCH(&method, &av_connect)) - { - AMFObject cobj; - AVal pname, pval; - int i; - AMFProp_GetObject(AMF_GetProp(&obj, NULL, 2), &cobj); - RTMP_LogPrintf("Processing connect\n"); - for (i=0; irc.Link.app = pval; - pval.av_val = NULL; - } - else if (AVMATCH(&pname, &av_flashVer)) - { - server->rc.Link.flashVer = pval; - pval.av_val = NULL; - } - else if (AVMATCH(&pname, &av_swfUrl)) - { -#ifdef CRYPTO - if (pval.av_val) - RTMP_HashSWF(pval.av_val, &server->rc.Link.SWFSize, - (unsigned char *)server->rc.Link.SWFHash, 30); -#endif - server->rc.Link.swfUrl = pval; - pval.av_val = NULL; - } - else if (AVMATCH(&pname, &av_tcUrl)) - { - char *r1 = NULL, *r2; - int len; - - server->rc.Link.tcUrl = pval; - if ((pval.av_val[0] | 0x40) == 'r' && - (pval.av_val[1] | 0x40) == 't' && - (pval.av_val[2] | 0x40) == 'm' && - (pval.av_val[3] | 0x40) == 'p') - { - if (pval.av_val[4] == ':') - { - server->rc.Link.protocol = RTMP_PROTOCOL_RTMP; - r1 = pval.av_val+7; - } - else if ((pval.av_val[4] | 0x40) == 'e' && pval.av_val[5] == ':') - { - server->rc.Link.protocol = RTMP_PROTOCOL_RTMPE; - r1 = pval.av_val+8; - } - r2 = strchr(r1, '/'); - if (r2) - len = r2 - r1; - else - len = pval.av_len - (r1 - pval.av_val); - r2 = malloc(len+1); - memcpy(r2, r1, len); - r2[len] = '\0'; - server->rc.Link.hostname.av_val = r2; - r1 = strrchr(r2, ':'); - if (r1) - { - server->rc.Link.hostname.av_len = r1 - r2; - *r1++ = '\0'; - server->rc.Link.port = atoi(r1); - } - else - { - server->rc.Link.hostname.av_len = len; - server->rc.Link.port = 1935; - } - } - pval.av_val = NULL; - } - else if (AVMATCH(&pname, &av_pageUrl)) - { - server->rc.Link.pageUrl = pval; - pval.av_val = NULL; - } - else if (AVMATCH(&pname, &av_audioCodecs)) - { - server->rc.m_fAudioCodecs = cobj.o_props[i].p_vu.p_number; - } - else if (AVMATCH(&pname, &av_videoCodecs)) - { - server->rc.m_fVideoCodecs = cobj.o_props[i].p_vu.p_number; - } - else if (AVMATCH(&pname, &av_objectEncoding)) - { - server->rc.m_fEncoding = cobj.o_props[i].p_vu.p_number; - server->rc.m_bSendEncoding = TRUE; - } - /* Dup'd a string we didn't recognize? */ - if (pval.av_val) - free(pval.av_val); - } - if (obj.o_num > 3) - { - if (AMFProp_GetBoolean(&obj.o_props[3])) - server->rc.Link.lFlags |= RTMP_LF_AUTH; - if (obj.o_num > 4) - { - AMFProp_GetString(&obj.o_props[4], &server->rc.Link.auth); - } - } - - if (!RTMP_Connect(&server->rc, pack)) - { - /* failed */ - return 1; - } - server->rc.m_bSendCounter = FALSE; - } - else if (AVMATCH(&method, &av_play)) - { - Flist *fl; - AVal av; - FILE *out; - char *file, *p, *q; - char flvHeader[] = { 'F', 'L', 'V', 0x01, - 0x05, // video + audio, we finalize later if the value is different - 0x00, 0x00, 0x00, 0x09, - 0x00, 0x00, 0x00, 0x00 // first prevTagSize=0 - }; - int count = 0, flen; - - server->rc.m_stream_id = pack->m_nInfoField2; - AMFProp_GetString(AMF_GetProp(&obj, NULL, 3), &av); - server->rc.Link.playpath = av; - if (!av.av_val) - goto out; - - /* check for duplicates */ - for (fl = server->f_head; fl; fl=fl->f_next) - { - if (AVMATCH(&av, &fl->f_path)) - count++; - } - /* strip trailing URL parameters */ - q = memchr(av.av_val, '?', av.av_len); - if (q) - { - if (q == av.av_val) - { - av.av_val++; - av.av_len--; - } - else - { - av.av_len = q - av.av_val; - } - } - /* strip leading slash components */ - for (p=av.av_val+av.av_len-1; p>=av.av_val; p--) - if (*p == '/') - { - p++; - av.av_len -= p - av.av_val; - av.av_val = p; - break; - } - /* skip leading dot */ - if (av.av_val[0] == '.') - { - av.av_val++; - av.av_len--; - } - flen = av.av_len; - /* hope there aren't more than 255 dups */ - if (count) - flen += 2; - file = malloc(flen+1); - - memcpy(file, av.av_val, av.av_len); - if (count) - sprintf(file+av.av_len, "%02x", count); - else - file[av.av_len] = '\0'; - for (p=file; *p; p++) - if (*p == ':') - *p = '_'; - RTMP_LogPrintf("Playpath: %.*s\nSaving as: %s\n", - server->rc.Link.playpath.av_len, server->rc.Link.playpath.av_val, - file); - out = fopen(file, "wb"); - free(file); - if (!out) - ret = 1; - else - { - fwrite(flvHeader, 1, sizeof(flvHeader), out); - av = server->rc.Link.playpath; - fl = malloc(sizeof(Flist)+av.av_len+1); - fl->f_file = out; - fl->f_path.av_len = av.av_len; - fl->f_path.av_val = (char *)(fl+1); - memcpy(fl->f_path.av_val, av.av_val, av.av_len); - fl->f_path.av_val[av.av_len] = '\0'; - fl->f_next = NULL; - if (server->f_tail) - server->f_tail->f_next = fl; - else - server->f_head = fl; - server->f_tail = fl; - } - } - else if (AVMATCH(&method, &av_onStatus)) - { - AMFObject obj2; - AVal code, level; - AMFProp_GetObject(AMF_GetProp(&obj, NULL, 3), &obj2); - AMFProp_GetString(AMF_GetProp(&obj2, &av_code, -1), &code); - AMFProp_GetString(AMF_GetProp(&obj2, &av_level, -1), &level); - - RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val); - if (AVMATCH(&code, &av_NetStream_Failed) - || AVMATCH(&code, &av_NetStream_Play_Failed) - || AVMATCH(&code, &av_NetStream_Play_StreamNotFound) - || AVMATCH(&code, &av_NetConnection_Connect_InvalidApp)) - { - ret = 1; - } - - if (AVMATCH(&code, &av_NetStream_Play_Start)) - { - /* set up the next stream */ - if (server->f_cur) - { - if (server->f_cur->f_next) - server->f_cur = server->f_cur->f_next; - } - else - { - for (server->f_cur = server->f_head; server->f_cur && - !server->f_cur->f_file; server->f_cur = server->f_cur->f_next) ; - } - server->rc.m_bPlaying = TRUE; - } - - // Return 1 if this is a Play.Complete or Play.Stop - if (AVMATCH(&code, &av_NetStream_Play_Complete) - || AVMATCH(&code, &av_NetStream_Play_Stop)) - { - ret = 1; - } - } - else if (AVMATCH(&method, &av_closeStream)) - { - ret = 1; - } - else if (AVMATCH(&method, &av_close)) - { - RTMP_Close(&server->rc); - ret = 1; - } -out: - AMF_Reset(&obj); - return ret; -} - -int -ServePacket(STREAMING_SERVER *server, int which, RTMPPacket *packet) -{ - int ret = 0; - - RTMP_Log(RTMP_LOGDEBUG, "%s, %s sent packet type %02X, size %u bytes", __FUNCTION__, - cst[which], packet->m_packetType, packet->m_nBodySize); - - switch (packet->m_packetType) - { - case RTMP_PACKET_TYPE_CHUNK_SIZE: - // chunk size -// HandleChangeChunkSize(r, packet); - break; - - case RTMP_PACKET_TYPE_BYTES_READ_REPORT: - // bytes read report - break; - - case RTMP_PACKET_TYPE_CONTROL: - // ctrl -// HandleCtrl(r, packet); - break; - - case RTMP_PACKET_TYPE_SERVER_BW: - // server bw -// HandleServerBW(r, packet); - break; - - case RTMP_PACKET_TYPE_CLIENT_BW: - // client bw - // HandleClientBW(r, packet); - break; - - case RTMP_PACKET_TYPE_AUDIO: - // audio data - //RTMP_Log(RTMP_LOGDEBUG, "%s, received: audio %lu bytes", __FUNCTION__, packet.m_nBodySize); - break; - - case RTMP_PACKET_TYPE_VIDEO: - // video data - //RTMP_Log(RTMP_LOGDEBUG, "%s, received: video %lu bytes", __FUNCTION__, packet.m_nBodySize); - break; - - case RTMP_PACKET_TYPE_FLEX_STREAM_SEND: - // flex stream send - break; - - case RTMP_PACKET_TYPE_FLEX_SHARED_OBJECT: - // flex shared object - break; - - case RTMP_PACKET_TYPE_FLEX_MESSAGE: - // flex message - { - ret = ServeInvoke(server, which, packet, packet->m_body + 1); - break; - } - case RTMP_PACKET_TYPE_INFO: - // metadata (notify) - break; - - case RTMP_PACKET_TYPE_SHARED_OBJECT: - /* shared object */ - break; - - case RTMP_PACKET_TYPE_INVOKE: - // invoke - ret = ServeInvoke(server, which, packet, packet->m_body); - break; - - case RTMP_PACKET_TYPE_FLASH_VIDEO: - /* flv */ - break; - - default: - RTMP_Log(RTMP_LOGDEBUG, "%s, unknown packet type received: 0x%02x", __FUNCTION__, - packet->m_packetType); -#ifdef _DEBUG - RTMP_LogHex(RTMP_LOGDEBUG, packet->m_body, packet->m_nBodySize); -#endif - } - return ret; -} - -int -WriteStream(char **buf, // target pointer, maybe preallocated - unsigned int *plen, // length of buffer if preallocated - uint32_t *nTimeStamp, - RTMPPacket *packet) -{ - uint32_t prevTagSize = 0; - int ret = -1, len = *plen; - - while (1) - { - char *packetBody = packet->m_body; - unsigned int nPacketLen = packet->m_nBodySize; - - // skip video info/command packets - if (packet->m_packetType == RTMP_PACKET_TYPE_VIDEO && - nPacketLen == 2 && ((*packetBody & 0xf0) == 0x50)) - { - ret = 0; - break; - } - - if (packet->m_packetType == RTMP_PACKET_TYPE_VIDEO && nPacketLen <= 5) - { - RTMP_Log(RTMP_LOGWARNING, "ignoring too small video packet: size: %d", - nPacketLen); - ret = 0; - break; - } - if (packet->m_packetType == RTMP_PACKET_TYPE_AUDIO && nPacketLen <= 1) - { - RTMP_Log(RTMP_LOGWARNING, "ignoring too small audio packet: size: %d", - nPacketLen); - ret = 0; - break; - } -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, "type: %02X, size: %d, TS: %d ms", packet->m_packetType, - nPacketLen, packet->m_nTimeStamp); - if (packet->m_packetType == RTMP_PACKET_TYPE_VIDEO) - RTMP_Log(RTMP_LOGDEBUG, "frametype: %02X", (*packetBody & 0xf0)); -#endif - - // calculate packet size and reallocate buffer if necessary - unsigned int size = nPacketLen - + - ((packet->m_packetType == RTMP_PACKET_TYPE_AUDIO - || packet->m_packetType == RTMP_PACKET_TYPE_VIDEO - || packet->m_packetType == RTMP_PACKET_TYPE_INFO) ? 11 : 0) - + (packet->m_packetType != 0x16 ? 4 : 0); - - if (size + 4 > len) - { - /* The extra 4 is for the case of an FLV stream without a last - * prevTagSize (we need extra 4 bytes to append it). */ - *buf = (char *) realloc(*buf, size + 4); - if (*buf == 0) - { - RTMP_Log(RTMP_LOGERROR, "Couldn't reallocate memory!"); - ret = -1; // fatal error - break; - } - } - char *ptr = *buf, *pend = ptr + size+4; - - /* audio (RTMP_PACKET_TYPE_AUDIO), video (RTMP_PACKET_TYPE_VIDEO) - * or metadata (RTMP_PACKET_TYPE_INFO) packets: construct 11 byte - * header then add rtmp packet's data. */ - if (packet->m_packetType == RTMP_PACKET_TYPE_AUDIO - || packet->m_packetType == RTMP_PACKET_TYPE_VIDEO - || packet->m_packetType == RTMP_PACKET_TYPE_INFO) - { - // set data type - //*dataType |= (((packet->m_packetType == RTMP_PACKET_TYPE_AUDIO)<<2)|(packet->m_packetType == RTMP_PACKET_TYPE_VIDEO)); - - (*nTimeStamp) = packet->m_nTimeStamp; - prevTagSize = 11 + nPacketLen; - - *ptr++ = packet->m_packetType; - ptr = AMF_EncodeInt24(ptr, pend, nPacketLen); - ptr = AMF_EncodeInt24(ptr, pend, *nTimeStamp); - *ptr = (char) (((*nTimeStamp) & 0xFF000000) >> 24); - ptr++; - - // stream id - ptr = AMF_EncodeInt24(ptr, pend, 0); - } - - memcpy(ptr, packetBody, nPacketLen); - unsigned int len = nPacketLen; - - // correct tagSize and obtain timestamp if we have an FLV stream - if (packet->m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) - { - unsigned int pos = 0; - - while (pos + 11 < nPacketLen) - { - uint32_t dataSize = AMF_DecodeInt24(packetBody + pos + 1); // size without header (11) and without prevTagSize (4) - *nTimeStamp = AMF_DecodeInt24(packetBody + pos + 4); - *nTimeStamp |= (packetBody[pos + 7] << 24); - -#if 0 - /* set data type */ - *dataType |= (((*(packetBody+pos) == RTMP_PACKET_TYPE_AUDIO) << 2) - | (*(packetBody+pos) == RTMP_PACKET_TYPE_VIDEO)); -#endif - - if (pos + 11 + dataSize + 4 > nPacketLen) - { - if (pos + 11 + dataSize > nPacketLen) - { - RTMP_Log(RTMP_LOGERROR, - "Wrong data size (%u), stream corrupted, aborting!", - dataSize); - ret = -2; - break; - } - RTMP_Log(RTMP_LOGWARNING, "No tagSize found, appending!"); - - // we have to append a last tagSize! - prevTagSize = dataSize + 11; - AMF_EncodeInt32(ptr + pos + 11 + dataSize, pend, prevTagSize); - size += 4; - len += 4; - } - else - { - prevTagSize = - AMF_DecodeInt32(packetBody + pos + 11 + dataSize); - -#ifdef _DEBUG - RTMP_Log(RTMP_LOGDEBUG, - "FLV Packet: type %02X, dataSize: %lu, tagSize: %lu, timeStamp: %lu ms", - (unsigned char) packetBody[pos], dataSize, prevTagSize, - *nTimeStamp); -#endif - - if (prevTagSize != (dataSize + 11)) - { -#ifdef _DEBUG - RTMP_Log(RTMP_LOGWARNING, - "Tag and data size are not consitent, writing tag size according to dataSize+11: %d", - dataSize + 11); -#endif - - prevTagSize = dataSize + 11; - AMF_EncodeInt32(ptr + pos + 11 + dataSize, pend, prevTagSize); - } - } - - pos += prevTagSize + 4; //(11+dataSize+4); - } - } - ptr += len; - - if (packet->m_packetType != RTMP_PACKET_TYPE_FLASH_VIDEO) - { // FLV tag packets contain their own prevTagSize - AMF_EncodeInt32(ptr, pend, prevTagSize); - //ptr += 4; - } - - ret = size; - break; - } - - if (len > *plen) - *plen = len; - - return ret; // no more media packets -} - -TFTYPE -controlServerThread(void *unused) -{ - char ich; - while (1) - { - ich = getchar(); - switch (ich) - { - case 'q': - RTMP_LogPrintf("Exiting\n"); - stopStreaming(rtmpServer); - free(rtmpServer); - exit(0); - break; - default: - RTMP_LogPrintf("Unknown command \'%c\', ignoring\n", ich); - } - } - TFRET(); -} - -TFTYPE doServe(void *arg) // server socket and state (our listening socket) -{ - STREAMING_SERVER *server = arg; - RTMPPacket pc = { 0 }, ps = { 0 }; - RTMPChunk rk = { 0 }; - char *buf = NULL; - unsigned int buflen = 131072; - int paused = FALSE; - int sockfd = server->socket; - - // timeout for http requests - fd_set rfds; - struct timeval tv; - - server->state = STREAMING_IN_PROGRESS; - - memset(&tv, 0, sizeof(struct timeval)); - tv.tv_sec = 5; - - FD_ZERO(&rfds); - FD_SET(sockfd, &rfds); - - if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0) - { - RTMP_Log(RTMP_LOGERROR, "Request timeout/select failed, ignoring request"); - goto quit; - } - else - { - RTMP_Init(&server->rs); - RTMP_Init(&server->rc); - server->rs.m_sb.sb_socket = sockfd; - if (!RTMP_Serve(&server->rs)) - { - RTMP_Log(RTMP_LOGERROR, "Handshake failed"); - goto cleanup; - } - } - - buf = malloc(buflen); - - /* Just process the Connect request */ - while (RTMP_IsConnected(&server->rs) && RTMP_ReadPacket(&server->rs, &ps)) - { - if (!RTMPPacket_IsReady(&ps)) - continue; - ServePacket(server, 0, &ps); - RTMPPacket_Free(&ps); - if (RTMP_IsConnected(&server->rc)) - break; - } - - pc.m_chunk = &rk; - - /* We have our own timeout in select() */ - server->rc.Link.timeout = 10; - server->rs.Link.timeout = 10; - while (RTMP_IsConnected(&server->rs) || RTMP_IsConnected(&server->rc)) - { - int n; - int sr, cr; - - cr = server->rc.m_sb.sb_size; - sr = server->rs.m_sb.sb_size; - - if (cr || sr) - { - } - else - { - n = server->rs.m_sb.sb_socket; - if (server->rc.m_sb.sb_socket > n) - n = server->rc.m_sb.sb_socket; - FD_ZERO(&rfds); - if (RTMP_IsConnected(&server->rs)) - FD_SET(sockfd, &rfds); - if (RTMP_IsConnected(&server->rc)) - FD_SET(server->rc.m_sb.sb_socket, &rfds); - - /* give more time to start up if we're not playing yet */ - tv.tv_sec = server->f_cur ? 30 : 60; - tv.tv_usec = 0; - - if (select(n + 1, &rfds, NULL, NULL, &tv) <= 0) - { - if (server->f_cur && server->rc.m_mediaChannel && !paused) - { - server->rc.m_pauseStamp = server->rc.m_channelTimestamp[server->rc.m_mediaChannel]; - if (RTMP_ToggleStream(&server->rc)) - { - paused = TRUE; - continue; - } - } - RTMP_Log(RTMP_LOGERROR, "Request timeout/select failed, ignoring request"); - goto cleanup; - } - if (server->rs.m_sb.sb_socket > 0 && - FD_ISSET(server->rs.m_sb.sb_socket, &rfds)) - sr = 1; - if (server->rc.m_sb.sb_socket > 0 && - FD_ISSET(server->rc.m_sb.sb_socket, &rfds)) - cr = 1; - } - if (sr) - { - while (RTMP_ReadPacket(&server->rs, &ps)) - if (RTMPPacket_IsReady(&ps)) - { - /* change chunk size */ - if (ps.m_packetType == RTMP_PACKET_TYPE_CHUNK_SIZE) - { - if (ps.m_nBodySize >= 4) - { - server->rs.m_inChunkSize = AMF_DecodeInt32(ps.m_body); - RTMP_Log(RTMP_LOGDEBUG, "%s, client: chunk size change to %d", __FUNCTION__, - server->rs.m_inChunkSize); - server->rc.m_outChunkSize = server->rs.m_inChunkSize; - } - } - /* bytes received */ - else if (ps.m_packetType == RTMP_PACKET_TYPE_BYTES_READ_REPORT) - { - if (ps.m_nBodySize >= 4) - { - int count = AMF_DecodeInt32(ps.m_body); - RTMP_Log(RTMP_LOGDEBUG, "%s, client: bytes received = %d", __FUNCTION__, - count); - } - } - /* ctrl */ - else if (ps.m_packetType == RTMP_PACKET_TYPE_CONTROL) - { - short nType = AMF_DecodeInt16(ps.m_body); - /* UpdateBufferMS */ - if (nType == 0x03) - { - char *ptr = ps.m_body+2; - int id; - int len; - id = AMF_DecodeInt32(ptr); - /* Assume the interesting media is on a non-zero stream */ - if (id) - { - len = AMF_DecodeInt32(ptr+4); -#if 1 - /* request a big buffer */ - if (len < BUFFERTIME) - { - AMF_EncodeInt32(ptr+4, ptr+8, BUFFERTIME); - } -#endif - RTMP_Log(RTMP_LOGDEBUG, "%s, client: BufferTime change in stream %d to %d", __FUNCTION__, - id, len); - } - } - } - else if (ps.m_packetType == RTMP_PACKET_TYPE_FLEX_MESSAGE - || ps.m_packetType == RTMP_PACKET_TYPE_INVOKE) - { - if (ServePacket(server, 0, &ps) && server->f_cur) - { - fclose(server->f_cur->f_file); - server->f_cur->f_file = NULL; - server->f_cur = NULL; - } - } - RTMP_SendPacket(&server->rc, &ps, FALSE); - RTMPPacket_Free(&ps); - break; - } - } - if (cr) - { - while (RTMP_ReadPacket(&server->rc, &pc)) - { - int sendit = 1; - if (RTMPPacket_IsReady(&pc)) - { - if (paused) - { - if (pc.m_nTimeStamp <= server->rc.m_mediaStamp) - continue; - paused = 0; - server->rc.m_pausing = 0; - } - /* change chunk size */ - if (pc.m_packetType == RTMP_PACKET_TYPE_CHUNK_SIZE) - { - if (pc.m_nBodySize >= 4) - { - server->rc.m_inChunkSize = AMF_DecodeInt32(pc.m_body); - RTMP_Log(RTMP_LOGDEBUG, "%s, server: chunk size change to %d", __FUNCTION__, - server->rc.m_inChunkSize); - server->rs.m_outChunkSize = server->rc.m_inChunkSize; - } - } - else if (pc.m_packetType == RTMP_PACKET_TYPE_CONTROL) - { - short nType = AMF_DecodeInt16(pc.m_body); - /* SWFverification */ - if (nType == 0x1a) -#ifdef CRYPTO - if (server->rc.Link.SWFSize) - { - RTMP_SendCtrl(&server->rc, 0x1b, 0, 0); - sendit = 0; - } -#else - /* The session will certainly fail right after this */ - RTMP_Log(RTMP_LOGERROR, "%s, server requested SWF verification, need CRYPTO support! ", __FUNCTION__); -#endif - } - else if (server->f_cur && ( - pc.m_packetType == RTMP_PACKET_TYPE_AUDIO || - pc.m_packetType == RTMP_PACKET_TYPE_VIDEO || - pc.m_packetType == RTMP_PACKET_TYPE_INFO || - pc.m_packetType == RTMP_PACKET_TYPE_FLASH_VIDEO) && - RTMP_ClientPacket(&server->rc, &pc)) - { - int len = WriteStream(&buf, &buflen, &server->stamp, &pc); - if (len > 0 && fwrite(buf, 1, len, server->f_cur->f_file) != len) - goto cleanup; - } - else if (pc.m_packetType == RTMP_PACKET_TYPE_FLEX_MESSAGE || - pc.m_packetType == RTMP_PACKET_TYPE_INVOKE) - { - if (ServePacket(server, 1, &pc) && server->f_cur) - { - fclose(server->f_cur->f_file); - server->f_cur->f_file = NULL; - server->f_cur = NULL; - } - } - } - if (sendit && RTMP_IsConnected(&server->rs)) - RTMP_SendChunk(&server->rs, &rk); - if (RTMPPacket_IsReady(&pc)) - RTMPPacket_Free(&pc); - break; - } - } - if (!RTMP_IsConnected(&server->rs) && RTMP_IsConnected(&server->rc) - && !server->f_cur) - RTMP_Close(&server->rc); - } - -cleanup: - RTMP_LogPrintf("Closing connection... "); - RTMP_Close(&server->rs); - RTMP_Close(&server->rc); - while (server->f_head) - { - Flist *fl = server->f_head; - server->f_head = fl->f_next; - if (fl->f_file) - fclose(fl->f_file); - free(fl); - } - server->f_tail = NULL; - server->f_cur = NULL; - free(buf); - /* Should probably be done by RTMP_Close() ... */ - server->rc.Link.hostname.av_val = NULL; - server->rc.Link.tcUrl.av_val = NULL; - server->rc.Link.swfUrl.av_val = NULL; - server->rc.Link.pageUrl.av_val = NULL; - server->rc.Link.app.av_val = NULL; - server->rc.Link.auth.av_val = NULL; - server->rc.Link.flashVer.av_val = NULL; - RTMP_LogPrintf("done!\n\n"); - -quit: - if (server->state == STREAMING_IN_PROGRESS) - server->state = STREAMING_ACCEPTING; - - TFRET(); -} - -TFTYPE -serverThread(void *arg) -{ - STREAMING_SERVER *server = arg; - server->state = STREAMING_ACCEPTING; - - while (server->state == STREAMING_ACCEPTING) - { - struct sockaddr_in addr; - socklen_t addrlen = sizeof(struct sockaddr_in); - STREAMING_SERVER *srv2 = malloc(sizeof(STREAMING_SERVER)); - int sockfd = - accept(server->socket, (struct sockaddr *) &addr, &addrlen); - - if (sockfd > 0) - { -#ifdef linux - struct sockaddr_in dest; - char destch[16]; - socklen_t destlen = sizeof(struct sockaddr_in); - getsockopt(sockfd, SOL_IP, SO_ORIGINAL_DST, &dest, &destlen); - strcpy(destch, inet_ntoa(dest.sin_addr)); - RTMP_Log(RTMP_LOGDEBUG, "%s: accepted connection from %s to %s\n", __FUNCTION__, - inet_ntoa(addr.sin_addr), destch); -#else - RTMP_Log(RTMP_LOGDEBUG, "%s: accepted connection from %s\n", __FUNCTION__, - inet_ntoa(addr.sin_addr)); -#endif - *srv2 = *server; - srv2->socket = sockfd; - /* Create a new thread and transfer the control to that */ - ThreadCreate(doServe, srv2); - RTMP_Log(RTMP_LOGDEBUG, "%s: processed request\n", __FUNCTION__); - } - else - { - RTMP_Log(RTMP_LOGERROR, "%s: accept failed", __FUNCTION__); - } - } - server->state = STREAMING_STOPPED; - TFRET(); -} - -STREAMING_SERVER * -startStreaming(const char *address, int port) -{ - struct sockaddr_in addr; - int sockfd, tmp; - STREAMING_SERVER *server; - - sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, couldn't create socket", __FUNCTION__); - return 0; - } - - tmp = 1; - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, - (char *) &tmp, sizeof(tmp) ); - - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(address); //htonl(INADDR_ANY); - addr.sin_port = htons(port); - - if (bind(sockfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) == - -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, TCP bind failed for port number: %d", __FUNCTION__, - port); - return 0; - } - - if (listen(sockfd, 10) == -1) - { - RTMP_Log(RTMP_LOGERROR, "%s, listen failed", __FUNCTION__); - closesocket(sockfd); - return 0; - } - - server = (STREAMING_SERVER *) calloc(1, sizeof(STREAMING_SERVER)); - server->socket = sockfd; - - ThreadCreate(serverThread, server); - - return server; -} - -void -stopStreaming(STREAMING_SERVER * server) -{ - assert(server); - - if (server->state != STREAMING_STOPPED) - { - int fd = server->socket; - server->socket = 0; - if (server->state == STREAMING_IN_PROGRESS) - { - server->state = STREAMING_STOPPING; - - // wait for streaming threads to exit - while (server->state != STREAMING_STOPPED) - msleep(1); - } - - if (fd && closesocket(fd)) - RTMP_Log(RTMP_LOGERROR, "%s: Failed to close listening socket, error %d", - __FUNCTION__, GetSockError()); - - server->state = STREAMING_STOPPED; - } -} - - -void -sigIntHandler(int sig) -{ - RTMP_ctrlC = TRUE; - RTMP_LogPrintf("Caught signal: %d, cleaning up, just a second...\n", sig); - if (rtmpServer) - stopStreaming(rtmpServer); - signal(SIGINT, SIG_DFL); -} - -int -main(int argc, char **argv) -{ - int nStatus = RD_SUCCESS; - - // rtmp streaming server - char DEFAULT_RTMP_STREAMING_DEVICE[] = "0.0.0.0"; // 0.0.0.0 is any device - - char *rtmpStreamingDevice = DEFAULT_RTMP_STREAMING_DEVICE; // streaming device, default 0.0.0.0 - int nRtmpStreamingPort = 1935; // port - - RTMP_LogPrintf("RTMP Proxy Server %s\n", RTMPDUMP_VERSION); - RTMP_LogPrintf("(c) 2010 Andrej Stepanchuk, Howard Chu; license: GPL\n\n"); - - RTMP_debuglevel = RTMP_LOGINFO; - - if (argc > 1 && !strcmp(argv[1], "-z")) - RTMP_debuglevel = RTMP_LOGALL; - - signal(SIGINT, sigIntHandler); -#ifndef WIN32 - signal(SIGPIPE, SIG_IGN); -#endif - -#ifdef _DEBUG - netstackdump = fopen("netstackdump", "wb"); - netstackdump_read = fopen("netstackdump_read", "wb"); -#endif - - InitSockets(); - - // start text UI - ThreadCreate(controlServerThread, 0); - - // start http streaming - if ((rtmpServer = - startStreaming(rtmpStreamingDevice, nRtmpStreamingPort)) == 0) - { - RTMP_Log(RTMP_LOGERROR, "Failed to start RTMP server, exiting!"); - return RD_FAILED; - } - RTMP_LogPrintf("Streaming on rtmp://%s:%d\n", rtmpStreamingDevice, - nRtmpStreamingPort); - - while (rtmpServer->state != STREAMING_STOPPED) - { - sleep(1); - } - RTMP_Log(RTMP_LOGDEBUG, "Done, exiting..."); - - free(rtmpServer); - - CleanupSockets(); - -#ifdef _DEBUG - if (netstackdump != 0) - fclose(netstackdump); - if (netstackdump_read != 0) - fclose(netstackdump_read); -#endif - return nStatus; -} diff --git a/rtmp/rtmp_c/thread.c b/rtmp/rtmp_c/thread.c deleted file mode 100644 index 0913c987..00000000 --- a/rtmp/rtmp_c/thread.c +++ /dev/null @@ -1,58 +0,0 @@ -/* 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, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - */ - -#include "thread.h" -#include "librtmp/log.h" - -#ifdef WIN32 - -#include - -HANDLE -ThreadCreate(thrfunc *routine, void *args) -{ - HANDLE thd; - - thd = (HANDLE) _beginthread(routine, 0, args); - if (thd == -1L) - RTMP_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) - RTMP_LogPrintf("%s, pthread_create failed with %d\n", __FUNCTION__, ret); - - return id; -} -#endif diff --git a/rtmp/rtmp_c/thread.h b/rtmp/rtmp_c/thread.h deleted file mode 100644 index e8deae65..00000000 --- a/rtmp/rtmp_c/thread.h +++ /dev/null @@ -1,40 +0,0 @@ -/* 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, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * http://www.gnu.org/copyleft/gpl.html - * - */ - -#ifndef __THREAD_H__ -#define __THREAD_H__ 1 - -#ifdef WIN32 -#include -#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__ */