Files
advanced-camera-card/tests/ha/resolve-endpoint.test.ts
Dermot Duffy 11cc543406 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
2026-08-21 20:13:58 -07:00

149 lines
4.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { EnabledProxyConfig } from '../../src/config/schema/common/proxy.js';
import {
PROXY_URL_SIGN_EXPIRY_SECONDS,
resolveEndpointURL,
} from '../../src/ha/resolve-endpoint.js';
import { homeAssistantGetSignedURLIfNecessary } from '../../src/ha/sign-path.js';
import { createProxiedEndpointIfNecessary } from '../../src/ha/web-proxy.js';
import { createHASS } from '../test-utils.js';
vi.mock('../../src/ha/sign-path.js');
vi.mock('../../src/ha/web-proxy.js');
const createEnabledProxyConfig = (
config: Partial<EnabledProxyConfig> = {},
): EnabledProxyConfig => ({
ssl_verification: true,
ssl_ciphers: 'default',
dynamic: true,
enabled: true,
...config,
});
// @vitest-environment jsdom
describe('resolveEndpointURL', () => {
beforeEach(() => {
vi.mocked(createProxiedEndpointIfNecessary).mockImplementation(
async (_hass, endpoint) => endpoint,
);
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockImplementation(
async (_hass, endpoint) => endpoint.endpoint,
);
});
afterEach(() => {
vi.restoreAllMocks();
});
it('should resolve an absolute endpoint', async () => {
expect(
await resolveEndpointURL(createHASS(), {
endpoint: 'http://go2rtc/api/ws',
sign: false,
}),
).toEqual({ success: true, url: 'http://go2rtc/api/ws' });
});
it('should make a relative endpoint absolute before using it', async () => {
// Proxy registration and signing both reject a relative URL.
await resolveEndpointURL(createHASS(), { endpoint: '/api/ws', sign: true });
expect(homeAssistantGetSignedURLIfNecessary).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
endpoint: new URL('/api/ws', document.baseURI).toString(),
}),
PROXY_URL_SIGN_EXPIRY_SECONDS,
);
});
it('should not proxy when proxying is disabled', async () => {
await resolveEndpointURL(
createHASS(),
{ endpoint: 'http://go2rtc/api/ws', sign: false },
{ proxyConfig: createEnabledProxyConfig({ enabled: false }) },
);
expect(createProxiedEndpointIfNecessary).not.toHaveBeenCalled();
});
it('should proxy when proxying is enabled', async () => {
await resolveEndpointURL(
createHASS(),
{ endpoint: 'http://go2rtc/api/ws', sign: false },
{
proxyConfig: createEnabledProxyConfig(),
proxyEndpointOptions: { websocket: true },
},
);
expect(createProxiedEndpointIfNecessary).toHaveBeenCalledWith(
expect.anything(),
// Signing is applied afterwards, to whatever the proxy returns.
{ endpoint: 'http://go2rtc/api/ws', sign: false },
expect.objectContaining({ enabled: true }),
{ ttl: PROXY_URL_SIGN_EXPIRY_SECONDS, openLimit: 0, websocket: true },
);
});
it('should sign whatever the proxy returned', async () => {
vi.mocked(createProxiedEndpointIfNecessary).mockResolvedValue({
endpoint: 'http://ha/api/hass_web_proxy/v0/ws?url=go2rtc',
sign: true,
});
expect(
await resolveEndpointURL(
createHASS(),
{ endpoint: 'http://go2rtc/api/ws', sign: false },
{ proxyConfig: createEnabledProxyConfig() },
),
).toEqual({
success: true,
url: 'http://ha/api/hass_web_proxy/v0/ws?url=go2rtc',
});
});
it('should report a proxy that is unavailable', async () => {
vi.mocked(createProxiedEndpointIfNecessary).mockResolvedValue(null);
expect(
await resolveEndpointURL(
createHASS(),
{ endpoint: 'http://go2rtc/api/ws', sign: false },
{ proxyConfig: createEnabledProxyConfig() },
),
).toEqual({ success: false, error: 'proxy' });
});
it('should report a proxy that throws', async () => {
vi.mocked(createProxiedEndpointIfNecessary).mockRejectedValue(new Error('nope'));
expect(
await resolveEndpointURL(
createHASS(),
{ endpoint: 'http://go2rtc/api/ws', sign: false },
{ proxyConfig: createEnabledProxyConfig() },
),
).toEqual({ success: false, error: 'proxy' });
});
it('should report signing that fails', async () => {
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockResolvedValue(null);
expect(
await resolveEndpointURL(createHASS(), { endpoint: '/api/ws', sign: true }),
).toEqual({ success: false, error: 'sign' });
});
it('should report signing that throws', async () => {
vi.mocked(homeAssistantGetSignedURLIfNecessary).mockRejectedValue(new Error('nope'));
expect(
await resolveEndpointURL(createHASS(), { endpoint: '/api/ws', sign: true }),
).toEqual({ success: false, error: 'sign' });
});
});