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
5 changes: 5 additions & 0 deletions .changeset/silver-llamas-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@webnect/driver": patch
---

Fix Linux Chromium iso transfer crash on `Camera.setMode`.
4 changes: 4 additions & 0 deletions driver/src/camera/stream/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export enum CamIsoInterface {
CAMERA = 0,
}

export enum CamIsoAltSetting {
CAMERA = 0,
}

export enum CamIsoStreamFlag {
VIDEO = 0b1000,
DEPTH = 0b0111,
Expand Down
27 changes: 27 additions & 0 deletions driver/src/camera/worker/get-device.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CamIsoAltSetting, type CamIsoInterface } from "../stream/enum.js";

export async function prepareCamDevice(
dev: USBDevice,
usbInterface: CamIsoInterface,
): Promise<USBDevice> {
if (!dev.opened) {
await dev.open();
}

const iface = dev.configuration?.interfaces.find(
(i) => i.interfaceNumber === usbInterface,
);
if (!iface) {
throw new ReferenceError(`Interface ${usbInterface} not found`);
}

if (!iface.claimed) {
await dev.claimInterface(usbInterface);
}

if (iface.alternate.alternateSetting !== CamIsoAltSetting.CAMERA) {
await dev.selectAlternateInterface(usbInterface, CamIsoAltSetting.CAMERA);
}

return dev;
}
20 changes: 2 additions & 18 deletions driver/src/camera/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { CamIsoStream } from "../stream/iso-parser.js";
import { UnderlyingIsochronousTransferSource } from "../stream/transfer-source.js";
import { ISOCHRONOUS_BATCH_SIZE } from "./constants.js";
import { prepareCamDevice } from "./get-device.js";
import {
type IsoWorkerRequest,
type IsoWorkerResponse,
Expand Down Expand Up @@ -40,24 +41,7 @@ const getDevice = async (
`Device with serial number ${serialNumber} not found`,
);
}

if (!dev.opened) {
await dev.open();
}

const iface = dev.configuration?.interfaces.find(
(iface) => iface.interfaceNumber === usbInterface,
);

if (!iface) {
throw new ReferenceError(`Interface ${usbInterface} not found`);
}

if (!iface.claimed) {
await dev.claimInterface(usbInterface);
}

return dev;
return prepareCamDevice(dev, usbInterface);
});

return activeDevice;
Expand Down
144 changes: 144 additions & 0 deletions driver/test/prepare-cam-device.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/// <reference types="w3c-web-usb" />
/** biome-ignore-all lint/style/noNonNullAssertion: test code */

import { describe, expect, test, vi } from "vitest";
import {
CamIsoAltSetting,
CamIsoInterface,
} from "../src/camera/stream/enum.js";
import { prepareCamDevice } from "../src/camera/worker/get-device.js";

const makeAlt = (alternateSetting: number): USBAlternateInterface =>
({
alternateSetting,
interfaceClass: 0xff,
interfaceSubclass: 0,
interfaceProtocol: 0,
interfaceName: null,
endpoints: [],
}) as unknown as USBAlternateInterface;

const makeIface = (
interfaceNumber: number,
{ claimed = false, alternateSetting = 0 } = {},
): USBInterface => {
const alt = makeAlt(alternateSetting);
return {
interfaceNumber,
alternate: alt,
alternates: [alt],
claimed,
} as unknown as USBInterface;
};

const makeDevice = (
iface: USBInterface,
{ opened = true } = {},
): {
device: USBDevice;
open: ReturnType<typeof vi.fn<USBDevice["open"]>>;
claimInterface: ReturnType<typeof vi.fn<USBDevice["claimInterface"]>>;
selectAlternateInterface: ReturnType<
typeof vi.fn<USBDevice["selectAlternateInterface"]>
>;
} => {
const open = vi.fn<USBDevice["open"]>().mockResolvedValue(undefined);
const claimInterface = vi
.fn<USBDevice["claimInterface"]>()
.mockResolvedValue(undefined);
const selectAlternateInterface = vi
.fn<USBDevice["selectAlternateInterface"]>()
.mockResolvedValue(undefined);
const device: Partial<USBDevice> = {
opened,
configuration: {
interfaces: [iface],
} as unknown as USBConfiguration,
open,
claimInterface,
selectAlternateInterface,
};
return {
device: device as USBDevice,
open,
claimInterface,
selectAlternateInterface,
};
};

describe("prepareCamDevice", () => {
test("claims then selects target alt when interface is unclaimed and on a different alt", async () => {
const iface = makeIface(CamIsoInterface.CAMERA, {
claimed: false,
alternateSetting: 1,
});
const { device, claimInterface, selectAlternateInterface } =
makeDevice(iface);

await prepareCamDevice(device, CamIsoInterface.CAMERA);

expect(claimInterface).toHaveBeenCalledWith(CamIsoInterface.CAMERA);
expect(selectAlternateInterface).toHaveBeenCalledWith(
CamIsoInterface.CAMERA,
CamIsoAltSetting.CAMERA,
);

const claimOrder = claimInterface.mock.invocationCallOrder[0]!;
const selectOrder = selectAlternateInterface.mock.invocationCallOrder[0]!;
expect(claimOrder).toBeLessThan(selectOrder);
});

test("skips claim when already claimed but still selects target alt if currently on a different alt", async () => {
const iface = makeIface(CamIsoInterface.CAMERA, {
claimed: true,
alternateSetting: 1,
});
const { device, claimInterface, selectAlternateInterface } =
makeDevice(iface);

await prepareCamDevice(device, CamIsoInterface.CAMERA);

expect(claimInterface).not.toHaveBeenCalled();
expect(selectAlternateInterface).toHaveBeenCalledWith(
CamIsoInterface.CAMERA,
CamIsoAltSetting.CAMERA,
);
});

test("skips selectAlternateInterface when device is already on the target alt", async () => {
const iface = makeIface(CamIsoInterface.CAMERA, {
claimed: false,
alternateSetting: CamIsoAltSetting.CAMERA,
});
const { device, claimInterface, selectAlternateInterface } =
makeDevice(iface);

await prepareCamDevice(device, CamIsoInterface.CAMERA);

expect(claimInterface).toHaveBeenCalledWith(CamIsoInterface.CAMERA);
expect(selectAlternateInterface).not.toHaveBeenCalled();
});

test("throws ReferenceError when the requested interface is not present", async () => {
const iface = makeIface(7);
const { device } = makeDevice(iface);

await expect(
prepareCamDevice(device, CamIsoInterface.CAMERA),
).rejects.toBeInstanceOf(ReferenceError);
});

test("opens the device before claiming when not yet open", async () => {
const iface = makeIface(CamIsoInterface.CAMERA, { claimed: false });
const { device, open, claimInterface } = makeDevice(iface, {
opened: false,
});

await prepareCamDevice(device, CamIsoInterface.CAMERA);

expect(open).toHaveBeenCalled();
const openOrder = open.mock.invocationCallOrder[0]!;
const claimOrder = claimInterface.mock.invocationCallOrder[0]!;
expect(openOrder).toBeLessThan(claimOrder);
});
});