diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 02ddfc05..15796e65 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -50,6 +50,10 @@ import ( "bitbucket.org/ausocean/av/tools" ) +const ( + minDataSize = 11 +) + // Session provides an interface for sending flv tags over rtmp. type Session interface { Open() error @@ -79,11 +83,14 @@ func NewSession(url string, connectTimeout uint) Session { // See #define AVC(str) {str, sizeof(str)-1} in amf.h func AVC(str string) C.AVal { var aval C.AVal - aval.av_val = (*C.char)(unsafe.Pointer(&([]byte(str)[0]))) - aval.av_len = (C.int)(int(unsafe.Sizeof([]byte(str))-1)) + aval.av_val = C.CString(str) + aval.av_len = C.int(len(str)) return aval } +// av_setDataFrame is a static const global in rtmp.c +var av_setDataFrame = AVC("@setDataFrame") + // Open establishes an rtmp connection with the url passed into the // constructor func (s *session) Open() error { @@ -98,10 +105,13 @@ func (s *session) Open() error { return nil } +func byteSliceToCArr(buf []byte) *C.char { + return (*C.char)(unsafe.Pointer(&buf[0])) +} func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int { var pkt *C.RTMPPacket = &r.m_write - var pend, enc *C.char + var pend, enc []byte size := len(buf) s2 := size var ret, num int @@ -111,7 +121,7 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int { for s2 > 0 { if pkt.m_nBytesRead == 0 { - if size < 11 { + if size < minDataSize { log.Printf("size: %d\n", size) log.Printf("too small \n") return 0 @@ -123,29 +133,15 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int { s2 -= 13 } - // pkt.m_packetType = *buf++ buf = buf[1:] pkt.m_packetType = C.uchar(buf[0]) - - cChar := (*C.char)(unsafe.Pointer(&buf[0])) - pkt.m_nBodySize = C.AMF_DecodeInt24(cChar) - - // C: buf+=3 + pkt.m_nBodySize = C.AMF_DecodeInt24(byteSliceToCArr(buf)) buf = buf[3:] - - cChar = (*C.char)(unsafe.Pointer(&buf[0])) - pkt.m_nTimeStamp = C.AMF_DecodeInt24(cChar) - - // C: buf+=3 + pkt.m_nTimeStamp = C.AMF_DecodeInt24(byteSliceToCArr(buf)) buf = buf[3:] - - // C: pkt->m_nTimeStamp |= *buf++ << 24; buf = buf[1:] pkt.m_nTimeStamp |= C.uint( buf[0] << 24 ) - - // C: buf+=3 buf = buf[3:] - s2 -= 11 if ((pkt.m_packetType == C.RTMP_PACKET_TYPE_AUDIO || @@ -161,7 +157,7 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int { } // C: if (!RTMPPacket_Alloc(pkt, pkt->m_nBodySize)) - if !tools.IntToBool(int( C.RTMPPacket_Alloc(pkt, pkt.m_nBodySize))) { + if int( C.RTMPPacket_Alloc(pkt, pkt.m_nBodySize)) == 0 { log.Println("Failed to allocated packet") return 0 } @@ -171,10 +167,8 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int { // pend = enc + pkt.m_nBodySize pend = (*C.char)(unsafe.Pointer( uintptr(unsafe.Pointer(enc)) + ( unsafe.Sizeof(C.char(0x00)) * (uintptr(pkt.m_nBodySize)) ) )) - if pkt.m_packetType == C.RTMP_PACKET_TYPE_INFO { - // av_setDataFrame is a static const global in rtmp.c - av_setDataFrame := AVC("@setDataFrame") + if pkt.m_packetType == C.RTMP_PACKET_TYPE_INFO { enc = C.AMF_EncodeString(enc, pend, &av_setDataFrame ) // C-code: pkt.m_nBytesRead enc - pkt.m_body