diff --git a/client.go b/client.go index 4ca17ba..007fd57 100644 --- a/client.go +++ b/client.go @@ -2,6 +2,7 @@ package rtmp import ( "crypto/tls" + "github.com/elobuff/goamf" "net" "net/url" "sync" @@ -33,6 +34,8 @@ type Client struct { lastTransactionId uint32 connectionId string + + amfExternalHandlers map[string]amf.ExternalHandler } func NewClient(url string) (c *Client) { @@ -78,11 +81,16 @@ func (c *Client) Reset() { c.inChunkSize = DEFAULT_CHUNK_SIZE c.inWindowSize = DEFAULT_WINDOW_SIZE c.inChunkStreams = make(map[uint32]*InboundChunkStream) + c.amfExternalHandlers = make(map[string]amf.ExternalHandler) c.responses = make(map[uint32]*Response) c.lastTransactionId = 0 c.connectionId = "" } +func (c *Client) RegisterExternalHandler(name string, fn amf.ExternalHandler) { + c.amfExternalHandlers[name] = fn +} + func (c *Client) Disconnect() { c.Reset() log.Info("disconnected from %s", c.url) diff --git a/client_route.go b/client_route.go index e6b6efe..b67ca69 100644 --- a/client_route.go +++ b/client_route.go @@ -23,7 +23,7 @@ func (c *Client) routeLoop() { } func (c *Client) routeCommandMessage(msg *Message) { - response, err := msg.DecodeResponse() + response, err := msg.DecodeResponse(c) if err != nil { log.Error("unable to decode message type %d on stream %d into command, discarding: %s", msg.Type, msg.ChunkStreamId, err) return diff --git a/message.go b/message.go index 17fbc8b..e5fa8e4 100644 --- a/message.go +++ b/message.go @@ -24,10 +24,14 @@ func (m *Message) RemainingBytes() uint32 { return m.Length - uint32(m.Buffer.Len()) } -func (m *Message) DecodeResponse() (response *Response, err error) { - dec := new(amf.Decoder) +func (m *Message) DecodeResponse(c *Client) (response *Response, err error) { + dec := amf.NewDecoder() response = new(Response) + for nam, fn := range c.amfExternalHandlers { + dec.RegisterExternalHandler(nam, fn) + } + if m.ChunkStreamId != CHUNK_STREAM_ID_COMMAND { return response, Error("message is not a command message") }