2021-09-05 15:35:04 +00:00
|
|
|
package hls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
2022-01-29 15:10:08 +00:00
|
|
|
"log"
|
2021-09-05 15:35:04 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-10-26 12:54:28 +00:00
|
|
|
"os"
|
2021-09-05 15:35:04 +00:00
|
|
|
"testing"
|
2022-02-19 11:25:23 +00:00
|
|
|
"time"
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2022-12-13 19:54:17 +00:00
|
|
|
"github.com/aler9/gortsplib/v2/pkg/format"
|
|
|
|
"github.com/aler9/gortsplib/v2/pkg/h264"
|
2021-09-05 15:35:04 +00:00
|
|
|
"github.com/asticode/go-astits"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
|
|
)
|
|
|
|
|
2022-01-29 15:10:08 +00:00
|
|
|
type testLogger struct{}
|
|
|
|
|
|
|
|
func (testLogger) Log(level logger.Level, format string, args ...interface{}) {
|
|
|
|
log.Printf(format, args...)
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:54:28 +00:00
|
|
|
var serverCert = []byte(`-----BEGIN CERTIFICATE-----
|
|
|
|
MIIDazCCAlOgAwIBAgIUXw1hEC3LFpTsllv7D3ARJyEq7sIwDQYJKoZIhvcNAQEL
|
|
|
|
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
|
|
|
|
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDEyMTMxNzQ0NThaFw0zMDEy
|
|
|
|
MTExNzQ0NThaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
|
|
|
|
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0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zNjIVmkwu38yS9RTCgdlbEVVDnS0hoifl
|
|
|
|
+jVMEGXjF3xjyMvL50BKbQUH+KAa+V4n1WGlnZOxX9TMny8MBjEuSX2+362vQ3BX
|
|
|
|
4vOlX00gvoc+sY+lrzvfx/OdPCHQGVYzoKCxhLsCgYA07HcviuIAV/HsO2/vyvhp
|
|
|
|
xF5gTu+BqNUHNOZDDDid+ge+Jre2yfQLCL8VPLXIQW3Jff53IH/PGl+NtjphuLvj
|
|
|
|
7UDJvgvpZZuymIojP6+2c3gJ3CASC9aR3JBnUzdoE1O9s2eaoMqc4scpe+SWtZYf
|
|
|
|
3vzSZ+cqF6zrD/Rf/M35IQKBgHTU4E6ShPm09CcoaeC5sp2WK8OevZw/6IyZi78a
|
|
|
|
r5Oiy18zzO97U/k6xVMy6F+38ILl/2Rn31JZDVJujniY6eSkIVsUHmPxrWoXV1HO
|
|
|
|
y++U32uuSFiXDcSLarfIsE992MEJLSAynbF1Rsgsr3gXbGiuToJRyxbIeVy7gwzD
|
|
|
|
94TpAoGAY4/PejWQj9psZfAhyk5dRGra++gYRQ/gK1IIc1g+Dd2/BxbT/RHr05GK
|
|
|
|
6vwrfjsoRyMWteC1SsNs/CurjfQ/jqCfHNP5XPvxgd5Ec8sRJIiV7V5RTuWJsPu1
|
|
|
|
+3K6cnKEyg+0ekYmLertRFIY6SwWmY1fyKgTvxudMcsBY7dC4xs=
|
|
|
|
-----END RSA PRIVATE KEY-----
|
|
|
|
`)
|
|
|
|
|
|
|
|
func writeTempFile(byts []byte) (string, error) {
|
2022-09-17 19:19:45 +00:00
|
|
|
tmpf, err := os.CreateTemp(os.TempDir(), "rtsp-")
|
2021-10-26 12:54:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer tmpf.Close()
|
|
|
|
|
|
|
|
_, err = tmpf.Write(byts)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmpf.Name(), nil
|
|
|
|
}
|
|
|
|
|
2021-09-05 15:35:04 +00:00
|
|
|
type testHLSServer struct {
|
|
|
|
s *http.Server
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:24:36 +00:00
|
|
|
func newTestHLSServer(ca string) (*testHLSServer, error) {
|
2021-09-05 15:35:04 +00:00
|
|
|
ln, err := net.Listen("tcp", "localhost:5780")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := &testHLSServer{}
|
|
|
|
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
router := gin.New()
|
|
|
|
|
2022-03-08 16:24:36 +00:00
|
|
|
segment := "segment.ts"
|
|
|
|
if ca == "segment with query" {
|
|
|
|
segment = "segment.ts?key=val"
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:54:28 +00:00
|
|
|
router.GET("/stream.m3u8", func(ctx *gin.Context) {
|
|
|
|
cnt := `#EXTM3U
|
2021-09-05 15:35:04 +00:00
|
|
|
#EXT-X-VERSION:3
|
|
|
|
#EXT-X-ALLOW-CACHE:NO
|
|
|
|
#EXT-X-TARGETDURATION:2
|
|
|
|
#EXT-X-MEDIA-SEQUENCE:0
|
|
|
|
#EXTINF:2,
|
2022-10-23 12:04:33 +00:00
|
|
|
` + segment + `
|
|
|
|
#EXT-X-ENDLIST
|
|
|
|
`
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2022-10-18 09:02:48 +00:00
|
|
|
ctx.Writer.Header().Set("Content-Type", `application/x-mpegURL`)
|
2021-10-26 12:54:28 +00:00
|
|
|
io.Copy(ctx.Writer, bytes.NewReader([]byte(cnt)))
|
|
|
|
})
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2021-10-26 12:54:28 +00:00
|
|
|
router.GET("/segment.ts", func(ctx *gin.Context) {
|
2022-03-08 16:24:36 +00:00
|
|
|
if ca == "segment with query" && ctx.Query("key") != "val" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:54:28 +00:00
|
|
|
ctx.Writer.Header().Set("Content-Type", `video/MP2T`)
|
|
|
|
|
2022-03-08 16:24:36 +00:00
|
|
|
mux := astits.NewMuxer(context.Background(), ctx.Writer)
|
2021-10-26 12:54:28 +00:00
|
|
|
mux.AddElementaryStream(astits.PMTElementaryStream{
|
|
|
|
ElementaryPID: 256,
|
|
|
|
StreamType: astits.StreamTypeH264Video,
|
|
|
|
})
|
|
|
|
mux.SetPCRPID(256)
|
|
|
|
mux.WriteTables()
|
|
|
|
|
2022-06-27 15:47:07 +00:00
|
|
|
enc, _ := h264.AnnexBMarshal([][]byte{
|
2021-10-26 12:54:28 +00:00
|
|
|
{7, 1, 2, 3}, // SPS
|
|
|
|
{8}, // PPS
|
|
|
|
{5}, // IDR
|
|
|
|
})
|
|
|
|
|
|
|
|
mux.WriteData(&astits.MuxerData{
|
|
|
|
PID: 256,
|
|
|
|
PES: &astits.PESData{
|
|
|
|
Header: &astits.PESHeader{
|
|
|
|
OptionalHeader: &astits.PESOptionalHeader{
|
|
|
|
MarkerBits: 2,
|
2022-10-09 10:16:29 +00:00
|
|
|
PTSDTSIndicator: astits.PTSDTSIndicatorBothPresent,
|
|
|
|
PTS: &astits.ClockReference{Base: 90000}, // +1 sec
|
|
|
|
DTS: &astits.ClockReference{Base: 0x1FFFFFFFF - 90000 + 1}, // -1 sec
|
2021-10-26 12:54:28 +00:00
|
|
|
},
|
|
|
|
StreamID: 224, // = video
|
|
|
|
},
|
|
|
|
Data: enc,
|
|
|
|
},
|
|
|
|
})
|
2021-09-05 15:35:04 +00:00
|
|
|
})
|
|
|
|
|
2021-10-26 12:54:28 +00:00
|
|
|
ts.s = &http.Server{Handler: router}
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2022-03-08 16:24:36 +00:00
|
|
|
if ca == "tls" {
|
2021-10-26 12:54:28 +00:00
|
|
|
go func() {
|
|
|
|
serverCertFpath, err := writeTempFile(serverCert)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(serverCertFpath)
|
|
|
|
|
|
|
|
serverKeyFpath, err := writeTempFile(serverKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(serverKeyFpath)
|
|
|
|
|
|
|
|
ts.s.ServeTLS(ln, serverCertFpath, serverKeyFpath)
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
go ts.s.Serve(ln)
|
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2021-10-26 12:54:28 +00:00
|
|
|
return ts, nil
|
|
|
|
}
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2021-10-26 12:54:28 +00:00
|
|
|
func (ts *testHLSServer) close() {
|
|
|
|
ts.s.Shutdown(context.Background())
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
2022-03-08 16:24:36 +00:00
|
|
|
for _, ca := range []string{
|
|
|
|
"plain",
|
|
|
|
"tls",
|
|
|
|
"segment with query",
|
|
|
|
} {
|
|
|
|
t.Run(ca, func(t *testing.T) {
|
|
|
|
ts, err := newTestHLSServer(ca)
|
2021-10-26 12:54:28 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ts.close()
|
|
|
|
|
2021-11-12 21:29:56 +00:00
|
|
|
packetRecv := make(chan struct{})
|
2021-10-26 12:54:28 +00:00
|
|
|
|
|
|
|
prefix := "http"
|
2022-03-08 16:24:36 +00:00
|
|
|
if ca == "tls" {
|
2021-10-26 12:54:28 +00:00
|
|
|
prefix = "https"
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:57:04 +00:00
|
|
|
c, err := NewClient(
|
2021-10-26 12:54:28 +00:00
|
|
|
prefix+"://localhost:5780/stream.m3u8",
|
|
|
|
"33949E05FFFB5FF3E8AA16F8213A6251B4D9363804BA53233C4DA9A46D6F2739",
|
2022-12-13 19:54:17 +00:00
|
|
|
func(*format.H264, *format.MPEG4Audio) error {
|
2021-10-26 12:54:28 +00:00
|
|
|
return nil
|
|
|
|
},
|
2022-02-19 11:25:23 +00:00
|
|
|
func(pts time.Duration, nalus [][]byte) {
|
2022-10-09 10:16:29 +00:00
|
|
|
require.Equal(t, 2*time.Second, pts)
|
2022-03-06 16:49:26 +00:00
|
|
|
require.Equal(t, [][]byte{
|
|
|
|
{7, 1, 2, 3},
|
|
|
|
{8},
|
2022-10-09 10:16:29 +00:00
|
|
|
{5},
|
2022-03-06 16:49:26 +00:00
|
|
|
}, nalus)
|
2021-11-12 21:29:56 +00:00
|
|
|
close(packetRecv)
|
2021-10-26 12:54:28 +00:00
|
|
|
},
|
2022-08-14 10:16:39 +00:00
|
|
|
func(pts time.Duration, au []byte) {
|
2022-02-19 11:25:23 +00:00
|
|
|
},
|
2022-01-29 15:10:08 +00:00
|
|
|
testLogger{},
|
2021-10-26 12:54:28 +00:00
|
|
|
)
|
2021-10-28 16:57:04 +00:00
|
|
|
require.NoError(t, err)
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2021-11-12 21:29:56 +00:00
|
|
|
<-packetRecv
|
2021-09-05 15:35:04 +00:00
|
|
|
|
2021-10-26 12:54:28 +00:00
|
|
|
c.Close()
|
|
|
|
c.Wait()
|
|
|
|
})
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
}
|