@zapiack/react-native 0.1.0 → 0.2.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/CHANGELOG.md +5 -0
- package/README.md +90 -0
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/zapiack/reactnative/ZapiackPushModule.kt +3 -0
- package/dist/calls.d.mts +198 -0
- package/dist/calls.d.ts +198 -0
- package/dist/calls.js +793 -0
- package/dist/calls.mjs +406 -0
- package/dist/chunk-CCLICX3L.mjs +371 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +389 -6
- package/dist/index.mjs +21 -2
- package/dist/realtime-BAcafCSr.d.mts +173 -0
- package/dist/realtime-BAcafCSr.d.ts +173 -0
- package/ios/Zapiack/Api.swift +6 -2
- package/ios/Zapiack/LayoutModel.swift +128 -0
- package/ios/Zapiack/LayoutView.swift +206 -0
- package/ios/Zapiack/Models.swift +1 -1
- package/ios/Zapiack/PushClient.swift +9 -1
- package/ios/Zapiack/ZapiackLiveActivityWidget.swift +170 -0
- package/ios/Zapiack/ZapiackLiveAttributes.swift +78 -0
- package/ios/Zapiack/ZapiackPush.swift +27 -0
- package/ios/ZapiackPushModule.m +1 -0
- package/ios/ZapiackPushModule.swift +5 -0
- package/package.json +32 -5
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to @zapiack/react-native are listed here. Versions follow [semantic versioning](https://semver.org).
|
|
4
4
|
|
|
5
|
+
## 0.2.0
|
|
6
|
+
|
|
7
|
+
- Realtime: `ZapiackRealtime` for live messages over one WebSocket (pure JavaScript): channels, presence, signals, resume after a dropped connection.
|
|
8
|
+
- Calls: `ZapiackCall` from `@zapiack/react-native/calls`, voice and video for one-to-one and group calls over the same connection, with camera switching, speaker routing and recovery when the phone changes network. Needs `react-native-webrtc` and `mediasoup-client`.
|
|
9
|
+
|
|
5
10
|
## 0.1.0
|
|
6
11
|
|
|
7
12
|
First release.
|
package/README.md
CHANGED
|
@@ -71,6 +71,96 @@ of them receives a push: call `ZapiackMessagingService.handle` from yours, as de
|
|
|
71
71
|
}
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
+
## Realtime
|
|
75
|
+
|
|
76
|
+
Live messages over one WebSocket: chat, live updates, presence, and signalling for calls. It is separate from push; use either or both.
|
|
77
|
+
|
|
78
|
+
Your app never holds an API key. Your **server** asks Zapiack for a short-lived token for the signed-in user (the key needs the **Realtime** scope, or no scope limits), and hands it to the app:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
curl -X POST https://api.zapiack.com/v1/realtime/tokens \
|
|
82
|
+
-H "Authorization: Bearer $ZAPIACK_API_KEY" -H "Content-Type: application/json" \
|
|
83
|
+
-d '{"userId":"user_42","capabilities":{"chat:*":["subscribe","publish","presence"]}}'
|
|
84
|
+
# → { "token": "eyJ…", "expiresAt": "…", "url": "wss://rt.zapiack.com/?token=eyJ…" }
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`capabilities` says what that user may do on which channels (`*` matches anything): `subscribe`, `publish`, `presence`, `history`, `signal`. Tokens last an hour unless you ask for less (`ttlSeconds`, at most a day).
|
|
88
|
+
|
|
89
|
+
In your app (plain JavaScript, no native setup):
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { ZapiackRealtime } from '@zapiack/react-native';
|
|
93
|
+
|
|
94
|
+
const rt = new ZapiackRealtime({
|
|
95
|
+
// Ask YOUR server for a token. Called on every connect, so an expired token is replaced.
|
|
96
|
+
getToken: () => fetch('https://your.server/realtime-token').then((r) => r.text()),
|
|
97
|
+
});
|
|
98
|
+
await rt.connect();
|
|
99
|
+
|
|
100
|
+
const stop = rt.subscribe('chat:room-1', (m) => console.log(m.from, m.data));
|
|
101
|
+
await rt.publish('chat:room-1', { text: 'hi' }, { persist: true }); // resolves when the server confirms
|
|
102
|
+
|
|
103
|
+
const here = rt.presence('chat:room-1');
|
|
104
|
+
here.enter({ name: 'Ann' });
|
|
105
|
+
here.onChange(({ action, member }) => console.log(action, member.user));
|
|
106
|
+
rt.signal('call-7', otherConnectionId, 'offer', { sdp }); // to one connection
|
|
107
|
+
rt.onError((e) => console.warn(e.code, e.message));
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
- **Reconnects by itself** with a growing, jittered pause, fetches a fresh token each time, re-joins every channel and replays what it missed since the last message it saw (publish with `persist` to make a message replayable). Messages already seen are not repeated.
|
|
111
|
+
- **Sends made while offline** wait (up to 100) and go out in order once the line is back.
|
|
112
|
+
- A message that cannot be replayed because the client was away longer than the project's retention is reported as an error with code 410: reload that channel's history over REST.
|
|
113
|
+
- The project can refuse a client for good (Realtime switched off, or the connection was removed in the dashboard). Then `connect` fails with code 403 and it does not retry.
|
|
114
|
+
|
|
115
|
+
## Voice and video calls
|
|
116
|
+
|
|
117
|
+
Calls are a separate entry, so apps that only use push or messaging never load WebRTC.
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
npm install react-native-webrtc mediasoup-client react-native-incall-manager
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Follow `react-native-webrtc`'s setup for your React Native version (camera and microphone permissions in `Info.plist` and `AndroidManifest.xml`, and the iOS pod install). Then:
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import { ZapiackCall } from '@zapiack/react-native/calls';
|
|
127
|
+
import InCallManager from 'react-native-incall-manager';
|
|
128
|
+
import { RTCView } from 'react-native-webrtc';
|
|
129
|
+
|
|
130
|
+
const call = new ZapiackCall({
|
|
131
|
+
// Your server asks Zapiack for a token with POST /v1/calls/tokens (or starts the call with POST /v1/calls) and returns it here.
|
|
132
|
+
getToken: () => fetch('https://my-server.example/call-token').then((r) => r.text()),
|
|
133
|
+
// Earpiece, speaker and the phone's call mode.
|
|
134
|
+
audioSession: {
|
|
135
|
+
start: ({ media }) => InCallManager.start({ media }),
|
|
136
|
+
stop: () => InCallManager.stop(),
|
|
137
|
+
setSpeaker: (on) => InCallManager.setForceSpeakerphoneOn(on),
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
call.on('track', ({ identity, source, stream }) => setStreams((s) => ({ ...s, [`${identity}:${source}`]: stream.toURL() })));
|
|
142
|
+
call.on('trackEnded', ({ identity, source }) => setStreams(({ [`${identity}:${source}`]: _gone, ...rest }) => rest));
|
|
143
|
+
call.on('ended', ({ reason }) => navigation.goBack());
|
|
144
|
+
|
|
145
|
+
await call.join();
|
|
146
|
+
await call.enableMicrophone();
|
|
147
|
+
await call.enableCamera(); // front camera; pass { camera: 'environment' } in the options for the back one
|
|
148
|
+
call.setSpeaker(true);
|
|
149
|
+
call.switchCamera(); // front <-> back, without interrupting the video
|
|
150
|
+
await call.setMuted('audio', true);
|
|
151
|
+
await call.leave();
|
|
152
|
+
|
|
153
|
+
// <RTCView streamURL={streams['ada:video']} style={{ flex: 1 }} objectFit="cover" />
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
A person who is a `listener` in the token can receive but never send. Audio from other people plays by itself; only video needs an `RTCView`.
|
|
157
|
+
|
|
158
|
+
A call also re-asks the server who is in the room every 30 seconds (`resyncMs` in the options, 0 turns it off), so an event that was missed costs seconds, not the rest of the call.
|
|
159
|
+
|
|
160
|
+
On a phone the network changes under a call (Wi-Fi to mobile data, a lift, a tunnel). The client waits a few seconds for the connection to recover, then restarts it with fresh parameters from the server, and if the socket itself dropped it rejoins the room and picks up who is there now.
|
|
161
|
+
|
|
162
|
+
**Not covered yet:** keeping a call alive in the background (iOS needs the `voip` background mode and CallKit; Android needs a foreground service) and screen sharing (`shareScreen` works once the Android foreground service and the iOS broadcast extension from `react-native-webrtc`'s guide are in place). Ringing a phone that is closed is done with push: see `POST /v1/calls`.
|
|
163
|
+
|
|
74
164
|
## Maintaining
|
|
75
165
|
|
|
76
166
|
The iOS code in this package is copied from the iOS library: run `npm run sync-ios` after changing it (it also runs before publishing).
|
package/android/build.gradle
CHANGED
|
@@ -77,6 +77,9 @@ class ZapiackPushModule(private val reactContext: ReactApplicationContext) : Rea
|
|
|
77
77
|
|
|
78
78
|
@ReactMethod fun flush(promise: Promise) { ZapiackPush.flush(); promise.resolve(null) }
|
|
79
79
|
|
|
80
|
+
/** Live Activities are iOS only; on Android a template's live parts are drawn in the notification with nothing to turn on. */
|
|
81
|
+
@ReactMethod fun enableLiveActivities(promise: Promise) { promise.resolve(null) }
|
|
82
|
+
|
|
80
83
|
/** The tap that opened the app. The library takes a tap out of the intent when it handles it, so a second call finds nothing. */
|
|
81
84
|
@ReactMethod
|
|
82
85
|
fun initialTap(promise: Promise) {
|
package/dist/calls.d.mts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { Device } from 'mediasoup-client';
|
|
2
|
+
import { MediaStream, MediaStreamTrack } from 'react-native-webrtc';
|
|
3
|
+
import { ZapiackRealtime, RealtimeEnv, ZapiackRealtimeError } from './index.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Zapiack Voice & Video for React Native: join a room and send and receive audio and video.
|
|
7
|
+
*
|
|
8
|
+
* Your server creates the room and mints a token for each person (`POST /v1/calls/rooms`, `POST /v1/calls/tokens`) and hands it to
|
|
9
|
+
* the app, so the API key never reaches the phone. The app joins with the token:
|
|
10
|
+
*
|
|
11
|
+
* const call = new ZapiackCall({ getToken: () => fetch('https://my-server/call-token').then((r) => r.text()) });
|
|
12
|
+
* call.on('track', ({ identity, source, stream }) => show(identity, source, stream.toURL())); // <RTCView streamURL=... />
|
|
13
|
+
* await call.join();
|
|
14
|
+
* await call.enableMicrophone();
|
|
15
|
+
* await call.enableCamera();
|
|
16
|
+
*
|
|
17
|
+
* What a person may send comes from their token: a listener can only receive. Needs `react-native-webrtc` and `mediasoup-client`
|
|
18
|
+
* (both peer dependencies of this entry only; push and messaging do not need them).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Where sound plays. Wire it to `react-native-incall-manager` (see the README) so the earpiece, speaker and the phone's call mode work. */
|
|
22
|
+
interface AudioSession {
|
|
23
|
+
start(options: {
|
|
24
|
+
media: 'audio' | 'video';
|
|
25
|
+
}): void;
|
|
26
|
+
stop(): void;
|
|
27
|
+
setSpeaker(on: boolean): void;
|
|
28
|
+
}
|
|
29
|
+
type CallRole = 'host' | 'speaker' | 'listener';
|
|
30
|
+
/** What a track is: a microphone, a camera, or a shared screen. */
|
|
31
|
+
type TrackSource = 'audio' | 'video' | 'screen';
|
|
32
|
+
interface CallOptions {
|
|
33
|
+
/** Returns a call token from your server (`POST /v1/calls/tokens`). Called on every connect, so an expired one is replaced. */
|
|
34
|
+
getToken: () => Promise<string>;
|
|
35
|
+
/** Default `wss://rt.zapiack.com`. */
|
|
36
|
+
url?: string;
|
|
37
|
+
/** Send and receive over TCP only. For networks that block UDP. Default false. */
|
|
38
|
+
forceTcp?: boolean;
|
|
39
|
+
/** How often, in milliseconds, the client asks the server who is in the room and catches up on anything it missed. Default 30000. 0 turns it off. */
|
|
40
|
+
resyncMs?: number;
|
|
41
|
+
/** Sound routing and the phone's call mode. Started when the call is joined and stopped when it ends. */
|
|
42
|
+
audioSession?: AudioSession;
|
|
43
|
+
/** Which camera `enableCamera` opens first. Default `user` (the front one). */
|
|
44
|
+
camera?: 'user' | 'environment';
|
|
45
|
+
}
|
|
46
|
+
interface Peer {
|
|
47
|
+
identity: string;
|
|
48
|
+
role: CallRole;
|
|
49
|
+
}
|
|
50
|
+
/** Someone else's audio, video or screen. Give `stream.toURL()` to an `<RTCView streamURL={...} />`; audio plays by itself. */
|
|
51
|
+
interface RemoteTrack {
|
|
52
|
+
identity: string;
|
|
53
|
+
source: TrackSource;
|
|
54
|
+
track: MediaStreamTrack;
|
|
55
|
+
stream: MediaStream;
|
|
56
|
+
/** The sender's track id: the same one `trackEnded` and `paused` carry. */
|
|
57
|
+
producerId: string;
|
|
58
|
+
}
|
|
59
|
+
interface CallEvents {
|
|
60
|
+
/** Someone's track arrived. */
|
|
61
|
+
track: RemoteTrack;
|
|
62
|
+
/** Someone stopped sending a track, or left. */
|
|
63
|
+
trackEnded: {
|
|
64
|
+
identity: string;
|
|
65
|
+
source: TrackSource;
|
|
66
|
+
producerId: string;
|
|
67
|
+
};
|
|
68
|
+
/** Someone muted or unmuted a track. The track stays; what it carries is silence or a frozen picture. */
|
|
69
|
+
paused: {
|
|
70
|
+
identity: string;
|
|
71
|
+
source: TrackSource;
|
|
72
|
+
producerId: string;
|
|
73
|
+
paused: boolean;
|
|
74
|
+
};
|
|
75
|
+
peerJoined: Peer;
|
|
76
|
+
peerLeft: {
|
|
77
|
+
identity: string;
|
|
78
|
+
};
|
|
79
|
+
/** The room was ended by your server. The call is already closed when this fires. */
|
|
80
|
+
ended: {
|
|
81
|
+
reason: string;
|
|
82
|
+
};
|
|
83
|
+
/** A problem worth showing: a track that could not be played, or a refused request. */
|
|
84
|
+
error: ZapiackRealtimeError;
|
|
85
|
+
}
|
|
86
|
+
type CallState = 'idle' | 'joining' | 'joined' | 'reconnecting' | 'closed';
|
|
87
|
+
type Listener<K extends keyof CallEvents> = (e: CallEvents[K]) => void;
|
|
88
|
+
/** What can be swapped in a test. */
|
|
89
|
+
interface CallEnv {
|
|
90
|
+
realtime?: ZapiackRealtime;
|
|
91
|
+
realtimeEnv?: RealtimeEnv;
|
|
92
|
+
device?: () => Device;
|
|
93
|
+
getUserMedia?: (c: Record<string, unknown>) => Promise<MediaStream>;
|
|
94
|
+
getDisplayMedia?: () => Promise<MediaStream>;
|
|
95
|
+
newStream?: (tracks: MediaStreamTrack[]) => MediaStream;
|
|
96
|
+
setTimeout?: (fn: () => void, ms: number) => unknown;
|
|
97
|
+
clearTimeout?: (id: unknown) => void;
|
|
98
|
+
}
|
|
99
|
+
declare class ZapiackCall {
|
|
100
|
+
private readonly options;
|
|
101
|
+
private readonly env;
|
|
102
|
+
private readonly rt;
|
|
103
|
+
private readonly device;
|
|
104
|
+
private readonly listeners;
|
|
105
|
+
private sendT;
|
|
106
|
+
private recvT;
|
|
107
|
+
private readonly producers;
|
|
108
|
+
/** Everything being received, by the sender's track id. */
|
|
109
|
+
private readonly consumers;
|
|
110
|
+
private readonly peers;
|
|
111
|
+
private joinedReply;
|
|
112
|
+
private roomChannel;
|
|
113
|
+
private stopRoom;
|
|
114
|
+
private stopState;
|
|
115
|
+
private _state;
|
|
116
|
+
/** Tracks being fetched right now, so two events for the same track do not make two consumers. */
|
|
117
|
+
private readonly fetching;
|
|
118
|
+
private wasConnected;
|
|
119
|
+
private resyncing;
|
|
120
|
+
private resyncTimer;
|
|
121
|
+
private facing;
|
|
122
|
+
private speaker;
|
|
123
|
+
/** A transport that went `disconnected` gets a few seconds to recover by itself before its path is restarted. */
|
|
124
|
+
private readonly lapse;
|
|
125
|
+
constructor(options: CallOptions, env?: CallEnv);
|
|
126
|
+
get state(): CallState;
|
|
127
|
+
/** What this person may do in the room: `listener` can only receive. */
|
|
128
|
+
get role(): CallRole | null;
|
|
129
|
+
get roomId(): string | null;
|
|
130
|
+
/** Who else is in the room now. */
|
|
131
|
+
get members(): Peer[];
|
|
132
|
+
/** The tracks this person is sending now. */
|
|
133
|
+
get sending(): TrackSource[];
|
|
134
|
+
on<K extends keyof CallEvents>(event: K, fn: Listener<K>): () => void;
|
|
135
|
+
private emit;
|
|
136
|
+
/** Connects, joins the room the token is for, and starts receiving what the others are sending. */
|
|
137
|
+
join(): Promise<{
|
|
138
|
+
role: CallRole;
|
|
139
|
+
roomId: string;
|
|
140
|
+
members: Peer[];
|
|
141
|
+
}>;
|
|
142
|
+
/** Turns the microphone on and sends it. Pass your own track to send something else. */
|
|
143
|
+
enableMicrophone(track?: MediaStreamTrack): Promise<MediaStreamTrack>;
|
|
144
|
+
/** Turns the camera on and sends it. Opens the camera set by the `camera` option, front by default. */
|
|
145
|
+
enableCamera(track?: MediaStreamTrack): Promise<MediaStreamTrack>;
|
|
146
|
+
/**
|
|
147
|
+
* Shares the phone's screen. Android needs a foreground service for screen capture and iOS needs a broadcast extension: see
|
|
148
|
+
* react-native-webrtc's screen sharing guide. Stops by itself when the person stops sharing.
|
|
149
|
+
*/
|
|
150
|
+
shareScreen(track?: MediaStreamTrack): Promise<MediaStreamTrack>;
|
|
151
|
+
/** Mutes or unmutes what is being sent. Nothing is sent while muted, and the others are told. */
|
|
152
|
+
setMuted(source: TrackSource, muted: boolean): Promise<void>;
|
|
153
|
+
/** Flips between the front and back camera while the camera is on, without interrupting what is being sent. */
|
|
154
|
+
switchCamera(): void;
|
|
155
|
+
/** Plays sound through the loudspeaker (true) or the earpiece (false). Needs an `audioSession` option; remembered across a reconnect. */
|
|
156
|
+
setSpeaker(on: boolean): void;
|
|
157
|
+
isMuted(source: TrackSource): boolean;
|
|
158
|
+
/** Stops sending a track and lets go of the device (the camera light goes off). */
|
|
159
|
+
stop(source: TrackSource): Promise<void>;
|
|
160
|
+
/**
|
|
161
|
+
* What is arriving now, one entry per track being received: how many bytes and packets, how many were lost, and the jitter in
|
|
162
|
+
* seconds. For a quality indicator, or to see that media is really flowing.
|
|
163
|
+
*/
|
|
164
|
+
stats(): Promise<Array<{
|
|
165
|
+
identity: string;
|
|
166
|
+
source: TrackSource;
|
|
167
|
+
bytesReceived: number;
|
|
168
|
+
packetsReceived: number;
|
|
169
|
+
packetsLost: number;
|
|
170
|
+
jitter: number;
|
|
171
|
+
}>>;
|
|
172
|
+
/** Leaves the room and lets go of the camera, the microphone and the connection. */
|
|
173
|
+
leave(): Promise<void>;
|
|
174
|
+
private userMedia;
|
|
175
|
+
private displayMedia;
|
|
176
|
+
private transport;
|
|
177
|
+
private restartIce;
|
|
178
|
+
private send;
|
|
179
|
+
private reconcile;
|
|
180
|
+
private consume;
|
|
181
|
+
private endTrack;
|
|
182
|
+
private dropFrom;
|
|
183
|
+
private onRoomEvent;
|
|
184
|
+
/** Who this person is in the room, as the server knows them: events about this name are about us. */
|
|
185
|
+
private identity;
|
|
186
|
+
/**
|
|
187
|
+
* Room events travel over best-effort publish and subscribe, so a client does not rely on every one arriving: now and then it asks the
|
|
188
|
+
* server who is in the room and what they are sending, and catches up on anything it missed.
|
|
189
|
+
*/
|
|
190
|
+
private startResync;
|
|
191
|
+
private resync;
|
|
192
|
+
/** The socket dropped and came back: take the place in the room again and see what changed while away. */
|
|
193
|
+
private onLine;
|
|
194
|
+
private teardown;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export { ZapiackCall };
|
|
198
|
+
export type { AudioSession, CallEnv, CallEvents, CallOptions, CallRole, CallState, Peer, RemoteTrack, TrackSource };
|
package/dist/calls.d.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { Device } from 'mediasoup-client';
|
|
2
|
+
import { MediaStream, MediaStreamTrack } from 'react-native-webrtc';
|
|
3
|
+
import { ZapiackRealtime, RealtimeEnv, ZapiackRealtimeError } from './index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Zapiack Voice & Video for React Native: join a room and send and receive audio and video.
|
|
7
|
+
*
|
|
8
|
+
* Your server creates the room and mints a token for each person (`POST /v1/calls/rooms`, `POST /v1/calls/tokens`) and hands it to
|
|
9
|
+
* the app, so the API key never reaches the phone. The app joins with the token:
|
|
10
|
+
*
|
|
11
|
+
* const call = new ZapiackCall({ getToken: () => fetch('https://my-server/call-token').then((r) => r.text()) });
|
|
12
|
+
* call.on('track', ({ identity, source, stream }) => show(identity, source, stream.toURL())); // <RTCView streamURL=... />
|
|
13
|
+
* await call.join();
|
|
14
|
+
* await call.enableMicrophone();
|
|
15
|
+
* await call.enableCamera();
|
|
16
|
+
*
|
|
17
|
+
* What a person may send comes from their token: a listener can only receive. Needs `react-native-webrtc` and `mediasoup-client`
|
|
18
|
+
* (both peer dependencies of this entry only; push and messaging do not need them).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** Where sound plays. Wire it to `react-native-incall-manager` (see the README) so the earpiece, speaker and the phone's call mode work. */
|
|
22
|
+
interface AudioSession {
|
|
23
|
+
start(options: {
|
|
24
|
+
media: 'audio' | 'video';
|
|
25
|
+
}): void;
|
|
26
|
+
stop(): void;
|
|
27
|
+
setSpeaker(on: boolean): void;
|
|
28
|
+
}
|
|
29
|
+
type CallRole = 'host' | 'speaker' | 'listener';
|
|
30
|
+
/** What a track is: a microphone, a camera, or a shared screen. */
|
|
31
|
+
type TrackSource = 'audio' | 'video' | 'screen';
|
|
32
|
+
interface CallOptions {
|
|
33
|
+
/** Returns a call token from your server (`POST /v1/calls/tokens`). Called on every connect, so an expired one is replaced. */
|
|
34
|
+
getToken: () => Promise<string>;
|
|
35
|
+
/** Default `wss://rt.zapiack.com`. */
|
|
36
|
+
url?: string;
|
|
37
|
+
/** Send and receive over TCP only. For networks that block UDP. Default false. */
|
|
38
|
+
forceTcp?: boolean;
|
|
39
|
+
/** How often, in milliseconds, the client asks the server who is in the room and catches up on anything it missed. Default 30000. 0 turns it off. */
|
|
40
|
+
resyncMs?: number;
|
|
41
|
+
/** Sound routing and the phone's call mode. Started when the call is joined and stopped when it ends. */
|
|
42
|
+
audioSession?: AudioSession;
|
|
43
|
+
/** Which camera `enableCamera` opens first. Default `user` (the front one). */
|
|
44
|
+
camera?: 'user' | 'environment';
|
|
45
|
+
}
|
|
46
|
+
interface Peer {
|
|
47
|
+
identity: string;
|
|
48
|
+
role: CallRole;
|
|
49
|
+
}
|
|
50
|
+
/** Someone else's audio, video or screen. Give `stream.toURL()` to an `<RTCView streamURL={...} />`; audio plays by itself. */
|
|
51
|
+
interface RemoteTrack {
|
|
52
|
+
identity: string;
|
|
53
|
+
source: TrackSource;
|
|
54
|
+
track: MediaStreamTrack;
|
|
55
|
+
stream: MediaStream;
|
|
56
|
+
/** The sender's track id: the same one `trackEnded` and `paused` carry. */
|
|
57
|
+
producerId: string;
|
|
58
|
+
}
|
|
59
|
+
interface CallEvents {
|
|
60
|
+
/** Someone's track arrived. */
|
|
61
|
+
track: RemoteTrack;
|
|
62
|
+
/** Someone stopped sending a track, or left. */
|
|
63
|
+
trackEnded: {
|
|
64
|
+
identity: string;
|
|
65
|
+
source: TrackSource;
|
|
66
|
+
producerId: string;
|
|
67
|
+
};
|
|
68
|
+
/** Someone muted or unmuted a track. The track stays; what it carries is silence or a frozen picture. */
|
|
69
|
+
paused: {
|
|
70
|
+
identity: string;
|
|
71
|
+
source: TrackSource;
|
|
72
|
+
producerId: string;
|
|
73
|
+
paused: boolean;
|
|
74
|
+
};
|
|
75
|
+
peerJoined: Peer;
|
|
76
|
+
peerLeft: {
|
|
77
|
+
identity: string;
|
|
78
|
+
};
|
|
79
|
+
/** The room was ended by your server. The call is already closed when this fires. */
|
|
80
|
+
ended: {
|
|
81
|
+
reason: string;
|
|
82
|
+
};
|
|
83
|
+
/** A problem worth showing: a track that could not be played, or a refused request. */
|
|
84
|
+
error: ZapiackRealtimeError;
|
|
85
|
+
}
|
|
86
|
+
type CallState = 'idle' | 'joining' | 'joined' | 'reconnecting' | 'closed';
|
|
87
|
+
type Listener<K extends keyof CallEvents> = (e: CallEvents[K]) => void;
|
|
88
|
+
/** What can be swapped in a test. */
|
|
89
|
+
interface CallEnv {
|
|
90
|
+
realtime?: ZapiackRealtime;
|
|
91
|
+
realtimeEnv?: RealtimeEnv;
|
|
92
|
+
device?: () => Device;
|
|
93
|
+
getUserMedia?: (c: Record<string, unknown>) => Promise<MediaStream>;
|
|
94
|
+
getDisplayMedia?: () => Promise<MediaStream>;
|
|
95
|
+
newStream?: (tracks: MediaStreamTrack[]) => MediaStream;
|
|
96
|
+
setTimeout?: (fn: () => void, ms: number) => unknown;
|
|
97
|
+
clearTimeout?: (id: unknown) => void;
|
|
98
|
+
}
|
|
99
|
+
declare class ZapiackCall {
|
|
100
|
+
private readonly options;
|
|
101
|
+
private readonly env;
|
|
102
|
+
private readonly rt;
|
|
103
|
+
private readonly device;
|
|
104
|
+
private readonly listeners;
|
|
105
|
+
private sendT;
|
|
106
|
+
private recvT;
|
|
107
|
+
private readonly producers;
|
|
108
|
+
/** Everything being received, by the sender's track id. */
|
|
109
|
+
private readonly consumers;
|
|
110
|
+
private readonly peers;
|
|
111
|
+
private joinedReply;
|
|
112
|
+
private roomChannel;
|
|
113
|
+
private stopRoom;
|
|
114
|
+
private stopState;
|
|
115
|
+
private _state;
|
|
116
|
+
/** Tracks being fetched right now, so two events for the same track do not make two consumers. */
|
|
117
|
+
private readonly fetching;
|
|
118
|
+
private wasConnected;
|
|
119
|
+
private resyncing;
|
|
120
|
+
private resyncTimer;
|
|
121
|
+
private facing;
|
|
122
|
+
private speaker;
|
|
123
|
+
/** A transport that went `disconnected` gets a few seconds to recover by itself before its path is restarted. */
|
|
124
|
+
private readonly lapse;
|
|
125
|
+
constructor(options: CallOptions, env?: CallEnv);
|
|
126
|
+
get state(): CallState;
|
|
127
|
+
/** What this person may do in the room: `listener` can only receive. */
|
|
128
|
+
get role(): CallRole | null;
|
|
129
|
+
get roomId(): string | null;
|
|
130
|
+
/** Who else is in the room now. */
|
|
131
|
+
get members(): Peer[];
|
|
132
|
+
/** The tracks this person is sending now. */
|
|
133
|
+
get sending(): TrackSource[];
|
|
134
|
+
on<K extends keyof CallEvents>(event: K, fn: Listener<K>): () => void;
|
|
135
|
+
private emit;
|
|
136
|
+
/** Connects, joins the room the token is for, and starts receiving what the others are sending. */
|
|
137
|
+
join(): Promise<{
|
|
138
|
+
role: CallRole;
|
|
139
|
+
roomId: string;
|
|
140
|
+
members: Peer[];
|
|
141
|
+
}>;
|
|
142
|
+
/** Turns the microphone on and sends it. Pass your own track to send something else. */
|
|
143
|
+
enableMicrophone(track?: MediaStreamTrack): Promise<MediaStreamTrack>;
|
|
144
|
+
/** Turns the camera on and sends it. Opens the camera set by the `camera` option, front by default. */
|
|
145
|
+
enableCamera(track?: MediaStreamTrack): Promise<MediaStreamTrack>;
|
|
146
|
+
/**
|
|
147
|
+
* Shares the phone's screen. Android needs a foreground service for screen capture and iOS needs a broadcast extension: see
|
|
148
|
+
* react-native-webrtc's screen sharing guide. Stops by itself when the person stops sharing.
|
|
149
|
+
*/
|
|
150
|
+
shareScreen(track?: MediaStreamTrack): Promise<MediaStreamTrack>;
|
|
151
|
+
/** Mutes or unmutes what is being sent. Nothing is sent while muted, and the others are told. */
|
|
152
|
+
setMuted(source: TrackSource, muted: boolean): Promise<void>;
|
|
153
|
+
/** Flips between the front and back camera while the camera is on, without interrupting what is being sent. */
|
|
154
|
+
switchCamera(): void;
|
|
155
|
+
/** Plays sound through the loudspeaker (true) or the earpiece (false). Needs an `audioSession` option; remembered across a reconnect. */
|
|
156
|
+
setSpeaker(on: boolean): void;
|
|
157
|
+
isMuted(source: TrackSource): boolean;
|
|
158
|
+
/** Stops sending a track and lets go of the device (the camera light goes off). */
|
|
159
|
+
stop(source: TrackSource): Promise<void>;
|
|
160
|
+
/**
|
|
161
|
+
* What is arriving now, one entry per track being received: how many bytes and packets, how many were lost, and the jitter in
|
|
162
|
+
* seconds. For a quality indicator, or to see that media is really flowing.
|
|
163
|
+
*/
|
|
164
|
+
stats(): Promise<Array<{
|
|
165
|
+
identity: string;
|
|
166
|
+
source: TrackSource;
|
|
167
|
+
bytesReceived: number;
|
|
168
|
+
packetsReceived: number;
|
|
169
|
+
packetsLost: number;
|
|
170
|
+
jitter: number;
|
|
171
|
+
}>>;
|
|
172
|
+
/** Leaves the room and lets go of the camera, the microphone and the connection. */
|
|
173
|
+
leave(): Promise<void>;
|
|
174
|
+
private userMedia;
|
|
175
|
+
private displayMedia;
|
|
176
|
+
private transport;
|
|
177
|
+
private restartIce;
|
|
178
|
+
private send;
|
|
179
|
+
private reconcile;
|
|
180
|
+
private consume;
|
|
181
|
+
private endTrack;
|
|
182
|
+
private dropFrom;
|
|
183
|
+
private onRoomEvent;
|
|
184
|
+
/** Who this person is in the room, as the server knows them: events about this name are about us. */
|
|
185
|
+
private identity;
|
|
186
|
+
/**
|
|
187
|
+
* Room events travel over best-effort publish and subscribe, so a client does not rely on every one arriving: now and then it asks the
|
|
188
|
+
* server who is in the room and what they are sending, and catches up on anything it missed.
|
|
189
|
+
*/
|
|
190
|
+
private startResync;
|
|
191
|
+
private resync;
|
|
192
|
+
/** The socket dropped and came back: take the place in the room again and see what changed while away. */
|
|
193
|
+
private onLine;
|
|
194
|
+
private teardown;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export { ZapiackCall };
|
|
198
|
+
export type { AudioSession, CallEnv, CallEvents, CallOptions, CallRole, CallState, Peer, RemoteTrack, TrackSource };
|