livevoice-sdk-react-native 2.0.0 → 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.
- package/README.md +158 -7
- package/android/build.gradle +18 -6
- package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +121 -221
- package/android/src/main/java/io/livevoice/react/__SDK.kt +17 -3
- package/ios/LiveVoiceRNBridge.swift +362 -0
- package/ios/LiveVoiceSdkReactNative.mm +247 -145
- package/ios/__SDK.h +16 -2
- package/ios/livevoice_spm_backport.rb +202 -0
- package/lib/module/components/BrandingView.js +71 -50
- package/lib/module/components/BrandingView.js.map +1 -1
- package/lib/module/components/ChannelView.js +368 -54
- package/lib/module/components/ChannelView.js.map +1 -1
- package/lib/module/components/ErrorView.js +17 -16
- package/lib/module/components/ErrorView.js.map +1 -1
- package/lib/module/components/LiveVoiceSpinner.js +81 -0
- package/lib/module/components/LiveVoiceSpinner.js.map +1 -0
- package/lib/module/components/LiveVoiceView.js +351 -104
- package/lib/module/components/LiveVoiceView.js.map +1 -1
- package/lib/module/components/PasswordPrompt.js +165 -0
- package/lib/module/components/PasswordPrompt.js.map +1 -0
- package/lib/module/components/PlayButtonView.js +192 -254
- package/lib/module/components/PlayButtonView.js.map +1 -1
- package/lib/module/components/ReconnectingView.js +43 -62
- package/lib/module/components/ReconnectingView.js.map +1 -1
- package/lib/module/components/WarningView.js +12 -10
- package/lib/module/components/WarningView.js.map +1 -1
- package/lib/module/icons/branding-ios.png +0 -0
- package/lib/module/icons/camera-off-android.png +0 -0
- package/lib/module/icons/camera-off-android@2x.png +0 -0
- package/lib/module/icons/camera-off-android@3x.png +0 -0
- package/lib/module/icons/cpu-android.png +0 -0
- package/lib/module/icons/cpu-android@2x.png +0 -0
- package/lib/module/icons/cpu-android@3x.png +0 -0
- package/lib/module/icons/cpu-ios.png +0 -0
- package/lib/module/icons/cpu-ios@2x.png +0 -0
- package/lib/module/icons/cpu-ios@3x.png +0 -0
- package/lib/module/icons/lock-android.png +0 -0
- package/lib/module/icons/lock-android@2x.png +0 -0
- package/lib/module/icons/lock-android@3x.png +0 -0
- package/lib/module/icons/lock-ios.png +0 -0
- package/lib/module/icons/lock-ios@2x.png +0 -0
- package/lib/module/icons/lock-ios@3x.png +0 -0
- package/lib/module/icons/mic-off-android.png +0 -0
- package/lib/module/icons/mic-off-android@2x.png +0 -0
- package/lib/module/icons/mic-off-android@3x.png +0 -0
- package/lib/module/icons/mic-slash-ios.png +0 -0
- package/lib/module/icons/mic-slash-ios@2x.png +0 -0
- package/lib/module/icons/mic-slash-ios@3x.png +0 -0
- package/lib/module/icons/video-slash-ios.png +0 -0
- package/lib/module/icons/video-slash-ios@2x.png +0 -0
- package/lib/module/icons/video-slash-ios@3x.png +0 -0
- package/lib/module/index.js +24 -225
- package/lib/module/index.js.map +1 -1
- package/lib/module/sdk/Bridge.js +236 -0
- package/lib/module/sdk/Bridge.js.map +1 -0
- package/lib/module/sdk/Event.js +160 -0
- package/lib/module/sdk/Event.js.map +1 -0
- package/lib/module/sdk/Lifecycle.js +331 -0
- package/lib/module/sdk/Lifecycle.js.map +1 -0
- package/lib/module/sdk/LiveVoiceSdk.js +365 -0
- package/lib/module/sdk/LiveVoiceSdk.js.map +1 -0
- package/lib/module/sdk/LiveVoiceState.js +234 -0
- package/lib/module/sdk/LiveVoiceState.js.map +1 -0
- package/lib/module/sdk/LogEntries.js +99 -0
- package/lib/module/sdk/LogEntries.js.map +1 -0
- package/lib/module/sdk/Models.js +2 -0
- package/lib/module/{util/UIState.js.map → sdk/Models.js.map} +1 -1
- package/lib/module/sdk/NativeSdkVersions.js +24 -0
- package/lib/module/sdk/NativeSdkVersions.js.map +1 -0
- package/lib/module/sdk/Observables.js +391 -0
- package/lib/module/sdk/Observables.js.map +1 -0
- package/lib/module/sdk/Passwords.js +298 -0
- package/lib/module/sdk/Passwords.js.map +1 -0
- package/lib/module/sdk/Playback.js +35 -0
- package/lib/module/sdk/Playback.js.map +1 -0
- package/lib/module/specs/NativeLiveVoiceSdk.js +12 -10
- package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
- package/lib/module/util/Colors.js +14 -11
- package/lib/module/util/Colors.js.map +1 -1
- package/lib/module/util/Translations.js +46 -21
- package/lib/module/util/Translations.js.map +1 -1
- package/lib/module/util/freeze.js +26 -0
- package/lib/module/util/freeze.js.map +1 -0
- package/lib/module/util/logger.js +114 -0
- package/lib/module/util/logger.js.map +1 -0
- package/lib/typescript/src/components/BrandingView.d.ts +4 -0
- package/lib/typescript/src/components/BrandingView.d.ts.map +1 -1
- package/lib/typescript/src/components/ChannelView.d.ts +29 -3
- package/lib/typescript/src/components/ChannelView.d.ts.map +1 -1
- package/lib/typescript/src/components/ErrorView.d.ts +5 -3
- package/lib/typescript/src/components/ErrorView.d.ts.map +1 -1
- package/lib/typescript/src/components/LiveVoiceSpinner.d.ts +16 -0
- package/lib/typescript/src/components/LiveVoiceSpinner.d.ts.map +1 -0
- package/lib/typescript/src/components/LiveVoiceView.d.ts +85 -3
- package/lib/typescript/src/components/LiveVoiceView.d.ts.map +1 -1
- package/lib/typescript/src/components/PasswordPrompt.d.ts +13 -0
- package/lib/typescript/src/components/PasswordPrompt.d.ts.map +1 -0
- package/lib/typescript/src/components/PlayButtonView.d.ts +12 -329
- package/lib/typescript/src/components/PlayButtonView.d.ts.map +1 -1
- package/lib/typescript/src/components/ReconnectingView.d.ts +6 -3
- package/lib/typescript/src/components/ReconnectingView.d.ts.map +1 -1
- package/lib/typescript/src/components/WarningView.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +15 -144
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/sdk/Bridge.d.ts +36 -0
- package/lib/typescript/src/sdk/Bridge.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Event.d.ts +97 -0
- package/lib/typescript/src/sdk/Event.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Lifecycle.d.ts +191 -0
- package/lib/typescript/src/sdk/Lifecycle.d.ts.map +1 -0
- package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts +215 -0
- package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -0
- package/lib/typescript/src/sdk/LiveVoiceState.d.ts +80 -0
- package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -0
- package/lib/typescript/src/sdk/LogEntries.d.ts +28 -0
- package/lib/typescript/src/sdk/LogEntries.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Models.d.ts +114 -0
- package/lib/typescript/src/sdk/Models.d.ts.map +1 -0
- package/lib/typescript/src/sdk/NativeSdkVersions.d.ts +6 -0
- package/lib/typescript/src/sdk/NativeSdkVersions.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Observables.d.ts +123 -0
- package/lib/typescript/src/sdk/Observables.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Passwords.d.ts +72 -0
- package/lib/typescript/src/sdk/Passwords.d.ts.map +1 -0
- package/lib/typescript/src/sdk/Playback.d.ts +9 -0
- package/lib/typescript/src/sdk/Playback.d.ts.map +1 -0
- package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts +35 -29
- package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts.map +1 -1
- package/lib/typescript/src/util/Colors.d.ts.map +1 -1
- package/lib/typescript/src/util/Translations.d.ts +30 -8
- package/lib/typescript/src/util/Translations.d.ts.map +1 -1
- package/lib/typescript/src/util/freeze.d.ts +6 -0
- package/lib/typescript/src/util/freeze.d.ts.map +1 -0
- package/lib/typescript/src/util/logger.d.ts +33 -0
- package/lib/typescript/src/util/logger.d.ts.map +1 -0
- package/livevoice-sdk-react-native.podspec +41 -16
- package/package.json +26 -14
- package/src/components/BrandingView.tsx +55 -66
- package/src/components/ChannelView.tsx +342 -48
- package/src/components/ErrorView.tsx +21 -22
- package/src/components/LiveVoiceSpinner.tsx +87 -0
- package/src/components/LiveVoiceView.tsx +360 -151
- package/src/components/PasswordPrompt.tsx +144 -0
- package/src/components/PlayButtonView.tsx +177 -279
- package/src/components/ReconnectingView.tsx +40 -86
- package/src/components/WarningView.tsx +12 -10
- package/src/icons/branding-ios.png +0 -0
- package/src/icons/camera-off-android.png +0 -0
- package/src/icons/camera-off-android@2x.png +0 -0
- package/src/icons/camera-off-android@3x.png +0 -0
- package/src/icons/cpu-android.png +0 -0
- package/src/icons/cpu-android@2x.png +0 -0
- package/src/icons/cpu-android@3x.png +0 -0
- package/src/icons/cpu-ios.png +0 -0
- package/src/icons/cpu-ios@2x.png +0 -0
- package/src/icons/cpu-ios@3x.png +0 -0
- package/src/icons/lock-android.png +0 -0
- package/src/icons/lock-android@2x.png +0 -0
- package/src/icons/lock-android@3x.png +0 -0
- package/src/icons/lock-ios.png +0 -0
- package/src/icons/lock-ios@2x.png +0 -0
- package/src/icons/lock-ios@3x.png +0 -0
- package/src/icons/mic-off-android.png +0 -0
- package/src/icons/mic-off-android@2x.png +0 -0
- package/src/icons/mic-off-android@3x.png +0 -0
- package/src/icons/mic-slash-ios.png +0 -0
- package/src/icons/mic-slash-ios@2x.png +0 -0
- package/src/icons/mic-slash-ios@3x.png +0 -0
- package/src/icons/video-slash-ios.png +0 -0
- package/src/icons/video-slash-ios@2x.png +0 -0
- package/src/icons/video-slash-ios@3x.png +0 -0
- package/src/index.tsx +105 -252
- package/src/sdk/Bridge.ts +259 -0
- package/src/sdk/Event.ts +187 -0
- package/src/sdk/Lifecycle.ts +366 -0
- package/src/sdk/LiveVoiceSdk.ts +384 -0
- package/src/sdk/LiveVoiceState.ts +259 -0
- package/src/sdk/LogEntries.ts +88 -0
- package/src/sdk/Models.ts +129 -0
- package/src/sdk/NativeSdkVersions.ts +21 -0
- package/src/sdk/Observables.ts +414 -0
- package/src/sdk/Passwords.ts +286 -0
- package/src/sdk/Playback.ts +30 -0
- package/src/specs/NativeLiveVoiceSdk.ts +56 -46
- package/src/util/Colors.ts +14 -11
- package/src/util/Translations.ts +46 -22
- package/src/util/freeze.ts +23 -0
- package/src/util/logger.ts +119 -0
- package/android/app/build/generated/source/codegen/java/com/facebook/fbreact/specs/NativeLiveVoiceSdkSpec.java +0 -186
- package/android/app/build/generated/source/codegen/jni/CMakeLists.txt +0 -28
- package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec-generated.cpp +0 -235
- package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec.h +0 -31
- package/android/app/build/generated/source/codegen/jni/react/renderer/components/RNLiveVoiceSpec/RNLiveVoiceSpecJSI.h +0 -341
- package/android/src/main/java/io/livevoice/react/Mappings.kt +0 -85
- package/android/src/main/java/io/livevoice/react/NativeModels.kt +0 -79
- package/lib/module/components/AudioOutputSwitchView.js +0 -138
- package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
- package/lib/module/components/index.js +0 -22
- package/lib/module/components/index.js.map +0 -1
- package/lib/module/icons/mic.png +0 -0
- package/lib/module/util/ForegroundServiceConfig.js +0 -28
- package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
- package/lib/module/util/PlayingState.js +0 -21
- package/lib/module/util/PlayingState.js.map +0 -1
- package/lib/module/util/UIState.js +0 -4
- package/lib/typescript/src/components/AudioOutputSwitchView.d.ts +0 -6
- package/lib/typescript/src/components/AudioOutputSwitchView.d.ts.map +0 -1
- package/lib/typescript/src/components/index.d.ts +0 -9
- package/lib/typescript/src/components/index.d.ts.map +0 -1
- package/lib/typescript/src/util/ForegroundServiceConfig.d.ts +0 -12
- package/lib/typescript/src/util/ForegroundServiceConfig.d.ts.map +0 -1
- package/lib/typescript/src/util/PlayingState.d.ts +0 -7
- package/lib/typescript/src/util/PlayingState.d.ts.map +0 -1
- package/lib/typescript/src/util/UIState.d.ts +0 -66
- package/lib/typescript/src/util/UIState.d.ts.map +0 -1
- package/src/components/AudioOutputSwitchView.tsx +0 -146
- package/src/components/index.tsx +0 -19
- package/src/icons/mic.png +0 -0
- package/src/util/ForegroundServiceConfig.tsx +0 -32
- package/src/util/PlayingState.ts +0 -18
- package/src/util/UIState.ts +0 -89
|
@@ -0,0 +1,384 @@
|
|
|
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
|
+
import { Image, Platform, type ImageSourcePropType } from 'react-native';
|
|
15
|
+
import LiveVoiceSdkReactNative from '../specs/NativeLiveVoiceSdk';
|
|
16
|
+
import { awaitRevisions, initializeBridge, isInitialized, record, runtimeId } from './Bridge';
|
|
17
|
+
import { nativeSdkVersions } from './NativeSdkVersions';
|
|
18
|
+
import {
|
|
19
|
+
invoke,
|
|
20
|
+
join,
|
|
21
|
+
normalizeError,
|
|
22
|
+
usageError,
|
|
23
|
+
channelId as validateChannelId,
|
|
24
|
+
type LiveVoiceJoinRequest,
|
|
25
|
+
type LiveVoiceJoinedEvent,
|
|
26
|
+
} from './Lifecycle';
|
|
27
|
+
import { type LiveVoiceAudioOutput } from './Models';
|
|
28
|
+
import { presetAudioOutput } from './Observables';
|
|
29
|
+
import { LiveVoiceLogEvent, logInfo } from '../util/logger';
|
|
30
|
+
import { hasPasswordStore } from './Passwords';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* How LiveVoice shares audio with other apps while a channel plays:
|
|
34
|
+
* - `'exclusive'`: other audio pauses, as for a media app. The default.
|
|
35
|
+
* - `'duckOthers'`: other audio keeps playing, quieter.
|
|
36
|
+
* - `'hostManaged'`: on iOS, your app configures the audio session and output route; LiveVoice
|
|
37
|
+
* changes neither. On Android, LiveVoice requests no audio focus, so your app handles focus
|
|
38
|
+
* changes and stops playback when needed. Android output selection still applies.
|
|
39
|
+
*
|
|
40
|
+
* iOS interruptions pause LiveVoice in every mode. Android's automatic focus-loss handling
|
|
41
|
+
* applies only when LiveVoice owns audio focus.
|
|
42
|
+
*/
|
|
43
|
+
export type LiveVoiceAudioFocus = 'exclusive' | 'duckOthers' | 'hostManaged';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Android only: whether a foreground service keeps playback alive while your app is in the
|
|
47
|
+
* background. Ignored on iOS.
|
|
48
|
+
*/
|
|
49
|
+
export type LiveVoiceForegroundServiceConfig =
|
|
50
|
+
| { kind: 'disabled' }
|
|
51
|
+
| { kind: 'enabled' };
|
|
52
|
+
|
|
53
|
+
/** Options for {@link initialize}. Every option has a default. */
|
|
54
|
+
export type LiveVoiceInitializeOptions = {
|
|
55
|
+
/**
|
|
56
|
+
* Android only: the foreground service that keeps playback alive in the background,
|
|
57
|
+
* `{ kind: 'enabled' }` by default. Ignored on iOS.
|
|
58
|
+
*/
|
|
59
|
+
androidServiceConfig?: LiveVoiceForegroundServiceConfig;
|
|
60
|
+
/**
|
|
61
|
+
* iOS only: whether the playing channel appears on the Lock Screen and in Control Center,
|
|
62
|
+
* `true` by default. It applies only while the audio focus is `'exclusive'`. Set it to `false`
|
|
63
|
+
* if your app shows its own media controls. Ignored on Android, where the foreground service
|
|
64
|
+
* governs the media notification.
|
|
65
|
+
*/
|
|
66
|
+
showsSystemMediaControls?: boolean;
|
|
67
|
+
/** How LiveVoice shares audio with other apps, `'exclusive'` by default; see {@link LiveVoiceAudioFocus}. */
|
|
68
|
+
audioFocus?: LiveVoiceAudioFocus;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const audioFocusValues: readonly string[] = ['exclusive', 'duckOthers', 'hostManaged'];
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Initializes the LiveVoice SDK. Await it before joining an event; reading values and adding
|
|
75
|
+
* listeners works before it.
|
|
76
|
+
*
|
|
77
|
+
* Calling it again with the same options (after defaults) succeeds. Changed options apply while
|
|
78
|
+
* no event is joined (the connection is `idle` or `failed`) and reject with `invalid_argument`
|
|
79
|
+
* otherwise; an option a platform ignores never counts as a change there.
|
|
80
|
+
*
|
|
81
|
+
* @param options - See {@link LiveVoiceInitializeOptions}; every option has a default.
|
|
82
|
+
* @throws {LiveVoiceError} `invalid_argument` for an option of the wrong type or a change while
|
|
83
|
+
* joined, `unsupported_sdk` when the native SDK is not the version this package was built for.
|
|
84
|
+
*/
|
|
85
|
+
export async function initialize(options: LiveVoiceInitializeOptions = {}): Promise<void> {
|
|
86
|
+
const configuration = initializeConfiguration(options);
|
|
87
|
+
logInfo(LiveVoiceLogEvent.init, {
|
|
88
|
+
androidForegroundService: configuration.androidForegroundService,
|
|
89
|
+
audioFocus: configuration.audioFocus,
|
|
90
|
+
showsSystemMediaControls: configuration.showsSystemMediaControls,
|
|
91
|
+
});
|
|
92
|
+
// Every key is always sent, so native compares complete configurations (bridge.md 3.2).
|
|
93
|
+
try {
|
|
94
|
+
await initializeBridge({
|
|
95
|
+
...configuration,
|
|
96
|
+
hasPasswordStore: hasPasswordStore(),
|
|
97
|
+
nativeSdkVersion: Platform.OS === 'android' ? nativeSdkVersions.android : nativeSdkVersions.ios,
|
|
98
|
+
runtimeId,
|
|
99
|
+
});
|
|
100
|
+
} catch (error) { throw normalizeError(error); }
|
|
101
|
+
sendPendingArtwork();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The options with their defaults applied, or `invalid_argument` without reaching native. */
|
|
105
|
+
function initializeConfiguration(options: LiveVoiceInitializeOptions): {
|
|
106
|
+
audioFocus: LiveVoiceAudioFocus;
|
|
107
|
+
showsSystemMediaControls: boolean;
|
|
108
|
+
androidForegroundService: 'enabled' | 'disabled';
|
|
109
|
+
} {
|
|
110
|
+
const invalid = (explanation: string) => usageError('invalid_argument', 'initialize', explanation);
|
|
111
|
+
if (options === null || typeof options !== 'object') throw invalid('The options are an object.');
|
|
112
|
+
const { androidServiceConfig, showsSystemMediaControls, audioFocus } = options;
|
|
113
|
+
if (showsSystemMediaControls !== undefined && typeof showsSystemMediaControls !== 'boolean') {
|
|
114
|
+
throw invalid('showsSystemMediaControls is a boolean.');
|
|
115
|
+
}
|
|
116
|
+
if (audioFocus !== undefined && !audioFocusValues.includes(audioFocus)) {
|
|
117
|
+
throw invalid("audioFocus is 'exclusive', 'duckOthers' or 'hostManaged'.");
|
|
118
|
+
}
|
|
119
|
+
if (androidServiceConfig !== undefined) {
|
|
120
|
+
const kind = androidServiceConfig !== null && typeof androidServiceConfig === 'object'
|
|
121
|
+
? androidServiceConfig.kind
|
|
122
|
+
: undefined;
|
|
123
|
+
if (kind !== 'enabled' && kind !== 'disabled') {
|
|
124
|
+
throw invalid("androidServiceConfig is { kind: 'enabled' } or { kind: 'disabled' }.");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
audioFocus: audioFocus ?? 'exclusive',
|
|
129
|
+
showsSystemMediaControls: showsSystemMediaControls ?? true,
|
|
130
|
+
// Enabled by default, so background playback works without setup.
|
|
131
|
+
androidForegroundService: androidServiceConfig?.kind === 'disabled' ? 'disabled' : 'enabled',
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Joins an event and resolves with the joined event once it is ready: authenticated, its initial
|
|
137
|
+
* state loaded, and commands accepted. A channel can be started right away; nothing plays yet and
|
|
138
|
+
* no subtitles are received.
|
|
139
|
+
*
|
|
140
|
+
* When the promise resolves, the observed values (connection, current event, linked events,
|
|
141
|
+
* requestable languages, available subtitles) already show the joined event, or the result of a
|
|
142
|
+
* newer call. If an update of those values cannot be delivered, the call still resolves, after 5
|
|
143
|
+
* seconds, and the problem is logged; the same holds for `leaveEvent`, the joined event's
|
|
144
|
+
* `leave()`, `reconnect` and `switchToLinkedEvent`. The features and the subtitle state are not
|
|
145
|
+
* among these values: right after the promise resolves they can still show the previous value, so
|
|
146
|
+
* observe them.
|
|
147
|
+
*
|
|
148
|
+
* Every call is a fresh join, also to the same code: it replaces whatever is joined or joining,
|
|
149
|
+
* and it never retries by itself.
|
|
150
|
+
*
|
|
151
|
+
* @param request - The join code, the API key and an optional password.
|
|
152
|
+
* @returns The joined event; its `leave()` leaves only this join.
|
|
153
|
+
* @throws {LiveVoiceError} `invalid_argument` for an empty API key, a malformed join code or a
|
|
154
|
+
* speaker's code; `superseded` when a newer join replaced it; `cancelled` when a leave ended it;
|
|
155
|
+
* `timed_out` when the event was not ready within 30 seconds, which leaves the connection
|
|
156
|
+
* `failed` (waiting for an earlier event's cleanup counts, entering a password does not);
|
|
157
|
+
* `not_initialized` before `initialize()`; otherwise why the join failed, see the joining codes
|
|
158
|
+
* of {@link LiveVoiceErrorCode}.
|
|
159
|
+
*/
|
|
160
|
+
export function joinEvent(request: LiveVoiceJoinRequest): Promise<LiveVoiceJoinedEvent> {
|
|
161
|
+
return join(request);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Leaves whatever event is joined or joining. Call it as often as you like once `initialize()`
|
|
166
|
+
* has resolved.
|
|
167
|
+
*
|
|
168
|
+
* Resolves when the event's local cleanup has ended, or at once when nothing is joined and no
|
|
169
|
+
* cleanup is still running. The observed values then already show the result: the connection is
|
|
170
|
+
* `idle` unless a newer join exists, and the current event and its lists are `null`. A pending
|
|
171
|
+
* `joinEvent`, `switchToLinkedEvent` or `reconnect` rejects with `cancelled`. To leave only one
|
|
172
|
+
* join, use the `leave()` of the joined event `joinEvent` returned.
|
|
173
|
+
*
|
|
174
|
+
* @throws {LiveVoiceError} `leave_incomplete` when the cleanup has not finished within 5 seconds:
|
|
175
|
+
* the event is left all the same, the connection is `idle` and the cleanup continues. Another
|
|
176
|
+
* leave then waits for the same cleanup and can fail with `leave_incomplete` again.
|
|
177
|
+
* `not_initialized` before `initialize()`.
|
|
178
|
+
*/
|
|
179
|
+
export async function leaveEvent(): Promise<void> {
|
|
180
|
+
logInfo(LiveVoiceLogEvent.leave);
|
|
181
|
+
// When leaveEvent() resolves, the values already show the idle connection.
|
|
182
|
+
const reply = await invoke('leave');
|
|
183
|
+
await awaitRevisions(record(reply) ? reply.revisions : undefined);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Reconnects the joined event with a fresh connection and resolves once that connection is ready.
|
|
188
|
+
*
|
|
189
|
+
* It keeps the join. Allowed while the connection is `ready` or `reconnecting`, where it forces
|
|
190
|
+
* an attempt now, and after a failure with a retryable error, where it joins again with the same
|
|
191
|
+
* code and key and the connection shows `joining`. Playback and the subtitle choice are restored
|
|
192
|
+
* unless a stop or a leave came in between; ready does not promise that playback has resumed.
|
|
193
|
+
*
|
|
194
|
+
* When the promise resolves, the observed values already show the result, or that of a newer
|
|
195
|
+
* call. The connection can already show `reconnecting` again if it dropped meanwhile.
|
|
196
|
+
*
|
|
197
|
+
* @throws {LiveVoiceError} `not_joined` without a join and while the first join or a switch is
|
|
198
|
+
* still pending, which is not disturbed; `superseded` when a newer reconnect, join or switch
|
|
199
|
+
* replaced it; `cancelled` when a leave ended it; `timed_out` after 30 seconds. For an
|
|
200
|
+
* established connection only this call fails then: the event stays joined, the connection keeps
|
|
201
|
+
* showing `reconnecting` and recovery continues by itself. The retry of a failed join that times
|
|
202
|
+
* out leaves the connection `failed`. After a failure that is not retryable, that same error
|
|
203
|
+
* again. See {@link LiveVoiceErrorCode}.
|
|
204
|
+
*/
|
|
205
|
+
export async function reconnect(): Promise<void> {
|
|
206
|
+
logInfo(LiveVoiceLogEvent.reconnectRequest);
|
|
207
|
+
const reply = await invoke('reconnect');
|
|
208
|
+
// When reconnect() resolves, the values already show its result (18 item 110).
|
|
209
|
+
await awaitRevisions(record(reply) ? reply.revisions : undefined);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Sets where LiveVoice plays its audio: a preference the SDK keeps and applies whenever it plays.
|
|
214
|
+
* It works at any time, also before {@link initialize} and before joining an event, and never
|
|
215
|
+
* rejects. {@link getCurrentAudioOutput} reports it.
|
|
216
|
+
*
|
|
217
|
+
* @param output - `'system'` for the route the system chooses, `'speaker'` for the loudspeaker.
|
|
218
|
+
*/
|
|
219
|
+
export async function setAudioOutput(output: LiveVoiceAudioOutput): Promise<void> {
|
|
220
|
+
// The public type allows nothing else; another value is ignored, as native ignores it.
|
|
221
|
+
if (output !== 'system' && output !== 'speaker') return;
|
|
222
|
+
logInfo(LiveVoiceLogEvent.audioOutputSet, { output });
|
|
223
|
+
// Until native reports a value, the wrapper's own one follows the preference (18 item 74).
|
|
224
|
+
presetAudioOutput(output);
|
|
225
|
+
try { LiveVoiceSdkReactNative.setAudioOutput(JSON.stringify({ output })); }
|
|
226
|
+
catch { /* It never rejects. */ }
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** The artwork set before {@link initialize}, which sends it once it succeeds; `{}` clears it. */
|
|
230
|
+
let pendingArtwork: { uri?: string } | undefined;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Sets the image the system's media controls show for the playing channel: the Lock Screen and
|
|
234
|
+
* Control Center on iOS, the media notification and the quick-settings player on Android. It is
|
|
235
|
+
* drawn fitted, with a margin, centred on a white square. Without one, the joined event's logo
|
|
236
|
+
* shows, if it has one.
|
|
237
|
+
*
|
|
238
|
+
* It works at any time, and a change shows at once. Before `initialize()` the image is kept and
|
|
239
|
+
* applied once `initialize()` succeeds; a failure to load it is then only logged.
|
|
240
|
+
*
|
|
241
|
+
* ```tsx
|
|
242
|
+
* await setNowPlayingArtwork(require('./assets/artwork.png'));
|
|
243
|
+
* ```
|
|
244
|
+
*
|
|
245
|
+
* @param image - A bundled image (`require(…)`) or a `{ uri }` source with a `file://` or
|
|
246
|
+
* `http(s)://` URL (on Android also `content://` or `android.resource://`), PNG or JPEG. `null`
|
|
247
|
+
* removes it, so the event's logo shows again.
|
|
248
|
+
* @throws {LiveVoiceError} `invalid_argument` for a source without a URI, or for an image that
|
|
249
|
+
* cannot be loaded or decoded; the previous image then stays.
|
|
250
|
+
*/
|
|
251
|
+
export async function setNowPlayingArtwork(image: ImageSourcePropType | null): Promise<void> {
|
|
252
|
+
const artwork = image === null ? {} : { uri: artworkUri(image) };
|
|
253
|
+
logInfo(LiveVoiceLogEvent.nowPlayingArtworkSet, { artwork: image === null ? 'none' : 'image' });
|
|
254
|
+
if (!isInitialized()) {
|
|
255
|
+
pendingArtwork = artwork;
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
// A newer call replaces one still waiting for initialize.
|
|
259
|
+
pendingArtwork = undefined;
|
|
260
|
+
await invoke('setNowPlayingArtwork', artwork);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** The URI of a single image source, or `invalid_argument` without reaching native. */
|
|
264
|
+
function artworkUri(image: ImageSourcePropType): string {
|
|
265
|
+
let uri: unknown;
|
|
266
|
+
try { uri = Image.resolveAssetSource(image)?.uri; }
|
|
267
|
+
catch { uri = undefined; }
|
|
268
|
+
if (typeof uri !== 'string' || uri.trim() === '') {
|
|
269
|
+
throw usageError('invalid_argument', 'setNowPlayingArtwork',
|
|
270
|
+
'The image is null or a single image source that resolves to a URI.');
|
|
271
|
+
}
|
|
272
|
+
return uri;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Sends the artwork set before `initialize`. Native logs a failure, which keeps the previous one. */
|
|
276
|
+
function sendPendingArtwork(): void {
|
|
277
|
+
const artwork = pendingArtwork;
|
|
278
|
+
if (artwork === undefined) return;
|
|
279
|
+
pendingArtwork = undefined;
|
|
280
|
+
invoke('setNowPlayingArtwork', artwork).catch(() => { /* Nobody awaits it any more. */ });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Stops the playing channel and resolves once local playback has stopped.
|
|
285
|
+
*
|
|
286
|
+
* Allowed whenever an event is joined, joining, reconnecting or failed; while joining or failed
|
|
287
|
+
* it resolves once an earlier event's audio is released. A pending start rejects with
|
|
288
|
+
* `cancelled`, and a later reconnect restores nothing. The channel's `playingState` shows
|
|
289
|
+
* `stopped` shortly after the promise resolves.
|
|
290
|
+
*
|
|
291
|
+
* @throws {LiveVoiceError} `not_joined` when no event is joined; `timed_out` when playback has
|
|
292
|
+
* not stopped within 10 seconds while connected; `leave_incomplete` when, while joining or
|
|
293
|
+
* failed, that audio is not released within 5 seconds (in both cases the stop itself continues);
|
|
294
|
+
* `generic` with `detail.reason` `stop_failed`. It can also fail with `cancelled`. See
|
|
295
|
+
* {@link LiveVoiceErrorCode}.
|
|
296
|
+
*/
|
|
297
|
+
export async function stopChannel(): Promise<void> {
|
|
298
|
+
logInfo(LiveVoiceLogEvent.channelStop);
|
|
299
|
+
await invoke('stopChannel');
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Starts receiving subtitles. The SDK receives one subtitle stream at a time: a new start
|
|
304
|
+
* replaces the previous selection. It never starts or stops playback.
|
|
305
|
+
* {@link getCurrentAvailableSubtitles} lists what can be started; the subtitles of an offline
|
|
306
|
+
* channel can be started too.
|
|
307
|
+
*
|
|
308
|
+
* For a given channel the promise resolves once the server accepted the subscription. Without a
|
|
309
|
+
* channel the subtitles follow the playing channel. That mode is on from the call and stays on
|
|
310
|
+
* when a channel's subtitles are refused, also when this call rejects for the channel playing
|
|
311
|
+
* now. While nothing plays the promise resolves at once; otherwise once the server accepted the
|
|
312
|
+
* playing channel's subscription. {@link getCurrentSubtitles} shows the selection shortly after
|
|
313
|
+
* the promise resolves.
|
|
314
|
+
*
|
|
315
|
+
* @param options - `channelId`: the channel to receive subtitles for; leave it out or pass `null`
|
|
316
|
+
* to follow the playing channel. `password`: the channel's password, if it is protected; a
|
|
317
|
+
* password you pass is final. Without one, a given channel's password comes from the SDK's
|
|
318
|
+
* memory, the password store, then the password provider or the built-in prompt. Subtitles that
|
|
319
|
+
* follow the playing channel never ask for a password: they use the one you pass, for the channel
|
|
320
|
+
* playing now, or one the SDK remembers, such as the one playback was started with.
|
|
321
|
+
* @throws {LiveVoiceError} `invalid_argument` for a channel id that is not a positive integer;
|
|
322
|
+
* `not_joined` unless the connection is `ready`; `command_in_progress` when the same start is
|
|
323
|
+
* still pending; `cancelled` when a different start, `stopSubtitles`, a leave or a new join ended
|
|
324
|
+
* it; `timed_out` when the server does not accept within 10 seconds (entering a password does not
|
|
325
|
+
* count); `leave_incomplete` when an unfinished subtitle stop holds it up for more than 5
|
|
326
|
+
* seconds; a channel error such as `channel_wrong_password`. A connection loss during the call
|
|
327
|
+
* can fail it with `cancelled` or `network_failed`; observe the connection to follow recovery,
|
|
328
|
+
* the rejection and the connection update need not arrive in a particular order. See
|
|
329
|
+
* {@link LiveVoiceErrorCode}.
|
|
330
|
+
*/
|
|
331
|
+
export async function startSubtitles(
|
|
332
|
+
options: { channelId?: number | null; password?: string | null } = {},
|
|
333
|
+
): Promise<void> {
|
|
334
|
+
if (options === null || typeof options !== 'object' ||
|
|
335
|
+
(options.password != null && typeof options.password !== 'string')) {
|
|
336
|
+
throw usageError('invalid_argument', 'startSubtitles',
|
|
337
|
+
'The options are an object with an optional channel id and a password that is a string or null.');
|
|
338
|
+
}
|
|
339
|
+
const channelId = options.channelId ?? null;
|
|
340
|
+
logInfo(LiveVoiceLogEvent.subtitlesStart, {
|
|
341
|
+
channelId: channelId == null ? 'current' : channelId,
|
|
342
|
+
});
|
|
343
|
+
await invoke('startSubtitles', {
|
|
344
|
+
channelId: channelId == null ? null : validateChannelId(channelId, 'startSubtitles'),
|
|
345
|
+
// `null` is the same as no password: the lookup provides it.
|
|
346
|
+
password: options.password ?? undefined,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Stops receiving subtitles and resolves once their local delivery has ended.
|
|
352
|
+
*
|
|
353
|
+
* It does not wait for the server and has no time limit. Allowed whenever an event is joined,
|
|
354
|
+
* joining, reconnecting or failed. A subtitle start that is still pending rejects with
|
|
355
|
+
* `cancelled`; playback is not affected. {@link getCurrentSubtitles} shows `disabled` shortly
|
|
356
|
+
* after the promise resolves at the latest.
|
|
357
|
+
*
|
|
358
|
+
* @throws {LiveVoiceError} `not_joined` when no event is joined. It can also fail with
|
|
359
|
+
* `cancelled`. See {@link LiveVoiceErrorCode}.
|
|
360
|
+
*/
|
|
361
|
+
export async function stopSubtitles(): Promise<void> {
|
|
362
|
+
logInfo(LiveVoiceLogEvent.subtitlesStop);
|
|
363
|
+
await invoke('stopSubtitles');
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Opens the LiveVoice website in the device's default browser.
|
|
368
|
+
*/
|
|
369
|
+
export function openLiveVoiceWebsite(): void {
|
|
370
|
+
logInfo(LiveVoiceLogEvent.openWebsite);
|
|
371
|
+
return LiveVoiceSdkReactNative.openLiveVoiceWebsite();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Returns the version of this React Native package, for example `"3.0.0"`. */
|
|
375
|
+
export function getSdkVersion(): string {
|
|
376
|
+
return LiveVoiceSdkReactNative.getSdkVersion();
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Returns the version of the native LiveVoice SDK this package uses, for example `"3.0.0"`.
|
|
381
|
+
*/
|
|
382
|
+
export function getNativeSdkVersion(): string {
|
|
383
|
+
return LiveVoiceSdkReactNative.getNativeSdkVersion();
|
|
384
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
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
|
+
// Internal: the default view's model, the bridge's `defaultView` (bridge.md 2.4). Only
|
|
15
|
+
// LiveVoiceView reads it; nothing here is exported from the package entry point.
|
|
16
|
+
|
|
17
|
+
import { decodeState, observeState, record } from './Bridge';
|
|
18
|
+
import { decodeError, decodeRetry, LiveVoiceError, type RetryValue } from './Lifecycle';
|
|
19
|
+
import {
|
|
20
|
+
type LiveVoiceAiTranslationInfo,
|
|
21
|
+
type LiveVoiceChannel,
|
|
22
|
+
type LiveVoiceDependentChannel,
|
|
23
|
+
type LiveVoiceFeatures,
|
|
24
|
+
type LiveVoicePlayingState,
|
|
25
|
+
} from './Models';
|
|
26
|
+
import { logDecodeError, logViewState } from '../util/logger';
|
|
27
|
+
|
|
28
|
+
/** The event's brand colours, `'#RRGGBB'`. */
|
|
29
|
+
export type BrandColors = { light: string; dark: string };
|
|
30
|
+
|
|
31
|
+
/** What a double tap or a named action on a default cell does (bridge.md 2.4). */
|
|
32
|
+
export type CellActionKind = 'toggleAudio' | 'toggleSubtitles';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A default cell's accessibility, computed natively by the native default cell's own code and in
|
|
36
|
+
* the native SDK's language (bridge.md 2.4). The cell renders exactly this.
|
|
37
|
+
*/
|
|
38
|
+
export type CellAccessibility = {
|
|
39
|
+
/** The channel name. */
|
|
40
|
+
label: string;
|
|
41
|
+
/** The row's states as the native cell joins them; `''` when there are none. */
|
|
42
|
+
value: string;
|
|
43
|
+
isButton: boolean;
|
|
44
|
+
isSelected: boolean;
|
|
45
|
+
/** What a double tap does, or `null` where it does nothing. */
|
|
46
|
+
activation: CellActionKind | null;
|
|
47
|
+
/** The named actions in the native order, each with its localised name. */
|
|
48
|
+
actions: { kind: CellActionKind; name: string }[];
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** A default cell's status line under the name, as native computed it (bridge.md 2.4). */
|
|
52
|
+
export type CellStatus = {
|
|
53
|
+
/** The localised line, for example "Inactive" or "AI active". */
|
|
54
|
+
text: string;
|
|
55
|
+
/** The CPU icon precedes the text: an online AI speaker. */
|
|
56
|
+
showsAiIcon: boolean;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** A default cell's mute mark, as native computed it (bridge.md 2.4). */
|
|
60
|
+
export type CellMuteMark = 'microphone' | 'camera';
|
|
61
|
+
|
|
62
|
+
/** The part the default view shows. */
|
|
63
|
+
export type ViewPart =
|
|
64
|
+
| { kind: 'loading' }
|
|
65
|
+
| {
|
|
66
|
+
kind: 'ready';
|
|
67
|
+
channels: LiveVoiceChannel[];
|
|
68
|
+
/** Each channel's `LiveVoiceChannelCell.isEnabled`, by id. */
|
|
69
|
+
isEnabled: ReadonlyMap<number, boolean>;
|
|
70
|
+
/** Whether each channel's subtitles are the open ones, by id: the default cell's marker. */
|
|
71
|
+
subtitlesOpen: ReadonlyMap<number, boolean>;
|
|
72
|
+
/** Each default cell's accessibility, by id; `null` for one native did not send. */
|
|
73
|
+
accessibility: ReadonlyMap<number, CellAccessibility | null>;
|
|
74
|
+
/** Each default cell's status line, by id; `null` for none. */
|
|
75
|
+
status: ReadonlyMap<number, CellStatus | null>;
|
|
76
|
+
/** Each default cell's mute mark, by id; `null` for none. */
|
|
77
|
+
muteMark: ReadonlyMap<number, CellMuteMark | null>;
|
|
78
|
+
/** The list stays, drawn disabled, while the connection briefly recovers. */
|
|
79
|
+
inReconnectGrace: boolean;
|
|
80
|
+
brandColors: BrandColors | null;
|
|
81
|
+
}
|
|
82
|
+
| { kind: 'reconnecting'; retry: RetryValue }
|
|
83
|
+
| { kind: 'error'; error: LiveVoiceError };
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The default view's model: the part to show, and the permission of the join it belongs to,
|
|
87
|
+
* `null` until that join has authenticated. The gate and the branding read this permission, so
|
|
88
|
+
* channels and permission always come from the same join.
|
|
89
|
+
*/
|
|
90
|
+
export type ViewModel = { part: ViewPart; features: LiveVoiceFeatures | null };
|
|
91
|
+
|
|
92
|
+
const loading: ViewModel = { part: { kind: 'loading' }, features: null };
|
|
93
|
+
|
|
94
|
+
let current: ViewModel = loading;
|
|
95
|
+
|
|
96
|
+
// The default view's decoder in the bridge, before any observer hears of a change. An undecodable
|
|
97
|
+
// view shows the loading part, is logged, and satisfies no command's revisions.
|
|
98
|
+
decodeState('defaultView', wire => {
|
|
99
|
+
try {
|
|
100
|
+
current = viewModel(wire);
|
|
101
|
+
return true;
|
|
102
|
+
} catch {
|
|
103
|
+
current = loading;
|
|
104
|
+
logDecodeError('state', 'defaultView');
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
export function getCurrentViewModel(): ViewModel {
|
|
110
|
+
return current;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Calls `handler` whenever the default view changes. */
|
|
114
|
+
export function onViewModelChanged(handler: (model: ViewModel) => void): { remove: () => void } {
|
|
115
|
+
const remove = observeState('defaultView', () => {
|
|
116
|
+
const model = getCurrentViewModel();
|
|
117
|
+
logViewState(model);
|
|
118
|
+
handler(model);
|
|
119
|
+
});
|
|
120
|
+
return { remove };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The model of a `defaultView` value; throws for one that is no view at all. */
|
|
124
|
+
function viewModel(wire: unknown): ViewModel {
|
|
125
|
+
if (!record(wire)) throw new TypeError('defaultView');
|
|
126
|
+
const permission = features(wire.features);
|
|
127
|
+
switch (wire.kind) {
|
|
128
|
+
case 'loading':
|
|
129
|
+
return loading;
|
|
130
|
+
case 'ready': {
|
|
131
|
+
const wireChannels = Array.isArray(wire.channels) ? wire.channels.filter(record) : [];
|
|
132
|
+
const isEnabled = new Map<number, boolean>();
|
|
133
|
+
const subtitlesOpen = new Map<number, boolean>();
|
|
134
|
+
const accessibility = new Map<number, CellAccessibility | null>();
|
|
135
|
+
const status = new Map<number, CellStatus | null>();
|
|
136
|
+
const muteMark = new Map<number, CellMuteMark | null>();
|
|
137
|
+
const channels = wireChannels.map(wireChannel => {
|
|
138
|
+
const channel = decodeChannel(wireChannel);
|
|
139
|
+
isEnabled.set(channel.id, wireChannel.isEnabled === true);
|
|
140
|
+
subtitlesOpen.set(channel.id, wireChannel.subtitlesOpen === true);
|
|
141
|
+
accessibility.set(channel.id, cellAccessibility(wireChannel.accessibility));
|
|
142
|
+
status.set(channel.id, cellStatus(wireChannel.status));
|
|
143
|
+
muteMark.set(channel.id, cellMuteMark(wireChannel.muteMark));
|
|
144
|
+
return channel;
|
|
145
|
+
});
|
|
146
|
+
return {
|
|
147
|
+
part: {
|
|
148
|
+
kind: 'ready',
|
|
149
|
+
channels,
|
|
150
|
+
isEnabled,
|
|
151
|
+
subtitlesOpen,
|
|
152
|
+
accessibility,
|
|
153
|
+
status,
|
|
154
|
+
muteMark,
|
|
155
|
+
inReconnectGrace: wire.inReconnectGrace === true,
|
|
156
|
+
brandColors: brandColors(wire.brandColors),
|
|
157
|
+
},
|
|
158
|
+
features: permission,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
case 'reconnecting':
|
|
162
|
+
return {
|
|
163
|
+
part: { kind: 'reconnecting', retry: decodeRetry(wire.retry) ?? { kind: 'inProgress' } },
|
|
164
|
+
features: permission,
|
|
165
|
+
};
|
|
166
|
+
case 'error':
|
|
167
|
+
return { part: { kind: 'error', error: decodeError(wire.error) }, features: permission };
|
|
168
|
+
default:
|
|
169
|
+
throw new TypeError('defaultView');
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function cellAccessibility(value: unknown): CellAccessibility | null {
|
|
174
|
+
if (!record(value)) return null;
|
|
175
|
+
const actions = Array.isArray(value.actions) ? value.actions.filter(record) : [];
|
|
176
|
+
return {
|
|
177
|
+
label: string(value.label),
|
|
178
|
+
value: string(value.value),
|
|
179
|
+
isButton: value.isButton === true,
|
|
180
|
+
isSelected: value.isSelected === true,
|
|
181
|
+
// iOS sends `null` and Android leaves the key out where a double tap does nothing.
|
|
182
|
+
activation: actionKind(value.activation),
|
|
183
|
+
actions: actions.flatMap(action => {
|
|
184
|
+
const kind = actionKind(action.kind);
|
|
185
|
+
return kind === null || typeof action.name !== 'string' ? [] : [{ kind, name: action.name }];
|
|
186
|
+
}),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** The status line; `null` and a missing key both mean none. */
|
|
191
|
+
function cellStatus(value: unknown): CellStatus | null {
|
|
192
|
+
if (!record(value) || typeof value.text !== 'string') return null;
|
|
193
|
+
return { text: value.text, showsAiIcon: value.showsAiIcon === true };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** The mute mark; `null` and a missing key both mean none. */
|
|
197
|
+
function cellMuteMark(value: unknown): CellMuteMark | null {
|
|
198
|
+
return value === 'microphone' || value === 'camera' ? value : null;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function actionKind(value: unknown): CellActionKind | null {
|
|
202
|
+
return value === 'toggleAudio' || value === 'toggleSubtitles' ? value : null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function features(value: unknown): LiveVoiceFeatures | null {
|
|
206
|
+
if (!record(value)) return null;
|
|
207
|
+
return { customUiAllowed: value.customUiAllowed === true, hideBranding: value.hideBranding === true };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function brandColors(value: unknown): BrandColors | null {
|
|
211
|
+
if (!record(value) || typeof value.light !== 'string' || typeof value.dark !== 'string') return null;
|
|
212
|
+
return { light: value.light, dark: value.dark };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** A wire channel (bridge.md 2.2) as the public `LiveVoiceChannel`. */
|
|
216
|
+
export function decodeChannel(wire: Record<string, unknown>): LiveVoiceChannel {
|
|
217
|
+
const translation = record(wire.aiTranslationInfo) ? wire.aiTranslationInfo : null;
|
|
218
|
+
const aiTranslationInfo: LiveVoiceAiTranslationInfo | null = translation === null ? null : {
|
|
219
|
+
sourceChannelId: number(translation.sourceChannelId),
|
|
220
|
+
targetLanguage: string(translation.targetLanguage),
|
|
221
|
+
};
|
|
222
|
+
const dependents = Array.isArray(wire.dependentChannels) ? wire.dependentChannels.filter(record) : [];
|
|
223
|
+
return {
|
|
224
|
+
id: number(wire.id),
|
|
225
|
+
name: string(wire.name),
|
|
226
|
+
isOnline: wire.isOnline === true,
|
|
227
|
+
hasAudio: wire.hasAudio === true,
|
|
228
|
+
// Older native SDKs do not send it.
|
|
229
|
+
hasVideo: wire.hasVideo === true,
|
|
230
|
+
hasSubtitles: wire.hasSubtitles === true,
|
|
231
|
+
isAudioMuted: wire.isAudioMuted === true,
|
|
232
|
+
isPasswordProtected: wire.isPasswordProtected === true,
|
|
233
|
+
isAiSpeaker: wire.isAiSpeaker === true,
|
|
234
|
+
isOnDemand: wire.isOnDemand === true,
|
|
235
|
+
aiTranslationInfo,
|
|
236
|
+
dependentChannels: dependents.map((dependent): LiveVoiceDependentChannel => ({
|
|
237
|
+
id: number(dependent.id),
|
|
238
|
+
name: string(dependent.name),
|
|
239
|
+
sourceChannelId: number(dependent.sourceChannelId),
|
|
240
|
+
// The transcript has no language; an empty one from an older native SDK means the same.
|
|
241
|
+
language: typeof dependent.language === 'string' && dependent.language !== '' ? dependent.language : null,
|
|
242
|
+
hasAudio: dependent.hasAudio === true,
|
|
243
|
+
hasSubtitles: dependent.hasSubtitles === true,
|
|
244
|
+
})),
|
|
245
|
+
playingState: playingState(wire.playingState),
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function playingState(value: unknown): LiveVoicePlayingState {
|
|
250
|
+
return value === 'playing' || value === 'transitioning' ? value : 'stopped';
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function number(value: unknown): number {
|
|
254
|
+
return typeof value === 'number' ? value : -1;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function string(value: unknown): string {
|
|
258
|
+
return typeof value === 'string' ? value : '';
|
|
259
|
+
}
|