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> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 32 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | #include <hardware/hardware.h> |
| 34 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 35 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 36 | #include <media/mediaplayer.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <utils/Errors.h> |
| 38 | #include <utils/Log.h> |
| 39 | #include <utils/String16.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 40 | #include <utils/Trace.h> |
| 41 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 42 | #include <system/camera_metadata.h> |
| 43 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | |
| 45 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 46 | #include "api1/CameraClient.h" |
| 47 | #include "api1/Camera2Client.h" |
| 48 | #include "api_pro/ProCamera2Client.h" |
| 49 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 50 | #include "utils/CameraTraces.h" |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 51 | #include "CameraDeviceFactory.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
| 53 | namespace android { |
| 54 | |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | // Logging support -- this is for debugging only |
| 57 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 58 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 59 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 60 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 61 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | |
| 63 | static void setLogLevel(int level) { |
| 64 | android_atomic_write(level, &gLogLevel); |
| 65 | } |
| 66 | |
| 67 | // ---------------------------------------------------------------------------- |
| 68 | |
| 69 | static int getCallingPid() { |
| 70 | return IPCThreadState::self()->getCallingPid(); |
| 71 | } |
| 72 | |
| 73 | static int getCallingUid() { |
| 74 | return IPCThreadState::self()->getCallingUid(); |
| 75 | } |
| 76 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 77 | extern "C" { |
| 78 | static void camera_device_status_change( |
| 79 | const struct camera_module_callbacks* callbacks, |
| 80 | int camera_id, |
| 81 | int new_status) { |
| 82 | sp<CameraService> cs = const_cast<CameraService*>( |
| 83 | static_cast<const CameraService*>(callbacks)); |
| 84 | |
| 85 | cs->onDeviceStatusChanged( |
| 86 | camera_id, |
| 87 | new_status); |
| 88 | } |
| 89 | } // extern "C" |
| 90 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 91 | // ---------------------------------------------------------------------------- |
| 92 | |
| 93 | // This is ugly and only safe if we never re-create the CameraService, but |
| 94 | // should be ok for now. |
| 95 | static CameraService *gCameraService; |
| 96 | |
| 97 | CameraService::CameraService() |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 98 | :mSoundRef(0), mModule(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 99 | { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 100 | ALOGI("CameraService started (pid=%d)", getpid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 101 | gCameraService = this; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 102 | |
| 103 | for (size_t i = 0; i < MAX_CAMERAS; ++i) { |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 104 | mStatusList[i] = ICameraServiceListener::STATUS_PRESENT; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 105 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 106 | |
| 107 | this->camera_device_status_change = android::camera_device_status_change; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 110 | void CameraService::onFirstRef() |
| 111 | { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 112 | LOG1("CameraService::onFirstRef"); |
| 113 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 114 | BnCameraService::onFirstRef(); |
| 115 | |
| 116 | if (hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
| 117 | (const hw_module_t **)&mModule) < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 118 | ALOGE("Could not load camera HAL module"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 119 | mNumberOfCameras = 0; |
| 120 | } |
| 121 | else { |
Alex Ray | c0dd54f | 2013-02-20 13:39:37 -0800 | [diff] [blame] | 122 | ALOGI("Loaded \"%s\" camera module", mModule->common.name); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 123 | mNumberOfCameras = mModule->get_number_of_cameras(); |
| 124 | if (mNumberOfCameras > MAX_CAMERAS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 125 | ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).", |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 126 | mNumberOfCameras, MAX_CAMERAS); |
| 127 | mNumberOfCameras = MAX_CAMERAS; |
| 128 | } |
| 129 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 130 | setCameraFree(i); |
| 131 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 132 | |
| 133 | if (mModule->common.module_api_version >= |
| 134 | CAMERA_MODULE_API_VERSION_2_1) { |
| 135 | mModule->set_callbacks(this); |
| 136 | } |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 137 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 138 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 139 | |
| 140 | if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_2) { |
| 141 | setUpVendorTags(); |
| 142 | } |
| 143 | |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 144 | CameraDeviceFactory::registerService(this); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 148 | CameraService::~CameraService() { |
| 149 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 150 | if (mBusy[i]) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 151 | ALOGE("camera %d is still in use in destructor!", i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 155 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 156 | gCameraService = NULL; |
| 157 | } |
| 158 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 159 | void CameraService::onDeviceStatusChanged(int cameraId, |
| 160 | int newStatus) |
| 161 | { |
| 162 | ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__, |
| 163 | cameraId, newStatus); |
| 164 | |
| 165 | if (cameraId < 0 || cameraId >= MAX_CAMERAS) { |
| 166 | ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | if ((int)getStatus(cameraId) == newStatus) { |
| 171 | ALOGE("%s: State transition to the same status 0x%x not allowed", |
| 172 | __FUNCTION__, (uint32_t)newStatus); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | /* don't do this in updateStatus |
| 177 | since it is also called from connect and we could get into a deadlock */ |
| 178 | if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) { |
| 179 | Vector<sp<BasicClient> > clientsToDisconnect; |
| 180 | { |
| 181 | Mutex::Autolock al(mServiceLock); |
| 182 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 183 | /* Remove cached parameters from shim cache */ |
| 184 | mShimParams.removeItem(cameraId); |
| 185 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 186 | /* Find all clients that we need to disconnect */ |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 187 | sp<BasicClient> client = mClient[cameraId].promote(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 188 | if (client.get() != NULL) { |
| 189 | clientsToDisconnect.push_back(client); |
| 190 | } |
| 191 | |
| 192 | int i = cameraId; |
| 193 | for (size_t j = 0; j < mProClientList[i].size(); ++j) { |
| 194 | sp<ProClient> cl = mProClientList[i][j].promote(); |
| 195 | if (cl != NULL) { |
| 196 | clientsToDisconnect.push_back(cl); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* now disconnect them. don't hold the lock |
| 202 | or we can get into a deadlock */ |
| 203 | |
| 204 | for (size_t i = 0; i < clientsToDisconnect.size(); ++i) { |
| 205 | sp<BasicClient> client = clientsToDisconnect[i]; |
| 206 | |
| 207 | client->disconnect(); |
| 208 | /** |
| 209 | * The remote app will no longer be able to call methods on the |
| 210 | * client since the client PID will be reset to 0 |
| 211 | */ |
| 212 | } |
| 213 | |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 214 | ALOGV("%s: After unplug, disconnected %zu clients", |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 215 | __FUNCTION__, clientsToDisconnect.size()); |
| 216 | } |
| 217 | |
| 218 | updateStatus( |
| 219 | static_cast<ICameraServiceListener::Status>(newStatus), cameraId); |
| 220 | |
| 221 | } |
| 222 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 223 | int32_t CameraService::getNumberOfCameras() { |
| 224 | return mNumberOfCameras; |
| 225 | } |
| 226 | |
| 227 | status_t CameraService::getCameraInfo(int cameraId, |
| 228 | struct CameraInfo* cameraInfo) { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 229 | if (!mModule) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 230 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 233 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 234 | return BAD_VALUE; |
| 235 | } |
| 236 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 237 | struct camera_info info; |
| 238 | status_t rc = mModule->get_camera_info(cameraId, &info); |
| 239 | cameraInfo->facing = info.facing; |
| 240 | cameraInfo->orientation = info.orientation; |
| 241 | return rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 244 | |
| 245 | status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) { |
| 246 | status_t ret = OK; |
| 247 | struct CameraInfo info; |
| 248 | if ((ret = getCameraInfo(cameraId, &info)) != OK) { |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | CameraMetadata shimInfo; |
| 253 | int32_t orientation = static_cast<int32_t>(info.orientation); |
| 254 | if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) { |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ? |
| 259 | ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK; |
| 260 | if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) { |
| 261 | return ret; |
| 262 | } |
| 263 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 264 | CameraParameters shimParams; |
| 265 | if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { |
| 266 | // Error logged by callee |
| 267 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | Vector<Size> sizes; |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 271 | Vector<Size> jpegSizes; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 272 | Vector<int32_t> formats; |
| 273 | const char* supportedPreviewFormats; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 274 | { |
| 275 | shimParams.getSupportedPreviewSizes(/*out*/sizes); |
| 276 | shimParams.getSupportedPreviewFormats(/*out*/formats); |
| 277 | shimParams.getSupportedPictureSizes(/*out*/jpegSizes); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Always include IMPLEMENTATION_DEFINED |
| 281 | formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED); |
| 282 | |
| 283 | const size_t INTS_PER_CONFIG = 4; |
| 284 | |
| 285 | // Build available stream configurations metadata |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 286 | size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG; |
| 287 | |
| 288 | Vector<int32_t> streamConfigs; |
| 289 | streamConfigs.setCapacity(streamConfigSize); |
| 290 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 291 | for (size_t i = 0; i < formats.size(); ++i) { |
| 292 | for (size_t j = 0; j < sizes.size(); ++j) { |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 293 | streamConfigs.add(formats[i]); |
| 294 | streamConfigs.add(sizes[j].width); |
| 295 | streamConfigs.add(sizes[j].height); |
| 296 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 300 | for (size_t i = 0; i < jpegSizes.size(); ++i) { |
| 301 | streamConfigs.add(HAL_PIXEL_FORMAT_BLOB); |
| 302 | streamConfigs.add(jpegSizes[i].width); |
| 303 | streamConfigs.add(jpegSizes[i].height); |
| 304 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
| 305 | } |
| 306 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 307 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 308 | streamConfigs.array(), streamConfigSize)) != OK) { |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | int64_t fakeMinFrames[0]; |
| 313 | // TODO: Fixme, don't fake min frame durations. |
| 314 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 315 | fakeMinFrames, 0)) != OK) { |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | int64_t fakeStalls[0]; |
| 320 | // TODO: Fixme, don't fake stall durations. |
| 321 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 322 | fakeStalls, 0)) != OK) { |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | *cameraInfo = shimInfo; |
| 327 | return OK; |
| 328 | } |
| 329 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 330 | status_t CameraService::getCameraCharacteristics(int cameraId, |
| 331 | CameraMetadata* cameraInfo) { |
| 332 | if (!cameraInfo) { |
| 333 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
| 334 | return BAD_VALUE; |
| 335 | } |
| 336 | |
| 337 | if (!mModule) { |
| 338 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 339 | return -ENODEV; |
| 340 | } |
| 341 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 342 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 343 | ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId); |
| 344 | return BAD_VALUE; |
| 345 | } |
| 346 | |
| 347 | int facing; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 348 | status_t ret = OK; |
| 349 | if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_0 || |
| 350 | getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) { |
| 351 | /** |
| 352 | * Backwards compatibility mode for old HALs: |
| 353 | * - Convert CameraInfo into static CameraMetadata properties. |
| 354 | * - Retrieve cached CameraParameters for this camera. If none exist, |
| 355 | * attempt to open CameraClient and retrieve the CameraParameters. |
| 356 | * - Convert cached CameraParameters into static CameraMetadata |
| 357 | * properties. |
| 358 | */ |
| 359 | ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 360 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 361 | if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) { |
| 362 | return ret; |
| 363 | } |
Zhijun He | f05e50e | 2013-10-01 11:05:33 -0700 | [diff] [blame] | 364 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 365 | } else { |
| 366 | /** |
| 367 | * Normal HAL 2.1+ codepath. |
| 368 | */ |
| 369 | struct camera_info info; |
| 370 | ret = mModule->get_camera_info(cameraId, &info); |
| 371 | *cameraInfo = info.static_camera_characteristics; |
| 372 | } |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 377 | status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) { |
| 378 | if (!mModule) { |
| 379 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 380 | return -ENODEV; |
| 381 | } |
| 382 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 383 | desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 384 | return OK; |
| 385 | } |
| 386 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 387 | int CameraService::getDeviceVersion(int cameraId, int* facing) { |
| 388 | struct camera_info info; |
| 389 | if (mModule->get_camera_info(cameraId, &info) != OK) { |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | int deviceVersion; |
| 394 | if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) { |
| 395 | deviceVersion = info.device_version; |
| 396 | } else { |
| 397 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 398 | } |
| 399 | |
| 400 | if (facing) { |
| 401 | *facing = info.facing; |
| 402 | } |
| 403 | |
| 404 | return deviceVersion; |
| 405 | } |
| 406 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 407 | bool CameraService::isValidCameraId(int cameraId) { |
| 408 | int facing; |
| 409 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 410 | |
| 411 | switch(deviceVersion) { |
| 412 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 413 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 414 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 415 | case CAMERA_DEVICE_API_VERSION_3_0: |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 416 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 417 | case CAMERA_DEVICE_API_VERSION_3_2: |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 418 | return true; |
| 419 | default: |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | return false; |
| 424 | } |
| 425 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 426 | bool CameraService::setUpVendorTags() { |
| 427 | vendor_tag_ops_t vOps = vendor_tag_ops_t(); |
| 428 | |
| 429 | // Check if vendor operations have been implemented |
| 430 | if (mModule->get_vendor_tag_ops == NULL) { |
| 431 | ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__); |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops"); |
| 436 | mModule->get_vendor_tag_ops(&vOps); |
| 437 | ATRACE_END(); |
| 438 | |
| 439 | // Ensure all vendor operations are present |
| 440 | if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL || |
| 441 | vOps.get_section_name == NULL || vOps.get_tag_name == NULL || |
| 442 | vOps.get_tag_type == NULL) { |
| 443 | ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions." |
| 444 | , __FUNCTION__); |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | // Read all vendor tag definitions into a descriptor |
| 449 | sp<VendorTagDescriptor> desc; |
| 450 | status_t res; |
| 451 | if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc)) |
| 452 | != OK) { |
| 453 | ALOGE("%s: Could not generate descriptor from vendor tag operations," |
| 454 | "received error %s (%d). Camera clients will not be able to use" |
| 455 | "vendor tags", __FUNCTION__, strerror(res), res); |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | // Set the global descriptor to use with camera metadata |
| 460 | VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 461 | return true; |
| 462 | } |
| 463 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 464 | status_t CameraService::initializeShimMetadata(int cameraId) { |
| 465 | int pid = getCallingPid(); |
| 466 | int uid = getCallingUid(); |
| 467 | status_t ret = validateConnect(cameraId, uid); |
| 468 | if (ret != OK) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 469 | // Error already logged by callee |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | bool needsNewClient = false; |
| 474 | sp<Client> client; |
| 475 | |
| 476 | String16 internalPackageName("media"); |
| 477 | { // Scope for service lock |
| 478 | Mutex::Autolock lock(mServiceLock); |
| 479 | if (mClient[cameraId] != NULL) { |
| 480 | client = static_cast<Client*>(mClient[cameraId].promote().get()); |
| 481 | } |
| 482 | if (client == NULL) { |
| 483 | needsNewClient = true; |
| 484 | ret = connectHelperLocked(/*cameraClient*/NULL, // Empty binder callbacks |
| 485 | cameraId, |
| 486 | internalPackageName, |
| 487 | uid, |
| 488 | pid, |
| 489 | client); |
| 490 | |
| 491 | if (ret != OK) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 492 | // Error already logged by callee |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 493 | return ret; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (client == NULL) { |
| 498 | ALOGE("%s: Could not connect to client camera device.", __FUNCTION__); |
| 499 | return BAD_VALUE; |
| 500 | } |
| 501 | |
| 502 | String8 rawParams = client->getParameters(); |
| 503 | CameraParameters params(rawParams); |
| 504 | mShimParams.add(cameraId, params); |
| 505 | } |
| 506 | |
| 507 | // Close client if one was opened solely for this call |
| 508 | if (needsNewClient) { |
| 509 | client->disconnect(); |
| 510 | } |
| 511 | return OK; |
| 512 | } |
| 513 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 514 | status_t CameraService::getLegacyParametersLazy(int cameraId, |
| 515 | /*out*/ |
| 516 | CameraParameters* parameters) { |
| 517 | |
| 518 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 519 | |
| 520 | status_t ret = 0; |
| 521 | |
| 522 | if (parameters == NULL) { |
| 523 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
| 524 | return BAD_VALUE; |
| 525 | } |
| 526 | |
| 527 | ssize_t index = -1; |
| 528 | { // Scope for service lock |
| 529 | Mutex::Autolock lock(mServiceLock); |
| 530 | index = mShimParams.indexOfKey(cameraId); |
| 531 | // Release service lock so initializeShimMetadata can be called correctly. |
| 532 | |
| 533 | if (index >= 0) { |
| 534 | *parameters = mShimParams[index]; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | if (index < 0) { |
| 539 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 540 | ret = initializeShimMetadata(cameraId); |
| 541 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 542 | if (ret != OK) { |
| 543 | // Error already logged by callee |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | { // Scope for service lock |
| 548 | Mutex::Autolock lock(mServiceLock); |
| 549 | index = mShimParams.indexOfKey(cameraId); |
| 550 | |
| 551 | LOG_ALWAYS_FATAL_IF(index < 0, "index should have been initialized"); |
| 552 | |
| 553 | *parameters = mShimParams[index]; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | return OK; |
| 558 | } |
| 559 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 560 | status_t CameraService::validateConnect(int cameraId, |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 561 | /*inout*/ |
| 562 | int& clientUid) const { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 563 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 564 | int callingPid = getCallingPid(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 565 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 566 | if (clientUid == USE_CALLING_UID) { |
| 567 | clientUid = getCallingUid(); |
| 568 | } else { |
| 569 | // We only trust our own process to forward client UIDs |
| 570 | if (callingPid != getpid()) { |
| 571 | ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)", |
| 572 | callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 573 | return PERMISSION_DENIED; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 574 | } |
| 575 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 576 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 577 | if (!mModule) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 578 | ALOGE("Camera HAL module not loaded"); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 579 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 582 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 583 | ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 584 | callingPid, cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 585 | return -ENODEV; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 588 | char value[PROPERTY_VALUE_MAX]; |
| 589 | property_get("sys.secpolicy.camera.disabled", value, "0"); |
| 590 | if (strcmp(value, "1") == 0) { |
| 591 | // Camera is disabled by DevicePolicyManager. |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 592 | ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 593 | return -EACCES; |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 594 | } |
| 595 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 596 | ICameraServiceListener::Status currentStatus = getStatus(cameraId); |
| 597 | if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) { |
| 598 | ALOGI("Camera is not plugged in," |
| 599 | " connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 600 | return -ENODEV; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 601 | } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) { |
| 602 | ALOGI("Camera is enumerating," |
| 603 | " connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 604 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 605 | } |
| 606 | // Else don't check for STATUS_NOT_AVAILABLE. |
| 607 | // -- It's done implicitly in canConnectUnsafe /w the mBusy array |
| 608 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 609 | return OK; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | bool CameraService::canConnectUnsafe(int cameraId, |
| 613 | const String16& clientPackageName, |
| 614 | const sp<IBinder>& remoteCallback, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 615 | sp<BasicClient> &client) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 616 | String8 clientName8(clientPackageName); |
| 617 | int callingPid = getCallingPid(); |
| 618 | |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 619 | if (mClient[cameraId] != 0) { |
| 620 | client = mClient[cameraId].promote(); |
| 621 | if (client != 0) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 622 | if (remoteCallback == client->getRemote()) { |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 623 | LOG1("CameraService::connect X (pid %d) (the same client)", |
| 624 | callingPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 625 | return true; |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 626 | } else { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 627 | // TODOSC: need to support 1 regular client, |
| 628 | // multiple shared clients here |
| 629 | ALOGW("CameraService::connect X (pid %d) rejected" |
| 630 | " (existing client).", callingPid); |
| 631 | return false; |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 632 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 633 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 634 | mClient[cameraId].clear(); |
| 635 | } |
| 636 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 637 | /* |
| 638 | mBusy is set to false as the last step of the Client destructor, |
| 639 | after which it is guaranteed that the Client destructor has finished ( |
| 640 | including any inherited destructors) |
| 641 | |
| 642 | We only need this for a Client subclasses since we don't allow |
| 643 | multiple Clents to be opened concurrently, but multiple BasicClient |
| 644 | would be fine |
| 645 | */ |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 646 | if (mBusy[cameraId]) { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 647 | ALOGW("CameraService::connect X (pid %d, \"%s\") rejected" |
| 648 | " (camera %d is still busy).", callingPid, |
| 649 | clientName8.string(), cameraId); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | |
| 653 | return true; |
| 654 | } |
| 655 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 656 | status_t CameraService::connectHelperLocked(const sp<ICameraClient>& cameraClient, |
| 657 | int cameraId, |
| 658 | const String16& clientPackageName, |
| 659 | int clientUid, |
| 660 | int callingPid, |
| 661 | /*out*/ |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 662 | sp<Client>& client, |
| 663 | int halVersion) { |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 664 | |
| 665 | int facing = -1; |
| 666 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 667 | |
| 668 | // If there are other non-exclusive users of the camera, |
| 669 | // this will tear them down before we can reuse the camera |
| 670 | if (isValidCameraId(cameraId)) { |
| 671 | // transition from PRESENT -> NOT_AVAILABLE |
| 672 | updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
| 673 | cameraId); |
| 674 | } |
| 675 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 676 | if (halVersion < 0 || halVersion == deviceVersion) { |
| 677 | // Default path: HAL version is unspecified by caller, create CameraClient |
| 678 | // based on device version reported by the HAL. |
| 679 | switch(deviceVersion) { |
| 680 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 681 | client = new CameraClient(this, cameraClient, |
| 682 | clientPackageName, cameraId, |
| 683 | facing, callingPid, clientUid, getpid()); |
| 684 | break; |
| 685 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 686 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 687 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 688 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 689 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 690 | client = new Camera2Client(this, cameraClient, |
| 691 | clientPackageName, cameraId, |
| 692 | facing, callingPid, clientUid, getpid(), |
| 693 | deviceVersion); |
| 694 | break; |
| 695 | case -1: |
| 696 | ALOGE("Invalid camera id %d", cameraId); |
| 697 | return BAD_VALUE; |
| 698 | default: |
| 699 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 700 | return INVALID_OPERATION; |
| 701 | } |
| 702 | } else { |
| 703 | // A particular HAL version is requested by caller. Create CameraClient |
| 704 | // based on the requested HAL version. |
| 705 | if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 && |
| 706 | halVersion == CAMERA_DEVICE_API_VERSION_1_0) { |
| 707 | // Only support higher HAL version device opened as HAL1.0 device. |
| 708 | client = new CameraClient(this, cameraClient, |
| 709 | clientPackageName, cameraId, |
| 710 | facing, callingPid, clientUid, getpid()); |
| 711 | } else { |
| 712 | // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet. |
| 713 | ALOGE("Invalid camera HAL version %x: HAL %x device can only be" |
| 714 | " opened as HAL %x device", halVersion, deviceVersion, |
| 715 | CAMERA_DEVICE_API_VERSION_1_0); |
| 716 | return INVALID_OPERATION; |
| 717 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 721 | if (status != OK) { |
| 722 | // this is probably not recoverable.. maybe the client can try again |
| 723 | // OK: we can only get here if we were originally in PRESENT state |
| 724 | updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId); |
| 725 | return status; |
| 726 | } |
| 727 | |
| 728 | mClient[cameraId] = client; |
| 729 | LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, |
| 730 | getpid()); |
| 731 | |
| 732 | return OK; |
| 733 | } |
| 734 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 735 | status_t CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 736 | const sp<ICameraClient>& cameraClient, |
| 737 | int cameraId, |
| 738 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 739 | int clientUid, |
| 740 | /*out*/ |
| 741 | sp<ICamera>& device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 742 | |
| 743 | String8 clientName8(clientPackageName); |
| 744 | int callingPid = getCallingPid(); |
| 745 | |
| 746 | LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid, |
| 747 | clientName8.string(), cameraId); |
| 748 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 749 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 750 | if (status != OK) { |
| 751 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 754 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 755 | sp<Client> client; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 756 | { |
| 757 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 758 | sp<BasicClient> clientTmp; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 759 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 760 | cameraClient->asBinder(), |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 761 | /*out*/clientTmp)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 762 | return -EBUSY; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 763 | } else if (client.get() != NULL) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 764 | device = static_cast<Client*>(clientTmp.get()); |
| 765 | return OK; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 768 | status = connectHelperLocked(cameraClient, |
| 769 | cameraId, |
| 770 | clientPackageName, |
| 771 | clientUid, |
| 772 | callingPid, |
| 773 | client); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 774 | if (status != OK) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 775 | return status; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 778 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 779 | // important: release the mutex here so the client can call back |
| 780 | // 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] | 781 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 782 | device = client; |
| 783 | return OK; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 786 | status_t CameraService::connectLegacy( |
| 787 | const sp<ICameraClient>& cameraClient, |
| 788 | int cameraId, int halVersion, |
| 789 | const String16& clientPackageName, |
| 790 | int clientUid, |
| 791 | /*out*/ |
| 792 | sp<ICamera>& device) { |
| 793 | |
Igor Murashkin | 3d07d1a | 2014-06-20 11:27:03 -0700 | [diff] [blame^] | 794 | if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED && |
| 795 | mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_3) { |
| 796 | /* |
| 797 | * Either the HAL version is unspecified in which case this just creates |
| 798 | * a camera client selected by the latest device version, or |
| 799 | * it's a particular version in which case the HAL must supported |
| 800 | * the open_legacy call |
| 801 | */ |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 802 | ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!", |
| 803 | __FUNCTION__, mModule->common.module_api_version); |
| 804 | return INVALID_OPERATION; |
| 805 | } |
| 806 | |
| 807 | String8 clientName8(clientPackageName); |
| 808 | int callingPid = getCallingPid(); |
| 809 | |
| 810 | LOG1("CameraService::connect legacy E (pid %d \"%s\", id %d)", callingPid, |
| 811 | clientName8.string(), cameraId); |
| 812 | |
| 813 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 814 | if (status != OK) { |
| 815 | return status; |
| 816 | } |
| 817 | |
| 818 | sp<Client> client; |
| 819 | { |
| 820 | Mutex::Autolock lock(mServiceLock); |
| 821 | sp<BasicClient> clientTmp; |
| 822 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 823 | cameraClient->asBinder(), |
| 824 | /*out*/clientTmp)) { |
| 825 | return -EBUSY; |
| 826 | } else if (client.get() != NULL) { |
| 827 | device = static_cast<Client*>(clientTmp.get()); |
| 828 | return OK; |
| 829 | } |
| 830 | |
| 831 | status = connectHelperLocked(cameraClient, |
| 832 | cameraId, |
| 833 | clientPackageName, |
| 834 | clientUid, |
| 835 | callingPid, |
| 836 | client, |
| 837 | halVersion); |
| 838 | if (status != OK) { |
| 839 | return status; |
| 840 | } |
| 841 | |
| 842 | } |
| 843 | // important: release the mutex here so the client can call back |
| 844 | // into the service from its destructor (can be at the end of the call) |
| 845 | |
| 846 | device = client; |
| 847 | return OK; |
| 848 | } |
| 849 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 850 | status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client, |
| 851 | const sp<IBinder>& remoteCallback) { |
| 852 | status_t status = client->initialize(mModule); |
| 853 | if (status != OK) { |
| 854 | return status; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 855 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 856 | if (remoteCallback != NULL) { |
| 857 | remoteCallback->linkToDeath(this); |
| 858 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 859 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 860 | return OK; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 863 | status_t CameraService::connectPro( |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 864 | const sp<IProCameraCallbacks>& cameraCb, |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 865 | int cameraId, |
| 866 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 867 | int clientUid, |
| 868 | /*out*/ |
| 869 | sp<IProCameraUser>& device) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 870 | { |
Natalie Silvanovich | e1b55da | 2014-05-01 14:44:52 -0700 | [diff] [blame] | 871 | if (cameraCb == 0) { |
| 872 | ALOGE("%s: Callback must not be null", __FUNCTION__); |
| 873 | return BAD_VALUE; |
| 874 | } |
| 875 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 876 | String8 clientName8(clientPackageName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 877 | int callingPid = getCallingPid(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 878 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 879 | LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid, |
| 880 | clientName8.string(), cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 881 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 882 | if (status != OK) { |
| 883 | return status; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 886 | sp<ProClient> client; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 887 | { |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 888 | Mutex::Autolock lock(mServiceLock); |
| 889 | { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 890 | sp<BasicClient> client; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 891 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 892 | cameraCb->asBinder(), |
| 893 | /*out*/client)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 894 | return -EBUSY; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | int facing = -1; |
| 899 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 900 | |
| 901 | switch(deviceVersion) { |
| 902 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 903 | ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", |
| 904 | cameraId); |
Ruben Brunk | 17963d1 | 2013-08-19 15:21:19 -0700 | [diff] [blame] | 905 | return -EOPNOTSUPP; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 906 | break; |
| 907 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 908 | case CAMERA_DEVICE_API_VERSION_2_1: |
Zhijun He | 4711005 | 2013-07-22 17:34:34 -0700 | [diff] [blame] | 909 | case CAMERA_DEVICE_API_VERSION_3_0: |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 910 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 911 | case CAMERA_DEVICE_API_VERSION_3_2: |
Eino-Ville Talvala | 63d877f | 2014-06-16 19:21:12 -0700 | [diff] [blame] | 912 | client = new ProCamera2Client(this, cameraCb, clientPackageName, |
| 913 | cameraId, facing, callingPid, clientUid, getpid()); |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 914 | break; |
| 915 | case -1: |
| 916 | ALOGE("Invalid camera id %d", cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 917 | return BAD_VALUE; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 918 | default: |
| 919 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 920 | return INVALID_OPERATION; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 921 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 922 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 923 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 924 | if (status != OK) { |
| 925 | return status; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | mProClientList[cameraId].push(client); |
| 929 | |
| 930 | LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId, |
| 931 | getpid()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 932 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 933 | // important: release the mutex here so the client can call back |
| 934 | // 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] | 935 | device = client; |
| 936 | return OK; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 937 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 938 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 939 | status_t CameraService::connectDevice( |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 940 | const sp<ICameraDeviceCallbacks>& cameraCb, |
| 941 | int cameraId, |
| 942 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 943 | int clientUid, |
| 944 | /*out*/ |
| 945 | sp<ICameraDeviceUser>& device) |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 946 | { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 947 | |
| 948 | String8 clientName8(clientPackageName); |
| 949 | int callingPid = getCallingPid(); |
| 950 | |
| 951 | LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid, |
| 952 | clientName8.string(), cameraId); |
| 953 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 954 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 955 | if (status != OK) { |
| 956 | return status; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | sp<CameraDeviceClient> client; |
| 960 | { |
| 961 | Mutex::Autolock lock(mServiceLock); |
| 962 | { |
| 963 | sp<BasicClient> client; |
| 964 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 965 | cameraCb->asBinder(), |
| 966 | /*out*/client)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 967 | return -EBUSY; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
| 971 | int facing = -1; |
| 972 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 973 | |
| 974 | // If there are other non-exclusive users of the camera, |
| 975 | // this will tear them down before we can reuse the camera |
| 976 | if (isValidCameraId(cameraId)) { |
| 977 | // transition from PRESENT -> NOT_AVAILABLE |
| 978 | updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
| 979 | cameraId); |
| 980 | } |
| 981 | |
| 982 | switch(deviceVersion) { |
| 983 | case CAMERA_DEVICE_API_VERSION_1_0: |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 984 | ALOGW("Camera using old HAL version: %d", deviceVersion); |
Ruben Brunk | 17963d1 | 2013-08-19 15:21:19 -0700 | [diff] [blame] | 985 | return -EOPNOTSUPP; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 986 | // TODO: don't allow 2.0 Only allow 2.1 and higher |
| 987 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 988 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 989 | case CAMERA_DEVICE_API_VERSION_3_0: |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 990 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 991 | case CAMERA_DEVICE_API_VERSION_3_2: |
Eino-Ville Talvala | 63d877f | 2014-06-16 19:21:12 -0700 | [diff] [blame] | 992 | client = new CameraDeviceClient(this, cameraCb, clientPackageName, |
| 993 | cameraId, facing, callingPid, clientUid, getpid()); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 994 | break; |
| 995 | case -1: |
| 996 | ALOGE("Invalid camera id %d", cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 997 | return BAD_VALUE; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 998 | default: |
| 999 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1000 | return INVALID_OPERATION; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1003 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 1004 | if (status != OK) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1005 | // this is probably not recoverable.. maybe the client can try again |
| 1006 | // OK: we can only get here if we were originally in PRESENT state |
| 1007 | updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId); |
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) { |
| 1037 | if ((*it)->asBinder() == listener->asBinder()) { |
| 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) { |
| 1070 | if ((*it)->asBinder() == listener->asBinder()) { |
| 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 | |
| 1183 | clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this); |
| 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) : |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1377 | CameraService::BasicClient(cameraService, cameraClient->asBinder(), |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1378 | clientPackageName, |
| 1379 | cameraId, cameraFacing, |
| 1380 | clientPid, clientUid, |
| 1381 | servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1382 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1383 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1384 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1385 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1386 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1387 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1388 | cameraService->setCameraBusy(cameraId); |
| 1389 | cameraService->loadSound(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1390 | |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1391 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1394 | // tear down the client |
| 1395 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1396 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1397 | mDestructionStarted = true; |
| 1398 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 1399 | mCameraService->releaseSound(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1400 | // unconditionally disconnect. function is idempotent |
| 1401 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1404 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1405 | const sp<IBinder>& remoteCallback, |
| 1406 | const String16& clientPackageName, |
| 1407 | int cameraId, int cameraFacing, |
| 1408 | int clientPid, uid_t clientUid, |
| 1409 | int servicePid): |
| 1410 | mClientPackageName(clientPackageName) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1411 | { |
| 1412 | mCameraService = cameraService; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1413 | mRemoteBinder = remoteCallback; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1414 | mCameraId = cameraId; |
| 1415 | mCameraFacing = cameraFacing; |
| 1416 | mClientPid = clientPid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1417 | mClientUid = clientUid; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1418 | mServicePid = servicePid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1419 | mOpsActive = false; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1420 | mDestructionStarted = false; |
| 1421 | } |
| 1422 | |
| 1423 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1424 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1425 | mDestructionStarted = true; |
| 1426 | } |
| 1427 | |
| 1428 | void CameraService::BasicClient::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1429 | ALOGV("BasicClient::disconnect"); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1430 | mCameraService->removeClientByRemote(mRemoteBinder); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1431 | // client shouldn't be able to call into us anymore |
| 1432 | mClientPid = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1435 | status_t CameraService::BasicClient::startCameraOps() { |
| 1436 | int32_t res; |
| 1437 | |
| 1438 | mOpsCallback = new OpsCallback(this); |
| 1439 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1440 | { |
| 1441 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
| 1442 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 1443 | } |
| 1444 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1445 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, |
| 1446 | mClientPackageName, mOpsCallback); |
| 1447 | res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, |
| 1448 | mClientUid, mClientPackageName); |
| 1449 | |
| 1450 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 1451 | ALOGI("Camera %d: Access for \"%s\" has been revoked", |
| 1452 | mCameraId, String8(mClientPackageName).string()); |
| 1453 | return PERMISSION_DENIED; |
| 1454 | } |
| 1455 | mOpsActive = true; |
| 1456 | return OK; |
| 1457 | } |
| 1458 | |
| 1459 | status_t CameraService::BasicClient::finishCameraOps() { |
| 1460 | if (mOpsActive) { |
| 1461 | mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 1462 | mClientPackageName); |
| 1463 | mOpsActive = false; |
| 1464 | } |
| 1465 | mAppOpsManager.stopWatchingMode(mOpsCallback); |
| 1466 | mOpsCallback.clear(); |
| 1467 | |
| 1468 | return OK; |
| 1469 | } |
| 1470 | |
| 1471 | void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) { |
| 1472 | String8 name(packageName); |
| 1473 | String8 myName(mClientPackageName); |
| 1474 | |
| 1475 | if (op != AppOpsManager::OP_CAMERA) { |
| 1476 | ALOGW("Unexpected app ops notification received: %d", op); |
| 1477 | return; |
| 1478 | } |
| 1479 | |
| 1480 | int32_t res; |
| 1481 | res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, |
| 1482 | mClientUid, mClientPackageName); |
| 1483 | ALOGV("checkOp returns: %d, %s ", res, |
| 1484 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 1485 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 1486 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 1487 | "UNKNOWN"); |
| 1488 | |
| 1489 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 1490 | ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId, |
| 1491 | myName.string()); |
| 1492 | // Reset the client PID to allow server-initiated disconnect, |
| 1493 | // and to prevent further calls by client. |
| 1494 | mClientPid = getCallingPid(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1495 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 1496 | notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1497 | disconnect(); |
| 1498 | } |
| 1499 | } |
| 1500 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1501 | // ---------------------------------------------------------------------------- |
| 1502 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1503 | Mutex* CameraService::Client::getClientLockFromCookie(void* user) { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1504 | return gCameraService->getClientLockById((int)(intptr_t) user); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | // Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should |
| 1508 | // be acquired for this to be safe |
| 1509 | CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1510 | BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1511 | // OK: only CameraClient calls this, and they already cast anyway. |
| 1512 | Client* client = static_cast<Client*>(basicClient); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1513 | |
| 1514 | // This could happen if the Client is in the process of shutting down (the |
| 1515 | // last strong reference is gone, but the destructor hasn't finished |
| 1516 | // stopping the hardware). |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1517 | if (client == NULL) return NULL; |
| 1518 | |
| 1519 | // destruction already started, so should not be accessed |
| 1520 | if (client->mDestructionStarted) return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1521 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1522 | return client; |
| 1523 | } |
| 1524 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1525 | void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 1526 | const CaptureResultExtras& resultExtras) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1527 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1528 | } |
| 1529 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1530 | // NOTE: function is idempotent |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1531 | void CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1532 | ALOGV("Client::disconnect"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1533 | BasicClient::disconnect(); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1534 | mCameraService->setCameraFree(mCameraId); |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1535 | |
| 1536 | StatusVector rejectSourceStates; |
| 1537 | rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT); |
| 1538 | rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING); |
| 1539 | |
| 1540 | // Transition to PRESENT if the camera is not in either of above 2 states |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1541 | mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT, |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1542 | mCameraId, |
| 1543 | &rejectSourceStates); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1546 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 1547 | mClient(client) { |
| 1548 | } |
| 1549 | |
| 1550 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 1551 | const String16& packageName) { |
| 1552 | sp<BasicClient> client = mClient.promote(); |
| 1553 | if (client != NULL) { |
| 1554 | client->opChanged(op, packageName); |
| 1555 | } |
| 1556 | } |
| 1557 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1558 | // ---------------------------------------------------------------------------- |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1559 | // IProCamera |
| 1560 | // ---------------------------------------------------------------------------- |
| 1561 | |
| 1562 | CameraService::ProClient::ProClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1563 | const sp<IProCameraCallbacks>& remoteCallback, |
| 1564 | const String16& clientPackageName, |
| 1565 | int cameraId, |
| 1566 | int cameraFacing, |
| 1567 | int clientPid, |
| 1568 | uid_t clientUid, |
| 1569 | int servicePid) |
| 1570 | : CameraService::BasicClient(cameraService, remoteCallback->asBinder(), |
| 1571 | clientPackageName, cameraId, cameraFacing, |
| 1572 | clientPid, clientUid, servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1573 | { |
| 1574 | mRemoteCallback = remoteCallback; |
| 1575 | } |
| 1576 | |
| 1577 | CameraService::ProClient::~ProClient() { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1578 | } |
| 1579 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1580 | void CameraService::ProClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 1581 | const CaptureResultExtras& resultExtras) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1582 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1583 | } |
| 1584 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1585 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1586 | |
| 1587 | static const int kDumpLockRetries = 50; |
| 1588 | static const int kDumpLockSleep = 60000; |
| 1589 | |
| 1590 | static bool tryLock(Mutex& mutex) |
| 1591 | { |
| 1592 | bool locked = false; |
| 1593 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1594 | if (mutex.tryLock() == NO_ERROR) { |
| 1595 | locked = true; |
| 1596 | break; |
| 1597 | } |
| 1598 | usleep(kDumpLockSleep); |
| 1599 | } |
| 1600 | return locked; |
| 1601 | } |
| 1602 | |
| 1603 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1604 | String8 result; |
| 1605 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1606 | result.appendFormat("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1607 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 1608 | getCallingPid(), |
| 1609 | getCallingUid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1610 | write(fd, result.string(), result.size()); |
| 1611 | } else { |
| 1612 | bool locked = tryLock(mServiceLock); |
| 1613 | // failed to lock - CameraService is probably deadlocked |
| 1614 | if (!locked) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1615 | result.append("CameraService may be deadlocked\n"); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1616 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | bool hasClient = false; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1620 | if (!mModule) { |
| 1621 | result = String8::format("No camera module available!\n"); |
| 1622 | write(fd, result.string(), result.size()); |
Kalle Lampila | 6ec3a15 | 2013-04-30 15:27:19 +0300 | [diff] [blame] | 1623 | if (locked) mServiceLock.unlock(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1624 | return NO_ERROR; |
| 1625 | } |
| 1626 | |
| 1627 | result = String8::format("Camera module HAL API version: 0x%x\n", |
| 1628 | mModule->common.hal_api_version); |
| 1629 | result.appendFormat("Camera module API version: 0x%x\n", |
| 1630 | mModule->common.module_api_version); |
| 1631 | result.appendFormat("Camera module name: %s\n", |
| 1632 | mModule->common.name); |
| 1633 | result.appendFormat("Camera module author: %s\n", |
| 1634 | mModule->common.author); |
| 1635 | result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1636 | |
| 1637 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1638 | if (desc == NULL) { |
| 1639 | result.appendFormat("Vendor tags left unimplemented.\n"); |
| 1640 | } else { |
| 1641 | result.appendFormat("Vendor tag definitions:\n"); |
| 1642 | } |
| 1643 | |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1644 | write(fd, result.string(), result.size()); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1645 | |
| 1646 | if (desc != NULL) { |
| 1647 | desc->dump(fd, /*verbosity*/2, /*indentation*/4); |
| 1648 | } |
| 1649 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1650 | for (int i = 0; i < mNumberOfCameras; i++) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1651 | result = String8::format("Camera %d static information:\n", i); |
| 1652 | camera_info info; |
| 1653 | |
| 1654 | status_t rc = mModule->get_camera_info(i, &info); |
| 1655 | if (rc != OK) { |
| 1656 | result.appendFormat(" Error reading static information!\n"); |
| 1657 | write(fd, result.string(), result.size()); |
| 1658 | } else { |
| 1659 | result.appendFormat(" Facing: %s\n", |
| 1660 | info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); |
| 1661 | result.appendFormat(" Orientation: %d\n", info.orientation); |
| 1662 | int deviceVersion; |
| 1663 | if (mModule->common.module_api_version < |
| 1664 | CAMERA_MODULE_API_VERSION_2_0) { |
| 1665 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 1666 | } else { |
| 1667 | deviceVersion = info.device_version; |
| 1668 | } |
| 1669 | result.appendFormat(" Device version: 0x%x\n", deviceVersion); |
| 1670 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { |
| 1671 | result.appendFormat(" Device static metadata:\n"); |
| 1672 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 428b77a | 2012-07-30 09:55:30 -0700 | [diff] [blame] | 1673 | dump_indented_camera_metadata(info.static_camera_characteristics, |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1674 | fd, /*verbosity*/2, /*indentation*/4); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1675 | } else { |
| 1676 | write(fd, result.string(), result.size()); |
| 1677 | } |
| 1678 | } |
| 1679 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1680 | sp<BasicClient> client = mClient[i].promote(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1681 | if (client == 0) { |
| 1682 | result = String8::format(" Device is closed, no client instance\n"); |
| 1683 | write(fd, result.string(), result.size()); |
| 1684 | continue; |
| 1685 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1686 | hasClient = true; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1687 | result = String8::format(" Device is open. Client instance dump:\n"); |
| 1688 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1689 | client->dump(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1690 | } |
| 1691 | if (!hasClient) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1692 | result = String8::format("\nNo active camera clients yet.\n"); |
| 1693 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | if (locked) mServiceLock.unlock(); |
| 1697 | |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 1698 | // Dump camera traces if there were any |
| 1699 | write(fd, "\n", 1); |
| 1700 | camera3::CameraTraces::dump(fd, args); |
| 1701 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1702 | // change logging level |
| 1703 | int n = args.size(); |
| 1704 | for (int i = 0; i + 1 < n; i++) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1705 | String16 verboseOption("-v"); |
| 1706 | if (args[i] == verboseOption) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1707 | String8 levelStr(args[i+1]); |
| 1708 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1709 | result = String8::format("\nSetting log level to %d.\n", level); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1710 | setLogLevel(level); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1711 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1712 | } |
| 1713 | } |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1714 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1715 | } |
| 1716 | return NO_ERROR; |
| 1717 | } |
| 1718 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1719 | /*virtual*/void CameraService::binderDied( |
| 1720 | const wp<IBinder> &who) { |
| 1721 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1722 | /** |
| 1723 | * While tempting to promote the wp<IBinder> into a sp, |
| 1724 | * it's actually not supported by the binder driver |
| 1725 | */ |
| 1726 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1727 | ALOGV("java clients' binder died"); |
| 1728 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1729 | sp<BasicClient> cameraClient = getClientByRemote(who); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1730 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1731 | if (cameraClient == 0) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1732 | ALOGV("java clients' binder death already cleaned up (normal case)"); |
| 1733 | return; |
| 1734 | } |
| 1735 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1736 | ALOGW("Disconnecting camera client %p since the binder for it " |
| 1737 | "died (this pid %d)", cameraClient.get(), getCallingPid()); |
| 1738 | |
| 1739 | cameraClient->disconnect(); |
| 1740 | |
| 1741 | } |
| 1742 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1743 | void CameraService::updateStatus(ICameraServiceListener::Status status, |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1744 | int32_t cameraId, |
| 1745 | const StatusVector *rejectSourceStates) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1746 | // do not lock mServiceLock here or can get into a deadlock from |
| 1747 | // connect() -> ProClient::disconnect -> updateStatus |
| 1748 | Mutex::Autolock lock(mStatusMutex); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1749 | |
| 1750 | ICameraServiceListener::Status oldStatus = mStatusList[cameraId]; |
| 1751 | |
| 1752 | mStatusList[cameraId] = status; |
| 1753 | |
| 1754 | if (oldStatus != status) { |
| 1755 | ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x", |
| 1756 | __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status); |
| 1757 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1758 | if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT && |
| 1759 | (status != ICameraServiceListener::STATUS_PRESENT && |
| 1760 | status != ICameraServiceListener::STATUS_ENUMERATING)) { |
| 1761 | |
| 1762 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT" |
| 1763 | " or ENUMERATING", __FUNCTION__); |
| 1764 | mStatusList[cameraId] = oldStatus; |
| 1765 | return; |
| 1766 | } |
| 1767 | |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1768 | if (rejectSourceStates != NULL) { |
| 1769 | const StatusVector &rejectList = *rejectSourceStates; |
| 1770 | StatusVector::const_iterator it = rejectList.begin(); |
| 1771 | |
| 1772 | /** |
| 1773 | * Sometimes we want to conditionally do a transition. |
| 1774 | * For example if a client disconnects, we want to go to PRESENT |
| 1775 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 1776 | */ |
| 1777 | for (; it != rejectList.end(); ++it) { |
| 1778 | if (oldStatus == *it) { |
| 1779 | ALOGV("%s: Rejecting status transition for Camera ID %d, " |
| 1780 | " since the source state was was in one of the bad " |
| 1781 | " states.", __FUNCTION__, cameraId); |
| 1782 | mStatusList[cameraId] = oldStatus; |
| 1783 | return; |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1788 | /** |
| 1789 | * ProClients lose their exclusive lock. |
| 1790 | * - Done before the CameraClient can initialize the HAL device, |
| 1791 | * since we want to be able to close it before they get to initialize |
| 1792 | */ |
| 1793 | if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
| 1794 | Vector<wp<ProClient> > proClients(mProClientList[cameraId]); |
| 1795 | Vector<wp<ProClient> >::const_iterator it; |
| 1796 | |
| 1797 | for (it = proClients.begin(); it != proClients.end(); ++it) { |
| 1798 | sp<ProClient> proCl = it->promote(); |
| 1799 | if (proCl.get() != NULL) { |
| 1800 | proCl->onExclusiveLockStolen(); |
| 1801 | } |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | Vector<sp<ICameraServiceListener> >::const_iterator it; |
| 1806 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
| 1807 | (*it)->onStatusChanged(status, cameraId); |
| 1808 | } |
| 1809 | } |
| 1810 | } |
| 1811 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1812 | ICameraServiceListener::Status CameraService::getStatus(int cameraId) const { |
| 1813 | if (cameraId < 0 || cameraId >= MAX_CAMERAS) { |
| 1814 | ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId); |
| 1815 | return ICameraServiceListener::STATUS_UNKNOWN; |
| 1816 | } |
| 1817 | |
| 1818 | Mutex::Autolock al(mStatusMutex); |
| 1819 | return mStatusList[cameraId]; |
| 1820 | } |
| 1821 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1822 | }; // namespace android |