Skip to content

Commit

Permalink
set front camera as initial source
Browse files Browse the repository at this point in the history
  • Loading branch information
burak-58 committed Dec 9, 2023
1 parent 75f158a commit 66e085b
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ void publish(String streamId, String token, boolean videoCallEnabled, boolean au
*/
void switchCamera();

/**
* This is used to change the capture format for the camera
* @param width: desired width
* @param height: desired height
* @param framerate: desired framerate
*/
void changeCaptureFormat(int width, int height, int framerate);

/**
* Return if data channel is enabled and open
* @return true if data channel is available
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ public WebRTCClientBuilder setActivity(Activity activity) {
return this;
}

public WebRTCClientBuilder setCustomVideoCapturerEnabled(boolean b) {
webRTCClientConfig.customVideoCapturerEnabled = b;
return this;
}

public WebRTCClientBuilder setInitiateBeforeStream(boolean b) {
webRTCClientConfig.initiateBeforeStream = b;
return this;
Expand Down Expand Up @@ -144,4 +139,9 @@ public WebRTCClientBuilder setReconnectionEnabled(boolean b) {
public WebRTCClientConfig getConfig() {
return webRTCClientConfig;
}

public WebRTCClientBuilder setVideoSource(IWebRTCClient.StreamSource rearCamera) {
webRTCClientConfig.videoSource = rearCamera;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ public class WebRTCClientConfig {
*/
public Activity activity;

/*
* Flag indicating whether screencapture is enabled
*/
public boolean screencaptureEnabled = false;

/*
* Data channel observer for the data channel events
*/
Expand All @@ -141,12 +136,8 @@ public class WebRTCClientConfig {
/*
* Current video source to publish
*/
public IWebRTCClient.StreamSource videoSource;
public IWebRTCClient.StreamSource videoSource = IWebRTCClient.StreamSource.FRONT_CAMERA;

/*
* Flag indicating whether custom video capturer is enabled
*/
public boolean customVideoCapturerEnabled;

/*
* Flag indicating whether initate WebRTCClient (renderers, websocket, capturers etc.) before stream publish starts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ public enum Mode {
public AppRTCAudioManager audioManager = null;
private boolean isError;
private final long callStartedTimeMs = 0;
private boolean micEnabled = true;


private EglBase eglBase;
private String errorString = null;
Expand Down Expand Up @@ -584,7 +582,7 @@ public boolean checkPermissions(PermissionsHandler.PermissionCallback permission

public void initializeParameters() {
// If capturing format is not specified for screencapture, use screen resolution.
if (config.screencaptureEnabled) {
if (config.videoSource.equals(StreamSource.SCREEN)) {
DisplayMetrics displayMetrics = getDisplayMetrics();
config.videoWidth = displayMetrics.widthPixels;
config.videoHeight = displayMetrics.heightPixels;
Expand Down Expand Up @@ -648,21 +646,7 @@ public void initializeVideoCapturer() {
}

if (config.videoCallEnabled) {

StreamSource source = StreamSource.REAR_CAMERA;

if(config.screencaptureEnabled) {
source = StreamSource.SCREEN;
}
else if(config.customVideoCapturerEnabled) {
source = StreamSource.CUSTOM;
}
else if(useCamera2()) {
source = StreamSource.FRONT_CAMERA;
}

videoCapturer = createVideoCapturer(source);
config.videoSource = source;
videoCapturer = createVideoCapturer(config.videoSource);
}

executor.execute(() -> {
Expand Down Expand Up @@ -705,11 +689,6 @@ public void onStop() {
});
}


public boolean useCamera2() {
return Camera2Enumerator.isSupported(config.activity);
}

@Nullable
public VideoCapturer createCameraCapturer(CameraEnumerator enumerator) {
final String[] deviceNames = enumerator.getDeviceNames();
Expand Down Expand Up @@ -937,7 +916,7 @@ public void reportError(String streamId, final String description) {
}

public void changeVideoSource(StreamSource newSource) {
if(config.videoSource == null || !config.videoSource.equals(newSource)) {
if(!config.videoSource.equals(newSource)) {
if(newSource.equals(StreamSource.SCREEN) && adm != null) {
adm.setMediaProjection(config.mediaProjection);
}
Expand All @@ -954,6 +933,7 @@ public void changeVideoSource(StreamSource newSource) {

public @Nullable VideoCapturer createVideoCapturer(StreamSource source) {
final VideoCapturer videoCapturer;
config.videoSource = source;

if (StreamSource.SCREEN.equals(source)) {
videoCapturer = createScreenCapturer();
Expand Down Expand Up @@ -1359,7 +1339,7 @@ public void changeVideoCapturer(VideoCapturer newVideoCapturer) {
videoCapturer = newVideoCapturer;
localVideoTrack = null;

MediaStreamTrack newTrack = (MediaStreamTrack) createVideoTrack(videoCapturer);
MediaStreamTrack newTrack = createVideoTrack(videoCapturer);
if(localVideoSender != null) {
localVideoSender.setTrack(newTrack, true);
}
Expand Down Expand Up @@ -1904,7 +1884,7 @@ private VideoTrack createVideoTrack(VideoCapturer capturer) {
capturer.startCapture(config.videoWidth, config.videoHeight, config.videoFps);

localVideoTrack = factory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
//localVideoTrack.setEnabled(renderVideo);
localVideoTrack.setEnabled(renderVideo);
localVideoTrack.addSink(localVideoSink);
videoCapturerStopped = false;
}
Expand Down Expand Up @@ -2146,10 +2126,9 @@ private void switchCameraInternal() {
}
}

private void changeCaptureFormatInternal(int width, int height, int framerate) {
public void changeCaptureFormat(int width, int height, int framerate) {
if (!config.videoCallEnabled || videoSource == null || isError) {
Log.e(TAG,
"Failed to change capture format. Video: " + config.videoCallEnabled
Log.e(TAG, "Failed to change capture format. Video: " + config.videoCallEnabled
+ ". Error : " + isError);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,21 +417,19 @@ public void testInitilizeVideoCapturer() {
Mockito.doNothing().when(webRTCClient).initializeRenderers();
Mockito.doReturn(null).when(webRTCClient).createVideoCapturer(any());

Mockito.doReturn(true).when(webRTCClient).useCamera2();
webRTCClient.getConfig().videoSource = IWebRTCClient.StreamSource.FRONT_CAMERA;
webRTCClient.initializeVideoCapturer();
assertEquals(IWebRTCClient.StreamSource.FRONT_CAMERA, webRTCClient.getConfig().videoSource);

Mockito.doReturn(false).when(webRTCClient).useCamera2();
webRTCClient.getConfig().videoSource = IWebRTCClient.StreamSource.REAR_CAMERA;
webRTCClient.initializeVideoCapturer();
assertEquals(IWebRTCClient.StreamSource.REAR_CAMERA, webRTCClient.getConfig().videoSource);

webRTCClient.getConfig().screencaptureEnabled = true;
webRTCClient.getConfig().customVideoCapturerEnabled = false;
webRTCClient.getConfig().videoSource = IWebRTCClient.StreamSource.SCREEN;
webRTCClient.initializeVideoCapturer();
assertEquals(IWebRTCClient.StreamSource.SCREEN, webRTCClient.getConfig().videoSource);

webRTCClient.getConfig().screencaptureEnabled = false;
webRTCClient.getConfig().customVideoCapturerEnabled = true;
webRTCClient.getConfig().videoSource = IWebRTCClient.StreamSource.CUSTOM;
webRTCClient.initializeVideoCapturer();
assertEquals(IWebRTCClient.StreamSource.CUSTOM, webRTCClient.getConfig().videoSource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void createList() {
addActivity(DataChannelOnlyActivity.class, "DC Only");
addActivity(MP3PublishActivity.class, "mp3");
addActivity(MP4PublishActivity.class, "mp4");
addActivity(MP4PublishWithSurfaceActivity.class, "mp4 s");
addActivity(MP4PublishWithSurfaceActivity.class, "mp4 with Surface");
addActivity(USBCameraActivity.class, "USB Camera");
addActivity(MultiTrackPlayActivity.class, "Multi Track");
addActivity(SettingsActivity.class, "Settings");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
.setLocalVideoRenderer(fullScreenRenderer)
.setServerUrl(serverUrl)
.setActivity(this)
.setCustomVideoCapturerEnabled(true)
.setVideoSource(IWebRTCClient.StreamSource.CUSTOM)
.setWebRTCListener(createWebRTCListener())
.setDataChannelObserver(createDatachannelObserver())
.setInitiateBeforeStream(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {
.setLocalVideoRenderer(fullScreenRenderer)
.setServerUrl(serverUrl)
.setActivity(this)
.setCustomVideoCapturerEnabled(true)
.setVideoSource(IWebRTCClient.StreamSource.CUSTOM)
.setWebRTCListener(createWebRTCListener())
.setDataChannelObserver(createDatachannelObserver())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,16 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
IWebRTCClient.StreamSource newSource;
if(checkedId == R.id.rbFront) {
newSource = IWebRTCClient.StreamSource.FRONT_CAMERA;
webRTCClient.getConfig().customVideoCapturerEnabled = false;
cameraHandler.obtainMessage(CAMERA_STOP).sendToTarget();
webRTCClient.changeVideoSource(newSource);
}
else if(checkedId == R.id.rbRear) {
newSource = IWebRTCClient.StreamSource.REAR_CAMERA;
webRTCClient.getConfig().customVideoCapturerEnabled = false;
cameraHandler.obtainMessage(CAMERA_STOP).sendToTarget();
webRTCClient.changeVideoSource(newSource);
}
else if(checkedId == R.id.rbUsb) {
newSource = IWebRTCClient.StreamSource.CUSTOM;
webRTCClient.getConfig().customVideoCapturerEnabled = true;
webRTCClient.changeVideoSource(newSource);
startUSBCamera();
}
Expand Down

0 comments on commit 66e085b

Please sign in to comment.