diff --git a/FrameWork/LFLiveKit.xcodeproj/project.pbxproj b/FrameWork/LFLiveKit.xcodeproj/project.pbxproj index 74b3450d..838223ba 100644 --- a/FrameWork/LFLiveKit.xcodeproj/project.pbxproj +++ b/FrameWork/LFLiveKit.xcodeproj/project.pbxproj @@ -1998,6 +1998,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, ); mainGroup = 84D8B3861D7574D600752B56; @@ -2643,6 +2644,8 @@ HEADER_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/../samples/LFLiveKitDemo/Pods\"/**", + "${PODS_ROOT}/Headers/Private/OpenSSL_Universal/**", + "${PODS_ROOT}/Headers/Public/OpenSSL_Universal/**", ); INFOPLIST_FILE = Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; @@ -2667,6 +2670,8 @@ HEADER_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/../samples/LFLiveKitDemo/Pods\"/**", + "${PODS_ROOT}/Headers/Private/OpenSSL_Universal/**", + "${PODS_ROOT}/Headers/Public/OpenSSL_Universal/**", ); INFOPLIST_FILE = Info.plist; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; diff --git a/LFLiveKit.podspec b/LFLiveKit.podspec index 4153bc8c..d85696c1 100644 --- a/LFLiveKit.podspec +++ b/LFLiveKit.podspec @@ -16,5 +16,9 @@ Pod::Spec.new do |s| s.frameworks = "VideoToolbox", "AudioToolbox","AVFoundation","Foundation","UIKit" s.libraries = "c++", "z" + s.pod_target_xcconfig = { 'HEADER_SEARCH_PATHS' => '${PODS_ROOT}/Headers/Private/OpenSSL_Universal/** ${PODS_ROOT}/Headers/Public/OpenSSL_Universal/**' } + + s.dependency 'OpenSSL-Universal', '1.0.2.17' + s.static_framework = true s.requires_arc = true end diff --git a/LFLiveKit/Vendor/GPUImage/iOS/GPUImageView.m b/LFLiveKit/Vendor/GPUImage/iOS/GPUImageView.m index e092b80e..ace3441f 100755 --- a/LFLiveKit/Vendor/GPUImage/iOS/GPUImageView.m +++ b/LFLiveKit/Vendor/GPUImage/iOS/GPUImageView.m @@ -230,54 +230,50 @@ - (void)presentFramebuffer; #pragma mark - #pragma mark Handling fill mode -- (void)recalculateViewGeometry; -{ - runSynchronouslyOnVideoProcessingQueue(^{ - CGFloat heightScaling, widthScaling; - - CGSize currentViewSize = self.bounds.size; - - // CGFloat imageAspectRatio = inputImageSize.width / inputImageSize.height; - // CGFloat viewAspectRatio = currentViewSize.width / currentViewSize.height; - - CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(inputImageSize, self.bounds); - - switch(_fillMode) - { - case kGPUImageFillModeStretch: - { - widthScaling = 1.0; - heightScaling = 1.0; - }; break; - case kGPUImageFillModePreserveAspectRatio: - { - widthScaling = insetRect.size.width / currentViewSize.width; - heightScaling = insetRect.size.height / currentViewSize.height; - }; break; - case kGPUImageFillModePreserveAspectRatioAndFill: +- (void)recalculateViewGeometry{ + __block CGRect currentBounds; + + dispatch_async(dispatch_get_main_queue(), ^(void){ + //Run UI Updates + currentBounds = self.bounds; + runSynchronouslyOnVideoProcessingQueue(^{ + CGFloat heightScaling, widthScaling; + + // CGFloat imageAspectRatio = inputImageSize.width / inputImageSize.height; + // CGFloat viewAspectRatio = currentViewSize.width / currentViewSize.height; + + CGRect insetRect = AVMakeRectWithAspectRatioInsideRect(inputImageSize, currentBounds); + + switch(_fillMode) { - // CGFloat widthHolder = insetRect.size.width / currentViewSize.width; - widthScaling = currentViewSize.height / insetRect.size.height; - heightScaling = currentViewSize.width / insetRect.size.width; - }; break; - } - - imageVertices[0] = -widthScaling; - imageVertices[1] = -heightScaling; - imageVertices[2] = widthScaling; - imageVertices[3] = -heightScaling; - imageVertices[4] = -widthScaling; - imageVertices[5] = heightScaling; - imageVertices[6] = widthScaling; - imageVertices[7] = heightScaling; + case kGPUImageFillModeStretch: + { + widthScaling = 1.0; + heightScaling = 1.0; + }; break; + case kGPUImageFillModePreserveAspectRatio: + { + widthScaling = insetRect.size.width / currentBounds.size.width; + heightScaling = insetRect.size.height / currentBounds.size.height; + }; break; + case kGPUImageFillModePreserveAspectRatioAndFill: + { + // CGFloat widthHolder = insetRect.size.width / currentBounds.size.width; + widthScaling = currentBounds.size.height / insetRect.size.height; + heightScaling = currentBounds.size.width / insetRect.size.width; + }; break; + } + + imageVertices[0] = -widthScaling; + imageVertices[1] = -heightScaling; + imageVertices[2] = widthScaling; + imageVertices[3] = -heightScaling; + imageVertices[4] = -widthScaling; + imageVertices[5] = heightScaling; + imageVertices[6] = widthScaling; + imageVertices[7] = heightScaling; + }); }); - -// static const GLfloat imageVertices[] = { -// -1.0f, -1.0f, -// 1.0f, -1.0f, -// -1.0f, 1.0f, -// 1.0f, 1.0f, -// }; } - (void)setBackgroundColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent alpha:(GLfloat)alphaComponent; diff --git a/LFLiveKit/Vendor/pili-librtmp/handshake.h b/LFLiveKit/Vendor/pili-librtmp/handshake.h index f791cf74..6a72a152 100644 --- a/LFLiveKit/Vendor/pili-librtmp/handshake.h +++ b/LFLiveKit/Vendor/pili-librtmp/handshake.h @@ -33,19 +33,19 @@ #endif #define HMAC_CTX sha2_context #define HMAC_setup(ctx, key, len) \ - sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0) +sha2_hmac_starts(&ctx, (unsigned char *)key, len, 0) #define HMAC_crunch(ctx, buf, len) sha2_hmac_update(&ctx, buf, len) #define HMAC_finish(ctx, dig, dlen) \ - dlen = SHA256_DIGEST_LENGTH; \ - sha2_hmac_finish(&ctx, dig) +dlen = SHA256_DIGEST_LENGTH; \ +sha2_hmac_finish(&ctx, dig) typedef arc4_context *RC4_handle; #define RC4_alloc(h) *h = malloc(sizeof(arc4_context)) #define RC4_setkey(h, l, k) arc4_setup(h, k, l) #define RC4_encrypt(h, l, d) \ - arc4_crypt(h, l, (unsigned char *)d, (unsigned char *)d) +arc4_crypt(h, l, (unsigned char *)d, (unsigned char *)d) #define RC4_encrypt2(h, l, s, d) \ - arc4_crypt(h, l, (unsigned char *)s, (unsigned char *)d) +arc4_crypt(h, l, (unsigned char *)s, (unsigned char *)d) #define RC4_free(h) free(h) #elif defined(USE_GNUTLS) @@ -55,21 +55,21 @@ typedef arc4_context *RC4_handle; #endif #define HMAC_CTX gcry_md_hd_t #define HMAC_setup(ctx, key, len) \ - gcry_md_open(&ctx, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); \ - gcry_md_setkey(ctx, key, len) +gcry_md_open(&ctx, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); \ +gcry_md_setkey(ctx, key, len) #define HMAC_crunch(ctx, buf, len) gcry_md_write(ctx, buf, len) #define HMAC_finish(ctx, dig, dlen) \ - dlen = SHA256_DIGEST_LENGTH; \ - memcpy(dig, gcry_md_read(ctx, 0), dlen); \ - gcry_md_close(ctx) +dlen = SHA256_DIGEST_LENGTH; \ +memcpy(dig, gcry_md_read(ctx, 0), dlen); \ +gcry_md_close(ctx) typedef gcry_cipher_hd_t RC4_handle; #define RC4_alloc(h) \ - gcry_cipher_open(h, GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM, 0) +gcry_cipher_open(h, GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM, 0) #define RC4_setkey(h, l, k) gcry_cipher_setkey(h, k, l) #define RC4_encrypt(h, l, d) gcry_cipher_encrypt(h, (void *)d, l, NULL, 0) #define RC4_encrypt2(h, l, s, d) \ - gcry_cipher_encrypt(h, (void *)d, l, (void *)s, l) +gcry_cipher_encrypt(h, (void *)d, l, (void *)s, l) #define RC4_free(h) gcry_cipher_close(h) #else /* USE_OPENSSL */ @@ -80,12 +80,12 @@ typedef gcry_cipher_hd_t RC4_handle; #error Your OpenSSL is too old, need 0.9.8 or newer with SHA256 #endif #define HMAC_setup(ctx, key, len) \ - HMAC_CTX_init(&ctx); \ - HMAC_Init_ex(&ctx, key, len, EVP_sha256(), 0) +HMAC_CTX_init(&ctx); \ +HMAC_Init_ex(&ctx, key, len, EVP_sha256(), 0) #define HMAC_crunch(ctx, buf, len) HMAC_Update(&ctx, buf, len) #define HMAC_finish(ctx, dig, dlen) \ - HMAC_Final(&ctx, dig, &dlen); \ - HMAC_CTX_cleanup(&ctx) +HMAC_Final(&ctx, dig, &dlen); \ +HMAC_CTX_cleanup(&ctx) typedef RC4_KEY *RC4_handle; #define RC4_alloc(h) *h = malloc(sizeof(RC4_KEY)) @@ -104,7 +104,7 @@ static const uint8_t GenuineFMSKey[] = { 0x62, 0x65, 0x20, 0x46, 0x6c, 0x61, 0x73, 0x68, 0x20, 0x4d, 0x65, 0x64, 0x69, 0x61, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x30, 0x30, 0x31, /* Genuine Adobe Flash Media Server 001 */ - + 0xf0, 0xee, 0xc2, 0x4a, 0x80, 0x68, 0xbe, 0xe8, 0x2e, 0x00, 0xd0, 0xd1, 0x02, 0x9e, 0x7e, 0x57, 0x6e, 0xec, 0x5d, 0x2d, 0x29, 0x80, 0x6f, 0xab, 0x93, 0xb8, 0xe6, 0x36, 0xcf, 0xeb, 0x31, 0xae}; /* 68 */ @@ -113,7 +113,7 @@ static const uint8_t GenuineFPKey[] = { 0x47, 0x65, 0x6E, 0x75, 0x69, 0x6E, 0x65, 0x20, 0x41, 0x64, 0x6F, 0x62, 0x65, 0x20, 0x46, 0x6C, 0x61, 0x73, 0x68, 0x20, 0x50, 0x6C, 0x61, 0x79, 0x65, 0x72, 0x20, 0x30, 0x30, 0x31, /* Genuine Adobe Flash - Player 001 */ + Player 001 */ 0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 0x02, 0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE}; /* 62 */ @@ -124,26 +124,26 @@ static void InitRC4Encryption(uint8_t *secretKey, uint8_t *pubKeyIn, uint8_t digest[SHA256_DIGEST_LENGTH]; unsigned int digestLen = 0; HMAC_CTX ctx; - + RC4_alloc(rc4keyIn); RC4_alloc(rc4keyOut); - + HMAC_setup(ctx, secretKey, 128); HMAC_crunch(ctx, pubKeyIn, 128); HMAC_finish(ctx, digest, digestLen); - + RTMP_Log(RTMP_LOGDEBUG, "RC4 Out Key: "); RTMP_LogHex(RTMP_LOGDEBUG, digest, 16); - + RC4_setkey(*rc4keyOut, 16, digest); - + HMAC_setup(ctx, secretKey, 128); HMAC_crunch(ctx, pubKeyOut, 128); HMAC_finish(ctx, digest, digestLen); - + RTMP_Log(RTMP_LOGDEBUG, "RC4 In Key: "); RTMP_LogHex(RTMP_LOGDEBUG, digest, 16); - + RC4_setkey(*rc4keyIn, 16, digest); } @@ -153,9 +153,9 @@ static unsigned int GetDHOffset2(uint8_t *handshake, unsigned int len) { unsigned int offset = 0; uint8_t *ptr = handshake + 768; unsigned int res; - + assert(RTMP_SIG_SIZE <= len); - + offset += (*ptr); ptr++; offset += (*ptr); @@ -163,9 +163,9 @@ static unsigned int GetDHOffset2(uint8_t *handshake, unsigned int len) { offset += (*ptr); ptr++; offset += (*ptr); - + res = (offset % 632) + 8; - + if (res + 128 > 767) { RTMP_Log(RTMP_LOGERROR, "%s: Couldn't calculate correct DH offset (got %d), exiting!", @@ -179,7 +179,7 @@ static unsigned int GetDigestOffset2(uint8_t *handshake, unsigned int len) { unsigned int offset = 0; uint8_t *ptr = handshake + 772; unsigned int res; - + offset += (*ptr); ptr++; offset += (*ptr); @@ -187,9 +187,9 @@ static unsigned int GetDigestOffset2(uint8_t *handshake, unsigned int len) { offset += (*ptr); ptr++; offset += (*ptr); - + res = (offset % 728) + 776; - + if (res + 32 > 1535) { RTMP_Log(RTMP_LOGERROR, "%s: Couldn't calculate correct digest offset (got %d), exiting", @@ -203,9 +203,9 @@ static unsigned int GetDHOffset1(uint8_t *handshake, unsigned int len) { unsigned int offset = 0; uint8_t *ptr = handshake + 1532; unsigned int res; - + assert(RTMP_SIG_SIZE <= len); - + offset += (*ptr); ptr++; offset += (*ptr); @@ -213,16 +213,16 @@ static unsigned int GetDHOffset1(uint8_t *handshake, unsigned int len) { offset += (*ptr); ptr++; offset += (*ptr); - + res = (offset % 632) + 772; - + if (res + 128 > 1531) { RTMP_Log(RTMP_LOGERROR, "%s: Couldn't calculate DH offset (got %d), exiting!", __FUNCTION__, res); exit(1); } - + return res; } @@ -230,9 +230,9 @@ static unsigned int GetDigestOffset1(uint8_t *handshake, unsigned int len) { unsigned int offset = 0; uint8_t *ptr = handshake + 8; unsigned int res; - + assert(12 <= len); - + offset += (*ptr); ptr++; offset += (*ptr); @@ -240,16 +240,16 @@ static unsigned int GetDigestOffset1(uint8_t *handshake, unsigned int len) { offset += (*ptr); ptr++; offset += (*ptr); - + res = (offset % 728) + 12; - + if (res + 32 > 771) { RTMP_Log(RTMP_LOGERROR, "%s: Couldn't calculate digest offset (got %d), exiting!", __FUNCTION__, res); exit(1); } - + return res; } @@ -260,11 +260,11 @@ static void HMACsha256(const uint8_t *message, size_t messageLen, const uint8_t *key, size_t keylen, uint8_t *digest) { unsigned int digestLen; HMAC_CTX ctx; - + HMAC_setup(ctx, key, keylen); HMAC_crunch(ctx, message, messageLen); HMAC_finish(ctx, digest, digestLen); - + assert(digestLen == 32); } @@ -273,32 +273,32 @@ static void CalculateDigest(unsigned int digestPos, uint8_t *handshakeMessage, uint8_t *digest) { const int messageLen = RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH; uint8_t message[RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH]; - + memcpy(message, handshakeMessage, digestPos); memcpy(message + digestPos, &handshakeMessage[digestPos + SHA256_DIGEST_LENGTH], messageLen - digestPos); - + HMACsha256(message, messageLen, key, keyLen, digest); } static int VerifyDigest(unsigned int digestPos, uint8_t *handshakeMessage, const uint8_t *key, size_t keyLen) { uint8_t calcDigest[SHA256_DIGEST_LENGTH]; - + CalculateDigest(digestPos, handshakeMessage, key, keyLen, calcDigest); - + return memcmp(&handshakeMessage[digestPos], calcDigest, SHA256_DIGEST_LENGTH) == 0; } /* handshake * - * Type = [1 bytes] plain: 0x03, encrypted: 0x06, 0x08, 0x09 + * Type = [1 bytes] plain: 0x03, encrypted: 0x06, 0x08, 0x09 * -------------------------------------------------------------------- [1536 * bytes] - * Uptime = [4 bytes] big endian unsigned number, uptime - * Version = [4 bytes] each byte represents a version number, e.g. + * Uptime = [4 bytes] big endian unsigned number, uptime + * Version = [4 bytes] each byte represents a version number, e.g. * 9.0.124.0 * ... * @@ -329,17 +329,17 @@ static void rtmpe8_sig(uint8_t *in, uint8_t *out, int keyid) { unsigned int i, num_rounds = 32; uint32_t v0, v1, sum = 0, delta = 0x9E3779B9; uint32_t const *k; - + v0 = in[0] | (in[1] << 8) | (in[2] << 16) | (in[3] << 24); v1 = in[4] | (in[5] << 8) | (in[6] << 16) | (in[7] << 24); k = rtmpe8_keys[keyid]; - + for (i = 0; i < num_rounds; i++) { v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); sum += delta; v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]); } - + out[0] = v0; v0 >>= 8; out[1] = v0; @@ -347,7 +347,7 @@ static void rtmpe8_sig(uint8_t *in, uint8_t *out, int keyid) { out[2] = v0; v0 >>= 8; out[3] = v0; - + out[4] = v1; v1 >>= 8; out[5] = v1; @@ -357,39 +357,39 @@ static void rtmpe8_sig(uint8_t *in, uint8_t *out, int keyid) { out[7] = v1; } -static int HandShake(RTMP *r, int FP9HandShake) { +static int HandShake(PILI_RTMP *r, int FP9HandShake, RTMPError* error) { int i, offalg = 0; int dhposClient = 0; int digestPosClient = 0; int encrypted = r->Link.protocol & RTMP_FEATURE_ENC; - + RC4_handle keyIn = 0; RC4_handle keyOut = 0; - + int32_t *ip; uint32_t uptime; - + uint8_t clientbuf[RTMP_SIG_SIZE + 4], *clientsig = clientbuf + 4; uint8_t serversig[RTMP_SIG_SIZE], client2[RTMP_SIG_SIZE], *reply; uint8_t type; getoff *getdh = NULL, *getdig = NULL; - + if (encrypted || r->Link.SWFSize) FP9HandShake = TRUE; else FP9HandShake = FALSE; - + r->Link.rc4keyIn = r->Link.rc4keyOut = 0; - + if (encrypted) { clientsig[-1] = 0x06; /* 0x08 is RTMPE as well */ offalg = 1; } else clientsig[-1] = 0x03; - - uptime = htonl(RTMP_GetTime()); + + uptime = htonl(PILI_RTMP_GetTime()); memcpy(clientsig, &uptime, 4); - + if (FP9HandShake) { /* set version to at least 9.0.115.0 */ if (encrypted) { @@ -401,7 +401,7 @@ static int HandShake(RTMP *r, int FP9HandShake) { } clientsig[5] = 0; clientsig[7] = 2; - + RTMP_Log(RTMP_LOGDEBUG, "%s: Client type: %02X", __FUNCTION__, clientsig[-1]); getdig = digoff[offalg]; @@ -409,8 +409,8 @@ static int HandShake(RTMP *r, int FP9HandShake) { } else { memset(&clientsig[4], 0, 4); } - -/* generate random data */ + + /* generate random data */ #ifdef _DEBUG memset(clientsig + 8, 0, RTMP_SIG_SIZE - 8); #else @@ -418,7 +418,7 @@ static int HandShake(RTMP *r, int FP9HandShake) { for (i = 2; i < RTMP_SIG_SIZE / 4; i++) *ip++ = rand(); #endif - + /* set handshake digest */ if (FP9HandShake) { if (encrypted) { @@ -429,105 +429,107 @@ static int HandShake(RTMP *r, int FP9HandShake) { __FUNCTION__); return FALSE; } - + dhposClient = getdh(clientsig, RTMP_SIG_SIZE); RTMP_Log(RTMP_LOGDEBUG, "%s: DH pubkey position: %d", __FUNCTION__, dhposClient); - + if (!DHGenerateKey(r->Link.dh)) { RTMP_Log(RTMP_LOGERROR, "%s: Couldn't generate Diffie-Hellmann public key!", __FUNCTION__); return FALSE; } - + if (!DHGetPublicKey(r->Link.dh, &clientsig[dhposClient], 128)) { RTMP_Log(RTMP_LOGERROR, "%s: Couldn't write public key!", __FUNCTION__); return FALSE; } } - + digestPosClient = - getdig(clientsig, RTMP_SIG_SIZE); /* reuse this value in verification */ + getdig(clientsig, RTMP_SIG_SIZE); /* reuse this value in verification */ RTMP_Log(RTMP_LOGDEBUG, "%s: Client digest offset: %d", __FUNCTION__, digestPosClient); - + CalculateDigest(digestPosClient, clientsig, GenuineFPKey, 30, &clientsig[digestPosClient]); - + RTMP_Log(RTMP_LOGDEBUG, "%s: Initial client digest: ", __FUNCTION__); RTMP_LogHex(RTMP_LOGDEBUG, clientsig + digestPosClient, SHA256_DIGEST_LENGTH); } - + #ifdef _DEBUG RTMP_Log(RTMP_LOGDEBUG, "Clientsig: "); RTMP_LogHex(RTMP_LOGDEBUG, clientsig, RTMP_SIG_SIZE); #endif - - if (!WriteN(r, (char *)clientsig - 1, RTMP_SIG_SIZE + 1)) + + + + if (!WriteN(r, (char *)clientsig - 1, RTMP_SIG_SIZE + 1, error)) return FALSE; - + if (ReadN(r, (char *)&type, 1) != 1) /* 0x03 or 0x06 */ return FALSE; - + RTMP_Log(RTMP_LOGDEBUG, "%s: Type Answer : %02X", __FUNCTION__, type); - + if (type != clientsig[-1]) RTMP_Log(RTMP_LOGWARNING, "%s: Type mismatch: client sent %d, server answered %d", __FUNCTION__, clientsig[-1], type); - + if (ReadN(r, (char *)serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) return FALSE; - + /* decode server response */ memcpy(&uptime, serversig, 4); uptime = ntohl(uptime); - + RTMP_Log(RTMP_LOGDEBUG, "%s: Server Uptime : %d", __FUNCTION__, uptime); RTMP_Log(RTMP_LOGDEBUG, "%s: FMS Version : %d.%d.%d.%d", __FUNCTION__, serversig[4], serversig[5], serversig[6], serversig[7]); - + if (FP9HandShake && type == 3 && !serversig[4]) FP9HandShake = FALSE; - + #ifdef _DEBUG RTMP_Log(RTMP_LOGDEBUG, "Server signature:"); RTMP_LogHex(RTMP_LOGDEBUG, serversig, RTMP_SIG_SIZE); #endif - + if (FP9HandShake) { uint8_t digestResp[SHA256_DIGEST_LENGTH]; uint8_t *signatureResp = NULL; - + /* we have to use this signature now to find the correct algorithms for - * getting the digest and DH positions */ + * getting the digest and DH positions */ int digestPosServer = getdig(serversig, RTMP_SIG_SIZE); - + if (!VerifyDigest(digestPosServer, serversig, GenuineFMSKey, 36)) { RTMP_Log(RTMP_LOGWARNING, "Trying different position for server digest!"); offalg ^= 1; getdig = digoff[offalg]; getdh = dhoff[offalg]; digestPosServer = getdig(serversig, RTMP_SIG_SIZE); - + if (!VerifyDigest(digestPosServer, serversig, GenuineFMSKey, 36)) { RTMP_Log( - RTMP_LOGERROR, - "Couldn't verify the server digest"); /* continuing anyway will - probably fail */ + RTMP_LOGERROR, + "Couldn't verify the server digest"); /* continuing anyway will + probably fail */ return FALSE; } } - + /* generate SWFVerification token (SHA256 HMAC hash of decompressed SWF, key - * are the last 32 bytes of the server handshake) */ + * are the last 32 bytes of the server handshake) */ if (r->Link.SWFSize) { const char swfVerify[] = {0x01, 0x01}; char *vend = r->Link.SWFVerificationResponse + - sizeof(r->Link.SWFVerificationResponse); - + sizeof(r->Link.SWFVerificationResponse); + memcpy(r->Link.SWFVerificationResponse, swfVerify, 2); AMF_EncodeInt32(&r->Link.SWFVerificationResponse[2], vend, r->Link.SWFSize); @@ -538,13 +540,13 @@ static int HandShake(RTMP *r, int FP9HandShake) { SHA256_DIGEST_LENGTH, (uint8_t *)&r->Link.SWFVerificationResponse[10]); } - + /* do Diffie-Hellmann Key exchange for encrypted RTMP */ if (encrypted) { /* compute secret key */ uint8_t secretKey[128] = {0}; int len, dhposServer; - + dhposServer = getdh(serversig, RTMP_SIG_SIZE); RTMP_Log(RTMP_LOGDEBUG, "%s: Server DH public key offset: %d", __FUNCTION__, dhposServer); @@ -554,14 +556,14 @@ static int HandShake(RTMP *r, int FP9HandShake) { RTMP_Log(RTMP_LOGDEBUG, "%s: Wrong secret key position!", __FUNCTION__); return FALSE; } - + RTMP_Log(RTMP_LOGDEBUG, "%s: Secret key: ", __FUNCTION__); RTMP_LogHex(RTMP_LOGDEBUG, secretKey, 128); - + InitRC4Encryption(secretKey, (uint8_t *)&serversig[dhposServer], (uint8_t *)&clientsig[dhposClient], &keyIn, &keyOut); } - + reply = client2; #ifdef _DEBUG memset(reply, 0xff, RTMP_SIG_SIZE); @@ -572,18 +574,18 @@ static int HandShake(RTMP *r, int FP9HandShake) { #endif /* calculate response now */ signatureResp = reply + RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH; - + HMACsha256(&serversig[digestPosServer], SHA256_DIGEST_LENGTH, GenuineFPKey, sizeof(GenuineFPKey), digestResp); HMACsha256(reply, RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH, digestResp, SHA256_DIGEST_LENGTH, signatureResp); - + /* some info output */ RTMP_Log(RTMP_LOGDEBUG, "%s: Calculated digest key from secure key and server digest: ", __FUNCTION__); RTMP_LogHex(RTMP_LOGDEBUG, digestResp, SHA256_DIGEST_LENGTH); - + #ifdef FP10 if (type == 8) { uint8_t *dptr = digestResp; @@ -593,13 +595,13 @@ static int HandShake(RTMP *r, int FP9HandShake) { rtmpe8_sig(sig + i, sig + i, dptr[i] % 15); } #if 0 - else if (type == 9)) + else if (type == 9)) { - uint8_t *dptr = digestResp; - uint8_t *sig = signatureResp; - /* encrypt signatureResp */ - for (i=0; iLink.rc4keyIn = keyIn; r->Link.rc4keyOut = keyOut; - + /* update the keystreams */ if (r->Link.rc4keyIn) { RC4_encrypt(r->Link.rc4keyIn, RTMP_SIG_SIZE, (uint8_t *)buff); } - + if (r->Link.rc4keyOut) { RC4_encrypt(r->Link.rc4keyOut, RTMP_SIG_SIZE, (uint8_t *)buff); } @@ -704,12 +706,12 @@ static int HandShake(RTMP *r, int FP9HandShake) { __FUNCTION__); } } - + RTMP_Log(RTMP_LOGDEBUG, "%s: Handshaking finished....", __FUNCTION__); return TRUE; } -static int SHandShake(RTMP *r) { +static int SHandShake(PILI_RTMP *r, RTMPError* error) { int i, offalg = 0; int dhposServer = 0; int digestPosServer = 0; @@ -718,22 +720,22 @@ static int SHandShake(RTMP *r) { int FP9HandShake = FALSE; int encrypted; int32_t *ip; - + uint8_t clientsig[RTMP_SIG_SIZE]; uint8_t serverbuf[RTMP_SIG_SIZE + 4], *serversig = serverbuf + 4; uint8_t type; uint32_t uptime; getoff *getdh = NULL, *getdig = NULL; - + if (ReadN(r, (char *)&type, 1) != 1) /* 0x03 or 0x06 */ return FALSE; - + if (ReadN(r, (char *)clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) return FALSE; - + RTMP_Log(RTMP_LOGDEBUG, "%s: Type Requested : %02X", __FUNCTION__, type); RTMP_LogHex(RTMP_LOGDEBUG2, clientsig, RTMP_SIG_SIZE); - + if (type == 3) { encrypted = FALSE; } else if (type == 6 || type == 8) { @@ -748,31 +750,31 @@ static int SHandShake(RTMP *r) { RTMP_Log(RTMP_LOGERROR, "%s: Unknown version %02x", __FUNCTION__, type); return FALSE; } - + if (!FP9HandShake && clientsig[4]) FP9HandShake = TRUE; - + serversig[-1] = type; - + r->Link.rc4keyIn = r->Link.rc4keyOut = 0; - - uptime = htonl(RTMP_GetTime()); + + uptime = htonl(PILI_RTMP_GetTime()); memcpy(serversig, &uptime, 4); - + if (FP9HandShake) { /* Server version */ serversig[4] = 3; serversig[5] = 5; serversig[6] = 1; serversig[7] = 1; - + getdig = digoff[offalg]; getdh = dhoff[offalg]; } else { memset(&serversig[4], 0, 4); } - -/* generate random data */ + + /* generate random data */ #ifdef _DEBUG memset(serversig + 8, 0, RTMP_SIG_SIZE - 8); #else @@ -780,7 +782,7 @@ static int SHandShake(RTMP *r) { for (i = 2; i < RTMP_SIG_SIZE / 4; i++) *ip++ = rand(); #endif - + /* set handshake digest */ if (FP9HandShake) { if (encrypted) { @@ -791,84 +793,84 @@ static int SHandShake(RTMP *r) { __FUNCTION__); return FALSE; } - + dhposServer = getdh(serversig, RTMP_SIG_SIZE); RTMP_Log(RTMP_LOGDEBUG, "%s: DH pubkey position: %d", __FUNCTION__, dhposServer); - + if (!DHGenerateKey(r->Link.dh)) { RTMP_Log(RTMP_LOGERROR, "%s: Couldn't generate Diffie-Hellmann public key!", __FUNCTION__); return FALSE; } - + if (!DHGetPublicKey(r->Link.dh, (uint8_t *)&serversig[dhposServer], 128)) { RTMP_Log(RTMP_LOGERROR, "%s: Couldn't write public key!", __FUNCTION__); return FALSE; } } - + digestPosServer = - getdig(serversig, RTMP_SIG_SIZE); /* reuse this value in verification */ + getdig(serversig, RTMP_SIG_SIZE); /* reuse this value in verification */ RTMP_Log(RTMP_LOGDEBUG, "%s: Server digest offset: %d", __FUNCTION__, digestPosServer); - + CalculateDigest(digestPosServer, serversig, GenuineFMSKey, 36, &serversig[digestPosServer]); - + RTMP_Log(RTMP_LOGDEBUG, "%s: Initial server digest: ", __FUNCTION__); RTMP_LogHex(RTMP_LOGDEBUG, serversig + digestPosServer, SHA256_DIGEST_LENGTH); } - + RTMP_Log(RTMP_LOGDEBUG2, "Serversig: "); RTMP_LogHex(RTMP_LOGDEBUG2, serversig, RTMP_SIG_SIZE); - - if (!WriteN(r, (char *)serversig - 1, RTMP_SIG_SIZE + 1)) + + if (!WriteN(r, (char *)serversig - 1, RTMP_SIG_SIZE + 1,error)) // ATIQ EDITED return FALSE; - + /* decode client response */ memcpy(&uptime, clientsig, 4); uptime = ntohl(uptime); - + RTMP_Log(RTMP_LOGDEBUG, "%s: Client Uptime : %d", __FUNCTION__, uptime); RTMP_Log(RTMP_LOGDEBUG, "%s: Player Version: %d.%d.%d.%d", __FUNCTION__, clientsig[4], clientsig[5], clientsig[6], clientsig[7]); - + if (FP9HandShake) { uint8_t digestResp[SHA256_DIGEST_LENGTH]; uint8_t *signatureResp = NULL; - + /* we have to use this signature now to find the correct algorithms for - * getting the digest and DH positions */ + * getting the digest and DH positions */ int digestPosClient = getdig(clientsig, RTMP_SIG_SIZE); - + if (!VerifyDigest(digestPosClient, clientsig, GenuineFPKey, 30)) { RTMP_Log(RTMP_LOGWARNING, "Trying different position for client digest!"); offalg ^= 1; getdig = digoff[offalg]; getdh = dhoff[offalg]; - + digestPosClient = getdig(clientsig, RTMP_SIG_SIZE); - + if (!VerifyDigest(digestPosClient, clientsig, GenuineFPKey, 30)) { RTMP_Log( - RTMP_LOGERROR, - "Couldn't verify the client digest"); /* continuing anyway will - probably fail */ + RTMP_LOGERROR, + "Couldn't verify the client digest"); /* continuing anyway will + probably fail */ return FALSE; } } - + /* generate SWFVerification token (SHA256 HMAC hash of decompressed SWF, key - * are the last 32 bytes of the server handshake) */ + * are the last 32 bytes of the server handshake) */ if (r->Link.SWFSize) { const char swfVerify[] = {0x01, 0x01}; char *vend = r->Link.SWFVerificationResponse + - sizeof(r->Link.SWFVerificationResponse); - + sizeof(r->Link.SWFVerificationResponse); + memcpy(r->Link.SWFVerificationResponse, swfVerify, 2); AMF_EncodeInt32(&r->Link.SWFVerificationResponse[2], vend, r->Link.SWFSize); @@ -879,33 +881,33 @@ static int SHandShake(RTMP *r) { SHA256_DIGEST_LENGTH, (uint8_t *)&r->Link.SWFVerificationResponse[10]); } - + /* do Diffie-Hellmann Key exchange for encrypted RTMP */ if (encrypted) { int dhposClient, len; /* compute secret key */ uint8_t secretKey[128] = {0}; - + dhposClient = getdh(clientsig, RTMP_SIG_SIZE); RTMP_Log(RTMP_LOGDEBUG, "%s: Client DH public key offset: %d", __FUNCTION__, dhposClient); len = DHComputeSharedSecretKey( - r->Link.dh, (uint8_t *)&clientsig[dhposClient], 128, secretKey); + r->Link.dh, (uint8_t *)&clientsig[dhposClient], 128, secretKey); if (len < 0) { RTMP_Log(RTMP_LOGDEBUG, "%s: Wrong secret key position!", __FUNCTION__); return FALSE; } - + RTMP_Log(RTMP_LOGDEBUG, "%s: Secret key: ", __FUNCTION__); RTMP_LogHex(RTMP_LOGDEBUG, secretKey, 128); - + InitRC4Encryption(secretKey, (uint8_t *)&clientsig[dhposClient], (uint8_t *)&serversig[dhposServer], &keyIn, &keyOut); } - + /* calculate response now */ signatureResp = clientsig + RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH; - + HMACsha256(&clientsig[digestPosClient], SHA256_DIGEST_LENGTH, GenuineFMSKey, sizeof(GenuineFMSKey), digestResp); HMACsha256(clientsig, RTMP_SIG_SIZE - SHA256_DIGEST_LENGTH, digestResp, @@ -919,55 +921,55 @@ static int SHandShake(RTMP *r) { rtmpe8_sig(sig + i, sig + i, dptr[i] % 15); } #if 0 - else if (type == 9)) + else if (type == 9)) { - uint8_t *dptr = digestResp; - uint8_t *sig = signatureResp; - /* encrypt signatureResp */ - for (i=0; iLink.rc4keyIn = keyIn; r->Link.rc4keyOut = keyOut; - + /* update the keystreams */ if (r->Link.rc4keyIn) { RC4_encrypt(r->Link.rc4keyIn, RTMP_SIG_SIZE, (uint8_t *)buff); } - + if (r->Link.rc4keyOut) { RC4_encrypt(r->Link.rc4keyOut, RTMP_SIG_SIZE, (uint8_t *)buff); } @@ -1028,7 +1030,7 @@ static int SHandShake(RTMP *r) { __FUNCTION__); } } - + RTMP_Log(RTMP_LOGDEBUG, "%s: Handshaking finished....", __FUNCTION__); return TRUE; } diff --git a/LFLiveKit/Vendor/pili-librtmp/hashswf.c b/LFLiveKit/Vendor/pili-librtmp/hashswf.c index 88d20c7f..89a57a53 100644 --- a/LFLiveKit/Vendor/pili-librtmp/hashswf.c +++ b/LFLiveKit/Vendor/pili-librtmp/hashswf.c @@ -71,7 +71,7 @@ #define HMAC_close(ctx) HMAC_CTX_cleanup(&ctx) #endif -extern void RTMP_TLS_Init(); +extern void PILI_RTMP_TLS_Init(); extern TLS_CTX RTMP_TLS_ctx; #endif /* CRYPTO */ @@ -110,7 +110,7 @@ HTTPResult ssl = 1; port = 443; if (!RTMP_TLS_ctx) - RTMP_TLS_Init(); + PILI_RTMP_TLS_Init(); #else return HTTPRES_BAD_REQUEST; #endif diff --git a/LFLiveKit/Vendor/pili-librtmp/rtmp.c b/LFLiveKit/Vendor/pili-librtmp/rtmp.c index 5043885b..af10de15 100644 --- a/LFLiveKit/Vendor/pili-librtmp/rtmp.c +++ b/LFLiveKit/Vendor/pili-librtmp/rtmp.c @@ -127,7 +127,7 @@ static int clk_tck; #endif uint32_t - PILI_RTMP_GetTime() { +PILI_RTMP_GetTime() { #ifdef _DEBUG return 0; #elif defined(_WIN32) @@ -189,9 +189,9 @@ void PILI_RTMP_TLS_Init() { havege_init(&RTMP_TLS_ctx->hs); #elif defined(USE_GNUTLS) && !defined(NO_SSL) /* Technically we need to initialize libgcrypt ourselves if - * we're not going to call gnutls_global_init(). Ignoring this - * for now. - */ + * we're not going to call gnutls_global_init(). Ignoring this + * for now. + */ gnutls_global_init(); RTMP_TLS_ctx = malloc(sizeof(struct tls_ctx)); gnutls_certificate_allocate_credentials(&RTMP_TLS_ctx->cred); @@ -211,7 +211,7 @@ void PILI_RTMP_TLS_Init() { } PILI_RTMP * - PILI_RTMP_Alloc() { +PILI_RTMP_Alloc() { return calloc(1, sizeof(PILI_RTMP)); } @@ -220,16 +220,17 @@ void PILI_RTMP_Free(PILI_RTMP *r) { r->m_userData = NULL; RTMPError_Free(r->m_error); r->m_error = NULL; - + free(r); } void PILI_RTMP_Init(PILI_RTMP *r) { #ifdef CRYPTO if (!RTMP_TLS_ctx) - RTMP_TLS_Init(); + //RTMP_TLS_Init(); + PILI_RTMP_TLS_Init(); // ATIQ EDITED #endif - + memset(r, 0, sizeof(PILI_RTMP)); r->m_sb.sb_socket = -1; r->m_inChunkSize = RTMP_DEFAULT_CHUNKSIZE; @@ -243,13 +244,13 @@ void PILI_RTMP_Init(PILI_RTMP *r) { r->Link.timeout = 10; r->Link.send_timeout = 10; r->Link.swfAge = 30; - + r->m_errorCallback = NULL; r->m_error = NULL; r->m_userData = NULL; r->m_is_closing = 0; r->m_tcp_nodelay = 1; - + r->m_connCallback = NULL; r->ip = 0; } @@ -259,7 +260,7 @@ void PILI_RTMP_EnableWrite(PILI_RTMP *r) { } double - PILI_RTMP_GetDuration(PILI_RTMP *r) { +PILI_RTMP_GetDuration(PILI_RTMP *r) { return r->m_fDuration; } @@ -298,7 +299,7 @@ void PILI_RTMP_UpdateBufferMS(PILI_RTMP *r, RTMPError *error) { #define DEF_VERSTR OSS " 10,0,32,18" static const char DEFAULT_FLASH_VER[] = DEF_VERSTR; const AVal RTMP_DefaultFlashVer = - {(char *)DEFAULT_FLASH_VER, sizeof(DEFAULT_FLASH_VER) - 1}; +{(char *)DEFAULT_FLASH_VER, sizeof(DEFAULT_FLASH_VER) - 1}; void PILI_RTMP_SetupStream(PILI_RTMP *r, int protocol, @@ -321,7 +322,7 @@ void PILI_RTMP_SetupStream(PILI_RTMP *r, RTMP_Log(RTMP_LOGDEBUG, "Hostname : %.*s", host->av_len, host->av_val); RTMP_Log(RTMP_LOGDEBUG, "Port : %d", port); RTMP_Log(RTMP_LOGDEBUG, "Playpath : %s", playpath->av_val); - + if (tcUrl && tcUrl->av_val) RTMP_Log(RTMP_LOGDEBUG, "tcUrl : %s", tcUrl->av_val); if (swfUrl && swfUrl->av_val) @@ -340,10 +341,10 @@ void PILI_RTMP_SetupStream(PILI_RTMP *r, RTMP_Log(RTMP_LOGDEBUG, "StartTime : %d msec", dStart); if (dStop > 0) RTMP_Log(RTMP_LOGDEBUG, "StopTime : %d msec", dStop); - + RTMP_Log(RTMP_LOGDEBUG, "live : %s", bLiveStream ? "yes" : "no"); RTMP_Log(RTMP_LOGDEBUG, "timeout : %d sec", timeout); - + #ifdef CRYPTO if (swfSHA256Hash != NULL && swfSize > 0) { memcpy(r->Link.SWFHash, swfSHA256Hash->av_val, sizeof(r->Link.SWFHash)); @@ -355,16 +356,16 @@ void PILI_RTMP_SetupStream(PILI_RTMP *r, r->Link.SWFSize = 0; } #endif - + if (sockshost->av_len) { const char *socksport = strchr(sockshost->av_val, ':'); char *hostname = strdup(sockshost->av_val); - + if (socksport) hostname[socksport - sockshost->av_val] = '\0'; r->Link.sockshost.av_val = hostname; r->Link.sockshost.av_len = strlen(hostname); - + r->Link.socksport = socksport ? atoi(socksport + 1) : 1080; RTMP_Log(RTMP_LOGDEBUG, "Connecting via SOCKS proxy: %s:%d", r->Link.sockshost.av_val, r->Link.socksport); @@ -373,7 +374,7 @@ void PILI_RTMP_SetupStream(PILI_RTMP *r, r->Link.sockshost.av_len = 0; r->Link.socksport = 0; } - + if (tcUrl && tcUrl->av_len) r->Link.tcUrl = *tcUrl; if (swfUrl && swfUrl->av_len) @@ -397,12 +398,12 @@ void PILI_RTMP_SetupStream(PILI_RTMP *r, if (bLiveStream) r->Link.lFlags |= RTMP_LF_LIVE; r->Link.timeout = timeout; - + r->Link.protocol = protocol; r->Link.hostname = *host; r->Link.port = port; r->Link.playpath = *playpath; - + if (r->Link.port == 0) { if (protocol & RTMP_FEATURE_SSL) r->Link.port = 443; @@ -414,9 +415,9 @@ void PILI_RTMP_SetupStream(PILI_RTMP *r, } enum { OPT_STR = 0, - OPT_INT, - OPT_BOOL, - OPT_CONN }; + OPT_INT, + OPT_BOOL, + OPT_CONN }; static const char *optinfo[] = { "string", "integer", "boolean", "AMF"}; @@ -430,41 +431,41 @@ static struct urlopt { char *use; } options[] = { {AVC("socks"), OFF(Link.sockshost), OPT_STR, 0, - "Use the specified SOCKS proxy"}, + "Use the specified SOCKS proxy"}, {AVC("app"), OFF(Link.app), OPT_STR, 0, - "Name of target app on server"}, + "Name of target app on server"}, {AVC("tcUrl"), OFF(Link.tcUrl), OPT_STR, 0, - "URL to played stream"}, + "URL to played stream"}, {AVC("pageUrl"), OFF(Link.pageUrl), OPT_STR, 0, - "URL of played media's web page"}, + "URL of played media's web page"}, {AVC("swfUrl"), OFF(Link.swfUrl), OPT_STR, 0, - "URL to player SWF file"}, + "URL to player SWF file"}, {AVC("flashver"), OFF(Link.flashVer), OPT_STR, 0, - "Flash version string (default " DEF_VERSTR ")"}, + "Flash version string (default " DEF_VERSTR ")"}, {AVC("conn"), OFF(Link.extras), OPT_CONN, 0, - "Append arbitrary AMF data to Connect message"}, + "Append arbitrary AMF data to Connect message"}, {AVC("playpath"), OFF(Link.playpath), OPT_STR, 0, - "Path to target media on server"}, + "Path to target media on server"}, {AVC("playlist"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_PLST, - "Set playlist before play command"}, + "Set playlist before play command"}, {AVC("live"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_LIVE, - "Stream is live, no seeking possible"}, + "Stream is live, no seeking possible"}, {AVC("subscribe"), OFF(Link.subscribepath), OPT_STR, 0, - "Stream to subscribe to"}, + "Stream to subscribe to"}, {AVC("token"), OFF(Link.token), OPT_STR, 0, - "Key for SecureToken response"}, + "Key for SecureToken response"}, {AVC("swfVfy"), OFF(Link.lFlags), OPT_BOOL, RTMP_LF_SWFV, - "Perform SWF Verification"}, + "Perform SWF Verification"}, {AVC("swfAge"), OFF(Link.swfAge), OPT_INT, 0, - "Number of days to use cached SWF hash"}, + "Number of days to use cached SWF hash"}, {AVC("start"), OFF(Link.seekTime), OPT_INT, 0, - "Stream start position in milliseconds"}, + "Stream start position in milliseconds"}, {AVC("stop"), OFF(Link.stopTime), OPT_INT, 0, - "Stream stop position in milliseconds"}, + "Stream stop position in milliseconds"}, {AVC("buffer"), OFF(m_nBufferMS), OPT_INT, 0, - "Buffer time in milliseconds"}, + "Buffer time in milliseconds"}, {AVC("timeout"), OFF(Link.timeout), OPT_INT, 0, - "Session timeout in seconds"}, + "Session timeout in seconds"}, {{NULL, 0}, 0, 0}}; static const AVal truth[] = { @@ -476,7 +477,7 @@ static const AVal truth[] = { static void RTMP_OptUsage() { int i; - + RTMP_Log(RTMP_LOGERROR, "Valid PILI_RTMP options are:\n"); for (i = 0; options[i].name.av_len; i++) { RTMP_Log(RTMP_LOGERROR, "%10s %-7s %s\n", options[i].name.av_val, @@ -485,11 +486,11 @@ static void RTMP_OptUsage() { } static int - parseAMF(AMFObject *obj, AVal *av, int *depth) { +parseAMF(AMFObject *obj, AVal *av, int *depth) { AMFObjectProperty prop = {{0, 0}}; int i; char *p, *arg = av->av_val; - + if (arg[1] == ':') { p = (char *)arg + 2; switch (arg[0]) { @@ -527,7 +528,7 @@ static int return -1; prop.p_name.av_val = (char *)arg + 3; prop.p_name.av_len = p - (arg + 3); - + p++; switch (arg[1]) { case 'B': @@ -551,7 +552,7 @@ static int } } else return -1; - + if (*depth) { AMFObject *o2; for (i = 0; i < *depth; i++) { @@ -568,7 +569,7 @@ static int int RTMP_SetOpt(PILI_RTMP *r, const AVal *opt, AVal *arg, RTMPError *error) { int i; void *v; - + for (i = 0; options[i].name.av_len; i++) { if (opt->av_len != options[i].name.av_len) continue; if (strcasecmp(opt->av_val, options[i].name.av_val)) continue; @@ -610,12 +611,12 @@ int RTMP_SetOpt(PILI_RTMP *r, const AVal *opt, AVal *arg, RTMPError *error) { error->code = RTMPErrorUnknowOption; strcpy(error->message, msg); } - + RTMP_Log(RTMP_LOGERROR, "Unknown option %s", opt->av_val); RTMP_OptUsage(); return FALSE; } - + return TRUE; } @@ -624,10 +625,10 @@ int PILI_RTMP_SetupURL(PILI_RTMP *r, const char *url, RTMPError *error) { char *p1, *p2, *ptr = strchr(url, ' '); int ret, len; unsigned int port = 0; - + if (ptr) *ptr = '\0'; - + len = (int)strlen(url); ret = PILI_RTMP_ParseURL2(url, &r->Link.protocol, &r->Link.hostname, &port, &r->Link.playpath0, &r->Link.app, &r->Link.domain); @@ -635,7 +636,7 @@ int PILI_RTMP_SetupURL(PILI_RTMP *r, const char *url, RTMPError *error) { return ret; r->Link.port = port; r->Link.playpath = r->Link.playpath0; - + while (ptr) { *ptr++ = '\0'; p1 = ptr; @@ -656,7 +657,7 @@ int PILI_RTMP_SetupURL(PILI_RTMP *r, const char *url, RTMPError *error) { } else { arg.av_len = strlen(p2); } - + /* unescape */ port = arg.av_len; for (p1 = p2; port > 0;) { @@ -674,12 +675,12 @@ int PILI_RTMP_SetupURL(PILI_RTMP *r, const char *url, RTMPError *error) { } } arg.av_len = p2 - arg.av_val; - + ret = RTMP_SetOpt(r, &opt, &arg, error); if (!ret) return ret; } - + if (!r->Link.tcUrl.av_len) { r->Link.tcUrl.av_val = url; if (r->Link.app.av_len) { @@ -708,13 +709,13 @@ int PILI_RTMP_SetupURL(PILI_RTMP *r, const char *url, RTMPError *error) { r->Link.tcUrl.av_len = strlen(url); } } - -#ifdef CRYPTO - if ((r->Link.lFlags & RTMP_LF_SWFV) && r->Link.swfUrl.av_len) - RTMP_HashSWF(r->Link.swfUrl.av_val, &r->Link.SWFSize, - (unsigned char *)r->Link.SWFHash, r->Link.swfAge); -#endif - + + /*#ifdef CRYPTO + if ((r->Link.lFlags & RTMP_LF_SWFV) && r->Link.swfUrl.av_len) + PILI_RTMP_HashSWF(r->Link.swfUrl.av_val, &r->Link.SWFSize, + (unsigned char *)r->Link.SWFHash, r->Link.swfAge); + #endif*/ + if (r->Link.port == 0) { if (r->Link.protocol & RTMP_FEATURE_SSL) r->Link.port = 443; @@ -736,7 +737,7 @@ static int add_addr_info(PILI_RTMP *r, struct addrinfo *hints, struct addrinfo * } else { hostname = host->av_val; } - + struct addrinfo *cur_ai; char portstr[10]; snprintf(portstr, sizeof(portstr), "%d", port); @@ -746,14 +747,14 @@ static int add_addr_info(PILI_RTMP *r, struct addrinfo *hints, struct addrinfo * memset(msg, 0, 100); strcat(msg, "Problem accessing the DNS. addr: "); strcat(msg, hostname); - + RTMPError_Alloc(error, strlen(msg)); error->code = RTMPErrorAccessDNSFailed; strcpy(error->message, msg); RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname); ret = FALSE; } - + if (hostname != host->av_val) { free(hostname); } @@ -764,7 +765,7 @@ int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, R r->m_sb.sb_timedout = FALSE; r->m_pausing = 0; r->m_fDuration = 0.0; - + r->m_sb.sb_socket = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (ai->ai_family == AF_INET6) { struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)ai->ai_addr; @@ -773,7 +774,7 @@ int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, R if (r->m_sb.sb_socket != -1) { if (connect(r->m_sb.sb_socket, ai->ai_addr, ai->ai_addrlen) < 0) { int err = GetSockError(); - + if (error) { char msg[100]; memset(msg, 0, 100); @@ -783,14 +784,14 @@ int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, R error->code = RTMPErrorFailedToConnectSocket; strcpy(error->message, msg); } - + RTMP_Log(RTMP_LOGERROR, "%s, failed to connect socket. %d (%s)", __FUNCTION__, err, strerror(err)); - + PILI_RTMP_Close(r, NULL); return FALSE; } - + if (r->Link.socksport) { RTMP_Log(RTMP_LOGDEBUG, "%s ... SOCKS negotiation", __FUNCTION__); if (!SocksNegotiate(r, error)) { @@ -802,7 +803,7 @@ int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, R error->code = RTMPErrorSocksNegotiationFailed; strcpy(error->message, msg); } - + RTMP_Log(RTMP_LOGERROR, "%s, SOCKS negotiation failed.", __FUNCTION__); PILI_RTMP_Close(r, NULL); return FALSE; @@ -810,7 +811,7 @@ int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, R } } else { int err = GetSockError(); - + if (error) { char msg[100]; memset(msg, 0, 100); @@ -820,12 +821,12 @@ int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, R error->code = RTMPErrorFailedToCreateSocket; strcpy(error->message, msg); } - + RTMP_Log(RTMP_LOGERROR, "%s, failed to create socket. Error: %d (%s)", __FUNCTION__, err, strerror(err)); - + return FALSE; } - + /* set receive timeout */ { SET_RCVTIMEO(tv, r->Link.timeout); @@ -834,19 +835,19 @@ int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, R __FUNCTION__, r->Link.timeout); } } - + /* set send timeout*/ { struct timeval timeout; timeout.tv_sec = r->Link.send_timeout; timeout.tv_usec = 0; - + if (setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout))) { RTMP_Log(RTMP_LOGERROR, "%s, Setting socket send timeout to %ds failed!", __FUNCTION__, r->Link.timeout); } } - + /* ignore sigpipe */ int kOne = 1; #ifdef __linux @@ -858,7 +859,7 @@ int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, R int on = 1; setsockopt(r->m_sb.sb_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on)); } - + return TRUE; } @@ -876,25 +877,25 @@ int PILI_RTMP_Connect1(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error) { error->code = RTMPErrorTLSConnectFailed; strcpy(error->message, msg); } - + RTMP_Log(RTMP_LOGERROR, "%s, TLS_Connect failed", __FUNCTION__); - RTMP_Close(r, NULL); + PILI_RTMP_Close(r, NULL); return FALSE; } #else if (error) { char msg[100]; memset(msg, 0, 100); - strcat(msg, "No SSL/TLS support."); + strcat(msg, "No SSL/TLS support 101."); RTMPError_Alloc(error, strlen(msg)); error->code = RTMPErrorNoSSLOrTLSSupport; strcpy(error->message, msg); } - - RTMP_Log(RTMP_LOGERROR, "%s, no SSL/TLS support", __FUNCTION__); + + RTMP_Log(RTMP_LOGERROR, "%s, no SSL/TLS support 102", __FUNCTION__); PILI_RTMP_Close(r, NULL); return FALSE; - + #endif } if (r->Link.protocol & RTMP_FEATURE_HTTP) { @@ -915,13 +916,13 @@ int PILI_RTMP_Connect1(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error) { error->code = RTMPErrorHandshakeFailed; strcpy(error->message, msg); } - + RTMP_Log(RTMP_LOGERROR, "%s, handshake failed.", __FUNCTION__); PILI_RTMP_Close(r, NULL); return FALSE; } RTMP_Log(RTMP_LOGDEBUG, "%s, handshaked", __FUNCTION__); - + if (!SendConnectPacket(r, cp, error)) { if (error) { char msg[100]; @@ -942,7 +943,7 @@ int PILI_RTMP_Connect(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error) { struct PILI_CONNECTION_TIME conn_time; if (!r->Link.hostname.av_len) return FALSE; - + struct addrinfo hints = {0}, *ai, *cur_ai; hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -963,7 +964,7 @@ int PILI_RTMP_Connect(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error) { } r->ip = 0; //useless for ipv6 cur_ai = ai; - + int t1 = PILI_RTMP_GetTime(); if (!PILI_RTMP_Connect0(r, cur_ai, port, error)) { freeaddrinfo(ai); @@ -971,11 +972,11 @@ int PILI_RTMP_Connect(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error) { } conn_time.connect_time = PILI_RTMP_GetTime() - t1; r->m_bSendCounter = TRUE; - + int t2 = PILI_RTMP_GetTime(); int ret = PILI_RTMP_Connect1(r, cp, error); conn_time.handshake_time = PILI_RTMP_GetTime() - t2; - + if (r->m_connCallback != NULL) { r->m_connCallback(&conn_time, r->m_userData); } @@ -985,7 +986,7 @@ int PILI_RTMP_Connect(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error) { //useless static int - SocksNegotiate(PILI_RTMP *r, RTMPError *error) { +SocksNegotiate(PILI_RTMP *r, RTMPError *error) { // unsigned long addr; // struct sockaddr_in service; // memset(&service, 0, sizeof(struct sockaddr_in)); @@ -995,13 +996,13 @@ static int // // { // char packet[] = { - // 4, 1, /* SOCKS 4, connect */ + // 4, 1, /* SOCKS 4, connect */ // (r->Link.port >> 8) & 0xFF, // (r->Link.port) & 0xFF, // (char)(addr >> 24) & 0xFF, (char)(addr >> 16) & 0xFF, // (char)(addr >> 8) & 0xFF, (char)addr & 0xFF, // 0 - // }; /* NULL terminate */ + // }; /* NULL terminate */ // // WriteN(r, packet, sizeof packet, error); // @@ -1023,15 +1024,15 @@ static int int PILI_RTMP_ConnectStream(PILI_RTMP *r, int seekTime, RTMPError *error) { PILI_RTMPPacket packet = {0}; - + /* seekTime was already set by SetupStream / SetupURL. - * This is only needed by ReconnectStream. - */ + * This is only needed by ReconnectStream. + */ if (seekTime > 0) r->Link.seekTime = seekTime; - + r->m_mediaChannel = 0; - + while (!r->m_bPlaying && PILI_RTMP_IsConnected(r) && PILI_RTMP_ReadPacket(r, &packet)) { if (RTMPPacket_IsReady(&packet)) { if (!packet.m_nBodySize) @@ -1043,38 +1044,38 @@ int PILI_RTMP_ConnectStream(PILI_RTMP *r, int seekTime, RTMPError *error) { PILI_RTMPPacket_Free(&packet); continue; } - + PILI_RTMP_ClientPacket(r, &packet); PILI_RTMPPacket_Free(&packet); } } - + if (!r->m_bPlaying && error) { char *msg = "PILI_RTMP connect stream failed."; RTMPError_Alloc(error, strlen(msg)); error->code = RTMPErrorRTMPConnectStreamFailed; strcpy(error->message, msg); } - + return r->m_bPlaying; } int PILI_RTMP_ReconnectStream(PILI_RTMP *r, int seekTime, RTMPError *error) { PILI_RTMP_DeleteStream(r, error); - + PILI_RTMP_SendCreateStream(r, error); - + return PILI_RTMP_ConnectStream(r, seekTime, error); } int PILI_RTMP_ToggleStream(PILI_RTMP *r, RTMPError *error) { int res; - + if (!r->m_pausing) { res = PILI_RTMP_SendPause(r, TRUE, r->m_pauseStamp, error); if (!res) return res; - + r->m_pausing = 1; sleep(1); } @@ -1086,23 +1087,23 @@ int PILI_RTMP_ToggleStream(PILI_RTMP *r, RTMPError *error) { void PILI_RTMP_DeleteStream(PILI_RTMP *r, RTMPError *error) { if (r->m_stream_id < 0) return; - + r->m_bPlaying = FALSE; - + SendDeleteStream(r, r->m_stream_id, error); r->m_stream_id = -1; } int PILI_RTMP_GetNextMediaPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { int bHasMediaPacket = 0; - + while (!bHasMediaPacket && PILI_RTMP_IsConnected(r) && PILI_RTMP_ReadPacket(r, packet)) { if (!RTMPPacket_IsReady(packet)) { continue; } - + bHasMediaPacket = PILI_RTMP_ClientPacket(r, packet); - + if (!bHasMediaPacket) { PILI_RTMPPacket_Free(packet); } else if (r->m_pausing == 3) { @@ -1120,12 +1121,12 @@ int PILI_RTMP_GetNextMediaPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { r->m_pausing = 0; } } - + if (bHasMediaPacket) r->m_bPlaying = TRUE; else if (r->m_sb.sb_timedout && !r->m_pausing) r->m_pauseStamp = r->m_channelTimestamp[r->m_mediaChannel]; - + return bHasMediaPacket; } @@ -1136,27 +1137,27 @@ int PILI_RTMP_ClientPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { /* chunk size */ HandleChangeChunkSize(r, packet); break; - + case 0x03: /* bytes read report */ RTMP_Log(RTMP_LOGDEBUG, "%s, received: bytes read report", __FUNCTION__); break; - + case 0x04: /* ctrl */ HandleCtrl(r, packet); break; - + case 0x05: /* server bw */ HandleServerBW(r, packet); break; - + case 0x06: /* client bw */ HandleClientBW(r, packet); break; - + case 0x08: /* audio data */ /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: audio %lu bytes", __FUNCTION__, packet.m_nBodySize); */ @@ -1167,7 +1168,7 @@ int PILI_RTMP_ClientPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { if (!r->m_pausing) r->m_mediaStamp = packet->m_nTimeStamp; break; - + case 0x09: /* video data */ /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: video %lu bytes", __FUNCTION__, packet.m_nBodySize); */ @@ -1178,38 +1179,38 @@ int PILI_RTMP_ClientPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { if (!r->m_pausing) r->m_mediaStamp = packet->m_nTimeStamp; break; - + case 0x0F: /* flex stream send */ RTMP_Log(RTMP_LOGDEBUG, "%s, flex stream send, size %lu bytes, not supported, ignoring", __FUNCTION__, packet->m_nBodySize); break; - + case 0x10: /* flex shared object */ RTMP_Log(RTMP_LOGDEBUG, "%s, flex shared object, size %lu bytes, not supported, ignoring", __FUNCTION__, packet->m_nBodySize); break; - + case 0x11: /* flex message */ { RTMP_Log(RTMP_LOGDEBUG, "%s, flex message, size %lu bytes, not fully supported", __FUNCTION__, packet->m_nBodySize); -/*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ - -/* some DEBUG code */ + /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ + + /* some DEBUG code */ #if 0 - RTMP_LIB_AMFObject obj; - int nRes = obj.Decode(packet.m_body+1, packet.m_nBodySize-1); - if(nRes < 0) { - RTMP_Log(RTMP_LOGERROR, "%s, error decoding AMF3 packet", __FUNCTION__); - /*return; */ - } - - obj.Dump(); + RTMP_LIB_AMFObject obj; + int nRes = obj.Decode(packet.m_body+1, packet.m_nBodySize-1); + if(nRes < 0) { + RTMP_Log(RTMP_LOGERROR, "%s, error decoding AMF3 packet", __FUNCTION__); + /*return; */ + } + + obj.Dump(); #endif - + if (HandleInvoke(r, packet->m_body + 1, packet->m_nBodySize - 1) == 1) bHasMediaPacket = 2; break; @@ -1221,30 +1222,30 @@ int PILI_RTMP_ClientPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { if (HandleMetadata(r, packet->m_body, packet->m_nBodySize)) bHasMediaPacket = 1; break; - + case 0x13: RTMP_Log(RTMP_LOGDEBUG, "%s, shared object, not supported, ignoring", __FUNCTION__); break; - + case 0x14: /* invoke */ RTMP_Log(RTMP_LOGDEBUG, "%s, received: invoke %lu bytes", __FUNCTION__, packet->m_nBodySize); /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ - + if (HandleInvoke(r, packet->m_body, packet->m_nBodySize) == 1) bHasMediaPacket = 2; break; - + case 0x16: { /* go through FLV packets and handle metadata packets */ unsigned int pos = 0; uint32_t nTimeStamp = packet->m_nTimeStamp; - + while (pos + 11 < packet->m_nBodySize) { uint32_t dataSize = AMF_DecodeInt24(packet->m_body + pos + 1); /* size without header (11) and prevTagSize (4) */ - + if (pos + 11 + dataSize + 4 > packet->m_nBodySize) { RTMP_Log(RTMP_LOGWARNING, "Stream corrupt?!"); break; @@ -1259,7 +1260,7 @@ int PILI_RTMP_ClientPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { } if (!r->m_pausing) r->m_mediaStamp = nTimeStamp; - + /* FLV tag(s) */ /*RTMP_Log(RTMP_LOGDEBUG, "%s, received: FLV tag(s) %lu bytes", __FUNCTION__, packet.m_nBodySize); */ bHasMediaPacket = 1; @@ -1272,7 +1273,7 @@ int PILI_RTMP_ClientPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { RTMP_LogHex(RTMP_LOGDEBUG, packet->m_body, packet->m_nBodySize); #endif } - + return bHasMediaPacket; } @@ -1282,17 +1283,17 @@ extern FILE *netstackdump_read; #endif static int - ReadN(PILI_RTMP *r, char *buffer, int n) { +ReadN(PILI_RTMP *r, char *buffer, int n) { int nOriginalSize = n; int avail; char *ptr; - + r->m_sb.sb_timedout = FALSE; - + #ifdef _DEBUG memset(buffer, 0, n); #endif - + ptr = buffer; while (n > 0) { int nBytes = 0, nRead; @@ -1306,19 +1307,19 @@ static int PILI_RTMP_Close(r, NULL); } else { RTMPError error = {0}; - + char msg[100]; memset(msg, 0, 100); strcat(msg, "PILI_RTMP socket timeout"); RTMPError_Alloc(&error, strlen(msg)); error.code = RTMPErrorSocketTimeout; strcpy(error.message, msg); - + PILI_RTMP_Close(r, &error); - + RTMPError_Free(&error); } - + return 0; } } @@ -1337,19 +1338,19 @@ static int PILI_RTMP_Close(r, NULL); } else { RTMPError error = {0}; - + char msg[100]; memset(msg, 0, 100); strcat(msg, "PILI_RTMP socket timeout"); RTMPError_Alloc(&error, strlen(msg)); error.code = RTMPErrorSocketTimeout; strcpy(error.message, msg); - + PILI_RTMP_Close(r, &error); - + RTMPError_Free(&error); } - + return 0; } avail = r->m_sb.sb_size; @@ -1365,52 +1366,52 @@ static int if (r->m_bSendCounter && r->m_nBytesIn > r->m_nBytesInSent + r->m_nClientBW / 2) SendBytesReceived(r, NULL); } -/*RTMP_Log(RTMP_LOGDEBUG, "%s: %d bytes\n", __FUNCTION__, nBytes); */ + /*RTMP_Log(RTMP_LOGDEBUG, "%s: %d bytes\n", __FUNCTION__, nBytes); */ #ifdef _DEBUG fwrite(ptr, 1, nBytes, netstackdump_read); #endif - + if (nBytes == 0) { RTMP_Log(RTMP_LOGDEBUG, "%s, PILI_RTMP socket closed by peer", __FUNCTION__); /*goto again; */ RTMPError error = {0}; - + char msg[100]; memset(msg, 0, 100); strcat(msg, "PILI_RTMP socket closed by peer. "); RTMPError_Alloc(&error, strlen(msg)); error.code = RTMPErrorSocketClosedByPeer; strcpy(error.message, msg); - + PILI_RTMP_Close(r, &error); - + RTMPError_Free(&error); break; } - + if (r->Link.protocol & RTMP_FEATURE_HTTP) r->m_resplen -= nBytes; - + #ifdef CRYPTO if (r->Link.rc4keyIn) { RC4_encrypt(r->Link.rc4keyIn, nBytes, ptr); } #endif - + n -= nBytes; ptr += nBytes; } - + return nOriginalSize - n; } static int - WriteN(PILI_RTMP *r, const char *buffer, int n, RTMPError *error) { +WriteN(PILI_RTMP *r, const char *buffer, int n, RTMPError *error) { const char *ptr = buffer; #ifdef CRYPTO char *encrypted = 0; char buf[RTMP_BUFFER_CACHE_SIZE]; - + if (r->Link.rc4keyOut) { if (n > sizeof(buf)) encrypted = (char *)malloc(n); @@ -1420,24 +1421,24 @@ static int RC4_encrypt2(r->Link.rc4keyOut, n, buffer, ptr); } #endif - + while (n > 0) { int nBytes; - + if (r->Link.protocol & RTMP_FEATURE_HTTP) nBytes = HTTP_Post(r, RTMPT_SEND, ptr, n); else nBytes = PILI_RTMPSockBuf_Send(&r->m_sb, ptr, n); /*RTMP_Log(RTMP_LOGDEBUG, "%s: %d\n", __FUNCTION__, nBytes); */ - + if (nBytes < 0) { int sockerr = GetSockError(); RTMP_Log(RTMP_LOGERROR, "%s, PILI_RTMP send error %d, %s, (%d bytes)", __FUNCTION__, sockerr, strerror(sockerr), n); - + if (sockerr == EINTR && !PILI_RTMP_ctrlC) continue; - + if (error) { char msg[100]; memset(msg, 0, 100); @@ -1447,27 +1448,27 @@ static int error->code = RTMPErrorSendFailed; strcpy(error->message, msg); } - + PILI_RTMP_Close(r, error); - + RTMPError_Free(error); - + n = 1; break; } - + if (nBytes == 0) break; - + n -= nBytes; ptr += nBytes; } - + #ifdef CRYPTO if (encrypted && encrypted != buf) free(encrypted); #endif - + return n == 0; } @@ -1491,14 +1492,14 @@ SAVC(type); SAVC(nonprivate); static int - SendConnectPacket(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error) { +SendConnectPacket(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[4096], *pend = pbuf + sizeof(pbuf); char *enc; - + if (cp) return PILI_RTMP_SendPacket(r, cp, TRUE, error); - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_LARGE; packet.m_packetType = 0x14; /* INVOKE */ @@ -1506,12 +1507,12 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_connect); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); *enc++ = AMF_OBJECT; - + enc = AMF_EncodeNamedString(enc, pend, &av_app, &r->Link.app); if (!enc) return FALSE; @@ -1567,7 +1568,7 @@ static int *enc++ = 0; *enc++ = 0; /* end of object - 0x00 0x00 0x09 */ *enc++ = AMF_OBJECT_END; - + /* add auth string */ if (r->Link.auth.av_len) { enc = AMF_EncodeBoolean(enc, pend, r->Link.lFlags & RTMP_LF_AUTH); @@ -1586,7 +1587,7 @@ static int } } packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, TRUE, error); } @@ -1596,30 +1597,30 @@ SAVC(bgHasStream); static int SendBGHasStream(PILI_RTMP *r, double dId, AVal *playpath) { - PILI_RTMPPacket packet; - char pbuf[1024], *pend = pbuf + sizeof(pbuf); - char *enc; - - packet.m_nChannel = 0x03; /* control channel (invoke) */ - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; - packet.m_packetType = 0x14; /* INVOKE */ - packet.m_nTimeStamp = 0; - packet.m_nInfoField2 = 0; - packet.m_hasAbsTimestamp = 0; - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - - enc = packet.m_body; - enc = AMF_EncodeString(enc, pend, &av_bgHasStream); - enc = AMF_EncodeNumber(enc, pend, dId); - *enc++ = AMF_NULL; - - enc = AMF_EncodeString(enc, pend, playpath); - if (enc == NULL) - return FALSE; - - packet.m_nBodySize = enc - packet.m_body; - - return PILI_RTMP_SendPacket(r, &packet, TRUE); + PILI_RTMPPacket packet; + char pbuf[1024], *pend = pbuf + sizeof(pbuf); + char *enc; + + packet.m_nChannel = 0x03; /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; + packet.m_packetType = 0x14; /* INVOKE */ + packet.m_nTimeStamp = 0; + packet.m_nInfoField2 = 0; + packet.m_hasAbsTimestamp = 0; + packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; + + enc = packet.m_body; + enc = AMF_EncodeString(enc, pend, &av_bgHasStream); + enc = AMF_EncodeNumber(enc, pend, dId); + *enc++ = AMF_NULL; + + enc = AMF_EncodeString(enc, pend, playpath); + if (enc == NULL) + return FALSE; + + packet.m_nBodySize = enc - packet.m_body; + + return PILI_RTMP_SendPacket(r, &packet, TRUE); } #endif @@ -1629,7 +1630,7 @@ int PILI_RTMP_SendCreateStream(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* INVOKE */ @@ -1637,21 +1638,21 @@ int PILI_RTMP_SendCreateStream(PILI_RTMP *r, RTMPError *error) { packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_createStream); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); *enc++ = AMF_NULL; /* NULL */ - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, TRUE, error); } SAVC(FCSubscribe); static int - SendFCSubscribe(PILI_RTMP *r, AVal *subscribepath, RTMPError *error) { +SendFCSubscribe(PILI_RTMP *r, AVal *subscribepath, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[512], *pend = pbuf + sizeof(pbuf); char *enc; @@ -1662,30 +1663,30 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + RTMP_Log(RTMP_LOGDEBUG, "FCSubscribe: %s", subscribepath->av_val); enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_FCSubscribe); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); *enc++ = AMF_NULL; enc = AMF_EncodeString(enc, pend, subscribepath); - + if (!enc) return FALSE; - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, TRUE, error); } SAVC(releaseStream); static int - SendReleaseStream(PILI_RTMP *r, RTMPError *error) { +SendReleaseStream(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[1024], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* INVOKE */ @@ -1693,7 +1694,7 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_releaseStream); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); @@ -1701,20 +1702,20 @@ static int enc = AMF_EncodeString(enc, pend, &r->Link.playpath); if (!enc) return FALSE; - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } SAVC(FCPublish); static int - SendFCPublish(PILI_RTMP *r, RTMPError *error) { +SendFCPublish(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[1024], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* INVOKE */ @@ -1722,7 +1723,7 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_FCPublish); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); @@ -1730,20 +1731,20 @@ static int enc = AMF_EncodeString(enc, pend, &r->Link.playpath); if (!enc) return FALSE; - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } SAVC(FCUnpublish); static int - SendFCUnpublish(PILI_RTMP *r, RTMPError *error) { +SendFCUnpublish(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[1024], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* INVOKE */ @@ -1751,7 +1752,7 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_FCUnpublish); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); @@ -1759,9 +1760,9 @@ static int enc = AMF_EncodeString(enc, pend, &r->Link.playpath); if (!enc) return FALSE; - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } @@ -1770,11 +1771,11 @@ SAVC(live); SAVC(record); static int - SendPublish(PILI_RTMP *r, RTMPError *error) { +SendPublish(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[1024], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x04; /* source channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_LARGE; packet.m_packetType = 0x14; /* INVOKE */ @@ -1782,7 +1783,7 @@ static int packet.m_nInfoField2 = r->m_stream_id; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_publish); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); @@ -1790,25 +1791,25 @@ static int enc = AMF_EncodeString(enc, pend, &r->Link.playpath); if (!enc) return FALSE; - + /* FIXME: should we choose live based on Link.lFlags & RTMP_LF_LIVE? */ enc = AMF_EncodeString(enc, pend, &av_live); if (!enc) return FALSE; - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, TRUE, error); } SAVC(deleteStream); static int - SendDeleteStream(PILI_RTMP *r, double dStreamId, RTMPError *error) { +SendDeleteStream(PILI_RTMP *r, double dStreamId, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* INVOKE */ @@ -1816,15 +1817,15 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_deleteStream); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); *enc++ = AMF_NULL; enc = AMF_EncodeNumber(enc, pend, dStreamId); - + packet.m_nBodySize = enc - packet.m_body; - + /* no response expected */ return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } @@ -1835,7 +1836,7 @@ int PILI_RTMP_SendPause(PILI_RTMP *r, int DoPause, int iTime, RTMPError *error) PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x08; /* video channel */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* invoke */ @@ -1843,16 +1844,16 @@ int PILI_RTMP_SendPause(PILI_RTMP *r, int DoPause, int iTime, RTMPError *error) packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_pause); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); *enc++ = AMF_NULL; enc = AMF_EncodeBoolean(enc, pend, DoPause); enc = AMF_EncodeNumber(enc, pend, (double)iTime); - + packet.m_nBodySize = enc - packet.m_body; - + RTMP_Log(RTMP_LOGDEBUG, "%s, %d, pauseTime=%d", __FUNCTION__, DoPause, iTime); return PILI_RTMP_SendPacket(r, &packet, TRUE, error); } @@ -1869,7 +1870,7 @@ int PILI_RTMP_SendSeek(PILI_RTMP *r, int iTime, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x08; /* video channel */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* invoke */ @@ -1877,25 +1878,25 @@ int PILI_RTMP_SendSeek(PILI_RTMP *r, int iTime, RTMPError *error) { packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_seek); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); *enc++ = AMF_NULL; enc = AMF_EncodeNumber(enc, pend, (double)iTime); - + packet.m_nBodySize = enc - packet.m_body; - + r->m_read.flags |= RTMP_READ_SEEKING; r->m_read.nResumeTS = 0; - + return PILI_RTMP_SendPacket(r, &packet, TRUE, error); } int PILI_RTMP_SendServerBW(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); - + packet.m_nChannel = 0x02; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_LARGE; packet.m_packetType = 0x05; /* Server BW */ @@ -1903,9 +1904,9 @@ int PILI_RTMP_SendServerBW(PILI_RTMP *r, RTMPError *error) { packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + packet.m_nBodySize = 4; - + AMF_EncodeInt32(packet.m_body, pend, r->m_nServerBW); return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } @@ -1913,7 +1914,7 @@ int PILI_RTMP_SendServerBW(PILI_RTMP *r, RTMPError *error) { int PILI_RTMP_SendClientBW(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); - + packet.m_nChannel = 0x02; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_LARGE; packet.m_packetType = 0x06; /* Client BW */ @@ -1921,19 +1922,19 @@ int PILI_RTMP_SendClientBW(PILI_RTMP *r, RTMPError *error) { packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + packet.m_nBodySize = 5; - + AMF_EncodeInt32(packet.m_body, pend, r->m_nClientBW); packet.m_body[4] = r->m_nClientBW2; return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } static int - SendBytesReceived(PILI_RTMP *r, RTMPError *error) { +SendBytesReceived(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); - + packet.m_nChannel = 0x02; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x03; /* bytes in */ @@ -1941,12 +1942,12 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + packet.m_nBodySize = 4; - + AMF_EncodeInt32(packet.m_body, pend, r->m_nBytesIn); /* hard coded for now */ r->m_nBytesInSent = r->m_nBytesIn; - + /*RTMP_Log(RTMP_LOGDEBUG, "Send bytes report. 0x%x (%d bytes)", (unsigned int)m_nBytesIn, m_nBytesIn); */ return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } @@ -1954,11 +1955,11 @@ static int SAVC(_checkbw); static int - SendCheckBW(PILI_RTMP *r, RTMPError *error) { +SendCheckBW(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_LARGE; packet.m_packetType = 0x14; /* INVOKE */ @@ -1966,14 +1967,14 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av__checkbw); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); *enc++ = AMF_NULL; - + packet.m_nBodySize = enc - packet.m_body; - + /* triggers _onbwcheck and eventually results in _onbwdone */ return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } @@ -1981,11 +1982,11 @@ static int SAVC(_result); static int - SendCheckBWResult(PILI_RTMP *r, double txn, RTMPError *error) { +SendCheckBWResult(PILI_RTMP *r, double txn, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* INVOKE */ @@ -1993,15 +1994,15 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av__result); enc = AMF_EncodeNumber(enc, pend, txn); *enc++ = AMF_NULL; enc = AMF_EncodeNumber(enc, pend, (double)r->m_nBWCheckCounter++); - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } @@ -2009,11 +2010,11 @@ SAVC(ping); SAVC(pong); static int - SendPong(PILI_RTMP *r, double txn, RTMPError *error) { +SendPong(PILI_RTMP *r, double txn, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; /* INVOKE */ @@ -2021,25 +2022,25 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_pong); enc = AMF_EncodeNumber(enc, pend, txn); *enc++ = AMF_NULL; - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } SAVC(play); static int - SendPlay(PILI_RTMP *r, RTMPError *error) { +SendPlay(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[1024], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x08; /* we make 8 our stream channel */ packet.m_headerType = RTMP_PACKET_SIZE_LARGE; packet.m_packetType = 0x14; /* INVOKE */ @@ -2047,27 +2048,27 @@ static int packet.m_nInfoField2 = r->m_stream_id; /*0x01000000; */ packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_play); enc = AMF_EncodeNumber(enc, pend, ++r->m_numInvokes); *enc++ = AMF_NULL; - + RTMP_Log(RTMP_LOGDEBUG, "%s, seekTime=%d, stopTime=%d, sending play: %s", __FUNCTION__, r->Link.seekTime, r->Link.stopTime, r->Link.playpath.av_val); enc = AMF_EncodeString(enc, pend, &r->Link.playpath); if (!enc) return FALSE; - + /* Optional parameters start and len. - * - * start: -2, -1, 0, positive number - * -2: looks for a live stream, then a recorded stream, - * if not found any open a live stream - * -1: plays a live stream - * >=0: plays a recorded streams from 'start' milliseconds - */ + * + * start: -2, -1, 0, positive number + * -2: looks for a live stream, then a recorded stream, + * if not found any open a live stream + * -1: plays a live stream + * >=0: plays a recorded streams from 'start' milliseconds + */ if (r->Link.lFlags & RTMP_LF_LIVE) enc = AMF_EncodeNumber(enc, pend, -1000.0); else { @@ -2078,21 +2079,21 @@ static int } if (!enc) return FALSE; - + /* len: -1, 0, positive number - * -1: plays live or recorded stream to the end (default) - * 0: plays a frame 'start' ms away from the beginning - * >0: plays a live or recoded stream for 'len' milliseconds - */ + * -1: plays live or recorded stream to the end (default) + * 0: plays a frame 'start' ms away from the beginning + * >0: plays a live or recoded stream for 'len' milliseconds + */ /*enc += EncodeNumber(enc, -1.0); */ /* len */ if (r->Link.stopTime) { enc = AMF_EncodeNumber(enc, pend, r->Link.stopTime - r->Link.seekTime); if (!enc) return FALSE; } - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, TRUE, error); } @@ -2100,11 +2101,11 @@ SAVC(set_playlist); SAVC(0); static int - SendPlaylist(PILI_RTMP *r, RTMPError *error) { +SendPlaylist(PILI_RTMP *r, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[1024], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x08; /* we make 8 our stream channel */ packet.m_headerType = RTMP_PACKET_SIZE_LARGE; packet.m_packetType = 0x14; /* INVOKE */ @@ -2112,7 +2113,7 @@ static int packet.m_nInfoField2 = r->m_stream_id; /*0x01000000; */ packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_set_playlist); enc = AMF_EncodeNumber(enc, pend, 0); @@ -2130,18 +2131,18 @@ static int *enc++ = 0; *enc++ = 0; *enc++ = AMF_OBJECT_END; - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, TRUE, error); } static int - SendSecureTokenResponse(PILI_RTMP *r, AVal *resp, RTMPError *error) { +SendSecureTokenResponse(PILI_RTMP *r, AVal *resp, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[1024], *pend = pbuf + sizeof(pbuf); char *enc; - + packet.m_nChannel = 0x03; /* control channel (invoke) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x14; @@ -2149,7 +2150,7 @@ static int packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + enc = packet.m_body; enc = AMF_EncodeString(enc, pend, &av_secureTokenResponse); enc = AMF_EncodeNumber(enc, pend, 0.0); @@ -2157,36 +2158,36 @@ static int enc = AMF_EncodeString(enc, pend, resp); if (!enc) return FALSE; - + packet.m_nBodySize = enc - packet.m_body; - + return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } /* -from http://jira.red5.org/confluence/display/docs/Ping: - -Ping is the most mysterious message in PILI_RTMP and till now we haven't fully interpreted it yet. In summary, Ping message is used as a special command that are exchanged between client and server. This page aims to document all known Ping messages. Expect the list to grow. - -The type of Ping packet is 0x4 and contains two mandatory parameters and two optional parameters. The first parameter is the type of Ping and in short integer. The second parameter is the target of the ping. As Ping is always sent in Channel 2 (control channel) and the target object in PILI_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 PILI_RTMP header. (The second value could also be used as other purposes, like RTT Ping/Pong. It is used as the timestamp.) The third and fourth parameters are optional and could be looked upon as the parameter of the Ping packet. Below is an unexhausted list of Ping messages. - - * type 0: Clear the stream. No third and fourth parameters. The second parameter could be 0. After the connection is established, a Ping 0,0 will be sent from server to client. The message will also be sent to client on the start of Play and in response of a Seek or Pause/Resume request. This Ping tells client to re-calibrate the clock with the timestamp of the next packet server sends. - * type 1: Tell the stream to clear the playing buffer. - * type 3: Buffer time of the client. The third parameter is the buffer time in millisecond. - * type 4: Reset a stream. Used together with type 0 in the case of VOD. Often sent before type 0. - * type 6: Ping the client from server. The second parameter is the current time. - * type 7: Pong reply from client. The second parameter is the time the server sent with his ping request. - * type 26: SWFVerification request - * type 27: SWFVerification response -*/ + from http://jira.red5.org/confluence/display/docs/Ping: + + Ping is the most mysterious message in PILI_RTMP and till now we haven't fully interpreted it yet. In summary, Ping message is used as a special command that are exchanged between client and server. This page aims to document all known Ping messages. Expect the list to grow. + + The type of Ping packet is 0x4 and contains two mandatory parameters and two optional parameters. The first parameter is the type of Ping and in short integer. The second parameter is the target of the ping. As Ping is always sent in Channel 2 (control channel) and the target object in PILI_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 PILI_RTMP header. (The second value could also be used as other purposes, like RTT Ping/Pong. It is used as the timestamp.) The third and fourth parameters are optional and could be looked upon as the parameter of the Ping packet. Below is an unexhausted list of Ping messages. + + * type 0: Clear the stream. No third and fourth parameters. The second parameter could be 0. After the connection is established, a Ping 0,0 will be sent from server to client. The message will also be sent to client on the start of Play and in response of a Seek or Pause/Resume request. This Ping tells client to re-calibrate the clock with the timestamp of the next packet server sends. + * type 1: Tell the stream to clear the playing buffer. + * type 3: Buffer time of the client. The third parameter is the buffer time in millisecond. + * type 4: Reset a stream. Used together with type 0 in the case of VOD. Often sent before type 0. + * type 6: Ping the client from server. The second parameter is the current time. + * type 7: Pong reply from client. The second parameter is the time the server sent with his ping request. + * type 26: SWFVerification request + * type 27: SWFVerification response + */ int PILI_RTMP_SendCtrl(PILI_RTMP *r, short nType, unsigned int nObject, unsigned int nTime, RTMPError *error) { PILI_RTMPPacket packet; char pbuf[256], *pend = pbuf + sizeof(pbuf); int nSize; char *buf; - + RTMP_Log(RTMP_LOGDEBUG, "sending ctrl. type: 0x%04x", (unsigned short)nType); - + packet.m_nChannel = 0x02; /* control channel (ping) */ packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM; packet.m_packetType = 0x04; /* ctrl */ @@ -2194,7 +2195,7 @@ int PILI_RTMP_SendCtrl(PILI_RTMP *r, short nType, unsigned int nObject, unsigned packet.m_nInfoField2 = 0; packet.m_hasAbsTimestamp = 0; packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE; - + switch (nType) { case 0x03: nSize = 10; @@ -2209,12 +2210,12 @@ int PILI_RTMP_SendCtrl(PILI_RTMP *r, short nType, unsigned int nObject, unsigned nSize = 6; break; } - + packet.m_nBodySize = nSize; - + buf = packet.m_body; buf = AMF_EncodeInt16(buf, pend, nType); - + if (nType == 0x1B) { #ifdef CRYPTO memcpy(buf, r->Link.SWFVerificationResponse, 42); @@ -2226,16 +2227,16 @@ int PILI_RTMP_SendCtrl(PILI_RTMP *r, short nType, unsigned int nObject, unsigned } else { if (nSize > 2) buf = AMF_EncodeInt32(buf, pend, nObject); - + if (nSize > 6) buf = AMF_EncodeInt32(buf, pend, nTime); } - + return PILI_RTMP_SendPacket(r, &packet, FALSE, error); } static void - AV_erase(PILI_RTMP_METHOD *vals, int *num, int i, int freeit) { +AV_erase(PILI_RTMP_METHOD *vals, int *num, int i, int freeit) { if (freeit) free(vals[i].name.av_val); (*num)--; @@ -2252,7 +2253,7 @@ void PILI_RTMP_DropRequest(PILI_RTMP *r, int i, int freeit) { } static void - AV_queue(PILI_RTMP_METHOD **vals, int *num, AVal *av, int txn) { +AV_queue(PILI_RTMP_METHOD **vals, int *num, AVal *av, int txn) { char *tmp; if (!(*num & 0x0f)) *vals = realloc(*vals, (*num + 16) * sizeof(PILI_RTMP_METHOD)); @@ -2265,7 +2266,7 @@ static void } static void - AV_clear(PILI_RTMP_METHOD *vals, int num) { +AV_clear(PILI_RTMP_METHOD *vals, int num) { int i; for (i = 0; i < num; i++) free(vals[i].name.av_val); @@ -2286,21 +2287,21 @@ SAVC(playlist_ready); static const AVal av_NetStream_Failed = AVC("NetStream.Failed"); static const AVal av_NetStream_Play_Failed = AVC("NetStream.Play.Failed"); static const AVal av_NetStream_Play_StreamNotFound = - AVC("NetStream.Play.StreamNotFound"); +AVC("NetStream.Play.StreamNotFound"); static const AVal av_NetConnection_Connect_InvalidApp = - AVC("NetConnection.Connect.InvalidApp"); +AVC("NetConnection.Connect.InvalidApp"); static const AVal av_NetStream_Play_Start = AVC("NetStream.Play.Start"); static const AVal av_NetStream_Play_Complete = AVC("NetStream.Play.Complete"); static const AVal av_NetStream_Play_Stop = AVC("NetStream.Play.Stop"); static const AVal av_NetStream_Seek_Notify = AVC("NetStream.Seek.Notify"); static const AVal av_NetStream_Pause_Notify = AVC("NetStream.Pause.Notify"); static const AVal av_NetStream_Play_UnpublishNotify = - AVC("NetStream.Play.UnpublishNotify"); +AVC("NetStream.Play.UnpublishNotify"); static const AVal av_NetStream_Publish_Start = AVC("NetStream.Publish.Start"); /* Returns 0 for OK/Failed/error, 1 for 'Stop or Complete' */ static int - HandleInvoke(PILI_RTMP *r, const char *body, unsigned int nBodySize) { +HandleInvoke(PILI_RTMP *r, const char *body, unsigned int nBodySize) { AMFObject obj; AVal method; int txn; @@ -2311,24 +2312,24 @@ static int __FUNCTION__); return 0; } - + nRes = AMF_Decode(&obj, body, nBodySize, FALSE); if (nRes < 0) { RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__); return 0; } - + AMF_Dump(&obj); AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &method); txn = (int)AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 1)); RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val); - + RTMPError error = {0}; - + if (AVMATCH(&method, &av__result)) { AVal methodInvoked = {0}; int i; - + for (i = 0; i < r->m_numCalls; i++) { if (r->m_methodCalls[i].num == txn) { methodInvoked = r->m_methodCalls[i].name; @@ -2341,10 +2342,10 @@ static int __FUNCTION__, txn); goto leave; } - + RTMP_Log(RTMP_LOGDEBUG, "%s, received result for method call <%s>", __FUNCTION__, methodInvoked.av_val); - + if (AVMATCH(&methodInvoked, &av_connect)) { if (r->Link.token.av_len) { AMFObjectProperty p; @@ -2361,7 +2362,7 @@ static int PILI_RTMP_SendCtrl(r, 3, 0, 300, &error); } PILI_RTMP_SendCreateStream(r, &error); - + if (!(r->Link.protocol & RTMP_FEATURE_WRITE)) { /* Send the FCSubscribe if live stream or if subscribepath is set */ if (r->Link.subscribepath.av_len) @@ -2371,7 +2372,7 @@ static int } } else if (AVMATCH(&methodInvoked, &av_createStream)) { r->m_stream_id = (int)AMFProp_GetNumber(AMF_GetProp(&obj, NULL, 3)); - + if (r->Link.protocol & RTMP_FEATURE_WRITE) { SendPublish(r, &error); } else { @@ -2413,9 +2414,9 @@ static int RTMPError_Alloc(&error, strlen(msg)); error.code = RTMPErrorServerRequestedClose; strcpy(error.message, msg); - + PILI_RTMP_Close(r, &error); - + RTMPError_Free(&error); } else if (AVMATCH(&method, &av_onStatus)) { AMFObject obj2; @@ -2423,15 +2424,15 @@ static int AMFProp_GetObject(AMF_GetProp(&obj, NULL, 3), &obj2); AMFProp_GetString(AMF_GetProp(&obj2, &av_code, -1), &code); AMFProp_GetString(AMF_GetProp(&obj2, &av_level, -1), &level); - + RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val); if (AVMATCH(&code, &av_NetStream_Failed) || AVMATCH(&code, &av_NetStream_Play_Failed) || AVMATCH(&code, &av_NetStream_Play_StreamNotFound) || AVMATCH(&code, &av_NetConnection_Connect_InvalidApp)) { r->m_stream_id = -1; - + int err_code; char msg[100]; memset(msg, 0, 100); - + if (AVMATCH(&code, &av_NetStream_Failed)) { err_code = RTMPErrorNetStreamFailed; strcpy(msg, "NetStream failed."); @@ -2448,18 +2449,18 @@ static int err_code = RTMPErrorUnknow; strcpy(msg, "Unknow error."); } - + RTMPError_Alloc(&error, strlen(msg)); error.code = err_code; strcpy(error.message, msg); - + PILI_RTMP_Close(r, &error); - + RTMPError_Free(&error); - + RTMP_Log(RTMP_LOGERROR, "Closing connection: %s", code.av_val); } - + else if (AVMATCH(&code, &av_NetStream_Play_Start)) { int i; r->m_bPlaying = TRUE; @@ -2470,7 +2471,7 @@ static int } } } - + else if (AVMATCH(&code, &av_NetStream_Publish_Start)) { int i; r->m_bPlaying = TRUE; @@ -2481,17 +2482,17 @@ static int } } } - + /* Return 1 if this is a Play.Complete or Play.Stop */ else if (AVMATCH(&code, &av_NetStream_Play_Complete) || AVMATCH(&code, &av_NetStream_Play_Stop) || AVMATCH(&code, &av_NetStream_Play_UnpublishNotify)) { PILI_RTMP_Close(r, NULL); ret = 1; } - + else if (AVMATCH(&code, &av_NetStream_Seek_Notify)) { r->m_read.flags &= ~RTMP_READ_SEEKING; } - + else if (AVMATCH(&code, &av_NetStream_Pause_Notify)) { if (r->m_pausing == 1 || r->m_pausing == 2) { PILI_RTMP_SendPause(r, FALSE, r->m_pauseStamp, &error); @@ -2519,12 +2520,12 @@ int PILI_RTMP_FindFirstMatchingProperty(AMFObject *obj, const AVal *name, /* this is a small object search to locate the "duration" property */ for (n = 0; n < obj->o_num; n++) { AMFObjectProperty *prop = AMF_GetProp(obj, NULL, n); - + if (AVMATCH(&prop->p_name, name)) { *p = *prop; return TRUE; } - + if (prop->p_type == AMF_OBJECT) { if (PILI_RTMP_FindFirstMatchingProperty(&prop->p_vu.p_object, name, p)) return TRUE; @@ -2539,13 +2540,13 @@ int PILI_RTMP_FindPrefixProperty(AMFObject *obj, const AVal *name, int n; for (n = 0; n < obj->o_num; n++) { AMFObjectProperty *prop = AMF_GetProp(obj, NULL, n); - + if (prop->p_name.av_len > name->av_len && !memcmp(prop->p_name.av_val, name->av_val, name->av_len)) { *p = *prop; return TRUE; } - + if (prop->p_type == AMF_OBJECT) { if (PILI_RTMP_FindPrefixProperty(&prop->p_vu.p_object, name, p)) return TRUE; @@ -2555,7 +2556,7 @@ int PILI_RTMP_FindPrefixProperty(AMFObject *obj, const AVal *name, } static int - DumpMetaData(AMFObject *obj) { +DumpMetaData(AMFObject *obj) { AMFObjectProperty *prop; int n; for (n = 0; n < obj->o_num; n++) { @@ -2603,23 +2604,23 @@ SAVC(video); SAVC(audio); static int - HandleMetadata(PILI_RTMP *r, char *body, unsigned int len) { +HandleMetadata(PILI_RTMP *r, char *body, unsigned int len) { /* allright we get some info here, so parse it and print it */ /* also keep duration or filesize to make a nice progress bar */ - + AMFObject obj; AVal metastring; int ret = FALSE; - + int nRes = AMF_Decode(&obj, body, len, FALSE); if (nRes < 0) { RTMP_Log(RTMP_LOGERROR, "%s, error decoding meta data packet", __FUNCTION__); return FALSE; } - + AMF_Dump(&obj); AMFProp_GetString(AMF_GetProp(&obj, NULL, 0), &metastring); - + if (AVMATCH(&metastring, &av_onMetaData)) { AMFObjectProperty prop; /* Show metadata */ @@ -2641,7 +2642,7 @@ static int } static void - HandleChangeChunkSize(PILI_RTMP *r, const PILI_RTMPPacket *packet) { +HandleChangeChunkSize(PILI_RTMP *r, const PILI_RTMPPacket *packet) { if (packet->m_nBodySize >= 4) { r->m_inChunkSize = AMF_DecodeInt32(packet->m_body); RTMP_Log(RTMP_LOGDEBUG, "%s, received: chunk size change to %d", __FUNCTION__, @@ -2650,15 +2651,15 @@ static void } static void - HandleAudio(PILI_RTMP *r, const PILI_RTMPPacket *packet) { +HandleAudio(PILI_RTMP *r, const PILI_RTMPPacket *packet) { } static void - HandleVideo(PILI_RTMP *r, const PILI_RTMPPacket *packet) { +HandleVideo(PILI_RTMP *r, const PILI_RTMPPacket *packet) { } static void - HandleCtrl(PILI_RTMP *r, const PILI_RTMPPacket *packet) { +HandleCtrl(PILI_RTMP *r, const PILI_RTMPPacket *packet) { short nType = -1; unsigned int tmp; if (packet->m_body && packet->m_nBodySize >= 2) @@ -2666,73 +2667,73 @@ static void RTMP_Log(RTMP_LOGDEBUG, "%s, received ctrl. type: %d, len: %d", __FUNCTION__, nType, packet->m_nBodySize); /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ - + if (packet->m_nBodySize >= 6) { switch (nType) { case 0: tmp = AMF_DecodeInt32(packet->m_body + 2); RTMP_Log(RTMP_LOGDEBUG, "%s, Stream Begin %d", __FUNCTION__, tmp); break; - + case 1: tmp = AMF_DecodeInt32(packet->m_body + 2); RTMP_Log(RTMP_LOGDEBUG, "%s, Stream EOF %d", __FUNCTION__, tmp); if (r->m_pausing == 1) r->m_pausing = 2; break; - + case 2: tmp = AMF_DecodeInt32(packet->m_body + 2); RTMP_Log(RTMP_LOGDEBUG, "%s, Stream Dry %d", __FUNCTION__, tmp); break; - + case 4: tmp = AMF_DecodeInt32(packet->m_body + 2); RTMP_Log(RTMP_LOGDEBUG, "%s, Stream IsRecorded %d", __FUNCTION__, tmp); break; - + case 6: /* server ping. reply with pong. */ tmp = AMF_DecodeInt32(packet->m_body + 2); RTMP_Log(RTMP_LOGDEBUG, "%s, Ping %d", __FUNCTION__, tmp); PILI_RTMP_SendCtrl(r, 0x07, tmp, 0, NULL); break; - - /* FMS 3.5 servers send the following two controls to let the client - * know when the server has sent a complete buffer. I.e., when the - * server has sent an amount of data equal to m_nBufferMS in duration. - * The server meters its output so that data arrives at the client - * in realtime and no faster. - * - * The rtmpdump program tries to set m_nBufferMS as large as - * possible, to force the server to send data as fast as possible. - * In practice, the server appears to cap this at about 1 hour's - * worth of data. After the server has sent a complete buffer, and - * sends this BufferEmpty message, it will wait until the play - * duration of that buffer has passed before sending a new buffer. - * The BufferReady message will be sent when the new buffer starts. - * (There is no BufferReady message for the very first buffer; - * presumably the Stream Begin message is sufficient for that - * purpose.) - * - * If the network speed is much faster than the data bitrate, then - * there may be long delays between the end of one buffer and the - * start of the next. - * - * Since usually the network allows data to be sent at - * faster than realtime, and rtmpdump wants to download the data - * as fast as possible, we use this RTMP_LF_BUFX hack: when we - * get the BufferEmpty message, we send a Pause followed by an - * Unpause. This causes the server to send the next buffer immediately - * instead of waiting for the full duration to elapse. (That's - * also the purpose of the ToggleStream function, which rtmpdump - * calls if we get a read timeout.) - * - * Media player apps don't need this hack since they are just - * going to play the data in realtime anyway. It also doesn't work - * for live streams since they obviously can only be sent in - * realtime. And it's all moot if the network speed is actually - * slower than the media bitrate. - */ + + /* FMS 3.5 servers send the following two controls to let the client + * know when the server has sent a complete buffer. I.e., when the + * server has sent an amount of data equal to m_nBufferMS in duration. + * The server meters its output so that data arrives at the client + * in realtime and no faster. + * + * The rtmpdump program tries to set m_nBufferMS as large as + * possible, to force the server to send data as fast as possible. + * In practice, the server appears to cap this at about 1 hour's + * worth of data. After the server has sent a complete buffer, and + * sends this BufferEmpty message, it will wait until the play + * duration of that buffer has passed before sending a new buffer. + * The BufferReady message will be sent when the new buffer starts. + * (There is no BufferReady message for the very first buffer; + * presumably the Stream Begin message is sufficient for that + * purpose.) + * + * If the network speed is much faster than the data bitrate, then + * there may be long delays between the end of one buffer and the + * start of the next. + * + * Since usually the network allows data to be sent at + * faster than realtime, and rtmpdump wants to download the data + * as fast as possible, we use this RTMP_LF_BUFX hack: when we + * get the BufferEmpty message, we send a Pause followed by an + * Unpause. This causes the server to send the next buffer immediately + * instead of waiting for the full duration to elapse. (That's + * also the purpose of the ToggleStream function, which rtmpdump + * calls if we get a read timeout.) + * + * Media player apps don't need this hack since they are just + * going to play the data in realtime anyway. It also doesn't work + * for live streams since they obviously can only be sent in + * realtime. And it's all moot if the network speed is actually + * slower than the media bitrate. + */ case 31: tmp = AMF_DecodeInt32(packet->m_body + 2); RTMP_Log(RTMP_LOGDEBUG, "%s, Stream BufferEmpty %d", __FUNCTION__, tmp); @@ -2747,27 +2748,27 @@ static void r->m_pausing = 3; } break; - + case 32: tmp = AMF_DecodeInt32(packet->m_body + 2); RTMP_Log(RTMP_LOGDEBUG, "%s, Stream BufferReady %d", __FUNCTION__, tmp); break; - + default: tmp = AMF_DecodeInt32(packet->m_body + 2); RTMP_Log(RTMP_LOGDEBUG, "%s, Stream xx %d", __FUNCTION__, tmp); break; } } - + if (nType == 0x1A) { RTMP_Log(RTMP_LOGDEBUG, "%s, SWFVerification ping received: ", __FUNCTION__); #ifdef CRYPTO /*RTMP_LogHex(packet.m_body, packet.m_nBodySize); */ - + /* respond with HMAC SHA256 of decompressed SWF, key is the 30byte player key, also the last 30 bytes of the server handshake are applied */ if (r->Link.SWFSize) { - PILI_RTMP_SendCtrl(r, 0x1B, 0, 0); + PILI_RTMP_SendCtrl(r, 0x1B, 0, 0, NULL); } else { RTMP_Log(RTMP_LOGERROR, "%s: Ignoring SWFVerification request, use --swfVfy!", @@ -2782,13 +2783,13 @@ static void } static void - HandleServerBW(PILI_RTMP *r, const PILI_RTMPPacket *packet) { +HandleServerBW(PILI_RTMP *r, const PILI_RTMPPacket *packet) { r->m_nServerBW = AMF_DecodeInt32(packet->m_body); RTMP_Log(RTMP_LOGDEBUG, "%s: server BW = %d", __FUNCTION__, r->m_nServerBW); } static void - HandleClientBW(PILI_RTMP *r, const PILI_RTMPPacket *packet) { +HandleClientBW(PILI_RTMP *r, const PILI_RTMPPacket *packet) { r->m_nClientBW = AMF_DecodeInt32(packet->m_body); if (packet->m_nBodySize > 4) r->m_nClientBW2 = packet->m_body[4]; @@ -2799,16 +2800,16 @@ static void } static int - DecodeInt32LE(const char *data) { +DecodeInt32LE(const char *data) { unsigned char *c = (unsigned char *)data; unsigned int val; - + val = (c[3] << 24) | (c[2] << 16) | (c[1] << 8) | c[0]; return val; } static int - EncodeInt32LE(char *output, int nVal) { +EncodeInt32LE(char *output, int nVal) { output[0] = nVal; nVal >>= 8; output[1] = nVal; @@ -2824,14 +2825,14 @@ int PILI_RTMP_ReadPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { char *header = (char *)hbuf; int nSize, hSize, nToRead, nChunk; int didAlloc = FALSE; - + RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d", __FUNCTION__, r->m_sb.sb_socket); - + if (ReadN(r, (char *)hbuf, 1) == 0) { RTMP_Log(RTMP_LOGERROR, "%s, failed to read PILI_RTMP packet header", __FUNCTION__); return FALSE; } - + packet->m_headerType = (hbuf[0] & 0xc0) >> 6; packet->m_nChannel = (hbuf[0] & 0x3f); header++; @@ -2856,41 +2857,41 @@ int PILI_RTMP_ReadPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { RTMP_Log(RTMP_LOGDEBUG, "%s, m_nChannel: %0x", __FUNCTION__, packet->m_nChannel); header += 2; } - + nSize = packetSize[packet->m_headerType]; - + if (nSize == RTMP_LARGE_HEADER_SIZE) /* if we get a full header the timestamp is absolute */ packet->m_hasAbsTimestamp = TRUE; - + else if (nSize < RTMP_LARGE_HEADER_SIZE) { /* using values from the last message of this channel */ if (r->m_vecChannelsIn[packet->m_nChannel]) memcpy(packet, r->m_vecChannelsIn[packet->m_nChannel], sizeof(PILI_RTMPPacket)); } - + nSize--; - + if (nSize > 0 && ReadN(r, header, nSize) != nSize) { RTMP_Log(RTMP_LOGERROR, "%s, failed to read PILI_RTMP packet header. type: %x", __FUNCTION__, (unsigned int)hbuf[0]); return FALSE; } - + hSize = nSize + (header - (char *)hbuf); - + if (nSize >= 3) { packet->m_nTimeStamp = AMF_DecodeInt24(header); - + /*RTMP_Log(RTMP_LOGDEBUG, "%s, reading PILI_RTMP packet chunk on channel %x, headersz %i, timestamp %i, abs timestamp %i", __FUNCTION__, packet.m_nChannel, nSize, packet.m_nTimeStamp, packet.m_hasAbsTimestamp); */ - + if (nSize >= 6) { packet->m_nBodySize = AMF_DecodeInt24(header + 3); packet->m_nBytesRead = 0; PILI_RTMPPacket_Free(packet); - + if (nSize > 6) { packet->m_packetType = header[6]; - + if (nSize == 11) packet->m_nInfoField2 = DecodeInt32LE(header + 7); } @@ -2905,9 +2906,9 @@ int PILI_RTMP_ReadPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { hSize += 4; } } - + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)hbuf, hSize); - + if (packet->m_nBodySize > 0 && packet->m_body == NULL) { if (!PILI_RTMPPacket_Alloc(packet, packet->m_nBodySize)) { RTMP_Log(RTMP_LOGDEBUG, "%s, failed to allocate packet", __FUNCTION__); @@ -2916,12 +2917,12 @@ int PILI_RTMP_ReadPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { didAlloc = TRUE; packet->m_headerType = (hbuf[0] & 0xc0) >> 6; } - + nToRead = packet->m_nBodySize - packet->m_nBytesRead; nChunk = r->m_inChunkSize; if (nToRead < nChunk) nChunk = nToRead; - + /* Does the caller want the raw chunk? */ if (packet->m_chunk) { packet->m_chunk->c_headerSize = hSize; @@ -2929,29 +2930,29 @@ int PILI_RTMP_ReadPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { packet->m_chunk->c_chunk = packet->m_body + packet->m_nBytesRead; packet->m_chunk->c_chunkSize = nChunk; } - + if (ReadN(r, packet->m_body + packet->m_nBytesRead, nChunk) != nChunk) { RTMP_Log(RTMP_LOGERROR, "%s, failed to read PILI_RTMP packet body. len: %lu", __FUNCTION__, packet->m_nBodySize); return FALSE; } - + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)packet->m_body + packet->m_nBytesRead, nChunk); - + packet->m_nBytesRead += nChunk; - + /* keep the packet as ref for other packets on this channel */ if (!r->m_vecChannelsIn[packet->m_nChannel]) r->m_vecChannelsIn[packet->m_nChannel] = malloc(sizeof(PILI_RTMPPacket)); memcpy(r->m_vecChannelsIn[packet->m_nChannel], packet, sizeof(PILI_RTMPPacket)); - + if (RTMPPacket_IsReady(packet)) { /* make packet's timestamp absolute */ if (!packet->m_hasAbsTimestamp) packet->m_nTimeStamp += r->m_channelTimestamp[packet->m_nChannel]; /* timestamps seem to be always relative!! */ - + r->m_channelTimestamp[packet->m_nChannel] = packet->m_nTimeStamp; - + /* reset the data from the stored packet. we keep the header since we may use it later if a new packet for this channel */ /* arrives and requests to re-use some info (small packet header) */ r->m_vecChannelsIn[packet->m_nChannel]->m_body = NULL; @@ -2960,27 +2961,27 @@ int PILI_RTMP_ReadPacket(PILI_RTMP *r, PILI_RTMPPacket *packet) { } else { packet->m_body = NULL; /* so it won't be erased on free */ } - + return TRUE; } #ifndef CRYPTO static int - HandShake(PILI_RTMP *r, int FP9HandShake, RTMPError *error) { +HandShake(PILI_RTMP *r, int FP9HandShake, RTMPError *error) { int i; uint32_t uptime, suptime; int bMatch; char type; char clientbuf[RTMP_SIG_SIZE + 1], *clientsig = clientbuf + 1; char serversig[RTMP_SIG_SIZE]; - + clientbuf[0] = 0x03; /* not encrypted */ - + uptime = htonl(PILI_RTMP_GetTime()); memcpy(clientsig, &uptime, 4); - + memset(&clientsig[4], 0, 4); - + #ifdef _DEBUG for (i = 8; i < RTMP_SIG_SIZE; i++) clientsig[i] = 0xff; @@ -2988,38 +2989,38 @@ static int for (i = 8; i < RTMP_SIG_SIZE; i++) clientsig[i] = (char)(rand() % 256); #endif - + if (!WriteN(r, clientbuf, RTMP_SIG_SIZE + 1, error)) return FALSE; - + if (ReadN(r, &type, 1) != 1) /* 0x03 or 0x06 */ return FALSE; - + RTMP_Log(RTMP_LOGDEBUG, "%s: Type Answer : %02X", __FUNCTION__, type); - + if (type != clientbuf[0]) RTMP_Log(RTMP_LOGWARNING, "%s: Type mismatch: client sent %d, server answered %d", __FUNCTION__, clientbuf[0], type); - + if (ReadN(r, serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) return FALSE; - + /* decode server response */ - + memcpy(&suptime, serversig, 4); suptime = ntohl(suptime); - + RTMP_Log(RTMP_LOGDEBUG, "%s: Server Uptime : %d", __FUNCTION__, suptime); RTMP_Log(RTMP_LOGDEBUG, "%s: FMS Version : %d.%d.%d.%d", __FUNCTION__, serversig[4], serversig[5], serversig[6], serversig[7]); - + /* 2nd part of handshake */ if (!WriteN(r, serversig, RTMP_SIG_SIZE, error)) return FALSE; - + if (ReadN(r, serversig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) return FALSE; - + bMatch = (memcmp(serversig, clientsig, RTMP_SIG_SIZE) == 0); if (!bMatch) { RTMP_Log(RTMP_LOGWARNING, "%s, client signature does not match!", __FUNCTION__); @@ -3028,27 +3029,27 @@ static int } static int - SHandShake(PILI_RTMP *r, RTMPError *error) { +SHandShake(PILI_RTMP *r, RTMPError *error) { int i; char serverbuf[RTMP_SIG_SIZE + 1], *serversig = serverbuf + 1; char clientsig[RTMP_SIG_SIZE]; uint32_t uptime; int bMatch; - + if (ReadN(r, serverbuf, 1) != 1) /* 0x03 or 0x06 */ return FALSE; - + RTMP_Log(RTMP_LOGDEBUG, "%s: Type Request : %02X", __FUNCTION__, serverbuf[0]); - + if (serverbuf[0] != 3) { RTMP_Log(RTMP_LOGERROR, "%s: Type unknown: client sent %02X", __FUNCTION__, serverbuf[0]); return FALSE; } - + uptime = htonl(PILI_RTMP_GetTime()); memcpy(serversig, &uptime, 4); - + memset(&serversig[4], 0, 4); #ifdef _DEBUG for (i = 8; i < RTMP_SIG_SIZE; i++) @@ -3057,29 +3058,29 @@ static int for (i = 8; i < RTMP_SIG_SIZE; i++) serversig[i] = (char)(rand() % 256); #endif - + if (!WriteN(r, serverbuf, RTMP_SIG_SIZE + 1, error)) return FALSE; - + if (ReadN(r, clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) return FALSE; - + /* decode client response */ - + memcpy(&uptime, clientsig, 4); uptime = ntohl(uptime); - + RTMP_Log(RTMP_LOGDEBUG, "%s: Client Uptime : %d", __FUNCTION__, uptime); RTMP_Log(RTMP_LOGDEBUG, "%s: Player Version: %d.%d.%d.%d", __FUNCTION__, clientsig[4], clientsig[5], clientsig[6], clientsig[7]); - + /* 2nd part of handshake */ if (!WriteN(r, clientsig, RTMP_SIG_SIZE, error)) return FALSE; - + if (ReadN(r, clientsig, RTMP_SIG_SIZE) != RTMP_SIG_SIZE) return FALSE; - + bMatch = (memcmp(serversig, clientsig, RTMP_SIG_SIZE) == 0); if (!bMatch) { RTMP_Log(RTMP_LOGWARNING, "%s, client signature does not match!", __FUNCTION__); @@ -3091,7 +3092,7 @@ static int int PILI_RTMP_SendChunk(PILI_RTMP *r, PILI_RTMPChunk *chunk, RTMPError *error) { int wrote; char hbuf[RTMP_MAX_HEADER_SIZE]; - + RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d, size=%d", __FUNCTION__, r->m_sb.sb_socket, chunk->c_chunkSize); RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)chunk->c_header, chunk->c_headerSize); @@ -3118,17 +3119,17 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE char *buffer, *tbuf = NULL, *toff = NULL; int nChunkSize; int tlen; - + if (prevPacket && packet->m_headerType != RTMP_PACKET_SIZE_LARGE) { /* compress a bit by using the prev packet's attributes */ if (prevPacket->m_nBodySize == packet->m_nBodySize && prevPacket->m_packetType == packet->m_packetType && packet->m_headerType == RTMP_PACKET_SIZE_MEDIUM) packet->m_headerType = RTMP_PACKET_SIZE_SMALL; - + if (prevPacket->m_nTimeStamp == packet->m_nTimeStamp && packet->m_headerType == RTMP_PACKET_SIZE_SMALL) packet->m_headerType = RTMP_PACKET_SIZE_MINIMUM; last = prevPacket->m_nTimeStamp; } - + if (packet->m_headerType > 3) /* sanity */ { if (error) { @@ -3137,18 +3138,18 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE error->code = RTMPErrorSanityFailed; strcpy(error->message, msg); } - + RTMP_Log(RTMP_LOGERROR, "sanity failed!! trying to send header of type: 0x%02x.", (unsigned char)packet->m_headerType); - + return FALSE; } - + nSize = packetSize[packet->m_headerType]; hSize = nSize; cSize = 0; t = packet->m_nTimeStamp - last; - + if (packet->m_body) { header = packet->m_body - nSize; hend = packet->m_body; @@ -3156,7 +3157,7 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE header = hbuf + 6; hend = hbuf + sizeof(hbuf); } - + if (packet->m_nChannel > 319) cSize = 2; else if (packet->m_nChannel > 63) @@ -3165,12 +3166,12 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE header -= cSize; hSize += cSize; } - + if (nSize > 1 && t >= 0xffffff) { header -= 4; hSize += 4; } - + hptr = header; c = packet->m_headerType << 6; switch (cSize) { @@ -3190,26 +3191,26 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE if (cSize == 2) *hptr++ = tmp >> 8; } - + if (nSize > 1) { hptr = AMF_EncodeInt24(hptr, hend, t > 0xffffff ? 0xffffff : t); } - + if (nSize > 4) { hptr = AMF_EncodeInt24(hptr, hend, packet->m_nBodySize); *hptr++ = packet->m_packetType; } - + if (nSize > 8) hptr += EncodeInt32LE(hptr, packet->m_nInfoField2); - + if (nSize > 1 && t >= 0xffffff) hptr = AMF_EncodeInt32(hptr, hend, t); - + nSize = packet->m_nBodySize; buffer = packet->m_body; nChunkSize = r->m_outChunkSize; - + RTMP_Log(RTMP_LOGDEBUG2, "%s: fd=%d, size=%d", __FUNCTION__, r->m_sb.sb_socket, nSize); /* send all chunks in one HTTP request */ @@ -3225,10 +3226,10 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE } while (nSize + hSize) { int wrote; - + if (nSize < nChunkSize) nChunkSize = nSize; - + RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)header, hSize); RTMP_LogHexString(RTMP_LOGDEBUG2, (uint8_t *)buffer, nChunkSize); if (tbuf) { @@ -3242,7 +3243,7 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE nSize -= nChunkSize; buffer += nChunkSize; hSize = 0; - + if (nSize > 0) { header = buffer - 1; hSize = 1; @@ -3266,7 +3267,7 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE if (!wrote) return FALSE; } - + /* we invoked a remote method */ if (packet->m_packetType == 0x14) { AVal method; @@ -3282,7 +3283,7 @@ int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, RTMPE AV_queue(&r->m_methodCalls, &r->m_numCalls, &method, txn); } } - + if (!r->m_vecChannelsOut[packet->m_nChannel]) r->m_vecChannelsOut[packet->m_nChannel] = malloc(sizeof(PILI_RTMPPacket)); memcpy(r->m_vecChannelsOut[packet->m_nChannel], packet, sizeof(PILI_RTMPPacket)); @@ -3314,18 +3315,18 @@ void PILI_RTMP_Close(PILI_RTMP *r, RTMPError *error) { r->m_clientID.av_len = 0; } PILI_RTMPSockBuf_Close(&r->m_sb); - + if (error && r->m_errorCallback) { r->m_errorCallback(error, r->m_userData); } } - + r->m_stream_id = -1; r->m_sb.sb_socket = -1; r->m_nBWCheckCounter = 0; r->m_nBytesIn = 0; r->m_nBytesInSent = 0; - + if (r->m_read.flags & RTMP_READ_HEADER) { free(r->m_read.buf); r->m_read.buf = NULL; @@ -3336,10 +3337,10 @@ void PILI_RTMP_Close(PILI_RTMP *r, RTMPError *error) { r->m_read.nResumeTS = 0; r->m_read.nIgnoredFrameCounter = 0; r->m_read.nIgnoredFlvFrameCounter = 0; - + r->m_write.m_nBytesRead = 0; PILI_RTMPPacket_Free(&r->m_write); - + for (i = 0; i < RTMP_CHANNELS; i++) { if (r->m_vecChannelsIn[i]) { PILI_RTMPPacket_Free(r->m_vecChannelsIn[i]); @@ -3355,23 +3356,23 @@ void PILI_RTMP_Close(PILI_RTMP *r, RTMPError *error) { r->m_methodCalls = NULL; r->m_numCalls = 0; r->m_numInvokes = 0; - + r->m_bPlaying = FALSE; r->m_sb.sb_size = 0; - + r->m_msgCounter = 0; r->m_resplen = 0; r->m_unackd = 0; - + free(r->Link.playpath0.av_val); r->Link.playpath0.av_val = NULL; - + if (r->Link.lFlags & RTMP_LF_FTCU) { free(r->Link.tcUrl.av_val); r->Link.tcUrl.av_val = NULL; r->Link.lFlags ^= RTMP_LF_FTCU; } - + #ifdef CRYPTO if (r->Link.dh) { MDH_free(r->Link.dh); @@ -3390,10 +3391,10 @@ void PILI_RTMP_Close(PILI_RTMP *r, RTMPError *error) { int PILI_RTMPSockBuf_Fill(PILI_RTMPSockBuf *sb) { int nBytes; - + if (!sb->sb_size) sb->sb_start = sb->sb_buf; - + while (1) { nBytes = sizeof(sb->sb_buf) - sb->sb_size - (sb->sb_start - sb->sb_buf); #if defined(CRYPTO) && !defined(NO_SSL) @@ -3412,7 +3413,7 @@ int PILI_RTMPSockBuf_Fill(PILI_RTMPSockBuf *sb) { __FUNCTION__, nBytes, sockerr, strerror(sockerr)); if (sockerr == EINTR && !PILI_RTMP_ctrlC) continue; - + if (sockerr == EWOULDBLOCK || sockerr == EAGAIN) { sb->sb_timedout = TRUE; nBytes = 0; @@ -3420,17 +3421,17 @@ int PILI_RTMPSockBuf_Fill(PILI_RTMPSockBuf *sb) { } break; } - + return nBytes; } int PILI_RTMPSockBuf_Send(PILI_RTMPSockBuf *sb, const char *buf, int len) { int rc; - + #ifdef _DEBUG fwrite(buf, 1, len, netstackdump); #endif - + #if defined(CRYPTO) && !defined(NO_SSL) if (sb->sb_ssl) { rc = TLS_write(sb->sb_ssl, buf, len); @@ -3456,13 +3457,13 @@ int PILI_RTMPSockBuf_Close(PILI_RTMPSockBuf *sb) { #define HEX2BIN(a) (((a)&0x40) ? ((a)&0xf) + 9 : ((a)&0xf)) static void - DecodeTEA(AVal *key, AVal *text) { +DecodeTEA(AVal *key, AVal *text) { uint32_t *v, k[4] = {0}, u; uint32_t z, y, sum = 0, e, DELTA = 0x9e3779b9; int32_t p, q; int i, n; unsigned char *ptr, *out; - + /* prep key: pack 1st 16 chars into 4 LittleEndian ints */ ptr = (unsigned char *)key->av_val; u = 0; @@ -3482,7 +3483,7 @@ static void /* any trailing chars */ if (u) *v = u; - + /* prep text: hex2bin, multiples of 4 */ n = (text->av_len + 7) / 8; out = malloc(n * 8); @@ -3497,8 +3498,8 @@ static void ptr += 8; } v = (uint32_t *)out; - -/* http://www.movable-type.co.uk/scripts/tea-block.html */ + + /* http://www.movable-type.co.uk/scripts/tea-block.html */ #define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (k[(p & 3) ^ e] ^ z)); z = v[n - 1]; y = v[0]; @@ -3512,23 +3513,23 @@ static void y = v[0] -= MX; sum -= DELTA; } - + text->av_len /= 2; memcpy(text->av_val, out, text->av_len); free(out); } static int - HTTP_Post(PILI_RTMP *r, RTMPTCmd cmd, const char *buf, int len) { +HTTP_Post(PILI_RTMP *r, RTMPTCmd cmd, const char *buf, int len) { char hbuf[512]; int hlen = snprintf(hbuf, sizeof(hbuf), "POST /%s%s/%d HTTP/1.1\r\n" - "Host: %.*s:%d\r\n" - "Accept: */*\r\n" - "User-Agent: Shockwave Flash\n" - "Connection: Keep-Alive\n" - "Cache-Control: no-cache\r\n" - "Content-type: application/x-fcs\r\n" - "Content-length: %d\r\n\r\n", + "Host: %.*s:%d\r\n" + "Accept: */*\r\n" + "User-Agent: Shockwave Flash\n" + "Connection: Keep-Alive\n" + "Cache-Control: no-cache\r\n" + "Content-type: application/x-fcs\r\n" + "Content-length: %d\r\n\r\n", RTMPT_cmds[cmd], r->m_clientID.av_val ? r->m_clientID.av_val : "", r->m_msgCounter, r->Link.hostname.av_len, r->Link.hostname.av_val, @@ -3541,10 +3542,10 @@ static int } static int - HTTP_read(PILI_RTMP *r, int fill) { +HTTP_read(PILI_RTMP *r, int fill) { char *ptr; int hlen; - + if (fill) PILI_RTMPSockBuf_Fill(&r->m_sb); if (r->m_sb.sb_size < 144) @@ -3562,7 +3563,7 @@ static int r->m_sb.sb_size -= ptr - r->m_sb.sb_start; r->m_sb.sb_start = ptr; r->m_unackd--; - + if (!r->m_clientID.av_val) { r->m_clientID.av_len = hlen; r->m_clientID.av_val = malloc(hlen + 1); @@ -3588,7 +3589,7 @@ static int * packets, 0 if ignorable error, >0 if there is a media packet */ static int - Read_1_Packet(PILI_RTMP *r, char *buf, unsigned int buflen) { +Read_1_Packet(PILI_RTMP *r, char *buf, unsigned int buflen) { uint32_t prevTagSize = 0; int rtnGetNextMediaPacket = 0, ret = RTMP_READ_EOF; PILI_RTMPPacket packet = {0}; @@ -3597,15 +3598,15 @@ static int char *ptr, *pend; uint32_t nTimeStamp = 0; unsigned int len; - + rtnGetNextMediaPacket = PILI_RTMP_GetNextMediaPacket(r, &packet); while (rtnGetNextMediaPacket) { char *packetBody = packet.m_body; unsigned int nPacketLen = packet.m_nBodySize; - + /* Return -3 if this was completed nicely with invoke message - * Play.Stop or Play.Complete - */ + * Play.Stop or Play.Complete + */ if (rtnGetNextMediaPacket == 2) { RTMP_Log(RTMP_LOGDEBUG, "Got Play.Complete or Play.Stop from server. " @@ -3613,10 +3614,10 @@ static int ret = RTMP_READ_COMPLETE; break; } - + r->m_read.dataType |= (((packet.m_packetType == 0x08) << 2) | (packet.m_packetType == 0x09)); - + if (packet.m_packetType == 0x09 && nPacketLen <= 5) { RTMP_Log(RTMP_LOGDEBUG, "ignoring too small video packet: size: %d", nPacketLen); @@ -3629,7 +3630,7 @@ static int ret = RTMP_READ_IGNORE; break; } - + if (r->m_read.flags & RTMP_READ_SEEKING) { ret = RTMP_READ_IGNORE; break; @@ -3641,19 +3642,19 @@ static int if (packet.m_packetType == 0x09) RTMP_Log(RTMP_LOGDEBUG, "frametype: %02X", (*packetBody & 0xf0)); #endif - + if (r->m_read.flags & RTMP_READ_RESUME) { /* check the header if we get one */ if (packet.m_nTimeStamp == 0) { if (r->m_read.nMetaHeaderSize > 0 && packet.m_packetType == 0x12) { AMFObject metaObj; int nRes = - AMF_Decode(&metaObj, packetBody, nPacketLen, FALSE); + AMF_Decode(&metaObj, packetBody, nPacketLen, FALSE); if (nRes >= 0) { AVal metastring; AMFProp_GetString(AMF_GetProp(&metaObj, NULL, 0), &metastring); - + if (AVMATCH(&metastring, &av_onMetaData)) { /* compare */ if ((r->m_read.nMetaHeaderSize != nPacketLen) || @@ -3667,57 +3668,57 @@ static int break; } } - + /* check first keyframe to make sure we got the right position - * in the stream! (the first non ignored frame) - */ + * in the stream! (the first non ignored frame) + */ if (r->m_read.nInitialFrameSize > 0) { /* video or audio data */ if (packet.m_packetType == r->m_read.initialFrameType && r->m_read.nInitialFrameSize == nPacketLen) { /* we don't compare the sizes since the packet can - * contain several FLV packets, just make sure the - * first frame is our keyframe (which we are going - * to rewrite) - */ + * contain several FLV packets, just make sure the + * first frame is our keyframe (which we are going + * to rewrite) + */ if (memcmp(r->m_read.initialFrame, packetBody, r->m_read.nInitialFrameSize) == 0) { RTMP_Log(RTMP_LOGDEBUG, "Checked keyframe successfully!"); r->m_read.flags |= RTMP_READ_GOTKF; /* ignore it! (what about audio data after it? it is - * handled by ignoring all 0ms frames, see below) - */ + * handled by ignoring all 0ms frames, see below) + */ ret = RTMP_READ_IGNORE; break; } } - + /* hande FLV streams, even though the server resends the - * keyframe as an extra video packet it is also included - * in the first FLV stream chunk and we have to compare - * it and filter it out !! - */ + * keyframe as an extra video packet it is also included + * in the first FLV stream chunk and we have to compare + * it and filter it out !! + */ if (packet.m_packetType == 0x16) { /* basically we have to find the keyframe with the - * correct TS being nResumeTS - */ + * correct TS being nResumeTS + */ unsigned int pos = 0; uint32_t ts = 0; - + while (pos + 11 < nPacketLen) { /* size without header (11) and prevTagSize (4) */ uint32_t dataSize = - AMF_DecodeInt24(packetBody + pos + 1); + AMF_DecodeInt24(packetBody + pos + 1); ts = AMF_DecodeInt24(packetBody + pos + 4); ts |= (packetBody[pos + 7] << 24); - + #ifdef _DEBUG RTMP_Log(RTMP_LOGDEBUG, "keyframe search: FLV Packet: type %02X, dataSize: %d, timeStamp: %d ms", packetBody[pos], dataSize, ts); #endif /* ok, is it a keyframe?: - * well doesn't work for audio! - */ + * well doesn't work for audio! + */ if (packetBody[pos /*6928, test 0 */] == r->m_read.initialFrameType /* && (packetBody[11]&0xf0) == 0x10 */) { @@ -3731,10 +3732,10 @@ static int break; } r->m_read.flags |= RTMP_READ_GOTFLVK; - + /* skip this packet? - * check whether skippable: - */ + * check whether skippable: + */ if (pos + 11 + dataSize + 4 > nPacketLen) { RTMP_Log(RTMP_LOGWARNING, "Non skipable packet since it doesn't end with chunk, stream corrupt!"); @@ -3743,13 +3744,13 @@ static int } packetBody += (pos + 11 + dataSize + 4); nPacketLen -= (pos + 11 + dataSize + 4); - + goto stopKeyframeSearch; - + } else if (r->m_read.nResumeTS < ts) { /* the timestamp ts will only increase with - * further packets, wait for seek - */ + * further packets, wait for seek + */ goto stopKeyframeSearch; } } @@ -3771,25 +3772,25 @@ static int } } } - + if (packet.m_nTimeStamp > 0 && (r->m_read.flags & (RTMP_READ_GOTKF | RTMP_READ_GOTFLVK))) { /* another problem is that the server can actually change from - * 09/08 video/audio packets to an FLV stream or vice versa and - * our keyframe check will prevent us from going along with the - * new stream if we resumed. - * - * in this case set the 'found keyframe' variables to true. - * We assume that if we found one keyframe somewhere and were - * already beyond TS > 0 we have written data to the output - * which means we can accept all forthcoming data including the - * change between 08/09 <-> FLV packets - */ + * 09/08 video/audio packets to an FLV stream or vice versa and + * our keyframe check will prevent us from going along with the + * new stream if we resumed. + * + * in this case set the 'found keyframe' variables to true. + * We assume that if we found one keyframe somewhere and were + * already beyond TS > 0 we have written data to the output + * which means we can accept all forthcoming data including the + * change between 08/09 <-> FLV packets + */ r->m_read.flags |= (RTMP_READ_GOTKF | RTMP_READ_GOTFLVK); } - + /* skip till we find our keyframe - * (seeking might put us somewhere before it) - */ + * (seeking might put us somewhere before it) + */ if (!(r->m_read.flags & RTMP_READ_GOTKF) && packet.m_packetType != 0x16) { RTMP_Log(RTMP_LOGWARNING, @@ -3813,16 +3814,16 @@ static int ret = RTMP_READ_IGNORE; break; } - + /* we have to ignore the 0ms frames since these are the first - * keyframes; we've got these so don't mess around with multiple - * copies sent by the server to us! (if the keyframe is found at a - * later position there is only one copy and it will be ignored by - * the preceding if clause) - */ + * keyframes; we've got these so don't mess around with multiple + * copies sent by the server to us! (if the keyframe is found at a + * later position there is only one copy and it will be ignored by + * the preceding if clause) + */ if (!(r->m_read.flags & RTMP_READ_NO_IGNORE) && packet.m_packetType != 0x16) { /* exclude type 0x16 (FLV) since it can - * contain several FLV packets */ + * contain several FLV packets */ if (packet.m_nTimeStamp == 0) { ret = RTMP_READ_IGNORE; break; @@ -3832,15 +3833,15 @@ static int } } } - + /* calculate packet size and allocate slop buffer if necessary */ size = nPacketLen + - ((packet.m_packetType == 0x08 || packet.m_packetType == 0x09 || packet.m_packetType == 0x12) ? 11 : 0) + - (packet.m_packetType != 0x16 ? 4 : 0); - + ((packet.m_packetType == 0x08 || packet.m_packetType == 0x09 || packet.m_packetType == 0x12) ? 11 : 0) + + (packet.m_packetType != 0x16 ? 4 : 0); + if (size + 4 > buflen) { /* the extra 4 is for the case of an FLV stream without a last - * prevTagSize (we need extra 4 bytes to append it) */ + * prevTagSize (we need extra 4 bytes to append it) */ r->m_read.buf = malloc(size + 4); if (r->m_read.buf == 0) { RTMP_Log(RTMP_LOGERROR, "Couldn't allocate memory!"); @@ -3853,75 +3854,75 @@ static int ptr = buf; } pend = ptr + size + 4; - + /* use to return timestamp of last processed packet */ - + /* audio (0x08), video (0x09) or metadata (0x12) packets : - * construct 11 byte header then add PILI_RTMP packet's data */ + * construct 11 byte header then add PILI_RTMP packet's data */ if (packet.m_packetType == 0x08 || packet.m_packetType == 0x09 || packet.m_packetType == 0x12) { nTimeStamp = r->m_read.nResumeTS + packet.m_nTimeStamp; prevTagSize = 11 + nPacketLen; - + *ptr = packet.m_packetType; ptr++; ptr = AMF_EncodeInt24(ptr, pend, nPacketLen); - + #if 0 - if(packet.m_packetType == 0x09) { /* video */ - - /* H264 fix: */ - if((packetBody[0] & 0x0f) == 7) { /* CodecId = H264 */ - uint8_t packetType = *(packetBody+1); - - uint32_t ts = AMF_DecodeInt24(packetBody+2); /* composition time */ - int32_t cts = (ts+0xff800000)^0xff800000; - RTMP_Log(RTMP_LOGDEBUG, "cts : %d\n", cts); - - nTimeStamp -= cts; - /* get rid of the composition time */ - CRTMP::EncodeInt24(packetBody+2, 0); - } - RTMP_Log(RTMP_LOGDEBUG, "VIDEO: nTimeStamp: 0x%08X (%d)\n", nTimeStamp, nTimeStamp); - } + if(packet.m_packetType == 0x09) { /* video */ + + /* H264 fix: */ + if((packetBody[0] & 0x0f) == 7) { /* CodecId = H264 */ + uint8_t packetType = *(packetBody+1); + + uint32_t ts = AMF_DecodeInt24(packetBody+2); /* composition time */ + int32_t cts = (ts+0xff800000)^0xff800000; + RTMP_Log(RTMP_LOGDEBUG, "cts : %d\n", cts); + + nTimeStamp -= cts; + /* get rid of the composition time */ + CRTMP::EncodeInt24(packetBody+2, 0); + } + RTMP_Log(RTMP_LOGDEBUG, "VIDEO: nTimeStamp: 0x%08X (%d)\n", nTimeStamp, nTimeStamp); + } #endif - + ptr = AMF_EncodeInt24(ptr, pend, nTimeStamp); *ptr = (char)((nTimeStamp & 0xFF000000) >> 24); ptr++; - + /* stream id */ ptr = AMF_EncodeInt24(ptr, pend, 0); } - + memcpy(ptr, packetBody, nPacketLen); len = nPacketLen; - + /* correct tagSize and obtain timestamp if we have an FLV stream */ if (packet.m_packetType == 0x16) { unsigned int pos = 0; int delta; - + /* grab first timestamp and see if it needs fixing */ nTimeStamp = AMF_DecodeInt24(packetBody + 4); nTimeStamp |= (packetBody[7] << 24); delta = packet.m_nTimeStamp - nTimeStamp; - + while (pos + 11 < nPacketLen) { /* size without header (11) and without prevTagSize (4) */ uint32_t dataSize = AMF_DecodeInt24(packetBody + pos + 1); nTimeStamp = AMF_DecodeInt24(packetBody + pos + 4); nTimeStamp |= (packetBody[pos + 7] << 24); - + if (delta) { nTimeStamp += delta; AMF_EncodeInt24(ptr + pos + 4, pend, nTimeStamp); ptr[pos + 7] = nTimeStamp >> 24; } - + /* set data type */ r->m_read.dataType |= (((*(packetBody + pos) == 0x08) << 2) | (*(packetBody + pos) == 0x09)); - + if (pos + 11 + dataSize + 4 > nPacketLen) { if (pos + 11 + dataSize > nPacketLen) { RTMP_Log(RTMP_LOGERROR, @@ -3931,7 +3932,7 @@ static int break; } RTMP_Log(RTMP_LOGWARNING, "No tagSize found, appending!"); - + /* we have to append a last tagSize! */ prevTagSize = dataSize + 11; AMF_EncodeInt32(ptr + pos + 11 + dataSize, pend, @@ -3940,52 +3941,52 @@ static int len += 4; } else { prevTagSize = - AMF_DecodeInt32(packetBody + pos + 11 + dataSize); - + AMF_DecodeInt32(packetBody + pos + 11 + dataSize); + #ifdef _DEBUG RTMP_Log(RTMP_LOGDEBUG, "FLV Packet: type %02X, dataSize: %lu, tagSize: %lu, timeStamp: %lu ms", (unsigned char)packetBody[pos], dataSize, prevTagSize, nTimeStamp); #endif - + if (prevTagSize != (dataSize + 11)) { #ifdef _DEBUG RTMP_Log(RTMP_LOGWARNING, "Tag and data size are not consitent, writing tag size according to dataSize+11: %d", dataSize + 11); #endif - + prevTagSize = dataSize + 11; AMF_EncodeInt32(ptr + pos + 11 + dataSize, pend, prevTagSize); } } - + pos += prevTagSize + 4; /*(11+dataSize+4); */ } } ptr += len; - + if (packet.m_packetType != 0x16) { /* FLV tag packets contain their own prevTagSize */ AMF_EncodeInt32(ptr, pend, prevTagSize); } - + /* In non-live this nTimeStamp can contain an absolute TS. - * Update ext timestamp with this absolute offset in non-live mode - * otherwise report the relative one - */ + * Update ext timestamp with this absolute offset in non-live mode + * otherwise report the relative one + */ /* RTMP_Log(RTMP_LOGDEBUG, "type: %02X, size: %d, pktTS: %dms, TS: %dms, bLiveStream: %d", packet.m_packetType, nPacketLen, packet.m_nTimeStamp, nTimeStamp, r->Link.lFlags & RTMP_LF_LIVE); */ r->m_read.timestamp = (r->Link.lFlags & RTMP_LF_LIVE) ? packet.m_nTimeStamp : nTimeStamp; - + ret = size; break; } - + if (rtnGetNextMediaPacket) PILI_RTMPPacket_Free(&packet); - + if (recopy) { len = ret > buflen ? buflen : ret; memcpy(buf, r->m_read.buf, len); @@ -3996,15 +3997,15 @@ static int } static const char flvHeader[] = {'F', 'L', 'V', 0x01, - 0x00, /* 0x04 == audio, 0x01 == video */ - 0x00, 0x00, 0x00, 0x09, - 0x00, 0x00, 0x00, 0x00}; + 0x00, /* 0x04 == audio, 0x01 == video */ + 0x00, 0x00, 0x00, 0x09, + 0x00, 0x00, 0x00, 0x00}; #define HEADERBUF (128 * 1024) int PILI_RTMP_Read(PILI_RTMP *r, char *buf, int size) { int nRead = 0, total = 0; - -/* can't continue */ + + /* can't continue */ fail: switch (r->m_read.status) { case RTMP_READ_EOF: @@ -4016,7 +4017,7 @@ int PILI_RTMP_Read(PILI_RTMP *r, char *buf, int size) { default: break; } - + if ((r->m_read.flags & RTMP_READ_SEEKING) && r->m_read.buf) { /* drop whatever's here */ free(r->m_read.buf); @@ -4024,7 +4025,7 @@ int PILI_RTMP_Read(PILI_RTMP *r, char *buf, int size) { r->m_read.bufpos = NULL; r->m_read.buflen = 0; } - + /* If there's leftover data buffered, use it up */ if (r->m_read.buf) { nRead = r->m_read.buflen; @@ -4043,7 +4044,7 @@ int PILI_RTMP_Read(PILI_RTMP *r, char *buf, int size) { total += nRead; size -= nRead; } - + while (size > 0 && (nRead = Read_1_Packet(r, buf, size)) >= 0) { if (!nRead) continue; buf += nRead; @@ -4053,7 +4054,7 @@ int PILI_RTMP_Read(PILI_RTMP *r, char *buf, int size) { } if (nRead < 0) r->m_read.status = nRead; - + if (size < 0) total += size; return total; @@ -4065,22 +4066,22 @@ int PILI_RTMP_Write(PILI_RTMP *r, const char *buf, int size, RTMPError *error) { PILI_RTMPPacket *pkt = &r->m_write; char *pend, *enc; int s2 = size, ret, num; - + pkt->m_nChannel = 0x04; /* source channel */ pkt->m_nInfoField2 = r->m_stream_id; - + while (s2) { if (!pkt->m_nBytesRead) { if (size < 11) { /* FLV pkt too small */ return 0; } - + if (buf[0] == 'F' && buf[1] == 'L' && buf[2] == 'V') { buf += 13; s2 -= 13; } - + pkt->m_packetType = *buf++; pkt->m_nBodySize = AMF_DecodeInt24(buf); buf += 3; @@ -4089,7 +4090,7 @@ int PILI_RTMP_Write(PILI_RTMP *r, const char *buf, int size, RTMPError *error) { pkt->m_nTimeStamp |= *buf++ << 24; buf += 3; s2 -= 11; - + if (((pkt->m_packetType == 0x08 || pkt->m_packetType == 0x09) && !pkt->m_nTimeStamp) || pkt->m_packetType == 0x12) { @@ -4099,7 +4100,7 @@ int PILI_RTMP_Write(PILI_RTMP *r, const char *buf, int size, RTMPError *error) { } else { pkt->m_headerType = RTMP_PACKET_SIZE_MEDIUM; } - + if (!PILI_RTMPPacket_Alloc(pkt, pkt->m_nBodySize)) { RTMP_Log(RTMP_LOGDEBUG, "%s, failed to allocate packet", __FUNCTION__); return FALSE; diff --git a/LFLiveKit/Vendor/pili-librtmp/rtmp.h b/LFLiveKit/Vendor/pili-librtmp/rtmp.h index d415e16c..2fbd500f 100644 --- a/LFLiveKit/Vendor/pili-librtmp/rtmp.h +++ b/LFLiveKit/Vendor/pili-librtmp/rtmp.h @@ -1,3 +1,4 @@ + #ifndef __RTMP_H__ #define __RTMP_H__ /* @@ -25,7 +26,8 @@ * http://www.gnu.org/copyleft/lgpl.html */ -#define NO_CRYPTO +//#define NO_CRYPTO +#define CRYPTO #if !defined(NO_CRYPTO) && !defined(CRYPTO) #define CRYPTO @@ -41,16 +43,16 @@ #ifdef __cplusplus extern "C" { #endif - + #define RTMP_LIB_VERSION 0x020300 /* 2.3 */ - + #define RTMP_FEATURE_HTTP 0x01 #define RTMP_FEATURE_ENC 0x02 #define RTMP_FEATURE_SSL 0x04 #define RTMP_FEATURE_MFP 0x08 /* not yet supported */ #define RTMP_FEATURE_WRITE 0x10 /* publish, not play */ #define RTMP_FEATURE_HTTP2 0x20 /* server-side rtmpt */ - + #define RTMP_PROTOCOL_UNDEFINED -1 #define RTMP_PROTOCOL_RTMP 0 #define RTMP_PROTOCOL_RTMPE RTMP_FEATURE_ENC @@ -59,296 +61,296 @@ extern "C" { #define RTMP_PROTOCOL_RTMPTE (RTMP_FEATURE_HTTP | RTMP_FEATURE_ENC) #define RTMP_PROTOCOL_RTMPTS (RTMP_FEATURE_HTTP | RTMP_FEATURE_SSL) #define RTMP_PROTOCOL_RTMFP RTMP_FEATURE_MFP - + #define RTMP_DEFAULT_CHUNKSIZE 128 - -/* needs to fit largest number of bytes recv() may return */ + + /* needs to fit largest number of bytes recv() may return */ #define RTMP_BUFFER_CACHE_SIZE (16 * 1024) - + #define RTMP_CHANNELS 65600 - -extern const char PILI_RTMPProtocolStringsLower[][7]; -extern const AVal PILI_RTMP_DefaultFlashVer; -extern int PILI_RTMP_ctrlC; - -uint32_t PILI_RTMP_GetTime(void); - + + extern const char PILI_RTMPProtocolStringsLower[][7]; + extern const AVal PILI_RTMP_DefaultFlashVer; + extern int PILI_RTMP_ctrlC; + + uint32_t PILI_RTMP_GetTime(void); + #define RTMP_PACKET_TYPE_AUDIO 0x08 #define RTMP_PACKET_TYPE_VIDEO 0x09 #define RTMP_PACKET_TYPE_INFO 0x12 - + #define RTMP_MAX_HEADER_SIZE 18 - + #define RTMP_PACKET_SIZE_LARGE 0 #define RTMP_PACKET_SIZE_MEDIUM 1 #define RTMP_PACKET_SIZE_SMALL 2 #define RTMP_PACKET_SIZE_MINIMUM 3 - -typedef struct PILI_RTMPChunk { - int c_headerSize; - int c_chunkSize; - char *c_chunk; - char c_header[RTMP_MAX_HEADER_SIZE]; -} PILI_RTMPChunk; - -typedef struct PILI_RTMPPacket { - uint8_t m_headerType; - uint8_t m_packetType; - uint8_t m_hasAbsTimestamp; /* timestamp absolute or relative? */ - int m_nChannel; - uint32_t m_nTimeStamp; /* timestamp */ - int32_t m_nInfoField2; /* last 4 bytes in a long header */ - uint32_t m_nBodySize; - uint32_t m_nBytesRead; - PILI_RTMPChunk *m_chunk; - char *m_body; -} PILI_RTMPPacket; - -typedef struct PILI_RTMPSockBuf { - int sb_socket; - int sb_size; /* number of unprocessed bytes in buffer */ - char *sb_start; /* pointer into sb_pBuffer of next byte to process */ - char sb_buf[RTMP_BUFFER_CACHE_SIZE]; /* data read from socket */ - int sb_timedout; - void *sb_ssl; -} PILI_RTMPSockBuf; - -void PILI_RTMPPacket_Reset(PILI_RTMPPacket *p); -void PILI_RTMPPacket_Dump(PILI_RTMPPacket *p); -int PILI_RTMPPacket_Alloc(PILI_RTMPPacket *p, int nSize); -void PILI_RTMPPacket_Free(PILI_RTMPPacket *p); - + + typedef struct PILI_RTMPChunk { + int c_headerSize; + int c_chunkSize; + char *c_chunk; + char c_header[RTMP_MAX_HEADER_SIZE]; + } PILI_RTMPChunk; + + typedef struct PILI_RTMPPacket { + uint8_t m_headerType; + uint8_t m_packetType; + uint8_t m_hasAbsTimestamp; /* timestamp absolute or relative? */ + int m_nChannel; + uint32_t m_nTimeStamp; /* timestamp */ + int32_t m_nInfoField2; /* last 4 bytes in a long header */ + uint32_t m_nBodySize; + uint32_t m_nBytesRead; + PILI_RTMPChunk *m_chunk; + char *m_body; + } PILI_RTMPPacket; + + typedef struct PILI_RTMPSockBuf { + int sb_socket; + int sb_size; /* number of unprocessed bytes in buffer */ + char *sb_start; /* pointer into sb_pBuffer of next byte to process */ + char sb_buf[RTMP_BUFFER_CACHE_SIZE]; /* data read from socket */ + int sb_timedout; + void *sb_ssl; + } PILI_RTMPSockBuf; + + void PILI_RTMPPacket_Reset(PILI_RTMPPacket *p); + void PILI_RTMPPacket_Dump(PILI_RTMPPacket *p); + int PILI_RTMPPacket_Alloc(PILI_RTMPPacket *p, int nSize); + void PILI_RTMPPacket_Free(PILI_RTMPPacket *p); + #define RTMPPacket_IsReady(a) ((a)->m_nBytesRead == (a)->m_nBodySize) - -typedef struct PILI_RTMP_LNK { - AVal hostname; - AVal domain; - AVal sockshost; - - AVal playpath0; /* parsed from URL */ - AVal playpath; /* passed in explicitly */ - AVal tcUrl; - AVal swfUrl; - AVal pageUrl; - AVal app; - AVal auth; - AVal flashVer; - AVal subscribepath; - AVal token; - AMFObject extras; - int edepth; - - int seekTime; - int stopTime; - + + typedef struct PILI_RTMP_LNK { + AVal hostname; + AVal domain; + AVal sockshost; + + AVal playpath0; /* parsed from URL */ + AVal playpath; /* passed in explicitly */ + AVal tcUrl; + AVal swfUrl; + AVal pageUrl; + AVal app; + AVal auth; + AVal flashVer; + AVal subscribepath; + AVal token; + AMFObject extras; + int edepth; + + int seekTime; + int stopTime; + #define RTMP_LF_AUTH 0x0001 /* using auth param */ #define RTMP_LF_LIVE 0x0002 /* stream is live */ #define RTMP_LF_SWFV 0x0004 /* do SWF verification */ #define RTMP_LF_PLST 0x0008 /* send playlist before play */ #define RTMP_LF_BUFX 0x0010 /* toggle stream on BufferEmpty msg */ #define RTMP_LF_FTCU 0x0020 /* free tcUrl on close */ - int lFlags; - - int swfAge; - - int protocol; - int timeout; /* connection timeout in seconds */ - int send_timeout; /* send data timeout */ - - unsigned short socksport; - unsigned short port; - + int lFlags; + + int swfAge; + + int protocol; + int timeout; /* connection timeout in seconds */ + int send_timeout; /* send data timeout */ + + unsigned short socksport; + unsigned short port; + #ifdef CRYPTO #define RTMP_SWF_HASHLEN 32 - void *dh; /* for encryption */ - void *rc4keyIn; - void *rc4keyOut; - - uint32_t SWFSize; - uint8_t SWFHash[RTMP_SWF_HASHLEN]; - char SWFVerificationResponse[RTMP_SWF_HASHLEN + 10]; + void *dh; /* for encryption */ + void *rc4keyIn; + void *rc4keyOut; + + uint32_t SWFSize; + uint8_t SWFHash[RTMP_SWF_HASHLEN]; + char SWFVerificationResponse[RTMP_SWF_HASHLEN + 10]; #endif -} PILI_RTMP_LNK; - -/* state for read() wrapper */ -typedef struct PILI_RTMP_READ { - char *buf; - char *bufpos; - unsigned int buflen; - uint32_t timestamp; - uint8_t dataType; - uint8_t flags; + } PILI_RTMP_LNK; + + /* state for read() wrapper */ + typedef struct PILI_RTMP_READ { + char *buf; + char *bufpos; + unsigned int buflen; + uint32_t timestamp; + uint8_t dataType; + uint8_t flags; #define RTMP_READ_HEADER 0x01 #define RTMP_READ_RESUME 0x02 #define RTMP_READ_NO_IGNORE 0x04 #define RTMP_READ_GOTKF 0x08 #define RTMP_READ_GOTFLVK 0x10 #define RTMP_READ_SEEKING 0x20 - int8_t status; + int8_t status; #define RTMP_READ_COMPLETE -3 #define RTMP_READ_ERROR -2 #define RTMP_READ_EOF -1 #define RTMP_READ_IGNORE 0 - - /* if bResume == TRUE */ - uint8_t initialFrameType; - uint32_t nResumeTS; - char *metaHeader; - char *initialFrame; - uint32_t nMetaHeaderSize; - uint32_t nInitialFrameSize; - uint32_t nIgnoredFrameCounter; - uint32_t nIgnoredFlvFrameCounter; -} PILI_RTMP_READ; - -typedef struct PILI_RTMP_METHOD { - AVal name; - int num; -} PILI_RTMP_METHOD; - -typedef void (*PILI_RTMPErrorCallback)(RTMPError *error, void *userData); - -typedef struct PILI_CONNECTION_TIME { - uint32_t connect_time; - uint32_t handshake_time; -} PILI_CONNECTION_TIME; - -typedef void (*PILI_RTMP_ConnectionTimeCallback)( - PILI_CONNECTION_TIME *conn_time, void *userData); - -typedef struct PILI_RTMP { - int m_inChunkSize; - int m_outChunkSize; - int m_nBWCheckCounter; - int m_nBytesIn; - int m_nBytesInSent; - int m_nBufferMS; - int m_stream_id; /* returned in _result from createStream */ - int m_mediaChannel; - uint32_t m_mediaStamp; - uint32_t m_pauseStamp; - int m_pausing; - int m_nServerBW; - int m_nClientBW; - uint8_t m_nClientBW2; - uint8_t m_bPlaying; - uint8_t m_bSendEncoding; - uint8_t m_bSendCounter; - - int m_numInvokes; - int m_numCalls; - PILI_RTMP_METHOD *m_methodCalls; /* remote method calls queue */ - - PILI_RTMPPacket *m_vecChannelsIn[RTMP_CHANNELS]; - PILI_RTMPPacket *m_vecChannelsOut[RTMP_CHANNELS]; - int m_channelTimestamp[RTMP_CHANNELS]; /* abs timestamp of last packet */ - - double m_fAudioCodecs; /* audioCodecs for the connect packet */ - double m_fVideoCodecs; /* videoCodecs for the connect packet */ - double m_fEncoding; /* AMF0 or AMF3 */ - - double m_fDuration; /* duration of stream in seconds */ - - int m_msgCounter; /* RTMPT stuff */ - int m_polling; - int m_resplen; - int m_unackd; - AVal m_clientID; - - PILI_RTMP_READ m_read; - PILI_RTMPPacket m_write; - PILI_RTMPSockBuf m_sb; - PILI_RTMP_LNK Link; - - PILI_RTMPErrorCallback m_errorCallback; - PILI_RTMP_ConnectionTimeCallback m_connCallback; - RTMPError *m_error; - void *m_userData; - int m_is_closing; - int m_tcp_nodelay; - uint32_t ip; -} PILI_RTMP; - -int PILI_RTMP_ParseURL(const char *url, int *protocol, AVal *host, - unsigned int *port, AVal *playpath, AVal *app); - -int PILI_RTMP_ParseURL2(const char *url, int *protocol, AVal *host, - unsigned int *port, AVal *playpath, AVal *app, AVal *domain); - -void PILI_RTMP_ParsePlaypath(AVal *in, AVal *out); -void PILI_RTMP_SetBufferMS(PILI_RTMP *r, int size); -void PILI_RTMP_UpdateBufferMS(PILI_RTMP *r, RTMPError *error); - -int PILI_RTMP_SetOpt(PILI_RTMP *r, const AVal *opt, AVal *arg, - RTMPError *error); -int PILI_RTMP_SetupURL(PILI_RTMP *r, const char *url, RTMPError *error); -void PILI_RTMP_SetupStream(PILI_RTMP *r, int protocol, AVal *hostname, - unsigned int port, AVal *sockshost, AVal *playpath, - AVal *tcUrl, AVal *swfUrl, AVal *pageUrl, AVal *app, - AVal *auth, AVal *swfSHA256Hash, uint32_t swfSize, - AVal *flashVer, AVal *subscribepath, int dStart, - int dStop, int bLiveStream, long int timeout); - -int PILI_RTMP_Connect(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error); -struct sockaddr; -int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, - RTMPError *error); -int PILI_RTMP_Connect1(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error); -int PILI_RTMP_Serve(PILI_RTMP *r, RTMPError *error); - -int PILI_RTMP_ReadPacket(PILI_RTMP *r, PILI_RTMPPacket *packet); -int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, + + /* if bResume == TRUE */ + uint8_t initialFrameType; + uint32_t nResumeTS; + char *metaHeader; + char *initialFrame; + uint32_t nMetaHeaderSize; + uint32_t nInitialFrameSize; + uint32_t nIgnoredFrameCounter; + uint32_t nIgnoredFlvFrameCounter; + } PILI_RTMP_READ; + + typedef struct PILI_RTMP_METHOD { + AVal name; + int num; + } PILI_RTMP_METHOD; + + typedef void (*PILI_RTMPErrorCallback)(RTMPError *error, void *userData); + + typedef struct PILI_CONNECTION_TIME { + uint32_t connect_time; + uint32_t handshake_time; + } PILI_CONNECTION_TIME; + + typedef void (*PILI_RTMP_ConnectionTimeCallback)( + PILI_CONNECTION_TIME *conn_time, void *userData); + + typedef struct PILI_RTMP { + int m_inChunkSize; + int m_outChunkSize; + int m_nBWCheckCounter; + int m_nBytesIn; + int m_nBytesInSent; + int m_nBufferMS; + int m_stream_id; /* returned in _result from createStream */ + int m_mediaChannel; + uint32_t m_mediaStamp; + uint32_t m_pauseStamp; + int m_pausing; + int m_nServerBW; + int m_nClientBW; + uint8_t m_nClientBW2; + uint8_t m_bPlaying; + uint8_t m_bSendEncoding; + uint8_t m_bSendCounter; + + int m_numInvokes; + int m_numCalls; + PILI_RTMP_METHOD *m_methodCalls; /* remote method calls queue */ + + PILI_RTMPPacket *m_vecChannelsIn[RTMP_CHANNELS]; + PILI_RTMPPacket *m_vecChannelsOut[RTMP_CHANNELS]; + int m_channelTimestamp[RTMP_CHANNELS]; /* abs timestamp of last packet */ + + double m_fAudioCodecs; /* audioCodecs for the connect packet */ + double m_fVideoCodecs; /* videoCodecs for the connect packet */ + double m_fEncoding; /* AMF0 or AMF3 */ + + double m_fDuration; /* duration of stream in seconds */ + + int m_msgCounter; /* RTMPT stuff */ + int m_polling; + int m_resplen; + int m_unackd; + AVal m_clientID; + + PILI_RTMP_READ m_read; + PILI_RTMPPacket m_write; + PILI_RTMPSockBuf m_sb; + PILI_RTMP_LNK Link; + + PILI_RTMPErrorCallback m_errorCallback; + PILI_RTMP_ConnectionTimeCallback m_connCallback; + RTMPError *m_error; + void *m_userData; + int m_is_closing; + int m_tcp_nodelay; + uint32_t ip; + } PILI_RTMP; + + int PILI_RTMP_ParseURL(const char *url, int *protocol, AVal *host, + unsigned int *port, AVal *playpath, AVal *app); + + int PILI_RTMP_ParseURL2(const char *url, int *protocol, AVal *host, + unsigned int *port, AVal *playpath, AVal *app, AVal *domain); + + void PILI_RTMP_ParsePlaypath(AVal *in, AVal *out); + void PILI_RTMP_SetBufferMS(PILI_RTMP *r, int size); + void PILI_RTMP_UpdateBufferMS(PILI_RTMP *r, RTMPError *error); + + int PILI_RTMP_SetOpt(PILI_RTMP *r, const AVal *opt, AVal *arg, RTMPError *error); -int PILI_RTMP_SendChunk(PILI_RTMP *r, PILI_RTMPChunk *chunk, RTMPError *error); -int PILI_RTMP_IsConnected(PILI_RTMP *r); -int PILI_RTMP_Socket(PILI_RTMP *r); -int PILI_RTMP_IsTimedout(PILI_RTMP *r); -double PILI_RTMP_GetDuration(PILI_RTMP *r); -int PILI_RTMP_ToggleStream(PILI_RTMP *r, RTMPError *error); - -int PILI_RTMP_ConnectStream(PILI_RTMP *r, int seekTime, RTMPError *error); -int PILI_RTMP_ReconnectStream(PILI_RTMP *r, int seekTime, RTMPError *error); -void PILI_RTMP_DeleteStream(PILI_RTMP *r, RTMPError *error); -int PILI_RTMP_GetNextMediaPacket(PILI_RTMP *r, PILI_RTMPPacket *packet); -int PILI_RTMP_ClientPacket(PILI_RTMP *r, PILI_RTMPPacket *packet); - -void PILI_RTMP_Init(PILI_RTMP *r); -void PILI_RTMP_Close(PILI_RTMP *r, RTMPError *error); -PILI_RTMP *PILI_RTMP_Alloc(void); -void PILI_RTMP_Free(PILI_RTMP *r); -void PILI_RTMP_EnableWrite(PILI_RTMP *r); - -int PILI_RTMP_LibVersion(void); -void PILI_RTMP_UserInterrupt(void); /* user typed Ctrl-C */ - -int PILI_RTMP_SendCtrl(PILI_RTMP *r, short nType, unsigned int nObject, - unsigned int nTime, RTMPError *error); - -/* caller probably doesn't know current timestamp, should - * just use RTMP_Pause instead - */ -int PILI_RTMP_SendPause(PILI_RTMP *r, int DoPause, int dTime, RTMPError *error); -int PILI_RTMP_Pause(PILI_RTMP *r, int DoPause, RTMPError *error); - -int PILI_RTMP_FindFirstMatchingProperty(AMFObject *obj, const AVal *name, - AMFObjectProperty *p); - -int PILI_RTMPSockBuf_Fill(PILI_RTMPSockBuf *sb); -int PILI_RTMPSockBuf_Send(PILI_RTMPSockBuf *sb, const char *buf, int len); -int PILI_RTMPSockBuf_Close(PILI_RTMPSockBuf *sb); - -int PILI_RTMP_SendCreateStream(PILI_RTMP *r, RTMPError *error); -int PILI_RTMP_SendSeek(PILI_RTMP *r, int dTime, RTMPError *error); -int PILI_RTMP_SendServerBW(PILI_RTMP *r, RTMPError *error); -int PILI_RTMP_SendClientBW(PILI_RTMP *r, RTMPError *error); -void PILI_RTMP_DropRequest(PILI_RTMP *r, int i, int freeit); -int PILI_RTMP_Read(PILI_RTMP *r, char *buf, int size); -int PILI_RTMP_Write(PILI_RTMP *r, const char *buf, int size, RTMPError *error); - -/* hashswf.c */ -int PILI_RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, - int age); - + int PILI_RTMP_SetupURL(PILI_RTMP *r, const char *url, RTMPError *error); + void PILI_RTMP_SetupStream(PILI_RTMP *r, int protocol, AVal *hostname, + unsigned int port, AVal *sockshost, AVal *playpath, + AVal *tcUrl, AVal *swfUrl, AVal *pageUrl, AVal *app, + AVal *auth, AVal *swfSHA256Hash, uint32_t swfSize, + AVal *flashVer, AVal *subscribepath, int dStart, + int dStop, int bLiveStream, long int timeout); + + int PILI_RTMP_Connect(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error); + struct sockaddr; + int PILI_RTMP_Connect0(PILI_RTMP *r, struct addrinfo *ai, unsigned short port, + RTMPError *error); + int PILI_RTMP_Connect1(PILI_RTMP *r, PILI_RTMPPacket *cp, RTMPError *error); + int PILI_RTMP_Serve(PILI_RTMP *r, RTMPError *error); + + int PILI_RTMP_ReadPacket(PILI_RTMP *r, PILI_RTMPPacket *packet); + int PILI_RTMP_SendPacket(PILI_RTMP *r, PILI_RTMPPacket *packet, int queue, + RTMPError *error); + int PILI_RTMP_SendChunk(PILI_RTMP *r, PILI_RTMPChunk *chunk, RTMPError *error); + int PILI_RTMP_IsConnected(PILI_RTMP *r); + int PILI_RTMP_Socket(PILI_RTMP *r); + int PILI_RTMP_IsTimedout(PILI_RTMP *r); + double PILI_RTMP_GetDuration(PILI_RTMP *r); + int PILI_RTMP_ToggleStream(PILI_RTMP *r, RTMPError *error); + + int PILI_RTMP_ConnectStream(PILI_RTMP *r, int seekTime, RTMPError *error); + int PILI_RTMP_ReconnectStream(PILI_RTMP *r, int seekTime, RTMPError *error); + void PILI_RTMP_DeleteStream(PILI_RTMP *r, RTMPError *error); + int PILI_RTMP_GetNextMediaPacket(PILI_RTMP *r, PILI_RTMPPacket *packet); + int PILI_RTMP_ClientPacket(PILI_RTMP *r, PILI_RTMPPacket *packet); + + void PILI_RTMP_Init(PILI_RTMP *r); + void PILI_RTMP_Close(PILI_RTMP *r, RTMPError *error); + PILI_RTMP *PILI_RTMP_Alloc(void); + void PILI_RTMP_Free(PILI_RTMP *r); + void PILI_RTMP_EnableWrite(PILI_RTMP *r); + + int PILI_RTMP_LibVersion(void); + void PILI_RTMP_UserInterrupt(void); /* user typed Ctrl-C */ + + int PILI_RTMP_SendCtrl(PILI_RTMP *r, short nType, unsigned int nObject, + unsigned int nTime, RTMPError *error); + + /* caller probably doesn't know current timestamp, should + * just use RTMP_Pause instead + */ + int PILI_RTMP_SendPause(PILI_RTMP *r, int DoPause, int dTime, RTMPError *error); + int PILI_RTMP_Pause(PILI_RTMP *r, int DoPause, RTMPError *error); + + int PILI_RTMP_FindFirstMatchingProperty(AMFObject *obj, const AVal *name, + AMFObjectProperty *p); + + int PILI_RTMPSockBuf_Fill(PILI_RTMPSockBuf *sb); + int PILI_RTMPSockBuf_Send(PILI_RTMPSockBuf *sb, const char *buf, int len); + int PILI_RTMPSockBuf_Close(PILI_RTMPSockBuf *sb); + + int PILI_RTMP_SendCreateStream(PILI_RTMP *r, RTMPError *error); + int PILI_RTMP_SendSeek(PILI_RTMP *r, int dTime, RTMPError *error); + int PILI_RTMP_SendServerBW(PILI_RTMP *r, RTMPError *error); + int PILI_RTMP_SendClientBW(PILI_RTMP *r, RTMPError *error); + void PILI_RTMP_DropRequest(PILI_RTMP *r, int i, int freeit); + int PILI_RTMP_Read(PILI_RTMP *r, char *buf, int size); + int PILI_RTMP_Write(PILI_RTMP *r, const char *buf, int size, RTMPError *error); + + /* hashswf.c */ + int PILI_RTMP_HashSWF(const char *url, unsigned int *size, unsigned char *hash, + int age); + #ifdef __cplusplus }; #endif diff --git a/README.md b/README.md index e34a28a3..de95a258 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ LFLiveKit ============== +## RTMPS +This fork added support to stream via RTMPS protocol +If you are getting error "'openssl/rc4.h' file not found" please add OpenSSL to your project. +The solution is implemented by author of this issue: https://github.com/LaiFengiOS/LFLiveKit/issues/237 @webdeutschland +Also mych thanks to @pcKanso + ![icon~](https://raw.github.com/LaiFengiOS/LFLiveKit/master/samples/Icon.png)