diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 37ff46df..38518c39 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -43,10 +43,10 @@ int end_session(RTMP* rtmp); import "C" import ( - _"errors" + "errors" _ "fmt" - _"log" - _"math" + "log" + "math" "reflect" "strconv" "unsafe" @@ -57,6 +57,12 @@ const ( debugMode = false ) +const ( + byteSize = 1 + int32Size = 4 + int64Size = 8 +) + const ( RTMP_PACKET_SIZE_LARGE = 0 RTMP_PACKET_SIZE_MEDIUM = 1 @@ -77,7 +83,7 @@ func AVC(str string) C.AVal { // av_setDataFrame is a static const global in rtmp.c var setDataFrame = AVC("@setDataFrame") -/* + // Session provides an interface for sending flv tags over rtmp. type Session interface { Open() error @@ -170,15 +176,15 @@ func rtmpWrite(r *C.RTMP, data []byte) int { } pkt.m_packetType = C.uchar(indxBytePtr(buf,0)) - buf = incPtr(buf, 1) + buf = incBytePtr(buf, 1) // TODO: port this pkt.m_nBodySize = C.AMF_DecodeInt24((*C.char)(buf)) - buf = incPtr(buf, 3) + buf = incBytePtr(buf, 3) // TODO: replace with ported version pkt.m_nTimeStamp = C.AMF_DecodeInt24((*C.char)(buf)) - buf = incPtr(buf, 3) + buf = incBytePtr(buf, 3) pkt.m_nTimeStamp |= C.uint(indxBytePtr(buf,0)) << 24 - buf = incPtr(buf, 4) + buf = incBytePtr(buf, 4) s2 -= 11 if ((pkt.m_packetType == RTMP_PACKET_TYPE_AUDIO || @@ -200,7 +206,7 @@ func rtmpWrite(r *C.RTMP, data []byte) int { } enc = unsafe.Pointer(pkt.m_body) - pend = incPtr(enc, int(pkt.m_nBodySize)) + pend = incBytePtr(enc, int(pkt.m_nBodySize)) if pkt.m_packetType == RTMP_PACKET_TYPE_INFO { // TODO: Port this @@ -210,7 +216,7 @@ func rtmpWrite(r *C.RTMP, data []byte) int { } } else { - enc = incPtr(unsafe.Pointer(pkt.m_body), int(pkt.m_nBytesRead)) + enc = incBytePtr(unsafe.Pointer(pkt.m_body), int(pkt.m_nBytesRead)) } num = int(pkt.m_nBodySize - pkt.m_nBytesRead) if num > s2 { @@ -221,7 +227,7 @@ func rtmpWrite(r *C.RTMP, data []byte) int { //copy(ptrToSlice(enc, num), ptrToSlice(buf, num)) pkt.m_nBytesRead += C.uint(num) s2 -= num - buf = incPtr(buf, num) + buf = incBytePtr(buf, num) if pkt.m_nBytesRead == pkt.m_nBodySize { // TODO: Port this ret = int(C.RTMP_SendPacket(r, pkt, 0)) @@ -231,7 +237,7 @@ func rtmpWrite(r *C.RTMP, data []byte) int { if ret == 0 { return -1 } - buf = incPtr(buf, 4) + buf = incBytePtr(buf, 4) s2 -= 4 if s2 < 0 { break @@ -241,6 +247,7 @@ func rtmpWrite(r *C.RTMP, data []byte) int { return size + s2 } + func sendPacket(r *C.RTMP, pkt *C.RTMPPacket, queue int) int { var prevPacket *C.RTMPPacket last := 0 @@ -485,7 +492,7 @@ func sendPacket(r *C.RTMP, pkt *C.RTMPPacket, queue int) int { return 1 } -/* + func writeN(r *C.RTMP, buffer unsafe.Pointer, n int) int { ptr := buffer for n > 0 { @@ -527,9 +534,7 @@ func writeN(r *C.RTMP, buffer unsafe.Pointer, n int) int { return n == 0 } -*/ -/* // TODO: port RTMP_METHOD func AV_queue(vals **C.RTMP_METHOD, num *int, av *AVal, txn int ) { var tmp unsafe.Pointer @@ -544,14 +549,53 @@ func AV_queue(vals **C.RTMP_METHOD, num *int, av *AVal, txn int ) { indxPtr(tpm,av.av_len) = '\0' } -*/ - - - // memmove copies n bytes from "from" to "to". //go:linkname memmove runtime.memmove func memmove(to, from unsafe.Pointer, n uintptr) +// 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)) +} + +// 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)) +} + +// 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)) +} + +// incBytePtr returns an unsafe.Pointer to a byte that is inc positions +// from the passed ptr +func incBytePtr(ptr unsafe.Pointer, inc int) unsafe.Pointer { + return incPtr(ptr,inc,byteSize) +} + +// incInt32Ptr returns an unsafe.Pointer to an int32 that is inc positions from +// the passed ptr +func incInt32Ptr(ptr unsafe.Pointer, inc int) unsafe.Pointer { + return incPtr(ptr,inc,int32Size) +} + +// incInt64Ptr returns an unsafe.Pointer to an int64 that is inc positions from +// the passed ptr +func incInt64Ptr(ptr unsafe.Pointer, inc int) unsafe.Pointer { + return incPtr(ptr,inc,int64Size) +} + +// incPtr attempts to replicate C like pointer arithmatic functionality +func incPtr(ptr unsafe.Pointer, inc, typeSize int) unsafe.Pointer { + return unsafe.Pointer(uintptr(ptr) + uintptr(inc*typeSize)) +} + +// incPtr attempts to replicate C like pointer arithmatic functionality +func decPtr(ptr unsafe.Pointer, dec, typeSize int) unsafe.Pointer { + return unsafe.Pointer(uintptr(ptr) - uintptr(dec*typeSize)) +} + // sliceToPtr get's the address of the first data element and returns as unsafe // pointer func sliceToPtr(data []byte) unsafe.Pointer { @@ -569,16 +613,6 @@ func ptrToSlice(data unsafe.Pointer, size int) []byte { return ret } -// incPtr attempts to replicate C like pointer arithmatic functionality -func incPtr(ptr unsafe.Pointer, inc, typeSize int) unsafe.Pointer { - return unsafe.Pointer(uintptr(ptr) + uintptr(inc*typeSize)) -} - -// incPtr attempts to replicate C like pointer arithmatic functionality -func decPtr(ptr unsafe.Pointer, dec, typeSize int) unsafe.Pointer { - return unsafe.Pointer(uintptr(ptr) - uintptr(dec*typeSize)) -} - var rtmpErrs = [...]string{ 1: "rtmp: not connected", 2: "rtmp: write error",