1010import android .view .View ;
1111import android .view .ViewGroup ;
1212import android .widget .Button ;
13- import android .widget .CompoundButton ;
1413import android .widget .EditText ;
1514import android .widget .FrameLayout ;
1615import android .widget .LinearLayout ;
2221import com .yanzhenjie .permission .AndPermission ;
2322import com .yanzhenjie .permission .runtime .Permission ;
2423
25- import io .agora .api .component .Constant ;
2624import io .agora .api .example .MainApplication ;
2725import io .agora .api .example .R ;
2826import io .agora .api .example .annotation .Example ;
3331import io .agora .rtc .RtcEngine ;
3432import io .agora .rtc .live .LiveTranscoding ;
3533import io .agora .rtc .models .ChannelMediaOptions ;
36- import io .agora .rtc .video .AgoraImage ;
3734import io .agora .rtc .video .VideoCanvas ;
3835import io .agora .rtc .video .VideoEncoderConfiguration ;
3936
4340import static io .agora .rtc .Constants .ERR_PUBLISH_STREAM_INTERNAL_SERVER_ERROR ;
4441import static io .agora .rtc .Constants .ERR_PUBLISH_STREAM_NOT_FOUND ;
4542import static io .agora .rtc .Constants .ERR_TIMEDOUT ;
43+ import static io .agora .rtc .Constants .RTMP_STREAM_PUBLISH_ERROR_CONNECTION_TIMEOUT ;
44+ import static io .agora .rtc .Constants .RTMP_STREAM_PUBLISH_ERROR_INTERNAL_SERVER_ERROR ;
45+ import static io .agora .rtc .Constants .RTMP_STREAM_PUBLISH_ERROR_OK ;
4646import static io .agora .rtc .video .VideoCanvas .RENDER_MODE_HIDDEN ;
47- import static io .agora .rtc .video .VideoEncoderConfiguration .FRAME_RATE .FRAME_RATE_FPS_15 ;
48- import static io .agora .rtc .video .VideoEncoderConfiguration .ORIENTATION_MODE .ORIENTATION_MODE_ADAPTIVE ;
4947import static io .agora .rtc .video .VideoEncoderConfiguration .STANDARD_BITRATE ;
5048import static io .agora .rtc .video .VideoEncoderConfiguration .VD_640x360 ;
5149
@@ -75,7 +73,7 @@ public class RTMPStreaming extends BaseFragment implements View.OnClickListener
7573 private int myUid ;
7674 private boolean joined = false , publishing = false ;
7775 private VideoEncoderConfiguration .VideoDimensions dimensions = VD_640x360 ;
78- private LiveTranscoding transcoding ;
76+ private LiveTranscoding transcoding = new LiveTranscoding () ;
7977 private static final Integer MAX_RETRY_TIMES = 3 ;
8078 private int retried = 0 ;
8179 private boolean unpublishing = false ;
@@ -254,7 +252,6 @@ private void startPublish() {
254252 if (transCodeSwitch .isChecked ()) {
255253 /**LiveTranscoding: A class for managing user-specific CDN live audio/video transcoding settings.
256254 * See <a href="https://docs.agora.io/en/Video/API%20Reference/java/classio_1_1agora_1_1rtc_1_1live_1_1_live_transcoding.html"></a>*/
257- transcoding = new LiveTranscoding ();
258255 transcoding .width = dimensions .height ;
259256 transcoding .height = dimensions .width ;
260257 /**The transcodingUser class which defines the video properties of the user displaying the
@@ -271,49 +268,9 @@ private void startPublish() {
271268 * 0: Success.
272269 * <0: Failure.*/
273270 int ret = transcoding .addUser (localTranscodingUser );
274- /**Sets the video layout and audio settings for CDN live.
275- * The SDK triggers the onTranscodingUpdated callback when you call this method to update
276- * the LiveTranscodingclass. If you call this method to set the LiveTranscoding class for
277- * the first time, the SDK does not trigger the onTranscodingUpdated callback.
278- * @param transcoding Sets the CDN live audio/video transcoding settings See
279- * <a href="https://docs.agora.io/en/Video/API%20Reference/java/classio_1_1agora_1_1rtc_1_1live_1_1_live_transcoding.html"></a>
280- * @return
281- * 0: Success.
282- * <0: Failure.
283- * PS:
284- * This method applies to Live Broadcast only.
285- * Ensure that you enable the RTMP Converter service before using this function. See
286- * Prerequisites in Push Streams to CDN.
287- * Ensure that you call the setClientRole method and set the user role as the host.
288- * Ensure that you call the setLiveTranscoding method before calling the addPublishStreamUrl method.*/
289- engine .setLiveTranscoding (transcoding );
290271 }
291- /**Publishes the local stream to the CDN.
292- * The addPublishStreamUrl method call triggers the onRtmpStreamingStateChanged callback on
293- * the local client to report the state of adding a local stream to the CDN.
294- * @param url The CDN streaming URL in the RTMP format. The maximum length of this parameter
295- * is 1024 bytes. The URL address must not contain special characters, such as
296- * Chinese language characters.
297- * @param transcodingEnabled Sets whether transcoding is enabled/disabled. If you set this
298- * parameter as true, ensure that you call the setLiveTranscoding
299- * method before this method.
300- * true: Enable transcoding. To transcode the audio or video
301- * streams when publishing them to CDN live, often used for
302- * combining the audio and video streams of multiple hosts in CDN live.
303- * false: Disable transcoding.
304- * @return
305- * 0: Success.
306- * < 0: Failure.
307- * ERR_INVALID_ARGUMENT(2): Invalid parameter, usually because the URL address is null or the string length is 0.
308- * ERR_NOT_INITIALIZED(7): You have not initialized RtcEngine when publishing the stream.
309- * PS:
310- * Ensure that you enable the RTMP Converter service before using this function. See
311- * Prerequisites in Push Streams to CDN.
312- * This method applies to Live Broadcast only.
313- * Ensure that the user joins a channel before calling this method.
314- * This method adds only one stream HTTP/HTTPS URL address each time it is called.*/
315- int code = engine .addPublishStreamUrl (et_url .getText ().toString (), transCodeSwitch .isChecked ());
316- if (code == 0 ){
272+
273+ if (startRtmpStreaming () == 0 ){
317274 retryTask = new AsyncTask () {
318275 @ Override
319276 protected Object doInBackground (Object [] objects ) {
@@ -338,6 +295,17 @@ protected Object doInBackground(Object[] objects) {
338295 transCodeSwitch .setEnabled (false );
339296 }
340297
298+ private int startRtmpStreaming (){
299+ int code ;
300+ if (transCodeSwitch .isChecked ()){
301+ code = engine .startRtmpStreamWithTranscoding (et_url .getText ().toString (), transcoding );
302+ }
303+ else {
304+ code = engine .startRtmpStreamWithoutTranscoding (et_url .getText ().toString ());
305+ }
306+ return code ;
307+ }
308+
341309 private void stopPublish () {
342310 /**Removes an RTMP stream from the CDN.
343311 * This method removes the RTMP URL address (added by addPublishStreamUrl) from a CDN live
@@ -356,7 +324,7 @@ private void stopPublish() {
356324 * This method removes only one stream RTMP URL address each time it is called.*/
357325 unpublishing = true ;
358326 retryTask .cancel (true );
359- int ret = engine .removePublishStreamUrl (et_url .getText ().toString ());
327+ int ret = engine .stopRtmpStream (et_url .getText ().toString ());
360328 }
361329
362330 /**
@@ -517,7 +485,7 @@ public void onRemoteVideoStateChanged(int uid, int state, int reason, int elapse
517485 * RTMP_STREAM_PUBLISH_STATE_FAILURE(4): The RTMP streaming fails. See the errCode parameter
518486 * for the detailed error information. You can also call the addPublishStreamUrl
519487 * method to publish the RTMP streaming again.
520- * @param errCode The detailed error information for streaming:
488+ * @param errorType The detailed error information for streaming:
521489 * RTMP_STREAM_PUBLISH_ERROR_OK(0): The RTMP streaming publishes successfully.
522490 * RTMP_STREAM_PUBLISH_ERROR_INVALID_ARGUMEN(1): Invalid argument used. If, for example,
523491 * you do not call the setLiveTranscoding method to configure the LiveTranscoding
@@ -540,28 +508,44 @@ public void onRemoteVideoStateChanged(int uid, int state, int reason, int elapse
540508 * RTMP_STREAM_PUBLISH_ERROR_FORMAT_NOT_SUPPORTED(10): The format of the RTMP streaming
541509 * URL is not supported. Check whether the URL format is correct.*/
542510 @ Override
543- public void onRtmpStreamingStateChanged (String url , int state , int errCode ) {
544- super .onRtmpStreamingStateChanged (url , state , errCode );
545- Log .i (TAG , "onRtmpStreamingStateChanged->" + url + ", state->" + state + ", errCode ->" + errCode );
511+ public void onRtmpStreamingStateChanged (String url , int state , int errorType ) {
512+ super .onRtmpStreamingStateChanged (url , state , errorType );
513+ Log .i (TAG , "onRtmpStreamingStateChanged->" + url + ", state->" + state + ", errorType ->" + errorType );
546514 if (state == Constants .RTMP_STREAM_PUBLISH_STATE_RUNNING ) {
547515 /**After confirming the successful push, make changes to the UI.*/
548- publishing = true ;
549- retryTask .cancel (true );
550- handler .post (() -> {
551- publish .setEnabled (true );
552- publish .setText (getString (R .string .stoppublish ));
553- });
516+ if (errorType == RTMP_STREAM_PUBLISH_ERROR_OK ){
517+ publishing = true ;
518+ retried = 0 ;
519+ retryTask .cancel (true );
520+ handler .post (() -> {
521+ publish .setEnabled (true );
522+ publish .setText (getString (R .string .stoppublish ));
523+ });
524+ }
554525 } else if (state == Constants .RTMP_STREAM_PUBLISH_STATE_FAILURE ) {
555- /**if failed, make changes to the UI.*/
556- publishing = true ;
557- retryTask .cancel (true );
558- handler .post (() -> {
559- publish .setEnabled (true );
560- publish .setText (getString (R .string .publish ));
561- transCodeSwitch .setEnabled (true );
562- publishing = false ;
563- });
526+ engine .stopRtmpStream (et_url .getText ().toString ());
527+ if ((errorType == RTMP_STREAM_PUBLISH_ERROR_CONNECTION_TIMEOUT
528+ || errorType == RTMP_STREAM_PUBLISH_ERROR_INTERNAL_SERVER_ERROR ))
529+ {
530+ /**if failed, make changes to the UI.*/
531+ publishing = true ;
532+ retryTask .cancel (true );
533+ handler .post (() -> {
534+ publish .setEnabled (true );
535+ publish .setText (getString (R .string .publish ));
536+ transCodeSwitch .setEnabled (true );
537+ publishing = false ;
538+ });
539+ }
540+ else if (url != null && !unpublishing && retried < MAX_RETRY_TIMES
541+ ){
542+ startRtmpStreaming ();
543+ retried ++;
544+ }
564545 } else if (state == Constants .RTMP_STREAM_PUBLISH_STATE_IDLE ) {
546+ if (unpublishing ){
547+ unpublishing = false ;
548+ }
565549 /**Push stream not started or ended, make changes to the UI.*/
566550 publishing = true ;
567551 handler .post (() -> {
@@ -573,51 +557,6 @@ public void onRtmpStreamingStateChanged(String url, int state, int errCode) {
573557 }
574558 }
575559
576- /**
577- * Reports the result of calling the removePublishStreamUrl method.
578- * This callback indicates whether you have successfully removed an RTMP or RTMPS stream from the CDN.
579- * @param url The CDN streaming URL.
580- */
581- @ Override
582- public void onStreamUnpublished (String url ) {
583- if (url != null && !unpublishing && retried < MAX_RETRY_TIMES ){
584- engine .addPublishStreamUrl (et_url .getText ().toString (), transCodeSwitch .isChecked ());
585- retried ++;
586- }
587- if (unpublishing ){
588- unpublishing = false ;
589- }
590- }
591-
592- /**
593- * Reports the result of calling the addPublishStreamUrl method.
594- * This callback indicates whether you have successfully added an RTMP or RTMPS stream to the CDN.
595- * @param url The CDN streaming URL.
596- * @param error The detailed error information:
597- */
598- @ Override
599- public void onStreamPublished (String url , int error ) {
600- if (error == ERR_OK ){
601- retried = 0 ;
602- retryTask .cancel (true );
603- }
604- else {
605- switch (error ){
606- case ERR_FAILED :
607- case ERR_TIMEDOUT :
608- case ERR_PUBLISH_STREAM_INTERNAL_SERVER_ERROR :
609- engine .removePublishStreamUrl (url );
610- break ;
611- case ERR_PUBLISH_STREAM_NOT_FOUND :
612- if (retried < MAX_RETRY_TIMES ){
613- engine .addPublishStreamUrl (et_url .getText ().toString (), transCodeSwitch .isChecked ());
614- retried ++;
615- }
616- break ;
617- }
618- }
619- }
620-
621560 /**Occurs when a remote user (Communication)/host (Live Broadcast) joins the channel.
622561 * @param uid ID of the user whose audio state changes.
623562 * @param elapsed Time delay (ms) from the local user calling joinChannel/setClientRole
@@ -655,9 +594,9 @@ public void onUserJoined(int uid, int elapsed) {
655594 transcodingUser .width = transcoding .width ;
656595 transcodingUser .height = transcoding .height / MAXUserCount ;
657596 transcodingUser .uid = uid ;
658- int ret = transcoding .addUser (transcodingUser );
597+ transcoding .addUser (transcodingUser );
659598 /**refresh transCoding configuration*/
660- engine .setLiveTranscoding (transcoding );
599+ int ret = engine .updateRtmpTranscoding (transcoding );
661600 }
662601 }
663602
@@ -698,7 +637,7 @@ public void run() {
698637 int code = transcoding .removeUser (uid );
699638 if (code == ERR_OK ) {
700639 /**refresh transCoding configuration*/
701- engine .setLiveTranscoding (transcoding );
640+ engine .updateRtmpTranscoding (transcoding );
702641 }
703642 }
704643 }
0 commit comments