fix: Clear stale media unavailable errors when a camera recovers (#2627)

- Closes: #2576
This commit is contained in:
Dermot Duffy
2026-07-28 21:21:50 -07:00
committed by GitHub
parent 231e3087b7
commit 15b08db3e8
49 changed files with 1128 additions and 229 deletions
@@ -47,7 +47,7 @@ describe('ImageSurfaceController', () => {
it('should have liveness when given liveness options', () => {
const controller = new ImageSurfaceController(createLitElement(), () => null, {
livenessOptions: { isFrameExpected: () => true, stallWindowSeconds: 10 },
livenessOptions: { isFrameExpected: () => true, getStallAfterSeconds: () => 10 },
});
expect(controller.getMediaPlayer().subscribeLiveness).toBeDefined();
@@ -178,7 +178,8 @@ describe('Go2RTCSessionController', () => {
const getControls = vi.fn(() => options?.controls ?? false);
const mediaLoadedCallback = vi.fn();
const surfaceCommittedCallback = vi.fn();
const errorCallback = vi.fn();
const streamErrorCallback = vi.fn();
const microphoneErrorCallback = vi.fn();
const session = new Go2RTCSessionController(
{
@@ -186,7 +187,8 @@ describe('Go2RTCSessionController', () => {
getCardWideConfig: () => options?.cardWideConfig ?? null,
mediaLoadedCallback,
surfaceCommittedCallback,
errorCallback,
streamErrorCallback,
microphoneErrorCallback,
},
{ createWebSocket, createBinarySource, createWebRTCSource, createVideoElement },
);
@@ -201,7 +203,8 @@ describe('Go2RTCSessionController', () => {
createBinarySource,
createWebRTCSource,
createWebSocket,
errorCallback,
streamErrorCallback,
microphoneErrorCallback,
mediaLoadedCallback,
offscreenVideos,
session,
@@ -281,7 +284,8 @@ describe('Go2RTCSessionController', () => {
surfaceCommittedCallback: vi.fn(),
getCardWideConfig: () => null,
mediaLoadedCallback: vi.fn(),
errorCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
});
session.connect('ws://localhost:1/api/ws', createSurfaces().surfaces, ['mse']);
session.reset();
@@ -536,6 +540,30 @@ describe('Go2RTCSessionController', () => {
expect(webRTCOptions[0]?.microphoneStream).toBe(micStream);
});
it('should report a microphone error without disturbing the stream', () => {
const {
session,
surfaces,
websockets,
webRTCOptions,
webRTCSources,
microphoneErrorCallback,
streamErrorCallback,
} = setup();
session.connect('http://host/api/ws?src=camera', surfaces, ['webrtc']);
websockets[0].fireOpen();
webRTCOptions[0]?.microphoneErrorCallback?.('InvalidStateError');
expect(microphoneErrorCallback).toHaveBeenCalledWith('InvalidStateError');
// The inbound video is unaffected by an outbound audio failure, so the
// source keeps running and the session neither escalates nor reconnects.
expect(streamErrorCallback).not.toHaveBeenCalled();
expect(webRTCSources[0].stop).not.toHaveBeenCalled();
expect(websockets).toHaveLength(1);
});
it('should re-dispatch loaded media on an audio mute transition', () => {
const peerConnection = new FakeRTCPeerConnection();
const audioTransceiver = peerConnection.addTransceiver('audio', {
@@ -863,7 +891,8 @@ describe('Go2RTCSessionController', () => {
});
it('should escalate via the error callback after exhausting reconnect attempts', () => {
const { session, surfaces, websockets, createWebSocket, errorCallback } = setup();
const { session, surfaces, websockets, createWebSocket, streamErrorCallback } =
setup();
session.connect('http://host/api/ws?src=camera', surfaces, ['mse']);
// Each fresh connection closes before loading, consuming one reconnect
@@ -878,16 +907,17 @@ describe('Go2RTCSessionController', () => {
websockets[3].fireClose();
expect(createWebSocket).toHaveBeenCalledTimes(4);
expect(errorCallback).toHaveBeenCalledTimes(1);
expect(streamErrorCallback).toHaveBeenCalledTimes(1);
// The socket dropped with no source reporting a cause.
expect(errorCallback).toHaveBeenCalledWith(null);
expect(streamErrorCallback).toHaveBeenCalledWith(null);
vi.advanceTimersByTime(2 * 1000);
expect(createWebSocket).toHaveBeenCalledTimes(4);
});
it('should escalate with the most recent source failure reason', () => {
const { session, surfaces, websockets, binaryContexts, errorCallback } = setup();
const { session, surfaces, websockets, binaryContexts, streamErrorCallback } =
setup();
session.connect('http://host/api/ws?src=camera', surfaces, ['mse']);
// Each attempt: the single binary source fails, which drains the mode
@@ -901,7 +931,7 @@ describe('Go2RTCSessionController', () => {
websockets[3].fireOpen();
binaryContexts[3].callbacks.failedCallback('unsupported');
expect(errorCallback).toHaveBeenCalledWith('unsupported');
expect(streamErrorCallback).toHaveBeenCalledWith('unsupported');
});
it('should reset the reconnect budget after a successful media load', () => {
@@ -911,7 +941,7 @@ describe('Go2RTCSessionController', () => {
websockets,
binaryContexts,
createWebSocket,
errorCallback,
streamErrorCallback,
} = setup();
session.connect('http://host/api/ws?src=camera', surfaces, ['mse']);
@@ -933,7 +963,7 @@ describe('Go2RTCSessionController', () => {
websockets[attempt + 1].fireOpen();
}
expect(errorCallback).not.toHaveBeenCalled();
expect(streamErrorCallback).not.toHaveBeenCalled();
expect(createWebSocket).toHaveBeenCalledTimes(6);
});
@@ -1081,7 +1111,8 @@ describe('Go2RTCSessionController', () => {
surfaceCommittedCallback: vi.fn(),
getCardWideConfig: () => null,
mediaLoadedCallback,
errorCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket, createBinarySource },
);
@@ -1115,7 +1146,8 @@ describe('Go2RTCSessionController', () => {
surfaceCommittedCallback: vi.fn(),
getCardWideConfig: () => null,
mediaLoadedCallback,
errorCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket, createWebRTCSource },
);
@@ -1141,7 +1173,8 @@ describe('Go2RTCSessionController', () => {
surfaceCommittedCallback: vi.fn(),
getCardWideConfig: () => null,
mediaLoadedCallback: vi.fn(),
errorCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket },
);
@@ -1172,7 +1205,8 @@ describe('Go2RTCSessionController', () => {
surfaceCommittedCallback: vi.fn(),
getCardWideConfig: () => null,
mediaLoadedCallback: vi.fn(),
errorCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket },
);
@@ -1212,7 +1246,8 @@ describe('Go2RTCSessionController', () => {
surfaceCommittedCallback: vi.fn(),
getCardWideConfig: () => null,
mediaLoadedCallback: vi.fn(),
errorCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket, createBinarySource, createWebRTCSource },
);
@@ -29,11 +29,13 @@ describe('WebRTCStreamSource', () => {
const pc = new FakeRTCPeerConnection();
const createPeerConnection = vi.fn(() => pc.asPeerConnection());
const microphoneErrorCallback = vi.fn();
const source = new WebRTCStreamSource(context, {
createPeerConnection,
createMediaStream: (tracks) =>
new FakeMediaStream(tracks as unknown as FakeMediaStreamTrack[]).asMediaStream(),
microphoneStream: options?.microphoneStream?.asMediaStream() ?? null,
microphoneErrorCallback,
});
return {
@@ -42,6 +44,7 @@ describe('WebRTCStreamSource', () => {
createPeerConnection,
failedCallback,
loadedCallback,
microphoneErrorCallback,
pc,
source,
video,
@@ -506,29 +509,77 @@ describe('WebRTCStreamSource', () => {
});
it('should do nothing before there is a peer connection', async () => {
const { source, failedCallback } = setup();
const { source, microphoneErrorCallback } = setup();
await source.setMicrophoneStream(
new FakeMediaStream([new FakeMediaStreamTrack('audio')]).asMediaStream(),
);
expect(failedCallback).not.toHaveBeenCalled();
expect(microphoneErrorCallback).not.toHaveBeenCalled();
});
it('should fail when a current replaceTrack rejects', async () => {
it.each([
[
'what the browser said when the rejection has a message',
new DOMException('The peer connection is closed', 'InvalidStateError'),
'The peer connection is closed',
],
[
'the rejection type when there is no message to quote',
new DOMException('', 'InvalidStateError'),
'InvalidStateError',
],
['nothing when the rejection is not an object', 'nope', undefined],
[
'nothing when the rejection describes itself with neither',
{ message: 5, name: 7 },
undefined,
],
] as const)(
'should report %s when a current replaceTrack rejects',
async (_summary, rejection, expected) => {
const { source, pc, microphoneErrorCallback } = setup();
source.start();
pc.getMicrophoneTransceiver().sender.replaceTrack.mockRejectedValue(rejection);
await source.setMicrophoneStream(
new FakeMediaStream([new FakeMediaStreamTrack('audio')]).asMediaStream(),
);
expect(microphoneErrorCallback).toHaveBeenCalledWith(expected);
},
);
it('should not fail the stream source when the microphone cannot attach', async () => {
const { source, pc, failedCallback } = setup();
source.start();
pc.getMicrophoneTransceiver().sender.replaceTrack.mockRejectedValue(
new Error('replace failed'),
new DOMException('replace failed', 'InvalidStateError'),
);
await source.setMicrophoneStream(
new FakeMediaStream([new FakeMediaStreamTrack('audio')]).asMediaStream(),
);
expect(failedCallback).toHaveBeenCalledWith('two_way_audio_error');
// The inbound video is unaffected by an outbound audio failure, so the
// source must keep running rather than failing over to another one.
expect(failedCallback).not.toHaveBeenCalled();
});
it('should not report a rejection when detaching the microphone', async () => {
const stream = new FakeMediaStream([new FakeMediaStreamTrack('audio')]);
const { source, pc, microphoneErrorCallback } = setup({
microphoneStream: stream,
});
source.start();
pc.getMicrophoneTransceiver().sender.replaceTrack.mockRejectedValue(
new DOMException('The peer connection is closed', 'InvalidStateError'),
);
await source.setMicrophoneStream(null);
// Ignore the error, the user is not trying to be heard anyway.
expect(microphoneErrorCallback).not.toHaveBeenCalled();
});
it('should ignore a stale replaceTrack rejection after stop', async () => {
const { source, pc, failedCallback } = setup();
const { source, pc, microphoneErrorCallback } = setup();
source.start();
let rejectReplace: (reason: Error) => void = () => {};
pc.getMicrophoneTransceiver().sender.replaceTrack.mockReturnValue(
@@ -543,7 +594,7 @@ describe('WebRTCStreamSource', () => {
rejectReplace(new Error('replace failed'));
await promise;
expect(failedCallback).not.toHaveBeenCalled();
expect(microphoneErrorCallback).not.toHaveBeenCalled();
});
});
});
@@ -1,21 +1,20 @@
import { describe, expect, it } from 'vitest';
import { mapFailureReasonToIssueReason } from '../../../../../../src/components-lib/live/providers/go2rtc-experimental/utils/failure-reason';
import { mapStreamFailureReasonToIssueReason } from '../../../../../../src/components-lib/live/providers/go2rtc-experimental/utils/stream-failure-reason';
describe('mapFailureReasonToIssueReason', () => {
describe('mapStreamFailureReasonToIssueReason', () => {
it.each([
['connect_timeout', 'not_loading'],
['negotiation_timeout', 'not_loading'],
['media_error', 'playback_error'],
['buffer_overflow', 'playback_error'],
['two_way_audio_error', 'two_way_audio_error'],
['server_error', 'server_error'],
['unsupported', 'unsupported'],
] as const)('should map %s to the %s cause', (reason, expected) => {
expect(mapFailureReasonToIssueReason(reason)).toBe(expected);
expect(mapStreamFailureReasonToIssueReason(reason)).toBe(expected);
});
it('should map a null reason to a generic playback error', () => {
expect(mapFailureReasonToIssueReason(null)).toBe('playback_error');
expect(mapStreamFailureReasonToIssueReason(null)).toBe('playback_error');
});
});