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