-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from cloudflare/improved-connection-indicators
Add connection quality indicators
- Loading branch information
Showing
7 changed files
with
201 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useState } from 'react' | ||
import { cn } from '~/utils/style' | ||
import { Icon } from './Icon/Icon' | ||
import { Tooltip } from './Tooltip' | ||
|
||
export type ConnectionQuality = 'healthy' | 'tolerable' | 'unhealthy' | 'bad' | ||
|
||
export function getConnectionQuality(packetLoss: number): ConnectionQuality { | ||
if (packetLoss > 0.05) return 'bad' | ||
if (packetLoss > 0.03) return 'unhealthy' | ||
if (packetLoss > 0.01) return 'tolerable' | ||
return 'healthy' | ||
} | ||
|
||
export function ConnectionIndicator(props: { quality: ConnectionQuality }) { | ||
const [open, setOpen] = useState(false) | ||
return ( | ||
<Tooltip | ||
open={open} | ||
onOpenChange={setOpen} | ||
content={`Connection is ${props.quality}`} | ||
> | ||
<button onClick={() => setOpen(!open)}> | ||
<Icon | ||
className={cn( | ||
props.quality === 'healthy' && 'text-green-400', | ||
props.quality === 'tolerable' && 'text-green-400', | ||
props.quality === 'unhealthy' && 'text-yellow-400', | ||
props.quality === 'bad' && 'text-red-400' | ||
)} | ||
type={ | ||
props.quality === 'bad' || props.quality === 'unhealthy' | ||
? 'SignalSlashIcon' | ||
: 'SignalIcon' | ||
} | ||
/> | ||
</button> | ||
</Tooltip> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Observable } from 'rxjs' | ||
import Ewma from '../ewma' | ||
|
||
export const ewma = | ||
(halflifeTime: number, defaultValue = 0) => | ||
(observable: Observable<number>) => | ||
new Observable<number>((subscribe) => { | ||
const ewma = new Ewma(halflifeTime, defaultValue) | ||
observable.subscribe({ | ||
...subscribe, | ||
next: (value) => { | ||
ewma.insert(value) | ||
subscribe.next(ewma.value()) | ||
}, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import type { Observable } from 'rxjs' | ||
import { combineLatest, interval, map, pairwise, switchMap } from 'rxjs' | ||
|
||
export interface PacketLossStats { | ||
inboundPacketLossPercentage: number | ||
outboundPacketLossPercentage: number | ||
} | ||
|
||
function statsReports$( | ||
peerConnection$: Observable<RTCPeerConnection>, | ||
statReportInterval = 3000 | ||
) { | ||
return combineLatest([peerConnection$, interval(statReportInterval)]).pipe( | ||
switchMap(([peerConnection]) => peerConnection.getStats()), | ||
pairwise() | ||
) | ||
} | ||
|
||
export function getPacketLoss$( | ||
peerConnection$: Observable<RTCPeerConnection>, | ||
tracks$: Observable<MediaStreamTrack[]> | ||
) { | ||
return combineLatest([ | ||
tracks$, | ||
peerConnection$, | ||
statsReports$(peerConnection$), | ||
]).pipe( | ||
map(([tracks, peerConnection, [previousStatsReport, newStatsReport]]) => { | ||
const trackToMidMap = peerConnection | ||
.getTransceivers() | ||
.reduce((map, t) => { | ||
const track = t.sender.track ?? t.receiver.track | ||
if (track !== null && t.mid !== null) { | ||
map.set(track, t.mid) | ||
} | ||
return map | ||
}, new Map<MediaStreamTrack, string>()) | ||
const relevantMids = new Set<string>() | ||
for (const track of tracks) { | ||
const mid = trackToMidMap.get(track) | ||
if (mid) { | ||
relevantMids.add(mid) | ||
} | ||
} | ||
let inboundPacketsReceived = 0 | ||
let inboundPacketsLost = 0 | ||
let outboundPacketsSent = 0 | ||
let outboundPacketsLost = 0 | ||
|
||
newStatsReport.forEach((report) => { | ||
if (!relevantMids.has(report.mid)) return | ||
const previous = previousStatsReport.get(report.id) | ||
if (!previous) return | ||
|
||
if (report.type === 'inbound-rtp') { | ||
inboundPacketsLost += report.packetsLost - previous.packetsLost | ||
inboundPacketsReceived += | ||
report.packetsReceived - previous.packetsReceived | ||
} else if (report.type === 'outbound-rtp') { | ||
const packetsSent = report.packetsSent - previous.packetsSent | ||
// Find the corresponding remote-inbound-rtp report | ||
const remoteInboundReport = Array.from(newStatsReport.values()).find( | ||
(r) => r.type === 'remote-inbound-rtp' && r.ssrc === report.ssrc | ||
) | ||
const previousRemoteInboundReport = Array.from( | ||
previousStatsReport.values() | ||
).find( | ||
(r) => r.type === 'remote-inbound-rtp' && r.ssrc === previous.ssrc | ||
) | ||
if ( | ||
remoteInboundReport && | ||
previousRemoteInboundReport && | ||
packetsSent > 0 | ||
) { | ||
outboundPacketsSent += report.packetsSent - previous.packetsSent | ||
outboundPacketsLost += | ||
remoteInboundReport.packetsLost - | ||
previousRemoteInboundReport.packetsLost | ||
} | ||
} | ||
}) | ||
|
||
let packetsLost = inboundPacketsLost + outboundPacketsLost | ||
let packetsSent = | ||
inboundPacketsReceived + outboundPacketsSent + packetsLost | ||
let packetLossPercentage = 0 | ||
|
||
if (packetsSent > 0) { | ||
packetLossPercentage = Math.max(0, packetsLost / packetsSent) | ||
} | ||
|
||
return packetLossPercentage | ||
}) | ||
) | ||
} |