2022-09-17 19:19:45 +00:00
|
|
|
// Package rtmp implements a RTMP connection.
|
2021-04-03 09:39:19 +00:00
|
|
|
package rtmp
|
2021-03-10 14:06:45 +00:00
|
|
|
|
|
|
|
import (
|
2022-02-12 16:48:55 +00:00
|
|
|
"errors"
|
2021-12-22 16:37:15 +00:00
|
|
|
"fmt"
|
2022-08-15 13:42:16 +00:00
|
|
|
"io"
|
2021-03-10 14:06:45 +00:00
|
|
|
"net/url"
|
2022-07-17 13:17:18 +00:00
|
|
|
"strings"
|
2021-12-22 16:33:37 +00:00
|
|
|
"time"
|
2021-03-10 14:06:45 +00:00
|
|
|
|
2021-12-22 16:37:15 +00:00
|
|
|
"github.com/aler9/gortsplib"
|
2022-08-05 21:50:45 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/mpeg4audio"
|
2021-12-22 16:37:15 +00:00
|
|
|
"github.com/notedit/rtmp/format/flv/flvio"
|
2022-07-17 13:17:18 +00:00
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/rtmp/bytecounter"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/rtmp/h264conf"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/rtmp/handshake"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/rtmp/message"
|
2021-03-10 14:06:45 +00:00
|
|
|
)
|
|
|
|
|
2021-12-22 16:37:15 +00:00
|
|
|
const (
|
2022-07-17 13:17:18 +00:00
|
|
|
codecH264 = 7
|
|
|
|
codecAAC = 10
|
2021-12-22 16:37:15 +00:00
|
|
|
)
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
func resultIsOK1(res *message.MsgCommandAMF0) bool {
|
|
|
|
if len(res.Arguments) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
ma, ok := res.Arguments[1].(flvio.AMFMap)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := ma.GetString("level")
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return v == "status"
|
2021-03-10 14:06:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
func resultIsOK2(res *message.MsgCommandAMF0) bool {
|
|
|
|
if len(res.Arguments) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
2022-07-09 15:25:33 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
v, ok := res.Arguments[1].(float64)
|
|
|
|
if !ok {
|
|
|
|
return false
|
2022-07-09 15:25:33 +00:00
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
|
|
|
|
return v == 1
|
2022-07-09 15:25:33 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
func splitPath(u *url.URL) (app, stream string) {
|
|
|
|
nu := *u
|
|
|
|
nu.ForceQuery = false
|
2022-07-09 15:25:33 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
pathsegs := strings.Split(nu.RequestURI(), "/")
|
|
|
|
if len(pathsegs) == 2 {
|
|
|
|
app = pathsegs[1]
|
|
|
|
}
|
|
|
|
if len(pathsegs) == 3 {
|
|
|
|
app = pathsegs[1]
|
|
|
|
stream = pathsegs[2]
|
|
|
|
}
|
|
|
|
if len(pathsegs) > 3 {
|
|
|
|
app = strings.Join(pathsegs[1:3], "/")
|
|
|
|
stream = strings.Join(pathsegs[3:], "/")
|
2022-07-09 15:25:33 +00:00
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
return
|
2021-12-22 16:33:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
func getTcURL(u *url.URL) string {
|
|
|
|
app, _ := splitPath(u)
|
|
|
|
nu, _ := url.Parse(u.String()) // perform a deep copy
|
|
|
|
nu.RawQuery = ""
|
|
|
|
nu.Path = "/"
|
|
|
|
return nu.String() + app
|
|
|
|
}
|
|
|
|
|
|
|
|
func createURL(tcurl, app, play string) (*url.URL, error) {
|
|
|
|
u, err := url.ParseRequestURI("/" + app + "/" + play)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tu, err := url.Parse(tcurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tu.Host == "" {
|
|
|
|
return nil, fmt.Errorf("invalid host")
|
2022-07-09 14:19:49 +00:00
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
u.Host = tu.Host
|
|
|
|
|
|
|
|
if tu.Scheme == "" {
|
|
|
|
return nil, fmt.Errorf("invalid scheme")
|
|
|
|
}
|
|
|
|
u.Scheme = tu.Scheme
|
|
|
|
|
|
|
|
return u, nil
|
2021-12-22 16:37:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
// Conn is a RTMP connection.
|
|
|
|
type Conn struct {
|
|
|
|
bc *bytecounter.ReadWriter
|
|
|
|
mrw *message.ReadWriter
|
2021-12-22 16:37:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
// NewConn initializes a connection.
|
2022-08-15 13:42:16 +00:00
|
|
|
func NewConn(rw io.ReadWriter) *Conn {
|
2022-07-17 13:17:18 +00:00
|
|
|
c := &Conn{}
|
2022-08-15 13:42:16 +00:00
|
|
|
c.bc = bytecounter.NewReadWriter(rw)
|
2022-07-17 13:17:18 +00:00
|
|
|
c.mrw = message.NewReadWriter(c.bc, false)
|
|
|
|
return c
|
2021-03-10 14:06:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
func (c *Conn) readCommand() (*message.MsgCommandAMF0, error) {
|
|
|
|
for {
|
|
|
|
msg, err := c.mrw.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd, ok := msg.(*message.MsgCommandAMF0); ok {
|
|
|
|
return cmd, nil
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 14:06:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 09:20:23 +00:00
|
|
|
func (c *Conn) readCommandResult(commandID int, commandName string, isValid func(*message.MsgCommandAMF0) bool) error {
|
2022-07-17 13:17:18 +00:00
|
|
|
for {
|
|
|
|
msg, err := c.mrw.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd, ok := msg.(*message.MsgCommandAMF0); ok {
|
2022-08-22 09:20:23 +00:00
|
|
|
if cmd.CommandID == commandID && cmd.Name == commandName {
|
2022-07-17 13:17:18 +00:00
|
|
|
if !isValid(cmd) {
|
|
|
|
return fmt.Errorf("server refused connect request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 14:06:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
// InitializeClient performs the initialization of a client-side connection.
|
2022-08-16 10:02:25 +00:00
|
|
|
func (c *Conn) InitializeClient(u *url.URL, isPublishing bool) error {
|
2022-07-17 13:17:18 +00:00
|
|
|
connectpath, actionpath := splitPath(u)
|
|
|
|
|
|
|
|
err := handshake.DoClient(c.bc, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgSetWindowAckSize{
|
|
|
|
Value: 2500000,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgSetPeerBandwidth{
|
|
|
|
Value: 2500000,
|
|
|
|
Type: 2,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgSetChunkSize{
|
|
|
|
Value: 65536,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 3,
|
|
|
|
Name: "connect",
|
|
|
|
CommandID: 1,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
flvio.AMFMap{
|
|
|
|
{K: "app", V: connectpath},
|
|
|
|
{K: "flashVer", V: "LNX 9,0,124,2"},
|
|
|
|
{K: "tcUrl", V: getTcURL(u)},
|
|
|
|
{K: "fpad", V: false},
|
|
|
|
{K: "capabilities", V: 15},
|
|
|
|
{K: "audioCodecs", V: 4071},
|
|
|
|
{K: "videoCodecs", V: 252},
|
|
|
|
{K: "videoFunction", V: 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-22 09:20:23 +00:00
|
|
|
err = c.readCommandResult(1, "_result", resultIsOK1)
|
2021-03-10 14:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
|
2022-08-16 10:02:25 +00:00
|
|
|
if !isPublishing {
|
2022-07-17 13:17:18 +00:00
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 3,
|
|
|
|
Name: "createStream",
|
|
|
|
CommandID: 2,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-22 09:20:23 +00:00
|
|
|
err = c.readCommandResult(2, "_result", resultIsOK2)
|
2022-07-17 13:17:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgUserControlSetBufferLength{
|
|
|
|
BufferLength: 0x64,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 4,
|
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Name: "play",
|
2022-08-22 08:57:29 +00:00
|
|
|
CommandID: 3,
|
2022-07-17 13:17:18 +00:00
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
actionpath,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-22 09:20:23 +00:00
|
|
|
return c.readCommandResult(3, "onStatus", resultIsOK1)
|
2022-08-22 08:55:06 +00:00
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
|
2022-08-22 08:55:06 +00:00
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 3,
|
|
|
|
Name: "releaseStream",
|
|
|
|
CommandID: 2,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
actionpath,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
|
2022-08-22 08:55:06 +00:00
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 3,
|
|
|
|
Name: "FCPublish",
|
|
|
|
CommandID: 3,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
actionpath,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
|
2022-08-22 08:55:06 +00:00
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 3,
|
|
|
|
Name: "createStream",
|
|
|
|
CommandID: 4,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
|
2022-08-22 09:20:23 +00:00
|
|
|
err = c.readCommandResult(4, "_result", resultIsOK2)
|
2022-08-22 08:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-17 13:17:18 +00:00
|
|
|
|
2022-08-22 08:55:06 +00:00
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 4,
|
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Name: "publish",
|
|
|
|
CommandID: 5,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
actionpath,
|
|
|
|
connectpath,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-07-17 13:17:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 09:20:23 +00:00
|
|
|
return c.readCommandResult(5, "onStatus", resultIsOK1)
|
2022-07-17 13:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitializeServer performs the initialization of a server-side connection.
|
|
|
|
func (c *Conn) InitializeServer() (*url.URL, bool, error) {
|
|
|
|
err := handshake.DoServer(c.bc, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd, err := c.readCommand()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Name != "connect" {
|
|
|
|
return nil, false, fmt.Errorf("unexpected command: %+v", cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cmd.Arguments) < 1 {
|
|
|
|
return nil, false, fmt.Errorf("invalid connect command: %+v", cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
ma, ok := cmd.Arguments[0].(flvio.AMFMap)
|
|
|
|
if !ok {
|
|
|
|
return nil, false, fmt.Errorf("invalid connect command: %+v", cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
connectpath, ok := ma.GetString("app")
|
|
|
|
if !ok {
|
|
|
|
return nil, false, fmt.Errorf("invalid connect command: %+v", cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
tcURL, ok := ma.GetString("tcUrl")
|
|
|
|
if !ok {
|
|
|
|
tcURL, ok = ma.GetString("tcurl")
|
|
|
|
if !ok {
|
|
|
|
return nil, false, fmt.Errorf("invalid connect command: %+v", cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgSetWindowAckSize{
|
|
|
|
Value: 2500000,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgSetPeerBandwidth{
|
|
|
|
Value: 2500000,
|
|
|
|
Type: 2,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgSetChunkSize{
|
|
|
|
Value: 65536,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
oe, _ := ma.GetFloat64("objectEncoding")
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: cmd.ChunkStreamID,
|
|
|
|
Name: "_result",
|
|
|
|
CommandID: cmd.CommandID,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
flvio.AMFMap{
|
|
|
|
{K: "fmsVer", V: "LNX 9,0,124,2"},
|
|
|
|
{K: "capabilities", V: float64(31)},
|
|
|
|
},
|
|
|
|
flvio.AMFMap{
|
|
|
|
{K: "level", V: "status"},
|
|
|
|
{K: "code", V: "NetConnection.Connect.Success"},
|
|
|
|
{K: "description", V: "Connection succeeded."},
|
|
|
|
{K: "objectEncoding", V: oe},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
cmd, err := c.readCommand()
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch cmd.Name {
|
|
|
|
case "createStream":
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: cmd.ChunkStreamID,
|
|
|
|
Name: "_result",
|
|
|
|
CommandID: cmd.CommandID,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
float64(1),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case "play":
|
|
|
|
if len(cmd.Arguments) < 2 {
|
|
|
|
return nil, false, fmt.Errorf("invalid play command arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
actionpath, ok := cmd.Arguments[1].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, false, fmt.Errorf("invalid play command arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := createURL(tcURL, connectpath, actionpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgUserControlStreamIsRecorded{
|
|
|
|
StreamID: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgUserControlStreamBegin{
|
|
|
|
StreamID: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 5,
|
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Name: "onStatus",
|
|
|
|
CommandID: cmd.CommandID,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
flvio.AMFMap{
|
|
|
|
{K: "level", V: "status"},
|
|
|
|
{K: "code", V: "NetStream.Play.Reset"},
|
|
|
|
{K: "description", V: "play reset"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 5,
|
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Name: "onStatus",
|
|
|
|
CommandID: cmd.CommandID,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
flvio.AMFMap{
|
|
|
|
{K: "level", V: "status"},
|
|
|
|
{K: "code", V: "NetStream.Play.Start"},
|
|
|
|
{K: "description", V: "play start"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 5,
|
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Name: "onStatus",
|
|
|
|
CommandID: cmd.CommandID,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
flvio.AMFMap{
|
|
|
|
{K: "level", V: "status"},
|
|
|
|
{K: "code", V: "NetStream.Data.Start"},
|
|
|
|
{K: "description", V: "data start"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 5,
|
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Name: "onStatus",
|
2022-08-16 10:55:54 +00:00
|
|
|
CommandID: cmd.CommandID,
|
2022-07-17 13:17:18 +00:00
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
flvio.AMFMap{
|
|
|
|
{K: "level", V: "status"},
|
|
|
|
{K: "code", V: "NetStream.Play.PublishNotify"},
|
|
|
|
{K: "description", V: "publish notify"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2022-08-16 10:02:25 +00:00
|
|
|
return u, false, nil
|
2022-07-17 13:17:18 +00:00
|
|
|
|
|
|
|
case "publish":
|
|
|
|
if len(cmd.Arguments) < 2 {
|
|
|
|
return nil, false, fmt.Errorf("invalid publish command arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
actionpath, ok := cmd.Arguments[1].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, false, fmt.Errorf("invalid publish command arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := createURL(tcURL, connectpath, actionpath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.mrw.Write(&message.MsgCommandAMF0{
|
|
|
|
ChunkStreamID: 5,
|
|
|
|
Name: "onStatus",
|
|
|
|
CommandID: cmd.CommandID,
|
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Arguments: []interface{}{
|
|
|
|
nil,
|
|
|
|
flvio.AMFMap{
|
|
|
|
{K: "level", V: "status"},
|
|
|
|
{K: "code", V: "NetStream.Publish.Start"},
|
|
|
|
{K: "description", V: "publish start"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2022-08-16 10:02:25 +00:00
|
|
|
return u, true, nil
|
2022-07-17 13:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadMessage reads a message.
|
|
|
|
func (c *Conn) ReadMessage() (message.Message, error) {
|
|
|
|
return c.mrw.Read()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMessage writes a message.
|
|
|
|
func (c *Conn) WriteMessage(msg message.Message) error {
|
|
|
|
return c.mrw.Write(msg)
|
2021-03-10 14:06:45 +00:00
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
|
2022-02-12 16:48:55 +00:00
|
|
|
func trackFromH264DecoderConfig(data []byte) (*gortsplib.TrackH264, error) {
|
2022-07-17 13:17:18 +00:00
|
|
|
var conf h264conf.Conf
|
|
|
|
err := conf.Unmarshal(data)
|
2022-02-12 16:48:55 +00:00
|
|
|
if err != nil {
|
2022-07-17 13:17:18 +00:00
|
|
|
return nil, fmt.Errorf("unable to parse H264 config: %v", err)
|
2022-02-12 16:48:55 +00:00
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
|
2022-06-23 11:54:48 +00:00
|
|
|
return &gortsplib.TrackH264{
|
|
|
|
PayloadType: 96,
|
2022-07-17 13:17:18 +00:00
|
|
|
SPS: conf.SPS,
|
|
|
|
PPS: conf.PPS,
|
2022-06-23 11:54:48 +00:00
|
|
|
}, nil
|
2022-02-12 16:48:55 +00:00
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
|
2022-08-05 21:50:45 +00:00
|
|
|
func trackFromAACDecoderConfig(data []byte) (*gortsplib.TrackMPEG4Audio, error) {
|
|
|
|
var mpegConf mpeg4audio.Config
|
2022-06-24 15:00:28 +00:00
|
|
|
err := mpegConf.Unmarshal(data)
|
2022-06-07 21:13:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:50:45 +00:00
|
|
|
return &gortsplib.TrackMPEG4Audio{
|
2022-07-17 07:54:16 +00:00
|
|
|
PayloadType: 96,
|
2022-06-24 15:00:28 +00:00
|
|
|
Config: &mpegConf,
|
|
|
|
SizeLength: 13,
|
|
|
|
IndexLength: 3,
|
|
|
|
IndexDeltaLength: 3,
|
2022-06-23 11:54:48 +00:00
|
|
|
}, nil
|
2022-06-07 21:13:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 16:48:55 +00:00
|
|
|
var errEmptyMetadata = errors.New("metadata is empty")
|
2021-12-22 16:37:15 +00:00
|
|
|
|
2022-08-05 21:50:45 +00:00
|
|
|
func (c *Conn) readTracksFromMetadata(payload []interface{}) (*gortsplib.TrackH264, *gortsplib.TrackMPEG4Audio, error) {
|
2022-07-17 13:17:18 +00:00
|
|
|
if len(payload) != 1 {
|
2022-02-12 16:48:55 +00:00
|
|
|
return nil, nil, fmt.Errorf("invalid metadata")
|
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
md, ok := payload[0].(flvio.AMFMap)
|
2022-02-12 16:48:55 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil, fmt.Errorf("invalid metadata")
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:37:15 +00:00
|
|
|
hasVideo, err := func() (bool, error) {
|
|
|
|
v, ok := md.GetV("videocodecid")
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch vt := v.(type) {
|
|
|
|
case float64:
|
|
|
|
switch vt {
|
|
|
|
case 0:
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
case codecH264:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if vt == "avc1" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("unsupported video codec %v", v)
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hasAudio, err := func() (bool, error) {
|
|
|
|
v, ok := md.GetV("audiocodecid")
|
|
|
|
if !ok {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch vt := v.(type) {
|
|
|
|
case float64:
|
|
|
|
switch vt {
|
|
|
|
case 0:
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
case codecAAC:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case string:
|
|
|
|
if vt == "mp4a" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("unsupported audio codec %v", v)
|
|
|
|
}()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasVideo && !hasAudio {
|
2022-02-12 16:48:55 +00:00
|
|
|
return nil, nil, errEmptyMetadata
|
2021-12-22 16:37:15 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 16:48:55 +00:00
|
|
|
var videoTrack *gortsplib.TrackH264
|
2022-08-05 21:50:45 +00:00
|
|
|
var audioTrack *gortsplib.TrackMPEG4Audio
|
2022-02-12 16:48:55 +00:00
|
|
|
|
2021-12-22 16:37:15 +00:00
|
|
|
for {
|
2022-07-17 13:17:18 +00:00
|
|
|
msg, err := c.ReadMessage()
|
2021-12-22 16:37:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
switch tmsg := msg.(type) {
|
|
|
|
case *message.MsgVideo:
|
|
|
|
if tmsg.H264Type == flvio.AVC_SEQHDR {
|
|
|
|
if !hasVideo {
|
|
|
|
return nil, nil, fmt.Errorf("unexpected video packet")
|
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
if videoTrack != nil {
|
|
|
|
return nil, nil, fmt.Errorf("video track setupped twice")
|
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
videoTrack, err = trackFromH264DecoderConfig(tmsg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
case *message.MsgAudio:
|
|
|
|
if tmsg.AACType == flvio.AVC_SEQHDR {
|
|
|
|
if !hasAudio {
|
|
|
|
return nil, nil, fmt.Errorf("unexpected audio packet")
|
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
if audioTrack != nil {
|
|
|
|
return nil, nil, fmt.Errorf("audio track setupped twice")
|
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
audioTrack, err = trackFromAACDecoderConfig(tmsg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-12-22 16:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasVideo || videoTrack != nil) &&
|
|
|
|
(!hasAudio || audioTrack != nil) {
|
|
|
|
return videoTrack, audioTrack, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:50:45 +00:00
|
|
|
func (c *Conn) readTracksFromMessages(msg message.Message) (*gortsplib.TrackH264, *gortsplib.TrackMPEG4Audio, error) {
|
2022-07-17 13:17:18 +00:00
|
|
|
var startTime *time.Duration
|
2022-06-07 21:13:49 +00:00
|
|
|
var videoTrack *gortsplib.TrackH264
|
2022-08-05 21:50:45 +00:00
|
|
|
var audioTrack *gortsplib.TrackMPEG4Audio
|
2022-06-07 21:13:49 +00:00
|
|
|
|
|
|
|
// analyze 1 second of packets
|
2022-07-17 13:17:18 +00:00
|
|
|
outer:
|
2022-06-07 21:13:49 +00:00
|
|
|
for {
|
2022-07-17 13:17:18 +00:00
|
|
|
switch tmsg := msg.(type) {
|
|
|
|
case *message.MsgVideo:
|
|
|
|
if startTime == nil {
|
|
|
|
v := tmsg.DTS
|
|
|
|
startTime = &v
|
|
|
|
}
|
2022-06-07 21:13:49 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
if tmsg.H264Type == flvio.AVC_SEQHDR {
|
|
|
|
if videoTrack == nil {
|
|
|
|
var err error
|
|
|
|
videoTrack, err = trackFromH264DecoderConfig(tmsg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop the analysis if both tracks are found
|
|
|
|
if videoTrack != nil && audioTrack != nil {
|
|
|
|
return videoTrack, audioTrack, nil
|
|
|
|
}
|
2022-06-07 21:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
if (tmsg.DTS - *startTime) >= 1*time.Second {
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
|
|
|
case *message.MsgAudio:
|
|
|
|
if startTime == nil {
|
|
|
|
v := tmsg.DTS
|
|
|
|
startTime = &v
|
|
|
|
}
|
2022-06-07 21:13:49 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
if tmsg.AACType == flvio.AVC_SEQHDR {
|
|
|
|
if audioTrack == nil {
|
|
|
|
var err error
|
|
|
|
audioTrack, err = trackFromAACDecoderConfig(tmsg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop the analysis if both tracks are found
|
|
|
|
if videoTrack != nil && audioTrack != nil {
|
|
|
|
return videoTrack, audioTrack, nil
|
|
|
|
}
|
2022-06-07 21:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
if (tmsg.DTS - *startTime) >= 1*time.Second {
|
|
|
|
break outer
|
|
|
|
}
|
2022-06-07 21:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2022-07-17 13:17:18 +00:00
|
|
|
msg, err = c.ReadMessage()
|
2022-06-07 21:13:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if videoTrack == nil && audioTrack == nil {
|
|
|
|
return nil, nil, fmt.Errorf("no tracks found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return videoTrack, audioTrack, nil
|
|
|
|
}
|
|
|
|
|
2022-02-12 16:48:55 +00:00
|
|
|
// ReadTracks reads track informations.
|
2022-08-05 21:50:45 +00:00
|
|
|
func (c *Conn) ReadTracks() (*gortsplib.TrackH264, *gortsplib.TrackMPEG4Audio, error) {
|
2022-08-16 10:53:41 +00:00
|
|
|
msg, err := func() (message.Message, error) {
|
|
|
|
for {
|
|
|
|
msg, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip play start and data start
|
|
|
|
if cmd, ok := msg.(*message.MsgCommandAMF0); ok && cmd.Name == "onStatus" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip RtmpSampleAccess
|
|
|
|
if data, ok := msg.(*message.MsgDataAMF0); ok && len(data.Payload) >= 1 {
|
|
|
|
if s, ok := data.Payload[0].(string); ok && s == "|RtmpSampleAccess" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
}()
|
2022-02-12 16:48:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
if data, ok := msg.(*message.MsgDataAMF0); ok && len(data.Payload) >= 1 {
|
|
|
|
payload := data.Payload
|
|
|
|
|
|
|
|
if s, ok := payload[0].(string); ok && s == "@setDataFrame" {
|
|
|
|
payload = payload[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(payload) >= 1 {
|
|
|
|
if s, ok := payload[0].(string); ok && s == "onMetaData" {
|
|
|
|
videoTrack, audioTrack, err := c.readTracksFromMetadata(payload[1:])
|
2022-02-12 16:48:55 +00:00
|
|
|
if err != nil {
|
2022-07-17 13:17:18 +00:00
|
|
|
if err == errEmptyMetadata {
|
|
|
|
msg, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.readTracksFromMessages(msg)
|
|
|
|
}
|
|
|
|
|
2022-02-12 16:48:55 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-07 21:13:49 +00:00
|
|
|
return videoTrack, audioTrack, nil
|
2022-02-12 16:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 21:13:49 +00:00
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
return c.readTracksFromMessages(msg)
|
2022-02-12 16:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTracks writes track informations.
|
2022-08-05 21:50:45 +00:00
|
|
|
func (c *Conn) WriteTracks(videoTrack *gortsplib.TrackH264, audioTrack *gortsplib.TrackMPEG4Audio) error {
|
2022-07-17 13:17:18 +00:00
|
|
|
err := c.WriteMessage(&message.MsgDataAMF0{
|
|
|
|
ChunkStreamID: 4,
|
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Payload: []interface{}{
|
|
|
|
"@setDataFrame",
|
|
|
|
"onMetaData",
|
|
|
|
flvio.AMFMap{
|
|
|
|
{
|
|
|
|
K: "videodatarate",
|
|
|
|
V: float64(0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
K: "videocodecid",
|
|
|
|
V: func() float64 {
|
|
|
|
if videoTrack != nil {
|
|
|
|
return codecH264
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
K: "audiodatarate",
|
|
|
|
V: float64(0),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
K: "audiocodecid",
|
|
|
|
V: func() float64 {
|
|
|
|
if audioTrack != nil {
|
|
|
|
return codecAAC
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}(),
|
|
|
|
},
|
2021-12-22 16:37:15 +00:00
|
|
|
},
|
2022-07-17 13:17:18 +00:00
|
|
|
},
|
2021-12-22 16:37:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-06 15:27:36 +00:00
|
|
|
// write decoder config only if SPS and PPS are available.
|
2022-07-17 13:17:18 +00:00
|
|
|
// if they're not available yet, they're sent later.
|
2022-06-23 11:54:48 +00:00
|
|
|
if videoTrack != nil && videoTrack.SafeSPS() != nil && videoTrack.SafePPS() != nil {
|
2022-07-17 13:17:18 +00:00
|
|
|
buf, _ := h264conf.Conf{
|
|
|
|
SPS: videoTrack.SafeSPS(),
|
|
|
|
PPS: videoTrack.SafePPS(),
|
|
|
|
}.Marshal()
|
|
|
|
|
|
|
|
err = c.WriteMessage(&message.MsgVideo{
|
2022-08-16 15:08:55 +00:00
|
|
|
ChunkStreamID: message.MsgVideoChunkStreamID,
|
2022-07-17 13:17:18 +00:00
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
IsKeyFrame: true,
|
|
|
|
H264Type: flvio.AVC_SEQHDR,
|
|
|
|
Payload: buf,
|
2021-12-22 16:37:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if audioTrack != nil {
|
2022-06-24 15:00:28 +00:00
|
|
|
enc, err := audioTrack.Config.Marshal()
|
2021-12-22 16:37:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-17 13:17:18 +00:00
|
|
|
err = c.WriteMessage(&message.MsgAudio{
|
2022-08-16 15:08:55 +00:00
|
|
|
ChunkStreamID: message.MsgAudioChunkStreamID,
|
2022-07-17 13:17:18 +00:00
|
|
|
MessageStreamID: 0x1000000,
|
|
|
|
Rate: flvio.SOUND_44Khz,
|
|
|
|
Depth: flvio.SOUND_16BIT,
|
|
|
|
Channels: flvio.SOUND_STEREO,
|
|
|
|
AACType: flvio.AAC_SEQHDR,
|
|
|
|
Payload: enc,
|
2021-12-22 16:37:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|