Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 16 | |
| 17 | #define LOG_TAG "CameraService" |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdio.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 21 | #include <string.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <pthread.h> |
| 24 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 25 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 26 | #include <binder/IPCThreadState.h> |
| 27 | #include <binder/IServiceManager.h> |
| 28 | #include <binder/MemoryBase.h> |
| 29 | #include <binder/MemoryHeapBase.h> |
| 30 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Amith Yamasani | 228711d | 2015-02-13 13:25:39 -0800 | [diff] [blame^] | 32 | #include <cutils/multiuser.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 33 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 34 | #include <hardware/hardware.h> |
| 35 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 36 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <media/mediaplayer.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 38 | #include <utils/Errors.h> |
| 39 | #include <utils/Log.h> |
| 40 | #include <utils/String16.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 41 | #include <utils/Trace.h> |
| 42 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 43 | #include <system/camera_metadata.h> |
| 44 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | |
| 46 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 47 | #include "api1/CameraClient.h" |
| 48 | #include "api1/Camera2Client.h" |
| 49 | #include "api_pro/ProCamera2Client.h" |
| 50 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 51 | #include "utils/CameraTraces.h" |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 52 | #include "CameraDeviceFactory.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 53 | |
| 54 | namespace android { |
| 55 | |
| 56 | // ---------------------------------------------------------------------------- |
| 57 | // Logging support -- this is for debugging only |
| 58 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 59 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 61 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 62 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 63 | |
| 64 | static void setLogLevel(int level) { |
| 65 | android_atomic_write(level, &gLogLevel); |
| 66 | } |
| 67 | |
| 68 | // ---------------------------------------------------------------------------- |
| 69 | |
| 70 | static int getCallingPid() { |
| 71 | return IPCThreadState::self()->getCallingPid(); |
| 72 | } |
| 73 | |
| 74 | static int getCallingUid() { |
| 75 | return IPCThreadState::self()->getCallingUid(); |
| 76 | } |
| 77 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 78 | extern "C" { |
| 79 | static void camera_device_status_change( |
| 80 | const struct camera_module_callbacks* callbacks, |
| 81 | int camera_id, |
| 82 | int new_status) { |
| 83 | sp<CameraService> cs = const_cast<CameraService*>( |
| 84 | static_cast<const CameraService*>(callbacks)); |
| 85 | |
| 86 | cs->onDeviceStatusChanged( |
| 87 | camera_id, |
| 88 | new_status); |
| 89 | } |
| 90 | } // extern "C" |
| 91 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 92 | // ---------------------------------------------------------------------------- |
| 93 | |
| 94 | // This is ugly and only safe if we never re-create the CameraService, but |
| 95 | // should be ok for now. |
| 96 | static CameraService *gCameraService; |
| 97 | |
| 98 | CameraService::CameraService() |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 99 | :mSoundRef(0), mModule(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 100 | { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 101 | ALOGI("CameraService started (pid=%d)", getpid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 102 | gCameraService = this; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 103 | |
| 104 | for (size_t i = 0; i < MAX_CAMERAS; ++i) { |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 105 | mStatusList[i] = ICameraServiceListener::STATUS_PRESENT; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 106 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 107 | |
| 108 | this->camera_device_status_change = android::camera_device_status_change; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 111 | void CameraService::onFirstRef() |
| 112 | { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 113 | LOG1("CameraService::onFirstRef"); |
| 114 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 115 | BnCameraService::onFirstRef(); |
| 116 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 117 | camera_module_t *rawModule; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 118 | if (hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 119 | (const hw_module_t **)&rawModule) < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 120 | ALOGE("Could not load camera HAL module"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 121 | mNumberOfCameras = 0; |
| 122 | } |
| 123 | else { |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 124 | mModule = new CameraModule(rawModule); |
| 125 | const hw_module_t *common = mModule->getRawModule(); |
| 126 | ALOGI("Loaded \"%s\" camera module", common->name); |
| 127 | mNumberOfCameras = mModule->getNumberOfCameras(); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 128 | if (mNumberOfCameras > MAX_CAMERAS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 129 | ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).", |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 130 | mNumberOfCameras, MAX_CAMERAS); |
| 131 | mNumberOfCameras = MAX_CAMERAS; |
| 132 | } |
| 133 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 134 | setCameraFree(i); |
| 135 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 136 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 137 | if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_1) { |
| 138 | mModule->setCallbacks(this); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 139 | } |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 140 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 141 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 142 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 143 | if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_2) { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 144 | setUpVendorTags(); |
| 145 | } |
| 146 | |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 147 | CameraDeviceFactory::registerService(this); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 151 | CameraService::~CameraService() { |
| 152 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 153 | if (mBusy[i]) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 154 | ALOGE("camera %d is still in use in destructor!", i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 158 | if (mModule) { |
| 159 | delete mModule; |
| 160 | } |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 161 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 162 | gCameraService = NULL; |
| 163 | } |
| 164 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 165 | void CameraService::onDeviceStatusChanged(int cameraId, |
| 166 | int newStatus) |
| 167 | { |
| 168 | ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__, |
| 169 | cameraId, newStatus); |
| 170 | |
| 171 | if (cameraId < 0 || cameraId >= MAX_CAMERAS) { |
| 172 | ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if ((int)getStatus(cameraId) == newStatus) { |
| 177 | ALOGE("%s: State transition to the same status 0x%x not allowed", |
| 178 | __FUNCTION__, (uint32_t)newStatus); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | /* don't do this in updateStatus |
| 183 | since it is also called from connect and we could get into a deadlock */ |
| 184 | if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) { |
| 185 | Vector<sp<BasicClient> > clientsToDisconnect; |
| 186 | { |
| 187 | Mutex::Autolock al(mServiceLock); |
| 188 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 189 | /* Remove cached parameters from shim cache */ |
| 190 | mShimParams.removeItem(cameraId); |
| 191 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 192 | /* Find all clients that we need to disconnect */ |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 193 | sp<BasicClient> client = mClient[cameraId].promote(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 194 | if (client.get() != NULL) { |
| 195 | clientsToDisconnect.push_back(client); |
| 196 | } |
| 197 | |
| 198 | int i = cameraId; |
| 199 | for (size_t j = 0; j < mProClientList[i].size(); ++j) { |
| 200 | sp<ProClient> cl = mProClientList[i][j].promote(); |
| 201 | if (cl != NULL) { |
| 202 | clientsToDisconnect.push_back(cl); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* now disconnect them. don't hold the lock |
| 208 | or we can get into a deadlock */ |
| 209 | |
| 210 | for (size_t i = 0; i < clientsToDisconnect.size(); ++i) { |
| 211 | sp<BasicClient> client = clientsToDisconnect[i]; |
| 212 | |
| 213 | client->disconnect(); |
| 214 | /** |
| 215 | * The remote app will no longer be able to call methods on the |
| 216 | * client since the client PID will be reset to 0 |
| 217 | */ |
| 218 | } |
| 219 | |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 220 | ALOGV("%s: After unplug, disconnected %zu clients", |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 221 | __FUNCTION__, clientsToDisconnect.size()); |
| 222 | } |
| 223 | |
| 224 | updateStatus( |
| 225 | static_cast<ICameraServiceListener::Status>(newStatus), cameraId); |
| 226 | |
| 227 | } |
| 228 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 229 | int32_t CameraService::getNumberOfCameras() { |
| 230 | return mNumberOfCameras; |
| 231 | } |
| 232 | |
| 233 | status_t CameraService::getCameraInfo(int cameraId, |
| 234 | struct CameraInfo* cameraInfo) { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 235 | if (!mModule) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 236 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 239 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 240 | return BAD_VALUE; |
| 241 | } |
| 242 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 243 | struct camera_info info; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 244 | status_t rc = filterGetInfoErrorCode( |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 245 | mModule->getCameraInfo(cameraId, &info)); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 246 | cameraInfo->facing = info.facing; |
| 247 | cameraInfo->orientation = info.orientation; |
| 248 | return rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 251 | |
| 252 | status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) { |
| 253 | status_t ret = OK; |
| 254 | struct CameraInfo info; |
| 255 | if ((ret = getCameraInfo(cameraId, &info)) != OK) { |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | CameraMetadata shimInfo; |
| 260 | int32_t orientation = static_cast<int32_t>(info.orientation); |
| 261 | if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) { |
| 262 | return ret; |
| 263 | } |
| 264 | |
| 265 | uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ? |
| 266 | ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK; |
| 267 | if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) { |
| 268 | return ret; |
| 269 | } |
| 270 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 271 | CameraParameters shimParams; |
| 272 | if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { |
| 273 | // Error logged by callee |
| 274 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | Vector<Size> sizes; |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 278 | Vector<Size> jpegSizes; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 279 | Vector<int32_t> formats; |
| 280 | const char* supportedPreviewFormats; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 281 | { |
| 282 | shimParams.getSupportedPreviewSizes(/*out*/sizes); |
| 283 | shimParams.getSupportedPreviewFormats(/*out*/formats); |
| 284 | shimParams.getSupportedPictureSizes(/*out*/jpegSizes); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // Always include IMPLEMENTATION_DEFINED |
| 288 | formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED); |
| 289 | |
| 290 | const size_t INTS_PER_CONFIG = 4; |
| 291 | |
| 292 | // Build available stream configurations metadata |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 293 | size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG; |
| 294 | |
| 295 | Vector<int32_t> streamConfigs; |
| 296 | streamConfigs.setCapacity(streamConfigSize); |
| 297 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 298 | for (size_t i = 0; i < formats.size(); ++i) { |
| 299 | for (size_t j = 0; j < sizes.size(); ++j) { |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 300 | streamConfigs.add(formats[i]); |
| 301 | streamConfigs.add(sizes[j].width); |
| 302 | streamConfigs.add(sizes[j].height); |
| 303 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 307 | for (size_t i = 0; i < jpegSizes.size(); ++i) { |
| 308 | streamConfigs.add(HAL_PIXEL_FORMAT_BLOB); |
| 309 | streamConfigs.add(jpegSizes[i].width); |
| 310 | streamConfigs.add(jpegSizes[i].height); |
| 311 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
| 312 | } |
| 313 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 314 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 315 | streamConfigs.array(), streamConfigSize)) != OK) { |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | int64_t fakeMinFrames[0]; |
| 320 | // TODO: Fixme, don't fake min frame durations. |
| 321 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 322 | fakeMinFrames, 0)) != OK) { |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | int64_t fakeStalls[0]; |
| 327 | // TODO: Fixme, don't fake stall durations. |
| 328 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 329 | fakeStalls, 0)) != OK) { |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | *cameraInfo = shimInfo; |
| 334 | return OK; |
| 335 | } |
| 336 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 337 | status_t CameraService::getCameraCharacteristics(int cameraId, |
| 338 | CameraMetadata* cameraInfo) { |
| 339 | if (!cameraInfo) { |
| 340 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
| 341 | return BAD_VALUE; |
| 342 | } |
| 343 | |
| 344 | if (!mModule) { |
| 345 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 346 | return -ENODEV; |
| 347 | } |
| 348 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 349 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 350 | ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId); |
| 351 | return BAD_VALUE; |
| 352 | } |
| 353 | |
| 354 | int facing; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 355 | status_t ret = OK; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 356 | if (mModule->getRawModule()->module_api_version < CAMERA_MODULE_API_VERSION_2_0 || |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 357 | getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) { |
| 358 | /** |
| 359 | * Backwards compatibility mode for old HALs: |
| 360 | * - Convert CameraInfo into static CameraMetadata properties. |
| 361 | * - Retrieve cached CameraParameters for this camera. If none exist, |
| 362 | * attempt to open CameraClient and retrieve the CameraParameters. |
| 363 | * - Convert cached CameraParameters into static CameraMetadata |
| 364 | * properties. |
| 365 | */ |
| 366 | ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 367 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 368 | if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) { |
| 369 | return ret; |
| 370 | } |
Zhijun He | f05e50e | 2013-10-01 11:05:33 -0700 | [diff] [blame] | 371 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 372 | } else { |
| 373 | /** |
| 374 | * Normal HAL 2.1+ codepath. |
| 375 | */ |
| 376 | struct camera_info info; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 377 | ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info)); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 378 | *cameraInfo = info.static_camera_characteristics; |
| 379 | } |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 384 | status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) { |
| 385 | if (!mModule) { |
| 386 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 387 | return -ENODEV; |
| 388 | } |
| 389 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 390 | desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 391 | return OK; |
| 392 | } |
| 393 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 394 | int CameraService::getDeviceVersion(int cameraId, int* facing) { |
| 395 | struct camera_info info; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 396 | if (mModule->getCameraInfo(cameraId, &info) != OK) { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 397 | return -1; |
| 398 | } |
| 399 | |
| 400 | int deviceVersion; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 401 | if (mModule->getRawModule()->module_api_version >= CAMERA_MODULE_API_VERSION_2_0) { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 402 | deviceVersion = info.device_version; |
| 403 | } else { |
| 404 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 405 | } |
| 406 | |
| 407 | if (facing) { |
| 408 | *facing = info.facing; |
| 409 | } |
| 410 | |
| 411 | return deviceVersion; |
| 412 | } |
| 413 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 414 | status_t CameraService::filterOpenErrorCode(status_t err) { |
| 415 | switch(err) { |
| 416 | case NO_ERROR: |
| 417 | case -EBUSY: |
| 418 | case -EINVAL: |
| 419 | case -EUSERS: |
| 420 | return err; |
| 421 | default: |
| 422 | break; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 423 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 424 | return -ENODEV; |
| 425 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 426 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 427 | status_t CameraService::filterGetInfoErrorCode(status_t err) { |
| 428 | switch(err) { |
| 429 | case NO_ERROR: |
| 430 | case -EINVAL: |
| 431 | return err; |
| 432 | default: |
| 433 | break; |
| 434 | } |
| 435 | return -ENODEV; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 438 | bool CameraService::setUpVendorTags() { |
| 439 | vendor_tag_ops_t vOps = vendor_tag_ops_t(); |
| 440 | |
| 441 | // Check if vendor operations have been implemented |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 442 | if (!mModule->isVendorTagDefined()) { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 443 | ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__); |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops"); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 448 | mModule->getVendorTagOps(&vOps); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 449 | ATRACE_END(); |
| 450 | |
| 451 | // Ensure all vendor operations are present |
| 452 | if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL || |
| 453 | vOps.get_section_name == NULL || vOps.get_tag_name == NULL || |
| 454 | vOps.get_tag_type == NULL) { |
| 455 | ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions." |
| 456 | , __FUNCTION__); |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | // Read all vendor tag definitions into a descriptor |
| 461 | sp<VendorTagDescriptor> desc; |
| 462 | status_t res; |
| 463 | if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc)) |
| 464 | != OK) { |
| 465 | ALOGE("%s: Could not generate descriptor from vendor tag operations," |
| 466 | "received error %s (%d). Camera clients will not be able to use" |
| 467 | "vendor tags", __FUNCTION__, strerror(res), res); |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | // Set the global descriptor to use with camera metadata |
| 472 | VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 473 | return true; |
| 474 | } |
| 475 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 476 | status_t CameraService::initializeShimMetadata(int cameraId) { |
| 477 | int pid = getCallingPid(); |
| 478 | int uid = getCallingUid(); |
| 479 | status_t ret = validateConnect(cameraId, uid); |
| 480 | if (ret != OK) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 481 | // Error already logged by callee |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | bool needsNewClient = false; |
| 486 | sp<Client> client; |
| 487 | |
| 488 | String16 internalPackageName("media"); |
| 489 | { // Scope for service lock |
| 490 | Mutex::Autolock lock(mServiceLock); |
| 491 | if (mClient[cameraId] != NULL) { |
| 492 | client = static_cast<Client*>(mClient[cameraId].promote().get()); |
| 493 | } |
| 494 | if (client == NULL) { |
| 495 | needsNewClient = true; |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 496 | ret = connectHelperLocked(/*out*/client, |
| 497 | /*cameraClient*/NULL, // Empty binder callbacks |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 498 | cameraId, |
| 499 | internalPackageName, |
| 500 | uid, |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 501 | pid); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 502 | |
| 503 | if (ret != OK) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 504 | // Error already logged by callee |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 505 | return ret; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | if (client == NULL) { |
| 510 | ALOGE("%s: Could not connect to client camera device.", __FUNCTION__); |
| 511 | return BAD_VALUE; |
| 512 | } |
| 513 | |
| 514 | String8 rawParams = client->getParameters(); |
| 515 | CameraParameters params(rawParams); |
| 516 | mShimParams.add(cameraId, params); |
| 517 | } |
| 518 | |
| 519 | // Close client if one was opened solely for this call |
| 520 | if (needsNewClient) { |
| 521 | client->disconnect(); |
| 522 | } |
| 523 | return OK; |
| 524 | } |
| 525 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 526 | status_t CameraService::getLegacyParametersLazy(int cameraId, |
| 527 | /*out*/ |
| 528 | CameraParameters* parameters) { |
| 529 | |
| 530 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 531 | |
| 532 | status_t ret = 0; |
| 533 | |
| 534 | if (parameters == NULL) { |
| 535 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
| 536 | return BAD_VALUE; |
| 537 | } |
| 538 | |
| 539 | ssize_t index = -1; |
| 540 | { // Scope for service lock |
| 541 | Mutex::Autolock lock(mServiceLock); |
| 542 | index = mShimParams.indexOfKey(cameraId); |
| 543 | // Release service lock so initializeShimMetadata can be called correctly. |
| 544 | |
| 545 | if (index >= 0) { |
| 546 | *parameters = mShimParams[index]; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | if (index < 0) { |
| 551 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 552 | ret = initializeShimMetadata(cameraId); |
| 553 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 554 | if (ret != OK) { |
| 555 | // Error already logged by callee |
| 556 | return ret; |
| 557 | } |
| 558 | |
| 559 | { // Scope for service lock |
| 560 | Mutex::Autolock lock(mServiceLock); |
| 561 | index = mShimParams.indexOfKey(cameraId); |
| 562 | |
| 563 | LOG_ALWAYS_FATAL_IF(index < 0, "index should have been initialized"); |
| 564 | |
| 565 | *parameters = mShimParams[index]; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | return OK; |
| 570 | } |
| 571 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 572 | status_t CameraService::validateConnect(int cameraId, |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 573 | /*inout*/ |
| 574 | int& clientUid) const { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 575 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 576 | int callingPid = getCallingPid(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 577 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 578 | if (clientUid == USE_CALLING_UID) { |
| 579 | clientUid = getCallingUid(); |
| 580 | } else { |
| 581 | // We only trust our own process to forward client UIDs |
| 582 | if (callingPid != getpid()) { |
| 583 | ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)", |
| 584 | callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 585 | return PERMISSION_DENIED; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 588 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 589 | if (!mModule) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 590 | ALOGE("Camera HAL module not loaded"); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 591 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 594 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 595 | ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 596 | callingPid, cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 597 | return -ENODEV; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 600 | char value[PROPERTY_VALUE_MAX]; |
Amith Yamasani | 228711d | 2015-02-13 13:25:39 -0800 | [diff] [blame^] | 601 | char key[PROPERTY_KEY_MAX]; |
| 602 | int clientUserId = multiuser_get_user_id(clientUid); |
| 603 | snprintf(key, PROPERTY_KEY_MAX, "sys.secpolicy.camera.off_%d", clientUserId); |
| 604 | property_get(key, value, "0"); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 605 | if (strcmp(value, "1") == 0) { |
| 606 | // Camera is disabled by DevicePolicyManager. |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 607 | ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 608 | return -EACCES; |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 609 | } |
| 610 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 611 | ICameraServiceListener::Status currentStatus = getStatus(cameraId); |
| 612 | if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) { |
| 613 | ALOGI("Camera is not plugged in," |
| 614 | " connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 615 | return -ENODEV; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 616 | } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) { |
| 617 | ALOGI("Camera is enumerating," |
| 618 | " connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 619 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 620 | } |
| 621 | // Else don't check for STATUS_NOT_AVAILABLE. |
| 622 | // -- It's done implicitly in canConnectUnsafe /w the mBusy array |
| 623 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 624 | return OK; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | bool CameraService::canConnectUnsafe(int cameraId, |
| 628 | const String16& clientPackageName, |
| 629 | const sp<IBinder>& remoteCallback, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 630 | sp<BasicClient> &client) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 631 | String8 clientName8(clientPackageName); |
| 632 | int callingPid = getCallingPid(); |
| 633 | |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 634 | if (mClient[cameraId] != 0) { |
| 635 | client = mClient[cameraId].promote(); |
| 636 | if (client != 0) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 637 | if (remoteCallback == client->getRemote()) { |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 638 | LOG1("CameraService::connect X (pid %d) (the same client)", |
| 639 | callingPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 640 | return true; |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 641 | } else { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 642 | // TODOSC: need to support 1 regular client, |
| 643 | // multiple shared clients here |
| 644 | ALOGW("CameraService::connect X (pid %d) rejected" |
| 645 | " (existing client).", callingPid); |
| 646 | return false; |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 647 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 648 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 649 | mClient[cameraId].clear(); |
| 650 | } |
| 651 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 652 | /* |
| 653 | mBusy is set to false as the last step of the Client destructor, |
| 654 | after which it is guaranteed that the Client destructor has finished ( |
| 655 | including any inherited destructors) |
| 656 | |
| 657 | We only need this for a Client subclasses since we don't allow |
| 658 | multiple Clents to be opened concurrently, but multiple BasicClient |
| 659 | would be fine |
| 660 | */ |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 661 | if (mBusy[cameraId]) { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 662 | ALOGW("CameraService::connect X (pid %d, \"%s\") rejected" |
| 663 | " (camera %d is still busy).", callingPid, |
| 664 | clientName8.string(), cameraId); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 665 | return false; |
| 666 | } |
| 667 | |
| 668 | return true; |
| 669 | } |
| 670 | |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 671 | status_t CameraService::connectHelperLocked( |
| 672 | /*out*/ |
| 673 | sp<Client>& client, |
| 674 | /*in*/ |
| 675 | const sp<ICameraClient>& cameraClient, |
| 676 | int cameraId, |
| 677 | const String16& clientPackageName, |
| 678 | int clientUid, |
| 679 | int callingPid, |
| 680 | int halVersion, |
| 681 | bool legacyMode) { |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 682 | |
| 683 | int facing = -1; |
| 684 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 685 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 686 | if (halVersion < 0 || halVersion == deviceVersion) { |
| 687 | // Default path: HAL version is unspecified by caller, create CameraClient |
| 688 | // based on device version reported by the HAL. |
| 689 | switch(deviceVersion) { |
| 690 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 691 | client = new CameraClient(this, cameraClient, |
| 692 | clientPackageName, cameraId, |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 693 | facing, callingPid, clientUid, getpid(), legacyMode); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 694 | break; |
| 695 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 696 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 697 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 698 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 699 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 700 | client = new Camera2Client(this, cameraClient, |
| 701 | clientPackageName, cameraId, |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 702 | facing, callingPid, clientUid, getpid(), legacyMode); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 703 | break; |
| 704 | case -1: |
| 705 | ALOGE("Invalid camera id %d", cameraId); |
| 706 | return BAD_VALUE; |
| 707 | default: |
| 708 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 709 | return INVALID_OPERATION; |
| 710 | } |
| 711 | } else { |
| 712 | // A particular HAL version is requested by caller. Create CameraClient |
| 713 | // based on the requested HAL version. |
| 714 | if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 && |
| 715 | halVersion == CAMERA_DEVICE_API_VERSION_1_0) { |
| 716 | // Only support higher HAL version device opened as HAL1.0 device. |
| 717 | client = new CameraClient(this, cameraClient, |
| 718 | clientPackageName, cameraId, |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 719 | facing, callingPid, clientUid, getpid(), legacyMode); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 720 | } else { |
| 721 | // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet. |
| 722 | ALOGE("Invalid camera HAL version %x: HAL %x device can only be" |
| 723 | " opened as HAL %x device", halVersion, deviceVersion, |
| 724 | CAMERA_DEVICE_API_VERSION_1_0); |
| 725 | return INVALID_OPERATION; |
| 726 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 730 | if (status != OK) { |
| 731 | // this is probably not recoverable.. maybe the client can try again |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 732 | return status; |
| 733 | } |
| 734 | |
| 735 | mClient[cameraId] = client; |
| 736 | LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, |
| 737 | getpid()); |
| 738 | |
| 739 | return OK; |
| 740 | } |
| 741 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 742 | status_t CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 743 | const sp<ICameraClient>& cameraClient, |
| 744 | int cameraId, |
| 745 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 746 | int clientUid, |
| 747 | /*out*/ |
| 748 | sp<ICamera>& device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 749 | |
| 750 | String8 clientName8(clientPackageName); |
| 751 | int callingPid = getCallingPid(); |
| 752 | |
| 753 | LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid, |
| 754 | clientName8.string(), cameraId); |
| 755 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 756 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 757 | if (status != OK) { |
| 758 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 761 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 762 | sp<Client> client; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 763 | { |
| 764 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 765 | sp<BasicClient> clientTmp; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 766 | if (!canConnectUnsafe(cameraId, clientPackageName, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 767 | IInterface::asBinder(cameraClient), |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 768 | /*out*/clientTmp)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 769 | return -EBUSY; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 770 | } else if (client.get() != NULL) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 771 | device = static_cast<Client*>(clientTmp.get()); |
| 772 | return OK; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 775 | status = connectHelperLocked(/*out*/client, |
| 776 | cameraClient, |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 777 | cameraId, |
| 778 | clientPackageName, |
| 779 | clientUid, |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 780 | callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 781 | if (status != OK) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 782 | return status; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 785 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 786 | // important: release the mutex here so the client can call back |
| 787 | // into the service from its destructor (can be at the end of the call) |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 788 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 789 | device = client; |
| 790 | return OK; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 793 | status_t CameraService::connectLegacy( |
| 794 | const sp<ICameraClient>& cameraClient, |
| 795 | int cameraId, int halVersion, |
| 796 | const String16& clientPackageName, |
| 797 | int clientUid, |
| 798 | /*out*/ |
| 799 | sp<ICamera>& device) { |
| 800 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 801 | int apiVersion = mModule->getRawModule()->module_api_version; |
Igor Murashkin | 3d07d1a | 2014-06-20 11:27:03 -0700 | [diff] [blame] | 802 | if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED && |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 803 | apiVersion < CAMERA_MODULE_API_VERSION_2_3) { |
Igor Murashkin | 3d07d1a | 2014-06-20 11:27:03 -0700 | [diff] [blame] | 804 | /* |
| 805 | * Either the HAL version is unspecified in which case this just creates |
| 806 | * a camera client selected by the latest device version, or |
| 807 | * it's a particular version in which case the HAL must supported |
| 808 | * the open_legacy call |
| 809 | */ |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 810 | ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!", |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 811 | __FUNCTION__, apiVersion); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 812 | return INVALID_OPERATION; |
| 813 | } |
| 814 | |
| 815 | String8 clientName8(clientPackageName); |
| 816 | int callingPid = getCallingPid(); |
| 817 | |
| 818 | LOG1("CameraService::connect legacy E (pid %d \"%s\", id %d)", callingPid, |
| 819 | clientName8.string(), cameraId); |
| 820 | |
| 821 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 822 | if (status != OK) { |
| 823 | return status; |
| 824 | } |
| 825 | |
| 826 | sp<Client> client; |
| 827 | { |
| 828 | Mutex::Autolock lock(mServiceLock); |
| 829 | sp<BasicClient> clientTmp; |
| 830 | if (!canConnectUnsafe(cameraId, clientPackageName, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 831 | IInterface::asBinder(cameraClient), |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 832 | /*out*/clientTmp)) { |
| 833 | return -EBUSY; |
| 834 | } else if (client.get() != NULL) { |
| 835 | device = static_cast<Client*>(clientTmp.get()); |
| 836 | return OK; |
| 837 | } |
| 838 | |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 839 | status = connectHelperLocked(/*out*/client, |
| 840 | cameraClient, |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 841 | cameraId, |
| 842 | clientPackageName, |
| 843 | clientUid, |
| 844 | callingPid, |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 845 | halVersion, |
| 846 | /*legacyMode*/true); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 847 | if (status != OK) { |
| 848 | return status; |
| 849 | } |
| 850 | |
| 851 | } |
| 852 | // important: release the mutex here so the client can call back |
| 853 | // into the service from its destructor (can be at the end of the call) |
| 854 | |
| 855 | device = client; |
| 856 | return OK; |
| 857 | } |
| 858 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 859 | status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client, |
| 860 | const sp<IBinder>& remoteCallback) { |
| 861 | status_t status = client->initialize(mModule); |
| 862 | if (status != OK) { |
Ruben Brunk | 5fc9d90 | 2014-11-20 13:34:36 -0800 | [diff] [blame] | 863 | ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 864 | return status; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 865 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 866 | if (remoteCallback != NULL) { |
| 867 | remoteCallback->linkToDeath(this); |
| 868 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 869 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 870 | return OK; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 871 | } |
| 872 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 873 | status_t CameraService::connectPro( |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 874 | const sp<IProCameraCallbacks>& cameraCb, |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 875 | int cameraId, |
| 876 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 877 | int clientUid, |
| 878 | /*out*/ |
| 879 | sp<IProCameraUser>& device) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 880 | { |
Natalie Silvanovich | e1b55da | 2014-05-01 14:44:52 -0700 | [diff] [blame] | 881 | if (cameraCb == 0) { |
| 882 | ALOGE("%s: Callback must not be null", __FUNCTION__); |
| 883 | return BAD_VALUE; |
| 884 | } |
| 885 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 886 | String8 clientName8(clientPackageName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 887 | int callingPid = getCallingPid(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 888 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 889 | LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid, |
| 890 | clientName8.string(), cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 891 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 892 | if (status != OK) { |
| 893 | return status; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 894 | } |
| 895 | |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 896 | sp<ProClient> client; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 897 | { |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 898 | Mutex::Autolock lock(mServiceLock); |
| 899 | { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 900 | sp<BasicClient> client; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 901 | if (!canConnectUnsafe(cameraId, clientPackageName, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 902 | IInterface::asBinder(cameraCb), |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 903 | /*out*/client)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 904 | return -EBUSY; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
| 908 | int facing = -1; |
| 909 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 910 | |
| 911 | switch(deviceVersion) { |
| 912 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 913 | ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", |
| 914 | cameraId); |
Ruben Brunk | 17963d1 | 2013-08-19 15:21:19 -0700 | [diff] [blame] | 915 | return -EOPNOTSUPP; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 916 | break; |
| 917 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 918 | case CAMERA_DEVICE_API_VERSION_2_1: |
Zhijun He | 4711005 | 2013-07-22 17:34:34 -0700 | [diff] [blame] | 919 | case CAMERA_DEVICE_API_VERSION_3_0: |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 920 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 921 | case CAMERA_DEVICE_API_VERSION_3_2: |
Eino-Ville Talvala | 63d877f | 2014-06-16 19:21:12 -0700 | [diff] [blame] | 922 | client = new ProCamera2Client(this, cameraCb, clientPackageName, |
| 923 | cameraId, facing, callingPid, clientUid, getpid()); |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 924 | break; |
| 925 | case -1: |
| 926 | ALOGE("Invalid camera id %d", cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 927 | return BAD_VALUE; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 928 | default: |
| 929 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 930 | return INVALID_OPERATION; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 931 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 932 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 933 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 934 | if (status != OK) { |
| 935 | return status; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | mProClientList[cameraId].push(client); |
| 939 | |
| 940 | LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId, |
| 941 | getpid()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 942 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 943 | // important: release the mutex here so the client can call back |
| 944 | // into the service from its destructor (can be at the end of the call) |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 945 | device = client; |
| 946 | return OK; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 947 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 948 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 949 | status_t CameraService::connectDevice( |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 950 | const sp<ICameraDeviceCallbacks>& cameraCb, |
| 951 | int cameraId, |
| 952 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 953 | int clientUid, |
| 954 | /*out*/ |
| 955 | sp<ICameraDeviceUser>& device) |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 956 | { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 957 | |
| 958 | String8 clientName8(clientPackageName); |
| 959 | int callingPid = getCallingPid(); |
| 960 | |
| 961 | LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid, |
| 962 | clientName8.string(), cameraId); |
| 963 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 964 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 965 | if (status != OK) { |
| 966 | return status; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | sp<CameraDeviceClient> client; |
| 970 | { |
| 971 | Mutex::Autolock lock(mServiceLock); |
| 972 | { |
| 973 | sp<BasicClient> client; |
| 974 | if (!canConnectUnsafe(cameraId, clientPackageName, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 975 | IInterface::asBinder(cameraCb), |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 976 | /*out*/client)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 977 | return -EBUSY; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
| 981 | int facing = -1; |
| 982 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 983 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 984 | switch(deviceVersion) { |
| 985 | case CAMERA_DEVICE_API_VERSION_1_0: |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 986 | ALOGW("Camera using old HAL version: %d", deviceVersion); |
Ruben Brunk | 17963d1 | 2013-08-19 15:21:19 -0700 | [diff] [blame] | 987 | return -EOPNOTSUPP; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 988 | // TODO: don't allow 2.0 Only allow 2.1 and higher |
| 989 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 990 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 991 | case CAMERA_DEVICE_API_VERSION_3_0: |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 992 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 993 | case CAMERA_DEVICE_API_VERSION_3_2: |
Eino-Ville Talvala | 63d877f | 2014-06-16 19:21:12 -0700 | [diff] [blame] | 994 | client = new CameraDeviceClient(this, cameraCb, clientPackageName, |
| 995 | cameraId, facing, callingPid, clientUid, getpid()); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 996 | break; |
| 997 | case -1: |
| 998 | ALOGE("Invalid camera id %d", cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 999 | return BAD_VALUE; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1000 | default: |
| 1001 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1002 | return INVALID_OPERATION; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1005 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 1006 | if (status != OK) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1007 | // this is probably not recoverable.. maybe the client can try again |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1008 | return status; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId, |
| 1012 | getpid()); |
| 1013 | |
| 1014 | mClient[cameraId] = client; |
| 1015 | } |
| 1016 | // important: release the mutex here so the client can call back |
| 1017 | // into the service from its destructor (can be at the end of the call) |
| 1018 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1019 | device = client; |
| 1020 | return OK; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1024 | status_t CameraService::addListener( |
| 1025 | const sp<ICameraServiceListener>& listener) { |
| 1026 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1027 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 1028 | if (listener == 0) { |
| 1029 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
| 1030 | return BAD_VALUE; |
| 1031 | } |
| 1032 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1033 | Mutex::Autolock lock(mServiceLock); |
| 1034 | |
| 1035 | Vector<sp<ICameraServiceListener> >::iterator it, end; |
| 1036 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1037 | if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1038 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 1039 | __FUNCTION__, listener.get()); |
| 1040 | return ALREADY_EXISTS; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | mListenerList.push_back(listener); |
| 1045 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1046 | /* Immediately signal current status to this listener only */ |
| 1047 | { |
| 1048 | Mutex::Autolock m(mStatusMutex) ; |
| 1049 | int numCams = getNumberOfCameras(); |
| 1050 | for (int i = 0; i < numCams; ++i) { |
| 1051 | listener->onStatusChanged(mStatusList[i], i); |
| 1052 | } |
| 1053 | } |
| 1054 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1055 | return OK; |
| 1056 | } |
| 1057 | status_t CameraService::removeListener( |
| 1058 | const sp<ICameraServiceListener>& listener) { |
| 1059 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 1060 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 1061 | if (listener == 0) { |
| 1062 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
| 1063 | return BAD_VALUE; |
| 1064 | } |
| 1065 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1066 | Mutex::Autolock lock(mServiceLock); |
| 1067 | |
| 1068 | Vector<sp<ICameraServiceListener> >::iterator it; |
| 1069 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1070 | if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1071 | mListenerList.erase(it); |
| 1072 | return OK; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 1077 | __FUNCTION__, listener.get()); |
| 1078 | |
| 1079 | return BAD_VALUE; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1082 | status_t CameraService::getLegacyParameters( |
| 1083 | int cameraId, |
| 1084 | /*out*/ |
| 1085 | String16* parameters) { |
| 1086 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 1087 | |
| 1088 | if (parameters == NULL) { |
| 1089 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
| 1090 | return BAD_VALUE; |
| 1091 | } |
| 1092 | |
| 1093 | status_t ret = 0; |
| 1094 | |
| 1095 | CameraParameters shimParams; |
| 1096 | if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { |
| 1097 | // Error logged by caller |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
| 1101 | String8 shimParamsString8 = shimParams.flatten(); |
| 1102 | String16 shimParamsString16 = String16(shimParamsString8); |
| 1103 | |
| 1104 | *parameters = shimParamsString16; |
| 1105 | |
| 1106 | return OK; |
| 1107 | } |
| 1108 | |
| 1109 | status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) { |
| 1110 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 1111 | |
| 1112 | switch (apiVersion) { |
| 1113 | case API_VERSION_1: |
| 1114 | case API_VERSION_2: |
| 1115 | break; |
| 1116 | default: |
| 1117 | ALOGE("%s: Bad API version %d", __FUNCTION__, apiVersion); |
| 1118 | return BAD_VALUE; |
| 1119 | } |
| 1120 | |
| 1121 | int facing = -1; |
| 1122 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 1123 | |
| 1124 | switch(deviceVersion) { |
| 1125 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1126 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 1127 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 1128 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1129 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1130 | if (apiVersion == API_VERSION_2) { |
| 1131 | ALOGV("%s: Camera id %d uses HAL prior to HAL3.2, doesn't support api2 without shim", |
| 1132 | __FUNCTION__, cameraId); |
| 1133 | return -EOPNOTSUPP; |
| 1134 | } else { // if (apiVersion == API_VERSION_1) { |
| 1135 | ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported", |
| 1136 | __FUNCTION__, cameraId); |
| 1137 | return OK; |
| 1138 | } |
| 1139 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 1140 | ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly", |
| 1141 | __FUNCTION__, cameraId); |
| 1142 | return OK; |
| 1143 | case -1: |
| 1144 | ALOGE("%s: Invalid camera id %d", __FUNCTION__, cameraId); |
| 1145 | return BAD_VALUE; |
| 1146 | default: |
| 1147 | ALOGE("%s: Unknown camera device HAL version: %d", __FUNCTION__, deviceVersion); |
| 1148 | return INVALID_OPERATION; |
| 1149 | } |
| 1150 | |
| 1151 | return OK; |
| 1152 | } |
| 1153 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1154 | void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) { |
| 1155 | int callingPid = getCallingPid(); |
| 1156 | LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1157 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1158 | // Declare this before the lock to make absolutely sure the |
| 1159 | // destructor won't be called with the lock held. |
| 1160 | Mutex::Autolock lock(mServiceLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1161 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1162 | int outIndex; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1163 | sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1164 | |
| 1165 | if (client != 0) { |
| 1166 | // Found our camera, clear and leave. |
| 1167 | LOG1("removeClient: clear camera %d", outIndex); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1168 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1169 | sp<IBinder> remote = client->getRemote(); |
| 1170 | if (remote != NULL) { |
| 1171 | remote->unlinkToDeath(this); |
| 1172 | } |
| 1173 | |
| 1174 | mClient[outIndex].clear(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1175 | } else { |
| 1176 | |
| 1177 | sp<ProClient> clientPro = findProClientUnsafe(remoteBinder); |
| 1178 | |
| 1179 | if (clientPro != NULL) { |
| 1180 | // Found our camera, clear and leave. |
| 1181 | LOG1("removeClient: clear pro %p", clientPro.get()); |
| 1182 | |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1183 | IInterface::asBinder(clientPro->getRemoteCallback())->unlinkToDeath(this); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1184 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1187 | LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid); |
| 1188 | } |
| 1189 | |
| 1190 | sp<CameraService::ProClient> CameraService::findProClientUnsafe( |
| 1191 | const wp<IBinder>& cameraCallbacksRemote) |
| 1192 | { |
| 1193 | sp<ProClient> clientPro; |
| 1194 | |
| 1195 | for (int i = 0; i < mNumberOfCameras; ++i) { |
| 1196 | Vector<size_t> removeIdx; |
| 1197 | |
| 1198 | for (size_t j = 0; j < mProClientList[i].size(); ++j) { |
| 1199 | wp<ProClient> cl = mProClientList[i][j]; |
| 1200 | |
| 1201 | sp<ProClient> clStrong = cl.promote(); |
| 1202 | if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) { |
| 1203 | clientPro = clStrong; |
| 1204 | break; |
| 1205 | } else if (clStrong == NULL) { |
| 1206 | // mark to clean up dead ptr |
| 1207 | removeIdx.push(j); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | // remove stale ptrs (in reverse so the indices dont change) |
| 1212 | for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) { |
| 1213 | mProClientList[i].removeAt(removeIdx[j]); |
| 1214 | } |
| 1215 | |
| 1216 | } |
| 1217 | |
| 1218 | return clientPro; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1221 | sp<CameraService::BasicClient> CameraService::findClientUnsafe( |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1222 | const wp<IBinder>& cameraClient, int& outIndex) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1223 | sp<BasicClient> client; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1224 | |
| 1225 | for (int i = 0; i < mNumberOfCameras; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1226 | |
| 1227 | // This happens when we have already disconnected (or this is |
| 1228 | // just another unused camera). |
| 1229 | if (mClient[i] == 0) continue; |
| 1230 | |
| 1231 | // Promote mClient. It can fail if we are called from this path: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1232 | // Client::~Client() -> disconnect() -> removeClientByRemote(). |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1233 | client = mClient[i].promote(); |
| 1234 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1235 | // Clean up stale client entry |
| 1236 | if (client == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1237 | mClient[i].clear(); |
| 1238 | continue; |
| 1239 | } |
| 1240 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1241 | if (cameraClient == client->getRemote()) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1242 | // Found our camera |
| 1243 | outIndex = i; |
| 1244 | return client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1248 | outIndex = -1; |
| 1249 | return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1252 | CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1253 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1254 | return mClient[cameraId].unsafe_get(); |
| 1255 | } |
| 1256 | |
| 1257 | Mutex* CameraService::getClientLockById(int cameraId) { |
| 1258 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
| 1259 | return &mClientLock[cameraId]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1262 | sp<CameraService::BasicClient> CameraService::getClientByRemote( |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1263 | const wp<IBinder>& cameraClient) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1264 | |
| 1265 | // Declare this before the lock to make absolutely sure the |
| 1266 | // destructor won't be called with the lock held. |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1267 | sp<BasicClient> client; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1268 | |
| 1269 | Mutex::Autolock lock(mServiceLock); |
| 1270 | |
| 1271 | int outIndex; |
| 1272 | client = findClientUnsafe(cameraClient, outIndex); |
| 1273 | |
| 1274 | return client; |
| 1275 | } |
| 1276 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1277 | status_t CameraService::onTransact( |
| 1278 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 1279 | // Permission checks |
| 1280 | switch (code) { |
| 1281 | case BnCameraService::CONNECT: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1282 | case BnCameraService::CONNECT_PRO: |
Eino-Ville Talvala | 63d877f | 2014-06-16 19:21:12 -0700 | [diff] [blame] | 1283 | case BnCameraService::CONNECT_DEVICE: |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1284 | case BnCameraService::CONNECT_LEGACY: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1285 | const int pid = getCallingPid(); |
| 1286 | const int self_pid = getpid(); |
| 1287 | if (pid != self_pid) { |
| 1288 | // we're called from a different process, do the real check |
| 1289 | if (!checkCallingPermission( |
| 1290 | String16("android.permission.CAMERA"))) { |
| 1291 | const int uid = getCallingUid(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1292 | ALOGE("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1293 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 1294 | return PERMISSION_DENIED; |
| 1295 | } |
| 1296 | } |
| 1297 | break; |
| 1298 | } |
| 1299 | |
| 1300 | return BnCameraService::onTransact(code, data, reply, flags); |
| 1301 | } |
| 1302 | |
| 1303 | // The reason we need this busy bit is a new CameraService::connect() request |
| 1304 | // may come in while the previous Client's destructor has not been run or is |
| 1305 | // still running. If the last strong reference of the previous Client is gone |
| 1306 | // but the destructor has not been finished, we should not allow the new Client |
| 1307 | // to be created because we need to wait for the previous Client to tear down |
| 1308 | // the hardware first. |
| 1309 | void CameraService::setCameraBusy(int cameraId) { |
| 1310 | android_atomic_write(1, &mBusy[cameraId]); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1311 | |
| 1312 | ALOGV("setCameraBusy cameraId=%d", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | void CameraService::setCameraFree(int cameraId) { |
| 1316 | android_atomic_write(0, &mBusy[cameraId]); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1317 | |
| 1318 | ALOGV("setCameraFree cameraId=%d", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | // We share the media players for shutter and recording sound for all clients. |
| 1322 | // A reference count is kept to determine when we will actually release the |
| 1323 | // media players. |
| 1324 | |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 1325 | MediaPlayer* CameraService::newMediaPlayer(const char *file) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1326 | MediaPlayer* mp = new MediaPlayer(); |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 1327 | if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 1328 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1329 | mp->prepare(); |
| 1330 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1331 | ALOGE("Failed to load CameraService sounds: %s", file); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1332 | return NULL; |
| 1333 | } |
| 1334 | return mp; |
| 1335 | } |
| 1336 | |
| 1337 | void CameraService::loadSound() { |
| 1338 | Mutex::Autolock lock(mSoundLock); |
| 1339 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 1340 | if (mSoundRef++) return; |
| 1341 | |
| 1342 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 1343 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 1344 | } |
| 1345 | |
| 1346 | void CameraService::releaseSound() { |
| 1347 | Mutex::Autolock lock(mSoundLock); |
| 1348 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 1349 | if (--mSoundRef) return; |
| 1350 | |
| 1351 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 1352 | if (mSoundPlayer[i] != 0) { |
| 1353 | mSoundPlayer[i]->disconnect(); |
| 1354 | mSoundPlayer[i].clear(); |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | void CameraService::playSound(sound_kind kind) { |
| 1360 | LOG1("playSound(%d)", kind); |
| 1361 | Mutex::Autolock lock(mSoundLock); |
| 1362 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 1363 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 1364 | player->seekTo(0); |
| 1365 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | // ---------------------------------------------------------------------------- |
| 1370 | |
| 1371 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 1372 | const sp<ICameraClient>& cameraClient, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1373 | const String16& clientPackageName, |
| 1374 | int cameraId, int cameraFacing, |
| 1375 | int clientPid, uid_t clientUid, |
| 1376 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 1377 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1378 | IInterface::asBinder(cameraClient), |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1379 | clientPackageName, |
| 1380 | cameraId, cameraFacing, |
| 1381 | clientPid, clientUid, |
| 1382 | servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1383 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1384 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1385 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1386 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1387 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1388 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1389 | cameraService->setCameraBusy(cameraId); |
| 1390 | cameraService->loadSound(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1391 | |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1392 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1395 | // tear down the client |
| 1396 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1397 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1398 | mDestructionStarted = true; |
| 1399 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 1400 | mCameraService->releaseSound(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1401 | // unconditionally disconnect. function is idempotent |
| 1402 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1405 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1406 | const sp<IBinder>& remoteCallback, |
| 1407 | const String16& clientPackageName, |
| 1408 | int cameraId, int cameraFacing, |
| 1409 | int clientPid, uid_t clientUid, |
| 1410 | int servicePid): |
| 1411 | mClientPackageName(clientPackageName) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1412 | { |
| 1413 | mCameraService = cameraService; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1414 | mRemoteBinder = remoteCallback; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1415 | mCameraId = cameraId; |
| 1416 | mCameraFacing = cameraFacing; |
| 1417 | mClientPid = clientPid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1418 | mClientUid = clientUid; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1419 | mServicePid = servicePid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1420 | mOpsActive = false; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1421 | mDestructionStarted = false; |
| 1422 | } |
| 1423 | |
| 1424 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1425 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1426 | mDestructionStarted = true; |
| 1427 | } |
| 1428 | |
| 1429 | void CameraService::BasicClient::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1430 | ALOGV("BasicClient::disconnect"); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1431 | mCameraService->removeClientByRemote(mRemoteBinder); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1432 | |
| 1433 | finishCameraOps(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1434 | // client shouldn't be able to call into us anymore |
| 1435 | mClientPid = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1436 | } |
| 1437 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1438 | status_t CameraService::BasicClient::startCameraOps() { |
| 1439 | int32_t res; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1440 | // Notify app ops that the camera is not available |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1441 | mOpsCallback = new OpsCallback(this); |
| 1442 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1443 | { |
| 1444 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
| 1445 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 1446 | } |
| 1447 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1448 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, |
| 1449 | mClientPackageName, mOpsCallback); |
| 1450 | res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, |
| 1451 | mClientUid, mClientPackageName); |
| 1452 | |
| 1453 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 1454 | ALOGI("Camera %d: Access for \"%s\" has been revoked", |
| 1455 | mCameraId, String8(mClientPackageName).string()); |
| 1456 | return PERMISSION_DENIED; |
| 1457 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1458 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1459 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1460 | |
| 1461 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
| 1462 | mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
| 1463 | mCameraId); |
| 1464 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1465 | return OK; |
| 1466 | } |
| 1467 | |
| 1468 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1469 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1470 | if (mOpsActive) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1471 | // Notify app ops that the camera is available again |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1472 | mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 1473 | mClientPackageName); |
| 1474 | mOpsActive = false; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1475 | |
| 1476 | // Notify device availability listeners that this camera is available |
| 1477 | // again |
| 1478 | |
| 1479 | StatusVector rejectSourceStates; |
| 1480 | rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT); |
| 1481 | rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING); |
| 1482 | |
| 1483 | // Transition to PRESENT if the camera is not in either of above 2 |
| 1484 | // states |
| 1485 | mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT, |
| 1486 | mCameraId, |
| 1487 | &rejectSourceStates); |
| 1488 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1489 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1490 | // Always stop watching, even if no camera op is active |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 1491 | if (mOpsCallback != NULL) { |
| 1492 | mAppOpsManager.stopWatchingMode(mOpsCallback); |
| 1493 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1494 | mOpsCallback.clear(); |
| 1495 | |
| 1496 | return OK; |
| 1497 | } |
| 1498 | |
| 1499 | void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) { |
| 1500 | String8 name(packageName); |
| 1501 | String8 myName(mClientPackageName); |
| 1502 | |
| 1503 | if (op != AppOpsManager::OP_CAMERA) { |
| 1504 | ALOGW("Unexpected app ops notification received: %d", op); |
| 1505 | return; |
| 1506 | } |
| 1507 | |
| 1508 | int32_t res; |
| 1509 | res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, |
| 1510 | mClientUid, mClientPackageName); |
| 1511 | ALOGV("checkOp returns: %d, %s ", res, |
| 1512 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 1513 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 1514 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 1515 | "UNKNOWN"); |
| 1516 | |
| 1517 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 1518 | ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId, |
| 1519 | myName.string()); |
| 1520 | // Reset the client PID to allow server-initiated disconnect, |
| 1521 | // and to prevent further calls by client. |
| 1522 | mClientPid = getCallingPid(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1523 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 1524 | notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1525 | disconnect(); |
| 1526 | } |
| 1527 | } |
| 1528 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1529 | // ---------------------------------------------------------------------------- |
| 1530 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1531 | Mutex* CameraService::Client::getClientLockFromCookie(void* user) { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1532 | return gCameraService->getClientLockById((int)(intptr_t) user); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | // Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should |
| 1536 | // be acquired for this to be safe |
| 1537 | CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1538 | BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1539 | // OK: only CameraClient calls this, and they already cast anyway. |
| 1540 | Client* client = static_cast<Client*>(basicClient); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1541 | |
| 1542 | // This could happen if the Client is in the process of shutting down (the |
| 1543 | // last strong reference is gone, but the destructor hasn't finished |
| 1544 | // stopping the hardware). |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1545 | if (client == NULL) return NULL; |
| 1546 | |
| 1547 | // destruction already started, so should not be accessed |
| 1548 | if (client->mDestructionStarted) return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1549 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1550 | return client; |
| 1551 | } |
| 1552 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1553 | void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 1554 | const CaptureResultExtras& resultExtras) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1555 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1558 | // NOTE: function is idempotent |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1559 | void CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1560 | ALOGV("Client::disconnect"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1561 | BasicClient::disconnect(); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1562 | mCameraService->setCameraFree(mCameraId); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 1563 | } |
| 1564 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1565 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 1566 | mClient(client) { |
| 1567 | } |
| 1568 | |
| 1569 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 1570 | const String16& packageName) { |
| 1571 | sp<BasicClient> client = mClient.promote(); |
| 1572 | if (client != NULL) { |
| 1573 | client->opChanged(op, packageName); |
| 1574 | } |
| 1575 | } |
| 1576 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1577 | // ---------------------------------------------------------------------------- |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1578 | // IProCamera |
| 1579 | // ---------------------------------------------------------------------------- |
| 1580 | |
| 1581 | CameraService::ProClient::ProClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1582 | const sp<IProCameraCallbacks>& remoteCallback, |
| 1583 | const String16& clientPackageName, |
| 1584 | int cameraId, |
| 1585 | int cameraFacing, |
| 1586 | int clientPid, |
| 1587 | uid_t clientUid, |
| 1588 | int servicePid) |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1589 | : CameraService::BasicClient(cameraService, IInterface::asBinder(remoteCallback), |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1590 | clientPackageName, cameraId, cameraFacing, |
| 1591 | clientPid, clientUid, servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1592 | { |
| 1593 | mRemoteCallback = remoteCallback; |
| 1594 | } |
| 1595 | |
| 1596 | CameraService::ProClient::~ProClient() { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1597 | } |
| 1598 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1599 | void CameraService::ProClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 1600 | const CaptureResultExtras& resultExtras) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1601 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1604 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1605 | |
| 1606 | static const int kDumpLockRetries = 50; |
| 1607 | static const int kDumpLockSleep = 60000; |
| 1608 | |
| 1609 | static bool tryLock(Mutex& mutex) |
| 1610 | { |
| 1611 | bool locked = false; |
| 1612 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1613 | if (mutex.tryLock() == NO_ERROR) { |
| 1614 | locked = true; |
| 1615 | break; |
| 1616 | } |
| 1617 | usleep(kDumpLockSleep); |
| 1618 | } |
| 1619 | return locked; |
| 1620 | } |
| 1621 | |
| 1622 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1623 | String8 result; |
| 1624 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1625 | result.appendFormat("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1626 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 1627 | getCallingPid(), |
| 1628 | getCallingUid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1629 | write(fd, result.string(), result.size()); |
| 1630 | } else { |
| 1631 | bool locked = tryLock(mServiceLock); |
| 1632 | // failed to lock - CameraService is probably deadlocked |
| 1633 | if (!locked) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1634 | result.append("CameraService may be deadlocked\n"); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1635 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | bool hasClient = false; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1639 | if (!mModule) { |
| 1640 | result = String8::format("No camera module available!\n"); |
| 1641 | write(fd, result.string(), result.size()); |
Kalle Lampila | 6ec3a15 | 2013-04-30 15:27:19 +0300 | [diff] [blame] | 1642 | if (locked) mServiceLock.unlock(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1643 | return NO_ERROR; |
| 1644 | } |
| 1645 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1646 | const hw_module_t* common = mModule->getRawModule(); |
| 1647 | result = String8::format("Camera module HAL API version: 0x%x\n", common->hal_api_version); |
| 1648 | result.appendFormat("Camera module API version: 0x%x\n", common->module_api_version); |
| 1649 | result.appendFormat("Camera module name: %s\n", common->name); |
| 1650 | result.appendFormat("Camera module author: %s\n", common->author); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1651 | result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1652 | |
| 1653 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1654 | if (desc == NULL) { |
| 1655 | result.appendFormat("Vendor tags left unimplemented.\n"); |
| 1656 | } else { |
| 1657 | result.appendFormat("Vendor tag definitions:\n"); |
| 1658 | } |
| 1659 | |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1660 | write(fd, result.string(), result.size()); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1661 | |
| 1662 | if (desc != NULL) { |
| 1663 | desc->dump(fd, /*verbosity*/2, /*indentation*/4); |
| 1664 | } |
| 1665 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1666 | for (int i = 0; i < mNumberOfCameras; i++) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1667 | result = String8::format("Camera %d static information:\n", i); |
| 1668 | camera_info info; |
| 1669 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1670 | status_t rc = mModule->getCameraInfo(i, &info); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1671 | if (rc != OK) { |
| 1672 | result.appendFormat(" Error reading static information!\n"); |
| 1673 | write(fd, result.string(), result.size()); |
| 1674 | } else { |
| 1675 | result.appendFormat(" Facing: %s\n", |
| 1676 | info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); |
| 1677 | result.appendFormat(" Orientation: %d\n", info.orientation); |
| 1678 | int deviceVersion; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1679 | if (common->module_api_version < CAMERA_MODULE_API_VERSION_2_0) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1680 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 1681 | } else { |
| 1682 | deviceVersion = info.device_version; |
| 1683 | } |
| 1684 | result.appendFormat(" Device version: 0x%x\n", deviceVersion); |
| 1685 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { |
| 1686 | result.appendFormat(" Device static metadata:\n"); |
| 1687 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 428b77a | 2012-07-30 09:55:30 -0700 | [diff] [blame] | 1688 | dump_indented_camera_metadata(info.static_camera_characteristics, |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1689 | fd, /*verbosity*/2, /*indentation*/4); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1690 | } else { |
| 1691 | write(fd, result.string(), result.size()); |
| 1692 | } |
| 1693 | } |
| 1694 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1695 | sp<BasicClient> client = mClient[i].promote(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1696 | if (client == 0) { |
| 1697 | result = String8::format(" Device is closed, no client instance\n"); |
| 1698 | write(fd, result.string(), result.size()); |
| 1699 | continue; |
| 1700 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1701 | hasClient = true; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1702 | result = String8::format(" Device is open. Client instance dump:\n"); |
| 1703 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1704 | client->dump(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1705 | } |
| 1706 | if (!hasClient) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1707 | result = String8::format("\nNo active camera clients yet.\n"); |
| 1708 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | if (locked) mServiceLock.unlock(); |
| 1712 | |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 1713 | // Dump camera traces if there were any |
| 1714 | write(fd, "\n", 1); |
| 1715 | camera3::CameraTraces::dump(fd, args); |
| 1716 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1717 | // change logging level |
| 1718 | int n = args.size(); |
| 1719 | for (int i = 0; i + 1 < n; i++) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1720 | String16 verboseOption("-v"); |
| 1721 | if (args[i] == verboseOption) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1722 | String8 levelStr(args[i+1]); |
| 1723 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1724 | result = String8::format("\nSetting log level to %d.\n", level); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1725 | setLogLevel(level); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1726 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1727 | } |
| 1728 | } |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1729 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1730 | } |
| 1731 | return NO_ERROR; |
| 1732 | } |
| 1733 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1734 | /*virtual*/void CameraService::binderDied( |
| 1735 | const wp<IBinder> &who) { |
| 1736 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1737 | /** |
| 1738 | * While tempting to promote the wp<IBinder> into a sp, |
| 1739 | * it's actually not supported by the binder driver |
| 1740 | */ |
| 1741 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1742 | ALOGV("java clients' binder died"); |
| 1743 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1744 | sp<BasicClient> cameraClient = getClientByRemote(who); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1745 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1746 | if (cameraClient == 0) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1747 | ALOGV("java clients' binder death already cleaned up (normal case)"); |
| 1748 | return; |
| 1749 | } |
| 1750 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1751 | ALOGW("Disconnecting camera client %p since the binder for it " |
| 1752 | "died (this pid %d)", cameraClient.get(), getCallingPid()); |
| 1753 | |
| 1754 | cameraClient->disconnect(); |
| 1755 | |
| 1756 | } |
| 1757 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1758 | void CameraService::updateStatus(ICameraServiceListener::Status status, |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1759 | int32_t cameraId, |
| 1760 | const StatusVector *rejectSourceStates) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1761 | // do not lock mServiceLock here or can get into a deadlock from |
| 1762 | // connect() -> ProClient::disconnect -> updateStatus |
| 1763 | Mutex::Autolock lock(mStatusMutex); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1764 | |
| 1765 | ICameraServiceListener::Status oldStatus = mStatusList[cameraId]; |
| 1766 | |
| 1767 | mStatusList[cameraId] = status; |
| 1768 | |
| 1769 | if (oldStatus != status) { |
| 1770 | ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x", |
| 1771 | __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status); |
| 1772 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1773 | if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT && |
| 1774 | (status != ICameraServiceListener::STATUS_PRESENT && |
| 1775 | status != ICameraServiceListener::STATUS_ENUMERATING)) { |
| 1776 | |
| 1777 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT" |
| 1778 | " or ENUMERATING", __FUNCTION__); |
| 1779 | mStatusList[cameraId] = oldStatus; |
| 1780 | return; |
| 1781 | } |
| 1782 | |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1783 | if (rejectSourceStates != NULL) { |
| 1784 | const StatusVector &rejectList = *rejectSourceStates; |
| 1785 | StatusVector::const_iterator it = rejectList.begin(); |
| 1786 | |
| 1787 | /** |
| 1788 | * Sometimes we want to conditionally do a transition. |
| 1789 | * For example if a client disconnects, we want to go to PRESENT |
| 1790 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 1791 | */ |
| 1792 | for (; it != rejectList.end(); ++it) { |
| 1793 | if (oldStatus == *it) { |
| 1794 | ALOGV("%s: Rejecting status transition for Camera ID %d, " |
| 1795 | " since the source state was was in one of the bad " |
| 1796 | " states.", __FUNCTION__, cameraId); |
| 1797 | mStatusList[cameraId] = oldStatus; |
| 1798 | return; |
| 1799 | } |
| 1800 | } |
| 1801 | } |
| 1802 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1803 | /** |
| 1804 | * ProClients lose their exclusive lock. |
| 1805 | * - Done before the CameraClient can initialize the HAL device, |
| 1806 | * since we want to be able to close it before they get to initialize |
| 1807 | */ |
| 1808 | if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
| 1809 | Vector<wp<ProClient> > proClients(mProClientList[cameraId]); |
| 1810 | Vector<wp<ProClient> >::const_iterator it; |
| 1811 | |
| 1812 | for (it = proClients.begin(); it != proClients.end(); ++it) { |
| 1813 | sp<ProClient> proCl = it->promote(); |
| 1814 | if (proCl.get() != NULL) { |
| 1815 | proCl->onExclusiveLockStolen(); |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | Vector<sp<ICameraServiceListener> >::const_iterator it; |
| 1821 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
| 1822 | (*it)->onStatusChanged(status, cameraId); |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1827 | ICameraServiceListener::Status CameraService::getStatus(int cameraId) const { |
| 1828 | if (cameraId < 0 || cameraId >= MAX_CAMERAS) { |
| 1829 | ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId); |
| 1830 | return ICameraServiceListener::STATUS_UNKNOWN; |
| 1831 | } |
| 1832 | |
| 1833 | Mutex::Autolock al(mStatusMutex); |
| 1834 | return mStatusList[cameraId]; |
| 1835 | } |
| 1836 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1837 | }; // namespace android |