forked from AgoraIO/API-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RawAudioData.swift
372 lines (334 loc) · 13.9 KB
/
RawAudioData.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
//
// RawAudioData.swift
// APIExample
//
// Created by XC on 2020/12/29.
// Copyright © 2020 Agora Corp. All rights reserved.
//
import Cocoa
import AgoraRtcKit
import AGEVideoLayout
class RawAudioData: BaseViewController {
var videos: [VideoView] = []
var agoraKit: AgoraRtcEngineKit!
@IBOutlet weak var Container: AGEVideoContainer!
/**
--- Microphones Picker ---
*/
@IBOutlet weak var selectMicsPicker: Picker!
var mics: [AgoraRtcDeviceInfo] = [] {
didSet {
DispatchQueue.main.async {[unowned self] in
self.selectMicsPicker.picker.addItems(withTitles: self.mics.map {$0.deviceName ?? "unknown"})
}
}
}
var selectedMicrophone: AgoraRtcDeviceInfo? {
let index = self.selectMicsPicker.indexOfSelectedItem
if index >= 0 && index < mics.count {
return mics[index]
} else {
return nil
}
}
func initSelectMicsPicker() {
selectMicsPicker.label.stringValue = "Microphone".localized
// find device in a separate thread to avoid blocking main thread
let queue = DispatchQueue(label: "device.enumerateDevices")
queue.async {[unowned self] in
self.mics = self.agoraKit.enumerateDevices(.audioRecording) ?? []
}
selectMicsPicker.onSelectChanged {
if !self.isJoined {
return
}
// use selected devices
guard let micId = self.selectedMicrophone?.deviceId else {
return
}
self.agoraKit.setDevice(.audioRecording, deviceId: micId)
}
}
/**
--- Layout Picker ---
*/
@IBOutlet weak var selectLayoutPicker: Picker!
let layouts = [Layout("1v1", 2), Layout("1v3", 4), Layout("1v8", 9), Layout("1v15", 16)]
var selectedLayout: Layout? {
let index = self.selectLayoutPicker.indexOfSelectedItem
if index >= 0 && index < layouts.count {
return layouts[index]
} else {
return nil
}
}
func initSelectLayoutPicker() {
layoutVideos(2)
selectLayoutPicker.label.stringValue = "Layout".localized
selectLayoutPicker.picker.addItems(withTitles: layouts.map { $0.label })
selectLayoutPicker.onSelectChanged {
if self.isJoined {
return
}
guard let layout = self.selectedLayout else { return }
self.layoutVideos(layout.value)
}
}
/**
--- Channel TextField ---
*/
@IBOutlet weak var channelField: Input!
func initChannelField() {
channelField.label.stringValue = "Channel".localized
channelField.field.placeholderString = "Channel Name".localized
}
/**
--- Button ---
*/
@IBOutlet weak var joinChannelButton: NSButton!
func initJoinChannelButton() {
joinChannelButton.title = isJoined ? "Leave Channel".localized : "Join Channel".localized
}
// indicate if current instance has joined channel
var isJoined: Bool = false {
didSet {
channelField.isEnabled = !isJoined
selectLayoutPicker.isEnabled = !isJoined
initJoinChannelButton()
}
}
// indicate for doing something
var isProcessing: Bool = false {
didSet {
joinChannelButton.isEnabled = !isProcessing
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
let config = AgoraRtcEngineConfig()
config.appId = KeyCenter.AppId
config.areaCode = GlobalSettings.shared.area.rawValue
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
agoraKit.enableVideo()
initSelectMicsPicker()
initSelectLayoutPicker()
initChannelField()
initJoinChannelButton()
}
override func viewWillBeRemovedFromSplitView() {
if isJoined {
agoraKit.leaveChannel { (stats:AgoraChannelStats) in
// unregister AudioFrameDelegate
self.agoraKit.setAudioDataFrame(nil)
LogUtils.log(message: "Left channel", level: .info)
}
}
AgoraRtcEngineKit.destroy()
}
@IBAction func onJoinPressed(_ sender:Any) {
if !isJoined {
// check configuration
let channel = channelField.stringValue
if channel.isEmpty {
return
}
guard let micId = selectedMicrophone?.deviceId else {
return
}
agoraKit.setDevice(.audioRecording, deviceId: micId)
// disable video module in audio scene
agoraKit.disableVideo()
// set live broadcaster mode
agoraKit.setChannelProfile(.liveBroadcasting)
// set proxy configuration
let proxySetting = GlobalSettings.shared.proxySetting.selectedOption().value
agoraKit.setCloudProxy(AgoraCloudProxyType.init(rawValue: UInt(proxySetting)) ?? .noneProxy)
// set myself as broadcaster to stream audio
agoraKit.setClientRole(.broadcaster)
agoraKit.setRecordingAudioFrameParametersWithSampleRate(44100, channel: 1, mode: .readWrite, samplesPerCall: 4410)
agoraKit.setMixedAudioFrameParametersWithSampleRate(44100, samplesPerCall: 4410)
agoraKit.setPlaybackAudioFrameParametersWithSampleRate(44100, channel: 1, mode: .readWrite, samplesPerCall: 4410)
// Register audio observer
agoraKit.setAudioDataFrame(self)
// set up local video to render your local camera preview
let localVideo = videos[0]
let videoCanvas = AgoraRtcVideoCanvas()
videoCanvas.uid = 0
// the view to be binded
videoCanvas.view = localVideo.videocanvas
videoCanvas.renderMode = .hidden
agoraKit.setupLocalVideo(videoCanvas)
// 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
isProcessing = true
let option = AgoraRtcChannelMediaOptions()
let result = agoraKit.joinChannel(byToken: KeyCenter.Token, channelId: channel, info: nil, uid: 0, options: option)
if result != 0 {
isProcessing = false
// 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")
}
} else {
isProcessing = true
agoraKit.leaveChannel { [unowned self] (stats:AgoraChannelStats) in
LogUtils.log(message: "Left channel", level: .info)
self.isProcessing = false
self.videos[0].uid = nil
self.isJoined = false
self.videos.forEach {
$0.uid = nil
$0.statsLabel.stringValue = ""
}
}
}
}
func layoutVideos(_ count: Int) {
videos = []
for i in 0...count - 1 {
let view = VideoView.createFromNib()!
if(i == 0) {
view.placeholder.stringValue = "Local"
} else {
view.placeholder.stringValue = "Remote \(i)"
}
videos.append(view)
}
// layout render view
Container.layoutStream(views: videos)
}
}
/// agora rtc engine delegate events
extension RawAudioData: 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)
if isProcessing {
isProcessing = false
}
self.showAlert(title: "Error", message: "Error \(errorCode.rawValue) 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) {
isProcessing = false
isJoined = true
let localVideo = videos[0]
localVideo.uid = uid
LogUtils.log(message: "Join \(channel) with uid \(uid) elapsed \(elapsed)ms", level: .info)
}
/// 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)
// find a VideoView w/o uid assigned
if let remoteVideo = videos.first(where: { $0.uid == nil }) {
let videoCanvas = AgoraRtcVideoCanvas()
videoCanvas.uid = uid
// the view to be binded
videoCanvas.view = remoteVideo.videocanvas
videoCanvas.renderMode = .hidden
agoraKit.setupRemoteVideo(videoCanvas)
remoteVideo.uid = uid
} else {
LogUtils.log(message: "no video canvas available for \(uid), cancel bind", level: .warning)
}
}
/// 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)
// to unlink your view from sdk, so that your view reference will be released
// note the video will stay at its last frame, to completely remove it
// you will need to remove the EAGL sublayer from your binded view
if let remoteVideo = videos.first(where: { $0.uid == uid }) {
let videoCanvas = AgoraRtcVideoCanvas()
videoCanvas.uid = uid
// the view to be binded
videoCanvas.view = nil
videoCanvas.renderMode = .hidden
agoraKit.setupRemoteVideo(videoCanvas)
remoteVideo.uid = nil
} else {
LogUtils.log(message: "no matching video canvas for \(uid), cancel unbind", level: .warning)
}
}
}
// audio data plugin, here you can process raw audio data
// note this all happens in CPU so it comes with a performance cost
extension RawAudioData: AgoraAudioDataFrameProtocol{
func getObservedAudioFramePosition() -> AgoraAudioFramePosition {
return .record
}
func onRecord(_ frame: AgoraAudioFrame) -> Bool {
return true
}
func onPlaybackAudioFrame(_ frame: AgoraAudioFrame) -> Bool {
return true
}
func onMixedAudioFrame(_ frame: AgoraAudioFrame) -> Bool {
return true
}
func onPlaybackAudioFrame(beforeMixing frame: AgoraAudioFrame, uid: UInt) -> Bool {
return true
}
func getObservedFramePosition() -> AgoraAudioFramePosition {
return .record
}
func isMultipleChannelFrameWanted() -> Bool {
return false
}
func onPlaybackAudioFrame(beforeMixingEx frame: AgoraAudioFrame, channelId: String, uid: UInt) -> Bool {
return true
}
func getMixedAudioParams() -> AgoraAudioParam {
let param = AgoraAudioParam()
param.channel = 1
param.mode = .readOnly
param.sampleRate = 44100
param.samplesPerCall = 1024
return param
}
func getRecordAudioParams() -> AgoraAudioParam {
let param = AgoraAudioParam()
param.channel = 1
param.mode = .readOnly
param.sampleRate = 44100
param.samplesPerCall = 1024
return param
}
func getPlaybackAudioParams() -> AgoraAudioParam {
let param = AgoraAudioParam()
param.channel = 1
param.mode = .readOnly
param.sampleRate = 44100
param.samplesPerCall = 1024
return param
}
}