Skip to content

Commit

Permalink
fix: calcuating difference average, av sync and remove unnecessary fi…
Browse files Browse the repository at this point in the history
…elds for subscriber stats sample
  • Loading branch information
eswarclynn authored Mar 12, 2024
1 parent 99513a1 commit b25e9cf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export abstract class RunningTrackAnalytics {

protected samples: (LocalBaseSample | LocalVideoSample | RemoteAudioSample | RemoteVideoSample)[] = [];
protected tempStats: TempPublishStats[] = [];
protected prevLatestStat?: TempPublishStats;

constructor({
track,
Expand Down Expand Up @@ -118,6 +119,7 @@ export abstract class RunningTrackAnalytics {
}

this.samples.push(this.collateSample());
this.prevLatestStat = this.getLatestStat();
this.tempStats.length = 0;
}

Expand Down Expand Up @@ -160,7 +162,7 @@ export abstract class RunningTrackAnalytics {
}

protected calculateDifferenceForSample(key: keyof TempPublishStats) {
const firstValue = Number(this.tempStats[0][key]) || 0;
const firstValue = Number(this.prevLatestStat?.[key]) || 0;
const latestValue = Number(this.getLatestStat()[key]) || 0;

return latestValue - firstValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,16 @@ export class SubscribeStatsAnalytics extends BaseStatsAnalytics {

// eslint-disable-next-line complexity
private calculateAvSyncForStat(trackStats: HMSTrackStats, hmsStats: HMSWebrtcStats) {
if (!trackStats.peerID || !(trackStats.kind === 'video')) {
if (!trackStats.peerID || !trackStats.estimatedPlayoutTimestamp || trackStats.kind !== 'video') {
return;
}
const peer = this.store.getPeerById(trackStats.peerID);
const audioTrack = peer?.audioTrack;
const videoTrack = peer?.videoTrack;
/**
* 1. Send value as MAX_SAFE_INTEGER when either audio or value track is muted for the entire window
* 2. When both audio and video are unmuted for a part of window , then divide the difference by those many number of samples only
*/
const areBothTracksEnabled = audioTrack && videoTrack && audioTrack.enabled && videoTrack.enabled;
if (!areBothTracksEnabled) {
return MAX_SAFE_INTEGER;
Expand All @@ -106,7 +110,12 @@ export class SubscribeStatsAnalytics extends BaseStatsAnalytics {
if (!audioStats) {
return MAX_SAFE_INTEGER;
}
return audioStats.timestamp - trackStats.timestamp;
if (!audioStats.estimatedPlayoutTimestamp) {
return;
}

// https://w3c.github.io/webrtc-stats/#dom-rtcinboundrtpstreamstats-estimatedplayouttimestamp
return audioStats.estimatedPlayoutTimestamp - trackStats.estimatedPlayoutTimestamp;
}
}

Expand All @@ -119,14 +128,9 @@ class RunningRemoteTrackAnalytics extends RunningTrackAnalytics {

const baseSample = {
timestamp: Date.now(),
fec_packets_discarded: this.calculateDifferenceForSample('fecPacketsDiscarded'),
fec_packets_received: this.calculateDifferenceForSample('fecPacketsReceived'),
total_samples_duration: this.calculateDifferenceForSample('totalSamplesDuration'),
total_packets_received: this.calculateDifferenceForSample('packetsReceived'),
total_packets_lost: this.calculateDifferenceForSample('packetsLost'),
total_pli_count: this.calculateDifferenceForSample('pliCount'),
total_nack_count: this.calculateDifferenceForSample('nackCount'),
avg_jitter_buffer_delay: this.calculateAverage('calculatedJitterBufferDelay'),
avg_jitter_buffer_delay: this.calculateAverage('calculatedJitterBufferDelay', false),
};

if (latestStat.kind === 'video') {
Expand Down Expand Up @@ -155,6 +159,11 @@ class RunningRemoteTrackAnalytics extends RunningTrackAnalytics {
audio_concealed_samples,
audio_total_samples_received: this.calculateDifferenceForSample('totalSamplesReceived'),
audio_concealment_events: this.calculateDifferenceForSample('concealmentEvents'),
fec_packets_discarded: this.calculateDifferenceForSample('fecPacketsDiscarded'),
fec_packets_received: this.calculateDifferenceForSample('fecPacketsReceived'),
total_samples_duration: this.calculateDifferenceForSample('totalSamplesDuration'),
total_packets_received: this.calculateDifferenceForSample('packetsReceived'),
total_packets_lost: this.calculateDifferenceForSample('packetsLost'),
});
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/hms-video-store/src/interfaces/webrtc-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface MissingInboundStats extends RTCInboundRtpStreamStats, MissingCo
totalFreezesDuration?: number;
jitterBufferDelay?: number;
jitterBufferEmittedCount?: number;
estimatedPlayoutTimestamp?: DOMHighResTimeStamp;
}

export type PeerConnectionType = 'publish' | 'subscribe';
Expand Down

0 comments on commit b25e9cf

Please sign in to comment.