From c31021149d15a9aa199feed67772500a7d1bea72 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 11:04:08 +0930 Subject: [PATCH 01/24] rtmp: remove redundant comments --- rtmp/rtmp.go | 105 +-------------------------------------------------- 1 file changed, 1 insertion(+), 104 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 8f05d953..d656fb3a 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -142,23 +142,17 @@ var ( func startSession(rtmp *C_RTMP, u string, timeout uint32) (*C_RTMP, error) { connect_timeout := int32(timeout) rtmp = C_RTMP_Alloc() - //rtmp = C.RTMP_Alloc() C_RTMP_Init(rtmp) - //C.RTMP_Init(rtmp) rtmp.Link.timeout = connect_timeout if C_RTMP_SetupURL(rtmp, u) == 0 { - // if C.RTMP_SetupURL(rtmp, C.CString(u)) == 0 { C_RTMP_Close(rtmp) //C.RTMP_Free(rtmp) return nil, errors.New("rtmp startSession: Failed to setup URL!") } C_RTMP_EnableWrite(rtmp) - //C.RTMP_EnableWrite(rtmp) C_RTMP_SetBufferMS(rtmp, 3600*1000) - //C.RTMP_SetBufferMS(rtmp, 3600*1000) if C_RTMP_Connect(rtmp, nil) == 0 { - //if C.RTMP_Connect(rtmp, nil) == 0 { C_RTMP_Close(rtmp) //C.RTMP_Free(rtmp) return nil, errors.New("rtmp startSession: Failed to connect!") @@ -166,7 +160,6 @@ func startSession(rtmp *C_RTMP, u string, timeout uint32) (*C_RTMP, error) { // TODO: port this if C_RTMP_ConnectStream(rtmp, 0) == 0 { - //if C.RTMP_ConnectStream(rtmp, 0) == 0 { C_RTMP_Close(rtmp) //C.RTMP_Free(rtmp) return nil, errors.New("rtmp startSession: Failed to connect stream!") @@ -216,7 +209,6 @@ func C_RTMPPacket_Alloc(p *C_RTMPPacket, nSize uint32) int { // rtmp.c +203 func C_RTMPPacket_Free(p *C_RTMPPacket) { if p.m_body != nil { - //C.free(decBytePtr(unsafe.Pointer(p.m_body), RTMP_MAX_HEADER_SIZE)) p.m_body = nil } } @@ -225,7 +217,6 @@ func C_RTMPPacket_Free(p *C_RTMPPacket) { // rtmp.c +317 func C_RTMP_Alloc() *C_RTMP { return &C_RTMP{} - //return (*C_RTMP)(allocate(unsafe.Sizeof(r))) } // void RTMP_Init(RTMP *r); @@ -309,8 +300,6 @@ func C_RTMP_SetupURL(r *C_RTMP, u string) int32 { length = strlen(url) // TODO: port this - //ret = int32(C.RTMP_ParseURL((*byte)(unsafe.Pointer(url)), &r.Link.protocol, - // &r.Link.hostname, (*C.uint)(&port), &r.Link.playpath0, &r.Link.app)) ret = int32(C_RTMP_ParseURL((*byte)(unsafe.Pointer(url)), (*int32)( unsafe.Pointer(&r.Link.protocol)), &r.Link.hostname, (*uint32)( unsafe.Pointer(&port)), &r.Link.playpath0, &r.Link.app)) @@ -416,7 +405,6 @@ func C_RTMP_Connect0(r *C_RTMP, service *C.sockaddr) int { log.Println("C_RTMP_Connect0: socks negotiation.") } - //if C.SocksNegotiate(r) == 0 { if C_SocksNegotiate(r) == 0 { log.Println("C_RTMP_Connect0: SOCKS negotiation failed.") return 0 @@ -430,7 +418,6 @@ func C_RTMP_Connect0(r *C_RTMP, service *C.sockaddr) int { { var tv int32 SET_RCVTIMEO(&tv, int32(r.Link.timeout)) - // tv := int32(r.Link.timeout * 1000) if C.setsockopt(C.int(r.m_sb.sb_socket), C.SOL_SOCKET, C.SO_RCVTIMEO, unsafe.Pointer(&tv), C.socklen_t(unsafe.Sizeof(tv))) != 0 { @@ -450,7 +437,6 @@ func C_RTMP_Connect1(r *C_RTMP, cp *C_RTMPPacket) int { if debugMode { log.Println("... connected, handshaking...") } - //if C.HandShake(r, 1) == 0 { if C_HandShake(r, 1) == 0 { log.Println("C_RTMP_Connect1: handshake failed!") return 0 @@ -458,7 +444,6 @@ func C_RTMP_Connect1(r *C_RTMP, cp *C_RTMPPacket) int { if debugMode { log.Println("... handshaked...") } - //if C.SendConnectPacket(r, cp) == 0 { if C_SendConnectPacket(r, cp) == 0 { log.Println("RTMP connect failed!") return 0 @@ -491,7 +476,6 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) int { return 0 } } - //if C.RTMP_Connect0(r, (*C.sockaddr)(unsafe.Pointer(&service))) == 0 { if C_RTMP_Connect0(r, (*C.sockaddr)(unsafe.Pointer(&service))) == 0 { return 0 } @@ -499,7 +483,6 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) int { r.m_bSendCounter = 1 return int(C_RTMP_Connect1(r, cp)) - //return int(C.RTMP_Connect1(r, cp)) } // int SocksNegotiate(RTMP* r); @@ -553,7 +536,6 @@ func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) int { // TODO: read packet for r.m_bPlaying == 0 && C_RTMP_IsConnected(r) != 0 && - //C.RTMP_ReadPacket(r, &packet) != 0 { C_RTMP_ReadPacket(r, &packet) != 0 { // TODO: port is ready @@ -570,7 +552,6 @@ func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) int { continue } - //C.RTMP_ClientPacket(r, &packet) C_RTMP_ClientPacket(r, &packet) C_RTMPPacket_Free(&packet) } @@ -587,7 +568,6 @@ func C_RTMP_ClientPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { case RTMP_PACKET_TYPE_CHUNK_SIZE: // TODO: port this - //C.HandleChangeChunkSize(r, packet) C_HandleChangeChunkSize(r, packet) case RTMP_PACKET_TYPE_BYTES_READ_REPORT: @@ -603,12 +583,10 @@ func C_RTMP_ClientPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { */ case RTMP_PACKET_TYPE_SERVER_BW: // TODO: port this - //C.HandleServerBW(r, packet) C_HandlServerBW(r, packet) case RTMP_PACKET_TYPE_CLIENT_BW: // TODO: port this - //C.HandleClientBW(r, packet) C_HandleClientBW(r, packet) case RTMP_PACKET_TYPE_AUDIO: @@ -628,7 +606,6 @@ func C_RTMP_ClientPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { // TODO use new logger here //RTMP_Log(RTMP_LOGDEBUG, "%s, received: invoke %u bytes", __FUNCTION__,packet.m_nBodySize); - //if C.HandleInvoke(r, packet.m_body, C.uint(packet.m_nBodySize)) == 1 { if C_HandleInvoke(r, (*byte)(unsafe.Pointer(packet.m_body)), uint32(packet.m_nBodySize)) == 1 { log.Println("HasMediaPacket") bHasMediaPacket = 2 @@ -662,7 +639,6 @@ func C_ReadN(r *C_RTMP, buffer *byte, n int) int { avail = int(r.m_sb.sb_size) if avail == 0 { if C_RTMPSockBuf_Fill(&r.m_sb) < 1 { - // if C.RTMPSockBuf_Fill(&r.m_sb) < 1 { if r.m_sb.sb_timedout == 0 { return 0 } @@ -685,7 +661,6 @@ func C_ReadN(r *C_RTMP, buffer *byte, n int) int { r.m_nBytesIn += int32(nRead) if r.m_bSendCounter != 0 && r.m_nBytesIn > (r.m_nBytesInSent+ r.m_nClientBW/10) { - //if C.SendBytesReceived(r) == 0 { if C_SendBytesReceived(r) == 0 { return 0 } @@ -750,7 +725,6 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { if cp != nil { return C_RTMP_SendPacket(r, cp, 1) - //return int(C.RTMP_SendPacket(r, cp, 1)) } packet.m_nChannel = 0x03 @@ -764,45 +738,33 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { enc = (*byte)(unsafe.Pointer(packet.m_body)) - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeString((*byte)(unsafe.Pointer(enc)), - //(*byte)(unsafe.Pointer(pend)), &av_connect))) enc = C_AMF_EncodeString(enc, pend, &av_connect) + r.m_numInvokes += 1 - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNumber((*byte)(unsafe.Pointer(enc)), - //(*byte)(unsafe.Pointer(pend)), float64(r.m_numInvokes)))) enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) (*[_Gi]byte)(unsafe.Pointer(enc))[0] = AMF_OBJECT enc = (*byte)(unsafe.Pointer(incBytePtr(unsafe.Pointer(enc), 1))) - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedString((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_app, &r.Link.app))) enc = C_AMF_EncodeNamedString(enc, pend, &av_app, &r.Link.app) if enc == nil { return 0 } if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedString((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_type, &av_nonprivate))) enc = C_AMF_EncodeNamedString(enc, pend, &av_type, &av_nonprivate) - if enc == nil { return 0 } } if r.Link.flashVer.av_len != 0 { - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedString((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_flashVer, &r.Link.flashVer))) enc = C_AMF_EncodeNamedString(enc, pend, &av_flashVer, &r.Link.flashVer) if enc == nil { return 0 } } if r.Link.swfUrl.av_len != 0 { - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedString((*byte)( - // unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_swfUrl, &r.Link.swfUrl))) enc = C_AMF_EncodeNamedString(enc, pend, &av_swfUrl, &r.Link.swfUrl) if enc == nil { return 0 @@ -810,8 +772,6 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { } if r.Link.tcUrl.av_len != 0 { - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedString((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_tcUrl, &r.Link.tcUrl))) enc = C_AMF_EncodeNamedString(enc, pend, &av_tcUrl, &r.Link.tcUrl) if enc == nil { return 0 @@ -819,39 +779,27 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { } if r.Link.protocol&RTMP_FEATURE_WRITE == 0 { - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedBoolean((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_fpad, 0))) enc = C_AMF_EncodeNamedBoolean(enc, pend, &av_fpad, 0) if enc == nil { return 0 } - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedNumber((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_capabilities, 15.0))) enc = C_AMF_EncodeNamedNumber(enc, pend, &av_capabilities, 15.0) if enc == nil { return 0 } - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedNumber((*byte)( - // unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_audioCodecs, r.m_fAudioCodecs))) enc = C_AMF_EncodeNamedNumber(enc, pend, &av_audioCodecs, float64(r.m_fAudioCodecs)) if enc == nil { return 0 } - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedNumber((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_videoCodecs, r.m_fVideoCodecs))) enc = C_AMF_EncodeNamedNumber(enc, pend, &av_videoCodecs, float64(r.m_fVideoCodecs)) if enc == nil { return 0 } - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedNumber((*byte)( - // unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_videoFunction, 1.0))) enc = C_AMF_EncodeNamedNumber(enc, pend, &av_videoFunction, 1.0) if enc == nil { return 0 } if r.Link.pageUrl.av_len != 0 { - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedString((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_pageUrl, &r.Link.pageUrl))) enc = C_AMF_EncodeNamedString(enc, pend, &av_pageUrl, &r.Link.pageUrl) if enc == nil { return 0 @@ -860,8 +808,6 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { } if r.m_fEncoding != 0.0 || r.m_bSendEncoding != 0 { - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeNamedNumber((*byte)( - //unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), &av_objectEncoding, r.m_fEncoding))) enc = C_AMF_EncodeNamedNumber(enc, pend, &av_objectEncoding, float64(r.m_fEncoding)) if enc == nil { return 0 @@ -882,14 +828,10 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { /* add auth string */ if r.Link.auth.av_len != 0 { - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeBoolean((*byte)( - // unsafe.Pointer(enc)), (*byte)(unsafe.Pointer(pend)), r.Link.lFlags&RTMP_LF_AUTH))) enc = C_AMF_EncodeBoolean(enc, pend, int(r.Link.lFlags&RTMP_LF_AUTH)) if enc == nil { return 0 } - //enc = (*byte)(unsafe.Pointer(C.AMF_EncodeString((*byte)(unsafe.Pointer(enc)), - //(*byte)(unsafe.Pointer(pend)), &r.Link.auth))) enc = C_AMF_EncodeString(enc, (*byte)(pend), &r.Link.auth) if enc == nil { return 0 @@ -898,10 +840,6 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { if r.Link.extras.o_num != 0 { for i := 0; i < int(r.Link.extras.o_num); i++ { - //enc = (*byte)(unsafe.Pointer(C.AMFProp_Encode((*C_AMFObjectProperty)( - //incPtr(unsafe.Pointer(&r.Link.extras.o_props), int(unsafe.Sizeof( - //r.Link.extras.o_props)), i)), (*byte)(unsafe.Pointer(enc)), (*byte)( - //unsafe.Pointer(pend))))) enc = C_AMF_PropEncode((*C_AMFObjectProperty)(incPtr(unsafe.Pointer( &r.Link.extras.o_props), int(unsafe.Sizeof(r.Link.extras.o_props)), i)), enc, pend) @@ -915,7 +853,6 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { packet.m_nBodySize = uint32(uintptr(decBytePtr(unsafe.Pointer(enc), int(uintptr(unsafe.Pointer(packet.m_body)))))) - //return int(C.RTMP_SendPacket(r, &packet, 1)) return C_RTMP_SendPacket(r, &packet, 1) } @@ -1114,11 +1051,9 @@ func C_SendBytesReceived(r *C_RTMP) int { packet.m_nBodySize = 4 C_AMF_EncodeInt32((*byte)(unsafe.Pointer(packet.m_body)), pend, int32(r.m_nBytesIn)) - // C.AMF_EncodeInt32(packet.m_body, (*byte)(unsafe.Pointer(pend)), r.m_nBytesIn) r.m_nBytesInSent = r.m_nBytesIn - //return int(C.RTMP_SendPacket(r, &packet, 0)) return C_RTMP_SendPacket(r, &packet, 0) } @@ -1178,8 +1113,6 @@ func C_AV_erase(vals *C_RTMP_METHOD, num *int32, i, freeit int32) { // rtmp.c +2414 func C_AV_queue(vals **C_RTMP_METHOD, num *int32, av *C_AVal, txn int32) { if (*num & 0x0f) == 0 { - //*vals = (*C_RTMP_METHOD)(realloc(unsafe.Pointer(*vals), uint32((*num+16)* - //int32(unsafe.Sizeof(*(*vals)))))) *vals = (*C_RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)* int32(unsafe.Sizeof(*(*vals)))))) } @@ -1213,7 +1146,6 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) int32 { return 0 } nRes = C_AMF_Decode(&obj, body, int32(nBodySize), 0) - //nRes = int32(C.AMF_Decode(&obj, (*byte)(unsafe.Pointer(body)), int32(nBodySize), 0)) if nRes < 0 { // TODO use new logger here //RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__); @@ -1238,7 +1170,6 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) int32 { int(unsafe.Sizeof(*r.m_methodCalls))))).num) == txn { methodInvoked = (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), int(unsafe.Sizeof(*r.m_methodCalls))))).name - //C.AV_erase(r.m_methodCalls, &r.m_numCalls, int32(i), 0) C_AV_erase(r.m_methodCalls, (*int32)(&r.m_numCalls), int32(i), 0) break } @@ -1332,7 +1263,6 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) int32 { for i = 0; i < int32(r.m_numCalls); i++ { if C_AVMATCH(&(*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), int(unsafe.Sizeof(*r.m_methodCalls))))).name, &av_publish) != 0 { - //C.AV_erase(r.m_methodCalls, &r.m_numCalls, int32(i), 1) C_AV_erase(r.m_methodCalls, (*int32)(&r.m_numCalls), int32(i), 1) break } @@ -1375,7 +1305,6 @@ func C_HandleChangeChunkSize(r *C_RTMP, packet *C_RTMPPacket) { // rtmp.c +3508 func C_HandlServerBW(r *C_RTMP, packet *C_RTMPPacket) { r.m_nServerBW = int32(C_AMF_DecodeInt32((*byte)(unsafe.Pointer(packet.m_body)))) - //r.m_nServerBW = int32(C.AMF_DecodeInt32((*byte)(unsafe.Pointer(packet.m_body)))) // TODO use new logger here // RTMP_Log(RTMP_LOGDEBUG, "%s: server BW = %d", __FUNCTION__, r.m_nServerBW); } @@ -1465,14 +1394,10 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if packet.m_nChannel >= r.m_channelsAllocatedIn { var n int32 = int32(packet.m_nChannel + 10) - //timestamp := (*int32)(realloc(unsafe.Pointer(r.m_channelTimestamp), - //uint32(int32(unsafe.Sizeof(n))*n))) timestamp := (*int32)(C.realloc(unsafe.Pointer(r.m_channelTimestamp), C.size_t(int32(unsafe.Sizeof(n))*n))) var packetPtr *C_RTMPPacket - //packets := (**C_RTMPPacket)(realloc(unsafe.Pointer(r.m_vecChannelsIn), - //uint32(int32(unsafe.Sizeof(packetPtr))*n))) packets := (**C_RTMPPacket)(C.realloc(unsafe.Pointer(r.m_vecChannelsIn), C.size_t(int32(unsafe.Sizeof(packetPtr))*n))) if timestamp == nil { @@ -1540,8 +1465,6 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { // TODO: port this packet.m_nInfoField2 = int32(C_DecodeInt32LE((*byte)(incBytePtr( unsafe.Pointer(header), 7)))) - //packet.m_nInfoField2 = int32(C.DecodeInt32LE((*byte)(incBytePtr( - //unsafe.Pointer(header), 7)))) } } } @@ -1559,8 +1482,6 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { // TODO: port this packet.m_nTimeStamp = uint32(C_AMF_DecodeInt32((*byte)(incBytePtr( unsafe.Pointer(header), int(nSize))))) - //packet.m_nTimeStamp = uint32(C.AMF_DecodeInt32((*byte)(incBytePtr( - //unsafe.Pointer(header), int(nSize))))) hSize += 4 } @@ -1643,9 +1564,6 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { // rtmp.c +3744 func C_HandShake(r *C_RTMP, FP9HandShake int32) int { var bMatch int - //uptime := uint32(0) - //suptime := uint32(0) - //typ := byte(0) var uptime, suptime uint32 var typ byte //clientbuf := make([]byte, RTMP_SIG_SIZE+1) @@ -1657,7 +1575,6 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) int { clientbuf[0] = 0x03 // not encrypted // TODO: port rtmp_getTime - //uptime = inet.Htonl(uint32(C.RTMP_GetTime())) uptime = inet.Htonl(uint32(C_RTMP_GetTime())) memmove(unsafe.Pointer(clientsig), unsafe.Pointer(&uptime), 4) @@ -1732,8 +1649,6 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { n := int(packet.m_nChannel + 10) packets = C.realloc(unsafe.Pointer(r.m_vecChannelsOut), C.size_t( unsafe.Sizeof(packet)*uintptr(n))) - //packets = realloc(unsafe.Pointer(r.m_vecChannelsOut), - //uint32(unsafe.Sizeof(packet)*uintptr(n))) if uintptr(packets) == uintptr(0) { //C.free(unsafe.Pointer(r.m_vecChannelsOut)) @@ -1747,9 +1662,6 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { int(unsafe.Sizeof(packet)))), 0, int(unsafe.Sizeof(packet)* uintptr(n-int(r.m_channelsAllocatedOut)))) - //memset((*byte)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int( - // r.m_channelsAllocatedOut), int(unsafe.Sizeof(packet)))), 0, int( - // unsafe.Sizeof(packet)*uintptr(n-int(r.m_channelsAllocatedOut)))) r.m_channelsAllocatedOut = int32(n) } prevPacket = *(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), @@ -1853,8 +1765,6 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { // TODO: port this hptr = incBytePtr(hptr, int(C_EncodeInt32LE((*byte)(hptr), int32(packet.m_nInfoField2)))) - //hptr = incBytePtr(hptr, int(C.EncodeInt32LE((*byte)(hptr), - //int32(packet.m_nInfoField2)))) } if t >= 0xffffff { @@ -1877,7 +1787,6 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { } if tbuf != nil { - //memmove(toff, header, uintptr(nChunkSize + hSize)) copy((*[_Gi]byte)(toff)[:nChunkSize+hSize], (*[_Gi]byte)(header)[:nChunkSize+hSize]) toff = incBytePtr(toff, nChunkSize+hSize) } else { @@ -1943,7 +1852,6 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { var method C_AVal var ptr unsafe.Pointer ptr = incBytePtr(unsafe.Pointer(packet.m_body), 1) - //C.AMF_DecodeString((*byte)(ptr), &method) C_AMF_DecodeString((*byte)(ptr), &method) if debugMode { @@ -1953,12 +1861,9 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { if queue != 0 { var txn int ptr = incBytePtr(ptr, 3+int(method.av_len)) - //txn = int(C.AMF_DecodeNumber((*byte)(ptr))) txn = int(C_AMF_DecodeNumber((*byte)(ptr))) C_AV_queue(&r.m_methodCalls, (*int32)(unsafe.Pointer(&r.m_numCalls)), &method, int32(txn)) - //C.AV_queue(&r.m_methodCalls, (*int32)(unsafe.Pointer(&r.m_numCalls)), &method, - //int32(txn)) } } @@ -2182,7 +2087,6 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int { // TODO: Port this ret = C_RTMP_SendPacket(r, pkt, 0) // TODO: Port this - //C.RTMPPacket_Free(pkt) C_RTMPPacket_Free(pkt) pkt.m_nBytesRead = 0 if ret == 0 { @@ -2197,13 +2101,6 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int { return size } -/* -func realloc(ptr unsafe.Pointer, newSize uint32) unsafe.Pointer { - return unsafe.Pointer(crt.Xrealloc(crt.TLS(uintptr(unsafe.Pointer(nil))), - uintptr(ptr), uint32(newSize))) -} -*/ - func memmove(to, from unsafe.Pointer, n uintptr) { if to != nil && from != nil && n != 0 { copy((*[_Gi]byte)(to)[:n], (*[_Gi]byte)(from)[:n]) From b7c148fd93b0f60ce55c9d80b8d65201b5b03d80 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 11:11:29 +0930 Subject: [PATCH 02/24] rtmp: simplify body pointer setup --- rtmp/rtmp.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index d656fb3a..3c0afeb2 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -733,8 +733,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { packet.m_nTimeStamp = 0 packet.m_nInfoField2 = 0 packet.m_hasAbsTimestamp = 0 - packet.m_body = (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), - RTMP_MAX_HEADER_SIZE)) + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) @@ -871,8 +870,7 @@ func C_RTMP_SendCreateStream(r *C_RTMP) int32 { packet.m_nTimeStamp = 0 packet.m_nInfoField2 = 0 packet.m_hasAbsTimestamp = 0 - packet.m_body = (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), - int(RTMP_MAX_HEADER_SIZE))) + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(enc, pend, &av_createStream) @@ -902,8 +900,7 @@ func C_SendReleaseStream(r *C_RTMP) int32 { packet.m_nTimeStamp = 0 packet.m_nInfoField2 = 0 packet.m_hasAbsTimestamp = 0 - packet.m_body = (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), - int(RTMP_MAX_HEADER_SIZE))) + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(enc, pend, &av_releaseStream) @@ -936,8 +933,7 @@ func C_SendFCPublish(r *C_RTMP) int32 { packet.m_nTimeStamp = 0 packet.m_nInfoField2 = 0 packet.m_hasAbsTimestamp = 0 - packet.m_body = (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), - int(RTMP_MAX_HEADER_SIZE))) + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(enc, pend, &av_FCPublish) @@ -1006,8 +1002,7 @@ func C_SendPublish(r *C_RTMP) int32 { packet.m_nTimeStamp = 0 packet.m_nInfoField2 = int32(r.m_stream_id) packet.m_hasAbsTimestamp = 0 - packet.m_body = (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), - int(RTMP_MAX_HEADER_SIZE))) + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(enc, pend, &av_publish) @@ -1045,8 +1040,7 @@ func C_SendBytesReceived(r *C_RTMP) int { packet.m_nTimeStamp = 0 packet.m_nInfoField2 = 0 packet.m_hasAbsTimestamp = 0 - packet.m_body = (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), - RTMP_MAX_HEADER_SIZE)) + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] packet.m_nBodySize = 4 @@ -1072,8 +1066,7 @@ func C_SendCheckBW(r *C_RTMP) int32 { packet.m_nTimeStamp = 0 packet.m_nInfoField2 = 0 packet.m_hasAbsTimestamp = 0 - packet.m_body = (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), - int(RTMP_MAX_HEADER_SIZE))) + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(enc, pend, &av__checkbw) From 8674b23b28588a8068818cb5807ffffebd2ecef0 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 11:26:52 +0930 Subject: [PATCH 03/24] rtmp: port C_SendFCUnpublish --- rtmp/rtmp.go | 54 ++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 3c0afeb2..7442e55a 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -114,6 +114,7 @@ var ( av_playlist_ready = AVC("playlist_ready") av_set_playlist = AVC("set_playlist") av_FCPublish = AVC("FCPublish") + av_FCUnpublish = AVC("FCUnpublish") av_live = AVC("live") ) @@ -954,37 +955,36 @@ func C_SendFCPublish(r *C_RTMP) int32 { // int SendFCUnpublish(RTMP *r); // rtmp.c +1875 func C_SendFCUnpublish(r *C_RTMP) int32 { - // TODO finish porting - /* - var packet C_RTMPPacket - var pbuf [1024]byte - var pend []byte = pbuf[1024:] - var enc []byte + var packet C_RTMPPacket + var pbuf [1024]byte + var pend *byte = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&pbuf[0])) + + unsafe.Sizeof(pbuf))) + var enc *byte - packet.m_nChannel = 0x03 - packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM - packet.m_packetType = RTMP_PACKET_TYPE_INVOKE - packet.m_nTimeStamp = 0 - packet.m_nInfoField2 = 0 - packet.m_hasAbsTimestamp = 0 - packet.m_body = pbuf + RTMP_MAX_HEADER_SIZE + packet.m_nChannel = 0x03 /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE + packet.m_nTimeStamp = 0 + packet.m_nInfoField2 = 0 + packet.m_hasAbsTimestamp = 0 + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] - // NOTE use of unsafe pointer will be remove here when packet.m_nBody becomes []byte - enc = []byte(unsafe.Pointer(packet.m_nBody)) - enc = C_AMF_EncodeString((*byte)(unsafe.Pointer(&enc[0])), - (*byte)(unsafe.Pointer(&pend[0])), &av_FCUnpublish) - r.m_numInvokes++ - enc = C_AMF_EncodeNumber(enc, pend, r->m_numInvokes); - *enc++ = AMF_NULL; - enc = AMF_EncodeString(enc, pend, &r->Link.playpath); - if (!enc) - return FALSE; + enc = (*byte)(unsafe.Pointer(packet.m_body)) + enc = C_AMF_EncodeString(enc, pend, &av_FCUnpublish) + r.m_numInvokes++ + enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + *enc = AMF_NULL + enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) + enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) - packet.m_nBodySize = enc - packet.m_body; + if enc == nil { + return 0 + } - return RTMP_SendPacket(r, &packet, FALSE); - */ - return 0 + packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( + unsafe.Pointer(packet.m_body))) + + return int32(C_RTMP_SendPacket(r, &packet, 0)) } // int SendPublish(RTMP* r); From f3a0968fb4f4a7da3dc21b1ab7a347c1263820c9 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 11:45:48 +0930 Subject: [PATCH 04/24] rtmp: port C_CloseInternal --- rtmp/rtmp.go | 179 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 108 insertions(+), 71 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 7442e55a..094e317f 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -35,6 +35,7 @@ package rtmp /* #include +#include #include #include #include @@ -116,6 +117,7 @@ var ( av_FCPublish = AVC("FCPublish") av_FCUnpublish = AVC("FCUnpublish") av_live = AVC("live") + av_deleteStream = AVC("deleteStream") ) var RTMPT_cmds = []string{ @@ -1027,6 +1029,38 @@ func C_SendPublish(r *C_RTMP) int32 { return int32(C_RTMP_SendPacket(r, &packet, 1)) } +// int +// SendDeleteStream(RTMP *r, double dStreamId) +// rtmp.c +1942 +func C_SendDeleteStream(r *C_RTMP, dStreamId float64) int32 { + var packet C_RTMPPacket + var pbuf [256]byte + pend := (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), 256)) + var enc *byte + + packet.m_nChannel = 0x03 /* control channel (invoke) */ + packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM + packet.m_packetType = RTMP_PACKET_TYPE_INVOKE + packet.m_nTimeStamp = 0 + packet.m_nInfoField2 = 0 + packet.m_hasAbsTimestamp = 0 + packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] + + enc = (*byte)(unsafe.Pointer(packet.m_body)) + enc = C_AMF_EncodeString(enc, pend, &av_deleteStream) + r.m_numInvokes++ + enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + *enc = AMF_NULL + enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) + enc = C_AMF_EncodeNumber(enc, pend, dStreamId) + + packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( + unsafe.Pointer(packet.m_body))) + + /* no response expected */ + return int32(C_RTMP_SendPacket(r, &packet, 0)) +} + // int SendBytesReceived(RTMP* r); // rtmp.c +2080 func C_SendBytesReceived(r *C_RTMP) int { @@ -1883,89 +1917,82 @@ func C_RTMP_Close(r *C_RTMP) { // static void CloseInternal(RTMP *r, int reconnect); // rtmp.c +4175 func C_CloseInternal(r *C_RTMP, reconnect int32) { - // TODO: port SendFCUnpublish - // TODO: port SendDeleteStream - // TODO: port RTMPSockBuf_Close - // TODO: port AV_Clear - /* - var i int32 + var i int32 - if C_RTMP_IsConnected(r) != 0 { - if r.m_stream_id > 0 { - i = int32(r.m_stream_id) - if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { - C.SendFCUnpublish(r) - } - C.SendDeleteStream(r, float64(i)) + if C_RTMP_IsConnected(r) != 0 { + if r.m_stream_id > 0 { + i = int32(r.m_stream_id) + if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { + C_SendFCUnpublish(r) } - C.RTMPSockBuf_Close(&r.m_sb) + C_SendDeleteStream(r, float64(i)) } + C_RTMPSockBuf_Close(&r.m_sb) + } - r.m_stream_id = -1 - r.m_sb.sb_socket = -1 - r.m_nBWCheckCounter = 0 - r.m_nBytesIn = 0 - r.m_nBytesInSent = 0 + 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 != 0 { - //C.free(unsafe.Pointer(r.m_read.buf)) - r.m_read.buf = nil + if r.m_read.flags&RTMP_READ_HEADER != 0 { + //C.free(unsafe.Pointer(r.m_read.buf)) + r.m_read.buf = nil + } + + r.m_read.dataType = 0 + r.m_read.flags = 0 + r.m_read.status = 0 + r.m_read.nResumeTS = 0 + r.m_read.nIgnoredFrameCounter = 0 + r.m_read.nIgnoredFlvFrameCounter = 0 + + r.m_write.m_nBytesRead = 0 + C_RTMPPacket_Free(&r.m_write) + + // NOTE: C frees - not using in our case + for i := 0; i < int(r.m_channelsAllocatedIn); i++ { + if *(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), i, + int(unsafe.Sizeof(&r.m_write)))) != nil { + + //C.RTMPPacket_Free(*(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), i, + //int(unsafe.Sizeof(&r.m_write))))) + + //C.free(unsafe.Pointer(*(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), + //i, int(unsafe.Sizeof(&r.m_write)))))) + + *(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), + i, int(unsafe.Sizeof(&r.m_write)))) = nil } + } - r.m_read.dataType = 0 - r.m_read.flags = 0 - r.m_read.status = 0 - r.m_read.nResumeTS = 0 - r.m_read.nIgnoredFrameCounter = 0 - r.m_read.nIgnoredFlvFrameCounter = 0 + //C.free(unsafe.Pointer(r.m_vecChannelsOut)) + r.m_vecChannelsOut = nil + r.m_channelsAllocatedOut = 0 + r.m_methodCalls = nil //C_AV_clear(r.m_methodCalls, r.m_numCalls) - r.m_write.m_nBytesRead = 0 - C.RTMPPacket_Free(&r.m_write) + r.m_methodCalls = nil + r.m_numCalls = 0 + r.m_numInvokes = 0 - // NOTE: C frees - not using in our case - for i := 0; i < int(r.m_channelsAllocatedIn); i++ { - if *(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), i, - int(unsafe.Sizeof(&r.m_write)))) != nil { + r.m_bPlaying = 0 + r.m_sb.sb_size = 0 - //C.RTMPPacket_Free(*(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), i, - //int(unsafe.Sizeof(&r.m_write))))) + r.m_msgCounter = 0 + r.m_resplen = 0 + r.m_unackd = 0 - //C.free(unsafe.Pointer(*(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), - //i, int(unsafe.Sizeof(&r.m_write)))))) + if ((r.Link.lFlags & RTMP_LF_FTCU) != 0) && (reconnect == 0) { + //C.free(unsafe.Pointer(r.Link.app.av_val)) + r.Link.app.av_val = nil + r.Link.lFlags ^= RTMP_LF_FAPU + } - *(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), - i, int(unsafe.Sizeof(&r.m_write)))) = nil - } - } - - - //C.free(unsafe.Pointer(r.m_vecChannelsOut)) - r.m_vecChannelsOut = nil - r.m_channelsAllocatedOut = 0 - C.AV_clear(r.m_methodCalls, r.m_numCalls) - - r.m_methodCalls = nil - r.m_numCalls = 0 - r.m_numInvokes = 0 - - r.m_bPlaying = C.uchar(0) - r.m_sb.sb_size = 0 - - r.m_msgCounter = 0 - r.m_resplen = 0 - r.m_unackd = 0 - - if ((r.Link.lFlags & RTMP_LF_FTCU) != 0) && (reconnect == 0) { - //C.free(unsafe.Pointer(r.Link.app.av_val)) - r.Link.app.av_val = nil - r.Link.lFlags ^= RTMP_LF_FAPU - } - - if reconnect == 0 { - //C.free(unsafe.Pointer(r.Link.playpath0.av_val)) - r.Link.playpath0.av_val = nil - } - */ + if reconnect == 0 { + //C.free(unsafe.Pointer(r.Link.playpath0.av_val)) + r.Link.playpath0.av_val = nil + } } // int RTMPSockBuf_Fill(RTMPSockBuf* sb); @@ -2003,6 +2030,16 @@ func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf *byte, l int32) int32 { return int32(C.send(C.int(sb.sb_socket), unsafe.Pointer(buf), C.size_t(l), 0)) } +// int +// RTMPSockBuf_Close(RTMPSockBuf *sb) +// rtmp.c +4369 +func C_RTMPSockBuf_Close(sb *C_RTMPSockBuf) int32 { + if sb.sb_socket != -1 { + return int32(C.close(C.int(sb.sb_socket))) + } + return 0 +} + // int RTMP_Write(RTMP* r, const char* buf, int size); // rtmp.c +5136 func C_RTMP_Write(r *C_RTMP, buf []byte) int { From 6c6ed78e8fdab82e8b240bcfdf62b7e739bc17e5 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 11:50:58 +0930 Subject: [PATCH 05/24] rtmp: convert boolish ints to bool --- rtmp/rtmp.go | 259 +++++++++++++++++++++---------------------- rtmp/rtmp_headers.go | 2 +- 2 files changed, 126 insertions(+), 135 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 094e317f..d2ffe248 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -147,7 +147,7 @@ func startSession(rtmp *C_RTMP, u string, timeout uint32) (*C_RTMP, error) { rtmp = C_RTMP_Alloc() C_RTMP_Init(rtmp) rtmp.Link.timeout = connect_timeout - if C_RTMP_SetupURL(rtmp, u) == 0 { + if !C_RTMP_SetupURL(rtmp, u) { C_RTMP_Close(rtmp) //C.RTMP_Free(rtmp) return nil, errors.New("rtmp startSession: Failed to setup URL!") @@ -155,14 +155,14 @@ func startSession(rtmp *C_RTMP, u string, timeout uint32) (*C_RTMP, error) { C_RTMP_EnableWrite(rtmp) C_RTMP_SetBufferMS(rtmp, 3600*1000) - if C_RTMP_Connect(rtmp, nil) == 0 { + if !C_RTMP_Connect(rtmp, nil) { C_RTMP_Close(rtmp) //C.RTMP_Free(rtmp) return nil, errors.New("rtmp startSession: Failed to connect!") } // TODO: port this - if C_RTMP_ConnectStream(rtmp, 0) == 0 { + if !C_RTMP_ConnectStream(rtmp, 0) { C_RTMP_Close(rtmp) //C.RTMP_Free(rtmp) return nil, errors.New("rtmp startSession: Failed to connect stream!") @@ -189,7 +189,7 @@ func C_RTMP_GetTime() int32 { // int RTMPPacket_Alloc(RTMPPacket* p, uint32_t nSize); // rtmp.c +189 -func C_RTMPPacket_Alloc(p *C_RTMPPacket, nSize uint32) int { +func C_RTMPPacket_Alloc(p *C_RTMPPacket, nSize uint32) (ok bool) { var ptr *byte // TODO: port C.SIZE_MAX // also work out how to deal with the overfloat @@ -201,11 +201,11 @@ func C_RTMPPacket_Alloc(p *C_RTMPPacket, nSize uint32) int { ptr = (*byte)(calloc(1, uintptr(nSize+RTMP_MAX_HEADER_SIZE))) if ptr == nil { - return 0 + return false } p.m_body = (*byte)(incBytePtr(unsafe.Pointer(ptr), RTMP_MAX_HEADER_SIZE)) p.m_nBytesRead = 0 - return 1 + return true } // void RTMPPacket_Free(RTMPPacket* p); @@ -294,20 +294,20 @@ func C_SocksSetup(r *C_RTMP, sockshost *C_AVal) { // int RTMP_SetupURL(RTMP *r, char* url); // rtmp.c +757 // NOTE: code dealing with rtmp over http has been disregarded -func C_RTMP_SetupURL(r *C_RTMP, u string) int32 { +func C_RTMP_SetupURL(r *C_RTMP, u string) (ok bool) { url := goStrToCStr(u) - var ret, length int32 + var length int32 var port uint32 port = 0 length = strlen(url) // TODO: port this - ret = int32(C_RTMP_ParseURL((*byte)(unsafe.Pointer(url)), (*int32)( + ret := int32(C_RTMP_ParseURL((*byte)(unsafe.Pointer(url)), (*int32)( unsafe.Pointer(&r.Link.protocol)), &r.Link.hostname, (*uint32)( unsafe.Pointer(&port)), &r.Link.playpath0, &r.Link.app)) if ret == 0 { - return ret + return false } r.Link.port = uint16(port) @@ -358,12 +358,12 @@ func C_RTMP_SetupURL(r *C_RTMP, u string) int32 { r.Link.port = 1935 } } - return 1 + return true } // int add_addr_info(struct sockaddr_in *service, AVal *host, int port) // rtmp.c +869 -func C_add_addr_info(service *C.sockaddr_in, host *C_AVal, port C.int) int32 { +func C_add_addr_info(service *C.sockaddr_in, host *C_AVal, port C.int) (ok bool) { var hostname *byte if (*[1 << 20]byte)(unsafe.Pointer(host.av_val))[host.av_len] != 0 { name := make([]byte, host.av_len+1) @@ -379,18 +379,18 @@ func C_add_addr_info(service *C.sockaddr_in, host *C_AVal, port C.int) int32 { host := C.gethostbyname((*C.char)(unsafe.Pointer(hostname))) if host == nil || *host.h_addr_list == nil { //RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname) - return 0 + return false } service.sin_addr = *(*C.struct_in_addr)(unsafe.Pointer(*host.h_addr_list)) } service.sin_port = C.ushort(inet.Htons(uint16(port))) - return 1 + return true } // int RTMP_Connect0(RTMP *r, struct sockaddr* service); // rtmp.c +906 -func C_RTMP_Connect0(r *C_RTMP, service *C.sockaddr) int { +func C_RTMP_Connect0(r *C_RTMP, service *C.sockaddr) (ok bool) { on := 1 r.m_sb.sb_timedout = 0 r.m_pausing = 0 @@ -408,14 +408,14 @@ func C_RTMP_Connect0(r *C_RTMP, service *C.sockaddr) int { log.Println("C_RTMP_Connect0: socks negotiation.") } - if C_SocksNegotiate(r) == 0 { + if !C_SocksNegotiate(r) { log.Println("C_RTMP_Connect0: SOCKS negotiation failed.") - return 0 + return false } } } else { log.Println("C_RTMP_Connect0: failed to create socket.") - return 0 + return false } { @@ -431,37 +431,37 @@ func C_RTMP_Connect0(r *C_RTMP, service *C.sockaddr) int { C.setsockopt(C.int(r.m_sb.sb_socket), C.IPPROTO_TCP, C.TCP_NODELAY, unsafe.Pointer(&on), C.socklen_t(unsafe.Sizeof(on))) - return 1 + return true } // int RTMP_Connect1(RTMP* r, RTMPPacket* cp); // rtmp.c +978 -func C_RTMP_Connect1(r *C_RTMP, cp *C_RTMPPacket) int { +func C_RTMP_Connect1(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { if debugMode { log.Println("... connected, handshaking...") } - if C_HandShake(r, 1) == 0 { + if !C_HandShake(r, 1) { log.Println("C_RTMP_Connect1: handshake failed!") - return 0 + return false } if debugMode { log.Println("... handshaked...") } - if C_SendConnectPacket(r, cp) == 0 { + if !C_SendConnectPacket(r, cp) { log.Println("RTMP connect failed!") - return 0 + return false } - return 1 + return true } // int RTMP_Connect(RTMP *r, RTMPPacket* cp); // rtmp.c +1032 -func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) int { +func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { // TODO: port this var service C.sockaddr_in if r.Link.hostname.av_len == 0 { - return 0 + return false } memset((*byte)(unsafe.Pointer(&service)), 0, int(unsafe.Sizeof(service))) @@ -469,28 +469,27 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) int { service.sin_family = C.AF_INET if r.Link.socksport != 0 { - if C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.sockshost)), C.int(r.Link.socksport)) == 0 { - return 0 + if !C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.sockshost)), C.int(r.Link.socksport)) { + return false } } else { // connect directly - if C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.hostname)), - C.int(r.Link.port)) == 0 { - return 0 + if !C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.hostname)), C.int(r.Link.port)) { + return false } } - if C_RTMP_Connect0(r, (*C.sockaddr)(unsafe.Pointer(&service))) == 0 { - return 0 + if !C_RTMP_Connect0(r, (*C.sockaddr)(unsafe.Pointer(&service))) { + return false } r.m_bSendCounter = 1 - return int(C_RTMP_Connect1(r, cp)) + return C_RTMP_Connect1(r, cp) } // int SocksNegotiate(RTMP* r); // rtmp.c +1062 -func C_SocksNegotiate(r *C_RTMP) int { +func C_SocksNegotiate(r *C_RTMP) (ok bool) { var addr int32 var service C.sockaddr_in @@ -512,22 +511,22 @@ func C_SocksNegotiate(r *C_RTMP) int { C_WriteN(r, unsafe.Pointer(&packet[0]), int(unsafe.Sizeof(packet))) if C_ReadN(r, &packet[0], 8) != 8 { - return 0 + return false } if packet[0] == 0 && packet[1] == 90 { - return 1 + return true } else { // TODO: use new logger here log.Println("C_SocksNegotitate: SOCKS returned error code!") - return 0 + return false } } } // int RTMP_ConnectStream(RTMP* r, int seekTime); // rtmp.c +1099 -func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) int { +func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) (playing bool) { var packet C_RTMPPacket memset((*byte)(unsafe.Pointer(&packet)), 0, int(unsafe.Sizeof(packet))) @@ -538,9 +537,7 @@ func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) int { r.m_mediaChannel = 0 // TODO: read packet - for r.m_bPlaying == 0 && C_RTMP_IsConnected(r) != 0 && - C_RTMP_ReadPacket(r, &packet) != 0 { - + for !r.m_bPlaying && C_RTMP_IsConnected(r) != 0 && C_RTMP_ReadPacket(r, &packet) { // TODO: port is ready if C_RTMPPacket_IsReady(&packet) != 0 { if packet.m_nBodySize == 0 { @@ -559,14 +556,14 @@ func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) int { C_RTMPPacket_Free(&packet) } } - return int(r.m_bPlaying) + return r.m_bPlaying } // int RTMP_ClientPacket() // rtmp.c +1226 // NOTE cases have been commented out that are not currently used by AusOcean func C_RTMP_ClientPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { - var bHasMediaPacket int32 = 0 + var bHasMediaPacket int32 switch packet.m_packetType { case RTMP_PACKET_TYPE_CHUNK_SIZE: @@ -609,7 +606,8 @@ func C_RTMP_ClientPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { // TODO use new logger here //RTMP_Log(RTMP_LOGDEBUG, "%s, received: invoke %u bytes", __FUNCTION__,packet.m_nBodySize); - if C_HandleInvoke(r, (*byte)(unsafe.Pointer(packet.m_body)), uint32(packet.m_nBodySize)) == 1 { + if C_HandleInvoke(r, (*byte)(unsafe.Pointer(packet.m_body)), uint32(packet.m_nBodySize)) { + // This will never happen with the methods we implement. log.Println("HasMediaPacket") bHasMediaPacket = 2 } @@ -664,7 +662,7 @@ func C_ReadN(r *C_RTMP, buffer *byte, n int) int { r.m_nBytesIn += int32(nRead) if r.m_bSendCounter != 0 && r.m_nBytesIn > (r.m_nBytesInSent+ r.m_nClientBW/10) { - if C_SendBytesReceived(r) == 0 { + if !C_SendBytesReceived(r) { return 0 } } @@ -685,7 +683,7 @@ func C_ReadN(r *C_RTMP, buffer *byte, n int) int { // int WriteN(RTMP* r, const char* buffer, int n); // rtmp.c +1502 -func C_WriteN(r *C_RTMP, buffer unsafe.Pointer, n int) int { +func C_WriteN(r *C_RTMP, buffer unsafe.Pointer, n int) (ok bool) { ptr := buffer for n > 0 { var nBytes int @@ -711,15 +709,13 @@ func C_WriteN(r *C_RTMP, buffer unsafe.Pointer, n int) int { ptr = incBytePtr(ptr, nBytes) } - if n == 0 { - return 1 - } - return 0 + // !ok here is equivalent to io.ErrShortWrite. + return n == 0 } // int SendConnectPacket(RTMP* r, RTMPPacket* cp); // rtmp.c +1579 -func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { +func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { var packet C_RTMPPacket var pbuf [4096]byte pend := (*byte)(unsafe.Pointer(incBytePtr(unsafe.Pointer(&pbuf[0]), @@ -751,60 +747,60 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { enc = C_AMF_EncodeNamedString(enc, pend, &av_app, &r.Link.app) if enc == nil { - return 0 + return false } if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { enc = C_AMF_EncodeNamedString(enc, pend, &av_type, &av_nonprivate) if enc == nil { - return 0 + return false } } if r.Link.flashVer.av_len != 0 { enc = C_AMF_EncodeNamedString(enc, pend, &av_flashVer, &r.Link.flashVer) if enc == nil { - return 0 + return false } } if r.Link.swfUrl.av_len != 0 { enc = C_AMF_EncodeNamedString(enc, pend, &av_swfUrl, &r.Link.swfUrl) if enc == nil { - return 0 + return false } } if r.Link.tcUrl.av_len != 0 { enc = C_AMF_EncodeNamedString(enc, pend, &av_tcUrl, &r.Link.tcUrl) if enc == nil { - return 0 + return false } } if r.Link.protocol&RTMP_FEATURE_WRITE == 0 { enc = C_AMF_EncodeNamedBoolean(enc, pend, &av_fpad, 0) if enc == nil { - return 0 + return false } enc = C_AMF_EncodeNamedNumber(enc, pend, &av_capabilities, 15.0) if enc == nil { - return 0 + return false } enc = C_AMF_EncodeNamedNumber(enc, pend, &av_audioCodecs, float64(r.m_fAudioCodecs)) if enc == nil { - return 0 + return false } enc = C_AMF_EncodeNamedNumber(enc, pend, &av_videoCodecs, float64(r.m_fVideoCodecs)) if enc == nil { - return 0 + return false } enc = C_AMF_EncodeNamedNumber(enc, pend, &av_videoFunction, 1.0) if enc == nil { - return 0 + return false } if r.Link.pageUrl.av_len != 0 { enc = C_AMF_EncodeNamedString(enc, pend, &av_pageUrl, &r.Link.pageUrl) if enc == nil { - return 0 + return false } } } @@ -812,13 +808,13 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { if r.m_fEncoding != 0.0 || r.m_bSendEncoding != 0 { enc = C_AMF_EncodeNamedNumber(enc, pend, &av_objectEncoding, float64(r.m_fEncoding)) if enc == nil { - return 0 + return false } } if int(uintptr(incBytePtr(unsafe.Pointer(enc), 3))) >= int(uintptr( unsafe.Pointer(pend))) { - return 0 + return false } (*[_Gi]byte)(unsafe.Pointer(enc))[0] = 0 @@ -832,11 +828,11 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { if r.Link.auth.av_len != 0 { enc = C_AMF_EncodeBoolean(enc, pend, int(r.Link.lFlags&RTMP_LF_AUTH)) if enc == nil { - return 0 + return false } enc = C_AMF_EncodeString(enc, (*byte)(pend), &r.Link.auth) if enc == nil { - return 0 + return false } } @@ -847,7 +843,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { enc, pend) if enc == nil { - return 0 + return false } } } @@ -860,7 +856,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) int { // int RTMP_SendCreateStream(RTMP* r); // rtmp.c +1725 -func C_RTMP_SendCreateStream(r *C_RTMP) int32 { +func C_RTMP_SendCreateStream(r *C_RTMP) (ok bool) { var packet C_RTMPPacket var pbuf [256]byte var pend *byte = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&pbuf[0])) + @@ -885,12 +881,12 @@ func C_RTMP_SendCreateStream(r *C_RTMP) int32 { packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( unsafe.Pointer(packet.m_body))) - return int32(C_RTMP_SendPacket(r, &packet, 1)) + return C_RTMP_SendPacket(r, &packet, 1) } // int SendReleaseStream(RTMP* r); // rtmp.c +1816 -func C_SendReleaseStream(r *C_RTMP) int32 { +func C_SendReleaseStream(r *C_RTMP) (ok bool) { var packet C_RTMPPacket var pbuf [1024]byte var pend *byte = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&pbuf[0])) + @@ -913,17 +909,17 @@ func C_SendReleaseStream(r *C_RTMP) int32 { enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) if enc == nil { - return 0 + return false } packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( unsafe.Pointer(packet.m_body))) - return int32(C_RTMP_SendPacket(r, &packet, 0)) + return C_RTMP_SendPacket(r, &packet, 0) } // int SendFCPublish(RTMP* r); // rtmp.c +1846 -func C_SendFCPublish(r *C_RTMP) int32 { +func C_SendFCPublish(r *C_RTMP) (ok bool) { var packet C_RTMPPacket var pbuf [1024]byte var pend *byte = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&pbuf[0])) + @@ -946,17 +942,17 @@ func C_SendFCPublish(r *C_RTMP) int32 { enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) if enc == nil { - return 0 + return false } packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( unsafe.Pointer(packet.m_body))) - return int32(C_RTMP_SendPacket(r, &packet, 0)) + return C_RTMP_SendPacket(r, &packet, 0) } // int SendFCUnpublish(RTMP *r); // rtmp.c +1875 -func C_SendFCUnpublish(r *C_RTMP) int32 { +func C_SendFCUnpublish(r *C_RTMP) (ok bool) { var packet C_RTMPPacket var pbuf [1024]byte var pend *byte = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&pbuf[0])) + @@ -980,18 +976,18 @@ func C_SendFCUnpublish(r *C_RTMP) int32 { enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) if enc == nil { - return 0 + return false } packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( unsafe.Pointer(packet.m_body))) - return int32(C_RTMP_SendPacket(r, &packet, 0)) + return C_RTMP_SendPacket(r, &packet, 0) } // int SendPublish(RTMP* r); // rtmp.c +1908 -func C_SendPublish(r *C_RTMP) int32 { +func C_SendPublish(r *C_RTMP) (ok bool) { var packet C_RTMPPacket var pbuf [1024]byte var pend *byte = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&pbuf[0])) + @@ -1015,24 +1011,24 @@ func C_SendPublish(r *C_RTMP) int32 { enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) if enc == nil { - return 0 + return false } enc = C_AMF_EncodeString(enc, pend, &av_live) if enc == nil { - return 0 + return false } packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( unsafe.Pointer(packet.m_body))) - return int32(C_RTMP_SendPacket(r, &packet, 1)) + return C_RTMP_SendPacket(r, &packet, 1) } // int // SendDeleteStream(RTMP *r, double dStreamId) // rtmp.c +1942 -func C_SendDeleteStream(r *C_RTMP, dStreamId float64) int32 { +func C_SendDeleteStream(r *C_RTMP, dStreamId float64) (ok bool) { var packet C_RTMPPacket var pbuf [256]byte pend := (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), 256)) @@ -1058,12 +1054,12 @@ func C_SendDeleteStream(r *C_RTMP, dStreamId float64) int32 { unsafe.Pointer(packet.m_body))) /* no response expected */ - return int32(C_RTMP_SendPacket(r, &packet, 0)) + return C_RTMP_SendPacket(r, &packet, 0) } // int SendBytesReceived(RTMP* r); // rtmp.c +2080 -func C_SendBytesReceived(r *C_RTMP) int { +func C_SendBytesReceived(r *C_RTMP) (ok bool) { var packet C_RTMPPacket var pbuf [256]byte pend := (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), 256)) @@ -1087,7 +1083,7 @@ func C_SendBytesReceived(r *C_RTMP) int { // int SendCheckBW(RTMP* r); // rtmp.c +2105 -func C_SendCheckBW(r *C_RTMP) int32 { +func C_SendCheckBW(r *C_RTMP) (ok bool) { var packet C_RTMPPacket var pbuf [256]byte var pend *byte = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(&pbuf[0])) + @@ -1112,7 +1108,7 @@ func C_SendCheckBW(r *C_RTMP) int32 { packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( unsafe.Pointer(packet.m_body))) - return int32(C_RTMP_SendPacket(r, &packet, 0)) + return C_RTMP_SendPacket(r, &packet, 0) } // void AV_erase(C_RTMP_METHOD* vals, int* num, int i, int freeit); @@ -1159,24 +1155,23 @@ func C_AV_queue(vals **C_RTMP_METHOD, num *int32, av *C_AVal, txn int32) { // int HandleInvoke(RTMP* r, const char* body, unsigned int nBodySize); // rtmp.c +2912 -func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) int32 { +func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { var obj C_AMFObject var method C_AVal var txn float64 - var ret int32 = 0 var nRes int32 if *body != 0x02 { // TODO use new logger here //RTMP_Log(RTMP_LOGWARNING, "%s, Sanity failed. no string method in invoke packet", //__FUNCTION__); - return 0 + return false } nRes = C_AMF_Decode(&obj, body, int32(nBodySize), 0) if nRes < 0 { // TODO use new logger here //RTMP_Log(RTMP_LOGERROR, "%s, error decoding invoke packet", __FUNCTION__); - return 0 + return false } // NOTE we don't really need this ?? still functions without it @@ -1286,7 +1281,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) int32 { case C_AVMATCH(&code, &av_NetStream_Publish_Start) != 0: var i int32 - r.m_bPlaying = 1 + r.m_bPlaying = true for i = 0; i < int32(r.m_numCalls); i++ { if C_AVMATCH(&(*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), int(unsafe.Sizeof(*r.m_methodCalls))))).name, &av_publish) != 0 { @@ -1314,7 +1309,8 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) int32 { } leave: C_AMF_Reset(&obj) - return ret + // None of the methods we implement will result in a true return. + return ok } // void HandleChangeChunkSize(RTMP* r, const RTMPPacket* packet); @@ -1377,7 +1373,7 @@ func C_EncodeInt32LE(output *byte, nVal int32) int32 { // int RTMP_ReadPacket(RTMP* r, RTMPPacket* packet); // rtmp.c +3550 -func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { +func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) { var hbuf [RTMP_MAX_HEADER_SIZE]uint8 memset((*byte)(&hbuf[0]), 0, RTMP_MAX_HEADER_SIZE) var header *byte @@ -1387,7 +1383,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if C_ReadN(r, (*byte)(&hbuf[0]), 1) == 0 { log.Println("C_RTMP_ReadPacket: failed to read RTMP packet header!") - return 0 + return false } packet.m_headerType = uint8((hbuf[0] & 0xc0) >> 6) @@ -1398,7 +1394,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { case packet.m_nChannel == 0: if C_ReadN(r, (*byte)(&hbuf[1]), 1) != 1 { log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header 2nd byte.") - return 0 + return false } packet.m_nChannel = int32(hbuf[1]) @@ -1409,7 +1405,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if C_ReadN(r, (*byte)(&hbuf[1]), 2) != 2 { log.Println("C_RTMP_ReadPacket: failed to read RTMP packet 3rd byte") - return 0 + return false } tmp = int32((hbuf[2] << 8) + hbuf[1]) @@ -1440,7 +1436,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if timestamp == nil || packets == nil { r.m_channelsAllocatedIn = 0 - return 0 + return false } memset((*byte)(incPtr(unsafe.Pointer(r.m_channelTimestamp), @@ -1471,7 +1467,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if nSize > 0 && C_ReadN(r, header, int(nSize)) != int(nSize) { log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header.") - return 0 + return false } hSize = int32(uintptr(incBytePtr(decBytePtr(unsafe.Pointer(header), int(uintptr( @@ -1504,7 +1500,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if extendedTimestamp != 0 { if C_ReadN(r, (*byte)(incBytePtr(unsafe.Pointer(header), int(nSize))), 4) != 4 { log.Println("RTMPRead_Packet: Failed to read extended timestamp") - return 0 + return false } // TODO: port this packet.m_nTimeStamp = uint32(C_AMF_DecodeInt32((*byte)(incBytePtr( @@ -1514,9 +1510,9 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if packet.m_nBodySize > 0 && packet.m_body == nil { // TODO: port this - if C_RTMPPacket_Alloc(packet, uint32(packet.m_nBodySize)) == 0 { + if !C_RTMPPacket_Alloc(packet, uint32(packet.m_nBodySize)) { log.Println("RTMPRead_Packet: failed to allocate packet") - return 0 + return false } packet.m_headerType = uint8((hbuf[0] & 0xc0) >> 6) } @@ -1540,7 +1536,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { if C_ReadN(r, (*byte)(incBytePtr(unsafe.Pointer(packet.m_body), int(packet.m_nBytesRead))), int(nChunk)) != int(nChunk) { log.Println("C_RTMP_ReadPacket: failed to read RTMP packet body") - return 0 + return false } packet.m_nBytesRead += uint32(nChunk) @@ -1584,12 +1580,12 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) int32 { } else { packet.m_body = nil /* so it won't be erased on free */ } - return 1 + return true } // int HandShake(RTMP* r, int FP9HandShake); // rtmp.c +3744 -func C_HandShake(r *C_RTMP, FP9HandShake int32) int { +func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) { var bMatch int var uptime, suptime uint32 var typ byte @@ -1611,13 +1607,13 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) int { (*[_Gi]byte)(unsafe.Pointer(clientsig))[i] = byte(rand.Intn(256)) } - if C_WriteN(r, unsafe.Pointer(&clientbuf[0]), RTMP_SIG_SIZE+1) == 0 { - return 0 + if !C_WriteN(r, unsafe.Pointer(&clientbuf[0]), RTMP_SIG_SIZE+1) { + return false } //if C.ReadN(r, (*byte)(unsafe.Pointer(&typ)), 1) != 1 { if C_ReadN(r, (*byte)(unsafe.Pointer(&typ)), 1) != 1 { - return 0 + return false } if debugMode { @@ -1629,7 +1625,7 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) int { } if C_ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE { //if C.ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE { - return 0 + return false } // decode server response @@ -1637,13 +1633,13 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) int { suptime = inet.Ntohl(suptime) // 2nd part of handshake - if C_WriteN(r, unsafe.Pointer(&serversig[0]), RTMP_SIG_SIZE) == 0 { - return 0 + if !C_WriteN(r, unsafe.Pointer(&serversig[0]), RTMP_SIG_SIZE) { + return false } if C_ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE { //if C.ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE { - return 0 + return false } // TODO: find golang memcmp @@ -1656,12 +1652,12 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) int { if bMatch == 0 { log.Println("Client signature does not match!") } - return 1 + return true } // int RTMP_SendPacket(RTMP* r, RTMPPacket* packet, int queue); // rtmp.c +3896 -func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { +func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { var prevPacket *C_RTMPPacket last := 0 var nSize, hSize, cSize, nChunkSize int @@ -1681,7 +1677,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { //C.free(unsafe.Pointer(r.m_vecChannelsOut)) r.m_vecChannelsOut = nil r.m_channelsAllocatedOut = 0 - return 0 + return false } r.m_vecChannelsOut = (**C_RTMPPacket)(packets) @@ -1714,7 +1710,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { if packet.m_headerType > 3 { log.Printf("Sanity failed! trying to send header of type: 0x%02x.", packet.m_headerType) - return 0 + return false } nSize = packetSize[int(packet.m_headerType)] @@ -1807,8 +1803,6 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { } for (nSize + hSize) != 0 { - var wrote int - if nSize < nChunkSize { nChunkSize = nSize } @@ -1818,10 +1812,8 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { toff = incBytePtr(toff, nChunkSize+hSize) } else { // TODO: port this - wrote = int(C_WriteN(r, header, nChunkSize+hSize)) - - if wrote == 0 { - return 0 + if !C_WriteN(r, header, nChunkSize+hSize) { + return false } } @@ -1862,13 +1854,12 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { } if tbuf != nil { - wrote := int(C_WriteN(r, tbuf, int(uintptr(decBytePtr(toff, - int(uintptr(unsafe.Pointer(tbuf)))))))) + ok := C_WriteN(r, tbuf, int(uintptr(decBytePtr(toff, int(uintptr(unsafe.Pointer(tbuf))))))) ////C.free(tbuf) tbuf = nil - if wrote == 0 { - return 0 + if !ok { + return false } } @@ -1905,7 +1896,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) int { memmove(unsafe.Pointer(*(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), int(packet.m_nChannel), int(unsafe.Sizeof(packet))))), unsafe.Pointer(packet), uintptr(unsafe.Sizeof(*packet))) - return 1 + return true } // void RTMP_Close(RTMP *r); @@ -1976,7 +1967,7 @@ func C_CloseInternal(r *C_RTMP, reconnect int32) { r.m_numCalls = 0 r.m_numInvokes = 0 - r.m_bPlaying = 0 + r.m_bPlaying = false r.m_sb.sb_size = 0 r.m_msgCounter = 0 @@ -2047,7 +2038,7 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int { var pkt = &r.m_write var pend, enc []byte size := len(buf) - var ret, num int + var num int pkt.m_nChannel = 0x04 pkt.m_nInfoField2 = int32(r.m_stream_id) @@ -2085,7 +2076,7 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int { pkt.m_headerType = RTMP_PACKET_SIZE_MEDIUM } // TODO: Port this - if int(C_RTMPPacket_Alloc(pkt, uint32(pkt.m_nBodySize))) == 0 { + if !C_RTMPPacket_Alloc(pkt, uint32(pkt.m_nBodySize)) { log.Println("Failed to allocate packet") return 0 } @@ -2115,11 +2106,11 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int { buf = buf[num:] if pkt.m_nBytesRead == pkt.m_nBodySize { // TODO: Port this - ret = C_RTMP_SendPacket(r, pkt, 0) + ok := C_RTMP_SendPacket(r, pkt, 0) // TODO: Port this C_RTMPPacket_Free(pkt) pkt.m_nBytesRead = 0 - if ret == 0 { + if !ok { return -1 } if len(buf) < 4 { diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index a45afd69..d7162615 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -229,7 +229,7 @@ type C_RTMP struct { m_nServerBW int32 m_nClientBW int32 m_nClientBW2 uint8 - m_bPlaying uint8 + m_bPlaying bool m_bSendEncoding uint8 m_bSendCounter uint8 m_numInvokes int32 From aac0801cdb63b1b5e88abbd2df252af1966cc3bf Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 12:26:35 +0930 Subject: [PATCH 06/24] rtmp: use bytes.Equal for signature matching --- rtmp/rtmp.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index d2ffe248..4db77ec0 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -46,6 +46,7 @@ typedef struct sockaddr sockaddr; import "C" import ( + "bytes" "errors" "fmt" "log" @@ -1586,7 +1587,6 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) { // int HandShake(RTMP* r, int FP9HandShake); // rtmp.c +3744 func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) { - var bMatch int var uptime, suptime uint32 var typ byte //clientbuf := make([]byte, RTMP_SIG_SIZE+1) @@ -1642,15 +1642,9 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) { return false } - // TODO: find golang memcmp - bMatch = 0 - if memcmp(unsafe.Pointer(&serversig[0]), unsafe.Pointer(clientsig), - RTMP_SIG_SIZE) == 0 { - bMatch = 1 - } - - if bMatch == 0 { - log.Println("Client signature does not match!") + if !bytes.Equal(serversig[:RTMP_SIG_SIZE], clientbuf[1:RTMP_SIG_SIZE+1]) { + log.Printf("Client signature does not match: %q != %q", + serversig[:RTMP_SIG_SIZE], clientbuf[1:RTMP_SIG_SIZE+1]) } return true } From a626a740ea21e3a60de8d55efc756229fa04969b Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 12:28:09 +0930 Subject: [PATCH 07/24] rtmp: remove missed comments --- rtmp/rtmp.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 4db77ec0..93a2e813 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -1611,7 +1611,6 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) { return false } - //if C.ReadN(r, (*byte)(unsafe.Pointer(&typ)), 1) != 1 { if C_ReadN(r, (*byte)(unsafe.Pointer(&typ)), 1) != 1 { return false } @@ -1624,7 +1623,6 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) { clientbuf[0], typ) } if C_ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE { - //if C.ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE { return false } @@ -1638,7 +1636,6 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) { } if C_ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE { - //if C.ReadN(r, (*byte)(unsafe.Pointer(&serversig[0])), RTMP_SIG_SIZE) != RTMP_SIG_SIZE { return false } From 019a1f1c90ac09ca4bbd7d2081a4156f0b2ca781 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 12:46:07 +0930 Subject: [PATCH 08/24] rtmp: make string decoding a returning function --- rtmp/amf.go | 26 ++++++++++++-------------- rtmp/rtmp.go | 3 +-- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index 46b3fdb7..2c96068d 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -91,26 +91,24 @@ func C_AMF_DecodeInt32(data *byte) uint32 { // void AMF_DecodeString(const char* data, C_AVal* bv); // amf.c +68 -func C_AMF_DecodeString(data *byte, bv *C_AVal) { - dataPtr := unsafe.Pointer(data) - //bv.av_len = int32(C.AMF_DecodeInt16((*byte)(dataPtr))) - bv.av_len = int32(C_AMF_DecodeInt16((*byte)(dataPtr))) - if bv.av_len > 0 { - bv.av_val = (*byte)(incBytePtr(dataPtr, 2)) - } else { - bv.av_val = nil +func C_AMF_DecodeString(data *byte) C_AVal { + var bv C_AVal + bv.av_len = int32(C_AMF_DecodeInt16(data)) + if bv.av_len != 0 { + bv.av_val = (*byte)(incBytePtr(unsafe.Pointer(data), 2)) } + return bv } // void AMF_DecodeLongString(const char *data, AVal *bv); // amf.c +75 -func C_AMF_DecodeLongString(data *byte, bv *C_AVal) { +func C_AMF_DecodeLongString(data *byte) C_AVal { + var bv C_AVal bv.av_len = int32(C_AMF_DecodeInt32(data)) - if bv.av_len > 0 { + if bv.av_len != 0 { bv.av_val = (*byte)(incBytePtr(unsafe.Pointer(data), 4)) - } else { - bv.av_val = nil } + return bv } // double AMF_DecodeNumber(const char* data); @@ -491,7 +489,7 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa return -1 } - C_AMF_DecodeString(pBuffer, &prop.p_name) + prop.p_name = C_AMF_DecodeString(pBuffer) nSize -= int32(2 + nNameSize) pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), int(2+nNameSize))) } @@ -522,7 +520,7 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa if int64(nSize) < int64(nStringSize)+2 { return -1 } - C_AMF_DecodeString(pBuffer, &prop.p_vu.p_aval) + prop.p_vu.p_aval = C_AMF_DecodeString(pBuffer) nSize -= int32(2 + nStringSize) case AMF_OBJECT: diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 93a2e813..8d74db94 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -1858,10 +1858,9 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { // TODO: port the const if packet.m_packetType == RTMP_PACKET_TYPE_INVOKE { // TODO: port C_AVal - var method C_AVal var ptr unsafe.Pointer ptr = incBytePtr(unsafe.Pointer(packet.m_body), 1) - C_AMF_DecodeString((*byte)(ptr), &method) + method := C_AMF_DecodeString((*byte)(ptr)) if debugMode { log.Printf("Invoking %v", method.av_val) From 470e61a465ebcd4d9a44e0d1732d90ae239c788e Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 13:43:40 +0930 Subject: [PATCH 09/24] rtmp: remove C_AVal from encoder/decoder functions --- rtmp/amf.go | 113 ++++++++++++++++++++++---------------------- rtmp/amf_headers.go | 4 ++ rtmp/rtmp.go | 83 +++++++++++++++----------------- 3 files changed, 99 insertions(+), 101 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index 2c96068d..6a190521 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -39,6 +39,7 @@ package rtmp import "C" import ( + "encoding/binary" "log" "unsafe" ) @@ -91,24 +92,22 @@ func C_AMF_DecodeInt32(data *byte) uint32 { // void AMF_DecodeString(const char* data, C_AVal* bv); // amf.c +68 -func C_AMF_DecodeString(data *byte) C_AVal { - var bv C_AVal - bv.av_len = int32(C_AMF_DecodeInt16(data)) - if bv.av_len != 0 { - bv.av_val = (*byte)(incBytePtr(unsafe.Pointer(data), 2)) +func C_AMF_DecodeString(data *byte) string { + n := C_AMF_DecodeInt16(data) + if n == 0 { + return "" } - return bv + return string((*[_Gi]byte)(incBytePtr(unsafe.Pointer(data), 2))[:n]) } // void AMF_DecodeLongString(const char *data, AVal *bv); // amf.c +75 -func C_AMF_DecodeLongString(data *byte) C_AVal { - var bv C_AVal - bv.av_len = int32(C_AMF_DecodeInt32(data)) - if bv.av_len != 0 { - bv.av_val = (*byte)(incBytePtr(unsafe.Pointer(data), 4)) +func C_AMF_DecodeLongString(data *byte) string { + n := C_AMF_DecodeInt32(data) + if n == 0 { + return "" } - return bv + return string((*[_Gi]byte)(incBytePtr(unsafe.Pointer(data), 4))[:n]) } // double AMF_DecodeNumber(const char* data); @@ -195,33 +194,27 @@ func C_AMF_EncodeInt32(output *byte, outend *byte, nVal int32) *byte { // char* AMF_EncodeString(char* output, char* outend, const C_AVal* bv); // amf.c +174 -func C_AMF_EncodeString(output *byte, outend *byte, bv *C_AVal) *byte { - outputPtr := unsafe.Pointer(output) - outendPtr := unsafe.Pointer(outend) - if (bv.av_len < 65536 && uintptr(incBytePtr(outputPtr, 1+2+int(bv.av_len))) > - uintptr(outendPtr)) || uintptr(incBytePtr(outputPtr, 1+4+int(bv.av_len))) > - uintptr(outendPtr) { +func C_AMF_EncodeString(output *byte, outend *byte, s string) *byte { + buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) + if len(s) < 65536 && 1+2+len(s) > buflen { return nil } - if bv.av_len < 65536 { - *(*byte)(outputPtr) = AMF_STRING - outputPtr = incBytePtr(outputPtr, 1) - // TODO: port AMF_EncodeInt16 - outputPtr = unsafe.Pointer(C_AMF_EncodeInt16((*byte)(outputPtr), (*byte)( - outendPtr), int16(bv.av_len))) - //outputPtr = unsafe.Pointer(C_AMF_EncodeInt16((*byte)(outputPtr), - //(*byte)(outendPtr), (int16)(bv.av_len))) - } else { - *(*byte)(outputPtr) = AMF_LONG_STRING - outputPtr = incBytePtr(outputPtr, 1) - outputPtr = unsafe.Pointer(C_AMF_EncodeInt32((*byte)(outputPtr), (*byte)( - outendPtr), int32(bv.av_len))) - //outputPtr = unsafe.Pointer(C_AMF_EncodeInt32((*byte)(outputPtr), - //(*byte)(outendPtr), (int32)(bv.av_len))) + if 1+4+len(s) > buflen { + return nil + } + outputPtr := unsafe.Pointer(output) + dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen] + if len(s) < 65536 { + dst[0] = AMF_STRING + binary.BigEndian.PutUint16(dst[1:3], uint16(len(s))) + copy(dst[3:], s) + outputPtr = incBytePtr(outputPtr, 3+len(s)) + } else { + dst[0] = AMF_LONG_STRING + binary.BigEndian.PutUint32(dst[1:5], uint32(len(s))) + copy(dst[5:], s) + outputPtr = incBytePtr(outputPtr, 5+len(s)) } - memmove(unsafe.Pointer(outputPtr), unsafe.Pointer(bv.av_val), uintptr(bv.av_len)) - //C.memcpy(unsafe.Pointer(outputPtr), unsafe.Pointer(bv.av_val), (C.size_t)(bv.av_len)) - outputPtr = incBytePtr(outputPtr, int(bv.av_len)) return (*byte)(outputPtr) } @@ -264,38 +257,44 @@ func C_AMF_EncodeBoolean(output *byte, outend *byte, bVal int) *byte { // char* AMF_EncodeNamedString(char* output, char* outend, const C_AVal* strName, const C_AVal* strValue); // amf.c +273 -func C_AMF_EncodeNamedString(output *byte, outend *byte, strName *C_AVal, strValue *C_AVal) *byte { - if int(uintptr(unsafe.Pointer(output)))+2+int(strName.av_len) > int(uintptr(unsafe.Pointer(outend))) { +func C_AMF_EncodeNamedString(output *byte, outend *byte, key, val string) *byte { + buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) + if 2+len(key) > buflen { return nil } - output = C_AMF_EncodeInt16(output, outend, int16(strName.av_len)) - memmove(unsafe.Pointer(output), unsafe.Pointer(strName.av_val), uintptr(strName.av_len)) - output = (*byte)(incBytePtr(unsafe.Pointer(output), int(strName.av_len))) - return C_AMF_EncodeString(output, outend, strValue) + dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen] + binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) + copy(dst[2:], key) + output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) + return C_AMF_EncodeString(output, outend, val) } // char* AMF_EncodeNamedNumber(char* output, char* outend, const C_AVal* strName, double dVal); // amf.c +286 -func C_AMF_EncodeNamedNumber(output *byte, outend *byte, strName *C_AVal, dVal float64) *byte { - if int(uintptr(unsafe.Pointer(output)))+2+int(strName.av_len) > int(uintptr(unsafe.Pointer(outend))) { +func C_AMF_EncodeNamedNumber(output *byte, outend *byte, key string, val float64) *byte { + buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) + if 2+len(key) > buflen { return nil } - output = C_AMF_EncodeInt16(output, outend, int16(strName.av_len)) - memmove(unsafe.Pointer(output), unsafe.Pointer(strName.av_val), uintptr(strName.av_len)) - output = (*byte)(incBytePtr(unsafe.Pointer(output), int(strName.av_len))) - return C_AMF_EncodeNumber(output, outend, dVal) + dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen] + binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) + copy(dst[2:], key) + output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) + return C_AMF_EncodeNumber(output, outend, val) } // char* AMF_EncodeNamedBoolean(char* output, char* outend, const C_AVal* strname, int bVal); // amf.c +299 -func C_AMF_EncodeNamedBoolean(output *byte, outend *byte, strName *C_AVal, bVal int) *byte { - if int(uintptr(unsafe.Pointer(output)))+2+int(strName.av_len) > int(uintptr(unsafe.Pointer(outend))) { +func C_AMF_EncodeNamedBoolean(output *byte, outend *byte, key string, val int) *byte { + buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) + if 2+len(key) > buflen { return nil } - output = C_AMF_EncodeInt16(output, outend, int16(strName.av_len)) - memmove(unsafe.Pointer(output), unsafe.Pointer(strName.av_val), uintptr(strName.av_len)) - output = (*byte)(incBytePtr(unsafe.Pointer(output), int(strName.av_len))) - return C_AMF_EncodeBoolean(output, outend, bVal) + dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen] + binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) + copy(dst[2:], key) + output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) + return C_AMF_EncodeBoolean(output, outend, val) } // void AMFProp_SetName(AMFObjectProperty *prop, AVal *name); @@ -363,7 +362,7 @@ func C_AMF_PropEncode(p *C_AMFObjectProperty, pBuffer *byte, pBufEnd *byte) *byt } pBuffer = C_AMF_EncodeBoolean(pBuffer, pBufEnd, val) case AMF_STRING: - pBuffer = C_AMF_EncodeString(pBuffer, pBufEnd, &p.p_vu.p_aval) + pBuffer = C_AMF_EncodeString(pBuffer, pBufEnd, CAV(&p.p_vu.p_aval)) case AMF_NULL: if uintptr(incBytePtr(unsafe.Pointer(pBuffer), 1)) >= uintptr(unsafe.Pointer( pBufEnd)) { @@ -489,7 +488,7 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa return -1 } - prop.p_name = C_AMF_DecodeString(pBuffer) + prop.p_name = AVC(C_AMF_DecodeString(pBuffer)) nSize -= int32(2 + nNameSize) pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), int(2+nNameSize))) } @@ -520,7 +519,7 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa if int64(nSize) < int64(nStringSize)+2 { return -1 } - prop.p_vu.p_aval = C_AMF_DecodeString(pBuffer) + prop.p_vu.p_aval = AVC(C_AMF_DecodeString(pBuffer)) nSize -= int32(2 + nStringSize) case AMF_OBJECT: diff --git a/rtmp/amf_headers.go b/rtmp/amf_headers.go index 02e5343b..27eb90f1 100644 --- a/rtmp/amf_headers.go +++ b/rtmp/amf_headers.go @@ -81,6 +81,10 @@ func AVC(str string) C_AVal { return aval } +func CAV(av *C_AVal) string { + return string((*[_Gi]byte)(unsafe.Pointer(av.av_val))[:av.av_len]) +} + // #define AVMATCH(a1,a2) // amf.h +63 func C_AVMATCH(a1, a2 *C_AVal) int32 { diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 8d74db94..8b77090d 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -737,7 +737,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, &av_connect) + enc = C_AMF_EncodeString(enc, pend, CAV(&av_connect)) r.m_numInvokes += 1 enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) @@ -746,60 +746,60 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(unsafe.Pointer(incBytePtr(unsafe.Pointer(enc), 1))) - enc = C_AMF_EncodeNamedString(enc, pend, &av_app, &r.Link.app) + enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_app), CAV(&r.Link.app)) if enc == nil { return false } if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, &av_type, &av_nonprivate) + enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_type), CAV(&av_nonprivate)) if enc == nil { return false } } if r.Link.flashVer.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, &av_flashVer, &r.Link.flashVer) + enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_flashVer), CAV(&r.Link.flashVer)) if enc == nil { return false } } if r.Link.swfUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, &av_swfUrl, &r.Link.swfUrl) + enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_swfUrl), CAV(&r.Link.swfUrl)) if enc == nil { return false } } if r.Link.tcUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, &av_tcUrl, &r.Link.tcUrl) + enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_tcUrl), CAV(&r.Link.tcUrl)) if enc == nil { return false } } if r.Link.protocol&RTMP_FEATURE_WRITE == 0 { - enc = C_AMF_EncodeNamedBoolean(enc, pend, &av_fpad, 0) + enc = C_AMF_EncodeNamedBoolean(enc, pend, CAV(&av_fpad), 0) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, &av_capabilities, 15.0) + enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_capabilities), 15) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, &av_audioCodecs, float64(r.m_fAudioCodecs)) + enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_audioCodecs), float64(r.m_fAudioCodecs)) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, &av_videoCodecs, float64(r.m_fVideoCodecs)) + enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_videoCodecs), float64(r.m_fVideoCodecs)) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, &av_videoFunction, 1.0) + enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_videoFunction), 1) if enc == nil { return false } if r.Link.pageUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, &av_pageUrl, &r.Link.pageUrl) + enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_pageUrl), CAV(&r.Link.pageUrl)) if enc == nil { return false } @@ -807,7 +807,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } if r.m_fEncoding != 0.0 || r.m_bSendEncoding != 0 { - enc = C_AMF_EncodeNamedNumber(enc, pend, &av_objectEncoding, float64(r.m_fEncoding)) + enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_objectEncoding), float64(r.m_fEncoding)) if enc == nil { return false } @@ -831,7 +831,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { if enc == nil { return false } - enc = C_AMF_EncodeString(enc, (*byte)(pend), &r.Link.auth) + enc = C_AMF_EncodeString(enc, (*byte)(pend), CAV(&r.Link.auth)) if enc == nil { return false } @@ -873,7 +873,7 @@ func C_RTMP_SendCreateStream(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, &av_createStream) + enc = C_AMF_EncodeString(enc, pend, CAV(&av_createStream)) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -903,12 +903,12 @@ func C_SendReleaseStream(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, &av_releaseStream) + enc = C_AMF_EncodeString(enc, pend, CAV(&av_releaseStream)) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) + enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) if enc == nil { return false } @@ -936,12 +936,12 @@ func C_SendFCPublish(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, &av_FCPublish) + enc = C_AMF_EncodeString(enc, pend, CAV(&av_FCPublish)) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) + enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) if enc == nil { return false } @@ -969,12 +969,12 @@ func C_SendFCUnpublish(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, &av_FCUnpublish) + enc = C_AMF_EncodeString(enc, pend, CAV(&av_FCUnpublish)) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) + enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) if enc == nil { return false @@ -1004,18 +1004,18 @@ func C_SendPublish(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, &av_publish) + enc = C_AMF_EncodeString(enc, pend, CAV(&av_publish)) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, &r.Link.playpath) + enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) if enc == nil { return false } - enc = C_AMF_EncodeString(enc, pend, &av_live) + enc = C_AMF_EncodeString(enc, pend, CAV(&av_live)) if enc == nil { return false } @@ -1044,7 +1044,7 @@ func C_SendDeleteStream(r *C_RTMP, dStreamId float64) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, &av_deleteStream) + enc = C_AMF_EncodeString(enc, pend, CAV(&av_deleteStream)) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -1100,7 +1100,7 @@ func C_SendCheckBW(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, &av__checkbw) + enc = C_AMF_EncodeString(enc, pend, CAV(&av__checkbw)) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -1135,23 +1135,20 @@ func C_AV_erase(vals *C_RTMP_METHOD, num *int32, i, freeit int32) { // void AV_queue(RTMP_METHOD** vals, int* num, C_AVal* av, int txn); // rtmp.c +2414 -func C_AV_queue(vals **C_RTMP_METHOD, num *int32, av *C_AVal, txn int32) { +func C_AV_queue(vals **C_RTMP_METHOD, num *int32, av string, txn int32) { if (*num & 0x0f) == 0 { - *vals = (*C_RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)* - int32(unsafe.Sizeof(*(*vals)))))) + *vals = (*C_RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)*int32(unsafe.Sizeof(**vals))))) } - tmp := malloc(uintptr(av.av_len + 1)) - //tmp := allocate(uintptr(av.av_len + 1)) - memmove(tmp, unsafe.Pointer(av.av_val), uintptr(av.av_len)) - (*[_Gi]byte)(tmp)[av.av_len] = '\000' + tmp := make([]byte, len(av)+1) + copy(tmp, av) (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), int(*num), - int(unsafe.Sizeof(*(*vals)))))).num = int32(txn) + int(unsafe.Sizeof(**vals))))).num = int32(txn) (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), int(*num), - int(unsafe.Sizeof(*(*vals)))))).name.av_len = av.av_len + int(unsafe.Sizeof(**vals))))).name.av_len = int32(len(av)) (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), int(*num), - int(unsafe.Sizeof(*(*vals)))))).name.av_val = (*byte)(tmp) - (*num)++ + int(unsafe.Sizeof(**vals))))).name.av_val = &tmp[0] + *num++ } // int HandleInvoke(RTMP* r, const char* body, unsigned int nBodySize); @@ -1863,15 +1860,13 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { method := C_AMF_DecodeString((*byte)(ptr)) if debugMode { - log.Printf("Invoking %v", method.av_val) + log.Printf("invoking %v", method) } // keep it in call queue till result arrives if queue != 0 { - var txn int - ptr = incBytePtr(ptr, 3+int(method.av_len)) - txn = int(C_AMF_DecodeNumber((*byte)(ptr))) - C_AV_queue(&r.m_methodCalls, (*int32)(unsafe.Pointer(&r.m_numCalls)), - &method, int32(txn)) + ptr = incBytePtr(ptr, 3+len(method)) + txn := int32(C_AMF_DecodeNumber((*byte)(ptr))) + C_AV_queue(&r.m_methodCalls, (*int32)(unsafe.Pointer(&r.m_numCalls)), method, txn) } } @@ -2076,7 +2071,7 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int { if pkt.m_packetType == RTMP_PACKET_TYPE_INFO { enc = (*[_Gi]byte)(unsafe.Pointer(C_AMF_EncodeString((*byte)(unsafe.Pointer(&enc[0])), - (*byte)(unsafe.Pointer(&pend[0])), &setDataFrame)))[:pkt.m_nBodySize] + (*byte)(unsafe.Pointer(&pend[0])), CAV(&setDataFrame))))[:pkt.m_nBodySize] // TODO: work out what to do with this pkt.m_nBytesRead = uint32(float64(uintptr(unsafe.Pointer(&enc[0])) - From f005c31b44e7214c8f46a9d37f1891b735c209a8 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 15:36:27 +0930 Subject: [PATCH 10/24] rtmp: encode/decode typed boolean --- rtmp/amf.go | 23 ++++++++--------------- rtmp/rtmp.go | 4 ++-- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index 6a190521..1d72d7ce 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -125,11 +125,8 @@ func C_AMF_DecodeNumber(data *byte) float64 { // int AMF_DecodeBoolean(const char *data); // amf.c +132 -func C_AMF_DecodeBoolean(data *byte) int32 { - if *data != 0 { - return 1 - } - return 0 +func C_AMF_DecodeBoolean(data *byte) bool { + return *data != 0 } // char* AMF_EncodeInt16(char* output, char* outend, short nVal); @@ -240,15 +237,15 @@ func C_AMF_EncodeNumber(output *byte, outend *byte, dVal float64) *byte { // char* AMF_EncodeBoolean(char* output, char* outend, int bVal); // amf.c +260 -func C_AMF_EncodeBoolean(output *byte, outend *byte, bVal int) *byte { +func C_AMF_EncodeBoolean(output *byte, outend *byte, bVal bool) *byte { if int(uintptr(unsafe.Pointer(output)))+2 > int(uintptr(unsafe.Pointer(outend))) { return nil } *(*byte)(unsafe.Pointer(output)) = AMF_BOOLEAN output = (*byte)(incBytePtr(unsafe.Pointer(output), 1)) - val := byte(0x01) - if bVal == 0 { - val = byte(0x00) + var val byte + if bVal { + val = 1 } *(*byte)(unsafe.Pointer(output)) = val output = (*byte)(incBytePtr(unsafe.Pointer(output), 1)) @@ -285,7 +282,7 @@ func C_AMF_EncodeNamedNumber(output *byte, outend *byte, key string, val float64 // char* AMF_EncodeNamedBoolean(char* output, char* outend, const C_AVal* strname, int bVal); // amf.c +299 -func C_AMF_EncodeNamedBoolean(output *byte, outend *byte, key string, val int) *byte { +func C_AMF_EncodeNamedBoolean(output *byte, outend *byte, key string, val bool) *byte { buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) if 2+len(key) > buflen { return nil @@ -356,11 +353,7 @@ func C_AMF_PropEncode(p *C_AMFObjectProperty, pBuffer *byte, pBufEnd *byte) *byt case AMF_NUMBER: pBuffer = C_AMF_EncodeNumber(pBuffer, pBufEnd, float64(p.p_vu.p_number)) case AMF_BOOLEAN: - val := 0 - if p.p_vu.p_number != 0 { - val = 1 - } - pBuffer = C_AMF_EncodeBoolean(pBuffer, pBufEnd, val) + pBuffer = C_AMF_EncodeBoolean(pBuffer, pBufEnd, p.p_vu.p_number != 0) case AMF_STRING: pBuffer = C_AMF_EncodeString(pBuffer, pBufEnd, CAV(&p.p_vu.p_aval)) case AMF_NULL: diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 8b77090d..deb3d1e5 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -778,7 +778,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } if r.Link.protocol&RTMP_FEATURE_WRITE == 0 { - enc = C_AMF_EncodeNamedBoolean(enc, pend, CAV(&av_fpad), 0) + enc = C_AMF_EncodeNamedBoolean(enc, pend, CAV(&av_fpad), false) if enc == nil { return false } @@ -827,7 +827,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { /* add auth string */ if r.Link.auth.av_len != 0 { - enc = C_AMF_EncodeBoolean(enc, pend, int(r.Link.lFlags&RTMP_LF_AUTH)) + enc = C_AMF_EncodeBoolean(enc, pend, r.Link.lFlags&RTMP_LF_AUTH != 0) if enc == nil { return false } From ed40f36ffe811723a22cf4a043536e36316949c9 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 15:57:06 +0930 Subject: [PATCH 11/24] rtmp: remove C_AVal from C_AMF3ReadString Note that this function is not used. --- rtmp/amf.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index 1d72d7ce..eb363535 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -424,7 +424,7 @@ func C_AMF3ReadInteger(data *byte, valp *int32) int32 { // int AMF3ReadString(const char *data, AVal *str); // amf.c +466 -func C_AMF3ReadString(data *byte, str *C_AVal) int32 { +func C_AMF3ReadString(data *byte, str *string) int32 { var ref int32 // assert elided - we will get a panic if it's nil. @@ -438,13 +438,11 @@ func C_AMF3ReadString(data *byte, str *C_AVal) int32 { // RTMP_Log(RTMP_LOGDEBUG, // "%s, string reference, index: %d, not supported, ignoring!", // __FUNCTION__, refIndex); - str.av_val = nil - str.av_len = 0 + *str = "" return len } else { nSize := (ref >> 1) - str.av_val = data - str.av_len = int32(nSize) + *str = string((*[_Gi]byte)(unsafe.Pointer(data))[:nSize]) return len + nSize } return len From c4800034a60f59f3e34a3d82239d1335c3fb3b60 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 16:10:15 +0930 Subject: [PATCH 12/24] rtmp: remove C_AVal from P_vu --- rtmp/amf.go | 14 +++++------- rtmp/amf_headers.go | 2 +- rtmp/rtmp.go | 55 +++++++++++++++++++++++---------------------- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index eb363535..72af2f80 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -308,12 +308,11 @@ func C_AMFProp_GetNumber(prop *C_AMFObjectProperty) float64 { // void AMFProp_GetString(AMFObjectProperty* prop, AVal* str); // amf.c +341 -func C_AMFProp_GetString(prop *C_AMFObjectProperty, str *C_AVal) { +func C_AMFProp_GetString(prop *C_AMFObjectProperty) string { if prop.p_type == AMF_STRING { - *str = prop.p_vu.p_aval - } else { - *str = AV_empty + return prop.p_vu.p_aval } + return "" } // void AMFProp_GetObject(AMFObjectProperty *prop, AMFObject *obj); @@ -355,7 +354,7 @@ func C_AMF_PropEncode(p *C_AMFObjectProperty, pBuffer *byte, pBufEnd *byte) *byt case AMF_BOOLEAN: pBuffer = C_AMF_EncodeBoolean(pBuffer, pBufEnd, p.p_vu.p_number != 0) case AMF_STRING: - pBuffer = C_AMF_EncodeString(pBuffer, pBufEnd, CAV(&p.p_vu.p_aval)) + pBuffer = C_AMF_EncodeString(pBuffer, pBufEnd, p.p_vu.p_aval) case AMF_NULL: if uintptr(incBytePtr(unsafe.Pointer(pBuffer), 1)) >= uintptr(unsafe.Pointer( pBufEnd)) { @@ -510,7 +509,7 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa if int64(nSize) < int64(nStringSize)+2 { return -1 } - prop.p_vu.p_aval = AVC(C_AMF_DecodeString(pBuffer)) + prop.p_vu.p_aval = C_AMF_DecodeString(pBuffer) nSize -= int32(2 + nStringSize) case AMF_OBJECT: @@ -593,8 +592,7 @@ func C_AMFProp_Reset(prop *C_AMFObjectProperty) { prop.p_type == AMF_STRICT_ARRAY { C_AMF_Reset(&prop.p_vu.p_object) } else { - prop.p_vu.p_aval.av_len = 0 - prop.p_vu.p_aval.av_val = nil + prop.p_vu.p_aval = "" } prop.p_type = AMF_INVALID } diff --git a/rtmp/amf_headers.go b/rtmp/amf_headers.go index 27eb90f1..6827c29e 100644 --- a/rtmp/amf_headers.go +++ b/rtmp/amf_headers.go @@ -107,7 +107,7 @@ type C_AMFObject struct { // amf.h +73 type P_vu struct { p_number float64 - p_aval C_AVal + p_aval string p_object C_AMFObject } diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index deb3d1e5..f7214f99 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -1155,7 +1155,6 @@ func C_AV_queue(vals **C_RTMP_METHOD, num *int32, av string, txn int32) { // rtmp.c +2912 func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { var obj C_AMFObject - var method C_AVal var txn float64 var nRes int32 @@ -1175,13 +1174,13 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { // NOTE we don't really need this ?? still functions without it //C.AMF_Dump(&obj) //C.AMFProp_GetString(C_AMF_GetProp(&obj, nil, 0), &method) - C_AMFProp_GetString(C_AMF_GetProp(&obj, nil, 0), &method) + method := C_AMFProp_GetString(C_AMF_GetProp(&obj, nil, 0)) txn = float64(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, nil, 1))) // TODO use new logger here // RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val); - + avmethod := AVC(method) switch { - case C_AVMATCH(&method, &av__result) != 0: + case C_AVMATCH(&avmethod, &av__result) != 0: { var methodInvoked C_AVal var i int32 @@ -1236,47 +1235,49 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { } //C.free(unsafe.Pointer(methodInvoked.av_val)) } - case C_AVMATCH(&method, &av_onBWDone) != 0: + case C_AVMATCH(&avmethod, &av_onBWDone) != 0: if r.m_nBWCheckCounter == 0 { C_SendCheckBW(r) } - case C_AVMATCH(&method, &av_onFCUnsubscribe) != 0 || C_AVMATCH(&method, &av_onFCSubscribe) != 0: + case C_AVMATCH(&avmethod, &av_onFCUnsubscribe) != 0 || C_AVMATCH(&avmethod, &av_onFCSubscribe) != 0: panic("Unsupported method av_onFCUnsubscribe/av_onFCSubscribe") - case C_AVMATCH(&method, &av_ping) != 0: + case C_AVMATCH(&avmethod, &av_ping) != 0: panic("Unsupported method av_ping") - case C_AVMATCH(&method, &av__onbwcheck) != 0: + case C_AVMATCH(&avmethod, &av__onbwcheck) != 0: panic("Unsupported method av_onbwcheck") - case C_AVMATCH(&method, &av__onbwdone) != 0: + case C_AVMATCH(&avmethod, &av__onbwdone) != 0: panic("Unsupported method av_onbwdone") - case C_AVMATCH(&method, &av_close) != 0: + case C_AVMATCH(&avmethod, &av_close) != 0: panic("Unsupported method av_close") - case C_AVMATCH(&method, &av_onStatus) != 0: + case C_AVMATCH(&avmethod, &av_onStatus) != 0: var obj2 C_AMFObject - var code, level C_AVal C_AMFProp_GetObject(C_AMF_GetProp(&obj, nil, 3), &obj2) - C_AMFProp_GetString(C_AMF_GetProp(&obj2, &av_code, -1), &code) - C_AMFProp_GetString(C_AMF_GetProp(&obj2, &av_level, -1), &level) + code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, &av_code, -1)) + + level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, &av_level, -1)) // Not used. + _ = level // TODO use new logger // RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val); + avcode := AVC(code) switch { - case C_AVMATCH(&code, &av_NetStream_Failed) != 0 || - C_AVMATCH(&code, &av_NetStream_Play_Failed) != 0 || - C_AVMATCH(&code, &av_NetStream_Play_StreamNotFound) != 0 || - C_AVMATCH(&code, &av_NetConnection_Connect_InvalidApp) != 0: + case C_AVMATCH(&avcode, &av_NetStream_Failed) != 0 || + C_AVMATCH(&avcode, &av_NetStream_Play_Failed) != 0 || + C_AVMATCH(&avcode, &av_NetStream_Play_StreamNotFound) != 0 || + C_AVMATCH(&avcode, &av_NetConnection_Connect_InvalidApp) != 0: panic("Unsupported method av_NetStream/av_NetStream_Play_Failed/av_netSTream_Play_StreamNotFound/av_netConnection_Connect_invalidApp") - case C_AVMATCH(&code, &av_NetStream_Play_Start) != 0 || - C_AVMATCH(&code, &av_NetStream_Play_PublishNotify) != 0: + case C_AVMATCH(&avcode, &av_NetStream_Play_Start) != 0 || + C_AVMATCH(&avcode, &av_NetStream_Play_PublishNotify) != 0: panic("Unsupported method av_NetStream_Play_Start/av_NetStream_Play_PublishNotify") - case C_AVMATCH(&code, &av_NetStream_Publish_Start) != 0: + case C_AVMATCH(&avcode, &av_NetStream_Publish_Start) != 0: var i int32 r.m_bPlaying = true @@ -1288,19 +1289,19 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { } } - case C_AVMATCH(&code, &av_NetStream_Play_Complete) != 0 || - C_AVMATCH(&code, &av_NetStream_Play_Stop) != 0 || - C_AVMATCH(&code, &av_NetStream_Play_UnpublishNotify) != 0: + case C_AVMATCH(&avcode, &av_NetStream_Play_Complete) != 0 || + C_AVMATCH(&avcode, &av_NetStream_Play_Stop) != 0 || + C_AVMATCH(&avcode, &av_NetStream_Play_UnpublishNotify) != 0: panic("Unsupported method av_NetStream_Play_Complete/av_NetStream_Play_Stop/av_NetStream_Play_UnpublishNotify") - case C_AVMATCH(&code, &av_NetStream_Seek_Notify) != 0: + case C_AVMATCH(&avcode, &av_NetStream_Seek_Notify) != 0: panic("Unsupported method av_netStream_Seek_Notify") - case C_AVMATCH(&code, &av_NetStream_Pause_Notify) != 0: + case C_AVMATCH(&avcode, &av_NetStream_Pause_Notify) != 0: panic("Unsupported method av_NetStream_Pause_Notify") } - case C_AVMATCH(&method, &av_playlist_ready) != 0: + case C_AVMATCH(&avmethod, &av_playlist_ready) != 0: panic("Unsupported method av_playlist_ready") default: From fed9d366c61c3283b2f42447cca73432ce82c27d Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 16:30:39 +0930 Subject: [PATCH 13/24] rtmp: remove C_Aval from C_AMFObjectProperty --- rtmp/amf.go | 40 ++++++++++++++++++---------------------- rtmp/amf_headers.go | 2 +- rtmp/rtmp.go | 12 ++++++------ 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index 72af2f80..339d6f72 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -296,8 +296,8 @@ func C_AMF_EncodeNamedBoolean(output *byte, outend *byte, key string, val bool) // void AMFProp_SetName(AMFObjectProperty *prop, AVal *name); // amf.c +318 -func C_AMFProp_SetName(prop *C_AMFObjectProperty, name *C_AVal) { - prop.p_name = *name +func C_AMFProp_SetName(prop *C_AMFObjectProperty, name string) { + prop.p_name = name } // double AMFProp_GetNumber(AMFObjectProperty* prop); @@ -332,20 +332,18 @@ func C_AMF_PropEncode(p *C_AMFObjectProperty, pBuffer *byte, pBufEnd *byte) *byt return nil } - if p.p_type != AMF_NULL && int(uintptr(unsafe.Pointer(pBuffer)))+ - int(p.p_name.av_len)+2+1 >= int( - uintptr(unsafe.Pointer(pBufEnd))) { + buflen := int(uintptr(unsafe.Pointer(pBufEnd)) - uintptr(unsafe.Pointer(pBuffer))) + if p.p_type != AMF_NULL && len(p.p_name)+2+1 >= buflen { return nil } - if p.p_type != AMF_NULL && p.p_name.av_len != 0 { - (*[_Gi]byte)(unsafe.Pointer(pBuffer))[0] = byte(p.p_name.av_len >> 8) + if p.p_type != AMF_NULL && len(p.p_name) != 0 { + (*[_Gi]byte)(unsafe.Pointer(pBuffer))[0] = byte(len(p.p_name) >> 8) pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), 1)) - (*[_Gi]byte)(unsafe.Pointer(pBuffer))[0] = byte(p.p_name.av_len & 0xff) + (*[_Gi]byte)(unsafe.Pointer(pBuffer))[0] = byte(len(p.p_name) & 0xff) pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), 1)) - memmove(unsafe.Pointer(pBuffer), unsafe.Pointer(p.p_name.av_val), - uintptr(p.p_name.av_len)) - pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), int(p.p_name.av_len))) + copy((*[_Gi]byte)(unsafe.Pointer(pBuffer))[:], p.p_name) + pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), len(p.p_name))) } switch p.p_type { @@ -356,8 +354,8 @@ func C_AMF_PropEncode(p *C_AMFObjectProperty, pBuffer *byte, pBufEnd *byte) *byt case AMF_STRING: pBuffer = C_AMF_EncodeString(pBuffer, pBufEnd, p.p_vu.p_aval) case AMF_NULL: - if uintptr(incBytePtr(unsafe.Pointer(pBuffer), 1)) >= uintptr(unsafe.Pointer( - pBufEnd)) { + buflen = int(uintptr(unsafe.Pointer(pBufEnd)) - uintptr(unsafe.Pointer(pBuffer))) + if 1 >= buflen { return nil } *(*byte)(unsafe.Pointer(pBuffer)) = AMF_NULL @@ -454,8 +452,7 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa var nOriginalSize int32 = nSize var nRes int32 - prop.p_name.av_len = 0 - prop.p_name.av_val = nil + prop.p_name = "" if nSize == 0 || pBuffer == nil { // TODO use new logger here @@ -478,7 +475,7 @@ func C_AMFProp_Decode(prop *C_AMFObjectProperty, pBuffer *byte, nSize, bDecodeNa return -1 } - prop.p_name = AVC(C_AMF_DecodeString(pBuffer)) + prop.p_name = C_AMF_DecodeString(pBuffer) nSize -= int32(2 + nNameSize) pBuffer = (*byte)(incBytePtr(unsafe.Pointer(pBuffer), int(2+nNameSize))) } @@ -788,18 +785,17 @@ func C_AMF_AddProp(obj *C_AMFObject, prop *C_AMFObjectProperty) { // AMFObjectProperty* AMF_GetProp(AMFObject *obj, const AVal* name, int nIndex); // amf.c + 1249 -func C_AMF_GetProp(obj *C_AMFObject, name *C_AVal, nIndex int32) *C_AMFObjectProperty { +func C_AMF_GetProp(obj *C_AMFObject, name string, nIndex int32) *C_AMFObjectProperty { if nIndex >= 0 { if nIndex < int32(obj.o_num) { return &(*(*C_AMFObjectProperty)(incPtr(unsafe.Pointer(obj.o_props), int(nIndex), int(unsafe.Sizeof(*obj.o_props))))) - //return &obj.o_props[nIndex] } } else { - var n int32 - for n = 0; n < int32(obj.o_num); n++ { - if C_AVMATCH(&(*(*C_AMFObjectProperty)(incPtr(unsafe.Pointer(obj.o_props), - int(n), int(unsafe.Sizeof(*obj.o_props))))).p_name, name) != 0 { + for n := int32(0); n < obj.o_num; n++ { + p_name := (*(*C_AMFObjectProperty)(incPtr(unsafe.Pointer(obj.o_props), + int(n), int(unsafe.Sizeof(*obj.o_props))))).p_name + if p_name == name { return &(*(*C_AMFObjectProperty)(incPtr(unsafe.Pointer(obj.o_props), int(n), int(unsafe.Sizeof(*obj.o_props))))) } diff --git a/rtmp/amf_headers.go b/rtmp/amf_headers.go index 6827c29e..7fc2dc3f 100644 --- a/rtmp/amf_headers.go +++ b/rtmp/amf_headers.go @@ -114,7 +114,7 @@ type P_vu struct { // typedef struct AMFObjectProperty // amf.h +79 type C_AMFObjectProperty struct { - p_name C_AVal + p_name string p_type C_AMFDataType p_vu P_vu p_UTCoffset int16 diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index f7214f99..6f8f32e5 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -1174,8 +1174,8 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { // NOTE we don't really need this ?? still functions without it //C.AMF_Dump(&obj) //C.AMFProp_GetString(C_AMF_GetProp(&obj, nil, 0), &method) - method := C_AMFProp_GetString(C_AMF_GetProp(&obj, nil, 0)) - txn = float64(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, nil, 1))) + method := C_AMFProp_GetString(C_AMF_GetProp(&obj, "", 0)) + txn = float64(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 1))) // TODO use new logger here // RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val); avmethod := AVC(method) @@ -1221,7 +1221,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { } case C_AVMATCH(&methodInvoked, &av_createStream) != 0: - r.m_stream_id = int32(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, nil, 3))) + r.m_stream_id = int32(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 3))) if (r.Link.protocol & RTMP_FEATURE_WRITE) != 0 { C_SendPublish(r) @@ -1257,10 +1257,10 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { case C_AVMATCH(&avmethod, &av_onStatus) != 0: var obj2 C_AMFObject - C_AMFProp_GetObject(C_AMF_GetProp(&obj, nil, 3), &obj2) - code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, &av_code, -1)) + C_AMFProp_GetObject(C_AMF_GetProp(&obj, "", 3), &obj2) + code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, CAV(&av_code), -1)) - level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, &av_level, -1)) // Not used. + level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, CAV(&av_level), -1)) // Not used. _ = level // TODO use new logger From 97127030b31927c43de5b6acb11b39bda79c07d5 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 16:52:48 +0930 Subject: [PATCH 14/24] rtmp: remove C_AVal from RTMP type --- rtmp/rtmp_headers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index d7162615..c9985361 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -248,7 +248,7 @@ type C_RTMP struct { m_polling int32 m_resplen int32 m_unackd int32 - m_clientID C_AVal + m_clientID string m_read C_RTMP_READ m_write C_RTMPPacket m_sb C_RTMPSockBuf From 569cc100fa4411769ea0e0ac00d79ef8d84d5a2b Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 18:03:55 +0930 Subject: [PATCH 15/24] retmp: simplify big switch --- rtmp/rtmp.go | 148 ++++++++++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 78 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 6f8f32e5..2f537625 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -1178,84 +1178,82 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { txn = float64(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 1))) // TODO use new logger here // RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val); - avmethod := AVC(method) - switch { - case C_AVMATCH(&avmethod, &av__result) != 0: - { - var methodInvoked C_AVal - var i int32 - for i = 0; i < int32(r.m_numCalls); i++ { - if float64((*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), - int(unsafe.Sizeof(*r.m_methodCalls))))).num) == txn { - methodInvoked = (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), - int(i), int(unsafe.Sizeof(*r.m_methodCalls))))).name - C_AV_erase(r.m_methodCalls, (*int32)(&r.m_numCalls), int32(i), 0) - break - } + + switch method { + case CAV(&av__result): + var methodInvoked C_AVal + for i := int32(0); i < r.m_numCalls; i++ { + if float64((*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), + int(unsafe.Sizeof(*r.m_methodCalls))))).num) == txn { + methodInvoked = (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), + int(i), int(unsafe.Sizeof(*r.m_methodCalls))))).name + C_AV_erase(r.m_methodCalls, (*int32)(&r.m_numCalls), int32(i), 0) + break } - if methodInvoked.av_val == nil { - // TODO use new logger here - //RTMP_Log(RTMP_LOGDEBUG, "%s, received result id %f without matching request", - //__FUNCTION__, txn); - goto leave - } - // TODO use new logger here - //RTMP_Log(RTMP_LOGDEBUG, "%s, received result for method call <%s>", __FUNCTION__, - //methodInvoked.av_val); - switch { - case C_AVMATCH(&methodInvoked, &av_connect) != 0: - if r.Link.token.av_len != 0 { - panic("No support for link token") - - } - if (r.Link.protocol & RTMP_FEATURE_WRITE) != 0 { - C_SendReleaseStream(r) - C_SendFCPublish(r) - } else { - panic("Link protocol has no RTMP_FEATURE_WRITE") - } - - C_RTMP_SendCreateStream(r) - if (r.Link.protocol & RTMP_FEATURE_WRITE) == 0 { - panic("Link protocol has no RTMP_FEATURE_WRITE") - } - - case C_AVMATCH(&methodInvoked, &av_createStream) != 0: - r.m_stream_id = int32(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 3))) - - if (r.Link.protocol & RTMP_FEATURE_WRITE) != 0 { - C_SendPublish(r) - } else { - panic("Link protocol has no RTMP_FEATURE_WRITE") - } - - case C_AVMATCH(&methodInvoked, &av_play) != 0 || - C_AVMATCH(&methodInvoked, &av_publish) != 0: - panic("Unsupported method av_play/av_publish") - } - //C.free(unsafe.Pointer(methodInvoked.av_val)) } - case C_AVMATCH(&avmethod, &av_onBWDone) != 0: + if methodInvoked.av_val == nil { + // TODO use new logger here + //RTMP_Log(RTMP_LOGDEBUG, "%s, received result id %f without matching request", + //__FUNCTION__, txn); + goto leave + } + // TODO use new logger here + //RTMP_Log(RTMP_LOGDEBUG, "%s, received result for method call <%s>", __FUNCTION__, + //methodInvoked.av_val); + switch { + case C_AVMATCH(&methodInvoked, &av_connect) != 0: + if r.Link.token.av_len != 0 { + panic("No support for link token") + + } + if (r.Link.protocol & RTMP_FEATURE_WRITE) != 0 { + C_SendReleaseStream(r) + C_SendFCPublish(r) + } else { + panic("Link protocol has no RTMP_FEATURE_WRITE") + } + + C_RTMP_SendCreateStream(r) + if (r.Link.protocol & RTMP_FEATURE_WRITE) == 0 { + panic("Link protocol has no RTMP_FEATURE_WRITE") + } + + case C_AVMATCH(&methodInvoked, &av_createStream) != 0: + r.m_stream_id = int32(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 3))) + + if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { + C_SendPublish(r) + } else { + panic("Link protocol has no RTMP_FEATURE_WRITE") + } + + case C_AVMATCH(&methodInvoked, &av_play) != 0 || + C_AVMATCH(&methodInvoked, &av_publish) != 0: + panic("Unsupported method av_play/av_publish") + } + //C.free(unsafe.Pointer(methodInvoked.av_val)) + + case CAV(&av_onBWDone): if r.m_nBWCheckCounter == 0 { C_SendCheckBW(r) } - case C_AVMATCH(&avmethod, &av_onFCUnsubscribe) != 0 || C_AVMATCH(&avmethod, &av_onFCSubscribe) != 0: + case CAV(&av_onFCUnsubscribe), CAV(&av_onFCSubscribe): panic("Unsupported method av_onFCUnsubscribe/av_onFCSubscribe") - case C_AVMATCH(&avmethod, &av_ping) != 0: + case CAV(&av_ping): panic("Unsupported method av_ping") - case C_AVMATCH(&avmethod, &av__onbwcheck) != 0: + case CAV(&av__onbwcheck): panic("Unsupported method av_onbwcheck") - case C_AVMATCH(&avmethod, &av__onbwdone) != 0: + case CAV(&av__onbwdone): panic("Unsupported method av_onbwdone") - case C_AVMATCH(&avmethod, &av_close) != 0: + case CAV(&av_close): panic("Unsupported method av_close") - case C_AVMATCH(&avmethod, &av_onStatus) != 0: + case CAV(&av_onStatus): var obj2 C_AMFObject C_AMFProp_GetObject(C_AMF_GetProp(&obj, "", 3), &obj2) code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, CAV(&av_code), -1)) @@ -1265,20 +1263,16 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { // TODO use new logger // RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val); - avcode := AVC(code) - switch { - case C_AVMATCH(&avcode, &av_NetStream_Failed) != 0 || - C_AVMATCH(&avcode, &av_NetStream_Play_Failed) != 0 || - C_AVMATCH(&avcode, &av_NetStream_Play_StreamNotFound) != 0 || - C_AVMATCH(&avcode, &av_NetConnection_Connect_InvalidApp) != 0: + + switch code { + case CAV(&av_NetStream_Failed), CAV(&av_NetStream_Play_Failed), + CAV(&av_NetStream_Play_StreamNotFound), CAV(&av_NetConnection_Connect_InvalidApp): panic("Unsupported method av_NetStream/av_NetStream_Play_Failed/av_netSTream_Play_StreamNotFound/av_netConnection_Connect_invalidApp") - case C_AVMATCH(&avcode, &av_NetStream_Play_Start) != 0 || - C_AVMATCH(&avcode, &av_NetStream_Play_PublishNotify) != 0: + case CAV(&av_NetStream_Play_Start), CAV(&av_NetStream_Play_PublishNotify): panic("Unsupported method av_NetStream_Play_Start/av_NetStream_Play_PublishNotify") - case C_AVMATCH(&avcode, &av_NetStream_Publish_Start) != 0: - + case CAV(&av_NetStream_Publish_Start): var i int32 r.m_bPlaying = true for i = 0; i < int32(r.m_numCalls); i++ { @@ -1289,19 +1283,17 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { } } - case C_AVMATCH(&avcode, &av_NetStream_Play_Complete) != 0 || - C_AVMATCH(&avcode, &av_NetStream_Play_Stop) != 0 || - C_AVMATCH(&avcode, &av_NetStream_Play_UnpublishNotify) != 0: + case CAV(&av_NetStream_Play_Complete), CAV(&av_NetStream_Play_Stop), CAV(&av_NetStream_Play_UnpublishNotify): panic("Unsupported method av_NetStream_Play_Complete/av_NetStream_Play_Stop/av_NetStream_Play_UnpublishNotify") - case C_AVMATCH(&avcode, &av_NetStream_Seek_Notify) != 0: + case CAV(&av_NetStream_Seek_Notify): panic("Unsupported method av_netStream_Seek_Notify") - case C_AVMATCH(&avcode, &av_NetStream_Pause_Notify) != 0: + case CAV(&av_NetStream_Pause_Notify): panic("Unsupported method av_NetStream_Pause_Notify") } - case C_AVMATCH(&avmethod, &av_playlist_ready) != 0: + case CAV(&av_playlist_ready): panic("Unsupported method av_playlist_ready") default: From 42f7316f09bf2c821bc43923363348223d3db3a0 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 18:06:45 +0930 Subject: [PATCH 16/24] rtmp: ensure we catch invalid methods --- rtmp/rtmp.go | 1 + 1 file changed, 1 insertion(+) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 2f537625..bb823f70 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -1297,6 +1297,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { panic("Unsupported method av_playlist_ready") default: + panic(fmt.Sprintf("unknown method: %q", method)) } leave: C_AMF_Reset(&obj) From b1c904ee22dd9be3a063bd53b5cf0523d55f7580 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 18:31:14 +0930 Subject: [PATCH 17/24] rtmp: reduce CAV/AVC conversions --- rtmp/rtmp.go | 185 ++++++++++++++++++++++++++------------------------- 1 file changed, 94 insertions(+), 91 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index bb823f70..f46b6d57 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -66,59 +66,63 @@ const ( length = 512 ) -// av_setDataFrame is a static const global in rtmp.c +// TODO(kortschak): Reduce this list. var ( - AV_empty = AVC("") - setDataFrame = AVC("@setDataFrame") - av_connect = AVC("connect") - av_app = AVC("app") - av_type = AVC("type") - av_nonprivate = AVC("nonprivate") - av_flashVer = AVC("flashVer") - av_swfUrl = AVC("swfUrl") - av_tcUrl = AVC("tcUrl") - av_fpad = AVC("fpad") - av_capabilities = AVC("capabilities") - av_audioCodecs = AVC("audioCodecs") - av_videoCodecs = AVC("videoCodecs") - av_videoFunction = AVC("videoFunction") - av_pageUrl = AVC("pageUrl") - av_objectEncoding = AVC("objectEncoding") - av__result = AVC("_result") - av_secureToken = AVC("secureToken") - av_createStream = AVC("createStream") - av_releaseStream = AVC("releaseStream") - av_play = AVC("play") - av_publish = AVC("publish") - av_onBWDone = AVC("onBWDone") - av_onFCSubscribe = AVC("onFCSubscribe") - av_onFCUnsubscribe = AVC("onFCUnsubscribe") - av__onbwcheck = AVC("_onbwcheck") - av__onbwdone = AVC("_onbwdone") - av_ping = AVC("ping") - av__checkbw = AVC("_checkbw") - av_close = AVC("close") - av_onStatus = AVC("onStatus") - av_code = AVC("code") - av_level = AVC("level") - av_NetStream_Failed = AVC("NetStream.Failed") - av_NetStream_Play_Failed = AVC("NetStream.Play.Failed") - av_NetConnection_Connect_InvalidApp = AVC("NetConnection.Connect.InvalidApp") - av_NetStream_Play_StreamNotFound = AVC("NetStream.Play.StreamNotFound") - av_NetStream_Play_Start = AVC("NetStream.Play.Start") - av_NetStream_Play_PublishNotify = AVC("NetStream.Play.PublishNotify") - av_NetStream_Publish_Start = AVC("NetStream.Publish.Start") - av_NetStream_Play_Complete = AVC("NetStream.Play.Complete") - av_NetStream_Play_Stop = AVC("NetStream.Play.Stop") - av_NetStream_Play_UnpublishNotify = AVC("NetStream.Play.UnpublishNotify") - av_NetStream_Seek_Notify = AVC("NetStream.Seek.Notify") - av_NetStream_Pause_Notify = AVC("NetStream.Pause.Notify") - av_playlist_ready = AVC("playlist_ready") - av_set_playlist = AVC("set_playlist") - av_FCPublish = AVC("FCPublish") - av_FCUnpublish = AVC("FCUnpublish") - av_live = AVC("live") - av_deleteStream = AVC("deleteStream") + av_connect = AVC("connect") + av_createStream = AVC("createStream") + av_play = AVC("play") + av_publish = AVC("publish") + av_secureToken = AVC("secureToken") + av_set_playlist = AVC("set_playlist") +) + +const ( + // av_setDataFrame is a static const global in rtmp.c + setDataFrame = "@setDataFrame" + + av__checkbw = "_checkbw" + av__onbwcheck = "_onbwcheck" + av__onbwdone = "_onbwdone" + av__result = "_result" + av_app = "app" + av_audioCodecs = "audioCodecs" + av_capabilities = "capabilities" + av_close = "close" + av_code = "code" + av_deleteStream = "deleteStream" + av_FCPublish = "FCPublish" + av_FCUnpublish = "FCUnpublish" + av_flashVer = "flashVer" + av_fpad = "fpad" + av_level = "level" + av_live = "live" + av_NetConnection_Connect_InvalidApp = "NetConnection.Connect.InvalidApp" + av_NetStream_Failed = "NetStream.Failed" + av_NetStream_Pause_Notify = "NetStream.Pause.Notify" + av_NetStream_Play_Complete = "NetStream.Play.Complete" + av_NetStream_Play_Failed = "NetStream.Play.Failed" + av_NetStream_Play_PublishNotify = "NetStream.Play.PublishNotify" + av_NetStream_Play_Start = "NetStream.Play.Start" + av_NetStream_Play_Stop = "NetStream.Play.Stop" + av_NetStream_Play_StreamNotFound = "NetStream.Play.StreamNotFound" + av_NetStream_Play_UnpublishNotify = "NetStream.Play.UnpublishNotify" + av_NetStream_Publish_Start = "NetStream.Publish.Start" + av_NetStream_Seek_Notify = "NetStream.Seek.Notify" + av_nonprivate = "nonprivate" + av_objectEncoding = "objectEncoding" + av_onBWDone = "onBWDone" + av_onFCSubscribe = "onFCSubscribe" + av_onFCUnsubscribe = "onFCUnsubscribe" + av_onStatus = "onStatus" + av_pageUrl = "pageUrl" + av_ping = "ping" + av_playlist_ready = "playlist_ready" + av_releaseStream = "releaseStream" + av_swfUrl = "swfUrl" + av_tcUrl = "tcUrl" + av_type = "type" + av_videoCodecs = "videoCodecs" + av_videoFunction = "videoFunction" ) var RTMPT_cmds = []string{ @@ -746,60 +750,60 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(unsafe.Pointer(incBytePtr(unsafe.Pointer(enc), 1))) - enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_app), CAV(&r.Link.app)) + enc = C_AMF_EncodeNamedString(enc, pend, av_app, CAV(&r.Link.app)) if enc == nil { return false } if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_type), CAV(&av_nonprivate)) + enc = C_AMF_EncodeNamedString(enc, pend, av_type, av_nonprivate) if enc == nil { return false } } if r.Link.flashVer.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_flashVer), CAV(&r.Link.flashVer)) + enc = C_AMF_EncodeNamedString(enc, pend, av_flashVer, CAV(&r.Link.flashVer)) if enc == nil { return false } } if r.Link.swfUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_swfUrl), CAV(&r.Link.swfUrl)) + enc = C_AMF_EncodeNamedString(enc, pend, av_swfUrl, CAV(&r.Link.swfUrl)) if enc == nil { return false } } if r.Link.tcUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_tcUrl), CAV(&r.Link.tcUrl)) + enc = C_AMF_EncodeNamedString(enc, pend, av_tcUrl, CAV(&r.Link.tcUrl)) if enc == nil { return false } } if r.Link.protocol&RTMP_FEATURE_WRITE == 0 { - enc = C_AMF_EncodeNamedBoolean(enc, pend, CAV(&av_fpad), false) + enc = C_AMF_EncodeNamedBoolean(enc, pend, av_fpad, false) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_capabilities), 15) + enc = C_AMF_EncodeNamedNumber(enc, pend, av_capabilities, 15) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_audioCodecs), float64(r.m_fAudioCodecs)) + enc = C_AMF_EncodeNamedNumber(enc, pend, av_audioCodecs, float64(r.m_fAudioCodecs)) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_videoCodecs), float64(r.m_fVideoCodecs)) + enc = C_AMF_EncodeNamedNumber(enc, pend, av_videoCodecs, float64(r.m_fVideoCodecs)) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_videoFunction), 1) + enc = C_AMF_EncodeNamedNumber(enc, pend, av_videoFunction, 1) if enc == nil { return false } if r.Link.pageUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, CAV(&av_pageUrl), CAV(&r.Link.pageUrl)) + enc = C_AMF_EncodeNamedString(enc, pend, av_pageUrl, CAV(&r.Link.pageUrl)) if enc == nil { return false } @@ -807,7 +811,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } if r.m_fEncoding != 0.0 || r.m_bSendEncoding != 0 { - enc = C_AMF_EncodeNamedNumber(enc, pend, CAV(&av_objectEncoding), float64(r.m_fEncoding)) + enc = C_AMF_EncodeNamedNumber(enc, pend, av_objectEncoding, float64(r.m_fEncoding)) if enc == nil { return false } @@ -903,7 +907,7 @@ func C_SendReleaseStream(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, CAV(&av_releaseStream)) + enc = C_AMF_EncodeString(enc, pend, av_releaseStream) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -936,7 +940,7 @@ func C_SendFCPublish(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, CAV(&av_FCPublish)) + enc = C_AMF_EncodeString(enc, pend, av_FCPublish) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -969,7 +973,7 @@ func C_SendFCUnpublish(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, CAV(&av_FCUnpublish)) + enc = C_AMF_EncodeString(enc, pend, av_FCUnpublish) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -1015,7 +1019,7 @@ func C_SendPublish(r *C_RTMP) (ok bool) { return false } - enc = C_AMF_EncodeString(enc, pend, CAV(&av_live)) + enc = C_AMF_EncodeString(enc, pend, av_live) if enc == nil { return false } @@ -1044,7 +1048,7 @@ func C_SendDeleteStream(r *C_RTMP, dStreamId float64) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, CAV(&av_deleteStream)) + enc = C_AMF_EncodeString(enc, pend, av_deleteStream) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -1100,7 +1104,7 @@ func C_SendCheckBW(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, CAV(&av__checkbw)) + enc = C_AMF_EncodeString(enc, pend, av__checkbw) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -1180,7 +1184,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { // RTMP_Log(RTMP_LOGDEBUG, "%s, server invoking <%s>", __FUNCTION__, method.av_val); switch method { - case CAV(&av__result): + case av__result: var methodInvoked C_AVal for i := int32(0); i < r.m_numCalls; i++ { if float64((*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), @@ -1233,49 +1237,48 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { } //C.free(unsafe.Pointer(methodInvoked.av_val)) - case CAV(&av_onBWDone): + case av_onBWDone: if r.m_nBWCheckCounter == 0 { C_SendCheckBW(r) } - case CAV(&av_onFCUnsubscribe), CAV(&av_onFCSubscribe): + case av_onFCUnsubscribe, av_onFCSubscribe: panic("Unsupported method av_onFCUnsubscribe/av_onFCSubscribe") - case CAV(&av_ping): + case av_ping: panic("Unsupported method av_ping") - case CAV(&av__onbwcheck): + case av__onbwcheck: panic("Unsupported method av_onbwcheck") - case CAV(&av__onbwdone): + case av__onbwdone: panic("Unsupported method av_onbwdone") - case CAV(&av_close): + case av_close: panic("Unsupported method av_close") - case CAV(&av_onStatus): + case av_onStatus: var obj2 C_AMFObject C_AMFProp_GetObject(C_AMF_GetProp(&obj, "", 3), &obj2) - code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, CAV(&av_code), -1)) + code := C_AMFProp_GetString(C_AMF_GetProp(&obj2, av_code, -1)) - level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, CAV(&av_level), -1)) // Not used. + level := C_AMFProp_GetString(C_AMF_GetProp(&obj2, av_level, -1)) // Not used. _ = level // TODO use new logger // RTMP_Log(RTMP_LOGDEBUG, "%s, onStatus: %s", __FUNCTION__, code.av_val); switch code { - case CAV(&av_NetStream_Failed), CAV(&av_NetStream_Play_Failed), - CAV(&av_NetStream_Play_StreamNotFound), CAV(&av_NetConnection_Connect_InvalidApp): + case av_NetStream_Failed, av_NetStream_Play_Failed, + av_NetStream_Play_StreamNotFound, av_NetConnection_Connect_InvalidApp: panic("Unsupported method av_NetStream/av_NetStream_Play_Failed/av_netSTream_Play_StreamNotFound/av_netConnection_Connect_invalidApp") - case CAV(&av_NetStream_Play_Start), CAV(&av_NetStream_Play_PublishNotify): + case av_NetStream_Play_Start, av_NetStream_Play_PublishNotify: panic("Unsupported method av_NetStream_Play_Start/av_NetStream_Play_PublishNotify") - case CAV(&av_NetStream_Publish_Start): - var i int32 + case av_NetStream_Publish_Start: r.m_bPlaying = true - for i = 0; i < int32(r.m_numCalls); i++ { + for i := int32(0); i < r.m_numCalls; i++ { if C_AVMATCH(&(*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), int(unsafe.Sizeof(*r.m_methodCalls))))).name, &av_publish) != 0 { C_AV_erase(r.m_methodCalls, (*int32)(&r.m_numCalls), int32(i), 1) @@ -1283,17 +1286,17 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { } } - case CAV(&av_NetStream_Play_Complete), CAV(&av_NetStream_Play_Stop), CAV(&av_NetStream_Play_UnpublishNotify): + case av_NetStream_Play_Complete, av_NetStream_Play_Stop, av_NetStream_Play_UnpublishNotify: panic("Unsupported method av_NetStream_Play_Complete/av_NetStream_Play_Stop/av_NetStream_Play_UnpublishNotify") - case CAV(&av_NetStream_Seek_Notify): + case av_NetStream_Seek_Notify: panic("Unsupported method av_netStream_Seek_Notify") - case CAV(&av_NetStream_Pause_Notify): + case av_NetStream_Pause_Notify: panic("Unsupported method av_NetStream_Pause_Notify") } - case CAV(&av_playlist_ready): + case av_playlist_ready: panic("Unsupported method av_playlist_ready") default: @@ -2065,7 +2068,7 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int { if pkt.m_packetType == RTMP_PACKET_TYPE_INFO { enc = (*[_Gi]byte)(unsafe.Pointer(C_AMF_EncodeString((*byte)(unsafe.Pointer(&enc[0])), - (*byte)(unsafe.Pointer(&pend[0])), CAV(&setDataFrame))))[:pkt.m_nBodySize] + (*byte)(unsafe.Pointer(&pend[0])), setDataFrame)))[:pkt.m_nBodySize] // TODO: work out what to do with this pkt.m_nBytesRead = uint32(float64(uintptr(unsafe.Pointer(&enc[0])) - From 28f5ec6a4768ce4523e78b932184fbaf06869d70 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 18:40:47 +0930 Subject: [PATCH 18/24] rtmp: make isready return a bool --- rtmp/rtmp.go | 4 ++-- rtmp/rtmp_headers.go | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index f46b6d57..0f1049ae 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -544,7 +544,7 @@ func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) (playing bool) { // TODO: read packet for !r.m_bPlaying && C_RTMP_IsConnected(r) != 0 && C_RTMP_ReadPacket(r, &packet) { // TODO: port is ready - if C_RTMPPacket_IsReady(&packet) != 0 { + if C_RTMPPacket_IsReady(&packet) { if packet.m_nBodySize == 0 { continue } @@ -1557,7 +1557,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) { } // TODO: port this - if C_RTMPPacket_IsReady(packet) != 0 { + if C_RTMPPacket_IsReady(packet) { if packet.m_hasAbsTimestamp == 0 { // timestamps seem to always be relative packet.m_nTimeStamp += *(*uint32)(incPtr(unsafe.Pointer(r.m_channelTimestamp), diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index c9985361..6b668c9c 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -147,11 +147,8 @@ type C_RTMPSockBuf struct { // RTMPPacket_IsReady(a) // rtmp.h +142 -func C_RTMPPacket_IsReady(p *C_RTMPPacket) int { - if p.m_nBytesRead == p.m_nBodySize { - return 1 - } - return 0 +func C_RTMPPacket_IsReady(p *C_RTMPPacket) bool { + return p.m_nBytesRead == p.m_nBodySize } // typedef struct RTMP_LNK From bf2a2ec7a8eae2db413ba628e3293acf395ca953 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Thu, 6 Sep 2018 19:00:28 +0930 Subject: [PATCH 19/24] rtmp: remove C_AVal from C_RTMP_METHOD --- rtmp/amf_headers.go | 11 -------- rtmp/rtmp.go | 67 +++++++++++++++++--------------------------- rtmp/rtmp_headers.go | 2 +- 3 files changed, 27 insertions(+), 53 deletions(-) diff --git a/rtmp/amf_headers.go b/rtmp/amf_headers.go index 7fc2dc3f..02473d6e 100644 --- a/rtmp/amf_headers.go +++ b/rtmp/amf_headers.go @@ -85,17 +85,6 @@ func CAV(av *C_AVal) string { return string((*[_Gi]byte)(unsafe.Pointer(av.av_val))[:av.av_len]) } -// #define AVMATCH(a1,a2) -// amf.h +63 -func C_AVMATCH(a1, a2 *C_AVal) int32 { - if a1.av_len == a2.av_len && memcmp(unsafe.Pointer(a1.av_val), - unsafe.Pointer(a2.av_val), int(a1.av_len)) == 0 { - return 1 - } else { - return 0 - } -} - // typedef struct AMF_Object // amf.h +67 type C_AMFObject struct { diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 0f1049ae..66f31fcd 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -66,16 +66,6 @@ const ( length = 512 ) -// TODO(kortschak): Reduce this list. -var ( - av_connect = AVC("connect") - av_createStream = AVC("createStream") - av_play = AVC("play") - av_publish = AVC("publish") - av_secureToken = AVC("secureToken") - av_set_playlist = AVC("set_playlist") -) - const ( // av_setDataFrame is a static const global in rtmp.c setDataFrame = "@setDataFrame" @@ -89,6 +79,8 @@ const ( av_capabilities = "capabilities" av_close = "close" av_code = "code" + av_connect = "connect" + av_createStream = "createStream" av_deleteStream = "deleteStream" av_FCPublish = "FCPublish" av_FCUnpublish = "FCUnpublish" @@ -116,8 +108,12 @@ const ( av_onStatus = "onStatus" av_pageUrl = "pageUrl" av_ping = "ping" + av_play = "play" av_playlist_ready = "playlist_ready" + av_publish = "publish" av_releaseStream = "releaseStream" + av_secureToken = "secureToken" + av_set_playlist = "set_playlist" av_swfUrl = "swfUrl" av_tcUrl = "tcUrl" av_type = "type" @@ -741,7 +737,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, CAV(&av_connect)) + enc = C_AMF_EncodeString(enc, pend, av_connect) r.m_numInvokes += 1 enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) @@ -877,7 +873,7 @@ func C_RTMP_SendCreateStream(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, CAV(&av_createStream)) + enc = C_AMF_EncodeString(enc, pend, av_createStream) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -1008,7 +1004,7 @@ func C_SendPublish(r *C_RTMP) (ok bool) { packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] enc = (*byte)(unsafe.Pointer(packet.m_body)) - enc = C_AMF_EncodeString(enc, pend, CAV(&av_publish)) + enc = C_AMF_EncodeString(enc, pend, av_publish) r.m_numInvokes++ enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL @@ -1123,18 +1119,13 @@ func C_AV_erase(vals *C_RTMP_METHOD, num *int32, i, freeit int32) { //C.free(unsafe.Pointer((*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(vals), int(i), //int(unsafe.Sizeof(*vals))))).name.av_val)) } - (*num)-- + *num-- for ; i < *num; i++ { *(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(vals), int(i), int(unsafe.Sizeof(*vals)))) = *(*C_RTMP_METHOD)(incPtr( unsafe.Pointer(vals), int(i+1), int(unsafe.Sizeof(*vals)))) } - (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(vals), int(i), - int(unsafe.Sizeof(*vals))))).name.av_val = nil - (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(vals), int(i), - int(unsafe.Sizeof(*vals))))).name.av_len = 0 - (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(vals), int(i), - int(unsafe.Sizeof(*vals))))).num = 0 + *(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(vals), int(i), int(unsafe.Sizeof(*vals)))) = C_RTMP_METHOD{} } // void AV_queue(RTMP_METHOD** vals, int* num, C_AVal* av, int txn); @@ -1144,14 +1135,11 @@ func C_AV_queue(vals **C_RTMP_METHOD, num *int32, av string, txn int32) { *vals = (*C_RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)*int32(unsafe.Sizeof(**vals))))) } - tmp := make([]byte, len(av)+1) - copy(tmp, av) - (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), int(*num), - int(unsafe.Sizeof(**vals))))).num = int32(txn) - (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), int(*num), - int(unsafe.Sizeof(**vals))))).name.av_len = int32(len(av)) - (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), int(*num), - int(unsafe.Sizeof(**vals))))).name.av_val = &tmp[0] + meth := (*C_RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), int(*num), int(unsafe.Sizeof(**vals)))) + *meth = C_RTMP_METHOD{ + name: av, + num: int32(txn), + } *num++ } @@ -1185,17 +1173,15 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { switch method { case av__result: - var methodInvoked C_AVal + var methodInvoked string for i := int32(0); i < r.m_numCalls; i++ { - if float64((*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), - int(unsafe.Sizeof(*r.m_methodCalls))))).num) == txn { - methodInvoked = (*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), - int(i), int(unsafe.Sizeof(*r.m_methodCalls))))).name + if float64((*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), int(unsafe.Sizeof(*r.m_methodCalls))))).num) == txn { + methodInvoked = (*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), int(unsafe.Sizeof(*r.m_methodCalls)))).name C_AV_erase(r.m_methodCalls, (*int32)(&r.m_numCalls), int32(i), 0) break } } - if methodInvoked.av_val == nil { + if methodInvoked == "" { // TODO use new logger here //RTMP_Log(RTMP_LOGDEBUG, "%s, received result id %f without matching request", //__FUNCTION__, txn); @@ -1204,8 +1190,8 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { // TODO use new logger here //RTMP_Log(RTMP_LOGDEBUG, "%s, received result for method call <%s>", __FUNCTION__, //methodInvoked.av_val); - switch { - case C_AVMATCH(&methodInvoked, &av_connect) != 0: + switch methodInvoked { + case av_connect: if r.Link.token.av_len != 0 { panic("No support for link token") @@ -1222,7 +1208,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { panic("Link protocol has no RTMP_FEATURE_WRITE") } - case C_AVMATCH(&methodInvoked, &av_createStream) != 0: + case av_createStream: r.m_stream_id = int32(C_AMFProp_GetNumber(C_AMF_GetProp(&obj, "", 3))) if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { @@ -1231,8 +1217,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { panic("Link protocol has no RTMP_FEATURE_WRITE") } - case C_AVMATCH(&methodInvoked, &av_play) != 0 || - C_AVMATCH(&methodInvoked, &av_publish) != 0: + case av_play, av_publish: panic("Unsupported method av_play/av_publish") } //C.free(unsafe.Pointer(methodInvoked.av_val)) @@ -1279,8 +1264,8 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { case av_NetStream_Publish_Start: r.m_bPlaying = true for i := int32(0); i < r.m_numCalls; i++ { - if C_AVMATCH(&(*(*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), - int(unsafe.Sizeof(*r.m_methodCalls))))).name, &av_publish) != 0 { + method_name := (*C_RTMP_METHOD)(incPtr(unsafe.Pointer(r.m_methodCalls), int(i), int(unsafe.Sizeof(*r.m_methodCalls)))).name + if method_name == av_publish { C_AV_erase(r.m_methodCalls, (*int32)(&r.m_numCalls), int32(i), 1) break } diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index 6b668c9c..a6a9ec83 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -205,7 +205,7 @@ type C_RTMP_READ struct { // typedef struct RTMPMethod // rtmp.h +231 type C_RTMP_METHOD struct { - name C_AVal + name string num int32 } From 9212bed0ed442ce7ce430ee3f5d23eefe53834f3 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 7 Sep 2018 07:01:59 +0930 Subject: [PATCH 20/24] rtmp: simplify url parsing --- rtmp/parseurl.go | 24 ++++++++++++------------ rtmp/rtmp.go | 27 +++++++++------------------ 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/rtmp/parseurl.go b/rtmp/parseurl.go index 7af55ebb..4c541fdc 100644 --- a/rtmp/parseurl.go +++ b/rtmp/parseurl.go @@ -39,17 +39,14 @@ import ( "unsafe" ) -// int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, -// AVal *playpath, AVal *app); +// int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, AVal *playpath, AVal *app); // parseurl.c +33 -func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32, - playpath *C_AVal, app *C_AVal) int { - +func C_RTMP_ParseURL(url *byte, host *C_AVal, port *uint16, playpath, app *C_AVal) (protocol int32, ok bool) { var p, end, col, ques, slash *byte // TODO: use our logger here // RTMP_Log(RTMP_LOGDEBUG, "Parsing..."); - *protocol = RTMP_PROTOCOL_RTMP + protocol = RTMP_PROTOCOL_RTMP *port = 0 playpath.av_len = 0 playpath.av_val = nil @@ -61,7 +58,7 @@ func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32, if p == nil { // TODO: use our logger here log.Println("RTMP URL: No :// in url!") - return 0 + return protocol, false } /* NOTE: the following code nees to be ported if we're using anything other than @@ -99,7 +96,7 @@ func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32, if *p == 0 { // TODO: use new logger here // RTMP_Log(RTMP_LOGWARNING, "No hostname in URL!"); - return 0 + return protocol, false } end = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + uintptr(strlen(p)))) @@ -135,20 +132,23 @@ func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32, if *p == ':' { var p2 uint32 p = (*byte)(incBytePtr(unsafe.Pointer(p), 1)) - tmp, _ := strconv.Atoi(cStrToGoStr(p)) + tmp, err := strconv.Atoi(cStrToGoStr(p)) + if err != nil { + return protocol, false + } p2 = uint32(tmp) if p2 > 65535 { // TODO: use new logger with this // RTMP_Log(RTMP_LOGWARNING, "Invalid port number!"); } else { - *port = p2 + *port = uint16(p2) } } if slash == nil { // TODO: use new logger // RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!"); - return 1 + return protocol, true } p = (*byte)(incBytePtr(unsafe.Pointer(slash), 1)) @@ -225,7 +225,7 @@ func C_RTMP_ParseURL(url *byte, protocol *int32, host *C_AVal, port *uint32, C_RTMP_ParsePlaypath(&av, playpath) } - return 1 + return protocol, true } // void RTMP_ParsePlaypath(AVal *in, AVal *out); diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 66f31fcd..3c574d04 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -295,34 +295,25 @@ func C_SocksSetup(r *C_RTMP, sockshost *C_AVal) { // int RTMP_SetupURL(RTMP *r, char* url); // rtmp.c +757 // NOTE: code dealing with rtmp over http has been disregarded -func C_RTMP_SetupURL(r *C_RTMP, u string) (ok bool) { - url := goStrToCStr(u) +func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) { + u := goStrToCStr(addr) + length := strlen(u) - var length int32 - var port uint32 - port = 0 - - length = strlen(url) - // TODO: port this - ret := int32(C_RTMP_ParseURL((*byte)(unsafe.Pointer(url)), (*int32)( - unsafe.Pointer(&r.Link.protocol)), &r.Link.hostname, (*uint32)( - unsafe.Pointer(&port)), &r.Link.playpath0, &r.Link.app)) - if ret == 0 { + r.Link.protocol, ok = C_RTMP_ParseURL(u, &r.Link.hostname, &r.Link.port, &r.Link.playpath0, &r.Link.app) + if !ok { return false } - - r.Link.port = uint16(port) r.Link.playpath = r.Link.playpath0 if r.Link.tcUrl.av_len == 0 { - r.Link.tcUrl.av_val = (*byte)(unsafe.Pointer(url)) + r.Link.tcUrl.av_val = (*byte)(unsafe.Pointer(u)) if r.Link.app.av_len != 0 { if int(uintptr(unsafe.Pointer(r.Link.app.av_val))) < - int(uintptr(incBytePtr(unsafe.Pointer(url), int(length)))) { + int(uintptr(incBytePtr(unsafe.Pointer(u), int(length)))) { r.Link.tcUrl.av_len = int32(int(r.Link.app.av_len) + int(uintptr(decBytePtr(unsafe.Pointer(r.Link.app.av_val), - int(uintptr(unsafe.Pointer(url))))))) + int(uintptr(unsafe.Pointer(u))))))) } else { length = int32(r.Link.hostname.av_len) + int32(r.Link.app.av_len) + int32(len("rtmpte://:65535/\x00")) @@ -343,7 +334,7 @@ func C_RTMP_SetupURL(r *C_RTMP, u string) (ok bool) { r.Link.lFlags |= RTMP_LF_FTCU } } else { - r.Link.tcUrl.av_len = int32(strlen(url)) + r.Link.tcUrl.av_len = int32(strlen(u)) } } From dde20e1a7bd625605c83d55374b98f76d59b8baa Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 7 Sep 2018 12:30:25 +0930 Subject: [PATCH 21/24] rtmp: more simplification of url parsing --- rtmp/amf_headers.go | 9 ++ rtmp/parseurl.go | 328 +++++++------------------------------------- rtmp/rtmp.go | 36 +---- 3 files changed, 63 insertions(+), 310 deletions(-) diff --git a/rtmp/amf_headers.go b/rtmp/amf_headers.go index 02473d6e..f706fdb1 100644 --- a/rtmp/amf_headers.go +++ b/rtmp/amf_headers.go @@ -32,6 +32,7 @@ LICENSE package rtmp import ( + "fmt" "unsafe" ) @@ -68,6 +69,8 @@ type C_AVal struct { av_len int32 } +func (s *C_AVal) String() string { return CAV(s) } + // C_AVal is in amf.h // amf.h +62 func AVC(str string) C_AVal { @@ -82,6 +85,12 @@ func AVC(str string) C_AVal { } func CAV(av *C_AVal) string { + if av.av_len <= 0 || av.av_val == nil { + if av.av_len < 0 { + panic(fmt.Sprintf("invalid length: %d", av.av_len)) + } + return "" + } return string((*[_Gi]byte)(unsafe.Pointer(av.av_val))[:av.av_len]) } diff --git a/rtmp/parseurl.go b/rtmp/parseurl.go index 4c541fdc..19df4d19 100644 --- a/rtmp/parseurl.go +++ b/rtmp/parseurl.go @@ -6,6 +6,7 @@ DESCRIPTION See Readme.md AUTHOR + Dan Kortschak Saxon Nelson-Milton LICENSE @@ -32,303 +33,72 @@ LICENSE package rtmp import ( - "fmt" "log" + "net/url" + "path" "strconv" "strings" - "unsafe" ) // int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, AVal *playpath, AVal *app); // parseurl.c +33 -func C_RTMP_ParseURL(url *byte, host *C_AVal, port *uint16, playpath, app *C_AVal) (protocol int32, ok bool) { - var p, end, col, ques, slash *byte - // TODO: use our logger here - // RTMP_Log(RTMP_LOGDEBUG, "Parsing..."); - - protocol = RTMP_PROTOCOL_RTMP - *port = 0 - playpath.av_len = 0 - playpath.av_val = nil - app.av_len = 0 - app.av_val = nil - - p = strstr(url, goStrToCStr("://")) - - if p == nil { - // TODO: use our logger here - log.Println("RTMP URL: No :// in url!") - return protocol, false - } - /* - NOTE: the following code nees to be ported if we're using anything other than - rtmp! - { - int len = (int)(p-url); - - if(len == 4 && strncasecmp(url, "rtmp", 4)==0) - *protocol = RTMP_PROTOCOL_RTMP; - else if(len == 5 && strncasecmp(url, "rtmpt", 5)==0) - *protocol = RTMP_PROTOCOL_RTMPT; - else if(len == 5 && strncasecmp(url, "rtmps", 5)==0) - *protocol = RTMP_PROTOCOL_RTMPS; - else if(len == 5 && strncasecmp(url, "rtmpe", 5)==0) - *protocol = RTMP_PROTOCOL_RTMPE; - else if(len == 5 && strncasecmp(url, "rtmfp", 5)==0) - *protocol = RTMP_PROTOCOL_RTMFP; - else if(len == 6 && strncasecmp(url, "rtmpte", 6)==0) - *protocol = RTMP_PROTOCOL_RTMPTE; - else if(len == 6 && strncasecmp(url, "rtmpts", 6)==0) - *protocol = RTMP_PROTOCOL_RTMPTS; - else { - RTMP_Log(RTMP_LOGWARNING, "Unknown protocol!\n"); - goto parsehost; - } - } - */ - // TODO: implement new logger here - // RTMP_Log(RTMP_LOGDEBUG, "Parsed protocol: %d", *protocol); - - // Get the hostname - p = (*byte)(incBytePtr(unsafe.Pointer(p), 3)) - - // check for sudden death - if *p == 0 { - // TODO: use new logger here - // RTMP_Log(RTMP_LOGWARNING, "No hostname in URL!"); - return protocol, false +func C_RTMP_ParseURL(addr string) (protocol int32, host C_AVal, port uint16, playpath, app C_AVal, ok bool) { + u, err := url.Parse(addr) + if err != nil { + log.Printf("failed to parse addr: %v", err) + return protocol, host, port, app, playpath, false } - end = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + uintptr(strlen(p)))) - col = strchr(p, ':') - ques = strchr(p, '?') - slash = strchr(p, '/') - - { - var hostlen int32 - if slash != nil { - hostlen = int32(uintptr(unsafe.Pointer(slash)) - uintptr(unsafe.Pointer(p))) - } else { - hostlen = int32(uintptr(unsafe.Pointer(end)) - uintptr(unsafe.Pointer(p))) - } - if col != nil && int32(uintptr(unsafe.Pointer(col))-uintptr(unsafe.Pointer(p))) < hostlen { - hostlen = int32(uintptr(unsafe.Pointer(col)) - uintptr(unsafe.Pointer(p))) - } - - if hostlen < 256 { - host.av_val = (*byte)(unsafe.Pointer(p)) - host.av_len = hostlen - // TODO: use new logger with this - //RTMP_Log(RTMP_LOGDEBUG, "Parsed host : %.*s", hostlen, host.av_val); - } else { - // TODO: use new logger with this - // RTMP_Log(RTMP_LOGWARNING, "Hostname exceeds 255 characters!"); - } - - p = (*byte)(incBytePtr(unsafe.Pointer(p), int(hostlen))) + switch u.Scheme { + case "rtmp": + protocol = RTMP_PROTOCOL_RTMP + case "rtmpt": + protocol = RTMP_PROTOCOL_RTMPT + case "rtmps": + protocol = RTMP_PROTOCOL_RTMPS + case "rtmpe": + protocol = RTMP_PROTOCOL_RTMPE + case "rtmfp": + protocol = RTMP_PROTOCOL_RTMFP + case "rtmpte": + protocol = RTMP_PROTOCOL_RTMPTE + case "rtmpts": + protocol = RTMP_PROTOCOL_RTMPTS + default: + log.Printf("unknown scheme: %q", u.Scheme) + return protocol, host, port, app, playpath, false } - // get port number if available - if *p == ':' { - var p2 uint32 - p = (*byte)(incBytePtr(unsafe.Pointer(p), 1)) - tmp, err := strconv.Atoi(cStrToGoStr(p)) + host = AVC(u.Host) + if p := u.Port(); p != "" { + pi, err := strconv.Atoi(p) if err != nil { - return protocol, false - } - p2 = uint32(tmp) - if p2 > 65535 { - // TODO: use new logger with this - // RTMP_Log(RTMP_LOGWARNING, "Invalid port number!"); - } else { - *port = uint16(p2) + return protocol, host, port, app, playpath, false } + port = uint16(pi) } - if slash == nil { - // TODO: use new logger - // RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!"); - return protocol, true + if !path.IsAbs(u.Path) { + return protocol, host, port, playpath, app, true } - - p = (*byte)(incBytePtr(unsafe.Pointer(slash), 1)) - - { - /* parse application - * - * rtmp://host[:port]/app[/appinstance][/...] - * application = app[/appinstance] - */ - - var slash2 *byte - var slash3 *byte = nil - var slash4 *byte = nil - var applen, appnamelen int32 - - slash2 = strchr(p, '/') - - if slash2 != nil { - slash3 = strchr((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(slash2))+ - uintptr(1))), '/') + elems := strings.SplitN(u.Path[1:], "/", 3) + app = AVC(elems[0]) + playpath = AVC(elems[1]) + if len(elems) == 3 && len(elems[2]) != 0 { + inst := path.Join(elems[1:]...) + switch ext := path.Ext(inst); ext { + case ".f4v", ".mp4": + inst = "mp4:" + inst[:len(inst)-len(ext)] + case ".mp3": + inst = "mp3:" + inst[:len(inst)-len(ext)] + case ".flv": + inst = inst[:len(inst)-len(ext)] } - if slash3 != nil { - slash4 = strchr((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(slash3))+ - uintptr(1))), '/') + if u.RawQuery != "" { + inst += "?" + u.RawQuery } - - // ondemand, pass all parameters as app - applen = int32(uintptr(unsafe.Pointer(end)) - uintptr(unsafe.Pointer(p))) - appnamelen = applen - - switch { - case ques != nil && strstr(p, goStrToCStr("slist=")) != nil: - appnamelen = int32(uintptr(unsafe.Pointer(ques)) - uintptr(unsafe.Pointer(p))) - case strings.Compare(cStrToGoStr(p)[:9], "ondemand/") == 0: - /* app = ondemand/foobar, only pass app=ondemand */ - applen = 8 - appnamelen = 8 - default: - switch { - case slash4 != nil: - appnamelen = int32(uintptr(unsafe.Pointer(slash4)) - uintptr( - unsafe.Pointer(p))) - case slash3 != nil: - appnamelen = int32(uintptr(unsafe.Pointer(slash3)) - uintptr( - unsafe.Pointer(p))) - case slash2 != nil: - appnamelen = int32(uintptr(unsafe.Pointer(slash2)) - uintptr( - unsafe.Pointer(p))) - } - - applen = appnamelen - } - - app.av_val = (*byte)(unsafe.Pointer(p)) - app.av_len = applen - // TODO: use new logging here - // RTMP_Log(RTMP_LOGDEBUG, "Parsed app : %.*s", applen, p); - - p = (*byte)(incBytePtr(unsafe.Pointer(p), int(appnamelen))) + playpath = AVC(inst) } - if *p == '/' { - p = (*byte)(incBytePtr(unsafe.Pointer(p), 1)) - } - // NOTE: don't think we currently need this section - see 787 for this func - - if int(uintptr(unsafe.Pointer(end))-uintptr(unsafe.Pointer(p))) != 0 { - var av C_AVal - av.av_val = (*byte)(unsafe.Pointer(p)) - av.av_len = int32(uintptr(unsafe.Pointer(end)) - uintptr(unsafe.Pointer(p))) - // TODO: port THis - //C.RTMP_ParsePlaypath(&av, playpath) - C_RTMP_ParsePlaypath(&av, playpath) - } - - return protocol, true -} - -// void RTMP_ParsePlaypath(AVal *in, AVal *out); -// parseurl.c +201 -func C_RTMP_ParsePlaypath(in, out *C_AVal) { - var addMP4 int32 = 0 - var addMP3 int32 = 0 - var subExt int32 = 0 - playpath := in.av_val - var temp, q *byte - var ext *byte = nil - ppstart := (*byte)(unsafe.Pointer(playpath)) - var streamname, destptr, p *byte - - pplen := in.av_len - - out.av_val = nil - out.av_len = 0 - - temp = strstr((*byte)(unsafe.Pointer(ppstart)), goStrToCStr("slist=")) - if *ppstart == '?' && temp != nil { - ppstart = (*byte)(incBytePtr(unsafe.Pointer(temp), 6)) - pplen = strlen(ppstart) - - temp = strchr(ppstart, '&') - - if temp != nil { - pplen = int32(uintptr(unsafe.Pointer(temp)) - uintptr(unsafe.Pointer(ppstart))) - } - } - - q = strchr(ppstart, '?') - - if pplen >= 4 { - if q != nil { - ext = (*byte)(decBytePtr(unsafe.Pointer(q), 4)) - } else { - ext = (*byte)(sliceToPtr((*[_Gi]byte)(unsafe.Pointer(ppstart))[uintptr(pplen)-4:])) - } - switch { - case strings.Compare(cStrToGoStr(ext)[:4], ".f4v") == 0 || - strings.Compare(cStrToGoStr(ext)[:4], ".mp4") == 0: - addMP4 = 1 - subExt = 1 - case ppstart == (*byte)(unsafe.Pointer(playpath)) && strings.Compare( - cStrToGoStr(ext)[:4], ".flv") == 0: - subExt = 1 - case strings.Compare(cStrToGoStr(ext)[:4], ".mp3") == 0: - addMP3 = 1 - subExt = 1 - } - } - - streamname = (*byte)(malloc(uintptr(pplen + 4 + 1))) - - if streamname == nil { - return - } - - destptr = streamname - switch { - case addMP4 != 0: - if strings.Compare(cStrToGoStr(ppstart)[:4], "mp4") != 0 { - memmove(unsafe.Pointer(destptr), unsafe.Pointer(goStrToCStr("mp4:")), - uintptr(len("mp4:"))) - destptr = (*byte)(incBytePtr(unsafe.Pointer(destptr), 4)) - } else { - subExt = 0 - } - case addMP3 != 0: - if strings.Compare(cStrToGoStr(ppstart)[:4], "mp3") != 0 { - memmove(unsafe.Pointer(destptr), unsafe.Pointer(goStrToCStr("mp3:")), - uintptr(len("mp4:"))) - destptr = (*byte)(incBytePtr(unsafe.Pointer(destptr), 4)) - } else { - subExt = 0 - } - } - - p = ppstart - for pplen > 0 { - if subExt != 0 && p == ext { - p = (*byte)(incBytePtr(unsafe.Pointer(p), 4)) - pplen -= 4 - continue - } - if *p == '%' { - var c uint32 - fmt.Sscanf(cStrToGoStr((*byte)(incBytePtr(unsafe.Pointer(p), 1))), "%02x", &c) - (*[_Gi]byte)(unsafe.Pointer(destptr))[0] = byte(c) - destptr = (*byte)(incBytePtr(unsafe.Pointer(destptr), 1)) - pplen -= 3 - p = (*byte)(incBytePtr(unsafe.Pointer(p), 3)) - } else { - (*[_Gi]byte)(unsafe.Pointer(destptr))[0] = *p - destptr = (*byte)(incBytePtr(unsafe.Pointer(destptr), 1)) - p = (*byte)(incBytePtr(unsafe.Pointer(p), 1)) - pplen-- - } - } - *destptr = '\x00' - - out.av_val = (*byte)(unsafe.Pointer(streamname)) - out.av_len = int32(uintptr(unsafe.Pointer(destptr)) - uintptr(unsafe.Pointer( - streamname))) + return protocol, host, port, app, playpath, true } diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 3c574d04..61484af0 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -296,45 +296,19 @@ func C_SocksSetup(r *C_RTMP, sockshost *C_AVal) { // rtmp.c +757 // NOTE: code dealing with rtmp over http has been disregarded func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) { - u := goStrToCStr(addr) - length := strlen(u) - - r.Link.protocol, ok = C_RTMP_ParseURL(u, &r.Link.hostname, &r.Link.port, &r.Link.playpath0, &r.Link.app) + r.Link.protocol, r.Link.hostname, r.Link.port, r.Link.app, r.Link.playpath0, ok = C_RTMP_ParseURL(addr) if !ok { return false } r.Link.playpath = r.Link.playpath0 if r.Link.tcUrl.av_len == 0 { - r.Link.tcUrl.av_val = (*byte)(unsafe.Pointer(u)) if r.Link.app.av_len != 0 { - if int(uintptr(unsafe.Pointer(r.Link.app.av_val))) < - int(uintptr(incBytePtr(unsafe.Pointer(u), int(length)))) { - - r.Link.tcUrl.av_len = int32(int(r.Link.app.av_len) + - int(uintptr(decBytePtr(unsafe.Pointer(r.Link.app.av_val), - int(uintptr(unsafe.Pointer(u))))))) - } else { - length = int32(r.Link.hostname.av_len) + int32(r.Link.app.av_len) + - int32(len("rtmpte://:65535/\x00")) - - r.Link.tcUrl.av_val = (*byte)(malloc(uintptr(uintptr(length)))) - hostname := string((*[_Gi]byte)(unsafe.Pointer(r.Link.hostname.av_val))[:r.Link.hostname.av_len]) - - app := string((*[_Gi]byte)(unsafe.Pointer(r.Link.app.av_val))[:r.Link.app.av_len]) - - fString := fmt.Sprintf("%v://%v:%v/%v", - RTMPProtocolStringsLower[r.Link.protocol], hostname, r.Link.port, app) - - r.Link.tcUrl.av_val = (*byte)(bToUP(goStrToCStr(fString))) - r.Link.tcUrl.av_len = int32(strLen(RTMPProtocolStringsLower[r.Link.protocol]) + - strLen(string("://")) + strLen(hostname) + strLen(string(":")) + - strLen(strconv.Itoa(int(r.Link.port))) + strLen(string("/")) + strLen(app)) - - r.Link.lFlags |= RTMP_LF_FTCU - } + r.Link.tcUrl = AVC(fmt.Sprintf("%v://%v:%v/%v", + RTMPProtocolStringsLower[r.Link.protocol], &r.Link.hostname, r.Link.port, &r.Link.app)) + r.Link.lFlags |= RTMP_LF_FTCU } else { - r.Link.tcUrl.av_len = int32(strlen(u)) + r.Link.tcUrl = AVC(addr) } } From 834c2bc632fc0ff218caaf7cdb334b16731a5a3c Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 7 Sep 2018 20:40:46 +0930 Subject: [PATCH 22/24] rtmp: remove C_AVal --- rtmp/amf_headers.go | 37 --------------- rtmp/parseurl.go | 23 +++++---- rtmp/rtmp.go | 108 ++++++++++++++++++------------------------- rtmp/rtmp_headers.go | 30 ++++++------ 4 files changed, 70 insertions(+), 128 deletions(-) diff --git a/rtmp/amf_headers.go b/rtmp/amf_headers.go index f706fdb1..8ca2d429 100644 --- a/rtmp/amf_headers.go +++ b/rtmp/amf_headers.go @@ -31,11 +31,6 @@ LICENSE */ package rtmp -import ( - "fmt" - "unsafe" -) - const ( AMF_NUMBER = iota AMF_BOOLEAN @@ -62,38 +57,6 @@ const ( // amf.h +40 type C_AMFDataType int32 -// typedef struct C_AVal -// amf.h +57 -type C_AVal struct { - av_val *byte - av_len int32 -} - -func (s *C_AVal) String() string { return CAV(s) } - -// C_AVal is in amf.h -// amf.h +62 -func AVC(str string) C_AVal { - var aval C_AVal - if len(str) != 0 { - aval.av_val = &([]byte(str)[0]) - } else { - aval.av_val = nil - } - aval.av_len = int32(len(str)) - return aval -} - -func CAV(av *C_AVal) string { - if av.av_len <= 0 || av.av_val == nil { - if av.av_len < 0 { - panic(fmt.Sprintf("invalid length: %d", av.av_len)) - } - return "" - } - return string((*[_Gi]byte)(unsafe.Pointer(av.av_val))[:av.av_len]) -} - // typedef struct AMF_Object // amf.h +67 type C_AMFObject struct { diff --git a/rtmp/parseurl.go b/rtmp/parseurl.go index 19df4d19..5c8872a5 100644 --- a/rtmp/parseurl.go +++ b/rtmp/parseurl.go @@ -42,7 +42,7 @@ import ( // int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, AVal *playpath, AVal *app); // parseurl.c +33 -func C_RTMP_ParseURL(addr string) (protocol int32, host C_AVal, port uint16, playpath, app C_AVal, ok bool) { +func C_RTMP_ParseURL(addr string) (protocol int32, host string, port uint16, app, playpath string, ok bool) { u, err := url.Parse(addr) if err != nil { log.Printf("failed to parse addr: %v", err) @@ -69,7 +69,7 @@ func C_RTMP_ParseURL(addr string) (protocol int32, host C_AVal, port uint16, pla return protocol, host, port, app, playpath, false } - host = AVC(u.Host) + host = u.Host if p := u.Port(); p != "" { pi, err := strconv.Atoi(p) if err != nil { @@ -79,25 +79,24 @@ func C_RTMP_ParseURL(addr string) (protocol int32, host C_AVal, port uint16, pla } if !path.IsAbs(u.Path) { - return protocol, host, port, playpath, app, true + return protocol, host, port, app, playpath, true } elems := strings.SplitN(u.Path[1:], "/", 3) - app = AVC(elems[0]) - playpath = AVC(elems[1]) + app = elems[0] + playpath = elems[1] if len(elems) == 3 && len(elems[2]) != 0 { - inst := path.Join(elems[1:]...) - switch ext := path.Ext(inst); ext { + playpath = path.Join(elems[1:]...) + switch ext := path.Ext(playpath); ext { case ".f4v", ".mp4": - inst = "mp4:" + inst[:len(inst)-len(ext)] + playpath = "mp4:" + playpath[:len(playpath)-len(ext)] case ".mp3": - inst = "mp3:" + inst[:len(inst)-len(ext)] + playpath = "mp3:" + playpath[:len(playpath)-len(ext)] case ".flv": - inst = inst[:len(inst)-len(ext)] + playpath = playpath[:len(playpath)-len(ext)] } if u.RawQuery != "" { - inst += "?" + u.RawQuery + playpath += "?" + u.RawQuery } - playpath = AVC(inst) } return protocol, host, port, app, playpath, true diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 61484af0..00528432 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -52,6 +52,7 @@ import ( "log" "math/rand" "strconv" + "strings" "time" "unsafe" @@ -263,31 +264,21 @@ func C_RTMP_SetBufferMS(r *C_RTMP, size int32) { // void SocksSetup(RTMP *r, C_AVal* sockshost); // rtmp.c +410 -func C_SocksSetup(r *C_RTMP, sockshost *C_AVal) { - if sockshost.av_len != 0 { - socksport := strchr((*byte)(unsafe.Pointer(sockshost.av_val)), ':') - hostname := strdup((*byte)(unsafe.Pointer(sockshost.av_val))) - - if unsafe.Pointer(socksport) != nil { - (*[_Gi]byte)(unsafe.Pointer(hostname))[uintptr(decBytePtr(unsafe.Pointer(socksport), - int(uintptr(unsafe.Pointer(sockshost.av_val)))))] = '\000' - r.Link.sockshost.av_val = (*byte)(unsafe.Pointer(hostname)) - r.Link.sockshost.av_len = int32(strlen(hostname)) - - value, err := strconv.Atoi(string((*[_Gi]byte)(unsafe.Pointer(uintptr( - unsafe.Pointer(socksport)) + uintptr(1)))[:strlen((*byte)(unsafe.Pointer( - uintptr(unsafe.Pointer(socksport))+uintptr(1))))+1])) +func C_SocksSetup(r *C_RTMP, sockshost string) { + if sockshost != "" { + p := strings.SplitN(sockshost, ":", 2) + r.Link.sockshost = p[0] + r.Link.socksport = 1080 + if len(p) != 1 { + port, err := strconv.Atoi(p[1]) if err != nil { + port = 1080 log.Println("C_SocksSetup: bad string conversion!") } - if uintptr(unsafe.Pointer(socksport)) == 0 { - value = 1080 - } - r.Link.socksport = uint16(value) + r.Link.socksport = uint16(port) } } else { - r.Link.sockshost.av_val = nil - r.Link.sockshost.av_len = 0 + r.Link.sockshost = "" r.Link.socksport = 0 } } @@ -302,17 +293,17 @@ func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) { } r.Link.playpath = r.Link.playpath0 - if r.Link.tcUrl.av_len == 0 { - if r.Link.app.av_len != 0 { - r.Link.tcUrl = AVC(fmt.Sprintf("%v://%v:%v/%v", - RTMPProtocolStringsLower[r.Link.protocol], &r.Link.hostname, r.Link.port, &r.Link.app)) + if r.Link.tcUrl == "" { + if r.Link.app != "" { + r.Link.tcUrl = fmt.Sprintf("%v://%v:%v/%v", + RTMPProtocolStringsLower[r.Link.protocol], r.Link.hostname, r.Link.port, r.Link.app) r.Link.lFlags |= RTMP_LF_FTCU } else { - r.Link.tcUrl = AVC(addr) + r.Link.tcUrl = addr } } - C_SocksSetup(r, &r.Link.sockshost) + C_SocksSetup(r, r.Link.sockshost) if r.Link.port == 0 { switch { @@ -329,20 +320,11 @@ func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) { // int add_addr_info(struct sockaddr_in *service, AVal *host, int port) // rtmp.c +869 -func C_add_addr_info(service *C.sockaddr_in, host *C_AVal, port C.int) (ok bool) { - var hostname *byte - if (*[1 << 20]byte)(unsafe.Pointer(host.av_val))[host.av_len] != 0 { - name := make([]byte, host.av_len+1) - copy(name, (*[1 << 20]byte)(unsafe.Pointer(host.av_val))[:host.av_len]) - name[len(name)-1] = 0 - hostname = (*byte)(unsafe.Pointer(&name[0])) - } else { - hostname = host.av_val - } - - service.sin_addr.s_addr = C.inet_addr((*C.char)(unsafe.Pointer(hostname))) +func C_add_addr_info(service *C.sockaddr_in, hostname string, port uint16) (ok bool) { + h := (*C.char)(unsafe.Pointer(goStrToCStr(hostname))) + service.sin_addr.s_addr = C.inet_addr(h) if service.sin_addr.s_addr == C.INADDR_NONE { - host := C.gethostbyname((*C.char)(unsafe.Pointer(hostname))) + host := C.gethostbyname(h) if host == nil || *host.h_addr_list == nil { //RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname) return false @@ -350,7 +332,7 @@ func C_add_addr_info(service *C.sockaddr_in, host *C_AVal, port C.int) (ok bool) service.sin_addr = *(*C.struct_in_addr)(unsafe.Pointer(*host.h_addr_list)) } - service.sin_port = C.ushort(inet.Htons(uint16(port))) + service.sin_port = C.ushort(inet.Htons(port)) return true } @@ -426,7 +408,7 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { // TODO: port this var service C.sockaddr_in - if r.Link.hostname.av_len == 0 { + if r.Link.hostname == "" { return false } memset((*byte)(unsafe.Pointer(&service)), 0, int(unsafe.Sizeof(service))) @@ -435,12 +417,12 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { service.sin_family = C.AF_INET if r.Link.socksport != 0 { - if !C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.sockshost)), C.int(r.Link.socksport)) { + if !C_add_addr_info(&service, r.Link.sockshost, r.Link.socksport) { return false } } else { // connect directly - if !C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.hostname)), C.int(r.Link.port)) { + if !C_add_addr_info(&service, r.Link.hostname, r.Link.port) { return false } } @@ -461,7 +443,7 @@ func C_SocksNegotiate(r *C_RTMP) (ok bool) { memset((*byte)(unsafe.Pointer(&service)), 0, int(unsafe.Sizeof(service))) - C_add_addr_info(&service, (*C_AVal)(unsafe.Pointer(&r.Link.hostname)), C.int(r.Link.port)) + C_add_addr_info(&service, r.Link.hostname, r.Link.port) addr = int32(inet.Htonl(uint32(service.sin_addr.s_addr))) { @@ -711,7 +693,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(unsafe.Pointer(incBytePtr(unsafe.Pointer(enc), 1))) - enc = C_AMF_EncodeNamedString(enc, pend, av_app, CAV(&r.Link.app)) + enc = C_AMF_EncodeNamedString(enc, pend, av_app, r.Link.app) if enc == nil { return false } @@ -722,21 +704,21 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } } - if r.Link.flashVer.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_flashVer, CAV(&r.Link.flashVer)) + if r.Link.flashVer != "" { + enc = C_AMF_EncodeNamedString(enc, pend, av_flashVer, r.Link.flashVer) if enc == nil { return false } } - if r.Link.swfUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_swfUrl, CAV(&r.Link.swfUrl)) + if r.Link.swfUrl != "" { + enc = C_AMF_EncodeNamedString(enc, pend, av_swfUrl, r.Link.swfUrl) if enc == nil { return false } } - if r.Link.tcUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_tcUrl, CAV(&r.Link.tcUrl)) + if r.Link.tcUrl != "" { + enc = C_AMF_EncodeNamedString(enc, pend, av_tcUrl, r.Link.tcUrl) if enc == nil { return false } @@ -763,8 +745,8 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { if enc == nil { return false } - if r.Link.pageUrl.av_len != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_pageUrl, CAV(&r.Link.pageUrl)) + if r.Link.pageUrl != "" { + enc = C_AMF_EncodeNamedString(enc, pend, av_pageUrl, r.Link.pageUrl) if enc == nil { return false } @@ -791,12 +773,12 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) /* add auth string */ - if r.Link.auth.av_len != 0 { + if r.Link.auth != "" { enc = C_AMF_EncodeBoolean(enc, pend, r.Link.lFlags&RTMP_LF_AUTH != 0) if enc == nil { return false } - enc = C_AMF_EncodeString(enc, (*byte)(pend), CAV(&r.Link.auth)) + enc = C_AMF_EncodeString(enc, (*byte)(pend), r.Link.auth) if enc == nil { return false } @@ -873,7 +855,7 @@ func C_SendReleaseStream(r *C_RTMP) (ok bool) { enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) + enc = C_AMF_EncodeString(enc, pend, r.Link.playpath) if enc == nil { return false } @@ -906,7 +888,7 @@ func C_SendFCPublish(r *C_RTMP) (ok bool) { enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) + enc = C_AMF_EncodeString(enc, pend, r.Link.playpath) if enc == nil { return false } @@ -939,7 +921,7 @@ func C_SendFCUnpublish(r *C_RTMP) (ok bool) { enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) + enc = C_AMF_EncodeString(enc, pend, r.Link.playpath) if enc == nil { return false @@ -974,7 +956,7 @@ func C_SendPublish(r *C_RTMP) (ok bool) { enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeString(enc, pend, CAV(&r.Link.playpath)) + enc = C_AMF_EncodeString(enc, pend, r.Link.playpath) if enc == nil { return false @@ -1157,7 +1139,7 @@ func C_HandleInvoke(r *C_RTMP, body *byte, nBodySize uint32) (ok bool) { //methodInvoked.av_val); switch methodInvoked { case av_connect: - if r.Link.token.av_len != 0 { + if r.Link.token != "" { panic("No support for link token") } @@ -1907,14 +1889,12 @@ func C_CloseInternal(r *C_RTMP, reconnect int32) { r.m_unackd = 0 if ((r.Link.lFlags & RTMP_LF_FTCU) != 0) && (reconnect == 0) { - //C.free(unsafe.Pointer(r.Link.app.av_val)) - r.Link.app.av_val = nil + r.Link.app = "" r.Link.lFlags ^= RTMP_LF_FAPU } if reconnect == 0 { - //C.free(unsafe.Pointer(r.Link.playpath0.av_val)) - r.Link.playpath0.av_val = nil + r.Link.playpath0 = "" } } diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index a6a9ec83..d9888681 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -154,21 +154,21 @@ func C_RTMPPacket_IsReady(p *C_RTMPPacket) bool { // typedef struct RTMP_LNK // rtmp.h +144 type C_RTMP_LNK struct { - hostname C_AVal - sockshost C_AVal - playpath0 C_AVal - playpath C_AVal - tcUrl C_AVal - swfUrl C_AVal - pageUrl C_AVal - app C_AVal - auth C_AVal - flashVer C_AVal - subscribepath C_AVal - usherToken C_AVal - token C_AVal - pubUser C_AVal - pubPasswd C_AVal + hostname string + sockshost string + playpath0 string + playpath string + tcUrl string + swfUrl string + pageUrl string + app string + auth string + flashVer string + subscribepath string + usherToken string + token string + pubUser string + pubPasswd string extras C_AMFObject edepth int32 seekTime int32 From fed685b107f59003642626a4e43bdb1def12b3dc Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sat, 8 Sep 2018 16:07:00 +0930 Subject: [PATCH 23/24] rtmp: fix test --- rtmp/rtmp_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rtmp/rtmp_test.go b/rtmp/rtmp_test.go index b33417cc..4e3cce70 100644 --- a/rtmp/rtmp_test.go +++ b/rtmp/rtmp_test.go @@ -76,7 +76,7 @@ func TestMemset(t *testing.T) { setNum := 5 testVal := byte('A') mem := malloc(uintptr(size)) - memset((*byte)(mem), int(testVal), setNum) + memset((*byte)(mem), testVal, setNum) for i := 0; i < size; i++ { if i > setNum-1 { testVal = byte(0) From f0462ee5111a9ac02ea70860a0af908e738a9935 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sun, 9 Sep 2018 19:35:56 +0930 Subject: [PATCH 24/24] rtmp: remove unused fields reported by honnef.co/go/tools/cmd/unused rtmp_headers.go:145:2: field sb_ssl is unused (U1000) rtmp_headers.go:167:2: field subscribepath is unused (U1000) rtmp_headers.go:168:2: field usherToken is unused (U1000) rtmp_headers.go:170:2: field pubUser is unused (U1000) rtmp_headers.go:171:2: field pubPasswd is unused (U1000) rtmp_headers.go:173:2: field edepth is unused (U1000) rtmp_headers.go:175:2: field stopTime is unused (U1000) rtmp_headers.go:180:2: field pFlags is unused (U1000) rtmp_headers.go:189:2: field bufpos is unused (U1000) rtmp_headers.go:190:2: field buflen is unused (U1000) rtmp_headers.go:191:2: field timestamp is unused (U1000) rtmp_headers.go:195:2: field initialFrameType is unused (U1000) rtmp_headers.go:197:2: field metaHeader is unused (U1000) rtmp_headers.go:198:2: field initialFrame is unused (U1000) rtmp_headers.go:199:2: field nMetaHeaderSize is unused (U1000) rtmp_headers.go:200:2: field nInitialFrameSize is unused (U1000) rtmp_headers.go:223:2: field m_mediaStamp is unused (U1000) rtmp_headers.go:224:2: field m_pauseStamp is unused (U1000) rtmp_headers.go:245:2: field m_polling is unused (U1000) rtmp_headers.go:248:2: field m_clientID is unused (U1000) --- rtmp/rtmp_headers.go | 58 +++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 39 deletions(-) diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index d9888681..e836a49d 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -142,7 +142,6 @@ type C_RTMPSockBuf struct { sb_start *byte sb_buf [RTMP_BUFFER_CACHE_SIZE]byte // port const sb_timedout int32 - sb_ssl uintptr } // RTMPPacket_IsReady(a) @@ -154,50 +153,35 @@ func C_RTMPPacket_IsReady(p *C_RTMPPacket) bool { // typedef struct RTMP_LNK // rtmp.h +144 type C_RTMP_LNK struct { - hostname string - sockshost string - playpath0 string - playpath string - tcUrl string - swfUrl string - pageUrl string - app string - auth string - flashVer string - subscribepath string - usherToken string - token string - pubUser string - pubPasswd string - extras C_AMFObject - edepth int32 - seekTime int32 - stopTime int32 - lFlags int32 - swfAge int32 - protocol int32 - timeout int32 - pFlags int32 - socksport uint16 - port uint16 + hostname string + sockshost string + playpath0 string + playpath string + tcUrl string + swfUrl string + pageUrl string + app string + auth string + flashVer string + token string + extras C_AMFObject + seekTime int32 + lFlags int32 + swfAge int32 + protocol int32 + timeout int32 + socksport uint16 + port uint16 } // typedef struct RTMP_READ // rtmp.h +200 type C_RTMP_READ struct { buf *byte - bufpos *byte - buflen uint - timestamp uint32 dataType uint8 flags uint8 status int8 - initialFrameType uint8 nResumeTS uint32 - metaHeader *byte - initialFrame *byte - nMetaHeaderSize uint32 - nInitialFrameSize uint32 nIgnoredFrameCounter uint32 nIgnoredFlvFrameCounter uint32 } @@ -220,8 +204,6 @@ type C_RTMP struct { m_nBufferMS int32 m_stream_id int32 m_mediaChannel int32 - m_mediaStamp uint32 - m_pauseStamp uint32 m_pausing int32 m_nServerBW int32 m_nClientBW int32 @@ -242,10 +224,8 @@ type C_RTMP struct { m_fEncoding float64 m_fDuration float64 m_msgCounter int32 - m_polling int32 m_resplen int32 m_unackd int32 - m_clientID string m_read C_RTMP_READ m_write C_RTMPPacket m_sb C_RTMPSockBuf