Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Nov 10, 2024
1 parent 5147655 commit df5de03
Showing 1 changed file with 87 additions and 87 deletions.
174 changes: 87 additions & 87 deletions src/stores/omniscientLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,93 +12,93 @@ export const webrtcStats = new WebRTCStats({ getStatsInterval: 250 })
export const useOmniscientLoggerStore = defineStore('omniscient-logger', () => {
const videoStore = useVideoStore()

// Routine to log the framerate of the video streams
const streamsFrameRateHistory = ref<{ [key in string]: number[] }>({})
let lastStreamAverageFramerateLog = new Date()
const streamAverageFramerateLogDelay = 10000
setInterval(() => {
Object.keys(videoStore.activeStreams).forEach((streamName) => {
if (videoStore.activeStreams[streamName] === undefined) return
videoStore.activeStreams[streamName]!.mediaStream?.getVideoTracks().forEach((track) => {
if (streamsFrameRateHistory.value[streamName] === undefined) streamsFrameRateHistory.value[streamName] = []
if (track.getSettings().frameRate === undefined) return

const streamHistory = streamsFrameRateHistory.value[streamName]
streamHistory.push(track.getSettings().frameRate as number)
streamHistory.splice(0, streamHistory.length - 10)
streamsFrameRateHistory.value[streamName] = streamHistory

const average = streamHistory.reduce((a, b) => a + b, 0) / streamHistory.length
const minThreshold = 0.9 * average
const newFrameRate = track.getSettings().frameRate as number

// Warn about drops in the framerate of the video stream
if (newFrameRate < minThreshold) {
console.warn(`Drop in the framerate detected for stream '${streamName}': ${newFrameRate.toFixed(2)} fps.`)
}

// Log the average framerate of the video stream recursively
if (new Date().getTime() - lastStreamAverageFramerateLog.getTime() > streamAverageFramerateLogDelay) {
console.debug(`Average frame rate for stream '${streamName}': ${average.toFixed(2)} fps.`)
lastStreamAverageFramerateLog = new Date()
}
})
})
}, 250)

// Routine to log the framerate of the application rendering
const appFrameRateHistory = ref<number[]>([])
const appAverageFrameRateSampleDelay = 100
const appAverageFrameRateLogDelay = 10000
let lastAppAverageFpsLog = new Date()

// Log tab visibility changes so we don't warn about framerate drops when the tab was not visible in the last seconds
const windowVisibility = useDocumentVisibility()
let timeLastTabOpening = new Date()
watch(windowVisibility, (newVisibility, oldVisibility) => {
if (newVisibility === 'visible' && oldVisibility === 'hidden') {
timeLastTabOpening = new Date()
}
})

const fpsMeter = (): void => {
let prevTime = performance.now()
let frames = 0

requestAnimationFrame(function loop() {
const time = performance.now()
frames++
if (time > prevTime + appAverageFrameRateSampleDelay) {
const currentFPS = Math.round((frames * 1000) / (time - prevTime))
prevTime = time
frames = 0

appFrameRateHistory.value.push(currentFPS)
appFrameRateHistory.value.splice(0, appFrameRateHistory.value.length - 10)

const average = appFrameRateHistory.value.reduce((a, b) => a + b, 0) / appFrameRateHistory.value.length

// Warn about drops in the framerate of the application rendering
// The threshold is set to 80% of the average framerate by default
// We don't warn if the framerate is above 60 because it's an already very high value, and we don't want to spam the console on high-end monitors
// We also don't warn if the tab was not visible in the last second, since the application was not being rendered
const minThreshold = Math.min(60, 0.8 * average)
const msSinceLastTabOpening = new Date().getTime() - timeLastTabOpening.getTime()
if (currentFPS < minThreshold && msSinceLastTabOpening > 1000) {
console.warn(`Drop in the framerate detected for the application rendering: ${currentFPS.toFixed(2)} fps.`)
}

// Log the average framerate of the application rendering recursively
if (new Date().getTime() - lastAppAverageFpsLog.getTime() > appAverageFrameRateLogDelay) {
console.debug(`Average framerate for the application rendering: ${average.toFixed(2)} fps.`)
lastAppAverageFpsLog = new Date()
}
}

requestAnimationFrame(loop)
})
}
fpsMeter()
// // Routine to log the framerate of the video streams
// const streamsFrameRateHistory = ref<{ [key in string]: number[] }>({})
// let lastStreamAverageFramerateLog = new Date()
// const streamAverageFramerateLogDelay = 10000
// setInterval(() => {
// Object.keys(videoStore.activeStreams).forEach((streamName) => {
// if (videoStore.activeStreams[streamName] === undefined) return
// videoStore.activeStreams[streamName]!.mediaStream?.getVideoTracks().forEach((track) => {
// if (streamsFrameRateHistory.value[streamName] === undefined) streamsFrameRateHistory.value[streamName] = []
// if (track.getSettings().frameRate === undefined) return

// const streamHistory = streamsFrameRateHistory.value[streamName]
// streamHistory.push(track.getSettings().frameRate as number)
// streamHistory.splice(0, streamHistory.length - 10)
// streamsFrameRateHistory.value[streamName] = streamHistory

// const average = streamHistory.reduce((a, b) => a + b, 0) / streamHistory.length
// const minThreshold = 0.9 * average
// const newFrameRate = track.getSettings().frameRate as number

// // Warn about drops in the framerate of the video stream
// if (newFrameRate < minThreshold) {
// console.warn(`Drop in the framerate detected for stream '${streamName}': ${newFrameRate.toFixed(2)} fps.`)
// }

// // Log the average framerate of the video stream recursively
// if (new Date().getTime() - lastStreamAverageFramerateLog.getTime() > streamAverageFramerateLogDelay) {
// console.debug(`Average frame rate for stream '${streamName}': ${average.toFixed(2)} fps.`)
// lastStreamAverageFramerateLog = new Date()
// }
// })
// })
// }, 250)

// // Routine to log the framerate of the application rendering
// const appFrameRateHistory = ref<number[]>([])
// const appAverageFrameRateSampleDelay = 100
// const appAverageFrameRateLogDelay = 10000
// let lastAppAverageFpsLog = new Date()

// // Log tab visibility changes so we don't warn about framerate drops when the tab was not visible in the last seconds
// const windowVisibility = useDocumentVisibility()
// let timeLastTabOpening = new Date()
// watch(windowVisibility, (newVisibility, oldVisibility) => {
// if (newVisibility === 'visible' && oldVisibility === 'hidden') {
// timeLastTabOpening = new Date()
// }
// })

// const fpsMeter = (): void => {
// let prevTime = performance.now()
// let frames = 0

// requestAnimationFrame(function loop() {
// const time = performance.now()
// frames++
// if (time > prevTime + appAverageFrameRateSampleDelay) {
// const currentFPS = Math.round((frames * 1000) / (time - prevTime))
// prevTime = time
// frames = 0

// appFrameRateHistory.value.push(currentFPS)
// appFrameRateHistory.value.splice(0, appFrameRateHistory.value.length - 10)

// const average = appFrameRateHistory.value.reduce((a, b) => a + b, 0) / appFrameRateHistory.value.length

// // Warn about drops in the framerate of the application rendering
// // The threshold is set to 80% of the average framerate by default
// // We don't warn if the framerate is above 60 because it's an already very high value, and we don't want to spam the console on high-end monitors
// // We also don't warn if the tab was not visible in the last second, since the application was not being rendered
// const minThreshold = Math.min(60, 0.8 * average)
// const msSinceLastTabOpening = new Date().getTime() - timeLastTabOpening.getTime()
// if (currentFPS < minThreshold && msSinceLastTabOpening > 1000) {
// console.warn(`Drop in the framerate detected for the application rendering: ${currentFPS.toFixed(2)} fps.`)
// }

// // Log the average framerate of the application rendering recursively
// if (new Date().getTime() - lastAppAverageFpsLog.getTime() > appAverageFrameRateLogDelay) {
// console.debug(`Average framerate for the application rendering: ${average.toFixed(2)} fps.`)
// lastAppAverageFpsLog = new Date()
// }
// }

// requestAnimationFrame(loop)
// })
// }
// fpsMeter()

// Routine to log the WebRTC statistics

Expand Down

0 comments on commit df5de03

Please sign in to comment.