rtmp: remove direct unsafe use in C_RTMP_SendPacket

This commit is contained in:
Dan Kortschak 2018-09-26 14:37:53 +09:30
parent dc040ccf9c
commit 5203982233
1 changed files with 28 additions and 27 deletions

View File

@ -1343,14 +1343,16 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) {
cSize := 0 cSize := 0
t := uint32(int(packet.m_nTimeStamp) - last) t := uint32(int(packet.m_nTimeStamp) - last)
var header, hend unsafe.Pointer var header, hend *byte
if packet.m_body != nil { if packet.m_body != nil {
header = decBytePtr(unsafe.Pointer(&packet.m_body[0]), nSize) // Span from -nSize to the start of the body.
hend = unsafe.Pointer(&packet.m_body[0]) header = decBytePtr(&packet.m_body[0], nSize)
hend = &packet.m_body[0]
} else { } else {
// Allocate a new header and allow 6 bytes of movement backward.
var hbuf [RTMP_MAX_HEADER_SIZE]byte var hbuf [RTMP_MAX_HEADER_SIZE]byte
header = incBytePtr(unsafe.Pointer(&hbuf[0]), 6) header = incBytePtr(&hbuf[0], 6)
hend = incBytePtr(unsafe.Pointer(&hbuf[0]), RTMP_MAX_HEADER_SIZE) hend = incBytePtr(&hbuf[0], RTMP_MAX_HEADER_SIZE)
} }
switch { switch {
@ -1381,16 +1383,16 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) {
case 2: case 2:
c |= byte(1) c |= byte(1)
} }
*(*byte)(hptr) = c *hptr = c
hptr = incBytePtr(hptr, 1) hptr = incBytePtr(hptr, 1)
if cSize != 0 { if cSize != 0 {
tmp := packet.m_nChannel - 64 tmp := packet.m_nChannel - 64
*(*byte)(hptr) = byte(tmp & 0xff) *hptr = byte(tmp & 0xff)
hptr = incBytePtr(hptr, 1) hptr = incBytePtr(hptr, 1)
if cSize == 2 { if cSize == 2 {
*(*byte)(hptr) = byte(tmp >> 8) *hptr = byte(tmp >> 8)
hptr = incBytePtr(hptr, 1) hptr = incBytePtr(hptr, 1)
} }
} }
@ -1400,43 +1402,42 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) {
if t > 0xffffff { if t > 0xffffff {
res = 0xffffff res = 0xffffff
} }
hptr = unsafe.Pointer(bAddr(C_AMF_EncodeInt24(pp2b((*byte)(hptr), (*byte)(hend)), int32(res)))) hptr = bAddr(C_AMF_EncodeInt24(pp2b(hptr, hend), int32(res)))
} }
if nSize > 4 { if nSize > 4 {
hptr = unsafe.Pointer(bAddr(C_AMF_EncodeInt24(pp2b((*byte)(hptr), (*byte)(hend)), (int32(packet.m_nBodySize))))) hptr = bAddr(C_AMF_EncodeInt24(pp2b(hptr, hend), int32(packet.m_nBodySize)))
*(*byte)(hptr) = packet.m_packetType *hptr = packet.m_packetType
hptr = incBytePtr(hptr, 1) hptr = incBytePtr(hptr, 1)
} }
if nSize > 8 { if nSize > 8 {
hptr = incBytePtr(hptr, int(C_EncodeInt32LE((*[_Gi]byte)(hptr)[:4], packet.m_nInfoField2))) hptr = incBytePtr(hptr, int(C_EncodeInt32LE(pl2b(hptr, 4), packet.m_nInfoField2)))
} }
if t >= 0xffffff { if t >= 0xffffff {
hptr = unsafe.Pointer(bAddr(C_AMF_EncodeInt32(pp2b((*byte)(hptr), (*byte)(hend)), (int32)(t)))) hptr = bAddr(C_AMF_EncodeInt32(pp2b(hptr, hend), int32(t)))
} }
nSize = int(packet.m_nBodySize) nSize = int(packet.m_nBodySize)
buffer := unsafe.Pointer(&packet.m_body[0]) buffer := &packet.m_body[0]
nChunkSize := int(r.m_outChunkSize) nChunkSize := int(r.m_outChunkSize)
if debugMode { if debugMode {
log.Printf("C_RTMP_SendPacket: fd=%v, size=%v", r.m_sb.sb_socket, nSize) log.Printf("C_RTMP_SendPacket: fd=%v, size=%v", r.m_sb.sb_socket, nSize)
} }
var tbuf, toff unsafe.Pointer var tbuf, toff *byte
for (nSize + hSize) != 0 { for (nSize + hSize) != 0 {
if nSize < nChunkSize { if nSize < nChunkSize {
nChunkSize = nSize nChunkSize = nSize
} }
if tbuf != nil { if tbuf != nil {
copy((*[_Gi]byte)(toff)[:nChunkSize+hSize], (*[_Gi]byte)(header)[:nChunkSize+hSize]) copy(pl2b(toff, nChunkSize+hSize), pl2b(header, nChunkSize+hSize))
toff = incBytePtr(toff, nChunkSize+hSize) toff = incBytePtr(toff, nChunkSize+hSize)
} else { } else {
// TODO: port this if !C_WriteN(r, pl2b(header, nChunkSize+hSize)) {
if !C_WriteN(r, pl2b((*byte)(header), nChunkSize+hSize)) {
return false return false
} }
} }
@ -1459,25 +1460,25 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) {
hSize += 4 hSize += 4
} }
*(*byte)(header) = 0xc0 | c *header = 0xc0 | c
if cSize != 0 { if cSize != 0 {
tmp := int(packet.m_nChannel) - 64 tmp := int(packet.m_nChannel) - 64
(*[_Gi]byte)(header)[1] = byte(tmp) pl2b(incBytePtr(header, 1), 2)[1] = byte(tmp)
if cSize == 2 { if cSize == 2 {
(*[_Gi]byte)(header)[2] = byte(tmp >> 8) pl2b(incBytePtr(header, 1), 3)[2] = byte(tmp >> 8)
} }
} }
if t >= 0xffffff { if t >= 0xffffff {
extendedTimestamp := incBytePtr(header, 1+cSize) extendedTimestamp := incBytePtr(header, 1+cSize)
C_AMF_EncodeInt32((*[_Gi]byte)(extendedTimestamp)[:4], (int32)(t)) C_AMF_EncodeInt32(pl2b(extendedTimestamp, 4), int32(t))
} }
} }
} }
if tbuf != nil { if tbuf != nil {
ok := C_WriteN(r, pl2b((*byte)(tbuf), int(uintptr(toff)-uintptr(tbuf)))) ok := C_WriteN(r, pp2b(tbuf, toff))
tbuf = nil tbuf = nil
if !ok { if !ok {
@ -1660,14 +1661,14 @@ func C_RTMP_Write(r *C_RTMP, buf []byte) int {
// incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions // incBytePtr returns an unsafe.Pointer to a byte that is inc positive positions
// from the passed ptr // from the passed ptr
func incBytePtr(ptr unsafe.Pointer, inc int) unsafe.Pointer { func incBytePtr(ptr *byte, inc int) *byte {
return unsafe.Pointer(uintptr(ptr) + uintptr(inc)) return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) + uintptr(inc)))
} }
// decBytePtr returns an unsafe.Pointer to a byte that is dec negative positions // decBytePtr returns an unsafe.Pointer to a byte that is dec negative positions
// from ptr // from ptr
func decBytePtr(ptr unsafe.Pointer, dec int) unsafe.Pointer { func decBytePtr(ptr *byte, dec int) *byte {
return unsafe.Pointer(uintptr(ptr) - uintptr(dec)) return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) - uintptr(dec)))
} }
var rtmpErrs = [...]string{ var rtmpErrs = [...]string{