rtmp: simplify header parsing

This commit is contained in:
Dan Kortschak 2018-09-26 13:22:44 +09:30
parent 85b74d6624
commit 22c926611d
1 changed files with 9 additions and 18 deletions

View File

@ -1079,16 +1079,12 @@ 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
var header *byte header := (*byte)(unsafe.Pointer(&hbuf[0]))
header = (*byte)(unsafe.Pointer(&hbuf[0]))
var nSize, hSize, nToRead, nChunk int32
var extendedTimestamp int32
if C_ReadN(r, hbuf[:1]) != 1 { if C_ReadN(r, hbuf[: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 = (hbuf[0] & 0xc0) >> 6
packet.m_nChannel = int32(hbuf[0] & 0x3f) packet.m_nChannel = int32(hbuf[0] & 0x3f)
header = (*byte)(incBytePtr(unsafe.Pointer(header), 1)) header = (*byte)(incBytePtr(unsafe.Pointer(header), 1))
@ -1114,8 +1110,6 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
header = (*byte)(incBytePtr(unsafe.Pointer(header), 2)) header = (*byte)(incBytePtr(unsafe.Pointer(header), 2))
} }
nSize = int32(packetSize[packet.m_headerType])
if packet.m_nChannel >= r.m_channelsAllocatedIn { if packet.m_nChannel >= r.m_channelsAllocatedIn {
n := packet.m_nChannel + 10 n := packet.m_nChannel + 10
timestamp := append(r.m_channelTimestamp, make([]int32, 10)...) timestamp := append(r.m_channelTimestamp, make([]int32, 10)...)
@ -1139,6 +1133,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
r.m_channelsAllocatedIn = n r.m_channelsAllocatedIn = n
} }
nSize := packetSize[packet.m_headerType]
switch { switch {
case nSize == RTMP_LARGE_HEADER_SIZE: case nSize == RTMP_LARGE_HEADER_SIZE:
packet.m_hasAbsTimestamp = true packet.m_hasAbsTimestamp = true
@ -1155,8 +1150,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
return false return false
} }
hSize = int32(uintptr(incBytePtr(decBytePtr(unsafe.Pointer(header), int(uintptr( hSize := int(uintptr(unsafe.Pointer(header))-uintptr(unsafe.Pointer(&hbuf[0]))) + nSize
unsafe.Pointer(&hbuf[0])))), int(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((*[_Gi]byte)(unsafe.Pointer(header))[:3])
@ -1174,12 +1168,9 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
} }
} }
} }
extendedTimestamp = 0
if packet.m_nTimeStamp == 0xffffff {
extendedTimestamp = 1
}
if extendedTimestamp != 0 { extendedTimestamp := packet.m_nTimeStamp == 0xffffff
if extendedTimestamp {
if C_ReadN(r, pl2b((*byte)(incBytePtr(unsafe.Pointer(header), int(nSize))), 4)) != 4 { if C_ReadN(r, pl2b((*byte)(incBytePtr(unsafe.Pointer(header), int(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
@ -1198,15 +1189,15 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
packet.m_headerType = (hbuf[0] & 0xc0) >> 6 packet.m_headerType = (hbuf[0] & 0xc0) >> 6
} }
nToRead = int32(packet.m_nBodySize - packet.m_nBytesRead) nToRead := int32(packet.m_nBodySize - packet.m_nBytesRead)
nChunk = r.m_inChunkSize nChunk := r.m_inChunkSize
if nToRead < nChunk { if nToRead < nChunk {
nChunk = nToRead nChunk = nToRead
} }
if packet.m_chunk != nil { if packet.m_chunk != nil {
packet.m_chunk.c_headerSize = hSize packet.m_chunk.c_headerSize = int32(hSize)
copy(packet.m_chunk.c_header[:], hbuf[:hSize]) copy(packet.m_chunk.c_header[:], hbuf[:hSize])
packet.m_chunk.c_chunk = packet.m_body[packet.m_nBytesRead : packet.m_nBytesRead+uint32(nChunk)] packet.m_chunk.c_chunk = packet.m_body[packet.m_nBytesRead : packet.m_nBytesRead+uint32(nChunk)]
} }
@ -1224,7 +1215,7 @@ func C_RTMP_ReadPacket(r *C_RTMP, packet *C_RTMPPacket) (ok bool) {
} }
*(r.m_vecChannelsIn[packet.m_nChannel]) = *packet *(r.m_vecChannelsIn[packet.m_nChannel]) = *packet
if extendedTimestamp != 0 { if extendedTimestamp {
r.m_vecChannelsIn[packet.m_nChannel].m_nTimeStamp = 0xffffff r.m_vecChannelsIn[packet.m_nChannel].m_nTimeStamp = 0xffffff
} }