From 358e32d2acb7ceb371615433e914e8511e9ceec6 Mon Sep 17 00:00:00 2001 From: saxon Date: Fri, 10 Aug 2018 16:10:08 +0930 Subject: [PATCH] Fixed alot of errors in code, now runnable --- rtmp/rtmp.go | 141 ++++++++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 68 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index ce1554a0..aff04707 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -244,11 +244,11 @@ type C_RTMP struct { m_bSendCounter uint8 m_numInvokes int m_numCalls int - m_methodCalls *RTMP_METHOD + m_methodCalls *C.RTMP_METHOD m_channelsAllocatedIn int m_channelsAllocatedOut int - m_vecChannelsIn **RTMPPacket - m_vecChannelsOut **RTMPPacket + m_vecChannelsIn **C.RTMPPacket + m_vecChannelsOut **C.RTMPPacket m_channelTimestamp *int m_fAudioCodecs float64 m_fVideoCodecs float64 @@ -258,11 +258,11 @@ type C_RTMP struct { m_polling int m_resplen int m_unackd int - m_clientID AVal - m_read RTMP_READ - m_write RTMPPacket - m_sb RTMPSockBuf - Link RTMP_LNK + m_clientID C.AVal + m_read C.RTMP_READ + m_write C.RTMPPacket + m_sb C.RTMPSockBuf + Link C.RTMP_LNK } // typedef struct RTMPPacket @@ -276,18 +276,18 @@ type C_RTMPPacket struct { m_nInfoField2 int32 m_nBodySize uint32 m_nBytesRead uint32 - m_chunk *RTMPChunk + m_chunk *C.RTMPChunk m_body *byte } // typedef struct RTMPMethod // rtmp.h +231 type C_RTMP_METHOD struct { - name AVal + name C.AVal num int } -// typedef struct AVal +// typedef struct C.AVal // amf.h +57 type C_AVal struct { av_val *byte @@ -337,22 +337,22 @@ type C_RTMPChunk struct { // typedef struct RTMP_LNK // rtmp.h +144 type C_RTMP_LNK struct { - hostname AVal - sockshost AVal - playpath0 AVal - playpath AVal - tcUrl AVal - swfUrl AVal - pageUrl AVal - app AVal - auth AVal - flashVer AVal - subscribepath AVal - usherToken AVal - token AVal - pubUser AVal - pubPasswd AVal - extras AMFObject + 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 + extras C.AMFObject edepth int seekTime int stopTime int @@ -374,7 +374,7 @@ type C_AMFObject struct { // NewSession returns a new session. func NewSession(url string, connectTimeout uint) Session { - return &session{ + return Session{ url: url, timeout: connectTimeout, } @@ -382,7 +382,7 @@ func NewSession(url string, connectTimeout uint) Session { // Open establishes an rtmp connection with the url passed into the // constructor -func (s *session) Open() error { +func (s *Session) Open() error { if s.rtmp != nil { return errors.New("rtmp: attempt to start already running session") } @@ -395,7 +395,7 @@ func (s *session) Open() error { } // Close terminates the rtmp connection -func (s *session) Close() error { +func (s *Session) Close() error { if s.rtmp == nil { return Err(3) } @@ -408,7 +408,7 @@ func (s *session) Close() error { } // Write writes a frame (flv tag) to the rtmp connection -func (s *session) Write(data []byte) (int, error) { +func (s *Session) Write(data []byte) (int, error) { if s.rtmp == nil { return 0, Err(3) } @@ -566,14 +566,14 @@ func C_RTMP_SetupURL(r *C.RTMP, u string) int32 { return 1 } -// void SocksSetup(RTMP *r, AVal* sockshost); +// 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 uintptr(unsafe.Pointer(socksport)) != nil { + if unsafe.Pointer(socksport) != nil { *indxBytePtr(unsafe.Pointer(hostname), int(uintptr(decBytePtr(unsafe.Pointer(socksport), int(uintptr(unsafe.Pointer(sockshost.av_val))))))) = '\000' @@ -762,7 +762,9 @@ func C_RTMP_Connect0(r *C.RTMP, service *C.sockaddr) int { } { - tv := C.int(r.Link.timeout * 1000) + var tv int32 + SET_RCVTIMEO(&tv, int32(r.Link.timeout)) + // tv := C.int(r.Link.timeout * 1000) if C.setsockopt(r.m_sb.sb_socket, C.SOL_SOCKET, C.SO_RCVTIMEO, unsafe.Pointer(&tv), C.socklen_t(unsafe.Sizeof(tv))) != 0 { @@ -776,6 +778,10 @@ func C_RTMP_Connect0(r *C.RTMP, service *C.sockaddr) int { return 1 } +func SET_RCVTIMEO(tv *int32, s int32) { + *tv = s * 1000 +} + // int RTMP_Connect1(RTMP* r, RTMPPacket* cp); // rtmp.c +978 func C_RTMP_Connect1(r *C.RTMP, cp *C.RTMPPacket) int { @@ -1332,7 +1338,7 @@ func C_RTMP_ConnectStream(r *C.RTMP, seekTime int32) int { C.RTMP_ReadPacket(r, &packet) != 0 { // TODO: port is ready - if C_RTMPPacketIsReady(&packet) != 0 { + if C_RTMPPacket_IsReady(&packet) != 0 { if packet.m_nBodySize == 0 { continue } @@ -1361,7 +1367,6 @@ func C_RTMP_ReadPacket(r *C.RTMP, packet *C.RTMPPacket) int32 { var header *byte header = (*byte)(unsafe.Pointer(&hbuf[0])) var nSize, hSize, nToRead, nChunk int32 - var didAlloc int32 = 0 var extendedTimestamp int32 if C_ReadN(r, (*byte)(&hbuf[0]), 1) == 0 { @@ -1423,7 +1428,7 @@ func C_RTMP_ReadPacket(r *C.RTMP, packet *C.RTMPPacket) int32 { memset((*byte)(incPtr(unsafe.Pointer(r.m_channelTimestamp), int(r.m_channelsAllocatedIn), int(unsafe.Sizeof(*r.m_channelTimestamp)))), - 0, int(int32Size*int32((n-int32(r.m_channelsAllocatedIn))))) + 0, int(4*int32((n-int32(r.m_channelsAllocatedIn))))) memset((*byte)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), int( r.m_channelsAllocatedIn), int(unsafe.Sizeof(*r.m_vecChannelsIn)))), 0, int(int32(unsafe.Sizeof(*packets))*(n-int32(r.m_channelsAllocatedIn)))) @@ -1496,7 +1501,6 @@ func C_RTMP_ReadPacket(r *C.RTMP, packet *C.RTMPPacket) int32 { log.Println("RTMPRead_Packet: failed to allocate packet") return 0 } - didAlloc = TRUE packet.m_headerType = C.uint8_t((hbuf[0] & 0xc0) >> 6) } @@ -1519,7 +1523,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 FALSE + return 0 } packet.m_nBytesRead += C.uint32_t(nChunk) @@ -1534,7 +1538,7 @@ func C_RTMP_ReadPacket(r *C.RTMP, packet *C.RTMPPacket) int32 { } memmove(unsafe.Pointer(*(**C.RTMPPacket)(incPtr(unsafe.Pointer( r.m_vecChannelsIn), int(packet.m_nChannel), int(unsafe.Sizeof(tmpPktPtr))))), - unsafe.Pointer(packet), unsafe.Sizeof(tmpPkt)) + unsafe.Pointer(packet), unsafe.Sizeof(tmpPktPtr)) if extendedTimestamp != 0 { (*(**C.RTMPPacket)(incPtr(unsafe.Pointer( @@ -1543,30 +1547,31 @@ func C_RTMP_ReadPacket(r *C.RTMP, packet *C.RTMPPacket) int32 { } // TODO: port this - if C.RTMPPacket_IsRead(packet) != 0 { - if !packet.m_hasAbsTimestamp { + if C_RTMPPacket_IsReady(packet) != 0 { + if packet.m_hasAbsTimestamp == 0 { // timestamps seem to always be relative - packet.m_nTimeStamp += r.m_channelTimestamp[packet.m_nChannel] + packet.m_nTimeStamp += *(*C.uint32_t)(incPtr(unsafe.Pointer(r.m_channelTimestamp), + int(packet.m_nChannel), 4)) } - r.m_channelTimestamp[packet.m_nChannel] = packet.m_nTimeStamp + *(*C.uint32_t)(incPtr(unsafe.Pointer(r.m_channelTimestamp), int(packet.m_nChannel), + 4)) = packet.m_nTimeStamp - /* reset the data from the stored packet. we keep the header since we may - use it later if a new packet for this channel - arrives and requests to re-use some info (small packet header) */ - r.m_vecChannelsIn[packet.m_nChannel].m_body = NULL - r.m_vecChannelsIn[packet.m_nChannel].m_nBytesRead = 0 - /* can only be false if we reuse header */ - r.m_vecChannelsIn[packet.m_nChannel].m_hasAbsTimestamp = FALSE + (*(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), int(packet.m_nChannel), + int(unsafe.Sizeof(*r.m_vecChannelsIn))))).m_body = nil + (*(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), int(packet.m_nChannel), + int(unsafe.Sizeof(*r.m_vecChannelsIn))))).m_nBytesRead = 0 + (*(**C.RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsIn), int(packet.m_nChannel), + int(unsafe.Sizeof(*r.m_vecChannelsIn))))).m_hasAbsTimestamp = 0 } else { packet.m_body = nil /* so it won't be erased on free */ } - return TRUE + return 1 } // #define RTMPPacket_IsReady(a) // rtmp.h +142 -func C_RTMPPacketIsReady(p *C.RTMPPacket) int { +func C_RTMPPacket_IsReady(p *C.RTMPPacket) int { if p.m_nBytesRead == p.m_nBodySize { return 1 } @@ -1987,7 +1992,7 @@ func C_RTMPSockBuf_Send(sb *C.RTMPSockBuf, buf *byte, l int32) int32 { return int32(C.send(sb.sb_socket, unsafe.Pointer(buf), C.size_t(l), 0)) } -// void AV_queue(RTMP_METHOD** vals, int* num, AVal* av, int txn); +// 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) { if (*num & 0x0f) == 0 { @@ -2011,7 +2016,7 @@ func C_AV_queue(vals **C.RTMP_METHOD, num *int32, av *C.AVal, txn int32) { (*num)++ } -// char* AMF_EncodeNamedNumber(char* output, char* outend, const AVal* strName, double dVal); +// 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))) { @@ -2023,7 +2028,7 @@ func C_AMF_EncodeNamedNumber(output *byte, outend *byte, strName *C.AVal, dVal f return C_AMF_EncodeNumber(output, outend, dVal) } -// char* AMF_EncodeNamedBoolean(char* output, char* outend, const AVal* strname, int bVal); +// 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))) { @@ -2085,7 +2090,7 @@ func C_AMF_DecodeNumber(data *byte) float64 { return dVal } -// char* AMF_EncodeNamedString(char* output, char* outend, const AVal* strName, const AVal* strValue); +// 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))) { @@ -2097,7 +2102,7 @@ func C_AMF_EncodeNamedString(output *byte, outend *byte, strName *C.AVal, strVal return C_AMF_EncodeString(output, outend, strValue) } -// void AMF_DecodeString(const char* data, AVal* bv); +// 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) @@ -2150,7 +2155,7 @@ func C_AMF_DecodeInt24(data *byte) uint32 { return dst } -// char* AMF_EncodeString(char* output, char* outend, const AVal* bv); +// 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) @@ -2329,35 +2334,35 @@ func allocate(nOfBytes uintptr) unsafe.Pointer { // indxBytePtr returns a byte at the indx inc give a ptr func indxBytePtr(ptr unsafe.Pointer, inc int) *byte { - return (*byte)(incPtr(ptr, inc, byteSize)) + return (*byte)(incPtr(ptr, inc, 1)) } // indxInt32Ptr returns an int32 at the indx inc given a ptr func indxInt32Ptr(ptr unsafe.Pointer, inc int) *int32 { - return (*int32)(incPtr(ptr, inc, int32Size)) + return (*int32)(incPtr(ptr, inc, 4)) } // indxInt64Ptr returns an int64 at the indx inc given a ptr func indxInt64Ptr(ptr unsafe.Pointer, inc int) *int64 { - return (*int64)(incPtr(ptr, inc, int64Size)) + return (*int64)(incPtr(ptr, inc, 8)) } // incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions // from the passed ptr func incBytePtr(ptr unsafe.Pointer, inc int) unsafe.Pointer { - return incPtr(ptr, inc, byteSize) + return incPtr(ptr, inc, 1) } // incInt32Ptr returns an unsafe.Pointer to an int32 that is inc positive // positions from the passed ptr func incInt32Ptr(ptr unsafe.Pointer, inc int) unsafe.Pointer { - return incPtr(ptr, inc, int32Size) + return incPtr(ptr, inc, 4) } // incInt64Ptr returns an unsafe.Pointer to an int64 that is inc positive // positions from the passed ptr func incInt64Ptr(ptr unsafe.Pointer, inc int) unsafe.Pointer { - return incPtr(ptr, inc, int64Size) + return incPtr(ptr, inc, 8) } // incPtr attempts to replicate C like pointer arithmatic functionality @@ -2373,19 +2378,19 @@ func decPtr(ptr unsafe.Pointer, dec, typeSize int) unsafe.Pointer { // decBytePtr returns an unsafe.Pointer to a byte that is dec negative positions // from ptr func decBytePtr(ptr unsafe.Pointer, dec int) unsafe.Pointer { - return decPtr(ptr, dec, byteSize) + return decPtr(ptr, dec, 1) } // decBytePtr returns an unsafe.Pointer to a int32 that is dec negative positions // from ptr func decInt32Ptr(ptr unsafe.Pointer, dec int) unsafe.Pointer { - return decPtr(ptr, dec, int32Size) + return decPtr(ptr, dec, 4) } // decBytePtr returns an unsafe.Pointer to a int64 that is dec negative positions // from ptr func decInt64Ptr(ptr unsafe.Pointer, dec int) unsafe.Pointer { - return decPtr(ptr, dec, int64Size) + return decPtr(ptr, dec, 8) } // sliceToPtr get's the address of the first data element and returns as unsafe