Skip to content

Commit 3e64ccf

Browse files
author
xianing
committed
fix screen share and mediaIO video source sample
1 parent 52063c5 commit 3e64ccf

4 files changed

Lines changed: 52 additions & 82 deletions

File tree

Android/APIExample/app/src/main/java/io/agora/api/example/examples/advanced/MultiProcess.java

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class MultiProcess extends BaseFragment implements View.OnClickListener
5252
private static final String TAG = MultiProcess.class.getSimpleName();
5353
private static final Integer SCREEN_SHARE_UID = 10000;
5454

55-
private FrameLayout fl_local, fl_remote;
55+
private FrameLayout fl_local;
5656
private Button join, screenShare;
5757
private EditText et_channel;
5858
private RtcEngine engine;
@@ -93,7 +93,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
9393
view.findViewById(R.id.btn_join).setOnClickListener(this);
9494
view.findViewById(R.id.screenShare).setOnClickListener(this);
9595
fl_local = view.findViewById(R.id.fl_local);
96-
fl_remote = view.findViewById(R.id.fl_remote);
9796
}
9897

9998
@Override
@@ -192,7 +191,9 @@ public void onClick(View v)
192191
* triggers the removeInjectStreamUrl method.*/
193192
engine.leaveChannel();
194193
join.setText(getString(R.string.join));
195-
mSSClient.stop(getContext());
194+
if(isSharing) {
195+
mSSClient.stop(getContext());
196+
}
196197
screenShare.setText(getResources().getString(R.string.screenshare));
197198
screenShare.setEnabled(false);
198199
isSharing = false;
@@ -245,8 +246,6 @@ private void joinChannel(String channelId)
245246
// Setup local video to render your local camera preview
246247
engine.setupLocalVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, 0));
247248
// Set audio route to microPhone
248-
engine.disableAudio();
249-
// engine.setDefaultAudioRoutetoSpeakerphone(false);
250249

251250
/** Sets the channel profile of the Agora RtcEngine.
252251
CHANNEL_PROFILE_COMMUNICATION(0): (Default) The Communication profile.
@@ -281,8 +280,8 @@ private void joinChannel(String channelId)
281280
if you do not specify the uid, we will generate the uid for you*/
282281

283282
ChannelMediaOptions option = new ChannelMediaOptions();
284-
option.autoSubscribeAudio = false;
285-
option.autoSubscribeVideo = false;
283+
option.autoSubscribeAudio = true;
284+
option.autoSubscribeVideo = true;
286285
int res = engine.joinChannel(accessToken, channelId, "Extra Optional Data", 0, option);
287286
if (res != 0)
288287
{
@@ -449,32 +448,6 @@ public void onUserJoined(int uid, int elapsed)
449448
super.onUserJoined(uid, elapsed);
450449
Log.i(TAG, "onUserJoined->" + uid);
451450
showLongToast(String.format("user %d joined!", uid));
452-
// don't render screen sharing view
453-
if (SCREEN_SHARE_UID == uid){
454-
return;
455-
}
456-
/**Check if the context is correct*/
457-
Context context = getContext();
458-
if (context == null) {
459-
return;
460-
}
461-
handler.post(() ->
462-
{
463-
/**Display remote video stream*/
464-
SurfaceView surfaceView = null;
465-
if (fl_remote.getChildCount() > 0)
466-
{
467-
fl_remote.removeAllViews();
468-
}
469-
// Create render view by RtcEngine
470-
surfaceView = RtcEngine.CreateRendererView(context);
471-
surfaceView.setZOrderMediaOverlay(true);
472-
// Add to the remote container
473-
fl_remote.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
474-
475-
// Setup remote video to render
476-
engine.setupRemoteVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, uid));
477-
});
478451
}
479452

