michael@0: /* michael@0: * Copyright (C) 2012-2014 Mozilla Foundation michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #include "ICameraControl.h" michael@0: michael@0: #include michael@0: michael@0: #include "CameraCommon.h" michael@0: #include "GonkCameraControl.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: // From ICameraControl, gonk-specific management functions michael@0: nsresult michael@0: ICameraControl::GetNumberOfCameras(int32_t& aDeviceCount) michael@0: { michael@0: aDeviceCount = android::Camera::getNumberOfCameras(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: ICameraControl::GetCameraName(uint32_t aDeviceNum, nsCString& aDeviceName) michael@0: { michael@0: int32_t count = android::Camera::getNumberOfCameras(); michael@0: int32_t deviceNum = static_cast(aDeviceNum); michael@0: michael@0: DOM_CAMERA_LOGI("GetCameraName : getNumberOfCameras() returned %d\n", count); michael@0: if (deviceNum < 0 || deviceNum > count) { michael@0: DOM_CAMERA_LOGE("GetCameraName : invalid device number (%u)\n", aDeviceNum); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: android::CameraInfo info; michael@0: int rv = android::Camera::getCameraInfo(deviceNum, &info); michael@0: if (rv != 0) { michael@0: DOM_CAMERA_LOGE("GetCameraName : get_camera_info(%d) failed: %d\n", deviceNum, rv); michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: switch (info.facing) { michael@0: case CAMERA_FACING_BACK: michael@0: aDeviceName.Assign("back"); michael@0: break; michael@0: michael@0: case CAMERA_FACING_FRONT: michael@0: aDeviceName.Assign("front"); michael@0: break; michael@0: michael@0: default: michael@0: aDeviceName.Assign("extra-camera-"); michael@0: aDeviceName.AppendInt(deviceNum); michael@0: break; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: ICameraControl::GetListOfCameras(nsTArray& aList) michael@0: { michael@0: int32_t count = android::Camera::getNumberOfCameras(); michael@0: DOM_CAMERA_LOGI("getListOfCameras : getNumberOfCameras() returned %d\n", count); michael@0: if (count <= 0) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Allocate 2 extra slots to reserve space for 'front' and 'back' cameras michael@0: // at the front of the array--we will collapse any empty slots below. michael@0: aList.SetLength(2); michael@0: uint32_t extraIdx = 2; michael@0: bool gotFront = false; michael@0: bool gotBack = false; michael@0: while (count--) { michael@0: nsCString cameraName; michael@0: nsresult result = GetCameraName(count, cameraName); michael@0: if (result != NS_OK) { michael@0: continue; michael@0: } michael@0: michael@0: // The first camera we find named 'back' gets slot 0; and the first michael@0: // we find named 'front' gets slot 1. All others appear after these. michael@0: if (cameraName.EqualsLiteral("back")) { michael@0: CopyUTF8toUTF16(cameraName, aList[0]); michael@0: gotBack = true; michael@0: } else if (cameraName.EqualsLiteral("front")) { michael@0: CopyUTF8toUTF16(cameraName, aList[1]); michael@0: gotFront = true; michael@0: } else { michael@0: CopyUTF8toUTF16(cameraName, *aList.InsertElementAt(extraIdx)); michael@0: extraIdx++; michael@0: } michael@0: } michael@0: michael@0: if (!gotFront) { michael@0: aList.RemoveElementAt(1); michael@0: } michael@0: michael@0: if (!gotBack) { michael@0: aList.RemoveElementAt(0); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // implementation-specific camera factory michael@0: already_AddRefed michael@0: ICameraControl::Create(uint32_t aCameraId) michael@0: { michael@0: nsRefPtr control = new nsGonkCameraControl(aCameraId); michael@0: return control.forget(); michael@0: }