|
| 1 | +package webrtc |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/bluenviron/gortsplib/v4/pkg/description" |
| 7 | + "github.com/bluenviron/gortsplib/v4/pkg/format" |
| 8 | + "github.com/pion/rtp" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/bluenviron/mediamtx/internal/logger" |
| 12 | + "github.com/bluenviron/mediamtx/internal/stream" |
| 13 | + "github.com/bluenviron/mediamtx/internal/unit" |
| 14 | +) |
| 15 | + |
| 16 | +// mockLogger implements logger.Writer for testing |
| 17 | +type mockLogger struct{} |
| 18 | + |
| 19 | +func (l *mockLogger) Log(_ logger.Level, _ string, _ ...interface{}) {} |
| 20 | + |
| 21 | +// TestKLVFormatDetection tests that KLV formats are properly detected |
| 22 | +func TestKLVFormatDetection(t *testing.T) { |
| 23 | + tests := []struct { |
| 24 | + name string |
| 25 | + formats []format.Format |
| 26 | + expectsKLV bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "KLV format detected", |
| 30 | + formats: []format.Format{ |
| 31 | + &format.KLV{}, |
| 32 | + }, |
| 33 | + expectsKLV: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Generic KLV format detected", |
| 37 | + formats: []format.Format{ |
| 38 | + &format.Generic{ |
| 39 | + PayloadTyp: 96, |
| 40 | + RTPMa: "KLV/90000", |
| 41 | + }, |
| 42 | + }, |
| 43 | + expectsKLV: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "No KLV format", |
| 47 | + formats: []format.Format{ |
| 48 | + &format.H264{PayloadTyp: 96}, |
| 49 | + }, |
| 50 | + expectsKLV: false, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tt := range tests { |
| 55 | + t.Run(tt.name, func(t *testing.T) { |
| 56 | + streamDesc := &description.Session{ |
| 57 | + Medias: []*description.Media{ |
| 58 | + { |
| 59 | + Type: description.MediaTypeApplication, |
| 60 | + Formats: tt.formats, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + mockStream := &stream.Stream{ |
| 66 | + WriteQueueSize: 512, |
| 67 | + RTPMaxPayloadSize: 1450, |
| 68 | + Desc: streamDesc, |
| 69 | + GenerateRTPPackets: false, |
| 70 | + Parent: &mockLogger{}, |
| 71 | + } |
| 72 | + |
| 73 | + err := mockStream.Initialize() |
| 74 | + require.NoError(t, err) |
| 75 | + defer mockStream.Close() |
| 76 | + |
| 77 | + // Test KLV detection logic (same as in setupKLVDataChannel) |
| 78 | + var klvFormat format.Format |
| 79 | + for _, media := range mockStream.Desc.Medias { |
| 80 | + for _, forma := range media.Formats { |
| 81 | + if genericFmt, ok := forma.(*format.Generic); ok { |
| 82 | + if genericFmt.RTPMap() == "KLV/90000" { |
| 83 | + klvFormat = genericFmt |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + if _, ok := forma.(*format.KLV); ok { |
| 88 | + klvFormat = forma |
| 89 | + break |
| 90 | + } |
| 91 | + if forma.Codec() == "KLV" { |
| 92 | + klvFormat = forma |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + if klvFormat != nil { |
| 97 | + break |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if tt.expectsKLV { |
| 102 | + require.NotNil(t, klvFormat, "Expected to find KLV format") |
| 103 | + } else { |
| 104 | + require.Nil(t, klvFormat, "Expected not to find KLV format") |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// TestKLVUnitHandling tests that different KLV unit types are handled correctly |
| 111 | +func TestKLVUnitHandling(t *testing.T) { |
| 112 | + tests := []struct { |
| 113 | + name string |
| 114 | + unit unit.Unit |
| 115 | + expectedData []byte |
| 116 | + }{ |
| 117 | + { |
| 118 | + name: "KLV unit with data", |
| 119 | + unit: &unit.KLV{ |
| 120 | + Unit: []byte{0x06, 0x0E, 0x2B, 0x34}, |
| 121 | + }, |
| 122 | + expectedData: []byte{0x06, 0x0E, 0x2B, 0x34}, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "KLV unit without data", |
| 126 | + unit: &unit.KLV{ |
| 127 | + Unit: nil, |
| 128 | + }, |
| 129 | + expectedData: nil, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "Generic unit with RTP packets", |
| 133 | + unit: &unit.Generic{ |
| 134 | + Base: unit.Base{ |
| 135 | + RTPPackets: []*rtp.Packet{ |
| 136 | + {Payload: []byte{0x06, 0x0E}}, |
| 137 | + {Payload: []byte{0x2B, 0x34}}, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + expectedData: []byte{0x06, 0x0E, 0x2B, 0x34}, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + for _, tt := range tests { |
| 146 | + t.Run(tt.name, func(t *testing.T) { |
| 147 | + // Simulate the unit handling logic from setupKLVDataChannel |
| 148 | + var klvData []byte |
| 149 | + |
| 150 | + switch tunit := tt.unit.(type) { |
| 151 | + case *unit.Generic: |
| 152 | + if tunit.RTPPackets != nil { |
| 153 | + for _, pkt := range tunit.RTPPackets { |
| 154 | + klvData = append(klvData, pkt.Payload...) |
| 155 | + } |
| 156 | + } |
| 157 | + case *unit.KLV: |
| 158 | + if tunit.Unit != nil { |
| 159 | + klvData = append(klvData, tunit.Unit...) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + require.Equal(t, tt.expectedData, klvData) |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// TestSetupKLVDataChannelIntegration tests that KLV setup doesn't break normal WebRTC flow |
| 169 | +func TestSetupKLVDataChannelIntegration(t *testing.T) { |
| 170 | + // Test that setupKLVDataChannel can be called without breaking anything |
| 171 | + streamDesc := &description.Session{ |
| 172 | + Medias: []*description.Media{ |
| 173 | + { |
| 174 | + Type: description.MediaTypeApplication, |
| 175 | + Formats: []format.Format{ |
| 176 | + &format.KLV{}, |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + mockStream := &stream.Stream{ |
| 183 | + WriteQueueSize: 512, |
| 184 | + RTPMaxPayloadSize: 1450, |
| 185 | + Desc: streamDesc, |
| 186 | + GenerateRTPPackets: false, |
| 187 | + Parent: &mockLogger{}, |
| 188 | + } |
| 189 | + |
| 190 | + err := mockStream.Initialize() |
| 191 | + require.NoError(t, err) |
| 192 | + defer mockStream.Close() |
| 193 | + |
| 194 | + // Create a mock peer connection (nil is fine for this test) |
| 195 | + pc := &PeerConnection{} |
| 196 | + |
| 197 | + // This should not panic or return an error, just return nil format |
| 198 | + klvFormat, err := setupKLVDataChannel(mockStream, &mockLogger{}, pc) |
| 199 | + require.NoError(t, err) |
| 200 | + require.Nil(t, klvFormat) // Should be nil due to defensive handling |
| 201 | +} |
0 commit comments