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