fix: Hold the 2-way audio backchannel open for the shortest possible time (#2697)

The card claimed a camera's ONVIF audio backchannel in two places that
had
nothing to do with a call: the capability probe at camera init
(`&microphone`),
and a pre-armed `sendonly` audio transceiver on every live WebRTC offer.
Merely
looking at a dashboard occupied the camera's speaker line. Outbound
audio now
travels on its own audio-only WebRTC connection, opened when a call is
answered
and closed when it ends.

- The backchannel is claimed only for the duration of a call. Idle
viewing
  claims nothing.
- Two-way audio now works in `mse`, `mp4` and `mjpeg` modes (note: the
outbound
  audio still traverses WebRTC).
- No renegotiation and no video blink at call start or end.
- A call that cannot carry audio now reports it and ends, instead of
showing a
  live microphone that goes nowhere.
- `live.microphone.always_connected` is now purely about the browser
microphone
  permission prompt.
- Call setup measured at 66ms (LAN) and ~260ms (cellular) for ICE and
DTLS, plus
  ~300ms for `go2rtc` to open an RTSP backchannel.

Verified against a live Frigate + `go2rtc` instance, and by unit tests
at 100%
coverage.

 - Closes #2691
 - Closes #2039
 - Closes #2178

Ref #2299 -- the probe no longer opens a backchannel, but it still runs
per
camera on every load and reconnect, and still dials the camera on the
direct-`go2rtc` path. Caching remains to be done.

Ref AlexxIT/go2rtc#1860 -- once a call has opened a backchannel,
`go2rtc` keeps
that media set up on the camera's RTSP session for the life of the
producer.

Diagnoses #2678
This commit is contained in:
Dermot Duffy
2026-08-21 20:13:58 -07:00
committed by GitHub
parent 2d7c86c93c
commit 11cc543406
65 changed files with 2251 additions and 1045 deletions
@@ -0,0 +1,81 @@
import { describe, expect, it } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { Camera } from '../../../../src/camera-manager/camera';
import type { CameraManagerEngine } from '../../../../src/camera-manager/engine';
import { FrigateCamera } from '../../../../src/camera-manager/frigate/camera';
import { createBackchannel } from '../../../../src/components-lib/live/backchannel/factory';
import { Go2RTCBackchannel } from '../../../../src/components-lib/live/backchannel/go2rtc';
import type { HomeAssistant } from '../../../../src/ha/types';
import { createCameraConfig } from '../../../config/test-utils';
describe('createBackchannel', () => {
it('should create a backchannel for a go2rtc camera', () => {
const camera = new Camera(
createCameraConfig({
live_provider: 'go2rtc',
go2rtc: { url: 'https://go2rtc', stream: 'office' },
}),
mock<CameraManagerEngine>(),
);
expect(createBackchannel(mock<HomeAssistant>(), camera)).toBeInstanceOf(
Go2RTCBackchannel,
);
});
it('should create a backchannel for a go2rtc-experimental camera', () => {
const camera = new Camera(
createCameraConfig({
live_provider: 'go2rtc-experimental',
go2rtc: { url: 'https://go2rtc', stream: 'office' },
}),
mock<CameraManagerEngine>(),
);
expect(createBackchannel(mock<HomeAssistant>(), camera)).toBeInstanceOf(
Go2RTCBackchannel,
);
});
it('should not create a backchannel for a non-go2rtc camera', () => {
const camera = new Camera(
createCameraConfig({
camera_entity: 'camera.office',
live_provider: 'ha',
}),
mock<CameraManagerEngine>(),
);
expect(createBackchannel(mock<HomeAssistant>(), camera)).toBeNull();
});
it('should not create a backchannel without a go2rtc endpoint', () => {
const camera = new Camera(
createCameraConfig({ live_provider: 'go2rtc' }),
mock<CameraManagerEngine>(),
);
expect(createBackchannel(mock<HomeAssistant>(), camera)).toBeNull();
});
it('should use the endpoint the camera engine resolves', () => {
// A Frigate camera serves go2rtc through the Frigate integration's proxy
// rather than at a directly-configured URL, so the endpoint must come from
// the camera rather than being rebuilt from its configuration.
const camera = new FrigateCamera(
createCameraConfig({
live_provider: 'go2rtc',
frigate: { client_id: 'frigate', camera_name: 'office' },
}),
mock<CameraManagerEngine>(),
);
expect(camera.getEndpoints()?.go2rtc?.endpoint).toBe(
'/api/frigate/frigate/mse/api/ws?src=office',
);
expect(createBackchannel(mock<HomeAssistant>(), camera)).toBeInstanceOf(
Go2RTCBackchannel,
);
});
});
@@ -0,0 +1,726 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { Go2RTCBackchannel } from '../../../../src/components-lib/live/backchannel/go2rtc';
import type { BackchannelErrorCallback } from '../../../../src/components-lib/live/backchannel/types';
import {
resolveEndpointURL,
type ResolvedEndpoint,
} from '../../../../src/ha/resolve-endpoint';
import type { HomeAssistant } from '../../../../src/ha/types';
import {
FakeMediaStream,
FakeMediaStreamTrack,
FakeRTCPeerConnection,
FakeWebSocket,
} from '../../../go2rtc/test-utils';
import { flushPromises } from '../../../test-utils';
vi.mock('../../../../src/ha/resolve-endpoint');
const createStream = (): FakeMediaStream =>
new FakeMediaStream([new FakeMediaStreamTrack('audio')]);
const setup = (options?: { errorCallback?: BackchannelErrorCallback }) => {
const pc = new FakeRTCPeerConnection();
const websocket = new FakeWebSocket();
const backchannel = new Go2RTCBackchannel(
mock<HomeAssistant>(),
{ endpoint: '/local/api/ws?src=camera', sign: true },
undefined,
{
createPeerConnection: () => pc.asPeerConnection(),
createWebSocket: () => websocket.asWebSocket(),
...(options?.errorCallback && { errorCallback: options.errorCallback }),
},
);
return { backchannel, pc, websocket };
};
// Drives a successful negotiation up to (but not including) the point the
// caller chooses to complete or fail it.
const negotiate = async (websocket: FakeWebSocket) => {
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireMessage(JSON.stringify({ type: 'webrtc/answer', value: 'v=0\r\n' }));
await flushPromises();
};
const connect = async (pc: FakeRTCPeerConnection, websocket: FakeWebSocket) => {
await negotiate(websocket);
pc.fireConnectionStateChange('connected');
await flushPromises();
};
// @vitest-environment jsdom
describe('Go2RTCBackchannel', () => {
beforeEach(() => {
vi.mocked(resolveEndpointURL).mockResolvedValue({
success: true,
url: 'http://go2rtc/api/ws',
});
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('start', () => {
it('should offer exactly one outbound audio slot and no video', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
expect(pc.transceivers).toHaveLength(1);
expect(pc.transceivers[0].direction).toBe('sendonly');
expect(pc.transceivers[0].sender.track?.kind).toBe('audio');
});
it('should resolve only once the camera is reachable', async () => {
const { backchannel, pc, websocket } = setup();
let resolved = false;
const started = backchannel.start(createStream().asMediaStream()).then(() => {
resolved = true;
});
await negotiate(websocket);
expect(resolved).toBe(false);
pc.fireConnectionStateChange('connected');
await started;
expect(resolved).toBe(true);
});
it('should send the offer over the signaling channel', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
expect(websocket.sent.map((message) => JSON.parse(message).type)).toContain(
'webrtc/offer',
);
});
it('should reject when the microphone has no audio track', async () => {
const { backchannel } = setup();
await expect(
backchannel.start(new FakeMediaStream().asMediaStream()),
).rejects.toMatchObject({ reason: 'no_microphone' });
});
it('should reject when the microphone track has already ended', async () => {
const { backchannel } = setup();
const track = new FakeMediaStreamTrack('audio');
track.readyState = 'ended';
await expect(
backchannel.start(new FakeMediaStream([track]).asMediaStream()),
).rejects.toMatchObject({ reason: 'no_microphone' });
});
it('should reject when the address cannot be resolved', async () => {
vi.mocked(resolveEndpointURL).mockResolvedValue({
success: false,
error: 'proxy',
});
const { backchannel } = setup();
await expect(
backchannel.start(createStream().asMediaStream()),
).rejects.toMatchObject({ reason: 'failed', description: 'proxy' });
});
it('should reject when the server reports the stream cannot take audio', async () => {
const { backchannel, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireMessage(
JSON.stringify({ type: 'error', value: 'webrtc: no backchannel' }),
);
await expect(started).rejects.toMatchObject({
reason: 'no_two_way_audio',
description: 'webrtc: no backchannel',
});
});
it('should reject when the camera declines to receive audio', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await negotiate(websocket);
pc.getMicrophoneTransceiver().currentDirection = 'inactive';
pc.fireConnectionStateChange('connected');
await expect(started).rejects.toMatchObject({ reason: 'no_two_way_audio' });
});
it('should reject when the signaling channel closes before connecting', async () => {
const { backchannel, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireClose();
await expect(started).rejects.toMatchObject({ reason: 'failed' });
});
it('should reject when the peer connection fails', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await negotiate(websocket);
pc.fireConnectionStateChange('failed');
await expect(started).rejects.toMatchObject({ reason: 'failed' });
});
it('should reject when the camera is not reached in time', async () => {
vi.useFakeTimers();
const { backchannel, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
vi.advanceTimersByTime(10 * 1000);
await expect(started).rejects.toMatchObject({ reason: 'failed' });
vi.useRealTimers();
});
it('should reject when the offer cannot be created', async () => {
const { backchannel, pc, websocket } = setup();
pc.createOffer.mockRejectedValue(new Error('no media'));
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
await expect(started).rejects.toMatchObject({
reason: 'failed',
description: 'no media',
});
});
it('should close the signaling channel once connected', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
expect(websocket.close).toHaveBeenCalled();
expect(pc.close).not.toHaveBeenCalled();
});
it('should send ICE candidates and signal the end of them', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
pc.fireIceCandidate('candidate:1');
pc.fireIceCandidate(null);
const candidates = websocket.sent
.map((message) => JSON.parse(message))
.filter((message) => message.type === 'webrtc/candidate')
.map((message) => message.value);
expect(candidates).toEqual(['candidate:1', '']);
await connect(pc, websocket);
await started;
});
it('should apply candidates from the server', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireMessage(
JSON.stringify({ type: 'webrtc/candidate', value: 'candidate:2' }),
);
await flushPromises();
expect(pc.addIceCandidate).toHaveBeenCalledWith({
candidate: 'candidate:2',
sdpMid: '0',
});
await connect(pc, websocket);
await started;
});
});
describe('after connecting', () => {
it('should report a peer connection that later fails', async () => {
const errorCallback = vi.fn();
const { backchannel, pc, websocket } = setup({ errorCallback });
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
pc.fireConnectionStateChange('failed');
expect(errorCallback).toHaveBeenCalledWith(
expect.objectContaining({ reason: 'failed' }),
);
});
it('should release the camera when the peer connection later fails', async () => {
const stream = createStream();
const { backchannel, pc, websocket } = setup({ errorCallback: vi.fn() });
const started = backchannel.start(stream.asMediaStream());
await connect(pc, websocket);
await started;
pc.fireConnectionStateChange('failed');
expect(pc.close).toHaveBeenCalled();
expect(stream.getAudioTracks()[0].readyState).toBe('live');
});
it('should ignore the signaling channel closing', async () => {
const errorCallback = vi.fn();
const { backchannel, pc, websocket } = setup({ errorCallback });
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
websocket.fireClose();
expect(errorCallback).not.toHaveBeenCalled();
});
});
describe('setStream', () => {
it('should swap the outbound track without reconnecting', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
const replacement = createStream();
await backchannel.setStream(replacement.asMediaStream());
expect(pc.getMicrophoneTransceiver().sender.replaceTrack).toHaveBeenCalledWith(
replacement.getAudioTracks()[0],
);
expect(pc.close).not.toHaveBeenCalled();
});
it('should do nothing without an established path', async () => {
const { backchannel } = setup();
await expect(
backchannel.setStream(createStream().asMediaStream()),
).resolves.toBeUndefined();
});
});
describe('stop', () => {
it('should close the connection but leave the microphone running', async () => {
const { backchannel, pc, websocket } = setup();
const stream = createStream();
const started = backchannel.start(stream.asMediaStream());
await connect(pc, websocket);
await started;
backchannel.stop();
expect(pc.close).toHaveBeenCalled();
expect(stream.getAudioTracks()[0].readyState).toBe('live');
});
it('should not report a peer connection that fails after being stopped', async () => {
const errorCallback = vi.fn();
const { backchannel, pc, websocket } = setup({ errorCallback });
const started = backchannel.start(createStream().asMediaStream());
await negotiate(websocket);
backchannel.stop();
pc.fireConnectionStateChange('failed');
await flushPromises();
await expect(started).rejects.toMatchObject({ reason: 'abandoned' });
expect(errorCallback).not.toHaveBeenCalled();
});
});
describe('losing the microphone', () => {
const endTrack = (stream: FakeMediaStream): void => {
const track = stream.getAudioTracks()[0];
track.readyState = 'ended';
track.dispatchEvent(new Event('ended'));
};
it('should fail a start whose microphone ends before connecting', async () => {
const stream = createStream();
const { backchannel, websocket } = setup();
const started = backchannel.start(stream.asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
endTrack(stream);
await expect(started).rejects.toMatchObject({ reason: 'no_microphone' });
});
it('should release the camera and report when the microphone ends mid-call', async () => {
const errorCallback = vi.fn();
const stream = createStream();
const { backchannel, pc, websocket } = setup({ errorCallback });
const started = backchannel.start(stream.asMediaStream());
await connect(pc, websocket);
await started;
endTrack(stream);
expect(pc.close).toHaveBeenCalled();
expect(errorCallback).toHaveBeenCalledWith(
expect.objectContaining({ reason: 'no_microphone' }),
);
});
it('should stop watching a microphone that has been replaced', async () => {
const stream = createStream();
const errorCallback = vi.fn();
const { backchannel, pc, websocket } = setup({ errorCallback });
const started = backchannel.start(stream.asMediaStream());
await connect(pc, websocket);
await started;
await backchannel.setStream(createStream().asMediaStream());
endTrack(stream);
expect(errorCallback).not.toHaveBeenCalled();
expect(pc.close).not.toHaveBeenCalled();
});
it('should stop watching the microphone once stopped', async () => {
const stream = createStream();
const errorCallback = vi.fn();
const { backchannel, pc, websocket } = setup({ errorCallback });
const started = backchannel.start(stream.asMediaStream());
await connect(pc, websocket);
await started;
backchannel.stop();
endTrack(stream);
expect(errorCallback).not.toHaveBeenCalled();
});
});
describe('stale and defensive paths', () => {
it('should use the browser factories when none are supplied', async () => {
const pc = new FakeRTCPeerConnection();
const websocket = new FakeWebSocket();
vi.stubGlobal('RTCPeerConnection', function () {
return pc.asPeerConnection();
});
vi.stubGlobal('WebSocket', function () {
return websocket.asWebSocket();
});
const backchannel = new Go2RTCBackchannel(mock<HomeAssistant>(), {
endpoint: '/local/api/ws?src=camera',
});
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
expect(pc.transceivers).toHaveLength(1);
vi.unstubAllGlobals();
});
it('should time out an address resolution that never returns', async () => {
vi.useFakeTimers();
vi.mocked(resolveEndpointURL).mockReturnValue(new Promise(() => {}));
const { backchannel } = setup();
const started = backchannel.start(createStream().asMediaStream());
vi.advanceTimersByTime(10 * 1000);
await expect(started).rejects.toMatchObject({ reason: 'failed' });
vi.useRealTimers();
});
it('should reject with a reason and release the connection when setup throws', async () => {
const { backchannel, pc } = setup();
pc.addTransceiver = () => {
throw new Error('bad track');
};
await expect(
backchannel.start(createStream().asMediaStream()),
).rejects.toMatchObject({ reason: 'failed', description: 'bad track' });
expect(pc.close).toHaveBeenCalled();
});
it('should reject without a description when setup throws something not error-like', async () => {
const { backchannel, pc } = setup();
pc.addTransceiver = () => {
throw 'a bare string';
};
await expect(
backchannel.start(createStream().asMediaStream()),
).rejects.toMatchObject({ reason: 'failed', description: null });
});
it('should abandon a start stopped while the address resolves', async () => {
let release: (value: ResolvedEndpoint) => void = () => {};
vi.mocked(resolveEndpointURL).mockReturnValue(
new Promise((resolve) => {
release = resolve;
}),
);
const { backchannel, pc } = setup();
const started = backchannel.start(createStream().asMediaStream());
backchannel.stop();
release({ success: true, url: 'http://go2rtc/api/ws' });
await flushPromises();
await expect(started).rejects.toMatchObject({ reason: 'abandoned' });
expect(pc.transceivers).toHaveLength(0);
});
it('should not send candidates after being stopped', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
const before = websocket.sent.length;
backchannel.stop();
pc.fireIceCandidate('candidate:late');
expect(websocket.sent).toHaveLength(before);
await expect(started).rejects.toThrow();
});
it('should not negotiate after being stopped', async () => {
const { backchannel, pc, websocket } = setup();
let releaseOffer: (value: { type: string; sdp?: string }) => void = () => {};
pc.createOffer.mockReturnValue(
new Promise((resolve) => {
releaseOffer = resolve;
}),
);
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
backchannel.stop();
releaseOffer({ type: 'offer', sdp: 'v=0' });
await flushPromises();
expect(pc.setLocalDescription).not.toHaveBeenCalled();
await expect(started).rejects.toThrow();
});
it('should not send an offer when the local description is stopped mid-flight', async () => {
const { backchannel, pc, websocket } = setup();
let releaseLocal: () => void = () => {};
pc.setLocalDescription.mockReturnValue(
new Promise<void>((resolve) => {
releaseLocal = resolve;
}),
);
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
backchannel.stop();
releaseLocal();
await flushPromises();
expect(
websocket.sent.filter((m) => JSON.parse(m).type === 'webrtc/offer'),
).toHaveLength(0);
await expect(started).rejects.toThrow();
});
it('should send an empty offer when the browser produces no SDP', async () => {
const { backchannel, pc, websocket } = setup();
pc.createOffer.mockResolvedValue({ type: 'offer' });
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
const offer = websocket.sent
.map((m) => JSON.parse(m))
.find((m) => m.type === 'webrtc/offer');
expect(offer.value).toBe('');
});
it('should reject without a description when a negotiation failure is not error-like', async () => {
const { backchannel, pc, websocket } = setup();
pc.createOffer.mockRejectedValue('a bare string');
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
await expect(started).rejects.toMatchObject({
reason: 'failed',
description: null,
});
});
it('should describe a negotiation failure by its name when it carries no message', async () => {
const { backchannel, pc, websocket } = setup();
pc.createOffer.mockRejectedValue({ name: 'InvalidStateError' });
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
await expect(started).rejects.toMatchObject({
reason: 'failed',
description: 'InvalidStateError',
});
});
it('should reject when the answer cannot be applied', async () => {
const { backchannel, pc, websocket } = setup();
pc.setRemoteDescription.mockRejectedValue(new Error('bad sdp'));
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireMessage(JSON.stringify({ type: 'webrtc/answer', value: 'v=0' }));
await expect(started).rejects.toMatchObject({
reason: 'failed',
description: 'bad sdp',
});
});
it('should ignore messages without a string payload', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireMessage(JSON.stringify({ type: 'webrtc/answer', value: 42 }));
await flushPromises();
expect(pc.setRemoteDescription).not.toHaveBeenCalled();
await connect(pc, websocket);
await started;
});
it('should tolerate a candidate the browser rejects', async () => {
const { backchannel, pc, websocket } = setup();
pc.addIceCandidate.mockRejectedValue(new Error('bad candidate'));
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireMessage(
JSON.stringify({ type: 'webrtc/candidate', value: 'candidate:3' }),
);
await flushPromises();
await connect(pc, websocket);
await expect(started).resolves.toBeUndefined();
});
it('should ignore an empty candidate from the server', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireMessage(JSON.stringify({ type: 'webrtc/candidate', value: '' }));
await flushPromises();
expect(pc.addIceCandidate).not.toHaveBeenCalled();
await connect(pc, websocket);
await started;
});
it('should ignore messages after being stopped', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
backchannel.stop();
websocket.fireMessage(JSON.stringify({ type: 'webrtc/answer', value: 'v=0' }));
await flushPromises();
expect(pc.setRemoteDescription).not.toHaveBeenCalled();
await expect(started).rejects.toThrow();
});
it('should ignore intermediate connection states', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await negotiate(websocket);
pc.fireConnectionStateChange('connecting');
await flushPromises();
expect(websocket.close).not.toHaveBeenCalled();
pc.fireConnectionStateChange('connected');
await started;
});
it('should reject a replacement carrying no audio', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
await expect(
backchannel.setStream(new FakeMediaStream().asMediaStream()),
).rejects.toMatchObject({ reason: 'no_microphone' });
expect(pc.getMicrophoneTransceiver().sender.replaceTrack).not.toHaveBeenCalled();
});
it('should reject a replacement whose track has ended', async () => {
const { backchannel, pc, websocket } = setup();
const started = backchannel.start(createStream().asMediaStream());
await connect(pc, websocket);
await started;
const track = new FakeMediaStreamTrack('audio');
track.readyState = 'ended';
await expect(
backchannel.setStream(new FakeMediaStream([track]).asMediaStream()),
).rejects.toMatchObject({ reason: 'no_microphone' });
});
it('should reject without a description when an answer failure names nothing', async () => {
const { backchannel, pc, websocket } = setup();
pc.setRemoteDescription.mockRejectedValue({});
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
websocket.fireMessage(JSON.stringify({ type: 'webrtc/answer', value: 'v=0' }));
await expect(started).rejects.toMatchObject({
reason: 'failed',
description: null,
});
});
it('should ignore a negotiation failure that arrives after being stopped', async () => {
const { backchannel, pc, websocket } = setup();
let rejectOffer: (error: unknown) => void = () => {};
pc.createOffer.mockReturnValue(
new Promise((_resolve, reject) => {
rejectOffer = reject;
}),
);
const started = backchannel.start(createStream().asMediaStream());
await flushPromises();
websocket.fireOpen();
await flushPromises();
backchannel.stop();
await expect(started).rejects.toMatchObject({ reason: 'abandoned' });
rejectOffer(new Error('too late'));
await flushPromises();
});
});
});
@@ -1,29 +0,0 @@
import { describe, expect, it, vi } from 'vitest';
import {
createBrowserPeerConnection,
GO2RTC_PEER_CONNECTION_CONFIG,
} from '../../../../../../src/components-lib/live/providers/go2rtc-experimental/adapters/peer-connection';
describe('peer-connection', () => {
it('should configure two STUN servers with max-bundle', () => {
expect(GO2RTC_PEER_CONNECTION_CONFIG).toEqual({
bundlePolicy: 'max-bundle',
iceServers: [
{
urls: ['stun:stun.l.google.com:19302', 'stun:stun.cloudflare.com:3478'],
},
],
});
});
it('should construct a real peer connection', () => {
const RTCPeerConnectionMock = vi.fn();
vi.stubGlobal('RTCPeerConnection', RTCPeerConnectionMock);
createBrowserPeerConnection(GO2RTC_PEER_CONNECTION_CONFIG);
expect(RTCPeerConnectionMock).toHaveBeenCalledWith(GO2RTC_PEER_CONNECTION_CONFIG);
vi.unstubAllGlobals();
});
});
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from 'vitest';
import { OffscreenVideo } from '../../../../../src/components-lib/live/providers/go2rtc-experimental/offscreen-video';
import { FakeMediaStream, FakeMediaStreamTrack } from './test-utils';
import { FakeMediaStream, FakeMediaStreamTrack } from '../../../../go2rtc/test-utils';
// @vitest-environment jsdom
describe('OffscreenVideo', () => {
@@ -26,7 +26,7 @@ import {
FakeMediaStreamTrack,
FakeRTCPeerConnection,
FakeWebSocket,
} from './test-utils';
} from '../../../../go2rtc/test-utils';
const H264_PROFILE: StreamProfile = {
hasVideo: true,
@@ -128,7 +128,6 @@ describe('Go2RTCSessionController', () => {
source.getCapabilities.mockReturnValue({
supportsPause: true,
hasAudio: true,
has2WayAudio: false,
});
source.getTechnology.mockReturnValue([mode]);
binarySources.push(source);
@@ -154,15 +153,12 @@ describe('Go2RTCSessionController', () => {
source.getCapabilities.mockReturnValue({
supportsPause: true,
hasAudio: true,
has2WayAudio: false,
});
source.getTechnology.mockReturnValue(['webrtc']);
source.getMediaStream.mockReturnValue(webRTCStream.asMediaStream());
source.getPeerConnection.mockReturnValue(
options?.webRTCPeerConnection?.asPeerConnection() ?? null,
);
source.setMicrophoneStream.mockResolvedValue(undefined);
webRTCSources.push(source);
return source;
},
@@ -179,7 +175,6 @@ describe('Go2RTCSessionController', () => {
const mediaLoadedCallback = vi.fn();
const surfaceCommittedCallback = vi.fn();
const streamErrorCallback = vi.fn();
const microphoneErrorCallback = vi.fn();
const session = new Go2RTCSessionController(
{
@@ -188,7 +183,6 @@ describe('Go2RTCSessionController', () => {
mediaLoadedCallback,
surfaceCommittedCallback,
streamErrorCallback,
microphoneErrorCallback,
},
{ createWebSocket, createBinarySource, createWebRTCSource, createVideoElement },
);
@@ -204,7 +198,6 @@ describe('Go2RTCSessionController', () => {
createWebRTCSource,
createWebSocket,
streamErrorCallback,
microphoneErrorCallback,
mediaLoadedCallback,
offscreenVideos,
session,
@@ -285,7 +278,6 @@ describe('Go2RTCSessionController', () => {
getCardWideConfig: () => null,
mediaLoadedCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
});
session.connect('ws://localhost:1/api/ws', createSurfaces().surfaces, ['mse']);
session.reset();
@@ -528,42 +520,6 @@ describe('Go2RTCSessionController', () => {
expect(createWebSocket).toHaveBeenCalledTimes(2);
});
it('should pre-arm the WebRTC source with the current microphone stream', () => {
const { session, surfaces, websockets, webRTCOptions } = setup();
const micStream = new FakeMediaStream([
new FakeMediaStreamTrack('audio'),
]).asMediaStream();
session.setMicrophoneStream(micStream);
session.connect('http://host/api/ws?src=camera', surfaces, ['webrtc']);
websockets[0].fireOpen();
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', {
@@ -858,26 +814,6 @@ describe('Go2RTCSessionController', () => {
});
});
describe('microphone', () => {
it('should forward a microphone change to the WebRTC source', () => {
const { session, surfaces, websockets, webRTCSources } = setup();
session.connect('http://host/api/ws?src=camera', surfaces, ['webrtc']);
websockets[0].fireOpen();
const micStream = new FakeMediaStream([
new FakeMediaStreamTrack('audio'),
]).asMediaStream();
session.setMicrophoneStream(micStream);
expect(webRTCSources[0].setMicrophoneStream).toHaveBeenCalledWith(micStream);
});
it('should tolerate a microphone change with no WebRTC source', () => {
const { session } = setup();
expect(() => session.setMicrophoneStream(null)).not.toThrow();
});
});
describe('lifecycle', () => {
it('should stop the source and reconnect on unexpected closure', () => {
const { session, surfaces, websockets, binarySources, createWebSocket } = setup();
@@ -1078,16 +1014,6 @@ describe('Go2RTCSessionController', () => {
expect(mediaLoadedCallback).not.toHaveBeenCalled();
});
it('should swallow a rejected microphone update', async () => {
const { session, surfaces, websockets, webRTCSources } = setup();
session.connect('http://host/api/ws?src=camera', surfaces, ['webrtc']);
websockets[0].fireOpen();
webRTCSources[0].setMicrophoneStream.mockRejectedValue(new Error('replace'));
expect(() => session.setMicrophoneStream(null)).not.toThrow();
await Promise.resolve();
});
it('should ignore callbacks fired while a binary source is constructed', () => {
const websockets: FakeWebSocket[] = [];
const createWebSocket = vi.fn<(url: string) => WebSocket>(() => {
@@ -1112,7 +1038,6 @@ describe('Go2RTCSessionController', () => {
getCardWideConfig: () => null,
mediaLoadedCallback,
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket, createBinarySource },
);
@@ -1147,7 +1072,6 @@ describe('Go2RTCSessionController', () => {
getCardWideConfig: () => null,
mediaLoadedCallback,
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket, createWebRTCSource },
);
@@ -1174,7 +1098,6 @@ describe('Go2RTCSessionController', () => {
getCardWideConfig: () => null,
mediaLoadedCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket },
);
@@ -1206,7 +1129,6 @@ describe('Go2RTCSessionController', () => {
getCardWideConfig: () => null,
mediaLoadedCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket },
);
@@ -1247,7 +1169,6 @@ describe('Go2RTCSessionController', () => {
getCardWideConfig: () => null,
mediaLoadedCallback: vi.fn(),
streamErrorCallback: vi.fn(),
microphoneErrorCallback: vi.fn(),
},
{ createWebSocket, createBinarySource, createWebRTCSource },
);
@@ -1,254 +0,0 @@
import { describe, expect, it, vi } from 'vitest';
import { SignalingChannel } from '../../../../../src/components-lib/live/providers/go2rtc-experimental/signaling';
import { FakeWebSocket } from './test-utils';
describe('SignalingChannel', () => {
const setup = (options?: {
openCallback?: () => void;
disconnectCallback?: () => void;
}) => {
const websockets: FakeWebSocket[] = [];
const createWebSocket = vi.fn<(url: string) => WebSocket>(() => {
const websocket = new FakeWebSocket();
websockets.push(websocket);
return websocket.asWebSocket();
});
const channel = new SignalingChannel(
'ws://host/api/ws?src=camera',
{
openCallback: options?.openCallback,
disconnectCallback: options?.disconnectCallback,
},
{ createWebSocket },
);
return { channel, createWebSocket, websockets };
};
it('should connect with an arraybuffer binary type', () => {
const { channel, createWebSocket, websockets } = setup();
channel.connect();
expect(createWebSocket).toHaveBeenCalledWith('ws://host/api/ws?src=camera');
expect(websockets[0].binaryType).toBe('arraybuffer');
});
it('should not connect twice', () => {
const { channel, createWebSocket } = setup();
channel.connect();
channel.connect();
expect(createWebSocket).toHaveBeenCalledTimes(1);
});
it('should report open state and call the open callback', () => {
const openCallback = vi.fn();
const { channel, websockets } = setup({ openCallback });
channel.connect();
expect(channel.isOpen()).toBe(false);
websockets[0].fireOpen();
expect(channel.isOpen()).toBe(true);
expect(openCallback).toHaveBeenCalled();
});
it('should tolerate an absent open callback', () => {
const { channel, websockets } = setup();
channel.connect();
expect(() => websockets[0].fireOpen()).not.toThrow();
});
it('should not send before the connection is open', () => {
const { channel, websockets } = setup();
channel.connect();
channel.send({ type: 'mse', value: 'codecs' });
expect(websockets[0].send).not.toHaveBeenCalled();
});
it('should send JSON once open', () => {
const { channel, websockets } = setup();
channel.connect();
websockets[0].fireOpen();
const message = { type: 'mse', value: 'codecs' };
channel.send(message);
expect(websockets[0].sent).toEqual([JSON.stringify(message)]);
});
it('should dispatch parsed messages to subscribers', () => {
const { channel, websockets } = setup();
const callback = vi.fn();
channel.subscribeToMessages(callback);
channel.connect();
websockets[0].fireMessage('{"type":"mse","value":"video/mp4"}');
expect(callback).toHaveBeenCalledWith({ type: 'mse', value: 'video/mp4' });
});
it('should stop dispatching after unsubscribe', () => {
const { channel, websockets } = setup();
const callback = vi.fn();
const unsubscribe = channel.subscribeToMessages(callback);
channel.connect();
unsubscribe();
websockets[0].fireMessage('{"type":"mse"}');
expect(callback).not.toHaveBeenCalled();
});
it('should dispatch to remaining subscribers when one unsubscribes during dispatch', () => {
const { channel, websockets } = setup();
const secondCallback = vi.fn();
const unsubscribeDuringDispatch = vi.fn((): void => {
unsubscribe();
});
const unsubscribe = channel.subscribeToMessages(unsubscribeDuringDispatch);
channel.subscribeToMessages(secondCallback);
channel.connect();
websockets[0].fireMessage('{"type":"mse"}');
expect(unsubscribeDuringDispatch).toHaveBeenCalledTimes(1);
expect(secondCallback).toHaveBeenCalledTimes(1);
});
it('should ignore invalid JSON', () => {
const { channel, websockets } = setup();
const callback = vi.fn();
channel.subscribeToMessages(callback);
channel.connect();
websockets[0].fireMessage('NOT JSON');
expect(callback).not.toHaveBeenCalled();
});
it('should ignore malformed messages', () => {
const { channel, websockets } = setup();
const callback = vi.fn();
channel.subscribeToMessages(callback);
channel.connect();
websockets[0].fireMessage('{"type":6}');
expect(callback).not.toHaveBeenCalled();
});
it('should ignore unexpected data types', () => {
const { channel, websockets } = setup();
const callback = vi.fn();
channel.subscribeToMessages(callback);
channel.connect();
websockets[0].fireMessage(42);
expect(callback).not.toHaveBeenCalled();
});
it('should route binary data to the binary callback', () => {
const { channel, websockets } = setup();
const binaryCallback = vi.fn();
channel.setBinaryCallback(binaryCallback);
channel.connect();
const data = new ArrayBuffer(8);
websockets[0].fireMessage(data);
expect(binaryCallback).toHaveBeenCalledWith(data);
});
it('should drop binary data without a binary callback', () => {
const { channel, websockets } = setup();
channel.connect();
expect(() => websockets[0].fireMessage(new ArrayBuffer(8))).not.toThrow();
});
it('should drop binary data after the binary callback is cleared', () => {
const { channel, websockets } = setup();
const binaryCallback = vi.fn();
channel.setBinaryCallback(binaryCallback);
channel.setBinaryCallback(null);
channel.connect();
websockets[0].fireMessage(new ArrayBuffer(8));
expect(binaryCallback).not.toHaveBeenCalled();
});
it('should close the underlying websocket without firing the disconnect callback', () => {
const disconnectCallback = vi.fn();
const { channel, websockets } = setup({ disconnectCallback });
channel.connect();
websockets[0].fireOpen();
channel.close();
expect(websockets[0].close).toHaveBeenCalled();
expect(channel.isOpen()).toBe(false);
expect(disconnectCallback).not.toHaveBeenCalled();
});
it('should tolerate closing when never connected', () => {
const { channel } = setup();
expect(() => channel.close()).not.toThrow();
});
it('should ignore websocket events delivered after close', () => {
const openCallback = vi.fn();
const disconnectCallback = vi.fn();
const messageCallback = vi.fn();
const { channel, websockets } = setup({ openCallback, disconnectCallback });
channel.subscribeToMessages(messageCallback);
channel.connect();
channel.close();
websockets[0].fireOpen();
websockets[0].fireMessage('{"type":"mse"}');
websockets[0].fireClose();
expect(openCallback).not.toHaveBeenCalled();
expect(messageCallback).not.toHaveBeenCalled();
expect(disconnectCallback).not.toHaveBeenCalled();
});
it('should fire the disconnect callback on unexpected closure', () => {
const disconnectCallback = vi.fn();
const { channel, websockets } = setup({ disconnectCallback });
channel.connect();
websockets[0].fireOpen();
websockets[0].fireClose();
expect(disconnectCallback).toHaveBeenCalledTimes(1);
expect(channel.isOpen()).toBe(false);
});
it('should tolerate an absent disconnect callback on unexpected closure', () => {
const { channel, websockets } = setup();
channel.connect();
expect(() => websockets[0].fireClose()).not.toThrow();
});
it('should allow reconnecting after unexpected closure', () => {
const { channel, createWebSocket, websockets } = setup();
channel.connect();
websockets[0].fireClose();
channel.connect();
expect(createWebSocket).toHaveBeenCalledTimes(2);
});
it('should construct a real websocket by default', () => {
// A mock implementation must be callable with `new`, so it cannot be an
// arrow function.
const webSocketConstructor = vi.fn(function () {
return new FakeWebSocket().asWebSocket();
});
vi.stubGlobal('WebSocket', webSocketConstructor);
const channel = new SignalingChannel('ws://host/api/ws', {});
channel.connect();
channel.close();
expect(webSocketConstructor).toHaveBeenCalledWith('ws://host/api/ws');
vi.unstubAllGlobals();
});
});
@@ -568,7 +568,6 @@ describe('MSEStreamSource', () => {
expect(source.getCapabilities()).toEqual({
supportsPause: true,
hasAudio: false,
has2WayAudio: false,
});
});
@@ -579,7 +578,6 @@ describe('MSEStreamSource', () => {
expect(setupResult.source.getCapabilities()).toEqual({
supportsPause: true,
hasAudio: true,
has2WayAudio: false,
});
});
@@ -595,7 +593,6 @@ describe('MSEStreamSource', () => {
expect(setupResult.source.getCapabilities()).toEqual({
supportsPause: true,
hasAudio: false,
has2WayAudio: false,
});
});
@@ -5,17 +5,17 @@ import type {
StreamSourceContext,
VideoStreamTarget,
} from '../../../../../../src/components-lib/live/providers/go2rtc-experimental/types';
import { flushPromises } from '../../../../../test-utils';
import {
FakeMediaStream,
FakeMediaStreamTrack,
FakeRTCPeerConnection,
FakeStreamSourceChannel,
} from '../test-utils';
type FakeMediaStreamTrack,
} from '../../../../../go2rtc/test-utils';
import { flushPromises } from '../../../../../test-utils';
import { FakeStreamSourceChannel } from '../test-utils';
// @vitest-environment jsdom
describe('WebRTCStreamSource', () => {
const setup = (options?: { microphoneStream?: FakeMediaStream | null }) => {
const setup = () => {
const video = document.createElement('video');
const channel = new FakeStreamSourceChannel();
const loadedCallback = vi.fn();
@@ -29,13 +29,10 @@ 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 {
@@ -44,7 +41,6 @@ describe('WebRTCStreamSource', () => {
createPeerConnection,
failedCallback,
loadedCallback,
microphoneErrorCallback,
pc,
source,
video,
@@ -60,27 +56,14 @@ describe('WebRTCStreamSource', () => {
});
describe('transceivers', () => {
it('should pre-arm a sendonly audio transceiver and recvonly video and audio', () => {
it('should offer inbound video and audio only', () => {
const { source, pc } = setup();
source.start();
expect(pc.transceivers).toHaveLength(3);
expect(pc.transceivers[0].direction).toBe('sendonly');
expect(pc.transceivers[1].direction).toBe('recvonly');
expect(pc.transceivers[2].direction).toBe('recvonly');
// Kind-only pre-arm: no track, so no getUserMedia and no permission prompt.
expect(pc.transceivers[0].sender.track).toBeNull();
});
it('should pre-arm with the current microphone track', () => {
const micTrack = new FakeMediaStreamTrack('audio');
const { source, pc } = setup({
microphoneStream: new FakeMediaStream([micTrack]),
});
source.start();
expect(pc.transceivers[0].sender.track).toBe(micTrack);
expect(pc.transceivers.map((transceiver) => transceiver.direction)).toEqual([
'recvonly',
'recvonly',
]);
});
});
@@ -431,6 +414,17 @@ describe('WebRTCStreamSource', () => {
expect(source.getPeerConnection()).toBe(pc.asPeerConnection());
});
it('should report its media capabilities', () => {
const { source, pc } = setup();
source.start();
pc.fireConnectionStateChange('connected');
expect(source.getCapabilities()).toEqual({
supportsPause: true,
hasAudio: expect.any(Boolean),
});
});
it('should report webrtc technology', () => {
const { source } = setup();
@@ -462,139 +456,5 @@ describe('WebRTCStreamSource', () => {
hasAACAudio: false,
});
});
it('should report 2-way audio capability once a mic track is armed', () => {
const micTrack = new FakeMediaStreamTrack('audio');
const { source, pc } = setup({
microphoneStream: new FakeMediaStream([micTrack]),
});
source.start();
pc.fireConnectionStateChange('connected');
expect(source.getCapabilities().has2WayAudio).toBe(true);
expect(source.getCapabilities().supportsPause).toBe(true);
});
});
describe('setMicrophoneStream', () => {
it('should do nothing for an unchanged stream', async () => {
const stream = new FakeMediaStream([new FakeMediaStreamTrack('audio')]);
const { source, pc } = setup({ microphoneStream: stream });
source.start();
await source.setMicrophoneStream(stream.asMediaStream());
expect(pc.getMicrophoneTransceiver().sender.replaceTrack).not.toHaveBeenCalled();
});
it('should replace the outbound track without renegotiating', async () => {
const { source, pc } = setup();
source.start();
const newTrack = new FakeMediaStreamTrack('audio');
await source.setMicrophoneStream(new FakeMediaStream([newTrack]).asMediaStream());
expect(pc.getMicrophoneTransceiver().sender.replaceTrack).toHaveBeenCalledWith(
newTrack,
);
});
it('should clear the outbound track for a null stream', async () => {
const stream = new FakeMediaStream([new FakeMediaStreamTrack('audio')]);
const { source, pc } = setup({ microphoneStream: stream });
source.start();
await source.setMicrophoneStream(null);
expect(pc.getMicrophoneTransceiver().sender.replaceTrack).toHaveBeenCalledWith(
null,
);
});
it('should do nothing before there is a peer connection', async () => {
const { source, microphoneErrorCallback } = setup();
await source.setMicrophoneStream(
new FakeMediaStream([new FakeMediaStreamTrack('audio')]).asMediaStream(),
);
expect(microphoneErrorCallback).not.toHaveBeenCalled();
});
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 DOMException('replace failed', 'InvalidStateError'),
);
await source.setMicrophoneStream(
new FakeMediaStream([new FakeMediaStreamTrack('audio')]).asMediaStream(),
);
// 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, microphoneErrorCallback } = setup();
source.start();
let rejectReplace: (reason: Error) => void = () => {};
pc.getMicrophoneTransceiver().sender.replaceTrack.mockReturnValue(
new Promise((_resolve, reject) => {
rejectReplace = reject;
}),
);
const promise = source.setMicrophoneStream(
new FakeMediaStream([new FakeMediaStreamTrack('audio')]).asMediaStream(),
);
source.stop();
rejectReplace(new Error('replace failed'));
await promise;
expect(microphoneErrorCallback).not.toHaveBeenCalled();
});
});
});
@@ -1,15 +1,15 @@
import { vi, type Mock } from 'vitest';
import { vi } from 'vitest';
import type {
MediaSourceFactory,
MediaSourceInterface,
} from '../../../../../src/components-lib/live/providers/go2rtc-experimental/adapters/media-source';
import type { StreamSourceChannel } from '../../../../../src/components-lib/live/providers/go2rtc-experimental/types';
import type {
BinaryCallback,
Go2RTCMessage,
MessageCallback,
StreamSourceChannel,
} from '../../../../../src/components-lib/live/providers/go2rtc-experimental/types';
} from '../../../../../src/go2rtc/messages';
import type { UnsubscribeCallback } from '../../../../../src/types';
// ===========================================================================
@@ -28,32 +28,6 @@ export const SAFARI_17_USER_AGENT =
// Fakes for browser APIs jsdom does not provide.
// ===========================================================================
export class FakeWebSocket extends EventTarget {
public binaryType = '';
public sent: string[] = [];
public close = vi.fn();
public send = vi.fn((data: string): void => {
this.sent.push(data);
});
public asWebSocket(): WebSocket {
return this as unknown as WebSocket;
}
public fireOpen(): void {
this.dispatchEvent(new Event('open'));
}
public fireClose(): void {
this.dispatchEvent(new Event('close'));
}
public fireMessage(data: unknown): void {
this.dispatchEvent(new MessageEvent('message', { data }));
}
}
export const createTimeRanges = (ranges: [number, number][]): TimeRanges => ({
length: ranges.length,
start: (index: number) => ranges[index][0],
@@ -77,129 +51,6 @@ class FakeSourceBuffer extends EventTarget {
}
}
export class FakeMediaStreamTrack extends EventTarget {
public muted = false;
public kind: string;
constructor(kind: string) {
super();
this.kind = kind;
}
public asTrack(): MediaStreamTrack {
return this as unknown as MediaStreamTrack;
}
public setMuted(muted: boolean): void {
this.muted = muted;
this.dispatchEvent(new Event(muted ? 'mute' : 'unmute'));
}
}
export class FakeMediaStream {
private _tracks: FakeMediaStreamTrack[];
constructor(tracks: FakeMediaStreamTrack[] = []) {
this._tracks = tracks;
}
public getTracks(): FakeMediaStreamTrack[] {
return this._tracks;
}
public getVideoTracks(): FakeMediaStreamTrack[] {
return this._tracks.filter((track) => track.kind === 'video');
}
public getAudioTracks(): FakeMediaStreamTrack[] {
return this._tracks.filter((track) => track.kind === 'audio');
}
public asMediaStream(): MediaStream {
return this as unknown as MediaStream;
}
}
class FakeRTCTransceiver {
public direction: string;
public currentDirection: string;
public sender: {
track: FakeMediaStreamTrack | null;
replaceTrack: Mock<(track: MediaStreamTrack | null) => Promise<void>>;
};
public receiver: { track: FakeMediaStreamTrack };
constructor(direction: string, kind: string, track: FakeMediaStreamTrack | null) {
this.direction = direction;
this.currentDirection = direction;
this.sender = {
track,
replaceTrack: vi.fn<(track: MediaStreamTrack | null) => Promise<void>>(() =>
Promise.resolve(),
),
};
this.receiver = { track: new FakeMediaStreamTrack(kind) };
}
}
export class FakeRTCPeerConnection extends EventTarget {
public connectionState: RTCPeerConnectionState = 'new';
public remoteDescription: { sdp: string } | null = null;
public transceivers: FakeRTCTransceiver[] = [];
public createOffer = vi.fn(
(): Promise<{ type: string; sdp?: string }> =>
Promise.resolve({ type: 'offer', sdp: 'v=0\r\noffer' }),
);
public setLocalDescription = vi.fn(() => Promise.resolve());
public setRemoteDescription = vi.fn((description: { sdp: string }) => {
this.remoteDescription = description;
return Promise.resolve();
});
public addIceCandidate = vi.fn(() => Promise.resolve());
public close = vi.fn();
public addTransceiver(
trackOrKind: FakeMediaStreamTrack | string,
init: { direction: string },
): FakeRTCTransceiver {
const kind = typeof trackOrKind === 'string' ? trackOrKind : trackOrKind.kind;
const track = typeof trackOrKind === 'string' ? null : trackOrKind;
const transceiver = new FakeRTCTransceiver(init.direction, kind, track);
this.transceivers.push(transceiver);
return transceiver;
}
public getTransceivers(): FakeRTCTransceiver[] {
return this.transceivers;
}
public getReceivers(): { track: FakeMediaStreamTrack }[] {
return this.transceivers.map((transceiver) => transceiver.receiver);
}
public asPeerConnection(): RTCPeerConnection {
return this as unknown as RTCPeerConnection;
}
public getMicrophoneTransceiver(): FakeRTCTransceiver {
return this.transceivers[0];
}
public fireConnectionStateChange(state: RTCPeerConnectionState): void {
this.connectionState = state;
this.dispatchEvent(new Event('connectionstatechange'));
}
public fireIceCandidate(candidate: string | null): void {
const event = new Event('icecandidate');
Object.assign(event, {
candidate: candidate === null ? null : { candidate },
});
this.dispatchEvent(event);
}
}
// ===========================================================================
// Fakes for custom interfaces.
// ===========================================================================
@@ -1,29 +0,0 @@
import { describe, expect, it } from 'vitest';
import { isServerErrorForMode } from '../../../../../../src/components-lib/live/providers/go2rtc-experimental/utils/messages';
describe('isServerErrorForMode', () => {
it('should match an error for the mode', () => {
expect(isServerErrorForMode({ type: 'error', value: 'mse: not found' }, 'mse')).toBe(
true,
);
});
it('should not match an error for another mode', () => {
expect(isServerErrorForMode({ type: 'error', value: 'webrtc: failed' }, 'mse')).toBe(
false,
);
});
it('should not match a non-error message', () => {
expect(isServerErrorForMode({ type: 'mse', value: 'codecs' }, 'mse')).toBe(false);
});
it('should not match an error with a non-string value', () => {
expect(isServerErrorForMode({ type: 'error', value: 42 }, 'mse')).toBe(false);
});
it('should not match an error with no value', () => {
expect(isServerErrorForMode({ type: 'error' }, 'mse')).toBe(false);
});
});