readPacket(), sendPacket() and resizePacket() now Packet methods read(), write() and resize() respectively.

This commit is contained in:
scruzin 2019-01-10 23:29:51 +10:30
parent 6a8e78a256
commit 058cc41356
3 changed files with 19 additions and 19 deletions

View File

@ -99,8 +99,8 @@ type chunk struct {
} }
// ToDo: Consider making the following functions into methods. // ToDo: Consider making the following functions into methods.
// readPacket reads a packet. // read reads a packet.
func readPacket(s *Session, pkt *packet) error { func (pkt *packet) read(s *Session) error {
var hbuf [RTMP_MAX_HEADER_SIZE]byte var hbuf [RTMP_MAX_HEADER_SIZE]byte
header := hbuf[:] header := hbuf[:]
@ -208,7 +208,7 @@ func readPacket(s *Session, pkt *packet) error {
} }
if pkt.bodySize > 0 && pkt.body == nil { 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) toRead := int32(pkt.bodySize - pkt.bytesRead)
@ -258,8 +258,8 @@ func readPacket(s *Session, pkt *packet) error {
return nil return nil
} }
// resizePacket adjusts the packet's storage to accommodate a body of the given size. // resize adjusts the packet's storage to accommodate a body of the given size.
func resizePacket(pkt *packet, size uint32, ht uint8) { func (pkt *packet) resize(size uint32, ht uint8) {
buf := make([]byte, RTMP_MAX_HEADER_SIZE+size) buf := make([]byte, RTMP_MAX_HEADER_SIZE+size)
pkt.header = buf pkt.header = buf
pkt.body = buf[RTMP_MAX_HEADER_SIZE:] pkt.body = buf[RTMP_MAX_HEADER_SIZE:]
@ -282,8 +282,8 @@ func resizePacket(pkt *packet, size uint32, ht uint8) {
} }
} }
// sendPacket sends a packet. // write sends a packet.
func sendPacket(s *Session, pkt *packet, queue bool) error { func (pkt *packet) write(s *Session, queue bool) error {
var prevPkt *packet var prevPkt *packet
var last int var last int

View File

@ -193,7 +193,7 @@ func connectStream(s *Session) error {
var err error var err error
for !s.isPlaying && s.isConnected() { for !s.isPlaying && s.isConnected() {
pkt := packet{} pkt := packet{}
err = readPacket(s, &pkt) err = pkt.read(s)
if err != nil { if err != nil {
break break
} }
@ -389,7 +389,7 @@ func sendConnectPacket(s *Session) error {
pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc)) 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 { 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)) 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 { 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)) 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 { 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)) 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 { 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)) 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 { 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)) 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 { 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)) pkt.bodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
/* no response expected */ /* no response expected */
return sendPacket(s, &pkt, false) return pkt.write(s, false)
} }
// sendBytesReceived tells the server how many bytes the client has received. // sendBytesReceived tells the server how many bytes the client has received.
@ -602,7 +602,7 @@ func sendBytesReceived(s *Session) error {
} }
pkt.bodySize = 4 pkt.bodySize = 4
return sendPacket(s, &pkt, false) return pkt.write(s, false)
} }
func sendCheckBW(s *Session) error { 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)) 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 { func eraseMethod(m []method, i int) []method {

View File

@ -179,9 +179,9 @@ func (s *Session) Write(data []byte) (int, error) {
info: s.streamID, 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]) copy(pkt.body, data[minDataSize:minDataSize+pkt.bodySize])
err := sendPacket(s, &pkt, false) err := pkt.write(s, false)
if err != nil { if err != nil {
return 0, err return 0, err
} }