-
Notifications
You must be signed in to change notification settings - Fork 16
feat: audio video stream missing data detector #30
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a4824b0
feat: add missing data in stream detector
eugeny-dementev d22bfa2
feat: preare kind common type for parsed audio/video stream stats
eugeny-dementev 312e23a
feat: rework detector to only check bytesReceived
eugeny-dementev da3b55f
lint: code fixes
eugeny-dementev 8f2084b
feat: calculate bytes received delta
eugeny-dementev fbeda95
feat: rework MissingStreamDataDetector to check last 3 received stats
eugeny-dementev a183d86
doc: add example for MissingStreamDataDetector
eugeny-dementev e7bbccf
feat: use last n stats to detect the issue
eugeny-dementev de8e19d
lint: code fixes
eugeny-dementev 642c6a4
fix: udpate comment to detector options parameter
eugeny-dementev 69b2c50
fix: code review
eugeny-dementev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,165 @@ | ||
import { | ||
CommonParsedInboundStreamStats, | ||
IssueDetectorResult, | ||
IssuePayload, | ||
IssueReason, | ||
IssueType, | ||
WebRTCStatsParsed, | ||
} from '../types'; | ||
import BaseIssueDetector from './BaseIssueDetector'; | ||
|
||
interface MissingStreamDetectorParams { | ||
timeoutMs?: number; // delay to report the issue no more often then once per specified timeout | ||
steps?: number; // number of last stats to check | ||
} | ||
|
||
export default class MissingStreamDataDetector extends BaseIssueDetector { | ||
readonly #lastMarkedAt = new Map<string, number>(); | ||
evgmel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
readonly #timeoutMs: number; | ||
|
||
readonly #steps: number; | ||
|
||
constructor(params: MissingStreamDetectorParams = {}) { | ||
super(); | ||
this.#timeoutMs = params.timeoutMs ?? 15_000; | ||
this.#steps = params.steps ?? 3; | ||
} | ||
|
||
performDetection(data: WebRTCStatsParsed): IssueDetectorResult { | ||
const { connection: { id: connectionId } } = data; | ||
const issues = this.processData(data); | ||
this.setLastProcessedStats(connectionId, data); | ||
return issues; | ||
} | ||
|
||
private processData(data: WebRTCStatsParsed): IssueDetectorResult { | ||
const issues: IssueDetectorResult = []; | ||
|
||
const allLastProcessedStats = [...this.getAllLastProcessedStats(data.connection.id), data]; | ||
if (allLastProcessedStats.length < this.#steps) { | ||
return issues; | ||
} | ||
|
||
const lastNProcessedStats = allLastProcessedStats.slice(-this.#steps); | ||
|
||
eugeny-dementev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const lastNVideoInbound = lastNProcessedStats.map((stats) => stats.video.inbound); | ||
const lastNAudioInbound = lastNProcessedStats.map((stats) => stats.audio.inbound); | ||
|
||
issues.push(...this.detectMissingData( | ||
lastNAudioInbound as unknown as CommonParsedInboundStreamStats[][], | ||
IssueType.Stream, | ||
IssueReason.MissingAudioStreamData, | ||
)); | ||
|
||
issues.push(...this.detectMissingData( | ||
lastNVideoInbound, | ||
IssueType.Stream, | ||
IssueReason.MissingVideoStreamData, | ||
)); | ||
|
||
const unvisitedTrackIds = new Set(this.#lastMarkedAt.keys()); | ||
evgmel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
unvisitedTrackIds.forEach((trackId) => { | ||
const lastMarkedAt = this.#lastMarkedAt.get(trackId); | ||
if (lastMarkedAt && Date.now() - lastMarkedAt > this.#timeoutMs) { | ||
this.removeMarkedIssue(trackId); | ||
} | ||
}); | ||
|
||
return issues; | ||
} | ||
|
||
private detectMissingData( | ||
lastNInboundStats: CommonParsedInboundStreamStats[][], | ||
type: IssueType, | ||
reason: IssueReason, | ||
): IssueDetectorResult { | ||
const issues: IssuePayload[] = []; | ||
|
||
const currentInboundStats = lastNInboundStats.pop()!; | ||
const prevInboundItemsByTrackId = MissingStreamDataDetector.mapStatsByTrackId(lastNInboundStats); | ||
|
||
currentInboundStats.forEach((inboundItem) => { | ||
const trackId = inboundItem.track.trackIdentifier; | ||
|
||
const prevInboundItems = prevInboundItemsByTrackId.get(trackId); | ||
|
||
if (!Array.isArray(prevInboundItems) || prevInboundItems.length === 0) { | ||
return; | ||
} | ||
|
||
if (inboundItem.track.detached || inboundItem.track.ended) { | ||
return; | ||
} | ||
|
||
if (!MissingStreamDataDetector.isAllBytesReceivedDidntChange(inboundItem.bytesReceived, prevInboundItems)) { | ||
this.removeMarkedIssue(trackId); | ||
return; | ||
} | ||
|
||
const issueMarked = this.markIssue(trackId); | ||
|
||
if (!issueMarked) { | ||
return; | ||
} | ||
|
||
const statsSample = { | ||
bytesReceived: inboundItem.bytesReceived, | ||
}; | ||
|
||
issues.push({ | ||
type, | ||
reason, | ||
statsSample, | ||
trackIdentifier: trackId, | ||
}); | ||
}); | ||
|
||
return issues; | ||
} | ||
|
||
private static mapStatsByTrackId( | ||
items: CommonParsedInboundStreamStats[][], | ||
): Map<string, CommonParsedInboundStreamStats[]> { | ||
const statsById = new Map<string, CommonParsedInboundStreamStats[]>(); | ||
items.forEach((inboundItems) => { | ||
inboundItems.forEach((inbountItem) => { | ||
const accumulatedItems = statsById.get(inbountItem.track.trackIdentifier) || []; | ||
accumulatedItems.push(inbountItem); | ||
statsById.set(inbountItem.track.trackIdentifier, accumulatedItems); | ||
}); | ||
}); | ||
|
||
return statsById; | ||
} | ||
|
||
private static isAllBytesReceivedDidntChange( | ||
bytesReceived: number, inboundItems: CommonParsedInboundStreamStats[], | ||
): boolean { | ||
for (let i = 0; i < inboundItems.length; i += 1) { | ||
const inboundItem = inboundItems[i]; | ||
if (inboundItem.bytesReceived !== bytesReceived) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private markIssue(trackId: string): boolean { | ||
const now = Date.now(); | ||
const lastMarkedAt = this.#lastMarkedAt.get(trackId); | ||
|
||
if (!lastMarkedAt || now - lastMarkedAt > this.#timeoutMs) { | ||
this.#lastMarkedAt.set(trackId, now); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private removeMarkedIssue(trackId: string): void { | ||
this.#lastMarkedAt.delete(trackId); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.