mirror of https://bitbucket.org/ausocean/av.git
Merged in improve-rtmp-errors (pull request #475)
protocol/rtmp: improving errors Approved-by: Trek Hopton
This commit is contained in:
commit
8c5a31b052
|
@ -138,14 +138,14 @@ func Dial(url string, log Log, options ...func(*Conn) error) (*Conn, error) {
|
|||
var err error
|
||||
c.link.protocol, c.link.host, c.link.port, c.link.app, c.link.playpath, err = parseURL(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("could not parse url: %w",err)
|
||||
}
|
||||
c.link.url = rtmpProtocolStrings[c.link.protocol] + "://" + c.link.host + ":" + strconv.Itoa(int(c.link.port)) + "/" + c.link.app
|
||||
c.link.protocol |= featureWrite
|
||||
|
||||
err = connect(&c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("could not connect: %w",err)
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
@ -159,11 +159,20 @@ func (c *Conn) Close() error {
|
|||
c.log(DebugLevel, pkg+"Conn.Close")
|
||||
if c.streamID > 0 {
|
||||
if c.link.protocol&featureWrite != 0 {
|
||||
sendFCUnpublish(c)
|
||||
err := sendFCUnpublish(c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not send fc unpublish: %w",err)
|
||||
}
|
||||
}
|
||||
err := sendDeleteStream(c, float64(c.streamID))
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not send delete stream: %w",err)
|
||||
}
|
||||
sendDeleteStream(c, float64(c.streamID))
|
||||
}
|
||||
c.link.conn.Close()
|
||||
err := c.link.conn.Close()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not close link conn: %w",err)
|
||||
}
|
||||
*c = Conn{}
|
||||
return nil
|
||||
}
|
||||
|
@ -192,7 +201,7 @@ func (c *Conn) Write(data []byte) (int, error) {
|
|||
copy(pkt.body, data[flvTagheaderSize:flvTagheaderSize+pkt.bodySize])
|
||||
err := pkt.writeTo(c, false)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, fmt.Errorf("could not write packet to connection: %w",err)
|
||||
}
|
||||
return len(data), nil
|
||||
}
|
||||
|
@ -205,18 +214,18 @@ func (c *Conn) Write(data []byte) (int, error) {
|
|||
func (c *Conn) read(buf []byte) (int, error) {
|
||||
err := c.link.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(c.link.timeout)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, fmt.Errorf("could not set read deadline: %w",err)
|
||||
}
|
||||
n, err := io.ReadFull(c.link.conn, buf)
|
||||
if err != nil {
|
||||
c.log(DebugLevel, pkg+"read failed", "error", err.Error())
|
||||
return 0, err
|
||||
return 0, fmt.Errorf("could not read conn: %w",err)
|
||||
}
|
||||
c.nBytesIn += uint32(n)
|
||||
if c.nBytesIn > (c.nBytesInSent + c.clientBW/10) {
|
||||
err := sendBytesReceived(c)
|
||||
if err != nil {
|
||||
return n, err // NB: we still read n bytes, even though send bytes failed
|
||||
return n, fmt.Errorf("could not send bytes received: %w",err) // NB: we still read n bytes, even though send bytes failed
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
|
@ -227,12 +236,12 @@ func (c *Conn) write(buf []byte) (int, error) {
|
|||
//ToDo: consider using a different timeout for writes than for reads
|
||||
err := c.link.conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(c.link.timeout)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return 0, fmt.Errorf("could not set write deadline: %w",err)
|
||||
}
|
||||
n, err := c.link.conn.Write(buf)
|
||||
if err != nil {
|
||||
c.log(WarnLevel, pkg+"write failed", "error", err.Error())
|
||||
return 0, err
|
||||
return 0, fmt.Errorf("could not write to conn: %w",err)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ package rtmp
|
|||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"bitbucket.org/ausocean/av/protocol/rtmp/amf"
|
||||
|
@ -105,7 +106,7 @@ func (pkt *packet) readFrom(c *Conn) error {
|
|||
if err == io.EOF {
|
||||
c.log(WarnLevel, pkg+"EOF error; connection likely terminated")
|
||||
}
|
||||
return err
|
||||
return fmt.Errorf("failed to read packet header 1st byte: %w", err)
|
||||
}
|
||||
pkt.headerType = (header[0] & 0xc0) >> 6
|
||||
pkt.channel = int32(header[0] & 0x3f)
|
||||
|
@ -116,7 +117,7 @@ func (pkt *packet) readFrom(c *Conn) error {
|
|||
_, err = c.read(header[:1])
|
||||
if err != nil {
|
||||
c.log(DebugLevel, pkg+"failed to read packet header 2nd byte", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("failed to read packet header second byte: %w", err)
|
||||
}
|
||||
header = header[1:]
|
||||
pkt.channel = int32(header[0]) + 64
|
||||
|
@ -125,7 +126,7 @@ func (pkt *packet) readFrom(c *Conn) error {
|
|||
_, err = c.read(header[:2])
|
||||
if err != nil {
|
||||
c.log(DebugLevel, pkg+"failed to read packet header 3rd byte", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("failed to read packet header 3rd byte: %w", err)
|
||||
}
|
||||
header = header[2:]
|
||||
pkt.channel = int32(binary.BigEndian.Uint16(header[:2])) + 64
|
||||
|
@ -169,7 +170,7 @@ func (pkt *packet) readFrom(c *Conn) error {
|
|||
_, err = c.read(header[:size])
|
||||
if err != nil {
|
||||
c.log(DebugLevel, pkg+"failed to read packet header", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("failed to read packet header: %w", err)
|
||||
}
|
||||
}
|
||||
hSize := len(hbuf) - len(header) + size
|
||||
|
@ -193,7 +194,7 @@ func (pkt *packet) readFrom(c *Conn) error {
|
|||
_, err = c.read(header[size : size+4])
|
||||
if err != nil {
|
||||
c.log(DebugLevel, pkg+"failed to read extended timestamp", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("failed to read extended timestamp: %w", err)
|
||||
}
|
||||
pkt.timestamp = amf.DecodeInt32(header[size : size+4])
|
||||
hSize += 4
|
||||
|
@ -208,7 +209,7 @@ func (pkt *packet) readFrom(c *Conn) error {
|
|||
_, err = c.read(pkt.body[:pkt.bodySize])
|
||||
if err != nil {
|
||||
c.log(DebugLevel, pkg+"failed to read packet body", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("failed to read packet body: %w", err)
|
||||
}
|
||||
|
||||
// Keep the packet as a reference for other packets on this channel.
|
||||
|
@ -404,7 +405,7 @@ func (pkt *packet) writeTo(c *Conn, queue bool) error {
|
|||
c.log(DebugLevel, pkg+"sending deferred packet separately", "size", len(c.deferred))
|
||||
_, err := c.write(c.deferred)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not write deferred packet: %w", err)
|
||||
}
|
||||
c.deferred = nil
|
||||
}
|
||||
|
@ -424,7 +425,7 @@ func (pkt *packet) writeTo(c *Conn, queue bool) error {
|
|||
}
|
||||
_, err := c.write(bytes)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not write combined packet: %w", err)
|
||||
}
|
||||
c.deferred = nil
|
||||
|
||||
|
|
|
@ -34,17 +34,25 @@ LICENSE
|
|||
package rtmp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Errors.
|
||||
var (
|
||||
errInvalidPath = errors.New("invalid url path")
|
||||
errInvalidElements = errors.New("invalid url elements")
|
||||
)
|
||||
|
||||
// parseURL parses an RTMP URL (ok, technically it is lexing).
|
||||
func parseURL(addr string) (protocol int32, host string, port uint16, app, playpath string, err error) {
|
||||
u, err := url.Parse(addr)
|
||||
if err != nil {
|
||||
return protocol, host, port, app, playpath, err
|
||||
return protocol, host, port, app, playpath, fmt.Errorf("could not parse to url value: %w", err)
|
||||
}
|
||||
|
||||
switch u.Scheme {
|
||||
|
@ -63,24 +71,24 @@ func parseURL(addr string) (protocol int32, host string, port uint16, app, playp
|
|||
case "rtmpts":
|
||||
protocol = protoRTMPTS
|
||||
default:
|
||||
return protocol, host, port, app, playpath, errUnknownScheme
|
||||
return protocol, host, port, app, playpath, fmt.Errorf("unknown scheme: %s", u.Scheme)
|
||||
}
|
||||
|
||||
host = u.Host
|
||||
if p := u.Port(); p != "" {
|
||||
pi, err := strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return protocol, host, port, app, playpath, err
|
||||
return protocol, host, port, app, playpath, fmt.Errorf("could convert port to integer: %w", err)
|
||||
}
|
||||
port = uint16(pi)
|
||||
}
|
||||
|
||||
if len(u.Path) < 1 || !path.IsAbs(u.Path) {
|
||||
return protocol, host, port, app, playpath, errInvalidURL
|
||||
return protocol, host, port, app, playpath, errInvalidPath
|
||||
}
|
||||
elems := strings.SplitN(u.Path[1:], "/", 3)
|
||||
if len(elems) < 2 || elems[0] == "" || elems[1] == "" {
|
||||
return protocol, host, port, app, playpath, errInvalidURL
|
||||
return protocol, host, port, app, playpath, errInvalidElements
|
||||
}
|
||||
app = elems[0]
|
||||
playpath = path.Join(elems[1:]...)
|
||||
|
@ -106,7 +114,7 @@ func parseURL(addr string) (protocol int32, host string, port uint16, app, playp
|
|||
switch {
|
||||
case port != 0:
|
||||
case (protocol & featureSSL) != 0:
|
||||
return protocol, host, port, app, playpath, errUnimplemented // port = 433
|
||||
return protocol, host, port, app, playpath, errors.New("ssl not implemented")
|
||||
case (protocol & featureHTTP) != 0:
|
||||
port = 80
|
||||
default:
|
||||
|
|
|
@ -38,6 +38,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"strconv"
|
||||
|
@ -164,14 +165,15 @@ var (
|
|||
|
||||
// connect establishes an RTMP connection.
|
||||
func connect(c *Conn) error {
|
||||
addr, err := net.ResolveTCPAddr("tcp4", c.link.host+":"+strconv.Itoa(int(c.link.port)))
|
||||
addrStr := c.link.host + ":" + strconv.Itoa(int(c.link.port))
|
||||
addr, err := net.ResolveTCPAddr("tcp4", addrStr)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not resolve tcp address (%s):%w", addrStr, err)
|
||||
}
|
||||
c.link.conn, err = net.DialTCP("tcp4", nil, addr)
|
||||
if err != nil {
|
||||
c.log(WarnLevel, pkg+"dial failed", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("could not dial tcp: %w", err)
|
||||
}
|
||||
c.log(DebugLevel, pkg+"connected")
|
||||
|
||||
|
@ -184,13 +186,13 @@ func connect(c *Conn) error {
|
|||
err = handshake(c)
|
||||
if err != nil {
|
||||
c.log(WarnLevel, pkg+"handshake failed", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("could not handshake: %w", err)
|
||||
}
|
||||
c.log(DebugLevel, pkg+"handshaked")
|
||||
err = sendConnectPacket(c)
|
||||
if err != nil {
|
||||
c.log(WarnLevel, pkg+"sendConnect failed", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("could not send connect packet: %w", err)
|
||||
}
|
||||
|
||||
c.log(DebugLevel, pkg+"negotiating")
|
||||
|
@ -199,7 +201,7 @@ func connect(c *Conn) error {
|
|||
pkt := packet{buf: buf[:]}
|
||||
err = pkt.readFrom(c)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not read from packet: %w", err)
|
||||
}
|
||||
|
||||
switch pkt.packetType {
|
||||
|
@ -208,7 +210,7 @@ func connect(c *Conn) error {
|
|||
default:
|
||||
err = handlePacket(c, &pkt)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not handle packet: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -248,7 +250,7 @@ func handlePacket(c *Conn, pkt *packet) error {
|
|||
err := handleInvoke(c, pkt.body[:pkt.bodySize])
|
||||
if err != nil {
|
||||
c.log(WarnLevel, pkg+"unexpected error from handleInvoke", "error", err.Error())
|
||||
return err
|
||||
return fmt.Errorf("could not handle invoke: %w", err)
|
||||
}
|
||||
|
||||
case packetTypeControl, packetTypeAudio, packetTypeVideo, packetTypeFlashVideo, packetTypeFlexMessage, packetTypeInfo:
|
||||
|
@ -273,12 +275,12 @@ func sendConnectPacket(c *Conn) error {
|
|||
|
||||
enc, err := amf.EncodeString(enc, avConnect)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode string: %w", err)
|
||||
}
|
||||
c.numInvokes += 1
|
||||
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of invokes: %w", err)
|
||||
}
|
||||
|
||||
// required link info
|
||||
|
@ -289,24 +291,29 @@ func sendConnectPacket(c *Conn) error {
|
|||
}
|
||||
enc, err = amf.Encode(&info, enc)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode info: %w", err)
|
||||
}
|
||||
|
||||
// optional link auth info
|
||||
if c.link.auth != "" {
|
||||
enc, err = amf.EncodeBoolean(enc, c.link.flags&linkAuth != 0)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode link auth bool: %w", err)
|
||||
}
|
||||
enc, err = amf.EncodeString(enc, c.link.auth)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode link auth string: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
||||
|
||||
return pkt.writeTo(c, true) // response expected
|
||||
err = pkt.writeTo(c, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendCreateStream(c *Conn) error {
|
||||
|
@ -322,19 +329,23 @@ func sendCreateStream(c *Conn) error {
|
|||
|
||||
enc, err := amf.EncodeString(enc, avCreatestream)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode av create stream token: %w", err)
|
||||
}
|
||||
c.numInvokes++
|
||||
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of invokes: %w", err)
|
||||
}
|
||||
enc[0] = amf.TypeNull
|
||||
enc = enc[1:]
|
||||
|
||||
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
||||
|
||||
return pkt.writeTo(c, true) // response expected
|
||||
err = pkt.writeTo(c, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendReleaseStream(c *Conn) error {
|
||||
|
@ -350,22 +361,27 @@ func sendReleaseStream(c *Conn) error {
|
|||
|
||||
enc, err := amf.EncodeString(enc, avReleasestream)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode av release stream token: %w", err)
|
||||
}
|
||||
c.numInvokes++
|
||||
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of invokes: %w", err)
|
||||
}
|
||||
enc[0] = amf.TypeNull
|
||||
enc = enc[1:]
|
||||
enc, err = amf.EncodeString(enc, c.link.playpath)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode playpath: %w", err)
|
||||
}
|
||||
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
||||
|
||||
return pkt.writeTo(c, false)
|
||||
err = pkt.writeTo(c, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendFCPublish(c *Conn) error {
|
||||
|
@ -381,23 +397,28 @@ func sendFCPublish(c *Conn) error {
|
|||
|
||||
enc, err := amf.EncodeString(enc, avFCPublish)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode av fc publish token: %w", err)
|
||||
}
|
||||
c.numInvokes++
|
||||
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of invokes: %w", err)
|
||||
}
|
||||
enc[0] = amf.TypeNull
|
||||
enc = enc[1:]
|
||||
enc, err = amf.EncodeString(enc, c.link.playpath)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode playpath: %w", err)
|
||||
}
|
||||
|
||||
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
||||
|
||||
return pkt.writeTo(c, false)
|
||||
err = pkt.writeTo(c, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendFCUnpublish(c *Conn) error {
|
||||
|
@ -413,23 +434,28 @@ func sendFCUnpublish(c *Conn) error {
|
|||
|
||||
enc, err := amf.EncodeString(enc, avFCUnpublish)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode av fc unpublish token: %w", err)
|
||||
}
|
||||
c.numInvokes++
|
||||
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of invokes: %w", err)
|
||||
}
|
||||
enc[0] = amf.TypeNull
|
||||
enc = enc[1:]
|
||||
enc, err = amf.EncodeString(enc, c.link.playpath)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode link playpath: %w", err)
|
||||
}
|
||||
|
||||
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
||||
|
||||
return pkt.writeTo(c, false)
|
||||
err = pkt.writeTo(c, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendPublish(c *Conn) error {
|
||||
|
@ -445,27 +471,32 @@ func sendPublish(c *Conn) error {
|
|||
|
||||
enc, err := amf.EncodeString(enc, avPublish)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode av publish token: %w", err)
|
||||
}
|
||||
c.numInvokes++
|
||||
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of invokes: %w", err)
|
||||
}
|
||||
enc[0] = amf.TypeNull
|
||||
enc = enc[1:]
|
||||
enc, err = amf.EncodeString(enc, c.link.playpath)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode link playpath: %w", err)
|
||||
}
|
||||
enc, err = amf.EncodeString(enc, avLive)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode av live token: %w", err)
|
||||
}
|
||||
|
||||
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
||||
|
||||
return pkt.writeTo(c, true) // response expected
|
||||
err = pkt.writeTo(c, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendDeleteStream(c *Conn, streamID float64) error {
|
||||
|
@ -481,22 +512,27 @@ func sendDeleteStream(c *Conn, streamID float64) error {
|
|||
|
||||
enc, err := amf.EncodeString(enc, avDeletestream)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode av delete stream token: %w", err)
|
||||
}
|
||||
c.numInvokes++
|
||||
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of invokes: %w", err)
|
||||
}
|
||||
enc[0] = amf.TypeNull
|
||||
enc = enc[1:]
|
||||
enc, err = amf.EncodeNumber(enc, streamID)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode stream id: %w", err)
|
||||
}
|
||||
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
||||
|
||||
return pkt.writeTo(c, false)
|
||||
err = pkt.writeTo(c, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendBytesReceived tells the server how many bytes the client has received.
|
||||
|
@ -515,11 +551,16 @@ func sendBytesReceived(c *Conn) error {
|
|||
|
||||
enc, err := amf.EncodeInt32(enc, c.nBytesIn)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of bytes in: %w", err)
|
||||
}
|
||||
pkt.bodySize = 4
|
||||
|
||||
return pkt.writeTo(c, false)
|
||||
err = pkt.writeTo(c, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendCheckBW(c *Conn) error {
|
||||
|
@ -535,19 +576,24 @@ func sendCheckBW(c *Conn) error {
|
|||
|
||||
enc, err := amf.EncodeString(enc, av_checkbw)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode av check bw token: %w", err)
|
||||
}
|
||||
c.numInvokes++
|
||||
enc, err = amf.EncodeNumber(enc, float64(c.numInvokes))
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not encode number of invokes: %w", err)
|
||||
}
|
||||
enc[0] = amf.TypeNull
|
||||
enc = enc[1:]
|
||||
|
||||
pkt.bodySize = uint32((len(pbuf) - fullHeaderSize) - len(enc))
|
||||
|
||||
return pkt.writeTo(c, false)
|
||||
err = pkt.writeTo(c, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not write packet: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func eraseMethod(m []method, i int) []method {
|
||||
|
@ -565,16 +611,16 @@ func handleInvoke(c *Conn, body []byte) error {
|
|||
var obj amf.Object
|
||||
_, err := amf.Decode(&obj, body, false)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not decode: %w", err)
|
||||
}
|
||||
|
||||
meth, err := obj.StringProperty("", 0)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not get value of string property meth: %w", err)
|
||||
}
|
||||
txn, err := obj.NumberProperty("", 1)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not get value of number property txn: %w", err)
|
||||
}
|
||||
|
||||
c.log(DebugLevel, pkg+"invoking method "+meth)
|
||||
|
@ -601,21 +647,21 @@ func handleInvoke(c *Conn, body []byte) error {
|
|||
case avConnect:
|
||||
err := sendReleaseStream(c)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not send release stream: %w", err)
|
||||
}
|
||||
err = sendFCPublish(c)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not send fc publish: %w", err)
|
||||
}
|
||||
err = sendCreateStream(c)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not send create stream: %w", err)
|
||||
}
|
||||
|
||||
case avCreatestream:
|
||||
n, err := obj.NumberProperty("", 3)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not get value for stream id number property: %w",err)
|
||||
}
|
||||
c.streamID = uint32(n)
|
||||
err = sendPublish(c)
|
||||
|
@ -630,27 +676,27 @@ func handleInvoke(c *Conn, body []byte) error {
|
|||
case avOnBWDone:
|
||||
err := sendCheckBW(c)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not send check bw: %w", err)
|
||||
}
|
||||
|
||||
case avOnStatus:
|
||||
obj2, err := obj.ObjectProperty("", 3)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not get object property value for obj2: %w", err)
|
||||
}
|
||||
code, err := obj2.StringProperty(avCode, -1)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not get string property value for code: %w", err)
|
||||
}
|
||||
level, err := obj2.StringProperty(avLevel, -1)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not get string property value for level: %w", err)
|
||||
}
|
||||
c.log(DebugLevel, pkg+"onStatus", "code", code, "level", level)
|
||||
|
||||
if code != avNetStreamPublish_Start {
|
||||
c.log(ErrorLevel, pkg+"unexpected response "+code)
|
||||
return errUnimplemented
|
||||
return fmt.Errorf("unimplemented code: %v", code)
|
||||
}
|
||||
c.log(DebugLevel, pkg+"playing")
|
||||
c.isPlaying = true
|
||||
|
@ -681,14 +727,14 @@ func handshake(c *Conn) error {
|
|||
|
||||
_, err := c.write(clientbuf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not write handshake: %w", err)
|
||||
}
|
||||
c.log(DebugLevel, pkg+"handshake sent")
|
||||
|
||||
var typ [1]byte
|
||||
_, err = c.read(typ[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not read handshake: %w", err)
|
||||
}
|
||||
c.log(DebugLevel, pkg+"handshake received")
|
||||
|
||||
|
@ -697,7 +743,7 @@ func handshake(c *Conn) error {
|
|||
}
|
||||
_, err = c.read(serversig[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not read server signal: %w", err)
|
||||
}
|
||||
|
||||
// decode server response
|
||||
|
@ -707,12 +753,12 @@ func handshake(c *Conn) error {
|
|||
// 2nd part of handshake
|
||||
_, err = c.write(serversig[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not write part 2 of handshake: %w", err)
|
||||
}
|
||||
|
||||
_, err = c.read(serversig[:])
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not read part 2 of handshake: %w", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(serversig[:signatureSize], clientbuf[1:signatureSize+1]) {
|
||||
|
|
Loading…
Reference in New Issue