webrtc icon indicating copy to clipboard operation
webrtc copied to clipboard

Add support for emitting inbound-rtp and media-playout stats from PeerConnection.GetStats

Open Carsinalys opened this issue 5 months ago • 2 comments

What version of Pion are you using?

v4.1.1

Current behaviour

StatsTypeInboundRTP ("inbound-rtp") and StatsTypeMediaPlayout ("media-playout") are defined in stats.go, but they are never instantiated or added to the StatsReport returned by PeerConnection.GetStats().

Calling pc.GetStats() on an actively receiving connection returns:

  • ICE / DTLS / SCTP transport stats
  • Candidate pair & certificate stats
  • Codec stats
  • A synthetic peer-connection node

...but no inbound-rtp or media-playout objects, so applications cannot obtain basic per-SSRC metrics (bytes/packets/jitter, etc.) or audio-playout metrics.

Expected behaviour (per WebRTC-Stats spec)

GetStats() should include:

  1. At least one InboundRTPStreamStats entry for every primary SSRC the RTPReceiver is consuming
  2. Optionally an AudioPlayoutStats entry (or equivalent) describing local audio playout for that track
  3. Key fields such as packetsReceived, packetsLost, jitter, bytesReceived, framesDecoded, etc. should be populated

Root cause analysis

PeerConnection.GetStats() only collects from:

  • ICEGatherer / ICETransport
  • DTLSTransport / SCTPTransport
  • DataChannels
  • Certificates
  • MediaEngine
  • A synthetic PeerConnectionStats

No collectStats method exists for:

  • RTPReceiver
  • RTPSender
  • TrackRemote
  • Any audio-playout component

Therefore the structs defined in stats.go are never instantiated.

Minimal Repro

package main

import (
    "fmt"
    "github.com/pion/webrtc/v4"
)

func main() {
    pc, _ := webrtc.NewPeerConnection(webrtc.Configuration{})
    // Set up remote SDP, receive an audio/video track...
    stats := pc.GetStats()
    for id, s := range stats {
        fmt.Printf("%s → %s\n", id, s.Type())
    }
    // No item prints "inbound-rtp" or "media-playout"
}

Proposed fix

  1. Implement collectStats() on media objects
    • RTPReceiver.collectStats() → build an InboundRTPStreamStats per SSRC
    • (Complementary RTPSender.collectStats() for OutboundRTPStreamStats would also be useful)
  2. Extend PeerConnection.GetStats()
    • Iterate pc.GetReceivers() / pc.GetSenders() and funnel their stats into the shared statsReportCollector
  3. (Optional) Add playout pipeline hook
    • Provide an interface (or a default no-op implementation) for applications to feed AudioPlayoutStats data back into Pion

Environment

Item Value
OS macOS 14.5 / Linux 6.x
Go 1.22
Pion v4.1.1 & master

Additional context/references

Thanks for the awesome project!

Carsinalys avatar May 30 '25 14:05 Carsinalys