Update LumaCallEngine.swift
iOS CI / Build and Test SwiftUI App (push) Canceled after 0s
Build Unsigned iOS and macOS Apps / Build Unsigned iOS IPA (push) Canceled after 0s
Build Unsigned iOS and macOS Apps / Build macOS ZIP (push) Canceled after 0s

This commit is contained in:
wt committed 2026-08-10 16:54:23 +07:00
1 parent ee9ace7615
commit 2380005593
1 file changed
+266 -210
+266 -210
View File
@@ -4,15 +4,15 @@ import Martin
import WebRTC import WebRTC
#if os(iOS) #if os(iOS)
import UIKit import UIKit
#elseif os(macOS) #elseif os(macOS)
import AppKit import AppKit
#endif #endif
/// Owns one active 1:1 Jingle RTP session and bridges Martin to WebRTC. /// Owns one active 1:1 Jingle RTP session and bridges Martin to WebRTC.
/// Incoming calls are available while the XMPP connection is alive; production /// Incoming calls are available while the XMPP connection is alive; production
/// wake-up for a terminated iOS app still requires VoIP push infrastructure. /// wake-up for a terminated iOS app still requires VoIP push infrastructure.
final class LumaCallEngine: NSObject { final class LumaCallEngine: NSObject, @unchecked Sendable {
var snapshotHandler: (@MainActor (CallSnapshot?) -> Void)? var snapshotHandler: (@MainActor (CallSnapshot?) -> Void)?
var historyHandler: (@MainActor (CallHistoryEntry) -> Void)? var historyHandler: (@MainActor (CallHistoryEntry) -> Void)?
var errorHandler: (@MainActor (String) -> Void)? var errorHandler: (@MainActor (String) -> Void)?
@@ -76,15 +76,16 @@ final class LumaCallEngine: NSObject {
media: media, media: media,
client: client client: client
) )
#if DEBUG #if DEBUG
print( print(
"Luma Call: selected \(destination.jid.stringValue) via " "Luma Call: selected \(destination.jid.stringValue) via "
+ (destination.usesMessageInitiation ? "JMI" : "direct Jingle") + (destination.usesMessageInitiation ? "JMI" : "direct Jingle")
+ " for \(media.map(\.rawValue).sorted().joined(separator: "+"))." + " for \(media.map(\.rawValue).sorted().joined(separator: "+"))."
) )
#endif #endif
let sid = UUID().uuidString.lowercased() let sid = UUID().uuidString.lowercased()
let initiationType: JingleSessionInitiationType = destination.usesMessageInitiation let initiationType: JingleSessionInitiationType =
destination.usesMessageInitiation
? .message ? .message
: .iq : .iq
let session = self.makeSession( let session = self.makeSession(
@@ -108,14 +109,16 @@ final class LumaCallEngine: NSObject {
if destination.usesMessageInitiation { if destination.usesMessageInitiation {
Task { Task {
do { do {
try await session.initiate(descriptions: media try await session.initiate(
.sorted { $0.rawValue < $1.rawValue } descriptions:
.map { media
Jingle.MessageInitiationAction.Description( .sorted { $0.rawValue < $1.rawValue }
xmlns: "urn:xmpp:jingle:apps:rtp:1", .map {
media: $0.rawValue Jingle.MessageInitiationAction.Description(
) xmlns: "urn:xmpp:jingle:apps:rtp:1",
}) media: $0.rawValue
)
})
completion(.success(())) completion(.success(()))
} catch { } catch {
self.queue.async { self.queue.async {
@@ -150,8 +153,9 @@ final class LumaCallEngine: NSObject {
func answerCall(completion: @escaping (Result<Void, Error>) -> Void) { func answerCall(completion: @escaping (Result<Void, Error>) -> Void) {
queue.async { queue.async {
guard let call = self.activeCall, guard let call = self.activeCall,
call.direction == .incoming, call.direction == .incoming,
call.phase == .ringing else { call.phase == .ringing
else {
completion(.failure(LumaCallError.noIncomingCall)) completion(.failure(LumaCallError.noIncomingCall))
return return
} }
@@ -324,8 +328,9 @@ final class LumaCallEngine: NSObject {
let capabilities = client.module(.caps) let capabilities = client.module(.caps)
let candidates = presences.compactMap(\.from).filter { jid in let candidates = presences.compactMap(\.from).filter { jid in
guard capabilities.isFeatureSupported(JingleModule.XMLNS, by: jid), guard capabilities.isFeatureSupported(JingleModule.XMLNS, by: jid),
capabilities.isFeatureSupported(Jingle.Transport.ICEUDPTransport.XMLNS, by: jid), capabilities.isFeatureSupported(Jingle.Transport.ICEUDPTransport.XMLNS, by: jid),
capabilities.isFeatureSupported("urn:xmpp:jingle:apps:rtp:audio", by: jid) else { capabilities.isFeatureSupported("urn:xmpp:jingle:apps:rtp:audio", by: jid)
else {
return false return false
} }
return !media.contains(.video) return !media.contains(.video)
@@ -375,9 +380,9 @@ final class LumaCallEngine: NSObject {
timeoutWorkItem?.cancel() timeoutWorkItem?.cancel()
let item = DispatchWorkItem { [weak self] in let item = DispatchWorkItem { [weak self] in
guard let self, self.activeCall?.id == callID else { return } guard let self, self.activeCall?.id == callID else { return }
#if DEBUG #if DEBUG
print("Luma Call: timeout during \(stage) after \(Int(delay)) seconds.") print("Luma Call: timeout during \(stage) after \(Int(delay)) seconds.")
#endif #endif
self.fail(self.activeCall, error: LumaCallError.timedOut) self.fail(self.activeCall, error: LumaCallError.timedOut)
} }
timeoutWorkItem = item timeoutWorkItem = item
@@ -387,9 +392,9 @@ final class LumaCallEngine: NSObject {
private func fail(_ call: LumaWebRTCCall?, error: Error) { private func fail(_ call: LumaWebRTCCall?, error: Error) {
guard call == nil || activeCall === call else { return } guard call == nil || activeCall === call else { return }
let text = (error as? LocalizedError)?.errorDescription ?? error.localizedDescription let text = (error as? LocalizedError)?.errorDescription ?? error.localizedDescription
#if DEBUG #if DEBUG
print("Luma Call: failure: \(text)") print("Luma Call: failure: \(text)")
#endif #endif
report(error) report(error)
let cause: CallTerminationCause let cause: CallTerminationCause
if let callError = error as? LumaCallError, case .timedOut = callError { if let callError = error as? LumaCallError, case .timedOut = callError {
@@ -424,14 +429,14 @@ final class LumaCallEngine: NSObject {
publish(nil) publish(nil)
return return
} }
#if DEBUG #if DEBUG
print( print(
"Luma Call: closing sid=\(call.session.sid) " "Luma Call: closing sid=\(call.session.sid) "
+ "direction=\(String(describing: call.direction)) " + "direction=\(String(describing: call.direction)) "
+ "phase=\(String(describing: call.phase)) " + "phase=\(String(describing: call.phase)) "
+ "peer=\(call.session.jid.stringValue) trigger=\(trigger)." + "peer=\(call.session.jid.stringValue) trigger=\(trigger)."
) )
#endif #endif
let endedAt = Date() let endedAt = Date()
let outcome = CallHistoryPolicy.outcome( let outcome = CallHistoryPolicy.outcome(
direction: call.direction, direction: call.direction,
@@ -515,20 +520,22 @@ extension LumaCallEngine: JingleSessionManager {
case .proceed(let id): case .proceed(let id):
guard let session = sessions[id], guard let session = sessions[id],
let call = activeCall, let call = activeCall,
call.session === session, call.session === session,
call.direction == .outgoing, call.direction == .outgoing,
call.phase == .ringing, call.phase == .ringing,
session.pinPeerResource(jid) else { session.pinPeerResource(jid)
#if DEBUG else {
print("Luma Jingle: ignored duplicate or foreign proceed for session \(id).") #if DEBUG
#endif print(
"Luma Jingle: ignored duplicate or foreign proceed for session \(id).")
#endif
return return
} }
call.phase = .connecting call.phase = .connecting
#if DEBUG #if DEBUG
print("Luma Jingle: proceed from \(jid.stringValue) for session \(id).") print("Luma Jingle: proceed from \(jid.stringValue) for session \(id).")
#endif #endif
scheduleTimeout( scheduleTimeout(
for: call.id, for: call.id,
after: 90, after: 90,
@@ -551,7 +558,8 @@ extension LumaCallEngine: JingleSessionManager {
case .retract(let id): case .retract(let id):
guard let session = sessions[id], guard let session = sessions[id],
session.matchesPeerEndpoint(jid) else { return } session.matchesPeerEndpoint(jid)
else { return }
finishActiveCall( finishActiveCall(
cause: .remoteCancelled, cause: .remoteCancelled,
sendTermination: false, sendTermination: false,
@@ -560,16 +568,17 @@ extension LumaCallEngine: JingleSessionManager {
case .reject(let id): case .reject(let id):
guard let session = sessions[id], guard let session = sessions[id],
let call = activeCall, let call = activeCall,
call.session === session, call.session === session,
session.matchesPeerEndpoint(jid) else { return } session.matchesPeerEndpoint(jid)
else { return }
// A JMI proposal is delivered to every online resource. Once a // A JMI proposal is delivered to every online resource. Once a
// resource has proceeded, a delayed reject from a sibling must // resource has proceeded, a delayed reject from a sibling must
// not tear down the session that already owns the call. // not tear down the session that already owns the call.
guard call.phase == .ringing else { guard call.phase == .ringing else {
#if DEBUG #if DEBUG
print("Luma Jingle: ignored late reject after proceed for session \(id).") print("Luma Jingle: ignored late reject after proceed for session \(id).")
#endif #endif
return return
} }
finishActiveCall( finishActiveCall(
@@ -599,7 +608,8 @@ extension LumaCallEngine: JingleSessionManager {
bundle: [String]? bundle: [String]?
) throws { ) throws {
try performSync { try performSync {
let media = Set(contents.compactMap { CallMedia(rawValue: $0.description?.media ?? "") }) let media = Set(
contents.compactMap { CallMedia(rawValue: $0.description?.media ?? "") })
guard media.contains(.audio) else { throw XMPPError.feature_not_implemented } guard media.contains(.audio) else { throw XMPPError.feature_not_implemented }
if let existing = sessions[sid] { if let existing = sessions[sid] {
@@ -616,7 +626,9 @@ extension LumaCallEngine: JingleSessionManager {
} }
return return
} }
guard activeCall == nil else { throw XMPPError.resource_constraint("Другой звонок уже активен") } guard activeCall == nil else {
throw XMPPError.resource_constraint("Другой звонок уже активен")
}
let session = makeSession( let session = makeSession(
context: context, context: context,
@@ -648,21 +660,22 @@ extension LumaCallEngine: JingleSessionManager {
) throws { ) throws {
try performSync { try performSync {
guard let session = sessions[sid], guard let session = sessions[sid],
session.pinPeerResource(jid) else { session.pinPeerResource(jid)
else {
throw XMPPError.item_not_found throw XMPPError.item_not_found
} }
// JMI may initially address a bare JID. session-accept is the first // JMI may initially address a bare JID. session-accept is the first
// IQ guaranteed to identify the resource that owns this Jingle // IQ guaranteed to identify the resource that owns this Jingle
// session, so keep every later transport-info stanza on it. // session, so keep every later transport-info stanza on it.
#if DEBUG #if DEBUG
let acceptedMedia = contents.map { let acceptedMedia = contents.map {
"\($0.name):\($0.description?.media ?? "unknown")" "\($0.name):\($0.description?.media ?? "unknown")"
}.joined(separator: ",") }.joined(separator: ",")
print( print(
"Luma Jingle: session-accept from \(jid.stringValue) " "Luma Jingle: session-accept from \(jid.stringValue) "
+ "sid=\(sid) contents=[\(acceptedMedia)]." + "sid=\(sid) contents=[\(acceptedMedia)]."
) )
#endif #endif
session.accepted(contents: contents, bundle: bundle) session.accepted(contents: contents, bundle: bundle)
if let call = activeCall, call.session === session { if let call = activeCall, call.session === session {
call.phase = .connecting call.phase = .connecting
@@ -679,7 +692,8 @@ extension LumaCallEngine: JingleSessionManager {
func sessionTerminated(for context: Context, with jid: JID, sid: String) throws { func sessionTerminated(for context: Context, with jid: JID, sid: String) throws {
try performSync { try performSync {
guard let session = sessions[sid], guard let session = sessions[sid],
session.matchesPeerEndpoint(jid) else { session.matchesPeerEndpoint(jid)
else {
throw XMPPError.item_not_found throw XMPPError.item_not_found
} }
if activeCall?.session.sid == sid { if activeCall?.session.sid == sid {
@@ -703,7 +717,8 @@ extension LumaCallEngine: JingleSessionManager {
) throws { ) throws {
try performSync { try performSync {
guard let session = sessions[sid], guard let session = sessions[sid],
session.pinPeerResource(jid) else { session.pinPeerResource(jid)
else {
throw XMPPError.item_not_found throw XMPPError.item_not_found
} }
// Also learn the endpoint from an early candidate sent by the // Also learn the endpoint from an early candidate sent by the
@@ -728,7 +743,8 @@ extension LumaCallEngine: JingleSessionManager {
) throws { ) throws {
try performSync { try performSync {
guard let session = sessions[sid], guard let session = sessions[sid],
session.matchesPeerEndpoint(jid) else { session.matchesPeerEndpoint(jid)
else {
throw XMPPError.item_not_found throw XMPPError.item_not_found
} }
session.contentModified(action: action, contents: contents, bundle: bundle) session.contentModified(action: action, contents: contents, bundle: bundle)
@@ -743,7 +759,8 @@ extension LumaCallEngine: JingleSessionManager {
) throws { ) throws {
try performSync { try performSync {
guard let session = sessions[sid], guard let session = sessions[sid],
session.matchesPeerEndpoint(jid) else { session.matchesPeerEndpoint(jid)
else {
throw XMPPError.item_not_found throw XMPPError.item_not_found
} }
session.sessionInfoReceived(info: info) session.sessionInfoReceived(info: info)
@@ -789,7 +806,8 @@ private final class LumaJingleSession: JingleSession {
terminate(reason: reason) terminate(reason: reason)
return return
} }
let action: Jingle.MessageInitiationAction = role == .initiator let action: Jingle.MessageInitiationAction =
role == .initiator
? .retract(id: sid) ? .retract(id: sid)
: .reject(id: sid) : .reject(id: sid)
owningContext?.module(.jingle).sendMessageInitiation(action: action, to: jid) owningContext?.module(.jingle).sendMessageInitiation(action: action, to: jid)
@@ -806,9 +824,9 @@ private final class LumaJingleSession: JingleSession {
if jid.resource != nil { if jid.resource != nil {
return peer == jid return peer == jid
} }
#if DEBUG #if DEBUG
print("Luma Jingle: pinned session \(sid) to resource \(peer.stringValue).") print("Luma Jingle: pinned session \(sid) to resource \(peer.stringValue).")
#endif #endif
accepted(by: peer) accepted(by: peer)
return true return true
} }
@@ -871,7 +889,7 @@ private final class LumaJingleSession: JingleSession {
// MARK: - Live WebRTC call // MARK: - Live WebRTC call
private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate { private final class LumaWebRTCCall: NSObject, @unchecked Sendable, RTCPeerConnectionDelegate {
let id = UUID() let id = UUID()
let startedAt = Date() let startedAt = Date()
unowned let engine: LumaCallEngine unowned let engine: LumaCallEngine
@@ -901,7 +919,7 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
private var connectionFailureWorkItem: DispatchWorkItem? private var connectionFailureWorkItem: DispatchWorkItem?
private var isPreparingPeerConnection = false private var isPreparingPeerConnection = false
private var prepareCallbacks: [(Result<Void, Error>) -> Void] = [] private var prepareCallbacks: [(Result<Void, Error>) -> Void] = []
private let webRTCSID = String(UInt64.random(in: UInt64.min ... UInt64.max)) private let webRTCSID = String(UInt64.random(in: UInt64.min...UInt64.max))
init( init(
engine: LumaCallEngine, engine: LumaCallEngine,
@@ -1001,22 +1019,25 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
} }
func configureSpeaker(_ enabled: Bool) throws { func configureSpeaker(_ enabled: Bool) throws {
#if os(iOS) #if os(iOS)
let audioSession = AVAudioSession.sharedInstance() let audioSession = AVAudioSession.sharedInstance()
try audioSession.overrideOutputAudioPort(enabled ? .speaker : .none) try audioSession.overrideOutputAudioPort(enabled ? .speaker : .none)
try audioSession.setActive(true) try audioSession.setActive(true)
#else #else
_ = enabled _ = enabled
#endif #endif
} }
func switchCamera() { func switchCamera() {
guard let capturer = videoCapturer else { return } guard let capturer = videoCapturer else { return }
let currentPosition = RTCCameraVideoCapturer.captureDevices() let currentPosition =
RTCCameraVideoCapturer.captureDevices()
.first(where: { $0.uniqueID == cameraDeviceID })?.position ?? .front .first(where: { $0.uniqueID == cameraDeviceID })?.position ?? .front
guard let device = RTCCameraVideoCapturer.captureDevices().first(where: { guard
$0.position != currentPosition && $0.position != .unspecified let device = RTCCameraVideoCapturer.captureDevices().first(where: {
}), let format = Self.preferredFormat(for: device) else { return } $0.position != currentPosition && $0.position != .unspecified
}), let format = Self.preferredFormat(for: device)
else { return }
cameraDeviceID = device.uniqueID cameraDeviceID = device.uniqueID
capturer.startCapture(with: device, format: format, fps: Self.preferredFPS(for: format)) capturer.startCapture(with: device, format: format, fps: Self.preferredFPS(for: format))
} }
@@ -1032,12 +1053,12 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
localAudioTrack = nil localAudioTrack = nil
localVideoTrack = nil localVideoTrack = nil
remoteVideoTrack = nil remoteVideoTrack = nil
#if os(iOS) #if os(iOS)
try? AVAudioSession.sharedInstance().setActive( try? AVAudioSession.sharedInstance().setActive(
false, false,
options: .notifyOthersOnDeactivation options: .notifyOthersOnDeactivation
) )
#endif #endif
} }
private func createPeerConnection(iceServers: [RTCIceServer]) throws { private func createPeerConnection(iceServers: [RTCIceServer]) throws {
@@ -1071,19 +1092,19 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
delegate: self delegate: self
) { ) {
createdPeerConnection = candidate createdPeerConnection = candidate
#if DEBUG #if DEBUG
if index > 0 { if index > 0 {
print("Luma WebRTC: RTCConfiguration recovered with \(attempt.name).") print("Luma WebRTC: RTCConfiguration recovered with \(attempt.name).")
} }
#endif #endif
break break
} }
#if DEBUG #if DEBUG
print( print(
"Luma WebRTC: RTCConfiguration rejected " "Luma WebRTC: RTCConfiguration rejected "
+ "\(attempt.name) (\(attempt.servers.count) ICE server(s))." + "\(attempt.name) (\(attempt.servers.count) ICE server(s))."
) )
#endif #endif
} }
guard let peerConnection = createdPeerConnection else { guard let peerConnection = createdPeerConnection else {
@@ -1091,22 +1112,22 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
} }
self.peerConnection = peerConnection self.peerConnection = peerConnection
#if os(iOS) #if os(iOS)
let audioSession = AVAudioSession.sharedInstance() let audioSession = AVAudioSession.sharedInstance()
let audioOptions: AVAudioSession.CategoryOptions = [ let audioOptions: AVAudioSession.CategoryOptions = [
.allowBluetoothHFP, .allowBluetoothHFP,
.allowBluetoothA2DP .allowBluetoothA2DP,
] ]
try audioSession.setCategory( try audioSession.setCategory(
.playAndRecord, .playAndRecord,
mode: media.contains(.video) ? .videoChat : .voiceChat, mode: media.contains(.video) ? .videoChat : .voiceChat,
options: audioOptions options: audioOptions
) )
try audioSession.setActive(true) try audioSession.setActive(true)
if media.contains(.video) { if media.contains(.video) {
try audioSession.overrideOutputAudioPort(.speaker) try audioSession.overrideOutputAudioPort(.speaker)
} }
#endif #endif
let audioConstraints = RTCMediaConstraints( let audioConstraints = RTCMediaConstraints(
mandatoryConstraints: nil, mandatoryConstraints: nil,
@@ -1192,9 +1213,11 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
guard let self else { return } guard let self else { return }
self.engine.performOnQueue { self.engine.performOnQueue {
if let error { if let error {
#if DEBUG #if DEBUG
print("Luma WebRTC: setRemoteDescription failed: \(error.localizedDescription)") print(
#endif "Luma WebRTC: setRemoteDescription failed: \(error.localizedDescription)"
)
#endif
self.engine.callDidFail(self, error: error) self.engine.callDidFail(self, error: error)
return return
} }
@@ -1209,7 +1232,8 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
// both the answer and the exact full JID of its owner. // both the answer and the exact full JID of its owner.
self.sendLocalCandidates() self.sendLocalCandidates()
if peerConnection.signalingState == .haveRemoteOffer { if peerConnection.signalingState == .haveRemoteOffer {
self.generateDescription(type: .answer, peerConnection: peerConnection) { result in self.generateDescription(type: .answer, peerConnection: peerConnection) {
result in
switch result { switch result {
case .success(let local): case .success(let local):
Task { Task {
@@ -1266,11 +1290,13 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
let martinSDP = CallSDPNormalization.martinParseInput( let martinSDP = CallSDPNormalization.martinParseInput(
description.sdp description.sdp
) )
guard let (sdp, _) = SDP.parse( guard
sdpString: martinSDP, let (sdp, _) = SDP.parse(
creatorProvider: self.session.contentCreator(of:), sdpString: martinSDP,
localRole: self.session.role creatorProvider: self.session.contentCreator(of:),
) else { localRole: self.session.role
)
else {
completion(.failure(LumaCallError.sdpGenerationFailed)) completion(.failure(LumaCallError.sdpGenerationFailed))
return return
} }
@@ -1280,14 +1306,14 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
completion(.failure(LumaCallError.incompatibleLocalDescription)) completion(.failure(LumaCallError.incompatibleLocalDescription))
return return
} }
#if DEBUG #if DEBUG
if fidStatus.total > 0 { if fidStatus.total > 0 {
print( print(
"Luma SDP: verified \(fidStatus.total) FID group(s); " "Luma SDP: verified \(fidStatus.total) FID group(s); "
+ "repaired \(repaired.repairedCount) source msid(s)." + "repaired \(repaired.repairedCount) source msid(s)."
) )
} }
#endif #endif
self.localSDP = repaired.sdp self.localSDP = repaired.sdp
completion(.success(repaired.sdp)) completion(.success(repaired.sdp))
} }
@@ -1307,30 +1333,33 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
// until the remote SDP arrives from that concrete full JID; otherwise // until the remote SDP arrives from that concrete full JID; otherwise
// Prosody can route transport-info to another resource, which correctly // Prosody can route transport-info to another resource, which correctly
// responds with item-not-found for the SID. // responds with item-not-found for the SID.
guard CallCandidateDispatchGate.canSend( guard
initialDescriptionWasSignaled: initialDescriptionWasSignaled, CallCandidateDispatchGate.canSend(
hasLocalDescription: localSDP != nil, initialDescriptionWasSignaled: initialDescriptionWasSignaled,
hasRemoteDescription: remoteSDP != nil, hasLocalDescription: localSDP != nil,
hasFullPeerJID: session.jid.resource != nil hasRemoteDescription: remoteSDP != nil,
), let localSDP else { return } hasFullPeerJID: session.jid.resource != nil
), let localSDP
else { return }
let candidates = localCandidates let candidates = localCandidates
localCandidates.removeAll() localCandidates.removeAll()
#if DEBUG #if DEBUG
if !candidates.isEmpty { if !candidates.isEmpty {
print( print(
"Luma Jingle: sending \(candidates.count) ICE candidate(s) " "Luma Jingle: sending \(candidates.count) ICE candidate(s) "
+ "to \(session.jid.stringValue) sid=\(session.sid)." + "to \(session.jid.stringValue) sid=\(session.sid)."
) )
} }
#endif #endif
for candidate in candidates { for candidate in candidates {
let line = candidate.sdp.hasPrefix("a=") ? candidate.sdp : "a=\(candidate.sdp)" let line = candidate.sdp.hasPrefix("a=") ? candidate.sdp : "a=\(candidate.sdp)"
guard let jingleCandidate = Jingle.Transport.ICEUDPTransport.Candidate(fromSDP: line), guard let jingleCandidate = Jingle.Transport.ICEUDPTransport.Candidate(fromSDP: line),
let contentName = candidate.sdpMid, let contentName = candidate.sdpMid,
let content = localSDP.contents.first(where: { $0.name == contentName }), let content = localSDP.contents.first(where: { $0.name == contentName }),
let transport = content.transports.first(where: { let transport = content.transports.first(where: {
$0 is Jingle.Transport.ICEUDPTransport $0 is Jingle.Transport.ICEUDPTransport
}) as? Jingle.Transport.ICEUDPTransport else { continue } }) as? Jingle.Transport.ICEUDPTransport
else { continue }
_ = session.transportInfo( _ = session.transportInfo(
contentName: contentName, contentName: contentName,
transport: Jingle.Transport.ICEUDPTransport( transport: Jingle.Transport.ICEUDPTransport(
@@ -1356,11 +1385,13 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
sdpMid: contentName sdpMid: contentName
), ),
completionHandler: { error in completionHandler: { error in
#if DEBUG #if DEBUG
if let error { if let error {
print("Luma WebRTC: remote ICE candidate rejected: \(error.localizedDescription)") print(
} "Luma WebRTC: remote ICE candidate rejected: \(error.localizedDescription)"
#endif )
}
#endif
} }
) )
} }
@@ -1372,17 +1403,20 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
let expectedNames = localSDP.contents.map(\.name) let expectedNames = localSDP.contents.map(\.name)
let receivedNames = sdp.contents.map(\.name) let receivedNames = sdp.contents.map(\.name)
guard let indices = CallSDPOrdering.answerIndices( guard
localOrder: expectedNames, let indices = CallSDPOrdering.answerIndices(
remoteOrder: receivedNames localOrder: expectedNames,
) else { remoteOrder: receivedNames
)
else {
throw LumaCallError.incompatibleRemoteDescription throw LumaCallError.incompatibleRemoteDescription
} }
let normalizedBundle: [String]? let normalizedBundle: [String]?
if let bundle = sdp.bundle { if let bundle = sdp.bundle {
let bundleNames = Set(bundle) let bundleNames = Set(bundle)
guard bundleNames.count == bundle.count, guard bundleNames.count == bundle.count,
bundleNames.isSubset(of: Set(receivedNames)) else { bundleNames.isSubset(of: Set(receivedNames))
else {
throw LumaCallError.incompatibleRemoteDescription throw LumaCallError.incompatibleRemoteDescription
} }
normalizedBundle = expectedNames.filter { bundleNames.contains($0) } normalizedBundle = expectedNames.filter { bundleNames.contains($0) }
@@ -1394,9 +1428,9 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
} }
let orderedContents = indices.map { sdp.contents[$0] } let orderedContents = indices.map { sdp.contents[$0] }
#if DEBUG #if DEBUG
print("Luma WebRTC: reordered session-accept contents to match the local offer.") print("Luma WebRTC: reordered session-accept contents to match the local offer.")
#endif #endif
return SDP(contents: orderedContents, bundle: normalizedBundle) return SDP(contents: orderedContents, bundle: normalizedBundle)
} }
@@ -1438,7 +1472,9 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
// MARK: RTCPeerConnectionDelegate // MARK: RTCPeerConnectionDelegate
func peerConnection(_ peerConnection: RTCPeerConnection, didChange stateChanged: RTCSignalingState) {} func peerConnection(
_ peerConnection: RTCPeerConnection, didChange stateChanged: RTCSignalingState
) {}
func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) { func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) {
guard let track = stream.videoTracks.first else { return } guard let track = stream.videoTracks.first else { return }
engine.performOnQueue { engine.performOnQueue {
@@ -1449,11 +1485,13 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) {} func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) {}
func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) {} func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) {}
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) { func peerConnection(
_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState
) {
engine.performOnQueue { engine.performOnQueue {
#if DEBUG #if DEBUG
print("Luma WebRTC: ICE state changed to \(String(describing: newState)).") print("Luma WebRTC: ICE state changed to \(String(describing: newState)).")
#endif #endif
switch newState { switch newState {
case .connected, .completed: case .connected, .completed:
self.cancelScheduledConnectionFailure() self.cancelScheduledConnectionFailure()
@@ -1475,16 +1513,21 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
} }
} }
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceGatheringState) {} func peerConnection(
_ peerConnection: RTCPeerConnection, didChange newState: RTCIceGatheringState
) {}
func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate) { func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate)
{
engine.performOnQueue { engine.performOnQueue {
self.localCandidates.append(candidate) self.localCandidates.append(candidate)
self.sendLocalCandidates() self.sendLocalCandidates()
} }
} }
func peerConnection(_ peerConnection: RTCPeerConnection, didRemove candidates: [RTCIceCandidate]) {} func peerConnection(
_ peerConnection: RTCPeerConnection, didRemove candidates: [RTCIceCandidate]
) {}
func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) {} func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) {}
func peerConnection( func peerConnection(
@@ -1499,7 +1542,8 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
} }
} }
func peerConnection(_ peerConnection: RTCPeerConnection, didRemove rtpReceiver: RTCRtpReceiver) { func peerConnection(_ peerConnection: RTCPeerConnection, didRemove rtpReceiver: RTCRtpReceiver)
{
guard let track = rtpReceiver.track as? RTCVideoTrack else { return } guard let track = rtpReceiver.track as? RTCVideoTrack else { return }
engine.performOnQueue { engine.performOnQueue {
guard self.remoteVideoTrack === track else { return } guard self.remoteVideoTrack === track else { return }
@@ -1511,40 +1555,43 @@ private final class LumaWebRTCCall: NSObject, RTCPeerConnectionDelegate {
// MARK: - Queue helpers kept fileprivate for the live call object // MARK: - Queue helpers kept fileprivate for the live call object
private extension LumaCallEngine { extension LumaCallEngine {
func performOnQueue(_ action: @escaping () -> Void) { fileprivate func performOnQueue(_ action: @escaping () -> Void) {
queue.async(execute: action) queue.async(execute: action)
} }
func performOnQueue(after delay: TimeInterval, execute item: DispatchWorkItem) { fileprivate func performOnQueue(after delay: TimeInterval, execute item: DispatchWorkItem) {
queue.asyncAfter(deadline: .now() + delay, execute: item) queue.asyncAfter(deadline: .now() + delay, execute: item)
} }
func performAttachedClient() -> XMPPClient? { fileprivate func performAttachedClient() -> XMPPClient? {
performSync { client } performSync { client }
} }
} }
private extension ExternalServiceDiscoveryModule.Service { extension ExternalServiceDiscoveryModule.Service {
var rtcIceServer: RTCIceServer? { fileprivate var rtcIceServer: RTCIceServer? {
let normalizedType = type.lowercased() let normalizedType = type.lowercased()
let normalizedHost = host.trimmingCharacters(in: .whitespacesAndNewlines) let normalizedHost = host.trimmingCharacters(in: .whitespacesAndNewlines)
guard ["stun", "stuns", "turn", "turns"].contains(normalizedType) else { return nil } guard ["stun", "stuns", "turn", "turns"].contains(normalizedType) else { return nil }
guard !normalizedHost.isEmpty, guard !normalizedHost.isEmpty,
normalizedHost.rangeOfCharacter(from: .whitespacesAndNewlines) == nil, normalizedHost.rangeOfCharacter(from: .whitespacesAndNewlines) == nil,
!normalizedHost.contains("/"), !normalizedHost.contains("/"),
!normalizedHost.contains("?"), !normalizedHost.contains("?"),
!normalizedHost.contains("#") else { return nil } !normalizedHost.contains("#")
else { return nil }
guard !normalizedType.hasSuffix("s") || transport == nil || transport == .tcp else { guard !normalizedType.hasSuffix("s") || transport == nil || transport == .tcp else {
return nil return nil
} }
if normalizedType.hasPrefix("turn") { if normalizedType.hasPrefix("turn") {
guard let username, !username.isEmpty, guard let username, !username.isEmpty,
let password, !password.isEmpty else { return nil } let password, !password.isEmpty
else { return nil }
} }
if let expires, expires <= Date() { return nil } if let expires, expires <= Date() { return nil }
let escapedHost = normalizedHost.contains(":") && !normalizedHost.hasPrefix("[") let escapedHost =
normalizedHost.contains(":") && !normalizedHost.hasPrefix("[")
? "[\(normalizedHost)]" ? "[\(normalizedHost)]"
: normalizedHost : normalizedHost
var url = "\(normalizedType):\(escapedHost)" var url = "\(normalizedType):\(escapedHost)"
@@ -1567,9 +1614,10 @@ private extension ExternalServiceDiscoveryModule.Service {
enum CallSDPOrdering { enum CallSDPOrdering {
static func answerIndices(localOrder: [String], remoteOrder: [String]) -> [Int]? { static func answerIndices(localOrder: [String], remoteOrder: [String]) -> [Int]? {
guard localOrder.count == remoteOrder.count, guard localOrder.count == remoteOrder.count,
Set(localOrder).count == localOrder.count, Set(localOrder).count == localOrder.count,
Set(remoteOrder).count == remoteOrder.count, Set(remoteOrder).count == remoteOrder.count,
Set(localOrder) == Set(remoteOrder) else { return nil } Set(localOrder) == Set(remoteOrder)
else { return nil }
let remoteIndices = Dictionary( let remoteIndices = Dictionary(
uniqueKeysWithValues: remoteOrder.enumerated().map { ($0.element, $0.offset) } uniqueKeysWithValues: remoteOrder.enumerated().map { ($0.element, $0.offset) }
@@ -1587,7 +1635,8 @@ enum CallSDPNormalization {
/// final two characters of its input. Give it one, and only one, trailing /// final two characters of its input. Give it one, and only one, trailing
/// CRLF so the last character of an SSRC/MSID value is never discarded. /// CRLF so the last character of an SSRC/MSID value is never discarded.
static func martinParseInput(_ webRTCSDP: String) -> String { static func martinParseInput(_ webRTCSDP: String) -> String {
var normalized = webRTCSDP var normalized =
webRTCSDP
.replacingOccurrences(of: "\r\n", with: "\n") .replacingOccurrences(of: "\r\n", with: "\n")
.replacingOccurrences(of: "\r", with: "\n") .replacingOccurrences(of: "\r", with: "\n")
while normalized.hasSuffix("\n") { while normalized.hasSuffix("\n") {
@@ -1632,9 +1681,11 @@ enum CallSDPCompatibility {
var currentMSIDs: [String: String] = [:] var currentMSIDs: [String: String] = [:]
for source in description.ssrcs { for source in description.ssrcs {
guard let value = source.parameters.first(where: { guard
$0.key.lowercased() == "msid" let value = source.parameters.first(where: {
})?.value, !value.isEmpty else { continue } $0.key.lowercased() == "msid"
})?.value, !value.isEmpty
else { continue }
currentMSIDs[source.ssrc] = value currentMSIDs[source.ssrc] = value
} }
let desiredMSIDs = canonicalFIDMSIDs( let desiredMSIDs = canonicalFIDMSIDs(
@@ -1647,19 +1698,21 @@ enum CallSDPCompatibility {
var changed = false var changed = false
let sources = description.ssrcs.map { source -> Jingle.RTP.Description.SSRC in let sources = description.ssrcs.map { source -> Jingle.RTP.Description.SSRC in
guard let desired = desiredMSIDs[source.ssrc], guard let desired = desiredMSIDs[source.ssrc],
currentMSIDs[source.ssrc] != desired else { currentMSIDs[source.ssrc] != desired
else {
return source return source
} }
changed = true changed = true
repairedCount += 1 repairedCount += 1
let parameters = source.parameters.filter { let parameters =
$0.key.lowercased() != "msid" source.parameters.filter {
} + [ $0.key.lowercased() != "msid"
Jingle.RTP.Description.SSRC.Parameter( } + [
key: "msid", Jingle.RTP.Description.SSRC.Parameter(
value: desired key: "msid",
) value: desired
] )
]
return Jingle.RTP.Description.SSRC( return Jingle.RTP.Description.SSRC(
ssrc: source.ssrc, ssrc: source.ssrc,
parameters: parameters parameters: parameters
@@ -1756,11 +1809,14 @@ private enum LumaCallError: LocalizedError {
case .unsupportedByContact: case .unsupportedByContact:
return "Устройства контакта не объявили поддержку совместимых Jingle-звонков." return "Устройства контакта не объявили поддержку совместимых Jingle-звонков."
case .mediaEngineUnavailable: case .mediaEngineUnavailable:
return "WebRTC не смог создать соединение даже без STUN/TURN. Очистите сборку и переустановите приложение." return
"WebRTC не смог создать соединение даже без STUN/TURN. Очистите сборку и переустановите приложение."
case .incompatibleLocalDescription: case .incompatibleLocalDescription:
return "WebRTC сформировал несовместимое описание видеопотока. Повторите звонок после перезапуска приложения." return
"WebRTC сформировал несовместимое описание видеопотока. Повторите звонок после перезапуска приложения."
case .incompatibleRemoteDescription: case .incompatibleRemoteDescription:
return "Ответ на видеозвонок содержит другой набор аудио/видео потоков. Обновите клиент собеседника или повторите аудиозвонок." return
"Ответ на видеозвонок содержит другой набор аудио/видео потоков. Обновите клиент собеседника или повторите аудиозвонок."
case .sdpGenerationFailed: case .sdpGenerationFailed:
return "Не удалось согласовать параметры аудио/видео." return "Не удалось согласовать параметры аудио/видео."
case .connectionLost: case .connectionLost: