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 6 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

Hi @JoeTurki , I hope you're doing well. Apologies if I'm interrupting—I just wanted to follow up since you were the last one to respond on the PR. I was wondering if there are any plans to support the missing stats? Any updates would be greatly appreciated. Thanks!

Carsinalys avatar Jun 09 '25 08:06 Carsinalys

Hello, Thank you for the report, We'll add this to the feature request, No promises. But Probably ready with 4.3 or 4.4.

JoeTurki avatar Jun 10 '25 01:06 JoeTurki

I think this auto-closed from my PR but I am keeping this open until I've resolved the media-playout stats collection as well. inbound-rtp stats collection (per-SSRC counters for receiver side) is now supported @Carsinalys per https://github.com/pion/webrtc/pull/3221

drshrey avatar Sep 19 '25 08:09 drshrey

@Carsinalys you can now get inbound-rtp and media-playout stats. I'll try and add an example of how to get media-playout stats since that requires some setup on the application side (you can look at PR body of https://github.com/pion/webrtc/pull/3234 to get a sense). Just a quick disclaimer this covers Audio media playout stats for now since that is what is supported in the spec for now. Closing this issue but I'll reference this issue in the PR including the media-playout example usage as well. Let us know if there's anything else you have in mind!

drshrey avatar Oct 03 '25 16:10 drshrey

#3234

@drshrey thank you a lot, I'll check with next pion release! I have idea to make PR about ivfwriter, I faced issue when I record audio & video mixed streams duration of audio files is normal, but video duration being scaled by 1.111... because of timebaseDenominator, I'm not aware how better to solve it. First idea in mind is to add more options to have ability redefine timebaseDenominator and timebaseNumerator (for my case it 1000 and 30). Right now I just rewrite file header

Carsinalys avatar Oct 03 '25 19:10 Carsinalys

I have idea to make PR about ivfwriter, I faced issue when I record audio & video mixed streams duration of audio files is normal, but video duration being scaled by 1.111... because of timebaseDenominator, I'm not aware how better to solve it. First idea in mind is to add more options to have ability redefine timebaseDenominator and timebaseNumerator (for my case it 1000 and 30). Right now I just rewrite file header

This is possible via WithFrameRate it was added recently. https://github.com/pion/webrtc/blob/master/pkg/media/ivfwriter/ivfwriter.go#L356

also we released v4.1.5

JoeTurki avatar Oct 04 '25 00:10 JoeTurki

I have idea to make PR about ivfwriter, I faced issue when I record audio & video mixed streams duration of audio files is normal, but video duration being scaled by 1.111... because of timebaseDenominator, I'm not aware how better to solve it. First idea in mind is to add more options to have ability redefine timebaseDenominator and timebaseNumerator (for my case it 1000 and 30). Right now I just rewrite file header

This is possible via WithFrameRate it was added recently. https://github.com/pion/webrtc/blob/master/pkg/media/ivfwriter/ivfwriter.go#L356

also we released v4.1.5

Great to see that, thanks a lot!

Carsinalys avatar Oct 05 '25 22:10 Carsinalys