Skip to content

Commit

Permalink
in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Nov 8, 2023
1 parent 4c1803b commit e6ff891
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
12 changes: 8 additions & 4 deletions packages/client/src/devices/InputMediaDeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export abstract class InputMediaDeviceManager<
disablePromise?: Promise<void>;
logger: Logger;
private subscriptions: Subscription[] = [];
private trackEndedTimeout: any;

protected constructor(
protected readonly call: Call,
Expand Down Expand Up @@ -234,7 +235,10 @@ export abstract class InputMediaDeviceManager<
if (this.state.status === 'disabled' || this.disablePromise) {
return;
}
await this.disable();
// Wait before disable, maybe the device was replaced not disconnected
this.trackEndedTimeout = setTimeout(async () => {
await this.disable();
}, 200);
});
});
}
Expand Down Expand Up @@ -284,9 +288,9 @@ export abstract class InputMediaDeviceManager<
await this.disable();
this.select(undefined);
}
if (isDeviceReplaced && this.state.status === 'enabled') {
await this.disable();
await this.enable();
if (isDeviceReplaced) {
clearTimeout(this.trackEndedTimeout);
await this.applySettingsToStream();
}
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ describe('InputMediaDeviceManager.test', () => {
});

it('should set status to disabled if track ends', async () => {
vi.useFakeTimers();

await manager.enable();

vi.spyOn(manager, 'enable');
Expand All @@ -245,9 +247,12 @@ describe('InputMediaDeviceManager.test', () => {
'ended'
] as Function
)();
await vi.runAllTimersAsync();

expect(manager.state.status).toBe('disabled');
expect(manager.enable).not.toHaveBeenCalled();

vi.useRealTimers();
});

it('should restart track if the default device is replaced and status is enabled', async () => {
Expand All @@ -258,8 +263,8 @@ describe('InputMediaDeviceManager.test', () => {
const device = mockVideoDevices[0];
await manager.select(device.deviceId);

vi.spyOn(manager, 'enable');
vi.spyOn(manager, 'disable');
//@ts-expect-error
vi.spyOn(manager, 'applySettingsToStream');

emitDeviceIds([
{ ...device, groupId: device.groupId + 'new' },
Expand All @@ -268,8 +273,7 @@ describe('InputMediaDeviceManager.test', () => {

await vi.runAllTimersAsync();

expect(manager.enable).toHaveBeenCalledOnce();
expect(manager.disable).toHaveBeenCalledOnce();
expect(manager['applySettingsToStream']).toHaveBeenCalledOnce();
expect(manager.state.status).toBe('enabled');

vi.useRealTimers();
Expand All @@ -283,19 +287,16 @@ describe('InputMediaDeviceManager.test', () => {
await manager.select(device.deviceId);
await manager.disable();

vi.spyOn(manager, 'enable');
vi.spyOn(manager, 'disable');

emitDeviceIds([
{ ...device, groupId: device.groupId + 'new' },
...mockVideoDevices.slice(1),
]);

await vi.runAllTimersAsync();

expect(manager.enable).not.toHaveBeenCalledOnce();
expect(manager.disable).not.toHaveBeenCalledOnce();
expect(manager.state.status).toBe('disabled');
expect(manager.disablePromise).toBeUndefined();
expect(manager.enablePromise).toBeUndefined();
expect(manager.state.selectedDevice).toBe(device.deviceId);

vi.useRealTimers();
Expand Down

0 comments on commit e6ff891

Please sign in to comment.