UNPKG

@4players/odin

Version:

A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects

51 lines (50 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mediaTimings = void 0; exports.registerAudioOutput = registerAudioOutput; exports.unregisterAudioOutput = unregisterAudioOutput; exports.mediaTimings = { ACTIVITY_INTERVAL: 100, POWER_LEVEL_INTERVAL: 500, JITTER_STATS_INTERVAL: 1000, }; const audioOutputs = new Map(); const timers = new Set(); function registerAudioOutput(output, handler, room) { audioOutputs.set(output, { handler, room }); if (timers.size === 0) { startLoops(); } } function unregisterAudioOutput(output) { audioOutputs.delete(output); if (audioOutputs.size === 0) { stopLoops(); } } function emit(event) { for (const [, data] of audioOutputs) { data.handler(event); } } function scheduleLoop(event, getInterval) { const tick = () => { timers.delete(currentTimer); emit(event); currentTimer = setTimeout(tick, getInterval()); timers.add(currentTimer); }; let currentTimer = setTimeout(tick, getInterval()); timers.add(currentTimer); } function startLoops() { scheduleLoop('Activity', () => exports.mediaTimings.ACTIVITY_INTERVAL); scheduleLoop('PowerLevel', () => exports.mediaTimings.POWER_LEVEL_INTERVAL); scheduleLoop('JitterStats', () => exports.mediaTimings.JITTER_STATS_INTERVAL); } function stopLoops() { for (const timer of timers) { clearTimeout(timer); } timers.clear(); }