480453
/**Occurs when a remote user (Communication)/host (Live Broadcast) leaves the channel.

Android/APIExample/app/src/main/java/io/agora/api/example/examples/advanced/SwitchExternalVideo.java

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import io.agora.rtc.Constants;
4343
import io.agora.rtc.IRtcEngineEventHandler;
4444
import io.agora.rtc.RtcEngine;
45-
import io.agora.rtc.models.ChannelMediaOptions;
4645
import io.agora.rtc.video.VideoCanvas;
4746
import io.agora.rtc.video.VideoEncoderConfiguration;
4847

@@ -69,6 +68,7 @@
6968
public class SwitchExternalVideo extends BaseFragment implements View.OnClickListener {
7069
private static final String TAG = SwitchExternalVideo.class.getSimpleName();
7170

71+
private FrameLayout fl_remote;
7272
private RelativeLayout fl_local;
7373
private Button join, localVideo;
7474
private EditText et_channel;
@@ -101,6 +101,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
101101
join = view.findViewById(R.id.btn_join);
102102
localVideo = view.findViewById(R.id.localVideo);
103103
et_channel = view.findViewById(R.id.et_channel);
104+
fl_remote = view.findViewById(R.id.fl_remote);
104105
fl_local = view.findViewById(R.id.fl_local);
105106
join.setOnClickListener(this);
106107
localVideo.setOnClickListener(this);
@@ -192,6 +193,7 @@ public void onClick(View v) {
192193
joined = false;
193194
join.setText(getString(R.string.join));
194195
localVideo.setEnabled(false);
196+
fl_remote.removeAllViews();
195197
fl_local.removeAllViews();
196198
/**After joining a channel, the user must call the leaveChannel method to end the
197199
* call before joining another channel. This method returns 0 if the user leaves the
@@ -307,11 +309,7 @@ private void joinChannel(String channelId) {
307309
}
308310
/** Allows a user to join a channel.
309311
if you do not specify the uid, we will generate the uid for you*/
310-
311-
ChannelMediaOptions option = new ChannelMediaOptions();
312-
option.autoSubscribeAudio = true;
313-
option.autoSubscribeVideo = true;
314-
int res = ENGINE.joinChannel(accessToken, channelId, "Extra Optional Data", 0, option);
312+
int res = ENGINE.joinChannel(accessToken, channelId, "Extra Optional Data", 0);
315313
if (res != 0) {
316314
// Usually happens with invalid parameters
317315
// Error code description can be found at:
@@ -419,26 +417,26 @@ public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
419417
public void onRemoteVideoStateChanged(int uid, int state, int reason, int elapsed) {
420418
super.onRemoteVideoStateChanged(uid, state, reason, elapsed);
421419
Log.i(TAG, "onRemoteVideoStateChanged:uid->" + uid + ", state->" + state);
422-
// if (state == REMOTE_VIDEO_STATE_STARTING) {
423-
// /**Check if the context is correct*/
424-
// Context context = getContext();
425-
// if (context == null) {
426-
// return;
427-
// }
428-
// handler.post(() ->
429-
// {
430-
// /**Display remote video stream*/
431-
// SurfaceView surfaceView = RtcEngine.CreateRendererView(context);
432-
// surfaceView.setZOrderMediaOverlay(true);
433-
// if (fl_remote.getChildCount() > 0) {
434-
// fl_remote.removeAllViews();
435-
// }
436-
// fl_remote.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
437-
// ViewGroup.LayoutParams.MATCH_PARENT));
438-
// /**Setup remote video to render*/
439-
// ENGINE.setupRemoteVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, uid));
440-
// });
441-
// }
420+
if (state == REMOTE_VIDEO_STATE_STARTING) {
421+
/**Check if the context is correct*/
422+
Context context = getContext();
423+
if (context == null) {
424+
return;
425+
}
426+
handler.post(() ->
427+
{
428+
/**Display remote video stream*/
429+
SurfaceView surfaceView = RtcEngine.CreateRendererView(context);
430+
surfaceView.setZOrderMediaOverlay(true);
431+
if (fl_remote.getChildCount() > 0) {
432+
fl_remote.removeAllViews();
433+
}
434+
fl_remote.addView(surfaceView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
435+
ViewGroup.LayoutParams.MATCH_PARENT));
436+
/**Setup remote video to render*/
437+
ENGINE.setupRemoteVideo(new VideoCanvas(surfaceView, RENDER_MODE_HIDDEN, uid));
438+
});
439+
}
442440
}
443441

444442
/**Occurs when a remote user (Communication)/host (Live Broadcast) joins the channel.
@@ -465,17 +463,17 @@ public void onUserJoined(int uid, int elapsed) {
465463
@Override
466464
public void onUserOffline(int uid, int reason) {
467465
Log.i(TAG, String.format("user %d offline! reason:%d", uid, reason));
468-
// showLongToast(String.format("user %d offline! reason:%d", uid, reason));
469-
// handler.post(new Runnable() {
470-
// @Override
471-
// public void run() {
472-
// /**Clear render view
473-
// Note: The video will stay at its last frame, to completely remove it you will need to
474-
// remove the SurfaceView from its parent*/
475-
// ENGINE.setupRemoteVideo(new VideoCanvas(null, RENDER_MODE_HIDDEN, uid));
476-
// fl_remote.removeAllViews();
477-
// }
478-
// });
466+
showLongToast(String.format("user %d offline! reason:%d", uid, reason));
467+
handler.post(new Runnable() {
468+
@Override
469+
public void run() {
470+
/**Clear render view
471+
Note: The video will stay at its last frame, to completely remove it you will need to
472+
remove the SurfaceView from its parent*/
473+
ENGINE.setupRemoteVideo(new VideoCanvas(null, RENDER_MODE_HIDDEN, uid));
474+
fl_remote.removeAllViews();
475+
}
476+
});
479477
}
480478
};
481479

