2 * Copyright (C) 2005-2008 Team XBMC
4 * Copyright (C) 2008-2009 Andrej Stepanchuk
5 * Copyright (C) 2009-2010 Howard Chu
7 * This file is part of librtmp.
9 * librtmp is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation; either version 2.1,
12 * or (at your option) any later version.
14 * librtmp is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with librtmp see the file COPYING. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/lgpl.html
37 #include <polarssl/havege.h>
38 #include <polarssl/md5.h>
39 #include <polarssl/base64.h>
40 #define MD5_DIGEST_LENGTH 16
42 static const char *my_dhm_P
=
43 "E4004C1F94182000103D883A448B3F80" \
44 "2CE4B44A83301270002C20D0321CFD00" \
45 "11CCEF784C26A400F43DFB901BCA7538" \
46 "F2C6B176001CF5A0FD16D2C48B1D0C1C" \
47 "F6AC8E1DA6BCC3B4E1F96B0564965300" \
48 "FFA1D0B601EB2800F489AA512C4B248C" \
49 "01F76949A60BB7F00A40B1EAB64BDD48" \
50 "E8A700D60B7F1200FA8E77B0A979DABF";
52 static const char *my_dhm_G
= "4";
54 #elif defined(USE_GNUTLS)
55 #include <gnutls/gnutls.h>
56 #define MD5_DIGEST_LENGTH 16
57 #include <nettle/base64.h>
58 #include <nettle/md5.h>
59 #else /* USE_OPENSSL */
60 #include <openssl/ssl.h>
61 #include <openssl/rc4.h>
62 #include <openssl/md5.h>
63 #include <openssl/bio.h>
64 #include <openssl/buffer.h>
69 #define RTMP_SIG_SIZE 1536
70 #define RTMP_LARGE_HEADER_SIZE 12
72 static const int packetSize
[] = { 12, 8, 4, 1 };
76 const char RTMPProtocolStrings
[][7] = {
88 const char RTMPProtocolStringsLower
[][7] = {
100 static const char *RTMPT_cmds
[] = {
108 RTMPT_OPEN
=0, RTMPT_SEND
, RTMPT_IDLE
, RTMPT_CLOSE
111 static int DumpMetaData(AMFObject
*obj
);
112 static int HandShake(RTMP
*r
, int FP9HandShake
);
113 static int SocksNegotiate(RTMP
*r
);
115 static int SendConnectPacket(RTMP
*r
, RTMPPacket
*cp
);
116 static int SendCheckBW(RTMP
*r
);
117 static int SendCheckBWResult(RTMP
*r
, double txn
);
118 static int SendDeleteStream(RTMP
*r
, double dStreamId
);
119 static int SendFCSubscribe(RTMP
*r
, AVal
*subscribepath
);
120 static int SendPlay(RTMP
*r
);
121 static int SendBytesReceived(RTMP
*r
);
122 static int SendUsherToken(RTMP
*r
, AVal
*usherToken
);
125 static int SendBGHasStream(RTMP
*r
, double dId
, AVal
*playpath
);
128 static int HandleInvoke(RTMP
*r
, const char *body
, unsigned int nBodySize
);
129 static int HandleMetadata(RTMP
*r
, char *body
, unsigned int len
);
130 static void HandleChangeChunkSize(RTMP
*r
, const RTMPPacket
*packet
);
131 static void HandleAudio(RTMP
*r
, const RTMPPacket
*packet
);
132 static void HandleVideo(RTMP
*r
, const RTMPPacket
*packet
);
133 static void HandleCtrl(RTMP
*r
, const RTMPPacket
*packet
);
134 static void HandleServerBW(RTMP
*r
, const RTMPPacket
*packet
);
135 static void HandleClientBW(RTMP
*r
, const RTMPPacket
*packet
);
137 static int ReadN(RTMP
*r
, char *buffer
, int n
);
138 static int WriteN(RTMP
*r
, const char *buffer
, int n
);
140 static void DecodeTEA(AVal
*key
, AVal
*text
);
142 static int HTTP_Post(RTMP
*r
, RTMPTCmd cmd
, const char *buf
, int len
);
143 static int HTTP_read(RTMP
*r
, int fill
);
150 #include "handshake.h"
158 #elif defined(_WIN32)
159 return timeGetTime();
162 if (!clk_tck
) clk_tck
= sysconf(_SC_CLK_TCK
);
163 return times(&t
) * 1000 / clk_tck
;
174 RTMPPacket_Reset(RTMPPacket
*p
)
180 p
->m_nInfoField2
= 0;
181 p
->m_hasAbsTimestamp
= FALSE
;
187 RTMPPacket_Alloc(RTMPPacket
*p
, int nSize
)
189 char *ptr
= calloc(1, nSize
+ RTMP_MAX_HEADER_SIZE
);
192 p
->m_body
= ptr
+ RTMP_MAX_HEADER_SIZE
;
198 RTMPPacket_Free(RTMPPacket
*p
)
202 free(p
->m_body
- RTMP_MAX_HEADER_SIZE
);
208 RTMPPacket_Dump(RTMPPacket
*p
)
210 RTMP_Log(RTMP_LOGDEBUG
,
211 "RTMP PACKET: packet type: 0x%02x. channel: 0x%02x. info 1: %d info 2: %d. Body size: %u. body: 0x%02x",
212 p
->m_packetType
, p
->m_nChannel
, p
->m_nTimeStamp
, p
->m_nInfoField2
,
213 p
->m_nBodySize
, p
->m_body
? (unsigned char)p
->m_body
[0] : 0);
219 return RTMP_LIB_VERSION
;
227 /* Do this regardless of NO_SSL, we use havege for rtmpe too */
228 RTMP_TLS_ctx
= calloc(1,sizeof(struct tls_ctx
));
229 havege_init(&RTMP_TLS_ctx
->hs
);
230 #elif defined(USE_GNUTLS) && !defined(NO_SSL)
231 /* Technically we need to initialize libgcrypt ourselves if
232 * we're not going to call gnutls_global_init(). Ignoring this
235 gnutls_global_init();
236 RTMP_TLS_ctx
= malloc(sizeof(struct tls_ctx
));
237 gnutls_certificate_allocate_credentials(&RTMP_TLS_ctx
->cred
);
238 gnutls_priority_init(&RTMP_TLS_ctx
->prios
, "NORMAL", NULL
);
239 gnutls_certificate_set_x509_trust_file(RTMP_TLS_ctx
->cred
,
240 "ca.pem", GNUTLS_X509_FMT_PEM
);
241 #elif !defined(NO_SSL) /* USE_OPENSSL */
242 /* libcrypto doesn't need anything special */
243 SSL_load_error_strings();
245 OpenSSL_add_all_digests();
246 RTMP_TLS_ctx
= SSL_CTX_new(SSLv23_method());
247 SSL_CTX_set_options(RTMP_TLS_ctx
, SSL_OP_ALL
);
248 SSL_CTX_set_default_verify_paths(RTMP_TLS_ctx
);
254 RTMP_TLS_AllocServerContext(const char* cert
, const char* key
)
261 tls_server_ctx
*tc
= ctx
= calloc(1, sizeof(struct tls_server_ctx
));
262 tc
->dhm_P
= my_dhm_P
;
263 tc
->dhm_G
= my_dhm_G
;
264 tc
->hs
= &RTMP_TLS_ctx
->hs
;
265 if (x509parse_crtfile(&tc
->cert
, cert
)) {
269 if (x509parse_keyfile(&tc
->key
, key
, NULL
)) {
270 x509_free(&tc
->cert
);
274 #elif defined(USE_GNUTLS) && !defined(NO_SSL)
275 gnutls_certificate_allocate_credentials((gnutls_certificate_credentials
*) &ctx
);
276 if (gnutls_certificate_set_x509_key_file(ctx
, cert
, key
, GNUTLS_X509_FMT_PEM
) != 0) {
277 gnutls_certificate_free_credentials(ctx
);
280 #elif !defined(NO_SSL) /* USE_OPENSSL */
281 ctx
= SSL_CTX_new(SSLv23_server_method());
282 if (!SSL_CTX_use_certificate_chain_file(ctx
, cert
)) {
286 if (!SSL_CTX_use_PrivateKey_file(ctx
, key
, SSL_FILETYPE_PEM
)) {
296 RTMP_TLS_FreeServerContext(void *ctx
)
300 x509_free(&((tls_server_ctx
*)ctx
)->cert
);
301 rsa_free(&((tls_server_ctx
*)ctx
)->key
);
303 #elif defined(USE_GNUTLS) && !defined(NO_SSL)
304 gnutls_certificate_free_credentials(ctx
);
305 #elif !defined(NO_SSL) /* USE_OPENSSL */
314 return calloc(1, sizeof(RTMP
));
331 memset(r
, 0, sizeof(RTMP
));
332 r
->m_sb
.sb_socket
= -1;
333 r
->m_inChunkSize
= RTMP_DEFAULT_CHUNKSIZE
;
334 r
->m_outChunkSize
= RTMP_DEFAULT_CHUNKSIZE
;
335 r
->m_nBufferMS
= 30000;
336 r
->m_nClientBW
= 2500000;
338 r
->m_nServerBW
= 2500000;
339 r
->m_fAudioCodecs
= 3191.0;
340 r
->m_fVideoCodecs
= 252.0;
341 r
->Link
.timeout
= 30;
346 RTMP_EnableWrite(RTMP
*r
)
348 r
->Link
.protocol
|= RTMP_FEATURE_WRITE
;
352 RTMP_GetDuration(RTMP
*r
)
354 return r
->m_fDuration
;
358 RTMP_IsConnected(RTMP
*r
)
360 return r
->m_sb
.sb_socket
!= -1;
366 return r
->m_sb
.sb_socket
;
370 RTMP_IsTimedout(RTMP
*r
)
372 return r
->m_sb
.sb_timedout
;
376 RTMP_SetBufferMS(RTMP
*r
, int size
)
378 r
->m_nBufferMS
= size
;
382 RTMP_UpdateBufferMS(RTMP
*r
)
384 RTMP_SendCtrl(r
, 3, r
->m_stream_id
, r
->m_nBufferMS
);
390 #elif defined(__sun__)
392 #elif defined(__APPLE__)
394 #elif defined(__linux__)
399 #define DEF_VERSTR OSS " 10,0,32,18"
400 static const char DEFAULT_FLASH_VER
[] = DEF_VERSTR
;
401 const AVal RTMP_DefaultFlashVer
=
402 { (char *)DEFAULT_FLASH_VER
, sizeof(DEFAULT_FLASH_VER
) - 1 };
405 SocksSetup(RTMP
*r
, AVal
*sockshost
)
407 if (sockshost
->av_len
)
409 const char *socksport
= strchr(sockshost
->av_val
, ':');
410 char *hostname
= strdup(sockshost
->av_val
);
413 hostname
[socksport
- sockshost
->av_val
] = '\0';
414 r
->Link
.sockshost
.av_val
= hostname
;
415 r
->Link
.sockshost
.av_len
= strlen(hostname
);
417 r
->Link
.socksport
= socksport
? atoi(socksport
+ 1) : 1080;
418 RTMP_Log(RTMP_LOGDEBUG
, "Connecting via SOCKS proxy: %s:%d", r
->Link
.sockshost
.av_val
,
423 r
->Link
.sockshost
.av_val
= NULL
;
424 r
->Link
.sockshost
.av_len
= 0;
425 r
->Link
.socksport
= 0;
430 RTMP_SetupStream(RTMP
*r
,
447 int dStop
, int bLiveStream
, long int timeout
)
449 RTMP_Log(RTMP_LOGDEBUG
, "Protocol : %s", RTMPProtocolStrings
[protocol
&7]);
450 RTMP_Log(RTMP_LOGDEBUG
, "Hostname : %.*s", host
->av_len
, host
->av_val
);
451 RTMP_Log(RTMP_LOGDEBUG
, "Port : %d", port
);
452 RTMP_Log(RTMP_LOGDEBUG
, "Playpath : %s", playpath
->av_val
);
454 if (tcUrl
&& tcUrl
->av_val
)
455 RTMP_Log(RTMP_LOGDEBUG
, "tcUrl : %s", tcUrl
->av_val
);
456 if (swfUrl
&& swfUrl
->av_val
)
457 RTMP_Log(RTMP_LOGDEBUG
, "swfUrl : %s", swfUrl
->av_val
);
458 if (pageUrl
&& pageUrl
->av_val
)
459 RTMP_Log(RTMP_LOGDEBUG
, "pageUrl : %s", pageUrl
->av_val
);
460 if (app
&& app
->av_val
)
461 RTMP_Log(RTMP_LOGDEBUG
, "app : %.*s", app
->av_len
, app
->av_val
);
462 if (auth
&& auth
->av_val
)
463 RTMP_Log(RTMP_LOGDEBUG
, "auth : %s", auth
->av_val
);
464 if (subscribepath
&& subscribepath
->av_val
)
465 RTMP_Log(RTMP_LOGDEBUG
, "subscribepath : %s", subscribepath
->av_val
);
466 if (usherToken
&& usherToken
->av_val
)
467 RTMP_Log(RTMP_LOGDEBUG
, "NetStream.Authenticate.UsherToken : %s", usherToken
->av_val
);
468 if (flashVer
&& flashVer
->av_val
)
469 RTMP_Log(RTMP_LOGDEBUG
, "flashVer : %s", flashVer
->av_val
);
471 RTMP_Log(RTMP_LOGDEBUG
, "StartTime : %d msec", dStart
);
473 RTMP_Log(RTMP_LOGDEBUG
, "StopTime : %d msec", dStop
);
475 RTMP_Log(RTMP_LOGDEBUG
, "live : %s", bLiveStream
? "yes" : "no");
476 RTMP_Log(RTMP_LOGDEBUG
, "timeout : %ld sec", timeout
);
479 if (swfSHA256Hash
!= NULL
&& swfSize
> 0)
481 memcpy(r
->Link
.SWFHash
, swfSHA256Hash
->av_val
, sizeof(r
->Link
.SWFHash
));
482 r
->Link
.SWFSize
= swfSize
;
483 RTMP_Log(RTMP_LOGDEBUG
, "SWFSHA256:");
484 RTMP_LogHex(RTMP_LOGDEBUG
, r
->Link
.SWFHash
, sizeof(r
->Link
.SWFHash
));
485 RTMP_Log(RTMP_LOGDEBUG
, "SWFSize : %u", r
->Link
.SWFSize
);
493 SocksSetup(r
, sockshost
);
495 if (tcUrl
&& tcUrl
->av_len
)
496 r
->Link
.tcUrl
= *tcUrl
;
497 if (swfUrl
&& swfUrl
->av_len
)
498 r
->Link
.swfUrl
= *swfUrl
;
499 if (pageUrl
&& pageUrl
->av_len
)
500 r
->Link
.pageUrl
= *pageUrl
;
501 if (app
&& app
->av_len
)
503 if (auth
&& auth
->av_len
)
505 r
->Link
.auth
= *auth
;
506 r
->Link
.lFlags
|= RTMP_LF_AUTH
;
508 if (flashVer
&& flashVer
->av_len
)
509 r
->Link
.flashVer
= *flashVer
;
511 r
->Link
.flashVer
= RTMP_DefaultFlashVer
;
512 if (subscribepath
&& subscribepath
->av_len
)
513 r
->Link
.subscribepath
= *subscribepath
;
514 if (usherToken
&& usherToken
->av_len
)
515 r
->Link
.usherToken
= *usherToken
;
516 r
->Link
.seekTime
= dStart
;
517 r
->Link
.stopTime
= dStop
;
519 r
->Link
.lFlags
|= RTMP_LF_LIVE
;
520 r
->Link
.timeout
= timeout
;
522 r
->Link
.protocol
= protocol
;
523 r
->Link
.hostname
= *host
;
525 r
->Link
.playpath
= *playpath
;
527 if (r
->Link
.port
== 0)
529 if (protocol
& RTMP_FEATURE_SSL
)
531 else if (protocol
& RTMP_FEATURE_HTTP
)
538 enum { OPT_STR
=0, OPT_INT
, OPT_BOOL
, OPT_CONN
};
539 static const char *optinfo
[] = {
540 "string", "integer", "boolean", "AMF" };
542 #define OFF(x) offsetof(struct RTMP,x)
544 static struct urlopt
{
551 { AVC("socks"), OFF(Link
.sockshost
), OPT_STR
, 0,
552 "Use the specified SOCKS proxy" },
553 { AVC("app"), OFF(Link
.app
), OPT_STR
, 0,
554 "Name of target app on server" },
555 { AVC("tcUrl"), OFF(Link
.tcUrl
), OPT_STR
, 0,
556 "URL to played stream" },
557 { AVC("pageUrl"), OFF(Link
.pageUrl
), OPT_STR
, 0,
558 "URL of played media's web page" },
559 { AVC("swfUrl"), OFF(Link
.swfUrl
), OPT_STR
, 0,
560 "URL to player SWF file" },
561 { AVC("flashver"), OFF(Link
.flashVer
), OPT_STR
, 0,
562 "Flash version string (default " DEF_VERSTR
")" },
563 { AVC("conn"), OFF(Link
.extras
), OPT_CONN
, 0,
564 "Append arbitrary AMF data to Connect message" },
565 { AVC("playpath"), OFF(Link
.playpath
), OPT_STR
, 0,
566 "Path to target media on server" },
567 { AVC("playlist"), OFF(Link
.lFlags
), OPT_BOOL
, RTMP_LF_PLST
,
568 "Set playlist before play command" },
569 { AVC("live"), OFF(Link
.lFlags
), OPT_BOOL
, RTMP_LF_LIVE
,
570 "Stream is live, no seeking possible" },
571 { AVC("subscribe"), OFF(Link
.subscribepath
), OPT_STR
, 0,
572 "Stream to subscribe to" },
573 { AVC("jtv"), OFF(Link
.usherToken
), OPT_STR
, 0,
574 "Justin.tv authentication token" },
575 { AVC("token"), OFF(Link
.token
), OPT_STR
, 0,
576 "Key for SecureToken response" },
577 { AVC("swfVfy"), OFF(Link
.lFlags
), OPT_BOOL
, RTMP_LF_SWFV
,
578 "Perform SWF Verification" },
579 { AVC("swfAge"), OFF(Link
.swfAge
), OPT_INT
, 0,
580 "Number of days to use cached SWF hash" },
581 { AVC("start"), OFF(Link
.seekTime
), OPT_INT
, 0,
582 "Stream start position in milliseconds" },
583 { AVC("stop"), OFF(Link
.stopTime
), OPT_INT
, 0,
584 "Stream stop position in milliseconds" },
585 { AVC("buffer"), OFF(m_nBufferMS
), OPT_INT
, 0,
586 "Buffer time in milliseconds" },
587 { AVC("timeout"), OFF(Link
.timeout
), OPT_INT
, 0,
588 "Session timeout in seconds" },
589 { AVC("pubUser"), OFF(Link
.pubUser
), OPT_STR
, 0,
590 "Publisher username" },
591 { AVC("pubPasswd"), OFF(Link
.pubPasswd
), OPT_STR
, 0,
592 "Publisher password" },
596 static const AVal truth
[] = {
604 static void RTMP_OptUsage()
608 RTMP_Log(RTMP_LOGERROR
, "Valid RTMP options are:\n");
609 for (i
=0; options
[i
].name
.av_len
; i
++) {
610 RTMP_Log(RTMP_LOGERROR
, "%10s %-7s %s\n", options
[i
].name
.av_val
,
611 optinfo
[options
[i
].otype
], options
[i
].use
);
616 parseAMF(AMFObject
*obj
, AVal
*av
, int *depth
)
618 AMFObjectProperty prop
= {{0,0}};
620 char *p
, *arg
= av
->av_val
;
628 prop
.p_type
= AMF_BOOLEAN
;
629 prop
.p_vu
.p_number
= atoi(p
);
632 prop
.p_type
= AMF_STRING
;
633 prop
.p_vu
.p_aval
.av_val
= p
;
634 prop
.p_vu
.p_aval
.av_len
= av
->av_len
- (p
-arg
);
637 prop
.p_type
= AMF_NUMBER
;
638 prop
.p_vu
.p_number
= strtod(p
, NULL
);
641 prop
.p_type
= AMF_NULL
;
647 prop
.p_type
= AMF_OBJECT
;
659 else if (arg
[2] == ':' && arg
[0] == 'N')
661 p
= strchr(arg
+3, ':');
664 prop
.p_name
.av_val
= (char *)arg
+3;
665 prop
.p_name
.av_len
= p
- (arg
+3);
671 prop
.p_type
= AMF_BOOLEAN
;
672 prop
.p_vu
.p_number
= atoi(p
);
675 prop
.p_type
= AMF_STRING
;
676 prop
.p_vu
.p_aval
.av_val
= p
;
677 prop
.p_vu
.p_aval
.av_len
= av
->av_len
- (p
-arg
);
680 prop
.p_type
= AMF_NUMBER
;
681 prop
.p_vu
.p_number
= strtod(p
, NULL
);
684 prop
.p_type
= AMF_OBJECT
;
696 for (i
=0; i
<*depth
; i
++)
698 o2
= &obj
->o_props
[obj
->o_num
-1].p_vu
.p_object
;
702 AMF_AddProp(obj
, &prop
);
703 if (prop
.p_type
== AMF_OBJECT
)
708 int RTMP_SetOpt(RTMP
*r
, const AVal
*opt
, AVal
*arg
)
713 for (i
=0; options
[i
].name
.av_len
; i
++) {
714 if (opt
->av_len
!= options
[i
].name
.av_len
) continue;
715 if (strcasecmp(opt
->av_val
, options
[i
].name
.av_val
)) continue;
716 v
= (char *)r
+ options
[i
].off
;
717 switch(options
[i
].otype
) {
723 long l
= strtol(arg
->av_val
, NULL
, 0);
729 for (j
=0; truth
[j
].av_len
; j
++) {
730 if (arg
->av_len
!= truth
[j
].av_len
) continue;
731 if (strcasecmp(arg
->av_val
, truth
[j
].av_val
)) continue;
732 fl
|= options
[i
].omisc
; break; }
737 if (parseAMF(&r
->Link
.extras
, arg
, &r
->Link
.edepth
))
743 if (!options
[i
].name
.av_len
) {
744 RTMP_Log(RTMP_LOGERROR
, "Unknown option %s", opt
->av_val
);
752 int RTMP_SetupURL(RTMP
*r
, char *url
)
755 char *p1
, *p2
, *ptr
= strchr(url
, ' ');
757 unsigned int port
= 0;
763 ret
= RTMP_ParseURL(url
, &r
->Link
.protocol
, &r
->Link
.hostname
,
764 &port
, &r
->Link
.playpath0
, &r
->Link
.app
);
768 r
->Link
.playpath
= r
->Link
.playpath0
;
773 p2
= strchr(p1
, '=');
777 opt
.av_len
= p2
- p1
;
780 ptr
= strchr(p2
, ' ');
783 arg
.av_len
= ptr
- p2
;
784 /* skip repeated spaces */
788 arg
.av_len
= strlen(p2
);
793 for (p1
=p2
; port
>0;) {
798 sscanf(p1
+1, "%02x", &c
);
807 arg
.av_len
= p2
- arg
.av_val
;
809 ret
= RTMP_SetOpt(r
, &opt
, &arg
);
814 if (!r
->Link
.tcUrl
.av_len
)
816 r
->Link
.tcUrl
.av_val
= url
;
817 if (r
->Link
.app
.av_len
)
819 if (r
->Link
.app
.av_val
< url
+ len
)
821 /* if app is part of original url, just use it */
822 r
->Link
.tcUrl
.av_len
= r
->Link
.app
.av_len
+ (r
->Link
.app
.av_val
- url
);
826 len
= r
->Link
.hostname
.av_len
+ r
->Link
.app
.av_len
+
827 sizeof("rtmpte://:65535/");
828 r
->Link
.tcUrl
.av_val
= malloc(len
);
829 r
->Link
.tcUrl
.av_len
= snprintf(r
->Link
.tcUrl
.av_val
, len
,
831 RTMPProtocolStringsLower
[r
->Link
.protocol
],
832 r
->Link
.hostname
.av_len
, r
->Link
.hostname
.av_val
,
834 r
->Link
.app
.av_len
, r
->Link
.app
.av_val
);
835 r
->Link
.lFlags
|= RTMP_LF_FTCU
;
840 r
->Link
.tcUrl
.av_len
= strlen(url
);
845 if ((r
->Link
.lFlags
& RTMP_LF_SWFV
) && r
->Link
.swfUrl
.av_len
)
846 RTMP_HashSWF(r
->Link
.swfUrl
.av_val
, &r
->Link
.SWFSize
,
847 (unsigned char *)r
->Link
.SWFHash
, r
->Link
.swfAge
);
850 SocksSetup(r
, &r
->Link
.sockshost
);
852 if (r
->Link
.port
== 0)
854 if (r
->Link
.protocol
& RTMP_FEATURE_SSL
)
856 else if (r
->Link
.protocol
& RTMP_FEATURE_HTTP
)
865 add_addr_info(struct sockaddr_in
*service
, AVal
*host
, int port
)
869 if (host
->av_val
[host
->av_len
])
871 hostname
= malloc(host
->av_len
+1);
872 memcpy(hostname
, host
->av_val
, host
->av_len
);
873 hostname
[host
->av_len
] = '\0';
877 hostname
= host
->av_val
;
880 service
->sin_addr
.s_addr
= inet_addr(hostname
);
881 if (service
->sin_addr
.s_addr
== INADDR_NONE
)
883 struct hostent
*host
= gethostbyname(hostname
);
884 if (host
== NULL
|| host
->h_addr
== NULL
)
886 RTMP_Log(RTMP_LOGERROR
, "Problem accessing the DNS. (addr: %s)", hostname
);
890 service
->sin_addr
= *(struct in_addr
*)host
->h_addr
;
893 service
->sin_port
= htons(port
);
895 if (hostname
!= host
->av_val
)
901 RTMP_Connect0(RTMP
*r
, struct sockaddr
* service
)
904 r
->m_sb
.sb_timedout
= FALSE
;
906 r
->m_fDuration
= 0.0;
908 r
->m_sb
.sb_socket
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
909 if (r
->m_sb
.sb_socket
!= -1)
911 if (connect(r
->m_sb
.sb_socket
, service
, sizeof(struct sockaddr
)) < 0)
913 int err
= GetSockError();
914 RTMP_Log(RTMP_LOGERROR
, "%s, failed to connect socket. %d (%s)",
915 __FUNCTION__
, err
, strerror(err
));
920 if (r
->Link
.socksport
)
922 RTMP_Log(RTMP_LOGDEBUG
, "%s ... SOCKS negotiation", __FUNCTION__
);
923 if (!SocksNegotiate(r
))
925 RTMP_Log(RTMP_LOGERROR
, "%s, SOCKS negotiation failed.", __FUNCTION__
);
933 RTMP_Log(RTMP_LOGERROR
, "%s, failed to create socket. Error: %d", __FUNCTION__
,
940 SET_RCVTIMEO(tv
, r
->Link
.timeout
);
942 (r
->m_sb
.sb_socket
, SOL_SOCKET
, SO_RCVTIMEO
, (char *)&tv
, sizeof(tv
)))
944 RTMP_Log(RTMP_LOGERROR
, "%s, Setting socket timeout to %ds failed!",
945 __FUNCTION__
, r
->Link
.timeout
);
949 setsockopt(r
->m_sb
.sb_socket
, IPPROTO_TCP
, TCP_NODELAY
, (char *) &on
, sizeof(on
));
955 RTMP_TLS_Accept(RTMP
*r
, void *ctx
)
957 #if defined(CRYPTO) && !defined(NO_SSL)
958 TLS_server(ctx
, r
->m_sb
.sb_ssl
);
959 TLS_setfd(r
->m_sb
.sb_ssl
, r
->m_sb
.sb_socket
);
960 if (TLS_accept(r
->m_sb
.sb_ssl
) < 0)
962 RTMP_Log(RTMP_LOGERROR
, "%s, TLS_Connect failed", __FUNCTION__
);
972 RTMP_Connect1(RTMP
*r
, RTMPPacket
*cp
)
974 if (r
->Link
.protocol
& RTMP_FEATURE_SSL
)
976 #if defined(CRYPTO) && !defined(NO_SSL)
977 TLS_client(RTMP_TLS_ctx
, r
->m_sb
.sb_ssl
);
978 TLS_setfd(r
->m_sb
.sb_ssl
, r
->m_sb
.sb_socket
);
979 if (TLS_connect(r
->m_sb
.sb_ssl
) < 0)
981 RTMP_Log(RTMP_LOGERROR
, "%s, TLS_Connect failed", __FUNCTION__
);
986 RTMP_Log(RTMP_LOGERROR
, "%s, no SSL/TLS support", __FUNCTION__
);
992 if (r
->Link
.protocol
& RTMP_FEATURE_HTTP
)
995 r
->m_clientID
.av_val
= NULL
;
996 r
->m_clientID
.av_len
= 0;
997 HTTP_Post(r
, RTMPT_OPEN
, "", 1);
998 if (HTTP_read(r
, 1) != 0)
1000 r
->m_msgCounter
= 0;
1001 RTMP_Log(RTMP_LOGDEBUG
, "%s, Could not connect for handshake", __FUNCTION__
);
1005 r
->m_msgCounter
= 0;
1007 RTMP_Log(RTMP_LOGDEBUG
, "%s, ... connected, handshaking", __FUNCTION__
);
1008 if (!HandShake(r
, TRUE
))
1010 RTMP_Log(RTMP_LOGERROR
, "%s, handshake failed.", __FUNCTION__
);
1014 RTMP_Log(RTMP_LOGDEBUG
, "%s, handshaked", __FUNCTION__
);
1016 if (!SendConnectPacket(r
, cp
))
1018 RTMP_Log(RTMP_LOGERROR
, "%s, RTMP connect failed.", __FUNCTION__
);
1026 RTMP_Connect(RTMP
*r
, RTMPPacket
*cp
)
1028 struct sockaddr_in service
;
1029 if (!r
->Link
.hostname
.av_len
)
1032 memset(&service
, 0, sizeof(struct sockaddr_in
));
1033 service
.sin_family
= AF_INET
;
1035 if (r
->Link
.socksport
)
1037 /* Connect via SOCKS */
1038 if (!add_addr_info(&service
, &r
->Link
.sockshost
, r
->Link
.socksport
))
1043 /* Connect directly */
1044 if (!add_addr_info(&service
, &r
->Link
.hostname
, r
->Link
.port
))
1048 if (!RTMP_Connect0(r
, (struct sockaddr
*)&service
))
1051 r
->m_bSendCounter
= TRUE
;
1053 return RTMP_Connect1(r
, cp
);
1057 SocksNegotiate(RTMP
*r
)
1060 struct sockaddr_in service
;
1061 memset(&service
, 0, sizeof(struct sockaddr_in
));
1063 add_addr_info(&service
, &r
->Link
.hostname
, r
->Link
.port
);
1064 addr
= htonl(service
.sin_addr
.s_addr
);
1068 4, 1, /* SOCKS 4, connect */
1069 (r
->Link
.port
>> 8) & 0xFF,
1070 (r
->Link
.port
) & 0xFF,
1071 (char)(addr
>> 24) & 0xFF, (char)(addr
>> 16) & 0xFF,
1072 (char)(addr
>> 8) & 0xFF, (char)addr
& 0xFF,
1074 }; /* NULL terminate */
1076 WriteN(r
, packet
, sizeof packet
);
1078 if (ReadN(r
, packet
, 8) != 8)
1081 if (packet
[0] == 0 && packet
[1] == 90)
1087 RTMP_Log(RTMP_LOGERROR
, "%s, SOCKS returned error code %d", __FUNCTION__
, packet
[1]);
1094 RTMP_ConnectStream(RTMP
*r
, int seekTime
)
1096 RTMPPacket packet
= { 0 };
1098 /* seekTime was already set by SetupStream / SetupURL.
1099 * This is only needed by ReconnectStream.
1102 r
->Link
.seekTime
= seekTime
;
1104 r
->m_mediaChannel
= 0;
1106 while (!r
->m_bPlaying
&& RTMP_IsConnected(r
) && RTMP_ReadPacket(r
, &packet
))
1108 if (RTMPPacket_IsReady(&packet
))
1110 if (!packet
.m_nBodySize
)
1112 if ((packet
.m_packetType
== RTMP_PACKET_TYPE_AUDIO
) ||
1113 (packet
.m_packetType
== RTMP_PACKET_TYPE_VIDEO
) ||
1114 (packet
.m_packetType
== RTMP_PACKET_TYPE_INFO
))
1116 RTMP_Log(RTMP_LOGWARNING
, "Received FLV packet before play()! Ignoring.");
1117 RTMPPacket_Free(&packet
);
1121 RTMP_ClientPacket(r
, &packet
);
1122 RTMPPacket_Free(&packet
);
1126 return r
->m_bPlaying
;
1130 RTMP_ReconnectStream(RTMP
*r
, int seekTime
)
1132 RTMP_DeleteStream(r
);
1134 RTMP_SendCreateStream(r
);
1136 return RTMP_ConnectStream(r
, seekTime
);
1140 RTMP_ToggleStream(RTMP
*r
)
1146 if (RTMP_IsTimedout(r
) && r
->m_read
.status
== RTMP_READ_EOF
)
1147 r
->m_read
.status
= 0;
1149 res
= RTMP_SendPause(r
, TRUE
, r
->m_pauseStamp
);
1156 res
= RTMP_SendPause(r
, FALSE
, r
->m_pauseStamp
);
1162 RTMP_DeleteStream(RTMP
*r
)
1164 if (r
->m_stream_id
< 0)
1167 r
->m_bPlaying
= FALSE
;
1169 SendDeleteStream(r
, r
->m_stream_id
);
1170 r
->m_stream_id
= -1;
1174 RTMP_GetNextMediaPacket(RTMP
*r
, RTMPPacket
*packet
)
1176 int bHasMediaPacket
= 0;
1178 while (!bHasMediaPacket
&& RTMP_IsConnected(r
)
1179 && RTMP_ReadPacket(r
, packet
))
1181 if (!RTMPPacket_IsReady(packet
))
1186 bHasMediaPacket
= RTMP_ClientPacket(r
, packet
);
1188 if (!bHasMediaPacket
)
1190 RTMPPacket_Free(packet
);
1192 else if (r
->m_pausing
== 3)
1194 if (packet
->m_nTimeStamp
<= r
->m_mediaStamp
)
1196 bHasMediaPacket
= 0;
1198 RTMP_Log(RTMP_LOGDEBUG
,
1199 "Skipped type: %02X, size: %d, TS: %d ms, abs TS: %d, pause: %d ms",
1200 packet
->m_packetType
, packet
->m_nBodySize
,
1201 packet
->m_nTimeStamp
, packet
->m_hasAbsTimestamp
,
1204 RTMPPacket_Free(packet
);
1211 if (bHasMediaPacket
)
1212 r
->m_bPlaying
= TRUE
;
1213 else if (r
->m_sb
.sb_timedout
&& !r
->m_pausing
)
1214 r
->m_pauseStamp
= r
->m_mediaChannel
< r
->m_channelsAllocatedIn
?
1215 r
->m_channelTimestamp
[r
->m_mediaChannel
] : 0;
1217 return bHasMediaPacket
;
1221 RTMP_ClientPacket(RTMP
*r
, RTMPPacket
*packet
)
1223 int bHasMediaPacket
= 0;
1224 switch (packet
->m_packetType
)
1226 case RTMP_PACKET_TYPE_CHUNK_SIZE
:
1228 HandleChangeChunkSize(r
, packet
);
1231 case RTMP_PACKET_TYPE_BYTES_READ_REPORT
:
1232 /* bytes read report */
1233 RTMP_Log(RTMP_LOGDEBUG
, "%s, received: bytes read report", __FUNCTION__
);
1236 case RTMP_PACKET_TYPE_CONTROL
:
1238 HandleCtrl(r
, packet
);
1241 case RTMP_PACKET_TYPE_SERVER_BW
:
1243 HandleServerBW(r
, packet
);
1246 case RTMP_PACKET_TYPE_CLIENT_BW
:
1248 HandleClientBW(r
, packet
);
1251 case RTMP_PACKET_TYPE_AUDIO
:
1253 /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: audio %lu bytes", __FUNCTION__, packet.m_nBodySize); */
1254 HandleAudio(r
, packet
);
1255 bHasMediaPacket
= 1;
1256 if (!r
->m_mediaChannel
)
1257 r
->m_mediaChannel
= packet
->m_nChannel
;
1259 r
->m_mediaStamp
= packet
->m_nTimeStamp
;
1262 case RTMP_PACKET_TYPE_VIDEO
:
1264 /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: video %lu bytes", __FUNCTION__, packet.m_nBodySize); */
1265 HandleVideo(r
, packet
);
1266 bHasMediaPacket
= 1;
1267 if (!r
->m_mediaChannel
)
1268 r
->m_mediaChannel
= packet
->m_nChannel
;
1270 r
->m_mediaStamp
= packet
->m_nTimeStamp
;
1273 case RTMP_PACKET_TYPE_FLEX_STREAM_SEND
:
1274 /* flex stream send */
1275 RTMP_Log(RTMP_LOGDEBUG
,
1276 "%s, flex stream send, size %u bytes, not supported, ignoring",
1277 __FUNCTION__
, packet
->m_nBodySize
);
1280 case RTMP_PACKET_TYPE_FLEX_SHARED_OBJECT
:
1281 /* flex shared object */
1282 RTMP_Log(RTMP_LOGDEBUG
,
1283 "%s, flex shared object, size %u bytes, not supported, ignoring",
1284 __FUNCTION__
, packet
->m_nBodySize
);
1287 case RTMP_PACKET_TYPE_FLEX_MESSAGE
:
1290 RTMP_Log(RTMP_LOGDEBUG
,
1291 "%s, flex message, size %u bytes, not fully supported",
1292 __FUNCTION__
, packet
->m_nBodySize
);
1293 /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */
1295 /* some DEBUG code */
1297 RTMP_LIB_AMFObject obj
;
1298 int nRes
= obj
.Decode(packet
.m_body
+1, packet
.m_nBodySize
-1);
1300 RTMP_Log(RTMP_LOGERROR
, "%s, error decoding AMF3 packet", __FUNCTION__
);
1307 if (HandleInvoke(r
, packet
->m_body
+ 1, packet
->m_nBodySize
- 1) == 1)
1308 bHasMediaPacket
= 2;
1311 case RTMP_PACKET_TYPE_INFO
:
1312 /* metadata (notify) */
1313 RTMP_Log(RTMP_LOGDEBUG
, "%s, received: notify %u bytes", __FUNCTION__
,
1314 packet
->m_nBodySize
);
1315 if (HandleMetadata(r
, packet
->m_body
, packet
->m_nBodySize
))
1316 bHasMediaPacket
= 1;
1319 case RTMP_PACKET_TYPE_SHARED_OBJECT
:
1320 RTMP_Log(RTMP_LOGDEBUG
, "%s, shared object, not supported, ignoring",
1324 case RTMP_PACKET_TYPE_INVOKE
:
1326 RTMP_Log(RTMP_LOGDEBUG
, "%s, received: invoke %u bytes", __FUNCTION__
,
1327 packet
->m_nBodySize
);
1328 /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */
1330 if (HandleInvoke(r
, packet
->m_body
, packet
->m_nBodySize
) == 1)
1331 bHasMediaPacket
= 2;
1334 case RTMP_PACKET_TYPE_FLASH_VIDEO
:
1336 /* go through FLV packets and handle metadata packets */
1337 unsigned int pos
= 0;
1338 uint32_t nTimeStamp
= packet
->m_nTimeStamp
;
1340 while (pos
+ 11 < packet
->m_nBodySize
)
1342 uint32_t dataSize
= AMF_DecodeInt24(packet
->m_body
+ pos
+ 1); /* size without header (11) and prevTagSize (4) */
1344 if (pos
+ 11 + dataSize
+ 4 > packet
->m_nBodySize
)
1346 RTMP_Log(RTMP_LOGWARNING
, "Stream corrupt?!");
1349 if (packet
->m_body
[pos
] == 0x12)
1351 HandleMetadata(r
, packet
->m_body
+ pos
+ 11, dataSize
);
1353 else if (packet
->m_body
[pos
] == 8 || packet
->m_body
[pos
] == 9)
1355 nTimeStamp
= AMF_DecodeInt24(packet
->m_body
+ pos
+ 4);
1356 nTimeStamp
|= (packet
->m_body
[pos
+ 7] << 24);
1358 pos
+= (11 + dataSize
+ 4);
1361 r
->m_mediaStamp
= nTimeStamp
;
1364 /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: FLV tag(s) %lu bytes", __FUNCTION__, packet.m_nBodySize); */
1365 bHasMediaPacket
= 1;
1369 RTMP_Log(RTMP_LOGDEBUG
, "%s, unknown packet type received: 0x%02x", __FUNCTION__
,
1370 packet
->m_packetType
);
1372 RTMP_LogHex(RTMP_LOGDEBUG
, packet
->m_body
, packet
->m_nBodySize
);
1376 return bHasMediaPacket
;
1380 extern FILE *netstackdump
;
1381 extern FILE *netstackdump_read
;
1385 ReadN(RTMP
*r
, char *buffer
, int n
)
1387 int nOriginalSize
= n
;
1391 r
->m_sb
.sb_timedout
= FALSE
;
1394 memset(buffer
, 0, n
);
1400 int nBytes
= 0, nRead
;
1401 if (r
->Link
.protocol
& RTMP_FEATURE_HTTP
)
1404 while (!r
->m_resplen
)
1407 if (r
->m_sb
.sb_size
< 13 || refill
)
1410 HTTP_Post(r
, RTMPT_IDLE
, "", 1);
1411 if (RTMPSockBuf_Fill(&r
->m_sb
) < 1)
1413 if (!r
->m_sb
.sb_timedout
)
1418 if ((ret
= HTTP_read(r
, 0)) == -1)
1420 RTMP_Log(RTMP_LOGDEBUG
, "%s, No valid HTTP response found", __FUNCTION__
);
1433 if (r
->m_resplen
&& !r
->m_sb
.sb_size
)
1434 RTMPSockBuf_Fill(&r
->m_sb
);
1435 avail
= r
->m_sb
.sb_size
;
1436 if (avail
> r
->m_resplen
)
1437 avail
= r
->m_resplen
;
1441 avail
= r
->m_sb
.sb_size
;
1444 if (RTMPSockBuf_Fill(&r
->m_sb
) < 1)
1446 if (!r
->m_sb
.sb_timedout
)
1450 avail
= r
->m_sb
.sb_size
;
1453 nRead
= ((n
< avail
) ? n
: avail
);
1456 memcpy(ptr
, r
->m_sb
.sb_start
, nRead
);
1457 r
->m_sb
.sb_start
+= nRead
;
1458 r
->m_sb
.sb_size
-= nRead
;
1460 r
->m_nBytesIn
+= nRead
;
1461 if (r
->m_bSendCounter
1462 && r
->m_nBytesIn
> ( r
->m_nBytesInSent
+ r
->m_nClientBW
/ 10))
1463 if (!SendBytesReceived(r
))
1466 /*RTMP_Log(RTMP_LOGDEBUG, "%s: %d bytes\n", __FUNCTION__, nBytes); */
1468 fwrite(ptr
, 1, nBytes
, netstackdump_read
);
1473 RTMP_Log(RTMP_LOGDEBUG
, "%s, RTMP socket closed by peer", __FUNCTION__
);
1479 if (r
->Link
.protocol
& RTMP_FEATURE_HTTP
)
1480 r
->m_resplen
-= nBytes
;
1483 if (r
->Link
.rc4keyIn
)
1485 RC4_encrypt(r
->Link
.rc4keyIn
, nBytes
, ptr
);
1493 return nOriginalSize
- n
;
1497 WriteN(RTMP
*r
, const char *buffer
, int n
)
1499 const char *ptr
= buffer
;
1501 char *encrypted
= 0;
1502 char buf
[RTMP_BUFFER_CACHE_SIZE
];
1504 if (r
->Link
.rc4keyOut
)
1506 if (n
> sizeof(buf
))
1507 encrypted
= (char *)malloc(n
);
1509 encrypted
= (char *)buf
;
1511 RC4_encrypt2(r
->Link
.rc4keyOut
, n
, buffer
, ptr
);
1519 if (r
->Link
.protocol
& RTMP_FEATURE_HTTP
)
1520 nBytes
= HTTP_Post(r
, RTMPT_SEND
, ptr
, n
);
1522 nBytes
= RTMPSockBuf_Send(&r
->m_sb
, ptr
, n
);
1523 /*RTMP_Log(RTMP_LOGDEBUG, "%s: %d\n", __FUNCTION__, nBytes); */
1527 int sockerr
= GetSockError();
1528 RTMP_Log(RTMP_LOGERROR
, "%s, RTMP send error %d (%d bytes)", __FUNCTION__
,
1531 if (sockerr
== EINTR
&& !RTMP_ctrlC
)
1547 if (encrypted
&& encrypted
!= buf
)
1554 #define SAVC(x) static const AVal av_##x = AVC(#x)
1566 SAVC(videoFunction
);
1567 SAVC(objectEncoding
);
1569 SAVC(secureTokenResponse
);
1574 SendConnectPacket(RTMP
*r
, RTMPPacket
*cp
)
1577 char pbuf
[4096], *pend
= pbuf
+ sizeof(pbuf
);
1581 return RTMP_SendPacket(r
, cp
, TRUE
);
1583 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1584 packet
.m_headerType
= RTMP_PACKET_SIZE_LARGE
;
1585 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1586 packet
.m_nTimeStamp
= 0;
1587 packet
.m_nInfoField2
= 0;
1588 packet
.m_hasAbsTimestamp
= 0;
1589 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1591 enc
= packet
.m_body
;
1592 enc
= AMF_EncodeString(enc
, pend
, &av_connect
);
1593 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1594 *enc
++ = AMF_OBJECT
;
1596 enc
= AMF_EncodeNamedString(enc
, pend
, &av_app
, &r
->Link
.app
);
1599 if (r
->Link
.protocol
& RTMP_FEATURE_WRITE
)
1601 enc
= AMF_EncodeNamedString(enc
, pend
, &av_type
, &av_nonprivate
);
1605 if (r
->Link
.flashVer
.av_len
)
1607 enc
= AMF_EncodeNamedString(enc
, pend
, &av_flashVer
, &r
->Link
.flashVer
);
1611 if (r
->Link
.swfUrl
.av_len
)
1613 enc
= AMF_EncodeNamedString(enc
, pend
, &av_swfUrl
, &r
->Link
.swfUrl
);
1617 if (r
->Link
.tcUrl
.av_len
)
1619 enc
= AMF_EncodeNamedString(enc
, pend
, &av_tcUrl
, &r
->Link
.tcUrl
);
1623 if (!(r
->Link
.protocol
& RTMP_FEATURE_WRITE
))
1625 enc
= AMF_EncodeNamedBoolean(enc
, pend
, &av_fpad
, FALSE
);
1628 enc
= AMF_EncodeNamedNumber(enc
, pend
, &av_capabilities
, 15.0);
1631 enc
= AMF_EncodeNamedNumber(enc
, pend
, &av_audioCodecs
, r
->m_fAudioCodecs
);
1634 enc
= AMF_EncodeNamedNumber(enc
, pend
, &av_videoCodecs
, r
->m_fVideoCodecs
);
1637 enc
= AMF_EncodeNamedNumber(enc
, pend
, &av_videoFunction
, 1.0);
1640 if (r
->Link
.pageUrl
.av_len
)
1642 enc
= AMF_EncodeNamedString(enc
, pend
, &av_pageUrl
, &r
->Link
.pageUrl
);
1647 if (r
->m_fEncoding
!= 0.0 || r
->m_bSendEncoding
)
1648 { /* AMF0, AMF3 not fully supported yet */
1649 enc
= AMF_EncodeNamedNumber(enc
, pend
, &av_objectEncoding
, r
->m_fEncoding
);
1653 if (enc
+ 3 >= pend
)
1656 *enc
++ = 0; /* end of object - 0x00 0x00 0x09 */
1657 *enc
++ = AMF_OBJECT_END
;
1659 /* add auth string */
1660 if (r
->Link
.auth
.av_len
)
1662 enc
= AMF_EncodeBoolean(enc
, pend
, r
->Link
.lFlags
& RTMP_LF_AUTH
);
1665 enc
= AMF_EncodeString(enc
, pend
, &r
->Link
.auth
);
1669 if (r
->Link
.extras
.o_num
)
1672 for (i
= 0; i
< r
->Link
.extras
.o_num
; i
++)
1674 enc
= AMFProp_Encode(&r
->Link
.extras
.o_props
[i
], enc
, pend
);
1679 packet
.m_nBodySize
= enc
- packet
.m_body
;
1681 return RTMP_SendPacket(r
, &packet
, TRUE
);
1688 SendBGHasStream(RTMP
*r
, double dId
, AVal
*playpath
)
1691 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
1694 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1695 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1696 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1697 packet
.m_nTimeStamp
= 0;
1698 packet
.m_nInfoField2
= 0;
1699 packet
.m_hasAbsTimestamp
= 0;
1700 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1702 enc
= packet
.m_body
;
1703 enc
= AMF_EncodeString(enc
, pend
, &av_bgHasStream
);
1704 enc
= AMF_EncodeNumber(enc
, pend
, dId
);
1707 enc
= AMF_EncodeString(enc
, pend
, playpath
);
1711 packet
.m_nBodySize
= enc
- packet
.m_body
;
1713 return RTMP_SendPacket(r
, &packet
, TRUE
);
1720 RTMP_SendCreateStream(RTMP
*r
)
1723 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
1726 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1727 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1728 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1729 packet
.m_nTimeStamp
= 0;
1730 packet
.m_nInfoField2
= 0;
1731 packet
.m_hasAbsTimestamp
= 0;
1732 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1734 enc
= packet
.m_body
;
1735 enc
= AMF_EncodeString(enc
, pend
, &av_createStream
);
1736 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1737 *enc
++ = AMF_NULL
; /* NULL */
1739 packet
.m_nBodySize
= enc
- packet
.m_body
;
1741 return RTMP_SendPacket(r
, &packet
, TRUE
);
1747 SendFCSubscribe(RTMP
*r
, AVal
*subscribepath
)
1750 char pbuf
[512], *pend
= pbuf
+ sizeof(pbuf
);
1752 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1753 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1754 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1755 packet
.m_nTimeStamp
= 0;
1756 packet
.m_nInfoField2
= 0;
1757 packet
.m_hasAbsTimestamp
= 0;
1758 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1760 RTMP_Log(RTMP_LOGDEBUG
, "FCSubscribe: %s", subscribepath
->av_val
);
1761 enc
= packet
.m_body
;
1762 enc
= AMF_EncodeString(enc
, pend
, &av_FCSubscribe
);
1763 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1765 enc
= AMF_EncodeString(enc
, pend
, subscribepath
);
1770 packet
.m_nBodySize
= enc
- packet
.m_body
;
1772 return RTMP_SendPacket(r
, &packet
, TRUE
);
1775 /* Justin.tv specific authentication */
1776 static const AVal av_NetStream_Authenticate_UsherToken
= AVC("NetStream.Authenticate.UsherToken");
1779 SendUsherToken(RTMP
*r
, AVal
*usherToken
)
1782 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
1784 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1785 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1786 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1787 packet
.m_nTimeStamp
= 0;
1788 packet
.m_nInfoField2
= 0;
1789 packet
.m_hasAbsTimestamp
= 0;
1790 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1792 RTMP_Log(RTMP_LOGDEBUG
, "UsherToken: %s", usherToken
->av_val
);
1793 enc
= packet
.m_body
;
1794 enc
= AMF_EncodeString(enc
, pend
, &av_NetStream_Authenticate_UsherToken
);
1795 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1797 enc
= AMF_EncodeString(enc
, pend
, usherToken
);
1802 packet
.m_nBodySize
= enc
- packet
.m_body
;
1804 return RTMP_SendPacket(r
, &packet
, FALSE
);
1806 /******************************************/
1808 SAVC(releaseStream
);
1811 SendReleaseStream(RTMP
*r
)
1814 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
1817 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1818 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1819 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1820 packet
.m_nTimeStamp
= 0;
1821 packet
.m_nInfoField2
= 0;
1822 packet
.m_hasAbsTimestamp
= 0;
1823 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1825 enc
= packet
.m_body
;
1826 enc
= AMF_EncodeString(enc
, pend
, &av_releaseStream
);
1827 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1829 enc
= AMF_EncodeString(enc
, pend
, &r
->Link
.playpath
);
1833 packet
.m_nBodySize
= enc
- packet
.m_body
;
1835 return RTMP_SendPacket(r
, &packet
, FALSE
);
1841 SendFCPublish(RTMP
*r
)
1844 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
1847 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1848 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1849 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1850 packet
.m_nTimeStamp
= 0;
1851 packet
.m_nInfoField2
= 0;
1852 packet
.m_hasAbsTimestamp
= 0;
1853 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1855 enc
= packet
.m_body
;
1856 enc
= AMF_EncodeString(enc
, pend
, &av_FCPublish
);
1857 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1859 enc
= AMF_EncodeString(enc
, pend
, &r
->Link
.playpath
);
1863 packet
.m_nBodySize
= enc
- packet
.m_body
;
1865 return RTMP_SendPacket(r
, &packet
, FALSE
);
1871 SendFCUnpublish(RTMP
*r
)
1874 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
1877 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1878 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1879 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1880 packet
.m_nTimeStamp
= 0;
1881 packet
.m_nInfoField2
= 0;
1882 packet
.m_hasAbsTimestamp
= 0;
1883 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1885 enc
= packet
.m_body
;
1886 enc
= AMF_EncodeString(enc
, pend
, &av_FCUnpublish
);
1887 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1889 enc
= AMF_EncodeString(enc
, pend
, &r
->Link
.playpath
);
1893 packet
.m_nBodySize
= enc
- packet
.m_body
;
1895 return RTMP_SendPacket(r
, &packet
, FALSE
);
1903 SendPublish(RTMP
*r
)
1906 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
1909 packet
.m_nChannel
= 0x04; /* source channel (invoke) */
1910 packet
.m_headerType
= RTMP_PACKET_SIZE_LARGE
;
1911 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1912 packet
.m_nTimeStamp
= 0;
1913 packet
.m_nInfoField2
= r
->m_stream_id
;
1914 packet
.m_hasAbsTimestamp
= 0;
1915 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1917 enc
= packet
.m_body
;
1918 enc
= AMF_EncodeString(enc
, pend
, &av_publish
);
1919 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1921 enc
= AMF_EncodeString(enc
, pend
, &r
->Link
.playpath
);
1925 /* FIXME: should we choose live based on Link.lFlags & RTMP_LF_LIVE? */
1926 enc
= AMF_EncodeString(enc
, pend
, &av_live
);
1930 packet
.m_nBodySize
= enc
- packet
.m_body
;
1932 return RTMP_SendPacket(r
, &packet
, TRUE
);
1938 SendDeleteStream(RTMP
*r
, double dStreamId
)
1941 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
1944 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
1945 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1946 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1947 packet
.m_nTimeStamp
= 0;
1948 packet
.m_nInfoField2
= 0;
1949 packet
.m_hasAbsTimestamp
= 0;
1950 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1952 enc
= packet
.m_body
;
1953 enc
= AMF_EncodeString(enc
, pend
, &av_deleteStream
);
1954 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1956 enc
= AMF_EncodeNumber(enc
, pend
, dStreamId
);
1958 packet
.m_nBodySize
= enc
- packet
.m_body
;
1960 /* no response expected */
1961 return RTMP_SendPacket(r
, &packet
, FALSE
);
1967 RTMP_SendPause(RTMP
*r
, int DoPause
, int iTime
)
1970 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
1973 packet
.m_nChannel
= 0x08; /* video channel */
1974 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
1975 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
1976 packet
.m_nTimeStamp
= 0;
1977 packet
.m_nInfoField2
= 0;
1978 packet
.m_hasAbsTimestamp
= 0;
1979 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
1981 enc
= packet
.m_body
;
1982 enc
= AMF_EncodeString(enc
, pend
, &av_pause
);
1983 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
1985 enc
= AMF_EncodeBoolean(enc
, pend
, DoPause
);
1986 enc
= AMF_EncodeNumber(enc
, pend
, (double)iTime
);
1988 packet
.m_nBodySize
= enc
- packet
.m_body
;
1990 RTMP_Log(RTMP_LOGDEBUG
, "%s, %d, pauseTime=%d", __FUNCTION__
, DoPause
, iTime
);
1991 return RTMP_SendPacket(r
, &packet
, TRUE
);
1994 int RTMP_Pause(RTMP
*r
, int DoPause
)
1997 r
->m_pauseStamp
= r
->m_mediaChannel
< r
->m_channelsAllocatedIn
?
1998 r
->m_channelTimestamp
[r
->m_mediaChannel
] : 0;
1999 return RTMP_SendPause(r
, DoPause
, r
->m_pauseStamp
);
2005 RTMP_SendSeek(RTMP
*r
, int iTime
)
2008 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
2011 packet
.m_nChannel
= 0x08; /* video channel */
2012 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
2013 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
2014 packet
.m_nTimeStamp
= 0;
2015 packet
.m_nInfoField2
= 0;
2016 packet
.m_hasAbsTimestamp
= 0;
2017 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2019 enc
= packet
.m_body
;
2020 enc
= AMF_EncodeString(enc
, pend
, &av_seek
);
2021 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
2023 enc
= AMF_EncodeNumber(enc
, pend
, (double)iTime
);
2025 packet
.m_nBodySize
= enc
- packet
.m_body
;
2027 r
->m_read
.flags
|= RTMP_READ_SEEKING
;
2028 r
->m_read
.nResumeTS
= 0;
2030 return RTMP_SendPacket(r
, &packet
, TRUE
);
2034 RTMP_SendServerBW(RTMP
*r
)
2037 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
2039 packet
.m_nChannel
= 0x02; /* control channel (invoke) */
2040 packet
.m_headerType
= RTMP_PACKET_SIZE_LARGE
;
2041 packet
.m_packetType
= RTMP_PACKET_TYPE_SERVER_BW
;
2042 packet
.m_nTimeStamp
= 0;
2043 packet
.m_nInfoField2
= 0;
2044 packet
.m_hasAbsTimestamp
= 0;
2045 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2047 packet
.m_nBodySize
= 4;
2049 AMF_EncodeInt32(packet
.m_body
, pend
, r
->m_nServerBW
);
2050 return RTMP_SendPacket(r
, &packet
, FALSE
);
2054 RTMP_SendClientBW(RTMP
*r
)
2057 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
2059 packet
.m_nChannel
= 0x02; /* control channel (invoke) */
2060 packet
.m_headerType
= RTMP_PACKET_SIZE_LARGE
;
2061 packet
.m_packetType
= RTMP_PACKET_TYPE_CLIENT_BW
;
2062 packet
.m_nTimeStamp
= 0;
2063 packet
.m_nInfoField2
= 0;
2064 packet
.m_hasAbsTimestamp
= 0;
2065 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2067 packet
.m_nBodySize
= 5;
2069 AMF_EncodeInt32(packet
.m_body
, pend
, r
->m_nClientBW
);
2070 packet
.m_body
[4] = r
->m_nClientBW2
;
2071 return RTMP_SendPacket(r
, &packet
, FALSE
);
2075 SendBytesReceived(RTMP
*r
)
2078 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
2080 packet
.m_nChannel
= 0x02; /* control channel (invoke) */
2081 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
2082 packet
.m_packetType
= RTMP_PACKET_TYPE_BYTES_READ_REPORT
;
2083 packet
.m_nTimeStamp
= 0;
2084 packet
.m_nInfoField2
= 0;
2085 packet
.m_hasAbsTimestamp
= 0;
2086 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2088 packet
.m_nBodySize
= 4;
2090 AMF_EncodeInt32(packet
.m_body
, pend
, r
->m_nBytesIn
); /* hard coded for now */
2091 r
->m_nBytesInSent
= r
->m_nBytesIn
;
2093 /*RTMP_Log(RTMP_LOGDEBUG, "Send bytes report. 0x%x (%d bytes)", (unsigned int)m_nBytesIn, m_nBytesIn); */
2094 return RTMP_SendPacket(r
, &packet
, FALSE
);
2100 SendCheckBW(RTMP
*r
)
2103 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
2106 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
2107 packet
.m_headerType
= RTMP_PACKET_SIZE_LARGE
;
2108 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
2109 packet
.m_nTimeStamp
= 0; /* RTMP_GetTime(); */
2110 packet
.m_nInfoField2
= 0;
2111 packet
.m_hasAbsTimestamp
= 0;
2112 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2114 enc
= packet
.m_body
;
2115 enc
= AMF_EncodeString(enc
, pend
, &av__checkbw
);
2116 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
2119 packet
.m_nBodySize
= enc
- packet
.m_body
;
2121 /* triggers _onbwcheck and eventually results in _onbwdone */
2122 return RTMP_SendPacket(r
, &packet
, FALSE
);
2128 SendCheckBWResult(RTMP
*r
, double txn
)
2131 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
2134 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
2135 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
2136 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
2137 packet
.m_nTimeStamp
= 0x16 * r
->m_nBWCheckCounter
; /* temp inc value. till we figure it out. */
2138 packet
.m_nInfoField2
= 0;
2139 packet
.m_hasAbsTimestamp
= 0;
2140 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2142 enc
= packet
.m_body
;
2143 enc
= AMF_EncodeString(enc
, pend
, &av__result
);
2144 enc
= AMF_EncodeNumber(enc
, pend
, txn
);
2146 enc
= AMF_EncodeNumber(enc
, pend
, (double)r
->m_nBWCheckCounter
++);
2148 packet
.m_nBodySize
= enc
- packet
.m_body
;
2150 return RTMP_SendPacket(r
, &packet
, FALSE
);
2157 SendPong(RTMP
*r
, double txn
)
2160 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
2163 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
2164 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
2165 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
2166 packet
.m_nTimeStamp
= 0x16 * r
->m_nBWCheckCounter
; /* temp inc value. till we figure it out. */
2167 packet
.m_nInfoField2
= 0;
2168 packet
.m_hasAbsTimestamp
= 0;
2169 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2171 enc
= packet
.m_body
;
2172 enc
= AMF_EncodeString(enc
, pend
, &av_pong
);
2173 enc
= AMF_EncodeNumber(enc
, pend
, txn
);
2176 packet
.m_nBodySize
= enc
- packet
.m_body
;
2178 return RTMP_SendPacket(r
, &packet
, FALSE
);
2187 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
2190 packet
.m_nChannel
= 0x08; /* we make 8 our stream channel */
2191 packet
.m_headerType
= RTMP_PACKET_SIZE_LARGE
;
2192 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
2193 packet
.m_nTimeStamp
= 0;
2194 packet
.m_nInfoField2
= r
->m_stream_id
; /*0x01000000; */
2195 packet
.m_hasAbsTimestamp
= 0;
2196 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2198 enc
= packet
.m_body
;
2199 enc
= AMF_EncodeString(enc
, pend
, &av_play
);
2200 enc
= AMF_EncodeNumber(enc
, pend
, ++r
->m_numInvokes
);
2203 RTMP_Log(RTMP_LOGDEBUG
, "%s, seekTime=%d, stopTime=%d, sending play: %s",
2204 __FUNCTION__
, r
->Link
.seekTime
, r
->Link
.stopTime
,
2205 r
->Link
.playpath
.av_val
);
2206 enc
= AMF_EncodeString(enc
, pend
, &r
->Link
.playpath
);
2210 /* Optional parameters start and len.
2212 * start: -2, -1, 0, positive number
2213 * -2: looks for a live stream, then a recorded stream,
2214 * if not found any open a live stream
2215 * -1: plays a live stream
2216 * >=0: plays a recorded streams from 'start' milliseconds
2218 if (r
->Link
.lFlags
& RTMP_LF_LIVE
)
2219 enc
= AMF_EncodeNumber(enc
, pend
, -1000.0);
2222 if (r
->Link
.seekTime
> 0.0)
2223 enc
= AMF_EncodeNumber(enc
, pend
, r
->Link
.seekTime
); /* resume from here */
2225 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 */
2230 /* len: -1, 0, positive number
2231 * -1: plays live or recorded stream to the end (default)
2232 * 0: plays a frame 'start' ms away from the beginning
2233 * >0: plays a live or recoded stream for 'len' milliseconds
2235 /*enc += EncodeNumber(enc, -1.0); */ /* len */
2236 if (r
->Link
.stopTime
)
2238 enc
= AMF_EncodeNumber(enc
, pend
, r
->Link
.stopTime
- r
->Link
.seekTime
);
2243 packet
.m_nBodySize
= enc
- packet
.m_body
;
2245 return RTMP_SendPacket(r
, &packet
, TRUE
);
2252 SendPlaylist(RTMP
*r
)
2255 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
2258 packet
.m_nChannel
= 0x08; /* we make 8 our stream channel */
2259 packet
.m_headerType
= RTMP_PACKET_SIZE_LARGE
;
2260 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
2261 packet
.m_nTimeStamp
= 0;
2262 packet
.m_nInfoField2
= r
->m_stream_id
; /*0x01000000; */
2263 packet
.m_hasAbsTimestamp
= 0;
2264 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2266 enc
= packet
.m_body
;
2267 enc
= AMF_EncodeString(enc
, pend
, &av_set_playlist
);
2268 enc
= AMF_EncodeNumber(enc
, pend
, 0);
2270 *enc
++ = AMF_ECMA_ARRAY
;
2274 *enc
++ = AMF_OBJECT
;
2275 enc
= AMF_EncodeNamedString(enc
, pend
, &av_0
, &r
->Link
.playpath
);
2278 if (enc
+ 3 >= pend
)
2282 *enc
++ = AMF_OBJECT_END
;
2284 packet
.m_nBodySize
= enc
- packet
.m_body
;
2286 return RTMP_SendPacket(r
, &packet
, TRUE
);
2290 SendSecureTokenResponse(RTMP
*r
, AVal
*resp
)
2293 char pbuf
[1024], *pend
= pbuf
+ sizeof(pbuf
);
2296 packet
.m_nChannel
= 0x03; /* control channel (invoke) */
2297 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
2298 packet
.m_packetType
= RTMP_PACKET_TYPE_INVOKE
;
2299 packet
.m_nTimeStamp
= 0;
2300 packet
.m_nInfoField2
= 0;
2301 packet
.m_hasAbsTimestamp
= 0;
2302 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2304 enc
= packet
.m_body
;
2305 enc
= AMF_EncodeString(enc
, pend
, &av_secureTokenResponse
);
2306 enc
= AMF_EncodeNumber(enc
, pend
, 0.0);
2308 enc
= AMF_EncodeString(enc
, pend
, resp
);
2312 packet
.m_nBodySize
= enc
- packet
.m_body
;
2314 return RTMP_SendPacket(r
, &packet
, FALSE
);
2318 from http://jira.red5.org/confluence/display/docs/Ping:
2320 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.
2322 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.
2324 * 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.
2325 * type 1: Tell the stream to clear the playing buffer.
2326 * type 3: Buffer time of the client. The third parameter is the buffer time in millisecond.
2327 * type 4: Reset a stream. Used together with type 0 in the case of VOD. Often sent before type 0.
2328 * type 6: Ping the client from server. The second parameter is the current time.
2329 * type 7: Pong reply from client. The second parameter is the time the server sent with his ping request.
2330 * type 26: SWFVerification request
2331 * type 27: SWFVerification response
2334 RTMP_SendCtrl(RTMP
*r
, short nType
, unsigned int nObject
, unsigned int nTime
)
2337 char pbuf
[256], *pend
= pbuf
+ sizeof(pbuf
);
2341 RTMP_Log(RTMP_LOGDEBUG
, "sending ctrl. type: 0x%04x", (unsigned short)nType
);
2343 packet
.m_nChannel
= 0x02; /* control channel (ping) */
2344 packet
.m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
2345 packet
.m_packetType
= RTMP_PACKET_TYPE_CONTROL
;
2346 packet
.m_nTimeStamp
= 0; /* RTMP_GetTime(); */
2347 packet
.m_nInfoField2
= 0;
2348 packet
.m_hasAbsTimestamp
= 0;
2349 packet
.m_body
= pbuf
+ RTMP_MAX_HEADER_SIZE
;
2352 case 0x03: nSize
= 10; break; /* buffer time */
2353 case 0x1A: nSize
= 3; break; /* SWF verify request */
2354 case 0x1B: nSize
= 44; break; /* SWF verify response */
2355 default: nSize
= 6; break;
2358 packet
.m_nBodySize
= nSize
;
2360 buf
= packet
.m_body
;
2361 buf
= AMF_EncodeInt16(buf
, pend
, nType
);
2366 memcpy(buf
, r
->Link
.SWFVerificationResponse
, 42);
2367 RTMP_Log(RTMP_LOGDEBUG
, "Sending SWFVerification response: ");
2368 RTMP_LogHex(RTMP_LOGDEBUG
, (uint8_t *)packet
.m_body
, packet
.m_nBodySize
);
2371 else if (nType
== 0x1A)
2373 *buf
= nObject
& 0xff;
2378 buf
= AMF_EncodeInt32(buf
, pend
, nObject
);
2381 buf
= AMF_EncodeInt32(buf
, pend
, nTime
);
2384 return RTMP_SendPacket(r
, &packet
, FALSE
);
2388 AV_erase(RTMP_METHOD
*vals
, int *num
, int i
, int freeit
)
2391 free(vals
[i
].name
.av_val
);
2393 for (; i
< *num
; i
++)
2395 vals
[i
] = vals
[i
+ 1];
2397 vals
[i
].name
.av_val
= NULL
;
2398 vals
[i
].name
.av_len
= 0;
2403 RTMP_DropRequest(RTMP
*r
, int i
, int freeit
)
2405 AV_erase(r
->m_methodCalls
, &r
->m_numCalls
, i
, freeit
);
2409 AV_queue(RTMP_METHOD
**vals
, int *num
, AVal
*av
, int txn
)
2413 *vals
= realloc(*vals
, (*num
+ 16) * sizeof(RTMP_METHOD
));
2414 tmp
= malloc(av
->av_len
+ 1);
2415 memcpy(tmp
, av
->av_val
, av
->av_len
);
2416 tmp
[av
->av_len
] = '\0';
2417 (*vals
)[*num
].num
= txn
;
2418 (*vals
)[*num
].name
.av_len
= av
->av_len
;
2419 (*vals
)[(*num
)++].name
.av_val
= tmp
;
2423 AV_clear(RTMP_METHOD
*vals
, int num
)
2426 for (i
= 0; i
< num
; i
++)
2427 free(vals
[i
].name
.av_val
);
2434 b64enc(const unsigned char *input
, int length
, char *output
, int maxsize
)
2437 size_t buf_size
= maxsize
;
2438 if(base64_encode((unsigned char *) output
, &buf_size
, input
, length
) == 0)
2440 output
[buf_size
] = '\0';
2445 RTMP_Log(RTMP_LOGDEBUG
, "%s, error", __FUNCTION__
);
2448 #elif defined(USE_GNUTLS)
2449 if (BASE64_ENCODE_RAW_LENGTH(length
) <= maxsize
)
2450 base64_encode_raw((uint8_t*) output
, length
, input
);
2453 RTMP_Log(RTMP_LOGDEBUG
, "%s, error", __FUNCTION__
);
2456 #else /* USE_OPENSSL */
2460 b64
= BIO_new(BIO_f_base64());
2461 bmem
= BIO_new(BIO_s_mem());
2462 b64
= BIO_push(b64
, bmem
);
2463 BIO_write(b64
, input
, length
);
2464 if (BIO_flush(b64
) == 1)
2466 BIO_get_mem_ptr(b64
, &bptr
);
2467 memcpy(output
, bptr
->data
, bptr
->length
-1);
2468 output
[bptr
->length
-1] = '\0';
2472 RTMP_Log(RTMP_LOGDEBUG
, "%s, error", __FUNCTION__
);
2481 #define MD5_CTX md5_context
2482 #define MD5_Init(ctx) md5_starts(ctx)
2483 #define MD5_Update(ctx,data,len) md5_update(ctx,(unsigned char *)data,len)
2484 #define MD5_Final(dig,ctx) md5_finish(ctx,dig)
2485 #elif defined(USE_GNUTLS)
2486 typedef struct md5_ctx MD5_CTX
;
2487 #define MD5_Init(ctx) md5_init(ctx)
2488 #define MD5_Update(ctx,data,len) md5_update(ctx,len,data)
2489 #define MD5_Final(dig,ctx) md5_digest(ctx,MD5_DIGEST_LENGTH,dig)
2493 static const AVal av_authmod_adobe
= AVC("authmod=adobe");
2494 static const AVal av_authmod_llnw
= AVC("authmod=llnw");
2496 static void hexenc(unsigned char *inbuf
, int len
, char *dst
)
2500 sprintf(ptr
, "%02x", *inbuf
++);
2507 PublisherAuth(RTMP
*r
, AVal
*description
)
2509 char *token_in
= NULL
;
2511 unsigned char md5sum_val
[MD5_DIGEST_LENGTH
+1];
2513 int challenge2_data
;
2514 #define RESPONSE_LEN 32
2515 #define CHALLENGE2_LEN 16
2516 #define SALTED2_LEN (32+8+8+8)
2517 #define B64DIGEST_LEN 24 /* 16 byte digest => 22 b64 chars + 2 chars padding */
2518 #define B64INT_LEN 8 /* 4 byte int => 6 b64 chars + 2 chars padding */
2519 #define HEXHASH_LEN (2*MD5_DIGEST_LENGTH)
2520 char response
[RESPONSE_LEN
];
2521 char challenge2
[CHALLENGE2_LEN
];
2522 char salted2
[SALTED2_LEN
];
2525 if (strstr(description
->av_val
, av_authmod_adobe
.av_val
) != NULL
)
2527 if(strstr(description
->av_val
, "code=403 need auth") != NULL
)
2529 if (strstr(r
->Link
.app
.av_val
, av_authmod_adobe
.av_val
) != NULL
) {
2530 RTMP_Log(RTMP_LOGERROR
, "%s, wrong pubUser & pubPasswd for publisher auth", __FUNCTION__
);
2531 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2533 } else if(r
->Link
.pubUser
.av_len
&& r
->Link
.pubPasswd
.av_len
) {
2534 pubToken
.av_val
= malloc(r
->Link
.pubUser
.av_len
+ av_authmod_adobe
.av_len
+ 8);
2535 pubToken
.av_len
= sprintf(pubToken
.av_val
, "?%s&user=%s",
2536 av_authmod_adobe
.av_val
,
2537 r
->Link
.pubUser
.av_val
);
2538 RTMP_Log(RTMP_LOGDEBUG
, "%s, pubToken1: %s", __FUNCTION__
, pubToken
.av_val
);
2539 r
->Link
.pFlags
|= RTMP_PUB_NAME
;
2541 RTMP_Log(RTMP_LOGERROR
, "%s, need to set pubUser & pubPasswd for publisher auth", __FUNCTION__
);
2542 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2546 else if((token_in
= strstr(description
->av_val
, "?reason=needauth")) != NULL
)
2548 char *par
, *val
= NULL
, *orig_ptr
;
2549 AVal user
, salt
, opaque
, challenge
, *aptr
= NULL
;
2551 challenge
.av_len
= 0;
2553 ptr
= orig_ptr
= strdup(token_in
);
2557 ptr
= strchr(par
, '&');
2561 val
= strchr(par
, '=');
2566 aptr
->av_len
= par
- aptr
->av_val
- 1;
2569 if (strcmp(par
, "user") == 0){
2572 } else if (strcmp(par
, "salt") == 0){
2575 } else if (strcmp(par
, "opaque") == 0){
2576 opaque
.av_val
= val
;
2578 } else if (strcmp(par
, "challenge") == 0){
2579 challenge
.av_val
= val
;
2583 RTMP_Log(RTMP_LOGDEBUG
, "%s, par:\"%s\" = val:\"%s\"", __FUNCTION__
, par
, val
);
2586 aptr
->av_len
= strlen(aptr
->av_val
);
2588 /* hash1 = base64enc(md5(user + _aodbeAuthSalt + password)) */
2590 MD5_Update(&md5ctx
, user
.av_val
, user
.av_len
);
2591 MD5_Update(&md5ctx
, salt
.av_val
, salt
.av_len
);
2592 MD5_Update(&md5ctx
, r
->Link
.pubPasswd
.av_val
, r
->Link
.pubPasswd
.av_len
);
2593 MD5_Final(md5sum_val
, &md5ctx
);
2594 RTMP_Log(RTMP_LOGDEBUG
, "%s, md5(%s%s%s) =>", __FUNCTION__
,
2595 user
.av_val
, salt
.av_val
, r
->Link
.pubPasswd
.av_val
);
2596 RTMP_LogHexString(RTMP_LOGDEBUG
, md5sum_val
, MD5_DIGEST_LENGTH
);
2598 b64enc(md5sum_val
, MD5_DIGEST_LENGTH
, salted2
, SALTED2_LEN
);
2599 RTMP_Log(RTMP_LOGDEBUG
, "%s, b64(md5_1) = %s", __FUNCTION__
, salted2
);
2601 /* FIXME: what byte order does this depend on? */
2602 challenge2_data
= rand();
2604 b64enc((unsigned char *) &challenge2_data
, sizeof(int), challenge2
, CHALLENGE2_LEN
);
2605 RTMP_Log(RTMP_LOGDEBUG
, "%s, b64(%d) = %s", __FUNCTION__
, challenge2_data
, challenge2
);
2608 MD5_Update(&md5ctx
, salted2
, B64DIGEST_LEN
);
2609 /* response = base64enc(md5(hash1 + opaque + challenge2)) */
2611 MD5_Update(&md5ctx
, opaque
.av_val
, opaque
.av_len
);
2612 if (challenge
.av_len
)
2613 MD5_Update(&md5ctx
, challenge
.av_val
, challenge
.av_len
);
2614 MD5_Update(&md5ctx
, challenge2
, B64INT_LEN
);
2615 MD5_Final(md5sum_val
, &md5ctx
);
2617 RTMP_Log(RTMP_LOGDEBUG
, "%s, md5(%s%s%s) =>", __FUNCTION__
,
2618 salted2
, opaque
.av_len
? opaque
.av_val
: "", challenge2
);
2619 RTMP_LogHexString(RTMP_LOGDEBUG
, md5sum_val
, MD5_DIGEST_LENGTH
);
2621 b64enc(md5sum_val
, MD5_DIGEST_LENGTH
, response
, RESPONSE_LEN
);
2622 RTMP_Log(RTMP_LOGDEBUG
, "%s, b64(md5_2) = %s", __FUNCTION__
, response
);
2624 /* have all hashes, create auth token for the end of app */
2625 pubToken
.av_val
= malloc(32 + B64INT_LEN
+ B64DIGEST_LEN
+ opaque
.av_len
);
2626 pubToken
.av_len
= sprintf(pubToken
.av_val
,
2627 "&challenge=%s&response=%s&opaque=%s",
2630 opaque
.av_len
? opaque
.av_val
: "");
2631 RTMP_Log(RTMP_LOGDEBUG
, "%s, pubToken2: %s", __FUNCTION__
, pubToken
.av_val
);
2633 r
->Link
.pFlags
|= RTMP_PUB_RESP
|RTMP_PUB_CLATE
;
2635 else if(strstr(description
->av_val
, "?reason=authfailed") != NULL
)
2637 RTMP_Log(RTMP_LOGERROR
, "%s, Authentication failed: wrong password", __FUNCTION__
);
2638 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2641 else if(strstr(description
->av_val
, "?reason=nosuchuser") != NULL
)
2643 RTMP_Log(RTMP_LOGERROR
, "%s, Authentication failed: no such user", __FUNCTION__
);
2644 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2649 RTMP_Log(RTMP_LOGERROR
, "%s, Authentication failed: unknown auth mode: %s",
2650 __FUNCTION__
, description
->av_val
);
2651 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2655 ptr
= malloc(r
->Link
.app
.av_len
+ pubToken
.av_len
);
2656 strncpy(ptr
, r
->Link
.app
.av_val
, r
->Link
.app
.av_len
);
2657 strncpy(ptr
+ r
->Link
.app
.av_len
, pubToken
.av_val
, pubToken
.av_len
);
2658 r
->Link
.app
.av_len
+= pubToken
.av_len
;
2659 if(r
->Link
.pFlags
& RTMP_PUB_ALLOC
)
2660 free(r
->Link
.app
.av_val
);
2661 r
->Link
.app
.av_val
= ptr
;
2663 ptr
= malloc(r
->Link
.tcUrl
.av_len
+ pubToken
.av_len
);
2664 strncpy(ptr
, r
->Link
.tcUrl
.av_val
, r
->Link
.tcUrl
.av_len
);
2665 strncpy(ptr
+ r
->Link
.tcUrl
.av_len
, pubToken
.av_val
, pubToken
.av_len
);
2666 r
->Link
.tcUrl
.av_len
+= pubToken
.av_len
;
2667 if(r
->Link
.pFlags
& RTMP_PUB_ALLOC
)
2668 free(r
->Link
.tcUrl
.av_val
);
2669 r
->Link
.tcUrl
.av_val
= ptr
;
2671 free(pubToken
.av_val
);
2672 r
->Link
.pFlags
|= RTMP_PUB_ALLOC
;
2674 RTMP_Log(RTMP_LOGDEBUG
, "%s, new app: %.*s tcUrl: %.*s playpath: %s", __FUNCTION__
,
2675 r
->Link
.app
.av_len
, r
->Link
.app
.av_val
,
2676 r
->Link
.tcUrl
.av_len
, r
->Link
.tcUrl
.av_val
,
2677 r
->Link
.playpath
.av_val
);
2679 else if (strstr(description
->av_val
, av_authmod_llnw
.av_val
) != NULL
)
2681 if(strstr(description
->av_val
, "code=403 need auth") != NULL
)
2683 /* This part seems to be the same for llnw and adobe */
2685 if (strstr(r
->Link
.app
.av_val
, av_authmod_llnw
.av_val
) != NULL
) {
2686 RTMP_Log(RTMP_LOGERROR
, "%s, wrong pubUser & pubPasswd for publisher auth", __FUNCTION__
);
2687 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2689 } else if(r
->Link
.pubUser
.av_len
&& r
->Link
.pubPasswd
.av_len
) {
2690 pubToken
.av_val
= malloc(r
->Link
.pubUser
.av_len
+ av_authmod_llnw
.av_len
+ 8);
2691 pubToken
.av_len
= sprintf(pubToken
.av_val
, "?%s&user=%s",
2692 av_authmod_llnw
.av_val
,
2693 r
->Link
.pubUser
.av_val
);
2694 RTMP_Log(RTMP_LOGDEBUG
, "%s, pubToken1: %s", __FUNCTION__
, pubToken
.av_val
);
2695 r
->Link
.pFlags
|= RTMP_PUB_NAME
;
2697 RTMP_Log(RTMP_LOGERROR
, "%s, need to set pubUser & pubPasswd for publisher auth", __FUNCTION__
);
2698 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2702 else if((token_in
= strstr(description
->av_val
, "?reason=needauth")) != NULL
)
2705 char *par
, *val
= NULL
;
2706 char hash1
[HEXHASH_LEN
+1], hash2
[HEXHASH_LEN
+1], hash3
[HEXHASH_LEN
+1];
2707 AVal user
, nonce
, *aptr
= NULL
;
2711 * Seems to be closely based on HTTP Digest Auth:
2712 * http://tools.ietf.org/html/rfc2617
2713 * http://en.wikipedia.org/wiki/Digest_access_authentication
2716 const char authmod
[] = "llnw";
2717 const char realm
[] = "live";
2718 const char method
[] = "publish";
2719 const char qop
[] = "auth";
2720 /* nc = 1..connection count (or rather, number of times cnonce has been reused) */
2722 /* nchex = hexenc(nc) (8 hex digits according to RFC 2617) */
2724 /* cnonce = hexenc(4 random bytes) (initialized on first connection) */
2727 ptr
= orig_ptr
= strdup(token_in
);
2728 /* Extract parameters (we need user and nonce) */
2732 ptr
= strchr(par
, '&');
2736 val
= strchr(par
, '=');
2741 aptr
->av_len
= par
- aptr
->av_val
- 1;
2744 if (strcmp(par
, "user") == 0){
2747 } else if (strcmp(par
, "nonce") == 0){
2752 RTMP_Log(RTMP_LOGDEBUG
, "%s, par:\"%s\" = val:\"%s\"", __FUNCTION__
, par
, val
);
2755 aptr
->av_len
= strlen(aptr
->av_val
);
2757 /* FIXME: handle case where user==NULL or nonce==NULL */
2759 sprintf(nchex
, "%08x", nc
);
2760 sprintf(cnonce
, "%08x", rand());
2762 /* hash1 = hexenc(md5(user + ":" + realm + ":" + password)) */
2764 MD5_Update(&md5ctx
, user
.av_val
, user
.av_len
);
2765 MD5_Update(&md5ctx
, ":", 1);
2766 MD5_Update(&md5ctx
, realm
, sizeof(realm
)-1);
2767 MD5_Update(&md5ctx
, ":", 1);
2768 MD5_Update(&md5ctx
, r
->Link
.pubPasswd
.av_val
, r
->Link
.pubPasswd
.av_len
);
2769 MD5_Final(md5sum_val
, &md5ctx
);
2770 RTMP_Log(RTMP_LOGDEBUG
, "%s, md5(%s:%s:%s) =>", __FUNCTION__
,
2771 user
.av_val
, realm
, r
->Link
.pubPasswd
.av_val
);
2772 RTMP_LogHexString(RTMP_LOGDEBUG
, md5sum_val
, MD5_DIGEST_LENGTH
);
2773 hexenc(md5sum_val
, MD5_DIGEST_LENGTH
, hash1
);
2775 /* hash2 = hexenc(md5(method + ":/" + app + "/" + appInstance)) */
2776 /* Extract appname + appinstance without query parameters */
2777 apptmp
= r
->Link
.app
;
2778 ptr
= strchr(apptmp
.av_val
, '?');
2780 apptmp
.av_len
= ptr
- apptmp
.av_val
;
2783 MD5_Update(&md5ctx
, method
, sizeof(method
)-1);
2784 MD5_Update(&md5ctx
, ":/", 2);
2785 MD5_Update(&md5ctx
, apptmp
.av_val
, apptmp
.av_len
);
2786 MD5_Final(md5sum_val
, &md5ctx
);
2787 RTMP_Log(RTMP_LOGDEBUG
, "%s, md5(%s:/%.*s) =>", __FUNCTION__
,
2788 method
, apptmp
.av_len
, apptmp
.av_val
);
2789 RTMP_LogHexString(RTMP_LOGDEBUG
, md5sum_val
, MD5_DIGEST_LENGTH
);
2790 hexenc(md5sum_val
, MD5_DIGEST_LENGTH
, hash2
);
2792 /* hash3 = hexenc(md5(hash1 + ":" + nonce + ":" + nchex + ":" + cnonce + ":" + qop + ":" + hash2)) */
2794 MD5_Update(&md5ctx
, hash1
, HEXHASH_LEN
);
2795 MD5_Update(&md5ctx
, ":", 1);
2796 MD5_Update(&md5ctx
, nonce
.av_val
, nonce
.av_len
);
2797 MD5_Update(&md5ctx
, ":", 1);
2798 MD5_Update(&md5ctx
, nchex
, sizeof(nchex
)-1);
2799 MD5_Update(&md5ctx
, ":", 1);
2800 MD5_Update(&md5ctx
, cnonce
, sizeof(cnonce
)-1);
2801 MD5_Update(&md5ctx
, ":", 1);
2802 MD5_Update(&md5ctx
, qop
, sizeof(qop
)-1);
2803 MD5_Update(&md5ctx
, ":", 1);
2804 MD5_Update(&md5ctx
, hash2
, HEXHASH_LEN
);
2805 MD5_Final(md5sum_val
, &md5ctx
);
2806 RTMP_Log(RTMP_LOGDEBUG
, "%s, md5(%s:%s:%s:%s:%s:%s) =>", __FUNCTION__
,
2807 hash1
, nonce
.av_val
, nchex
, cnonce
, qop
, hash2
);
2808 RTMP_LogHexString(RTMP_LOGDEBUG
, md5sum_val
, MD5_DIGEST_LENGTH
);
2809 hexenc(md5sum_val
, MD5_DIGEST_LENGTH
, hash3
);
2811 /* pubToken = &authmod=<authmod>&user=<username>&nonce=<nonce>&cnonce=<cnonce>&nc=<nchex>&response=<hash3> */
2812 /* Append nonces and response to query string which already contains
2814 pubToken
.av_val
= malloc(64 + sizeof(authmod
)-1 + user
.av_len
+ nonce
.av_len
+ sizeof(cnonce
)-1 + sizeof(nchex
)-1 + HEXHASH_LEN
);
2815 sprintf(pubToken
.av_val
,
2816 "&nonce=%s&cnonce=%s&nc=%s&response=%s",
2817 nonce
.av_val
, cnonce
, nchex
, hash3
);
2818 pubToken
.av_len
= strlen(pubToken
.av_val
);
2819 RTMP_Log(RTMP_LOGDEBUG
, "%s, pubToken2: %s", __FUNCTION__
, pubToken
.av_val
);
2820 r
->Link
.pFlags
|= RTMP_PUB_RESP
|RTMP_PUB_CLATE
;
2824 else if(strstr(description
->av_val
, "?reason=authfail") != NULL
)
2826 RTMP_Log(RTMP_LOGERROR
, "%s, Authentication failed", __FUNCTION__
);
2827 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2830 else if(strstr(description
->av_val
, "?reason=nosuchuser") != NULL
)
2832 RTMP_Log(RTMP_LOGERROR
, "%s, Authentication failed: no such user", __FUNCTION__
);
2833 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2838 RTMP_Log(RTMP_LOGERROR
, "%s, Authentication failed: unknown auth mode: %s",
2839 __FUNCTION__
, description
->av_val
);
2840 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
2844 ptr
= malloc(r
->Link
.app
.av_len
+ pubToken
.av_len
);
2845 strncpy(ptr
, r
->Link
.app
.av_val
, r
->Link
.app
.av_len
);
2846 strncpy(ptr
+ r
->Link
.app
.av_len
, pubToken
.av_val
, pubToken
.av_len
);
2847 r
->Link
.app
.av_len
+= pubToken
.av_len
;
2848 if(r
->Link
.pFlags
& RTMP_PUB_ALLOC
)
2849 free(r
->Link
.app
.av_val
);
2850 r
->Link
.app
.av_val
= ptr
;
2852 ptr
= malloc(r
->Link
.tcUrl
.av_len
+ pubToken
.av_len
);
2853 strncpy(ptr
, r
->Link
.tcUrl
.av_val
, r
->Link
.tcUrl
.av_len
);
2854 strncpy(ptr
+ r
->Link
.tcUrl
.av_len
, pubToken
.av_val
, pubToken
.av_len
);
2855 r
->Link
.tcUrl
.av_len
+= pubToken
.av_len
;
2856 if(r
->Link
.pFlags
& RTMP_PUB_ALLOC
)
2857 free(r
->Link
.tcUrl
.av_val
);
2858 r
->Link
.tcUrl
.av_val
= ptr
;
2860 free(pubToken
.av_val
);
2861 r
->Link
.pFlags
|= RTMP_PUB_ALLOC
;
2863 RTMP_Log(RTMP_LOGDEBUG
, "%s, new app: %.*s tcUrl: %.*s playpath: %s", __FUNCTION__
,
2864 r
->Link
.app
.av_len
, r
->Link
.app
.av_val
,
2865 r
->Link
.tcUrl
.av_len
, r
->Link
.tcUrl
.av_val
,
2866 r
->Link
.playpath
.av_val
);
2878 SAVC(onFCSubscribe
);
2879 SAVC(onFCUnsubscribe
);
2888 SAVC(playlist_ready
);
2889 static const AVal av_NetStream_Failed
= AVC("NetStream.Failed");
2890 static const AVal av_NetStream_Play_Failed
= AVC("NetStream.Play.Failed");
2891 static const AVal av_NetStream_Play_StreamNotFound
=
2892 AVC("NetStream.Play.StreamNotFound");
2893 static const AVal av_NetConnection_Connect_InvalidApp
=
2894 AVC("NetConnection.Connect.InvalidApp");
2895 static const AVal av_NetStream_Play_Start
= AVC("NetStream.Play.Start");
2896 static const AVal av_NetStream_Play_Complete
= AVC("NetStream.Play.Complete");
2897 static const AVal av_NetStream_Play_Stop
= AVC("NetStream.Play.Stop");
2898 static const AVal av_NetStream_Seek_Notify
= AVC("NetStream.Seek.Notify");
2899 static const AVal av_NetStream_Pause_Notify
= AVC("NetStream.Pause.Notify");
2900 static const AVal av_NetStream_Play_PublishNotify
=
2901 AVC("NetStream.Play.PublishNotify");
2902 static const AVal av_NetStream_Play_UnpublishNotify
=
2903 AVC("NetStream.Play.UnpublishNotify");
2904 static const AVal av_NetStream_Publish_Start
= AVC("NetStream.Publish.Start");
2905 static const AVal av_NetConnection_Connect_Rejected
=
2906 AVC("NetConnection.Connect.Rejected");
2908 /* Returns 0 for OK/Failed/error, 1 for 'Stop or Complete' */
2910 HandleInvoke(RTMP
*r
, const char *body
, unsigned int nBodySize
)
2916 if (body
[0] != 0x02) /* make sure it is a string method name we start with */
2918 RTMP_Log(RTMP_LOGWARNING
, "%s, Sanity failed. no string method in invoke packet",
2923 nRes
= AMF_Decode(&obj
, body
, nBodySize
, FALSE
);
2926 RTMP_Log(RTMP_LOGERROR
, "%s, error decoding invoke packet", __FUNCTION__
);
2931 AMFProp_GetString(AMF_GetProp(&obj
, NULL
, 0), &method
);
2932 txn
= AMFProp_GetNumber(AMF_GetProp(&obj
, NULL
, 1));
2933 RTMP_Log(RTMP_LOGDEBUG
, "%s, server invoking <%s>", __FUNCTION__
, method
.av_val
);
2935 if (AVMATCH(&method
, &av__result
))
2937 AVal methodInvoked
= {0};
2940 for (i
=0; i
<r
->m_numCalls
; i
++) {
2941 if (r
->m_methodCalls
[i
].num
== (int)txn
) {
2942 methodInvoked
= r
->m_methodCalls
[i
].name
;
2943 AV_erase(r
->m_methodCalls
, &r
->m_numCalls
, i
, FALSE
);
2947 if (!methodInvoked
.av_val
) {
2948 RTMP_Log(RTMP_LOGDEBUG
, "%s, received result id %f without matching request",
2953 RTMP_Log(RTMP_LOGDEBUG
, "%s, received result for method call <%s>", __FUNCTION__
,
2954 methodInvoked
.av_val
);
2956 if (AVMATCH(&methodInvoked
, &av_connect
))
2958 if (r
->Link
.token
.av_len
)
2960 AMFObjectProperty p
;
2961 if (RTMP_FindFirstMatchingProperty(&obj
, &av_secureToken
, &p
))
2963 DecodeTEA(&r
->Link
.token
, &p
.p_vu
.p_aval
);
2964 SendSecureTokenResponse(r
, &p
.p_vu
.p_aval
);
2967 if (r
->Link
.protocol
& RTMP_FEATURE_WRITE
)
2969 SendReleaseStream(r
);
2974 RTMP_SendServerBW(r
);
2975 RTMP_SendCtrl(r
, 3, 0, 300);
2977 RTMP_SendCreateStream(r
);
2979 if (!(r
->Link
.protocol
& RTMP_FEATURE_WRITE
))
2981 /* Authenticate on Justin.tv legacy servers before sending FCSubscribe */
2982 if (r
->Link
.usherToken
.av_len
)
2983 SendUsherToken(r
, &r
->Link
.usherToken
);
2984 /* Send the FCSubscribe if live stream or if subscribepath is set */
2985 if (r
->Link
.subscribepath
.av_len
)
2986 SendFCSubscribe(r
, &r
->Link
.subscribepath
);
2987 else if (r
->Link
.lFlags
& RTMP_LF_LIVE
)
2988 SendFCSubscribe(r
, &r
->Link
.playpath
);
2991 else if (AVMATCH(&methodInvoked
, &av_createStream
))
2993 r
->m_stream_id
= (int)AMFProp_GetNumber(AMF_GetProp(&obj
, NULL
, 3));
2995 if (r
->Link
.protocol
& RTMP_FEATURE_WRITE
)
3001 if (r
->Link
.lFlags
& RTMP_LF_PLST
)
3004 RTMP_SendCtrl(r
, 3, r
->m_stream_id
, r
->m_nBufferMS
);
3007 else if (AVMATCH(&methodInvoked
, &av_play
) ||
3008 AVMATCH(&methodInvoked
, &av_publish
))
3010 r
->m_bPlaying
= TRUE
;
3012 free(methodInvoked
.av_val
);
3014 else if (AVMATCH(&method
, &av_onBWDone
))
3016 if (!r
->m_nBWCheckCounter
)
3019 else if (AVMATCH(&method
, &av_onFCSubscribe
))
3021 /* SendOnFCSubscribe(); */
3023 else if (AVMATCH(&method
, &av_onFCUnsubscribe
))
3028 else if (AVMATCH(&method
, &av_ping
))
3032 else if (AVMATCH(&method
, &av__onbwcheck
))
3034 SendCheckBWResult(r
, txn
);
3036 else if (AVMATCH(&method
, &av__onbwdone
))
3039 for (i
= 0; i
< r
->m_numCalls
; i
++)
3040 if (AVMATCH(&r
->m_methodCalls
[i
].name
, &av__checkbw
))
3042 AV_erase(r
->m_methodCalls
, &r
->m_numCalls
, i
, TRUE
);
3046 else if (AVMATCH(&method
, &av__error
))
3049 AVal methodInvoked
= {0};
3052 if (r
->Link
.protocol
& RTMP_FEATURE_WRITE
)
3054 for (i
=0; i
<r
->m_numCalls
; i
++)
3056 if (r
->m_methodCalls
[i
].num
== txn
)
3058 methodInvoked
= r
->m_methodCalls
[i
].name
;
3059 AV_erase(r
->m_methodCalls
, &r
->m_numCalls
, i
, FALSE
);
3063 if (!methodInvoked
.av_val
)
3065 RTMP_Log(RTMP_LOGDEBUG
, "%s, received result id %f without matching request",
3070 RTMP_Log(RTMP_LOGDEBUG
, "%s, received error for method call <%s>", __FUNCTION__
,
3071 methodInvoked
.av_val
);
3073 if (AVMATCH(&methodInvoked
, &av_connect
))
3076 AVal code
, level
, description
;
3077 AMFProp_GetObject(AMF_GetProp(&obj
, NULL
, 3), &obj2
);
3078 AMFProp_GetString(AMF_GetProp(&obj2
, &av_code
, -1), &code
);
3079 AMFProp_GetString(AMF_GetProp(&obj2
, &av_level
, -1), &level
);
3080 AMFProp_GetString(AMF_GetProp(&obj2
, &av_description
, -1), &description
);
3081 RTMP_Log(RTMP_LOGDEBUG
, "%s, error description: %s", __FUNCTION__
, description
.av_val
);
3082 /* if PublisherAuth returns 1, then reconnect */
3083 PublisherAuth(r
, &description
);
3088 RTMP_Log(RTMP_LOGERROR
, "rtmp server sent error");
3090 free(methodInvoked
.av_val
);
3092 RTMP_Log(RTMP_LOGERROR
, "rtmp server sent error");
3095 else if (AVMATCH(&method
, &av_close
))
3097 RTMP_Log(RTMP_LOGERROR
, "rtmp server requested close");
3100 if ((r
->Link
.protocol
& RTMP_FEATURE_WRITE
) &&
3101 !(r
->Link
.pFlags
& RTMP_PUB_CLEAN
) &&
3102 ( !(r
->Link
.pFlags
& RTMP_PUB_NAME
) ||
3103 !(r
->Link
.pFlags
& RTMP_PUB_RESP
) ||
3104 (r
->Link
.pFlags
& RTMP_PUB_CLATE
) ) )
3107 if(r
->Link
.pFlags
& RTMP_PUB_CLATE
)
3108 r
->Link
.pFlags
|= RTMP_PUB_CLEAN
;
3109 RTMP_Log(RTMP_LOGERROR
, "authenticating publisher");
3111 if (!RTMP_Connect(r
, NULL
) || !RTMP_ConnectStream(r
, 0))
3116 else if (AVMATCH(&method
, &av_onStatus
))
3120 AMFProp_GetObject(AMF_GetProp(&obj
, NULL
, 3), &obj2
);
3121 AMFProp_GetString(AMF_GetProp(&obj2
, &av_code
, -1), &code
);
3122 AMFProp_GetString(AMF_GetProp(&obj2
, &av_level
, -1), &level
);
3124 RTMP_Log(RTMP_LOGDEBUG
, "%s, onStatus: %s", __FUNCTION__
, code
.av_val
);
3125 if (AVMATCH(&code
, &av_NetStream_Failed
)
3126 || AVMATCH(&code
, &av_NetStream_Play_Failed
)
3127 || AVMATCH(&code
, &av_NetStream_Play_StreamNotFound
)
3128 || AVMATCH(&code
, &av_NetConnection_Connect_InvalidApp
))
3130 r
->m_stream_id
= -1;
3132 RTMP_Log(RTMP_LOGERROR
, "Closing connection: %s", code
.av_val
);
3135 else if (AVMATCH(&code
, &av_NetStream_Play_Start
)
3136 || AVMATCH(&code
, &av_NetStream_Play_PublishNotify
))
3139 r
->m_bPlaying
= TRUE
;
3140 for (i
= 0; i
< r
->m_numCalls
; i
++)
3142 if (AVMATCH(&r
->m_methodCalls
[i
].name
, &av_play
))
3144 AV_erase(r
->m_methodCalls
, &r
->m_numCalls
, i
, TRUE
);
3150 else if (AVMATCH(&code
, &av_NetStream_Publish_Start
))
3153 r
->m_bPlaying
= TRUE
;
3154 for (i
= 0; i
< r
->m_numCalls
; i
++)
3156 if (AVMATCH(&r
->m_methodCalls
[i
].name
, &av_publish
))
3158 AV_erase(r
->m_methodCalls
, &r
->m_numCalls
, i
, TRUE
);
3164 /* Return 1 if this is a Play.Complete or Play.Stop */
3165 else if (AVMATCH(&code
, &av_NetStream_Play_Complete
)
3166 || AVMATCH(&code
, &av_NetStream_Play_Stop
)
3167 || AVMATCH(&code
, &av_NetStream_Play_UnpublishNotify
))
3173 else if (AVMATCH(&code
, &av_NetStream_Seek_Notify
))
3175 r
->m_read
.flags
&= ~RTMP_READ_SEEKING
;
3178 else if (AVMATCH(&code
, &av_NetStream_Pause_Notify
))
3180 if (r
->m_pausing
== 1 || r
->m_pausing
== 2)
3182 RTMP_SendPause(r
, FALSE
, r
->m_pauseStamp
);
3187 else if (AVMATCH(&method
, &av_playlist_ready
))
3190 for (i
= 0; i
< r
->m_numCalls
; i
++)
3192 if (AVMATCH(&r
->m_methodCalls
[i
].name
, &av_set_playlist
))
3194 AV_erase(r
->m_methodCalls
, &r
->m_numCalls
, i
, TRUE
);
3209 RTMP_FindFirstMatchingProperty(AMFObject
*obj
, const AVal
*name
,
3210 AMFObjectProperty
* p
)
3213 /* this is a small object search to locate the "duration" property */
3214 for (n
= 0; n
< obj
->o_num
; n
++)
3216 AMFObjectProperty
*prop
= AMF_GetProp(obj
, NULL
, n
);
3218 if (AVMATCH(&prop
->p_name
, name
))
3220 memcpy(p
, prop
, sizeof(*prop
));
3224 if (prop
->p_type
== AMF_OBJECT
|| prop
->p_type
== AMF_ECMA_ARRAY
)
3226 if (RTMP_FindFirstMatchingProperty(&prop
->p_vu
.p_object
, name
, p
))
3233 /* Like above, but only check if name is a prefix of property */
3235 RTMP_FindPrefixProperty(AMFObject
*obj
, const AVal
*name
,
3236 AMFObjectProperty
* p
)
3239 for (n
= 0; n
< obj
->o_num
; n
++)
3241 AMFObjectProperty
*prop
= AMF_GetProp(obj
, NULL
, n
);
3243 if (prop
->p_name
.av_len
> name
->av_len
&&
3244 !memcmp(prop
->p_name
.av_val
, name
->av_val
, name
->av_len
))
3246 memcpy(p
, prop
, sizeof(*prop
));
3250 if (prop
->p_type
== AMF_OBJECT
)
3252 if (RTMP_FindPrefixProperty(&prop
->p_vu
.p_object
, name
, p
))
3260 DumpMetaData(AMFObject
*obj
)
3262 AMFObjectProperty
*prop
;
3264 for (n
= 0; n
< obj
->o_num
; n
++)
3267 prop
= AMF_GetProp(obj
, NULL
, n
);
3268 switch (prop
->p_type
)
3271 case AMF_ECMA_ARRAY
:
3272 case AMF_STRICT_ARRAY
:
3273 if (prop
->p_name
.av_len
)
3274 RTMP_Log(RTMP_LOGINFO
, "%.*s:", prop
->p_name
.av_len
, prop
->p_name
.av_val
);
3275 DumpMetaData(&prop
->p_vu
.p_object
);
3278 snprintf(str
, 255, "%.2f", prop
->p_vu
.p_number
);
3281 snprintf(str
, 255, "%s",
3282 prop
->p_vu
.p_number
!= 0. ? "TRUE" : "FALSE");
3285 len
= snprintf(str
, 255, "%.*s", prop
->p_vu
.p_aval
.av_len
,
3286 prop
->p_vu
.p_aval
.av_val
);
3287 if (len
>= 1 && str
[len
-1] == '\n')
3291 snprintf(str
, 255, "timestamp:%.2f", prop
->p_vu
.p_number
);
3294 snprintf(str
, 255, "INVALID TYPE 0x%02x",
3295 (unsigned char)prop
->p_type
);
3297 if (str
[0] && prop
->p_name
.av_len
)
3299 RTMP_Log(RTMP_LOGINFO
, " %-22.*s%s", prop
->p_name
.av_len
,
3300 prop
->p_name
.av_val
, str
);
3312 HandleMetadata(RTMP
*r
, char *body
, unsigned int len
)
3314 /* allright we get some info here, so parse it and print it */
3315 /* also keep duration or filesize to make a nice progress bar */
3321 int nRes
= AMF_Decode(&obj
, body
, len
, FALSE
);
3324 RTMP_Log(RTMP_LOGERROR
, "%s, error decoding meta data packet", __FUNCTION__
);
3329 AMFProp_GetString(AMF_GetProp(&obj
, NULL
, 0), &metastring
);
3331 if (AVMATCH(&metastring
, &av_onMetaData
))
3333 AMFObjectProperty prop
;
3335 RTMP_Log(RTMP_LOGINFO
, "Metadata:");
3337 if (RTMP_FindFirstMatchingProperty(&obj
, &av_duration
, &prop
))
3339 r
->m_fDuration
= prop
.p_vu
.p_number
;
3340 /*RTMP_Log(RTMP_LOGDEBUG, "Set duration: %.2f", m_fDuration); */
3342 /* Search for audio or video tags */
3343 if (RTMP_FindPrefixProperty(&obj
, &av_video
, &prop
))
3344 r
->m_read
.dataType
|= 1;
3345 if (RTMP_FindPrefixProperty(&obj
, &av_audio
, &prop
))
3346 r
->m_read
.dataType
|= 4;
3354 HandleChangeChunkSize(RTMP
*r
, const RTMPPacket
*packet
)
3356 if (packet
->m_nBodySize
>= 4)
3358 r
->m_inChunkSize
= AMF_DecodeInt32(packet
->m_body
);
3359 RTMP_Log(RTMP_LOGDEBUG
, "%s, received: chunk size change to %d", __FUNCTION__
,
3365 HandleAudio(RTMP
*r
, const RTMPPacket
*packet
)
3370 HandleVideo(RTMP
*r
, const RTMPPacket
*packet
)
3375 HandleCtrl(RTMP
*r
, const RTMPPacket
*packet
)
3379 if (packet
->m_body
&& packet
->m_nBodySize
>= 2)
3380 nType
= AMF_DecodeInt16(packet
->m_body
);
3381 RTMP_Log(RTMP_LOGDEBUG
, "%s, received ctrl. type: %d, len: %d", __FUNCTION__
, nType
,
3382 packet
->m_nBodySize
);
3383 /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */
3385 if (packet
->m_nBodySize
>= 6)
3390 tmp
= AMF_DecodeInt32(packet
->m_body
+ 2);
3391 RTMP_Log(RTMP_LOGDEBUG
, "%s, Stream Begin %d", __FUNCTION__
, tmp
);
3395 tmp
= AMF_DecodeInt32(packet
->m_body
+ 2);
3396 RTMP_Log(RTMP_LOGDEBUG
, "%s, Stream EOF %d", __FUNCTION__
, tmp
);
3397 if (r
->m_pausing
== 1)
3402 tmp
= AMF_DecodeInt32(packet
->m_body
+ 2);
3403 RTMP_Log(RTMP_LOGDEBUG
, "%s, Stream Dry %d", __FUNCTION__
, tmp
);
3407 tmp
= AMF_DecodeInt32(packet
->m_body
+ 2);
3408 RTMP_Log(RTMP_LOGDEBUG
, "%s, Stream IsRecorded %d", __FUNCTION__
, tmp
);
3411 case 6: /* server ping. reply with pong. */
3412 tmp
= AMF_DecodeInt32(packet
->m_body
+ 2);
3413 RTMP_Log(RTMP_LOGDEBUG
, "%s, Ping %d", __FUNCTION__
, tmp
);
3414 RTMP_SendCtrl(r
, 0x07, tmp
, 0);
3417 /* FMS 3.5 servers send the following two controls to let the client
3418 * know when the server has sent a complete buffer. I.e., when the
3419 * server has sent an amount of data equal to m_nBufferMS in duration.
3420 * The server meters its output so that data arrives at the client
3421 * in realtime and no faster.
3423 * The rtmpdump program tries to set m_nBufferMS as large as
3424 * possible, to force the server to send data as fast as possible.
3425 * In practice, the server appears to cap this at about 1 hour's
3426 * worth of data. After the server has sent a complete buffer, and
3427 * sends this BufferEmpty message, it will wait until the play
3428 * duration of that buffer has passed before sending a new buffer.
3429 * The BufferReady message will be sent when the new buffer starts.
3430 * (There is no BufferReady message for the very first buffer;
3431 * presumably the Stream Begin message is sufficient for that
3434 * If the network speed is much faster than the data bitrate, then
3435 * there may be long delays between the end of one buffer and the
3436 * start of the next.
3438 * Since usually the network allows data to be sent at
3439 * faster than realtime, and rtmpdump wants to download the data
3440 * as fast as possible, we use this RTMP_LF_BUFX hack: when we
3441 * get the BufferEmpty message, we send a Pause followed by an
3442 * Unpause. This causes the server to send the next buffer immediately
3443 * instead of waiting for the full duration to elapse. (That's
3444 * also the purpose of the ToggleStream function, which rtmpdump
3445 * calls if we get a read timeout.)
3447 * Media player apps don't need this hack since they are just
3448 * going to play the data in realtime anyway. It also doesn't work
3449 * for live streams since they obviously can only be sent in
3450 * realtime. And it's all moot if the network speed is actually
3451 * slower than the media bitrate.
3454 tmp
= AMF_DecodeInt32(packet
->m_body
+ 2);
3455 RTMP_Log(RTMP_LOGDEBUG
, "%s, Stream BufferEmpty %d", __FUNCTION__
, tmp
);
3456 if (!(r
->Link
.lFlags
& RTMP_LF_BUFX
))
3460 r
->m_pauseStamp
= r
->m_mediaChannel
< r
->m_channelsAllocatedIn
?
3461 r
->m_channelTimestamp
[r
->m_mediaChannel
] : 0;
3462 RTMP_SendPause(r
, TRUE
, r
->m_pauseStamp
);
3465 else if (r
->m_pausing
== 2)
3467 RTMP_SendPause(r
, FALSE
, r
->m_pauseStamp
);
3473 tmp
= AMF_DecodeInt32(packet
->m_body
+ 2);
3474 RTMP_Log(RTMP_LOGDEBUG
, "%s, Stream BufferReady %d", __FUNCTION__
, tmp
);
3478 tmp
= AMF_DecodeInt32(packet
->m_body
+ 2);
3479 RTMP_Log(RTMP_LOGDEBUG
, "%s, Stream xx %d", __FUNCTION__
, tmp
);
3487 RTMP_Log(RTMP_LOGDEBUG
, "%s, SWFVerification ping received: ", __FUNCTION__
);
3488 if (packet
->m_nBodySize
> 2 && packet
->m_body
[2] > 0x01)
3490 RTMP_Log(RTMP_LOGERROR
,
3491 "%s: SWFVerification Type %d request not supported! Patches welcome...",
3492 __FUNCTION__
, packet
->m_body
[2]);
3495 /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */
3497 /* respond with HMAC SHA256 of decompressed SWF, key is the 30byte player key, also the last 30 bytes of the server handshake are applied */
3498 else if (r
->Link
.SWFSize
)
3500 RTMP_SendCtrl(r
, 0x1B, 0, 0);
3504 RTMP_Log(RTMP_LOGERROR
,
3505 "%s: Ignoring SWFVerification request, use --swfVfy!",
3509 RTMP_Log(RTMP_LOGERROR
,
3510 "%s: Ignoring SWFVerification request, no CRYPTO support!",
3517 HandleServerBW(RTMP
*r
, const RTMPPacket
*packet
)
3519 r
->m_nServerBW
= AMF_DecodeInt32(packet
->m_body
);
3520 RTMP_Log(RTMP_LOGDEBUG
, "%s: server BW = %d", __FUNCTION__
, r
->m_nServerBW
);
3524 HandleClientBW(RTMP
*r
, const RTMPPacket
*packet
)
3526 r
->m_nClientBW
= AMF_DecodeInt32(packet
->m_body
);
3527 if (packet
->m_nBodySize
> 4)
3528 r
->m_nClientBW2
= packet
->m_body
[4];
3530 r
->m_nClientBW2
= -1;
3531 RTMP_Log(RTMP_LOGDEBUG
, "%s: client BW = %d %d", __FUNCTION__
, r
->m_nClientBW
,
3536 DecodeInt32LE(const char *data
)
3538 unsigned char *c
= (unsigned char *)data
;
3541 val
= (c
[3] << 24) | (c
[2] << 16) | (c
[1] << 8) | c
[0];
3546 EncodeInt32LE(char *output
, int nVal
)
3559 RTMP_ReadPacket(RTMP
*r
, RTMPPacket
*packet
)
3561 uint8_t hbuf
[RTMP_MAX_HEADER_SIZE
] = { 0 };
3562 char *header
= (char *)hbuf
;
3563 int nSize
, hSize
, nToRead
, nChunk
;
3564 int didAlloc
= FALSE
;
3566 RTMP_Log(RTMP_LOGDEBUG2
, "%s: fd=%d", __FUNCTION__
, r
->m_sb
.sb_socket
);
3568 if (ReadN(r
, (char *)hbuf
, 1) == 0)
3570 RTMP_Log(RTMP_LOGERROR
, "%s, failed to read RTMP packet header", __FUNCTION__
);
3574 packet
->m_headerType
= (hbuf
[0] & 0xc0) >> 6;
3575 packet
->m_nChannel
= (hbuf
[0] & 0x3f);
3577 if (packet
->m_nChannel
== 0)
3579 if (ReadN(r
, (char *)&hbuf
[1], 1) != 1)
3581 RTMP_Log(RTMP_LOGERROR
, "%s, failed to read RTMP packet header 2nd byte",
3585 packet
->m_nChannel
= hbuf
[1];
3586 packet
->m_nChannel
+= 64;
3589 else if (packet
->m_nChannel
== 1)
3592 if (ReadN(r
, (char *)&hbuf
[1], 2) != 2)
3594 RTMP_Log(RTMP_LOGERROR
, "%s, failed to read RTMP packet header 3nd byte",
3598 tmp
= (hbuf
[2] << 8) + hbuf
[1];
3599 packet
->m_nChannel
= tmp
+ 64;
3600 RTMP_Log(RTMP_LOGDEBUG
, "%s, m_nChannel: %0x", __FUNCTION__
, packet
->m_nChannel
);
3604 nSize
= packetSize
[packet
->m_headerType
];
3606 if (packet
->m_nChannel
>= r
->m_channelsAllocatedIn
)
3608 int n
= packet
->m_nChannel
+ 10;
3609 int *timestamp
= realloc(r
->m_channelTimestamp
, sizeof(int) * n
);
3610 RTMPPacket
**packets
= realloc(r
->m_vecChannelsIn
, sizeof(RTMPPacket
*) * n
);
3612 free(r
->m_channelTimestamp
);
3614 free(r
->m_vecChannelsIn
);
3615 r
->m_channelTimestamp
= timestamp
;
3616 r
->m_vecChannelsIn
= packets
;
3617 if (!timestamp
|| !packets
) {
3618 r
->m_channelsAllocatedIn
= 0;
3621 memset(r
->m_channelTimestamp
+ r
->m_channelsAllocatedIn
, 0, sizeof(int) * (n
- r
->m_channelsAllocatedIn
));
3622 memset(r
->m_vecChannelsIn
+ r
->m_channelsAllocatedIn
, 0, sizeof(RTMPPacket
*) * (n
- r
->m_channelsAllocatedIn
));
3623 r
->m_channelsAllocatedIn
= n
;
3626 if (nSize
== RTMP_LARGE_HEADER_SIZE
) /* if we get a full header the timestamp is absolute */
3627 packet
->m_hasAbsTimestamp
= TRUE
;
3629 else if (nSize
< RTMP_LARGE_HEADER_SIZE
)
3630 { /* using values from the last message of this channel */
3631 if (r
->m_vecChannelsIn
[packet
->m_nChannel
])
3632 memcpy(packet
, r
->m_vecChannelsIn
[packet
->m_nChannel
],
3633 sizeof(RTMPPacket
));
3638 if (nSize
> 0 && ReadN(r
, header
, nSize
) != nSize
)
3640 RTMP_Log(RTMP_LOGERROR
, "%s, failed to read RTMP packet header. type: %x",
3641 __FUNCTION__
, (unsigned int)hbuf
[0]);
3645 hSize
= nSize
+ (header
- (char *)hbuf
);
3649 packet
->m_nTimeStamp
= AMF_DecodeInt24(header
);
3651 /*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); */
3655 packet
->m_nBodySize
= AMF_DecodeInt24(header
+ 3);
3656 packet
->m_nBytesRead
= 0;
3657 RTMPPacket_Free(packet
);
3661 packet
->m_packetType
= header
[6];
3664 packet
->m_nInfoField2
= DecodeInt32LE(header
+ 7);
3667 if (packet
->m_nTimeStamp
== 0xffffff)
3669 if (ReadN(r
, header
+ nSize
, 4) != 4)
3671 RTMP_Log(RTMP_LOGERROR
, "%s, failed to read extended timestamp",
3675 packet
->m_nTimeStamp
= AMF_DecodeInt32(header
+ nSize
);
3680 RTMP_LogHexString(RTMP_LOGDEBUG2
, (uint8_t *)hbuf
, hSize
);
3682 if (packet
->m_nBodySize
> 0 && packet
->m_body
== NULL
)
3684 if (!RTMPPacket_Alloc(packet
, packet
->m_nBodySize
))
3686 RTMP_Log(RTMP_LOGDEBUG
, "%s, failed to allocate packet", __FUNCTION__
);
3690 packet
->m_headerType
= (hbuf
[0] & 0xc0) >> 6;
3693 nToRead
= packet
->m_nBodySize
- packet
->m_nBytesRead
;
3694 nChunk
= r
->m_inChunkSize
;
3695 if (nToRead
< nChunk
)
3698 /* Does the caller want the raw chunk? */
3699 if (packet
->m_chunk
)
3701 packet
->m_chunk
->c_headerSize
= hSize
;
3702 memcpy(packet
->m_chunk
->c_header
, hbuf
, hSize
);
3703 packet
->m_chunk
->c_chunk
= packet
->m_body
+ packet
->m_nBytesRead
;
3704 packet
->m_chunk
->c_chunkSize
= nChunk
;
3707 if (ReadN(r
, packet
->m_body
+ packet
->m_nBytesRead
, nChunk
) != nChunk
)
3709 RTMP_Log(RTMP_LOGERROR
, "%s, failed to read RTMP packet body. len: %u",
3710 __FUNCTION__
, packet
->m_nBodySize
);
3714 RTMP_LogHexString(RTMP_LOGDEBUG2
, (uint8_t *)packet
->m_body
+ packet
->m_nBytesRead
, nChunk
);
3716 packet
->m_nBytesRead
+= nChunk
;
3718 /* keep the packet as ref for other packets on this channel */
3719 if (!r
->m_vecChannelsIn
[packet
->m_nChannel
])
3720 r
->m_vecChannelsIn
[packet
->m_nChannel
] = malloc(sizeof(RTMPPacket
));
3721 memcpy(r
->m_vecChannelsIn
[packet
->m_nChannel
], packet
, sizeof(RTMPPacket
));
3723 if (RTMPPacket_IsReady(packet
))
3725 /* make packet's timestamp absolute */
3726 if (!packet
->m_hasAbsTimestamp
)
3727 packet
->m_nTimeStamp
+= r
->m_channelTimestamp
[packet
->m_nChannel
]; /* timestamps seem to be always relative!! */
3729 r
->m_channelTimestamp
[packet
->m_nChannel
] = packet
->m_nTimeStamp
;
3731 /* reset the data from the stored packet. we keep the header since we may use it later if a new packet for this channel */
3732 /* arrives and requests to re-use some info (small packet header) */
3733 r
->m_vecChannelsIn
[packet
->m_nChannel
]->m_body
= NULL
;
3734 r
->m_vecChannelsIn
[packet
->m_nChannel
]->m_nBytesRead
= 0;
3735 r
->m_vecChannelsIn
[packet
->m_nChannel
]->m_hasAbsTimestamp
= FALSE
; /* can only be false if we reuse header */
3739 packet
->m_body
= NULL
; /* so it won't be erased on free */
3747 HandShake(RTMP
*r
, int FP9HandShake
)
3750 uint32_t uptime
, suptime
;
3753 char clientbuf
[RTMP_SIG_SIZE
+ 1], *clientsig
= clientbuf
+ 1;
3754 char serversig
[RTMP_SIG_SIZE
];
3756 clientbuf
[0] = 0x03; /* not encrypted */
3758 uptime
= htonl(RTMP_GetTime());
3759 memcpy(clientsig
, &uptime
, 4);
3761 memset(&clientsig
[4], 0, 4);
3764 for (i
= 8; i
< RTMP_SIG_SIZE
; i
++)
3765 clientsig
[i
] = 0xff;
3767 for (i
= 8; i
< RTMP_SIG_SIZE
; i
++)
3768 clientsig
[i
] = (char)(rand() % 256);
3771 if (!WriteN(r
, clientbuf
, RTMP_SIG_SIZE
+ 1))
3774 if (ReadN(r
, &type
, 1) != 1) /* 0x03 or 0x06 */
3777 RTMP_Log(RTMP_LOGDEBUG
, "%s: Type Answer : %02X", __FUNCTION__
, type
);
3779 if (type
!= clientbuf
[0])
3780 RTMP_Log(RTMP_LOGWARNING
, "%s: Type mismatch: client sent %d, server answered %d",
3781 __FUNCTION__
, clientbuf
[0], type
);
3783 if (ReadN(r
, serversig
, RTMP_SIG_SIZE
) != RTMP_SIG_SIZE
)
3786 /* decode server response */
3788 memcpy(&suptime
, serversig
, 4);
3789 suptime
= ntohl(suptime
);
3791 RTMP_Log(RTMP_LOGDEBUG
, "%s: Server Uptime : %d", __FUNCTION__
, suptime
);
3792 RTMP_Log(RTMP_LOGDEBUG
, "%s: FMS Version : %d.%d.%d.%d", __FUNCTION__
,
3793 serversig
[4], serversig
[5], serversig
[6], serversig
[7]);
3795 /* 2nd part of handshake */
3796 if (!WriteN(r
, serversig
, RTMP_SIG_SIZE
))
3799 if (ReadN(r
, serversig
, RTMP_SIG_SIZE
) != RTMP_SIG_SIZE
)
3802 bMatch
= (memcmp(serversig
, clientsig
, RTMP_SIG_SIZE
) == 0);
3805 RTMP_Log(RTMP_LOGWARNING
, "%s, client signature does not match!", __FUNCTION__
);
3814 char serverbuf
[RTMP_SIG_SIZE
+ 1], *serversig
= serverbuf
+ 1;
3815 char clientsig
[RTMP_SIG_SIZE
];
3819 if (ReadN(r
, serverbuf
, 1) != 1) /* 0x03 or 0x06 */
3822 RTMP_Log(RTMP_LOGDEBUG
, "%s: Type Request : %02X", __FUNCTION__
, serverbuf
[0]);
3824 if (serverbuf
[0] != 3)
3826 RTMP_Log(RTMP_LOGERROR
, "%s: Type unknown: client sent %02X",
3827 __FUNCTION__
, serverbuf
[0]);
3831 uptime
= htonl(RTMP_GetTime());
3832 memcpy(serversig
, &uptime
, 4);
3834 memset(&serversig
[4], 0, 4);
3836 for (i
= 8; i
< RTMP_SIG_SIZE
; i
++)
3837 serversig
[i
] = 0xff;
3839 for (i
= 8; i
< RTMP_SIG_SIZE
; i
++)
3840 serversig
[i
] = (char)(rand() % 256);
3843 if (!WriteN(r
, serverbuf
, RTMP_SIG_SIZE
+ 1))
3846 if (ReadN(r
, clientsig
, RTMP_SIG_SIZE
) != RTMP_SIG_SIZE
)
3849 /* decode client response */
3851 memcpy(&uptime
, clientsig
, 4);
3852 uptime
= ntohl(uptime
);
3854 RTMP_Log(RTMP_LOGDEBUG
, "%s: Client Uptime : %d", __FUNCTION__
, uptime
);
3855 RTMP_Log(RTMP_LOGDEBUG
, "%s: Player Version: %d.%d.%d.%d", __FUNCTION__
,
3856 clientsig
[4], clientsig
[5], clientsig
[6], clientsig
[7]);
3858 /* 2nd part of handshake */
3859 if (!WriteN(r
, clientsig
, RTMP_SIG_SIZE
))
3862 if (ReadN(r
, clientsig
, RTMP_SIG_SIZE
) != RTMP_SIG_SIZE
)
3865 bMatch
= (memcmp(serversig
, clientsig
, RTMP_SIG_SIZE
) == 0);
3868 RTMP_Log(RTMP_LOGWARNING
, "%s, client signature does not match!", __FUNCTION__
);
3875 RTMP_SendChunk(RTMP
*r
, RTMPChunk
*chunk
)
3878 char hbuf
[RTMP_MAX_HEADER_SIZE
];
3880 RTMP_Log(RTMP_LOGDEBUG2
, "%s: fd=%d, size=%d", __FUNCTION__
, r
->m_sb
.sb_socket
,
3881 chunk
->c_chunkSize
);
3882 RTMP_LogHexString(RTMP_LOGDEBUG2
, (uint8_t *)chunk
->c_header
, chunk
->c_headerSize
);
3883 if (chunk
->c_chunkSize
)
3885 char *ptr
= chunk
->c_chunk
- chunk
->c_headerSize
;
3886 RTMP_LogHexString(RTMP_LOGDEBUG2
, (uint8_t *)chunk
->c_chunk
, chunk
->c_chunkSize
);
3887 /* save header bytes we're about to overwrite */
3888 memcpy(hbuf
, ptr
, chunk
->c_headerSize
);
3889 memcpy(ptr
, chunk
->c_header
, chunk
->c_headerSize
);
3890 wrote
= WriteN(r
, ptr
, chunk
->c_headerSize
+ chunk
->c_chunkSize
);
3891 memcpy(ptr
, hbuf
, chunk
->c_headerSize
);
3894 wrote
= WriteN(r
, chunk
->c_header
, chunk
->c_headerSize
);
3899 RTMP_SendPacket(RTMP
*r
, RTMPPacket
*packet
, int queue
)
3901 const RTMPPacket
*prevPacket
;
3905 char *header
, *hptr
, *hend
, hbuf
[RTMP_MAX_HEADER_SIZE
], c
;
3907 char *buffer
, *tbuf
= NULL
, *toff
= NULL
;
3911 if (packet
->m_nChannel
>= r
->m_channelsAllocatedOut
)
3913 int n
= packet
->m_nChannel
+ 10;
3914 RTMPPacket
**packets
= realloc(r
->m_vecChannelsOut
, sizeof(RTMPPacket
*) * n
);
3916 free(r
->m_vecChannelsOut
);
3917 r
->m_vecChannelsOut
= NULL
;
3918 r
->m_channelsAllocatedOut
= 0;
3921 r
->m_vecChannelsOut
= packets
;
3922 memset(r
->m_vecChannelsOut
+ r
->m_channelsAllocatedOut
, 0, sizeof(RTMPPacket
*) * (n
- r
->m_channelsAllocatedOut
));
3923 r
->m_channelsAllocatedOut
= n
;
3926 prevPacket
= r
->m_vecChannelsOut
[packet
->m_nChannel
];
3927 if (prevPacket
&& packet
->m_headerType
!= RTMP_PACKET_SIZE_LARGE
)
3929 /* compress a bit by using the prev packet's attributes */
3930 if (prevPacket
->m_nBodySize
== packet
->m_nBodySize
3931 && prevPacket
->m_packetType
== packet
->m_packetType
3932 && packet
->m_headerType
== RTMP_PACKET_SIZE_MEDIUM
)
3933 packet
->m_headerType
= RTMP_PACKET_SIZE_SMALL
;
3935 if (prevPacket
->m_nTimeStamp
== packet
->m_nTimeStamp
3936 && packet
->m_headerType
== RTMP_PACKET_SIZE_SMALL
)
3937 packet
->m_headerType
= RTMP_PACKET_SIZE_MINIMUM
;
3938 last
= prevPacket
->m_nTimeStamp
;
3941 if (packet
->m_headerType
> 3) /* sanity */
3943 RTMP_Log(RTMP_LOGERROR
, "sanity failed!! trying to send header of type: 0x%02x.",
3944 (unsigned char)packet
->m_headerType
);
3948 nSize
= packetSize
[packet
->m_headerType
];
3949 hSize
= nSize
; cSize
= 0;
3950 t
= packet
->m_nTimeStamp
- last
;
3954 header
= packet
->m_body
- nSize
;
3955 hend
= packet
->m_body
;
3960 hend
= hbuf
+ sizeof(hbuf
);
3963 if (packet
->m_nChannel
> 319)
3965 else if (packet
->m_nChannel
> 63)
3973 if (nSize
> 1 && t
>= 0xffffff)
3980 c
= packet
->m_headerType
<< 6;
3984 c
|= packet
->m_nChannel
;
3995 int tmp
= packet
->m_nChannel
- 64;
3996 *hptr
++ = tmp
& 0xff;
4003 hptr
= AMF_EncodeInt24(hptr
, hend
, t
> 0xffffff ? 0xffffff : t
);
4008 hptr
= AMF_EncodeInt24(hptr
, hend
, packet
->m_nBodySize
);
4009 *hptr
++ = packet
->m_packetType
;
4013 hptr
+= EncodeInt32LE(hptr
, packet
->m_nInfoField2
);
4015 if (nSize
> 1 && t
>= 0xffffff)
4016 hptr
= AMF_EncodeInt32(hptr
, hend
, t
);
4018 nSize
= packet
->m_nBodySize
;
4019 buffer
= packet
->m_body
;
4020 nChunkSize
= r
->m_outChunkSize
;
4022 RTMP_Log(RTMP_LOGDEBUG2
, "%s: fd=%d, size=%d", __FUNCTION__
, r
->m_sb
.sb_socket
,
4024 /* send all chunks in one HTTP request */
4025 if (r
->Link
.protocol
& RTMP_FEATURE_HTTP
)
4027 int chunks
= (nSize
+nChunkSize
-1) / nChunkSize
;
4030 tlen
= chunks
* (cSize
+ 1) + nSize
+ hSize
;
4031 tbuf
= malloc(tlen
);
4037 while (nSize
+ hSize
)
4041 if (nSize
< nChunkSize
)
4044 RTMP_LogHexString(RTMP_LOGDEBUG2
, (uint8_t *)header
, hSize
);
4045 RTMP_LogHexString(RTMP_LOGDEBUG2
, (uint8_t *)buffer
, nChunkSize
);
4048 memcpy(toff
, header
, nChunkSize
+ hSize
);
4049 toff
+= nChunkSize
+ hSize
;
4053 wrote
= WriteN(r
, header
, nChunkSize
+ hSize
);
4057 nSize
-= nChunkSize
;
4058 buffer
+= nChunkSize
;
4063 header
= buffer
- 1;
4070 *header
= (0xc0 | c
);
4073 int tmp
= packet
->m_nChannel
- 64;
4074 header
[1] = tmp
& 0xff;
4076 header
[2] = tmp
>> 8;
4082 int wrote
= WriteN(r
, tbuf
, toff
-tbuf
);
4089 /* we invoked a remote method */
4090 if (packet
->m_packetType
== RTMP_PACKET_TYPE_INVOKE
)
4094 ptr
= packet
->m_body
+ 1;
4095 AMF_DecodeString(ptr
, &method
);
4096 RTMP_Log(RTMP_LOGDEBUG
, "Invoking %s", method
.av_val
);
4097 /* keep it in call queue till result arrives */
4100 ptr
+= 3 + method
.av_len
;
4101 txn
= (int)AMF_DecodeNumber(ptr
);
4102 AV_queue(&r
->m_methodCalls
, &r
->m_numCalls
, &method
, txn
);
4106 if (!r
->m_vecChannelsOut
[packet
->m_nChannel
])
4107 r
->m_vecChannelsOut
[packet
->m_nChannel
] = malloc(sizeof(RTMPPacket
));
4108 memcpy(r
->m_vecChannelsOut
[packet
->m_nChannel
], packet
, sizeof(RTMPPacket
));
4115 return SHandShake(r
);
4123 if (RTMP_IsConnected(r
))
4125 if (r
->m_stream_id
> 0)
4129 if ((r
->Link
.protocol
& RTMP_FEATURE_WRITE
))
4131 SendDeleteStream(r
, i
);
4133 if (r
->m_clientID
.av_val
)
4135 HTTP_Post(r
, RTMPT_CLOSE
, "", 1);
4136 free(r
->m_clientID
.av_val
);
4137 r
->m_clientID
.av_val
= NULL
;
4138 r
->m_clientID
.av_len
= 0;
4140 RTMPSockBuf_Close(&r
->m_sb
);
4143 r
->m_stream_id
= -1;
4144 r
->m_sb
.sb_socket
= -1;
4145 r
->m_nBWCheckCounter
= 0;
4147 r
->m_nBytesInSent
= 0;
4149 if (r
->m_read
.flags
& RTMP_READ_HEADER
) {
4150 free(r
->m_read
.buf
);
4151 r
->m_read
.buf
= NULL
;
4153 r
->m_read
.dataType
= 0;
4154 r
->m_read
.flags
= 0;
4155 r
->m_read
.status
= 0;
4156 r
->m_read
.nResumeTS
= 0;
4157 r
->m_read
.nIgnoredFrameCounter
= 0;
4158 r
->m_read
.nIgnoredFlvFrameCounter
= 0;
4160 r
->m_write
.m_nBytesRead
= 0;
4161 RTMPPacket_Free(&r
->m_write
);
4163 for (i
= 0; i
< r
->m_channelsAllocatedIn
; i
++)
4165 if (r
->m_vecChannelsIn
[i
])
4167 RTMPPacket_Free(r
->m_vecChannelsIn
[i
]);
4168 free(r
->m_vecChannelsIn
[i
]);
4169 r
->m_vecChannelsIn
[i
] = NULL
;
4172 free(r
->m_vecChannelsIn
);
4173 r
->m_vecChannelsIn
= NULL
;
4174 free(r
->m_channelTimestamp
);
4175 r
->m_channelTimestamp
= NULL
;
4176 r
->m_channelsAllocatedIn
= 0;
4177 for (i
= 0; i
< r
->m_channelsAllocatedOut
; i
++)
4179 if (r
->m_vecChannelsOut
[i
])
4181 free(r
->m_vecChannelsOut
[i
]);
4182 r
->m_vecChannelsOut
[i
] = NULL
;
4185 free(r
->m_vecChannelsOut
);
4186 r
->m_vecChannelsOut
= NULL
;
4187 r
->m_channelsAllocatedOut
= 0;
4188 AV_clear(r
->m_methodCalls
, r
->m_numCalls
);
4189 r
->m_methodCalls
= NULL
;
4191 r
->m_numInvokes
= 0;
4193 r
->m_bPlaying
= FALSE
;
4194 r
->m_sb
.sb_size
= 0;
4196 r
->m_msgCounter
= 0;
4200 if (r
->Link
.lFlags
& RTMP_LF_FTCU
)
4202 free(r
->Link
.tcUrl
.av_val
);
4203 r
->Link
.tcUrl
.av_val
= NULL
;
4204 r
->Link
.lFlags
^= RTMP_LF_FTCU
;
4208 if (!(r
->Link
.protocol
& RTMP_FEATURE_WRITE
) || (r
->Link
.pFlags
& RTMP_PUB_CLEAN
))
4210 free(r
->Link
.playpath0
.av_val
);
4211 r
->Link
.playpath0
.av_val
= NULL
;
4213 if ((r
->Link
.protocol
& RTMP_FEATURE_WRITE
) &&
4214 (r
->Link
.pFlags
& RTMP_PUB_CLEAN
) &&
4215 (r
->Link
.pFlags
& RTMP_PUB_ALLOC
))
4217 free(r
->Link
.app
.av_val
);
4218 r
->Link
.app
.av_val
= NULL
;
4219 free(r
->Link
.tcUrl
.av_val
);
4220 r
->Link
.tcUrl
.av_val
= NULL
;
4224 MDH_free(r
->Link
.dh
);
4227 if (r
->Link
.rc4keyIn
)
4229 RC4_free(r
->Link
.rc4keyIn
);
4230 r
->Link
.rc4keyIn
= NULL
;
4232 if (r
->Link
.rc4keyOut
)
4234 RC4_free(r
->Link
.rc4keyOut
);
4235 r
->Link
.rc4keyOut
= NULL
;
4238 free(r
->Link
.playpath0
.av_val
);
4239 r
->Link
.playpath0
.av_val
= NULL
;
4244 RTMPSockBuf_Fill(RTMPSockBuf
*sb
)
4249 sb
->sb_start
= sb
->sb_buf
;
4253 nBytes
= sizeof(sb
->sb_buf
) - 1 - sb
->sb_size
- (sb
->sb_start
- sb
->sb_buf
);
4254 #if defined(CRYPTO) && !defined(NO_SSL)
4257 nBytes
= TLS_read(sb
->sb_ssl
, sb
->sb_start
+ sb
->sb_size
, nBytes
);
4262 nBytes
= recv(sb
->sb_socket
, sb
->sb_start
+ sb
->sb_size
, nBytes
, 0);
4266 sb
->sb_size
+= nBytes
;
4270 int sockerr
= GetSockError();
4271 RTMP_Log(RTMP_LOGDEBUG
, "%s, recv returned %d. GetSockError(): %d (%s)",
4272 __FUNCTION__
, nBytes
, sockerr
, strerror(sockerr
));
4273 if (sockerr
== EINTR
&& !RTMP_ctrlC
)
4276 if (sockerr
== EWOULDBLOCK
|| sockerr
== EAGAIN
)
4278 sb
->sb_timedout
= TRUE
;
4289 RTMPSockBuf_Send(RTMPSockBuf
*sb
, const char *buf
, int len
)
4294 fwrite(buf
, 1, len
, netstackdump
);
4297 #if defined(CRYPTO) && !defined(NO_SSL)
4300 rc
= TLS_write(sb
->sb_ssl
, buf
, len
);
4305 rc
= send(sb
->sb_socket
, buf
, len
, 0);
4311 RTMPSockBuf_Close(RTMPSockBuf
*sb
)
4313 #if defined(CRYPTO) && !defined(NO_SSL)
4316 TLS_shutdown(sb
->sb_ssl
);
4317 TLS_close(sb
->sb_ssl
);
4321 if (sb
->sb_socket
!= -1)
4322 return closesocket(sb
->sb_socket
);
4326 #define HEX2BIN(a) (((a)&0x40)?((a)&0xf)+9:((a)&0xf))
4329 DecodeTEA(AVal
*key
, AVal
*text
)
4331 uint32_t *v
, k
[4] = { 0 }, u
;
4332 uint32_t z
, y
, sum
= 0, e
, DELTA
= 0x9e3779b9;
4335 unsigned char *ptr
, *out
;
4337 /* prep key: pack 1st 16 chars into 4 LittleEndian ints */
4338 ptr
= (unsigned char *)key
->av_val
;
4342 p
= key
->av_len
> 16 ? 16 : key
->av_len
;
4343 for (i
= 0; i
< p
; i
++)
4345 u
|= ptr
[i
] << (n
* 8);
4357 /* any trailing chars */
4361 /* prep text: hex2bin, multiples of 4 */
4362 n
= (text
->av_len
+ 7) / 8;
4363 out
= malloc(n
* 8);
4364 ptr
= (unsigned char *)text
->av_val
;
4365 v
= (uint32_t *) out
;
4366 for (i
= 0; i
< n
; i
++)
4368 u
= (HEX2BIN(ptr
[0]) << 4) + HEX2BIN(ptr
[1]);
4369 u
|= ((HEX2BIN(ptr
[2]) << 4) + HEX2BIN(ptr
[3])) << 8;
4370 u
|= ((HEX2BIN(ptr
[4]) << 4) + HEX2BIN(ptr
[5])) << 16;
4371 u
|= ((HEX2BIN(ptr
[6]) << 4) + HEX2BIN(ptr
[7])) << 24;
4375 v
= (uint32_t *) out
;
4377 /* http://www.movable-type.co.uk/scripts/tea-block.html */
4378 #define MX (((z>>5)^(y<<2)) + ((y>>3)^(z<<4))) ^ ((sum^y) + (k[(p&3)^e]^z));
4386 for (p
= n
- 1; p
> 0; p
--)
4387 z
= v
[p
- 1], y
= v
[p
] -= MX
;
4394 memcpy(text
->av_val
, out
, text
->av_len
);
4399 HTTP_Post(RTMP
*r
, RTMPTCmd cmd
, const char *buf
, int len
)
4402 int hlen
= snprintf(hbuf
, sizeof(hbuf
), "POST /%s%s/%d HTTP/1.1\r\n"
4405 "User-Agent: Shockwave Flash\r\n"
4406 "Connection: Keep-Alive\r\n"
4407 "Cache-Control: no-cache\r\n"
4408 "Content-type: application/x-fcs\r\n"
4409 "Content-length: %d\r\n\r\n", RTMPT_cmds
[cmd
],
4410 r
->m_clientID
.av_val
? r
->m_clientID
.av_val
: "",
4411 r
->m_msgCounter
, r
->Link
.hostname
.av_len
, r
->Link
.hostname
.av_val
,
4413 RTMPSockBuf_Send(&r
->m_sb
, hbuf
, hlen
);
4414 hlen
= RTMPSockBuf_Send(&r
->m_sb
, buf
, len
);
4421 HTTP_read(RTMP
*r
, int fill
)
4428 RTMPSockBuf_Fill(&r
->m_sb
);
4429 if (r
->m_sb
.sb_size
< 13) {
4434 if (strncmp(r
->m_sb
.sb_start
, "HTTP/1.1 200 ", 13))
4436 r
->m_sb
.sb_start
[r
->m_sb
.sb_size
] = '\0';
4437 if (!strstr(r
->m_sb
.sb_start
, "\r\n\r\n")) {
4443 ptr
= r
->m_sb
.sb_start
+ sizeof("HTTP/1.1 200");
4444 while ((ptr
= strstr(ptr
, "Content-"))) {
4445 if (!strncasecmp(ptr
+8, "length:", 7)) break;
4450 hlen
= atoi(ptr
+16);
4451 ptr
= strstr(ptr
+16, "\r\n\r\n");
4455 if (ptr
+ (r
->m_clientID
.av_val
? 1 : hlen
) > r
->m_sb
.sb_start
+ r
->m_sb
.sb_size
)
4461 r
->m_sb
.sb_size
-= ptr
- r
->m_sb
.sb_start
;
4462 r
->m_sb
.sb_start
= ptr
;
4465 if (!r
->m_clientID
.av_val
)
4467 r
->m_clientID
.av_len
= hlen
;
4468 r
->m_clientID
.av_val
= malloc(hlen
+1);
4469 if (!r
->m_clientID
.av_val
)
4471 r
->m_clientID
.av_val
[0] = '/';
4472 memcpy(r
->m_clientID
.av_val
+1, ptr
, hlen
-1);
4473 r
->m_clientID
.av_val
[hlen
] = 0;
4474 r
->m_sb
.sb_size
= 0;
4478 r
->m_polling
= *ptr
++;
4479 r
->m_resplen
= hlen
- 1;
4486 #define MAX_IGNORED_FRAMES 50
4488 /* Read from the stream until we get a media packet.
4489 * Returns -3 if Play.Close/Stop, -2 if fatal error, -1 if no more media
4490 * packets, 0 if ignorable error, >0 if there is a media packet
4493 Read_1_Packet(RTMP
*r
, char *buf
, unsigned int buflen
)
4495 uint32_t prevTagSize
= 0;
4496 int rtnGetNextMediaPacket
= 0, ret
= RTMP_READ_EOF
;
4497 RTMPPacket packet
= { 0 };
4501 uint32_t nTimeStamp
= 0;
4504 rtnGetNextMediaPacket
= RTMP_GetNextMediaPacket(r
, &packet
);
4505 while (rtnGetNextMediaPacket
)
4507 char *packetBody
= packet
.m_body
;
4508 unsigned int nPacketLen
= packet
.m_nBodySize
;
4510 /* Return RTMP_READ_COMPLETE if this was completed nicely with
4511 * invoke message Play.Stop or Play.Complete
4513 if (rtnGetNextMediaPacket
== 2)
4515 RTMP_Log(RTMP_LOGDEBUG
,
4516 "Got Play.Complete or Play.Stop from server. "
4517 "Assuming stream is complete");
4518 ret
= RTMP_READ_COMPLETE
;
4522 r
->m_read
.dataType
|= (((packet
.m_packetType
== RTMP_PACKET_TYPE_AUDIO
) << 2) |
4523 (packet
.m_packetType
== RTMP_PACKET_TYPE_VIDEO
));
4525 if (packet
.m_packetType
== RTMP_PACKET_TYPE_VIDEO
&& nPacketLen
<= 5)
4527 RTMP_Log(RTMP_LOGDEBUG
, "ignoring too small video packet: size: %d",
4529 ret
= RTMP_READ_IGNORE
;
4532 if (packet
.m_packetType
== RTMP_PACKET_TYPE_AUDIO
&& nPacketLen
<= 1)
4534 RTMP_Log(RTMP_LOGDEBUG
, "ignoring too small audio packet: size: %d",
4536 ret
= RTMP_READ_IGNORE
;
4540 if (r
->m_read
.flags
& RTMP_READ_SEEKING
)
4542 ret
= RTMP_READ_IGNORE
;
4546 RTMP_Log(RTMP_LOGDEBUG
, "type: %02X, size: %d, TS: %d ms, abs TS: %d",
4547 packet
.m_packetType
, nPacketLen
, packet
.m_nTimeStamp
,
4548 packet
.m_hasAbsTimestamp
);
4549 if (packet
.m_packetType
== RTMP_PACKET_TYPE_VIDEO
)
4550 RTMP_Log(RTMP_LOGDEBUG
, "frametype: %02X", (*packetBody
& 0xf0));
4553 if (r
->m_read
.flags
& RTMP_READ_RESUME
)
4555 /* check the header if we get one */
4556 if (packet
.m_nTimeStamp
== 0)
4558 if (r
->m_read
.nMetaHeaderSize
> 0
4559 && packet
.m_packetType
== RTMP_PACKET_TYPE_INFO
)
4563 AMF_Decode(&metaObj
, packetBody
, nPacketLen
, FALSE
);
4567 AMFProp_GetString(AMF_GetProp(&metaObj
, NULL
, 0),
4570 if (AVMATCH(&metastring
, &av_onMetaData
))
4573 if ((r
->m_read
.nMetaHeaderSize
!= nPacketLen
) ||
4575 (r
->m_read
.metaHeader
, packetBody
,
4576 r
->m_read
.nMetaHeaderSize
) != 0))
4578 ret
= RTMP_READ_ERROR
;
4581 AMF_Reset(&metaObj
);
4582 if (ret
== RTMP_READ_ERROR
)
4587 /* check first keyframe to make sure we got the right position
4588 * in the stream! (the first non ignored frame)
4590 if (r
->m_read
.nInitialFrameSize
> 0)
4592 /* video or audio data */
4593 if (packet
.m_packetType
== r
->m_read
.initialFrameType
4594 && r
->m_read
.nInitialFrameSize
== nPacketLen
)
4596 /* we don't compare the sizes since the packet can
4597 * contain several FLV packets, just make sure the
4598 * first frame is our keyframe (which we are going
4602 (r
->m_read
.initialFrame
, packetBody
,
4603 r
->m_read
.nInitialFrameSize
) == 0)
4605 RTMP_Log(RTMP_LOGDEBUG
, "Checked keyframe successfully!");
4606 r
->m_read
.flags
|= RTMP_READ_GOTKF
;
4607 /* ignore it! (what about audio data after it? it is
4608 * handled by ignoring all 0ms frames, see below)
4610 ret
= RTMP_READ_IGNORE
;
4615 /* hande FLV streams, even though the server resends the
4616 * keyframe as an extra video packet it is also included
4617 * in the first FLV stream chunk and we have to compare
4618 * it and filter it out !!
4620 if (packet
.m_packetType
== RTMP_PACKET_TYPE_FLASH_VIDEO
)
4622 /* basically we have to find the keyframe with the
4623 * correct TS being nResumeTS
4625 unsigned int pos
= 0;
4628 while (pos
+ 11 < nPacketLen
)
4630 /* size without header (11) and prevTagSize (4) */
4632 AMF_DecodeInt24(packetBody
+ pos
+ 1);
4633 ts
= AMF_DecodeInt24(packetBody
+ pos
+ 4);
4634 ts
|= (packetBody
[pos
+ 7] << 24);
4637 RTMP_Log(RTMP_LOGDEBUG
,
4638 "keyframe search: FLV Packet: type %02X, dataSize: %d, timeStamp: %d ms",
4639 packetBody
[pos
], dataSize
, ts
);
4641 /* ok, is it a keyframe?:
4642 * well doesn't work for audio!
4644 if (packetBody
[pos
/*6928, test 0 */ ] ==
4645 r
->m_read
.initialFrameType
4646 /* && (packetBody[11]&0xf0) == 0x10 */ )
4648 if (ts
== r
->m_read
.nResumeTS
)
4650 RTMP_Log(RTMP_LOGDEBUG
,
4651 "Found keyframe with resume-keyframe timestamp!");
4652 if (r
->m_read
.nInitialFrameSize
!= dataSize
4653 || memcmp(r
->m_read
.initialFrame
,
4654 packetBody
+ pos
+ 11,
4656 nInitialFrameSize
) != 0)
4658 RTMP_Log(RTMP_LOGERROR
,
4659 "FLV Stream: Keyframe doesn't match!");
4660 ret
= RTMP_READ_ERROR
;
4663 r
->m_read
.flags
|= RTMP_READ_GOTFLVK
;
4665 /* skip this packet?
4666 * check whether skippable:
4668 if (pos
+ 11 + dataSize
+ 4 > nPacketLen
)
4670 RTMP_Log(RTMP_LOGWARNING
,
4671 "Non skipable packet since it doesn't end with chunk, stream corrupt!");
4672 ret
= RTMP_READ_ERROR
;
4675 packetBody
+= (pos
+ 11 + dataSize
+ 4);
4676 nPacketLen
-= (pos
+ 11 + dataSize
+ 4);
4678 goto stopKeyframeSearch
;
4681 else if (r
->m_read
.nResumeTS
< ts
)
4683 /* the timestamp ts will only increase with
4684 * further packets, wait for seek
4686 goto stopKeyframeSearch
;
4689 pos
+= (11 + dataSize
+ 4);
4691 if (ts
< r
->m_read
.nResumeTS
)
4693 RTMP_Log(RTMP_LOGERROR
,
4694 "First packet does not contain keyframe, all "
4695 "timestamps are smaller than the keyframe "
4696 "timestamp; probably the resume seek failed?");
4700 if (!(r
->m_read
.flags
& RTMP_READ_GOTFLVK
))
4702 RTMP_Log(RTMP_LOGERROR
,
4703 "Couldn't find the seeked keyframe in this chunk!");
4704 ret
= RTMP_READ_IGNORE
;
4711 if (packet
.m_nTimeStamp
> 0
4712 && (r
->m_read
.flags
& (RTMP_READ_GOTKF
|RTMP_READ_GOTFLVK
)))
4714 /* another problem is that the server can actually change from
4715 * 09/08 video/audio packets to an FLV stream or vice versa and
4716 * our keyframe check will prevent us from going along with the
4717 * new stream if we resumed.
4719 * in this case set the 'found keyframe' variables to true.
4720 * We assume that if we found one keyframe somewhere and were
4721 * already beyond TS > 0 we have written data to the output
4722 * which means we can accept all forthcoming data including the
4723 * change between 08/09 <-> FLV packets
4725 r
->m_read
.flags
|= (RTMP_READ_GOTKF
|RTMP_READ_GOTFLVK
);
4728 /* skip till we find our keyframe
4729 * (seeking might put us somewhere before it)
4731 if (!(r
->m_read
.flags
& RTMP_READ_GOTKF
) &&
4732 packet
.m_packetType
!= RTMP_PACKET_TYPE_FLASH_VIDEO
)
4734 RTMP_Log(RTMP_LOGWARNING
,
4735 "Stream does not start with requested frame, ignoring data... ");
4736 r
->m_read
.nIgnoredFrameCounter
++;
4737 if (r
->m_read
.nIgnoredFrameCounter
> MAX_IGNORED_FRAMES
)
4738 ret
= RTMP_READ_ERROR
; /* fatal error, couldn't continue stream */
4740 ret
= RTMP_READ_IGNORE
;
4743 /* ok, do the same for FLV streams */
4744 if (!(r
->m_read
.flags
& RTMP_READ_GOTFLVK
) &&
4745 packet
.m_packetType
== RTMP_PACKET_TYPE_FLASH_VIDEO
)
4747 RTMP_Log(RTMP_LOGWARNING
,
4748 "Stream does not start with requested FLV frame, ignoring data... ");
4749 r
->m_read
.nIgnoredFlvFrameCounter
++;
4750 if (r
->m_read
.nIgnoredFlvFrameCounter
> MAX_IGNORED_FRAMES
)
4751 ret
= RTMP_READ_ERROR
;
4753 ret
= RTMP_READ_IGNORE
;
4757 /* we have to ignore the 0ms frames since these are the first
4758 * keyframes; we've got these so don't mess around with multiple
4759 * copies sent by the server to us! (if the keyframe is found at a
4760 * later position there is only one copy and it will be ignored by
4761 * the preceding if clause)
4763 if (!(r
->m_read
.flags
& RTMP_READ_NO_IGNORE
) &&
4764 packet
.m_packetType
!= RTMP_PACKET_TYPE_FLASH_VIDEO
)
4766 /* exclude type RTMP_PACKET_TYPE_FLASH_VIDEO since it can
4767 * contain several FLV packets
4769 if (packet
.m_nTimeStamp
== 0)
4771 ret
= RTMP_READ_IGNORE
;
4776 /* stop ignoring packets */
4777 r
->m_read
.flags
|= RTMP_READ_NO_IGNORE
;
4782 /* calculate packet size and allocate slop buffer if necessary */
4784 ((packet
.m_packetType
== RTMP_PACKET_TYPE_AUDIO
4785 || packet
.m_packetType
== RTMP_PACKET_TYPE_VIDEO
4786 || packet
.m_packetType
== RTMP_PACKET_TYPE_INFO
) ? 11 : 0) +
4787 (packet
.m_packetType
!= RTMP_PACKET_TYPE_FLASH_VIDEO
? 4 : 0);
4789 if (size
+ 4 > buflen
)
4791 /* the extra 4 is for the case of an FLV stream without a last
4792 * prevTagSize (we need extra 4 bytes to append it) */
4793 r
->m_read
.buf
= malloc(size
+ 4);
4794 if (r
->m_read
.buf
== 0)
4796 RTMP_Log(RTMP_LOGERROR
, "Couldn't allocate memory!");
4797 ret
= RTMP_READ_ERROR
; /* fatal error */
4801 ptr
= r
->m_read
.buf
;
4807 pend
= ptr
+ size
+ 4;
4809 /* use to return timestamp of last processed packet */
4811 /* audio (0x08), video (0x09) or metadata (0x12) packets :
4812 * construct 11 byte header then add rtmp packet's data */
4813 if (packet
.m_packetType
== RTMP_PACKET_TYPE_AUDIO
4814 || packet
.m_packetType
== RTMP_PACKET_TYPE_VIDEO
4815 || packet
.m_packetType
== RTMP_PACKET_TYPE_INFO
)
4817 nTimeStamp
= r
->m_read
.nResumeTS
+ packet
.m_nTimeStamp
;
4818 prevTagSize
= 11 + nPacketLen
;
4820 *ptr
= packet
.m_packetType
;
4822 ptr
= AMF_EncodeInt24(ptr
, pend
, nPacketLen
);
4825 if(packet
.m_packetType
== RTMP_PACKET_TYPE_VIDEO
) {
4828 if((packetBody
[0] & 0x0f) == 7) { /* CodecId = H264 */
4829 uint8_t packetType
= *(packetBody
+1);
4831 uint32_t ts
= AMF_DecodeInt24(packetBody
+2); /* composition time */
4832 int32_t cts
= (ts
+0xff800000)^0xff800000;
4833 RTMP_Log(RTMP_LOGDEBUG
, "cts : %d\n", cts
);
4836 /* get rid of the composition time */
4837 CRTMP::EncodeInt24(packetBody
+2, 0);
4839 RTMP_Log(RTMP_LOGDEBUG
, "VIDEO: nTimeStamp: 0x%08X (%d)\n", nTimeStamp
, nTimeStamp
);
4843 ptr
= AMF_EncodeInt24(ptr
, pend
, nTimeStamp
);
4844 *ptr
= (char)((nTimeStamp
& 0xFF000000) >> 24);
4848 ptr
= AMF_EncodeInt24(ptr
, pend
, 0);
4851 memcpy(ptr
, packetBody
, nPacketLen
);
4854 /* correct tagSize and obtain timestamp if we have an FLV stream */
4855 if (packet
.m_packetType
== RTMP_PACKET_TYPE_FLASH_VIDEO
)
4857 unsigned int pos
= 0;
4860 /* grab first timestamp and see if it needs fixing */
4861 nTimeStamp
= AMF_DecodeInt24(packetBody
+ 4);
4862 nTimeStamp
|= (packetBody
[7] << 24);
4863 delta
= packet
.m_nTimeStamp
- nTimeStamp
+ r
->m_read
.nResumeTS
;
4865 while (pos
+ 11 < nPacketLen
)
4867 /* size without header (11) and without prevTagSize (4) */
4868 uint32_t dataSize
= AMF_DecodeInt24(packetBody
+ pos
+ 1);
4869 nTimeStamp
= AMF_DecodeInt24(packetBody
+ pos
+ 4);
4870 nTimeStamp
|= (packetBody
[pos
+ 7] << 24);
4874 nTimeStamp
+= delta
;
4875 AMF_EncodeInt24(ptr
+pos
+4, pend
, nTimeStamp
);
4876 ptr
[pos
+7] = nTimeStamp
>>24;
4880 r
->m_read
.dataType
|= (((*(packetBody
+ pos
) == 0x08) << 2) |
4881 (*(packetBody
+ pos
) == 0x09));
4883 if (pos
+ 11 + dataSize
+ 4 > nPacketLen
)
4885 if (pos
+ 11 + dataSize
> nPacketLen
)
4887 RTMP_Log(RTMP_LOGERROR
,
4888 "Wrong data size (%u), stream corrupted, aborting!",
4890 ret
= RTMP_READ_ERROR
;
4893 RTMP_Log(RTMP_LOGWARNING
, "No tagSize found, appending!");
4895 /* we have to append a last tagSize! */
4896 prevTagSize
= dataSize
+ 11;
4897 AMF_EncodeInt32(ptr
+ pos
+ 11 + dataSize
, pend
,
4905 AMF_DecodeInt32(packetBody
+ pos
+ 11 + dataSize
);
4908 RTMP_Log(RTMP_LOGDEBUG
,
4909 "FLV Packet: type %02X, dataSize: %lu, tagSize: %lu, timeStamp: %lu ms",
4910 (unsigned char)packetBody
[pos
], dataSize
, prevTagSize
,
4914 if (prevTagSize
!= (dataSize
+ 11))
4917 RTMP_Log(RTMP_LOGWARNING
,
4918 "Tag and data size are not consitent, writing tag size according to dataSize+11: %d",
4922 prevTagSize
= dataSize
+ 11;
4923 AMF_EncodeInt32(ptr
+ pos
+ 11 + dataSize
, pend
,
4928 pos
+= prevTagSize
+ 4; /*(11+dataSize+4); */
4933 if (packet
.m_packetType
!= RTMP_PACKET_TYPE_FLASH_VIDEO
)
4935 /* FLV tag packets contain their own prevTagSize */
4936 AMF_EncodeInt32(ptr
, pend
, prevTagSize
);
4939 /* In non-live this nTimeStamp can contain an absolute TS.
4940 * Update ext timestamp with this absolute offset in non-live mode
4941 * otherwise report the relative one
4943 /* 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); */
4944 r
->m_read
.timestamp
= (r
->Link
.lFlags
& RTMP_LF_LIVE
) ? packet
.m_nTimeStamp
: nTimeStamp
;
4950 if (rtnGetNextMediaPacket
)
4951 RTMPPacket_Free(&packet
);
4955 len
= ret
> buflen
? buflen
: ret
;
4956 memcpy(buf
, r
->m_read
.buf
, len
);
4957 r
->m_read
.bufpos
= r
->m_read
.buf
+ len
;
4958 r
->m_read
.buflen
= ret
- len
;
4963 static const char flvHeader
[] = { 'F', 'L', 'V', 0x01,
4964 0x00, /* 0x04 == audio, 0x01 == video */
4965 0x00, 0x00, 0x00, 0x09,
4966 0x00, 0x00, 0x00, 0x00
4969 #define HEADERBUF (128*1024)
4971 RTMP_Read(RTMP
*r
, char *buf
, int size
)
4973 int nRead
= 0, total
= 0;
4975 /* can't continue */
4977 switch (r
->m_read
.status
) {
4979 case RTMP_READ_COMPLETE
:
4981 case RTMP_READ_ERROR
: /* corrupted stream, resume failed */
4982 SetSockError(EINVAL
);
4988 /* first time thru */
4989 if (!(r
->m_read
.flags
& RTMP_READ_HEADER
))
4991 if (!(r
->m_read
.flags
& RTMP_READ_RESUME
))
4993 char *mybuf
= malloc(HEADERBUF
), *end
= mybuf
+ HEADERBUF
;
4995 r
->m_read
.buf
= mybuf
;
4996 r
->m_read
.buflen
= HEADERBUF
;
4998 memcpy(mybuf
, flvHeader
, sizeof(flvHeader
));
4999 r
->m_read
.buf
+= sizeof(flvHeader
);
5000 r
->m_read
.buflen
-= sizeof(flvHeader
);
5002 while (r
->m_read
.timestamp
== 0)
5004 nRead
= Read_1_Packet(r
, r
->m_read
.buf
, r
->m_read
.buflen
);
5008 r
->m_read
.buf
= NULL
;
5009 r
->m_read
.buflen
= 0;
5010 r
->m_read
.status
= nRead
;
5013 /* buffer overflow, fix buffer and give up */
5014 if (r
->m_read
.buf
< mybuf
|| r
->m_read
.buf
> end
) {
5015 mybuf
= realloc(mybuf
, cnt
+ nRead
);
5016 memcpy(mybuf
+cnt
, r
->m_read
.buf
, nRead
);
5017 r
->m_read
.buf
= mybuf
+cnt
+nRead
;
5021 r
->m_read
.buf
+= nRead
;
5022 r
->m_read
.buflen
-= nRead
;
5023 if (r
->m_read
.dataType
== 5)
5026 mybuf
[4] = r
->m_read
.dataType
;
5027 r
->m_read
.buflen
= r
->m_read
.buf
- mybuf
;
5028 r
->m_read
.buf
= mybuf
;
5029 r
->m_read
.bufpos
= mybuf
;
5031 r
->m_read
.flags
|= RTMP_READ_HEADER
;
5034 if ((r
->m_read
.flags
& RTMP_READ_SEEKING
) && r
->m_read
.buf
)
5036 /* drop whatever's here */
5037 free(r
->m_read
.buf
);
5038 r
->m_read
.buf
= NULL
;
5039 r
->m_read
.bufpos
= NULL
;
5040 r
->m_read
.buflen
= 0;
5043 /* If there's leftover data buffered, use it up */
5046 nRead
= r
->m_read
.buflen
;
5049 memcpy(buf
, r
->m_read
.bufpos
, nRead
);
5050 r
->m_read
.buflen
-= nRead
;
5051 if (!r
->m_read
.buflen
)
5053 free(r
->m_read
.buf
);
5054 r
->m_read
.buf
= NULL
;
5055 r
->m_read
.bufpos
= NULL
;
5059 r
->m_read
.bufpos
+= nRead
;
5066 while (size
> 0 && (nRead
= Read_1_Packet(r
, buf
, size
)) >= 0)
5068 if (!nRead
) continue;
5075 r
->m_read
.status
= nRead
;
5082 static const AVal av_setDataFrame
= AVC("@setDataFrame");
5085 RTMP_Write(RTMP
*r
, const char *buf
, int size
)
5087 RTMPPacket
*pkt
= &r
->m_write
;
5089 int s2
= size
, ret
, num
;
5091 pkt
->m_nChannel
= 0x04; /* source channel */
5092 pkt
->m_nInfoField2
= r
->m_stream_id
;
5096 if (!pkt
->m_nBytesRead
)
5099 /* FLV pkt too small */
5103 if (buf
[0] == 'F' && buf
[1] == 'L' && buf
[2] == 'V')
5109 pkt
->m_packetType
= *buf
++;
5110 pkt
->m_nBodySize
= AMF_DecodeInt24(buf
);
5112 pkt
->m_nTimeStamp
= AMF_DecodeInt24(buf
);
5114 pkt
->m_nTimeStamp
|= *buf
++ << 24;
5118 if (((pkt
->m_packetType
== RTMP_PACKET_TYPE_AUDIO
5119 || pkt
->m_packetType
== RTMP_PACKET_TYPE_VIDEO
) &&
5120 !pkt
->m_nTimeStamp
) || pkt
->m_packetType
== RTMP_PACKET_TYPE_INFO
)
5122 pkt
->m_headerType
= RTMP_PACKET_SIZE_LARGE
;
5123 if (pkt
->m_packetType
== RTMP_PACKET_TYPE_INFO
)
5124 pkt
->m_nBodySize
+= 16;
5128 pkt
->m_headerType
= RTMP_PACKET_SIZE_MEDIUM
;
5131 if (!RTMPPacket_Alloc(pkt
, pkt
->m_nBodySize
))
5133 RTMP_Log(RTMP_LOGDEBUG
, "%s, failed to allocate packet", __FUNCTION__
);
5137 pend
= enc
+ pkt
->m_nBodySize
;
5138 if (pkt
->m_packetType
== RTMP_PACKET_TYPE_INFO
)
5140 enc
= AMF_EncodeString(enc
, pend
, &av_setDataFrame
);
5141 pkt
->m_nBytesRead
= enc
- pkt
->m_body
;
5146 enc
= pkt
->m_body
+ pkt
->m_nBytesRead
;
5148 num
= pkt
->m_nBodySize
- pkt
->m_nBytesRead
;
5151 memcpy(enc
, buf
, num
);
5152 pkt
->m_nBytesRead
+= num
;
5155 if (pkt
->m_nBytesRead
== pkt
->m_nBodySize
)
5157 ret
= RTMP_SendPacket(r
, pkt
, FALSE
);
5158 RTMPPacket_Free(pkt
);
5159 pkt
->m_nBytesRead
= 0;