Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion packages/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export * from './src/helpers/DynascaleManager';
export * from './src/helpers/ViewportTracker';
export * from './src/helpers/sound-detector';
export * from './src/helpers/participantUtils';
export * from './src/helpers/RNSpeechDetector';
export * as Browsers from './src/helpers/browsers';

export * from './src/logger';
14 changes: 9 additions & 5 deletions packages/client/src/devices/MicrophoneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
createSafeAsyncSubscription,
createSubscription,
} from '../store/rxUtils';
import { RNSpeechDetector } from '../helpers/RNSpeechDetector';
import { withoutConcurrency } from '../helpers/concurrency';
import { disposeOfMediaStream } from './utils';
import { promiseWithResolvers } from '../helpers/promise';
Expand All @@ -36,7 +35,6 @@ export class MicrophoneManager extends AudioDeviceManager<MicrophoneManagerState
private soundDetectorCleanup?: () => Promise<void>;
private soundDetectorDeviceId?: string;
private noAudioDetectorCleanup?: () => Promise<void>;
private rnSpeechDetector: RNSpeechDetector | undefined;
private noiseCancellation: INoiseCancellation | undefined;
private noiseCancellationChangeUnsubscribe: (() => void) | undefined;
private noiseCancellationRegistration?: Promise<void>;
Expand Down Expand Up @@ -422,13 +420,19 @@ export class MicrophoneManager extends AudioDeviceManager<MicrophoneManagerState
await this.teardownSpeakingWhileMutedDetection();

if (isReactNative()) {
this.rnSpeechDetector = new RNSpeechDetector();
const unsubscribe = await this.rnSpeechDetector.start((event) => {
const speechActivity =
globalThis.streamRNVideoSDK?.nativeEvents?.speechActivity;
if (!speechActivity) {
this.logger.warn(
'Native speech activity not available, make sure the "@stream-io/react-native-webrtc" peer dependency version is satisfied',
);
return;
}
const unsubscribe = speechActivity.subscribe((event) => {
this.state.setSpeakingWhileMuted(event.isSoundDetected);
});
this.soundDetectorCleanup = async () => {
unsubscribe();
this.rnSpeechDetector = undefined;
};
} else {
// Need to start a new stream that's not connected to publisher
Expand Down
57 changes: 28 additions & 29 deletions packages/client/src/devices/__tests__/MicrophoneManagerRN.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import {
import { of } from 'rxjs';
import '../../rtc/__tests__/mocks/webrtc.mocks';
import { OwnCapability } from '../../gen/coordinator';
import { SoundStateChangeHandler } from '../../helpers/sound-detector';
import { settled, withoutConcurrency } from '../../helpers/concurrency';

let handler: SoundStateChangeHandler = () => {};
let unsubscribeHandlers: ReturnType<typeof vi.fn>[] = [];
let speechActivityCallback:
| ((state: { isSoundDetected: boolean }) => void)
| null = null;
let unsubscribeMocks: ReturnType<typeof vi.fn>[] = [];

vi.mock('../../helpers/platforms.ts', () => {
return {
Expand Down Expand Up @@ -46,28 +47,21 @@ vi.mock('../../Call.ts', () => {
};
});

vi.mock('../../helpers/RNSpeechDetector.ts', () => {
console.log('MOCKING RNSpeechDetector');
return {
RNSpeechDetector: vi.fn().mockImplementation(() => ({
start: vi.fn((callback) => {
handler = callback;
const unsubscribe = vi.fn();
unsubscribeHandlers.push(unsubscribe);
return unsubscribe;
}),
stop: vi.fn(),
onSpeakingDetectedStateChange: vi.fn(),
})),
};
});

describe('MicrophoneManager React Native', () => {
let manager: MicrophoneManager;
let checkPermissionMock: ReturnType<typeof vi.fn>;
let subscribeMock: ReturnType<typeof vi.fn>;

beforeEach(() => {
unsubscribeHandlers = [];
speechActivityCallback = null;
unsubscribeMocks = [];
checkPermissionMock = vi.fn(async () => true);
subscribeMock = vi.fn((cb) => {
speechActivityCallback = cb;
const unsub = vi.fn();
unsubscribeMocks.push(unsub);
return unsub;
});

globalThis.streamRNVideoSDK = {
callManager: {
Expand All @@ -78,6 +72,11 @@ describe('MicrophoneManager React Native', () => {
permissions: {
check: checkPermissionMock,
},
nativeEvents: {
speechActivity: {
subscribe: subscribeMock,
},
},
};

const devicePersistence = { enabled: false, storageKey: '' };
Expand All @@ -100,7 +99,7 @@ describe('MicrophoneManager React Native', () => {

await vi.waitUntil(() => fn.mock.calls.length > 0, { timeout: 100 });
expect(fn).toHaveBeenCalled();
expect(manager['rnSpeechDetector']?.start).toHaveBeenCalled();
expect(subscribeMock).toHaveBeenCalled();
});

it('should check native microphone permission before starting detection', async () => {
Expand Down Expand Up @@ -146,15 +145,15 @@ describe('MicrophoneManager React Native', () => {

it('should update speaking while muted state', async () => {
await manager['startSpeakingWhileMutedDetection']();
expect(manager['rnSpeechDetector']?.start).toHaveBeenCalled();
expect(subscribeMock).toHaveBeenCalled();

expect(manager.state.speakingWhileMuted).toBe(false);

handler!({ isSoundDetected: true, audioLevel: 2 });
speechActivityCallback!({ isSoundDetected: true });

expect(manager.state.speakingWhileMuted).toBe(true);

handler!({ isSoundDetected: false, audioLevel: 0 });
speechActivityCallback!({ isSoundDetected: false });

expect(manager.state.speakingWhileMuted).toBe(false);
});
Expand All @@ -163,21 +162,21 @@ describe('MicrophoneManager React Native', () => {
await manager['startSpeakingWhileMutedDetection']('device-1');
await manager['startSpeakingWhileMutedDetection']('device-1');

expect(unsubscribeHandlers).toHaveLength(1);
expect(unsubscribeMocks).toHaveLength(1);

await manager['stopSpeakingWhileMutedDetection']();
expect(unsubscribeHandlers[0]).toHaveBeenCalledTimes(1);
expect(unsubscribeMocks[0]).toHaveBeenCalledTimes(1);
});

it('should cleanup previous speech detector before starting a new one', async () => {
await manager['startSpeakingWhileMutedDetection']('device-1');
await manager['startSpeakingWhileMutedDetection']('device-2');

expect(unsubscribeHandlers).toHaveLength(2);
expect(unsubscribeHandlers[0]).toHaveBeenCalledTimes(1);
expect(unsubscribeMocks).toHaveLength(2);
expect(unsubscribeMocks[0]).toHaveBeenCalledTimes(1);

await manager['stopSpeakingWhileMutedDetection']();
expect(unsubscribeHandlers[1]).toHaveBeenCalledTimes(1);
expect(unsubscribeMocks[1]).toHaveBeenCalledTimes(1);
});

it('should stop speaking while muted notifications if user loses permission to send audio', async () => {
Expand Down
224 changes: 0 additions & 224 deletions packages/client/src/helpers/RNSpeechDetector.ts

This file was deleted.

Loading
Loading