@peterchijioke/callkit-core 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +196 -2
- package/dist/index.d.mts +460 -0
- package/dist/index.d.ts +460 -0
- package/dist/index.js +1226 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1193 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +53 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Peter Chukwu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,197 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@peterchijioke/callkit-core`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Framework-agnostic, SFU-ready WebRTC audio and video call engine with transport-agnostic signaling, pluggable media transports, and full audio/video device controls.
|
|
4
|
+
|
|
5
|
+
## Companion Packages
|
|
6
|
+
|
|
7
|
+
| Package | Purpose |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| `@peterchijioke/callkit-core` | Shared WebRTC call engine (this package) |
|
|
10
|
+
| `@peterchijioke/callkit-server` | Standalone Node.js signaling & room management server |
|
|
11
|
+
| `@peterchijioke/callkit-react` | SSR-safe React & Next.js App Router hooks + `<VideoView>` / `<AudioView>` |
|
|
12
|
+
| `@peterchijioke/callkit-react-native` | React Native adapter, camera flip, speakerphone routing & `<NativeVideoView>` |
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @peterchijioke/callkit-core
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { CallClient, WebSocketSignalingAdapter } from '@peterchijioke/callkit-core';
|
|
24
|
+
|
|
25
|
+
const client = new CallClient({
|
|
26
|
+
signaling: new WebSocketSignalingAdapter({
|
|
27
|
+
url: 'wss://signal.example.com/ws',
|
|
28
|
+
}),
|
|
29
|
+
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Listen to room & media events
|
|
33
|
+
client.on('stateChange', (state) => console.log('Call state:', state));
|
|
34
|
+
client.on('participantJoined', (p) => console.log('Joined:', p.id, p.displayName));
|
|
35
|
+
client.on('participantLeft', (peerId) => console.log('Left:', peerId));
|
|
36
|
+
client.on('trackSubscribed', ({ peerId, track, kind }) => {
|
|
37
|
+
console.log(`Subscribed to ${kind} track from ${peerId}`, track);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Join a room (captures mic + camera by default)
|
|
41
|
+
await client.join({
|
|
42
|
+
roomId: 'standup-123',
|
|
43
|
+
peerId: 'user-1',
|
|
44
|
+
displayName: 'Alice',
|
|
45
|
+
token: 'optional-auth-jwt',
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Audio & video controls
|
|
49
|
+
client.toggleAudio(); // mute/unmute microphone
|
|
50
|
+
client.setVideoEnabled(false); // turn camera off
|
|
51
|
+
|
|
52
|
+
// Device enumeration & hot-swapping
|
|
53
|
+
const devices = await client.getDevices();
|
|
54
|
+
const usbMic = devices.find((d) => d.kind === 'audioinput');
|
|
55
|
+
if (usbMic) {
|
|
56
|
+
await client.switchAudioInput(usbMic.deviceId);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Leave cleanly
|
|
60
|
+
await client.leave();
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## API Reference
|
|
66
|
+
|
|
67
|
+
### `CallClient`
|
|
68
|
+
|
|
69
|
+
High-level orchestrator managing room lifecycle, signaling, media capture, device switching, and transport negotiation.
|
|
70
|
+
|
|
71
|
+
#### Constructor: `new CallClient(options)`
|
|
72
|
+
|
|
73
|
+
| Option | Type | Required | Description |
|
|
74
|
+
| --- | --- | --- | --- |
|
|
75
|
+
| `signaling` | `SignalingAdapter` | Yes | Signaling transport (e.g. `WebSocketSignalingAdapter` or custom). |
|
|
76
|
+
| `transport` | `MediaTransport` | No | Media transport. Defaults to `new MeshTransport()`. Pass `new SfuTransport(driver)` for SFU rooms. |
|
|
77
|
+
| `mediaAdapter` | `MediaAdapter` | No | Platform WebRTC/MediaDevices bridge. Defaults to `new BrowserMediaAdapter()`. |
|
|
78
|
+
| `iceServers` | `IceServerConfig[]` | No | Fallback STUN/TURN servers. Server-provided `iceServers` override these when present. |
|
|
79
|
+
|
|
80
|
+
#### Properties
|
|
81
|
+
|
|
82
|
+
- `client.state: CallState` — Current lifecycle state: `'idle' | 'connecting' | 'connected' | 'reconnecting' | 'disconnected' | 'failed'`.
|
|
83
|
+
- `client.roomId: string | null` — Active room ID, or `null` when not in a room.
|
|
84
|
+
- `client.localPeerId: string | null` — Local peer ID.
|
|
85
|
+
- `client.localParticipant: Participant | null` — Local participant snapshot with stream & mute flags.
|
|
86
|
+
- `client.media: MediaController` — Direct access to the underlying `MediaController`.
|
|
87
|
+
|
|
88
|
+
#### Methods
|
|
89
|
+
|
|
90
|
+
| Method | Description |
|
|
91
|
+
| --- | --- |
|
|
92
|
+
| `join(options: JoinOptions): Promise<void>` | Captures local media (unless `media: false`), connects signaling, sends `join`, initializes the media transport, and resolves once the server confirms `joined`. |
|
|
93
|
+
| `leave(): Promise<void>` | Sends `leave`, closes peer connections / SFU session, stops local tracks, and disconnects signaling. |
|
|
94
|
+
| `destroy(): Promise<void>` | Calls `leave()`, unsubscribes all listeners, and tears down the `MediaController`. |
|
|
95
|
+
| `getParticipants(): Participant[]` | Returns all participants (local + remote). |
|
|
96
|
+
| `getRemoteParticipants(): Participant[]` | Returns only remote participants. |
|
|
97
|
+
| `getParticipant(peerId: string): Participant \| undefined` | Looks up a single participant by ID. |
|
|
98
|
+
| `setAudioEnabled(enabled: boolean): boolean` | Enables or disables the local microphone track and broadcasts `media-state`. |
|
|
99
|
+
| `toggleAudio(): boolean` | Toggles microphone mute state and returns the new `audioEnabled` value. |
|
|
100
|
+
| `setVideoEnabled(enabled: boolean): boolean` | Enables or disables the local camera track and broadcasts `media-state`. |
|
|
101
|
+
| `toggleVideo(): boolean` | Toggles camera state and returns the new `videoEnabled` value. |
|
|
102
|
+
| `getDevices(): Promise<CallDeviceInfo[]>` | Refreshes and returns all available `audioinput`, `videoinput`, and `audiooutput` devices. |
|
|
103
|
+
| `getSelectedDevices(): SelectedDevices` | Returns `{ audioInputId, videoInputId, audioOutputId }`. |
|
|
104
|
+
| `switchAudioInput(deviceId: string): Promise<MediaStreamTrack>` | Captures a new mic track and hot-swaps it on all active senders via `replaceTrack()` without renegotiation. |
|
|
105
|
+
| `switchVideoInput(deviceId: string): Promise<MediaStreamTrack>` | Captures a new camera track and hot-swaps it on all active senders via `replaceTrack()`. |
|
|
106
|
+
| `setAudioOutput(deviceId: string): void` | Updates the preferred speaker output device ID and emits `selectedDevicesChanged`. |
|
|
107
|
+
| `on(event, handler): () => void` | Subscribes to a typed event and returns an unsubscribe function. |
|
|
108
|
+
| `once(event, handler): () => void` | Subscribes to the next emission of an event. |
|
|
109
|
+
| `off(event, handler): void` | Removes a previously registered event listener. |
|
|
110
|
+
|
|
111
|
+
#### Events (`CallClientEvents`)
|
|
112
|
+
|
|
113
|
+
| Event | Payload | Fired When |
|
|
114
|
+
| --- | --- | --- |
|
|
115
|
+
| `stateChange` | `CallState` | Connection state changes (`connecting`, `connected`, `reconnecting`, `disconnected`, `failed`). |
|
|
116
|
+
| `participantJoined` | `Participant` | A remote participant joins the room. |
|
|
117
|
+
| `participantUpdated` | `Participant` | A participant's stream, mute state, or connection state updates. |
|
|
118
|
+
| `participantLeft` | `string` (`peerId`) | A remote participant leaves the room. |
|
|
119
|
+
| `localStreamUpdated` | `MediaStream \| null` | Local camera/mic stream starts, stops, or swaps a track. |
|
|
120
|
+
| `trackSubscribed` | `{ peerId, track, kind }` | A remote audio or video track is received. |
|
|
121
|
+
| `trackUnsubscribed` | `{ peerId, kind }` | A remote track ends or is removed. |
|
|
122
|
+
| `devicesChanged` | `CallDeviceInfo[]` | Available media devices change (e.g. headset plugged in). |
|
|
123
|
+
| `selectedDevicesChanged` | `SelectedDevices` | Active mic, camera, or speaker selection changes. |
|
|
124
|
+
| `error` | `Error` | An asynchronous signaling, media, or transport error occurs. |
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
### `WebSocketSignalingAdapter`
|
|
129
|
+
|
|
130
|
+
Built-in WebSocket implementation of `SignalingAdapter` with message queueing, heartbeat ping/pong, and exponential-backoff reconnection.
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
const signaling = new WebSocketSignalingAdapter({
|
|
134
|
+
url: 'wss://signal.example.com/ws',
|
|
135
|
+
reconnect: true, // default: true
|
|
136
|
+
maxReconnectAttempts: 10, // default: 10
|
|
137
|
+
reconnectDelayMs: 1000, // default: 1000
|
|
138
|
+
maxReconnectDelayMs: 15000,// default: 15000
|
|
139
|
+
heartbeatIntervalMs: 25000,// default: 25000 (0 to disable)
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
#### Custom `SignalingAdapter`
|
|
144
|
+
|
|
145
|
+
Implement the 6-member `SignalingAdapter` interface to use Socket.IO, Supabase Realtime, Firebase, or Ably:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import type { SignalingAdapter, SignalMessage, SignalingState } from '@peterchijioke/callkit-core';
|
|
149
|
+
|
|
150
|
+
export class MyCustomSignaling implements SignalingAdapter {
|
|
151
|
+
state: SignalingState = 'disconnected';
|
|
152
|
+
async connect() { /* open transport */ }
|
|
153
|
+
async disconnect() { /* close transport */ }
|
|
154
|
+
send(message: SignalMessage) { /* publish message */ }
|
|
155
|
+
onMessage(handler: (msg: SignalMessage) => void) { /* subscribe & return unsubscribe fn */ return () => {}; }
|
|
156
|
+
onStateChange(handler: (state: SignalingState) => void) { return () => {}; }
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
### Pluggable Transports (`MeshTransport` & `SfuTransport`)
|
|
163
|
+
|
|
164
|
+
- **`MeshTransport`** (default) — Peer-to-peer mesh using the WebRTC Perfect Negotiation pattern (polite/impolite glare resolution + ICE candidate buffering). Ideal for 1-to-1 and small group calls (~2–6 peers) with zero media server.
|
|
165
|
+
- **`SfuTransport`** — Routes `sfu-signal` protocol frames between your `SignalingAdapter` and any `SfuDriver` (mediasoup-client, LiveKit, Pion, Janus) for large rooms:
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import {
|
|
169
|
+
CallClient,
|
|
170
|
+
SfuTransport,
|
|
171
|
+
WebSocketSignalingAdapter,
|
|
172
|
+
type SfuDriver,
|
|
173
|
+
} from '@peterchijioke/callkit-core';
|
|
174
|
+
|
|
175
|
+
const mediasoupDriver: SfuDriver = {
|
|
176
|
+
name: 'mediasoup',
|
|
177
|
+
async connect({ roomId, localPeerId, sfuConfig, sendSfuSignal, onRemoteTrack }) {
|
|
178
|
+
sendSfuSignal('getRouterRtpCapabilities', { roomId });
|
|
179
|
+
},
|
|
180
|
+
async handleSfuSignal(action, payload, from) { /* handle server SFU responses */ },
|
|
181
|
+
async publishTrack(track, kind) { /* sendTransport.produce({ track }) */ },
|
|
182
|
+
async unpublishTrack(kind) { /* close producer */ },
|
|
183
|
+
async replaceTrack(kind, track) { /* producer.replaceTrack({ track }) */ },
|
|
184
|
+
async disconnect() { /* close transports */ },
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const client = new CallClient({
|
|
188
|
+
signaling: new WebSocketSignalingAdapter({ url: 'wss://signal.example.com/ws' }),
|
|
189
|
+
transport: new SfuTransport(mediasoupDriver),
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
MIT — see the [LICENSE](./LICENSE) file for details.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
type EventHandler = (...args: never[]) => void;
|
|
2
|
+
declare class TypedEventEmitter<Events extends {
|
|
3
|
+
[K in keyof Events]: EventHandler;
|
|
4
|
+
}> {
|
|
5
|
+
private listeners;
|
|
6
|
+
on<K extends keyof Events>(event: K, handler: Events[K]): () => void;
|
|
7
|
+
once<K extends keyof Events>(event: K, handler: Events[K]): () => void;
|
|
8
|
+
off<K extends keyof Events>(event: K, handler: Events[K]): void;
|
|
9
|
+
protected emit<K extends keyof Events>(event: K, ...args: Parameters<Events[K]>): void;
|
|
10
|
+
removeAllListeners(): void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type CallState = 'idle' | 'connecting' | 'connected' | 'reconnecting' | 'disconnected' | 'failed';
|
|
14
|
+
type SignalingState = 'disconnected' | 'connecting' | 'connected' | 'reconnecting';
|
|
15
|
+
type TrackKind = 'audio' | 'video';
|
|
16
|
+
type CallMediaDeviceKind = 'audioinput' | 'videoinput' | 'audiooutput';
|
|
17
|
+
interface CallDeviceInfo {
|
|
18
|
+
deviceId: string;
|
|
19
|
+
kind: CallMediaDeviceKind;
|
|
20
|
+
label: string;
|
|
21
|
+
groupId?: string;
|
|
22
|
+
}
|
|
23
|
+
interface MediaConstraintsOptions {
|
|
24
|
+
audio?: boolean | MediaTrackConstraints;
|
|
25
|
+
video?: boolean | MediaTrackConstraints;
|
|
26
|
+
}
|
|
27
|
+
interface RemoteParticipantInfo {
|
|
28
|
+
id: string;
|
|
29
|
+
displayName?: string;
|
|
30
|
+
metadata?: Record<string, unknown>;
|
|
31
|
+
audioEnabled?: boolean;
|
|
32
|
+
videoEnabled?: boolean;
|
|
33
|
+
}
|
|
34
|
+
interface Participant {
|
|
35
|
+
id: string;
|
|
36
|
+
displayName?: string;
|
|
37
|
+
metadata?: Record<string, unknown>;
|
|
38
|
+
isLocal: boolean;
|
|
39
|
+
audioEnabled: boolean;
|
|
40
|
+
videoEnabled: boolean;
|
|
41
|
+
audioTrack: MediaStreamTrack | null;
|
|
42
|
+
videoTrack: MediaStreamTrack | null;
|
|
43
|
+
stream: MediaStream | null;
|
|
44
|
+
connectionState: RTCPeerConnectionState | 'connected';
|
|
45
|
+
}
|
|
46
|
+
interface JoinOptions {
|
|
47
|
+
roomId: string;
|
|
48
|
+
peerId: string;
|
|
49
|
+
displayName?: string;
|
|
50
|
+
metadata?: Record<string, unknown>;
|
|
51
|
+
token?: string;
|
|
52
|
+
/** Initial media capture options. Defaults to `{ audio: true, video: true }`. Pass `false` to join without capturing local media. */
|
|
53
|
+
media?: MediaConstraintsOptions | false;
|
|
54
|
+
/** Maximum time in ms to wait for server `joined` confirmation. Defaults to `15000`. */
|
|
55
|
+
joinTimeoutMs?: number;
|
|
56
|
+
}
|
|
57
|
+
interface SelectedDevices {
|
|
58
|
+
audioInputId: string | null;
|
|
59
|
+
videoInputId: string | null;
|
|
60
|
+
audioOutputId: string | null;
|
|
61
|
+
}
|
|
62
|
+
interface IceServerConfig {
|
|
63
|
+
urls: string | string[];
|
|
64
|
+
username?: string;
|
|
65
|
+
credential?: string;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Signaling protocol messages exchanged between client and server.
|
|
69
|
+
*/
|
|
70
|
+
type SignalMessage = {
|
|
71
|
+
type: 'join';
|
|
72
|
+
roomId: string;
|
|
73
|
+
peerId: string;
|
|
74
|
+
displayName?: string;
|
|
75
|
+
metadata?: Record<string, unknown>;
|
|
76
|
+
token?: string;
|
|
77
|
+
} | {
|
|
78
|
+
type: 'joined';
|
|
79
|
+
roomId: string;
|
|
80
|
+
peerId: string;
|
|
81
|
+
participants: RemoteParticipantInfo[];
|
|
82
|
+
iceServers?: IceServerConfig[];
|
|
83
|
+
sfuConfig?: Record<string, unknown>;
|
|
84
|
+
} | {
|
|
85
|
+
type: 'leave';
|
|
86
|
+
roomId: string;
|
|
87
|
+
peerId: string;
|
|
88
|
+
} | {
|
|
89
|
+
type: 'peer-joined';
|
|
90
|
+
roomId: string;
|
|
91
|
+
participant: RemoteParticipantInfo;
|
|
92
|
+
} | {
|
|
93
|
+
type: 'peer-left';
|
|
94
|
+
roomId: string;
|
|
95
|
+
peerId: string;
|
|
96
|
+
} | {
|
|
97
|
+
type: 'offer';
|
|
98
|
+
roomId: string;
|
|
99
|
+
from: string;
|
|
100
|
+
to: string;
|
|
101
|
+
sdp: RTCSessionDescriptionInit;
|
|
102
|
+
} | {
|
|
103
|
+
type: 'answer';
|
|
104
|
+
roomId: string;
|
|
105
|
+
from: string;
|
|
106
|
+
to: string;
|
|
107
|
+
sdp: RTCSessionDescriptionInit;
|
|
108
|
+
} | {
|
|
109
|
+
type: 'ice-candidate';
|
|
110
|
+
roomId: string;
|
|
111
|
+
from: string;
|
|
112
|
+
to: string;
|
|
113
|
+
candidate: RTCIceCandidateInit;
|
|
114
|
+
} | {
|
|
115
|
+
type: 'media-state';
|
|
116
|
+
roomId: string;
|
|
117
|
+
peerId: string;
|
|
118
|
+
audioEnabled: boolean;
|
|
119
|
+
videoEnabled: boolean;
|
|
120
|
+
} | {
|
|
121
|
+
type: 'sfu-signal';
|
|
122
|
+
roomId: string;
|
|
123
|
+
from: string;
|
|
124
|
+
to?: string;
|
|
125
|
+
action: string;
|
|
126
|
+
payload: unknown;
|
|
127
|
+
} | {
|
|
128
|
+
type: 'ping';
|
|
129
|
+
timestamp?: number;
|
|
130
|
+
} | {
|
|
131
|
+
type: 'pong';
|
|
132
|
+
timestamp?: number;
|
|
133
|
+
} | {
|
|
134
|
+
type: 'error';
|
|
135
|
+
code: string;
|
|
136
|
+
message: string;
|
|
137
|
+
};
|
|
138
|
+
interface CallClientEvents {
|
|
139
|
+
stateChange: (state: CallState) => void;
|
|
140
|
+
participantJoined: (participant: Participant) => void;
|
|
141
|
+
participantUpdated: (participant: Participant) => void;
|
|
142
|
+
participantLeft: (peerId: string) => void;
|
|
143
|
+
localStreamUpdated: (stream: MediaStream | null) => void;
|
|
144
|
+
trackSubscribed: (payload: {
|
|
145
|
+
peerId: string;
|
|
146
|
+
track: MediaStreamTrack;
|
|
147
|
+
kind: TrackKind;
|
|
148
|
+
}) => void;
|
|
149
|
+
trackUnsubscribed: (payload: {
|
|
150
|
+
peerId: string;
|
|
151
|
+
kind: TrackKind;
|
|
152
|
+
}) => void;
|
|
153
|
+
devicesChanged: (devices: CallDeviceInfo[]) => void;
|
|
154
|
+
selectedDevicesChanged: (devices: SelectedDevices) => void;
|
|
155
|
+
error: (error: Error) => void;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Platform abstraction over WebRTC & MediaDevices APIs.
|
|
160
|
+
* Defaults to BrowserMediaAdapter on web; React Native injects ReactNativeMediaAdapter.
|
|
161
|
+
*/
|
|
162
|
+
interface MediaAdapter {
|
|
163
|
+
getUserMedia(constraints: MediaConstraintsOptions): Promise<MediaStream>;
|
|
164
|
+
enumerateDevices(): Promise<CallDeviceInfo[]>;
|
|
165
|
+
createPeerConnection(config?: RTCConfiguration): RTCPeerConnection;
|
|
166
|
+
createMediaStream(tracks?: MediaStreamTrack[]): MediaStream;
|
|
167
|
+
onDeviceChange?(handler: () => void): () => void;
|
|
168
|
+
}
|
|
169
|
+
declare class BrowserMediaAdapter implements MediaAdapter {
|
|
170
|
+
getUserMedia(constraints: MediaConstraintsOptions): Promise<MediaStream>;
|
|
171
|
+
enumerateDevices(): Promise<CallDeviceInfo[]>;
|
|
172
|
+
createPeerConnection(config?: RTCConfiguration): RTCPeerConnection;
|
|
173
|
+
createMediaStream(tracks?: MediaStreamTrack[]): MediaStream;
|
|
174
|
+
onDeviceChange(handler: () => void): () => void;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface MediaControllerEvents {
|
|
178
|
+
streamUpdated: (stream: MediaStream | null) => void;
|
|
179
|
+
trackChanged: (payload: {
|
|
180
|
+
kind: TrackKind;
|
|
181
|
+
track: MediaStreamTrack | null;
|
|
182
|
+
}) => void;
|
|
183
|
+
muteStateChanged: (payload: {
|
|
184
|
+
audioEnabled: boolean;
|
|
185
|
+
videoEnabled: boolean;
|
|
186
|
+
}) => void;
|
|
187
|
+
devicesChanged: (devices: CallDeviceInfo[]) => void;
|
|
188
|
+
selectedDevicesChanged: (selected: SelectedDevices) => void;
|
|
189
|
+
}
|
|
190
|
+
declare class MediaController extends TypedEventEmitter<MediaControllerEvents> {
|
|
191
|
+
private readonly adapter;
|
|
192
|
+
private stream;
|
|
193
|
+
private audioTrack;
|
|
194
|
+
private videoTrack;
|
|
195
|
+
private devices;
|
|
196
|
+
private selectedDevices;
|
|
197
|
+
private unsubDeviceChange;
|
|
198
|
+
constructor(adapter: MediaAdapter);
|
|
199
|
+
get localStream(): MediaStream | null;
|
|
200
|
+
get currentAudioTrack(): MediaStreamTrack | null;
|
|
201
|
+
get currentVideoTrack(): MediaStreamTrack | null;
|
|
202
|
+
get audioEnabled(): boolean;
|
|
203
|
+
get videoEnabled(): boolean;
|
|
204
|
+
get availableDevices(): CallDeviceInfo[];
|
|
205
|
+
get currentSelectedDevices(): SelectedDevices;
|
|
206
|
+
startLocalMedia(constraints?: MediaConstraintsOptions): Promise<MediaStream>;
|
|
207
|
+
stopLocalMedia(): void;
|
|
208
|
+
setAudioEnabled(enabled: boolean): boolean;
|
|
209
|
+
toggleAudio(): boolean;
|
|
210
|
+
setVideoEnabled(enabled: boolean): boolean;
|
|
211
|
+
toggleVideo(): boolean;
|
|
212
|
+
refreshDevices(): Promise<CallDeviceInfo[]>;
|
|
213
|
+
switchAudioInput(deviceId: string): Promise<MediaStreamTrack>;
|
|
214
|
+
switchVideoInput(deviceId: string): Promise<MediaStreamTrack>;
|
|
215
|
+
setAudioOutput(deviceId: string): void;
|
|
216
|
+
destroy(): void;
|
|
217
|
+
private mergeDeviceId;
|
|
218
|
+
private syncSelectedFromTracks;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Transport-agnostic signaling contract.
|
|
223
|
+
* Implement this interface to plug in WebSocket, Socket.IO, Supabase Realtime,
|
|
224
|
+
* Firebase, Ably, or any custom messaging transport.
|
|
225
|
+
*/
|
|
226
|
+
interface SignalingAdapter {
|
|
227
|
+
readonly state: SignalingState;
|
|
228
|
+
connect(): Promise<void>;
|
|
229
|
+
disconnect(): Promise<void>;
|
|
230
|
+
send(message: SignalMessage): void;
|
|
231
|
+
onMessage(handler: (message: SignalMessage) => void): () => void;
|
|
232
|
+
onStateChange(handler: (state: SignalingState) => void): () => void;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
interface MediaTransportContext {
|
|
236
|
+
roomId: string;
|
|
237
|
+
localPeerId: string;
|
|
238
|
+
iceServers: IceServerConfig[];
|
|
239
|
+
sfuConfig?: Record<string, unknown>;
|
|
240
|
+
mediaAdapter: MediaAdapter;
|
|
241
|
+
sendSignal: (message: SignalMessage) => void;
|
|
242
|
+
onRemoteTrack: (peerId: string, track: MediaStreamTrack, kind: TrackKind) => void;
|
|
243
|
+
onRemoteTrackRemoved: (peerId: string, kind: TrackKind) => void;
|
|
244
|
+
onPeerConnectionStateChange: (peerId: string, state: RTCPeerConnectionState) => void;
|
|
245
|
+
onError: (error: Error) => void;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Pluggable media transport contract.
|
|
249
|
+
* Swap between `MeshTransport` (P2P) and `SfuTransport` (mediasoup, LiveKit, Pion, Janus, etc.)
|
|
250
|
+
* without changing application code.
|
|
251
|
+
*/
|
|
252
|
+
interface MediaTransport {
|
|
253
|
+
readonly name: string;
|
|
254
|
+
init(context: MediaTransportContext): Promise<void>;
|
|
255
|
+
handlePeerJoined(peerId: string): Promise<void>;
|
|
256
|
+
handlePeerLeft(peerId: string): Promise<void>;
|
|
257
|
+
handleSignal(message: SignalMessage): Promise<void>;
|
|
258
|
+
publishTrack(track: MediaStreamTrack, kind: TrackKind): Promise<void>;
|
|
259
|
+
unpublishTrack(kind: TrackKind): Promise<void>;
|
|
260
|
+
replaceTrack(kind: TrackKind, track: MediaStreamTrack | null): Promise<void>;
|
|
261
|
+
destroy(): Promise<void>;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
interface CallClientOptions {
|
|
265
|
+
signaling: SignalingAdapter;
|
|
266
|
+
/** Defaults to `MeshTransport`. Pass `new SfuTransport(driver)` or a custom `MediaTransport` for SFU rooms. */
|
|
267
|
+
transport?: MediaTransport;
|
|
268
|
+
/** Defaults to `BrowserMediaAdapter`. React Native passes `ReactNativeMediaAdapter`. */
|
|
269
|
+
mediaAdapter?: MediaAdapter;
|
|
270
|
+
/** Default ICE servers (STUN/TURN). Server-provided `iceServers` in `joined` merge on top of these. */
|
|
271
|
+
iceServers?: IceServerConfig[];
|
|
272
|
+
}
|
|
273
|
+
declare class CallClient extends TypedEventEmitter<CallClientEvents> {
|
|
274
|
+
readonly media: MediaController;
|
|
275
|
+
private readonly signaling;
|
|
276
|
+
private readonly transport;
|
|
277
|
+
private readonly mediaAdapter;
|
|
278
|
+
private readonly defaultIceServers;
|
|
279
|
+
private _state;
|
|
280
|
+
private _roomId;
|
|
281
|
+
private _localPeerId;
|
|
282
|
+
private participants;
|
|
283
|
+
private unsubSignalMsg;
|
|
284
|
+
private unsubSignalState;
|
|
285
|
+
private joinResolver;
|
|
286
|
+
private joinRejecter;
|
|
287
|
+
private joinTimer;
|
|
288
|
+
private lastJoinOptions;
|
|
289
|
+
constructor(options: CallClientOptions);
|
|
290
|
+
get state(): CallState;
|
|
291
|
+
get roomId(): string | null;
|
|
292
|
+
get localPeerId(): string | null;
|
|
293
|
+
get localParticipant(): Participant | null;
|
|
294
|
+
getParticipants(): Participant[];
|
|
295
|
+
getRemoteParticipants(): Participant[];
|
|
296
|
+
getParticipant(peerId: string): Participant | undefined;
|
|
297
|
+
join(options: JoinOptions): Promise<void>;
|
|
298
|
+
leave(): Promise<void>;
|
|
299
|
+
setAudioEnabled(enabled: boolean): boolean;
|
|
300
|
+
toggleAudio(): boolean;
|
|
301
|
+
setVideoEnabled(enabled: boolean): boolean;
|
|
302
|
+
toggleVideo(): boolean;
|
|
303
|
+
getDevices(): Promise<CallDeviceInfo[]>;
|
|
304
|
+
getSelectedDevices(): SelectedDevices;
|
|
305
|
+
switchAudioInput(deviceId: string): Promise<MediaStreamTrack>;
|
|
306
|
+
switchVideoInput(deviceId: string): Promise<MediaStreamTrack>;
|
|
307
|
+
setAudioOutput(deviceId: string): void;
|
|
308
|
+
destroy(): Promise<void>;
|
|
309
|
+
private bindMediaEvents;
|
|
310
|
+
private bindSignalingEvents;
|
|
311
|
+
private handleSignalMessage;
|
|
312
|
+
private onJoined;
|
|
313
|
+
private onPeerJoined;
|
|
314
|
+
private onPeerLeft;
|
|
315
|
+
private onRemoteMediaState;
|
|
316
|
+
private handleRemoteTrack;
|
|
317
|
+
private handleRemoteTrackRemoved;
|
|
318
|
+
private handlePeerConnectionStateChange;
|
|
319
|
+
private broadcastMediaState;
|
|
320
|
+
private clearJoinTimer;
|
|
321
|
+
private cleanupRoom;
|
|
322
|
+
private setState;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
interface WebSocketLike {
|
|
326
|
+
readonly readyState: number;
|
|
327
|
+
send(data: string): void;
|
|
328
|
+
close(code?: number, reason?: string): void;
|
|
329
|
+
onopen: ((ev: unknown) => void) | null;
|
|
330
|
+
onclose: ((ev: unknown) => void) | null;
|
|
331
|
+
onerror: ((ev: unknown) => void) | null;
|
|
332
|
+
onmessage: ((ev: {
|
|
333
|
+
data: unknown;
|
|
334
|
+
}) => void) | null;
|
|
335
|
+
}
|
|
336
|
+
type WebSocketConstructor = new (url: string, protocols?: string | string[]) => WebSocketLike;
|
|
337
|
+
interface WebSocketSignalingOptions {
|
|
338
|
+
/** WebSocket server URL, e.g. `wss://signal.example.com/ws` */
|
|
339
|
+
url: string;
|
|
340
|
+
/** Optional subprotocols */
|
|
341
|
+
protocols?: string | string[];
|
|
342
|
+
/** Auto-reconnect when dropped unexpectedly. Defaults to `true`. */
|
|
343
|
+
reconnect?: boolean;
|
|
344
|
+
/** Maximum reconnect attempts. Defaults to `10`. */
|
|
345
|
+
maxReconnectAttempts?: number;
|
|
346
|
+
/** Base delay in ms for exponential backoff. Defaults to `1000`. */
|
|
347
|
+
reconnectDelayMs?: number;
|
|
348
|
+
/** Maximum backoff delay in ms. Defaults to `15000`. */
|
|
349
|
+
maxReconnectDelayMs?: number;
|
|
350
|
+
/** Heartbeat ping interval in ms (`0` to disable). Defaults to `25000`. */
|
|
351
|
+
heartbeatIntervalMs?: number;
|
|
352
|
+
/** Maximum queued outbound messages while connecting/reconnecting. Defaults to `100`. */
|
|
353
|
+
maxQueueSize?: number;
|
|
354
|
+
/** Custom WebSocket constructor (useful for Node or React Native overrides). */
|
|
355
|
+
WebSocketImpl?: WebSocketConstructor;
|
|
356
|
+
}
|
|
357
|
+
declare class WebSocketSignalingAdapter implements SignalingAdapter {
|
|
358
|
+
private ws;
|
|
359
|
+
private _state;
|
|
360
|
+
private messageHandlers;
|
|
361
|
+
private stateHandlers;
|
|
362
|
+
private pendingQueue;
|
|
363
|
+
private manualDisconnect;
|
|
364
|
+
private reconnectAttempts;
|
|
365
|
+
private reconnectTimer;
|
|
366
|
+
private heartbeatTimer;
|
|
367
|
+
private readonly url;
|
|
368
|
+
private readonly protocols?;
|
|
369
|
+
private readonly reconnect;
|
|
370
|
+
private readonly maxReconnectAttempts;
|
|
371
|
+
private readonly reconnectDelayMs;
|
|
372
|
+
private readonly maxReconnectDelayMs;
|
|
373
|
+
private readonly heartbeatIntervalMs;
|
|
374
|
+
private readonly maxQueueSize;
|
|
375
|
+
private readonly WebSocketImpl?;
|
|
376
|
+
constructor(options: WebSocketSignalingOptions);
|
|
377
|
+
get state(): SignalingState;
|
|
378
|
+
connect(): Promise<void>;
|
|
379
|
+
disconnect(): Promise<void>;
|
|
380
|
+
send(message: SignalMessage): void;
|
|
381
|
+
onMessage(handler: (message: SignalMessage) => void): () => void;
|
|
382
|
+
onStateChange(handler: (state: SignalingState) => void): () => void;
|
|
383
|
+
private setState;
|
|
384
|
+
private flushQueue;
|
|
385
|
+
private scheduleReconnect;
|
|
386
|
+
private startHeartbeat;
|
|
387
|
+
private stopHeartbeat;
|
|
388
|
+
private clearTimers;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Built-in WebRTC Mesh (P2P) transport using the Perfect Negotiation pattern.
|
|
393
|
+
* Works out of the box for 1-to-1 and small group calls with zero media server overhead.
|
|
394
|
+
*/
|
|
395
|
+
declare class MeshTransport implements MediaTransport {
|
|
396
|
+
readonly name = "mesh";
|
|
397
|
+
private ctx;
|
|
398
|
+
private peers;
|
|
399
|
+
private localTracks;
|
|
400
|
+
init(context: MediaTransportContext): Promise<void>;
|
|
401
|
+
handlePeerJoined(peerId: string): Promise<void>;
|
|
402
|
+
handlePeerLeft(peerId: string): Promise<void>;
|
|
403
|
+
handleSignal(message: SignalMessage): Promise<void>;
|
|
404
|
+
publishTrack(track: MediaStreamTrack, kind: TrackKind): Promise<void>;
|
|
405
|
+
unpublishTrack(kind: TrackKind): Promise<void>;
|
|
406
|
+
replaceTrack(kind: TrackKind, track: MediaStreamTrack | null): Promise<void>;
|
|
407
|
+
/**
|
|
408
|
+
* Triggers ICE restart for a specific peer or all connected peers.
|
|
409
|
+
*/
|
|
410
|
+
restartIce(peerId?: string): void;
|
|
411
|
+
destroy(): Promise<void>;
|
|
412
|
+
private createPeerEntry;
|
|
413
|
+
private attachOrReplaceTrack;
|
|
414
|
+
private handleSessionDescription;
|
|
415
|
+
private handleIceCandidate;
|
|
416
|
+
private flushIceCandidates;
|
|
417
|
+
private closePeerEntry;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Driver interface for plugging any SFU SDK (mediasoup-client, LiveKit, Pion, Janus, etc.)
|
|
422
|
+
* into `@peterchijioke/callkit-core`'s `SfuTransport`.
|
|
423
|
+
*/
|
|
424
|
+
interface SfuDriver {
|
|
425
|
+
readonly name: string;
|
|
426
|
+
connect(params: {
|
|
427
|
+
roomId: string;
|
|
428
|
+
localPeerId: string;
|
|
429
|
+
sfuConfig?: Record<string, unknown>;
|
|
430
|
+
sendSfuSignal: (action: string, payload: unknown, to?: string) => void;
|
|
431
|
+
onRemoteTrack: (peerId: string, track: MediaStreamTrack, kind: TrackKind) => void;
|
|
432
|
+
onRemoteTrackRemoved: (peerId: string, kind: TrackKind) => void;
|
|
433
|
+
onConnectionStateChange?: (state: RTCPeerConnectionState) => void;
|
|
434
|
+
}): Promise<void>;
|
|
435
|
+
handleSfuSignal(action: string, payload: unknown, from: string): Promise<void>;
|
|
436
|
+
publishTrack(track: MediaStreamTrack, kind: TrackKind): Promise<void>;
|
|
437
|
+
unpublishTrack(kind: TrackKind): Promise<void>;
|
|
438
|
+
replaceTrack(kind: TrackKind, track: MediaStreamTrack | null): Promise<void>;
|
|
439
|
+
disconnect(): Promise<void>;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* SFU-ready MediaTransport implementation.
|
|
443
|
+
* Routes `sfu-signal` protocol messages between the SignalingAdapter and any `SfuDriver`.
|
|
444
|
+
*/
|
|
445
|
+
declare class SfuTransport implements MediaTransport {
|
|
446
|
+
private readonly driver;
|
|
447
|
+
readonly name: string;
|
|
448
|
+
private ctx;
|
|
449
|
+
constructor(driver: SfuDriver);
|
|
450
|
+
init(context: MediaTransportContext): Promise<void>;
|
|
451
|
+
handlePeerJoined(_peerId: string): Promise<void>;
|
|
452
|
+
handlePeerLeft(peerId: string): Promise<void>;
|
|
453
|
+
handleSignal(message: SignalMessage): Promise<void>;
|
|
454
|
+
publishTrack(track: MediaStreamTrack, kind: TrackKind): Promise<void>;
|
|
455
|
+
unpublishTrack(kind: TrackKind): Promise<void>;
|
|
456
|
+
replaceTrack(kind: TrackKind, track: MediaStreamTrack | null): Promise<void>;
|
|
457
|
+
destroy(): Promise<void>;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export { BrowserMediaAdapter, CallClient, type CallClientEvents, type CallClientOptions, type CallDeviceInfo, type CallMediaDeviceKind, type CallState, type IceServerConfig, type JoinOptions, type MediaAdapter, type MediaConstraintsOptions, MediaController, type MediaControllerEvents, type MediaTransport, type MediaTransportContext, MeshTransport, type Participant, type RemoteParticipantInfo, type SelectedDevices, type SfuDriver, SfuTransport, type SignalMessage, type SignalingAdapter, type SignalingState, type TrackKind, TypedEventEmitter, type WebSocketConstructor, type WebSocketLike, WebSocketSignalingAdapter, type WebSocketSignalingOptions };
|