@@ -30,9 +30,7 @@ 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- NetworkManager . shared. generateToken ( channelName: channelName, uid: SCREEN_SHARE_UID) {
34- self . navigationController? . pushViewController ( newViewController, animated: true )
35- }
33+ self . navigationController? . pushViewController ( newViewController, animated: true )
3634 }
3735}
3836
@@ -63,9 +61,22 @@ class ScreenShareMain: BaseViewController {
6361 option. clientRoleType = GlobalSettings . shared. getUserRole ( )
6462 option. publishCameraTrack = true
6563 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
6675 return option
6776 } ( )
6877
78+ private let screenShareId = SCREEN_SHARE_UID
79+ private let screenShareBroadcasterId = SCREEN_SHARE_BROADCASTER_UID
6980 private var systemBroadcastPicker : RPSystemBroadcastPickerView ?
7081
7182 // indicate if current instance has joined channel
@@ -89,10 +100,6 @@ class ScreenShareMain: BaseViewController {
89100 config. channelProfile = . liveBroadcasting
90101 agoraKit = AgoraRtcEngineKit . sharedEngine ( with: config, delegate: self )
91102 agoraKit. setLogFile ( LogUtils . sdkLogPath ( ) )
92-
93- // get channel name from configs
94- guard let channelName = configs [ " channelName " ] as? String else { return }
95-
96103
97104 // make myself a broadcaster
98105 agoraKit. setClientRole ( GlobalSettings . shared. getUserRole ( ) )
@@ -123,16 +130,23 @@ class ScreenShareMain: BaseViewController {
123130 // when joining channel. The channel name and uid used to calculate
124131 // the token has to match the ones used for channel join
125132
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 " )
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+ }
136150 }
137151 }
138152
@@ -180,32 +194,48 @@ class ScreenShareMain: BaseViewController {
180194 override func willMove( toParent parent: UIViewController ? ) {
181195 if parent == nil {
182196 // leave channel when exiting the view
183- // deregister packet processing
184- AgoraCustomEncryption . deregisterPacketProcessing ( agoraKit)
185197 if isJoined {
198+ guard let channelName = configs [ " channelName " ] as? String else { return }
186199 agoraKit. disableAudio ( )
187200 agoraKit. disableVideo ( )
188- agoraKit. leaveChannel { ( stats) -> Void in
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
189211 LogUtils . log ( message: " left channel, duration: \( stats. duration) " , level: . info)
190212 }
213+ AgoraRtcEngineKit . destroy ( )
191214 }
192215 }
193216 }
194217 @IBAction func stopScreenCapture( _ sender: Any ) {
218+ guard let channelName = configs [ " channelName " ] as? String else { return }
195219 agoraKit. stopScreenCapture ( )
196- option. publishCustomVideoTrack = false
197- agoraKit. updateChannel ( with: option)
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+ }
198228 }
199229 @IBAction func startScreenCapture( _ sender: Any ) {
200- agoraKit. startScreenCapture ( screenParams)
201- option. publishCustomVideoTrack = true
202- agoraKit. updateChannel ( with: option)
203230 prepareSystemBroadcaster ( )
231+ agoraKit. startScreenCapture ( screenParams)
232+
204233 guard let picker = systemBroadcastPicker else { return }
205234 for view in picker. subviews where view is UIButton {
206235 ( view as? UIButton ) ? . sendActions ( for: . allEvents)
207236 break
208237 }
238+ joinChannel ( uid: UInt ( screenShareBroadcasterId) , option: screenOption)
209239 }
210240 @IBAction func updateScreenCapture( _ sender: Any ) {
211241
@@ -309,4 +339,21 @@ extension ScreenShareMain: AgoraRtcEngineDelegate {
309339 func rtcEngine( _ engine: AgoraRtcEngineKit , remoteAudioStats stats: AgoraRtcRemoteAudioStats ) {
310340 remoteVideo. statsInfo? . updateAudioStats ( stats)
311341 }
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+ }
312359}
0 commit comments