|
21 | 21 | import com.yanzhenjie.permission.AndPermission;
|
22 | 22 | import com.yanzhenjie.permission.runtime.Permission;
|
23 | 23 |
|
| 24 | +import io.agora.api.component.Constant; |
24 | 25 | import io.agora.api.example.R;
|
25 | 26 | import io.agora.api.example.annotation.Example;
|
26 | 27 | import io.agora.api.example.common.BaseFragment;
|
@@ -66,7 +67,7 @@ public class RTMPStreaming extends BaseFragment implements View.OnClickListener
|
66 | 67 | private int myUid;
|
67 | 68 | private boolean joined = false, publishing = false;
|
68 | 69 | private VideoEncoderConfiguration.VideoDimensions dimensions = VD_640x360;
|
69 |
| - private LiveTranscoding transcoding = new LiveTranscoding(); |
| 70 | + private LiveTranscoding transcoding; |
70 | 71 | /**
|
71 | 72 | * Maximum number of users participating in transcoding (even number)
|
72 | 73 | */
|
@@ -177,7 +178,6 @@ private void joinChannel(String channelId) {
|
177 | 178 | if (context == null) {
|
178 | 179 | return;
|
179 | 180 | }
|
180 |
| - |
181 | 181 | // Create render view by RtcEngine
|
182 | 182 | SurfaceView surfaceView = RtcEngine.CreateRendererView(context);
|
183 | 183 | // Local video is on the top
|
@@ -239,6 +239,7 @@ private void startPublish() {
|
239 | 239 | if (transCodeSwitch.isChecked()) {
|
240 | 240 | /**LiveTranscoding: A class for managing user-specific CDN live audio/video transcoding settings.
|
241 | 241 | * 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>*/
|
| 242 | + transcoding = new LiveTranscoding(); |
242 | 243 | transcoding.width = dimensions.height;
|
243 | 244 | transcoding.height = dimensions.width;
|
244 | 245 | /**The transcodingUser class which defines the video properties of the user displaying the
|
@@ -299,12 +300,11 @@ private void startPublish() {
|
299 | 300 | int code = engine.addPublishStreamUrl(et_url.getText().toString(), transCodeSwitch.isChecked());
|
300 | 301 | /**Prevent repeated entry*/
|
301 | 302 | publish.setEnabled(false);
|
| 303 | + /**Prevent duplicate clicks*/ |
| 304 | + transCodeSwitch.setEnabled(false); |
302 | 305 | }
|
303 | 306 |
|
304 | 307 | private void stopPublish() {
|
305 |
| - publishing = false; |
306 |
| - publish.setEnabled(true); |
307 |
| - publish.setText(getString(R.string.publish)); |
308 | 308 | /**Removes an RTMP stream from the CDN.
|
309 | 309 | * This method removes the RTMP URL address (added by addPublishStreamUrl) from a CDN live
|
310 | 310 | * stream. The SDK reports the result of this method call in the onRtmpStreamingStateChanged callback.
|
@@ -368,7 +368,6 @@ public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
|
368 | 368 | handler.post(new Runnable() {
|
369 | 369 | @Override
|
370 | 370 | public void run() {
|
371 |
| - transCodeSwitch.setEnabled(false); |
372 | 371 | join.setEnabled(true);
|
373 | 372 | join.setText(getString(R.string.leave));
|
374 | 373 | publish.setEnabled(true);
|
@@ -511,12 +510,27 @@ public void onRtmpStreamingStateChanged(String url, int state, int errCode) {
|
511 | 510 | if (state == Constants.RTMP_STREAM_PUBLISH_STATE_RUNNING) {
|
512 | 511 | /**After confirming the successful push, make changes to the UI.*/
|
513 | 512 | publishing = true;
|
514 |
| - handler.post(new Runnable() { |
515 |
| - @Override |
516 |
| - public void run() { |
517 |
| - publish.setEnabled(true); |
518 |
| - publish.setText(getString(R.string.stoppublish)); |
519 |
| - } |
| 513 | + handler.post(() -> { |
| 514 | + publish.setEnabled(true); |
| 515 | + publish.setText(getString(R.string.stoppublish)); |
| 516 | + }); |
| 517 | + } else if (state == Constants.RTMP_STREAM_PUBLISH_STATE_FAILURE) { |
| 518 | + /**if failed, make changes to the UI.*/ |
| 519 | + publishing = true; |
| 520 | + handler.post(() -> { |
| 521 | + publish.setEnabled(true); |
| 522 | + publish.setText(getString(R.string.publish)); |
| 523 | + transCodeSwitch.setEnabled(true); |
| 524 | + publishing = false; |
| 525 | + }); |
| 526 | + } else if (state == Constants.RTMP_STREAM_PUBLISH_STATE_IDLE) { |
| 527 | + /**Push stream not started or ended, make changes to the UI.*/ |
| 528 | + publishing = true; |
| 529 | + handler.post(() -> { |
| 530 | + publish.setEnabled(true); |
| 531 | + publish.setText(getString(R.string.publish)); |
| 532 | + transCodeSwitch.setEnabled(true); |
| 533 | + publishing = false; |
520 | 534 | });
|
521 | 535 | }
|
522 | 536 | }
|
@@ -585,6 +599,17 @@ public void run() {
|
585 | 599 | Note: The video will stay at its last frame, to completely remove it you will need to
|
586 | 600 | remove the SurfaceView from its parent*/
|
587 | 601 | engine.setupRemoteVideo(new VideoCanvas(null, RENDER_MODE_HIDDEN, uid));
|
| 602 | + if(transcoding != null) { |
| 603 | + /**Removes a user from CDN live. |
| 604 | + * @return |
| 605 | + * 0: Success. |
| 606 | + * < 0: Failure.*/ |
| 607 | + int code = transcoding.removeUser(uid); |
| 608 | + if (code == Constants.ERR_OK) { |
| 609 | + /**refresh transCoding configuration*/ |
| 610 | + engine.setLiveTranscoding(transcoding); |
| 611 | + } |
| 612 | + } |
588 | 613 | }
|
589 | 614 | });
|
590 | 615 | }
|
|
0 commit comments