/
LiveStreaming.java
584 lines (544 loc) · 30.2 KB
/
LiveStreaming.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package io.agora.api.example.examples.advanced;
import static io.agora.api.example.common.model.Examples.ADVANCED;
import static io.agora.rtc.video.VideoCanvas.RENDER_MODE_HIDDEN;
import static io.agora.rtc.video.VideoEncoderConfiguration.STANDARD_BITRATE;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;
import java.util.Random;
import io.agora.api.example.MainApplication;
import io.agora.api.example.R;
import io.agora.api.example.annotation.Example;
import io.agora.api.example.common.BaseFragment;
import io.agora.api.example.utils.CommonUtil;
import io.agora.api.example.utils.TokenUtils;
import io.agora.rtc.Constants;
import io.agora.rtc.IRtcEngineEventHandler;
import io.agora.rtc.RtcEngine;
import io.agora.rtc.models.ChannelMediaOptions;
import io.agora.rtc.models.ClientRoleOptions;
import io.agora.rtc.video.VideoCanvas;
import io.agora.rtc.video.VideoEncoderConfiguration;
/**
* This demo demonstrates how to make a one-to-one video call
*/
@Example(
index = 23,
group = ADVANCED,
name = R.string.item_livestreaming,
actionId = R.id.action_mainFragment_to_live_streaming,
tipsId = R.string.livestreaming
)
public class LiveStreaming extends BaseFragment implements View.OnClickListener {
private static final String TAG = LiveStreaming.class.getSimpleName();
private FrameLayout foreGroundVideo, backGroundVideo;
private Button join, publish, latency, super_resolution;
private EditText et_channel;
private RtcEngine engine;
private int myUid;
private int remoteUid;
private boolean joined = false;
private boolean isHost = false;
private boolean isLowLatency = false;
private boolean isLocalVideoForeground = true;
private boolean enableSuperResolution = false;
private SeekBar sbCameraRotate;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_live_streaming, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
join = view.findViewById(R.id.btn_join);
publish = view.findViewById(R.id.btn_publish);
latency = view.findViewById(R.id.btn_latency);
et_channel = view.findViewById(R.id.et_channel);
super_resolution = view.findViewById(R.id.btn_super_resolution);
super_resolution.setOnClickListener(this);
latency.setEnabled(false);
publish.setEnabled(false);
view.findViewById(R.id.btn_join).setOnClickListener(this);
view.findViewById(R.id.btn_publish).setOnClickListener(this);
view.findViewById(R.id.btn_latency).setOnClickListener(this);
view.findViewById(R.id.foreground_video).setOnClickListener(this);
foreGroundVideo = view.findViewById(R.id.background_video);
backGroundVideo = view.findViewById(R.id.foreground_video);
initCameraRotateView();
}
private void initCameraRotateView(){
TextView tvValue = getView().findViewById(R.id.tv_camera_rotation_value);
sbCameraRotate = getView().findViewById(R.id.sb_camera_rotation);
sbCameraRotate.setMax(3);
sbCameraRotate.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvValue.setText(progress * 90 + "");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
engine.setCameraCaptureRotation(progress);
}
});
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Check if the context is valid
Context context = getContext();
if (context == null) {
return;
}
try {
/**Creates an RtcEngine instance.
* @param context The context of Android Activity
* @param appId The App ID issued to you by Agora. See <a href="https://docs.agora.io/en/Agora%20Platform/token#get-an-app-id">
* How to get the App ID</a>
* @param handler IRtcEngineEventHandler is an abstract class providing default implementation.
* The SDK uses this class to report to the app on SDK runtime events.*/
engine = RtcEngine.create(context.getApplicationContext(), getString(R.string.agora_app_id), iRtcEngineEventHandler);
} catch (Exception e) {
e.printStackTrace();
getActivity().onBackPressed();
}
}
@Override
public void onDestroy() {
super.onDestroy();
/**leaveChannel and Destroy the RtcEngine instance*/
if (engine != null) {
engine.leaveChannel();
}
handler.post(RtcEngine::destroy);
engine = null;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_join) {
if (!joined) {
CommonUtil.hideInputBoard(getActivity(), et_channel);
// call when join button hit
String channelId = et_channel.getText().toString();
// Check permission
if (AndPermission.hasPermissions(this, Permission.Group.STORAGE, Permission.Group.MICROPHONE, Permission.Group.CAMERA)) {
joinChannel(channelId);
return;
}
// Request permission
AndPermission.with(this).runtime().permission(
Permission.Group.STORAGE,
Permission.Group.MICROPHONE,
Permission.Group.CAMERA
).onGranted(permissions ->
{
// Permissions Granted
joinChannel(channelId);
}).start();
} else {
remoteUid = 0;
isHost = false;
joined = false;
/**After joining a channel, the user must call the leaveChannel method to end the
* call before joining another channel. This method returns 0 if the user leaves the
* channel and releases all resources related to the call. This method call is
* asynchronous, and the user has not exited the channel when the method call returns.
* Once the user leaves the channel, the SDK triggers the onLeaveChannel callback.
* A successful leaveChannel method call triggers the following callbacks:
* 1:The local client: onLeaveChannel.
* 2:The remote client: onUserOffline, if the user leaving the channel is in the
* Communication channel, or is a BROADCASTER in the Live Broadcast profile.
* @returns 0: Success.
* < 0: Failure.
* PS:
* 1:If you call the destroy method immediately after calling the leaveChannel
* method, the leaveChannel process interrupts, and the SDK does not trigger
* the onLeaveChannel callback.
* 2:If you call the leaveChannel method during CDN live streaming, the SDK
* triggers the removeInjectStreamUrl method.*/
engine.leaveChannel();
engine.stopPreview();
foreGroundVideo.removeAllViews();
backGroundVideo.removeAllViews();
publish.setText(getString(R.string.enable_publish));
join.setText(getString(R.string.join));
publish.setEnabled(false);
latency.setEnabled(false);
super_resolution.setEnabled(false);
}
} else if (v.getId() == R.id.btn_publish) {
isHost = !isHost;
if(isHost){
engine.setClientRole(IRtcEngineEventHandler.ClientRole.CLIENT_ROLE_BROADCASTER);
}
else{
ClientRoleOptions clientRoleOptions = new ClientRoleOptions();
clientRoleOptions.audienceLatencyLevel = isLowLatency ? Constants.AUDIENCE_LATENCY_LEVEL_ULTRA_LOW_LATENCY : Constants.AUDIENCE_LATENCY_LEVEL_LOW_LATENCY;
engine.setClientRole(IRtcEngineEventHandler.ClientRole.CLIENT_ROLE_AUDIENCE, clientRoleOptions);
}
publish.setEnabled(false);
publish.setText(isHost ? getString(R.string.disnable_publish) : getString(R.string.enable_publish));
} else if (v.getId() == R.id.btn_latency) {
isLowLatency = !isLowLatency;
latency.setText(isLowLatency ? getString(R.string.disable_low_latency) : getString(R.string.enable_low_latency));
} else if (v.getId() == R.id.foreground_video) {
isLocalVideoForeground = !isLocalVideoForeground;
if (foreGroundVideo.getChildCount() > 0) {
foreGroundVideo.removeAllViews();
}
if (backGroundVideo.getChildCount() > 0) {
backGroundVideo.removeAllViews();
}
// Create render view by RtcEngine
SurfaceView localView = RtcEngine.CreateRendererView(getContext());
SurfaceView remoteView = RtcEngine.CreateRendererView(getContext());
if (isLocalVideoForeground){
// Add to the local container
foreGroundVideo.addView(localView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// Add to the remote container
backGroundVideo.addView(remoteView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// Setup remote video to render
engine.setupRemoteVideo(new VideoCanvas(remoteView, RENDER_MODE_HIDDEN, remoteUid));
// Setup local video to render your local camera preview
engine.setupLocalVideo(new VideoCanvas(localView, RENDER_MODE_HIDDEN, 0));
remoteView.setZOrderMediaOverlay(true);
remoteView.setZOrderOnTop(true);
}
else{
// Add to the local container
foreGroundVideo.addView(remoteView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// Add to the remote container
backGroundVideo.addView(localView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// Setup local video to render your local camera preview
engine.setupLocalVideo(new VideoCanvas(localView, RENDER_MODE_HIDDEN, 0));
// Setup remote video to render
engine.setupRemoteVideo(new VideoCanvas(remoteView, RENDER_MODE_HIDDEN, remoteUid));
localView.setZOrderMediaOverlay(true);
localView.setZOrderOnTop(true);
}
}else if(v.getId() == R.id.btn_super_resolution){
int ret = engine.enableRemoteSuperResolution(remoteUid, !enableSuperResolution);
if(ret!=0){
Log.w(TAG, String.format("enableRemoteSuperResolution error code %d ", ret));
}
}
}
private void joinChannel(String channelId) {
// Check if the context is valid
Context context = getContext();
if (context == null) {
return;
}
// Create render view by RtcEngine
SurfaceView surfaceView = RtcEngine.CreateRendererView(context);
if (foreGroundVideo.getChildCount() > 0) {
foreGroundVideo.removeAllViews();
}
// Add to the local container
foreGroundVideo.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// Setup local video to render your local camera preview
engine.setupLocalVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, 0));
// Set audio route to microPhone
engine.setDefaultAudioRoutetoSpeakerphone(true);
/** Sets the channel profile of the Agora RtcEngine.
CHANNEL_PROFILE_COMMUNICATION(0): (Default) The Communication profile.
Use this profile in one-on-one calls or group calls, where all users can talk freely.
CHANNEL_PROFILE_LIVE_BROADCASTING(1): The Live-Broadcast profile. Users in a live-broadcast
channel have a role as either broadcaster or audience. A broadcaster can both send and receive streams;
an audience can only receive streams.*/
engine.setChannelProfile(Constants.CHANNEL_PROFILE_LIVE_BROADCASTING);
/**In the demo, the default is to enter as the anchor.*/
engine.setClientRole(IRtcEngineEventHandler.ClientRole.CLIENT_ROLE_AUDIENCE);
// Enable video module
engine.enableVideo();
// Setup video encoding configs
engine.setVideoEncoderConfiguration(new VideoEncoderConfiguration(
((MainApplication)getActivity().getApplication()).getGlobalSettings().getVideoEncodingDimensionObject(),
VideoEncoderConfiguration.FRAME_RATE.valueOf(((MainApplication)getActivity().getApplication()).getGlobalSettings().getVideoEncodingFrameRate()),
STANDARD_BITRATE,
VideoEncoderConfiguration.ORIENTATION_MODE.valueOf(((MainApplication)getActivity().getApplication()).getGlobalSettings().getVideoEncodingOrientation())
));
/**Please configure accessToken in the string_config file.
* A temporary token generated in Console. A temporary token is valid for 24 hours. For details, see
* https://docs.agora.io/en/Agora%20Platform/token?platform=All%20Platforms#get-a-temporary-token
* A token generated at the server. This applies to scenarios with high-security requirements. For details, see
* https://docs.agora.io/en/cloud-recording/token_server_java?platform=Java*/
int uid = new Random(System.currentTimeMillis()).nextInt(1000) + 10000;
TokenUtils.gen(requireContext(), channelId, uid, accessToken -> {
/** Allows a user to join a channel.
if you do not specify the uid, we will generate the uid for you*/
ChannelMediaOptions option = new ChannelMediaOptions();
option.autoSubscribeAudio = true;
option.autoSubscribeVideo = true;
int res = engine.joinChannel(accessToken, channelId, "Extra Optional Data", uid, option);
if (res != 0) {
// Usually happens with invalid parameters
// Error code description can be found at:
// en: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_error_code.html
// cn: https://docs.agora.io/cn/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_error_code.html
showAlert(RtcEngine.getErrorDescription(Math.abs(res)));
return;
}
// Prevent repeated entry
join.setEnabled(false);
});
}
/**
* IRtcEngineEventHandler is an abstract class providing default implementation.
* The SDK uses this class to report to the app on SDK runtime events.
*/
private final IRtcEngineEventHandler iRtcEngineEventHandler = new IRtcEngineEventHandler() {
/**Reports a warning during SDK runtime.
* Warning code: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_warn_code.html*/
@Override
public void onWarning(int warn) {
Log.w(TAG, String.format("onWarning code %d message %s", warn, RtcEngine.getErrorDescription(warn)));
}
/**Reports an error during SDK runtime.
* Error code: https://docs.agora.io/en/Voice/API%20Reference/java/classio_1_1agora_1_1rtc_1_1_i_rtc_engine_event_handler_1_1_error_code.html*/
@Override
public void onError(int err) {
Log.e(TAG, String.format("onError code %d message %s", err, RtcEngine.getErrorDescription(err)));
showAlert(String.format("onError code %d message %s", err, RtcEngine.getErrorDescription(err)));
}
/**Occurs when a user leaves the channel.
* @param stats With this callback, the application retrieves the channel information,
* such as the call duration and statistics.*/
@Override
public void onLeaveChannel(RtcStats stats) {
super.onLeaveChannel(stats);
Log.i(TAG, String.format("local user %d leaveChannel!", myUid));
showLongToast(String.format("local user %d leaveChannel!", myUid));
}
/**Occurs when the local user joins a specified channel.
* The channel name assignment is based on channelName specified in the joinChannel method.
* If the uid is not specified when joinChannel is called, the server automatically assigns a uid.
* @param channel Channel name
* @param uid User ID
* @param elapsed Time elapsed (ms) from the user calling joinChannel until this callback is triggered*/
@Override
public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
Log.i(TAG, String.format("onJoinChannelSuccess channel %s uid %d", channel, uid));
showLongToast(String.format("onJoinChannelSuccess channel %s uid %d", channel, uid));
myUid = uid;
joined = true;
handler.post(new Runnable() {
@Override
public void run() {
join.setEnabled(true);
join.setText(getString(R.string.leave));
publish.setEnabled(true);
latency.setEnabled(true);
sbCameraRotate.setEnabled(true);
}
});
}
/**Since v2.9.0.
* This callback indicates the state change of the remote audio stream.
* PS: This callback does not work properly when the number of users (in the Communication profile) or
* broadcasters (in the Live-broadcast profile) in the channel exceeds 17.
* @param uid ID of the user whose audio state changes.
* @param state State of the remote audio
* REMOTE_AUDIO_STATE_STOPPED(0): The remote audio is in the default state, probably due
* to REMOTE_AUDIO_REASON_LOCAL_MUTED(3), REMOTE_AUDIO_REASON_REMOTE_MUTED(5),
* or REMOTE_AUDIO_REASON_REMOTE_OFFLINE(7).
* REMOTE_AUDIO_STATE_STARTING(1): The first remote audio packet is received.
* REMOTE_AUDIO_STATE_DECODING(2): The remote audio stream is decoded and plays normally,
* probably due to REMOTE_AUDIO_REASON_NETWORK_RECOVERY(2),
* REMOTE_AUDIO_REASON_LOCAL_UNMUTED(4) or REMOTE_AUDIO_REASON_REMOTE_UNMUTED(6).
* REMOTE_AUDIO_STATE_FROZEN(3): The remote audio is frozen, probably due to
* REMOTE_AUDIO_REASON_NETWORK_CONGESTION(1).
* REMOTE_AUDIO_STATE_FAILED(4): The remote audio fails to start, probably due to
* REMOTE_AUDIO_REASON_INTERNAL(0).
* @param reason The reason of the remote audio state change.
* REMOTE_AUDIO_REASON_INTERNAL(0): Internal reasons.
* REMOTE_AUDIO_REASON_NETWORK_CONGESTION(1): Network congestion.
* REMOTE_AUDIO_REASON_NETWORK_RECOVERY(2): Network recovery.
* REMOTE_AUDIO_REASON_LOCAL_MUTED(3): The local user stops receiving the remote audio
* stream or disables the audio module.
* REMOTE_AUDIO_REASON_LOCAL_UNMUTED(4): The local user resumes receiving the remote audio
* stream or enables the audio module.
* REMOTE_AUDIO_REASON_REMOTE_MUTED(5): The remote user stops sending the audio stream or
* disables the audio module.
* REMOTE_AUDIO_REASON_REMOTE_UNMUTED(6): The remote user resumes sending the audio stream
* or enables the audio module.
* REMOTE_AUDIO_REASON_REMOTE_OFFLINE(7): The remote user leaves the channel.
* @param elapsed Time elapsed (ms) from the local user calling the joinChannel method
* until the SDK triggers this callback.*/
@Override
public void onRemoteAudioStateChanged(int uid, int state, int reason, int elapsed) {
super.onRemoteAudioStateChanged(uid, state, reason, elapsed);
Log.i(TAG, "onRemoteAudioStateChanged->" + uid + ", state->" + state + ", reason->" + reason);
}
/**Since v2.9.0.
* Occurs when the remote video state changes.
* PS: This callback does not work properly when the number of users (in the Communication
* profile) or broadcasters (in the Live-broadcast profile) in the channel exceeds 17.
* @param uid ID of the remote user whose video state changes.
* @param state State of the remote video:
* REMOTE_VIDEO_STATE_STOPPED(0): The remote video is in the default state, probably due
* to REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED(3), REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED(5),
* or REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE(7).
* REMOTE_VIDEO_STATE_STARTING(1): The first remote video packet is received.
* REMOTE_VIDEO_STATE_DECODING(2): The remote video stream is decoded and plays normally,
* probably due to REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY (2),
* REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED(4), REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED(6),
* or REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK_RECOVERY(9).
* REMOTE_VIDEO_STATE_FROZEN(3): The remote video is frozen, probably due to
* REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION(1) or REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK(8).
* REMOTE_VIDEO_STATE_FAILED(4): The remote video fails to start, probably due to
* REMOTE_VIDEO_STATE_REASON_INTERNAL(0).
* @param reason The reason of the remote video state change:
* REMOTE_VIDEO_STATE_REASON_INTERNAL(0): Internal reasons.
* REMOTE_VIDEO_STATE_REASON_NETWORK_CONGESTION(1): Network congestion.
* REMOTE_VIDEO_STATE_REASON_NETWORK_RECOVERY(2): Network recovery.
* REMOTE_VIDEO_STATE_REASON_LOCAL_MUTED(3): The local user stops receiving the remote
* video stream or disables the video module.
* REMOTE_VIDEO_STATE_REASON_LOCAL_UNMUTED(4): The local user resumes receiving the remote
* video stream or enables the video module.
* REMOTE_VIDEO_STATE_REASON_REMOTE_MUTED(5): The remote user stops sending the video
* stream or disables the video module.
* REMOTE_VIDEO_STATE_REASON_REMOTE_UNMUTED(6): The remote user resumes sending the video
* stream or enables the video module.
* REMOTE_VIDEO_STATE_REASON_REMOTE_OFFLINE(7): The remote user leaves the channel.
* REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK(8): The remote media stream falls back to the
* audio-only stream due to poor network conditions.
* REMOTE_VIDEO_STATE_REASON_AUDIO_FALLBACK_RECOVERY(9): The remote media stream switches
* back to the video stream after the network conditions improve.
* @param elapsed Time elapsed (ms) from the local user calling the joinChannel method until
* the SDK triggers this callback.*/
@Override
public void onRemoteVideoStateChanged(int uid, int state, int reason, int elapsed) {
super.onRemoteVideoStateChanged(uid, state, reason, elapsed);
Log.i(TAG, "onRemoteVideoStateChanged->" + uid + ", state->" + state + ", reason->" + reason);
}
/**Occurs when a remote user (Communication)/host (Live Broadcast) joins the channel.
* @param uid ID of the user whose audio state changes.
* @param elapsed Time delay (ms) from the local user calling joinChannel/setClientRole
* until this callback is triggered.*/
@Override
public void onUserJoined(int uid, int elapsed) {
super.onUserJoined(uid, elapsed);
Log.i(TAG, "onUserJoined->" + uid);
showLongToast(String.format("user %d joined!", uid));
/**Check if the context is correct*/
Context context = getContext();
if (context == null) {
return;
}
if(remoteUid != 0) {
return;
}
else{
remoteUid = uid;
}
handler.post(() ->
{
/**Display remote video stream*/
SurfaceView surfaceView = null;
if (backGroundVideo.getChildCount() > 0) {
backGroundVideo.removeAllViews();
}
// Create render view by RtcEngine
surfaceView = RtcEngine.CreateRendererView(context);
surfaceView.setZOrderMediaOverlay(true);
// Add to the remote container
backGroundVideo.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// Setup remote video to render
engine.setupRemoteVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, remoteUid));
super_resolution.setEnabled(true);
});
}
/**Occurs when a remote user (Communication)/host (Live Broadcast) leaves the channel.
* @param uid ID of the user whose audio state changes.
* @param reason Reason why the user goes offline:
* USER_OFFLINE_QUIT(0): The user left the current channel.
* USER_OFFLINE_DROPPED(1): The SDK timed out and the user dropped offline because no data
* packet was received within a certain period of time. If a user quits the
* call and the message is not passed to the SDK (due to an unreliable channel),
* the SDK assumes the user dropped offline.
* USER_OFFLINE_BECOME_AUDIENCE(2): (Live broadcast only.) The client role switched from
* the host to the audience.*/
@Override
public void onUserOffline(int uid, int reason) {
Log.i(TAG, String.format("user %d offline! reason:%d", uid, reason));
showLongToast(String.format("user %d offline! reason:%d", uid, reason));
remoteUid = 0;
handler.post(new Runnable() {
@Override
public void run() {
/**Clear render view
Note: The video will stay at its last frame, to completely remove it you will need to
remove the SurfaceView from its parent*/
engine.setupRemoteVideo(new VideoCanvas(null, RENDER_MODE_HIDDEN, uid));
super_resolution.setEnabled(false);
}
});
}
/**
* Occurs when the user role switches in a live streaming. For example, from a host to an audience or vice versa.
*
* The SDK triggers this callback when the local user switches the user role by calling the setClientRole method after joining the channel.
* @param oldRole Role that the user switches from.
* @param newRole Role that the user switches to.
*/
@Override
public void onClientRoleChanged(int oldRole, int newRole) {
Log.i(TAG, String.format("client role changed from state %d to %d", oldRole, newRole)); handler.post(new Runnable() {
@Override
public void run() {
publish.setEnabled(true);
}
});
}
/**
*
* @param uid remote user id
* @param enabled updated status of super resolution
* @param reason possible reasons are:
* SR_STATE_REASON_SUCCESS(0)
* SR_STATE_REASON_STREAM_OVER_LIMITATION(1)
* SR_STATE_REASON_USER_COUNT_OVER_LIMITATION(2)
* SR_STATE_REASON_DEVICE_NOT_SUPPORTED(3)
*/
@Override
public void onUserSuperResolutionEnabled(int uid, boolean enabled, int reason) {
super.onUserSuperResolutionEnabled(uid, enabled, reason);
if(uid == 0 && !enabled && reason == 3){
showLongToast(String.format("Unfortunately, Super Resolution can't enabled because your device doesn't support this feature."));
return;
}
if(remoteUid == uid){
if(reason!=0){
showLongToast(String.format("Super Resolution can't enabled because of reason code: %d", reason));
}
enableSuperResolution = enabled;
handler.post(new Runnable() {
@Override
public void run() {
super_resolution.setText(enableSuperResolution?getText(R.string.closesuperr):getText(R.string.opensuperr));
}
});
}
}
};
}