mediamtx/internal/protocols/rtmp/conn_test.go

509 lines
12 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/stretchr/testify/require"
"github.com/bluenviron/mediamtx/internal/protocols/rtmp/amf0"
"github.com/bluenviron/mediamtx/internal/protocols/rtmp/bytecounter"
"github.com/bluenviron/mediamtx/internal/protocols/rtmp/handshake"
"github.com/bluenviron/mediamtx/internal/protocols/rtmp/message"
2022-02-12 15:21:16 +00:00
)
2023-07-31 17:41:59 +00:00
func TestNewClientConn(t *testing.T) {
for _, ca := range []string{
"read",
"read nginx rtmp",
"publish",
} {
2022-07-14 19:48:53 +00:00
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() {
2024-04-28 16:20:08 +00:00
conn, err2 := ln.Accept()
require.NoError(t, err2)
2022-07-14 19:48:53 +00:00
defer conn.Close()
bc := bytecounter.NewReadWriter(conn)
2024-04-28 16:20:08 +00:00
_, _, err2 = handshake.DoServer(bc, false)
require.NoError(t, err2)
2022-07-14 19:48:53 +00:00
mrw := message.NewReadWriter(bc, bc, true)
2022-07-14 19:48:53 +00:00
2024-04-28 16:20:08 +00:00
msg, err2 := mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.SetWindowAckSize{
2022-07-14 19:48:53 +00:00
Value: 2500000,
}, msg)
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.SetPeerBandwidth{
Value: 2500000,
2022-07-14 19:48:53 +00:00
Type: 2,
}, msg)
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.SetChunkSize{
2022-07-14 19:48:53 +00:00
Value: 65536,
}, msg)
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "connect",
CommandID: 1,
Arguments: []interface{}{
amf0.Object{
{Key: "app", Value: "stream"},
{Key: "flashVer", Value: "LNX 9,0,124,2"},
{Key: "tcUrl", Value: "rtmp://127.0.0.1:9121/stream"},
{Key: "fpad", Value: false},
{Key: "capabilities", Value: float64(15)},
{Key: "audioCodecs", Value: float64(4071)},
{Key: "videoCodecs", Value: float64(252)},
{Key: "videoFunction", Value: float64(1)},
2022-07-14 19:48:53 +00:00
},
},
}, msg)
2024-04-28 16:20:08 +00:00
err2 = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "_result",
CommandID: 1,
Arguments: []interface{}{
amf0.Object{
{Key: "fmsVer", Value: "LNX 9,0,124,2"},
{Key: "capabilities", Value: float64(31)},
2022-07-14 19:48:53 +00:00
},
amf0.Object{
{Key: "level", Value: "status"},
{Key: "code", Value: "NetConnection.Connect.Success"},
{Key: "description", Value: "Connection succeeded."},
{Key: "objectEncoding", Value: float64(0)},
2022-07-14 19:48:53 +00:00
},
},
})
2024-04-28 16:20:08 +00:00
require.NoError(t, err2)
2022-07-14 19:48:53 +00:00
switch ca {
case "read", "read nginx rtmp":
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "createStream",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
},
}, msg)
2024-04-28 16:20:08 +00:00
err2 = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "_result",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
float64(1),
},
})
2024-04-28 16:20:08 +00:00
require.NoError(t, err2)
2022-07-14 19:48:53 +00:00
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.UserControlSetBufferLength{
2022-07-14 19:48:53 +00:00
BufferLength: 0x64,
}, msg)
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.CommandAMF0{
2022-07-14 19:48:53 +00:00
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)
2024-04-28 16:20:08 +00:00
err2 = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: func() int {
if ca == "read nginx rtmp" {
return 0
}
return 3
}(),
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
amf0.Object{
{Key: "level", Value: "status"},
{Key: "code", Value: "NetStream.Play.Reset"},
{Key: "description", Value: "play reset"},
2022-07-14 19:48:53 +00:00
},
},
})
2024-04-28 16:20:08 +00:00
require.NoError(t, err2)
case "publish":
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "releaseStream",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
},
}, msg)
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "FCPublish",
CommandID: 3,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
},
}, msg)
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "createStream",
CommandID: 4,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
},
}, msg)
2024-04-28 16:20:08 +00:00
err2 = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "_result",
CommandID: 4,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
float64(1),
},
})
2024-04-28 16:20:08 +00:00
require.NoError(t, err2)
2022-07-14 19:48:53 +00:00
2024-04-28 16:20:08 +00:00
msg, err2 = mrw.Read()
require.NoError(t, err2)
require.Equal(t, &message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 4,
MessageStreamID: 0x1000000,
Name: "publish",
CommandID: 5,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
"",
"stream",
},
}, msg)
2024-04-28 16:20:08 +00:00
err2 = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 5,
MessageStreamID: 0x1000000,
Name: "onStatus",
CommandID: 5,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
amf0.Object{
{Key: "level", Value: "status"},
{Key: "code", Value: "NetStream.Publish.Start"},
{Key: "description", Value: "publish start"},
2022-07-14 19:48:53 +00:00
},
},
})
2024-04-28 16:20:08 +00:00
require.NoError(t, err2)
2022-07-14 19:48:53 +00:00
}
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()
2023-07-31 17:41:59 +00:00
conn, err := NewClientConn(nconn, u, ca == "publish")
2022-07-14 19:48:53 +00:00
require.NoError(t, err)
switch ca {
case "read", "read nginx rtmp":
require.Equal(t, uint64(3421), conn.BytesReceived())
require.Equal(t, uint64(3409), conn.BytesSent())
case "publish":
require.Equal(t, uint64(3427), conn.BytesReceived())
require.Equal(t, uint64(3466), conn.BytesSent())
}
2022-07-14 19:48:53 +00:00
<-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
}
2023-07-31 17:41:59 +00:00
func TestNewServerConn(t *testing.T) {
for _, ca := range []string{
"read",
"publish",
"publish neko",
} {
2022-07-14 19:48:53 +00:00
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() {
2024-04-28 16:20:08 +00:00
nconn, err2 := ln.Accept()
require.NoError(t, err2)
2022-07-14 19:48:53 +00:00
defer nconn.Close()
2024-04-28 16:20:08 +00:00
_, u, isPublishing, err2 := NewServerConn(nconn)
require.NoError(t, err2)
2023-07-31 17:41:59 +00:00
require.Equal(t, &url.URL{
Scheme: "rtmp",
Host: "127.0.0.1:9121",
Path: "//stream/",
}, u)
require.Equal(t, ca == "publish" || ca == "publish neko", 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, false, false)
2022-07-14 19:48:53 +00:00
require.NoError(t, err)
mrw := message.NewReadWriter(bc, bc, true)
2022-07-14 19:48:53 +00:00
tcURL := "rtmp://127.0.0.1:9121/stream"
if ca == "publish neko" {
tcURL = "'rtmp://127.0.0.1:9121/stream"
}
err = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "connect",
CommandID: 1,
Arguments: []interface{}{
amf0.Object{
{Key: "app", Value: "/stream"},
{Key: "flashVer", Value: "LNX 9,0,124,2"},
{Key: "tcUrl", Value: tcURL},
{Key: "fpad", Value: false},
{Key: "capabilities", Value: float64(15)},
{Key: "audioCodecs", Value: float64(4071)},
{Key: "videoCodecs", Value: float64(252)},
{Key: "videoFunction", Value: float64(1)},
2022-07-14 19:48:53 +00:00
},
},
})
require.NoError(t, err)
msg, err := mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.SetWindowAckSize{
2022-07-14 19:48:53 +00:00
Value: 2500000,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.SetPeerBandwidth{
2022-07-14 19:48:53 +00:00
Value: 2500000,
Type: 2,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.SetChunkSize{
2022-07-14 19:48:53 +00:00
Value: 65536,
}, msg)
msg, err = mrw.Read()
require.NoError(t, err)
require.Equal(t, &message.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "_result",
CommandID: 1,
Arguments: []interface{}{
amf0.Object{
{Key: "fmsVer", Value: "LNX 9,0,124,2"},
{Key: "capabilities", Value: float64(31)},
2022-07-14 19:48:53 +00:00
},
amf0.Object{
{Key: "level", Value: "status"},
{Key: "code", Value: "NetConnection.Connect.Success"},
{Key: "description", Value: "Connection succeeded."},
{Key: "objectEncoding", Value: float64(0)},
2022-07-14 19:48:53 +00:00
},
},
}, msg)
err = mrw.Write(&message.SetChunkSize{
2022-07-14 19:48:53 +00:00
Value: 65536,
})
require.NoError(t, err)
if ca == "read" {
err = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
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.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "_result",
CommandID: 2,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
float64(1),
},
}, msg)
err = mrw.Write(&message.UserControlSetBufferLength{
2022-07-14 19:48:53 +00:00
BufferLength: 0x64,
})
require.NoError(t, err)
err = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
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.CommandAMF0{
2022-07-14 19:48:53 +00:00
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.CommandAMF0{
2022-07-14 19:48:53 +00:00
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.CommandAMF0{
2022-07-14 19:48:53 +00:00
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.CommandAMF0{
2022-07-14 19:48:53 +00:00
ChunkStreamID: 3,
Name: "_result",
CommandID: 4,
Arguments: []interface{}{
2022-07-14 19:48:53 +00:00
nil,
float64(1),
},
}, msg)
err = mrw.Write(&message.CommandAMF0{
2022-07-14 19:48:53 +00:00
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 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,
})
}
2023-07-31 17:41:59 +00:00
conn := newNoHandshakeConn(&buf)
for n := 0; n < b.N; n++ {
2023-08-13 14:38:23 +00:00
conn.Read() //nolint:errcheck
}
}