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
diff --git a/talk/app/webrtc/java/src/org/webrtc/VideoCapturerAndroid.java b/talk/app/webrtc/java/src/org/webrtc/VideoCapturerAndroid.java
index ef6666a..ede2549 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 @@
     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 @@
       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 @@
       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 7910023..46354c9 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 @@
   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 @@
     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 @@
 
     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 @@
   }
 
   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();