livevoice-sdk-react-native 2.0.1 → 3.0.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.
Files changed (207) hide show
  1. package/README.md +105 -66
  2. package/android/build.gradle +5 -4
  3. package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +122 -188
  4. package/android/src/main/java/io/livevoice/react/__SDK.kt +2 -2
  5. package/ios/LiveVoiceRNBridge.swift +362 -0
  6. package/ios/LiveVoiceSdkReactNative.mm +187 -158
  7. package/ios/__SDK.h +1 -1
  8. package/ios/livevoice_spm_backport.rb +202 -0
  9. package/lib/module/components/BrandingView.js +58 -39
  10. package/lib/module/components/BrandingView.js.map +1 -1
  11. package/lib/module/components/ChannelView.js +355 -44
  12. package/lib/module/components/ChannelView.js.map +1 -1
  13. package/lib/module/components/ErrorView.js +5 -6
  14. package/lib/module/components/ErrorView.js.map +1 -1
  15. package/lib/module/components/LiveVoiceSpinner.js +81 -0
  16. package/lib/module/components/LiveVoiceSpinner.js.map +1 -0
  17. package/lib/module/components/LiveVoiceView.js +311 -133
  18. package/lib/module/components/LiveVoiceView.js.map +1 -1
  19. package/lib/module/components/PasswordPrompt.js +165 -0
  20. package/lib/module/components/PasswordPrompt.js.map +1 -0
  21. package/lib/module/components/PlayButtonView.js +180 -244
  22. package/lib/module/components/PlayButtonView.js.map +1 -1
  23. package/lib/module/components/ReconnectingView.js +31 -53
  24. package/lib/module/components/ReconnectingView.js.map +1 -1
  25. package/lib/module/icons/branding-ios.png +0 -0
  26. package/lib/module/icons/camera-off-android.png +0 -0
  27. package/lib/module/icons/camera-off-android@2x.png +0 -0
  28. package/lib/module/icons/camera-off-android@3x.png +0 -0
  29. package/lib/module/icons/cpu-android.png +0 -0
  30. package/lib/module/icons/cpu-android@2x.png +0 -0
  31. package/lib/module/icons/cpu-android@3x.png +0 -0
  32. package/lib/module/icons/cpu-ios.png +0 -0
  33. package/lib/module/icons/cpu-ios@2x.png +0 -0
  34. package/lib/module/icons/cpu-ios@3x.png +0 -0
  35. package/lib/module/icons/lock-android.png +0 -0
  36. package/lib/module/icons/lock-android@2x.png +0 -0
  37. package/lib/module/icons/lock-android@3x.png +0 -0
  38. package/lib/module/icons/lock-ios.png +0 -0
  39. package/lib/module/icons/lock-ios@2x.png +0 -0
  40. package/lib/module/icons/lock-ios@3x.png +0 -0
  41. package/lib/module/icons/mic-off-android.png +0 -0
  42. package/lib/module/icons/mic-off-android@2x.png +0 -0
  43. package/lib/module/icons/mic-off-android@3x.png +0 -0
  44. package/lib/module/icons/mic-slash-ios.png +0 -0
  45. package/lib/module/icons/mic-slash-ios@2x.png +0 -0
  46. package/lib/module/icons/mic-slash-ios@3x.png +0 -0
  47. package/lib/module/icons/video-slash-ios.png +0 -0
  48. package/lib/module/icons/video-slash-ios@2x.png +0 -0
  49. package/lib/module/icons/video-slash-ios@3x.png +0 -0
  50. package/lib/module/index.js +11 -6
  51. package/lib/module/index.js.map +1 -1
  52. package/lib/module/sdk/Bridge.js +236 -0
  53. package/lib/module/sdk/Bridge.js.map +1 -0
  54. package/lib/module/sdk/Event.js +160 -0
  55. package/lib/module/sdk/Event.js.map +1 -0
  56. package/lib/module/sdk/Lifecycle.js +331 -0
  57. package/lib/module/sdk/Lifecycle.js.map +1 -0
  58. package/lib/module/sdk/LiveVoiceSdk.js +262 -268
  59. package/lib/module/sdk/LiveVoiceSdk.js.map +1 -1
  60. package/lib/module/sdk/LiveVoiceState.js +208 -40
  61. package/lib/module/sdk/LiveVoiceState.js.map +1 -1
  62. package/lib/module/sdk/LogEntries.js +99 -0
  63. package/lib/module/sdk/LogEntries.js.map +1 -0
  64. package/lib/module/sdk/Models.js +2 -0
  65. package/lib/module/{util/UIState.js.map → sdk/Models.js.map} +1 -1
  66. package/lib/module/{util/PlayingState.js → sdk/NativeSdkVersions.js} +9 -8
  67. package/lib/module/sdk/NativeSdkVersions.js.map +1 -0
  68. package/lib/module/sdk/Observables.js +391 -0
  69. package/lib/module/sdk/Observables.js.map +1 -0
  70. package/lib/module/sdk/Passwords.js +298 -0
  71. package/lib/module/sdk/Passwords.js.map +1 -0
  72. package/lib/module/sdk/Playback.js +35 -0
  73. package/lib/module/sdk/Playback.js.map +1 -0
  74. package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
  75. package/lib/module/util/Colors.js +2 -1
  76. package/lib/module/util/Colors.js.map +1 -1
  77. package/lib/module/util/Translations.js +34 -11
  78. package/lib/module/util/Translations.js.map +1 -1
  79. package/lib/module/util/freeze.js +26 -0
  80. package/lib/module/util/freeze.js.map +1 -0
  81. package/lib/module/util/logger.js +79 -41
  82. package/lib/module/util/logger.js.map +1 -1
  83. package/lib/typescript/src/components/BrandingView.d.ts +4 -0
  84. package/lib/typescript/src/components/BrandingView.d.ts.map +1 -1
  85. package/lib/typescript/src/components/ChannelView.d.ts +29 -3
  86. package/lib/typescript/src/components/ChannelView.d.ts.map +1 -1
  87. package/lib/typescript/src/components/ErrorView.d.ts +5 -3
  88. package/lib/typescript/src/components/ErrorView.d.ts.map +1 -1
  89. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts +16 -0
  90. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts.map +1 -0
  91. package/lib/typescript/src/components/LiveVoiceView.d.ts +81 -21
  92. package/lib/typescript/src/components/LiveVoiceView.d.ts.map +1 -1
  93. package/lib/typescript/src/components/PasswordPrompt.d.ts +13 -0
  94. package/lib/typescript/src/components/PasswordPrompt.d.ts.map +1 -0
  95. package/lib/typescript/src/components/PlayButtonView.d.ts +12 -329
  96. package/lib/typescript/src/components/PlayButtonView.d.ts.map +1 -1
  97. package/lib/typescript/src/components/ReconnectingView.d.ts +6 -3
  98. package/lib/typescript/src/components/ReconnectingView.d.ts.map +1 -1
  99. package/lib/typescript/src/index.d.ts +15 -6
  100. package/lib/typescript/src/index.d.ts.map +1 -1
  101. package/lib/typescript/src/sdk/Bridge.d.ts +36 -0
  102. package/lib/typescript/src/sdk/Bridge.d.ts.map +1 -0
  103. package/lib/typescript/src/sdk/Event.d.ts +97 -0
  104. package/lib/typescript/src/sdk/Event.d.ts.map +1 -0
  105. package/lib/typescript/src/sdk/Lifecycle.d.ts +191 -0
  106. package/lib/typescript/src/sdk/Lifecycle.d.ts.map +1 -0
  107. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts +195 -190
  108. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -1
  109. package/lib/typescript/src/sdk/LiveVoiceState.d.ts +77 -9
  110. package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -1
  111. package/lib/typescript/src/sdk/LogEntries.d.ts +28 -0
  112. package/lib/typescript/src/sdk/LogEntries.d.ts.map +1 -0
  113. package/lib/typescript/src/sdk/Models.d.ts +114 -0
  114. package/lib/typescript/src/sdk/Models.d.ts.map +1 -0
  115. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts +6 -0
  116. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts.map +1 -0
  117. package/lib/typescript/src/sdk/Observables.d.ts +123 -0
  118. package/lib/typescript/src/sdk/Observables.d.ts.map +1 -0
  119. package/lib/typescript/src/sdk/Passwords.d.ts +72 -0
  120. package/lib/typescript/src/sdk/Passwords.d.ts.map +1 -0
  121. package/lib/typescript/src/sdk/Playback.d.ts +9 -0
  122. package/lib/typescript/src/sdk/Playback.d.ts.map +1 -0
  123. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts +34 -31
  124. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts.map +1 -1
  125. package/lib/typescript/src/util/Colors.d.ts.map +1 -1
  126. package/lib/typescript/src/util/Translations.d.ts +30 -8
  127. package/lib/typescript/src/util/Translations.d.ts.map +1 -1
  128. package/lib/typescript/src/util/freeze.d.ts +6 -0
  129. package/lib/typescript/src/util/freeze.d.ts.map +1 -0
  130. package/lib/typescript/src/util/logger.d.ts +17 -7
  131. package/lib/typescript/src/util/logger.d.ts.map +1 -1
  132. package/livevoice-sdk-react-native.podspec +29 -4
  133. package/package.json +12 -9
  134. package/src/components/BrandingView.tsx +42 -55
  135. package/src/components/ChannelView.tsx +329 -38
  136. package/src/components/ErrorView.tsx +9 -12
  137. package/src/components/LiveVoiceSpinner.tsx +87 -0
  138. package/src/components/LiveVoiceView.tsx +319 -201
  139. package/src/components/PasswordPrompt.tsx +144 -0
  140. package/src/components/PlayButtonView.tsx +165 -269
  141. package/src/components/ReconnectingView.tsx +28 -81
  142. package/src/icons/branding-ios.png +0 -0
  143. package/src/icons/camera-off-android.png +0 -0
  144. package/src/icons/camera-off-android@2x.png +0 -0
  145. package/src/icons/camera-off-android@3x.png +0 -0
  146. package/src/icons/cpu-android.png +0 -0
  147. package/src/icons/cpu-android@2x.png +0 -0
  148. package/src/icons/cpu-android@3x.png +0 -0
  149. package/src/icons/cpu-ios.png +0 -0
  150. package/src/icons/cpu-ios@2x.png +0 -0
  151. package/src/icons/cpu-ios@3x.png +0 -0
  152. package/src/icons/lock-android.png +0 -0
  153. package/src/icons/lock-android@2x.png +0 -0
  154. package/src/icons/lock-android@3x.png +0 -0
  155. package/src/icons/lock-ios.png +0 -0
  156. package/src/icons/lock-ios@2x.png +0 -0
  157. package/src/icons/lock-ios@3x.png +0 -0
  158. package/src/icons/mic-off-android.png +0 -0
  159. package/src/icons/mic-off-android@2x.png +0 -0
  160. package/src/icons/mic-off-android@3x.png +0 -0
  161. package/src/icons/mic-slash-ios.png +0 -0
  162. package/src/icons/mic-slash-ios@2x.png +0 -0
  163. package/src/icons/mic-slash-ios@3x.png +0 -0
  164. package/src/icons/video-slash-ios.png +0 -0
  165. package/src/icons/video-slash-ios@2x.png +0 -0
  166. package/src/icons/video-slash-ios@3x.png +0 -0
  167. package/src/index.tsx +92 -6
  168. package/src/sdk/Bridge.ts +259 -0
  169. package/src/sdk/Event.ts +187 -0
  170. package/src/sdk/Lifecycle.ts +366 -0
  171. package/src/sdk/LiveVoiceSdk.ts +285 -309
  172. package/src/sdk/LiveVoiceState.ts +230 -60
  173. package/src/sdk/LogEntries.ts +88 -0
  174. package/src/sdk/Models.ts +129 -0
  175. package/src/{util/PlayingState.ts → sdk/NativeSdkVersions.ts} +7 -6
  176. package/src/sdk/Observables.ts +414 -0
  177. package/src/sdk/Passwords.ts +286 -0
  178. package/src/sdk/Playback.ts +30 -0
  179. package/src/specs/NativeLiveVoiceSdk.ts +42 -38
  180. package/src/util/Colors.ts +2 -1
  181. package/src/util/Translations.ts +34 -12
  182. package/src/util/freeze.ts +23 -0
  183. package/src/util/logger.ts +79 -52
  184. package/lib/module/components/AudioOutputSwitchView.js +0 -138
  185. package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
  186. package/lib/module/components/index.js +0 -24
  187. package/lib/module/components/index.js.map +0 -1
  188. package/lib/module/icons/mic.png +0 -0
  189. package/lib/module/util/ForegroundServiceConfig.js +0 -38
  190. package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
  191. package/lib/module/util/PlayingState.js.map +0 -1
  192. package/lib/module/util/UIState.js +0 -4
  193. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts +0 -6
  194. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts.map +0 -1
  195. package/lib/typescript/src/components/index.d.ts +0 -9
  196. package/lib/typescript/src/components/index.d.ts.map +0 -1
  197. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts +0 -19
  198. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts.map +0 -1
  199. package/lib/typescript/src/util/PlayingState.d.ts +0 -7
  200. package/lib/typescript/src/util/PlayingState.d.ts.map +0 -1
  201. package/lib/typescript/src/util/UIState.d.ts +0 -86
  202. package/lib/typescript/src/util/UIState.d.ts.map +0 -1
  203. package/src/components/AudioOutputSwitchView.tsx +0 -146
  204. package/src/components/index.tsx +0 -21
  205. package/src/icons/mic.png +0 -0
  206. package/src/util/ForegroundServiceConfig.tsx +0 -42
  207. package/src/util/UIState.ts +0 -106
