stats for pli and bitrate (#487)
* stats for pli and bitrate * solve comments * return 0 if no pli sent
This commit is contained in:
@@ -321,6 +321,10 @@ func (b *Buffer) SetRTT(rtt uint32) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Buffer) LastPLI() int64 {
|
||||
return b.lastPli
|
||||
}
|
||||
|
||||
func (b *Buffer) calc(pkt []byte, arrivalTime int64) {
|
||||
isRTX := false
|
||||
|
||||
@@ -638,6 +642,7 @@ func (b *Buffer) buildReceptionReport() *rtcp.ReceptionReport {
|
||||
packetsLost: b.stats.TotalPacketsLost,
|
||||
lastLossRate: lossRate,
|
||||
}
|
||||
b.stats.LostRate = lossRate
|
||||
|
||||
return &rtcp.ReceptionReport{
|
||||
SSRC: b.mediaSSRC,
|
||||
|
||||
@@ -20,6 +20,7 @@ type StreamStats struct {
|
||||
TotalNACKs uint32
|
||||
TotalPLIs uint32
|
||||
TotalFIRs uint32
|
||||
LostRate float32
|
||||
}
|
||||
|
||||
type StreamStatsWithLayers struct {
|
||||
|
||||
+38
-2
@@ -104,6 +104,10 @@ type DownTrack struct {
|
||||
|
||||
connectionStats *connectionquality.ConnectionStats
|
||||
|
||||
bitrateHelper uint64
|
||||
bitrate uint64
|
||||
lastBitrateReport time.Time
|
||||
|
||||
// Debug info
|
||||
lastPli atomic.Time
|
||||
lastRTP atomic.Time
|
||||
@@ -181,7 +185,7 @@ func NewDownTrack(
|
||||
d.connectionStats = connectionquality.NewConnectionStats(connectionquality.ConnectionStatsParams{
|
||||
CodecType: kind,
|
||||
ClockRate: c.ClockRate,
|
||||
GetTrackStats: d.getTrackStats,
|
||||
GetTrackStats: d.GetTrackStats,
|
||||
GetIsReducedQuality: func() bool {
|
||||
return d.GetForwardingStatus() != ForwardingStatusOptimal
|
||||
},
|
||||
@@ -229,6 +233,7 @@ func (d *DownTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters,
|
||||
d.callbacksQueue.Enqueue(d.onBind)
|
||||
}
|
||||
d.bound.Store(true)
|
||||
d.lastBitrateReport = time.Now()
|
||||
|
||||
go d.requestFirstKeyframe()
|
||||
|
||||
@@ -377,6 +382,7 @@ func (d *DownTrack) WriteRTP(extPkt *buffer.ExtPacket, layer int32) error {
|
||||
if extPkt.KeyFrame {
|
||||
d.isNACKThrottled.Store(false)
|
||||
}
|
||||
d.updateBitrate()
|
||||
} else {
|
||||
d.logger.Errorw("writing rtp packet err", err)
|
||||
d.pktsDropped.Inc()
|
||||
@@ -467,6 +473,7 @@ func (d *DownTrack) WritePaddingRTP(bytesToSend int) int {
|
||||
for _, f := range d.onPaddingSentUnsafe {
|
||||
f(d, size)
|
||||
}
|
||||
d.updateBitrate()
|
||||
|
||||
//
|
||||
// Register with sequencer with invalid layer so that NACKs for these can be filtered out.
|
||||
@@ -483,6 +490,26 @@ func (d *DownTrack) WritePaddingRTP(bytesToSend int) int {
|
||||
return bytesSent
|
||||
}
|
||||
|
||||
func (d *DownTrack) updateBitrate() {
|
||||
lastRtp := d.lastRTP.Load()
|
||||
d.statsLock.RLock()
|
||||
timeDiff := lastRtp.Sub(d.lastBitrateReport).Seconds()
|
||||
d.statsLock.RUnlock()
|
||||
if timeDiff < 1 {
|
||||
return
|
||||
}
|
||||
octets, _ := d.getSRStats()
|
||||
d.statsLock.Lock()
|
||||
d.bitrate = uint64(float64(octets*8-d.bitrateHelper) / timeDiff)
|
||||
d.bitrateHelper = octets * 8
|
||||
d.lastBitrateReport = lastRtp
|
||||
d.statsLock.Unlock()
|
||||
}
|
||||
|
||||
func (d *DownTrack) Bitrate() uint64 {
|
||||
return d.bitrate
|
||||
}
|
||||
|
||||
// Mute enables or disables media forwarding
|
||||
func (d *DownTrack) Mute(muted bool) {
|
||||
changed, maxLayers := d.forwarder.Mute(muted)
|
||||
@@ -985,6 +1012,7 @@ func (d *DownTrack) handleRTCP(bytes []byte) {
|
||||
d.stats.RTT = rtt
|
||||
|
||||
d.stats.Jitter = float64(r.Jitter)
|
||||
d.stats.LostRate = float32(r.FractionLost) / 256
|
||||
d.statsLock.Unlock()
|
||||
|
||||
d.connectionStats.UpdateWindow(r.SSRC, r.LastSequenceNumber, r.TotalLost, rtt, r.Jitter)
|
||||
@@ -1251,7 +1279,7 @@ func (d *DownTrack) GetConnectionScore() float32 {
|
||||
return d.connectionStats.GetScore()
|
||||
}
|
||||
|
||||
func (d *DownTrack) getTrackStats() map[uint32]*buffer.StreamStatsWithLayers {
|
||||
func (d *DownTrack) GetTrackStats() map[uint32]*buffer.StreamStatsWithLayers {
|
||||
d.statsLock.RLock()
|
||||
defer d.statsLock.RUnlock()
|
||||
|
||||
@@ -1280,3 +1308,11 @@ func (d *DownTrack) GetNackStats() (totalPackets uint32, totalRepeatedNACKs uint
|
||||
totalRepeatedNACKs = d.totalRepeatedNACKs
|
||||
return
|
||||
}
|
||||
|
||||
func (d *DownTrack) LastPLI() int64 {
|
||||
t := d.lastPli.Load()
|
||||
if t.IsZero() {
|
||||
return 0
|
||||
}
|
||||
return t.UnixNano()
|
||||
}
|
||||
|
||||
+15
-2
@@ -39,6 +39,7 @@ type TrackReceiver interface {
|
||||
GetBitrateTemporalCumulative() Bitrates
|
||||
|
||||
SendPLI(layer int32)
|
||||
LastPLI() int64
|
||||
|
||||
SetUpTrackPaused(paused bool)
|
||||
SetMaxExpectedSpatialLayer(layer int32)
|
||||
@@ -169,7 +170,7 @@ func NewWebRTCReceiver(
|
||||
w.connectionStats = connectionquality.NewConnectionStats(connectionquality.ConnectionStatsParams{
|
||||
CodecType: w.kind,
|
||||
ClockRate: w.codec.ClockRate,
|
||||
GetTrackStats: w.getTrackStats,
|
||||
GetTrackStats: w.GetTrackStats,
|
||||
GetIsReducedQuality: func() bool {
|
||||
return w.streamTrackerManager.IsReducedQuality()
|
||||
},
|
||||
@@ -402,6 +403,18 @@ func (w *WebRTCReceiver) SendPLI(layer int32) {
|
||||
buff.SendPLI()
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) LastPLI() int64 {
|
||||
var lastPLI int64
|
||||
w.bufferMu.RLock()
|
||||
for _, b := range w.buffers {
|
||||
if b != nil && b.LastPLI() > lastPLI {
|
||||
lastPLI = b.LastPLI()
|
||||
}
|
||||
}
|
||||
w.bufferMu.RUnlock()
|
||||
return lastPLI
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) SetRTCPCh(ch chan []rtcp.Packet) {
|
||||
w.rtcpCh = ch
|
||||
}
|
||||
@@ -422,7 +435,7 @@ func (w *WebRTCReceiver) ReadRTP(buf []byte, layer uint8, sn uint16) (int, error
|
||||
return buff.GetPacket(buf, sn)
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) getTrackStats() map[uint32]*buffer.StreamStatsWithLayers {
|
||||
func (w *WebRTCReceiver) GetTrackStats() map[uint32]*buffer.StreamStatsWithLayers {
|
||||
w.bufferMu.RLock()
|
||||
defer w.bufferMu.RUnlock()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user