@poolse/native 0.0.0-stage → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +206 -0
- package/README.md +116 -2
- package/android/build.gradle +58 -0
- package/android/consumer-rules.pro +11 -0
- package/android/src/main/AndroidManifest.xml +46 -0
- package/android/src/main/java/dev/poolse/callcore/CallCoreRuntime.kt +540 -0
- package/android/src/main/java/dev/poolse/callcore/CallMachine.kt +492 -0
- package/android/src/main/java/dev/poolse/callcore/Ports.kt +132 -0
- package/android/src/main/java/dev/poolse/callcore/android/AndroidRingSurface.kt +383 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallActionReceiver.kt +45 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallCore.kt +578 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallForegroundService.kt +294 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallHttpClient.kt +207 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallMessagingService.kt +161 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallVideo.kt +36 -0
- package/android/src/main/java/dev/poolse/callcore/android/HandlerTimerPort.kt +42 -0
- package/android/src/main/java/dev/poolse/callcore/android/IncomingCallActivity.kt +314 -0
- package/android/src/main/java/dev/poolse/callcore/android/MessageNotifications.kt +353 -0
- package/android/src/main/java/dev/poolse/callcore/android/MissedCallNotification.kt +103 -0
- package/android/src/main/java/dev/poolse/callcore/android/RingBundle.kt +42 -0
- package/android/src/main/java/dev/poolse/callcore/android/SnapshotJson.kt +99 -0
- package/android/src/main/java/expo/modules/poolsecalls/CallLockscreen.kt +42 -0
- package/android/src/main/java/expo/modules/poolsecalls/CallPictureInPicture.kt +92 -0
- package/android/src/main/java/expo/modules/poolsecalls/PoolseCallVideoView.kt +64 -0
- package/android/src/main/java/expo/modules/poolsecalls/PoolseCallsModule.kt +205 -0
- package/android/src/main/livekit/java/dev/poolse/callcore/livekit/LiveKitMediaPort.kt +594 -0
- package/android/src/main/livekit/java/dev/poolse/callcore/livekit/LiveKitVideoSurface.kt +107 -0
- package/android/src/test/java/dev/poolse/callcore/android/CallHttpClientTest.kt +61 -0
- package/app.plugin.js +5 -0
- package/dist/index.cjs +197 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +313 -0
- package/dist/index.d.ts +313 -0
- package/dist/index.js +161 -0
- package/dist/index.js.map +1 -0
- package/expo-module.config.json +11 -0
- package/ios/Core/CallCoreRuntime.swift +543 -0
- package/ios/Core/CallHttpClient.swift +213 -0
- package/ios/Core/CallMachine.swift +394 -0
- package/ios/Core/PORTS_READY.md +93 -0
- package/ios/Core/Ports.swift +238 -0
- package/ios/Core/SnapshotEncoding.swift +122 -0
- package/ios/Module/PoolseCallVideoView.swift +70 -0
- package/ios/Module/PoolseCallsAppDelegateSubscriber.swift +16 -0
- package/ios/Module/PoolseCallsModule.swift +150 -0
- package/ios/Platform/CallCore.swift +623 -0
- package/ios/Platform/CallKitController.swift +326 -0
- package/ios/Platform/CallPictureInPicture.swift +145 -0
- package/ios/Platform/LiveKitMediaPort.swift +798 -0
- package/ios/Platform/LiveKitVideoSurface.swift +68 -0
- package/ios/Platform/MessageNotifications.swift +163 -0
- package/ios/Platform/Ringback.swift +61 -0
- package/ios/Platform/VoipPush.swift +174 -0
- package/ios/PoolseCalls.podspec +68 -0
- package/package.json +52 -3
- package/plugin/index.js +42 -0
- package/plugin/package.json +1 -0
- package/plugin/withCallDelegate.js +35 -0
- package/plugin/withLockscreenCalls.js +84 -0
- package/plugin/withNativeCallMedia.js +19 -0
- package/plugin/withPictureInPicture.js +28 -0
- package/plugin/withRingback.js +86 -0
- package/src/expo-modules-core.d.ts +16 -0
- package/src/index.ts +564 -0
- package/src/react-shims.d.ts +22 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import AVFoundation
|
|
2
|
+
import CallKit
|
|
3
|
+
import Foundation
|
|
4
|
+
import UIKit
|
|
5
|
+
|
|
6
|
+
/// What CallKit's provider actions mean to the call core. Implemented by
|
|
7
|
+
/// `CallCore`; absent (never configured) the controller only keeps
|
|
8
|
+
/// CallKit consistent and fails what it cannot honour.
|
|
9
|
+
protocol CallKitActions: AnyObject {
|
|
10
|
+
func callKitAnswer(callId: String)
|
|
11
|
+
func callKitEnd(callId: String)
|
|
12
|
+
func callKitSetMuted(callId: String, muted: Bool)
|
|
13
|
+
func callKitAudioActivated(_ session: AVAudioSession)
|
|
14
|
+
func callKitAudioDeactivated(_ session: AVAudioSession)
|
|
15
|
+
func callKitReset()
|
|
16
|
+
/// "Hold & Accept" on the call-waiting screen: true if `callId` is the
|
|
17
|
+
/// current call with another waiting (it will be ENDED, not held).
|
|
18
|
+
func callKitHoldForWaiting(callId: String) -> Bool
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// The iOS ring surface + call session: one CXProvider for the process.
|
|
22
|
+
///
|
|
23
|
+
/// CallKit is the source of truth for the system UI only; the call core
|
|
24
|
+
/// is the source of truth for the call. Every provider action is
|
|
25
|
+
/// forwarded to the core, and `CallCore` reconciles CallKit against each
|
|
26
|
+
/// final snapshot (answer started from JS, outgoing connected, every
|
|
27
|
+
/// terminal path) — so whatever ends a call, CallKit is told exactly once
|
|
28
|
+
/// with the right reason.
|
|
29
|
+
///
|
|
30
|
+
/// Main thread only (the provider delegate queue is main, PushKit's queue
|
|
31
|
+
/// is main, the runtime runs on main).
|
|
32
|
+
final class CallKitController: NSObject, CXProviderDelegate {
|
|
33
|
+
static let shared = CallKitController()
|
|
34
|
+
|
|
35
|
+
final class Tracked {
|
|
36
|
+
let uuid: UUID
|
|
37
|
+
var callId: String
|
|
38
|
+
let outgoing: Bool
|
|
39
|
+
var hasVideo: Bool
|
|
40
|
+
var title: String?
|
|
41
|
+
/// Answered (incoming) — by the CallKit UI or a request we made.
|
|
42
|
+
var answered = false
|
|
43
|
+
/// CallKit has been told it ended (or is being told).
|
|
44
|
+
var ended = false
|
|
45
|
+
var connectedReported = false
|
|
46
|
+
|
|
47
|
+
init(uuid: UUID, callId: String, outgoing: Bool, hasVideo: Bool, title: String?) {
|
|
48
|
+
self.uuid = uuid
|
|
49
|
+
self.callId = callId
|
|
50
|
+
self.outgoing = outgoing
|
|
51
|
+
self.hasVideo = hasVideo
|
|
52
|
+
self.title = title
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let provider: CXProvider
|
|
57
|
+
private let controller = CXCallController()
|
|
58
|
+
private var calls: [UUID: Tracked] = [:]
|
|
59
|
+
|
|
60
|
+
weak var actions: CallKitActions?
|
|
61
|
+
|
|
62
|
+
private override init() {
|
|
63
|
+
// init(localizedName:) is deprecated (iOS 14 reads the bundle's
|
|
64
|
+
// display name) but still honoured; the label is the product's.
|
|
65
|
+
let config = CXProviderConfiguration(localizedName: "Loop")
|
|
66
|
+
config.supportsVideo = true
|
|
67
|
+
// Two groups: room for a second call to ring beside the live one, so
|
|
68
|
+
// iOS presents its call-waiting screen (End & Accept / Decline). With
|
|
69
|
+
// one, a second call rang as a plain Accept/Decline that silently
|
|
70
|
+
// ended the first. There is never a second call in progress — Loop
|
|
71
|
+
// cannot hold: answering one ends the other (`callKitHoldForWaiting`).
|
|
72
|
+
config.maximumCallGroups = 2
|
|
73
|
+
config.maximumCallsPerCallGroup = 1
|
|
74
|
+
config.supportedHandleTypes = [.generic]
|
|
75
|
+
config.includesCallsInRecents = false
|
|
76
|
+
if let icon = UIImage(named: "CallKitIcon")?.pngData() { config.iconTemplateImageData = icon }
|
|
77
|
+
provider = CXProvider(configuration: config)
|
|
78
|
+
super.init()
|
|
79
|
+
provider.setDelegate(self, queue: nil) // nil = main queue
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// MARK: - lookup
|
|
83
|
+
|
|
84
|
+
func tracked(callId: String) -> Tracked? {
|
|
85
|
+
calls.values.first { $0.callId == callId && !$0.ended }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
var liveCalls: [Tracked] { calls.values.filter { !$0.ended } }
|
|
89
|
+
|
|
90
|
+
// MARK: - incoming
|
|
91
|
+
|
|
92
|
+
/// Report a live ring. MUST run synchronously inside PushKit's
|
|
93
|
+
/// didReceiveIncomingPush. A ring already reported (push redelivery,
|
|
94
|
+
/// or the runtime's SHOW_RING after the push path) is re-reported with
|
|
95
|
+
/// the SAME UUID: CallKit rejects it as a duplicate and nothing changes,
|
|
96
|
+
/// which still satisfies PushKit's "report a call for every push".
|
|
97
|
+
/// Put the shared session in the call's category BEFORE CallKit activates
|
|
98
|
+
/// it — at ring time, in the answer action, in the start action (Apple's
|
|
99
|
+
/// CallKit sample does the same). Setting the category does not activate
|
|
100
|
+
/// anything. Without it the session is still the default
|
|
101
|
+
/// SoloAmbientSound when CallKit activates it, and with the phone locked
|
|
102
|
+
/// and the app in the background iOS REFUSES that activation (seen in a
|
|
103
|
+
/// device log as an activate "early exit due to failure", -16980): no
|
|
104
|
+
/// didActivate, so no audio at all on a call answered with the app closed.
|
|
105
|
+
static func prepareSessionForCall(video: Bool) {
|
|
106
|
+
let s = AVAudioSession.sharedInstance()
|
|
107
|
+
let mode: AVAudioSession.Mode = video ? .videoChat : .voiceChat
|
|
108
|
+
guard s.category != .playAndRecord || s.mode != mode else { return }
|
|
109
|
+
do {
|
|
110
|
+
try s.setCategory(.playAndRecord, mode: mode, options: [.allowBluetooth, .allowBluetoothA2DP])
|
|
111
|
+
} catch {
|
|
112
|
+
NSLog("[PoolseCalls] pre-activation category failed: %@", "\(error)")
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
func reportIncoming(ring: RingContext) {
|
|
117
|
+
let hasVideo = ring.media == "video"
|
|
118
|
+
let title = ring.conversationName ?? ring.callerName
|
|
119
|
+
if let existing = calls.values.first(where: { $0.callId == ring.callId && !$0.ended }) {
|
|
120
|
+
provider.reportNewIncomingCall(with: existing.uuid, update: update(title: title, handle: ring.conversationId, video: hasVideo)) { _ in }
|
|
121
|
+
return
|
|
122
|
+
}
|
|
123
|
+
let t = Tracked(uuid: UUID(), callId: ring.callId, outgoing: false, hasVideo: hasVideo, title: title)
|
|
124
|
+
// A call waiting beside a live one must not touch the live call's
|
|
125
|
+
// session (a voice ring switched a video call's mode, and its route).
|
|
126
|
+
let beside = !liveCalls.isEmpty
|
|
127
|
+
calls[t.uuid] = t
|
|
128
|
+
pcTrace("CallKit report incoming \(ring.callId) beside=\(beside) live=\(liveCalls.count)")
|
|
129
|
+
if !beside { Self.prepareSessionForCall(video: hasVideo) }
|
|
130
|
+
provider.reportNewIncomingCall(with: t.uuid, update: update(title: title, handle: ring.conversationId, video: hasVideo)) { [weak self] error in
|
|
131
|
+
guard let error = error else { return }
|
|
132
|
+
pcTrace("CallKit refused incoming \(ring.callId): \(error.localizedDescription)")
|
|
133
|
+
NSLog("[PoolseCalls] CallKit refused incoming call %@: %@", ring.callId, error.localizedDescription)
|
|
134
|
+
// Refused (DND, blocked, busy group): CallKit holds nothing for it.
|
|
135
|
+
// End the ring in the core too — silently (no decline is sent), so
|
|
136
|
+
// the engine is not left RINGING_IN with no UI, counting as busy.
|
|
137
|
+
DispatchQueue.main.async {
|
|
138
|
+
t.ended = true
|
|
139
|
+
self?.calls.removeValue(forKey: t.uuid)
|
|
140
|
+
// A refused call-waiting report: the caller would ring on into
|
|
141
|
+
// a void — turn it away as busy instead.
|
|
142
|
+
if let core = CallCore.peek(), core.isWaiting(ring.callId) {
|
|
143
|
+
core.declineWaiting(callId: ring.callId)
|
|
144
|
+
} else {
|
|
145
|
+
CallCore.peek()?.onRingEnded(callId: ring.callId, reason: "cancelled")
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/// A push that must not ring (stale, duplicate of a resolved call,
|
|
152
|
+
/// unparseable, a control event): report it — iOS kills an app that
|
|
153
|
+
/// does not — then end it at once. A push for a call CallKit is still
|
|
154
|
+
/// showing reuses its UUID, so the live call is untouched.
|
|
155
|
+
func reportAndFail(callId: String?, title: String?) {
|
|
156
|
+
if let callId = callId, let existing = calls.values.first(where: { $0.callId == callId && !$0.ended }) {
|
|
157
|
+
provider.reportNewIncomingCall(with: existing.uuid, update: update(title: existing.title, handle: callId, video: existing.hasVideo)) { _ in }
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
let uuid = UUID()
|
|
161
|
+
provider.reportNewIncomingCall(with: uuid, update: update(title: title, handle: callId ?? "loop", video: false)) { [weak self] error in
|
|
162
|
+
if error == nil { self?.provider.reportCall(with: uuid, endedAt: nil, reason: .failed) }
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// MARK: - outgoing
|
|
167
|
+
|
|
168
|
+
/// Start an outgoing (placed or joined) call in CallKit. CallKit then
|
|
169
|
+
/// activates the audio session for it. `onFailure` runs if CallKit
|
|
170
|
+
/// refuses the transaction (the call proceeds without CallKit).
|
|
171
|
+
func startOutgoing(callId: String, handle: String, title: String?, video: Bool, onFailure: @escaping () -> Void) {
|
|
172
|
+
if tracked(callId: callId) != nil { return }
|
|
173
|
+
let t = Tracked(uuid: UUID(), callId: callId, outgoing: true, hasVideo: video, title: title)
|
|
174
|
+
calls[t.uuid] = t
|
|
175
|
+
let action = CXStartCallAction(call: t.uuid, handle: CXHandle(type: .generic, value: handle))
|
|
176
|
+
action.isVideo = video
|
|
177
|
+
action.contactIdentifier = title
|
|
178
|
+
controller.request(CXTransaction(action: action)) { [weak self] error in
|
|
179
|
+
DispatchQueue.main.async {
|
|
180
|
+
guard let error = error else {
|
|
181
|
+
if let title = t.title { self?.provider.reportCall(with: t.uuid, updated: self?.update(title: title, handle: handle, video: video) ?? CXCallUpdate()) }
|
|
182
|
+
return
|
|
183
|
+
}
|
|
184
|
+
NSLog("[PoolseCalls] CallKit start refused: %@", error.localizedDescription)
|
|
185
|
+
t.ended = true
|
|
186
|
+
self?.calls.removeValue(forKey: t.uuid)
|
|
187
|
+
onFailure()
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/// `pending:<conversation>` → the server-minted id: the same call.
|
|
193
|
+
func rename(from old: String, to new: String) {
|
|
194
|
+
tracked(callId: old)?.callId = new
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
func reportConnected(callId: String) {
|
|
198
|
+
guard let t = tracked(callId: callId), t.outgoing, !t.connectedReported else { return }
|
|
199
|
+
t.connectedReported = true
|
|
200
|
+
provider.reportOutgoingCall(with: t.uuid, connectedAt: nil)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
func updateTitle(callId: String, title: String?, video: Bool) {
|
|
204
|
+
guard let t = tracked(callId: callId), let title = title, title != t.title || video != t.hasVideo else { return }
|
|
205
|
+
t.title = title
|
|
206
|
+
t.hasVideo = video
|
|
207
|
+
provider.reportCall(with: t.uuid, updated: update(title: title, handle: nil, video: video))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// MARK: - requests from the app (JS buttons)
|
|
211
|
+
|
|
212
|
+
/// The app answered from its own UI: tell CallKit (it activates audio).
|
|
213
|
+
func requestAnswer(callId: String) {
|
|
214
|
+
guard let t = tracked(callId: callId), !t.outgoing, !t.answered else { return }
|
|
215
|
+
t.answered = true
|
|
216
|
+
controller.request(CXTransaction(action: CXAnswerCallAction(call: t.uuid))) { error in
|
|
217
|
+
if let error = error { NSLog("[PoolseCalls] CallKit answer refused: %@", error.localizedDescription) }
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
func requestMute(callId: String, muted: Bool) {
|
|
222
|
+
guard let t = tracked(callId: callId) else { return }
|
|
223
|
+
controller.request(CXTransaction(action: CXSetMutedCallAction(call: t.uuid, muted: muted))) { _ in }
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/// End a call in CallKit. `reason` nil = ended locally (a user action
|
|
227
|
+
/// in our UI) → an end transaction; otherwise the call ended remotely
|
|
228
|
+
/// or failed → reported with that reason.
|
|
229
|
+
func end(_ t: Tracked, reason: CXCallEndedReason?) {
|
|
230
|
+
guard !t.ended else { return }
|
|
231
|
+
t.ended = true
|
|
232
|
+
if let reason = reason {
|
|
233
|
+
provider.reportCall(with: t.uuid, endedAt: nil, reason: reason)
|
|
234
|
+
calls.removeValue(forKey: t.uuid)
|
|
235
|
+
} else {
|
|
236
|
+
controller.request(CXTransaction(action: CXEndCallAction(call: t.uuid))) { [weak self] error in
|
|
237
|
+
DispatchQueue.main.async {
|
|
238
|
+
if error != nil { self?.provider.reportCall(with: t.uuid, endedAt: nil, reason: .remoteEnded) }
|
|
239
|
+
self?.calls.removeValue(forKey: t.uuid)
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private func update(title: String?, handle: String?, video: Bool) -> CXCallUpdate {
|
|
246
|
+
let u = CXCallUpdate()
|
|
247
|
+
if let handle = handle { u.remoteHandle = CXHandle(type: .generic, value: handle) }
|
|
248
|
+
u.localizedCallerName = title ?? "Loop"
|
|
249
|
+
u.hasVideo = video
|
|
250
|
+
u.supportsHolding = false
|
|
251
|
+
u.supportsGrouping = false
|
|
252
|
+
u.supportsUngrouping = false
|
|
253
|
+
u.supportsDTMF = false
|
|
254
|
+
return u
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// MARK: - CXProviderDelegate
|
|
258
|
+
|
|
259
|
+
func providerDidReset(_ provider: CXProvider) {
|
|
260
|
+
for t in calls.values { t.ended = true }
|
|
261
|
+
calls.removeAll()
|
|
262
|
+
actions?.callKitReset()
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
|
|
266
|
+
let found = calls[action.callUUID]
|
|
267
|
+
pcTrace("CallKit answer action \(found?.callId ?? "unknown") live=\(liveCalls.count)")
|
|
268
|
+
guard let t = found, !t.ended else { action.fail(); return }
|
|
269
|
+
let fromUs = t.answered
|
|
270
|
+
t.answered = true
|
|
271
|
+
// Before fulfil: CallKit activates the session right after.
|
|
272
|
+
Self.prepareSessionForCall(video: t.hasVideo)
|
|
273
|
+
if !fromUs {
|
|
274
|
+
guard let actions = actions else { action.fail(); return }
|
|
275
|
+
actions.callKitAnswer(callId: t.callId)
|
|
276
|
+
}
|
|
277
|
+
action.fulfill()
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
|
|
281
|
+
pcTrace("CallKit end action \(calls[action.callUUID]?.callId ?? "unknown") live=\(liveCalls.count)")
|
|
282
|
+
guard let t = calls[action.callUUID] else { action.fulfill(); return }
|
|
283
|
+
let fromUs = t.ended
|
|
284
|
+
t.ended = true
|
|
285
|
+
calls.removeValue(forKey: t.uuid)
|
|
286
|
+
if !fromUs { actions?.callKitEnd(callId: t.callId) }
|
|
287
|
+
action.fulfill()
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
|
|
291
|
+
guard let t = calls[action.callUUID] else { action.fail(); return }
|
|
292
|
+
Self.prepareSessionForCall(video: t.hasVideo)
|
|
293
|
+
provider.reportOutgoingCall(with: action.callUUID, startedConnectingAt: nil)
|
|
294
|
+
action.fulfill()
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
|
|
298
|
+
if let t = calls[action.callUUID] { actions?.callKitSetMuted(callId: t.callId, muted: action.isMuted) }
|
|
299
|
+
action.fulfill()
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
func provider(_ provider: CXProvider, perform action: CXSetHeldCallAction) {
|
|
303
|
+
let t = calls[action.callUUID]
|
|
304
|
+
pcTrace("CallKit held action \(t?.callId ?? "unknown") onHold=\(action.isOnHold)")
|
|
305
|
+
// No hold, one call at a time. "Hold & Accept" on the call-waiting
|
|
306
|
+
// screen holds this call and answers the other: here the answer that
|
|
307
|
+
// follows ENDS this one (end-and-answer), so the hold is honoured as
|
|
308
|
+
// that. Already gone (the answer came first and ended it): nothing to do.
|
|
309
|
+
guard let t = t else { action.fulfill(); return }
|
|
310
|
+
if action.isOnHold, actions?.callKitHoldForWaiting(callId: t.callId) == true {
|
|
311
|
+
action.fulfill()
|
|
312
|
+
} else {
|
|
313
|
+
action.fail()
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
|
|
318
|
+
pcTrace("CallKit didActivate \(pcSessionDescription(audioSession))")
|
|
319
|
+
actions?.callKitAudioActivated(audioSession)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
|
|
323
|
+
pcTrace("CallKit didDeactivate")
|
|
324
|
+
actions?.callKitAudioDeactivated(audioSession)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import AVKit
|
|
2
|
+
#if canImport(LiveKitClient)
|
|
3
|
+
import LiveKitClient // CocoaPods module name
|
|
4
|
+
#else
|
|
5
|
+
import LiveKit // SwiftPM module name
|
|
6
|
+
#endif
|
|
7
|
+
import UIKit
|
|
8
|
+
|
|
9
|
+
/// Video-call picture-in-picture on iOS (15+): leave the app during a
|
|
10
|
+
/// video call and the other person keeps playing in a floating window,
|
|
11
|
+
/// as in FaceTime.
|
|
12
|
+
///
|
|
13
|
+
/// iOS needs two things for it. A SOURCE view inside the app — the
|
|
14
|
+
/// `<CallVideoView pictureInPicture>` on screen, which the window grows
|
|
15
|
+
/// out of and shrinks back into — and the CONTENT, a view controller iOS
|
|
16
|
+
/// moves into its own window. The content draws the same participant
|
|
17
|
+
/// through its own surface, so it keeps rendering while the app's views
|
|
18
|
+
/// are off screen; it uses LiveKit's sample-buffer renderer, the one iOS
|
|
19
|
+
/// composites into a PiP window (a Metal view stays black there).
|
|
20
|
+
///
|
|
21
|
+
/// Armed by the host (`setPictureInPictureEnabled`) only while there is a
|
|
22
|
+
/// picture worth floating. Main thread only.
|
|
23
|
+
final class CallPictureInPicture: NSObject, AVPictureInPictureControllerDelegate {
|
|
24
|
+
static let shared = CallPictureInPicture()
|
|
25
|
+
|
|
26
|
+
/// Fires with true when the window opens, false when it closes.
|
|
27
|
+
static var listener: ((Bool) -> Void)?
|
|
28
|
+
|
|
29
|
+
private(set) var active = false
|
|
30
|
+
private var enabled = false
|
|
31
|
+
private weak var sourceView: UIView?
|
|
32
|
+
private var participantId: String?
|
|
33
|
+
|
|
34
|
+
private var controller: AVPictureInPictureController?
|
|
35
|
+
private var content: UIViewController?
|
|
36
|
+
private var surface: LiveKitVideoSurface?
|
|
37
|
+
|
|
38
|
+
private override init() { super.init() }
|
|
39
|
+
|
|
40
|
+
static var supported: Bool {
|
|
41
|
+
if #available(iOS 15.0, *) { return AVPictureInPictureController.isPictureInPictureSupported() }
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// The host turns it on while a video call shows someone's picture.
|
|
46
|
+
func setEnabled(_ on: Bool) {
|
|
47
|
+
enabled = on
|
|
48
|
+
rebuild()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/// The on-screen view to grow the window out of, and who it shows.
|
|
52
|
+
func setSource(_ view: UIView?, participantId: String?) {
|
|
53
|
+
let sameView = view === sourceView
|
|
54
|
+
if sameView, participantId == self.participantId { return }
|
|
55
|
+
sourceView = view
|
|
56
|
+
self.participantId = participantId
|
|
57
|
+
if sameView, view != nil, controller != nil, let id = participantId {
|
|
58
|
+
// Same source, someone else (a 1-on-1 swap): just redirect the picture.
|
|
59
|
+
surface?.configure(participantId: id, local: false, mirror: nil, fit: "cover")
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
rebuild()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/// A source view going away: drop it only if it is still the source.
|
|
66
|
+
func clearSource(_ view: UIView) {
|
|
67
|
+
if sourceView === view { setSource(nil, participantId: nil) }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private func rebuild() {
|
|
71
|
+
guard Self.supported, #available(iOS 15.0, *) else { return }
|
|
72
|
+
guard enabled, let source = sourceView, let id = participantId, let core = CallCore.peek() else {
|
|
73
|
+
tearDown()
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
if let controller = controller, controller.contentSource?.activeVideoCallSourceView === source {
|
|
77
|
+
surface?.configure(participantId: id, local: false, mirror: nil, fit: "cover")
|
|
78
|
+
controller.canStartPictureInPictureAutomaticallyFromInline = true
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
tearDown()
|
|
82
|
+
|
|
83
|
+
let vc = AVPictureInPictureVideoCallViewController()
|
|
84
|
+
// Portrait, the shape of a phone camera; iOS sizes the window from it.
|
|
85
|
+
vc.preferredContentSize = CGSize(width: 9, height: 16)
|
|
86
|
+
vc.view.backgroundColor = .black
|
|
87
|
+
let s = core.createVideoSurface(renderMode: .sampleBuffer)
|
|
88
|
+
s.frame = vc.view.bounds
|
|
89
|
+
s.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
90
|
+
vc.view.addSubview(s)
|
|
91
|
+
s.configure(participantId: id, local: false, mirror: nil, fit: "cover")
|
|
92
|
+
|
|
93
|
+
let c = AVPictureInPictureController(
|
|
94
|
+
contentSource: .init(activeVideoCallSourceView: source, contentViewController: vc))
|
|
95
|
+
c.canStartPictureInPictureAutomaticallyFromInline = true
|
|
96
|
+
c.delegate = self
|
|
97
|
+
controller = c
|
|
98
|
+
content = vc
|
|
99
|
+
surface = s
|
|
100
|
+
pcTrace("pip armed participant=\(id)")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private func tearDown() {
|
|
104
|
+
guard let c = controller else { return }
|
|
105
|
+
if c.isPictureInPictureActive { c.stopPictureInPicture() }
|
|
106
|
+
c.delegate = nil
|
|
107
|
+
controller = nil
|
|
108
|
+
content = nil
|
|
109
|
+
surface?.release()
|
|
110
|
+
surface?.removeFromSuperview()
|
|
111
|
+
surface = nil
|
|
112
|
+
setActive(false)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private func setActive(_ on: Bool) {
|
|
116
|
+
guard active != on else { return }
|
|
117
|
+
active = on
|
|
118
|
+
CallPictureInPicture.listener?(on)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// MARK: - AVPictureInPictureControllerDelegate
|
|
122
|
+
|
|
123
|
+
func pictureInPictureControllerDidStartPictureInPicture(_ c: AVPictureInPictureController) {
|
|
124
|
+
pcTrace("pip started")
|
|
125
|
+
setActive(true)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
func pictureInPictureControllerDidStopPictureInPicture(_ c: AVPictureInPictureController) {
|
|
129
|
+
pcTrace("pip stopped")
|
|
130
|
+
setActive(false)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
func pictureInPictureController(_ c: AVPictureInPictureController,
|
|
134
|
+
failedToStartPictureInPictureWithError error: Error) {
|
|
135
|
+
pcTrace("pip failed: \(error.localizedDescription)")
|
|
136
|
+
setActive(false)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Back into the app: the call screen is still there underneath.
|
|
140
|
+
func pictureInPictureController(_ c: AVPictureInPictureController,
|
|
141
|
+
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler
|
|
142
|
+
completionHandler: @escaping (Bool) -> Void) {
|
|
143
|
+
completionHandler(true)
|
|
144
|
+
}
|
|
145
|
+
}
|