dom/camera/GonkCameraManager.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/camera/GonkCameraManager.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012-2014 Mozilla Foundation
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +#include "ICameraControl.h"
    1.21 +
    1.22 +#include <camera/Camera.h>
    1.23 +
    1.24 +#include "CameraCommon.h"
    1.25 +#include "GonkCameraControl.h"
    1.26 +
    1.27 +using namespace mozilla;
    1.28 +
    1.29 +// From ICameraControl, gonk-specific management functions
    1.30 +nsresult
    1.31 +ICameraControl::GetNumberOfCameras(int32_t& aDeviceCount)
    1.32 +{
    1.33 +  aDeviceCount = android::Camera::getNumberOfCameras();
    1.34 +  return NS_OK;
    1.35 +}
    1.36 +
    1.37 +nsresult
    1.38 +ICameraControl::GetCameraName(uint32_t aDeviceNum, nsCString& aDeviceName)
    1.39 +{
    1.40 +  int32_t count = android::Camera::getNumberOfCameras();
    1.41 +  int32_t deviceNum = static_cast<int32_t>(aDeviceNum);
    1.42 +
    1.43 +  DOM_CAMERA_LOGI("GetCameraName : getNumberOfCameras() returned %d\n", count);
    1.44 +  if (deviceNum < 0 || deviceNum > count) {
    1.45 +    DOM_CAMERA_LOGE("GetCameraName : invalid device number (%u)\n", aDeviceNum);
    1.46 +    return NS_ERROR_NOT_AVAILABLE;
    1.47 +  }
    1.48 +
    1.49 +  android::CameraInfo info;
    1.50 +  int rv = android::Camera::getCameraInfo(deviceNum, &info);
    1.51 +  if (rv != 0) {
    1.52 +    DOM_CAMERA_LOGE("GetCameraName : get_camera_info(%d) failed: %d\n", deviceNum, rv);
    1.53 +    return NS_ERROR_NOT_AVAILABLE;
    1.54 +  }
    1.55 +
    1.56 +  switch (info.facing) {
    1.57 +    case CAMERA_FACING_BACK:
    1.58 +      aDeviceName.Assign("back");
    1.59 +      break;
    1.60 +
    1.61 +    case CAMERA_FACING_FRONT:
    1.62 +      aDeviceName.Assign("front");
    1.63 +      break;
    1.64 +
    1.65 +    default:
    1.66 +      aDeviceName.Assign("extra-camera-");
    1.67 +      aDeviceName.AppendInt(deviceNum);
    1.68 +      break;
    1.69 +  }
    1.70 +  return NS_OK;
    1.71 +}
    1.72 +
    1.73 +nsresult
    1.74 +ICameraControl::GetListOfCameras(nsTArray<nsString>& aList)
    1.75 +{
    1.76 +  int32_t count = android::Camera::getNumberOfCameras();
    1.77 +  DOM_CAMERA_LOGI("getListOfCameras : getNumberOfCameras() returned %d\n", count);
    1.78 +  if (count <= 0) {
    1.79 +    return NS_OK;
    1.80 +  }
    1.81 +
    1.82 +  // Allocate 2 extra slots to reserve space for 'front' and 'back' cameras
    1.83 +  // at the front of the array--we will collapse any empty slots below.
    1.84 +  aList.SetLength(2);
    1.85 +  uint32_t extraIdx = 2;
    1.86 +  bool gotFront = false;
    1.87 +  bool gotBack = false;
    1.88 +  while (count--) {
    1.89 +    nsCString cameraName;
    1.90 +    nsresult result = GetCameraName(count, cameraName);
    1.91 +    if (result != NS_OK) {
    1.92 +      continue;
    1.93 +    }
    1.94 +
    1.95 +    // The first camera we find named 'back' gets slot 0; and the first
    1.96 +    // we find named 'front' gets slot 1.  All others appear after these.
    1.97 +    if (cameraName.EqualsLiteral("back")) {
    1.98 +      CopyUTF8toUTF16(cameraName, aList[0]);
    1.99 +      gotBack = true;
   1.100 +    } else if (cameraName.EqualsLiteral("front")) {
   1.101 +      CopyUTF8toUTF16(cameraName, aList[1]);
   1.102 +      gotFront = true;
   1.103 +    } else {
   1.104 +      CopyUTF8toUTF16(cameraName, *aList.InsertElementAt(extraIdx));
   1.105 +      extraIdx++;
   1.106 +    }
   1.107 +  }
   1.108 +
   1.109 +  if (!gotFront) {
   1.110 +    aList.RemoveElementAt(1);
   1.111 +  }
   1.112 +
   1.113 +  if (!gotBack) {
   1.114 +    aList.RemoveElementAt(0);
   1.115 +  }
   1.116 +
   1.117 +  return NS_OK;
   1.118 +}
   1.119 +
   1.120 +// implementation-specific camera factory
   1.121 +already_AddRefed<ICameraControl>
   1.122 +ICameraControl::Create(uint32_t aCameraId)
   1.123 +{
   1.124 +  nsRefPtr<nsGonkCameraControl> control = new nsGonkCameraControl(aCameraId);
   1.125 +  return control.forget();
   1.126 +}

mercurial