11package io .agora .api .example .examples .advanced ;
22
33import android .content .Context ;
4+ import android .os .AsyncTask ;
45import android .os .Bundle ;
56import android .text .TextUtils ;
67import android .util .Log ;
3738import io .agora .rtc .video .VideoEncoderConfiguration ;
3839
3940import static io .agora .api .example .common .model .Examples .ADVANCED ;
41+ import static io .agora .rtc .Constants .ERR_FAILED ;
42+ import static io .agora .rtc .Constants .ERR_OK ;
43+ import static io .agora .rtc .Constants .ERR_PUBLISH_STREAM_INTERNAL_SERVER_ERROR ;
44+ import static io .agora .rtc .Constants .ERR_PUBLISH_STREAM_NOT_FOUND ;
45+ import static io .agora .rtc .Constants .ERR_TIMEDOUT ;
4046import static io .agora .rtc .video .VideoCanvas .RENDER_MODE_HIDDEN ;
4147import static io .agora .rtc .video .VideoEncoderConfiguration .FRAME_RATE .FRAME_RATE_FPS_15 ;
4248import static io .agora .rtc .video .VideoEncoderConfiguration .ORIENTATION_MODE .ORIENTATION_MODE_ADAPTIVE ;
@@ -70,6 +76,9 @@ public class RTMPStreaming extends BaseFragment implements View.OnClickListener
7076 private boolean joined = false , publishing = false ;
7177 private VideoEncoderConfiguration .VideoDimensions dimensions = VD_640x360 ;
7278 private LiveTranscoding transcoding ;
79+ private static final Integer MAX_RETRY_TIMES = 3 ;
80+ private int retried = 0 ;
81+ private boolean unpublishing = false ;
7382 /**
7483 * Maximum number of users participating in transcoding (even number)
7584 */
@@ -166,6 +175,7 @@ public void onClick(View v) {
166175 }
167176 } else if (v .getId () == R .id .btn_publish ) {
168177 /**Ensure that the user joins a channel before calling this method.*/
178+ retried = 0 ;
169179 if (joined && !publishing ) {
170180 startPublish ();
171181 } else if (joined && publishing ) {
@@ -302,6 +312,9 @@ private void startPublish() {
302312 * Ensure that the user joins a channel before calling this method.
303313 * This method adds only one stream HTTP/HTTPS URL address each time it is called.*/
304314 int code = engine .addPublishStreamUrl (et_url .getText ().toString (), transCodeSwitch .isChecked ());
315+ if (code == 0 ){
316+ retryTask .execute ();
317+ }
305318 /**Prevent repeated entry*/
306319 publish .setEnabled (false );
307320 /**Prevent duplicate clicks*/
@@ -324,6 +337,7 @@ private void stopPublish() {
324337 * Ensure that the user joins a channel before calling this method.
325338 * This method applies to Live Broadcast only.
326339 * This method removes only one stream RTMP URL address each time it is called.*/
340+ unpublishing = true ;
327341 int ret = engine .removePublishStreamUrl (et_url .getText ().toString ());
328342 }
329343
@@ -523,19 +537,33 @@ public void onRtmpStreamingStateChanged(String url, int state, int errCode) {
523537 if (state == Constants .RTMP_STREAM_PUBLISH_STATE_RUNNING ) {
524538 /**After confirming the successful push, make changes to the UI.*/
525539 publishing = true ;
540+ retryTask .cancel (true );
526541 handler .post (() -> {
527542 publish .setEnabled (true );
528543 publish .setText (getString (R .string .stoppublish ));
529544 });
530545 } else if (state == Constants .RTMP_STREAM_PUBLISH_STATE_FAILURE ) {
531546 /**if failed, make changes to the UI.*/
532547 publishing = true ;
548+ retryTask .cancel (true );
533549 handler .post (() -> {
534550 publish .setEnabled (true );
535551 publish .setText (getString (R .string .publish ));
536552 transCodeSwitch .setEnabled (true );
537553 publishing = false ;
538554 });
555+ switch (errCode ){
556+ case ERR_FAILED :
557+ case ERR_TIMEDOUT :
558+ case ERR_PUBLISH_STREAM_INTERNAL_SERVER_ERROR :
559+ engine .removePublishStreamUrl (url );
560+ break ;
561+ case ERR_PUBLISH_STREAM_NOT_FOUND :
562+ if (retried < MAX_RETRY_TIMES ){
563+ engine .addPublishStreamUrl (et_url .getText ().toString (), transCodeSwitch .isChecked ());
564+ }
565+ break ;
566+ }
539567 } else if (state == Constants .RTMP_STREAM_PUBLISH_STATE_IDLE ) {
540568 /**Push stream not started or ended, make changes to the UI.*/
541569 publishing = true ;
@@ -548,6 +576,36 @@ public void onRtmpStreamingStateChanged(String url, int state, int errCode) {
548576 }
549577 }
550578
579+ /**
580+ * Reports the result of calling the removePublishStreamUrl method.
581+ * This callback indicates whether you have successfully removed an RTMP or RTMPS stream from the CDN.
582+ * @param url The CDN streaming URL.
583+ */
584+ @ Override
585+ public void onStreamUnpublished (String url ) {
586+ if (url != null && !unpublishing && retried < MAX_RETRY_TIMES ){
587+ engine .addPublishStreamUrl (et_url .getText ().toString (), transCodeSwitch .isChecked ());
588+ retried ++;
589+ }
590+ if (unpublishing ){
591+ unpublishing = false ;
592+ }
593+ }
594+
595+ /**
596+ * Reports the result of calling the addPublishStreamUrl method.
597+ * This callback indicates whether you have successfully added an RTMP or RTMPS stream to the CDN.
598+ * @param url The CDN streaming URL.
599+ * @param error The detailed error information:
600+ */
601+ @ Override
602+ public void onStreamPublished (String url , int error ) {
603+ if (error == ERR_OK ){
604+ retried = 0 ;
605+ retryTask .cancel (true );
606+ }
607+ }
608+
551609 /**Occurs when a remote user (Communication)/host (Live Broadcast) joins the channel.
552610 * @param uid ID of the user whose audio state changes.
553611 * @param elapsed Time delay (ms) from the local user calling joinChannel/setClientRole
@@ -618,7 +676,7 @@ public void run() {
618676 * 0: Success.
619677 * < 0: Failure.*/
620678 int code = transcoding .removeUser (uid );
621- if (code == Constants . ERR_OK ) {
679+ if (code == ERR_OK ) {
622680 /**refresh transCoding configuration*/
623681 engine .setLiveTranscoding (transcoding );
624682 }
@@ -627,4 +685,20 @@ public void run() {
627685 });
628686 }
629687 };
688+
689+ private final AsyncTask retryTask = new AsyncTask () {
690+ @ Override
691+ protected Object doInBackground (Object [] objects ) {
692+ Integer result = null ;
693+ for (int i = 0 ; i < MAX_RETRY_TIMES ; i ++) {
694+ try {
695+ Thread .sleep (60 * 1000 );
696+ } catch (InterruptedException e ) {
697+ Log .e (TAG , e .getMessage ());
698+ }
699+ result = engine .addPublishStreamUrl (et_url .getText ().toString (), transCodeSwitch .isChecked ());
700+ }
701+ return result ;
702+ }
703+ };
630704}
0 commit comments