@@ -60,7 +60,7 @@ class RTMPStreamingMain: BaseViewController {
6060 didSet {
6161 rtmpTextField. isEnabled = !isPublished
6262 transcodingSwitch. isEnabled = !isPublished
63- publishButton. title = isPublished ? " stop " : " push "
63+ publishButton. title = isPublished ? " stop " . localized : " push " . localized
6464 }
6565 }
6666
@@ -164,25 +164,29 @@ class RTMPStreamingMain: BaseViewController {
164164 if ( isPublished) {
165165 // stop rtmp streaming
166166 unpublishing = true
167- agoraKit. removePublishStreamUrl ( rtmpURL)
167+ agoraKit. stopRtmpStream ( rtmpURL)
168168 } else {
169169 // resign rtmp text field
170170 rtmpTextField. resignFirstResponder ( )
171-
172- // check whether we need to enable transcoding
173- let transcodingEnabled = transcodingSwitch. isOn
174- if ( transcodingEnabled) {
175- // we will use transcoding to composite multiple hosts' video
176- // therefore we have to create a livetranscoding object and call before addPublishStreamUrl
177- transcoding. size = CGSize ( width: CANVAS_WIDTH, height: CANVAS_HEIGHT)
178- agoraKit. setLiveTranscoding ( transcoding)
179- }
180- agoraKit. addPublishStreamUrl ( rtmpURL, transcodingEnabled: transcodingEnabled)
171+ startRtmpStreaming ( isTranscoding: transcodingSwitch. isOn, rtmpURL: rtmpURL)
181172 startRetryTimer ( )
182173 self . rtmpURL = rtmpURL
183174 }
184175 }
185176
177+ func startRtmpStreaming( isTranscoding: Bool , rtmpURL: String ) {
178+ if ( isTranscoding) {
179+ // we will use transcoding to composite multiple hosts' video
180+ // therefore we have to create a livetranscoding object and call before addPublishStreamUrl
181+ transcoding. size = CGSize ( width: CANVAS_WIDTH, height: CANVAS_HEIGHT)
182+ agoraKit. setLiveTranscoding ( transcoding)
183+ agoraKit. startRtmpStreamWithTranscoding ( rtmpURL, transcoding: transcoding)
184+ }
185+ else {
186+ agoraKit. startRtmpStreamWithoutTranscoding ( rtmpURL)
187+ }
188+ }
189+
186190 func startRetryTimer( ) {
187191 // begin timer to update progress
188192 if ( timer == nil ) {
@@ -192,7 +196,7 @@ class RTMPStreamingMain: BaseViewController {
192196 return
193197 }
194198 let transcodingEnabled = weakself. transcodingSwitch. isOn
195- weakself . agoraKit . addPublishStreamUrl ( rtmpURL , transcodingEnabled: transcodingEnabled )
199+ self ? . startRtmpStreaming ( isTranscoding : transcodingEnabled, rtmpURL : rtmpURL )
196200 } )
197201 }
198202 }
@@ -275,7 +279,7 @@ extension RTMPStreamingMain: AgoraRtcEngineDelegate {
275279 user. uid = uid
276280 self . transcoding. add ( user)
277281 // remember you need to call setLiveTranscoding again if you changed the layout
278- agoraKit. setLiveTranscoding ( transcoding)
282+ agoraKit. updateRtmpTranscoding ( transcoding)
279283 }
280284 }
281285
@@ -305,7 +309,7 @@ extension RTMPStreamingMain: AgoraRtcEngineDelegate {
305309 }
306310 remoteUid = nil
307311 // remember you need to call setLiveTranscoding again if you changed the layout
308- agoraKit. setLiveTranscoding ( transcoding)
312+ agoraKit. updateRtmpTranscoding ( transcoding)
309313 }
310314 }
311315
@@ -315,62 +319,45 @@ extension RTMPStreamingMain: AgoraRtcEngineDelegate {
315319 /// @param reason
316320 func rtcEngine( _ engine: AgoraRtcEngineKit , rtmpStreamingChangedToState url: String , state: AgoraRtmpStreamingState , errorCode: AgoraRtmpStreamingErrorCode ) {
317321 LogUtils . log ( message: " rtmp streaming: \( url) state \( state. rawValue) error \( errorCode. rawValue) " , level: . info)
318- if ( state == . running) {
319- self . showAlert ( title: " Notice " , message: " RTMP Publish Success " )
320- isPublished = true
321- stopRetryTimer ( )
322- } else if ( state == . failure) {
323- self . showAlert ( title: " Error " , message: " RTMP Publish Failed: \( errorCode. rawValue) " )
324- stopRetryTimer ( )
325- } else if ( state == . idle) {
326- self . showAlert ( title: " Notice " , message: " RTMP Publish Stopped " )
327- isPublished = false
328- }
329- }
330-
331- func rtcEngine( _ engine: AgoraRtcEngineKit , rtmpStreamingEventWithUrl url: String , eventCode: AgoraRtmpStreamingEvent ) {
332- if ( eventCode == . urlAlreadyInUse) {
333- self . showAlert ( title: " Error " , message: " The URL is already in Use. " )
322+ guard let rtmpURL = rtmpTextField. text else {
323+ return
334324 }
335- }
336-
337- func rtcEngine( _ engine: AgoraRtcEngineKit , streamPublishedWithUrl url: String , errorCode: AgoraErrorCode ) {
338- if ( errorCode == AgoraErrorCode . noError) {
339- retried = 0
340- stopRetryTimer ( )
341- } else {
342- switch errorCode {
343- case . failed , . timedOut, . publishStreamInternalServerError:
344- engine. removePublishStreamUrl ( url)
345- break
346- case . publishStreamNotFound:
347- if retried >= MAX_RETRY_TIMES {
348- return
349- }
350- guard let rtmpURL = rtmpTextField. text else {
351- return
352- }
353- let transcodingEnabled = transcodingSwitch. isOn
354- agoraKit. addPublishStreamUrl ( rtmpURL, transcodingEnabled: transcodingEnabled)
325+ if state == . running {
326+ if errorCode == . streamingErrorCodeOK {
327+ self . showAlert ( title: " Notice " , message: " RTMP Publish Success " )
328+ isPublished = true
329+ stopRetryTimer ( )
330+ retried = 0
331+ }
332+ } else if state == . failure {
333+ agoraKit. stopRtmpStream ( rtmpURL)
334+ if errorCode == . streamingErrorCodeInternalServerError || errorCode == . streamingErrorCodeConnectionTimeout {
335+ self . showAlert ( title: " Error " , message: " RTMP Publish Failed: \( errorCode. rawValue) " )
336+ stopRetryTimer ( )
337+ unpublishing = true
338+ }
339+ } else if state == . idle {
340+ if unpublishing {
341+ unpublishing = false
342+ self . showAlert ( title: " Notice " , message: " RTMP Publish Stopped " )
343+ isPublished = false
344+ }
345+ else if retried >= MAX_RETRY_TIMES{
346+ stopRetryTimer ( )
347+ retried = 0
348+ self . showAlert ( title: " Notice " , message: " RTMP Publish Stopped " )
349+ isPublished = false
350+ }
351+ else {
355352 retried += 1
356- default :
357- print ( " unhandled rtmp streaming error: \( errorCode. rawValue) " )
353+ startRtmpStreaming ( isTranscoding: transcodingSwitch. isOn, rtmpURL: rtmpURL)
358354 }
359355 }
360356 }
361357
362- func rtcEngine( _ engine: AgoraRtcEngineKit , streamUnpublishedWithUrl url: String ) {
363- if unpublishing || retried >= MAX_RETRY_TIMES {
364- return
365- }
366- guard let rtmpURL = rtmpTextField. text else {
367- return
368- }
369- let transcodingEnabled = transcodingSwitch. isOn
370- agoraKit. addPublishStreamUrl ( rtmpURL, transcodingEnabled: transcodingEnabled)
371- retried += 1
372- if ( unpublishing) {
373- unpublishing = false ;
358+ func rtcEngine( _ engine: AgoraRtcEngineKit , rtmpStreamingEventWithUrl url: String , eventCode: AgoraRtmpStreamingEvent ) {
359+ if ( eventCode == . urlAlreadyInUse) {
360+ self . showAlert ( title: " Error " , message: " The URL is already in Use. " )
374361 }
375362 }
376363
0 commit comments