2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2021-03-19 22:04:30 +00:00
|
|
|
|
|
|
|
import (
|
2022-08-16 11:53:04 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"os"
|
2021-03-19 22:04:30 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/formats"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/url"
|
|
|
|
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
|
2022-12-13 19:54:17 +00:00
|
|
|
"github.com/pion/rtp"
|
2021-03-19 22:04:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-16 11:53:04 +00:00
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/rtmp"
|
2021-03-19 22:04:30 +00:00
|
|
|
)
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func TestRTMPSource(t *testing.T) {
|
2022-08-16 11:53:04 +00:00
|
|
|
for _, ca := range []string{
|
|
|
|
"plain",
|
|
|
|
"tls",
|
2021-03-19 22:04:30 +00:00
|
|
|
} {
|
2022-08-16 11:53:04 +00:00
|
|
|
t.Run(ca, func(t *testing.T) {
|
|
|
|
ln, err := func() (net.Listener, error) {
|
|
|
|
if ca == "plain" {
|
|
|
|
return net.Listen("tcp", "127.0.0.1:1937")
|
|
|
|
}
|
|
|
|
|
|
|
|
serverCertFpath, err := writeTempFile(serverCert)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(serverCertFpath)
|
|
|
|
|
|
|
|
serverKeyFpath, err := writeTempFile(serverKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(serverKeyFpath)
|
|
|
|
|
|
|
|
var cert tls.Certificate
|
|
|
|
cert, err = tls.LoadX509KeyPair(serverCertFpath, serverKeyFpath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return tls.Listen("tcp", "127.0.0.1:1937", &tls.Config{Certificates: []tls.Certificate{cert}})
|
|
|
|
}()
|
2021-09-05 15:35:04 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-16 11:53:04 +00:00
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
connected := make(chan struct{})
|
|
|
|
received := make(chan struct{})
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
nconn, err := ln.Accept()
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer nconn.Close()
|
|
|
|
conn := rtmp.NewConn(nconn)
|
|
|
|
|
|
|
|
_, _, err = conn.InitializeServer()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
videoTrack := &formats.H264{
|
2022-12-13 19:54:17 +00:00
|
|
|
PayloadTyp: 96,
|
2022-08-16 11:53:04 +00:00
|
|
|
SPS: []byte{ // 1920x1080 baseline
|
|
|
|
0x67, 0x42, 0xc0, 0x28, 0xd9, 0x00, 0x78, 0x02,
|
|
|
|
0x27, 0xe5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04,
|
|
|
|
0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c, 0x60, 0xc9, 0x20,
|
|
|
|
},
|
2022-11-15 22:46:40 +00:00
|
|
|
PPS: []byte{0x08, 0x06, 0x07, 0x08},
|
|
|
|
PacketizationMode: 1,
|
2022-08-16 11:53:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
audioTrack := &formats.MPEG4Audio{
|
2022-12-13 19:54:17 +00:00
|
|
|
PayloadTyp: 96,
|
2022-08-16 11:53:04 +00:00
|
|
|
Config: &mpeg4audio.Config{
|
|
|
|
Type: 2,
|
|
|
|
SampleRate: 44100,
|
|
|
|
ChannelCount: 2,
|
|
|
|
},
|
|
|
|
SizeLength: 13,
|
|
|
|
IndexLength: 3,
|
|
|
|
IndexDeltaLength: 3,
|
|
|
|
}
|
|
|
|
|
2023-07-30 21:15:22 +00:00
|
|
|
w, err := rtmp.NewWriter(conn, videoTrack, audioTrack)
|
2022-08-16 11:53:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
<-connected
|
|
|
|
|
2023-07-30 21:15:22 +00:00
|
|
|
err = w.WriteH264(0, 0, true, [][]byte{{0x05, 0x02, 0x03, 0x04}})
|
2022-08-16 11:53:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}()
|
|
|
|
|
|
|
|
if ca == "plain" {
|
|
|
|
p, ok := newInstance("paths:\n" +
|
|
|
|
" proxied:\n" +
|
|
|
|
" source: rtmp://localhost:1937/teststream\n" +
|
|
|
|
" sourceOnDemand: yes\n")
|
|
|
|
require.Equal(t, true, ok)
|
2022-11-02 17:25:49 +00:00
|
|
|
defer p.Close()
|
2022-08-16 11:53:04 +00:00
|
|
|
} else {
|
|
|
|
p, ok := newInstance("paths:\n" +
|
|
|
|
" proxied:\n" +
|
|
|
|
" source: rtmps://localhost:1937/teststream\n" +
|
|
|
|
" sourceFingerprint: 33949E05FFFB5FF3E8AA16F8213A6251B4D9363804BA53233C4DA9A46D6F2739\n" +
|
|
|
|
" sourceOnDemand: yes\n")
|
|
|
|
require.Equal(t, true, ok)
|
2022-11-02 17:25:49 +00:00
|
|
|
defer p.Close()
|
2022-08-16 11:53:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
c := gortsplib.Client{}
|
2022-08-16 11:53:04 +00:00
|
|
|
|
|
|
|
u, err := url.Parse("rtsp://127.0.0.1:8554/proxied")
|
2021-09-05 15:35:04 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-16 11:53:04 +00:00
|
|
|
|
|
|
|
err = c.Start(u.Scheme, u.Host)
|
2021-03-19 22:04:30 +00:00
|
|
|
require.NoError(t, err)
|
2022-08-16 11:53:04 +00:00
|
|
|
defer c.Close()
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
medias, baseURL, _, err := c.Describe(u)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = c.SetupAll(medias, baseURL)
|
2022-08-16 11:53:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
c.OnPacketRTP(medias[0], medias[0].Formats[0], func(pkt *rtp.Packet) {
|
|
|
|
require.Equal(t, []byte{
|
|
|
|
0x18, 0x0, 0x19, 0x67, 0x42, 0xc0, 0x28, 0xd9,
|
|
|
|
0x0, 0x78, 0x2, 0x27, 0xe5, 0x84, 0x0, 0x0,
|
|
|
|
0x3, 0x0, 0x4, 0x0, 0x0, 0x3, 0x0, 0xf0,
|
|
|
|
0x3c, 0x60, 0xc9, 0x20, 0x0, 0x4, 0x8, 0x6,
|
|
|
|
0x7, 0x8, 0x0, 0x4, 0x5, 0x2, 0x3, 0x4,
|
|
|
|
}, pkt.Payload)
|
|
|
|
close(received)
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = c.Play(nil)
|
2022-08-16 11:53:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
close(connected)
|
|
|
|
<-received
|
|
|
|
close(done)
|
2021-03-19 22:04:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|