@@ -0,0 +1,414 @@
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the LiveVoice SDK project
4
+ //
5
+ // Copyright (c) LiveVoice GmbH
6
+ // Licensed under the MIT license
7
+ //
8
+ // See LICENSE for license information
9
+ //
10
+ // SPDX-License-Identifier: MIT
11
+ //
12
+ //===----------------------------------------------------------------------===//
13
+
14
+ // Every public state of the SDK as `getCurrent…()`, `add…Listener()` and `useLiveVoice…()`. Each
15
+ // has one store: hydrated from the snapshot the bridge's `initialize` returns, kept current by the
16
+ // one `onState` callback with its revisions (Bridge.ts), and changed only when the value changes.
17
+ // Values are frozen, nested objects and arrays too, and keep their identity until they change, as
18
+ // `useSyncExternalStore` requires. Before `initialize()` each store has its initial value.
19
+
20
+ import { useMemo, useSyncExternalStore } from 'react';
21
+
22
+ import { currentEncoded, decodeState, record, type StateName } from './Bridge';
23
+ import {
24
+ decodeEvent,
25
+ decodeLanguages,
26
+ decodeLinkedEvents,
27
+ type LiveVoiceEvent,
28
+ type LiveVoiceLinkedEvent,
29
+ type LiveVoiceRequestableLanguage,
30
+ } from './Event';
31
+ import {
32
+ decodeConnection,
33
+ publicConnection,
34
+ type ConnectionValue,
35
+ type LiveVoiceConnection,
36
+ } from './Lifecycle';
37
+ import {
38
+ type LiveVoiceAudioOutput,
39
+ type LiveVoiceAvailableSubtitle,
40
+ type LiveVoiceFeatures,
41
+ type LiveVoiceSubscription,
42
+ type LiveVoiceSubtitleState,
43
+ } from './Models';
44
+ import { deepFreeze } from '../util/freeze';
45
+ import { logDecodeError, logListenerError } from '../util/logger';
46
+
47
+ type Store<Value, Public> = {
48
+ /** The current value, in public form. */
49
+ get(): Public;
50
+ /** Calls `listener` with the current value at once, then once for every change. */
51
+ add(listener: (value: Public) => void): LiveVoiceSubscription;
52
+ /** The current value in a component, which re-renders on every change. */
53
+ use(): Public;
54
+ /** Sets the value as long as native has delivered none, as before `initialize()`. */
55
+ preset(value: Value): void;
56
+ };
57
+
58
+ /**
59
+ * A store of the state `name`, which is that state's decoder in the bridge. `decode` reads the
60
+ * wire's value and throws for one it cannot read, which is dropped: the last good value stays, the
61
+ * SDK log gets `LV_BRIDGE_DECODE_ERROR`, and the value satisfies no command's revisions.
62
+ * `publish` turns the cached value into the one hosts get; it creates the `Date`s, so every
63
+ * getter call and listener delivery gets its own, and a hook one per update.
64
+ */
65
+ function store<Value, Public = Value>(
66
+ name: StateName,
67
+ decode: (wire: unknown) => Value,
68
+ initial: Value,
69
+ publish: (value: Value) => Public = value => value as unknown as Public,
70
+ ): Store<Value, Public> {
71
+ let value = deepFreeze(initial);
72
+ /** The wire's JSON of the last value native delivered and this store applied. */
73
+ let applied: string | undefined;
74
+ const subscribers = new Set<() => void>();
75
+
76
+ const change = (next: Value) => {
77
+ value = deepFreeze(next);
78
+ for (const subscriber of [...subscribers]) {
79
+ try { subscriber(); }
80
+ catch (error) { logListenerError('LV_LISTENER_ERROR', error); }
81
+ }
82
+ };
83
+ /** Whether the store holds the wire's value, decoded, afterwards. */
84
+ const receive = (wire: unknown): boolean => {
85
+ // The bridge reports only changed values; after a dropped one, the last good one can return.
86
+ const encoded = currentEncoded(name) ?? JSON.stringify(wire ?? null);
87
+ if (encoded === applied) return true;
88
+ let next: Value;
89
+ try { next = decode(wire); }
90
+ catch {
91
+ logDecodeError('state', name);
92
+ return false;
93
+ }
94
+ // The first value from native may equal the initial one: nothing changed, nobody hears.
95
+ const unchanged = applied === undefined && JSON.stringify(next) === JSON.stringify(value);
96
+ applied = encoded;
97
+ if (!unchanged) change(next);
98
+ return true;
99
+ };
100
+ // Also decodes the value the bridge already holds.
101
+ decodeState(name, receive);
102
+
103
+ const subscribe = (onChange: () => void) => {
104
+ subscribers.add(onChange);
105
+ return () => { subscribers.delete(onChange); };
106
+ };
107
+ const snapshot = () => value;
108
+
109
+ return {
110
+ get: () => publish(value),
111
+ add(listener) {
112
+ let active = true;
113
+ const deliver = () => {
114
+ if (!active) return;
115
+ // A listener that throws reaches neither the other listeners nor the SDK.
116
+ try { listener(publish(value)); }
117
+ catch (error) { logListenerError('LV_LISTENER_ERROR', error); }
118
+ };
119
+ subscribers.add(deliver);
120
+ deliver();
121
+ return {
122
+ remove: () => {
123
+ if (!active) return;
124
+ active = false;
125
+ subscribers.delete(deliver);
126
+ },
127
+ };
128
+ },
129
+ use() {
130
+ const current = useSyncExternalStore(subscribe, snapshot, snapshot);
131
+ return useMemo(() => publish(current), [current]);
132
+ },
133
+ preset(next) {
134
+ if (applied !== undefined || JSON.stringify(next) === JSON.stringify(value)) return;
135
+ change(next);
136
+ },
137
+ };
138
+ }
139
+
140
+ // MARK: Decoders of bridge.md 2.5 to 2.8
141
+
142
+ function decodeFeatures(wire: unknown): LiveVoiceFeatures {
143
+ if (!record(wire) || typeof wire.customUiAllowed !== 'boolean' || typeof wire.hideBranding !== 'boolean') {
144
+ throw new TypeError('features');
145
+ }
146
+ return { customUiAllowed: wire.customUiAllowed, hideBranding: wire.hideBranding };
147
+ }
148
+
149
+ function decodeAudioOutput(wire: unknown): LiveVoiceAudioOutput {
150
+ if (wire !== 'system' && wire !== 'speaker') throw new TypeError('audioOutput');
151
+ return wire;
152
+ }
153
+
154
+ function fragments(wire: Record<string, unknown>): string[] {
155
+ if (!Array.isArray(wire.fragments)) throw new TypeError('subtitles');
156
+ return wire.fragments.filter((fragment): fragment is string => typeof fragment === 'string');
157
+ }
158
+
159
+ function decodeSubtitles(wire: unknown): LiveVoiceSubtitleState {
160
+ if (!record(wire)) throw new TypeError('subtitles');
161
+ switch (wire.kind) {
162
+ case 'disabled':
163
+ return { kind: 'disabled' };
164
+ case 'automatic':
165
+ return { kind: 'automatic', fragments: fragments(wire) };
166
+ case 'specific':
167
+ if (typeof wire.channelId !== 'number') throw new TypeError('subtitles');
168
+ return { kind: 'specific', channelId: wire.channelId, fragments: fragments(wire) };
169
+ default:
170
+ throw new TypeError('subtitles');
171
+ }
172
+ }
173
+
174
+ /** `null` until the joined event's list arrived, `[]` when it has none (bridge.md 2.8). */
175
+ function decodeAvailableSubtitles(wire: unknown): LiveVoiceAvailableSubtitle[] | null {
176
+ if (wire === null) return null;
177
+ if (!Array.isArray(wire)) throw new TypeError('availableSubtitles');
178
+ return wire.filter(record).flatMap(subtitle =>
179
+ typeof subtitle.channelId === 'number' && typeof subtitle.name === 'string'
180
+ ? [{
181
+ channelId: subtitle.channelId,
182
+ name: subtitle.name,
183
+ language: typeof subtitle.language === 'string' ? subtitle.language : null,
184
+ parentChannelId: typeof subtitle.parentChannelId === 'number' ? subtitle.parentChannelId : null,
185
+ }]
186
+ : []);
187
+ }
188
+
189
+ // MARK: The stores
190
+
191
+ const connection = store<ConnectionValue, LiveVoiceConnection>(
192
+ 'connection', decodeConnection, { status: 'idle' }, publicConnection,
193
+ );
194
+ const subtitles = store<LiveVoiceSubtitleState>('subtitles', decodeSubtitles, { kind: 'disabled' });
195
+ const availableSubtitles = store('availableSubtitles', decodeAvailableSubtitles, null);
196
+ const audioOutput = store<LiveVoiceAudioOutput>('audioOutput', decodeAudioOutput, 'system');
197
+ const features = store('features', decodeFeatures, { customUiAllowed: false, hideBranding: false });
198
+ const currentEvent = store('currentEvent', decodeEvent, null);
199
+ const requestableLanguages = store('requestableLanguages', decodeLanguages, null);
200
+ const linkedEvents = store('linkedEvents', decodeLinkedEvents, null);
201
+
202
+ // MARK: Connection
203
+
204
+ /**
205
+ * The connection to the event: `idle` before `initialize()` and while nothing is joined or
206
+ * joining.
207
+ */
208
+ export function getCurrentConnection(): LiveVoiceConnection {
209
+ return connection.get();
210
+ }
211
+
212
+ /**
213
+ * Calls `listener` with the current connection at once, then on every change. A state change is a
214
+ * display update; await `joinEvent()` to confirm a particular join.
215
+ *
216
+ * @param listener - Called with the current value at once, then with every change.
217
+ * @returns A subscription; its `remove()` ends the observation.
218
+ */
219
+ export function addConnectionListener(listener: (connection: LiveVoiceConnection) => void): LiveVoiceSubscription {
220
+ return connection.add(listener);
221
+ }
222
+
223
+ /** The connection in a component. */
224
+ export function useLiveVoiceConnection(): LiveVoiceConnection {
225
+ return connection.use();
226
+ }
227
+
228
+ // MARK: Subtitles
229
+
230
+ /**
231
+ * Which subtitles the SDK receives, with the fragments so far. It is `disabled` after a join, a
232
+ * leave and `stopSubtitles`. The value follows a call shortly, also an awaited `joinEvent`: right
233
+ * after the promise resolves it can still show the previous value, so observe it with
234
+ * {@link addSubtitlesListener} or {@link useLiveVoiceSubtitles}.
235
+ */
236
+ export function getCurrentSubtitles(): LiveVoiceSubtitleState {
237
+ return subtitles.get();
238
+ }
239
+
240
+ /**
241
+ * Calls `listener` with the current subtitles at once, then on every change.
242
+ *
243
+ * @param listener - Called with the current value at once, then with every change.
244
+ * @returns A subscription; its `remove()` ends the observation.
245
+ */
246
+ export function addSubtitlesListener(listener: (subtitles: LiveVoiceSubtitleState) => void): LiveVoiceSubscription {
247
+ return subtitles.add(listener);
248
+ }
249
+
250
+ /** The subtitles in a component. */
251
+ export function useLiveVoiceSubtitles(): LiveVoiceSubtitleState {
252
+ return subtitles.use();
253
+ }
254
+
255
+ /**
256
+ * The subtitles that can be started: `null` while no event is joined, a list once `joinEvent` has
257
+ * resolved, `[]` when the event has none. The list stays during a reconnect.
258
+ */
259
+ export function getCurrentAvailableSubtitles(): LiveVoiceAvailableSubtitle[] | null {
260
+ return availableSubtitles.get();
261
+ }
262
+
263
+ /**
264
+ * Calls `listener` with the current available subtitles at once, then on every change.
265
+ *
266
+ * @param listener - Called with the current value at once, then with every change.
267
+ * @returns A subscription; its `remove()` ends the observation.
268
+ */
269
+ export function addAvailableSubtitlesListener(
270
+ listener: (availableSubtitles: LiveVoiceAvailableSubtitle[] | null) => void,
271
+ ): LiveVoiceSubscription {
272
+ return availableSubtitles.add(listener);
273
+ }
274
+
275
+ /** The available subtitles in a component. */
276
+ export function useLiveVoiceAvailableSubtitles(): LiveVoiceAvailableSubtitle[] | null {
277
+ return availableSubtitles.use();
278
+ }
279
+
280
+ // MARK: Audio output
281
+
282
+ /**
283
+ * The audio output preference. Before `initialize()` it is `'system'` until `setAudioOutput()`
284
+ * sets a preference, then that preference.
285
+ */
286
+ export function getCurrentAudioOutput(): LiveVoiceAudioOutput {
287
+ return audioOutput.get();
288
+ }
289
+
290
+ /**
291
+ * Calls `listener` with the current audio output at once, then on every change.
292
+ *
293
+ * @param listener - Called with the current value at once, then with every change.
294
+ * @returns A subscription; its `remove()` ends the observation.
295
+ */
296
+ export function addAudioOutputListener(listener: (audioOutput: LiveVoiceAudioOutput) => void): LiveVoiceSubscription {
297
+ return audioOutput.add(listener);
298
+ }
299
+
300
+ /** The audio output in a component. */
301
+ export function useLiveVoiceAudioOutput(): LiveVoiceAudioOutput {
302
+ return audioOutput.use();
303
+ }
304
+
305
+ /** Internal: `setAudioOutput()` before native reported a value, which then replaces it. */
306
+ export function presetAudioOutput(output: LiveVoiceAudioOutput): void {
307
+ audioOutput.preset(output);
308
+ }
309
+
310
+ // MARK: Features
311
+
312
+ /**
313
+ * What the API key of the joined event permits; `false` for both before the first join, after a
314
+ * leave and after a failure. Right after an awaited `joinEvent` it can still show the previous
315
+ * value; observe it with {@link addFeaturesListener} or {@link useLiveVoiceFeatures}.
316
+ */
317
+ export function getCurrentFeatures(): LiveVoiceFeatures {
318
+ return features.get();
319
+ }
320
+
321
+ /**
322
+ * Calls `listener` with the current features at once, then whenever they change.
323
+ *
324
+ * @param listener - Called with the current value at once, then with every change.
325
+ * @returns A subscription; its `remove()` ends the observation.
326
+ */
327
+ export function addFeaturesListener(listener: (features: LiveVoiceFeatures) => void): LiveVoiceSubscription {
328
+ return features.add(listener);
329
+ }
330
+
331
+ /** The features in a component. */
332
+ export function useLiveVoiceFeatures(): LiveVoiceFeatures {
333
+ return features.use();
334
+ }
335
+
336
+ // MARK: Current event
337
+
338
+ /**
339
+ * The joined event: `null` whenever no event is ready, set once the join is ready, kept during a
340
+ * reconnect, and following a linked switch. Its brand colours come with every API key.
341
+ */
342
+ export function getCurrentEvent(): LiveVoiceEvent | null {
343
+ return currentEvent.get();
344
+ }
345
+
346
+ /**
347
+ * Calls `listener` with the current event at once, then on every change.
348
+ *
349
+ * @param listener - Called with the current value at once, then with every change.
350
+ * @returns A subscription; its `remove()` ends the observation.
351
+ */
352
+ export function addCurrentEventListener(listener: (event: LiveVoiceEvent | null) => void): LiveVoiceSubscription {
353
+ return currentEvent.add(listener);
354
+ }
355
+
356
+ /** The current event in a component. */
357
+ export function useLiveVoiceCurrentEvent(): LiveVoiceEvent | null {
358
+ return currentEvent.use();
359
+ }
360
+
361
+ // MARK: Requestable languages
362
+
363
+ /**
364
+ * The languages listeners can request: `null` until the join has authenticated, for a key without
365
+ * the custom-UI permission, when the event disables requests, and whenever no event is ready; kept
366
+ * during a reconnect.
367
+ */
368
+ export function getCurrentRequestableLanguages(): LiveVoiceRequestableLanguage[] | null {
369
+ return requestableLanguages.get();
370
+ }
371
+
372
+ /**
373
+ * Calls `listener` with the current languages at once, then on every change.
374
+ *
375
+ * @param listener - Called with the current value at once, then with every change.
376
+ * @returns A subscription; its `remove()` ends the observation.
377
+ */
378
+ export function addRequestableLanguagesListener(
379
+ listener: (languages: LiveVoiceRequestableLanguage[] | null) => void,
380
+ ): LiveVoiceSubscription {
381
+ return requestableLanguages.add(listener);
382
+ }
383
+
384
+ /** The requestable languages in a component. */
385
+ export function useLiveVoiceRequestableLanguages(): LiveVoiceRequestableLanguage[] | null {
386
+ return requestableLanguages.use();
387
+ }
388
+
389
+ // MARK: Linked events
390
+
391
+ /**
392
+ * The events linked to the joined one: `null` until the join has authenticated, for a key without
393
+ * the custom-UI permission, and whenever no event is ready; kept during a reconnect.
394
+ */
395
+ export function getCurrentLinkedEvents(): LiveVoiceLinkedEvent[] | null {
396
+ return linkedEvents.get();
397
+ }
398
+
399
+ /**
400
+ * Calls `listener` with the current linked events at once, then on every change.
401
+ *
402
+ * @param listener - Called with the current value at once, then with every change.
403
+ * @returns A subscription; its `remove()` ends the observation.
404
+ */
405
+ export function addLinkedEventsListener(
406
+ listener: (linkedEvents: LiveVoiceLinkedEvent[] | null) => void,
407
+ ): LiveVoiceSubscription {
408
+ return linkedEvents.add(listener);
409
+ }
410
+
411
+ /** The linked events in a component. */
412
+ export function useLiveVoiceLinkedEvents(): LiveVoiceLinkedEvent[] | null {
413
+ return linkedEvents.use();
414
+ }
@@ -0,0 +1,286 @@
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the LiveVoice SDK project
4
+ //
5
+ // Copyright (c) LiveVoice GmbH
6
+ // Licensed under the MIT license
7
+ //
8
+ // See LICENSE for license information
9
+ //
10
+ // SPDX-License-Identifier: MIT
11
+ //
12
+ //===----------------------------------------------------------------------===//
13
+
14
+ // The password round trips of the bridge (bridge.md 4.2, 4.3). Native looks up its memory and the
15
+ // host's store first; it asks here only for a password neither had. This module answers from the
16
+ // host's provider when one is set, otherwise from the built-in prompt of the most recently shown
17
+ // LiveVoiceView, otherwise it gives up. It never logs a password or a host exception's text.
18
+
19
+ import Native from '../specs/NativeLiveVoiceSdk';
20
+ import { writeLogEntry } from '../util/logger';
21
+
22
+ /** What a password is for, keyed by join code, since an event's id is unknown before joining. */
23
+ export type LiveVoicePasswordSubject =
24
+ | { kind: 'channel'; joinCode: string; channelId: number }
25
+ | { kind: 'event'; joinCode: string };
26
+
27
+ /** A password the SDK needs. `isRetry` is `true` after a rejected password in the same operation. */
28
+ export type LiveVoicePasswordRequest = {
29
+ subject: LiveVoicePasswordSubject;
30
+ isRetry: boolean;
31
+ };
32
+
33
+ /**
34
+ * Supplies a password, or `null` to give up. Giving up fails the call with `password_required`
35
+ * (for a channel `channel_password_required`), or after a rejected password with `wrong_password`
36
+ * (`channel_wrong_password`). A rejection or a thrown error gives up as well.
37
+ */
38
+ export type LiveVoicePasswordProvider = (request: LiveVoicePasswordRequest) => Promise<string | null>;
39
+
40
+ /**
41
+ * Keeps passwords between launches. The SDK loads one before asking for it, saves it once the
42
+ * server accepted it and removes it once the server rejected it. Calls for one subject complete in
43
+ * the order they were made. A failure is a cache miss; it never fails an operation.
44
+ */
45
+ export type LiveVoicePasswordStore = {
46
+ load(subject: LiveVoicePasswordSubject): Promise<string | null>;
47
+ save(subject: LiveVoicePasswordSubject, password: string): Promise<void>;
48
+ remove(subject: LiveVoicePasswordSubject): Promise<void>;
49
+ };
50
+
51
+ /** A request waiting for the built-in prompt. `name` is its title when known. */
52
+ export type PromptRequest = {
53
+ requestId: string;
54
+ subject: LiveVoicePasswordSubject;
55
+ isRetry: boolean;
56
+ name: string | null;
57
+ };
58
+
59
+ let provider: LiveVoicePasswordProvider | null = null;
60
+ let store: LiveVoicePasswordStore | null = null;
61
+ let bridgeInitialized = false;
62
+
63
+ /** Requests already taken or cancelled: a snapshot delivering one again never reopens it. */
64
+ const seen = new Set<string>();
65
+ const cancelled = new Set<string>();
66
+ /** Requests for the built-in prompt, oldest first; the first one is shown. */
67
+ const queue: PromptRequest[] = [];
68
+
69
+ /** Views that can present the prompt, in the order they were shown; the last one presents. */
70
+ const presenters: number[] = [];
71
+ let nextPresenter = 1;
72
+ const promptListeners = new Set<() => void>();
73
+
74
+ /**
75
+ * Sets the provider every later password request asks, or `null` for the built-in prompt. It can
76
+ * be set before `initialize()`. The built-in prompt needs a `LiveVoiceView` on screen: without a
77
+ * provider and without a view, a request gives up and its call fails with the password error.
78
+ */
79
+ export function setPasswordProvider(newProvider: LiveVoicePasswordProvider | null): void {
80
+ provider = typeof newProvider === 'function' ? newProvider : null;
81
+ }
82
+
83
+ /**
84
+ * Sets the store the SDK loads passwords from and saves them to, or `null` for none. It can be set
85
+ * before `initialize()`.
86
+ */
87
+ export function setPasswordStore(newStore: LiveVoicePasswordStore | null): void {
88
+ store = newStore != null && typeof newStore === 'object' ? newStore : null;
89
+ if (bridgeInitialized) Native.setPasswordStore(JSON.stringify({ isSet: store !== null }));
90
+ }
91
+
92
+ /** Whether a store is set, for `initialize`'s configuration. */
93
+ export function hasPasswordStore(): boolean {
94
+ return store !== null;
95
+ }
96
+
97
+ /** `initialize` delivered its configuration; later store changes go to native at once. */
98
+ export function bridgeDidInitialize(): void {
99
+ bridgeInitialized = true;
100
+ }
101
+
102
+ /** `onPasswordRequest` and the snapshot's waiting requests. Each id is answered once. */
103
+ export function receivePasswordRequest(wire: unknown): void {
104
+ const request = promptRequest(wire);
105
+ if (request === undefined || seen.has(request.requestId) || cancelled.has(request.requestId)) return;
106
+ seen.add(request.requestId);
107
+
108
+ const host = provider;
109
+ if (host !== null) {
110
+ const publicRequest = Object.freeze({ subject: Object.freeze({ ...request.subject }), isRetry: request.isRetry });
111
+ let answer: Promise<string | null>;
112
+ try { answer = Promise.resolve(host(publicRequest)); }
113
+ catch { answerPasswordRequest(request.requestId, null, true); return; }
114
+ answer.then(
115
+ password => answerPasswordRequest(request.requestId, typeof password === 'string' ? password : null),
116
+ () => answerPasswordRequest(request.requestId, null, true),
117
+ );
118
+ return;
119
+ }
120
+
121
+ if (presenters.length === 0) {
122
+ // No view on screen to ask: give up with the password error.
123
+ logPrompt(request, 'noView');
124
+ answerPasswordRequest(request.requestId, null);
125
+ return;
126
+ }
127
+ queue.push(request);
128
+ notifyPrompt();
129
+ }
130
+
131
+ /** `onPasswordRequestCancelled`: the operation ended without the password. */
132
+ export function receivePasswordRequestCancellation(wire: unknown): void {
133
+ const requestId = record(wire) && typeof wire.requestId === 'string' ? wire.requestId : undefined;
134
+ if (requestId === undefined) return;
135
+ cancelled.add(requestId);
136
+ const index = queue.findIndex(request => request.requestId === requestId);
137
+ if (index >= 0) {
138
+ const [dismissed] = queue.splice(index, 1);
139
+ logPrompt(dismissed!, 'dismissed');
140
+ notifyPrompt();
141
+ }
142
+ }
143
+
144
+ /** `onPasswordStoreRequest`: calls the host's store and answers once its call has finished. */
145
+ export function receivePasswordStoreRequest(wire: unknown): void {
146
+ if (!record(wire) || typeof wire.requestId !== 'string') return;
147
+ const requestId = wire.requestId;
148
+ const subject = passwordSubject(wire.subject);
149
+ const host = store;
150
+ if (subject === undefined || host === null) {
151
+ answerStoreRequest(requestId, false);
152
+ return;
153
+ }
154
+ const frozenSubject = Object.freeze(subject);
155
+ let call: Promise<unknown>;
156
+ try {
157
+ switch (wire.operation) {
158
+ case 'load':
159
+ call = Promise.resolve(host.load(frozenSubject)).then(password => {
160
+ answerStoreRequest(requestId, true, typeof password === 'string' ? password : null);
161
+ });
162
+ break;
163
+ case 'save':
164
+ if (typeof wire.password !== 'string') { answerStoreRequest(requestId, false); return; }
165
+ call = Promise.resolve(host.save(frozenSubject, wire.password)).then(() => answerStoreRequest(requestId, true));
166
+ break;
167
+ case 'remove':
168
+ call = Promise.resolve(host.remove(frozenSubject)).then(() => answerStoreRequest(requestId, true));
169
+ break;
170
+ default:
171
+ answerStoreRequest(requestId, false);
172
+ return;
173
+ }
174
+ } catch {
175
+ answerStoreRequest(requestId, false);
176
+ return;
177
+ }
178
+ // Native logs a failure without credentials; a failed load is a cache miss.
179
+ call.catch(() => answerStoreRequest(requestId, false));
180
+ }
181
+
182
+ // MARK: The built-in prompt
183
+
184
+ /** A view that can present the prompt. The most recently registered one still mounted presents. */
185
+ export function registerPresenter(): number {
186
+ const id = nextPresenter++;
187
+ presenters.push(id);
188
+ notifyPrompt();
189
+ return id;
190
+ }
191
+
192
+ /**
193
+ * The view went away. If it was presenting, its request gives up with the password error; with no
194
+ * view left, every waiting request does.
195
+ */
196
+ export function unregisterPresenter(id: number): void {
197
+ const index = presenters.indexOf(id);
198
+ if (index < 0) return;
199
+ const wasPresenting = index === presenters.length - 1;
200
+ presenters.splice(index, 1);
201
+ if (wasPresenting && queue.length > 0) {
202
+ const shown = queue.shift()!;
203
+ logPrompt(shown, 'dismissed');
204
+ answerPasswordRequest(shown.requestId, null);
205
+ }
206
+ if (presenters.length === 0) {
207
+ for (const request of queue.splice(0)) {
208
+ logPrompt(request, 'noView');
209
+ answerPasswordRequest(request.requestId, null);
210
+ }
211
+ }
212
+ notifyPrompt();
213
+ }
214
+
215
+ /** The request `presenter` shows now, or `null`. */
216
+ export function promptFor(presenter: number): PromptRequest | null {
217
+ if (presenters[presenters.length - 1] !== presenter) return null;
218
+ return queue[0] ?? null;
219
+ }
220
+
221
+ /** Calls `listener` whenever the shown prompt or its presenter changes. */
222
+ export function observePrompt(listener: () => void): () => void {
223
+ promptListeners.add(listener);
224
+ return () => { promptListeners.delete(listener); };
225
+ }
226
+
227
+ /** The prompt's answer: a password, or `null` for Cancel. */
228
+ export function answerPrompt(requestId: string, password: string | null): void {
229
+ const index = queue.findIndex(request => request.requestId === requestId);
230
+ if (index < 0) return;
231
+ const [answered] = queue.splice(index, 1);
232
+ logPrompt(answered!, password === null ? 'cancelled' : 'entered');
233
+ answerPasswordRequest(requestId, password);
234
+ notifyPrompt();
235
+ }
236
+
237
+ /** How the built-in prompt ended (bridge.md 3.6); never with the password. */
238
+ function logPrompt(request: PromptRequest, result: 'entered' | 'cancelled' | 'dismissed' | 'noView'): void {
239
+ writeLogEntry('LV_PASSWORD_PROMPT', 'info', {
240
+ subject: request.subject.kind,
241
+ result,
242
+ isRetry: String(request.isRetry),
243
+ });
244
+ }
245
+
246
+ function notifyPrompt(): void {
247
+ for (const listener of [...promptListeners]) {
248
+ try { listener(); }
249
+ catch { /* A view's update must not stop the others. */ }
250
+ }
251
+ }
252
+
253
+ // MARK: Wire
254
+
255
+ function answerPasswordRequest(requestId: string, password: string | null, failed = false): void {
256
+ Native.answerPasswordRequest(JSON.stringify(failed ? { requestId, password: null, failed: true } : { requestId, password }));
257
+ }
258
+
259
+ function answerStoreRequest(requestId: string, ok: boolean, password?: string | null): void {
260
+ Native.answerPasswordStoreRequest(JSON.stringify(ok && password !== undefined ? { requestId, ok, password } : { requestId, ok }));
261
+ }
262
+
263
+ function promptRequest(wire: unknown): PromptRequest | undefined {
264
+ if (!record(wire) || typeof wire.requestId !== 'string') return undefined;
265
+ const subject = passwordSubject(wire.subject);
266
+ if (subject === undefined) return undefined;
267
+ return {
268
+ requestId: wire.requestId,
269
+ subject,
270
+ isRetry: wire.isRetry === true,
271
+ name: typeof wire.name === 'string' && wire.name !== '' ? wire.name : null,
272
+ };
273
+ }
274
+
275
+ function passwordSubject(wire: unknown): LiveVoicePasswordSubject | undefined {
276
+ if (!record(wire) || typeof wire.joinCode !== 'string') return undefined;
277
+ if (wire.kind === 'event') return { kind: 'event', joinCode: wire.joinCode };
278
+ if (wire.kind === 'channel' && typeof wire.channelId === 'number') {
279
+ return { kind: 'channel', joinCode: wire.joinCode, channelId: wire.channelId };
280
+ }
281
+ return undefined;
282
+ }
283
+
284
+ function record(value: unknown): value is Record<string, unknown> {
285
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
286
+ }