Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export WebRTC transport stats #802

Open
marcovidonis opened this issue Jan 11, 2023 · 4 comments
Open

Export WebRTC transport stats #802

marcovidonis opened this issue Jan 11, 2023 · 4 comments

Comments

@marcovidonis
Copy link
Collaborator

I'm interested in monitoring the quality of the transport via data channel when a transfer is carried out via webtorrent. To do so, I need to access the WebRTC transport stats.

Pion provides stats for the data channel by calling GetStats() on the peerConnection struct. To be able to call this method during a transfer, I've done the following:

  • Exposed a GetTransportStats() method on TrackerClient, which has access to the wrapped peerConnection via the outboundOffers map:
func (tc *TrackerClient) GetTransportStats() map[string]*webrtc.StatsReport {
	stats := make(map[string]*webrtc.StatsReport)
	for i, o := range tc.outboundOffers {
		stat := o.peerConnection.GetStats()
		stats[i] = &stat
	}
	return stats
}
  • Exposed a GetWebRtcPeerConnStats() method on Torrent that calls GetTransportStats() on every TrackerClient in t.cl.websocketTrackers.clients:
type webRtcStatsReports map[string]*webrtc.StatsReport

func (t *Torrent) GetWebRtcPeerConnStats() map[string]webRtcStatsReports {
	stats := make(map[string]webRtcStatsReports)
	trackersMap := t.cl.websocketTrackers.clients
	for i, trackerClient := range trackersMap {
		ts := trackerClient.GetTransportStats()
		stats[i] = ts
	}
	return stats
}

This is a quick solution I came up with and it can surely be improved.

@anacrolix I'm interested in hearing your thoughts on this approach as I'd like to make a PR of this. I'm thinking, from an API perspective, it would probably make sense to have a single 'transport stats' function regardless of the type of transport, but then I'm not sure what this function would return, since each transport will provide a different set of stats...

@anacrolix
Copy link
Owner

Yeah I would lean toward it being fine to have a WebRTC specific stats thing here. Your method on Torrent, GetWebRtcPeerConnStats, only returns the tracker stats, would you also want to return WebRTC (torrent) peer connections too? That would probably involve iterating the peer conns list and filtering for those that are over WebRTC. If you're not interested in that, perhaps the method could be renamed to indicate it's only the trackers that are reported.

@marcovidonis
Copy link
Collaborator Author

Yeah I'm actually interested in the torrent peer connection to evaluate, for example, packet loss during file transfer. I worked on TrackerClient because it seemed to me that that's the only place where the WebRTC peer connection struct was accessible, but perhaps I'm wrong..?
Do I need to expose something in PeerConn perhaps?

@marcovidonis
Copy link
Collaborator Author

I opened #803 in draft, as I think there might be some issue getting the stats at the right time before the peer connection is closed, which might result in missing stats.
As far as I know, the connection to the tracker server should only be through a simple websocket, not a WebRTC peer connection, so I believe the stats I get should refer to the actual file transfer.

@anacrolix
Copy link
Owner

Thanks for your efforts on this. I'm not using the WebRTC stuff heavily right now, so I'm a bit in the dark about your insights.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants