11package rtsp
22
33import (
4+ "bytes"
45 "context"
56 "errors"
67 "sync"
78 "testing"
89 "time"
10+
11+ "github.com/bluenviron/gortsplib/v5/pkg/description"
12+ "github.com/bluenviron/gortsplib/v5/pkg/format"
913)
1014
1115func TestHasH264IDR (t * testing.T ) {
@@ -31,6 +35,75 @@ func TestHasH264IDR(t *testing.T) {
3135 }
3236}
3337
38+ func TestLiveSourceFillsH264ParameterSetsFromFirstIDR (t * testing.T ) {
39+ t .Parallel ()
40+ sps := []byte {0x67 , 0x42 , 0xc0 , 0x1e }
41+ pps := []byte {0x68 , 0xce , 0x3c , 0x80 }
42+ idr := []byte {0x65 , 0xb8 , 0x00 , 0x01 }
43+
44+ cases := []struct {
45+ name string
46+ probe * ProbeResult
47+ nals [][]byte
48+ wantSPS []byte
49+ wantPPS []byte
50+ }{
51+ {
52+ name : "writes SPS+PPS when probe is empty" ,
53+ probe : & ProbeResult {Codec : CodecH264 },
54+ nals : [][]byte {sps , pps , idr },
55+ wantSPS : sps ,
56+ wantPPS : pps ,
57+ },
58+ {
59+ name : "missing PPS leaves PPS untouched" ,
60+ probe : & ProbeResult {Codec : CodecH264 },
61+ nals : [][]byte {sps , idr },
62+ wantSPS : sps ,
63+ wantPPS : nil ,
64+ },
65+ {
66+ name : "preserves probe-supplied SPS+PPS" ,
67+ probe : & ProbeResult {Codec : CodecH264 , SPS : []byte {0xAA }, PPS : []byte {0xBB }},
68+ nals : [][]byte {sps , pps , idr },
69+ wantSPS : []byte {0xAA },
70+ wantPPS : []byte {0xBB },
71+ },
72+ }
73+ for _ , tc := range cases {
74+ t .Run (tc .name , func (t * testing.T ) {
75+ t .Parallel ()
76+ ls := NewLiveSource (tc .probe , nil )
77+ h := & format.H264 {
78+ PayloadTyp : 96 ,
79+ PacketizationMode : 1 ,
80+ SPS : tc .probe .SPS ,
81+ PPS : tc .probe .PPS ,
82+ }
83+ media := & description.Media {
84+ Type : description .MediaTypeVideo ,
85+ Formats : []format.Format {h },
86+ }
87+ ls .AttachStream (nil , media )
88+
89+ ls .fillH264ParameterSets (tc .nals )
90+
91+ if ! bytes .Equal (h .SPS , tc .wantSPS ) {
92+ t .Fatalf ("SPS=%x want %x" , h .SPS , tc .wantSPS )
93+ }
94+ if ! bytes .Equal (h .PPS , tc .wantPPS ) {
95+ t .Fatalf ("PPS=%x want %x" , h .PPS , tc .wantPPS )
96+ }
97+ })
98+ }
99+ }
100+
101+ func TestLiveSourceFillH264ParameterSetsNoMediaIsNoOp (t * testing.T ) {
102+ t .Parallel ()
103+ ls := NewLiveSource (& ProbeResult {Codec : CodecH264 }, nil )
104+ ls .fillH264ParameterSets ([][]byte {{0x67 , 0x42 }, {0x68 , 0xce }})
105+ }
106+
34107func TestLiveSourceReadyBeforeIDRBlocks (t * testing.T ) {
35108 t .Parallel ()
36109 probe := & ProbeResult {Codec : CodecH264 , Width : 1920 , Height : 1080 , FPS : 30 }
0 commit comments