@@ -43,6 +43,15 @@ type trackStreams struct {
43
43
type rtxPacketWithAttributes struct {
44
44
pkt []byte
45
45
attributes interceptor.Attributes
46
+ pool * sync.Pool
47
+ }
48
+
49
+ func (p * rtxPacketWithAttributes ) release () {
50
+ if p .pkt != nil {
51
+ b := p .pkt [:cap (p .pkt )]
52
+ p .pool .Put (b ) // nolint:staticcheck
53
+ p .pkt = nil
54
+ }
46
55
}
47
56
48
57
// RTPReceiver allows an application to inspect the receipt of a TrackRemote
@@ -59,6 +68,8 @@ type RTPReceiver struct {
59
68
60
69
// A reference to the associated api object
61
70
api * API
71
+
72
+ rtxPool sync.Pool
62
73
}
63
74
64
75
// NewRTPReceiver constructs a new RTPReceiver
@@ -74,6 +85,9 @@ func (api *API) NewRTPReceiver(kind RTPCodecType, transport *DTLSTransport) (*RT
74
85
closed : make (chan interface {}),
75
86
received : make (chan interface {}),
76
87
tracks : []trackStreams {},
88
+ rtxPool : sync.Pool {New : func () interface {} {
89
+ return make ([]byte , api .settingEngine .getReceiveMTU ())
90
+ }},
77
91
}
78
92
79
93
return r , nil
@@ -411,10 +425,11 @@ func (r *RTPReceiver) receiveForRtx(ssrc SSRC, rsid string, streamInfo *intercep
411
425
track .repairStreamChannel = make (chan rtxPacketWithAttributes )
412
426
413
427
go func () {
414
- b := make ([]byte , r .api .settingEngine .getReceiveMTU ())
415
428
for {
429
+ b := r .rtxPool .Get ().([]byte ) // nolint:forcetypeassert
416
430
i , attributes , err := track .repairInterceptor .Read (b , nil )
417
431
if err != nil {
432
+ r .rtxPool .Put (b ) // nolint:staticcheck
418
433
return
419
434
}
420
435
@@ -435,6 +450,7 @@ func (r *RTPReceiver) receiveForRtx(ssrc SSRC, rsid string, streamInfo *intercep
435
450
436
451
if i - int (headerLength )- paddingLength < 2 {
437
452
// BWE probe packet, ignore
453
+ r .rtxPool .Put (b ) // nolint:staticcheck
438
454
continue
439
455
}
440
456
@@ -450,8 +466,9 @@ func (r *RTPReceiver) receiveForRtx(ssrc SSRC, rsid string, streamInfo *intercep
450
466
451
467
select {
452
468
case <- r .closed :
469
+ r .rtxPool .Put (b ) // nolint:staticcheck
453
470
return
454
- case track .repairStreamChannel <- rtxPacketWithAttributes {pkt : b [:i - 2 ], attributes : attributes }:
471
+ case track .repairStreamChannel <- rtxPacketWithAttributes {pkt : b [:i - 2 ], attributes : attributes , pool : & r . rtxPool }:
455
472
}
456
473
}
457
474
}()
0 commit comments