@lynkoria/react-native 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 +201 -0
- package/README.md +9 -2
- package/android/build.gradle +32 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/lynkoria/reactnative/LynkoriaModule.kt +177 -0
- package/android/src/main/java/com/lynkoria/reactnative/LynkoriaPackage.kt +20 -0
- package/android/src/main/java/com/lynkoria/reactnative/LynkoriaVideoViewManager.kt +69 -0
- package/app.plugin.js +33 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +157 -0
- package/ios/LynkoriaRNImpl.swift +159 -0
- package/ios/LynkoriaVideoHost.swift +17 -0
- package/ios/RNLynkoria.mm +85 -0
- package/ios/RNLynkoriaVideoView.mm +42 -0
- package/lynkoria-react-native.podspec +26 -0
- package/package.json +74 -3
- package/src/index.ts +208 -0
- package/src/specs/LynkoriaVideoViewNativeComponent.ts +16 -0
- package/src/specs/NativeLynkoria.ts +48 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { PushPayloadKeys } from '@lynkoria/contracts';
|
|
2
|
+
import NativeLynkoria, {
|
|
3
|
+
type CallActionEvent,
|
|
4
|
+
type NativeParticipant,
|
|
5
|
+
} from './specs/NativeLynkoria.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Messaging, presence and call signaling reuse `@lynkoria/client` (pure JS over React Native's
|
|
9
|
+
* native WebSocket); media is native. See docs/react-native-sdk.md.
|
|
10
|
+
*/
|
|
11
|
+
export { createLynkoria, LynkoriaError, ErrorCode } from '@lynkoria/client';
|
|
12
|
+
export type { LynkoriaClient, LynkoriaClientOptions } from '@lynkoria/client';
|
|
13
|
+
export { default as LynkoriaVideoView } from './specs/LynkoriaVideoViewNativeComponent.ts';
|
|
14
|
+
export type { NativeProps as LynkoriaVideoViewProps } from './specs/LynkoriaVideoViewNativeComponent.ts';
|
|
15
|
+
export type { NativeParticipant } from './specs/NativeLynkoria.ts';
|
|
16
|
+
|
|
17
|
+
export type CallState = 'connecting' | 'connected' | 'reconnecting' | 'disconnected';
|
|
18
|
+
type Unsubscribe = () => void;
|
|
19
|
+
|
|
20
|
+
const STATES: readonly CallState[] = ['connecting', 'connected', 'reconnecting', 'disconnected'];
|
|
21
|
+
const toState = (s: string): CallState =>
|
|
22
|
+
(STATES as readonly string[]).includes(s) ? (s as CallState) : 'disconnected';
|
|
23
|
+
|
|
24
|
+
export type JoinCallOptions = {
|
|
25
|
+
/** Calls your product backend, which authorizes the user and returns Lynkoria join credentials. */
|
|
26
|
+
getJoinToken: () => Promise<{ url: string; token: string }>;
|
|
27
|
+
audio?: boolean;
|
|
28
|
+
video?: boolean;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** The active native call. Only one call is active per app; join again after `leave()`. */
|
|
32
|
+
export class NativeCall {
|
|
33
|
+
private current: CallState = 'connecting';
|
|
34
|
+
private ended = false;
|
|
35
|
+
private readonly subscriptions: { remove(): void }[] = [];
|
|
36
|
+
private readonly stateListeners = new Set<(s: CallState) => void>();
|
|
37
|
+
private readonly participantListeners = new Set<() => void>();
|
|
38
|
+
|
|
39
|
+
/** @internal use {@link joinCall} */
|
|
40
|
+
constructor() {
|
|
41
|
+
this.subscriptions.push(
|
|
42
|
+
NativeLynkoria.onStateChanged(({ state }) => {
|
|
43
|
+
this.current = toState(state);
|
|
44
|
+
for (const l of this.stateListeners) l(this.current);
|
|
45
|
+
}),
|
|
46
|
+
NativeLynkoria.onParticipantsChanged(() => {
|
|
47
|
+
for (const l of this.participantListeners) l();
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get state(): CallState {
|
|
53
|
+
return this.current;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
onStateChange(listener: (state: CallState) => void): Unsubscribe {
|
|
57
|
+
this.stateListeners.add(listener);
|
|
58
|
+
return () => this.stateListeners.delete(listener);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
onParticipantsChange(listener: () => void): Unsubscribe {
|
|
62
|
+
this.participantListeners.add(listener);
|
|
63
|
+
return () => this.participantListeners.delete(listener);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
participants(): Promise<readonly NativeParticipant[]> {
|
|
67
|
+
return NativeLynkoria.participants();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
setMicrophoneEnabled(enabled: boolean): Promise<void> {
|
|
71
|
+
return NativeLynkoria.setMicrophoneEnabled(enabled);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
setCameraEnabled(enabled: boolean): Promise<void> {
|
|
75
|
+
return NativeLynkoria.setCameraEnabled(enabled);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
startScreenShare(): Promise<void> {
|
|
79
|
+
return NativeLynkoria.startScreenShare();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
stopScreenShare(): Promise<void> {
|
|
83
|
+
return NativeLynkoria.stopScreenShare();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
audioDevices(): Promise<readonly string[]> {
|
|
87
|
+
return NativeLynkoria.audioDevices();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
selectAudioDevice(name: string): Promise<void> {
|
|
91
|
+
return NativeLynkoria.selectAudioDevice(name);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async leave(): Promise<void> {
|
|
95
|
+
if (this.ended) return;
|
|
96
|
+
this.ended = true;
|
|
97
|
+
try {
|
|
98
|
+
await NativeLynkoria.leave();
|
|
99
|
+
} finally {
|
|
100
|
+
for (const s of this.subscriptions) s.remove();
|
|
101
|
+
this.current = 'disconnected';
|
|
102
|
+
for (const l of this.stateListeners) l('disconnected');
|
|
103
|
+
this.stateListeners.clear();
|
|
104
|
+
this.participantListeners.clear();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let active: NativeCall | null = null;
|
|
110
|
+
|
|
111
|
+
/** Requests permissions, fetches join credentials from your backend and connects natively. */
|
|
112
|
+
export async function joinCall(opts: JoinCallOptions): Promise<NativeCall> {
|
|
113
|
+
const audio = opts.audio ?? true;
|
|
114
|
+
const video = opts.video ?? false;
|
|
115
|
+
if (active && active.state !== 'disconnected')
|
|
116
|
+
throw new Error('A call is already active; leave it first');
|
|
117
|
+
if (!(await NativeLynkoria.requestPermissions(video)))
|
|
118
|
+
throw new Error(
|
|
119
|
+
video ? 'Microphone and camera permission denied' : 'Microphone permission denied',
|
|
120
|
+
);
|
|
121
|
+
const { url, token } = await opts.getJoinToken();
|
|
122
|
+
const call = new NativeCall();
|
|
123
|
+
active = call;
|
|
124
|
+
try {
|
|
125
|
+
await NativeLynkoria.join(url, token, audio, video);
|
|
126
|
+
} catch (err) {
|
|
127
|
+
await call.leave().catch(() => undefined);
|
|
128
|
+
active = null;
|
|
129
|
+
throw err;
|
|
130
|
+
}
|
|
131
|
+
return call;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ---------------------------------------------------------------------------
|
|
135
|
+
// Push and incoming calls
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
|
|
138
|
+
export type LynkoriaPush =
|
|
139
|
+
| {
|
|
140
|
+
kind: 'call_incoming';
|
|
141
|
+
callId: string;
|
|
142
|
+
callType: string;
|
|
143
|
+
fromUserId?: string;
|
|
144
|
+
callerName?: string;
|
|
145
|
+
tenantId?: string;
|
|
146
|
+
}
|
|
147
|
+
| { kind: 'call_cancelled'; callId: string }
|
|
148
|
+
| { kind: 'message' | 'custom'; data: Record<string, string> };
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Parses a Lynkoria push from FCM `data` or the APNs `lynkoria` dictionary (same keys as the
|
|
152
|
+
* Kotlin and Swift SDKs). Returns null for pushes that are not from Lynkoria.
|
|
153
|
+
*/
|
|
154
|
+
export function parsePush(data: Record<string, string | undefined>): LynkoriaPush | null {
|
|
155
|
+
const kind = data[PushPayloadKeys.kind];
|
|
156
|
+
const callId = data[PushPayloadKeys.callId];
|
|
157
|
+
if (kind === 'call_incoming' && callId) {
|
|
158
|
+
const fromUserId = data[PushPayloadKeys.fromUserId];
|
|
159
|
+
const callerName = data.lynkoria_caller_name;
|
|
160
|
+
const tenantId = data[PushPayloadKeys.tenantId];
|
|
161
|
+
return {
|
|
162
|
+
kind,
|
|
163
|
+
callId,
|
|
164
|
+
callType: data[PushPayloadKeys.callType] ?? 'one_to_one',
|
|
165
|
+
...(fromUserId ? { fromUserId } : {}),
|
|
166
|
+
...(callerName ? { callerName } : {}),
|
|
167
|
+
...(tenantId ? { tenantId } : {}),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
if (kind === 'call_cancelled' && callId) return { kind, callId };
|
|
171
|
+
if (kind === 'message' || kind === 'custom') {
|
|
172
|
+
const clean: Record<string, string> = {};
|
|
173
|
+
for (const [k, v] of Object.entries(data)) if (typeof v === 'string') clean[k] = v;
|
|
174
|
+
return { kind, data: clean };
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** System incoming-call UI (CallKit / Android CallStyle notification) and VoIP registration. */
|
|
180
|
+
export const incomingCalls = {
|
|
181
|
+
/** Shows the system ringing UI for a Lynkoria `call_incoming` push. Returns true if handled. */
|
|
182
|
+
handlePush(data: Record<string, string | undefined>): boolean {
|
|
183
|
+
const push = parsePush(data);
|
|
184
|
+
if (push?.kind === 'call_incoming') {
|
|
185
|
+
NativeLynkoria.displayIncomingCall(
|
|
186
|
+
push.callId,
|
|
187
|
+
push.callerName ?? push.fromUserId ?? 'Incoming call',
|
|
188
|
+
push.callType !== 'audio',
|
|
189
|
+
);
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
if (push?.kind === 'call_cancelled') {
|
|
193
|
+
NativeLynkoria.endIncomingCall(push.callId);
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
},
|
|
198
|
+
onAction(listener: (event: CallActionEvent) => void): Unsubscribe {
|
|
199
|
+
const sub = NativeLynkoria.onCallAction(listener);
|
|
200
|
+
return () => sub.remove();
|
|
201
|
+
},
|
|
202
|
+
/** iOS: PushKit token for `registerPushToken({ provider: 'apns_voip' })`. No-op on Android. */
|
|
203
|
+
registerVoip(listener: (token: string) => void): Unsubscribe {
|
|
204
|
+
const sub = NativeLynkoria.onVoipToken(({ token }) => listener(token));
|
|
205
|
+
NativeLynkoria.registerVoip();
|
|
206
|
+
return () => sub.remove();
|
|
207
|
+
},
|
|
208
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CodegenTypes, HostComponent, ViewProps } from 'react-native';
|
|
2
|
+
import { codegenNativeComponent } from 'react-native';
|
|
3
|
+
|
|
4
|
+
export interface NativeProps extends ViewProps {
|
|
5
|
+
/** Participant identity from `participants()`; the local participant uses its own identity. */
|
|
6
|
+
participantIdentity: string;
|
|
7
|
+
/** `camera` (default) or `screen`. */
|
|
8
|
+
source?: CodegenTypes.WithDefault<'camera' | 'screen', 'camera'>;
|
|
9
|
+
mirror?: CodegenTypes.WithDefault<boolean, false>;
|
|
10
|
+
/** `fill` crops to cover the view; `fit` letterboxes. */
|
|
11
|
+
scaleMode?: CodegenTypes.WithDefault<'fill' | 'fit', 'fill'>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default codegenNativeComponent<NativeProps>(
|
|
15
|
+
'LynkoriaVideoView',
|
|
16
|
+
) as HostComponent<NativeProps>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CodegenTypes, TurboModule } from 'react-native';
|
|
2
|
+
import { TurboModuleRegistry } from 'react-native';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* TurboModule backed by the native Lynkoria SDKs (`lynkoria-android` on LiveKit Android,
|
|
6
|
+
* `Lynkoria` on LiveKit Swift). Media never runs in JavaScript or a WebView. One active call per app.
|
|
7
|
+
*/
|
|
8
|
+
export type NativeParticipant = {
|
|
9
|
+
identity: string;
|
|
10
|
+
isLocal: boolean;
|
|
11
|
+
hasAudio: boolean;
|
|
12
|
+
hasVideo: boolean;
|
|
13
|
+
hasScreenShare: boolean;
|
|
14
|
+
isSpeaking: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type CallStateEvent = { state: string };
|
|
18
|
+
export type CallActionEvent = { callId: string; action: string };
|
|
19
|
+
export type TokenEvent = { token: string };
|
|
20
|
+
|
|
21
|
+
export interface Spec extends TurboModule {
|
|
22
|
+
/** Asks for microphone (and camera) permission; resolves true when granted. */
|
|
23
|
+
requestPermissions(video: boolean): Promise<boolean>;
|
|
24
|
+
join(url: string, token: string, enableMicrophone: boolean, enableCamera: boolean): Promise<void>;
|
|
25
|
+
leave(): Promise<void>;
|
|
26
|
+
setMicrophoneEnabled(enabled: boolean): Promise<void>;
|
|
27
|
+
setCameraEnabled(enabled: boolean): Promise<void>;
|
|
28
|
+
/** Android: MediaProjection consent + foreground service. iOS: ReplayKit broadcast picker. */
|
|
29
|
+
startScreenShare(): Promise<void>;
|
|
30
|
+
stopScreenShare(): Promise<void>;
|
|
31
|
+
audioDevices(): Promise<ReadonlyArray<string>>;
|
|
32
|
+
selectAudioDevice(name: string): Promise<void>;
|
|
33
|
+
participants(): Promise<ReadonlyArray<NativeParticipant>>;
|
|
34
|
+
|
|
35
|
+
/** Incoming-call UI: CallKit on iOS, a CallStyle full-screen notification on Android. */
|
|
36
|
+
displayIncomingCall(callId: string, callerName: string, hasVideo: boolean): void;
|
|
37
|
+
endIncomingCall(callId: string): void;
|
|
38
|
+
/** iOS only: start PushKit registration; the token arrives through onVoipToken. */
|
|
39
|
+
registerVoip(): void;
|
|
40
|
+
|
|
41
|
+
readonly onStateChanged: CodegenTypes.EventEmitter<CallStateEvent>;
|
|
42
|
+
readonly onParticipantsChanged: CodegenTypes.EventEmitter<CallStateEvent>;
|
|
43
|
+
/** `answer` or `decline` from the system call UI. */
|
|
44
|
+
readonly onCallAction: CodegenTypes.EventEmitter<CallActionEvent>;
|
|
45
|
+
readonly onVoipToken: CodegenTypes.EventEmitter<TokenEvent>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default TurboModuleRegistry.getEnforcing<Spec>('Lynkoria');
|