From e147fd9ca17c1eb71b6ca7c3de709fb3563e49a1 Mon Sep 17 00:00:00 2001 From: KaustubhKumar05 Date: Tue, 19 Nov 2024 15:46:56 +0530 Subject: [PATCH 01/12] fix: use existing audio context --- .../plugins/audio/HMSAudioPluginsManager.ts | 3 +- .../src/reactive-store/HMSSDKActions.ts | 35 ++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts b/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts index 75100bb9a5..29da67abae 100644 --- a/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts +++ b/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts @@ -4,6 +4,7 @@ import AnalyticsEventFactory from '../../analytics/AnalyticsEventFactory'; import { ErrorFactory } from '../../error/ErrorFactory'; import { HMSAction } from '../../error/HMSAction'; import { EventBus } from '../../events/EventBus'; +import { HMSAudioContextHandler } from '../../internal'; import { HMSLocalAudioTrack } from '../../media/tracks'; import Room from '../../sdk/models/HMSRoom'; import HMSLogger from '../../utils/logger'; @@ -48,7 +49,7 @@ export class HMSAudioPluginsManager { this.hmsTrack = track; this.pluginsMap = new Map(); this.analytics = new AudioPluginsAnalytics(eventBus); - this.createAudioContext(); + this.audioContext = HMSAudioContextHandler.getAudioContext(); this.room = room; } diff --git a/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts b/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts index 9b880b70f7..7fb0960e47 100644 --- a/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts +++ b/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts @@ -1534,25 +1534,28 @@ export class HMSSDKActions Date: Tue, 19 Nov 2024 15:48:18 +0530 Subject: [PATCH 02/12] fix: catch audio plugin errors --- packages/hms-video-store/src/reactive-store/HMSSDKActions.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts b/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts index 7fb0960e47..2d82826750 100644 --- a/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts +++ b/packages/hms-video-store/src/reactive-store/HMSSDKActions.ts @@ -1555,7 +1555,9 @@ export class HMSSDKActions Date: Tue, 19 Nov 2024 16:06:32 +0530 Subject: [PATCH 03/12] fix: types --- .../plugins/audio/HMSAudioPluginsManager.ts | 22 +------------------ packages/hms-video-store/src/utils/media.ts | 19 +++++++++++++--- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts b/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts index 29da67abae..212774b513 100644 --- a/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts +++ b/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts @@ -11,11 +11,6 @@ import HMSLogger from '../../utils/logger'; const DEFAULT_SAMPLE_RATE = 48000; -//Handling sample rate error in case of firefox -const checkBrowserSupport = () => { - return navigator.userAgent.indexOf('Firefox') !== -1; -}; - /** * This class manages applying different plugins on a local audio track. Plugins which need to modify the audio * are called in the order they were added. Plugins which do not need to modify the audio are called @@ -49,7 +44,7 @@ export class HMSAudioPluginsManager { this.hmsTrack = track; this.pluginsMap = new Map(); this.analytics = new AudioPluginsAnalytics(eventBus); - this.audioContext = HMSAudioContextHandler.getAudioContext(); + this.audioContext = HMSAudioContextHandler.getAudioContext({ sampleRate: DEFAULT_SAMPLE_RATE }); this.room = room; } @@ -283,19 +278,4 @@ export class HMSAudioPluginsManager { plugin.stop(); this.analytics.removed(name); } - - private createAudioContext() { - if (!this.audioContext) { - if (checkBrowserSupport()) { - /** - Not setting default sample rate for firefox since connecting - audio nodes from context with different sample rate is not - supported in firefox - */ - this.audioContext = new AudioContext(); - } else { - this.audioContext = new AudioContext({ sampleRate: DEFAULT_SAMPLE_RATE }); - } - } - } } diff --git a/packages/hms-video-store/src/utils/media.ts b/packages/hms-video-store/src/utils/media.ts index 05d60e443f..cb2979994f 100644 --- a/packages/hms-video-store/src/utils/media.ts +++ b/packages/hms-video-store/src/utils/media.ts @@ -2,6 +2,11 @@ import HMSLogger from './logger'; import { BuildGetMediaError } from '../error/utils'; import { HMSTrackExceptionTrackType } from '../media/tracks/HMSTrackExceptionTrackType'; +//Handling sample rate error in case of firefox +const checkBrowserSupport = () => { + return navigator.userAgent.indexOf('Firefox') !== -1; +}; + export async function getLocalStream(constraints: MediaStreamConstraints): Promise { try { const stream = await navigator.mediaDevices.getUserMedia(constraints); @@ -48,11 +53,19 @@ export interface HMSAudioContext { resumeContext: () => Promise; } -export const HMSAudioContextHandler: HMSAudioContext = { +export const HMSAudioContextHandler: { + audioContext: AudioContext | null; + getAudioContext(options?: AudioContextOptions): AudioContext; + resumeContext(): Promise; +} = { audioContext: null, - getAudioContext() { + getAudioContext(options?: AudioContextOptions) { if (!this.audioContext) { - this.audioContext = new AudioContext(); + if (checkBrowserSupport()) { + this.audioContext = new AudioContext(); + } else { + this.audioContext = new AudioContext(options); + } } return this.audioContext; }, From c580c14798ab77111ce6b5540fe469e69b326785 Mon Sep 17 00:00:00 2001 From: KaustubhKumar05 Date: Tue, 19 Nov 2024 16:16:51 +0530 Subject: [PATCH 04/12] fix: re-add comment --- packages/hms-video-store/src/utils/media.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/hms-video-store/src/utils/media.ts b/packages/hms-video-store/src/utils/media.ts index cb2979994f..ef9b280da5 100644 --- a/packages/hms-video-store/src/utils/media.ts +++ b/packages/hms-video-store/src/utils/media.ts @@ -62,6 +62,11 @@ export const HMSAudioContextHandler: { getAudioContext(options?: AudioContextOptions) { if (!this.audioContext) { if (checkBrowserSupport()) { + /** + Not setting default sample rate for firefox since connecting + audio nodes from context with different sample rate is not + supported in firefox + */ this.audioContext = new AudioContext(); } else { this.audioContext = new AudioContext(options); From 34e1c4efdaa788fc6e72ad4f8f3f037c366df2ad Mon Sep 17 00:00:00 2001 From: KaustubhKumar05 Date: Tue, 19 Nov 2024 16:35:28 +0530 Subject: [PATCH 05/12] fix: resume context in unblockautoplay --- .../src/audio-sink-manager/AudioSinkManager.ts | 2 ++ packages/hms-video-store/src/utils/media.ts | 16 ++++++---------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts index 9359b0b88e..184b02267f 100644 --- a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts +++ b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts @@ -5,6 +5,7 @@ import { ErrorFactory } from '../error/ErrorFactory'; import { HMSAction } from '../error/HMSAction'; import { EventBus } from '../events/EventBus'; import { HMSDeviceChangeEvent, HMSTrackUpdate, HMSUpdateListener } from '../interfaces'; +import { HMSAudioContextHandler } from '../internal'; import { HMSRemoteAudioTrack } from '../media/tracks'; import { HMSRemotePeer } from '../sdk/models/peer'; import { Store } from '../sdk/store'; @@ -72,6 +73,7 @@ export class AudioSinkManager { */ async unblockAutoplay() { if (this.autoPausedTracks.size > 0) { + HMSAudioContextHandler.resumeContext(); this.unpauseAudioTracks(); } } diff --git a/packages/hms-video-store/src/utils/media.ts b/packages/hms-video-store/src/utils/media.ts index ef9b280da5..73d0ac4664 100644 --- a/packages/hms-video-store/src/utils/media.ts +++ b/packages/hms-video-store/src/utils/media.ts @@ -1,12 +1,8 @@ import HMSLogger from './logger'; +import { isFirefox } from './support'; import { BuildGetMediaError } from '../error/utils'; import { HMSTrackExceptionTrackType } from '../media/tracks/HMSTrackExceptionTrackType'; -//Handling sample rate error in case of firefox -const checkBrowserSupport = () => { - return navigator.userAgent.indexOf('Firefox') !== -1; -}; - export async function getLocalStream(constraints: MediaStreamConstraints): Promise { try { const stream = await navigator.mediaDevices.getUserMedia(constraints); @@ -61,12 +57,12 @@ export const HMSAudioContextHandler: { audioContext: null, getAudioContext(options?: AudioContextOptions) { if (!this.audioContext) { - if (checkBrowserSupport()) { + if (isFirefox) { /** - Not setting default sample rate for firefox since connecting - audio nodes from context with different sample rate is not - supported in firefox - */ + * Not setting default sample rate for firefox since connecting + * audio nodes from context with different sample rate is not + * supported in firefox + */ this.audioContext = new AudioContext(); } else { this.audioContext = new AudioContext(options); From c9202842faf01c072dfafd5c9508440658b0a463 Mon Sep 17 00:00:00 2001 From: KaustubhKumar05 Date: Tue, 19 Nov 2024 16:43:11 +0530 Subject: [PATCH 06/12] fix: await async methods --- .../src/audio-sink-manager/AudioSinkManager.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts index 184b02267f..35264f3328 100644 --- a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts +++ b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts @@ -73,8 +73,8 @@ export class AudioSinkManager { */ async unblockAutoplay() { if (this.autoPausedTracks.size > 0) { - HMSAudioContextHandler.resumeContext(); - this.unpauseAudioTracks(); + await HMSAudioContextHandler.resumeContext(); + await this.unpauseAudioTracks(); } } From 7fa8f2bab0e3aa4626483ddbee95ff139816eb41 Mon Sep 17 00:00:00 2001 From: KaustubhKumar05 Date: Tue, 19 Nov 2024 17:36:14 +0530 Subject: [PATCH 07/12] fix: create new context if sample rate differs --- packages/hms-video-store/src/utils/media.ts | 24 ++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/hms-video-store/src/utils/media.ts b/packages/hms-video-store/src/utils/media.ts index 73d0ac4664..2d4924b418 100644 --- a/packages/hms-video-store/src/utils/media.ts +++ b/packages/hms-video-store/src/utils/media.ts @@ -56,19 +56,19 @@ export const HMSAudioContextHandler: { } = { audioContext: null, getAudioContext(options?: AudioContextOptions) { - if (!this.audioContext) { - if (isFirefox) { - /** - * Not setting default sample rate for firefox since connecting - * audio nodes from context with different sample rate is not - * supported in firefox - */ - this.audioContext = new AudioContext(); - } else { - this.audioContext = new AudioContext(options); - } + const newAudioContextNeeded = + !this.audioContext || (options?.sampleRate && this.audioContext.sampleRate !== options.sampleRate); + + if (newAudioContextNeeded) { + /** + * Not setting default sample rate for firefox since connecting + * audio nodes from context with different sample rate is not + * supported in firefox + */ + this.audioContext = isFirefox ? new AudioContext() : new AudioContext(options); } - return this.audioContext; + + return this.audioContext!; }, async resumeContext() { try { From 70ca28e82c9da96679fe3f5cf9a957744204cc28 Mon Sep 17 00:00:00 2001 From: KaustubhKumar05 Date: Tue, 19 Nov 2024 18:47:11 +0530 Subject: [PATCH 08/12] fix: missing awaits --- .../src/audio-sink-manager/AudioSinkManager.ts | 2 -- .../src/plugins/audio/HMSAudioPluginsManager.ts | 2 +- packages/hms-video-store/src/utils/media.ts | 8 ++------ .../components/Notifications/AutoplayBlockedModal.tsx | 8 ++++---- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts index 35264f3328..fcd4593071 100644 --- a/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts +++ b/packages/hms-video-store/src/audio-sink-manager/AudioSinkManager.ts @@ -5,7 +5,6 @@ import { ErrorFactory } from '../error/ErrorFactory'; import { HMSAction } from '../error/HMSAction'; import { EventBus } from '../events/EventBus'; import { HMSDeviceChangeEvent, HMSTrackUpdate, HMSUpdateListener } from '../interfaces'; -import { HMSAudioContextHandler } from '../internal'; import { HMSRemoteAudioTrack } from '../media/tracks'; import { HMSRemotePeer } from '../sdk/models/peer'; import { Store } from '../sdk/store'; @@ -73,7 +72,6 @@ export class AudioSinkManager { */ async unblockAutoplay() { if (this.autoPausedTracks.size > 0) { - await HMSAudioContextHandler.resumeContext(); await this.unpauseAudioTracks(); } } diff --git a/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts b/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts index 212774b513..93f378890d 100644 --- a/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts +++ b/packages/hms-video-store/src/plugins/audio/HMSAudioPluginsManager.ts @@ -210,7 +210,7 @@ export class HMSAudioPluginsManager { for (const plugin of plugins) { await this.addPlugin(plugin); } - this.updateProcessedTrack(); + await this.updateProcessedTrack(); } private async initAudioNodes() { diff --git a/packages/hms-video-store/src/utils/media.ts b/packages/hms-video-store/src/utils/media.ts index 2d4924b418..abd26a4b66 100644 --- a/packages/hms-video-store/src/utils/media.ts +++ b/packages/hms-video-store/src/utils/media.ts @@ -45,15 +45,11 @@ export async function getLocalDevices(): Promise { export interface HMSAudioContext { audioContext: AudioContext | null; - getAudioContext: () => AudioContext; + getAudioContext: (options?: AudioContextOptions) => AudioContext; resumeContext: () => Promise; } -export const HMSAudioContextHandler: { - audioContext: AudioContext | null; - getAudioContext(options?: AudioContextOptions): AudioContext; - resumeContext(): Promise; -} = { +export const HMSAudioContextHandler: HMSAudioContext = { audioContext: null, getAudioContext(options?: AudioContextOptions) { const newAudioContextNeeded = diff --git a/packages/roomkit-react/src/Prebuilt/components/Notifications/AutoplayBlockedModal.tsx b/packages/roomkit-react/src/Prebuilt/components/Notifications/AutoplayBlockedModal.tsx index ecd2a93f98..a8ba68b958 100644 --- a/packages/roomkit-react/src/Prebuilt/components/Notifications/AutoplayBlockedModal.tsx +++ b/packages/roomkit-react/src/Prebuilt/components/Notifications/AutoplayBlockedModal.tsx @@ -9,9 +9,9 @@ export function AutoplayBlockedModal() { return ( { + onOpenChange={async value => { if (!value) { - unblockAudio(); + await unblockAudio(); } resetError(); }} @@ -25,8 +25,8 @@ export function AutoplayBlockedModal() {