Android, fixes crash on devices with only front cameras.

BUG=2807
R=fischman@webrtc.org

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk/webrtc@5415 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/examples/android/media_demo/src/org/webrtc/webrtcdemo/MediaEngine.java b/examples/android/media_demo/src/org/webrtc/webrtcdemo/MediaEngine.java
index ac73203..654d6a1 100644
--- a/examples/android/media_demo/src/org/webrtc/webrtcdemo/MediaEngine.java
+++ b/examples/android/media_demo/src/org/webrtc/webrtcdemo/MediaEngine.java
@@ -568,7 +568,7 @@
     return useFrontCamera;
   }
 
-  // Set camera to front if one exists otherwise use back camera.
+  // Set default camera to front if there is a front camera.
   private void setDefaultCamera() {
     useFrontCamera = hasFrontCamera();
   }
@@ -584,7 +584,7 @@
   }
 
   private void startCamera() {
-    CameraDesc cameraInfo = vie.getCaptureDevice(getCameraId());
+    CameraDesc cameraInfo = vie.getCaptureDevice(getCameraId(getCameraIndex()));
     currentCameraHandle = vie.allocateCaptureDevice(cameraInfo);
     cameraInfo.dispose();
     check(vie.connectCaptureDevice(currentCameraHandle, videoChannel) == 0,
@@ -700,11 +700,22 @@
         "vie StartRtpDump");
   }
 
-  private int getCameraId() {
+  private int getCameraIndex() {
     return useFrontCamera ? Camera.CameraInfo.CAMERA_FACING_FRONT :
         Camera.CameraInfo.CAMERA_FACING_BACK;
   }
 
+  private int getCameraId(int index) {
+    for (int i = Camera.getNumberOfCameras() - 1; i >= 0; --i) {
+      CameraInfo info = new CameraInfo();
+      Camera.getCameraInfo(i, info);
+      if (index == info.facing) {
+        return i;
+      }
+    }
+    throw new RuntimeException("Index does not match a camera");
+  }
+
   private void compensateRotation() {
     if (svLocal == null) {
       // Not rendering (or sending).
@@ -714,7 +725,7 @@
       return;
     }
     int cameraRotation = rotationFromRealWorldUp(
-        cameras[getCameraId()], deviceOrientation);
+        cameras[getCameraIndex()], deviceOrientation);
     // Egress streams should have real world up as up.
     check(
         vie.setRotateCapturedFrames(currentCameraHandle, cameraRotation) == 0,