From 058cc4135686eb7803e2b68424a3728b1e3e8607 Mon Sep 17 00:00:00 2001 From: scruzin Date: Thu, 10 Jan 2019 23:29:51 +1030 Subject: [PATCH] readPacket(), sendPacket() and resizePacket() now Packet methods read(), write() and resize() respectively. --- rtmp/packet.go | 14 +++++++------- rtmp/rtmp.go | 20 ++++++++++---------- rtmp/session.go | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/rtmp/packet.go b/rtmp/packet.go index 0c272657..99c9e029 100644 --- a/rtmp/packet.go +++ b/rtmp/packet.go @@ -99,8 +99,8 @@ type chunk struct { } // ToDo: Consider making the following functions into methods. -// readPacket reads a packet. -func readPacket(s *Session, pkt *packet) error { +// read reads a packet. +func (pkt *packet) read(s *Session) error { var hbuf [RTMP_MAX_HEADER_SIZE]byte header := hbuf[:] @@ -208,7 +208,7 @@ func readPacket(s *Session, pkt *packet) error { } if pkt.bodySize > 0 && pkt.body == nil { - resizePacket(pkt, pkt.bodySize, (hbuf[0]&0xc0)>>6) + pkt.resize(pkt.bodySize, (hbuf[0]&0xc0)>>6) } toRead := int32(pkt.bodySize - pkt.bytesRead) @@ -258,8 +258,8 @@ func readPacket(s *Session, pkt *packet) error { return nil } -// resizePacket adjusts the packet's storage to accommodate a body of the given size. -func resizePacket(pkt *packet, size uint32, ht uint8) { +// resize adjusts the packet's storage to accommodate a body of the given size. +func (pkt *packet) resize(size uint32, ht uint8) { buf := make([]byte, RTMP_MAX_HEADER_SIZE+size) pkt.header = buf pkt.body = buf[RTMP_MAX_HEADER_SIZE:] @@ -282,8 +282,8 @@ func resizePacket(pkt *packet, size uint32, ht uint8) { } } -// sendPacket sends a packet. -func sendPacket(s *Session, pkt *packet, queue bool) error { +// write sends a packet. +func (pkt *packet) write(s *Session, queue bool) error { var prevPkt *packet var last int diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index b38a1a1a..791c1b93 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -193,7 +193,7 @@ func connectStream(s *Session) error { var err error for !s.isPlaying && s.isConnected() { pkt := packet{} - err = readPacket(s, &pkt) + err = pkt.read(s) if err != nil { break } @@ -389,7 +389,7 @@ func sendConnectPacket(s *Session) error { pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) - return sendPacket(s, &pkt, true) // response expected + return pkt.write(s, true) // response expected } func sendCreateStream(s *Session) error { @@ -417,7 +417,7 @@ func sendCreateStream(s *Session) error { pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) - return sendPacket(s, &pkt, true) // response expected + return pkt.write(s, true) // response expected } func sendReleaseStream(s *Session) error { @@ -448,7 +448,7 @@ func sendReleaseStream(s *Session) error { } pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) - return sendPacket(s, &pkt, false) + return pkt.write(s, false) } func sendFCPublish(s *Session) error { @@ -480,7 +480,7 @@ func sendFCPublish(s *Session) error { pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) - return sendPacket(s, &pkt, false) + return pkt.write(s, false) } func sendFCUnpublish(s *Session) error { @@ -512,7 +512,7 @@ func sendFCUnpublish(s *Session) error { pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) - return sendPacket(s, &pkt, false) + return pkt.write(s, false) } func sendPublish(s *Session) error { @@ -548,7 +548,7 @@ func sendPublish(s *Session) error { pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) - return sendPacket(s, &pkt, true) // response expected + return pkt.write(s, true) // response expected } func sendDeleteStream(s *Session, dStreamId float64) error { @@ -580,7 +580,7 @@ func sendDeleteStream(s *Session, dStreamId float64) error { pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) /* no response expected */ - return sendPacket(s, &pkt, false) + return pkt.write(s, false) } // sendBytesReceived tells the server how many bytes the client has received. @@ -602,7 +602,7 @@ func sendBytesReceived(s *Session) error { } pkt.bodySize = 4 - return sendPacket(s, &pkt, false) + return pkt.write(s, false) } func sendCheckBW(s *Session) error { @@ -630,7 +630,7 @@ func sendCheckBW(s *Session) error { pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) - return sendPacket(s, &pkt, false) + return pkt.write(s, false) } func eraseMethod(m []method, i int) []method { diff --git a/rtmp/session.go b/rtmp/session.go index 2aa62fe0..e09b3caa 100644 --- a/rtmp/session.go +++ b/rtmp/session.go @@ -179,9 +179,9 @@ func (s *Session) Write(data []byte) (int, error) { info: s.streamID, } - resizePacket(&pkt, pkt.bodySize, RTMP_PACKET_SIZE_AUTO) + pkt.resize(pkt.bodySize, RTMP_PACKET_SIZE_AUTO) copy(pkt.body, data[minDataSize:minDataSize+pkt.bodySize]) - err := sendPacket(s, &pkt, false) + err := pkt.write(s, false) if err != nil { return 0, err }