From fc516077ed87ee99f420b3d76eb96bae3775714d Mon Sep 17 00:00:00 2001 From: "glaznev@webrtc.org" Date: Tue, 10 Mar 2015 18:20:56 +0000 Subject: [PATCH] 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 --- .../src/org/webrtc/VideoCapturerAndroid.java | 9 ++++++-- .../appspot/apprtc/PeerConnectionClient.java | 21 +++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) 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();