Fix not getting the correct candidate pair

This commit is contained in:
redphx 2024-12-11 18:55:28 +07:00
parent 0fb3b7b7f7
commit 1bf2f41813
2 changed files with 29 additions and 9 deletions

View File

@ -253,8 +253,9 @@ export class StreamBadges {
const allAudioCodecs: Record<string, RTCBasicStat> = {}; const allAudioCodecs: Record<string, RTCBasicStat> = {};
let audioCodecId; let audioCodecId;
const allCandidates: Record<string, string> = {}; const allCandidatePairs: Record<string, string> = {};
let candidateId; const allRemoteCandidates: Record<string, string> = {};
let candidatePairId;
stats.forEach((stat: RTCBasicStat) => { stats.forEach((stat: RTCBasicStat) => {
if (stat.type === 'codec') { if (stat.type === 'codec') {
@ -275,10 +276,12 @@ export class StreamBadges {
} else if (stat.kind === 'audio') { } else if (stat.kind === 'audio') {
audioCodecId = stat.codecId; audioCodecId = stat.codecId;
} }
} else if (stat.type === 'candidate-pair' && stat.packetsReceived > 0 && stat.state === 'succeeded') { } else if (stat.type === 'transport' && (stat as unknown as RTCTransportStats).selectedCandidatePairId) {
candidateId = stat.remoteCandidateId; candidatePairId = (stat as unknown as RTCTransportStats).selectedCandidatePairId;
} else if (stat.type === 'candidate-pair') {
allCandidatePairs[stat.id] = (stat as unknown as RTCIceCandidatePairStats).remoteCandidateId;
} else if (stat.type === 'remote-candidate') { } else if (stat.type === 'remote-candidate') {
allCandidates[stat.id] = stat.address; allRemoteCandidates[stat.id] = stat.address;
} }
}); });
@ -336,12 +339,12 @@ export class StreamBadges {
} }
// Get server type // Get server type
if (candidateId) { if (candidatePairId) {
BxLogger.info('candidate', candidateId, allCandidates); BxLogger.info('candidate', candidatePairId, allCandidatePairs);
// Server + Region // Server + Region
let text = ''; let text = '';
const isIpv6 = allCandidates[candidateId].includes(':'); const isIpv6 = allRemoteCandidates[allCandidatePairs[candidatePairId]].includes(':');
const server = this.serverInfo.server; const server = this.serverInfo.server;
if (server && server.region) { if (server && server.region) {

View File

@ -201,6 +201,7 @@ export class StreamStatsCollector {
}; };
private lastVideoStat?: RTCInboundRtpStreamStats | null; private lastVideoStat?: RTCInboundRtpStreamStats | null;
private selectedCandidatePairId: string | null | undefined = null;
private constructor() { private constructor() {
BxLogger.info(this.LOG_TAG, 'constructor()'); BxLogger.info(this.LOG_TAG, 'constructor()');
@ -212,6 +213,22 @@ export class StreamStatsCollector {
return; return;
} }
// Find selected candidate
if (!this.selectedCandidatePairId) {
let found = false;
stats.forEach(stat => {
if (found || stat.type !== 'transport') {
return;
}
stat = (stat as unknown as RTCTransportStats);
if (stat.iceState === 'connected' && stat.selectedCandidatePairId) {
this.selectedCandidatePairId = (stat as unknown as RTCTransportStats).selectedCandidatePairId;
found = true;
}
});
}
stats.forEach(stat => { stats.forEach(stat => {
if (stat.type === 'inbound-rtp' && stat.kind === 'video') { if (stat.type === 'inbound-rtp' && stat.kind === 'video') {
// FPS // FPS
@ -256,7 +273,7 @@ export class StreamStatsCollector {
dt.current = dt.total / framesDecodedDiff * 1000; dt.current = dt.total / framesDecodedDiff * 1000;
this.lastVideoStat = stat; this.lastVideoStat = stat;
} else if (stat.type === 'candidate-pair' && stat.packetsReceived > 0 && stat.state === 'succeeded') { } else if (this.selectedCandidatePairId && stat.type === 'candidate-pair' && stat.id === this.selectedCandidatePairId) {
// Round Trip Time // Round Trip Time
const ping = this.currentStats[StreamStat.PING]; const ping = this.currentStats[StreamStat.PING];
ping.current = stat.currentRoundTripTime ? stat.currentRoundTripTime * 1000 : -1; ping.current = stat.currentRoundTripTime ? stat.currentRoundTripTime * 1000 : -1;