-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoiceChanger.swift
502 lines (449 loc) · 25.4 KB
/
VoiceChanger.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//
// VoiceChanger.swift
// APIExample
//
// Created by 张乾泽 on 2020/7/24.
// Copyright © 2020 Agora Corp. All rights reserved.
//
import Foundation
import UIKit
import AgoraRtcKit
import AGEVideoLayout
class VoiceChangerEntry : UIViewController
{
@IBOutlet weak var joinButton: AGButton!
@IBOutlet weak var channelTextField: AGTextField!
let identifier = "VoiceChanger"
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func doJoinPressed(sender: AGButton) {
guard let channelName = channelTextField.text else {return}
//resign channel text field
channelTextField.resignFirstResponder()
let storyBoard: UIStoryboard = UIStoryboard(name: identifier, bundle: nil)
// create new view controller every time to ensure we get a clean vc
guard let newViewController = storyBoard.instantiateViewController(withIdentifier: identifier) as? BaseViewController else {return}
newViewController.title = channelName
newViewController.configs = ["channelName":channelName]
self.navigationController?.pushViewController(newViewController, animated: true)
}
}
class VoiceChangerMain: BaseViewController {
var agoraKit: AgoraRtcEngineKit!
@IBOutlet weak var chatBeautifierBtn: UIButton!
@IBOutlet weak var timbreTransformationBtn: UIButton!
@IBOutlet weak var voiceChangerBtn: UIButton!
@IBOutlet weak var styleTransformationBtn: UIButton!
@IBOutlet weak var roomAcousticsBtn: UIButton!
@IBOutlet weak var pitchCorrectionBtn: UIButton!
@IBOutlet weak var voiceConversionBtn: UIButton!
@IBOutlet weak var equalizationFreqBtn: UIButton!
@IBOutlet weak var reverbKeyBtn: UIButton!
@IBOutlet weak var reverbValueSlider: UISlider!
@IBOutlet weak var audioEffectParam1Slider: UISlider!
@IBOutlet weak var audioEffectParam2Slider: UISlider!
@IBOutlet weak var audioEffectParam1Label: UILabel!
@IBOutlet weak var audioEffectParam2Label: UILabel!
@IBOutlet weak var container: AGEVideoContainer!
var audioViews: [UInt:VideoView] = [:]
var equalizationFreq: AgoraAudioEqualizationBandFrequency = .band31
var equalizationGain: Int = 0
var reverbType: AgoraAudioReverbType = .dryLevel
var reverbMap:[AgoraAudioReverbType:Int] = [
.dryLevel:0,
.wetLevel:0,
.roomSize:0,
.wetDelay:0,
.strength:0
]
var currentAudioEffects:AgoraAudioEffectPreset = .audioEffectOff
// indicate if current instance has joined channel
var isJoined: Bool = false
func resetVoiceChanger() {
chatBeautifierBtn.setTitle("Off", for: .normal)
timbreTransformationBtn.setTitle("Off", for: .normal)
voiceChangerBtn.setTitle("Off", for: .normal)
styleTransformationBtn.setTitle("Off", for: .normal)
roomAcousticsBtn.setTitle("Off", for: .normal)
pitchCorrectionBtn.setTitle("Off", for: .normal)
voiceConversionBtn.setTitle("Off", for: .normal)
}
func updateAudioEffectsControls(_ effect:AgoraAudioEffectPreset) {
currentAudioEffects = effect
if(effect == .roomAcoustics3DVoice) {
audioEffectParam1Slider.isEnabled = true
audioEffectParam2Slider.isEnabled = false
audioEffectParam1Label.text = "Cycle"
audioEffectParam2Label.text = "N/A"
audioEffectParam1Slider.minimumValue = 0
audioEffectParam1Slider.maximumValue = 60
audioEffectParam1Slider.value = 10
} else if(effect == .pitchCorrection) {
audioEffectParam1Slider.isEnabled = true
audioEffectParam2Slider.isEnabled = true
audioEffectParam1Label.text = "Tonic Mode"
audioEffectParam2Label.text = "Tonic Pitch"
audioEffectParam1Slider.minimumValue = 1
audioEffectParam1Slider.maximumValue = 3
audioEffectParam1Slider.value = 1
audioEffectParam2Slider.minimumValue = 1
audioEffectParam2Slider.maximumValue = 12
audioEffectParam2Slider.value = 4
} else {
audioEffectParam1Slider.isEnabled = false
audioEffectParam2Slider.isEnabled = false
audioEffectParam1Label.text = "N/A"
audioEffectParam2Label.text = "N/A"
}
}
func getChatBeautifierAction(_ chatBeautifier:AgoraVoiceBeautifierPreset) -> UIAlertAction{
return UIAlertAction(title: "\(chatBeautifier.description())", style: .default, handler: {[unowned self] action in
self.resetVoiceChanger()
self.updateAudioEffectsControls(.audioEffectOff)
//when using this method with setLocalVoiceReverbPreset,
//the method called later overrides the one called earlier
self.agoraKit.setVoiceBeautifierPreset(chatBeautifier)
self.chatBeautifierBtn.setTitle("\(chatBeautifier.description())", for: .normal)
})
}
func getTimbreTransformationAction(_ timbreTransformation:AgoraVoiceBeautifierPreset) -> UIAlertAction{
return UIAlertAction(title: "\(timbreTransformation.description())", style: .default, handler: {[unowned self] action in
self.resetVoiceChanger()
self.updateAudioEffectsControls(.audioEffectOff)
//when using this method with setLocalVoiceReverbPreset,
//the method called later overrides the one called earlier
self.agoraKit.setVoiceBeautifierPreset(timbreTransformation)
self.timbreTransformationBtn.setTitle("\(timbreTransformation.description())", for: .normal)
})
}
func getVoiceChangerAction(_ voiceChanger:AgoraAudioEffectPreset) -> UIAlertAction{
return UIAlertAction(title: "\(voiceChanger.description())", style: .default, handler: {[unowned self] action in
self.resetVoiceChanger()
self.updateAudioEffectsControls(voiceChanger)
//when using this method with setLocalVoiceReverbPreset,
//the method called later overrides the one called earlier
self.agoraKit.setAudioEffectPreset(voiceChanger)
self.voiceChangerBtn.setTitle("\(voiceChanger.description())", for: .normal)
})
}
func getStyleTransformationAction(_ styleTransformation:AgoraAudioEffectPreset) -> UIAlertAction{
return UIAlertAction(title: "\(styleTransformation.description())", style: .default, handler: {[unowned self] action in
self.resetVoiceChanger()
self.updateAudioEffectsControls(styleTransformation)
//when using this method with setLocalVoiceChanger,
//the method called later overrides the one called earlier
self.agoraKit.setAudioEffectPreset(styleTransformation)
self.styleTransformationBtn.setTitle("\(styleTransformation.description())", for: .normal)
})
}
func getRoomAcousticsAction(_ roomAcoustics:AgoraAudioEffectPreset) -> UIAlertAction{
return UIAlertAction(title: "\(roomAcoustics.description())", style: .default, handler: {[unowned self] action in
self.resetVoiceChanger()
self.updateAudioEffectsControls(roomAcoustics)
//when using this method with setLocalVoiceReverbPreset,
//the method called later overrides the one called earlier
self.agoraKit.setAudioEffectPreset(roomAcoustics)
self.roomAcousticsBtn.setTitle("\(roomAcoustics.description())", for: .normal)
})
}
func getPitchCorrectionAction(_ pitchCorrection:AgoraAudioEffectPreset) -> UIAlertAction{
return UIAlertAction(title: "\(pitchCorrection.description())", style: .default, handler: {[unowned self] action in
self.resetVoiceChanger()
self.updateAudioEffectsControls(pitchCorrection)
//when using this method with setLocalVoiceReverbPreset,
//the method called later overrides the one called earlier
self.agoraKit.setAudioEffectPreset(pitchCorrection)
self.pitchCorrectionBtn.setTitle("\(pitchCorrection.description())", for: .normal)
})
}
func getVoiceConversionAction(_ voiceConversion:AgoraVoiceConversionPreset) -> UIAlertAction{
return UIAlertAction(title: "\(voiceConversion.description())", style: .default, handler: {[unowned self] action in
self.resetVoiceChanger()
self.updateAudioEffectsControls(.audioEffectOff)
self.agoraKit.setVoiceConversionPreset(voiceConversion)
self.voiceConversionBtn.setTitle("\(voiceConversion.description())", for: .normal)
})
}
func getEqualizationFreqAction(_ freq:AgoraAudioEqualizationBandFrequency) -> UIAlertAction {
return UIAlertAction(title: "\(freq.description())", style: .default, handler: {[unowned self] action in
self.equalizationFreq = freq
self.equalizationFreqBtn.setTitle("\(freq.description())", for: .normal)
LogUtils.log(message: "onLocalVoiceEqualizationGain \(self.equalizationFreq.description()) \(self.equalizationGain)", level: .info)
self.agoraKit.setLocalVoiceEqualizationOf(self.equalizationFreq, withGain: self.equalizationGain)
})
}
func getReverbKeyAction(_ reverbType:AgoraAudioReverbType) -> UIAlertAction {
return UIAlertAction(title: "\(reverbType.description())", style: .default, handler: {[unowned self] action in
self.updateReverbValueRange(reverbKey: reverbType)
self.reverbKeyBtn.setTitle("\(reverbType.description())", for: .normal)
})
}
/// callback when voice changer button hit
@IBAction func onChatBeautifier() {
let alert = UIAlertController(title: "Set Chat Beautifier".localized, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? UIAlertController.Style.alert : UIAlertController.Style.actionSheet)
alert.addAction(getChatBeautifierAction(.voiceBeautifierOff))
alert.addAction(getChatBeautifierAction(.chatBeautifierFresh))
alert.addAction(getChatBeautifierAction(.chatBeautifierVitality))
alert.addAction(getChatBeautifierAction(.chatBeautifierMagnetic))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
/// callback when voice changer button hit
@IBAction func onTimbreTransformation() {
let alert = UIAlertController(title: "Set Timbre Transformation".localized, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? UIAlertController.Style.alert : UIAlertController.Style.actionSheet)
alert.addAction(getTimbreTransformationAction(.voiceBeautifierOff))
alert.addAction(getTimbreTransformationAction(.timbreTransformationVigorous))
alert.addAction(getTimbreTransformationAction(.timbreTransformationDeep))
alert.addAction(getTimbreTransformationAction(.timbreTransformationMellow))
alert.addAction(getTimbreTransformationAction(.timbreTransformationFalsetto))
alert.addAction(getTimbreTransformationAction(.timbreTransformationFull))
alert.addAction(getTimbreTransformationAction(.timbreTransformationClear))
alert.addAction(getTimbreTransformationAction(.timbreTransformationResounding))
alert.addAction(getTimbreTransformationAction(.timbreTransformationRinging))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
/// callback when voice changer button hit
@IBAction func onVoiceChanger() {
let alert = UIAlertController(title: "Set Voice Changer".localized, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? UIAlertController.Style.alert : UIAlertController.Style.actionSheet)
alert.addAction(getVoiceChangerAction(.audioEffectOff))
alert.addAction(getVoiceChangerAction(.voiceChangerEffectUncle))
alert.addAction(getVoiceChangerAction(.voiceChangerEffectOldMan))
alert.addAction(getVoiceChangerAction(.voiceChangerEffectBoy))
alert.addAction(getVoiceChangerAction(.voiceChangerEffectSister))
alert.addAction(getVoiceChangerAction(.voiceChangerEffectGirl))
alert.addAction(getVoiceChangerAction(.voiceChangerEffectPigKing))
alert.addAction(getVoiceChangerAction(.voiceChangerEffectHulk))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
/// callback when voice changer button hit
@IBAction func onStyleTransformation() {
let alert = UIAlertController(title: "Set Style Transformation".localized, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? UIAlertController.Style.alert : UIAlertController.Style.actionSheet)
alert.addAction(getStyleTransformationAction(.audioEffectOff))
alert.addAction(getStyleTransformationAction(.styleTransformationPopular))
alert.addAction(getStyleTransformationAction(.styleTransformationRnB))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
/// callback when voice changer button hit
@IBAction func onRoomAcoustics() {
let alert = UIAlertController(title: "Set Room Acoustics".localized, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? UIAlertController.Style.alert : UIAlertController.Style.actionSheet)
alert.addAction(getRoomAcousticsAction(.roomAcousticsKTV))
alert.addAction(getRoomAcousticsAction(.roomAcousticsVocalConcert))
alert.addAction(getRoomAcousticsAction(.roomAcousticsStudio))
alert.addAction(getRoomAcousticsAction(.roomAcousticsPhonograph))
alert.addAction(getRoomAcousticsAction(.roomAcousticsVirtualStereo))
alert.addAction(getRoomAcousticsAction(.roomAcousticsSpacial))
alert.addAction(getRoomAcousticsAction(.roomAcousticsEthereal))
alert.addAction(getRoomAcousticsAction(.roomAcoustics3DVoice))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
/// callback when voice changer button hit
@IBAction func onPitchCorrection() {
let alert = UIAlertController(title: "Set Pitch Correction".localized, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? UIAlertController.Style.alert : UIAlertController.Style.actionSheet)
alert.addAction(getPitchCorrectionAction(.pitchCorrection))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
@IBAction func onVoiceConversion(_ sender: Any) {
let alert = UIAlertController(title: "Set Voice Conversion".localized, message: nil, preferredStyle: .actionSheet)
alert.addAction(getVoiceConversionAction(.conversionOff))
alert.addAction(getVoiceConversionAction(.changerNeutral))
alert.addAction(getVoiceConversionAction(.changerSweet))
alert.addAction(getVoiceConversionAction(.changerSolid))
alert.addAction(getVoiceConversionAction(.changerBass))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
@IBAction func onAudioEffectsParamUpdated(_ sender: UISlider) {
let param1 = audioEffectParam1Slider.isEnabled ? Int32(audioEffectParam1Slider.value) : 0
let param2 = audioEffectParam2Slider.isEnabled ? Int32(audioEffectParam2Slider.value) : 0
LogUtils.log(message: "onAudioEffectsParamUpdated \(currentAudioEffects.description()) \(param1) \(param2)", level: .info)
agoraKit.setAudioEffectParameters(currentAudioEffects, param1: param1, param2: param2)
}
@IBAction func onLocalVoicePitch(_ sender:UISlider) {
LogUtils.log(message: "onLocalVoicePitch \(Double(sender.value))", level: .info)
agoraKit.setLocalVoicePitch(Double(sender.value))
}
@IBAction func onLocalVoiceEqualizaitonFreq(_ sender:UIButton) {
let alert = UIAlertController(title: "Set Band Frequency".localized, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? UIAlertController.Style.alert : UIAlertController.Style.actionSheet)
alert.addAction(getEqualizationFreqAction(.band31))
alert.addAction(getEqualizationFreqAction(.band62))
alert.addAction(getEqualizationFreqAction(.band125))
alert.addAction(getEqualizationFreqAction(.band250))
alert.addAction(getEqualizationFreqAction(.band500))
alert.addAction(getEqualizationFreqAction(.band1K))
alert.addAction(getEqualizationFreqAction(.band2K))
alert.addAction(getEqualizationFreqAction(.band4K))
alert.addAction(getEqualizationFreqAction(.band8K))
alert.addAction(getEqualizationFreqAction(.band16K))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
@IBAction func onLocalVoiceEqualizationGain(_ sender:UISlider) {
equalizationGain = Int(sender.value)
LogUtils.log(message: "onLocalVoiceEqualizationGain \(equalizationFreq.description()) \(equalizationGain)", level: .info)
agoraKit.setLocalVoiceEqualizationOf(equalizationFreq, withGain: equalizationGain)
}
func updateReverbValueRange(reverbKey:AgoraAudioReverbType) {
var min:Float = 0, max:Float = 0
switch reverbKey {
case .dryLevel:
min = -20
max = 10
break
case .wetLevel:
min = -20
max = 10
break
case .roomSize:
min = 0
max = 100
break
case .wetDelay:
min = 0
max = 200
break
case .strength:
min = 0
max = 100
break
default: break
}
reverbValueSlider.minimumValue = min
reverbValueSlider.maximumValue = max
reverbValueSlider.value = Float(reverbMap[reverbType] ?? 0)
}
@IBAction func onLocalVoiceReverbKey(_ sender:UIButton) {
let alert = UIAlertController(title: "Set Reverb Key".localized, message: nil, preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? UIAlertController.Style.alert : UIAlertController.Style.actionSheet)
alert.addAction(getReverbKeyAction(.dryLevel))
alert.addAction(getReverbKeyAction(.wetLevel))
alert.addAction(getReverbKeyAction(.roomSize))
alert.addAction(getReverbKeyAction(.wetDelay))
alert.addAction(getReverbKeyAction(.strength))
alert.addCancelAction()
present(alert, animated: true, completion: nil)
}
@IBAction func onLocalVoiceReverbValue(_ sender:UISlider) {
let value = Int(sender.value)
reverbMap[reverbType] = value
LogUtils.log(message: "onLocalVoiceReverbValue \(reverbType.description()) \(value)", level: .info)
agoraKit.setLocalVoiceReverbOf(reverbType, withValue: value)
}
override func viewDidLoad(){
super.viewDidLoad()
// set up agora instance when view loadedlet config = AgoraRtcEngineConfig()
let config = AgoraRtcEngineConfig()
config.appId = KeyCenter.AppId
config.areaCode = GlobalSettings.shared.area.rawValue
// setup log file path
let logConfig = AgoraLogConfig()
logConfig.filePath = LogUtils.sdkLogPath()
config.logConfig = logConfig
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
guard let channelName = configs["channelName"] as? String else {return}
self.title = channelName
// reset voice changer options
resetVoiceChanger()
equalizationFreqBtn.setTitle("\(equalizationFreq.description())", for: .normal)
reverbKeyBtn.setTitle("\(reverbType.description())", for: .normal)
// Before calling the method, you need to set the profile
// parameter of setAudioProfile to AUDIO_PROFILE_MUSIC_HIGH_QUALITY(4)
// or AUDIO_PROFILE_MUSIC_HIGH_QUALITY_STEREO(5), and to set
// scenario parameter to AUDIO_SCENARIO_GAME_STREAMING(3).
agoraKit.setAudioProfile(.musicHighQualityStereo, scenario: .gameStreaming)
// make myself a broadcaster
agoraKit.setChannelProfile(.liveBroadcasting)
agoraKit.setClientRole(.broadcaster)
// disable video module
agoraKit.disableVideo()
// Set audio route to speaker
agoraKit.setDefaultAudioRouteToSpeakerphone(true)
// start joining channel
// 1. Users can only see each other after they join the
// same channel successfully using the same app id.
// 2. If app certificate is turned on at dashboard, token is needed
// when joining channel. The channel name and uid used to calculate
// the token has to match the ones used for channel join
let option = AgoraRtcChannelMediaOptions()
let result = agoraKit.joinChannel(byToken: KeyCenter.Token, channelId: channelName, info: nil, uid: 0, options: option)
if result != 0 {
// Usually happens with invalid parameters
// 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
self.showAlert(title: "Error", message: "joinChannel call failed: \(result), please check your params")
}
}
override func willMove(toParent parent: UIViewController?) {
if parent == nil {
// leave channel when exiting the view
if isJoined {
agoraKit.leaveChannel { (stats) -> Void in
LogUtils.log(message: "left channel, duration: \(stats.duration)", level: .info)
}
}
}
}
}
/// agora rtc engine delegate events
extension VoiceChangerMain: 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.description)", 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.description) occur")
}
/// callback when the local user joins a specified channel.
/// @param channel
/// @param uid uid of local user
/// @param elapsed time elapse since current sdk instance join the channel in ms
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinChannel channel: String, withUid uid: UInt, elapsed: Int) {
isJoined = true
LogUtils.log(message: "Join \(channel) with uid \(uid) elapsed \(elapsed)ms", level: .info)
//set up local audio view, this view will not show video but just a placeholder
let view = Bundle.loadView(fromNib: "VideoView", withType: VideoView.self)
audioViews[uid] = view
view.setPlaceholder(text: self.getAudioLabel(uid: uid, isLocal: true))
container.layoutStream2x1(views: Array(self.audioViews.values))
}
/// callback when a remote user is joinning the channel, note audience in live broadcast mode will NOT trigger this event
/// @param uid uid of remote joined user
/// @param elapsed time elapse since current sdk instance join the channel in ms
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
LogUtils.log(message: "remote user join: \(uid) \(elapsed)ms", level: .info)
//set up remote audio view, this view will not show video but just a placeholder
let view = Bundle.loadView(fromNib: "VideoView", withType: VideoView.self)
self.audioViews[uid] = view
view.setPlaceholder(text: self.getAudioLabel(uid: uid, isLocal: false))
self.container.layoutStream2x1(views: Array(self.audioViews.values))
self.container.reload(level: 0, animated: true)
}
/// callback when a remote user is leaving the channel, note audience in live broadcast mode will NOT trigger this event
/// @param uid uid of remote joined user
/// @param reason reason why this user left, note this event may be triggered when the remote user
/// become an audience in live broadcasting profile
func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid: UInt, reason: AgoraUserOfflineReason) {
LogUtils.log(message: "remote user left: \(uid) reason \(reason)", level: .info)
//remove remote audio view
self.audioViews.removeValue(forKey: uid)
self.container.layoutStream2x1(views: Array(self.audioViews.values))
self.container.reload(level: 0, animated: true)
}
}