diff --git a/go.mod b/go.mod index a4f749bd..ae649404 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( code.cloudfoundry.org/bytefmt v0.0.0-20211005130812-5bb3c17173e5 github.com/abema/go-mp4 v0.8.0 - github.com/aler9/gortsplib v0.0.0-20221110211534-12c8845fef0d + github.com/aler9/gortsplib v0.0.0-20221115222755-87d5a512b129 github.com/asticode/go-astits v1.10.1-0.20220319093903-4abe66a9b757 github.com/fsnotify/fsnotify v1.4.9 github.com/gin-gonic/gin v1.8.1 diff --git a/go.sum b/go.sum index 45e752c6..0f4c3186 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafo github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= -github.com/aler9/gortsplib v0.0.0-20221110211534-12c8845fef0d h1:fRx79L1YMXaoiSMkB32xgVCUMbOcmQ4JfySaUv7XZpc= -github.com/aler9/gortsplib v0.0.0-20221110211534-12c8845fef0d/go.mod h1:BOWNZ/QBkY/eVcRqUzJbPFEsRJshwxaxBT01K260Jeo= +github.com/aler9/gortsplib v0.0.0-20221115222755-87d5a512b129 h1:CG96FPsxizdlpsRIsjU2xQR6G3QC0sPK+f+AeVEr0Tg= +github.com/aler9/gortsplib v0.0.0-20221115222755-87d5a512b129/go.mod h1:BOWNZ/QBkY/eVcRqUzJbPFEsRJshwxaxBT01K260Jeo= github.com/aler9/writerseeker v0.0.0-20220601075008-6f0e685b9c82 h1:9WgSzBLo3a9ToSVV7sRTBYZ1GGOZUpq4+5H3SN0UZq4= github.com/aler9/writerseeker v0.0.0-20220601075008-6f0e685b9c82/go.mod h1:qsMrZCbeBf/mCLOeF16KDkPu4gktn/pOWyaq1aYQE7U= github.com/asticode/go-astikit v0.20.0 h1:+7N+J4E4lWx2QOkRdOf6DafWJMv6O4RRfgClwQokrH8= diff --git a/internal/core/api_test.go b/internal/core/api_test.go index f4940f05..f3f0fa2f 100644 --- a/internal/core/api_test.go +++ b/internal/core/api_test.go @@ -193,9 +193,10 @@ func TestAPIPathsList(t *testing.T) { "rtsp://localhost:8554/mypath", gortsplib.Tracks{ &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, }, &gortsplib.TrackMPEG4Audio{ PayloadType: 96, @@ -218,7 +219,7 @@ func TestAPIPathsList(t *testing.T) { PayloadType: 96, }, Payload: []byte{0x01, 0x02, 0x03, 0x04}, - }, true) + }) var out pathList err = httpRequest(http.MethodGet, "http://localhost:9997/v1/paths/list", nil, &out) @@ -257,9 +258,10 @@ func TestAPIPathsList(t *testing.T) { tracks := gortsplib.Tracks{ &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, }, &gortsplib.TrackMPEG4Audio{ PayloadType: 97, @@ -413,9 +415,10 @@ func TestAPIProtocolSpecificList(t *testing.T) { defer p.Close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } switch ca { @@ -468,7 +471,8 @@ func TestAPIProtocolSpecificList(t *testing.T) { 0x27, 0xe5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c, 0x60, 0xc9, 0x20, }, - PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PacketizationMode: 1, } err = conn.WriteTracks(videoTrack, nil) @@ -585,9 +589,10 @@ func TestAPIKick(t *testing.T) { defer p.Close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } switch ca { @@ -628,7 +633,8 @@ func TestAPIKick(t *testing.T) { 0x27, 0xe5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c, 0x60, 0xc9, 0x20, }, - PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PacketizationMode: 1, } err = conn.WriteTracks(videoTrack, nil) diff --git a/internal/core/core_test.go b/internal/core/core_test.go index 6e488feb..b8433a16 100644 --- a/internal/core/core_test.go +++ b/internal/core/core_test.go @@ -194,6 +194,7 @@ func main() { PayloadType: 96, SPS: []byte{0x01, 0x02, 0x03, 0x04}, PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } source := gortsplib.Client{} @@ -321,9 +322,10 @@ func TestCorePathRunOnReady(t *testing.T) { defer p.Close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } c := gortsplib.Client{} @@ -357,9 +359,10 @@ func TestCoreHotReloading(t *testing.T) { func() { track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } c := gortsplib.Client{} @@ -379,9 +382,10 @@ func TestCoreHotReloading(t *testing.T) { func() { track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } conn := gortsplib.Client{} diff --git a/internal/core/data.go b/internal/core/data.go index cc29dd9d..4dadfc71 100644 --- a/internal/core/data.go +++ b/internal/core/data.go @@ -10,13 +10,11 @@ import ( type data interface { getTrackID() int getRTPPackets() []*rtp.Packet - getPTSEqualsDTS() bool } type dataGeneric struct { - trackID int - rtpPackets []*rtp.Packet - ptsEqualsDTS bool + trackID int + rtpPackets []*rtp.Packet } func (d *dataGeneric) getTrackID() int { @@ -27,16 +25,11 @@ func (d *dataGeneric) getRTPPackets() []*rtp.Packet { return d.rtpPackets } -func (d *dataGeneric) getPTSEqualsDTS() bool { - return d.ptsEqualsDTS -} - type dataH264 struct { - trackID int - rtpPackets []*rtp.Packet - ptsEqualsDTS bool - pts time.Duration - nalus [][]byte + trackID int + rtpPackets []*rtp.Packet + pts time.Duration + nalus [][]byte } func (d *dataH264) getTrackID() int { @@ -47,10 +40,6 @@ func (d *dataH264) getRTPPackets() []*rtp.Packet { return d.rtpPackets } -func (d *dataH264) getPTSEqualsDTS() bool { - return d.ptsEqualsDTS -} - type dataMPEG4Audio struct { trackID int rtpPackets []*rtp.Packet @@ -65,7 +54,3 @@ func (d *dataMPEG4Audio) getTrackID() int { func (d *dataMPEG4Audio) getRTPPackets() []*rtp.Packet { return d.rtpPackets } - -func (d *dataMPEG4Audio) getPTSEqualsDTS() bool { - return true -} diff --git a/internal/core/hls_source.go b/internal/core/hls_source.go index f2c178c2..2bf98d59 100644 --- a/internal/core/hls_source.go +++ b/internal/core/hls_source.go @@ -5,7 +5,6 @@ import ( "time" "github.com/aler9/gortsplib" - "github.com/aler9/gortsplib/pkg/h264" "github.com/aler9/rtsp-simple-server/internal/hls" "github.com/aler9/rtsp-simple-server/internal/logger" @@ -80,10 +79,9 @@ func (s *hlsSource) run(ctx context.Context) error { onVideoData := func(pts time.Duration, nalus [][]byte) { err := stream.writeData(&dataH264{ - trackID: videoTrackID, - ptsEqualsDTS: h264.IDRPresent(nalus), - pts: pts, - nalus: nalus, + trackID: videoTrackID, + pts: pts, + nalus: nalus, }) if err != nil { s.Log(logger.Warn, "%v", err) diff --git a/internal/core/metrics_test.go b/internal/core/metrics_test.go index 49587711..eb26a9f1 100644 --- a/internal/core/metrics_test.go +++ b/internal/core/metrics_test.go @@ -34,9 +34,10 @@ func TestMetrics(t *testing.T) { defer p.Close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } source := gortsplib.Client{} @@ -69,7 +70,8 @@ func TestMetrics(t *testing.T) { 0x27, 0xe5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c, 0x60, 0xc9, 0x20, }, - PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PacketizationMode: 1, } err = conn.WriteTracks(videoTrack, nil) diff --git a/internal/core/rpicamera_source.go b/internal/core/rpicamera_source.go index d34b13f4..de819c87 100644 --- a/internal/core/rpicamera_source.go +++ b/internal/core/rpicamera_source.go @@ -5,8 +5,6 @@ import ( "time" "github.com/aler9/gortsplib" - "github.com/aler9/gortsplib/pkg/h264" - "github.com/aler9/gortsplib/pkg/rtph264" "github.com/aler9/rtsp-simple-server/internal/logger" "github.com/aler9/rtsp-simple-server/internal/rpicamera" @@ -39,10 +37,11 @@ func (s *rpiCameraSource) Log(level logger.Level, format string, args ...interfa // run implements sourceStaticImpl. func (s *rpiCameraSource) run(ctx context.Context) error { - track := &gortsplib.TrackH264{PayloadType: 96} + track := &gortsplib.TrackH264{ + PayloadType: 96, + PacketizationMode: 1, + } tracks := gortsplib.Tracks{track} - enc := &rtph264.Encoder{PayloadType: 96} - enc.Init() var stream *stream onData := func(dts time.Duration, nalus [][]byte) { @@ -60,10 +59,9 @@ func (s *rpiCameraSource) run(ctx context.Context) error { } err := stream.writeData(&dataH264{ - trackID: 0, - ptsEqualsDTS: h264.IDRPresent(nalus), - pts: dts, - nalus: nalus, + trackID: 0, + pts: dts, + nalus: nalus, }) if err != nil { s.Log(logger.Warn, "%v", err) diff --git a/internal/core/rtmp_conn.go b/internal/core/rtmp_conn.go index b927ff07..994006c1 100644 --- a/internal/core/rtmp_conn.go +++ b/internal/core/rtmp_conn.go @@ -559,10 +559,9 @@ func (c *rtmpConn) runPublish(ctx context.Context, u *url.URL) error { } err := rres.stream.writeData(&dataH264{ - trackID: videoTrackID, - ptsEqualsDTS: false, - pts: tmsg.DTS + tmsg.PTSDelta, - nalus: nalus, + trackID: videoTrackID, + pts: tmsg.DTS + tmsg.PTSDelta, + nalus: nalus, }) if err != nil { c.log(logger.Warn, "%v", err) @@ -598,10 +597,9 @@ func (c *rtmpConn) runPublish(ctx context.Context, u *url.URL) error { } err = rres.stream.writeData(&dataH264{ - trackID: videoTrackID, - ptsEqualsDTS: h264.IDRPresent(validNALUs), - pts: tmsg.DTS + tmsg.PTSDelta, - nalus: validNALUs, + trackID: videoTrackID, + pts: tmsg.DTS + tmsg.PTSDelta, + nalus: validNALUs, }) if err != nil { c.log(logger.Warn, "%v", err) diff --git a/internal/core/rtmp_server_test.go b/internal/core/rtmp_server_test.go index f13f0354..333ce407 100644 --- a/internal/core/rtmp_server_test.go +++ b/internal/core/rtmp_server_test.go @@ -75,7 +75,8 @@ func TestRTMPServerPublishRead(t *testing.T) { 0x27, 0xe5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c, 0x60, 0xc9, 0x20, }, - PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PacketizationMode: 1, } audioTrack := &gortsplib.TrackMPEG4Audio{ @@ -199,6 +200,7 @@ func TestRTMPServerAuth(t *testing.T) { PPS: []byte{ 0x68, 0xee, 0x3c, 0x80, }, + PacketizationMode: 1, } err = conn1.WriteTracks(videoTrack, nil) @@ -262,6 +264,7 @@ func TestRTMPServerAuthFail(t *testing.T) { PPS: []byte{ 0x68, 0xee, 0x3c, 0x80, }, + PacketizationMode: 1, } err = conn1.WriteTracks(videoTrack, nil) @@ -316,6 +319,7 @@ func TestRTMPServerAuthFail(t *testing.T) { PPS: []byte{ 0x68, 0xee, 0x3c, 0x80, }, + PacketizationMode: 1, } err = conn1.WriteTracks(videoTrack, nil) @@ -369,6 +373,7 @@ func TestRTMPServerAuthFail(t *testing.T) { PPS: []byte{ 0x68, 0xee, 0x3c, 0x80, }, + PacketizationMode: 1, } err = conn1.WriteTracks(videoTrack, nil) diff --git a/internal/core/rtmp_source.go b/internal/core/rtmp_source.go index 396d1efb..4085acdb 100644 --- a/internal/core/rtmp_source.go +++ b/internal/core/rtmp_source.go @@ -171,10 +171,9 @@ func (s *rtmpSource) run(ctx context.Context) error { } err = res.stream.writeData(&dataH264{ - trackID: videoTrackID, - ptsEqualsDTS: h264.IDRPresent(nalus), - pts: tmsg.DTS + tmsg.PTSDelta, - nalus: nalus, + trackID: videoTrackID, + pts: tmsg.DTS + tmsg.PTSDelta, + nalus: nalus, }) if err != nil { s.Log(logger.Warn, "%v", err) diff --git a/internal/core/rtmp_source_test.go b/internal/core/rtmp_source_test.go index fa6f283a..e9579997 100644 --- a/internal/core/rtmp_source_test.go +++ b/internal/core/rtmp_source_test.go @@ -64,7 +64,8 @@ func TestRTMPSource(t *testing.T) { 0x27, 0xe5, 0x84, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xf0, 0x3c, 0x60, 0xc9, 0x20, }, - PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PPS: []byte{0x08, 0x06, 0x07, 0x08}, + PacketizationMode: 1, } audioTrack := &gortsplib.TrackMPEG4Audio{ diff --git a/internal/core/rtsp_server_test.go b/internal/core/rtsp_server_test.go index da57ed7b..b600057d 100644 --- a/internal/core/rtsp_server_test.go +++ b/internal/core/rtsp_server_test.go @@ -45,9 +45,10 @@ func TestRTSPServerAuth(t *testing.T) { } track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } source := gortsplib.Client{} @@ -94,9 +95,10 @@ func TestRTSPServerAuth(t *testing.T) { defer p.Close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } source := gortsplib.Client{} @@ -142,9 +144,10 @@ func TestRTSPServerAuthFail(t *testing.T) { defer p.Close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } c := gortsplib.Client{} @@ -212,9 +215,10 @@ func TestRTSPServerAuthFail(t *testing.T) { defer p.Close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } c := gortsplib.Client{} @@ -238,9 +242,10 @@ func TestRTSPServerAuthFail(t *testing.T) { defer a.close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } c := gortsplib.Client{} @@ -273,9 +278,10 @@ func TestRTSPServerPublisherOverride(t *testing.T) { defer p.Close() track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } s1 := gortsplib.Client{} @@ -332,7 +338,7 @@ func TestRTSPServerPublisherOverride(t *testing.T) { Marker: true, }, Payload: []byte{0x01, 0x02, 0x03, 0x04}, - }, true) + }) if ca == "enabled" { require.Error(t, err) } else { @@ -350,7 +356,7 @@ func TestRTSPServerPublisherOverride(t *testing.T) { Marker: true, }, Payload: []byte{0x05, 0x06, 0x07, 0x08}, - }, true) + }) require.NoError(t, err) } @@ -384,9 +390,10 @@ func TestRTSPServerFallback(t *testing.T) { source := gortsplib.Client{} err := source.StartPublishing("rtsp://localhost:8554/path2", gortsplib.Tracks{&gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, }}) require.NoError(t, err) defer source.Close() diff --git a/internal/core/rtsp_session.go b/internal/core/rtsp_session.go index 80dc33b0..f5261b5a 100644 --- a/internal/core/rtsp_session.go +++ b/internal/core/rtsp_session.go @@ -384,9 +384,8 @@ func (s *rtspSession) onPacketRTP(ctx *gortsplib.ServerHandlerOnPacketRTPCtx) { switch s.session.AnnouncedTracks()[ctx.TrackID].(type) { case *gortsplib.TrackH264: err = s.stream.writeData(&dataH264{ - trackID: ctx.TrackID, - rtpPackets: []*rtp.Packet{ctx.Packet}, - ptsEqualsDTS: ctx.PTSEqualsDTS, + trackID: ctx.TrackID, + rtpPackets: []*rtp.Packet{ctx.Packet}, }) case *gortsplib.TrackMPEG4Audio: @@ -397,9 +396,8 @@ func (s *rtspSession) onPacketRTP(ctx *gortsplib.ServerHandlerOnPacketRTPCtx) { default: err = s.stream.writeData(&dataGeneric{ - trackID: ctx.TrackID, - rtpPackets: []*rtp.Packet{ctx.Packet}, - ptsEqualsDTS: ctx.PTSEqualsDTS, + trackID: ctx.TrackID, + rtpPackets: []*rtp.Packet{ctx.Packet}, }) } diff --git a/internal/core/rtsp_source.go b/internal/core/rtsp_source.go index 355f90c0..3e722ea1 100644 --- a/internal/core/rtsp_source.go +++ b/internal/core/rtsp_source.go @@ -149,9 +149,8 @@ func (s *rtspSource) run(ctx context.Context) error { switch tracks[ctx.TrackID].(type) { case *gortsplib.TrackH264: err = res.stream.writeData(&dataH264{ - trackID: ctx.TrackID, - rtpPackets: []*rtp.Packet{ctx.Packet}, - ptsEqualsDTS: ctx.PTSEqualsDTS, + trackID: ctx.TrackID, + rtpPackets: []*rtp.Packet{ctx.Packet}, }) case *gortsplib.TrackMPEG4Audio: @@ -162,9 +161,8 @@ func (s *rtspSource) run(ctx context.Context) error { default: err = res.stream.writeData(&dataGeneric{ - trackID: ctx.TrackID, - rtpPackets: []*rtp.Packet{ctx.Packet}, - ptsEqualsDTS: ctx.PTSEqualsDTS, + trackID: ctx.TrackID, + rtpPackets: []*rtp.Packet{ctx.Packet}, }) } diff --git a/internal/core/rtsp_source_test.go b/internal/core/rtsp_source_test.go index 73b8c372..a912abf5 100644 --- a/internal/core/rtsp_source_test.go +++ b/internal/core/rtsp_source_test.go @@ -14,7 +14,6 @@ import ( "github.com/aler9/gortsplib/pkg/base" "github.com/aler9/gortsplib/pkg/conn" "github.com/aler9/gortsplib/pkg/headers" - "github.com/aler9/gortsplib/pkg/rtph264" "github.com/aler9/gortsplib/pkg/url" "github.com/pion/rtp" "github.com/stretchr/testify/require" @@ -47,9 +46,10 @@ func TestRTSPSource(t *testing.T) { } { t.Run(source, func(t *testing.T) { track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } stream := gortsplib.NewServerStream(gortsplib.Tracks{track}) @@ -96,7 +96,7 @@ func TestRTSPSource(t *testing.T) { Marker: true, }, Payload: []byte{0x01, 0x02, 0x03, 0x04}, - }, true) + }) }() return &base.Response{ @@ -179,9 +179,10 @@ func TestRTSPSource(t *testing.T) { func TestRTSPSourceNoPassword(t *testing.T) { track := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, } stream := gortsplib.NewServerStream(gortsplib.Tracks{track}) @@ -242,9 +243,11 @@ func TestRTSPSourceNoPassword(t *testing.T) { } func TestRTSPSourceDynamicH264Params(t *testing.T) { - stream := gortsplib.NewServerStream(gortsplib.Tracks{&gortsplib.TrackH264{ - PayloadType: 96, - }}) + track := &gortsplib.TrackH264{ + PayloadType: 96, + PacketizationMode: 1, + } + stream := gortsplib.NewServerStream(gortsplib.Tracks{track}) defer stream.Close() s := gortsplib.Server{ @@ -282,16 +285,15 @@ func TestRTSPSourceDynamicH264Params(t *testing.T) { time.Sleep(1 * time.Second) - enc := &rtph264.Encoder{PayloadType: 96} - enc.Init() + enc := track.CreateEncoder() pkts, err := enc.Encode([][]byte{{7, 1, 2, 3}}, 0) // SPS require.NoError(t, err) - stream.WritePacketRTP(0, pkts[0], true) + stream.WritePacketRTP(0, pkts[0]) pkts, err = enc.Encode([][]byte{{8}}, 0) // PPS require.NoError(t, err) - stream.WritePacketRTP(0, pkts[0], true) + stream.WritePacketRTP(0, pkts[0]) time.Sleep(500 * time.Millisecond) @@ -316,11 +318,11 @@ func TestRTSPSourceDynamicH264Params(t *testing.T) { pkts, err = enc.Encode([][]byte{{7, 4, 5, 6}}, 0) // SPS require.NoError(t, err) - stream.WritePacketRTP(0, pkts[0], true) + stream.WritePacketRTP(0, pkts[0]) pkts, err = enc.Encode([][]byte{{8, 1}}, 0) // PPS require.NoError(t, err) - stream.WritePacketRTP(0, pkts[0], true) + stream.WritePacketRTP(0, pkts[0]) time.Sleep(500 * time.Millisecond) @@ -346,7 +348,8 @@ func TestRTSPSourceDynamicH264Params(t *testing.T) { func TestRTSPSourceRemovePadding(t *testing.T) { stream := gortsplib.NewServerStream(gortsplib.Tracks{&gortsplib.TrackH264{ - PayloadType: 96, + PayloadType: 96, + PacketizationMode: 1, }}) defer stream.Close() @@ -430,7 +433,7 @@ func TestRTSPSourceRemovePadding(t *testing.T) { }, Payload: []byte{0x01, 0x02, 0x03, 0x04}, PaddingSize: 20, - }, true) + }) <-packetRecv } @@ -473,9 +476,10 @@ func TestRTSPSourceOversizedPackets(t *testing.T) { require.Equal(t, base.Describe, req.Method) tracks := gortsplib.Tracks{&gortsplib.TrackH264{ - PayloadType: 96, - SPS: []byte{0x01, 0x02, 0x03, 0x04}, - PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PayloadType: 96, + SPS: []byte{0x01, 0x02, 0x03, 0x04}, + PPS: []byte{0x01, 0x02, 0x03, 0x04}, + PacketizationMode: 1, }} err = conn.WriteResponse(&base.Response{ diff --git a/internal/core/stream.go b/internal/core/stream.go index 07d73381..a98cdbbb 100644 --- a/internal/core/stream.go +++ b/internal/core/stream.go @@ -112,7 +112,7 @@ func (s *stream) writeData(data data) error { // forward RTP packets to RTSP readers for _, pkt := range data.getRTPPackets() { atomic.AddUint64(s.bytesReceived, uint64(pkt.MarshalSize())) - s.rtspStream.WritePacketRTP(data.getTrackID(), pkt, data.getPTSEqualsDTS()) + s.rtspStream.WritePacketRTP(data.getTrackID(), pkt) } // forward data to non-RTSP readers diff --git a/internal/core/streamtrack_h264.go b/internal/core/streamtrack_h264.go index 8efa9dc2..16eee47e 100644 --- a/internal/core/streamtrack_h264.go +++ b/internal/core/streamtrack_h264.go @@ -5,7 +5,7 @@ import ( "github.com/aler9/gortsplib" "github.com/aler9/gortsplib/pkg/h264" - "github.com/aler9/gortsplib/pkg/rtph264" + "github.com/aler9/gortsplib/pkg/rtpcodecs/rtph264" "github.com/pion/rtp" ) @@ -77,8 +77,7 @@ func newStreamTrackH264( } if allocateEncoder { - t.encoder = &rtph264.Encoder{PayloadType: 96} - t.encoder.Init() + t.encoder = track.CreateEncoder() } return t @@ -200,6 +199,7 @@ func (t *streamTrackH264) onData(dat data, hasNonRTSPReaders bool) error { SSRC: &v1, InitialSequenceNumber: &v2, InitialTimestamp: &v3, + PacketizationMode: t.track.PacketizationMode, } t.encoder.Init() } @@ -208,8 +208,7 @@ func (t *streamTrackH264) onData(dat data, hasNonRTSPReaders bool) error { // decode from RTP if hasNonRTSPReaders || t.encoder != nil { if t.decoder == nil { - t.decoder = &rtph264.Decoder{} - t.decoder.Init() + t.decoder = t.track.CreateDecoder() } nalus, pts, err := t.decoder.Decode(pkt) diff --git a/internal/core/streamtrack_mpeg4audio.go b/internal/core/streamtrack_mpeg4audio.go index f11a47d1..1f968d0a 100644 --- a/internal/core/streamtrack_mpeg4audio.go +++ b/internal/core/streamtrack_mpeg4audio.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/aler9/gortsplib" - "github.com/aler9/gortsplib/pkg/rtpmpeg4audio" + "github.com/aler9/gortsplib/pkg/rtpcodecs/rtpmpeg4audio" ) type streamTrackMPEG4Audio struct { @@ -22,14 +22,7 @@ func newStreamTrackMPEG4Audio( } if allocateEncoder { - t.encoder = &rtpmpeg4audio.Encoder{ - PayloadType: 96, - SampleRate: track.ClockRate(), - SizeLength: 13, - IndexLength: 3, - IndexDeltaLength: 3, - } - t.encoder.Init() + t.encoder = track.CreateEncoder() } return t @@ -63,13 +56,7 @@ func (t *streamTrackMPEG4Audio) onData(dat data, hasNonRTSPReaders bool) error { // decode from RTP if hasNonRTSPReaders { if t.decoder == nil { - t.decoder = &rtpmpeg4audio.Decoder{ - SampleRate: t.track.Config.SampleRate, - SizeLength: t.track.SizeLength, - IndexLength: t.track.IndexLength, - IndexDeltaLength: t.track.IndexDeltaLength, - } - t.decoder.Init() + t.decoder = t.track.CreateDecoder() } aus, pts, err := t.decoder.Decode(pkt) diff --git a/internal/hls/fmp4/init.go b/internal/hls/fmp4/init.go index 6261e870..11f5bad4 100644 --- a/internal/hls/fmp4/init.go +++ b/internal/hls/fmp4/init.go @@ -106,9 +106,10 @@ func (i *Init) Unmarshal(byts []byte) error { } curTrack.Track = &gortsplib.TrackH264{ - PayloadType: 96, - SPS: sps, - PPS: pps, + PayloadType: 96, + SPS: sps, + PPS: pps, + PacketizationMode: 1, } state = waitingTrak diff --git a/internal/hls/fmp4/init_test.go b/internal/hls/fmp4/init_test.go index a13d5041..2ae11256 100644 --- a/internal/hls/fmp4/init_test.go +++ b/internal/hls/fmp4/init_test.go @@ -17,9 +17,10 @@ var testSPS = []byte{ } var testVideoTrack = &gortsplib.TrackH264{ - PayloadType: 96, - SPS: testSPS, - PPS: []byte{0x08}, + PayloadType: 96, + SPS: testSPS, + PPS: []byte{0x08}, + PacketizationMode: 1, } var testAudioTrack = &gortsplib.TrackMPEG4Audio{ @@ -567,6 +568,7 @@ func TestInitUnmarshal(t *testing.T) { PPS: []byte{ 0x68, 0xcb, 0x8c, 0xb2, }, + PacketizationMode: 1, }, }, }, diff --git a/internal/hls/mpegts/tracks.go b/internal/hls/mpegts/tracks.go index 443771f7..3beaf070 100644 --- a/internal/hls/mpegts/tracks.go +++ b/internal/hls/mpegts/tracks.go @@ -78,7 +78,8 @@ func FindTracks(byts []byte) ([]*Track, error) { switch t.ES.StreamType { case astits.StreamTypeH264Video: t.Track = &gortsplib.TrackH264{ - PayloadType: 96, + PayloadType: 96, + PacketizationMode: 1, } case astits.StreamTypeAACAudio: diff --git a/internal/hls/mpegts/writer_test.go b/internal/hls/mpegts/writer_test.go index 64665ff2..d909221b 100644 --- a/internal/hls/mpegts/writer_test.go +++ b/internal/hls/mpegts/writer_test.go @@ -22,9 +22,10 @@ func TestWriter(t *testing.T) { } testVideoTrack := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: testSPS, - PPS: []byte{0x08}, + PayloadType: 96, + SPS: testSPS, + PPS: []byte{0x08}, + PacketizationMode: 1, } testAudioTrack := &gortsplib.TrackMPEG4Audio{ diff --git a/internal/hls/muxer_test.go b/internal/hls/muxer_test.go index d8c3a11c..e6c61490 100644 --- a/internal/hls/muxer_test.go +++ b/internal/hls/muxer_test.go @@ -23,9 +23,10 @@ var testSPS = []byte{ func TestMuxerVideoAudio(t *testing.T) { videoTrack := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: testSPS, - PPS: []byte{0x08}, + PayloadType: 96, + SPS: testSPS, + PPS: []byte{0x08}, + PacketizationMode: 1, } audioTrack := &gortsplib.TrackMPEG4Audio{ @@ -180,9 +181,10 @@ func TestMuxerVideoAudio(t *testing.T) { func TestMuxerVideoOnly(t *testing.T) { videoTrack := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: testSPS, - PPS: []byte{0x08}, + PayloadType: 96, + SPS: testSPS, + PPS: []byte{0x08}, + PacketizationMode: 1, } for _, ca := range []string{ @@ -404,9 +406,10 @@ func TestMuxerAudioOnly(t *testing.T) { func TestMuxerCloseBeforeFirstSegmentReader(t *testing.T) { videoTrack := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: testSPS, - PPS: []byte{0x08}, + PayloadType: 96, + SPS: testSPS, + PPS: []byte{0x08}, + PacketizationMode: 1, } m, err := NewMuxer(MuxerVariantMPEGTS, 3, 1*time.Second, 0, 50*1024*1024, videoTrack, nil) @@ -428,9 +431,10 @@ func TestMuxerCloseBeforeFirstSegmentReader(t *testing.T) { func TestMuxerMaxSegmentSize(t *testing.T) { videoTrack := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: testSPS, - PPS: []byte{0x08}, + PayloadType: 96, + SPS: testSPS, + PPS: []byte{0x08}, + PacketizationMode: 1, } m, err := NewMuxer(MuxerVariantMPEGTS, 3, 1*time.Second, 0, 0, videoTrack, nil) @@ -446,9 +450,10 @@ func TestMuxerMaxSegmentSize(t *testing.T) { func TestMuxerDoubleRead(t *testing.T) { videoTrack := &gortsplib.TrackH264{ - PayloadType: 96, - SPS: testSPS, - PPS: []byte{0x08}, + PayloadType: 96, + SPS: testSPS, + PPS: []byte{0x08}, + PacketizationMode: 1, } m, err := NewMuxer(MuxerVariantMPEGTS, 3, 1*time.Second, 0, 50*1024*1024, videoTrack, nil) diff --git a/internal/rtmp/conn.go b/internal/rtmp/conn.go index 3b445914..83db60a2 100644 --- a/internal/rtmp/conn.go +++ b/internal/rtmp/conn.go @@ -588,9 +588,10 @@ func trackFromH264DecoderConfig(data []byte) (*gortsplib.TrackH264, error) { } return &gortsplib.TrackH264{ - PayloadType: 96, - SPS: conf.SPS, - PPS: conf.PPS, + PayloadType: 96, + SPS: conf.SPS, + PPS: conf.PPS, + PacketizationMode: 1, }, nil } diff --git a/internal/rtmp/conn_test.go b/internal/rtmp/conn_test.go index 404e9f18..ff7d7343 100644 --- a/internal/rtmp/conn_test.go +++ b/internal/rtmp/conn_test.go @@ -499,9 +499,10 @@ func TestReadTracks(t *testing.T) { switch ca { case "video+audio": require.Equal(t, &gortsplib.TrackH264{ - PayloadType: 96, - SPS: sps, - PPS: pps, + PayloadType: 96, + SPS: sps, + PPS: pps, + PacketizationMode: 1, }, videoTrack) require.Equal(t, &gortsplib.TrackMPEG4Audio{ @@ -518,18 +519,20 @@ func TestReadTracks(t *testing.T) { case "video": require.Equal(t, &gortsplib.TrackH264{ - PayloadType: 96, - SPS: sps, - PPS: pps, + PayloadType: 96, + SPS: sps, + PPS: pps, + PacketizationMode: 1, }, videoTrack) require.Nil(t, audioTrack) case "metadata without codec id": require.Equal(t, &gortsplib.TrackH264{ - PayloadType: 96, - SPS: sps, - PPS: pps, + PayloadType: 96, + SPS: sps, + PPS: pps, + PacketizationMode: 1, }, videoTrack) require.Equal(t, &gortsplib.TrackMPEG4Audio{ @@ -546,9 +549,10 @@ func TestReadTracks(t *testing.T) { case "missing metadata": require.Equal(t, &gortsplib.TrackH264{ - PayloadType: 96, - SPS: sps, - PPS: pps, + PayloadType: 96, + SPS: sps, + PPS: pps, + PacketizationMode: 1, }, videoTrack) require.Equal(t, &gortsplib.TrackMPEG4Audio{ @@ -936,6 +940,7 @@ func TestWriteTracks(t *testing.T) { PPS: []byte{ 0x68, 0xee, 0x3c, 0x80, }, + PacketizationMode: 1, } audioTrack := &gortsplib.TrackMPEG4Audio{