From fc26aaa15322558bc7f7600cbbd310a323a80c5b Mon Sep 17 00:00:00 2001 From: Brikend Date: Mon, 23 Aug 2021 15:20:36 +0200 Subject: [PATCH] fix: Handle Huddly BASE devices properly Since there is no transport implementation for Huddly BASE adapter, we need to make sure that device-api-usb exits immediately when such device is sent over for usb transport validation. Also refactoring the usage of huddly VID and PID by referencing their values from SDK instead of hardcoding them here. --- src/index.ts | 12 ++++++++++-- src/manager.ts | 6 +++--- tests/bulkusbdeviceSingleton.spec.ts | 3 +-- tests/manager.spec.ts | 10 +++++----- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8c7b706..c418992 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,11 @@ import IDeviceDiscovery from '@huddly/sdk/lib/src/interfaces/iDeviceDiscovery'; import DeviceApiOpts from '@huddly/sdk/lib/src/interfaces/IDeviceApiOpts'; import DeviceDiscoveryManager from './manager'; import Logger from '@huddly/sdk/lib/src/utilitis/logger'; +import { + HUDDLY_GO_PID, + HUDDLY_L1_PID, + HUDDLY_BASE_PID, +} from '@huddly/sdk/lib/src/components/device/factory'; export default class HuddlyDeviceAPIUSB implements IHuddlyDeviceAPI { eventEmitter: EventEmitter; @@ -32,8 +37,11 @@ export default class HuddlyDeviceAPIUSB implements IHuddlyDeviceAPI { } async getValidatedTransport(device): Promise { - if (device.productId === 0x11 || device.productId === 3e9) { - Logger.warn('HLink is not supported for this Huddly device', 'Device API USB'); + if ([HUDDLY_GO_PID, HUDDLY_L1_PID, HUDDLY_BASE_PID].includes(device.productId)) { + Logger.warn( + `HLink is not supported for Huddly device with PID ${device.productId}`, + 'Device API USB' + ); return undefined; } try { diff --git a/src/manager.ts b/src/manager.ts index b1d94a0..6e48cd6 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -2,9 +2,9 @@ import BulkUsb, { BulkUsbDevice } from './bulkusbdevice'; import EventEmitter from 'events'; import IDeviceDiscovery from '@huddly/sdk/lib/src/interfaces/iDeviceDiscovery'; import Logger from '@huddly/sdk/lib/src/utilitis/logger'; +import { HUDDLY_VID } from '@huddly/sdk/lib/src/components/device/factory'; export default class DeviceDiscoveryManager implements IDeviceDiscovery { - readonly HUDDLY_VID: number = 0x2bd9; private attachedDevices: Array = []; eventEmitter: EventEmitter; pollInterval: any; @@ -31,7 +31,7 @@ export default class DeviceDiscoveryManager implements IDeviceDiscovery { discoverCameras(): void {} private deviceAttached(attachedDevice): void { - if (attachedDevice.vid !== this.HUDDLY_VID) { + if (attachedDevice.vid !== HUDDLY_VID) { return; } const newDevice = this.getDeviceObject(attachedDevice); @@ -42,7 +42,7 @@ export default class DeviceDiscoveryManager implements IDeviceDiscovery { } private deviceDetached(removedDevice): void { - if (removedDevice.vid !== this.HUDDLY_VID) { + if (removedDevice.vid !== HUDDLY_VID) { return; } this.attachedDevices = this.attachedDevices.filter((d) => !removedDevice.equals(d)); diff --git a/tests/bulkusbdeviceSingleton.spec.ts b/tests/bulkusbdeviceSingleton.spec.ts index 7ac2418..90482c4 100644 --- a/tests/bulkusbdeviceSingleton.spec.ts +++ b/tests/bulkusbdeviceSingleton.spec.ts @@ -136,8 +136,7 @@ describe('BulkUsbDeviceSingleton', () => { }); // Two list loops await sleep(251); - dummyCpp.listDevices.yields([ - ]); + dummyCpp.listDevices.yields([]); await sleep(251); expect(detachSpy).to.have.been.calledOnce; }); diff --git a/tests/manager.spec.ts b/tests/manager.spec.ts index 4ab59e3..444ddd2 100644 --- a/tests/manager.spec.ts +++ b/tests/manager.spec.ts @@ -4,7 +4,7 @@ import sinonChai from 'sinon-chai'; import BulkUsb from './../src/bulkusbdevice'; import DeviceDiscoveryManager from './../src/manager'; import { EventEmitter } from 'events'; -import { doesNotReject } from 'assert'; +import { HUDDLY_VID } from '@huddly/sdk/lib/src/components/device/factory'; chai.should(); chai.use(sinonChai); @@ -150,7 +150,7 @@ describe('HuddlyUsbDeviceManager', () => { done(); }); devicemanager.registerForHotplugEvents(emitter); - attachStub.callArgWith(0, { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: devicemanager.HUDDLY_VID, onDetach: () => {}}); + attachStub.callArgWith(0, { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: HUDDLY_VID, onDetach: () => {}}); }); it('should fire detach events for all undiscovered cached cameras', (done) => { @@ -159,8 +159,8 @@ describe('HuddlyUsbDeviceManager', () => { done(); }); devicemanager.registerForHotplugEvents(emitter); - attachStub.callArgWith(0, { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: devicemanager.HUDDLY_VID, onDetach: (cb) => { - cb({ id: '456', serialNumber: 'SRL456', pid: 0x21, vid: devicemanager.HUDDLY_VID }); + attachStub.callArgWith(0, { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: HUDDLY_VID, onDetach: (cb) => { + cb({ id: '456', serialNumber: 'SRL456', pid: 0x21, vid: HUDDLY_VID }); }}); }); @@ -173,7 +173,7 @@ describe('HuddlyUsbDeviceManager', () => { * This device will serve as a detached device. */ listDeviceStub = sinon.stub(BulkUsb, 'listDevices').resolves([ - { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: devicemanager.HUDDLY_VID, onDetach: (cb) => {} }, + { id: '123', serialNumber: 'SRL123', pid: 0x21, vid: HUDDLY_VID, onDetach: (cb) => {} }, ]); });