Tidied up some comments and simplified queuing logic in writeTo().

This commit is contained in:
scruzin 2019-01-13 13:28:34 +10:30
parent dd562f1a28
commit 9f3d49faa5
1 changed files with 15 additions and 18 deletions

View File

@ -220,7 +220,7 @@ func (pkt *packet) readFrom(s *Session) error {
pkt.bytesRead += uint32(chunkSize) pkt.bytesRead += uint32(chunkSize)
// keep the packet as a reference for other packets on this channel // Keep the packet as a reference for other packets on this channel.
if s.channelsIn[pkt.channel] == nil { if s.channelsIn[pkt.channel] == nil {
s.channelsIn[pkt.channel] = &packet{} s.channelsIn[pkt.channel] = &packet{}
} }
@ -231,7 +231,7 @@ func (pkt *packet) readFrom(s *Session) error {
} }
if !pkt.hasAbsTimestamp { if !pkt.hasAbsTimestamp {
// timestamps seem to always be relative // Timestamps seem to always be relative.
pkt.timestamp += uint32(s.channelTimestamp[pkt.channel]) pkt.timestamp += uint32(s.channelTimestamp[pkt.channel])
} }
s.channelTimestamp[pkt.channel] = int32(pkt.timestamp) s.channelTimestamp[pkt.channel] = int32(pkt.timestamp)
@ -269,7 +269,7 @@ func (pkt *packet) resize(size uint32, ht uint8) {
// writeTo writes a packet to the RTMP connection. // writeTo writes a packet to the RTMP connection.
// Packets are written in chunks which are Session.chunkSize in length (128 bytes in length). // Packets are written in chunks which are Session.chunkSize in length (128 bytes in length).
// We defers sending small audio packets and combine consecutive small audio packets where possible to reduce I/O. // We defer sending small audio packets and combine consecutive small audio packets where possible to reduce I/O.
// When queue is true, we expect a response to this request and cache the method on s.methodCalls. // When queue is true, we expect a response to this request and cache the method on s.methodCalls.
func (pkt *packet) writeTo(s *Session, queue bool) error { func (pkt *packet) writeTo(s *Session, queue bool) error {
if pkt.body == nil { if pkt.body == nil {
@ -298,7 +298,7 @@ func (pkt *packet) writeTo(s *Session, queue bool) error {
prevPkt := s.channelsOut[pkt.channel] prevPkt := s.channelsOut[pkt.channel]
var last int var last int
if prevPkt != nil && pkt.headerType != headerSizeLarge { if prevPkt != nil && pkt.headerType != headerSizeLarge {
// compress a bit by using the prev packet's attributes // Compress header by using the previous packet's attributes.
if prevPkt.bodySize == pkt.bodySize && prevPkt.packetType == pkt.packetType && pkt.headerType == headerSizeMedium { if prevPkt.bodySize == pkt.bodySize && prevPkt.packetType == pkt.packetType && pkt.headerType == headerSizeMedium {
pkt.headerType = headerSizeSmall pkt.headerType = headerSizeSmall
} }
@ -321,7 +321,7 @@ func (pkt *packet) writeTo(s *Session, queue bool) error {
hSize := headerSizes[pkt.headerType] hSize := headerSizes[pkt.headerType]
origIdx := fullHeaderSize - hSize origIdx := fullHeaderSize - hSize
// adjust 1 or 2 bytes for the channel // Adjust 1 or 2 bytes depending on the channel.
cSize := 0 cSize := 0
switch { switch {
case pkt.channel > 319: case pkt.channel > 319:
@ -335,7 +335,7 @@ func (pkt *packet) writeTo(s *Session, queue bool) error {
hSize += cSize hSize += cSize
} }
// adjust 4 bytes for the timestamp // Adjust 4 bytes for the timestamp.
var ts uint32 var ts uint32
if prevPkt != nil { if prevPkt != nil {
ts = uint32(int(pkt.timestamp) - last) ts = uint32(int(pkt.timestamp) - last)
@ -372,11 +372,11 @@ func (pkt *packet) writeTo(s *Session, queue bool) error {
} }
if headerSizes[pkt.headerType] > 1 { if headerSizes[pkt.headerType] > 1 {
res := ts tmp := ts
if ts > 0xffffff { if ts > 0xffffff {
res = 0xffffff tmp = 0xffffff
} }
amf.EncodeInt24(headBytes[headerIdx:], int32(res)) amf.EncodeInt24(headBytes[headerIdx:], int32(tmp))
headerIdx += 3 // 24bits headerIdx += 3 // 24bits
} }
@ -468,17 +468,14 @@ func (pkt *packet) writeTo(s *Session, queue bool) error {
} }
} }
// We invoked a remote method, // If we invoked a remote method and queue is true, we queue the method until the result arrives.
if pkt.packetType == packetTypeInvoke { if pkt.packetType == packetTypeInvoke && queue {
buf := pkt.body[1:] buf := pkt.body[1:]
meth := amf.DecodeString(buf) meth := amf.DecodeString(buf)
s.log(DebugLevel, pkg+"invoking method "+meth) s.log(DebugLevel, pkg+"queuing method "+meth)
// keep it in call queue till result arrives buf = buf[3+len(meth):]
if queue { txn := int32(amf.DecodeNumber(buf[:8]))
buf = buf[3+len(meth):] s.methodCalls = append(s.methodCalls, method{name: meth, num: txn})
txn := int32(amf.DecodeNumber(buf[:8]))
s.methodCalls = append(s.methodCalls, method{name: meth, num: txn})
}
} }
if s.channelsOut[pkt.channel] == nil { if s.channelsOut[pkt.channel] == nil {