Skip to content

Commit

Permalink
feat: Disable AudioBasedLatency on non-Android-Chrome platforms (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-stormtrooper committed Jun 5, 2024
1 parent 416b178 commit d33f3db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/sources/audio_base_latency.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { getBrowserVersion, isGecko, isWebKit } from '../../tests/utils'
import getAudioBaseLatency from './audio_base_latency'
import { getBrowserVersion, isAndroid, isGecko, isWebKit } from '../../tests/utils'
import getAudioBaseLatency, { SpecialFingerprint } from './audio_base_latency'

describe('Sources', () => {
describe('audioBaseLatency', () => {
it("doesn't fail", () => {
const result = getAudioBaseLatency()
const isAllowedPlatform = isAndroid() || isWebKit()
if (!isAllowedPlatform) {
expect(result).toBe(SpecialFingerprint.Disabled)
return
}

if (!hasBaseLatencySupport()) {
expect(result).toBe(undefined)
expect(result).toBe(SpecialFingerprint.NotSupported)
return
}

Expand Down
23 changes: 19 additions & 4 deletions src/sources/audio_base_latency.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
export default function getAudioContextBaseLatency(): number | undefined {
if (!window.AudioContext) {
return undefined
import { isAndroid, isWebKit } from '../utils/browser'

export const enum SpecialFingerprint {
/** The browser doesn't support AudioContext or baseLatency */
NotSupported = -1,
/** Entropy source is disabled because of console warnings */
Disabled = -2,
}

export default function getAudioContextBaseLatency(): number {
// The signal emits warning in Chrome and Firefox, therefore it is enabled on Safari where it doesn't produce warning
// and on Android where it's less visible
const isAllowedPlatform = isAndroid() || isWebKit()
if (!isAllowedPlatform) {
return SpecialFingerprint.Disabled
}

return new AudioContext().baseLatency
if (!window.AudioContext) {
return SpecialFingerprint.NotSupported
}
return new AudioContext().baseLatency ?? SpecialFingerprint.NotSupported
}

0 comments on commit d33f3db

Please sign in to comment.