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 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <climits> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 23 | #include <cstring> |
| 24 | #include <ctime> |
| 25 | #include <string> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 26 | #include <sys/types.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 27 | #include <inttypes.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 28 | #include <pthread.h> |
| 29 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 30 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 31 | #include <binder/IPCThreadState.h> |
| 32 | #include <binder/IServiceManager.h> |
| 33 | #include <binder/MemoryBase.h> |
| 34 | #include <binder/MemoryHeapBase.h> |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 35 | #include <binder/ProcessInfoService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 36 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 37 | #include <cutils/properties.h> |
Amith Yamasani | 228711d | 2015-02-13 13:25:39 -0800 | [diff] [blame] | 38 | #include <cutils/multiuser.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 39 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | #include <hardware/hardware.h> |
| 41 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 42 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 43 | #include <media/mediaplayer.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | #include <utils/Errors.h> |
| 45 | #include <utils/Log.h> |
| 46 | #include <utils/String16.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 47 | #include <utils/Trace.h> |
| 48 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 49 | #include <system/camera_metadata.h> |
| 50 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | |
| 52 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 53 | #include "api1/CameraClient.h" |
| 54 | #include "api1/Camera2Client.h" |
| 55 | #include "api_pro/ProCamera2Client.h" |
| 56 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 57 | #include "utils/CameraTraces.h" |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 58 | #include "CameraDeviceFactory.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 59 | |
| 60 | namespace android { |
| 61 | |
| 62 | // ---------------------------------------------------------------------------- |
| 63 | // Logging support -- this is for debugging only |
| 64 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 65 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 66 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 67 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 68 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 69 | |
| 70 | static void setLogLevel(int level) { |
| 71 | android_atomic_write(level, &gLogLevel); |
| 72 | } |
| 73 | |
| 74 | // ---------------------------------------------------------------------------- |
| 75 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 76 | extern "C" { |
| 77 | static void camera_device_status_change( |
| 78 | const struct camera_module_callbacks* callbacks, |
| 79 | int camera_id, |
| 80 | int new_status) { |
| 81 | sp<CameraService> cs = const_cast<CameraService*>( |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 82 | static_cast<const CameraService*>(callbacks)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 83 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 84 | cs->onDeviceStatusChanged(static_cast<camera_device_status_t>(camera_id), |
| 85 | static_cast<camera_device_status_t>(new_status)); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 86 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 87 | |
| 88 | static void torch_mode_status_change( |
| 89 | const struct camera_module_callbacks* callbacks, |
| 90 | const char* camera_id, |
| 91 | int new_status) { |
| 92 | if (!callbacks || !camera_id) { |
| 93 | ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__, |
| 94 | callbacks, camera_id); |
| 95 | } |
| 96 | sp<CameraService> cs = const_cast<CameraService*>( |
| 97 | static_cast<const CameraService*>(callbacks)); |
| 98 | |
| 99 | ICameraServiceListener::TorchStatus status; |
| 100 | switch (new_status) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 101 | case TORCH_MODE_STATUS_NOT_AVAILABLE: |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 102 | status = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE; |
| 103 | break; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 104 | case TORCH_MODE_STATUS_AVAILABLE_OFF: |
| 105 | status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF; |
| 106 | break; |
| 107 | case TORCH_MODE_STATUS_AVAILABLE_ON: |
| 108 | status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 109 | break; |
| 110 | default: |
| 111 | ALOGE("Unknown torch status %d", new_status); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | cs->onTorchStatusChanged( |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 116 | String8(camera_id), |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 117 | status); |
| 118 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 119 | } // extern "C" |
| 120 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 121 | // ---------------------------------------------------------------------------- |
| 122 | |
| 123 | // This is ugly and only safe if we never re-create the CameraService, but |
| 124 | // should be ok for now. |
| 125 | static CameraService *gCameraService; |
| 126 | |
| 127 | CameraService::CameraService() |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 128 | : mEventLog(DEFAULT_EVICTION_LOG_LENGTH), mSoundRef(0), mModule(0), mFlashlight(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 129 | { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 130 | ALOGI("CameraService started (pid=%d)", getpid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 131 | gCameraService = this; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 132 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 133 | this->camera_device_status_change = android::camera_device_status_change; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 134 | this->torch_mode_status_change = android::torch_mode_status_change; |
| 135 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 136 | mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 139 | void CameraService::onFirstRef() |
| 140 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 141 | ALOGI("CameraService process starting"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 142 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 143 | BnCameraService::onFirstRef(); |
| 144 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 145 | camera_module_t *rawModule; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 146 | if (hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 147 | (const hw_module_t **)&rawModule) < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 148 | ALOGE("Could not load camera HAL module"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 149 | mNumberOfCameras = 0; |
| 150 | } |
| 151 | else { |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 152 | mModule = new CameraModule(rawModule); |
| 153 | const hw_module_t *common = mModule->getRawModule(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 154 | ALOGI("Loaded \"%s\" camera module", common->name); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 155 | mNumberOfCameras = mModule->getNumberOfCameras(); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 156 | |
| 157 | mFlashlight = new CameraFlashlight(*mModule, *this); |
| 158 | status_t res = mFlashlight->findFlashUnits(); |
| 159 | if (res) { |
| 160 | // impossible because we haven't open any camera devices. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 161 | ALOGE("Failed to find flash units."); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 164 | for (int i = 0; i < mNumberOfCameras; i++) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 165 | String8 cameraId = String8::format("%d", i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 166 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 167 | // Defaults to use for cost and conflicting devices |
| 168 | int cost = 100; |
| 169 | char** conflicting_devices = nullptr; |
| 170 | size_t conflicting_devices_length = 0; |
| 171 | |
| 172 | // If using post-2.4 module version, query the cost + conflicting devices from the HAL |
| 173 | if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_4) { |
| 174 | struct camera_info info; |
| 175 | status_t rc = mModule->getCameraInfo(i, &info); |
| 176 | if (rc == NO_ERROR) { |
| 177 | cost = info.resource_cost; |
| 178 | conflicting_devices = info.conflicting_devices; |
| 179 | conflicting_devices_length = info.conflicting_devices_length; |
| 180 | } else { |
| 181 | ALOGE("%s: Received error loading camera info for device %d, cost and" |
| 182 | " conflicting devices fields set to defaults for this device.", |
| 183 | __FUNCTION__, i); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | std::set<String8> conflicting; |
| 188 | for (size_t i = 0; i < conflicting_devices_length; i++) { |
| 189 | conflicting.emplace(String8(conflicting_devices[i])); |
| 190 | } |
| 191 | |
| 192 | // Initialize state for each camera device |
| 193 | { |
| 194 | Mutex::Autolock lock(mCameraStatesLock); |
| 195 | mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost, |
| 196 | conflicting)); |
| 197 | } |
| 198 | |
| 199 | if (mFlashlight->hasFlashUnit(cameraId)) { |
| 200 | mTorchStatusMap.add(cameraId, |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 201 | ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 202 | } |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 203 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 204 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 205 | if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_1) { |
| 206 | mModule->setCallbacks(this); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 207 | } |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 208 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 209 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 210 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 211 | if (common->module_api_version >= CAMERA_MODULE_API_VERSION_2_2) { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 212 | setUpVendorTags(); |
| 213 | } |
| 214 | |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 215 | CameraDeviceFactory::registerService(this); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 219 | CameraService::~CameraService() { |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 220 | if (mModule) { |
| 221 | delete mModule; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 222 | mModule = nullptr; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 223 | } |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 224 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 225 | gCameraService = nullptr; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 228 | void CameraService::onDeviceStatusChanged(camera_device_status_t cameraId, |
| 229 | camera_device_status_t newStatus) { |
| 230 | ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__, |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 231 | cameraId, newStatus); |
| 232 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 233 | String8 id = String8::format("%d", cameraId); |
| 234 | std::shared_ptr<CameraState> state = getCameraState(id); |
| 235 | |
| 236 | if (state == nullptr) { |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 237 | ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId); |
| 238 | return; |
| 239 | } |
| 240 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 241 | ICameraServiceListener::Status oldStatus = state->getStatus(); |
| 242 | |
| 243 | if (oldStatus == static_cast<ICameraServiceListener::Status>(newStatus)) { |
| 244 | ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 245 | return; |
| 246 | } |
| 247 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 248 | if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 249 | sp<BasicClient> clientToDisconnect; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 250 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 251 | // Don't do this in updateStatus to avoid deadlock over mServiceLock |
| 252 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 253 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 254 | // Set the device status to NOT_PRESENT, clients will no longer be able to connect |
| 255 | // to this device until the status changes |
| 256 | updateStatus(ICameraServiceListener::STATUS_NOT_PRESENT, id); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 257 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 258 | // Remove cached shim parameters |
| 259 | state->setShimParams(CameraParameters()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 260 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 261 | // Remove the client from the list of active clients |
| 262 | clientToDisconnect = removeClientLocked(id); |
| 263 | |
| 264 | // Notify the client of disconnection |
| 265 | clientToDisconnect->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 266 | CaptureResultExtras{}); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 269 | ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL", |
| 270 | __FUNCTION__, id.string()); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 271 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 272 | // Disconnect client |
| 273 | if (clientToDisconnect.get() != nullptr) { |
| 274 | // Ensure not in binder RPC so client disconnect PID checks work correctly |
| 275 | LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(), |
| 276 | "onDeviceStatusChanged must be called from the camera service process!"); |
| 277 | clientToDisconnect->disconnect(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 280 | } else { |
| 281 | updateStatus(static_cast<ICameraServiceListener::Status>(newStatus), id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 286 | void CameraService::onTorchStatusChanged(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 287 | ICameraServiceListener::TorchStatus newStatus) { |
| 288 | Mutex::Autolock al(mTorchStatusMutex); |
| 289 | onTorchStatusChangedLocked(cameraId, newStatus); |
| 290 | } |
| 291 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 292 | void CameraService::onTorchStatusChangedLocked(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 293 | ICameraServiceListener::TorchStatus newStatus) { |
| 294 | ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d", |
| 295 | __FUNCTION__, cameraId.string(), newStatus); |
| 296 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 297 | ICameraServiceListener::TorchStatus status; |
| 298 | status_t res = getTorchStatusLocked(cameraId, &status); |
| 299 | if (res) { |
| 300 | ALOGE("%s: cannot get torch status of camera %s", cameraId.string()); |
| 301 | return; |
| 302 | } |
| 303 | if (status == newStatus) { |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 304 | ALOGE("%s: Torch state transition to the same status 0x%x not allowed", |
| 305 | __FUNCTION__, (uint32_t)newStatus); |
| 306 | return; |
| 307 | } |
| 308 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 309 | res = setTorchStatusLocked(cameraId, newStatus); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 310 | if (res) { |
| 311 | ALOGE("%s: Failed to set the torch status", __FUNCTION__, |
| 312 | (uint32_t)newStatus); |
| 313 | return; |
| 314 | } |
| 315 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 316 | { |
| 317 | Mutex::Autolock lock(mStatusListenerLock); |
| 318 | for (auto& i : mListenerList) { |
| 319 | i->onTorchStatusChanged(newStatus, String16{cameraId}); |
| 320 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
| 324 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 325 | int32_t CameraService::getNumberOfCameras() { |
| 326 | return mNumberOfCameras; |
| 327 | } |
| 328 | |
| 329 | status_t CameraService::getCameraInfo(int cameraId, |
| 330 | struct CameraInfo* cameraInfo) { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 331 | if (!mModule) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 332 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 335 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 336 | return BAD_VALUE; |
| 337 | } |
| 338 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 339 | struct camera_info info; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 340 | status_t rc = filterGetInfoErrorCode( |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 341 | mModule->getCameraInfo(cameraId, &info)); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 342 | cameraInfo->facing = info.facing; |
| 343 | cameraInfo->orientation = info.orientation; |
| 344 | return rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 347 | int CameraService::cameraIdToInt(const String8& cameraId) { |
| 348 | errno = 0; |
| 349 | size_t pos = 0; |
| 350 | int ret = stoi(std::string{cameraId.string()}, &pos); |
| 351 | if (errno != 0 || pos != cameraId.size()) { |
| 352 | return -1; |
| 353 | } |
| 354 | return ret; |
| 355 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 356 | |
| 357 | status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) { |
| 358 | status_t ret = OK; |
| 359 | struct CameraInfo info; |
| 360 | if ((ret = getCameraInfo(cameraId, &info)) != OK) { |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | CameraMetadata shimInfo; |
| 365 | int32_t orientation = static_cast<int32_t>(info.orientation); |
| 366 | if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) { |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ? |
| 371 | ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK; |
| 372 | if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) { |
| 373 | return ret; |
| 374 | } |
| 375 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 376 | CameraParameters shimParams; |
| 377 | if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { |
| 378 | // Error logged by callee |
| 379 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | Vector<Size> sizes; |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 383 | Vector<Size> jpegSizes; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 384 | Vector<int32_t> formats; |
| 385 | const char* supportedPreviewFormats; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 386 | { |
| 387 | shimParams.getSupportedPreviewSizes(/*out*/sizes); |
| 388 | shimParams.getSupportedPreviewFormats(/*out*/formats); |
| 389 | shimParams.getSupportedPictureSizes(/*out*/jpegSizes); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Always include IMPLEMENTATION_DEFINED |
| 393 | formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED); |
| 394 | |
| 395 | const size_t INTS_PER_CONFIG = 4; |
| 396 | |
| 397 | // Build available stream configurations metadata |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 398 | size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG; |
| 399 | |
| 400 | Vector<int32_t> streamConfigs; |
| 401 | streamConfigs.setCapacity(streamConfigSize); |
| 402 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 403 | for (size_t i = 0; i < formats.size(); ++i) { |
| 404 | for (size_t j = 0; j < sizes.size(); ++j) { |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 405 | streamConfigs.add(formats[i]); |
| 406 | streamConfigs.add(sizes[j].width); |
| 407 | streamConfigs.add(sizes[j].height); |
| 408 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 412 | for (size_t i = 0; i < jpegSizes.size(); ++i) { |
| 413 | streamConfigs.add(HAL_PIXEL_FORMAT_BLOB); |
| 414 | streamConfigs.add(jpegSizes[i].width); |
| 415 | streamConfigs.add(jpegSizes[i].height); |
| 416 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
| 417 | } |
| 418 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 419 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame] | 420 | streamConfigs.array(), streamConfigSize)) != OK) { |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | int64_t fakeMinFrames[0]; |
| 425 | // TODO: Fixme, don't fake min frame durations. |
| 426 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 427 | fakeMinFrames, 0)) != OK) { |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | int64_t fakeStalls[0]; |
| 432 | // TODO: Fixme, don't fake stall durations. |
| 433 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 434 | fakeStalls, 0)) != OK) { |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | *cameraInfo = shimInfo; |
| 439 | return OK; |
| 440 | } |
| 441 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 442 | status_t CameraService::getCameraCharacteristics(int cameraId, |
| 443 | CameraMetadata* cameraInfo) { |
| 444 | if (!cameraInfo) { |
| 445 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
| 446 | return BAD_VALUE; |
| 447 | } |
| 448 | |
| 449 | if (!mModule) { |
| 450 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 451 | return -ENODEV; |
| 452 | } |
| 453 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 454 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 455 | ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId); |
| 456 | return BAD_VALUE; |
| 457 | } |
| 458 | |
| 459 | int facing; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 460 | status_t ret = OK; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 461 | if (mModule->getRawModule()->module_api_version < CAMERA_MODULE_API_VERSION_2_0 || |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 462 | getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) { |
| 463 | /** |
| 464 | * Backwards compatibility mode for old HALs: |
| 465 | * - Convert CameraInfo into static CameraMetadata properties. |
| 466 | * - Retrieve cached CameraParameters for this camera. If none exist, |
| 467 | * attempt to open CameraClient and retrieve the CameraParameters. |
| 468 | * - Convert cached CameraParameters into static CameraMetadata |
| 469 | * properties. |
| 470 | */ |
| 471 | ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 472 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 473 | if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) { |
| 474 | return ret; |
| 475 | } |
Zhijun He | f05e50e | 2013-10-01 11:05:33 -0700 | [diff] [blame] | 476 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 477 | } else { |
| 478 | /** |
| 479 | * Normal HAL 2.1+ codepath. |
| 480 | */ |
| 481 | struct camera_info info; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 482 | ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info)); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 483 | *cameraInfo = info.static_camera_characteristics; |
| 484 | } |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 485 | |
| 486 | return ret; |
| 487 | } |
| 488 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 489 | int CameraService::getCallingPid() { |
| 490 | return IPCThreadState::self()->getCallingPid(); |
| 491 | } |
| 492 | |
| 493 | int CameraService::getCallingUid() { |
| 494 | return IPCThreadState::self()->getCallingUid(); |
| 495 | } |
| 496 | |
| 497 | String8 CameraService::getFormattedCurrentTime() { |
| 498 | time_t now = time(nullptr); |
| 499 | char formattedTime[64]; |
| 500 | strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now)); |
| 501 | return String8(formattedTime); |
| 502 | } |
| 503 | |
| 504 | int CameraService::getCameraPriorityFromProcState(int procState) { |
| 505 | // Find the priority for the camera usage based on the process state. Higher priority clients |
| 506 | // win for evictions. |
| 507 | // Note: Unlike the ordering for ActivityManager, persistent system processes will always lose |
| 508 | // the camera to the top/foreground applications. |
| 509 | switch(procState) { |
| 510 | case PROCESS_STATE_TOP: // User visible |
| 511 | return 100; |
| 512 | case PROCESS_STATE_IMPORTANT_FOREGROUND: // Foreground |
| 513 | return 90; |
| 514 | case PROCESS_STATE_PERSISTENT: // Persistent system services |
| 515 | case PROCESS_STATE_PERSISTENT_UI: |
| 516 | return 80; |
| 517 | case PROCESS_STATE_IMPORTANT_BACKGROUND: // "Important" background processes |
| 518 | return 70; |
| 519 | case PROCESS_STATE_BACKUP: // Everything else |
| 520 | case PROCESS_STATE_HEAVY_WEIGHT: |
| 521 | case PROCESS_STATE_SERVICE: |
| 522 | case PROCESS_STATE_RECEIVER: |
| 523 | case PROCESS_STATE_HOME: |
| 524 | case PROCESS_STATE_LAST_ACTIVITY: |
| 525 | case PROCESS_STATE_CACHED_ACTIVITY: |
| 526 | case PROCESS_STATE_CACHED_ACTIVITY_CLIENT: |
| 527 | case PROCESS_STATE_CACHED_EMPTY: |
| 528 | return 1; |
| 529 | case PROCESS_STATE_NONEXISTENT: |
| 530 | return -1; |
| 531 | default: |
| 532 | ALOGE("%s: Received unknown process state from ActivityManagerService!", __FUNCTION__); |
| 533 | return -1; |
| 534 | } |
| 535 | } |
| 536 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 537 | status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) { |
| 538 | if (!mModule) { |
| 539 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 540 | return -ENODEV; |
| 541 | } |
| 542 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 543 | desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 544 | return OK; |
| 545 | } |
| 546 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 547 | int CameraService::getDeviceVersion(int cameraId, int* facing) { |
| 548 | struct camera_info info; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 549 | if (mModule->getCameraInfo(cameraId, &info) != OK) { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | int deviceVersion; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 554 | if (mModule->getRawModule()->module_api_version >= CAMERA_MODULE_API_VERSION_2_0) { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 555 | deviceVersion = info.device_version; |
| 556 | } else { |
| 557 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 558 | } |
| 559 | |
| 560 | if (facing) { |
| 561 | *facing = info.facing; |
| 562 | } |
| 563 | |
| 564 | return deviceVersion; |
| 565 | } |
| 566 | |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 567 | status_t CameraService::filterGetInfoErrorCode(status_t err) { |
| 568 | switch(err) { |
| 569 | case NO_ERROR: |
| 570 | case -EINVAL: |
| 571 | return err; |
| 572 | default: |
| 573 | break; |
| 574 | } |
| 575 | return -ENODEV; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 578 | bool CameraService::setUpVendorTags() { |
| 579 | vendor_tag_ops_t vOps = vendor_tag_ops_t(); |
| 580 | |
| 581 | // Check if vendor operations have been implemented |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 582 | if (!mModule->isVendorTagDefined()) { |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 583 | ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__); |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops"); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 588 | mModule->getVendorTagOps(&vOps); |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 589 | ATRACE_END(); |
| 590 | |
| 591 | // Ensure all vendor operations are present |
| 592 | if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL || |
| 593 | vOps.get_section_name == NULL || vOps.get_tag_name == NULL || |
| 594 | vOps.get_tag_type == NULL) { |
| 595 | ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions." |
| 596 | , __FUNCTION__); |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | // Read all vendor tag definitions into a descriptor |
| 601 | sp<VendorTagDescriptor> desc; |
| 602 | status_t res; |
| 603 | if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc)) |
| 604 | != OK) { |
| 605 | ALOGE("%s: Could not generate descriptor from vendor tag operations," |
| 606 | "received error %s (%d). Camera clients will not be able to use" |
| 607 | "vendor tags", __FUNCTION__, strerror(res), res); |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | // Set the global descriptor to use with camera metadata |
| 612 | VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 613 | return true; |
| 614 | } |
| 615 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 616 | status_t CameraService::makeClient(const sp<CameraService>& cameraService, |
| 617 | const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId, |
| 618 | int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode, |
| 619 | int halVersion, int deviceVersion, apiLevel effectiveApiLevel, |
| 620 | /*out*/sp<BasicClient>* client) { |
| 621 | |
| 622 | // TODO: Update CameraClients + HAL interface to use strings for Camera IDs |
| 623 | int id = cameraIdToInt(cameraId); |
| 624 | if (id == -1) { |
| 625 | ALOGE("%s: Invalid camera ID %s, cannot convert to integer.", __FUNCTION__, |
| 626 | cameraId.string()); |
| 627 | return BAD_VALUE; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 630 | if (halVersion < 0 || halVersion == deviceVersion) { |
| 631 | // Default path: HAL version is unspecified by caller, create CameraClient |
| 632 | // based on device version reported by the HAL. |
| 633 | switch(deviceVersion) { |
| 634 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 635 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 636 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
| 637 | *client = new CameraClient(cameraService, tmp, packageName, id, facing, |
| 638 | clientPid, clientUid, getpid(), legacyMode); |
| 639 | } else { // Camera2 API route |
| 640 | ALOGW("Camera using old HAL version: %d", deviceVersion); |
| 641 | return -EOPNOTSUPP; |
| 642 | } |
| 643 | break; |
| 644 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 645 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 646 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 647 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 648 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 649 | case CAMERA_DEVICE_API_VERSION_3_3: |
| 650 | if (effectiveApiLevel == API_1) { // Camera1 API route |
| 651 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
| 652 | *client = new Camera2Client(cameraService, tmp, packageName, id, facing, |
| 653 | clientPid, clientUid, servicePid, legacyMode); |
| 654 | } else { // Camera2 API route |
| 655 | sp<ICameraDeviceCallbacks> tmp = |
| 656 | static_cast<ICameraDeviceCallbacks*>(cameraCb.get()); |
| 657 | *client = new CameraDeviceClient(cameraService, tmp, packageName, id, |
| 658 | facing, clientPid, clientUid, servicePid); |
| 659 | } |
| 660 | break; |
| 661 | default: |
| 662 | // Should not be reachable |
| 663 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 664 | return INVALID_OPERATION; |
| 665 | } |
| 666 | } else { |
| 667 | // A particular HAL version is requested by caller. Create CameraClient |
| 668 | // based on the requested HAL version. |
| 669 | if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 && |
| 670 | halVersion == CAMERA_DEVICE_API_VERSION_1_0) { |
| 671 | // Only support higher HAL version device opened as HAL1.0 device. |
| 672 | sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get()); |
| 673 | *client = new CameraClient(cameraService, tmp, packageName, id, facing, |
| 674 | clientPid, clientUid, servicePid, legacyMode); |
| 675 | } else { |
| 676 | // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet. |
| 677 | ALOGE("Invalid camera HAL version %x: HAL %x device can only be" |
| 678 | " opened as HAL %x device", halVersion, deviceVersion, |
| 679 | CAMERA_DEVICE_API_VERSION_1_0); |
| 680 | return INVALID_OPERATION; |
| 681 | } |
| 682 | } |
| 683 | return NO_ERROR; |
| 684 | } |
| 685 | |
| 686 | status_t CameraService::initializeShimMetadata(int cameraId) { |
| 687 | int uid = getCallingUid(); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 688 | |
| 689 | String16 internalPackageName("media"); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 690 | String8 id = String8::format("%d", cameraId); |
| 691 | status_t ret = NO_ERROR; |
| 692 | sp<Client> tmp = nullptr; |
| 693 | if ((ret = connectHelper<ICameraClient,Client>(sp<ICameraClient>{nullptr}, id, |
| 694 | static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED), internalPackageName, uid, API_1, |
| 695 | false, true, tmp)) != NO_ERROR) { |
| 696 | ALOGE("%s: Error %d (%s) initializing shim metadata.", __FUNCTION__, ret, strerror(ret)); |
| 697 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 698 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 699 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 702 | status_t CameraService::getLegacyParametersLazy(int cameraId, |
| 703 | /*out*/ |
| 704 | CameraParameters* parameters) { |
| 705 | |
| 706 | ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId); |
| 707 | |
| 708 | status_t ret = 0; |
| 709 | |
| 710 | if (parameters == NULL) { |
| 711 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
| 712 | return BAD_VALUE; |
| 713 | } |
| 714 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 715 | String8 id = String8::format("%d", cameraId); |
| 716 | |
| 717 | // Check if we already have parameters |
| 718 | { |
| 719 | // Scope for service lock |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 720 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 721 | auto cameraState = getCameraState(id); |
| 722 | if (cameraState == nullptr) { |
| 723 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
| 724 | return BAD_VALUE; |
| 725 | } |
| 726 | CameraParameters p = cameraState->getShimParams(); |
| 727 | if (!p.isEmpty()) { |
| 728 | *parameters = p; |
| 729 | return NO_ERROR; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 733 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 734 | ret = initializeShimMetadata(cameraId); |
| 735 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 736 | if (ret != NO_ERROR) { |
| 737 | // Error already logged by callee |
| 738 | return ret; |
| 739 | } |
| 740 | |
| 741 | // Check for parameters again |
| 742 | { |
| 743 | // Scope for service lock |
| 744 | Mutex::Autolock lock(mServiceLock); |
| 745 | auto cameraState = getCameraState(id); |
| 746 | if (cameraState == nullptr) { |
| 747 | ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string()); |
| 748 | return BAD_VALUE; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 749 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 750 | CameraParameters p = cameraState->getShimParams(); |
| 751 | if (!p.isEmpty()) { |
| 752 | *parameters = p; |
| 753 | return NO_ERROR; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 757 | ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.", |
| 758 | __FUNCTION__); |
| 759 | return INVALID_OPERATION; |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 762 | status_t CameraService::validateConnect(const String8& cameraId, /*inout*/int& clientUid) const { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 763 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 764 | int callingPid = getCallingPid(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 765 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 766 | if (clientUid == USE_CALLING_UID) { |
| 767 | clientUid = getCallingUid(); |
| 768 | } else { |
| 769 | // We only trust our own process to forward client UIDs |
| 770 | if (callingPid != getpid()) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 771 | ALOGE("CameraService::connect X (PID %d) rejected (don't trust clientUid)", |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 772 | callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 773 | return PERMISSION_DENIED; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 774 | } |
| 775 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 776 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 777 | if (!mModule) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 778 | ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)", |
| 779 | callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 780 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 783 | if (getCameraState(cameraId) == nullptr) { |
| 784 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 785 | cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 786 | return -ENODEV; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 789 | // Check device policy for this camera |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 790 | char value[PROPERTY_VALUE_MAX]; |
Amith Yamasani | 228711d | 2015-02-13 13:25:39 -0800 | [diff] [blame] | 791 | char key[PROPERTY_KEY_MAX]; |
| 792 | int clientUserId = multiuser_get_user_id(clientUid); |
| 793 | snprintf(key, PROPERTY_KEY_MAX, "sys.secpolicy.camera.off_%d", clientUserId); |
| 794 | property_get(key, value, "0"); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 795 | if (strcmp(value, "1") == 0) { |
| 796 | // Camera is disabled by DevicePolicyManager. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 797 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is disabled by device " |
| 798 | "policy)", callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 799 | return -EACCES; |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 800 | } |
| 801 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 802 | return checkIfDeviceIsUsable(cameraId); |
| 803 | } |
| 804 | |
| 805 | status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const { |
| 806 | auto cameraState = getCameraState(cameraId); |
| 807 | int callingPid = getCallingPid(); |
| 808 | if (cameraState == nullptr) { |
| 809 | ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid, |
| 810 | cameraId.string()); |
| 811 | return -ENODEV; |
| 812 | } |
| 813 | |
| 814 | ICameraServiceListener::Status currentStatus = cameraState->getStatus(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 815 | if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 816 | ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)", |
| 817 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 818 | return -ENODEV; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 819 | } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 820 | ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)", |
| 821 | callingPid, cameraId.string()); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 822 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 823 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 824 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 825 | return NO_ERROR; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 828 | void CameraService::finishConnectLocked(const sp<BasicClient>& client, |
| 829 | const CameraService::DescriptorPtr& desc) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 830 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 831 | // Make a descriptor for the incoming client |
| 832 | auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc); |
| 833 | auto evicted = mActiveClientManager.addAndEvict(clientDescriptor); |
| 834 | |
| 835 | logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()), |
| 836 | String8(client->getPackageName())); |
| 837 | |
| 838 | if (evicted.size() > 0) { |
| 839 | // This should never happen - clients should already have been removed in disconnect |
| 840 | for (auto& i : evicted) { |
| 841 | ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect", |
| 842 | __FUNCTION__, i->getKey().string()); |
| 843 | } |
| 844 | |
| 845 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly", |
| 846 | __FUNCTION__); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid, |
| 851 | apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName, |
| 852 | /*out*/ |
| 853 | sp<BasicClient>* client, |
| 854 | std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) { |
| 855 | |
| 856 | status_t ret = NO_ERROR; |
| 857 | std::vector<sp<BasicClient>> evictedClients; |
| 858 | DescriptorPtr clientDescriptor; |
| 859 | { |
| 860 | if (effectiveApiLevel == API_1) { |
| 861 | // If we are using API1, any existing client for this camera ID with the same remote |
| 862 | // should be returned rather than evicted to allow MediaRecorder to work properly. |
| 863 | |
| 864 | auto current = mActiveClientManager.get(cameraId); |
| 865 | if (current != nullptr) { |
| 866 | auto clientSp = current->getValue(); |
| 867 | if (clientSp.get() != nullptr) { // should never be needed |
| 868 | if (clientSp->getRemote() == remoteCallback) { |
| 869 | ALOGI("CameraService::connect X (PID %d) (second call from same" |
| 870 | "app binder, returning the same client)", clientPid); |
| 871 | *client = clientSp; |
| 872 | return NO_ERROR; |
| 873 | } |
| 874 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 875 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 876 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 877 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 878 | // Return error if the device was unplugged or removed by the HAL for some reason |
| 879 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 880 | return ret; |
| 881 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 882 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 883 | // Get current active client PIDs |
| 884 | std::vector<int> ownerPids(mActiveClientManager.getAllOwners()); |
| 885 | ownerPids.push_back(clientPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 886 | |
Chih-Hung Hsieh | 54b4246 | 2015-03-19 12:04:54 -0700 | [diff] [blame^] | 887 | // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking |
| 888 | // address of PROCESS_STATE_NONEXISTENT as a reference argument |
| 889 | // for the vector constructor. PROCESS_STATE_NONEXISTENT does |
| 890 | // not have an out-of-class definition. |
| 891 | std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 892 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 893 | // Get priorites of all active PIDs |
| 894 | ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0], |
| 895 | /*out*/&priorities[0]); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 896 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 897 | // Update all active clients' priorities |
| 898 | std::map<int,int> pidToPriorityMap; |
| 899 | for (size_t i = 0; i < ownerPids.size() - 1; i++) { |
| 900 | pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i])); |
| 901 | } |
| 902 | mActiveClientManager.updatePriorities(pidToPriorityMap); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 903 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 904 | // Get state for the given cameraId |
| 905 | auto state = getCameraState(cameraId); |
| 906 | if (state == nullptr) { |
| 907 | ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)", |
| 908 | clientPid, cameraId.string()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 909 | return BAD_VALUE; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 910 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 911 | |
| 912 | // Make descriptor for incoming client |
| 913 | clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId, |
| 914 | sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()), |
| 915 | state->getConflicting(), |
| 916 | getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid); |
| 917 | |
| 918 | // Find clients that would be evicted |
| 919 | auto evicted = mActiveClientManager.wouldEvict(clientDescriptor); |
| 920 | |
| 921 | // If the incoming client was 'evicted,' higher priority clients have the camera in the |
| 922 | // background, so we cannot do evictions |
| 923 | if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) { |
| 924 | ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher" |
| 925 | " priority).", clientPid); |
| 926 | |
| 927 | sp<BasicClient> clientSp = clientDescriptor->getValue(); |
| 928 | String8 curTime = getFormattedCurrentTime(); |
| 929 | auto incompatibleClients = |
| 930 | mActiveClientManager.getIncompatibleClients(clientDescriptor); |
| 931 | |
| 932 | String8 msg = String8::format("%s : DENIED connect device %s client for package %s " |
| 933 | "(PID %d, priority %d)", curTime.string(), |
| 934 | cameraId.string(), packageName.string(), clientPid, |
| 935 | getCameraPriorityFromProcState(priorities[priorities.size() - 1])); |
| 936 | |
| 937 | for (auto& i : incompatibleClients) { |
| 938 | msg.appendFormat("\n - Blocked by existing device %s client for package %s" |
| 939 | "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(), |
| 940 | String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(), |
| 941 | i->getPriority()); |
| 942 | } |
| 943 | |
| 944 | // Log the client's attempt |
| 945 | mEventLog.add(msg); |
| 946 | |
| 947 | return -EBUSY; |
| 948 | } |
| 949 | |
| 950 | for (auto& i : evicted) { |
| 951 | sp<BasicClient> clientSp = i->getValue(); |
| 952 | if (clientSp.get() == nullptr) { |
| 953 | ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__); |
| 954 | |
| 955 | // TODO: Remove this |
| 956 | LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list", |
| 957 | __FUNCTION__); |
| 958 | mActiveClientManager.remove(i); |
| 959 | continue; |
| 960 | } |
| 961 | |
| 962 | ALOGE("CameraService::connect evicting conflicting client for camera ID %s", |
| 963 | i->getKey().string()); |
| 964 | evictedClients.push_back(clientSp); |
| 965 | |
| 966 | String8 curTime = getFormattedCurrentTime(); |
| 967 | |
| 968 | // Log the clients evicted |
| 969 | mEventLog.add(String8::format("%s : EVICT device %s client for package %s (PID %" |
| 970 | PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for " |
| 971 | "package %s (PID %d, priority %" PRId32 ")", curTime.string(), |
| 972 | i->getKey().string(), String8{clientSp->getPackageName()}.string(), |
| 973 | i->getOwnerId(), i->getPriority(), cameraId.string(), |
| 974 | packageName.string(), clientPid, |
| 975 | getCameraPriorityFromProcState(priorities[priorities.size() - 1]))); |
| 976 | |
| 977 | // Notify the client of disconnection |
| 978 | clientSp->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 979 | CaptureResultExtras()); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 980 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 983 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 984 | // other clients from connecting in mServiceLockWrapper if held |
| 985 | mServiceLock.unlock(); |
| 986 | |
| 987 | // Clear caller identity temporarily so client disconnect PID checks work correctly |
| 988 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 989 | |
| 990 | // Destroy evicted clients |
| 991 | for (auto& i : evictedClients) { |
| 992 | // Disconnect is blocking, and should only have returned when HAL has cleaned up |
| 993 | i->disconnect(); // Clients will remove themselves from the active client list here |
| 994 | } |
| 995 | evictedClients.clear(); |
| 996 | |
| 997 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 998 | |
| 999 | // Once clients have been disconnected, relock |
| 1000 | mServiceLock.lock(); |
| 1001 | |
| 1002 | // Check again if the device was unplugged or something while we weren't holding mServiceLock |
| 1003 | if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) { |
| 1004 | return ret; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1007 | *partial = clientDescriptor; |
| 1008 | return NO_ERROR; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1011 | status_t CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1012 | const sp<ICameraClient>& cameraClient, |
| 1013 | int cameraId, |
| 1014 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1015 | int clientUid, |
| 1016 | /*out*/ |
| 1017 | sp<ICamera>& device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1018 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1019 | status_t ret = NO_ERROR; |
| 1020 | String8 id = String8::format("%d", cameraId); |
| 1021 | sp<Client> client = nullptr; |
| 1022 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, CAMERA_HAL_API_VERSION_UNSPECIFIED, |
| 1023 | clientPackageName, clientUid, API_1, false, false, /*out*/client); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1024 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1025 | if(ret != NO_ERROR) { |
| 1026 | return ret; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 1029 | device = client; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1030 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1033 | status_t CameraService::connectLegacy( |
| 1034 | const sp<ICameraClient>& cameraClient, |
| 1035 | int cameraId, int halVersion, |
| 1036 | const String16& clientPackageName, |
| 1037 | int clientUid, |
| 1038 | /*out*/ |
| 1039 | sp<ICamera>& device) { |
| 1040 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1041 | int apiVersion = mModule->getRawModule()->module_api_version; |
Igor Murashkin | 3d07d1a | 2014-06-20 11:27:03 -0700 | [diff] [blame] | 1042 | if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED && |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1043 | apiVersion < CAMERA_MODULE_API_VERSION_2_3) { |
Igor Murashkin | 3d07d1a | 2014-06-20 11:27:03 -0700 | [diff] [blame] | 1044 | /* |
| 1045 | * Either the HAL version is unspecified in which case this just creates |
| 1046 | * a camera client selected by the latest device version, or |
| 1047 | * it's a particular version in which case the HAL must supported |
| 1048 | * the open_legacy call |
| 1049 | */ |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1050 | ALOGE("%s: camera HAL module version %x doesn't support connecting to legacy HAL devices!", |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1051 | __FUNCTION__, apiVersion); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1052 | return INVALID_OPERATION; |
| 1053 | } |
| 1054 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1055 | status_t ret = NO_ERROR; |
| 1056 | String8 id = String8::format("%d", cameraId); |
| 1057 | sp<Client> client = nullptr; |
| 1058 | ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion, clientPackageName, |
| 1059 | clientUid, API_1, true, false, /*out*/client); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1060 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1061 | if(ret != NO_ERROR) { |
| 1062 | return ret; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1065 | device = client; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1066 | return NO_ERROR; |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1069 | status_t CameraService::connectPro(const sp<IProCameraCallbacks>& cameraCb, |
| 1070 | int cameraId, |
| 1071 | const String16& clientPackageName, |
| 1072 | int clientUid, |
| 1073 | /*out*/ |
| 1074 | sp<IProCameraUser>& device) { |
| 1075 | ALOGE("%s: Unimplemented, please use connectDevice", __FUNCTION__); |
| 1076 | return INVALID_OPERATION; |
| 1077 | } |
| 1078 | |
| 1079 | status_t CameraService::connectDevice( |
| 1080 | const sp<ICameraDeviceCallbacks>& cameraCb, |
| 1081 | int cameraId, |
| 1082 | const String16& clientPackageName, |
| 1083 | int clientUid, |
| 1084 | /*out*/ |
| 1085 | sp<ICameraDeviceUser>& device) { |
| 1086 | |
| 1087 | status_t ret = NO_ERROR; |
| 1088 | String8 id = String8::format("%d", cameraId); |
| 1089 | sp<CameraDeviceClient> client = nullptr; |
| 1090 | ret = connectHelper<ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id, |
| 1091 | CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, API_2, false, false, |
| 1092 | /*out*/client); |
| 1093 | |
| 1094 | if(ret != NO_ERROR) { |
| 1095 | return ret; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1098 | device = client; |
| 1099 | return NO_ERROR; |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1102 | status_t CameraService::setTorchMode(const String16& cameraId, bool enabled, |
| 1103 | const sp<IBinder>& clientBinder) { |
| 1104 | if (enabled && clientBinder == NULL) { |
| 1105 | ALOGE("%s: torch client binder is NULL", __FUNCTION__); |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1106 | return -EINVAL; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1109 | String8 id = String8(cameraId.string()); |
| 1110 | |
| 1111 | // verify id is valid. |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1112 | auto state = getCameraState(id); |
| 1113 | if (state == nullptr) { |
| 1114 | ALOGE("%s: camera id is invalid %s", id.string()); |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | |
| 1118 | ICameraServiceListener::Status cameraStatus = state->getStatus(); |
| 1119 | if (cameraStatus != ICameraServiceListener::STATUS_PRESENT && |
| 1120 | cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1121 | ALOGE("%s: camera id is invalid %s", id.string()); |
| 1122 | return -EINVAL; |
| 1123 | } |
| 1124 | |
| 1125 | { |
| 1126 | Mutex::Autolock al(mTorchStatusMutex); |
| 1127 | ICameraServiceListener::TorchStatus status; |
| 1128 | status_t res = getTorchStatusLocked(id, &status); |
| 1129 | if (res) { |
| 1130 | ALOGE("%s: getting current torch status failed for camera %s", |
| 1131 | __FUNCTION__, id.string()); |
| 1132 | return -EINVAL; |
| 1133 | } |
| 1134 | |
| 1135 | if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1136 | if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1137 | ALOGE("%s: torch mode of camera %s is not available because " |
| 1138 | "camera is in use", __FUNCTION__, id.string()); |
| 1139 | return -EBUSY; |
| 1140 | } else { |
| 1141 | ALOGE("%s: torch mode of camera %s is not available due to " |
| 1142 | "insufficient resources", __FUNCTION__, id.string()); |
| 1143 | return -EUSERS; |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | status_t res = mFlashlight->setTorchMode(id, enabled); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1149 | if (res) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1150 | ALOGE("%s: setting torch mode of camera %s to %d failed. %s (%d)", |
| 1151 | __FUNCTION__, id.string(), enabled, strerror(-res), res); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1152 | return res; |
| 1153 | } |
| 1154 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1155 | { |
| 1156 | // update the link to client's death |
| 1157 | Mutex::Autolock al(mTorchClientMapMutex); |
| 1158 | ssize_t index = mTorchClientMap.indexOfKey(id); |
| 1159 | if (enabled) { |
| 1160 | if (index == NAME_NOT_FOUND) { |
| 1161 | mTorchClientMap.add(id, clientBinder); |
| 1162 | } else { |
| 1163 | const sp<IBinder> oldBinder = mTorchClientMap.valueAt(index); |
| 1164 | oldBinder->unlinkToDeath(this); |
| 1165 | |
| 1166 | mTorchClientMap.replaceValueAt(index, clientBinder); |
| 1167 | } |
| 1168 | clientBinder->linkToDeath(this); |
| 1169 | } else if (index != NAME_NOT_FOUND) { |
| 1170 | sp<IBinder> oldBinder = mTorchClientMap.valueAt(index); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1171 | oldBinder->unlinkToDeath(this); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1172 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1175 | return OK; |
| 1176 | } |
| 1177 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1178 | status_t CameraService::addListener( |
| 1179 | const sp<ICameraServiceListener>& listener) { |
| 1180 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1181 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 1182 | if (listener == 0) { |
| 1183 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
| 1184 | return BAD_VALUE; |
| 1185 | } |
| 1186 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1187 | Mutex::Autolock lock(mServiceLock); |
| 1188 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1189 | { |
| 1190 | Mutex::Autolock lock(mStatusListenerLock); |
| 1191 | for (auto& it : mListenerList) { |
| 1192 | if (IInterface::asBinder(it) == IInterface::asBinder(listener)) { |
| 1193 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 1194 | __FUNCTION__, listener.get()); |
| 1195 | return ALREADY_EXISTS; |
| 1196 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1197 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1198 | |
| 1199 | mListenerList.push_back(listener); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1200 | } |
| 1201 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1202 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1203 | /* Immediately signal current status to this listener only */ |
| 1204 | { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1205 | Mutex::Autolock lock(mCameraStatesLock); |
| 1206 | for (auto& i : mCameraStates) { |
| 1207 | // TODO: Update binder to use String16 for camera IDs and remove; |
| 1208 | int id = cameraIdToInt(i.first); |
| 1209 | if (id == -1) continue; |
| 1210 | |
| 1211 | listener->onStatusChanged(i.second->getStatus(), id); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1215 | /* Immediately signal current torch status to this listener only */ |
| 1216 | { |
| 1217 | Mutex::Autolock al(mTorchStatusMutex); |
| 1218 | for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1219 | String16 id = String16(mTorchStatusMap.keyAt(i).string()); |
| 1220 | listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1221 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1224 | return OK; |
| 1225 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1226 | |
| 1227 | status_t CameraService::removeListener(const sp<ICameraServiceListener>& listener) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1228 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 1229 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 1230 | if (listener == 0) { |
| 1231 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
| 1232 | return BAD_VALUE; |
| 1233 | } |
| 1234 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1235 | Mutex::Autolock lock(mServiceLock); |
| 1236 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1237 | { |
| 1238 | Mutex::Autolock lock(mStatusListenerLock); |
| 1239 | for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) { |
| 1240 | if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) { |
| 1241 | mListenerList.erase(it); |
| 1242 | return OK; |
| 1243 | } |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 1248 | __FUNCTION__, listener.get()); |
| 1249 | |
| 1250 | return BAD_VALUE; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1251 | } |
| 1252 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1253 | status_t CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) { |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1254 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 1255 | |
| 1256 | if (parameters == NULL) { |
| 1257 | ALOGE("%s: parameters must not be null", __FUNCTION__); |
| 1258 | return BAD_VALUE; |
| 1259 | } |
| 1260 | |
| 1261 | status_t ret = 0; |
| 1262 | |
| 1263 | CameraParameters shimParams; |
| 1264 | if ((ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)) != OK) { |
| 1265 | // Error logged by caller |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | String8 shimParamsString8 = shimParams.flatten(); |
| 1270 | String16 shimParamsString16 = String16(shimParamsString8); |
| 1271 | |
| 1272 | *parameters = shimParamsString16; |
| 1273 | |
| 1274 | return OK; |
| 1275 | } |
| 1276 | |
| 1277 | status_t CameraService::supportsCameraApi(int cameraId, int apiVersion) { |
| 1278 | ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId); |
| 1279 | |
| 1280 | switch (apiVersion) { |
| 1281 | case API_VERSION_1: |
| 1282 | case API_VERSION_2: |
| 1283 | break; |
| 1284 | default: |
| 1285 | ALOGE("%s: Bad API version %d", __FUNCTION__, apiVersion); |
| 1286 | return BAD_VALUE; |
| 1287 | } |
| 1288 | |
| 1289 | int facing = -1; |
| 1290 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 1291 | |
| 1292 | switch(deviceVersion) { |
| 1293 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 1294 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 1295 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 1296 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 1297 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 1298 | if (apiVersion == API_VERSION_2) { |
| 1299 | ALOGV("%s: Camera id %d uses HAL prior to HAL3.2, doesn't support api2 without shim", |
| 1300 | __FUNCTION__, cameraId); |
| 1301 | return -EOPNOTSUPP; |
| 1302 | } else { // if (apiVersion == API_VERSION_1) { |
| 1303 | ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported", |
| 1304 | __FUNCTION__, cameraId); |
| 1305 | return OK; |
| 1306 | } |
| 1307 | case CAMERA_DEVICE_API_VERSION_3_2: |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1308 | case CAMERA_DEVICE_API_VERSION_3_3: |
Igor Murashkin | 65d14b9 | 2014-06-17 12:03:20 -0700 | [diff] [blame] | 1309 | ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly", |
| 1310 | __FUNCTION__, cameraId); |
| 1311 | return OK; |
| 1312 | case -1: |
| 1313 | ALOGE("%s: Invalid camera id %d", __FUNCTION__, cameraId); |
| 1314 | return BAD_VALUE; |
| 1315 | default: |
| 1316 | ALOGE("%s: Unknown camera device HAL version: %d", __FUNCTION__, deviceVersion); |
| 1317 | return INVALID_OPERATION; |
| 1318 | } |
| 1319 | |
| 1320 | return OK; |
| 1321 | } |
| 1322 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1323 | void CameraService::removeByClient(const BasicClient* client) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1324 | Mutex::Autolock lock(mServiceLock); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1325 | for (auto& i : mActiveClientManager.getAll()) { |
| 1326 | auto clientSp = i->getValue(); |
| 1327 | if (clientSp.get() == client) { |
| 1328 | mActiveClientManager.remove(i); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1329 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1330 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1333 | bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) { |
| 1334 | const int callingPid = getCallingPid(); |
| 1335 | const int servicePid = getpid(); |
| 1336 | bool ret = false; |
| 1337 | { |
| 1338 | // Acquire mServiceLock and prevent other clients from connecting |
| 1339 | std::unique_ptr<AutoConditionLock> lock = |
| 1340 | AutoConditionLock::waitAndAcquire(mServiceLockWrapper); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1341 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1342 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1343 | std::vector<sp<BasicClient>> evicted; |
| 1344 | for (auto& i : mActiveClientManager.getAll()) { |
| 1345 | auto clientSp = i->getValue(); |
| 1346 | if (clientSp.get() == nullptr) { |
| 1347 | ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__); |
| 1348 | mActiveClientManager.remove(i); |
| 1349 | continue; |
| 1350 | } |
| 1351 | if (remote == clientSp->getRemote() && (callingPid == servicePid || |
| 1352 | callingPid == clientSp->getClientPid())) { |
| 1353 | mActiveClientManager.remove(i); |
| 1354 | evicted.push_back(clientSp); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1355 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1356 | // Notify the client of disconnection |
| 1357 | clientSp->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED, |
| 1358 | CaptureResultExtras()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1359 | } |
| 1360 | } |
| 1361 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1362 | // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking |
| 1363 | // other clients from connecting in mServiceLockWrapper if held |
| 1364 | mServiceLock.unlock(); |
| 1365 | |
| 1366 | for (auto& i : evicted) { |
| 1367 | if (i.get() != nullptr) { |
| 1368 | i->disconnect(); |
| 1369 | ret = true; |
| 1370 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1373 | // Reacquire mServiceLock |
| 1374 | mServiceLock.lock(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1375 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1376 | } // lock is destroyed, allow further connect calls |
| 1377 | |
| 1378 | return ret; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1381 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1382 | std::shared_ptr<CameraService::CameraState> CameraService::getCameraState( |
| 1383 | const String8& cameraId) const { |
| 1384 | std::shared_ptr<CameraState> state; |
| 1385 | { |
| 1386 | Mutex::Autolock lock(mCameraStatesLock); |
| 1387 | auto iter = mCameraStates.find(cameraId); |
| 1388 | if (iter != mCameraStates.end()) { |
| 1389 | state = iter->second; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1390 | } |
| 1391 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1392 | return state; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1395 | sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) { |
| 1396 | // Remove from active clients list |
| 1397 | auto clientDescriptorPtr = mActiveClientManager.remove(cameraId); |
| 1398 | if (clientDescriptorPtr == nullptr) { |
| 1399 | ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__, |
| 1400 | cameraId.string()); |
| 1401 | return sp<BasicClient>{nullptr}; |
| 1402 | } |
| 1403 | |
| 1404 | return clientDescriptorPtr->getValue(); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1407 | |
| 1408 | void CameraService::logDisconnected(const String8& cameraId, int clientPid, |
| 1409 | const String8& clientPackage) { |
| 1410 | |
| 1411 | String8 curTime = getFormattedCurrentTime(); |
| 1412 | // Log the clients evicted |
| 1413 | mEventLog.add(String8::format("%s : DISCONNECT device %s client for package %s (PID %d)", |
| 1414 | curTime.string(), cameraId.string(), clientPackage.string(), clientPid)); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1417 | void CameraService::logConnected(const String8& cameraId, int clientPid, |
| 1418 | const String8& clientPackage) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1419 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1420 | String8 curTime = getFormattedCurrentTime(); |
| 1421 | // Log the clients evicted |
| 1422 | mEventLog.add(String8::format("%s : CONNECT device %s client for package %s (PID %d)", |
| 1423 | curTime.string(), cameraId.string(), clientPackage.string(), clientPid)); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1426 | status_t CameraService::onTransact( |
| 1427 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 1428 | // Permission checks |
| 1429 | switch (code) { |
| 1430 | case BnCameraService::CONNECT: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1431 | case BnCameraService::CONNECT_PRO: |
Eino-Ville Talvala | 63d877f | 2014-06-16 19:21:12 -0700 | [diff] [blame] | 1432 | case BnCameraService::CONNECT_DEVICE: |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 1433 | case BnCameraService::CONNECT_LEGACY: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1434 | const int pid = getCallingPid(); |
| 1435 | const int self_pid = getpid(); |
| 1436 | if (pid != self_pid) { |
| 1437 | // we're called from a different process, do the real check |
| 1438 | if (!checkCallingPermission( |
| 1439 | String16("android.permission.CAMERA"))) { |
| 1440 | const int uid = getCallingUid(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1441 | ALOGE("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1442 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 1443 | return PERMISSION_DENIED; |
| 1444 | } |
| 1445 | } |
| 1446 | break; |
| 1447 | } |
| 1448 | |
| 1449 | return BnCameraService::onTransact(code, data, reply, flags); |
| 1450 | } |
| 1451 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1452 | // We share the media players for shutter and recording sound for all clients. |
| 1453 | // A reference count is kept to determine when we will actually release the |
| 1454 | // media players. |
| 1455 | |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 1456 | MediaPlayer* CameraService::newMediaPlayer(const char *file) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1457 | MediaPlayer* mp = new MediaPlayer(); |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 1458 | if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 1459 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1460 | mp->prepare(); |
| 1461 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1462 | ALOGE("Failed to load CameraService sounds: %s", file); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1463 | return NULL; |
| 1464 | } |
| 1465 | return mp; |
| 1466 | } |
| 1467 | |
| 1468 | void CameraService::loadSound() { |
| 1469 | Mutex::Autolock lock(mSoundLock); |
| 1470 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 1471 | if (mSoundRef++) return; |
| 1472 | |
| 1473 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 1474 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 1475 | } |
| 1476 | |
| 1477 | void CameraService::releaseSound() { |
| 1478 | Mutex::Autolock lock(mSoundLock); |
| 1479 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 1480 | if (--mSoundRef) return; |
| 1481 | |
| 1482 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 1483 | if (mSoundPlayer[i] != 0) { |
| 1484 | mSoundPlayer[i]->disconnect(); |
| 1485 | mSoundPlayer[i].clear(); |
| 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | void CameraService::playSound(sound_kind kind) { |
| 1491 | LOG1("playSound(%d)", kind); |
| 1492 | Mutex::Autolock lock(mSoundLock); |
| 1493 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 1494 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 1495 | player->seekTo(0); |
| 1496 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | // ---------------------------------------------------------------------------- |
| 1501 | |
| 1502 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 1503 | const sp<ICameraClient>& cameraClient, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1504 | const String16& clientPackageName, |
| 1505 | int cameraId, int cameraFacing, |
| 1506 | int clientPid, uid_t clientUid, |
| 1507 | int servicePid) : |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 1508 | CameraService::BasicClient(cameraService, |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1509 | IInterface::asBinder(cameraClient), |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1510 | clientPackageName, |
| 1511 | cameraId, cameraFacing, |
| 1512 | clientPid, clientUid, |
| 1513 | servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1514 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1515 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1516 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1517 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1518 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1519 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1520 | cameraService->loadSound(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1521 | |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1522 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1525 | // tear down the client |
| 1526 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1527 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1528 | mDestructionStarted = true; |
| 1529 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 1530 | mCameraService->releaseSound(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1531 | // unconditionally disconnect. function is idempotent |
| 1532 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1535 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1536 | const sp<IBinder>& remoteCallback, |
| 1537 | const String16& clientPackageName, |
| 1538 | int cameraId, int cameraFacing, |
| 1539 | int clientPid, uid_t clientUid, |
| 1540 | int servicePid): |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1541 | mClientPackageName(clientPackageName), mDisconnected(false) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1542 | { |
| 1543 | mCameraService = cameraService; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1544 | mRemoteBinder = remoteCallback; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1545 | mCameraId = cameraId; |
| 1546 | mCameraFacing = cameraFacing; |
| 1547 | mClientPid = clientPid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1548 | mClientUid = clientUid; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1549 | mServicePid = servicePid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1550 | mOpsActive = false; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1551 | mDestructionStarted = false; |
| 1552 | } |
| 1553 | |
| 1554 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1555 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1556 | mDestructionStarted = true; |
| 1557 | } |
| 1558 | |
| 1559 | void CameraService::BasicClient::disconnect() { |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1560 | if (mDisconnected) return; |
| 1561 | mDisconnected = true;; |
| 1562 | |
| 1563 | mCameraService->removeByClient(this); |
| 1564 | mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid, |
| 1565 | String8(mClientPackageName)); |
| 1566 | |
| 1567 | sp<IBinder> remote = getRemote(); |
| 1568 | if (remote != nullptr) { |
| 1569 | remote->unlinkToDeath(mCameraService); |
| 1570 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1571 | |
| 1572 | finishCameraOps(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1573 | ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid); |
| 1574 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1575 | // client shouldn't be able to call into us anymore |
| 1576 | mClientPid = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1577 | } |
| 1578 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1579 | String16 CameraService::BasicClient::getPackageName() const { |
| 1580 | return mClientPackageName; |
| 1581 | } |
| 1582 | |
| 1583 | |
| 1584 | int CameraService::BasicClient::getClientPid() const { |
| 1585 | return mClientPid; |
| 1586 | } |
| 1587 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1588 | status_t CameraService::BasicClient::startCameraOps() { |
| 1589 | int32_t res; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1590 | // Notify app ops that the camera is not available |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1591 | mOpsCallback = new OpsCallback(this); |
| 1592 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1593 | { |
| 1594 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
| 1595 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 1596 | } |
| 1597 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1598 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, |
| 1599 | mClientPackageName, mOpsCallback); |
| 1600 | res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, |
| 1601 | mClientUid, mClientPackageName); |
| 1602 | |
| 1603 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 1604 | ALOGI("Camera %d: Access for \"%s\" has been revoked", |
| 1605 | mCameraId, String8(mClientPackageName).string()); |
| 1606 | return PERMISSION_DENIED; |
| 1607 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1608 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1609 | mOpsActive = true; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1610 | |
| 1611 | // Transition device availability listeners from PRESENT -> NOT_AVAILABLE |
| 1612 | mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1613 | String8::format("%d", mCameraId)); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1614 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1615 | return OK; |
| 1616 | } |
| 1617 | |
| 1618 | status_t CameraService::BasicClient::finishCameraOps() { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1619 | // Check if startCameraOps succeeded, and if so, finish the camera op |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1620 | if (mOpsActive) { |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1621 | // Notify app ops that the camera is available again |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1622 | mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 1623 | mClientPackageName); |
| 1624 | mOpsActive = false; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1625 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1626 | auto rejected = {ICameraServiceListener::STATUS_NOT_PRESENT, |
| 1627 | ICameraServiceListener::STATUS_ENUMERATING}; |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1628 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1629 | // Transition to PRESENT if the camera is not in either of the rejected states |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1630 | mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT, |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1631 | String8::format("%d", mCameraId), rejected); |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1632 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 1633 | // Notify flashlight that a camera device is closed. |
| 1634 | mCameraService->mFlashlight->deviceClosed( |
| 1635 | String8::format("%d", mCameraId)); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1636 | } |
Eino-Ville Talvala | f67e23e | 2014-07-23 17:17:59 -0700 | [diff] [blame] | 1637 | // Always stop watching, even if no camera op is active |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 1638 | if (mOpsCallback != NULL) { |
| 1639 | mAppOpsManager.stopWatchingMode(mOpsCallback); |
| 1640 | } |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1641 | mOpsCallback.clear(); |
| 1642 | |
| 1643 | return OK; |
| 1644 | } |
| 1645 | |
| 1646 | void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) { |
| 1647 | String8 name(packageName); |
| 1648 | String8 myName(mClientPackageName); |
| 1649 | |
| 1650 | if (op != AppOpsManager::OP_CAMERA) { |
| 1651 | ALOGW("Unexpected app ops notification received: %d", op); |
| 1652 | return; |
| 1653 | } |
| 1654 | |
| 1655 | int32_t res; |
| 1656 | res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, |
| 1657 | mClientUid, mClientPackageName); |
| 1658 | ALOGV("checkOp returns: %d, %s ", res, |
| 1659 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 1660 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 1661 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 1662 | "UNKNOWN"); |
| 1663 | |
| 1664 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 1665 | ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId, |
| 1666 | myName.string()); |
| 1667 | // Reset the client PID to allow server-initiated disconnect, |
| 1668 | // and to prevent further calls by client. |
| 1669 | mClientPid = getCallingPid(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1670 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 1671 | notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1672 | disconnect(); |
| 1673 | } |
| 1674 | } |
| 1675 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1676 | // ---------------------------------------------------------------------------- |
| 1677 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1678 | // Provide client strong pointer for callbacks. |
| 1679 | sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) { |
| 1680 | String8 cameraId = String8::format("%d", (int)(intptr_t) user); |
| 1681 | auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId); |
| 1682 | if (clientDescriptor != nullptr) { |
| 1683 | return sp<Client>{ |
| 1684 | static_cast<Client*>(clientDescriptor->getValue().get())}; |
| 1685 | } |
| 1686 | return sp<Client>{nullptr}; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1689 | void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 1690 | const CaptureResultExtras& resultExtras) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1691 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1694 | // NOTE: function is idempotent |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1695 | void CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1696 | ALOGV("Client::disconnect"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1697 | BasicClient::disconnect(); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 1698 | } |
| 1699 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1700 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 1701 | mClient(client) { |
| 1702 | } |
| 1703 | |
| 1704 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 1705 | const String16& packageName) { |
| 1706 | sp<BasicClient> client = mClient.promote(); |
| 1707 | if (client != NULL) { |
| 1708 | client->opChanged(op, packageName); |
| 1709 | } |
| 1710 | } |
| 1711 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1712 | // ---------------------------------------------------------------------------- |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1713 | // IProCamera |
| 1714 | // ---------------------------------------------------------------------------- |
| 1715 | |
| 1716 | CameraService::ProClient::ProClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1717 | const sp<IProCameraCallbacks>& remoteCallback, |
| 1718 | const String16& clientPackageName, |
| 1719 | int cameraId, |
| 1720 | int cameraFacing, |
| 1721 | int clientPid, |
| 1722 | uid_t clientUid, |
| 1723 | int servicePid) |
Marco Nelissen | f888020 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 1724 | : CameraService::BasicClient(cameraService, IInterface::asBinder(remoteCallback), |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1725 | clientPackageName, cameraId, cameraFacing, |
| 1726 | clientPid, clientUid, servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1727 | { |
| 1728 | mRemoteCallback = remoteCallback; |
| 1729 | } |
| 1730 | |
| 1731 | CameraService::ProClient::~ProClient() { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1734 | void CameraService::ProClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 1735 | const CaptureResultExtras& resultExtras) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1736 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1739 | // ---------------------------------------------------------------------------- |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1740 | // CameraState |
| 1741 | // ---------------------------------------------------------------------------- |
| 1742 | |
| 1743 | CameraService::CameraState::CameraState(const String8& id, int cost, |
| 1744 | const std::set<String8>& conflicting) : mId(id), |
| 1745 | mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {} |
| 1746 | |
| 1747 | CameraService::CameraState::~CameraState() {} |
| 1748 | |
| 1749 | ICameraServiceListener::Status CameraService::CameraState::getStatus() const { |
| 1750 | Mutex::Autolock lock(mStatusLock); |
| 1751 | return mStatus; |
| 1752 | } |
| 1753 | |
| 1754 | CameraParameters CameraService::CameraState::getShimParams() const { |
| 1755 | return mShimParams; |
| 1756 | } |
| 1757 | |
| 1758 | void CameraService::CameraState::setShimParams(const CameraParameters& params) { |
| 1759 | mShimParams = params; |
| 1760 | } |
| 1761 | |
| 1762 | int CameraService::CameraState::getCost() const { |
| 1763 | return mCost; |
| 1764 | } |
| 1765 | |
| 1766 | std::set<String8> CameraService::CameraState::getConflicting() const { |
| 1767 | return mConflicting; |
| 1768 | } |
| 1769 | |
| 1770 | String8 CameraService::CameraState::getId() const { |
| 1771 | return mId; |
| 1772 | } |
| 1773 | |
| 1774 | // ---------------------------------------------------------------------------- |
| 1775 | // CameraClientManager |
| 1776 | // ---------------------------------------------------------------------------- |
| 1777 | |
| 1778 | CameraService::CameraClientManager::~CameraClientManager() {} |
| 1779 | |
| 1780 | sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient( |
| 1781 | const String8& id) const { |
| 1782 | auto descriptor = get(id); |
| 1783 | if (descriptor == nullptr) { |
| 1784 | return sp<BasicClient>{nullptr}; |
| 1785 | } |
| 1786 | return descriptor->getValue(); |
| 1787 | } |
| 1788 | |
| 1789 | String8 CameraService::CameraClientManager::toString() const { |
| 1790 | auto all = getAll(); |
| 1791 | String8 ret("["); |
| 1792 | bool hasAny = false; |
| 1793 | for (auto& i : all) { |
| 1794 | hasAny = true; |
| 1795 | String8 key = i->getKey(); |
| 1796 | int32_t cost = i->getCost(); |
| 1797 | int32_t pid = i->getOwnerId(); |
| 1798 | int32_t priority = i->getPriority(); |
| 1799 | auto conflicting = i->getConflicting(); |
| 1800 | auto clientSp = i->getValue(); |
| 1801 | String8 packageName; |
| 1802 | if (clientSp.get() != nullptr) { |
| 1803 | packageName = String8{clientSp->getPackageName()}; |
| 1804 | } |
| 1805 | ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %" |
| 1806 | PRId32 ", ", key.string(), cost, pid, priority); |
| 1807 | |
| 1808 | if (packageName.size() != 0) { |
| 1809 | ret.appendFormat("Client Package Name: %s", packageName.string()); |
| 1810 | } |
| 1811 | |
| 1812 | ret.append(", Conflicting Client Devices: {"); |
| 1813 | for (auto& j : conflicting) { |
| 1814 | ret.appendFormat("%s, ", j.string()); |
| 1815 | } |
| 1816 | ret.append("})"); |
| 1817 | } |
| 1818 | if (hasAny) ret.append("\n"); |
| 1819 | ret.append("]\n"); |
| 1820 | return ret; |
| 1821 | } |
| 1822 | |
| 1823 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 1824 | const String8& key, const sp<BasicClient>& value, int32_t cost, |
| 1825 | const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) { |
| 1826 | |
| 1827 | return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>( |
| 1828 | key, value, cost, conflictingKeys, priority, ownerId); |
| 1829 | } |
| 1830 | |
| 1831 | CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor( |
| 1832 | const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) { |
| 1833 | return makeClientDescriptor(partial->getKey(), value, partial->getCost(), |
| 1834 | partial->getConflicting(), partial->getPriority(), partial->getOwnerId()); |
| 1835 | } |
| 1836 | |
| 1837 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1838 | |
| 1839 | static const int kDumpLockRetries = 50; |
| 1840 | static const int kDumpLockSleep = 60000; |
| 1841 | |
| 1842 | static bool tryLock(Mutex& mutex) |
| 1843 | { |
| 1844 | bool locked = false; |
| 1845 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1846 | if (mutex.tryLock() == NO_ERROR) { |
| 1847 | locked = true; |
| 1848 | break; |
| 1849 | } |
| 1850 | usleep(kDumpLockSleep); |
| 1851 | } |
| 1852 | return locked; |
| 1853 | } |
| 1854 | |
| 1855 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1856 | String8 result; |
| 1857 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1858 | result.appendFormat("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1859 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 1860 | getCallingPid(), |
| 1861 | getCallingUid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1862 | write(fd, result.string(), result.size()); |
| 1863 | } else { |
| 1864 | bool locked = tryLock(mServiceLock); |
| 1865 | // failed to lock - CameraService is probably deadlocked |
| 1866 | if (!locked) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1867 | result.append("CameraService may be deadlocked\n"); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1868 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | bool hasClient = false; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1872 | if (!mModule) { |
| 1873 | result = String8::format("No camera module available!\n"); |
| 1874 | write(fd, result.string(), result.size()); |
Kalle Lampila | 6ec3a15 | 2013-04-30 15:27:19 +0300 | [diff] [blame] | 1875 | if (locked) mServiceLock.unlock(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1876 | return NO_ERROR; |
| 1877 | } |
| 1878 | |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1879 | const hw_module_t* common = mModule->getRawModule(); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1880 | result = String8::format("Camera module HAL API version: %#x\n", common->hal_api_version); |
| 1881 | result.appendFormat("Camera module API version: %#x\n", common->module_api_version); |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1882 | result.appendFormat("Camera module name: %s\n", common->name); |
| 1883 | result.appendFormat("Camera module author: %s\n", common->author); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1884 | result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras); |
| 1885 | String8 activeClientString = mActiveClientManager.toString(); |
| 1886 | result.appendFormat("Active Camera Clients:\n%s", activeClientString.string()); |
| 1887 | |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1888 | |
| 1889 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1890 | if (desc == NULL) { |
| 1891 | result.appendFormat("Vendor tags left unimplemented.\n"); |
| 1892 | } else { |
| 1893 | result.appendFormat("Vendor tag definitions:\n"); |
| 1894 | } |
| 1895 | |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1896 | write(fd, result.string(), result.size()); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1897 | |
| 1898 | if (desc != NULL) { |
| 1899 | desc->dump(fd, /*verbosity*/2, /*indentation*/4); |
| 1900 | } |
| 1901 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1902 | result = String8("Prior client events (most recent at top):\n"); |
| 1903 | |
| 1904 | for (const auto& msg : mEventLog) { |
| 1905 | result.appendFormat("%s\n", msg.string()); |
| 1906 | } |
| 1907 | |
| 1908 | if (mEventLog.size() == DEFAULT_EVICTION_LOG_LENGTH) { |
| 1909 | result.append("...\n"); |
| 1910 | } |
| 1911 | |
| 1912 | write(fd, result.string(), result.size()); |
| 1913 | |
| 1914 | bool stateLocked = tryLock(mCameraStatesLock); |
| 1915 | if (!stateLocked) { |
| 1916 | result = String8::format("CameraStates in use, may be deadlocked\n"); |
| 1917 | write(fd, result.string(), result.size()); |
| 1918 | } |
| 1919 | |
| 1920 | for (auto& state : mCameraStates) { |
| 1921 | String8 cameraId = state.first; |
| 1922 | result = String8::format("Camera %s information:\n", cameraId.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1923 | camera_info info; |
| 1924 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1925 | // TODO: Change getCameraInfo + HAL to use String cameraIds |
| 1926 | status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1927 | if (rc != OK) { |
| 1928 | result.appendFormat(" Error reading static information!\n"); |
| 1929 | write(fd, result.string(), result.size()); |
| 1930 | } else { |
| 1931 | result.appendFormat(" Facing: %s\n", |
| 1932 | info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); |
| 1933 | result.appendFormat(" Orientation: %d\n", info.orientation); |
| 1934 | int deviceVersion; |
Yin-Chia Yeh | e074a93 | 2015-01-30 10:29:02 -0800 | [diff] [blame] | 1935 | if (common->module_api_version < CAMERA_MODULE_API_VERSION_2_0) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1936 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 1937 | } else { |
| 1938 | deviceVersion = info.device_version; |
| 1939 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1940 | |
| 1941 | auto conflicting = state.second->getConflicting(); |
| 1942 | result.appendFormat(" Resource Cost: %d\n", state.second->getCost()); |
| 1943 | result.appendFormat(" Conflicting Devices:"); |
| 1944 | for (auto& id : conflicting) { |
| 1945 | result.appendFormat(" %s", cameraId.string()); |
| 1946 | } |
| 1947 | if (conflicting.size() == 0) { |
| 1948 | result.appendFormat(" NONE"); |
| 1949 | } |
| 1950 | result.appendFormat("\n"); |
| 1951 | |
| 1952 | result.appendFormat(" Device version: %#x\n", deviceVersion); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1953 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { |
| 1954 | result.appendFormat(" Device static metadata:\n"); |
| 1955 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 428b77a | 2012-07-30 09:55:30 -0700 | [diff] [blame] | 1956 | dump_indented_camera_metadata(info.static_camera_characteristics, |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1957 | fd, /*verbosity*/2, /*indentation*/4); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1958 | } else { |
| 1959 | write(fd, result.string(), result.size()); |
| 1960 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1961 | |
| 1962 | CameraParameters p = state.second->getShimParams(); |
| 1963 | if (!p.isEmpty()) { |
| 1964 | result = String8::format(" Camera1 API shim is using parameters:\n "); |
| 1965 | write(fd, result.string(), result.size()); |
| 1966 | p.dump(fd, args); |
| 1967 | } |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1970 | auto clientDescriptor = mActiveClientManager.get(cameraId); |
| 1971 | if (clientDescriptor == nullptr) { |
| 1972 | result = String8::format(" Device %s is closed, no client instance\n", |
| 1973 | cameraId.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1974 | write(fd, result.string(), result.size()); |
| 1975 | continue; |
| 1976 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1977 | hasClient = true; |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1978 | result = String8::format(" Device %s is open. Client instance dump:\n\n", |
| 1979 | cameraId.string()); |
| 1980 | result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority()); |
| 1981 | result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId()); |
| 1982 | |
| 1983 | auto client = clientDescriptor->getValue(); |
| 1984 | result.appendFormat("Client package: %s\n", |
| 1985 | String8(client->getPackageName()).string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1986 | write(fd, result.string(), result.size()); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1987 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1988 | client->dump(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1989 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 1990 | |
| 1991 | if (stateLocked) mCameraStatesLock.unlock(); |
| 1992 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1993 | if (!hasClient) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1994 | result = String8::format("\nNo active camera clients yet.\n"); |
| 1995 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | if (locked) mServiceLock.unlock(); |
| 1999 | |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 2000 | // Dump camera traces if there were any |
| 2001 | write(fd, "\n", 1); |
| 2002 | camera3::CameraTraces::dump(fd, args); |
| 2003 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2004 | // change logging level |
| 2005 | int n = args.size(); |
| 2006 | for (int i = 0; i + 1 < n; i++) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 2007 | String16 verboseOption("-v"); |
| 2008 | if (args[i] == verboseOption) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2009 | String8 levelStr(args[i+1]); |
| 2010 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2011 | result = String8::format("\nSetting log level to %d.\n", level); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2012 | setLogLevel(level); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 2013 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | return NO_ERROR; |
| 2018 | } |
| 2019 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2020 | void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2021 | Mutex::Autolock al(mTorchClientMapMutex); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2022 | for (size_t i = 0; i < mTorchClientMap.size(); i++) { |
| 2023 | if (mTorchClientMap[i] == who) { |
| 2024 | // turn off the torch mode that was turned on by dead client |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2025 | String8 cameraId = mTorchClientMap.keyAt(i); |
| 2026 | status_t res = mFlashlight->setTorchMode(cameraId, false); |
| 2027 | if (res) { |
| 2028 | ALOGE("%s: torch client died but couldn't turn off torch: " |
| 2029 | "%s (%d)", __FUNCTION__, strerror(-res), res); |
| 2030 | return; |
| 2031 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2032 | mTorchClientMap.removeItemsAt(i); |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2033 | break; |
| 2034 | } |
| 2035 | } |
| 2036 | } |
| 2037 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2038 | /*virtual*/void CameraService::binderDied(const wp<IBinder> &who) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2039 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 2040 | /** |
| 2041 | * While tempting to promote the wp<IBinder> into a sp, |
| 2042 | * it's actually not supported by the binder driver |
| 2043 | */ |
| 2044 | |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2045 | // check torch client |
| 2046 | handleTorchClientBinderDied(who); |
| 2047 | |
| 2048 | // check camera device client |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2049 | if(!evictClientIdByRemote(who)) { |
| 2050 | ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2051 | return; |
| 2052 | } |
| 2053 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2054 | ALOGE("%s: Java client's binder died, removing it from the list of active clients", |
| 2055 | __FUNCTION__); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2058 | void CameraService::updateStatus(ICameraServiceListener::Status status, const String8& cameraId) { |
| 2059 | updateStatus(status, cameraId, {}); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 2060 | } |
| 2061 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2062 | void CameraService::updateStatus(ICameraServiceListener::Status status, const String8& cameraId, |
| 2063 | std::initializer_list<ICameraServiceListener::Status> rejectSourceStates) { |
| 2064 | // Do not lock mServiceLock here or can get into a deadlock from |
| 2065 | // connect() -> disconnect -> updateStatus |
| 2066 | |
| 2067 | auto state = getCameraState(cameraId); |
| 2068 | |
| 2069 | if (state == nullptr) { |
| 2070 | ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__, |
| 2071 | cameraId.string()); |
| 2072 | return; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2073 | } |
| 2074 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 2075 | // Update the status for this camera state, then send the onStatusChangedCallbacks to each |
| 2076 | // of the listeners with both the mStatusStatus and mStatusListenerLock held |
| 2077 | state->updateStatus(status, cameraId, rejectSourceStates, [this] |
| 2078 | (const String8& cameraId, ICameraServiceListener::Status status) { |
| 2079 | |
| 2080 | // Update torch status |
| 2081 | if (status == ICameraServiceListener::STATUS_NOT_PRESENT || |
| 2082 | status == ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
| 2083 | // Update torch status to not available when the camera device becomes not present |
| 2084 | // or not available. |
| 2085 | onTorchStatusChanged(cameraId, ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE); |
| 2086 | } else if (status == ICameraServiceListener::STATUS_PRESENT) { |
| 2087 | // Update torch status to available when the camera device becomes present or |
| 2088 | // available |
| 2089 | onTorchStatusChanged(cameraId, ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF); |
| 2090 | } |
| 2091 | |
| 2092 | Mutex::Autolock lock(mStatusListenerLock); |
| 2093 | |
| 2094 | for (auto& listener : mListenerList) { |
| 2095 | // TODO: Refactor status listeners to use strings for Camera IDs and remove this. |
| 2096 | int id = cameraIdToInt(cameraId); |
| 2097 | if (id != -1) listener->onStatusChanged(status, id); |
| 2098 | } |
| 2099 | }); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 2100 | } |
| 2101 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2102 | status_t CameraService::getTorchStatusLocked( |
| 2103 | const String8& cameraId, |
| 2104 | ICameraServiceListener::TorchStatus *status) const { |
| 2105 | if (!status) { |
| 2106 | return BAD_VALUE; |
| 2107 | } |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2108 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 2109 | if (index == NAME_NOT_FOUND) { |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2110 | // invalid camera ID or the camera doesn't have a flash unit |
| 2111 | return NAME_NOT_FOUND; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2112 | } |
| 2113 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2114 | *status = mTorchStatusMap.valueAt(index); |
| 2115 | return OK; |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2116 | } |
| 2117 | |
Chien-Yu Chen | 88da526 | 2015-02-17 13:56:46 -0800 | [diff] [blame] | 2118 | status_t CameraService::setTorchStatusLocked(const String8& cameraId, |
Chien-Yu Chen | 3068d73 | 2015-02-09 13:29:57 -0800 | [diff] [blame] | 2119 | ICameraServiceListener::TorchStatus status) { |
| 2120 | ssize_t index = mTorchStatusMap.indexOfKey(cameraId); |
| 2121 | if (index == NAME_NOT_FOUND) { |
| 2122 | return BAD_VALUE; |
| 2123 | } |
| 2124 | ICameraServiceListener::TorchStatus& item = |
| 2125 | mTorchStatusMap.editValueAt(index); |
| 2126 | item = status; |
| 2127 | |
| 2128 | return OK; |
| 2129 | } |
| 2130 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 2131 | }; // namespace android |