anacrolix / torrent

Full-featured BitTorrent client package and utilities
Mozilla Public License 2.0
5.38k stars 616 forks source link

Export WebRTC transport stats #802

Open marcovidonis opened 1 year ago

marcovidonis commented 1 year ago

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:

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.