rtmp: use slices in C_RTMP_ReadPacket

This commit is contained in:
Dan Kortschak 2018-09-26 13:52:28 +09:30
parent 22c926611d
commit 6e5e597b11
1 changed files with 19 additions and 21 deletions

View File

@ -1079,35 +1079,33 @@ func C_EncodeInt32LE(dst []byte, v int32) int32 {
// rtmp.c +3550 // rtmp.c +3550
func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) { func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
var hbuf [RTMP_MAX_HEADER_SIZE]byte var hbuf [RTMP_MAX_HEADER_SIZE]byte
header := (*byte)(unsafe.Pointer(&hbuf[0])) header := hbuf[:]
if C_ReadN(r, hbuf[:1]) != 1 { if C_ReadN(r, header[:1]) != 1 {
log.Println("C_RTMP_ReadPacket: failed to read RTMP packet header!") log.Println("C_RTMP_ReadPacket: failed to read RTMP packet header!")
return false return false
} }
packet.m_headerType = (hbuf[0] & 0xc0) >> 6 packet.m_headerType = (header[0] & 0xc0) >> 6
packet.m_nChannel = int32(hbuf[0] & 0x3f) packet.m_nChannel = int32(header[0] & 0x3f)
header = (*byte)(incBytePtr(unsafe.Pointer(header), 1)) header = header[1:]
switch { switch {
case packet.m_nChannel == 0: case packet.m_nChannel == 0:
if C_ReadN(r, hbuf[1:2]) != 1 { if C_ReadN(r, header[:1]) != 1 {
log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header 2nd byte.") log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header 2nd byte.")
return false return false
} }
header = header[1:]
packet.m_nChannel = int32(header[0]) + 64
packet.m_nChannel = int32(hbuf[1])
packet.m_nChannel += 64
header = (*byte)(incBytePtr(unsafe.Pointer(header), 1))
case packet.m_nChannel == 1: case packet.m_nChannel == 1:
if C_ReadN(r, hbuf[1:3]) != 2 { if C_ReadN(r, header[:2]) != 2 {
log.Println("C_RTMP_ReadPacket: failed to read RTMP packet 3rd byte") log.Println("C_RTMP_ReadPacket: failed to read RTMP packet 3rd byte")
return false return false
} }
header = header[2:]
packet.m_nChannel = int32(binary.BigEndian.Uint16(header[:2])) + 64
tmp := int32((hbuf[2] << 8) + hbuf[1])
packet.m_nChannel = tmp + 64
header = (*byte)(incBytePtr(unsafe.Pointer(header), 2))
} }
if packet.m_nChannel >= r.m_channelsAllocatedIn { if packet.m_nChannel >= r.m_channelsAllocatedIn {
@ -1145,25 +1143,25 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
nSize-- nSize--
if nSize > 0 && C_ReadN(r, pl2b(header, int(nSize))) != int(nSize) { if nSize > 0 && C_ReadN(r, header[:nSize]) != int(nSize) {
log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header.") log.Println("C_RTMP_ReadPacket: failed to read rtmp packet header.")
return false return false
} }
hSize := int(uintptr(unsafe.Pointer(header))-uintptr(unsafe.Pointer(&hbuf[0]))) + nSize hSize := len(hbuf) - len(header) + nSize
if nSize >= 3 { if nSize >= 3 {
packet.m_nTimeStamp = C_AMF_DecodeInt24((*[_Gi]byte)(unsafe.Pointer(header))[:3]) packet.m_nTimeStamp = C_AMF_DecodeInt24(header[:3])
if nSize >= 6 { if nSize >= 6 {
packet.m_nBodySize = C_AMF_DecodeInt24((*[_Gi]byte)(unsafe.Pointer(header))[3:6]) packet.m_nBodySize = C_AMF_DecodeInt24(header[3:6])
packet.m_nBytesRead = 0 packet.m_nBytesRead = 0
if nSize > 6 { if nSize > 6 {
packet.m_packetType = (*[_Gi]byte)(unsafe.Pointer(header))[6] packet.m_packetType = header[6]
if nSize == 11 { if nSize == 11 {
packet.m_nInfoField2 = C_DecodeInt32LE((*[_Gi]byte)(unsafe.Pointer(header))[7:11]) packet.m_nInfoField2 = C_DecodeInt32LE(header[7:11])
} }
} }
} }
@ -1171,12 +1169,12 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
extendedTimestamp := packet.m_nTimeStamp == 0xffffff extendedTimestamp := packet.m_nTimeStamp == 0xffffff
if extendedTimestamp { if extendedTimestamp {
if C_ReadN(r, pl2b((*byte)(incBytePtr(unsafe.Pointer(header), int(nSize))), 4)) != 4 { if C_ReadN(r, header[nSize:nSize+4]) != 4 {
log.Println("RTMPRead_Packet: Failed to read extended timestamp") log.Println("RTMPRead_Packet: Failed to read extended timestamp")
return false return false
} }
// TODO: port this // TODO: port this
packet.m_nTimeStamp = C_AMF_DecodeInt32((*[_Gi]byte)(unsafe.Pointer(header))[nSize : nSize+4]) packet.m_nTimeStamp = C_AMF_DecodeInt32(header[nSize : nSize+4])
hSize += 4 hSize += 4
} }