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.
- package/README.md +105 -66
- package/android/build.gradle +5 -4
- package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +122 -188
- package/android/src/main/java/io/livevoice/react/__SDK.kt +2 -2
- package/ios/LiveVoiceRNBridge.swift +362 -0
- package/ios/LiveVoiceSdkReactNative.mm +187 -158
- package/ios/__SDK.h +1 -1
- package/ios/livevoice_spm_backport.rb +202 -0
- package/lib/module/components/BrandingView.js +58 -39
- package/lib/module/components/BrandingView.js.map +1 -1
- package/lib/module/components/ChannelView.js +355 -44
- package/lib/module/components/ChannelView.js.map +1 -1
- package/lib/module/components/ErrorView.js +5 -6
- 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 +311 -133
- 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 +180 -244
- package/lib/module/components/PlayButtonView.js.map +1 -1
- package/lib/module/components/ReconnectingView.js +31 -53
- package/lib/module/components/ReconnectingView.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 +11 -6
- 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 +262 -268
- package/lib/module/sdk/LiveVoiceSdk.js.map +1 -1
- package/lib/module/sdk/LiveVoiceState.js +208 -40
- package/lib/module/sdk/LiveVoiceState.js.map +1 -1
- 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/{util/PlayingState.js → sdk/NativeSdkVersions.js} +9 -8
- 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.map +1 -1
- package/lib/module/util/Colors.js +2 -1
- package/lib/module/util/Colors.js.map +1 -1
- package/lib/module/util/Translations.js +34 -11
- 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 +79 -41
- package/lib/module/util/logger.js.map +1 -1
- 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 +81 -21
- 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/index.d.ts +15 -6
- 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 +195 -190
- package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -1
- package/lib/typescript/src/sdk/LiveVoiceState.d.ts +77 -9
- package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -1
- 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 +34 -31
- 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 +17 -7
- package/lib/typescript/src/util/logger.d.ts.map +1 -1
- package/livevoice-sdk-react-native.podspec +29 -4
- package/package.json +12 -9
- package/src/components/BrandingView.tsx +42 -55
- package/src/components/ChannelView.tsx +329 -38
- package/src/components/ErrorView.tsx +9 -12
- package/src/components/LiveVoiceSpinner.tsx +87 -0
- package/src/components/LiveVoiceView.tsx +319 -201
- package/src/components/PasswordPrompt.tsx +144 -0
- package/src/components/PlayButtonView.tsx +165 -269
- package/src/components/ReconnectingView.tsx +28 -81
- 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 +92 -6
- 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 +285 -309
- package/src/sdk/LiveVoiceState.ts +230 -60
- package/src/sdk/LogEntries.ts +88 -0
- package/src/sdk/Models.ts +129 -0
- package/src/{util/PlayingState.ts → sdk/NativeSdkVersions.ts} +7 -6
- 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 +42 -38
- package/src/util/Colors.ts +2 -1
- package/src/util/Translations.ts +34 -12
- package/src/util/freeze.ts +23 -0
- package/src/util/logger.ts +79 -52
- package/lib/module/components/AudioOutputSwitchView.js +0 -138
- package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
- package/lib/module/components/index.js +0 -24
- package/lib/module/components/index.js.map +0 -1
- package/lib/module/icons/mic.png +0 -0
- package/lib/module/util/ForegroundServiceConfig.js +0 -38
- package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
- 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 -19
- 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 -86
- package/lib/typescript/src/util/UIState.d.ts.map +0 -1
- package/src/components/AudioOutputSwitchView.tsx +0 -146
- package/src/components/index.tsx +0 -21
- package/src/icons/mic.png +0 -0
- package/src/util/ForegroundServiceConfig.tsx +0 -42
- package/src/util/UIState.ts +0 -106
|
@@ -0,0 +1,362 @@
|
|
|
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 only place the React Native wrapper talks to the native SDK, through
|
|
15
|
+
// its wrapper bridge. The TurboModule (.mm) forwards here and imports no SDK
|
|
16
|
+
// header itself. Every structured value is a JSON string the SDK encodes and
|
|
17
|
+
// decodes; this file only forwards them (bridge.md 1.1).
|
|
18
|
+
//
|
|
19
|
+
// Commands return their native outcome. The SDK bridge admits them in order.
|
|
20
|
+
//
|
|
21
|
+
// Not for apps. The class is SPI rather than internal: the .mm reaches it
|
|
22
|
+
// through the generated Swift header, which lists only public declarations
|
|
23
|
+
// of a pod, and SPI keeps it out of an app's plain `import`.
|
|
24
|
+
|
|
25
|
+
import Foundation
|
|
26
|
+
@_spi(LiveVoiceWrappers) import LiveVoiceSDK
|
|
27
|
+
|
|
28
|
+
@_spi(LiveVoiceReactNative)
|
|
29
|
+
@objc(LiveVoiceRNBridge)
|
|
30
|
+
public final class LiveVoiceRNBridge: NSObject, @unchecked Sendable {
|
|
31
|
+
// `@unchecked Sendable`: `callbacks` and `commands` never change after
|
|
32
|
+
// `init`, `owner` is locked, and the SDK's bridge is used only on the main
|
|
33
|
+
// actor, apart from its thread-safe `requestInvalidation()`.
|
|
34
|
+
|
|
35
|
+
private struct Command: Sendable {
|
|
36
|
+
let name: String
|
|
37
|
+
let arguments: String
|
|
38
|
+
let reply: CommandReply
|
|
39
|
+
}
|
|
40
|
+
private let replies = CommandReplies()
|
|
41
|
+
|
|
42
|
+
private struct Callbacks: Sendable {
|
|
43
|
+
let onState: @Sendable (String) -> Void
|
|
44
|
+
let onPasswordRequest: @Sendable (String) -> Void
|
|
45
|
+
let onPasswordRequestCancelled: @Sendable (String) -> Void
|
|
46
|
+
let onPasswordStoreRequest: @Sendable (String) -> Void
|
|
47
|
+
let onLogEntry: @Sendable (String) -> Void
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private let callbacks: Callbacks
|
|
51
|
+
private let commands: AsyncStream<Command>
|
|
52
|
+
private let commandSink: AsyncStream<Command>.Continuation
|
|
53
|
+
/// The SDK's bridge, set once in `start`, and whether the runtime went away.
|
|
54
|
+
private let owner = Owner()
|
|
55
|
+
/// The module's one long-lived task; `invalidate()` ends it.
|
|
56
|
+
@MainActor private var runTask: Task<Void, Never>?
|
|
57
|
+
|
|
58
|
+
private init(callbacks: Callbacks) {
|
|
59
|
+
self.callbacks = callbacks
|
|
60
|
+
(commands, commandSink) = AsyncStream<Command>.makeStream()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// MARK: Lifecycle
|
|
64
|
+
|
|
65
|
+
/// Attaches the SDK's wrapper bridge and starts forwarding its state,
|
|
66
|
+
/// password and store requests and log entries. The identity headers and
|
|
67
|
+
/// the configuration apply with ``initialize(configuration:completion:)``.
|
|
68
|
+
@objc(startWithReactNativeVersion:wrapperVersion:onState:onPasswordRequest:onPasswordRequestCancelled:onPasswordStoreRequest:onLogEntry:completion:)
|
|
69
|
+
public static func start(
|
|
70
|
+
reactNativeVersion: String,
|
|
71
|
+
wrapperVersion: String,
|
|
72
|
+
onState: @escaping @Sendable (String) -> Void,
|
|
73
|
+
onPasswordRequest: @escaping @Sendable (String) -> Void,
|
|
74
|
+
onPasswordRequestCancelled: @escaping @Sendable (String) -> Void,
|
|
75
|
+
onPasswordStoreRequest: @escaping @Sendable (String) -> Void,
|
|
76
|
+
onLogEntry: @escaping @Sendable (String) -> Void,
|
|
77
|
+
completion: @escaping @Sendable (LiveVoiceRNBridge) -> Void
|
|
78
|
+
) {
|
|
79
|
+
let module = LiveVoiceRNBridge(callbacks: Callbacks(
|
|
80
|
+
onState: onState,
|
|
81
|
+
onPasswordRequest: onPasswordRequest,
|
|
82
|
+
onPasswordRequestCancelled: onPasswordRequestCancelled,
|
|
83
|
+
onPasswordStoreRequest: onPasswordStoreRequest,
|
|
84
|
+
onLogEntry: onLogEntry
|
|
85
|
+
))
|
|
86
|
+
Task { @MainActor in
|
|
87
|
+
let bridge = LiveVoiceWrapperBridge.attach(
|
|
88
|
+
framework: .reactNative(version: reactNativeVersion),
|
|
89
|
+
wrapperVersion: wrapperVersion
|
|
90
|
+
)
|
|
91
|
+
module.owner.attach(bridge)
|
|
92
|
+
// None of these is replayed, so every stream is taken before
|
|
93
|
+
// anything suspends: nothing of this bridge is missed.
|
|
94
|
+
let streams = Streams(
|
|
95
|
+
states: bridge.stateUpdates,
|
|
96
|
+
passwordRequests: bridge.passwordRequestUpdates,
|
|
97
|
+
passwordCancellations: bridge.passwordRequestCancellations,
|
|
98
|
+
storeRequests: bridge.passwordStoreRequestUpdates,
|
|
99
|
+
logEntries: bridge.logEntries
|
|
100
|
+
)
|
|
101
|
+
module.runTask = Task { @MainActor in await module.run(bridge, streams: streams) }
|
|
102
|
+
completion(module)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// The host method `initialize` (bridge.md 3.2): applies the configuration
|
|
107
|
+
/// and replies with the snapshot JSON, or with the bridge form of the error.
|
|
108
|
+
@objc(initializeWithConfiguration:completion:)
|
|
109
|
+
public func initializeSDK(configuration: String, completion: @escaping @Sendable (String?, String?) -> Void) {
|
|
110
|
+
let reply = replies.make(completion)
|
|
111
|
+
guard !reply.isFinished else { return }
|
|
112
|
+
Task { @MainActor in
|
|
113
|
+
guard let bridge = self.owner.bridge, !reply.isFinished else {
|
|
114
|
+
reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(code: .cancelled))
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
do {
|
|
118
|
+
let snapshot = try await bridge.initialize(configuration: configuration)
|
|
119
|
+
reply.finish(snapshot, nil)
|
|
120
|
+
} catch {
|
|
121
|
+
reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(error))
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// The runtime is going away: ends the module's task, which detaches the
|
|
127
|
+
/// SDK's bridge and leaves the event.
|
|
128
|
+
@objc public func invalidate() {
|
|
129
|
+
guard let bridge = owner.invalidate() else { return }
|
|
130
|
+
// Recorded on this thread, before main-actor teardown can be overtaken.
|
|
131
|
+
bridge?.requestInvalidation()
|
|
132
|
+
replies.close()
|
|
133
|
+
commandSink.finish()
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/// Wait for the command loop and all observers to finish detaching.
|
|
137
|
+
@MainActor
|
|
138
|
+
func waitUntilDetached() async {
|
|
139
|
+
await runTask?.value
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@objc public static var nativeSdkVersion: String {
|
|
143
|
+
LiveVoiceWrapperBridge.nativeSDKVersion
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private struct Streams: @unchecked Sendable {
|
|
147
|
+
let states: AsyncStream<String>
|
|
148
|
+
let passwordRequests: AsyncStream<String>
|
|
149
|
+
let passwordCancellations: AsyncStream<String>
|
|
150
|
+
let storeRequests: AsyncStream<String>
|
|
151
|
+
let logEntries: AsyncStream<String>
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
@MainActor
|
|
155
|
+
private func run(_ bridge: LiveVoiceWrapperBridge, streams: Streams) async {
|
|
156
|
+
await withTaskGroup(of: Void.self) { group in
|
|
157
|
+
// The SDK's diagnostic events, in the order they were logged,
|
|
158
|
+
// until the bridge's streams finish.
|
|
159
|
+
group.addTask { @MainActor in
|
|
160
|
+
for await entry in streams.logEntries {
|
|
161
|
+
self.callbacks.onLogEntry(entry)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
group.addTask { @MainActor in
|
|
165
|
+
// The streams finish when a newer runtime replaced this bridge.
|
|
166
|
+
defer { self.invalidate() }
|
|
167
|
+
for await state in streams.states {
|
|
168
|
+
self.callbacks.onState(state)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
group.addTask { @MainActor in
|
|
172
|
+
for await request in streams.passwordRequests {
|
|
173
|
+
self.callbacks.onPasswordRequest(request)
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
group.addTask { @MainActor in
|
|
177
|
+
for await cancellation in streams.passwordCancellations {
|
|
178
|
+
self.callbacks.onPasswordRequestCancelled(cancellation)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
group.addTask { @MainActor in
|
|
182
|
+
for await request in streams.storeRequests {
|
|
183
|
+
self.callbacks.onPasswordStoreRequest(request)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
// The bridge starts each command once the previous one has been
|
|
187
|
+
// admitted, so they reach the SDK in call order.
|
|
188
|
+
await bridge.runCommands(commands) { command in
|
|
189
|
+
await Self.execute(command, on: bridge)
|
|
190
|
+
}
|
|
191
|
+
group.cancelAll()
|
|
192
|
+
}
|
|
193
|
+
await bridge.detach()
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
@MainActor
|
|
197
|
+
private static func execute(_ command: Command, on bridge: LiveVoiceWrapperBridge) async {
|
|
198
|
+
guard !command.reply.isFinished else { return }
|
|
199
|
+
do {
|
|
200
|
+
let value = try await bridge.execute(name: command.name, arguments: command.arguments)
|
|
201
|
+
let delivered = command.reply.finish(value, nil)
|
|
202
|
+
if command.name == "join", let data = value.data(using: .utf8),
|
|
203
|
+
let payload = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any],
|
|
204
|
+
let entryID = payload["entryId"] as? String {
|
|
205
|
+
bridge.completeJoinDelivery(entryID: entryID, delivered: delivered)
|
|
206
|
+
}
|
|
207
|
+
} catch {
|
|
208
|
+
command.reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(error))
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
@objc public func execute(name: String, arguments: String,
|
|
213
|
+
completion: @escaping @Sendable (String?, String?) -> Void) {
|
|
214
|
+
let reply = replies.make(completion)
|
|
215
|
+
if case .terminated = commandSink.yield(Command(name: name, arguments: arguments, reply: reply)) {
|
|
216
|
+
reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(code: .cancelled))
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/// The host method `setAudioOutput` (bridge.md 3.2): works before
|
|
221
|
+
/// `initialize`, once the module has attached the bridge, and never
|
|
222
|
+
/// fails; a replaced runtime's call is ignored.
|
|
223
|
+
@objc public func setAudioOutput(_ payload: String) {
|
|
224
|
+
Task { @MainActor in self.owner.bridge?.setAudioOutput(payload) }
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/// The host method `writeLogEntry` (bridge.md 3.6): one of the wrapper's
|
|
228
|
+
/// own entries, `{event, level, fields}`, into the SDK log. Works before
|
|
229
|
+
/// a bridge is attached.
|
|
230
|
+
@objc public static func writeLogEntry(_ payload: String) {
|
|
231
|
+
LiveVoiceWrapperBridge.writeLogEntry(payload)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// The host method `answerPasswordRequest` (bridge.md 4.2); a late or
|
|
235
|
+
/// unknown answer is ignored.
|
|
236
|
+
@objc public func answerPasswordRequest(_ payload: String) {
|
|
237
|
+
Task { @MainActor in self.owner.bridge?.answerPasswordRequest(payload) }
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/// The host method `answerPasswordStoreRequest` (bridge.md 4.3); a late
|
|
241
|
+
/// or unknown answer is ignored.
|
|
242
|
+
@objc public func answerPasswordStoreRequest(_ payload: String) {
|
|
243
|
+
Task { @MainActor in self.owner.bridge?.answerPasswordStoreRequest(payload) }
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/// The host method `setPasswordStore` (bridge.md 4.3): whether the host
|
|
247
|
+
/// set a store, after `initialize` carried the first value.
|
|
248
|
+
@objc public func setPasswordStore(_ payload: String) {
|
|
249
|
+
Task { @MainActor in self.owner.bridge?.setPasswordStore(payload) }
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
@objc public static func openLiveVoiceWebsite() {
|
|
253
|
+
Task { @MainActor in LiveVoiceWrapperBridge.openLiveVoiceWebsite() }
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// MARK: Errors
|
|
257
|
+
|
|
258
|
+
/// `getErrorMessages` (bridge.md 3.2): the text of every error code.
|
|
259
|
+
@objc public static var errorMessages: String {
|
|
260
|
+
LiveVoiceWrapperBridge.errorMessagesJSON()
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/// The bridge form of an error the module raises itself, by its code
|
|
264
|
+
/// (`cancelled`, `not_initialized`).
|
|
265
|
+
@objc public static func errorJSON(code: String) -> String {
|
|
266
|
+
LiveVoiceWrapperBridge.errorJSON(code: LiveVoiceErrorCode(rawValue: code) ?? .generic)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// MARK: Translations
|
|
270
|
+
|
|
271
|
+
/// The SDK's localized string for `key`, or an empty string for an
|
|
272
|
+
/// unknown key. `seconds` fills the strings that take a value.
|
|
273
|
+
@objc public static func translation(_ key: String, seconds: Int) -> String {
|
|
274
|
+
let translationKey: LiveVoiceWrapperBridge.TranslationKey
|
|
275
|
+
switch key {
|
|
276
|
+
case "action_retry_now": translationKey = .retryNow
|
|
277
|
+
case "channel_status_offline": translationKey = .channelOffline
|
|
278
|
+
case "error_screen_retrying": translationKey = .reconnecting
|
|
279
|
+
case "error_screen_retrying_in": translationKey = .reconnectingIn(seconds: seconds)
|
|
280
|
+
case "sdk_no_matching_channels": translationKey = .noMatchingChannels
|
|
281
|
+
case "no_channels": translationKey = .noChannels
|
|
282
|
+
case "streaming_by_title": translationKey = .streamingBy
|
|
283
|
+
case "a11y_channel_play": translationKey = .accessibilityChannelPlay
|
|
284
|
+
case "a11y_channel_status_muted": translationKey = .accessibilityChannelMuted
|
|
285
|
+
case "a11y_channel_stop": translationKey = .accessibilityChannelStop
|
|
286
|
+
case "channel_status_is_ai": translationKey = .channelIsAI
|
|
287
|
+
case "password_prompt_title": translationKey = .passwordPromptTitle
|
|
288
|
+
case "password_prompt_channel_message": translationKey = .passwordPromptChannelMessage
|
|
289
|
+
case "password_prompt_event_message": translationKey = .passwordPromptEventMessage
|
|
290
|
+
case "password_prompt_wrong_password": translationKey = .passwordPromptWrongPassword
|
|
291
|
+
case "password_prompt_field": translationKey = .passwordPromptField
|
|
292
|
+
case "password_prompt_cancel_button": translationKey = .passwordPromptCancelButton
|
|
293
|
+
case "password_prompt_channel_confirm_button": translationKey = .passwordPromptChannelConfirmButton
|
|
294
|
+
case "password_prompt_event_confirm_button": translationKey = .passwordPromptEventConfirmButton
|
|
295
|
+
default: return ""
|
|
296
|
+
}
|
|
297
|
+
return LiveVoiceWrapperBridge.translation(translationKey)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/// The SDK's bridge of a module and whether its runtime went away. Locked;
|
|
302
|
+
/// no call into the SDK happens under the lock.
|
|
303
|
+
private final class Owner: @unchecked Sendable {
|
|
304
|
+
private let lock = NSLock()
|
|
305
|
+
private var current: LiveVoiceWrapperBridge?
|
|
306
|
+
private var invalidated = false
|
|
307
|
+
|
|
308
|
+
var bridge: LiveVoiceWrapperBridge? {
|
|
309
|
+
lock.lock(); defer { lock.unlock() }
|
|
310
|
+
return invalidated ? nil : current
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
func attach(_ bridge: LiveVoiceWrapperBridge) {
|
|
314
|
+
lock.lock(); defer { lock.unlock() }
|
|
315
|
+
current = bridge
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/// Marks the runtime as gone. `nil` when it was already; otherwise the
|
|
319
|
+
/// bridge, if one was attached.
|
|
320
|
+
func invalidate() -> LiveVoiceWrapperBridge?? {
|
|
321
|
+
lock.lock(); defer { lock.unlock() }
|
|
322
|
+
if invalidated { return nil }
|
|
323
|
+
invalidated = true
|
|
324
|
+
return .some(current)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/// Callback crossing the native thread/actor boundary. All mutable state is lock protected.
|
|
329
|
+
/// Replace with an isolated completion type when the generated bridge supports Sendable replies.
|
|
330
|
+
private final class CommandReply: @unchecked Sendable {
|
|
331
|
+
private let lock = NSLock()
|
|
332
|
+
private var callback: (@Sendable (String?, String?) -> Void)?
|
|
333
|
+
init(_ callback: @escaping @Sendable (String?, String?) -> Void) { self.callback = callback }
|
|
334
|
+
var isFinished: Bool { lock.lock(); defer { lock.unlock() }; return callback == nil }
|
|
335
|
+
@discardableResult
|
|
336
|
+
func finish(_ value: String?, _ error: String?) -> Bool {
|
|
337
|
+
lock.lock(); let reply = callback; callback = nil; lock.unlock()
|
|
338
|
+
reply?(value, error)
|
|
339
|
+
return reply != nil
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/// Entries and closed state are locked; no callback executes under this lock.
|
|
344
|
+
private final class CommandReplies: @unchecked Sendable {
|
|
345
|
+
private let lock = NSLock()
|
|
346
|
+
private var closed = false
|
|
347
|
+
private var values: [CommandReply] = []
|
|
348
|
+
func make(_ callback: @escaping @Sendable (String?, String?) -> Void) -> CommandReply {
|
|
349
|
+
let reply = CommandReply(callback)
|
|
350
|
+
lock.lock()
|
|
351
|
+
let reject = closed
|
|
352
|
+
values.removeAll { $0.isFinished }
|
|
353
|
+
if !reject { values.append(reply) }
|
|
354
|
+
lock.unlock()
|
|
355
|
+
if reject { reply.finish(nil, LiveVoiceWrapperBridge.errorJSON(code: .cancelled)) }
|
|
356
|
+
return reply
|
|
357
|
+
}
|
|
358
|
+
func close() {
|
|
359
|
+
lock.lock(); closed = true; let pending = values; values = []; lock.unlock()
|
|
360
|
+
pending.forEach { $0.finish(nil, LiveVoiceWrapperBridge.errorJSON(code: .cancelled)) }
|
|
361
|
+
}
|
|
362
|
+
}
|