forked from AgoraIO/API-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrecallTest.swift
More file actions
313 lines (277 loc) · 12.3 KB
/
PrecallTest.swift
File metadata and controls
313 lines (277 loc) · 12.3 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// JoinChannelVC.swift
// APIExample
//
// Created by 张乾泽 on 2020/4/17.
// Copyright © 2020 Agora Corp. All rights reserved.
//
import Cocoa
import AgoraRtcKit
import AGEVideoLayout
class PrecallTest: BaseViewController {
var videos: [VideoView] = []
var timer:Timer?
@IBOutlet weak var cameraPicker: NSPopUpButton!
@IBOutlet weak var micPicker: NSPopUpButton!
@IBOutlet weak var speakerPicker: NSPopUpButton!
@IBOutlet weak var startCameraTestBtn: NSButton!
@IBOutlet weak var stopCameraTestBtn: NSButton!
@IBOutlet weak var startMicTestBtn: NSButton!
@IBOutlet weak var stopMicTestBtn: NSButton!
@IBOutlet weak var startSpeakerTestBtn: NSButton!
@IBOutlet weak var stopSpeakerTestBtn: NSButton!
@IBOutlet weak var startLoopbackTestBtn: NSButton!
@IBOutlet weak var stopLoopbackTestBtn: NSButton!
@IBOutlet weak var startLastmileTestBtn: NSButton!
@IBOutlet weak var lastmileResultLabel: NSTextField!
@IBOutlet weak var lastmileProbResultLabel: NSTextField!
@IBOutlet weak var lastmileActivityView: NSProgressIndicator!
@IBOutlet weak var micTestingVolumeIndicator: NSProgressIndicator!
@IBOutlet weak var echoTestCountDownLabel: NSTextField!
@IBOutlet weak var echoTestPopover: NSView!
@IBOutlet weak var echoValidateCountDownLabel: NSTextField!
@IBOutlet weak var echoValidatePopover: NSView!
var cameras:[AgoraRtcDeviceInfo] = [] {
didSet {
DispatchQueue.main.async {[unowned self] in
self.cameraPicker.addItems(withTitles: self.cameras.map({ (device: AgoraRtcDeviceInfo) -> String in
return (device.deviceName ?? "")
}))
}
}
}
var mics:[AgoraRtcDeviceInfo] = [] {
didSet {
DispatchQueue.main.async {[unowned self] in
self.micPicker.addItems(withTitles: self.mics.map({ (device: AgoraRtcDeviceInfo) -> String in
return (device.deviceName ?? "")
}))
}
}
}
var speakers:[AgoraRtcDeviceInfo] = [] {
didSet {
DispatchQueue.main.async {[unowned self] in
self.speakerPicker.addItems(withTitles: self.speakers.map({ (device: AgoraRtcDeviceInfo) -> String in
return (device.deviceName ?? "")
}))
}
}
}
// indicate if camera testing is going on
var isTestingCamera: Bool = false {
didSet {
startCameraTestBtn.isHidden = isTestingCamera
stopCameraTestBtn.isHidden = !isTestingCamera
}
}
// indicate if mic testing is going on
var isTestingMic: Bool = false {
didSet {
startMicTestBtn.isHidden = isTestingMic
stopMicTestBtn.isHidden = !isTestingMic
startLoopbackTestBtn.isEnabled = !isTestingMic
}
}
// indicate if speaker testing is going on
var isTestingSpeaker: Bool = false {
didSet {
startSpeakerTestBtn.isHidden = isTestingSpeaker
stopSpeakerTestBtn.isHidden = !isTestingSpeaker
startLoopbackTestBtn.isEnabled = !isTestingSpeaker
}
}
// indicate if speaker testing is going on
var isTestingLoopback: Bool = false {
didSet {
startLoopbackTestBtn.isHidden = isTestingLoopback
stopLoopbackTestBtn.isHidden = !isTestingLoopback
startMicTestBtn.isEnabled = !isTestingLoopback
startSpeakerTestBtn.isEnabled = !isTestingLoopback
}
}
@IBOutlet weak var container: AGEVideoContainer!
var agoraKit: AgoraRtcEngineKit!
override func viewDidLoad() {
super.viewDidLoad()
layoutVideos()
// set up agora instance when view loaded
let config = AgoraRtcEngineConfig()
config.appId = KeyCenter.AppId
config.areaCode = GlobalSettings.shared.area.rawValue
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
// this is mandatory to get camera list
agoraKit.enableVideo()
//find device in a separate thread to avoid blocking main thread
let queue = DispatchQueue(label: "device.enumerateDevices")
queue.async {[unowned self] in
self.cameras = self.agoraKit.enumerateDevices(.videoCapture) ?? []
self.mics = self.agoraKit.enumerateDevices(.audioRecording) ?? []
self.speakers = self.agoraKit.enumerateDevices(.audioPlayout) ?? []
}
}
override func viewWillBeRemovedFromSplitView() {
timer?.invalidate()
agoraKit.stopEchoTest()
agoraKit.stopLastmileProbeTest()
AgoraRtcEngineKit.destroy()
}
@IBAction func onStartCameraTest(_ sender:NSButton) {
// use selected devices
if let cameraId = cameras[cameraPicker.indexOfSelectedItem].deviceId {
agoraKit.setDevice(.videoCapture, deviceId: cameraId)
}
agoraKit.startCaptureDeviceTest(videos[0])
isTestingCamera = true
}
@IBAction func onStopCameraTest(_ sender:NSButton) {
agoraKit.stopCaptureDeviceTest()
isTestingCamera = false
}
@IBAction func onStartMicTest(_ sender:NSButton) {
// use selected devices
if let micId = mics[micPicker.indexOfSelectedItem].deviceId {
agoraKit.setDevice(.audioRecording, deviceId: micId)
}
agoraKit.startRecordingDeviceTest(50)
isTestingMic = true
}
@IBAction func onStopMicTest(_ sender:NSButton) {
agoraKit.stopRecordingDeviceTest()
isTestingMic = false
}
@IBAction func onStartSpeakerTest(_ sender:NSButton) {
// use selected devices
if let speakerId = speakers[speakerPicker.indexOfSelectedItem].deviceId {
agoraKit.setDevice(.audioPlayout, deviceId: speakerId)
}
if let filepath = Bundle.main.path(forResource: "audiomixing", ofType: "mp3") {
let result = agoraKit.startPlaybackDeviceTest(filepath)
if result != 0 {
self.showAlert(title: "Error", message: "startAudioMixing call failed: \(result), please check your params")
}
isTestingSpeaker = true
}
}
@IBAction func onStopSpeakerTest(_ sender:NSButton) {
agoraKit.stopPlaybackDeviceTest()
isTestingSpeaker = false
}
@IBAction func onStartLoopbackTest(_ sender:NSButton) {
// use selected devices
if let micId = mics[micPicker.indexOfSelectedItem].deviceId {
agoraKit.setDevice(.audioRecording, deviceId: micId)
}
if let speakerId = speakers[speakerPicker.indexOfSelectedItem].deviceId {
agoraKit.setDevice(.audioPlayout, deviceId: speakerId)
}
agoraKit.startAudioDeviceLoopbackTest(50)
isTestingLoopback = true
}
@IBAction func onStopLoopbackTest(_ sender:NSButton) {
agoraKit.stopAudioDeviceLoopbackTest()
isTestingLoopback = false
}
@IBAction func onStartLastmileTest(_ sender:NSButton) {
lastmileActivityView.startAnimation(nil)
let config = AgoraLastmileProbeConfig()
// do uplink testing
config.probeUplink = true;
// do downlink testing
config.probeDownlink = true;
// expected uplink bitrate, range: [100000, 5000000]
config.expectedUplinkBitrate = 100000;
// expected downlink bitrate, range: [100000, 5000000]
config.expectedDownlinkBitrate = 100000;
agoraKit.startLastmileProbeTest(config)
}
@IBAction func doEchoTest(sender: NSButton) {
agoraKit.startEchoTest(withInterval: 10)
showPopover(isValidate: false, seconds: 10) {[unowned self] in
self.showPopover(isValidate: true, seconds: 10) {[unowned self] in
self.agoraKit.stopEchoTest()
}
}
}
// show popover and hide after seconds
func showPopover(isValidate:Bool, seconds:Int, callback:@escaping (() -> Void)) {
var count = seconds
var countDownLabel:NSTextField?
var popover:NSView?
if(isValidate) {
countDownLabel = echoValidateCountDownLabel
popover = echoValidatePopover
} else {
countDownLabel = echoTestCountDownLabel
popover = echoTestPopover
}
countDownLabel?.stringValue = "\(count)"
popover?.isHidden = false
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {[unowned self] (timer) in
count -= 1
countDownLabel?.stringValue = "\(count)"
if(count == 0) {
self.timer?.invalidate()
popover?.isHidden = true
callback()
}
}
}
func layoutVideos() {
let view = VideoView.createFromNib()!
view.placeholder.stringValue = "Camera Test Preview"
videos = [view]
// layout render view
container.layoutStream(views: videos)
}
}
/// agora rtc engine delegate events
extension PrecallTest: AgoraRtcEngineDelegate {
/// callback when warning occured for agora sdk, warning can usually be ignored, still it's nice to check out
/// what is happening
/// Warning code description can be found at:
/// en: https://docs.agora.io/en/Voice/API%20Reference/oc/Constants/AgoraWarningCode.html
/// cn: https://docs.agora.io/cn/Voice/API%20Reference/oc/Constants/AgoraWarningCode.html
/// @param warningCode warning code of the problem
func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurWarning warningCode: AgoraWarningCode) {
LogUtils.log(message: "warning: \(warningCode.rawValue)", level: .warning)
}
/// callback when error occured for agora sdk, you are recommended to display the error descriptions on demand
/// to let user know something wrong is happening
/// Error code description can be found at:
/// en: https://docs.agora.io/en/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
/// cn: https://docs.agora.io/cn/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
/// @param errorCode error code of the problem
func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurError errorCode: AgoraErrorCode) {
LogUtils.log(message: "error: \(errorCode)", level: .error)
self.showAlert(title: "Error", message: "Error \(errorCode.rawValue) occur")
}
/// Reports which users are speaking, the speakers' volumes, and whether the local user is speaking.
/// @params speakers volume info for all speakers
/// @params totalVolume Total volume after audio mixing. The value range is [0,255].
func rtcEngine(_ engine: AgoraRtcEngineKit, reportAudioVolumeIndicationOfSpeakers speakers: [AgoraRtcAudioVolumeInfo], totalVolume: Int) {
for speaker in speakers {
if(speaker.uid == 0) {
micTestingVolumeIndicator.doubleValue = Double(speaker.volume)
}
}
}
/// callback to get lastmile quality 2seconds after startLastmileProbeTest
func rtcEngine(_ engine: AgoraRtcEngineKit, lastmileQuality quality: AgoraNetworkQuality) {
lastmileResultLabel.stringValue = "Quality: \(quality.description())"
}
/// callback to get more detail lastmile quality after startLastmileProbeTest
func rtcEngine(_ engine: AgoraRtcEngineKit, lastmileProbeTest result: AgoraLastmileProbeResult) {
let rtt = "Rtt: \(result.rtt)ms"
let downlinkBandwidth = "DownlinkAvailableBandwidth: \(result.downlinkReport.availableBandwidth)Kbps"
let downlinkJitter = "DownlinkJitter: \(result.downlinkReport.jitter)ms"
let downlinkLoss = "DownlinkLoss: \(result.downlinkReport.packetLossRate)%"
let uplinkBandwidth = "UplinkAvailableBandwidth: \(result.uplinkReport.availableBandwidth)Kbps"
let uplinkJitter = "UplinkJitter: \(result.uplinkReport.jitter)ms"
let uplinkLoss = "UplinkLoss: \(result.uplinkReport.packetLossRate)%"
lastmileProbResultLabel.stringValue = [rtt, downlinkBandwidth, downlinkJitter, downlinkLoss, uplinkBandwidth, uplinkJitter, uplinkLoss].joined(separator: "\n")
// stop testing after get last mile detail result
engine.stopLastmileProbeTest()
lastmileActivityView.stopAnimation(nil)
}
}