diff --git a/rtmp/packet.go b/rtmp/packet.go index adbe32f3..70ead13a 100644 --- a/rtmp/packet.go +++ b/rtmp/packet.go @@ -91,19 +91,11 @@ type packet struct { info int32 bodySize uint32 bytesRead uint32 - chunk *chunk header []byte body []byte } -// chunk defines an RTMP packet chunk. -type chunk struct { - headerSize int32 - data []byte - header [fullHeaderSize]byte -} - -// read reads a packet. +// read reads an RTMP packet. func (pkt *packet) read(s *Session) error { var hbuf [fullHeaderSize]byte header := hbuf[:] @@ -222,13 +214,6 @@ func (pkt *packet) read(s *Session) error { chunkSize = toRead } - if pkt.chunk != nil { - panic("non-nil chunk") - pkt.chunk.headerSize = int32(hSize) - copy(pkt.chunk.header[:], hbuf[:hSize]) - pkt.chunk.data = pkt.body[pkt.bytesRead : pkt.bytesRead+uint32(chunkSize)] - } - _, err = s.read(pkt.body[pkt.bytesRead:][:chunkSize]) if err != nil { s.log(DebugLevel, pkg+"failed to read packet body", "error", err.Error()) @@ -237,7 +222,7 @@ func (pkt *packet) read(s *Session) error { pkt.bytesRead += uint32(chunkSize) - // keep the packet as ref for other packets on this channel + // keep the packet as a reference for other packets on this channel if s.channelsIn[pkt.channel] == nil { s.channelsIn[pkt.channel] = &packet{} } @@ -264,6 +249,7 @@ func (pkt *packet) read(s *Session) error { } // resize adjusts the packet's storage to accommodate a body of the given size and header type. +// When headerSizeAuto is specified, the header type is computed based on packet type. func (pkt *packet) resize(size uint32, ht uint8) { buf := make([]byte, fullHeaderSize+size) pkt.header = buf @@ -287,7 +273,9 @@ func (pkt *packet) resize(size uint32, ht uint8) { } } -// write sends a packet. +// write sends an RTMP packet. +// 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. // When queue is true, we expect a response to this request and cache the method on s.methodCalls. func (pkt *packet) write(s *Session, queue bool) error { if pkt.body == nil { @@ -438,7 +426,6 @@ func (pkt *packet) write(s *Session, queue bool) error { } // TODO(kortschak): Rewrite this horrific peice of premature optimisation. - // NB: RTMP wants packets in chunks which are 128 bytes by default, but the server may request a different size. s.log(DebugLevel, pkg+"sending packet", "la", s.link.conn.LocalAddr(), "ra", s.link.conn.RemoteAddr(), "size", size) for size+hSize != 0 { if chunkSize > size { @@ -461,6 +448,7 @@ func (pkt *packet) write(s *Session, queue bool) error { hSize = 0 if size > 0 { + // We are writing the 2nd or subsequent chunk. origIdx -= 1 + cSize hSize = 1 + cSize @@ -486,7 +474,7 @@ func (pkt *packet) write(s *Session, queue bool) error { } } - // We invoked a remote method + // We invoked a remote method, if pkt.packetType == packetTypeInvoke { buf := pkt.body[1:] meth := amf.DecodeString(buf)