-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathCallSettingsController.swift
181 lines (142 loc) · 8.36 KB
/
CallSettingsController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// CallSettingsController.swift
// Telegram
//
// Created by Mikhail Filimonov on 06/10/2020.
// Copyright © 2020 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import TelegramCore
import SwiftSignalKit
import InAppSettings
import Postbox
import TgVoipWebrtc
final class CallSettingsArguments {
let sharedContext: SharedAccountContext
let toggleInputAudioDevice:(String?)->Void
let toggleOutputAudioDevice:(String?)->Void
let toggleInputVideoDevice:(String?)->Void
let finishCall:()->Void
init(sharedContext: SharedAccountContext, toggleInputAudioDevice: @escaping(String?)->Void, toggleOutputAudioDevice:@escaping(String?)->Void, toggleInputVideoDevice:@escaping(String?)->Void, finishCall:@escaping()->Void) {
self.sharedContext = sharedContext
self.toggleInputAudioDevice = toggleInputAudioDevice
self.toggleOutputAudioDevice = toggleOutputAudioDevice
self.toggleInputVideoDevice = toggleInputVideoDevice
self.finishCall = finishCall
}
}
private let _id_input_camera = InputDataIdentifier("_id_input_camera")
private let _id_camera = InputDataIdentifier("_id_camera")
private let _id_input_audio = InputDataIdentifier("_id_input_audio")
private let _id_output_audio = InputDataIdentifier("_id_output_audio")
private let _id_micro = InputDataIdentifier("_id_micro")
private func callSettingsEntries(settings: VoiceCallSettings, devices: IODevices, arguments: CallSettingsArguments, theme: TelegramPresentationTheme) -> [InputDataEntry] {
var entries:[InputDataEntry] = []
var sectionId: Int32 = 0
var index: Int32 = 0
entries.append(.sectionId(sectionId, type: .customModern(10)))
sectionId += 1
var cameraDevice = devices.camera.first(where: { $0.uniqueID == settings.cameraInputDeviceId })
var microDevice = devices.audioInput.first(where: { $0.uniqueID == settings.audioInputDeviceId })
var outputDevice = devices.audioOutput.first(where: { $0.uniqueID == settings.audioOutputDeviceId })
let activeCameraDevice: AVCaptureDevice?
if let cameraDevice = cameraDevice {
if cameraDevice.isConnected && !cameraDevice.isSuspended {
activeCameraDevice = cameraDevice
} else {
activeCameraDevice = nil
}
} else if settings.cameraInputDeviceId == nil {
activeCameraDevice = AVCaptureDevice.default(for: .video)
} else {
cameraDevice = devices.camera.first(where: { $0.isConnected && !$0.isSuspended })
activeCameraDevice = cameraDevice
}
let customTheme = GeneralRowItem.Theme.initialize(theme)
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().callSettingsCameraTitle), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
entries.append(.general(sectionId: sectionId, index: index, value: .none, error: nil, identifier: _id_input_camera, data: .init(name: strings().callSettingsInputText, color: theme.colors.text, type: .contextSelector(cameraDevice?.localizedName ?? strings().callSettingsDeviceDefault, [ContextMenuItem(strings().callSettingsDeviceDefault, handler: {
arguments.toggleInputVideoDevice(nil)
})] + devices.camera.map { value in
return ContextMenuItem(value.localizedName, handler: {
arguments.toggleInputVideoDevice(value.uniqueID)
})
}), viewType: activeCameraDevice == nil ? .singleItem : .firstItem, theme: customTheme)))
index += 1
if let activeCameraDevice = activeCameraDevice {
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: _id_camera, equatable: InputDataEquatable(activeCameraDevice.uniqueID), comparable: nil, item: { initialSize, stableId -> TableRowItem in
return CameraPreviewRowItem(initialSize, stableId: stableId, device: activeCameraDevice, viewType: .lastItem)
}))
index += 1
}
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
let activeMicroDevice: AVCaptureDevice?
if let microDevice = microDevice {
if microDevice.isConnected && !microDevice.isSuspended {
activeMicroDevice = microDevice
} else {
activeMicroDevice = nil
}
} else if settings.audioInputDeviceId == nil {
activeMicroDevice = AVCaptureDevice.default(for: .audio)
} else {
microDevice = devices.audioInput.first(where: { $0.isConnected && !$0.isSuspended })
activeMicroDevice = microDevice
}
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().callSettingsInputTitle), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
entries.append(.general(sectionId: sectionId, index: index, value: .none, error: nil, identifier: _id_input_audio, data: .init(name: strings().callSettingsInputText, color: theme.colors.text, type: .contextSelector(microDevice?.localizedName ?? strings().callSettingsDeviceDefault, [ContextMenuItem(strings().callSettingsDeviceDefault, handler: {
arguments.toggleInputAudioDevice(nil)
})] + devices.audioInput.map { value in
return ContextMenuItem(value.localizedName, handler: {
arguments.toggleInputAudioDevice(value.uniqueID)
})
}), viewType: activeMicroDevice == nil ? .singleItem : .firstItem, theme: customTheme)))
index += 1
if let activeMicroDevice = activeMicroDevice {
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: _id_micro, equatable: InputDataEquatable(activeMicroDevice.uniqueID), comparable: nil, item: { initialSize, stableId -> TableRowItem in
return MicrophonePreviewRowItem(initialSize, stableId: stableId, context: arguments.sharedContext, viewType: .lastItem, customTheme: customTheme)
}))
index += 1
}
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().voiceChatSettingsOutput), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
entries.append(.general(sectionId: sectionId, index: index, value: .none, error: nil, identifier: _id_output_audio, data: .init(name: strings().voiceChatSettingsOutputDevice, color: customTheme.textColor, type: .contextSelector(outputDevice?.localizedName ?? strings().callSettingsDeviceDefault, [ContextMenuItem(strings().callSettingsDeviceDefault, handler: {
arguments.toggleOutputAudioDevice(nil)
})] + devices.audioOutput.map { value in
return ContextMenuItem(value.localizedName, handler: {
arguments.toggleOutputAudioDevice(value.uniqueID)
})
}), viewType: .singleItem, theme: customTheme)))
index += 1
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
return entries
}
func CallSettingsController(sharedContext: SharedAccountContext, presentation: TelegramPresentationTheme = theme) -> InputDataController {
let devicesContext = sharedContext.devicesContext
let arguments = CallSettingsArguments(sharedContext: sharedContext, toggleInputAudioDevice: { value in
_ = updateVoiceCallSettingsSettingsInteractively(accountManager: sharedContext.accountManager, {
$0.withUpdatedAudioInputDeviceId(value)
}).start()
}, toggleOutputAudioDevice: { value in
_ = updateVoiceCallSettingsSettingsInteractively(accountManager: sharedContext.accountManager, {
$0.withUpdatedAudioOutputDeviceId(value)
}).start()
}, toggleInputVideoDevice: { value in
_ = updateVoiceCallSettingsSettingsInteractively(accountManager: sharedContext.accountManager, {
$0.withUpdatedCameraInputDeviceId(value)
}).start()
}, finishCall: {
})
let signal = combineLatest(voiceCallSettings(sharedContext.accountManager), devicesContext.signal) |> map { settings, devices in
return InputDataSignalValue(entries: callSettingsEntries(settings: settings, devices: devices, arguments: arguments, theme: presentation))
}
let controller = InputDataController(dataSignal: signal, title: strings().callSettingsTitle, hasDone: false)
controller.contextObject = combineLatest(requestCameraPermission(), requestMicrophonePermission()).start()
return controller
}