2021-08-12 09:48:47 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2022-11-11 10:59:52 +00:00
|
|
|
"crypto/tls"
|
2022-09-17 19:19:45 +00:00
|
|
|
"io"
|
2022-11-02 17:25:49 +00:00
|
|
|
"net"
|
2021-08-12 09:48:47 +00:00
|
|
|
"net/http"
|
2022-11-02 17:25:49 +00:00
|
|
|
"net/url"
|
2021-08-12 09:48:47 +00:00
|
|
|
"os"
|
|
|
|
"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/media"
|
2021-08-12 09:48:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-02 17:25:49 +00:00
|
|
|
|
2023-04-01 17:52:06 +00:00
|
|
|
"github.com/aler9/mediamtx/internal/rtmp"
|
2021-08-12 09:48:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetrics(t *testing.T) {
|
|
|
|
serverCertFpath, err := writeTempFile(serverCert)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(serverCertFpath)
|
|
|
|
|
|
|
|
serverKeyFpath, err := writeTempFile(serverKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(serverKeyFpath)
|
|
|
|
|
|
|
|
p, ok := newInstance("metrics: yes\n" +
|
2022-12-15 23:50:47 +00:00
|
|
|
"webrtcServerCert: " + serverCertFpath + "\n" +
|
|
|
|
"webrtcServerKey: " + serverKeyFpath + "\n" +
|
2021-08-12 09:48:47 +00:00
|
|
|
"encryption: optional\n" +
|
|
|
|
"serverCert: " + serverCertFpath + "\n" +
|
2021-11-06 11:51:38 +00:00
|
|
|
"serverKey: " + serverKeyFpath + "\n" +
|
|
|
|
"paths:\n" +
|
|
|
|
" all:\n")
|
2021-08-12 09:48:47 +00:00
|
|
|
require.Equal(t, true, ok)
|
2022-11-02 17:25:49 +00:00
|
|
|
defer p.Close()
|
2021-08-12 09:48:47 +00:00
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
medi := testMediaH264
|
2021-08-12 09:48:47 +00:00
|
|
|
|
2021-11-12 21:29:56 +00:00
|
|
|
source := gortsplib.Client{}
|
2022-12-13 19:54:17 +00:00
|
|
|
err = source.StartRecording("rtsp://localhost:8554/rtsp_path",
|
|
|
|
media.Medias{medi})
|
2021-08-12 09:48:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer source.Close()
|
|
|
|
|
2022-11-11 10:59:52 +00:00
|
|
|
source2 := gortsplib.Client{TLSConfig: &tls.Config{InsecureSkipVerify: true}}
|
2022-12-13 19:54:17 +00:00
|
|
|
err = source2.StartRecording("rtsps://localhost:8322/rtsps_path",
|
|
|
|
media.Medias{medi})
|
2022-11-11 10:59:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer source2.Close()
|
|
|
|
|
2022-11-02 17:25:49 +00:00
|
|
|
u, err := url.Parse("rtmp://localhost:1935/rtmp_path")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
nconn, err := net.Dial("tcp", u.Host)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer nconn.Close()
|
|
|
|
conn := rtmp.NewConn(nconn)
|
|
|
|
|
|
|
|
err = conn.InitializeClient(u, true)
|
|
|
|
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-11-02 17:25:49 +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-11-02 17:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.WriteTracks(videoTrack, nil)
|
2021-08-12 09:48:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-05 16:29:13 +00:00
|
|
|
func() {
|
|
|
|
res, err := http.Get("http://localhost:8888/rtsp_path/index.m3u8")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
require.Equal(t, 200, res.StatusCode)
|
|
|
|
}()
|
|
|
|
|
2021-08-12 09:48:47 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://localhost:9998/metrics", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
2022-09-17 19:19:45 +00:00
|
|
|
bo, err := io.ReadAll(res.Body)
|
2021-08-12 09:48:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-11-11 10:59:52 +00:00
|
|
|
require.Regexp(t,
|
|
|
|
`^paths\{name=".*?",state="ready"\} 1`+"\n"+
|
|
|
|
`paths_bytes_received\{name=".*?",state="ready"\} 0`+"\n"+
|
|
|
|
`paths\{name=".*?",state="ready"\} 1`+"\n"+
|
|
|
|
`paths_bytes_received\{name=".*?",state="ready"\} 0`+"\n"+
|
|
|
|
`paths\{name=".*?",state="ready"\} 1`+"\n"+
|
|
|
|
`paths_bytes_received\{name=".*?",state="ready"\} 0`+"\n"+
|
2022-12-15 23:50:47 +00:00
|
|
|
`hls_muxers\{name="rtsp_path"\} 1`+"\n"+
|
|
|
|
`hls_muxers_bytes_sent\{name="rtsp_path"\} [0-9]+`+"\n"+
|
2022-11-11 10:59:52 +00:00
|
|
|
`rtsp_conns\{id=".*?"\} 1`+"\n"+
|
|
|
|
`rtsp_conns_bytes_received\{id=".*?"\} [0-9]+`+"\n"+
|
|
|
|
`rtsp_conns_bytes_sent\{id=".*?"\} [0-9]+`+"\n"+
|
|
|
|
`rtsp_sessions\{id=".*?",state="publish"\} 1`+"\n"+
|
|
|
|
`rtsp_sessions_bytes_received\{id=".*?",state="publish"\} 0`+"\n"+
|
|
|
|
`rtsp_sessions_bytes_sent\{id=".*?",state="publish"\} [0-9]+`+"\n"+
|
|
|
|
`rtsps_conns\{id=".*?"\} 1`+"\n"+
|
|
|
|
`rtsps_conns_bytes_received\{id=".*?"\} [0-9]+`+"\n"+
|
|
|
|
`rtsps_conns_bytes_sent\{id=".*?"\} [0-9]+`+"\n"+
|
|
|
|
`rtsps_sessions\{id=".*?",state="publish"\} 1`+"\n"+
|
|
|
|
`rtsps_sessions_bytes_received\{id=".*?",state="publish"\} 0`+"\n"+
|
|
|
|
`rtsps_sessions_bytes_sent\{id=".*?",state="publish"\} [0-9]+`+"\n"+
|
|
|
|
`rtmp_conns\{id=".*?",state="publish"\} 1`+"\n"+
|
|
|
|
`rtmp_conns_bytes_received\{id=".*?",state="publish"\} [0-9]+`+"\n"+
|
|
|
|
`rtmp_conns_bytes_sent\{id=".*?",state="publish"\} [0-9]+`+"\n"+
|
2022-12-15 23:50:47 +00:00
|
|
|
"$",
|
2022-11-11 10:59:52 +00:00
|
|
|
string(bo))
|
2021-08-12 09:48:47 +00:00
|
|
|
}
|