From d117b4d93dba5f9f935ddebdc4742a1d600b4797 Mon Sep 17 00:00:00 2001 From: Peter <> Date: Fri, 23 Nov 2018 04:37:39 +0300 Subject: [PATCH] Take voipMaxLayer as an argument --- TelegramCore/Account.swift | 2 +- TelegramCore/CallSessionManager.swift | 48 +++++++++++++-------------- TelegramCore/Network.swift | 4 ++- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/TelegramCore/Account.swift b/TelegramCore/Account.swift index 0dc16ffe9a..71c42dd976 100644 --- a/TelegramCore/Account.swift +++ b/TelegramCore/Account.swift @@ -851,7 +851,7 @@ public class Account { self.peerInputActivityManager = PeerInputActivityManager() self.stateManager = AccountStateManager(account: self, peerInputActivityManager: self.peerInputActivityManager, auxiliaryMethods: auxiliaryMethods) self.contactSyncManager = ContactSyncManager(postbox: postbox, network: network, accountPeerId: peerId, stateManager: self.stateManager) - self.callSessionManager = CallSessionManager(postbox: postbox, network: network, addUpdates: { [weak self] updates in + self.callSessionManager = CallSessionManager(postbox: postbox, network: network, maxLayer: networkArguments.voipMaxLayer, addUpdates: { [weak self] updates in self?.stateManager.addUpdates(updates) }) self.localInputActivityManager = PeerInputActivityManager() diff --git a/TelegramCore/CallSessionManager.swift b/TelegramCore/CallSessionManager.swift index c45c75753c..1e67871fce 100644 --- a/TelegramCore/CallSessionManager.swift +++ b/TelegramCore/CallSessionManager.swift @@ -9,8 +9,7 @@ import MtProtoKitDynamic import SwiftSignalKit #endif -let kCallMinLayer:Int32 = 65; -let kCallMaxLayer:Int32 = 92; // TODO figure out how to replace this with [OngoingCallThreadLocalContext getConnectionMaxLayer] or call VoIPController::GetConnectionMaxLayer() directly. These modern programming languages drive me crazy. +private let minLayer: Int32 = 65 public enum CallSessionError: Equatable { case generic @@ -205,10 +204,10 @@ private final class CallSessionContext { } private final class CallSessionManagerContext { - private let queue: Queue private let postbox: Postbox private let network: Network + private let maxLayer: Int32 private let addUpdates: (Api.Updates) -> Void private let ringingSubscribers = Bag<([CallSessionRingingState]) -> Void>() @@ -217,10 +216,11 @@ private final class CallSessionManagerContext { private let disposables = DisposableSet() - init(queue: Queue, postbox: Postbox, network: Network, addUpdates: @escaping (Api.Updates) -> Void) { + init(queue: Queue, postbox: Postbox, network: Network, maxLayer: Int32, addUpdates: @escaping (Api.Updates) -> Void) { self.queue = queue self.postbox = postbox self.network = network + self.maxLayer = maxLayer self.addUpdates = addUpdates } @@ -422,7 +422,7 @@ private final class CallSessionManagerContext { if let context = self.contexts[internalId] { switch context.state { case let .ringing(id, accessHash, gAHash, b): - context.state = .accepting(id: id, accessHash: accessHash, gAHash: gAHash, b: b, disposable: (acceptCallSession(postbox: self.postbox, network: self.network, stableId: id, accessHash: accessHash, b: b) |> deliverOn(self.queue)).start(next: { [weak self] result in + context.state = .accepting(id: id, accessHash: accessHash, gAHash: gAHash, b: b, disposable: (acceptCallSession(postbox: self.postbox, network: self.network, stableId: id, accessHash: accessHash, b: b, maxLayer: <#Int32#>) |> deliverOn(self.queue)).start(next: { [weak self] result in if let strongSelf = self, let context = strongSelf.contexts[internalId] { if case .accepting = context.state { switch result { @@ -485,7 +485,7 @@ private final class CallSessionManagerContext { let keyVisualHash = MTSha256(key + gA)! - context.state = .confirming(id: id, accessHash: accessHash, key: key, keyId: keyId, keyVisualHash: keyVisualHash, disposable: (confirmCallSession(network: self.network, stableId: id, accessHash: accessHash, gA: gA, keyFingerprint: keyId) |> deliverOnMainQueue).start(next: { [weak self] updatedCall in + context.state = .confirming(id: id, accessHash: accessHash, key: key, keyId: keyId, keyVisualHash: keyVisualHash, disposable: (confirmCallSession(network: self.network, stableId: id, accessHash: accessHash, gA: gA, keyFingerprint: keyId, maxLayer: self.maxLayer) |> deliverOnMainQueue).start(next: { [weak self] updatedCall in if let strongSelf = self, let context = strongSelf.contexts[internalId], case .confirming = context.state { if let updatedCall = updatedCall { strongSelf.updateSession(updatedCall) @@ -502,21 +502,21 @@ private final class CallSessionManagerContext { assertionFailure() } } - case let .phoneCallDiscarded(flags, id, reason, duration): + case let .phoneCallDiscarded(flags, id, reason, _): let reportRating = (flags & (1 << 2)) != 0 if let internalId = self.contextIdByStableId[id] { if let context = self.contexts[internalId] { let parsedReason: CallSessionTerminationReason if let reason = reason { switch reason { - case .phoneCallDiscardReasonBusy: - parsedReason = .ended(.busy) - case .phoneCallDiscardReasonDisconnect: - parsedReason = .error(.disconnected) - case .phoneCallDiscardReasonHangup: - parsedReason = .ended(.hungUp) - case .phoneCallDiscardReasonMissed: - parsedReason = .ended(.missed) + case .phoneCallDiscardReasonBusy: + parsedReason = .ended(.busy) + case .phoneCallDiscardReasonDisconnect: + parsedReason = .error(.disconnected) + case .phoneCallDiscardReasonHangup: + parsedReason = .ended(.hungUp) + case .phoneCallDiscardReasonMissed: + parsedReason = .ended(.missed) } } else { parsedReason = .ended(.hungUp) @@ -642,7 +642,7 @@ private final class CallSessionManagerContext { let randomStatus = SecRandomCopyBytes(nil, 256, aBytes.assumingMemoryBound(to: UInt8.self)) let a = Data(bytesNoCopy: aBytes, count: 256, deallocator: .free) if randomStatus == 0 { - self.contexts[internalId] = CallSessionContext(peerId: peerId, isOutgoing: true, state: .requesting(a: a, disposable: (requestCallSession(postbox: self.postbox, network: self.network, peerId: peerId, a: a) |> deliverOn(queue)).start(next: { [weak self] result in + self.contexts[internalId] = CallSessionContext(peerId: peerId, isOutgoing: true, state: .requesting(a: a, disposable: (requestCallSession(postbox: self.postbox, network: self.network, peerId: peerId, a: a, maxLayer: self.maxLayer) |> deliverOn(queue)).start(next: { [weak self] result in if let strongSelf = self, let context = strongSelf.contexts[internalId] { if case .requesting = context.state { switch result { @@ -676,9 +676,9 @@ public final class CallSessionManager { private let queue = Queue() private var contextRef: Unmanaged? - init(postbox: Postbox, network: Network, addUpdates: @escaping (Api.Updates) -> Void) { + init(postbox: Postbox, network: Network, maxLayer: Int32, addUpdates: @escaping (Api.Updates) -> Void) { self.queue.async { - let context = CallSessionManagerContext(queue: self.queue, postbox: postbox, network: network, addUpdates: addUpdates) + let context = CallSessionManagerContext(queue: self.queue, postbox: postbox, network: network, maxLayer: maxLayer, addUpdates: addUpdates) self.contextRef = Unmanaged.passRetained(context) } } @@ -779,7 +779,7 @@ private enum AcceptCallResult { case success(AcceptedCall) } -private func acceptCallSession(postbox: Postbox, network: Network, stableId: CallSessionStableId, accessHash: Int64, b: Data) -> Signal { +private func acceptCallSession(postbox: Postbox, network: Network, stableId: CallSessionStableId, accessHash: Int64, b: Data, maxLayer: Int32) -> Signal { return validatedEncryptionConfig(postbox: postbox, network: network) |> mapToSignal { config -> Signal in var gValue: Int32 = config.g.byteSwapped @@ -794,7 +794,7 @@ private func acceptCallSession(postbox: Postbox, network: Network, stableId: Cal return .single(.failed) } - return network.request(Api.functions.phone.acceptCall(peer: .inputPhoneCall(id: stableId, accessHash: accessHash), gB: Buffer(data: gb), protocol: .phoneCallProtocol(flags: (1 << 0) | (1 << 1), minLayer: kCallMinLayer, maxLayer: kCallMaxLayer))) + return network.request(Api.functions.phone.acceptCall(peer: .inputPhoneCall(id: stableId, accessHash: accessHash), gB: Buffer(data: gb), protocol: .phoneCallProtocol(flags: (1 << 0) | (1 << 1), minLayer: minLayer, maxLayer: maxLayer))) |> map(Optional.init) |> `catch` { _ -> Signal in return .single(nil) @@ -841,7 +841,7 @@ private enum RequestCallSessionResult { case failed(CallSessionError) } -private func requestCallSession(postbox: Postbox, network: Network, peerId: PeerId, a: Data) -> Signal { +private func requestCallSession(postbox: Postbox, network: Network, peerId: PeerId, a: Data, maxLayer: Int32) -> Signal { return validatedEncryptionConfig(postbox: postbox, network: network) |> mapToSignal { config -> Signal in return postbox.transaction { transaction -> Signal in @@ -857,7 +857,7 @@ private func requestCallSession(postbox: Postbox, network: Network, peerId: Peer let gAHash = MTSha256(ga)! - return network.request(Api.functions.phone.requestCall(userId: inputUser, randomId: Int32(bitPattern: arc4random()), gAHash: Buffer(data: gAHash), protocol: .phoneCallProtocol(flags: (1 << 0) | (1 << 1), minLayer: kCallMinLayer, maxLayer: kCallMaxLayer))) + return network.request(Api.functions.phone.requestCall(userId: inputUser, randomId: Int32(bitPattern: arc4random()), gAHash: Buffer(data: gAHash), protocol: .phoneCallProtocol(flags: (1 << 0) | (1 << 1), minLayer: minLayer, maxLayer: maxLayer))) |> map { result -> RequestCallSessionResult in switch result { case let .phoneCall(phoneCall, _): @@ -893,8 +893,8 @@ private func requestCallSession(postbox: Postbox, network: Network, peerId: Peer } } -private func confirmCallSession(network: Network, stableId: CallSessionStableId, accessHash: Int64, gA: Data, keyFingerprint: Int64) -> Signal { - return network.request(Api.functions.phone.confirmCall(peer: Api.InputPhoneCall.inputPhoneCall(id: stableId, accessHash: accessHash), gA: Buffer(data: gA), keyFingerprint: keyFingerprint, protocol: .phoneCallProtocol(flags: (1 << 0) | (1 << 1), minLayer: kCallMinLayer, maxLayer: kCallMaxLayer))) +private func confirmCallSession(network: Network, stableId: CallSessionStableId, accessHash: Int64, gA: Data, keyFingerprint: Int64, maxLayer: Int32) -> Signal { + return network.request(Api.functions.phone.confirmCall(peer: Api.InputPhoneCall.inputPhoneCall(id: stableId, accessHash: accessHash), gA: Buffer(data: gA), keyFingerprint: keyFingerprint, protocol: .phoneCallProtocol(flags: (1 << 0) | (1 << 1), minLayer: minLayer, maxLayer: maxLayer))) |> map(Optional.init) |> `catch` { _ -> Signal in return .single(nil) diff --git a/TelegramCore/Network.swift b/TelegramCore/Network.swift index adc802cfb5..90badf42d2 100644 --- a/TelegramCore/Network.swift +++ b/TelegramCore/Network.swift @@ -375,11 +375,13 @@ public struct NetworkInitializationArguments { public let apiId: Int32 public let languagesCategory: String public let appVersion: String + public let voipMaxLayer: Int32 - public init(apiId: Int32, languagesCategory: String, appVersion: String) { + public init(apiId: Int32, languagesCategory: String, appVersion: String, voipMaxLayer: Int32) { self.apiId = apiId self.languagesCategory = languagesCategory self.appVersion = appVersion + self.voipMaxLayer = voipMaxLayer } }