diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index e02f9542..bf50c0cb 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -1343,14 +1343,16 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { cSize := 0 t := uint32(int(packet.m_nTimeStamp) - last) - var header, hend unsafe.Pointer + var header, hend *byte if packet.m_body != nil { - header = decBytePtr(unsafe.Pointer(&packet.m_body[0]), nSize) - hend = unsafe.Pointer(&packet.m_body[0]) + // Span from -nSize to the start of the body. + header = decBytePtr(&packet.m_body[0], nSize) + hend = &packet.m_body[0] } else { + // Allocate a new header and allow 6 bytes of movement backward. var hbuf [RTMP_MAX_HEADER_SIZE]byte - header = incBytePtr(unsafe.Pointer(&hbuf[0]), 6) - hend = incBytePtr(unsafe.Pointer(&hbuf[0]), RTMP_MAX_HEADER_SIZE) + header = incBytePtr(&hbuf[0], 6) + hend = incBytePtr(&hbuf[0], RTMP_MAX_HEADER_SIZE) } switch { @@ -1381,16 +1383,16 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { case 2: c |= byte(1) } - *(*byte)(hptr) = c + *hptr = c hptr = incBytePtr(hptr, 1) if cSize != 0 { tmp := packet.m_nChannel - 64 - *(*byte)(hptr) = byte(tmp & 0xff) + *hptr = byte(tmp & 0xff) hptr = incBytePtr(hptr, 1) if cSize == 2 { - *(*byte)(hptr) = byte(tmp >> 8) + *hptr = byte(tmp >> 8) 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 { 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 { - hptr = unsafe.Pointer(bAddr(C_AMF_EncodeInt24(pp2b((*byte)(hptr), (*byte)(hend)), (int32(packet.m_nBodySize))))) - *(*byte)(hptr) = packet.m_packetType + hptr = bAddr(C_AMF_EncodeInt24(pp2b(hptr, hend), int32(packet.m_nBodySize))) + *hptr = packet.m_packetType hptr = incBytePtr(hptr, 1) } 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 { - 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) - buffer := unsafe.Pointer(&packet.m_body[0]) + buffer := &packet.m_body[0] nChunkSize := int(r.m_outChunkSize) if debugMode { 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 { if nSize < nChunkSize { nChunkSize = nSize } 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) } else { - // TODO: port this - if !C_WriteN(r, pl2b((*byte)(header), nChunkSize+hSize)) { + if !C_WriteN(r, pl2b(header, nChunkSize+hSize)) { return false } } @@ -1459,25 +1460,25 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { hSize += 4 } - *(*byte)(header) = 0xc0 | c + *header = 0xc0 | c if cSize != 0 { tmp := int(packet.m_nChannel) - 64 - (*[_Gi]byte)(header)[1] = byte(tmp) + pl2b(incBytePtr(header, 1), 2)[1] = byte(tmp) if cSize == 2 { - (*[_Gi]byte)(header)[2] = byte(tmp >> 8) + pl2b(incBytePtr(header, 1), 3)[2] = byte(tmp >> 8) } } if t >= 0xffffff { 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 { - ok := C_WriteN(r, pl2b((*byte)(tbuf), int(uintptr(toff)-uintptr(tbuf)))) + ok := C_WriteN(r, pp2b(tbuf, toff)) tbuf = nil 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 // from the passed ptr -func incBytePtr(ptr unsafe.Pointer, inc int) unsafe.Pointer { - return unsafe.Pointer(uintptr(ptr) + uintptr(inc)) +func incBytePtr(ptr *byte, inc int) *byte { + return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) + uintptr(inc))) } // 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 unsafe.Pointer(uintptr(ptr) - uintptr(dec)) +func decBytePtr(ptr *byte, dec int) *byte { + return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(ptr)) - uintptr(dec))) } var rtmpErrs = [...]string{