matterbridge-example-camera 0.0.3 → 0.0.4-dev-20260719-49470d3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/README.md +25 -2
- package/assets/camera-color-1280-720.jpeg +0 -0
- package/assets/camera-color-1920-1080.jpeg +0 -0
- package/assets/camera-color-640-480.jpeg +0 -0
- package/assets/camera-color-test-1280-720-low.jpeg +0 -0
- package/assets/camera-color-test-1280-720-low20.jpg +0 -0
- package/assets/camera-color-test-1280-720.jpeg +0 -0
- package/assets/camera-color-test-1920-1080-low.jpeg +0 -0
- package/assets/camera-color-test-1920-1080-low20.jpg +0 -0
- package/assets/camera-color-test-1920-1080.jpeg +0 -0
- package/assets/camera-color-test-480-270.jpeg +0 -0
- package/assets/camera-color-test-640-480-low.jpg +0 -0
- package/assets/camera-color-test-640-480-low20.jpg +0 -0
- package/assets/camera-color-test-640-480.jpeg +0 -0
- package/assets/camera-color-test-960-540.jpeg +0 -0
- package/dist/behaviors/cameraAvStreamManagementServer.js +12 -5
- package/dist/behaviors/webRtcTransportProviderServer.js +179 -17
- package/dist/devices/camera.js +7 -1
- package/dist/devices/snapshotCamera.js +3 -6
- package/dist/webrtc/weriftSession.js +322 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
[](https://github.com/microsoft/typescript-go)
|
|
16
16
|
[](https://nodejs.org/)
|
|
17
17
|
[](https://matterbridge.io)
|
|
18
|
+

|
|
18
19
|
|
|
19
20
|
[](https://www.npmjs.com/package/matterbridge)
|
|
20
21
|
[](https://www.npmjs.com/package/matter-history)
|
|
@@ -27,6 +28,42 @@ If you like this project and find it useful, please consider giving it a star on
|
|
|
27
28
|
|
|
28
29
|
<a href="https://www.buymeacoffee.com/luligugithub"><img src="https://matterbridge.io/assets/bmc-button.svg" alt="Buy me a coffee" width="120"></a>
|
|
29
30
|
|
|
31
|
+
## [0.0.5] - Dev branch
|
|
32
|
+
|
|
33
|
+
## Added
|
|
34
|
+
|
|
35
|
+
- [wirift]: Add codec negotiation and ffmpeg windows path.
|
|
36
|
+
|
|
37
|
+
## Changed
|
|
38
|
+
|
|
39
|
+
- [package]: Update dependencies.
|
|
40
|
+
- [package]: Upgrade package.
|
|
41
|
+
|
|
42
|
+
<a href="https://www.buymeacoffee.com/luligugithub"><img src="https://matterbridge.io/assets/bmc-button.svg" alt="Buy me a coffee" width="80"></a>
|
|
43
|
+
|
|
44
|
+
## [0.0.4] - 2026-07-19
|
|
45
|
+
|
|
46
|
+
### Added
|
|
47
|
+
|
|
48
|
+
- [webrtc]: `WebRtcTransportProvider` now negotiates real SDP offers/answers through a werift `RTCPeerConnection` (`WeriftWebRtcSession`) instead of a placeholder SDP string, and applies real ICE candidates and connection teardown.
|
|
49
|
+
- [webrtc]: Injects a real video track into the negotiated connection via ffmpeg, so the media path can be validated end to end without a physical camera. Defaults to a synthetic SMPTE bars test pattern, or captures from a real local webcam (`MATTERBRIDGE_CAMERA_VIDEO_SOURCE=webcam`, `MATTERBRIDGE_CAMERA_WEBCAM_DEVICE`) at 640x480/1280x720/1920x1080, following the resolution the client actually allocated via `CameraAvStreamManagement.VideoStreamAllocate`. See the README for the full list of environment variables.
|
|
50
|
+
- [webrtc]: `SolicitOffer`/`ProvideOffer` now automatically select or allocate a video/audio stream when the client omits `videoStreams`/`audioStreams` (and their deprecated single-id counterparts), per the Matter specification's automatic stream selection for revision 1 clients. This is required to interoperate with Home Assistant's Matter camera integration ([home-assistant/core#176080](https://github.com/home-assistant/core/pull/176080)), which never allocates streams explicitly and expects the camera to select them on its own.
|
|
51
|
+
|
|
52
|
+
### Changed
|
|
53
|
+
|
|
54
|
+
- [webrtc]: Offer/Answer invokes now address the peer's `WebRtcTransportRequestor` directly using the peer node id captured from the session (matching matter.js's OTA Provider/Requestor pattern), instead of the Binding cluster.
|
|
55
|
+
- [webrtc]: Improved ICE candidate handling and WebRTC session logging, including logging why the peer's `WebRtcTransportRequestor` endpoint couldn't be resolved (previously a silent failure), and closing dangling WebRTC sessions left open when the requestor is unreachable.
|
|
56
|
+
- [camera]: Document in the JSDoc that the CameraAvStreamManagement Snapshot feature is implemented.
|
|
57
|
+
- [snapshot camera]: Document the CameraAvStreamManagement features implemented in the JSDoc.
|
|
58
|
+
|
|
59
|
+
### Fixed
|
|
60
|
+
|
|
61
|
+
- [webrtc]: `ProvideOfferResponse`/`SolicitOfferResponse` now echo back the deprecated `videoStreamId`/`audioStreamId` fields when the request used them, as required by the Matter specification's conformance rules. Revision 1 clients (e.g. Home Assistant) send these fields as `null` to request automatic stream selection, and rely on the echoed value to learn which stream was selected; without it, they could not determine that a stream had in fact been negotiated.
|
|
62
|
+
- [camera]: Fix `Camera` to provide default `snapshotCapabilities` and `allocatedSnapshotStreams` values for the CameraAvStreamManagement Snapshot feature, which was enabled but left the `SnapshotCapabilities` attribute as an empty list.
|
|
63
|
+
- [snapshot]: `CaptureSnapshot` calibration cards were 480×270 and 960×540, which don't match any standard camera resolution. Regenerated the two cards as basic SMPTE color-bars test patterns (mires) at 640×480 and 1280×720 1920-1080, matching the resolutions the webcam capture path actually negotiates. `Camera` and `SnapshotCamera` now default `snapshotCapabilities` to advertise all three resolutions, since `CaptureSnapshot` can genuinely serve a matching calibration card for each.
|
|
64
|
+
|
|
65
|
+
<a href="https://www.buymeacoffee.com/luligugithub"><img src="https://matterbridge.io/assets/bmc-button.svg" alt="Buy me a coffee" width="80"></a>
|
|
66
|
+
|
|
30
67
|
## [0.0.3] - 2026-07-17
|
|
31
68
|
|
|
32
69
|
### Breaking changes
|
package/README.md
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
[](https://github.com/microsoft/typescript-go)
|
|
14
14
|
[](https://nodejs.org/)
|
|
15
15
|
[](https://matterbridge.io)
|
|
16
|
+

|
|
16
17
|
|
|
17
18
|
[](https://www.npmjs.com/package/matterbridge)
|
|
18
19
|
[](https://www.npmjs.com/package/matter-history)
|
|
@@ -59,6 +60,7 @@ Features:
|
|
|
59
60
|
|
|
60
61
|
- Exposes the Camera AV Stream Management cluster with the Video, Audio and ImageControl features (the Snapshot feature is not implemented in this example; see Snapshot Camera).
|
|
61
62
|
- Exposes the WebRtcTransportProvider cluster and registers a WebRtcTransportRequestor client, so a bound device can solicit and receive WebRTC offers.
|
|
63
|
+
- Automatically selects or allocates a video/audio stream when a client's `SolicitOffer`/`ProvideOffer` omits `videoStreams`/`audioStreams` (and their deprecated single-id counterparts), per the Matter specification's automatic stream selection for revision 1 clients. This is required to interoperate with clients that never allocate streams explicitly, such as Home Assistant's Matter camera integration.
|
|
62
64
|
- Supports configurable stream usages and priorities, encoder limits, video sensor parameters, viewport, rate-distortion trade-off points, and microphone capabilities.
|
|
63
65
|
- Optional Identify cluster support, with configurable identify time and type. Set to Identify.IdentifyType.None to omit the cluster entirely.
|
|
64
66
|
- Configurable Power Source cluster type: Rechargeable, Replaceable, Battery, Wired, or None to omit the Power Source cluster entirely.
|
|
@@ -75,6 +77,27 @@ Features:
|
|
|
75
77
|
- Optional Identify cluster support, with configurable identify time and type. Set to Identify.IdentifyType.None to omit the cluster entirely.
|
|
76
78
|
- Configurable Power Source cluster type: Rechargeable, Replaceable, Battery, Wired, or None to omit the Power Source cluster entirely.
|
|
77
79
|
|
|
80
|
+
## WebRTC test video injection
|
|
81
|
+
|
|
82
|
+
`WeriftWebRtcSession` (see `src/webrtc/weriftSession.ts`) wraps a real werift `RTCPeerConnection` for each WebRtcTransportProvider session (see `MatterbridgeWebRtcTransportProviderServer` in `src/behaviors/webRtcTransportProviderServer.ts`), so the session's SDP offer/answer and ICE candidates are handled by a real WebRTC peer connection instead of being just recorded. It can also inject a real ffmpeg-generated video track into the negotiated connection, so the end-to-end media path can be validated without a real camera capture pipeline.
|
|
83
|
+
|
|
84
|
+
The video source defaults to a synthetic SMPTE bars test pattern, or can be switched to a real local webcam. Configure it with environment variables:
|
|
85
|
+
|
|
86
|
+
- `MATTERBRIDGE_CAMERA_DISABLE_TEST_VIDEO=1`: disables video injection entirely (only the negotiated transceiver is created, with no track attached).
|
|
87
|
+
- `MATTERBRIDGE_CAMERA_VIDEO_SOURCE=webcam`: capture from a local webcam via ffmpeg instead of the SMPTE bars test pattern. Requires `MATTERBRIDGE_CAMERA_WEBCAM_DEVICE`; falls back to the test pattern (with a warning) if unset.
|
|
88
|
+
- `MATTERBRIDGE_CAMERA_WEBCAM_DEVICE=<device>`: the OS-specific ffmpeg device identifier — e.g. `/dev/video0` on Linux (v4l2), an avfoundation index such as `0` on macOS, or a device name such as `Integrated Camera` on Windows (dshow).
|
|
89
|
+
- `MATTERBRIDGE_CAMERA_WEBCAM_RESOLUTION=<width>x<height>`: default webcam capture resolution — `640x480` (default), `1280x720`, or `1920x1080`. Falls back to `640x480` (with a warning) for unsupported values. The actual achievable frame rate depends on the webcam and can be much lower than 30 FPS at higher resolutions (check with `v4l2-ctl -d <device> --list-formats-ext` on Linux).
|
|
90
|
+
|
|
91
|
+
A real client's resolution/quality picker (e.g. in Home Assistant) takes precedence over `MATTERBRIDGE_CAMERA_WEBCAM_RESOLUTION`: it allocates a video stream with `CameraAvStreamManagement.VideoStreamAllocate` before soliciting or providing a WebRTC offer, and `MatterbridgeWebRtcTransportProviderServer` looks up that stream's `maxResolution` to select the webcam capture resolution for the session. `MATTERBRIDGE_CAMERA_WEBCAM_RESOLUTION` is used when no matching allocated stream is found, or when the requested resolution isn't one of the three supported above.
|
|
92
|
+
|
|
93
|
+
Requires `ffmpeg` to be installed and reachable on `PATH` (or under `/usr/bin`, `/bin`, or `/usr/local/bin`).
|
|
94
|
+
|
|
95
|
+
Example, capturing from a real Linux webcam at 720p:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
MATTERBRIDGE_CAMERA_VIDEO_SOURCE=webcam MATTERBRIDGE_CAMERA_WEBCAM_DEVICE=/dev/video0 MATTERBRIDGE_CAMERA_WEBCAM_RESOLUTION=1280x720 npm start
|
|
99
|
+
```
|
|
100
|
+
|
|
78
101
|
## Werift integration test
|
|
79
102
|
|
|
80
103
|
The `vitest/werift.test.ts` integration test creates local client and server peers and verifies SDP offer/answer negotiation, ICE candidate exchange, a bidirectional data-channel transfer, and connection teardown with `werift`.
|
|
@@ -123,8 +146,8 @@ The `assets` directory contains deterministic three-second media fixtures for ex
|
|
|
123
146
|
- `test-audio.opus`: Ogg container with mono Opus audio at 48 kHz and 64 kbit/s, containing a 1 kHz test tone. Use the Opus packets for an audio RTP track; the Ogg container itself is not sent over WebRTC.
|
|
124
147
|
- `test-camera.mp4`: playable reference containing the same 640×360 H.264 test pattern and a mono 1 kHz AAC track. The werift test transfers the complete file over its SCTP data channel and verifies its integrity. This exercises binary file transport, not a WebRTC video RTP track.
|
|
125
148
|
- `camera-color-test-1920-1080.jpeg`: 1920×1080 broadcast-style snapshot calibration card returned by the example's `CaptureSnapshot` command. It contains color bars, grayscale references, geometry targets, focus patterns, safe-area guides, and near-black/near-white patches for inspecting hue, saturation, brightness, contrast, geometry, overscan, and focus.
|
|
126
|
-
- `camera-color-test-
|
|
127
|
-
- `camera-color-test-480
|
|
149
|
+
- `camera-color-test-1280-720.jpeg`: 1280×720 SMPTE color-bars test pattern (mire) returned by the example's `CaptureSnapshot` command, generated with `ffmpeg -f lavfi -i smptebars=size=1280x720`.
|
|
150
|
+
- `camera-color-test-640-480.jpeg`: 640×480 SMPTE color-bars test pattern (mire) returned by the example's `CaptureSnapshot` command, generated with `ffmpeg -f lavfi -i smptebars=size=640x480`.
|
|
128
151
|
|
|
129
152
|
WebRTC media tracks transport encoded H.264 or Opus frames in RTP packets; they do not send an MP4, Ogg, or MPEG container directly. The current MP4 transfer deliberately uses the separate data-channel path. A future video-track test should parse the relevant elementary frames, packetize them as RTP, call werift's media track `writeRtp()`, and verify reception through `onTrack` and `onReceiveRtp`.
|
|
130
153
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -3,9 +3,15 @@ import { MatterbridgeServer } from 'matterbridge/behaviors';
|
|
|
3
3
|
import { CameraAvStreamManagementServer } from 'matterbridge/matter/behaviors';
|
|
4
4
|
import { CameraAvStreamManagement } from 'matterbridge/matter/clusters';
|
|
5
5
|
import { Status, StatusResponseError } from 'matterbridge/matter/types';
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
6
|
+
const DEFAULT_CAMERA_COLOR_TEST_RESOLUTION = '640x480';
|
|
7
|
+
const cameraColorTestJpegs = {
|
|
8
|
+
'640x480': { data: readFileSync(new URL('../../assets/camera-color-640-480.jpeg', import.meta.url)), resolution: { width: 640, height: 480 } },
|
|
9
|
+
'1280x720': { data: readFileSync(new URL('../../assets/camera-color-1280-720.jpeg', import.meta.url)), resolution: { width: 1280, height: 720 } },
|
|
10
|
+
'1920x1080': { data: readFileSync(new URL('../../assets/camera-color-1920-1080.jpeg', import.meta.url)), resolution: { width: 1920, height: 1080 } },
|
|
11
|
+
};
|
|
12
|
+
export function cameraColorTestJpegForResolution(requestedResolution) {
|
|
13
|
+
return cameraColorTestJpegs[`${requestedResolution.width}x${requestedResolution.height}`] ?? cameraColorTestJpegs[DEFAULT_CAMERA_COLOR_TEST_RESOLUTION];
|
|
14
|
+
}
|
|
9
15
|
export class MatterbridgeCameraAvStreamManagementServer extends CameraAvStreamManagementServer.with(CameraAvStreamManagement.Feature.Video, CameraAvStreamManagement.Feature.Audio, CameraAvStreamManagement.Feature.Snapshot, CameraAvStreamManagement.Feature.ImageControl) {
|
|
10
16
|
setStreamPriorities(request) {
|
|
11
17
|
const device = this.endpoint.stateOf(MatterbridgeServer);
|
|
@@ -131,10 +137,11 @@ export class MatterbridgeCameraAvStreamManagementServer extends CameraAvStreamMa
|
|
|
131
137
|
const device = this.endpoint.stateOf(MatterbridgeServer);
|
|
132
138
|
device.log.info(`Capturing snapshot ${request.snapshotStreamId ?? 'auto'} (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
133
139
|
device.log.debug(`MatterbridgeCameraAvStreamManagementServer: captureSnapshot called with snapshotStreamId ${request.snapshotStreamId}`);
|
|
140
|
+
const { data, resolution } = cameraColorTestJpegForResolution(request.requestedResolution);
|
|
134
141
|
return {
|
|
135
|
-
data
|
|
142
|
+
data,
|
|
136
143
|
imageCodec: CameraAvStreamManagement.ImageCodec.Jpeg,
|
|
137
|
-
resolution
|
|
144
|
+
resolution,
|
|
138
145
|
};
|
|
139
146
|
}
|
|
140
147
|
}
|
|
@@ -1,9 +1,35 @@
|
|
|
1
1
|
import { MatterbridgeBindingServer, MatterbridgeServer } from 'matterbridge/behaviors';
|
|
2
|
+
import { Node } from 'matterbridge/matter';
|
|
2
3
|
import { WebRtcTransportProviderServer, WebRtcTransportRequestorClient } from 'matterbridge/matter/behaviors';
|
|
3
4
|
import { WebRtcTransportRequestor } from 'matterbridge/matter/clusters';
|
|
4
5
|
import { EndpointNumber, FabricIndex, NodeId, StreamUsage, Status, StatusResponseError } from 'matterbridge/matter/types';
|
|
5
|
-
|
|
6
|
+
import { WeriftWebRtcSession } from '../webrtc/weriftSession.js';
|
|
7
|
+
import { MatterbridgeCameraAvStreamManagementServer } from './cameraAvStreamManagementServer.js';
|
|
8
|
+
const DEFERRED_INVOKE_DELAY_MS = 250;
|
|
6
9
|
export class MatterbridgeWebRtcTransportProviderServer extends WebRtcTransportProviderServer {
|
|
10
|
+
#invokeDeferred(action, description) {
|
|
11
|
+
const device = this.endpoint.stateOf(MatterbridgeServer);
|
|
12
|
+
setTimeout(() => {
|
|
13
|
+
Promise.resolve(action()).catch((error) => {
|
|
14
|
+
device.log.error(`Failed to invoke ${description} on the peer's WebRtcTransportRequestor: ${error instanceof Error ? error.message : String(error)}`);
|
|
15
|
+
});
|
|
16
|
+
}, DEFERRED_INVOKE_DELAY_MS);
|
|
17
|
+
}
|
|
18
|
+
async #resolvePeerRequestorEndpoint(peerNodeId, fabricIndex, peerEndpointId) {
|
|
19
|
+
try {
|
|
20
|
+
const serverNode = Node.forEndpoint(this.endpoint);
|
|
21
|
+
const peerNode = await serverNode.peers.forAddress({ nodeId: peerNodeId, fabricIndex });
|
|
22
|
+
const peerEndpoint = peerNode.endpoints.require(peerEndpointId);
|
|
23
|
+
peerEndpoint.behaviors.require(WebRtcTransportRequestorClient);
|
|
24
|
+
return peerEndpoint;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
this.endpoint
|
|
28
|
+
.stateOf(MatterbridgeServer)
|
|
29
|
+
.log.debug(`Could not resolve peer WebRtcTransportRequestor endpoint (peerNodeId=${peerNodeId}, fabricIndex=${fabricIndex}, peerEndpointId=${peerEndpointId}): ${error instanceof Error ? error.message : String(error)}`);
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
7
33
|
#getPeerInfo() {
|
|
8
34
|
const session = this.context.session;
|
|
9
35
|
return {
|
|
@@ -17,10 +43,69 @@ export class MatterbridgeWebRtcTransportProviderServer extends WebRtcTransportPr
|
|
|
17
43
|
audioStreams: request.audioStreams ?? (request.audioStreamId !== undefined && request.audioStreamId !== null ? [request.audioStreamId] : undefined),
|
|
18
44
|
};
|
|
19
45
|
}
|
|
46
|
+
#resolveVideoResolution(videoStreams) {
|
|
47
|
+
const videoStreamId = videoStreams?.[0];
|
|
48
|
+
if (videoStreamId === undefined)
|
|
49
|
+
return undefined;
|
|
50
|
+
if (!this.endpoint.behaviors.has(MatterbridgeCameraAvStreamManagementServer))
|
|
51
|
+
return undefined;
|
|
52
|
+
const { allocatedVideoStreams } = this.endpoint.stateOf(MatterbridgeCameraAvStreamManagementServer);
|
|
53
|
+
const stream = allocatedVideoStreams.find((allocatedStream) => allocatedStream.videoStreamId === videoStreamId);
|
|
54
|
+
if (!stream)
|
|
55
|
+
return undefined;
|
|
56
|
+
return `${stream.maxResolution.width}x${stream.maxResolution.height}`;
|
|
57
|
+
}
|
|
58
|
+
async #autoAssignStreams(streamUsage) {
|
|
59
|
+
if (!this.endpoint.behaviors.has(MatterbridgeCameraAvStreamManagementServer))
|
|
60
|
+
return {};
|
|
61
|
+
const state = this.endpoint.stateOf(MatterbridgeCameraAvStreamManagementServer);
|
|
62
|
+
const allocatedVideoStreams = [...state.allocatedVideoStreams];
|
|
63
|
+
let videoStreamId = (allocatedVideoStreams.find((stream) => stream.streamUsage === streamUsage) ?? allocatedVideoStreams[0])?.videoStreamId;
|
|
64
|
+
if (videoStreamId === undefined && state.rateDistortionTradeOffPoints.length > 0) {
|
|
65
|
+
const [{ codec, resolution, minBitRate }] = state.rateDistortionTradeOffPoints;
|
|
66
|
+
({ videoStreamId } = await this.endpoint.act((agent) => agent.get(MatterbridgeCameraAvStreamManagementServer).videoStreamAllocate({
|
|
67
|
+
streamUsage,
|
|
68
|
+
videoCodec: codec,
|
|
69
|
+
minFrameRate: 1,
|
|
70
|
+
maxFrameRate: state.videoSensorParams.maxFps,
|
|
71
|
+
minResolution: state.minViewportResolution,
|
|
72
|
+
maxResolution: resolution,
|
|
73
|
+
minBitRate,
|
|
74
|
+
maxBitRate: minBitRate,
|
|
75
|
+
keyFrameInterval: 4000,
|
|
76
|
+
})));
|
|
77
|
+
}
|
|
78
|
+
const allocatedAudioStreams = [...state.allocatedAudioStreams];
|
|
79
|
+
let audioStreamId = (allocatedAudioStreams.find((stream) => stream.streamUsage === streamUsage) ?? allocatedAudioStreams[0])?.audioStreamId;
|
|
80
|
+
if (audioStreamId === undefined && state.microphoneCapabilities.supportedCodecs.length > 0) {
|
|
81
|
+
const { microphoneCapabilities } = state;
|
|
82
|
+
({ audioStreamId } = await this.endpoint.act((agent) => agent.get(MatterbridgeCameraAvStreamManagementServer).audioStreamAllocate({
|
|
83
|
+
streamUsage,
|
|
84
|
+
audioCodec: microphoneCapabilities.supportedCodecs[0],
|
|
85
|
+
channelCount: microphoneCapabilities.maxNumberOfChannels,
|
|
86
|
+
sampleRate: microphoneCapabilities.supportedSampleRates[0],
|
|
87
|
+
bitRate: 32_000,
|
|
88
|
+
bitDepth: microphoneCapabilities.supportedBitDepths[0],
|
|
89
|
+
})));
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
videoStreams: videoStreamId === undefined ? undefined : [videoStreamId],
|
|
93
|
+
audioStreams: audioStreamId === undefined ? undefined : [audioStreamId],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
#echoDeprecatedStreamIds(request, videoStreams, audioStreams) {
|
|
97
|
+
return {
|
|
98
|
+
videoStreamId: request.videoStreamId === undefined ? undefined : (videoStreams?.[0] ?? null),
|
|
99
|
+
audioStreamId: request.audioStreamId === undefined ? undefined : (audioStreams?.[0] ?? null),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
20
102
|
async solicitOffer(request) {
|
|
21
|
-
|
|
103
|
+
let { videoStreams, audioStreams } = this.#resolveStreamLists(request);
|
|
104
|
+
if (!videoStreams?.length && !audioStreams?.length) {
|
|
105
|
+
({ videoStreams, audioStreams } = await this.#autoAssignStreams(request.streamUsage));
|
|
106
|
+
}
|
|
22
107
|
if (!videoStreams?.length && !audioStreams?.length) {
|
|
23
|
-
throw new StatusResponseError('solicitOffer requires at least one of videoStreams or audioStreams;
|
|
108
|
+
throw new StatusResponseError('solicitOffer requires at least one of videoStreams or audioStreams; the camera has no video or audio stream to assign automatically', Status.ConstraintError);
|
|
24
109
|
}
|
|
25
110
|
const device = this.endpoint.stateOf(MatterbridgeServer);
|
|
26
111
|
let webRtcSessionId = 0;
|
|
@@ -42,25 +127,29 @@ export class MatterbridgeWebRtcTransportProviderServer extends WebRtcTransportPr
|
|
|
42
127
|
},
|
|
43
128
|
];
|
|
44
129
|
device.log.info(`Solicited a WebRTC offer for session ${webRtcSessionId} (stream usage ${request.streamUsage}) (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
130
|
+
const webRtcPeer = new WeriftWebRtcSession((level, message) => device.log[level](message), `WebRTC session ${webRtcSessionId}`);
|
|
131
|
+
this.internal.sessions.set(webRtcSessionId, webRtcPeer);
|
|
132
|
+
const sdp = await webRtcPeer.createOffer({ video: !!videoStreams?.length, audio: !!audioStreams?.length, videoResolution: this.#resolveVideoResolution(videoStreams) });
|
|
133
|
+
const requestorEndpoint = await this.#resolvePeerRequestorEndpoint(peerNodeId, fabricIndex, request.originatingEndpointId);
|
|
48
134
|
if (requestorEndpoint) {
|
|
49
|
-
|
|
50
|
-
device.log.info(`
|
|
135
|
+
this.#invokeDeferred(async () => requestorEndpoint.commandsOf(WebRtcTransportRequestorClient).offer({ webRtcSessionId, sdp }), `Offer for session ${webRtcSessionId}`);
|
|
136
|
+
device.log.info(`Invoking Offer on the peer's WebRtcTransportRequestor for session ${webRtcSessionId} (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
51
137
|
}
|
|
52
138
|
else {
|
|
53
|
-
device.log.info(`
|
|
139
|
+
device.log.info(`Could not reach the peer's WebRtcTransportRequestor; the Offer for session ${webRtcSessionId} was not sent (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
54
140
|
}
|
|
55
|
-
return { webRtcSessionId, deferredOffer: true };
|
|
141
|
+
return { webRtcSessionId, deferredOffer: true, ...this.#echoDeprecatedStreamIds(request, videoStreams, audioStreams) };
|
|
56
142
|
}
|
|
57
|
-
provideOffer(request) {
|
|
143
|
+
async provideOffer(request) {
|
|
58
144
|
const device = this.endpoint.stateOf(MatterbridgeServer);
|
|
59
145
|
let webRtcSessionId = request.webRtcSessionId;
|
|
60
146
|
if (webRtcSessionId === null) {
|
|
61
|
-
|
|
147
|
+
let { videoStreams, audioStreams } = this.#resolveStreamLists(request);
|
|
62
148
|
if (!videoStreams?.length && !audioStreams?.length) {
|
|
63
|
-
|
|
149
|
+
({ videoStreams, audioStreams } = await this.#autoAssignStreams(request.streamUsage ?? StreamUsage.LiveView));
|
|
150
|
+
}
|
|
151
|
+
if (!videoStreams?.length && !audioStreams?.length) {
|
|
152
|
+
throw new StatusResponseError('provideOffer requires at least one of videoStreams or audioStreams; the camera has no video or audio stream to assign automatically', Status.ConstraintError);
|
|
64
153
|
}
|
|
65
154
|
webRtcSessionId = 0;
|
|
66
155
|
for (const session of this.state.currentSessions) {
|
|
@@ -86,29 +175,102 @@ export class MatterbridgeWebRtcTransportProviderServer extends WebRtcTransportPr
|
|
|
86
175
|
}
|
|
87
176
|
device.log.info(`Received an SDP offer for session ${webRtcSessionId} (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
88
177
|
device.log.debug(`MatterbridgeWebRtcTransportProviderServer: received SDP offer for session ${webRtcSessionId}: ${request.sdp}`);
|
|
89
|
-
|
|
178
|
+
const session = this.state.currentSessions.find((s) => s.id === webRtcSessionId);
|
|
179
|
+
const webRtcPeer = this.internal.sessions.get(webRtcSessionId) ?? new WeriftWebRtcSession((level, message) => device.log[level](message), `WebRTC session ${webRtcSessionId}`);
|
|
180
|
+
this.internal.sessions.set(webRtcSessionId, webRtcPeer);
|
|
181
|
+
const sdp = await webRtcPeer.createAnswer(request.sdp, this.#resolveVideoResolution(session.videoStreams));
|
|
182
|
+
const requestorEndpoint = await this.#resolvePeerRequestorEndpoint(session.peerNodeId, session.fabricIndex, session.peerEndpointId);
|
|
183
|
+
if (requestorEndpoint) {
|
|
184
|
+
this.#invokeDeferred(async () => requestorEndpoint.commandsOf(WebRtcTransportRequestorClient).answer({ webRtcSessionId, sdp }), `Answer for session ${webRtcSessionId}`);
|
|
185
|
+
device.log.info(`Invoking Answer on the peer's WebRtcTransportRequestor for session ${webRtcSessionId} (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
device.log.info(`Could not reach the peer's WebRtcTransportRequestor; the Answer for session ${webRtcSessionId} was not sent (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
189
|
+
}
|
|
190
|
+
return { webRtcSessionId, ...this.#echoDeprecatedStreamIds(request, session.videoStreams && [...session.videoStreams], session.audioStreams && [...session.audioStreams]) };
|
|
90
191
|
}
|
|
91
|
-
provideAnswer(request) {
|
|
192
|
+
async provideAnswer(request) {
|
|
92
193
|
const device = this.endpoint.stateOf(MatterbridgeServer);
|
|
93
194
|
if (!this.state.currentSessions.some((session) => session.id === request.webRtcSessionId)) {
|
|
94
195
|
throw new StatusResponseError(`WebRTC session ${request.webRtcSessionId} is not present in currentSessions`, Status.NotFound);
|
|
95
196
|
}
|
|
96
197
|
device.log.info(`Received an SDP answer for session ${request.webRtcSessionId} (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
97
198
|
device.log.debug(`MatterbridgeWebRtcTransportProviderServer: received SDP answer for session ${request.webRtcSessionId}: ${request.sdp}`);
|
|
199
|
+
const webRtcPeer = this.internal.sessions.get(request.webRtcSessionId);
|
|
200
|
+
if (webRtcPeer) {
|
|
201
|
+
await webRtcPeer.applyAnswer(request.sdp);
|
|
202
|
+
}
|
|
98
203
|
}
|
|
99
|
-
provideIceCandidates(request) {
|
|
204
|
+
async provideIceCandidates(request) {
|
|
100
205
|
const device = this.endpoint.stateOf(MatterbridgeServer);
|
|
101
206
|
if (!this.state.currentSessions.some((session) => session.id === request.webRtcSessionId)) {
|
|
102
207
|
throw new StatusResponseError(`WebRTC session ${request.webRtcSessionId} is not present in currentSessions`, Status.NotFound);
|
|
103
208
|
}
|
|
104
209
|
device.log.info(`Received ${request.iceCandidates.length} ICE candidate(s) for session ${request.webRtcSessionId} (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
210
|
+
const webRtcPeer = this.internal.sessions.get(request.webRtcSessionId);
|
|
211
|
+
if (webRtcPeer) {
|
|
212
|
+
for (const [index, candidate] of request.iceCandidates.entries()) {
|
|
213
|
+
const startedAt = Date.now();
|
|
214
|
+
const isMdnsHostCandidate = /\.local\s+\d+\s+typ\s+host\b/i.test(candidate.candidate);
|
|
215
|
+
if (isMdnsHostCandidate) {
|
|
216
|
+
device.log.debug(`Skipping mDNS host ICE candidate ${index + 1}/${request.iceCandidates.length} for session ${request.webRtcSessionId} ` +
|
|
217
|
+
`(mid=${candidate.sdpMid ?? 'null'}, mLine=${candidate.sdpmLineIndex ?? 'null'}) ` +
|
|
218
|
+
`(endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
device.log.debug(`Applying ICE candidate ${index + 1}/${request.iceCandidates.length} for session ${request.webRtcSessionId} ` +
|
|
222
|
+
`(mid=${candidate.sdpMid ?? 'null'}, mLine=${candidate.sdpmLineIndex ?? 'null'}, endOfCandidates=${candidate.candidate.trim() === ''}) ` +
|
|
223
|
+
`(endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
224
|
+
try {
|
|
225
|
+
await Promise.race([
|
|
226
|
+
webRtcPeer.addIceCandidate(candidate.candidate, candidate.sdpMid, candidate.sdpmLineIndex),
|
|
227
|
+
new Promise((_resolve, reject) => setTimeout(() => reject(new Error('ICE candidate apply timeout after 2000ms')), 2000)),
|
|
228
|
+
]);
|
|
229
|
+
device.log.debug(`Applied ICE candidate ${index + 1}/${request.iceCandidates.length} for session ${request.webRtcSessionId} ` +
|
|
230
|
+
`in ${Date.now() - startedAt}ms (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
device.log.warn(`Failed ICE candidate ${index + 1}/${request.iceCandidates.length} for session ${request.webRtcSessionId} after ${Date.now() - startedAt}ms ` +
|
|
234
|
+
`(endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber}): ${String(error)}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
105
238
|
}
|
|
106
|
-
endSession(request) {
|
|
239
|
+
async endSession(request) {
|
|
107
240
|
const device = this.endpoint.stateOf(MatterbridgeServer);
|
|
108
241
|
if (!this.state.currentSessions.some((session) => session.id === request.webRtcSessionId)) {
|
|
109
242
|
throw new StatusResponseError(`WebRTC session ${request.webRtcSessionId} is not present in currentSessions`, Status.NotFound);
|
|
110
243
|
}
|
|
111
244
|
this.state.currentSessions = this.state.currentSessions.filter((session) => session.id !== request.webRtcSessionId);
|
|
245
|
+
const webRtcPeer = this.internal.sessions.get(request.webRtcSessionId);
|
|
246
|
+
if (webRtcPeer) {
|
|
247
|
+
this.internal.sessions.delete(request.webRtcSessionId);
|
|
248
|
+
await webRtcPeer.close();
|
|
249
|
+
}
|
|
112
250
|
device.log.info(`Ended WebRTC session ${request.webRtcSessionId} (reason ${request.reason}) (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber})`);
|
|
113
251
|
}
|
|
114
252
|
}
|
|
253
|
+
(function (MatterbridgeWebRtcTransportProviderServer) {
|
|
254
|
+
class Internal {
|
|
255
|
+
sessions = new Map();
|
|
256
|
+
}
|
|
257
|
+
MatterbridgeWebRtcTransportProviderServer.Internal = Internal;
|
|
258
|
+
})(MatterbridgeWebRtcTransportProviderServer || (MatterbridgeWebRtcTransportProviderServer = {}));
|
|
259
|
+
export function createDefaultWebRtcTransportProviderClusterServer(endpoint) {
|
|
260
|
+
endpoint.behaviors.require(MatterbridgeWebRtcTransportProviderServer, { currentSessions: [] });
|
|
261
|
+
return endpoint;
|
|
262
|
+
}
|
|
263
|
+
export function addWebRtcTransportRequestorClient(endpoint) {
|
|
264
|
+
if (endpoint.behaviors.has(MatterbridgeBindingServer)) {
|
|
265
|
+
const existing = endpoint.behaviors.optionsFor(MatterbridgeBindingServer)?.clientList ?? [];
|
|
266
|
+
if (!existing.includes(WebRtcTransportRequestor.id)) {
|
|
267
|
+
endpoint.behaviors.inject(MatterbridgeBindingServer, { clientList: [...existing, WebRtcTransportRequestor.id] });
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
endpoint.behaviors.require(MatterbridgeBindingServer, { clientList: [WebRtcTransportRequestor.id] });
|
|
272
|
+
}
|
|
273
|
+
const clientClusters = endpoint.type.clientClusters;
|
|
274
|
+
clientClusters.webRtcTransportRequestor ??= WebRtcTransportRequestorClient;
|
|
275
|
+
return endpoint;
|
|
276
|
+
}
|
package/dist/devices/camera.js
CHANGED
|
@@ -7,7 +7,11 @@ import { MatterbridgeCameraAvStreamManagementServer } from '../behaviors/cameraA
|
|
|
7
7
|
import { MatterbridgeWebRtcTransportProviderServer } from '../behaviors/webRtcTransportProviderServer.js';
|
|
8
8
|
export class Camera extends MatterbridgeEndpoint {
|
|
9
9
|
constructor(name, serial, options = {}) {
|
|
10
|
-
const { identifyTime = 0, identifyType = Identify.IdentifyType.None, powerSourceType = 'Wired', maxContentBufferSize = 4_194_304, maxNetworkBandwidth = 10_000_000, supportedStreamUsages = [StreamUsage.LiveView, StreamUsage.Recording], streamUsagePriorities = supportedStreamUsages, maxConcurrentEncoders = 1, maxEncodedPixelRate = 1920 * 1080 * 30, videoSensorParams = { sensorWidth: 1920, sensorHeight: 1080, maxFps: 30 }, minViewportResolution = { width: 640, height: 360 }, rateDistortionTradeOffPoints = [{ codec: CameraAvStreamManagement.VideoCodec.H264, resolution: { width: 1920, height: 1080 }, minBitRate: 1_000_000 }], currentFrameRate = 30, viewport = { x1: 0, y1: 0, x2: videoSensorParams.sensorWidth, y2: videoSensorParams.sensorHeight }, microphoneCapabilities = { maxNumberOfChannels: 1, supportedCodecs: [CameraAvStreamManagement.AudioCodec.Opus], supportedSampleRates: [48000], supportedBitDepths: [16] },
|
|
10
|
+
const { identifyTime = 0, identifyType = Identify.IdentifyType.None, powerSourceType = 'Wired', maxContentBufferSize = 4_194_304, maxNetworkBandwidth = 10_000_000, supportedStreamUsages = [StreamUsage.LiveView, StreamUsage.Recording], streamUsagePriorities = supportedStreamUsages, maxConcurrentEncoders = 1, maxEncodedPixelRate = 1920 * 1080 * 30, videoSensorParams = { sensorWidth: 1920, sensorHeight: 1080, maxFps: 30 }, minViewportResolution = { width: 640, height: 360 }, rateDistortionTradeOffPoints = [{ codec: CameraAvStreamManagement.VideoCodec.H264, resolution: { width: 1920, height: 1080 }, minBitRate: 1_000_000 }], currentFrameRate = 30, viewport = { x1: 0, y1: 0, x2: videoSensorParams.sensorWidth, y2: videoSensorParams.sensorHeight }, microphoneCapabilities = { maxNumberOfChannels: 1, supportedCodecs: [CameraAvStreamManagement.AudioCodec.Opus], supportedSampleRates: [48000], supportedBitDepths: [16] }, snapshotCapabilities = [
|
|
11
|
+
{ resolution: { width: 640, height: 480 }, maxFrameRate: 10, imageCodec: CameraAvStreamManagement.ImageCodec.Jpeg, requiresEncodedPixels: false },
|
|
12
|
+
{ resolution: { width: 1280, height: 720 }, maxFrameRate: 10, imageCodec: CameraAvStreamManagement.ImageCodec.Jpeg, requiresEncodedPixels: false },
|
|
13
|
+
{ resolution: { width: 1920, height: 1080 }, maxFrameRate: 10, imageCodec: CameraAvStreamManagement.ImageCodec.Jpeg, requiresEncodedPixels: false },
|
|
14
|
+
], allocatedSnapshotStreams = [], } = options;
|
|
11
15
|
super(powerSourceType === 'None' ? [camera] : [camera, powerSource], { id: `${name.replaceAll(' ', '')}-${serial.replaceAll(' ', '')}` });
|
|
12
16
|
if (identifyType !== Identify.IdentifyType.None) {
|
|
13
17
|
this.createDefaultIdentifyClusterServer(identifyTime, identifyType);
|
|
@@ -43,6 +47,8 @@ export class Camera extends MatterbridgeEndpoint {
|
|
|
43
47
|
currentFrameRate,
|
|
44
48
|
viewport,
|
|
45
49
|
microphoneCapabilities,
|
|
50
|
+
snapshotCapabilities,
|
|
51
|
+
allocatedSnapshotStreams,
|
|
46
52
|
});
|
|
47
53
|
createDefaultWebRtcTransportProviderClusterServer(this);
|
|
48
54
|
addWebRtcTransportRequestorClient(this);
|
|
@@ -5,12 +5,9 @@ import { MatterbridgeCameraAvStreamManagementServer } from '../behaviors/cameraA
|
|
|
5
5
|
export class SnapshotCamera extends MatterbridgeEndpoint {
|
|
6
6
|
constructor(name, serial, options = {}) {
|
|
7
7
|
const { identifyTime = 0, identifyType = Identify.IdentifyType.None, powerSourceType = 'Wired', maxConcurrentEncoders = 1, maxEncodedPixelRate = 10000000, maxContentBufferSize = 1024, snapshotCapabilities = [
|
|
8
|
-
{
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
imageCodec: CameraAvStreamManagement.ImageCodec.Jpeg,
|
|
12
|
-
requiresEncodedPixels: false,
|
|
13
|
-
},
|
|
8
|
+
{ resolution: { width: 640, height: 480 }, maxFrameRate: 10, imageCodec: CameraAvStreamManagement.ImageCodec.Jpeg, requiresEncodedPixels: false },
|
|
9
|
+
{ resolution: { width: 1280, height: 720 }, maxFrameRate: 10, imageCodec: CameraAvStreamManagement.ImageCodec.Jpeg, requiresEncodedPixels: false },
|
|
10
|
+
{ resolution: { width: 1920, height: 1080 }, maxFrameRate: 10, imageCodec: CameraAvStreamManagement.ImageCodec.Jpeg, requiresEncodedPixels: false },
|
|
14
11
|
], maxNetworkBandwidth = 10000, supportedStreamUsages = [StreamUsage.Recording], allocatedSnapshotStreams = [], streamUsagePriorities = [StreamUsage.Recording], } = options;
|
|
15
12
|
super(powerSourceType === 'None' ? [snapshotCamera] : [snapshotCamera, powerSource], { id: `${name.replaceAll(' ', '')}-${serial.replaceAll(' ', '')}` });
|
|
16
13
|
if (identifyType !== Identify.IdentifyType.None) {
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createSocket } from 'node:dgram';
|
|
3
|
+
import { constants } from 'node:fs';
|
|
4
|
+
import { access, readdir } from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { RTCPeerConnection, RTCRtpCodecParameters, useH264, useOPUS, usePCMU, useVP8 } from 'werift';
|
|
7
|
+
import { navigator } from 'werift/nonstandard';
|
|
8
|
+
export class WeriftWebRtcSession {
|
|
9
|
+
peerConnection;
|
|
10
|
+
logger;
|
|
11
|
+
label;
|
|
12
|
+
testVideoGenerator;
|
|
13
|
+
testVideoUdpDisposer;
|
|
14
|
+
testVideoAttached = false;
|
|
15
|
+
constructor(logger, label = 'WebRTC session') {
|
|
16
|
+
this.peerConnection = new RTCPeerConnection({ codecs: { audio: [useOPUS(), usePCMU()], video: [useVP8(), useH264()] } });
|
|
17
|
+
this.logger = logger;
|
|
18
|
+
this.label = label;
|
|
19
|
+
this.log('debug', 'Created RTCPeerConnection');
|
|
20
|
+
}
|
|
21
|
+
log(level, message) {
|
|
22
|
+
this.logger?.(level, `${this.label}: ${message}`);
|
|
23
|
+
}
|
|
24
|
+
summarizeSdp(sdp) {
|
|
25
|
+
const mediaKinds = sdp
|
|
26
|
+
.split('\n')
|
|
27
|
+
.map((line) => line.trim())
|
|
28
|
+
.filter((line) => line.startsWith('m='))
|
|
29
|
+
.map((line) => line.slice(2).split(' ')[0]);
|
|
30
|
+
return `length=${sdp.length} media=[${mediaKinds.join(',')}]`;
|
|
31
|
+
}
|
|
32
|
+
async runProcess(command, args) {
|
|
33
|
+
await new Promise((resolve, reject) => {
|
|
34
|
+
const child = spawn(command, args, { stdio: 'ignore' });
|
|
35
|
+
child.once('error', reject);
|
|
36
|
+
child.once('exit', (code) => {
|
|
37
|
+
if (code === 0) {
|
|
38
|
+
resolve();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
reject(new Error(`${command} exited with code ${code ?? -1}`));
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async hasCommand(command) {
|
|
46
|
+
for (const versionArg of ['--version', '-version']) {
|
|
47
|
+
try {
|
|
48
|
+
await this.runProcess(command, [versionArg]);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
async getWindowsCommandCandidates(command) {
|
|
57
|
+
if (process.platform !== 'win32')
|
|
58
|
+
return [];
|
|
59
|
+
const commandName = command.toLowerCase().replace(/\.exe$/, '');
|
|
60
|
+
const executable = command.toLowerCase().endsWith('.exe') ? command : `${command}.exe`;
|
|
61
|
+
const candidates = [];
|
|
62
|
+
if (commandName === 'ffmpeg' && process.env.LOCALAPPDATA) {
|
|
63
|
+
const wingetPackages = path.join(process.env.LOCALAPPDATA, 'Microsoft', 'WinGet', 'Packages');
|
|
64
|
+
try {
|
|
65
|
+
const packageDirs = await readdir(wingetPackages, { withFileTypes: true });
|
|
66
|
+
for (const packageDir of packageDirs) {
|
|
67
|
+
if (!packageDir.isDirectory() || !packageDir.name.startsWith('Gyan.FFmpeg_'))
|
|
68
|
+
continue;
|
|
69
|
+
const packagePath = path.join(wingetPackages, packageDir.name);
|
|
70
|
+
try {
|
|
71
|
+
const versionDirs = await readdir(packagePath, { withFileTypes: true });
|
|
72
|
+
for (const versionDir of versionDirs) {
|
|
73
|
+
if (versionDir.isDirectory() && versionDir.name.startsWith('ffmpeg-'))
|
|
74
|
+
candidates.push(path.join(packagePath, versionDir.name, 'bin', executable));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (const programFiles of [process.env.ProgramFiles, process.env['ProgramFiles(x86)']]) {
|
|
85
|
+
if (!programFiles)
|
|
86
|
+
continue;
|
|
87
|
+
candidates.push(path.join(programFiles, 'ffmpeg', 'bin', executable), path.join(programFiles, 'Gyan', 'FFmpeg', 'bin', executable));
|
|
88
|
+
}
|
|
89
|
+
return candidates;
|
|
90
|
+
}
|
|
91
|
+
async resolveCommand(command) {
|
|
92
|
+
const candidates = [command, `/usr/bin/${command}`, `/bin/${command}`, `/usr/local/bin/${command}`, ...(await this.getWindowsCommandCandidates(command))];
|
|
93
|
+
for (const candidate of candidates) {
|
|
94
|
+
if (candidate.includes('/')) {
|
|
95
|
+
try {
|
|
96
|
+
await access(candidate, constants.X_OK);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (await this.hasCommand(candidate))
|
|
103
|
+
return candidate;
|
|
104
|
+
}
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
async getFreeUdpPort() {
|
|
108
|
+
return await new Promise((resolve, reject) => {
|
|
109
|
+
const socket = createSocket('udp4');
|
|
110
|
+
socket.once('error', reject);
|
|
111
|
+
socket.bind(0, '127.0.0.1', () => {
|
|
112
|
+
const address = socket.address();
|
|
113
|
+
if (typeof address === 'string') {
|
|
114
|
+
socket.close();
|
|
115
|
+
reject(new Error('Failed to allocate UDP port'));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const port = address.port;
|
|
119
|
+
socket.close(() => resolve(port));
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
getPreferredInjectableVideoCodec() {
|
|
124
|
+
for (const transceiver of this.peerConnection.getTransceivers()) {
|
|
125
|
+
if (transceiver.kind !== 'video')
|
|
126
|
+
continue;
|
|
127
|
+
const preferredCodec = transceiver.codecs.find((codec) => {
|
|
128
|
+
const mimeType = codec.mimeType.toLowerCase();
|
|
129
|
+
return mimeType === 'video/vp8' || mimeType === 'video/h264';
|
|
130
|
+
});
|
|
131
|
+
if (preferredCodec)
|
|
132
|
+
return preferredCodec;
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
static SUPPORTED_WEBCAM_RESOLUTIONS = ['640x480', '1280x720', '1920x1080'];
|
|
137
|
+
static BITRATE_KBPS_BY_RESOLUTION = {
|
|
138
|
+
'640x480': 1000,
|
|
139
|
+
'1280x720': 2500,
|
|
140
|
+
'1920x1080': 2000,
|
|
141
|
+
};
|
|
142
|
+
getConfiguredWebcamResolution(requestedResolution) {
|
|
143
|
+
const [defaultResolution] = WeriftWebRtcSession.SUPPORTED_WEBCAM_RESOLUTIONS;
|
|
144
|
+
if (requestedResolution) {
|
|
145
|
+
if (WeriftWebRtcSession.SUPPORTED_WEBCAM_RESOLUTIONS.includes(requestedResolution))
|
|
146
|
+
return requestedResolution;
|
|
147
|
+
this.log('warn', `Requested video stream resolution "${requestedResolution}" is not supported for webcam capture (supported: ${WeriftWebRtcSession.SUPPORTED_WEBCAM_RESOLUTIONS.join(', ')}); falling back to MATTERBRIDGE_CAMERA_WEBCAM_RESOLUTION`);
|
|
148
|
+
}
|
|
149
|
+
const requested = process.env.MATTERBRIDGE_CAMERA_WEBCAM_RESOLUTION;
|
|
150
|
+
if (!requested)
|
|
151
|
+
return defaultResolution;
|
|
152
|
+
if (WeriftWebRtcSession.SUPPORTED_WEBCAM_RESOLUTIONS.includes(requested))
|
|
153
|
+
return requested;
|
|
154
|
+
this.log('warn', `Unsupported MATTERBRIDGE_CAMERA_WEBCAM_RESOLUTION "${requested}" (supported: ${WeriftWebRtcSession.SUPPORTED_WEBCAM_RESOLUTIONS.join(', ')}); falling back to ${defaultResolution}`);
|
|
155
|
+
return defaultResolution;
|
|
156
|
+
}
|
|
157
|
+
buildFfmpegVideoInputArgs(requestedResolution) {
|
|
158
|
+
const testPatternInput = {
|
|
159
|
+
args: ['-re', '-stream_loop', '-1', '-f', 'lavfi', '-i', 'smptebars=size=640x480:rate=10'],
|
|
160
|
+
description: 'synthetic SMPTE bars test',
|
|
161
|
+
bitrateKbps: WeriftWebRtcSession.BITRATE_KBPS_BY_RESOLUTION['640x480'],
|
|
162
|
+
};
|
|
163
|
+
if (process.env.MATTERBRIDGE_CAMERA_VIDEO_SOURCE?.toLowerCase() !== 'webcam')
|
|
164
|
+
return testPatternInput;
|
|
165
|
+
const device = process.env.MATTERBRIDGE_CAMERA_WEBCAM_DEVICE;
|
|
166
|
+
if (!device) {
|
|
167
|
+
this.log('warn', 'MATTERBRIDGE_CAMERA_VIDEO_SOURCE=webcam requires MATTERBRIDGE_CAMERA_WEBCAM_DEVICE to be set; falling back to the synthetic test video');
|
|
168
|
+
return testPatternInput;
|
|
169
|
+
}
|
|
170
|
+
const resolution = this.getConfiguredWebcamResolution(requestedResolution);
|
|
171
|
+
const bitrateKbps = WeriftWebRtcSession.BITRATE_KBPS_BY_RESOLUTION[resolution];
|
|
172
|
+
const description = `local webcam (${device}, ${resolution})`;
|
|
173
|
+
switch (process.platform) {
|
|
174
|
+
case 'linux':
|
|
175
|
+
return { args: ['-f', 'v4l2', '-input_format', 'yuyv422', '-video_size', resolution, '-framerate', '30', '-i', device], description, bitrateKbps };
|
|
176
|
+
case 'darwin':
|
|
177
|
+
return { args: ['-f', 'avfoundation', '-video_size', resolution, '-framerate', '30', '-i', device], description, bitrateKbps };
|
|
178
|
+
case 'win32':
|
|
179
|
+
return { args: ['-f', 'dshow', '-video_size', resolution, '-framerate', '30', '-i', `video=${device}`], description, bitrateKbps };
|
|
180
|
+
default:
|
|
181
|
+
this.log('warn', `Webcam capture via ffmpeg is not supported on platform "${process.platform}"; falling back to the synthetic test video`);
|
|
182
|
+
return testPatternInput;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async ensureTestVideoTrack(codec, videoResolution) {
|
|
186
|
+
if (this.testVideoAttached)
|
|
187
|
+
return;
|
|
188
|
+
if (process.env.MATTERBRIDGE_CAMERA_DISABLE_TEST_VIDEO === '1') {
|
|
189
|
+
this.log('info', 'Test video injection disabled by MATTERBRIDGE_CAMERA_DISABLE_TEST_VIDEO=1');
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const videoInput = this.buildFfmpegVideoInputArgs(videoResolution);
|
|
193
|
+
this.log('debug', `Attempting to attach ${videoInput.description} video track`);
|
|
194
|
+
const ffmpegCommand = await this.resolveCommand('ffmpeg');
|
|
195
|
+
if (!ffmpegCommand) {
|
|
196
|
+
this.log('warn', 'Cannot inject video stream: missing dependency ffmpeg');
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const selectedMimeType = (codec?.mimeType ?? 'video/vp8').toLowerCase();
|
|
200
|
+
const selectedPayloadType = codec?.payloadType ?? 120;
|
|
201
|
+
try {
|
|
202
|
+
const udpPort = await this.getFreeUdpPort();
|
|
203
|
+
const { track, disposer } = navigator.mediaDevices.getUdpMedia({
|
|
204
|
+
port: udpPort,
|
|
205
|
+
codec: new RTCRtpCodecParameters({ mimeType: selectedMimeType, clockRate: 90000, payloadType: selectedPayloadType }),
|
|
206
|
+
});
|
|
207
|
+
this.peerConnection.addTrack(track);
|
|
208
|
+
const bitrate = `${videoInput.bitrateKbps}k`;
|
|
209
|
+
const encoderArgs = selectedMimeType === 'video/h264'
|
|
210
|
+
? ['-c:v', 'libx264', '-preset', 'ultrafast', '-tune', 'zerolatency', '-pix_fmt', 'yuv420p', '-g', '20', '-b:v', bitrate, '-maxrate', bitrate, '-bufsize', bitrate]
|
|
211
|
+
: ['-c:v', 'libvpx', '-deadline', 'realtime', '-cpu-used', '4', '-pix_fmt', 'yuv420p', '-g', '20', '-b:v', bitrate, '-maxrate', bitrate, '-bufsize', bitrate];
|
|
212
|
+
const generator = spawn(ffmpegCommand, [
|
|
213
|
+
'-hide_banner',
|
|
214
|
+
'-loglevel',
|
|
215
|
+
'error',
|
|
216
|
+
...videoInput.args,
|
|
217
|
+
'-an',
|
|
218
|
+
...encoderArgs,
|
|
219
|
+
'-f',
|
|
220
|
+
'rtp',
|
|
221
|
+
'-payload_type',
|
|
222
|
+
String(selectedPayloadType),
|
|
223
|
+
`rtp://127.0.0.1:${udpPort}`,
|
|
224
|
+
]);
|
|
225
|
+
generator.once('error', (error) => {
|
|
226
|
+
this.log('warn', `Video generator failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
227
|
+
});
|
|
228
|
+
this.testVideoUdpDisposer = disposer;
|
|
229
|
+
this.testVideoGenerator = generator;
|
|
230
|
+
this.testVideoAttached = true;
|
|
231
|
+
this.log('info', `Attached ${videoInput.description} video track (ffmpeg=${ffmpegCommand}, codec=${selectedMimeType}, payloadType=${selectedPayloadType}, sourcePort=${udpPort})`);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
this.log('warn', `Failed to attach ${videoInput.description} video track: ${error instanceof Error ? error.message : String(error)}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
preferVideoCodecOnTransceivers(mimeType) {
|
|
238
|
+
let adjustedTransceivers = 0;
|
|
239
|
+
for (const transceiver of this.peerConnection.getTransceivers()) {
|
|
240
|
+
if (transceiver.kind !== 'video')
|
|
241
|
+
continue;
|
|
242
|
+
const preferredCodecs = transceiver.codecs.filter((codec) => codec.mimeType.toLowerCase() === mimeType);
|
|
243
|
+
if (!preferredCodecs.length)
|
|
244
|
+
continue;
|
|
245
|
+
transceiver.codecs = preferredCodecs;
|
|
246
|
+
adjustedTransceivers += 1;
|
|
247
|
+
}
|
|
248
|
+
if (adjustedTransceivers > 0) {
|
|
249
|
+
this.log('info', `Preferred ${mimeType.toUpperCase()} codecs on ${adjustedTransceivers} video transceiver(s)`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
cleanupTestVideoArtifacts() {
|
|
253
|
+
if (this.testVideoGenerator) {
|
|
254
|
+
this.testVideoGenerator.kill('SIGTERM');
|
|
255
|
+
this.testVideoGenerator = undefined;
|
|
256
|
+
}
|
|
257
|
+
this.testVideoUdpDisposer?.();
|
|
258
|
+
this.testVideoUdpDisposer = undefined;
|
|
259
|
+
this.testVideoAttached = false;
|
|
260
|
+
}
|
|
261
|
+
async createOffer(options) {
|
|
262
|
+
this.log('debug', `createOffer requested (video=${options.video}, audio=${options.audio})`);
|
|
263
|
+
if (options.video) {
|
|
264
|
+
const preferredCodec = this.getPreferredInjectableVideoCodec();
|
|
265
|
+
if (preferredCodec) {
|
|
266
|
+
this.preferVideoCodecOnTransceivers(preferredCodec.mimeType.toLowerCase());
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
this.log('warn', 'No injectable video codec available on negotiated transceivers (supported: VP8, H264)');
|
|
270
|
+
}
|
|
271
|
+
await this.ensureTestVideoTrack(preferredCodec, options.videoResolution);
|
|
272
|
+
if (!this.testVideoAttached)
|
|
273
|
+
this.peerConnection.addTransceiver('video', { direction: 'sendonly' });
|
|
274
|
+
}
|
|
275
|
+
if (options.audio)
|
|
276
|
+
this.peerConnection.addTransceiver('audio', { direction: 'sendonly' });
|
|
277
|
+
const offer = await this.peerConnection.createOffer();
|
|
278
|
+
await this.peerConnection.setLocalDescription(offer);
|
|
279
|
+
const sdp = this.peerConnection.localDescription.sdp;
|
|
280
|
+
this.log('info', `Created local SDP offer (${this.summarizeSdp(sdp)})`);
|
|
281
|
+
return sdp;
|
|
282
|
+
}
|
|
283
|
+
async createAnswer(offerSdp, videoResolution) {
|
|
284
|
+
this.log('debug', `createAnswer requested for remote offer (${this.summarizeSdp(offerSdp)})`);
|
|
285
|
+
await this.peerConnection.setRemoteDescription({ type: 'offer', sdp: offerSdp });
|
|
286
|
+
const hasVideoTransceiver = this.peerConnection.getTransceivers().some((transceiver) => transceiver.kind === 'video');
|
|
287
|
+
this.log('debug', `Remote offer created video transceiver: ${hasVideoTransceiver}`);
|
|
288
|
+
if (hasVideoTransceiver) {
|
|
289
|
+
const preferredCodec = this.getPreferredInjectableVideoCodec();
|
|
290
|
+
if (preferredCodec) {
|
|
291
|
+
this.preferVideoCodecOnTransceivers(preferredCodec.mimeType.toLowerCase());
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
this.log('warn', 'No injectable video codec available on negotiated transceivers (supported: VP8, H264)');
|
|
295
|
+
}
|
|
296
|
+
await this.ensureTestVideoTrack(preferredCodec, videoResolution);
|
|
297
|
+
}
|
|
298
|
+
for (const transceiver of this.peerConnection.getTransceivers()) {
|
|
299
|
+
transceiver.setDirection('sendonly');
|
|
300
|
+
}
|
|
301
|
+
const answer = await this.peerConnection.createAnswer();
|
|
302
|
+
await this.peerConnection.setLocalDescription(answer);
|
|
303
|
+
const sdp = this.peerConnection.localDescription.sdp;
|
|
304
|
+
this.log('info', `Created local SDP answer (${this.summarizeSdp(sdp)})`);
|
|
305
|
+
return sdp;
|
|
306
|
+
}
|
|
307
|
+
async applyAnswer(answerSdp) {
|
|
308
|
+
this.log('debug', `applyAnswer requested (${this.summarizeSdp(answerSdp)})`);
|
|
309
|
+
await this.peerConnection.setRemoteDescription({ type: 'answer', sdp: answerSdp });
|
|
310
|
+
this.log('info', `Remote SDP answer applied (signalingState=${this.peerConnection.signalingState})`);
|
|
311
|
+
}
|
|
312
|
+
async addIceCandidate(candidate, sdpMid, sdpMLineIndex) {
|
|
313
|
+
this.log('debug', `Applying ICE candidate (mid=${sdpMid ?? 'null'}, mLine=${sdpMLineIndex ?? 'null'}, endOfCandidates=${candidate.trim() === ''})`);
|
|
314
|
+
await this.peerConnection.addIceCandidate({ candidate, sdpMid: sdpMid ?? undefined, sdpMLineIndex: sdpMLineIndex ?? undefined });
|
|
315
|
+
}
|
|
316
|
+
async close() {
|
|
317
|
+
this.log('debug', 'Closing RTCPeerConnection');
|
|
318
|
+
await this.peerConnection.close();
|
|
319
|
+
this.cleanupTestVideoArtifacts();
|
|
320
|
+
this.log('info', `RTCPeerConnection closed (connectionState=${this.peerConnection.connectionState})`);
|
|
321
|
+
}
|
|
322
|
+
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "matterbridge-example-camera",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4-dev-20260719-49470d3",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "matterbridge-example-camera",
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.4-dev-20260719-49470d3",
|
|
10
10
|
"license": "Apache-2.0",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"node-ansi-logger": "3.3.0",
|