@@ -2,6 +2,7 @@ package sfu
2
2
3
3
import (
4
4
"sync"
5
+ "sync/atomic"
5
6
"time"
6
7
7
8
log "github.com/pion/ion-log"
@@ -99,8 +100,6 @@ func NewBuffer(track *webrtc.Track, o BufferOptions) *Buffer {
99
100
100
101
// push adds a RTP Packet, out of order, new packet may be arrived later
101
102
func (b * Buffer ) push (p * rtp.Packet ) {
102
- b .mu .Lock ()
103
- defer b .mu .Unlock ()
104
103
b .lastPacketTime = time .Now ().UnixNano ()
105
104
b .totalByte += uint64 (p .MarshalSize ())
106
105
if b .packetCount == 0 {
@@ -186,8 +185,11 @@ func (b *Buffer) buildReceptionReport() rtcp.ReceptionReport {
186
185
fracLost = uint8 ((lostInterval << 8 ) / expectedInterval )
187
186
}
188
187
var dlsr uint32
189
- if b .lastSRRecv != 0 {
190
- delayMS := uint32 ((time .Now ().UnixNano () - b .lastSRRecv ) / 1e6 )
188
+ lastSRRecv := atomic .LoadInt64 (& b .lastSRRecv )
189
+ lastSRNTPTime := atomic .LoadUint64 (& b .lastSRNTPTime )
190
+
191
+ if lastSRRecv != 0 {
192
+ delayMS := uint32 ((time .Now ().UnixNano () - lastSRRecv ) / 1e6 )
191
193
dlsr = (delayMS / 1e3 ) << 16
192
194
dlsr |= (delayMS % 1e3 ) * 65536 / 1000
193
195
}
@@ -198,18 +200,16 @@ func (b *Buffer) buildReceptionReport() rtcp.ReceptionReport {
198
200
TotalLost : lost ,
199
201
LastSequenceNumber : extMaxSeq ,
200
202
Jitter : uint32 (b .jitter ),
201
- LastSenderReport : uint32 (b . lastSRNTPTime >> 16 ),
203
+ LastSenderReport : uint32 (lastSRNTPTime >> 16 ),
202
204
Delay : dlsr ,
203
205
}
204
206
return rr
205
207
}
206
208
207
209
func (b * Buffer ) setSenderReportData (rtpTime uint32 , ntpTime uint64 ) {
208
- b .mu .Lock ()
209
- defer b .mu .Unlock ()
210
- b .lastSRRTPTime = rtpTime
211
- b .lastSRNTPTime = ntpTime
212
- b .lastSRRecv = time .Now ().UnixNano ()
210
+ atomic .StoreUint32 (& b .lastSRRTPTime , rtpTime )
211
+ atomic .StoreUint64 (& b .lastSRNTPTime , ntpTime )
212
+ atomic .StoreInt64 (& b .lastSRRecv , time .Now ().UnixNano ())
213
213
}
214
214
215
215
func (b * Buffer ) getRTCP () []rtcp.Packet {
@@ -226,25 +226,11 @@ func (b *Buffer) getRTCP() []rtcp.Packet {
226
226
return pkts
227
227
}
228
228
229
- // WritePacket write buffer packet to requested track. and modify headers
230
- func (b * Buffer ) WritePacket (sn uint16 , track * webrtc.Track , snOffset uint16 , tsOffset , ssrc uint32 ) error {
231
- b .mu .RLock ()
232
- defer b .mu .RUnlock ()
229
+ func (b * Buffer ) getPacket (sn uint16 ) (rtp.Header , []byte , error ) {
233
230
if bufferPkt := b .pktQueue .GetPacket (sn ); bufferPkt != nil {
234
- bSsrc := bufferPkt .SSRC
235
- bPT := bufferPkt .PayloadType
236
- bufferPkt .PayloadType = track .PayloadType ()
237
- bufferPkt .SequenceNumber -= snOffset
238
- bufferPkt .Timestamp -= tsOffset
239
- bufferPkt .SSRC = ssrc
240
- err := track .WriteRTP (bufferPkt )
241
- bufferPkt .PayloadType = bPT
242
- bufferPkt .Timestamp += tsOffset
243
- bufferPkt .SequenceNumber += snOffset
244
- bufferPkt .SSRC = bSsrc
245
- return err
231
+ return bufferPkt .Header , bufferPkt .Payload , nil
246
232
}
247
- return errPacketNotFound
233
+ return rtp. Header {}, nil , errPacketNotFound
248
234
}
249
235
250
236
func (b * Buffer ) onLostHandler (fn func (nack * rtcp.TransportLayerNack )) {
0 commit comments