fix: Improve audio channel detection for WebRTC (#2280)

- Related: #2191
This commit is contained in:
Dermot Duffy
2025-12-11 05:48:43 -08:00
committed by GitHub
parent f64690335b
commit 588ed6c98a
2 changed files with 35 additions and 1 deletions
+4
View File
@@ -16,5 +16,9 @@ export const mayHaveAudio = (video: HTMLVideoElement & AudioProperties): boolean
if (video.audioTracks !== undefined) { if (video.audioTracks !== undefined) {
return Boolean(video.audioTracks?.length); return Boolean(video.audioTracks?.length);
} }
// Check MediaStream audio tracks (reliable for WebRTC at load time)
if (typeof MediaStream !== 'undefined' && video.srcObject instanceof MediaStream) {
return video.srcObject.getAudioTracks().length > 0;
}
return true; return true;
}; };
+31 -1
View File
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import { AudioProperties, mayHaveAudio } from '../../src/utils/audio'; import { AudioProperties, mayHaveAudio } from '../../src/utils/audio';
// @vitest-environment jsdom // @vitest-environment jsdom
@@ -29,6 +29,36 @@ describe('mayHaveAudio', () => {
expect(mayHaveAudio(element)).toBeFalsy(); expect(mayHaveAudio(element)).toBeFalsy();
}); });
it('should detect audio when srcObject MediaStream has audio tracks', () => {
// Mock MediaStream for jsdom environment
const MockMediaStream = class MediaStream {};
vi.stubGlobal('MediaStream', MockMediaStream);
const element = {} as HTMLVideoElement & AudioProperties;
const mockStream = new MockMediaStream();
(mockStream as unknown as { getAudioTracks: () => unknown[] }).getAudioTracks =
() => [{}];
element.srcObject = mockStream as unknown as MediaStream;
expect(mayHaveAudio(element)).toBeTruthy();
vi.unstubAllGlobals();
});
it('should not detect audio when srcObject MediaStream has no audio tracks', () => {
// Mock MediaStream for jsdom environment
const MockMediaStream = class MediaStream {};
vi.stubGlobal('MediaStream', MockMediaStream);
const element = {} as HTMLVideoElement & AudioProperties;
const mockStream = new MockMediaStream();
(mockStream as unknown as { getAudioTracks: () => unknown[] }).getAudioTracks =
() => [];
element.srcObject = mockStream as unknown as MediaStream;
expect(mayHaveAudio(element)).toBeFalsy();
vi.unstubAllGlobals();
});
it('should detect audio when no evidence to the contrary', () => { it('should detect audio when no evidence to the contrary', () => {
const element = {} as HTMLVideoElement & AudioProperties; const element = {} as HTMLVideoElement & AudioProperties;
expect(mayHaveAudio(element)).toBeTruthy(); expect(mayHaveAudio(element)).toBeTruthy();