@@ -33,6 +33,9 @@ class JoinMultiChannelEntry : UIViewController
3333 }
3434}
3535
36+ let CONNECTION_1_UID = UInt . random ( in: 1001 ... 2000 )
37+ let CONNECTION_2_UID = UInt . random ( in: 2001 ... 3000 )
38+
3639class JoinMultiChannelMain : BaseViewController , AgoraRtcEngineDelegate {
3740 var localVideo = Bundle . loadView ( fromNib: " VideoView " , withType: VideoView . self)
3841 var channel1RemoteVideo = Bundle . loadView ( fromNib: " VideoView " , withType: VideoView . self)
@@ -46,8 +49,6 @@ class JoinMultiChannelMain: BaseViewController, AgoraRtcEngineDelegate {
4649 var channel2 : JoinMultiChannelMainEventListener = JoinMultiChannelMainEventListener ( )
4750 var channelName1 = " "
4851 var channelName2 = " "
49- var connectionId1 : UInt32 ?
50- var connectionId2 : UInt32 ?
5152 var agoraKit : AgoraRtcEngineKit !
5253 var imageSource : AgoraYUVImageSourcePush = AgoraYUVImageSourcePush ( )
5354
@@ -63,7 +64,7 @@ class JoinMultiChannelMain: BaseViewController, AgoraRtcEngineDelegate {
6364 config. appId = KeyCenter . AppId
6465 config. areaCode = GlobalSettings . shared. area
6566 config. channelProfile = . liveBroadcasting
66- agoraKit = AgoraRtcEngineKit . sharedEngine ( with: config, delegate: self )
67+ agoraKit = AgoraRtcEngineKit . sharedEngine ( with: config, delegate: nil )
6768 agoraKit. setLogFile ( LogUtils . sdkLogPath ( ) )
6869
6970 // get channel name from configs
@@ -107,7 +108,6 @@ class JoinMultiChannelMain: BaseViewController, AgoraRtcEngineDelegate {
107108 agoraKit. startPreview ( )
108109
109110 // join channel1
110- let connectionIdPointer = UnsafeMutablePointer< UInt> . allocate( capacity: MemoryLayout< UInt32> . stride)
111111 var mediaOptions = AgoraRtcChannelMediaOptions ( )
112112 // publish audio and camera track for channel 1
113113 mediaOptions. publishAudioTrack = . of( true )
@@ -116,11 +116,12 @@ class JoinMultiChannelMain: BaseViewController, AgoraRtcEngineDelegate {
116116 mediaOptions. autoSubscribeAudio = . of( true )
117117 mediaOptions. channelProfile = . of( ( Int32) ( AgoraChannelProfile . liveBroadcasting. rawValue) )
118118 mediaOptions. clientRoleType = . of( ( Int32) ( AgoraClientRole . broadcaster. rawValue) )
119- var result = agoraKit. joinChannelEx ( byToken: nil , channelId: channelName1, uid: 0 , connectionId: connectionIdPointer, delegate: channel1, mediaOptions: mediaOptions, joinSuccess: nil )
120- channel1. connectionId = UInt32 ( connectionIdPointer. pointee)
121- connectionId1 = UInt32 ( connectionIdPointer. pointee)
119+ let connection1 = AgoraRtcConnection ( )
120+ connection1. channelId = channelName1
121+ connection1. localUid = CONNECTION_1_UID
122+ var result = agoraKit. joinChannelEx ( byToken: KeyCenter . Token, connection: connection1, delegate: channel1, mediaOptions: mediaOptions, joinSuccess: nil )
123+ channel1. channelId = channelName1
122124 channel1. connecitonDelegate = self
123- connectionIdPointer. deallocate ( )
124125 if result != 0 {
125126 // Usually happens with invalid parameters
126127 // Error code description can be found at:
@@ -130,7 +131,6 @@ class JoinMultiChannelMain: BaseViewController, AgoraRtcEngineDelegate {
130131 }
131132
132133 // join channel2
133- let connectionIdPointer2 = UnsafeMutablePointer< UInt> . allocate( capacity: MemoryLayout< UInt> . stride)
134134 mediaOptions = AgoraRtcChannelMediaOptions ( )
135135 // publish custom video track for channel 2
136136 mediaOptions. publishAudioTrack = . of( false )
@@ -139,11 +139,12 @@ class JoinMultiChannelMain: BaseViewController, AgoraRtcEngineDelegate {
139139 mediaOptions. clientRoleType = . of( ( Int32) ( AgoraClientRole . audience. rawValue) )
140140 mediaOptions. autoSubscribeVideo = . of( true )
141141 mediaOptions. autoSubscribeAudio = . of( true )
142- result = agoraKit. joinChannelEx ( byToken: nil , channelId: channelName2, uid: 0 , connectionId: connectionIdPointer2, delegate: channel2, mediaOptions: mediaOptions)
143- channel2. connectionId = UInt32 ( connectionIdPointer2. pointee)
144- connectionId2 = UInt32 ( connectionIdPointer2. pointee)
142+ let connection2 = AgoraRtcConnection ( )
143+ connection2. channelId = channelName2
144+ connection2. localUid = CONNECTION_2_UID
145+ result = agoraKit. joinChannelEx ( byToken: KeyCenter . Token, connection: connection2, delegate: channel2, mediaOptions: mediaOptions, joinSuccess: nil )
146+ channel2. channelId = channelName2
145147 channel2. connecitonDelegate = self
146- connectionIdPointer2. deallocate ( )
147148 if result != 0 {
148149 // Usually happens with invalid parameters
149150 // Error code description can be found at:
@@ -158,15 +159,22 @@ class JoinMultiChannelMain: BaseViewController, AgoraRtcEngineDelegate {
158159 agoraKit. stopPreview ( )
159160 imageSource. stopSource ( )
160161 // leave channel when exiting the view
161- agoraKit. leaveChannelEx ( channelName1, connectionId: UInt ( connectionId1 ?? 0 ) , leaveChannelBlock: nil )
162- agoraKit. leaveChannelEx ( channelName2, connectionId: UInt ( connectionId2 ?? 0 ) , leaveChannelBlock: nil )
162+ let channel1 = AgoraRtcConnection ( )
163+ channel1. channelId = channelName1
164+ agoraKit. leaveChannelEx ( channel1, leaveChannelBlock: nil )
165+ let channel2 = AgoraRtcConnection ( )
166+ channel2. channelId = channelName2
167+ agoraKit. leaveChannelEx ( channel2, leaveChannelBlock: nil )
163168 }
164169 }
170+
171+ func rtcEngine( _ engine: AgoraRtcEngineKit , didJoinChannel channel: String , withUid uid: UInt , elapsed: Int ) {
172+ LogUtils . log ( message: " Join \( channel) with uid \( uid) elapsed \( elapsed) ms " , level: . info)
173+ }
165174}
166175
167176extension JoinMultiChannelMain : AgoraYUVImageSourcePushDelegate {
168177 func onVideoFrame( _ buffer: Data , size: CGSize , rotation: Int32 ) {
169- guard let connectionId = connectionId2 else { return }
170178 let time = CMTime ( seconds: CACurrentMediaTime ( ) , preferredTimescale: 1000 )
171179 let videoFrame = AgoraVideoFrame ( )
172180 videoFrame. format = 1
@@ -175,24 +183,26 @@ extension JoinMultiChannelMain : AgoraYUVImageSourcePushDelegate {
175183 videoFrame. strideInPixels = Int32 ( size. width)
176184 videoFrame. height = Int32 ( size. height)
177185 videoFrame. rotation = Int32 ( rotation)
178- agoraKit. pushExternalVideoFrame ( videoFrame, connectionId: UInt ( connectionId) )
186+ let connection = AgoraRtcConnection ( )
187+ connection. channelId = channelName2
188+ agoraKit. pushExternalVideoFrame ( videoFrame, connection: connection)
179189 }
180190}
181191
182192extension JoinMultiChannelMain : JoinMultiChannelMainConnectionProtocol {
183- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didOccurWarning warningCode: AgoraWarningCode ) {
193+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didOccurWarning warningCode: AgoraWarningCode ) {
184194 LogUtils . log ( message: " warning: \( warningCode. description) " , level: . warning)
185195 }
186196
187- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didOccurError errorCode: AgoraErrorCode ) {
197+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didOccurError errorCode: AgoraErrorCode ) {
188198 self . showAlert ( title: " Error " , message: " Error \( errorCode. description) occur " )
189199 }
190200
191- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didJoinChannel channel: String , withUid uid: UInt , elapsed: Int ) {
201+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didJoinChannel channel: String , withUid uid: UInt , elapsed: Int ) {
192202 LogUtils . log ( message: " Join \( channel) with uid \( uid) elapsed \( elapsed) ms " , level: . info)
193203 }
194204
195- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didJoinedOfUid uid: UInt , elapsed: Int ) {
205+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didJoinedOfUid uid: UInt , elapsed: Int ) {
196206 LogUtils . log ( message: " remote user join: \( uid) \( elapsed) ms " , level: . info)
197207
198208 // Only one remote video view is available for this
@@ -201,12 +211,13 @@ extension JoinMultiChannelMain :JoinMultiChannelMainConnectionProtocol {
201211 let videoCanvas = AgoraRtcVideoCanvas ( )
202212 videoCanvas. uid = uid
203213 // the view to be binded
204- videoCanvas. view = connectionId == connectionId1 ? channel1RemoteVideo. videoView : channel2RemoteVideo. videoView
214+ videoCanvas. view = channelId == channelName1 ? channel1RemoteVideo. videoView : channel2RemoteVideo. videoView
205215 videoCanvas. renderMode = . hidden
206- agoraKit. setupRemoteVideoEx ( videoCanvas, connectionId: UInt ( connectionId) )
216+ let connection = AgoraRtcConnection ( )
217+ agoraKit. setupRemoteVideoEx ( videoCanvas, connection: connection)
207218 }
208219
209- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didOfflineOfUid uid: UInt , reason: AgoraUserOfflineReason ) {
220+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didOfflineOfUid uid: UInt , reason: AgoraUserOfflineReason ) {
210221 LogUtils . log ( message: " remote user left: \( uid) reason \( reason) " , level: . info)
211222
212223 // to unlink your view from sdk, so that your view reference will be released
@@ -217,33 +228,35 @@ extension JoinMultiChannelMain :JoinMultiChannelMainConnectionProtocol {
217228 // the view to be binded
218229 videoCanvas. view = nil
219230 videoCanvas. renderMode = . hidden
220- agoraKit. setupRemoteVideoEx ( videoCanvas, connectionId: UInt ( connectionId) )
231+ let connection = AgoraRtcConnection ( )
232+ agoraKit. setupRemoteVideoEx ( videoCanvas, connection: connection)
221233 }
222234
223235
224236}
225237
226238protocol JoinMultiChannelMainConnectionProtocol : NSObject {
227- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didOccurWarning warningCode: AgoraWarningCode )
228- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didOccurError errorCode: AgoraErrorCode )
229- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didJoinChannel channel: String , withUid uid: UInt , elapsed: Int )
230- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didJoinedOfUid uid: UInt , elapsed: Int )
231- func rtcEngine( _ engine: AgoraRtcEngineKit , connectionId : UInt32 , didOfflineOfUid uid: UInt , reason: AgoraUserOfflineReason )
239+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didOccurWarning warningCode: AgoraWarningCode )
240+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didOccurError errorCode: AgoraErrorCode )
241+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didJoinChannel channel: String , withUid uid: UInt , elapsed: Int )
242+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didJoinedOfUid uid: UInt , elapsed: Int )
243+ func rtcEngine( _ engine: AgoraRtcEngineKit , channelId : String , didOfflineOfUid uid: UInt , reason: AgoraUserOfflineReason )
232244}
233245
234246/// agora rtc engine delegate events
235247class JoinMultiChannelMainEventListener : NSObject , AgoraRtcEngineDelegate {
236248 weak var connecitonDelegate : JoinMultiChannelMainConnectionProtocol ?
237- var connectionId : UInt32 ?
249+ var channelId : String ?
238250 /// callback when warning occured for agora sdk, warning can usually be ignored, still it's nice to check out
239251 /// what is happening
252+ ///
240253 /// Warning code description can be found at:
241254 /// en: https://docs.agora.io/en/Voice/API%20Reference/oc/Constants/AgoraWarningCode.html
242255 /// cn: https://docs.agora.io/cn/Voice/API%20Reference/oc/Constants/AgoraWarningCode.html
243256 /// @param warningCode warning code of the problem
244257 func rtcEngine( _ engine: AgoraRtcEngineKit , didOccurWarning warningCode: AgoraWarningCode ) {
245- if let connId = self . connectionId {
246- self . connecitonDelegate? . rtcEngine ( engine, connectionId : connId , didOccurWarning: warningCode)
258+ if let channelId = self . channelId {
259+ self . connecitonDelegate? . rtcEngine ( engine, channelId : channelId , didOccurWarning: warningCode)
247260 }
248261 }
249262
@@ -254,25 +267,23 @@ class JoinMultiChannelMainEventListener: NSObject, AgoraRtcEngineDelegate {
254267 /// cn: https://docs.agora.io/cn/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
255268 /// @param errorCode error code of the problem
256269 func rtcEngine( _ engine: AgoraRtcEngineKit , didOccurError errorCode: AgoraErrorCode ) {
257- if let connId = self . connectionId {
258- self . connecitonDelegate? . rtcEngine ( engine, connectionId : connId , didOccurError: errorCode)
270+ if let channelId = self . channelId {
271+ self . connecitonDelegate? . rtcEngine ( engine, channelId : channelId , didOccurError: errorCode)
259272 }
260273 }
261274
262275
263276 internal func rtcEngine( _ engine: AgoraRtcEngineKit , didJoinChannel channel: String , withUid uid: UInt , elapsed: Int ) {
264- if let connId = self . connectionId {
265- self . connecitonDelegate? . rtcEngine ( engine, connectionId: connId, didJoinChannel: channel, withUid: uid, elapsed: elapsed)
266- }
277+ self . connecitonDelegate? . rtcEngine ( engine, channelId: channel, didJoinChannel: channel, withUid: uid, elapsed: elapsed)
267278 }
268279
269280 /// callback when a remote user is joinning the channel, note audience in live broadcast mode will NOT trigger this event
270281 /// @param uid uid of remote joined user
271282 /// @param elapsed time elapse since current sdk instance join the channel in ms
272283// func rtcChannel(_ rtcChannel: AgoraRtcChannel, didJoinedOfUid uid: UInt, elapsed: Int) {
273284 internal func rtcEngine( _ engine: AgoraRtcEngineKit , didJoinedOfUid uid: UInt , elapsed: Int ) {
274- if let connId = self . connectionId {
275- self . connecitonDelegate? . rtcEngine ( engine, connectionId : connId , didJoinedOfUid: uid, elapsed: elapsed)
285+ if let channelId = self . channelId {
286+ self . connecitonDelegate? . rtcEngine ( engine, channelId : channelId , didJoinedOfUid: uid, elapsed: elapsed)
276287 }
277288 }
278289
@@ -281,8 +292,9 @@ class JoinMultiChannelMainEventListener: NSObject, AgoraRtcEngineDelegate {
281292 /// @param reason reason why this user left, note this event may be triggered when the remote user
282293 /// become an audience in live broadcasting profile
283294 internal func rtcEngine( _ engine: AgoraRtcEngineKit , didOfflineOfUid uid: UInt , reason: AgoraUserOfflineReason ) {
284- if let connId = self . connectionId {
285- self . connecitonDelegate? . rtcEngine ( engine, connectionId : connId , didOfflineOfUid: uid, reason: reason)
295+ if let channelId = self . channelId {
296+ self . connecitonDelegate? . rtcEngine ( engine, channelId : channelId , didOfflineOfUid: uid, reason: reason)
286297 }
287298 }
288299}
300+
0 commit comments