@@ -53,19 +53,31 @@ class LiveStreamingEntry : UIViewController
5353}
5454
5555class LiveStreamingMain : BaseViewController {
56- var localVideo = Bundle . loadView ( fromNib: " VideoView " , withType: VideoView . self)
57- var remoteVideo = Bundle . loadView ( fromNib: " VideoView " , withType: VideoView . self)
58- @IBOutlet weak var localVideoContainer : UIView !
59- @IBOutlet weak var remoteVideoContainer : UIView !
56+ var foregroundVideo = Bundle . loadView ( fromNib: " VideoView " , withType: VideoView . self)
57+ var backgroundVideo = Bundle . loadView ( fromNib: " VideoView " , withType: VideoView . self)
58+ @IBOutlet weak var foregroundVideoContainer : UIView !
59+ @IBOutlet weak var backgroundVideoContainer : UIView !
6060 @IBOutlet weak var clientRoleToggle : UISwitch !
6161 @IBOutlet weak var ultraLowLatencyToggle : UISwitch !
62+ var remoteUid : UInt ?
6263 var agoraKit : AgoraRtcEngineKit !
6364 var role : AgoraClientRole = . broadcaster {
6465 didSet {
65- localVideoContainer . isHidden = role != . broadcaster
66+ foregroundVideoContainer . isHidden = role != . broadcaster
6667 ultraLowLatencyToggle. isEnabled = role == . audience
6768 }
6869 }
70+ var isLocalVideoForeground = false {
71+ didSet {
72+ if ( isLocalVideoForeground) {
73+ foregroundVideo. setPlaceholder ( text: " Local Host " . localized)
74+ backgroundVideo. setPlaceholder ( text: " Remote Host " . localized)
75+ } else {
76+ foregroundVideo. setPlaceholder ( text: " Remote Host " . localized)
77+ backgroundVideo. setPlaceholder ( text: " Local Host " . localized)
78+ }
79+ }
80+ }
6981 var isUltraLowLatencyOn : Bool = false
7082
7183 // indicate if current instance has joined channel
@@ -75,12 +87,10 @@ class LiveStreamingMain: BaseViewController {
7587 super. viewDidLoad ( )
7688
7789 // layout render view
78- localVideoContainer. addSubview ( localVideo)
79- remoteVideoContainer. addSubview ( remoteVideo)
80- localVideo. setPlaceholder ( text: " Local Host " . localized)
81- localVideo. bindFrameToSuperviewBounds ( )
82- remoteVideo. setPlaceholder ( text: " Remote Host " . localized)
83- remoteVideo. bindFrameToSuperviewBounds ( )
90+ foregroundVideoContainer. addSubview ( foregroundVideo)
91+ backgroundVideoContainer. addSubview ( backgroundVideo)
92+ foregroundVideo. bindFrameToSuperviewBounds ( )
93+ backgroundVideo. bindFrameToSuperviewBounds ( )
8494
8595 // set up agora instance when view loadedlet config = AgoraRtcEngineConfig()
8696 let config = AgoraRtcEngineConfig ( )
@@ -92,6 +102,9 @@ class LiveStreamingMain: BaseViewController {
92102 guard let channelName = configs [ " channelName " ] as? String ,
93103 let role = configs [ " role " ] as? AgoraClientRole else { return }
94104
105+ // for audience put local video in foreground
106+ isLocalVideoForeground = role == . audience
107+
95108 // make this room live broadcasting room
96109 agoraKit. setChannelProfile ( . liveBroadcasting)
97110 updateClientRole ( role)
@@ -108,7 +121,7 @@ class LiveStreamingMain: BaseViewController {
108121 // 2. If app certificate is turned on at dashboard, token is needed
109122 // when joining channel. The channel name and uid used to calculate
110123 // the token has to match the ones used for channel join
111- let result = agoraKit. joinChannel ( byToken: nil , channelId: channelName, info: nil , uid: SCREEN_SHARE_BROADCASTER_UID ) { [ unowned self] ( channel, uid, elapsed) -> Void in
124+ let result = agoraKit. joinChannel ( byToken: nil , channelId: channelName, info: nil , uid: 0 ) { [ unowned self] ( channel, uid, elapsed) -> Void in
112125 self . isJoined = true
113126 LogUtils . log ( message: " Join \( channel) with uid \( uid) elapsed \( elapsed) ms " , level: . info)
114127 }
@@ -138,7 +151,7 @@ class LiveStreamingMain: BaseViewController {
138151 let videoCanvas = AgoraRtcVideoCanvas ( )
139152 videoCanvas. uid = 0
140153 // the view to be binded
141- videoCanvas. view = localVideo . videoView
154+ videoCanvas. view = foregroundVideo . videoView
142155 videoCanvas. renderMode = . hidden
143156 agoraKit. setupLocalVideo ( videoCanvas)
144157
@@ -159,6 +172,31 @@ class LiveStreamingMain: BaseViewController {
159172 agoraKit. setClientRole ( . audience, options: options)
160173 }
161174
175+ @IBAction func onTapForegroundVideo( _ sender: UIButton ) {
176+ isLocalVideoForeground = !isLocalVideoForeground
177+
178+ let localVideoCanvas = AgoraRtcVideoCanvas ( )
179+ localVideoCanvas. uid = 0
180+ localVideoCanvas. renderMode = . hidden
181+
182+ let remoteVideoCanvas = AgoraRtcVideoCanvas ( )
183+ remoteVideoCanvas. renderMode = . hidden
184+
185+ if ( isLocalVideoForeground) {
186+ localVideoCanvas. view = foregroundVideo. videoView
187+ remoteVideoCanvas. view = backgroundVideo. videoView
188+ } else {
189+ localVideoCanvas. view = foregroundVideo. videoView
190+ remoteVideoCanvas. view = backgroundVideo. videoView
191+ }
192+
193+ agoraKit. setupLocalVideo ( localVideoCanvas)
194+ if let uid = remoteUid {
195+ remoteVideoCanvas. uid = uid
196+ agoraKit. setupRemoteVideo ( remoteVideoCanvas)
197+ }
198+ }
199+
162200 @IBAction func onToggleClientRole( _ sender: UISwitch ) {
163201 let role : AgoraClientRole = sender. isOn ? . broadcaster : . audience
164202 updateClientRole ( role)
@@ -227,13 +265,16 @@ extension LiveStreamingMain: AgoraRtcEngineDelegate {
227265 func rtcEngine( _ engine: AgoraRtcEngineKit , didJoinedOfUid uid: UInt , elapsed: Int ) {
228266 LogUtils . log ( message: " remote user join: \( uid) \( elapsed) ms " , level: . info)
229267
268+ //record remote uid
269+ remoteUid = uid
270+
230271 // Only one remote video view is available for this
231272 // tutorial. Here we check if there exists a surface
232273 // view tagged as this uid.
233274 let videoCanvas = AgoraRtcVideoCanvas ( )
234275 videoCanvas. uid = uid
235276 // the view to be binded
236- videoCanvas. view = remoteVideo . videoView
277+ videoCanvas. view = backgroundVideo . videoView
237278 videoCanvas. renderMode = . hidden
238279 agoraKit. setupRemoteVideo ( videoCanvas)
239280 }
@@ -245,6 +286,11 @@ extension LiveStreamingMain: AgoraRtcEngineDelegate {
245286 func rtcEngine( _ engine: AgoraRtcEngineKit , didOfflineOfUid uid: UInt , reason: AgoraUserOfflineReason ) {
246287 LogUtils . log ( message: " remote user left: \( uid) reason \( reason) " , level: . info)
247288
289+ //clear remote uid
290+ if ( remoteUid == uid) {
291+ remoteUid = nil
292+ }
293+
248294 // to unlink your view from sdk, so that your view reference will be released
249295 // note the video will stay at its last frame, to completely remove it
250296 // you will need to remove the EAGL sublayer from your binded view
0 commit comments