mediamtx/internal/rtmp/conn_test.go

1228 lines
27 KiB
Go
Raw Normal View History

2022-02-12 15:21:16 +00:00
package rtmp
import (
"bytes"
2022-02-12 15:21:16 +00:00
"net"
"net/url"
"testing"
"github.com/aler9/gortsplib"
2022-08-05 21:50:45 +00:00
"github.com/aler9/gortsplib/pkg/mpeg4audio"
2022-02-12 15:21:16 +00:00
"github.com/notedit/rtmp/format/flv/flvio"
"github.com/stretchr/testify/require"
2022-06-08 18:47:36 +00:00
"github.com/aler9/rtsp-simple-server/internal/rtmp/bytecounter"
"github.com/aler9/rtsp-simple-server/internal/rtmp/h264conf"
2022-06-04 23:06:40 +00:00
"github.com/aler9/rtsp-simple-server/internal/rtmp/handshake"
"github.com/aler9/rtsp-simple-server/internal/rtmp/message"
2022-02-12 15:21:16 +00:00
)
func TestInitializeClient(t *testing.T) {
2022-07-14 19:48:53 +00:00
for _, ca := range []string{"read", "publish"} {
t.Run(ca, func(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:9121")
require.NoError(t, err)
defer ln.Close()
done := make(chan struct{})
go func() {
conn, err := ln.Accept()
require.NoError(t, err)
defer conn.Close()
bc := bytecounter.NewReadWriter(conn)
err = handshake.DoServer(bc, true)
2022-07-14 19:48:53 +00:00
require.NoError(t, err)
mrw := message.NewReadWriter(bc, true)
2022-07-14 19:48:53 +00:00
msg, err := mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgSetWindowAckSize{
Value: 2500000,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgSetPeerBandwidth{
Value: 2500000,
2022-07-14 19:48:53 +00:00
Type: 2,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgSetChunkSize{
Value: 65536,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "connect",
CommandID: 1,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
flvio.AMFMap{
{K: "app", V: "stream"},
{K: "flashVer", V: "LNX 9,0,124,2"},
{K: "tcUrl", V: "rtmp://127.0.0.1:9121/stream"},
2022-07-14 19:48:53 +00:00
{K: "fpad", V: false},
{K: "capabilities", V: float64(15)},
{K: "audioCodecs", V: float64(4071)},
{K: "videoCodecs", V: float64(252)},
{K: "videoFunction", V: float64(1)},
},
},
}, msg)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 1,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
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: float64(0)},
},
},
})
require.NoError(t, err)
if ca == "read" {
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "createStream",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
},
}, msg)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
float64(1),
},
})
require.NoError(t, err)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgUserControlSetBufferLength{
BufferLength: 0x64,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 4,
MessageStreamID: 0x1000000,
Name: "play",
2022-08-22 08:57:29 +00:00
CommandID: 3,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
},
}, msg)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: 3,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
flvio.AMFMap{
{K: "level", V: "status"},
{K: "code", V: "NetStream.Play.Reset"},
{K: "description", V: "play reset"},
},
},
})
require.NoError(t, err)
} else {
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "releaseStream",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
},
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "FCPublish",
CommandID: 3,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
},
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "createStream",
CommandID: 4,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
},
}, msg)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 4,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
float64(1),
},
})
require.NoError(t, err)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 4,
MessageStreamID: 0x1000000,
Name: "publish",
CommandID: 5,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
"stream",
},
}, msg)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: 5,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
flvio.AMFMap{
{K: "level", V: "status"},
{K: "code", V: "NetStream.Publish.Start"},
{K: "description", V: "publish start"},
},
},
})
require.NoError(t, err)
}
close(done)
}()
u, err := url.Parse("rtmp://127.0.0.1:9121/stream")
require.NoError(t, err)
nconn, err := net.Dial("tcp", u.Host)
require.NoError(t, err)
defer nconn.Close()
conn := NewConn(nconn)
2022-07-14 19:48:53 +00:00
err = conn.InitializeClient(u, ca == "publish")
2022-07-14 19:48:53 +00:00
require.NoError(t, err)
<-done
2022-07-09 14:19:49 +00:00
})
2022-07-14 19:48:53 +00:00
}
2022-07-09 14:19:49 +00:00
}
func TestInitializeServer(t *testing.T) {
2022-07-14 19:48:53 +00:00
for _, ca := range []string{"read", "publish"} {
t.Run(ca, func(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:9121")
require.NoError(t, err)
defer ln.Close()
done := make(chan struct{})
go func() {
nconn, err := ln.Accept()
require.NoError(t, err)
defer nconn.Close()
conn := NewConn(nconn)
u, isPublishing, err := conn.InitializeServer()
2022-07-14 19:48:53 +00:00
require.NoError(t, err)
require.Equal(t, &url.URL{
Scheme: "rtmp",
Host: "127.0.0.1:9121",
Path: "//stream/",
}, u)
require.Equal(t, ca == "publish", isPublishing)
2022-07-14 19:48:53 +00:00
close(done)
}()
conn, err := net.Dial("tcp", "127.0.0.1:9121")
require.NoError(t, err)
defer conn.Close()
bc := bytecounter.NewReadWriter(conn)
err = handshake.DoClient(bc, true)
2022-07-14 19:48:53 +00:00
require.NoError(t, err)
mrw := message.NewReadWriter(bc, true)
2022-07-14 19:48:53 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "connect",
CommandID: 1,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
flvio.AMFMap{
{K: "app", V: "/stream"},
{K: "flashVer", V: "LNX 9,0,124,2"},
{K: "tcUrl", V: "rtmp://127.0.0.1:9121/stream"},
2022-07-14 19:48:53 +00:00
{K: "fpad", V: false},
{K: "capabilities", V: 15},
{K: "audioCodecs", V: 4071},
{K: "videoCodecs", V: 252},
{K: "videoFunction", V: 1},
},
},
})
require.NoError(t, err)
msg, err := mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgSetWindowAckSize{
Value: 2500000,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgSetPeerBandwidth{
Value: 2500000,
Type: 2,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgSetChunkSize{
Value: 65536,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 1,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
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: float64(0)},
},
},
}, msg)
err = mrw.Write(&message.MsgSetChunkSize{
Value: 65536,
})
require.NoError(t, err)
if ca == "read" {
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "createStream",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
},
})
require.NoError(t, err)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
float64(1),
},
}, msg)
err = mrw.Write(&message.MsgUserControlSetBufferLength{
BufferLength: 0x64,
})
require.NoError(t, err)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 4,
MessageStreamID: 0x1000000,
Name: "play",
CommandID: 0,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
},
})
require.NoError(t, err)
} else {
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "releaseStream",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
},
})
require.NoError(t, err)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "FCPublish",
CommandID: 3,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
},
})
require.NoError(t, err)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "createStream",
CommandID: 4,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
},
})
require.NoError(t, err)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 4,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
float64(1),
},
}, msg)
err = mrw.Write(&message.MsgCommandAMF0{
ChunkStreamID: 4,
MessageStreamID: 0x1000000,
Name: "publish",
CommandID: 5,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
"stream",
},
})
require.NoError(t, err)
}
<-done
})
}
2022-07-09 14:19:49 +00:00
}
func TestReadTracks(t *testing.T) {
2022-04-18 15:04:59 +00:00
sps := []byte{
0x67, 0x64, 0x00, 0x0c, 0xac, 0x3b, 0x50, 0xb0,
0x4b, 0x42, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00,
0x00, 0x03, 0x00, 0x3d, 0x08,
}
pps := []byte{
0x68, 0xee, 0x3c, 0x80,
}
for _, ca := range []string{
"video+audio",
"video",
2022-04-18 15:04:59 +00:00
"metadata without codec id",
"missing metadata",
} {
t.Run(ca, func(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:9121")
require.NoError(t, err)
defer ln.Close()
done := make(chan struct{})
go func() {
conn, err := ln.Accept()
require.NoError(t, err)
defer conn.Close()
rconn := NewConn(conn)
_, _, err = rconn.InitializeServer()
require.NoError(t, err)
videoTrack, audioTrack, err := rconn.ReadTracks()
require.NoError(t, err)
switch ca {
case "video+audio":
2022-06-23 11:54:48 +00:00
require.Equal(t, &gortsplib.TrackH264{
PayloadType: 96,
SPS: sps,
PPS: pps,
}, videoTrack)
2022-08-05 21:50:45 +00:00
require.Equal(t, &gortsplib.TrackMPEG4Audio{
2022-07-17 10:02:17 +00:00
PayloadType: 96,
2022-08-05 21:50:45 +00:00
Config: &mpeg4audio.Config{
2022-06-24 15:00:28 +00:00
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
},
2022-06-23 11:54:48 +00:00
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}, audioTrack)
case "video":
require.Equal(t, &gortsplib.TrackH264{
PayloadType: 96,
SPS: sps,
PPS: pps,
}, videoTrack)
require.Nil(t, audioTrack)
2022-04-18 15:04:59 +00:00
case "metadata without codec id":
2022-06-23 11:54:48 +00:00
require.Equal(t, &gortsplib.TrackH264{
PayloadType: 96,
SPS: sps,
PPS: pps,
}, videoTrack)
2022-08-05 21:50:45 +00:00
require.Equal(t, &gortsplib.TrackMPEG4Audio{
2022-07-17 10:02:17 +00:00
PayloadType: 96,
2022-08-05 21:50:45 +00:00
Config: &mpeg4audio.Config{
2022-06-24 15:00:28 +00:00
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
},
2022-06-23 11:54:48 +00:00
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}, audioTrack)
case "missing metadata":
2022-06-23 11:54:48 +00:00
require.Equal(t, &gortsplib.TrackH264{
PayloadType: 96,
SPS: sps,
PPS: pps,
}, videoTrack)
2022-08-05 21:50:45 +00:00
require.Equal(t, &gortsplib.TrackMPEG4Audio{
2022-07-17 10:02:17 +00:00
PayloadType: 96,
2022-08-05 21:50:45 +00:00
Config: &mpeg4audio.Config{
2022-06-24 15:00:28 +00:00
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
},
2022-06-23 11:54:48 +00:00
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}, audioTrack)
}
close(done)
}()
conn, err := net.Dial("tcp", "127.0.0.1:9121")
require.NoError(t, err)
defer conn.Close()
2022-06-08 18:47:36 +00:00
bc := bytecounter.NewReadWriter(conn)
err = handshake.DoClient(bc, true)
require.NoError(t, err)
mrw := message.NewReadWriter(bc, true)
2022-05-13 18:53:52 +00:00
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-05-13 17:04:47 +00:00
ChunkStreamID: 3,
Name: "connect",
CommandID: 1,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
flvio.AMFMap{
{K: "app", V: "/stream"},
{K: "flashVer", V: "LNX 9,0,124,2"},
{K: "tcUrl", V: "rtmp://127.0.0.1:9121/stream"},
2022-06-04 23:06:40 +00:00
{K: "fpad", V: false},
{K: "capabilities", V: 15},
{K: "audioCodecs", V: 4071},
{K: "videoCodecs", V: 252},
{K: "videoFunction", V: 1},
},
},
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
msg, err := mrw.Read()
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgSetWindowAckSize{
Value: 2500000,
2022-05-16 09:57:29 +00:00
}, msg)
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgSetPeerBandwidth{
Value: 2500000,
Type: 2,
2022-05-16 09:57:29 +00:00
}, msg)
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgSetChunkSize{
Value: 65536,
2022-05-16 09:57:29 +00:00
}, msg)
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 1,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
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: float64(0)},
},
},
2022-06-04 23:06:40 +00:00
}, msg)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgSetChunkSize{
2022-06-04 23:06:40 +00:00
Value: 65536,
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-05-13 17:04:47 +00:00
ChunkStreamID: 3,
Name: "releaseStream",
CommandID: 2,
Arguments: []interface{}{
nil,
"",
2022-06-04 23:06:40 +00:00
},
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-05-13 17:04:47 +00:00
ChunkStreamID: 3,
Name: "FCPublish",
CommandID: 3,
Arguments: []interface{}{
nil,
"",
2022-06-04 23:06:40 +00:00
},
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-05-13 17:04:47 +00:00
ChunkStreamID: 3,
Name: "createStream",
CommandID: 4,
Arguments: []interface{}{
nil,
2022-06-04 23:06:40 +00:00
},
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 4,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
float64(1),
},
}, msg)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-05-13 18:53:52 +00:00
ChunkStreamID: 8,
MessageStreamID: 1,
Name: "publish",
CommandID: 5,
Arguments: []interface{}{
2022-05-13 18:53:52 +00:00
nil,
"",
"live",
2022-06-04 23:06:40 +00:00
},
})
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: 5,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
flvio.AMFMap{
{K: "level", V: "status"},
{K: "code", V: "NetStream.Publish.Start"},
{K: "description", V: "publish start"},
},
},
2022-06-04 23:06:40 +00:00
}, msg)
switch ca {
case "video+audio":
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgDataAMF0{
2022-05-13 18:53:52 +00:00
ChunkStreamID: 4,
MessageStreamID: 1,
2022-06-04 23:06:40 +00:00
Payload: []interface{}{
"@setDataFrame",
"onMetaData",
flvio.AMFMap{
{
K: "videodatarate",
V: float64(0),
},
{
K: "videocodecid",
V: float64(codecH264),
},
{
K: "audiodatarate",
V: float64(0),
},
{
K: "audiocodecid",
V: float64(codecAAC),
},
},
},
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
buf, _ := h264conf.Conf{
SPS: sps,
PPS: pps,
}.Marshal()
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
2022-06-07 20:48:10 +00:00
IsKeyFrame: true,
H264Type: flvio.AVC_SEQHDR,
Payload: buf,
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
2022-08-05 21:50:45 +00:00
enc, err := mpeg4audio.Config{
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
2022-06-24 15:00:28 +00:00
}.Marshal()
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
2022-06-07 20:48:10 +00:00
Rate: flvio.SOUND_44Khz,
Depth: flvio.SOUND_16BIT,
Channels: flvio.SOUND_STEREO,
AACType: flvio.AAC_SEQHDR,
Payload: enc,
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
case "video":
err = mrw.Write(&message.MsgDataAMF0{
ChunkStreamID: 4,
MessageStreamID: 1,
Payload: []interface{}{
"@setDataFrame",
"onMetaData",
flvio.AMFMap{
{
K: "videodatarate",
V: float64(0),
},
{
K: "videocodecid",
V: float64(codecH264),
},
{
K: "audiodatarate",
V: float64(0),
},
{
K: "audiocodecid",
V: float64(0),
},
},
},
})
require.NoError(t, err)
buf, _ := h264conf.Conf{
SPS: sps,
PPS: pps,
}.Marshal()
err = mrw.Write(&message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
IsKeyFrame: true,
H264Type: flvio.AVC_SEQHDR,
Payload: buf,
})
require.NoError(t, err)
2022-04-18 15:04:59 +00:00
case "metadata without codec id":
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgDataAMF0{
2022-05-13 18:53:52 +00:00
ChunkStreamID: 4,
MessageStreamID: 1,
2022-06-04 23:06:40 +00:00
Payload: []interface{}{
"@setDataFrame",
"onMetaData",
flvio.AMFMap{
{
K: "width",
V: float64(2688),
},
{
K: "height",
V: float64(1520),
},
{
K: "framerate",
V: float64(0o25),
},
},
},
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
buf, _ := h264conf.Conf{
SPS: sps,
PPS: pps,
}.Marshal()
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
2022-06-07 20:48:10 +00:00
IsKeyFrame: true,
H264Type: flvio.AVC_SEQHDR,
Payload: buf,
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
2022-08-05 21:50:45 +00:00
enc, err := mpeg4audio.Config{
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
2022-06-24 15:00:28 +00:00
}.Marshal()
require.NoError(t, err)
err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
Rate: flvio.SOUND_44Khz,
Depth: flvio.SOUND_16BIT,
Channels: flvio.SOUND_STEREO,
AACType: flvio.AAC_SEQHDR,
Payload: enc,
})
require.NoError(t, err)
case "missing metadata":
buf, _ := h264conf.Conf{
SPS: sps,
PPS: pps,
}.Marshal()
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
2022-06-07 20:48:10 +00:00
IsKeyFrame: true,
H264Type: flvio.AVC_SEQHDR,
Payload: buf,
2022-05-13 18:53:52 +00:00
})
require.NoError(t, err)
2022-08-05 21:50:45 +00:00
enc, err := mpeg4audio.Config{
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
2022-06-24 15:00:28 +00:00
}.Marshal()
require.NoError(t, err)
err = mrw.Write(&message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
Rate: flvio.SOUND_44Khz,
Depth: flvio.SOUND_16BIT,
Channels: flvio.SOUND_STEREO,
AACType: flvio.AAC_SEQHDR,
Payload: enc,
})
require.NoError(t, err)
}
<-done
})
2022-02-12 15:21:16 +00:00
}
}
func TestWriteTracks(t *testing.T) {
2022-02-12 15:21:16 +00:00
ln, err := net.Listen("tcp", "127.0.0.1:9121")
require.NoError(t, err)
defer ln.Close()
go func() {
conn, err := ln.Accept()
require.NoError(t, err)
defer conn.Close()
rconn := NewConn(conn)
_, _, err = rconn.InitializeServer()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-23 11:54:48 +00:00
videoTrack := &gortsplib.TrackH264{
PayloadType: 96,
SPS: []byte{
2022-02-12 15:21:16 +00:00
0x67, 0x64, 0x00, 0x0c, 0xac, 0x3b, 0x50, 0xb0,
0x4b, 0x42, 0x00, 0x00, 0x03, 0x00, 0x02, 0x00,
0x00, 0x03, 0x00, 0x3d, 0x08,
},
2022-06-23 11:54:48 +00:00
PPS: []byte{
2022-02-12 15:21:16 +00:00
0x68, 0xee, 0x3c, 0x80,
},
2022-06-23 11:54:48 +00:00
}
2022-08-05 21:50:45 +00:00
audioTrack := &gortsplib.TrackMPEG4Audio{
2022-07-17 10:02:17 +00:00
PayloadType: 96,
2022-08-05 21:50:45 +00:00
Config: &mpeg4audio.Config{
2022-06-24 15:00:28 +00:00
Type: 2,
SampleRate: 44100,
ChannelCount: 2,
},
2022-06-23 11:54:48 +00:00
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}
2022-02-12 15:21:16 +00:00
err = rconn.WriteTracks(videoTrack, audioTrack)
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
}()
conn, err := net.Dial("tcp", "127.0.0.1:9121")
require.NoError(t, err)
defer conn.Close()
2022-06-08 18:47:36 +00:00
bc := bytecounter.NewReadWriter(conn)
2022-02-12 15:21:16 +00:00
err = handshake.DoClient(bc, true)
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
mrw := message.NewReadWriter(bc, true)
2022-05-13 18:53:52 +00:00
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-05-13 17:04:47 +00:00
ChunkStreamID: 3,
Name: "connect",
CommandID: 1,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
flvio.AMFMap{
{K: "app", V: "/stream"},
{K: "flashVer", V: "LNX 9,0,124,2"},
{K: "tcUrl", V: "rtmp://127.0.0.1:9121/stream"},
2022-06-04 23:06:40 +00:00
{K: "fpad", V: false},
{K: "capabilities", V: 15},
{K: "audioCodecs", V: 4071},
{K: "videoCodecs", V: 252},
{K: "videoFunction", V: 1},
},
},
2022-05-13 18:53:52 +00:00
})
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
msg, err := mrw.Read()
2022-05-13 17:04:47 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgSetWindowAckSize{
Value: 2500000,
2022-05-16 09:57:29 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgSetPeerBandwidth{
Value: 2500000,
Type: 2,
2022-05-16 09:57:29 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgSetChunkSize{
Value: 65536,
2022-05-16 09:57:29 +00:00
}, msg)
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 1,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
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: float64(0)},
},
2022-02-12 15:21:16 +00:00
},
2022-06-04 23:06:40 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgSetWindowAckSize{
2022-06-04 23:06:40 +00:00
Value: 2500000,
2022-05-13 18:53:52 +00:00
})
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgSetChunkSize{
2022-06-04 23:06:40 +00:00
Value: 65536,
2022-05-13 18:53:52 +00:00
})
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-05-13 17:04:47 +00:00
ChunkStreamID: 3,
Name: "createStream",
CommandID: 2,
Arguments: []interface{}{
2022-02-12 15:21:16 +00:00
nil,
2022-06-04 23:06:40 +00:00
},
2022-05-13 18:53:52 +00:00
})
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 3,
Name: "_result",
CommandID: 2,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
float64(1),
},
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-05-13 17:04:47 +00:00
ChunkStreamID: 8,
Name: "getStreamLength",
CommandID: 3,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
"",
},
2022-05-13 18:53:52 +00:00
})
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
err = mrw.Write(&message.MsgCommandAMF0{
2022-07-09 14:19:49 +00:00
ChunkStreamID: 8,
MessageStreamID: 0x1000000,
Name: "play",
CommandID: 4,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
"",
float64(-2000),
},
2022-05-13 18:53:52 +00:00
})
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-05 20:44:55 +00:00
require.Equal(t, &message.MsgUserControlStreamIsRecorded{
StreamID: 1,
2022-05-16 09:57:29 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-05 20:44:55 +00:00
require.Equal(t, &message.MsgUserControlStreamBegin{
StreamID: 1,
2022-05-16 09:57:29 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: 4,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
flvio.AMFMap{
{K: "level", V: "status"},
{K: "code", V: "NetStream.Play.Reset"},
{K: "description", V: "play reset"},
},
2022-02-12 15:21:16 +00:00
},
2022-06-04 23:06:40 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: 4,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
flvio.AMFMap{
{K: "level", V: "status"},
{K: "code", V: "NetStream.Play.Start"},
{K: "description", V: "play start"},
},
2022-02-12 15:21:16 +00:00
},
2022-06-04 23:06:40 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: 4,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
flvio.AMFMap{
{K: "level", V: "status"},
{K: "code", V: "NetStream.Data.Start"},
{K: "description", V: "data start"},
},
2022-02-12 15:21:16 +00:00
},
2022-06-04 23:06:40 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgCommandAMF0{
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: 4,
Arguments: []interface{}{
2022-06-04 23:06:40 +00:00
nil,
flvio.AMFMap{
{K: "level", V: "status"},
{K: "code", V: "NetStream.Play.PublishNotify"},
{K: "description", V: "publish notify"},
},
2022-02-12 15:21:16 +00:00
},
2022-06-04 23:06:40 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgDataAMF0{
ChunkStreamID: 4,
MessageStreamID: 0x1000000,
2022-06-04 23:06:40 +00:00
Payload: []interface{}{
"@setDataFrame",
2022-06-04 23:06:40 +00:00
"onMetaData",
flvio.AMFMap{
{K: "videodatarate", V: float64(0)},
{K: "videocodecid", V: float64(7)},
{K: "audiodatarate", V: float64(0)},
{K: "audiocodecid", V: float64(10)},
},
2022-02-12 15:21:16 +00:00
},
2022-06-04 23:06:40 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgVideo{
ChunkStreamID: message.MsgVideoChunkStreamID,
MessageStreamID: 0x1000000,
2022-06-07 20:48:10 +00:00
IsKeyFrame: true,
H264Type: flvio.AVC_SEQHDR,
Payload: []byte{
0x1, 0x64, 0x0,
2022-06-04 23:06:40 +00:00
0xc, 0xff, 0xe1, 0x0, 0x15, 0x67, 0x64, 0x0,
0xc, 0xac, 0x3b, 0x50, 0xb0, 0x4b, 0x42, 0x0,
0x0, 0x3, 0x0, 0x2, 0x0, 0x0, 0x3, 0x0,
0x3d, 0x8, 0x1, 0x0, 0x4, 0x68, 0xee, 0x3c,
0x80,
},
}, msg)
2022-02-12 15:21:16 +00:00
2022-06-08 18:47:36 +00:00
msg, err = mrw.Read()
2022-02-12 15:21:16 +00:00
require.NoError(t, err)
2022-06-04 23:06:40 +00:00
require.Equal(t, &message.MsgAudio{
ChunkStreamID: message.MsgAudioChunkStreamID,
MessageStreamID: 0x1000000,
2022-06-07 20:48:10 +00:00
Rate: flvio.SOUND_44Khz,
Depth: flvio.SOUND_16BIT,
Channels: flvio.SOUND_STEREO,
AACType: flvio.AAC_SEQHDR,
Payload: []byte{0x12, 0x10},
2022-06-04 23:06:40 +00:00
}, msg)
2022-02-12 15:21:16 +00:00
}
func BenchmarkRead(b *testing.B) {
var buf bytes.Buffer
for n := 0; n < b.N; n++ {
buf.Write([]byte{
7, 0, 0, 23, 0, 0, 98, 8,
0, 0, 0, 64, 175, 1, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4, 1, 2,
3, 4, 1, 2, 3, 4,
})
}
conn := NewConn(&buf)
for n := 0; n < b.N; n++ {
conn.ReadMessage()
}
}