Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

/**
* This demo demonstrates how to make a one-to-one video call
*
* By default, Everyone is a host, entered a channel will see yourself in the background( the big one ).
* click the frame will switch the position.
* When turn the Co-host on, others will see you.
*/
@Example(
index = 23,
Expand All @@ -49,6 +53,8 @@
public class LiveStreaming extends BaseFragment implements View.OnClickListener {
private static final String TAG = LiveStreaming.class.getSimpleName();

// foreground is the small one
// background is the large one
private FrameLayout foreGroundVideo, backGroundVideo;
private Button join, publish;
private Switch cloudGameMode;
Expand All @@ -60,11 +66,13 @@ public class LiveStreaming extends BaseFragment implements View.OnClickListener
private boolean isHost = false;
private boolean isLocalVideoForeground = false;

private SurfaceView localView;
private SurfaceView remoteView;

@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;
return inflater.inflate(R.layout.fragment_live_streaming, container, false);
}

@Override
Expand All @@ -73,13 +81,14 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
join = view.findViewById(R.id.btn_join);
publish = view.findViewById(R.id.btn_publish);
et_channel = view.findViewById(R.id.et_channel);
foreGroundVideo = view.findViewById(R.id.foreground_video);
backGroundVideo = view.findViewById(R.id.background_video);
cloudGameMode = view.findViewById(R.id.cloudGameMode);
publish.setEnabled(false);
view.findViewById(R.id.btn_join).setOnClickListener(this);
view.findViewById(R.id.btn_publish).setOnClickListener(this);
view.findViewById(R.id.foreground_video).setOnClickListener(this);
foreGroundVideo = view.findViewById(R.id.background_video);
backGroundVideo = view.findViewById(R.id.foreground_video);
publish.setOnClickListener(this);
join.setOnClickListener(this);
foreGroundVideo.setOnClickListener(this);
backGroundVideo.setOnClickListener(this);
}

@Override
Expand All @@ -90,7 +99,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
@Override
public void onDestroy() {
super.onDestroy();
/**leaveChannel and Destroy the RtcEngine instance*/
/*leaveChannel and Destroy the RtcEngine instance*/
if (engine != null) {
engine.leaveChannel();
}
Expand Down Expand Up @@ -127,15 +136,14 @@ public void onClick(View v) {
engine = RtcEngine.create(rtcEngineConfig);
if(cloudGameMode.isChecked()){
engine.disableAudio();
}
else{
} else{
engine.enableAudio();
}
} catch (Exception e) {
requireActivity().onBackPressed();
e.printStackTrace();
getActivity().onBackPressed();
}
CommonUtil.hideInputBoard(getActivity(), et_channel);
CommonUtil.hideInputBoard(requireActivity(), et_channel);
// call when join button hit
String channelId = et_channel.getText().toString();
// Check permission
Expand Down Expand Up @@ -188,45 +196,54 @@ public void onClick(View v) {
publish.setEnabled(false);
publish.setText(isHost ? getString(R.string.disnable_publish) : getString(R.string.enable_publish));

} else if (v.getId() == R.id.foreground_video) {
} else if (v.getId() == R.id.foreground_video || v.getId() == R.id.background_video) {
isLocalVideoForeground = !isLocalVideoForeground;
if (foreGroundVideo.getChildCount() > 0) {
foreGroundVideo.removeAllViews();
}
if (backGroundVideo.getChildCount() > 0) {
backGroundVideo.removeAllViews();
}
// Create render view by RtcEngine
SurfaceView localView = new SurfaceView(getContext());
SurfaceView remoteView = new SurfaceView(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));
engine.startPreview();
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));
engine.startPreview();
// Setup remote video to render
engine.setupRemoteVideo(new VideoCanvas(remoteView, RENDER_MODE_HIDDEN, remoteUid));
localView.setZOrderMediaOverlay(true);
localView.setZOrderOnTop(true);
}
switchPreview();
}

}

/**
* Total 3 steps
* Step 1: remove all view
* Step 2: config and add new view
* Step 3: setup engine
*/
private void switchPreview() {
// Step 1
if (foreGroundVideo.getChildCount() > 0) {
foreGroundVideo.removeAllViews();
}
if (backGroundVideo.getChildCount() > 0) {
backGroundVideo.removeAllViews();
}

// Step 2
// Create render view by RtcEngine
SurfaceView localView = new SurfaceView(getContext());
SurfaceView remoteView = new SurfaceView(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));
localView.setZOrderMediaOverlay(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));
remoteView.setZOrderMediaOverlay(true);
}

// Step 3

// 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));
engine.startPreview();
}

private void joinChannel(String channelId) {
Expand All @@ -238,11 +255,17 @@ private void joinChannel(String channelId) {

// Create render view by RtcEngine
SurfaceView surfaceView = new SurfaceView(context);
if (foreGroundVideo.getChildCount() > 0) {

if (backGroundVideo.getChildCount() > 0)
backGroundVideo.removeAllViews();
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));

// Add to the container
if(isLocalVideoForeground)
foreGroundVideo.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
else
backGroundVideo.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));
engine.startPreview();
Expand Down Expand Up @@ -401,15 +424,17 @@ public void onUserJoined(int uid, int elapsed) {
handler.post(() ->
{
/**Display remote video stream*/
SurfaceView surfaceView = null;
if (backGroundVideo.getChildCount() > 0) {
backGroundVideo.removeAllViews();
}
SurfaceView surfaceView;

// Create render view by RtcEngine
surfaceView = new SurfaceView(context);
surfaceView.setZOrderMediaOverlay(true);
// Add to the remote container
backGroundVideo.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
if(isLocalVideoForeground) {
backGroundVideo.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
} else {
foreGroundVideo.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
surfaceView.setZOrderMediaOverlay(true);
}

// Setup remote video to render
engine.setupRemoteVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, remoteUid));
Expand All @@ -430,12 +455,14 @@ public void onUserJoined(int uid, int elapsed) {
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));
if(uid == remoteUid)
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*/
remoteUid = 0;
engine.setupRemoteVideo(new VideoCanvas(null, RENDER_MODE_HIDDEN, uid));
}
});
Expand Down