Android/APIExample/app/src/main/res/layout/fragment_switch_external_video.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
android:layout_height="match_parent"
1313
android:layout_above="@+id/ll_join" />
1414

15+
<FrameLayout
16+
android:id="@+id/fl_remote"
17+
android:layout_alignParentEnd="true"
18+
android:layout_width="180dp"
19+
android:layout_height="240dp"/>
20+
1521
<LinearLayout
1622
android:id="@+id/ll_join"
1723
android:layout_width="match_parent"

Android/APIExample/lib-switch-external-video/src/main/java/io/agora/advancedvideo/externvideosource/ExternalVideoInputManager.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.opengl.EGLSurface;
77
import android.opengl.GLES11Ext;
88
import android.opengl.GLES20;
9+
import android.opengl.GLUtils;
910
import android.os.Build;
1011
import android.util.Log;
1112
import android.util.Size;
@@ -18,15 +19,12 @@
1819
import io.agora.api.component.gles.ProgramTextureOES;
1920
import io.agora.api.component.gles.core.EglCore;
2021
import io.agora.api.component.gles.core.GlUtil;
21-
import io.agora.rtc.gl.TextureTransformer;
2222
import io.agora.rtc.mediaio.IVideoFrameConsumer;
2323
import io.agora.rtc.mediaio.IVideoSource;
2424
import io.agora.rtc.mediaio.MediaIO;
2525

2626
import static io.agora.api.component.Constant.ENGINE;
2727
import static io.agora.api.component.Constant.TEXTUREVIEW;
28-
import static io.agora.rtc.mediaio.MediaIO.BufferType.TEXTURE;
29-
import static io.agora.rtc.mediaio.MediaIO.PixelFormat.TEXTURE_OES;
3028

3129
/**
3230
* {@link IVideoSource}
@@ -67,8 +65,6 @@ public class ExternalVideoInputManager implements IVideoSource {
6765
private volatile IVideoFrameConsumer mConsumer;
6866

6967
private Context context;
70-
private TextureTransformer textureTransformer;
71-
private static final int MAX_TEXTURE_COPY = 1;
7268

7369
public ExternalVideoInputManager(Context context) {
7470
this.context = context;
@@ -87,7 +83,6 @@ boolean setExternalVideoInput(int type, Intent intent) {
8783
return false;
8884
}
8985

90-
type = TYPE_SCREEN_SHARE;
9186
IExternalVideoInput input;
9287
switch (type) {
9388
case TYPE_LOCAL_VIDEO:
@@ -183,19 +178,20 @@ public void onDispose() {
183178

184179
@Override
185180
public int getBufferType() {
186-
return TEXTURE.intValue();
181+
return MediaIO.BufferType.TEXTURE.intValue();
187182
}
188183

189184
@Override
190185
public int getCaptureType() {
191-
return MediaIO.CaptureType.SCREEN.intValue();
186+
return MediaIO.CaptureType.CAMERA.intValue();
192187
}
193188

194189
@Override
195190
public int getContentHint() {
196191
return MediaIO.ContentHint.NONE.intValue();
197192
}
198193

194+
199195
private class ExternalVideoInputThread extends Thread
200196
{
201197
private final String TAG = ExternalVideoInputThread.class.getSimpleName();
@@ -224,7 +220,6 @@ private void prepare() {
224220
mThreadContext.eglCore = mEglCore;
225221
mThreadContext.context = mEglCore.getEGLContext();
226222
mThreadContext.program = new ProgramTextureOES();
227-
textureTransformer = new TextureTransformer(MAX_TEXTURE_COPY);
228223
/**Customizes the video source.
229224
* Call this method to add an external video source to the SDK.*/
230225
ENGINE.setVideoSource(ExternalVideoInputManager.this);
@@ -235,7 +230,6 @@ private void release() {
235230
return;
236231
}
237232
/**release external video source*/
238-
textureTransformer.release();
239233
ENGINE.setVideoSource(null);
240234
mSurface.release();
241235
mEglCore.makeNothingCurrent();
@@ -327,9 +321,8 @@ public void run() {
327321
* @param rotation Clockwise rotating angle (0, 90, 180, and 270 degrees) of the video frame
328322
* @param timestamp Timestamp of the video frame. For each video frame, you need to set a timestamp
329323
* @param matrix Matrix of the texture. The float value is between 0 and 1, such as 0.1, 0.2, and so on*/
330-
textureTransformer.copy(mTextureId, TEXTURE_OES.intValue(), mVideoWidth, mVideoHeight);
331324
mConsumer.consumeTextureFrame(mTextureId,
332-
TEXTURE_OES.intValue(),
325+
MediaIO.PixelFormat.TEXTURE_OES.intValue(),
333326
mVideoWidth, mVideoHeight, 0,
334327
System.currentTimeMillis(), mTransform);
335328
}

0 commit comments

Comments
 (0)