@@ -30,7 +30,9 @@ class ScreenShareEntry : UIViewController
3030 guard let newViewController = storyBoard. instantiateViewController ( withIdentifier: identifier) as? BaseViewController else { return }
3131 newViewController. title = channelName
3232 newViewController. configs = [ " channelName " : channelName]
33- self . navigationController? . pushViewController ( newViewController, animated: true )
33+ NetworkManager . shared. generateToken ( channelName: channelName, uid: SCREEN_SHARE_UID) {
34+ self . navigationController? . pushViewController ( newViewController, animated: true )
35+ }
3436 }
3537}
3638
@@ -61,22 +63,9 @@ class ScreenShareMain: BaseViewController {
6163 option. clientRoleType = GlobalSettings . shared. getUserRole ( )
6264 option. publishCameraTrack = true
6365 option. publishMicrophoneTrack = true
64- option. autoSubscribeAudio = true
65- option. autoSubscribeVideo = true
66- return option
67- } ( )
68- private lazy var screenOption : AgoraRtcChannelMediaOptions = {
69- let option = AgoraRtcChannelMediaOptions ( )
70- option. clientRoleType = . broadcaster
71- option. publishCameraTrack = false
72- option. publishMicrophoneTrack = false
73- option. publishScreenCaptureAudio = true
74- option. publishScreenCaptureVideo = true
7566 return option
7667 } ( )
7768
78- private let screenShareId = SCREEN_SHARE_UID
79- private let screenShareBroadcasterId = SCREEN_SHARE_BROADCASTER_UID
8069 private var systemBroadcastPicker : RPSystemBroadcastPickerView ?
8170
8271 // indicate if current instance has joined channel
@@ -100,6 +89,10 @@ class ScreenShareMain: BaseViewController {
10089 config. channelProfile = . liveBroadcasting
10190 agoraKit = AgoraRtcEngineKit . sharedEngine ( with: config, delegate: self )
10291 agoraKit. setLogFile ( LogUtils . sdkLogPath ( ) )
92+
93+ // get channel name from configs
94+ guard let channelName = configs [ " channelName " ] as? String else { return }
95+
10396
10497 // make myself a broadcaster
10598 agoraKit. setClientRole ( GlobalSettings . shared. getUserRole ( ) )
@@ -130,23 +123,16 @@ class ScreenShareMain: BaseViewController {
130123 // when joining channel. The channel name and uid used to calculate
131124 // the token has to match the ones used for channel join
132125
133- joinChannel ( uid: screenShareId, option: option)
134- }
135-
136- private func joinChannel( uid: UInt , option: AgoraRtcChannelMediaOptions ) {
137- guard let channelName = configs [ " channelName " ] as? String else { return }
138- let connection = AgoraRtcConnection ( )
139- connection. channelId = channelName
140- connection. localUid = uid
141- NetworkManager . shared. generateToken ( channelName: channelName, uid: uid) { token in
142- let result = self . agoraKit. joinChannelEx ( byToken: token, connection: connection, delegate: self , mediaOptions: option, joinSuccess: nil )
143- if result != 0 {
144- // Usually happens with invalid parameters
145- // Error code description can be found at:
146- // en: https://docs.agora.io/en/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
147- // cn: https://docs.agora.io/cn/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
148- self . showAlert ( title: " Error " , message: " joinChannel call failed: \( result) , please check your params " )
149- }
126+
127+ let result = agoraKit. joinChannel ( byToken: KeyCenter . Token, channelId: channelName, uid: SCREEN_SHARE_UID, mediaOptions: option)
128+ agoraKit. muteRemoteAudioStream ( UInt ( SCREEN_SHARE_BROADCASTER_UID) , mute: true )
129+ agoraKit. muteRemoteVideoStream ( UInt ( SCREEN_SHARE_BROADCASTER_UID) , mute: true )
130+ if result != 0 {
131+ // Usually happens with invalid parameters
132+ // Error code description can be found at:
133+ // en: https://docs.agora.io/en/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
134+ // cn: https://docs.agora.io/cn/Voice/API%20Reference/oc/Constants/AgoraErrorCode.html
135+ self . showAlert ( title: " Error " , message: " joinChannel call failed: \( result) , please check your params " )
150136 }
151137 }
152138
@@ -194,48 +180,34 @@ class ScreenShareMain: BaseViewController {
194180 override func willMove( toParent parent: UIViewController ? ) {
195181 if parent == nil {
196182 // leave channel when exiting the view
183+ // deregister packet processing
197184 if isJoined {
198- guard let channelName = configs [ " channelName " ] as? String else { return }
199185 agoraKit. disableAudio ( )
200186 agoraKit. disableVideo ( )
201- let connection = AgoraRtcConnection ( )
202- connection. localUid = screenShareId
203- connection. channelId = channelName
204- agoraKit. leaveChannelEx ( connection) { stats in
205- LogUtils . log ( message: " left channel, duration: \( stats. duration) " , level: . info)
206- }
207- let screenConnection = AgoraRtcConnection ( )
208- screenConnection. localUid = UInt ( screenShareBroadcasterId)
209- screenConnection. channelId = channelName
210- agoraKit. leaveChannelEx ( screenConnection) { stats in
187+ agoraKit. leaveChannel { ( stats) -> Void in
211188 LogUtils . log ( message: " left channel, duration: \( stats. duration) " , level: . info)
212189 }
213190 AgoraRtcEngineKit . destroy ( )
214191 }
215192 }
216193 }
217194 @IBAction func stopScreenCapture( _ sender: Any ) {
218- guard let channelName = configs [ " channelName " ] as? String else { return }
219195 agoraKit. stopScreenCapture ( )
220- screenOption. publishCustomVideoTrack = false
221- agoraKit. updateChannel ( with: screenOption)
222- let screenConnection = AgoraRtcConnection ( )
223- screenConnection. localUid = UInt ( screenShareBroadcasterId)
224- screenConnection. channelId = channelName
225- agoraKit. leaveChannelEx ( screenConnection) { stats in
226- LogUtils . log ( message: " left channel, duration: \( stats. duration) " , level: . info)
227- }
196+ option. publishScreenCaptureVideo = false
197+ option. publishCameraTrack = true
198+ agoraKit. updateChannel ( with: option)
228199 }
229200 @IBAction func startScreenCapture( _ sender: Any ) {
230- prepareSystemBroadcaster ( )
231201 agoraKit. startScreenCapture ( screenParams)
232-
202+ option. publishScreenCaptureVideo = true
203+ option. publishCameraTrack = false
204+ agoraKit. updateChannel ( with: option)
205+ prepareSystemBroadcaster ( )
233206 guard let picker = systemBroadcastPicker else { return }
234207 for view in picker. subviews where view is UIButton {
235208 ( view as? UIButton ) ? . sendActions ( for: . allEvents)
236209 break
237210 }
238- joinChannel ( uid: UInt ( screenShareBroadcasterId) , option: screenOption)
239211 }
240212 @IBAction func updateScreenCapture( _ sender: Any ) {
241213
@@ -282,11 +254,6 @@ extension ScreenShareMain: AgoraRtcEngineDelegate {
282254 func rtcEngine( _ engine: AgoraRtcEngineKit , didJoinedOfUid uid: UInt , elapsed: Int ) {
283255 LogUtils . log ( message: " remote user join: \( uid) \( elapsed) ms " , level: . info)
284256
285- if isScreenShareUid ( uid: uid) {
286- LogUtils . log ( message: " Ignore screen share uid " , level: . info)
287- return
288- }
289-
290257 // Only one remote video view is available for this
291258 // tutorial. Here we check if there exists a surface
292259 // view tagged as this uid.
@@ -339,21 +306,4 @@ extension ScreenShareMain: AgoraRtcEngineDelegate {
339306 func rtcEngine( _ engine: AgoraRtcEngineKit , remoteAudioStats stats: AgoraRtcRemoteAudioStats ) {
340307 remoteVideo. statsInfo? . updateAudioStats ( stats)
341308 }
342-
343- func rtcEngine( _ engine: AgoraRtcEngineKit , localVideoStateChangedOf state: AgoraVideoLocalState , error: AgoraLocalVideoStreamError , sourceType: AgoraVideoSourceType ) {
344-
345- print ( " state == \( state) error == \( error) " )
346- switch ( sourceType, state) {
347- case ( . screen, . capturing) :
348- print ( " 屏幕共享开始 " )
349-
350- case ( . screen, . stopped) :
351- print ( " 屏幕共享停止 " )
352-
353- case ( . screen, . failed) :
354- print ( " 断开连接 " )
355-
356- default : break
357- }
358- }
359309}
0 commit comments