From b45f70bdf89e191bc4a81e0cc4517f9e520b5f12 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Wed, 12 Sep 2018 16:57:54 +0930 Subject: [PATCH] rtmp: make C_RTMP.m_vecChannelsOut a []*C_RTMPPacket --- rtmp/rtmp.go | 43 +++++++++++++++++-------------------------- rtmp/rtmp_headers.go | 2 +- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 4d8c8a92..5fd8a48e 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -1500,35 +1500,30 @@ func C_HandShake(r *C_RTMP, FP9HandShake int32) (ok bool) { 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 var header, hptr, hend, buffer, tbuf, toff unsafe.Pointer var goHbuf [RTMP_MAX_HEADER_SIZE]byte var hbuf = unsafe.Pointer(&goHbuf[0]) var c byte var t uint32 - var packets unsafe.Pointer if packet.m_nChannel >= r.m_channelsAllocatedOut { n := int(packet.m_nChannel + 10) - packets = C.realloc(unsafe.Pointer(r.m_vecChannelsOut), C.size_t( - unsafe.Sizeof(packet)*uintptr(n))) - if uintptr(packets) == uintptr(0) { - //C.free(unsafe.Pointer(r.m_vecChannelsOut)) - r.m_vecChannelsOut = nil - r.m_channelsAllocatedOut = 0 - return false + var packets []*C_RTMPPacket + if r.m_vecChannelsOut == nil { + packets = make([]*C_RTMPPacket, n) + } else { + packets = append(r.m_vecChannelsOut[:packet.m_nChannel:packet.m_nChannel], make([]*C_RTMPPacket, 10)...) } - r.m_vecChannelsOut = (**C_RTMPPacket)(packets) + r.m_vecChannelsOut = packets - 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)))) + for i := int(r.m_channelsAllocatedOut); i < n; i++ { + r.m_vecChannelsOut[i] = nil + } r.m_channelsAllocatedOut = int32(n) } - prevPacket = *(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), - int(packet.m_nChannel), int(unsafe.Sizeof(packet)))) + prevPacket = r.m_vecChannelsOut[packet.m_nChannel] if prevPacket != nil && packet.m_headerType != RTMP_PACKET_SIZE_LARGE { // compress a bit by using the prev packet's attributes @@ -1553,9 +1548,9 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { return false } - nSize = packetSize[int(packet.m_headerType)] - hSize = nSize - cSize = 0 + nSize := packetSize[int(packet.m_headerType)] + hSize := nSize + cSize := 0 t = uint32(int(packet.m_nTimeStamp) - last) if packet.m_body != nil { @@ -1635,7 +1630,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { nSize = int(packet.m_nBodySize) buffer = unsafe.Pointer(packet.m_body) - nChunkSize = int(r.m_outChunkSize) + nChunkSize := int(r.m_outChunkSize) if debugMode { log.Printf("C_RTMP_SendPacket: fd=%v, size=%v", r.m_sb.sb_socket, nSize) @@ -1721,15 +1716,11 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { } } - if *(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), - int(packet.m_nChannel), int(unsafe.Sizeof(packet)))) == nil { - - *(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), - int(packet.m_nChannel), int(unsafe.Sizeof(packet)))) = (*C_RTMPPacket)(malloc(unsafe.Sizeof(*packet))) + if r.m_vecChannelsOut[packet.m_nChannel] == nil { + r.m_vecChannelsOut[packet.m_nChannel] = &C_RTMPPacket{} } + *(r.m_vecChannelsOut[packet.m_nChannel]) = *packet - memmove(unsafe.Pointer(*(**C_RTMPPacket)(incPtr(unsafe.Pointer(r.m_vecChannelsOut), - int(packet.m_nChannel), int(unsafe.Sizeof(packet))))), unsafe.Pointer(packet), unsafe.Sizeof(*packet)) return true } diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index 7f0e6ee2..55f8208c 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -203,7 +203,7 @@ type C_RTMP struct { m_channelsAllocatedIn int32 m_channelsAllocatedOut int32 m_vecChannelsIn []*C_RTMPPacket - m_vecChannelsOut **C_RTMPPacket + m_vecChannelsOut []*C_RTMPPacket m_channelTimestamp []int32 m_fAudioCodecs float64 m_fVideoCodecs float64