5
5
"encoding/hex"
6
6
"encoding/json"
7
7
"fmt"
8
-
9
- "github.com/negbie/heplify/logp"
10
8
)
11
9
12
10
/* RTCP header
@@ -134,11 +132,11 @@ const (
134
132
)
135
133
136
134
type RTCP_header struct {
137
- Version uint8 `json:"version"` // 2 bit
138
- Padding uint8 `json:"padding"` // 1 bit
139
- ReceptionReportCount uint8 `json:"report_count"` // 5 bit
140
- RTCPType uint8 `json:"type"` // 8 bit
141
- Length uint16 `json:"length"` // 16 bit
135
+ Version uint8 `json:"version"` // 2 bit
136
+ Padding uint8 `json:"padding"` // 1 bit
137
+ ReportCount uint8 `json:"report_count"` // 5 bit
138
+ RTCPType uint8 `json:"type"` // 8 bit
139
+ Length uint16 `json:"length"` // 16 bit
142
140
}
143
141
144
142
type RTCP_Packet struct {
@@ -175,40 +173,37 @@ func (rp *RTCP_Packet) MarshalJSON() ([]byte, error) {
175
173
return bytes , err
176
174
}
177
175
178
- func ParseRTCP (data []byte ) ([]byte , error ) {
176
+ func ParseRTCP (data []byte ) (rtcpPkt []byte , infoMsg string ) {
179
177
dataLen := len (data )
180
178
if dataLen < 28 {
181
- return nil , fmt .Errorf ("Fishy RTCP packet length =%d in packet:\n %v\n " , dataLen , hex .Dump (data ))
179
+ return nil , fmt .Sprintf ("Fishy RTCP dataLen =%d in packet:\n %v" , dataLen , hex .Dump (data ))
182
180
}
183
181
var err error
184
182
pkt := & RTCP_Packet {}
185
- rtcpPkt := []byte {}
186
183
offset := 0
187
184
188
185
for dataLen > 0 {
189
186
if dataLen < 4 || dataLen > 576 {
190
- return nil , fmt .Errorf ("Fishy RTCP packet length =%d in packet:\n %v\n " , dataLen , hex .Dump (data ))
187
+ return rtcpPkt , fmt .Sprintf ("Fishy RTCP dataLen =%d in packet:\n %v" , dataLen , hex .Dump (data ))
191
188
}
192
189
193
- //version := ( data[offset] & 0xc0) >> 6
190
+ RTCPVersion := int (( data [offset ] & 0xc0 ) >> 6 )
194
191
//padding := (data[offset] & 0x20) >> 5
195
- receptionReportCount := int (data [offset ] & 0x1f )
192
+ RTCPReportCount := int (data [offset ] & 0x1f )
196
193
RTCPType := int (data [offset + 1 ])
197
194
RTCPLength := int (binary .BigEndian .Uint16 (data [offset + 2 :]) * 4 )
198
195
offset += 4
199
196
200
- if receptionReportCount < 0 || receptionReportCount > 4 {
201
- return rtcpPkt , fmt .Errorf ("Fishy RTCP receptionReportCount=%v type=%d length=%d offset=%d in packet:\n %v" , receptionReportCount , RTCPType , dataLen , offset , hex .Dump (data ))
202
- } else if RTCPLength > dataLen {
203
- return rtcpPkt , fmt .Errorf ("Fishy RTCP report length=%d in packet:\n %v" , RTCPLength , hex .Dump (data ))
204
- } else if RTCPType < 200 || RTCPType > 207 {
205
- return rtcpPkt , fmt .Errorf ("Fishy RTCP type=%d in packet:\n %v" , RTCPType , hex .Dump (data ))
197
+ if RTCPVersion != 2 || RTCPReportCount < 0 || RTCPReportCount > 4 || RTCPType < 200 || RTCPType > 207 || RTCPLength > dataLen {
198
+ return rtcpPkt , fmt .Sprintf ("Fishy RTCPVersion=%d, RTCPReportCount=%d, RTCPType=%d, RTCPLength=%d, dataLen=%d, offset=%d in packet:\n %v" ,
199
+ RTCPVersion , RTCPReportCount , RTCPType , RTCPLength , dataLen , offset , hex .Dump (data ))
206
200
}
207
201
208
202
switch RTCPType {
209
203
case TYPE_RTCP_SR :
210
204
if RTCPLength < 24 || offset + 24 > len (data ) {
211
- return rtcpPkt , fmt .Errorf ("Fishy RTCP packet=%v length=%d type=%d offset=%d" , data , RTCPLength , RTCPType , offset )
205
+ return rtcpPkt , fmt .Sprintf ("Fishy RTCPVersion=%d, RTCPReportCount=%d, RTCPType=%d, RTCPLength=%d, dataLen=%d, offset=%d in packet:\n %v" ,
206
+ RTCPVersion , RTCPReportCount , RTCPType , RTCPLength , dataLen , offset , hex .Dump (data ))
212
207
}
213
208
214
209
pkt .Ssrc = binary .BigEndian .Uint32 (data [offset :])
@@ -219,9 +214,9 @@ func ParseRTCP(data []byte) ([]byte, error) {
219
214
pkt .SenderInformation .Octet_count = binary .BigEndian .Uint32 (data [offset + 20 :])
220
215
offset += 24
221
216
222
- if receptionReportCount > 0 && RTCPLength >= 24 && offset + 24 <= len (data ) {
223
- tmpReportBlocks := make ([]RTCP_report_block , receptionReportCount )
224
- for i := 0 ; i < receptionReportCount ; i ++ {
217
+ if RTCPReportCount > 0 && RTCPLength >= 24 && offset + 24 <= len (data ) {
218
+ tmpReportBlocks := make ([]RTCP_report_block , RTCPReportCount )
219
+ for i := 0 ; i < RTCPReportCount ; i ++ {
225
220
tmpReportBlocks [i ].SourceSsrc = binary .BigEndian .Uint32 (data [offset :])
226
221
tmpReportBlocks [i ].Fraction_lost = data [offset + 4 ]
227
222
var cumBuf [4 ]byte
@@ -231,7 +226,7 @@ func ParseRTCP(data []byte) ([]byte, error) {
231
226
tmpReportBlocks [i ].Jitter = binary .BigEndian .Uint32 (data [offset + 12 :])
232
227
tmpReportBlocks [i ].LastSR = binary .BigEndian .Uint32 (data [offset + 16 :])
233
228
tmpReportBlocks [i ].Delay_last_SR = binary .BigEndian .Uint32 (data [offset + 20 :])
234
- tmpReportBlocks [i ].ReportCount = uint8 (receptionReportCount )
229
+ tmpReportBlocks [i ].ReportCount = uint8 (RTCPReportCount )
235
230
tmpReportBlocks [i ].RTCPType = uint8 (RTCPType )
236
231
offset += 24
237
232
RTCPLength -= 24
@@ -240,20 +235,21 @@ func ParseRTCP(data []byte) ([]byte, error) {
240
235
}
241
236
rtcpPkt , err = pkt .MarshalJSON ()
242
237
if err != nil {
243
- return nil , err
238
+ return nil , fmt . Sprintf ( "RTCP MarshalJSON %v" , err )
244
239
}
245
240
246
241
case TYPE_RTCP_RR :
247
242
if RTCPLength < 4 || offset + 4 > len (data ) {
248
- return rtcpPkt , fmt .Errorf ("Fishy RTCP packet=%v length=%d type=%d offset=%d" , data , RTCPLength , RTCPType , offset )
243
+ return rtcpPkt , fmt .Sprintf ("Fishy RTCPVersion=%d, RTCPReportCount=%d, RTCPType=%d, RTCPLength=%d, dataLen=%d, offset=%d in packet:\n %v" ,
244
+ RTCPVersion , RTCPReportCount , RTCPType , RTCPLength , dataLen , offset , hex .Dump (data ))
249
245
}
250
246
251
247
pkt .Ssrc = binary .BigEndian .Uint32 (data [offset :])
252
248
offset += 4
253
249
254
- if receptionReportCount > 0 && RTCPLength >= 24 && offset + 24 <= len (data ) {
255
- tmpReportBlocks := make ([]RTCP_report_block , receptionReportCount )
256
- for i := 0 ; i < receptionReportCount ; i ++ {
250
+ if RTCPReportCount > 0 && RTCPLength >= 24 && offset + 24 <= len (data ) {
251
+ tmpReportBlocks := make ([]RTCP_report_block , RTCPReportCount )
252
+ for i := 0 ; i < RTCPReportCount ; i ++ {
257
253
tmpReportBlocks [i ].SourceSsrc = binary .BigEndian .Uint32 (data [offset :])
258
254
tmpReportBlocks [i ].Fraction_lost = data [offset + 4 ]
259
255
var cumBuf [4 ]byte
@@ -263,7 +259,7 @@ func ParseRTCP(data []byte) ([]byte, error) {
263
259
tmpReportBlocks [i ].Jitter = binary .BigEndian .Uint32 (data [offset + 12 :])
264
260
tmpReportBlocks [i ].LastSR = binary .BigEndian .Uint32 (data [offset + 16 :])
265
261
tmpReportBlocks [i ].Delay_last_SR = binary .BigEndian .Uint32 (data [offset + 20 :])
266
- tmpReportBlocks [i ].ReportCount = uint8 (receptionReportCount )
262
+ tmpReportBlocks [i ].ReportCount = uint8 (RTCPReportCount )
267
263
tmpReportBlocks [i ].RTCPType = uint8 (RTCPType )
268
264
offset += 24
269
265
RTCPLength -= 24
@@ -272,27 +268,25 @@ func ParseRTCP(data []byte) ([]byte, error) {
272
268
}
273
269
rtcpPkt , err = pkt .MarshalJSON ()
274
270
if err != nil {
275
- return nil , err
271
+ return nil , fmt . Sprintf ( "RTCP MarshalJSON %v" , err )
276
272
}
277
273
278
274
case TYPE_RTCP_SDES :
279
- logp . Debug ( "rtcp" , "Discard RTCP_SDES packet type=%d" , RTCPType )
275
+ infoMsg = fmt . Sprintf ( "Discard RTCP_SDES packet type=%d" , RTCPType )
280
276
offset += RTCPLength
281
277
case TYPE_RTCP_APP :
282
- logp . Debug ( "rtcp" , "Discard RTCP_APP packet type=%d" , RTCPType )
278
+ infoMsg = fmt . Sprintf ( "Discard RTCP_APP packet type=%d" , RTCPType )
283
279
offset += RTCPLength
284
280
case TYPE_RTCP_BYE :
285
- logp . Debug ( "rtcp" , "Discard RTCP_BYE packet type=%d" , RTCPType )
281
+ infoMsg = fmt . Sprintf ( "Discard RTCP_BYE packet type=%d" , RTCPType )
286
282
offset += RTCPLength
287
283
case TYPE_RTCP_XR :
288
- logp . Debug ( "rtcp" , "Discard RTCP_XR packet type=%d" , RTCPType )
284
+ infoMsg = fmt . Sprintf ( "Discard RTCP_XR packet type=%d" , RTCPType )
289
285
offset += RTCPLength
290
- default :
291
- logp .Warn ("rtcp" , "Discard unsupported packet type=%d length=%d offset=%d in packet:\n %v" , RTCPType , dataLen , offset , hex .Dump (data ))
292
- return nil , fmt .Errorf ("Discard unsupported packet type: %d" , RTCPType )
293
286
}
294
287
295
288
dataLen -= offset
296
289
}
297
- return rtcpPkt , nil
290
+
291
+ return
298
292
}
0 commit comments