diff --git a/talk/app/webrtc/java/src/org/webrtc/VideoCapturerAndroid.java b/talk/app/webrtc/java/src/org/webrtc/VideoCapturerAndroid.java index ef6666a53..ede254911 100644 --- a/talk/app/webrtc/java/src/org/webrtc/VideoCapturerAndroid.java +++ b/talk/app/webrtc/java/src/org/webrtc/VideoCapturerAndroid.java @@ -98,6 +98,11 @@ public class VideoCapturerAndroid extends VideoCapturer implements PreviewCallba return names; } + // Returns number of cameras on device. + public static int getDeviceCount() { + return Camera.getNumberOfCameras(); + } + public static String getDeviceName(int index) { Camera.CameraInfo info = new Camera.CameraInfo(); Camera.getCameraInfo(index, info); @@ -114,7 +119,7 @@ public class VideoCapturerAndroid extends VideoCapturer implements PreviewCallba if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) return getDeviceName(i); } - throw new RuntimeException("Front facing camera does not exist."); + return null; } public static String getNameOfBackFacingDevice() { @@ -124,7 +129,7 @@ public class VideoCapturerAndroid extends VideoCapturer implements PreviewCallba if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) return getDeviceName(i); } - throw new RuntimeException("Back facing camera does not exist."); + return null; } public static VideoCapturerAndroid create(String name) { diff --git a/talk/examples/android/src/org/appspot/apprtc/PeerConnectionClient.java b/talk/examples/android/src/org/appspot/apprtc/PeerConnectionClient.java index 791002384..46354c9af 100644 --- a/talk/examples/android/src/org/appspot/apprtc/PeerConnectionClient.java +++ b/talk/examples/android/src/org/appspot/apprtc/PeerConnectionClient.java @@ -114,6 +114,7 @@ public class PeerConnectionClient { private boolean isInitiator; private SessionDescription localSdp = null; // either offer or answer SDP private MediaStream mediaStream = null; + private int numberOfCameras; private VideoCapturerAndroid videoCapturer = null; // enableVideo is set to true if video should be rendered and sent. private boolean renderVideo = true; @@ -235,6 +236,12 @@ public class PeerConnectionClient { if (signalingParameters.videoConstraints == null) { 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) { int videoWidth = peerConnectionParameters.videoWidth; int videoHeight = peerConnectionParameters.videoHeight; @@ -365,8 +372,14 @@ public class PeerConnectionClient { mediaStream = factory.createLocalMediaStream("ARDAMS"); if (videoCallEnabled) { - videoCapturer = VideoCapturerAndroid.create( - VideoCapturerAndroid.getNameOfFrontFacingDevice()); + String cameraDeviceName = VideoCapturerAndroid.getDeviceName(0); + 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)); } @@ -735,8 +748,8 @@ public class PeerConnectionClient { } private void switchCameraInternal() { - if (!videoCallEnabled) { - return; // No video is sent. + if (!videoCallEnabled || numberOfCameras < 2) { + return; // No video is sent or only one camera is available. } Log.d(TAG, "Switch camera"); videoCapturer.switchCamera();