Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reuse audio context #3379

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -72,6 +73,7 @@ export class AudioSinkManager {
*/
async unblockAutoplay() {
if (this.autoPausedTracks.size > 0) {
HMSAudioContextHandler.resumeContext();
KaustubhKumar05 marked this conversation as resolved.
Show resolved Hide resolved
this.unpauseAudioTracks();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ 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';

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
Expand Down Expand Up @@ -48,7 +44,7 @@ export class HMSAudioPluginsManager {
this.hmsTrack = track;
this.pluginsMap = new Map();
this.analytics = new AudioPluginsAnalytics(eventBus);
this.createAudioContext();
this.audioContext = HMSAudioContextHandler.getAudioContext({ sampleRate: DEFAULT_SAMPLE_RATE });
this.room = room;
}

Expand Down Expand Up @@ -282,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 });
}
}
}
}
35 changes: 20 additions & 15 deletions packages/hms-video-store/src/reactive-store/HMSSDKActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1534,24 +1534,29 @@ export class HMSSDKActions<T extends HMSGenericTypes = { sessionStore: Record<st
}
}

//eslint-disable-next-line complexity
private async addRemoveAudioPlugin(plugin: HMSAudioPlugin, action: 'add' | 'remove') {
if (!plugin) {
HMSLogger.w('Invalid plugin received in store');
return;
}
const trackID = this.store.getState(selectLocalAudioTrackID);
if (trackID) {
const sdkTrack = this.getLocalTrack(trackID);
if (sdkTrack) {
if (action === 'add') {
await (sdkTrack as SDKHMSLocalAudioTrack).addPlugin(plugin);
} else if (action === 'remove') {
await (sdkTrack as SDKHMSLocalAudioTrack).removePlugin(plugin);
try {
if (!plugin) {
HMSLogger.w('Invalid plugin received in store');
return;
}
const trackID = this.store.getState(selectLocalAudioTrackID);
if (trackID) {
const sdkTrack = this.getLocalTrack(trackID);
if (sdkTrack) {
if (action === 'add') {
await (sdkTrack as SDKHMSLocalAudioTrack).addPlugin(plugin);
} else if (action === 'remove') {
await (sdkTrack as SDKHMSLocalAudioTrack).removePlugin(plugin);
}
this.syncRoomState(`${action}AudioPlugin`);
} else {
this.logPossibleInconsistency(`track ${trackID} not present, unable to ${action} plugin`);
}
this.syncRoomState(`${action}AudioPlugin`);
} else {
this.logPossibleInconsistency(`track ${trackID} not present, unable to ${action} plugin`);
}
} catch (err) {
console.error(err);
}
}

Expand Down
20 changes: 17 additions & 3 deletions packages/hms-video-store/src/utils/media.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import HMSLogger from './logger';
import { isFirefox } from './support';
import { BuildGetMediaError } from '../error/utils';
import { HMSTrackExceptionTrackType } from '../media/tracks/HMSTrackExceptionTrackType';

Expand Down Expand Up @@ -48,11 +49,24 @@ export interface HMSAudioContext {
resumeContext: () => Promise<void>;
}

export const HMSAudioContextHandler: HMSAudioContext = {
export const HMSAudioContextHandler: {
audioContext: AudioContext | null;
getAudioContext(options?: AudioContextOptions): AudioContext;
resumeContext(): Promise<void>;
} = {
audioContext: null,
getAudioContext() {
getAudioContext(options?: AudioContextOptions) {
if (!this.audioContext) {
this.audioContext = new 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);
KaustubhKumar05 marked this conversation as resolved.
Show resolved Hide resolved
}
}
return this.audioContext;
},
Expand Down