Fix Android AppRTCDemo failure on devices with one or no camera.

- Disable video call on devices with no camera.
- Open default camera and disable camera switch on
devices with one camera.

BUG=4373
R=braveyao@webrtc.org, wzh@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/46539004

Cr-Commit-Position: refs/heads/master@{#8674}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8674 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
glaznev@webrtc.org 2015-03-10 18:20:56 +00:00
parent 4052d88162
commit fc516077ed
2 changed files with 24 additions and 6 deletions

View File

@ -98,6 +98,11 @@ public class VideoCapturerAndroid extends VideoCapturer implements PreviewCallba
return names; return names;
} }
// Returns number of cameras on device.
public static int getDeviceCount() {
return Camera.getNumberOfCameras();
}
public static String getDeviceName(int index) { public static String getDeviceName(int index) {
Camera.CameraInfo info = new Camera.CameraInfo(); Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(index, info); Camera.getCameraInfo(index, info);
@ -114,7 +119,7 @@ public class VideoCapturerAndroid extends VideoCapturer implements PreviewCallba
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
return getDeviceName(i); return getDeviceName(i);
} }
throw new RuntimeException("Front facing camera does not exist."); return null;
} }
public static String getNameOfBackFacingDevice() { public static String getNameOfBackFacingDevice() {
@ -124,7 +129,7 @@ public class VideoCapturerAndroid extends VideoCapturer implements PreviewCallba
if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK)
return getDeviceName(i); return getDeviceName(i);
} }
throw new RuntimeException("Back facing camera does not exist."); return null;
} }
public static VideoCapturerAndroid create(String name) { public static VideoCapturerAndroid create(String name) {

View File

@ -114,6 +114,7 @@ public class PeerConnectionClient {
private boolean isInitiator; private boolean isInitiator;
private SessionDescription localSdp = null; // either offer or answer SDP private SessionDescription localSdp = null; // either offer or answer SDP
private MediaStream mediaStream = null; private MediaStream mediaStream = null;
private int numberOfCameras;
private VideoCapturerAndroid videoCapturer = null; private VideoCapturerAndroid videoCapturer = null;
// enableVideo is set to true if video should be rendered and sent. // enableVideo is set to true if video should be rendered and sent.
private boolean renderVideo = true; private boolean renderVideo = true;
@ -235,6 +236,12 @@ public class PeerConnectionClient {
if (signalingParameters.videoConstraints == null) { if (signalingParameters.videoConstraints == null) {
videoCallEnabled = false; videoCallEnabled = false;
} }
// Check if there is a camera on device and disable video call if not.
numberOfCameras = VideoCapturerAndroid.getDeviceCount();
if (numberOfCameras == 0) {
Log.w(TAG, "No camera on device. Switch to audio only call.");
videoCallEnabled = false;
}
if (videoCallEnabled) { if (videoCallEnabled) {
int videoWidth = peerConnectionParameters.videoWidth; int videoWidth = peerConnectionParameters.videoWidth;
int videoHeight = peerConnectionParameters.videoHeight; int videoHeight = peerConnectionParameters.videoHeight;
@ -365,8 +372,14 @@ public class PeerConnectionClient {
mediaStream = factory.createLocalMediaStream("ARDAMS"); mediaStream = factory.createLocalMediaStream("ARDAMS");
if (videoCallEnabled) { if (videoCallEnabled) {
videoCapturer = VideoCapturerAndroid.create( String cameraDeviceName = VideoCapturerAndroid.getDeviceName(0);
VideoCapturerAndroid.getNameOfFrontFacingDevice()); String frontCameraDeviceName =
VideoCapturerAndroid.getNameOfFrontFacingDevice();
if (numberOfCameras > 1 && frontCameraDeviceName != null) {
cameraDeviceName = frontCameraDeviceName;
}
Log.d(TAG, "Opening camera: " + cameraDeviceName);
videoCapturer = VideoCapturerAndroid.create(cameraDeviceName);
mediaStream.addTrack(createVideoTrack(videoCapturer)); mediaStream.addTrack(createVideoTrack(videoCapturer));
} }
@ -735,8 +748,8 @@ public class PeerConnectionClient {
} }
private void switchCameraInternal() { private void switchCameraInternal() {
if (!videoCallEnabled) { if (!videoCallEnabled || numberOfCameras < 2) {
return; // No video is sent. return; // No video is sent or only one camera is available.
} }
Log.d(TAG, "Switch camera"); Log.d(TAG, "Switch camera");
videoCapturer.switchCamera(); videoCapturer.switchCamera();