blob: f4395900153e4b112c2bc5d01024ebea969d8850 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
Ruben Brunkd1176ef2014-02-21 10:51:38 -08002 * 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 Agopian65ab4712010-07-14 17:59:35 -070016
17#define LOG_TAG "CameraService"
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -070018#define ATRACE_TAG ATRACE_TAG_CAMERA
Iliyan Malchev8951a972011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080021#ifdef ENABLE_TREBLE
22 #define USE_HIDL true
23#else
24 #define USE_HIDL false
25#endif
26
Ruben Brunkcc776712015-02-17 20:18:47 -080027#include <algorithm>
28#include <climits>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <stdio.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080030#include <cstring>
31#include <ctime>
32#include <string>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <sys/types.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080034#include <inttypes.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <pthread.h>
36
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080037#include <android/hardware/ICamera.h>
38#include <android/hardware/ICameraClient.h>
39
Alex Deymo9c2a2c22016-08-25 11:59:14 -070040#include <android-base/macros.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080041#include <android-base/parseint.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080042#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043#include <binder/IPCThreadState.h>
44#include <binder/IServiceManager.h>
45#include <binder/MemoryBase.h>
46#include <binder/MemoryHeapBase.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080047#include <binder/ProcessInfoService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070049#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080050#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070051#include <hardware/hardware.h>
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -070052#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070053#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080054#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070055#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070056#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070057#include <utils/Errors.h>
58#include <utils/Log.h>
59#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080060#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080061#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080062#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070063#include <system/camera_metadata.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080064
Ruben Brunkb2119af2014-05-09 19:57:56 -070065#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070066
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080067#include <android/hidl/manager/1.0/IServiceManager.h>
68#include <hidl/ServiceManagement.h>
69
Mathias Agopian65ab4712010-07-14 17:59:35 -070070#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070071#include "api1/CameraClient.h"
72#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070073#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070074#include "utils/CameraTraces.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070075
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080076namespace {
77 const char* kPermissionServiceName = "permission";
78}; // namespace anonymous
79
Mathias Agopian65ab4712010-07-14 17:59:35 -070080namespace android {
81
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080082using binder::Status;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080083using hardware::ICamera;
84using hardware::ICameraClient;
85using hardware::ICameraServiceListener;
86using hardware::camera::common::V1_0::CameraDeviceStatus;
87using hardware::camera::common::V1_0::TorchModeStatus;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080088
Mathias Agopian65ab4712010-07-14 17:59:35 -070089// ----------------------------------------------------------------------------
90// Logging support -- this is for debugging only
91// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070092volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070093
Steve Blockb8a80522011-12-20 16:23:08 +000094#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
95#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070096
97static void setLogLevel(int level) {
98 android_atomic_write(level, &gLogLevel);
99}
100
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800101// Convenience methods for constructing binder::Status objects for error returns
102
103#define STATUS_ERROR(errorCode, errorString) \
104 binder::Status::fromServiceSpecificError(errorCode, \
105 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
106
107#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
108 binder::Status::fromServiceSpecificError(errorCode, \
109 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
110 __VA_ARGS__))
111
Mathias Agopian65ab4712010-07-14 17:59:35 -0700112// ----------------------------------------------------------------------------
113
Igor Murashkincba2c162013-03-20 15:56:31 -0700114extern "C" {
115static void camera_device_status_change(
116 const struct camera_module_callbacks* callbacks,
117 int camera_id,
118 int new_status) {
119 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -0800120 static_cast<const CameraService*>(callbacks));
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800121 String8 id = String8::format("%d", camera_id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700122
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800123 CameraDeviceStatus newStatus{CameraDeviceStatus::NOT_PRESENT};
124 switch (new_status) {
125 case CAMERA_DEVICE_STATUS_NOT_PRESENT:
126 newStatus = CameraDeviceStatus::NOT_PRESENT;
127 break;
128 case CAMERA_DEVICE_STATUS_PRESENT:
129 newStatus = CameraDeviceStatus::PRESENT;
130 break;
131 case CAMERA_DEVICE_STATUS_ENUMERATING:
132 newStatus = CameraDeviceStatus::ENUMERATING;
133 break;
134 default:
135 ALOGW("Unknown device status change to %d", new_status);
136 break;
137 }
138 cs->onDeviceStatusChanged(id, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700139}
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800140
141static void torch_mode_status_change(
142 const struct camera_module_callbacks* callbacks,
143 const char* camera_id,
144 int new_status) {
145 if (!callbacks || !camera_id) {
146 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
147 callbacks, camera_id);
148 }
149 sp<CameraService> cs = const_cast<CameraService*>(
150 static_cast<const CameraService*>(callbacks));
151
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800152 TorchModeStatus status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800153 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800154 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800155 status = TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800156 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800157 case TORCH_MODE_STATUS_AVAILABLE_OFF:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800158 status = TorchModeStatus::AVAILABLE_OFF;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800159 break;
160 case TORCH_MODE_STATUS_AVAILABLE_ON:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800161 status = TorchModeStatus::AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800162 break;
163 default:
164 ALOGE("Unknown torch status %d", new_status);
165 return;
166 }
167
168 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800169 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800170 status);
171}
Igor Murashkincba2c162013-03-20 15:56:31 -0700172} // extern "C"
173
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174// ----------------------------------------------------------------------------
175
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800176CameraService::CameraService() :
177 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800178 mNumberOfCameras(0), mNumberOfNormalCameras(0),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800179 mSoundRef(0), mInitialized(false), mModule(nullptr) {
Steve Blockdf64d152012-01-04 20:05:49 +0000180 ALOGI("CameraService started (pid=%d)", getpid());
Igor Murashkinbfc99152013-02-27 12:55:20 -0800181
Igor Murashkincba2c162013-03-20 15:56:31 -0700182 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800183 this->torch_mode_status_change = android::torch_mode_status_change;
184
Ruben Brunkcc776712015-02-17 20:18:47 -0800185 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186}
187
Iliyan Malchev8951a972011-04-14 16:55:59 -0700188void CameraService::onFirstRef()
189{
Ruben Brunkcc776712015-02-17 20:18:47 -0800190 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800191
Iliyan Malchev8951a972011-04-14 16:55:59 -0700192 BnCameraService::onFirstRef();
193
Ruben Brunk99e69712015-05-26 17:25:07 -0700194 // Update battery life tracking if service is restarting
195 BatteryNotifier& notifier(BatteryNotifier::getInstance());
196 notifier.noteResetCamera();
197 notifier.noteResetFlashlight();
198
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800199 status_t res = INVALID_OPERATION;
200 if (USE_HIDL) {
201 res = enumerateProviders();
202 } else {
203 res = loadLegacyHalModule();
204 }
205 if (res == OK) {
206 mInitialized = true;
207 }
208
209 CameraService::pingCameraServiceProxy();
210}
211
212status_t CameraService::loadLegacyHalModule() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800213 camera_module_t *rawModule;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700214 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
215 (const hw_module_t **)&rawModule);
216 if (err < 0) {
217 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
218 logServiceError("Could not load camera HAL module", err);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800219 return INVALID_OPERATION;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700220 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800221
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700222 mModule = new CameraModule(rawModule);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700223 err = mModule->init();
224 if (err != OK) {
225 ALOGE("Could not initialize camera HAL module: %d (%s)", err,
226 strerror(-err));
227 logServiceError("Could not initialize camera HAL module", err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800228
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700229 delete mModule;
230 mModule = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800231 return INVALID_OPERATION;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700232 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700233 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700234
235 mNumberOfCameras = mModule->getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700236 mNumberOfNormalCameras = mNumberOfCameras;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700237
Yin-Chia Yehd4a653a2015-10-14 11:05:44 -0700238 // Setup vendor tags before we call get_camera_info the first time
239 // because HAL might need to setup static vendor keys in get_camera_info
240 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
241 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
242 setUpVendorTags();
243 }
244
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800245 mFlashlight = new CameraFlashlight(mModule, this);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700246 status_t res = mFlashlight->findFlashUnits();
247 if (res) {
248 // impossible because we haven't open any camera devices.
249 ALOGE("Failed to find flash units.");
250 }
251
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700252 int latestStrangeCameraId = INT_MAX;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700253 for (int i = 0; i < mNumberOfCameras; i++) {
254 String8 cameraId = String8::format("%d", i);
255
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700256 // Get camera info
257
258 struct camera_info info;
259 bool haveInfo = true;
260 status_t rc = mModule->getCameraInfo(i, &info);
261 if (rc != NO_ERROR) {
262 ALOGE("%s: Received error loading camera info for device %d, cost and"
263 " conflicting devices fields set to defaults for this device.",
264 __FUNCTION__, i);
265 haveInfo = false;
266 }
267
268 // Check for backwards-compatibility support
269 if (haveInfo) {
270 if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) {
271 delete mModule;
272 mModule = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800273 return INVALID_OPERATION;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700274 }
275 }
276
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700277 // Defaults to use for cost and conflicting devices
278 int cost = 100;
279 char** conflicting_devices = nullptr;
280 size_t conflicting_devices_length = 0;
281
282 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700283 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
284 cost = info.resource_cost;
285 conflicting_devices = info.conflicting_devices;
286 conflicting_devices_length = info.conflicting_devices_length;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700287 }
288
289 std::set<String8> conflicting;
290 for (size_t i = 0; i < conflicting_devices_length; i++) {
291 conflicting.emplace(String8(conflicting_devices[i]));
292 }
293
294 // Initialize state for each camera device
295 {
296 Mutex::Autolock lock(mCameraStatesLock);
297 mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost,
298 conflicting));
299 }
300
301 if (mFlashlight->hasFlashUnit(cameraId)) {
302 mTorchStatusMap.add(cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800303 TorchModeStatus::AVAILABLE_OFF);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700304 }
305 }
306
307 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
308 mModule->setCallbacks(this);
309 }
310
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800311 return OK;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700312}
313
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800314status_t CameraService::enumerateProviders() {
315 mCameraProviderManager = new CameraProviderManager();
316 status_t res;
317 res = mCameraProviderManager->initialize(this);
318 if (res != OK) {
319 ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
320 __FUNCTION__, strerror(-res), res);
321 return res;
322 }
323
324 mNumberOfCameras = mCameraProviderManager->getCameraCount();
325 mNumberOfNormalCameras = mCameraProviderManager->getStandardCameraCount();
326
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800327 // Setup vendor tags before we call get_camera_info the first time
328 // because HAL might need to setup static vendor keys in get_camera_info
329 // TODO: maybe put this into CameraProviderManager::initialize()?
330 mCameraProviderManager->setUpVendorTags();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800331
332 mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
333 res = mFlashlight->findFlashUnits();
334 if (res != OK) {
335 ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
336 }
337
338 // TODO: Verify device versions are in support
339
340 for (auto& cameraId : mCameraProviderManager->getCameraDeviceIds()) {
341 hardware::camera::common::V1_0::CameraResourceCost cost;
342 res = mCameraProviderManager->getResourceCost(cameraId, &cost);
343 if (res != OK) {
344 ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res);
345 continue;
346 }
347 std::set<String8> conflicting;
348 for (size_t i = 0; i < cost.conflictingDevices.size(); i++) {
349 conflicting.emplace(String8(cost.conflictingDevices[i].c_str()));
350 }
351 String8 id8 = String8(cameraId.c_str());
352
353 Mutex::Autolock lock(mCameraStatesLock);
354 mCameraStates.emplace(id8,
355 std::make_shared<CameraState>(id8, cost.resourceCost, conflicting));
356
357 if (mFlashlight->hasFlashUnit(id8)) {
358 mTorchStatusMap.add(id8,
359 TorchModeStatus::AVAILABLE_OFF);
360 }
361 }
362
363 return OK;
364}
365
366
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700367sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800368 sp<ICameraServiceProxy> proxyBinder = nullptr;
369#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700370 sp<IServiceManager> sm = defaultServiceManager();
Eino-Ville Talvalaf4db13d2016-12-08 11:20:51 -0800371 // Use checkService because cameraserver normally starts before the
372 // system server and the proxy service. So the long timeout that getService
373 // has before giving up is inappropriate.
374 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800375 if (binder != nullptr) {
376 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700377 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800378#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700379 return proxyBinder;
380}
381
382void CameraService::pingCameraServiceProxy() {
383 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
384 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700385 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700386}
387
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800389 if (mModule) {
390 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800391 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800392 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800393 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700394}
395
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800396void CameraService::onDeviceStatusChanged(const String8& id,
397 CameraDeviceStatus newHalStatus) {
398 ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__,
399 id.string(), newHalStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700400
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800401 StatusInternal newStatus = mapToInternal(newHalStatus);
402
Ruben Brunkcc776712015-02-17 20:18:47 -0800403 std::shared_ptr<CameraState> state = getCameraState(id);
404
405 if (state == nullptr) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800406 ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700407 return;
408 }
409
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800410 StatusInternal oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800411
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800412 if (oldStatus == newStatus) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800413 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700414 return;
415 }
416
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800417 if (newStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700418 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
419 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800420 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700421 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800422 // Don't do this in updateStatus to avoid deadlock over mServiceLock
423 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700424
Ruben Brunkcc776712015-02-17 20:18:47 -0800425 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
426 // to this device until the status changes
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800427 updateStatus(StatusInternal::NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700428
Ruben Brunkcc776712015-02-17 20:18:47 -0800429 // Remove cached shim parameters
430 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700431
Ruben Brunkcc776712015-02-17 20:18:47 -0800432 // Remove the client from the list of active clients
433 clientToDisconnect = removeClientLocked(id);
434
435 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800436 clientToDisconnect->notifyError(
437 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800438 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700439 }
440
Ruben Brunkcc776712015-02-17 20:18:47 -0800441 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
442 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700443
Ruben Brunkcc776712015-02-17 20:18:47 -0800444 // Disconnect client
445 if (clientToDisconnect.get() != nullptr) {
446 // Ensure not in binder RPC so client disconnect PID checks work correctly
447 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
448 "onDeviceStatusChanged must be called from the camera service process!");
449 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700450 }
451
Ruben Brunkcc776712015-02-17 20:18:47 -0800452 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800453 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700454 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
455 newStatus));
456 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800457 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700458 }
459
Igor Murashkincba2c162013-03-20 15:56:31 -0700460}
461
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800462void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800463 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800464 Mutex::Autolock al(mTorchStatusMutex);
465 onTorchStatusChangedLocked(cameraId, newStatus);
466}
467
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800468void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800469 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800470 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
471 __FUNCTION__, cameraId.string(), newStatus);
472
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800473 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800474 status_t res = getTorchStatusLocked(cameraId, &status);
475 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700476 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
477 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800478 return;
479 }
480 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800481 return;
482 }
483
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800484 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800485 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800486 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
487 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800488 return;
489 }
490
Ruben Brunkcc776712015-02-17 20:18:47 -0800491 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700492 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700493 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700494 auto iter = mTorchUidMap.find(cameraId);
495 if (iter != mTorchUidMap.end()) {
496 int oldUid = iter->second.second;
497 int newUid = iter->second.first;
498 BatteryNotifier& notifier(BatteryNotifier::getInstance());
499 if (oldUid != newUid) {
500 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800501 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700502 notifier.noteFlashlightOff(cameraId, oldUid);
503 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800504 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700505 notifier.noteFlashlightOn(cameraId, newUid);
506 }
507 iter->second.second = newUid;
508 } else {
509 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800510 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700511 notifier.noteFlashlightOn(cameraId, oldUid);
512 } else {
513 notifier.noteFlashlightOff(cameraId, oldUid);
514 }
515 }
516 }
517 }
518
519 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800520 Mutex::Autolock lock(mStatusListenerLock);
521 for (auto& i : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800522 i->onTorchStatusChanged(mapToInterface(newStatus), String16{cameraId});
Ruben Brunkcc776712015-02-17 20:18:47 -0800523 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800524 }
525}
526
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800527Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700528 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700529 switch (type) {
530 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 *numCameras = mNumberOfNormalCameras;
532 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700533 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800534 *numCameras = mNumberOfCameras;
535 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700536 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800537 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700538 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800539 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
540 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700541 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800542 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543}
544
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545Status CameraService::getCameraInfo(int cameraId,
546 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700547 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800548 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800549 return STATUS_ERROR(ERROR_DISCONNECTED,
550 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700551 }
552
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800554 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
555 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556 }
557
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800558 Status ret = Status::ok();
559 if (mModule != nullptr) {
560 struct camera_info info;
561 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800562
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800563 if (ret.isOk()) {
564 cameraInfo->facing = info.facing;
565 cameraInfo->orientation = info.orientation;
566 // CameraInfo is for android.hardware.Camera which does not
567 // support external camera facing. The closest approximation would be
568 // front camera.
569 if (cameraInfo->facing == CAMERA_FACING_EXTERNAL) {
570 cameraInfo->facing = hardware::CAMERA_FACING_FRONT;
571 }
Yin-Chia Yeh9b5a6e92016-04-22 13:24:05 -0700572 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800573 } else {
574 status_t err = mCameraProviderManager->getCameraInfo(std::to_string(cameraId), cameraInfo);
575 if (err != OK) {
576 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
577 "Error retrieving camera info from device %d: %s (%d)", cameraId,
578 strerror(-err), err);
579 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800580 }
581 return ret;
582}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700583
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800584int CameraService::cameraIdToInt(const String8& cameraId) {
585 int id;
586 bool success = base::ParseInt(cameraId.string(), &id, 0);
587 if (!success) {
588 return -1;
589 }
590 return id;
591}
592
593Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800594 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700595 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700596 if (!cameraInfo) {
597 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800598 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700599 }
600
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800601 if (!mInitialized) {
602 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800603 return STATUS_ERROR(ERROR_DISCONNECTED,
604 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700605 }
606
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800607 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800608
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800609 if (mModule != nullptr) {
610 int id = cameraIdToInt(String8(cameraId));
Zhijun He2b59be82013-09-25 10:14:30 -0700611
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800612 if (id < 0 || id >= mNumberOfCameras) {
613 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, id);
614 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
615 "Invalid camera id: %d", id);
616 }
617
618 int version = getDeviceVersion(String8(cameraId));
619 if (version < CAMERA_DEVICE_API_VERSION_3_0) {
620 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Can't get camera characteristics"
621 " for devices with HAL version < 3.0, %d is version %x", id, version);
622 }
623
624 struct camera_info info;
625 ret = filterGetInfoErrorCode(mModule->getCameraInfo(id, &info));
626 if (ret.isOk()) {
627 *cameraInfo = info.static_camera_characteristics;
628 }
629 } else {
630 status_t res = mCameraProviderManager->getCameraCharacteristics(
631 String16::std_string(cameraId), cameraInfo);
632 if (res != OK) {
633 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
634 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
635 strerror(-res), res);
636 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700637 }
Zhijun He2b59be82013-09-25 10:14:30 -0700638
639 return ret;
640}
641
Ruben Brunkcc776712015-02-17 20:18:47 -0800642int CameraService::getCallingPid() {
643 return IPCThreadState::self()->getCallingPid();
644}
645
646int CameraService::getCallingUid() {
647 return IPCThreadState::self()->getCallingUid();
648}
649
650String8 CameraService::getFormattedCurrentTime() {
651 time_t now = time(nullptr);
652 char formattedTime[64];
653 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
654 return String8(formattedTime);
655}
656
657int CameraService::getCameraPriorityFromProcState(int procState) {
658 // Find the priority for the camera usage based on the process state. Higher priority clients
659 // win for evictions.
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700660 if (procState < 0) {
661 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
662 procState);
663 return -1;
Ruben Brunkcc776712015-02-17 20:18:47 -0800664 }
Eino-Ville Talvala52aad852015-09-03 12:24:24 -0700665 // Treat sleeping TOP processes the same as regular TOP processes, for
666 // access priority. This is important for lock-screen camera launch scenarios
667 if (procState == PROCESS_STATE_TOP_SLEEPING) {
668 procState = PROCESS_STATE_TOP;
669 }
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700670 return INT_MAX - procState;
Ruben Brunkcc776712015-02-17 20:18:47 -0800671}
672
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800673Status CameraService::getCameraVendorTagDescriptor(
674 /*out*/
675 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700676 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800677 if (!mInitialized) {
678 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800679 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800680 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800681 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
682 if (globalDescriptor != nullptr) {
683 *desc = *(globalDescriptor.get());
684 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800685 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800686}
687
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800688int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700689 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800690 struct camera_info info;
Igor Murashkin634a5152013-02-20 17:15:11 -0800691
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800692 int deviceVersion = 0;
693
694 if (mModule != nullptr) {
695 int id = cameraIdToInt(cameraId);
696 if (id < 0) return -1;
697
698 if (mModule->getCameraInfo(id, &info) != OK) {
699 return -1;
700 }
701
702 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
703 deviceVersion = info.device_version;
704 } else {
705 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
706 }
707
708 if (facing) {
709 *facing = info.facing;
710 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800711 } else {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800712 status_t res;
713 hardware::hidl_version maxVersion{0,0};
714 res = mCameraProviderManager->getHighestSupportedVersion(String8::std_string(cameraId),
715 &maxVersion);
716 if (res == NAME_NOT_FOUND) return -1;
717 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Igor Murashkin634a5152013-02-20 17:15:11 -0800718 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800719 return deviceVersion;
720}
721
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800722Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700723 switch(err) {
724 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800725 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800726 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800727 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
728 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800729 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800730 return STATUS_ERROR(ERROR_DISCONNECTED,
731 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700732 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800733 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
734 "Camera HAL encountered error %d: %s",
735 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700736 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800737}
738
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800739bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700740 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800741 if (mModule == nullptr) return false;
742
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800743 vendor_tag_ops_t vOps = vendor_tag_ops_t();
744
745 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800746 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800747 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
748 return false;
749 }
750
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800751 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800752
753 // Ensure all vendor operations are present
754 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
755 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
756 vOps.get_tag_type == NULL) {
757 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
758 , __FUNCTION__);
759 return false;
760 }
761
762 // Read all vendor tag definitions into a descriptor
763 sp<VendorTagDescriptor> desc;
764 status_t res;
765 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
766 != OK) {
767 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
768 "received error %s (%d). Camera clients will not be able to use"
769 "vendor tags", __FUNCTION__, strerror(res), res);
770 return false;
771 }
772
773 // Set the global descriptor to use with camera metadata
774 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
775 return true;
776}
777
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800778Status CameraService::makeClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800779 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800780 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
781 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
782 /*out*/sp<BasicClient>* client) {
783
Ruben Brunkcc776712015-02-17 20:18:47 -0800784 if (halVersion < 0 || halVersion == deviceVersion) {
785 // Default path: HAL version is unspecified by caller, create CameraClient
786 // based on device version reported by the HAL.
787 switch(deviceVersion) {
788 case CAMERA_DEVICE_API_VERSION_1_0:
789 if (effectiveApiLevel == API_1) { // Camera1 API route
790 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800791 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
792 facing, clientPid, clientUid, getpid(), legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800793 } else { // Camera2 API route
794 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800795 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800796 "Camera device \"%s\" HAL version %d does not support camera2 API",
797 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800798 }
799 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800800 case CAMERA_DEVICE_API_VERSION_3_0:
801 case CAMERA_DEVICE_API_VERSION_3_1:
802 case CAMERA_DEVICE_API_VERSION_3_2:
803 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700804 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800805 if (effectiveApiLevel == API_1) { // Camera1 API route
806 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800807 *client = new Camera2Client(cameraService, tmp, packageName, cameraIdToInt(cameraId),
808 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800809 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800810 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
811 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
812 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800813 facing, clientPid, clientUid, servicePid);
814 }
815 break;
816 default:
817 // Should not be reachable
818 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800819 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800820 "Camera device \"%s\" has unknown HAL version %d",
821 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800822 }
823 } else {
824 // A particular HAL version is requested by caller. Create CameraClient
825 // based on the requested HAL version.
826 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
827 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
828 // Only support higher HAL version device opened as HAL1.0 device.
829 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800830 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
831 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800832 } else {
833 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
834 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
835 " opened as HAL %x device", halVersion, deviceVersion,
836 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800837 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800838 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
839 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800840 }
841 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800842 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800843}
844
Ruben Brunk6267b532015-04-30 17:44:07 -0700845String8 CameraService::toString(std::set<userid_t> intSet) {
846 String8 s("");
847 bool first = true;
848 for (userid_t i : intSet) {
849 if (first) {
850 s.appendFormat("%d", i);
851 first = false;
852 } else {
853 s.appendFormat(", %d", i);
854 }
855 }
856 return s;
857}
858
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800859int32_t CameraService::mapToInterface(TorchModeStatus status) {
860 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
861 switch (status) {
862 case TorchModeStatus::NOT_AVAILABLE:
863 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
864 break;
865 case TorchModeStatus::AVAILABLE_OFF:
866 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
867 break;
868 case TorchModeStatus::AVAILABLE_ON:
869 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
870 break;
871 default:
872 ALOGW("Unknown new flash status: %d", status);
873 }
874 return serviceStatus;
875}
876
877CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
878 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
879 switch (status) {
880 case CameraDeviceStatus::NOT_PRESENT:
881 serviceStatus = StatusInternal::NOT_PRESENT;
882 break;
883 case CameraDeviceStatus::PRESENT:
884 serviceStatus = StatusInternal::PRESENT;
885 break;
886 case CameraDeviceStatus::ENUMERATING:
887 serviceStatus = StatusInternal::ENUMERATING;
888 break;
889 default:
890 ALOGW("Unknown new HAL device status: %d", status);
891 }
892 return serviceStatus;
893}
894
895int32_t CameraService::mapToInterface(StatusInternal status) {
896 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
897 switch (status) {
898 case StatusInternal::NOT_PRESENT:
899 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
900 break;
901 case StatusInternal::PRESENT:
902 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
903 break;
904 case StatusInternal::ENUMERATING:
905 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
906 break;
907 case StatusInternal::NOT_AVAILABLE:
908 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
909 break;
910 case StatusInternal::UNKNOWN:
911 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
912 break;
913 default:
914 ALOGW("Unknown new internal device status: %d", status);
915 }
916 return serviceStatus;
917}
918
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800919Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800920 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700921
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800922 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800923 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800924 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800925 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800926 if (!(ret = connectHelper<ICameraClient,Client>(
927 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
928 internalPackageName, uid, USE_CALLING_PID,
929 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
930 /*out*/ tmp)
931 ).isOk()) {
932 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700933 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800934 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700935}
936
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800937Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700938 /*out*/
939 CameraParameters* parameters) {
940
941 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
942
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800943 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700944
945 if (parameters == NULL) {
946 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800947 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700948 }
949
Ruben Brunkcc776712015-02-17 20:18:47 -0800950 String8 id = String8::format("%d", cameraId);
951
952 // Check if we already have parameters
953 {
954 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700955 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800956 auto cameraState = getCameraState(id);
957 if (cameraState == nullptr) {
958 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800959 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
960 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800961 }
962 CameraParameters p = cameraState->getShimParams();
963 if (!p.isEmpty()) {
964 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800965 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700966 }
967 }
968
Ruben Brunkcc776712015-02-17 20:18:47 -0800969 int64_t token = IPCThreadState::self()->clearCallingIdentity();
970 ret = initializeShimMetadata(cameraId);
971 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800972 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800973 // Error already logged by callee
974 return ret;
975 }
976
977 // Check for parameters again
978 {
979 // Scope for service lock
980 Mutex::Autolock lock(mServiceLock);
981 auto cameraState = getCameraState(id);
982 if (cameraState == nullptr) {
983 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800984 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
985 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700986 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800987 CameraParameters p = cameraState->getShimParams();
988 if (!p.isEmpty()) {
989 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800990 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700991 }
992 }
993
Ruben Brunkcc776712015-02-17 20:18:47 -0800994 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
995 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800996 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700997}
998
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800999// Can camera service trust the caller based on the calling UID?
1000static bool isTrustedCallingUid(uid_t uid) {
1001 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -07001002 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001003 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -07001004 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001005 return true;
1006 default:
1007 return false;
1008 }
1009}
1010
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001011Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001012 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
1013 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -05001014
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001015#ifdef __BRILLO__
1016 UNUSED(clientName8);
1017 UNUSED(clientUid);
1018 UNUSED(clientPid);
1019 UNUSED(originalClientPid);
1020#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001021 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
1022 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001023 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -08001024 return allowed;
1025 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001026#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -08001027
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001028 int callingPid = getCallingPid();
1029
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001030 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001031 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
1032 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001033 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1034 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -07001035 }
1036
Ruben Brunkcc776712015-02-17 20:18:47 -08001037 if (getCameraState(cameraId) == nullptr) {
1038 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1039 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001040 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1041 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042 }
1043
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001044 status_t err = checkIfDeviceIsUsable(cameraId);
1045 if (err != NO_ERROR) {
1046 switch(err) {
1047 case -ENODEV:
1048 case -EBUSY:
1049 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1050 "No camera device with ID \"%s\" currently available", cameraId.string());
1051 default:
1052 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1053 "Unknown error connecting to ID \"%s\"", cameraId.string());
1054 }
1055 }
1056 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001057}
1058
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001059Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001060 const String8& clientName8, int& clientUid, int& clientPid,
1061 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001062 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001063 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001065 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001067 clientUid = callingUid;
1068 } else if (!isTrustedCallingUid(callingUid)) {
1069 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1070 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001071 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1072 "Untrusted caller (calling PID %d, UID %d) trying to "
1073 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1074 callingPid, callingUid, cameraId.string(),
1075 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001076 }
1077
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001078 // Check if we can trust clientPid
1079 if (clientPid == USE_CALLING_PID) {
1080 clientPid = callingPid;
1081 } else if (!isTrustedCallingUid(callingUid)) {
1082 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1083 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001084 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1085 "Untrusted caller (calling PID %d, UID %d) trying to "
1086 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1087 callingPid, callingUid, cameraId.string(),
1088 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001089 }
1090
1091 // If it's not calling from cameraserver, check the permission.
1092 if (callingPid != getpid() &&
1093 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
1094 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001095 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1096 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1097 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001098 }
1099
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001100 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1101 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001102 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001103 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104
Ruben Brunk6267b532015-04-30 17:44:07 -07001105 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001106
Ruben Brunka8ca9152015-04-07 14:23:40 -07001107 // Only allow clients who are being used by the current foreground device user, unless calling
1108 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001109 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1110 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1111 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1112 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001113 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1114 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1115 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001116 }
1117
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001118 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001119}
1120
1121status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1122 auto cameraState = getCameraState(cameraId);
1123 int callingPid = getCallingPid();
1124 if (cameraState == nullptr) {
1125 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1126 cameraId.string());
1127 return -ENODEV;
1128 }
1129
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001130 StatusInternal currentStatus = cameraState->getStatus();
1131 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001132 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1133 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001134 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001135 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001136 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1137 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001138 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001139 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001140
Ruben Brunkcc776712015-02-17 20:18:47 -08001141 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001142}
1143
Ruben Brunkcc776712015-02-17 20:18:47 -08001144void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1145 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001146
Ruben Brunkcc776712015-02-17 20:18:47 -08001147 // Make a descriptor for the incoming client
1148 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1149 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1150
1151 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1152 String8(client->getPackageName()));
1153
1154 if (evicted.size() > 0) {
1155 // This should never happen - clients should already have been removed in disconnect
1156 for (auto& i : evicted) {
1157 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1158 __FUNCTION__, i->getKey().string());
1159 }
1160
1161 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1162 __FUNCTION__);
1163 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001164
1165 // And register a death notification for the client callback. Do
1166 // this last to avoid Binder policy where a nested Binder
1167 // transaction might be pre-empted to service the client death
1168 // notification if the client process dies before linkToDeath is
1169 // invoked.
1170 sp<IBinder> remoteCallback = client->getRemote();
1171 if (remoteCallback != nullptr) {
1172 remoteCallback->linkToDeath(this);
1173 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001174}
1175
1176status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1177 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1178 /*out*/
1179 sp<BasicClient>* client,
1180 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001181 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001182 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001183 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001184 DescriptorPtr clientDescriptor;
1185 {
1186 if (effectiveApiLevel == API_1) {
1187 // If we are using API1, any existing client for this camera ID with the same remote
1188 // should be returned rather than evicted to allow MediaRecorder to work properly.
1189
1190 auto current = mActiveClientManager.get(cameraId);
1191 if (current != nullptr) {
1192 auto clientSp = current->getValue();
1193 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001194 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1195 ALOGW("CameraService connect called from same client, but with a different"
1196 " API level, evicting prior client...");
1197 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001198 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001199 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001200 *client = clientSp;
1201 return NO_ERROR;
1202 }
1203 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001204 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001205 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001206
Ruben Brunkcc776712015-02-17 20:18:47 -08001207 // Get current active client PIDs
1208 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1209 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001210
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001211 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1212 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1213 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1214 // not have an out-of-class definition.
1215 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001216
Ruben Brunkcc776712015-02-17 20:18:47 -08001217 // Get priorites of all active PIDs
1218 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1219 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001220
Ruben Brunkcc776712015-02-17 20:18:47 -08001221 // Update all active clients' priorities
1222 std::map<int,int> pidToPriorityMap;
1223 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1224 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1225 }
1226 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001227
Ruben Brunkcc776712015-02-17 20:18:47 -08001228 // Get state for the given cameraId
1229 auto state = getCameraState(cameraId);
1230 if (state == nullptr) {
1231 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1232 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001233 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001234 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001235 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001236
1237 // Make descriptor for incoming client
1238 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1239 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1240 state->getConflicting(),
1241 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1242
1243 // Find clients that would be evicted
1244 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1245
1246 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1247 // background, so we cannot do evictions
1248 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1249 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1250 " priority).", clientPid);
1251
1252 sp<BasicClient> clientSp = clientDescriptor->getValue();
1253 String8 curTime = getFormattedCurrentTime();
1254 auto incompatibleClients =
1255 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1256
1257 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001258 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001259 cameraId.string(), packageName.string(), clientPid,
1260 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1261
1262 for (auto& i : incompatibleClients) {
1263 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1264 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1265 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1266 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001267 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1268 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1269 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1270 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001271 }
1272
1273 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001274 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001275 mEventLog.add(msg);
1276
1277 return -EBUSY;
1278 }
1279
1280 for (auto& i : evicted) {
1281 sp<BasicClient> clientSp = i->getValue();
1282 if (clientSp.get() == nullptr) {
1283 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1284
1285 // TODO: Remove this
1286 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1287 __FUNCTION__);
1288 mActiveClientManager.remove(i);
1289 continue;
1290 }
1291
1292 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1293 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001294 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001295
Ruben Brunkcc776712015-02-17 20:18:47 -08001296 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001297 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1298 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1299 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001300 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1301 i->getOwnerId(), i->getPriority(), cameraId.string(),
1302 packageName.string(), clientPid,
1303 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1304
1305 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001306 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001307 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001308 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001309 }
1310
Ruben Brunkcc776712015-02-17 20:18:47 -08001311 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1312 // other clients from connecting in mServiceLockWrapper if held
1313 mServiceLock.unlock();
1314
1315 // Clear caller identity temporarily so client disconnect PID checks work correctly
1316 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1317
1318 // Destroy evicted clients
1319 for (auto& i : evictedClients) {
1320 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001321 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001322 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001323
1324 IPCThreadState::self()->restoreCallingIdentity(token);
1325
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001326 for (const auto& i : evictedClients) {
1327 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1328 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1329 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1330 if (ret == TIMED_OUT) {
1331 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1332 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1333 mActiveClientManager.toString().string());
1334 return -EBUSY;
1335 }
1336 if (ret != NO_ERROR) {
1337 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1338 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1339 ret, mActiveClientManager.toString().string());
1340 return ret;
1341 }
1342 }
1343
1344 evictedClients.clear();
1345
Ruben Brunkcc776712015-02-17 20:18:47 -08001346 // Once clients have been disconnected, relock
1347 mServiceLock.lock();
1348
1349 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1350 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1351 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001352 }
1353
Ruben Brunkcc776712015-02-17 20:18:47 -08001354 *partial = clientDescriptor;
1355 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001356}
1357
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001358Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001359 const sp<ICameraClient>& cameraClient,
1360 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001361 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001362 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001363 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001364 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001365 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001366
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001367 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001368 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001369 String8 id = String8::format("%d", cameraId);
1370 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001371 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1372 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1373 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1374 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001376 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001377 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001378 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001379 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380 }
1381
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001382 *device = client;
1383 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384}
1385
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001386Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001387 const sp<ICameraClient>& cameraClient,
1388 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001389 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001390 int clientUid,
1391 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001392 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001393
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001394 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001395 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001396 if (mModule != nullptr) {
1397 int apiVersion = mModule->getModuleApiVersion();
1398 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
1399 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
1400 /*
1401 * Either the HAL version is unspecified in which case this just creates
1402 * a camera client selected by the latest device version, or
1403 * it's a particular version in which case the HAL must supported
1404 * the open_legacy call
1405 */
1406 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1407 apiVersion);
1408 ALOGE("%s: %s",
1409 __FUNCTION__, msg.string());
1410 logRejected(id, getCallingPid(), String8(clientPackageName),
1411 msg);
1412 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
1413 }
Zhijun Heb10cdad2014-06-16 16:38:35 -07001414 }
1415
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001416 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001417 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001418 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1419 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1420 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1421 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001422
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001424 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001425 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001426 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001427 }
1428
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001429 *device = client;
1430 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001431}
1432
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001433Status CameraService::connectDevice(
1434 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001435 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001436 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001437 int clientUid,
1438 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001439 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001440
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001441 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001443 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001444 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001445 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1446 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1447 clientUid, USE_CALLING_PID, API_2,
1448 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1449 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001450
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001451 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001452 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001453 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001454 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001455 }
1456
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001457 *device = client;
1458 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001459}
1460
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001461template<class CALLBACK, class CLIENT>
1462Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
1463 int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
1464 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
1465 /*out*/sp<CLIENT>& device) {
1466 binder::Status ret = binder::Status::ok();
1467
1468 String8 clientName8(clientPackageName);
1469
1470 int originalClientPid = 0;
1471
1472 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1473 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1474 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1475 static_cast<int>(effectiveApiLevel));
1476
1477 sp<CLIENT> client = nullptr;
1478 {
1479 // Acquire mServiceLock and prevent other clients from connecting
1480 std::unique_ptr<AutoConditionLock> lock =
1481 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1482
1483 if (lock == nullptr) {
1484 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1485 , clientPid);
1486 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1487 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1488 cameraId.string(), clientName8.string(), clientPid);
1489 }
1490
1491 // Enforce client permissions and do basic sanity checks
1492 if(!(ret = validateConnectLocked(cameraId, clientName8,
1493 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1494 return ret;
1495 }
1496
1497 // Check the shim parameters after acquiring lock, if they have already been updated and
1498 // we were doing a shim update, return immediately
1499 if (shimUpdateOnly) {
1500 auto cameraState = getCameraState(cameraId);
1501 if (cameraState != nullptr) {
1502 if (!cameraState->getShimParams().isEmpty()) return ret;
1503 }
1504 }
1505
1506 status_t err;
1507
1508 sp<BasicClient> clientTmp = nullptr;
1509 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1510 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1511 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1512 /*out*/&partial)) != NO_ERROR) {
1513 switch (err) {
1514 case -ENODEV:
1515 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1516 "No camera device with ID \"%s\" currently available",
1517 cameraId.string());
1518 case -EBUSY:
1519 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1520 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1521 cameraId.string());
1522 default:
1523 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1524 "Unexpected error %s (%d) opening camera \"%s\"",
1525 strerror(-err), err, cameraId.string());
1526 }
1527 }
1528
1529 if (clientTmp.get() != nullptr) {
1530 // Handle special case for API1 MediaRecorder where the existing client is returned
1531 device = static_cast<CLIENT*>(clientTmp.get());
1532 return ret;
1533 }
1534
1535 // give flashlight a chance to close devices if necessary.
1536 mFlashlight->prepareDeviceOpen(cameraId);
1537
1538 // TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs
1539 int id = cameraIdToInt(cameraId);
1540 if (id == -1) {
1541 ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__,
1542 cameraId.string());
1543 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1544 "Bad camera ID \"%s\" passed to camera open", cameraId.string());
1545 }
1546
1547 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001548 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001549 sp<BasicClient> tmp = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001550 if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001551 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
1552 /*out*/&tmp)).isOk()) {
1553 return ret;
1554 }
1555 client = static_cast<CLIENT*>(tmp.get());
1556
1557 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1558 __FUNCTION__);
1559
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001560 if (mModule != nullptr) {
1561 err = client->initialize(mModule);
1562 } else {
1563 err = client->initialize(mCameraProviderManager);
1564 }
1565
1566 if (err != OK) {
1567 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001568 // Errors could be from the HAL module open call or from AppOpsManager
1569 switch(err) {
1570 case BAD_VALUE:
1571 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1572 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1573 case -EBUSY:
1574 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1575 "Camera \"%s\" is already open", cameraId.string());
1576 case -EUSERS:
1577 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1578 "Too many cameras already open, cannot open camera \"%s\"",
1579 cameraId.string());
1580 case PERMISSION_DENIED:
1581 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1582 "No permission to open camera \"%s\"", cameraId.string());
1583 case -EACCES:
1584 return STATUS_ERROR_FMT(ERROR_DISABLED,
1585 "Camera \"%s\" disabled by policy", cameraId.string());
1586 case -ENODEV:
1587 default:
1588 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1589 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1590 strerror(-err), err);
1591 }
1592 }
1593
1594 // Update shim paremeters for legacy clients
1595 if (effectiveApiLevel == API_1) {
1596 // Assume we have always received a Client subclass for API1
1597 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1598 String8 rawParams = shimClient->getParameters();
1599 CameraParameters params(rawParams);
1600
1601 auto cameraState = getCameraState(cameraId);
1602 if (cameraState != nullptr) {
1603 cameraState->setShimParams(params);
1604 } else {
1605 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1606 __FUNCTION__, cameraId.string());
1607 }
1608 }
1609
1610 if (shimUpdateOnly) {
1611 // If only updating legacy shim parameters, immediately disconnect client
1612 mServiceLock.unlock();
1613 client->disconnect();
1614 mServiceLock.lock();
1615 } else {
1616 // Otherwise, add client to active clients list
1617 finishConnectLocked(client, partial);
1618 }
1619 } // lock is destroyed, allow further connect calls
1620
1621 // Important: release the mutex here so the client can call back into the service from its
1622 // destructor (can be at the end of the call)
1623 device = client;
1624 return ret;
1625}
1626
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001627Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001628 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001629 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001630
1631 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001632 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001633 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001634 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1635 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001636 }
1637
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001638 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001639 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001640
1641 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001642 auto state = getCameraState(id);
1643 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001644 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001645 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1646 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001647 }
1648
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001649 StatusInternal cameraStatus = state->getStatus();
1650 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001651 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1652 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001653 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1654 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001655 }
1656
1657 {
1658 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001659 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001660 status_t err = getTorchStatusLocked(id, &status);
1661 if (err != OK) {
1662 if (err == NAME_NOT_FOUND) {
1663 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1664 "Camera \"%s\" does not have a flash unit", id.string());
1665 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001666 ALOGE("%s: getting current torch status failed for camera %s",
1667 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001668 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1669 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1670 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001671 }
1672
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001673 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001674 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001675 ALOGE("%s: torch mode of camera %s is not available because "
1676 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001677 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1678 "Torch for camera \"%s\" is not available due to an existing camera user",
1679 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001680 } else {
1681 ALOGE("%s: torch mode of camera %s is not available due to "
1682 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001683 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1684 "Torch for camera \"%s\" is not available due to insufficient resources",
1685 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001686 }
1687 }
1688 }
1689
Ruben Brunk99e69712015-05-26 17:25:07 -07001690 {
1691 // Update UID map - this is used in the torch status changed callbacks, so must be done
1692 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001693 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001694 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1695 mTorchUidMap[id].first = uid;
1696 mTorchUidMap[id].second = uid;
1697 } else {
1698 // Set the pending UID
1699 mTorchUidMap[id].first = uid;
1700 }
1701 }
1702
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001703 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001704
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001705 if (err != OK) {
1706 int32_t errorCode;
1707 String8 msg;
1708 switch (err) {
1709 case -ENOSYS:
1710 msg = String8::format("Camera \"%s\" has no flashlight",
1711 id.string());
1712 errorCode = ERROR_ILLEGAL_ARGUMENT;
1713 break;
1714 default:
1715 msg = String8::format(
1716 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1717 id.string(), enabled, strerror(-err), err);
1718 errorCode = ERROR_INVALID_OPERATION;
1719 }
1720 ALOGE("%s: %s", __FUNCTION__, msg.string());
1721 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001722 }
1723
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001724 {
1725 // update the link to client's death
1726 Mutex::Autolock al(mTorchClientMapMutex);
1727 ssize_t index = mTorchClientMap.indexOfKey(id);
1728 if (enabled) {
1729 if (index == NAME_NOT_FOUND) {
1730 mTorchClientMap.add(id, clientBinder);
1731 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001732 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001733 mTorchClientMap.replaceValueAt(index, clientBinder);
1734 }
1735 clientBinder->linkToDeath(this);
1736 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001737 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001738 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001739 }
1740
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001741 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001742}
1743
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001744Status CameraService::notifySystemEvent(int32_t eventId,
1745 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001746 ATRACE_CALL();
1747
Ruben Brunk36597b22015-03-20 22:15:57 -07001748 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001749 case ICameraService::EVENT_USER_SWITCHED: {
1750 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001751 break;
1752 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001753 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001754 default: {
1755 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1756 eventId);
1757 break;
1758 }
1759 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001760 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001761}
1762
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001763Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1764 /*out*/
1765 std::vector<hardware::CameraStatus> *cameraStatuses) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001766 ATRACE_CALL();
1767
Igor Murashkinbfc99152013-02-27 12:55:20 -08001768 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001769
Ruben Brunk3450ba72015-06-16 11:00:37 -07001770 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001771 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001772 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001773 }
1774
Igor Murashkinbfc99152013-02-27 12:55:20 -08001775 Mutex::Autolock lock(mServiceLock);
1776
Ruben Brunkcc776712015-02-17 20:18:47 -08001777 {
1778 Mutex::Autolock lock(mStatusListenerLock);
1779 for (auto& it : mListenerList) {
1780 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1781 ALOGW("%s: Tried to add listener %p which was already subscribed",
1782 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001783 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001784 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001785 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001786
1787 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001788 }
1789
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001790 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001791 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001792 Mutex::Autolock lock(mCameraStatesLock);
1793 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001794 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001795 }
1796 }
1797
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001798 /*
1799 * Immediately signal current torch status to this listener only
1800 * This may be a subset of all the devices, so don't include it in the response directly
1801 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001802 {
1803 Mutex::Autolock al(mTorchStatusMutex);
1804 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001805 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001806 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001807 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001808 }
1809
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001810 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001811}
Ruben Brunkcc776712015-02-17 20:18:47 -08001812
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001813Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001814 ATRACE_CALL();
1815
Igor Murashkinbfc99152013-02-27 12:55:20 -08001816 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1817
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001818 if (listener == 0) {
1819 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001820 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001821 }
1822
Igor Murashkinbfc99152013-02-27 12:55:20 -08001823 Mutex::Autolock lock(mServiceLock);
1824
Ruben Brunkcc776712015-02-17 20:18:47 -08001825 {
1826 Mutex::Autolock lock(mStatusListenerLock);
1827 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1828 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1829 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001830 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001831 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001832 }
1833 }
1834
1835 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1836 __FUNCTION__, listener.get());
1837
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001838 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001839}
1840
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001841Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001842
1843 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001844 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1845
1846 if (parameters == NULL) {
1847 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001848 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001849 }
1850
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001851 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001852
1853 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001854 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001855 // Error logged by caller
1856 return ret;
1857 }
1858
1859 String8 shimParamsString8 = shimParams.flatten();
1860 String16 shimParamsString16 = String16(shimParamsString8);
1861
1862 *parameters = shimParamsString16;
1863
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001864 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001865}
1866
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001867Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
1868 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001869 ATRACE_CALL();
1870
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001871 const String8 id = String8(cameraId);
1872
1873 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001874
1875 switch (apiVersion) {
1876 case API_VERSION_1:
1877 case API_VERSION_2:
1878 break;
1879 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001880 String8 msg = String8::format("Unknown API version %d", apiVersion);
1881 ALOGE("%s: %s", __FUNCTION__, msg.string());
1882 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001883 }
1884
1885 int facing = -1;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001886
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001887 int deviceVersion = getDeviceVersion(id, &facing);
Igor Murashkin65d14b92014-06-17 12:03:20 -07001888
1889 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001890 case CAMERA_DEVICE_API_VERSION_1_0:
1891 case CAMERA_DEVICE_API_VERSION_3_0:
1892 case CAMERA_DEVICE_API_VERSION_3_1:
1893 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001894 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
1895 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001896 *isSupported = false;
1897 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001898 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
1899 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001900 *isSupported = true;
1901 }
1902 break;
1903 case CAMERA_DEVICE_API_VERSION_3_2:
1904 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001905 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001906 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
1907 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001908 *isSupported = true;
1909 break;
1910 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001911 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001912 ALOGE("%s: %s", __FUNCTION__, msg.string());
1913 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001914 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001915 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001916 String8 msg = String8::format("Unknown device version %x for device %s",
1917 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001918 ALOGE("%s: %s", __FUNCTION__, msg.string());
1919 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1920 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001921 }
1922
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001923 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001924}
1925
Ruben Brunkcc776712015-02-17 20:18:47 -08001926void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001927 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001928 for (auto& i : mActiveClientManager.getAll()) {
1929 auto clientSp = i->getValue();
1930 if (clientSp.get() == client) {
1931 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001932 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001933 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001934}
1935
Ruben Brunkcc776712015-02-17 20:18:47 -08001936bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1937 const int callingPid = getCallingPid();
1938 const int servicePid = getpid();
1939 bool ret = false;
1940 {
1941 // Acquire mServiceLock and prevent other clients from connecting
1942 std::unique_ptr<AutoConditionLock> lock =
1943 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001944
Igor Murashkin634a5152013-02-20 17:15:11 -08001945
Ruben Brunkcc776712015-02-17 20:18:47 -08001946 std::vector<sp<BasicClient>> evicted;
1947 for (auto& i : mActiveClientManager.getAll()) {
1948 auto clientSp = i->getValue();
1949 if (clientSp.get() == nullptr) {
1950 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1951 mActiveClientManager.remove(i);
1952 continue;
1953 }
1954 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1955 callingPid == clientSp->getClientPid())) {
1956 mActiveClientManager.remove(i);
1957 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001958
Ruben Brunkcc776712015-02-17 20:18:47 -08001959 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001960 clientSp->notifyError(
1961 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001962 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001963 }
1964 }
1965
Ruben Brunkcc776712015-02-17 20:18:47 -08001966 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1967 // other clients from connecting in mServiceLockWrapper if held
1968 mServiceLock.unlock();
1969
Ruben Brunk36597b22015-03-20 22:15:57 -07001970 // Do not clear caller identity, remote caller should be client proccess
1971
Ruben Brunkcc776712015-02-17 20:18:47 -08001972 for (auto& i : evicted) {
1973 if (i.get() != nullptr) {
1974 i->disconnect();
1975 ret = true;
1976 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001977 }
1978
Ruben Brunkcc776712015-02-17 20:18:47 -08001979 // Reacquire mServiceLock
1980 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001981
Ruben Brunkcc776712015-02-17 20:18:47 -08001982 } // lock is destroyed, allow further connect calls
1983
1984 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001985}
1986
Igor Murashkinecf17e82012-10-02 16:05:11 -07001987
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001988/**
1989 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001990 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001991 */
1992int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001993 if (mModule == nullptr) return NO_INIT;
1994
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001995 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1996 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1997 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1998 // Verify the device version is in the supported range
1999 switch (info.device_version) {
2000 case CAMERA_DEVICE_API_VERSION_1_0:
2001 case CAMERA_DEVICE_API_VERSION_3_0:
2002 case CAMERA_DEVICE_API_VERSION_3_1:
2003 case CAMERA_DEVICE_API_VERSION_3_2:
2004 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07002005 case CAMERA_DEVICE_API_VERSION_3_4:
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08002006 // in support
2007 break;
2008 case CAMERA_DEVICE_API_VERSION_2_0:
2009 case CAMERA_DEVICE_API_VERSION_2_1:
2010 // no longer supported
2011 default:
2012 ALOGE("%s: Device %d has HAL version %x, which is not supported",
2013 __FUNCTION__, id, info.device_version);
2014 String8 msg = String8::format(
2015 "Unsupported device HAL version %x for device %d",
2016 info.device_version, id);
2017 logServiceError(msg.string(), NO_INIT);
2018 return NO_INIT;
2019 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002020 }
2021
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002022 // Assume all devices pre-v3.3 are backward-compatible
2023 bool isBackwardCompatible = true;
2024 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
2025 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
2026 isBackwardCompatible = false;
2027 status_t res;
2028 camera_metadata_ro_entry_t caps;
2029 res = find_camera_metadata_ro_entry(
2030 info.static_camera_characteristics,
2031 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
2032 &caps);
2033 if (res != 0) {
2034 ALOGW("%s: Unable to find camera capabilities for camera device %d",
2035 __FUNCTION__, id);
2036 caps.count = 0;
2037 }
2038 for (size_t i = 0; i < caps.count; i++) {
2039 if (caps.data.u8[i] ==
2040 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
2041 isBackwardCompatible = true;
2042 break;
2043 }
2044 }
2045 }
2046
2047 if (!isBackwardCompatible) {
2048 mNumberOfNormalCameras--;
2049 *latestStrangeCameraId = id;
2050 } else {
2051 if (id > *latestStrangeCameraId) {
2052 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
2053 "This is not allowed due backward-compatibility requirements",
2054 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002055 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002056 mNumberOfCameras = 0;
2057 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002058 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002059 }
2060 }
2061 return OK;
2062}
2063
Ruben Brunkcc776712015-02-17 20:18:47 -08002064std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
2065 const String8& cameraId) const {
2066 std::shared_ptr<CameraState> state;
2067 {
2068 Mutex::Autolock lock(mCameraStatesLock);
2069 auto iter = mCameraStates.find(cameraId);
2070 if (iter != mCameraStates.end()) {
2071 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002072 }
2073 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002074 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002075}
2076
Ruben Brunkcc776712015-02-17 20:18:47 -08002077sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
2078 // Remove from active clients list
2079 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
2080 if (clientDescriptorPtr == nullptr) {
2081 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
2082 cameraId.string());
2083 return sp<BasicClient>{nullptr};
2084 }
2085
2086 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07002087}
2088
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002089void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07002090 // Acquire mServiceLock and prevent other clients from connecting
2091 std::unique_ptr<AutoConditionLock> lock =
2092 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
2093
Ruben Brunk6267b532015-04-30 17:44:07 -07002094 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002095 for (size_t i = 0; i < newUserIds.size(); i++) {
2096 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07002097 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002098 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07002099 return;
2100 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002101 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07002102 }
2103
Ruben Brunka8ca9152015-04-07 14:23:40 -07002104
Ruben Brunk6267b532015-04-30 17:44:07 -07002105 if (newAllowedUsers == mAllowedUsers) {
2106 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
2107 return;
2108 }
2109
2110 logUserSwitch(mAllowedUsers, newAllowedUsers);
2111
2112 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07002113
2114 // Current user has switched, evict all current clients.
2115 std::vector<sp<BasicClient>> evicted;
2116 for (auto& i : mActiveClientManager.getAll()) {
2117 auto clientSp = i->getValue();
2118
2119 if (clientSp.get() == nullptr) {
2120 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
2121 continue;
2122 }
2123
Ruben Brunk6267b532015-04-30 17:44:07 -07002124 // Don't evict clients that are still allowed.
2125 uid_t clientUid = clientSp->getClientUid();
2126 userid_t clientUserId = multiuser_get_user_id(clientUid);
2127 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
2128 continue;
2129 }
2130
Ruben Brunk36597b22015-03-20 22:15:57 -07002131 evicted.push_back(clientSp);
2132
2133 String8 curTime = getFormattedCurrentTime();
2134
2135 ALOGE("Evicting conflicting client for camera ID %s due to user change",
2136 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07002137
Ruben Brunk36597b22015-03-20 22:15:57 -07002138 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002139 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07002140 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07002141 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
2142 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07002143
2144 }
2145
2146 // Do not hold mServiceLock while disconnecting clients, but retain the condition
2147 // blocking other clients from connecting in mServiceLockWrapper if held.
2148 mServiceLock.unlock();
2149
2150 // Clear caller identity temporarily so client disconnect PID checks work correctly
2151 int64_t token = IPCThreadState::self()->clearCallingIdentity();
2152
2153 for (auto& i : evicted) {
2154 i->disconnect();
2155 }
2156
2157 IPCThreadState::self()->restoreCallingIdentity(token);
2158
2159 // Reacquire mServiceLock
2160 mServiceLock.lock();
2161}
Ruben Brunkcc776712015-02-17 20:18:47 -08002162
Ruben Brunka8ca9152015-04-07 14:23:40 -07002163void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002164 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07002165 Mutex::Autolock l(mLogLock);
2166 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07002167}
2168
Ruben Brunka8ca9152015-04-07 14:23:40 -07002169void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002170 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002171 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002172 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002173 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002174}
2175
2176void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002177 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002178 // Log the clients evicted
2179 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002180 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002181}
2182
2183void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002184 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002185 // Log the client rejected
2186 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002187 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002188}
2189
Ruben Brunk6267b532015-04-30 17:44:07 -07002190void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
2191 const std::set<userid_t>& newUserIds) {
2192 String8 newUsers = toString(newUserIds);
2193 String8 oldUsers = toString(oldUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07002194 // Log the new and old users
Ruben Brunk6267b532015-04-30 17:44:07 -07002195 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
2196 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002197}
2198
2199void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
2200 // Log the device removal
2201 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
2202}
2203
2204void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
2205 // Log the device removal
2206 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
2207}
2208
2209void CameraService::logClientDied(int clientPid, const char* reason) {
2210 // Log the device removal
2211 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07002212}
2213
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002214void CameraService::logServiceError(const char* msg, int errorCode) {
2215 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002216 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002217}
2218
Ruben Brunk36597b22015-03-20 22:15:57 -07002219status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
2220 uint32_t flags) {
2221
2222 const int pid = getCallingPid();
2223 const int selfPid = getpid();
2224
Mathias Agopian65ab4712010-07-14 17:59:35 -07002225 // Permission checks
2226 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002227 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07002228 if (pid != selfPid) {
2229 // Ensure we're being called by system_server, or similar process with
2230 // permissions to notify the camera service about system events
2231 if (!checkCallingPermission(
2232 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
2233 const int uid = getCallingUid();
2234 ALOGE("Permission Denial: cannot send updates to camera service about system"
2235 " events from pid=%d, uid=%d", pid, uid);
2236 return PERMISSION_DENIED;
2237 }
2238 }
2239 break;
2240 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241 }
2242
2243 return BnCameraService::onTransact(code, data, reply, flags);
2244}
2245
Mathias Agopian65ab4712010-07-14 17:59:35 -07002246// We share the media players for shutter and recording sound for all clients.
2247// A reference count is kept to determine when we will actually release the
2248// media players.
2249
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08002250MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08002252 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08002253 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254 mp->prepare();
2255 } else {
Steve Block29357bc2012-01-06 19:20:56 +00002256 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002257 return NULL;
2258 }
2259 return mp;
2260}
2261
2262void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002263 ATRACE_CALL();
2264
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265 Mutex::Autolock lock(mSoundLock);
2266 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2267 if (mSoundRef++) return;
2268
2269 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002270 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2271 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002272}
2273
2274void CameraService::releaseSound() {
2275 Mutex::Autolock lock(mSoundLock);
2276 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2277 if (--mSoundRef) return;
2278
2279 for (int i = 0; i < NUM_SOUNDS; i++) {
2280 if (mSoundPlayer[i] != 0) {
2281 mSoundPlayer[i]->disconnect();
2282 mSoundPlayer[i].clear();
2283 }
2284 }
2285}
2286
2287void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002288 ATRACE_CALL();
2289
Mathias Agopian65ab4712010-07-14 17:59:35 -07002290 LOG1("playSound(%d)", kind);
2291 Mutex::Autolock lock(mSoundLock);
2292 sp<MediaPlayer> player = mSoundPlayer[kind];
2293 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002294 player->seekTo(0);
2295 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002296 }
2297}
2298
2299// ----------------------------------------------------------------------------
2300
2301CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002302 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002303 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002304 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002305 int clientPid, uid_t clientUid,
2306 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002307 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002308 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002309 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002310 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002311 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002312 servicePid),
2313 mCameraId(CameraService::cameraIdToInt(cameraIdStr))
Igor Murashkin634a5152013-02-20 17:15:11 -08002314{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002316 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002317
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002318 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002319
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002321
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002322 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002323}
2324
Mathias Agopian65ab4712010-07-14 17:59:35 -07002325// tear down the client
2326CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002327 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002328 mDestructionStarted = true;
2329
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002330 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002331 // unconditionally disconnect. function is idempotent
2332 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002333}
2334
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002335sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2336
Igor Murashkin634a5152013-02-20 17:15:11 -08002337CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002338 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002339 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002340 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002341 int clientPid, uid_t clientUid,
2342 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002343 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2344 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2345 mServicePid(servicePid),
2346 mDisconnected(false),
2347 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002348{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002349 if (sCameraService == nullptr) {
2350 sCameraService = cameraService;
2351 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002352 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002353 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002354
2355 // In some cases the calling code has no access to the package it runs under.
2356 // For example, NDK camera API.
2357 // In this case we will get the packages for the calling UID and pick the first one
2358 // for attributing the app op. This will work correctly for runtime permissions
2359 // as for legacy apps we will toggle the app op for all packages in the UID.
2360 // The caveat is that the operation may be attributed to the wrong package and
2361 // stats based on app ops may be slightly off.
2362 if (mClientPackageName.size() <= 0) {
2363 sp<IServiceManager> sm = defaultServiceManager();
2364 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2365 if (binder == 0) {
2366 ALOGE("Cannot get permission service");
2367 // Leave mClientPackageName unchanged (empty) and the further interaction
2368 // with camera will fail in BasicClient::startCameraOps
2369 return;
2370 }
2371
2372 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2373 Vector<String16> packages;
2374
2375 permCtrl->getPackagesForUid(mClientUid, packages);
2376
2377 if (packages.isEmpty()) {
2378 ALOGE("No packages for calling UID");
2379 // Leave mClientPackageName unchanged (empty) and the further interaction
2380 // with camera will fail in BasicClient::startCameraOps
2381 return;
2382 }
2383 mClientPackageName = packages[0];
2384 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002385}
2386
2387CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002388 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002389 mDestructionStarted = true;
2390}
2391
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002392binder::Status CameraService::BasicClient::disconnect() {
2393 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002394 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002395 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002396 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002397 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002398
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002399 sCameraService->removeByClient(this);
2400 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002401 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002402
2403 sp<IBinder> remote = getRemote();
2404 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002405 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002406 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002407
2408 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002409 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002410 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2411 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2412 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002413
Igor Murashkincba2c162013-03-20 15:56:31 -07002414 // client shouldn't be able to call into us anymore
2415 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002416
2417 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002418}
2419
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002420status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2421 // No dumping of clients directly over Binder,
2422 // must go through CameraService::dump
2423 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2424 IPCThreadState::self()->getCallingUid(), NULL, 0);
2425 return OK;
2426}
2427
Ruben Brunkcc776712015-02-17 20:18:47 -08002428String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002429 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002430}
2431
2432
2433int CameraService::BasicClient::getClientPid() const {
2434 return mClientPid;
2435}
2436
Ruben Brunk6267b532015-04-30 17:44:07 -07002437uid_t CameraService::BasicClient::getClientUid() const {
2438 return mClientUid;
2439}
2440
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002441bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2442 // Defaults to API2.
2443 return level == API_2;
2444}
2445
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002446status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002447 ATRACE_CALL();
2448
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002449 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002450 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002451 mOpsCallback = new OpsCallback(this);
2452
Igor Murashkine6800ce2013-03-04 17:25:57 -08002453 {
2454 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002455 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002456 }
2457
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002458 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002459 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002460 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002461 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002462
Svetoslav28e8ef72015-05-11 19:21:31 -07002463 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002464 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2465 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002466 return PERMISSION_DENIED;
2467 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002468
Svetoslav28e8ef72015-05-11 19:21:31 -07002469 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002470 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2471 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002472 // Return the same error as for device policy manager rejection
2473 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002474 }
2475
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002476 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002477
2478 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002479 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002480
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002481 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002482 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2483 mCameraIdStr);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002484
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002485 return OK;
2486}
2487
2488status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002489 ATRACE_CALL();
2490
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002491 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002492 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002493 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002494 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002495 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002496 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002497
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002498 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
2499 StatusInternal::ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002500
Ruben Brunkcc776712015-02-17 20:18:47 -08002501 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002502 sCameraService->updateStatus(StatusInternal::PRESENT,
2503 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002504
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002505 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002506 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2507 mCameraIdStr);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002508 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002509 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002510 if (mOpsCallback != NULL) {
2511 mAppOpsManager.stopWatchingMode(mOpsCallback);
2512 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002513 mOpsCallback.clear();
2514
2515 return OK;
2516}
2517
2518void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002519 ATRACE_CALL();
2520
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002521 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002522 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002523
2524 if (op != AppOpsManager::OP_CAMERA) {
2525 ALOGW("Unexpected app ops notification received: %d", op);
2526 return;
2527 }
2528
2529 int32_t res;
2530 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002531 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002532 ALOGV("checkOp returns: %d, %s ", res,
2533 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2534 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2535 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2536 "UNKNOWN");
2537
2538 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002539 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002540 myName.string());
2541 // Reset the client PID to allow server-initiated disconnect,
2542 // and to prevent further calls by client.
2543 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002544 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002545 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002546 disconnect();
2547 }
2548}
2549
Mathias Agopian65ab4712010-07-14 17:59:35 -07002550// ----------------------------------------------------------------------------
2551
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002552void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002553 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002554 (void) errorCode;
2555 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002556 if (mRemoteCallback != NULL) {
2557 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2558 } else {
2559 ALOGE("mRemoteCallback is NULL!!");
2560 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002561}
2562
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002563// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002564binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002565 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002566 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002567}
2568
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002569bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2570 return level == API_1;
2571}
2572
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002573CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2574 mClient(client) {
2575}
2576
2577void CameraService::Client::OpsCallback::opChanged(int32_t op,
2578 const String16& packageName) {
2579 sp<BasicClient> client = mClient.promote();
2580 if (client != NULL) {
2581 client->opChanged(op, packageName);
2582 }
2583}
2584
Mathias Agopian65ab4712010-07-14 17:59:35 -07002585// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002586// CameraState
2587// ----------------------------------------------------------------------------
2588
2589CameraService::CameraState::CameraState(const String8& id, int cost,
2590 const std::set<String8>& conflicting) : mId(id),
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002591 mStatus(StatusInternal::PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002592
2593CameraService::CameraState::~CameraState() {}
2594
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002595CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002596 Mutex::Autolock lock(mStatusLock);
2597 return mStatus;
2598}
2599
2600CameraParameters CameraService::CameraState::getShimParams() const {
2601 return mShimParams;
2602}
2603
2604void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2605 mShimParams = params;
2606}
2607
2608int CameraService::CameraState::getCost() const {
2609 return mCost;
2610}
2611
2612std::set<String8> CameraService::CameraState::getConflicting() const {
2613 return mConflicting;
2614}
2615
2616String8 CameraService::CameraState::getId() const {
2617 return mId;
2618}
2619
2620// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002621// ClientEventListener
2622// ----------------------------------------------------------------------------
2623
2624void CameraService::ClientEventListener::onClientAdded(
2625 const resource_policy::ClientDescriptor<String8,
2626 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002627 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002628 if (basicClient.get() != nullptr) {
2629 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2630 notifier.noteStartCamera(descriptor.getKey(),
2631 static_cast<int>(basicClient->getClientUid()));
2632 }
2633}
2634
2635void CameraService::ClientEventListener::onClientRemoved(
2636 const resource_policy::ClientDescriptor<String8,
2637 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002638 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002639 if (basicClient.get() != nullptr) {
2640 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2641 notifier.noteStopCamera(descriptor.getKey(),
2642 static_cast<int>(basicClient->getClientUid()));
2643 }
2644}
2645
2646
2647// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002648// CameraClientManager
2649// ----------------------------------------------------------------------------
2650
Ruben Brunk99e69712015-05-26 17:25:07 -07002651CameraService::CameraClientManager::CameraClientManager() {
2652 setListener(std::make_shared<ClientEventListener>());
2653}
2654
Ruben Brunkcc776712015-02-17 20:18:47 -08002655CameraService::CameraClientManager::~CameraClientManager() {}
2656
2657sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2658 const String8& id) const {
2659 auto descriptor = get(id);
2660 if (descriptor == nullptr) {
2661 return sp<BasicClient>{nullptr};
2662 }
2663 return descriptor->getValue();
2664}
2665
2666String8 CameraService::CameraClientManager::toString() const {
2667 auto all = getAll();
2668 String8 ret("[");
2669 bool hasAny = false;
2670 for (auto& i : all) {
2671 hasAny = true;
2672 String8 key = i->getKey();
2673 int32_t cost = i->getCost();
2674 int32_t pid = i->getOwnerId();
2675 int32_t priority = i->getPriority();
2676 auto conflicting = i->getConflicting();
2677 auto clientSp = i->getValue();
2678 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002679 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002680 if (clientSp.get() != nullptr) {
2681 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002682 uid_t clientUid = clientSp->getClientUid();
2683 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002684 }
2685 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2686 PRId32 ", ", key.string(), cost, pid, priority);
2687
Ruben Brunk6267b532015-04-30 17:44:07 -07002688 if (clientSp.get() != nullptr) {
2689 ret.appendFormat("User Id: %d, ", clientUserId);
2690 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002691 if (packageName.size() != 0) {
2692 ret.appendFormat("Client Package Name: %s", packageName.string());
2693 }
2694
2695 ret.append(", Conflicting Client Devices: {");
2696 for (auto& j : conflicting) {
2697 ret.appendFormat("%s, ", j.string());
2698 }
2699 ret.append("})");
2700 }
2701 if (hasAny) ret.append("\n");
2702 ret.append("]\n");
2703 return ret;
2704}
2705
2706CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2707 const String8& key, const sp<BasicClient>& value, int32_t cost,
2708 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2709
2710 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2711 key, value, cost, conflictingKeys, priority, ownerId);
2712}
2713
2714CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2715 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2716 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2717 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2718}
2719
2720// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002721
2722static const int kDumpLockRetries = 50;
2723static const int kDumpLockSleep = 60000;
2724
2725static bool tryLock(Mutex& mutex)
2726{
2727 bool locked = false;
2728 for (int i = 0; i < kDumpLockRetries; ++i) {
2729 if (mutex.tryLock() == NO_ERROR) {
2730 locked = true;
2731 break;
2732 }
2733 usleep(kDumpLockSleep);
2734 }
2735 return locked;
2736}
2737
2738status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002739 ATRACE_CALL();
2740
Ruben Brunka8ca9152015-04-07 14:23:40 -07002741 String8 result("Dump of the Camera Service:\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002742 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
dcashmanfefa6142015-09-09 13:43:01 -07002743 result = result.format("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07002744 "can't dump CameraService from pid=%d, uid=%d\n",
2745 getCallingPid(),
2746 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002747 write(fd, result.string(), result.size());
2748 } else {
2749 bool locked = tryLock(mServiceLock);
2750 // failed to lock - CameraService is probably deadlocked
2751 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002752 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002753 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002754 }
2755
2756 bool hasClient = false;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002757 if (!mInitialized) {
2758 result = String8::format("No camera HAL available!\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002759 write(fd, result.string(), result.size());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002760
2761 // Dump event log for error information
2762 dumpEventLog(fd);
2763
Kalle Lampila6ec3a152013-04-30 15:27:19 +03002764 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002765 return NO_ERROR;
2766 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002767 if (mModule == nullptr) {
2768 mCameraProviderManager->dump(fd, args);
2769 // TODO - need way more dumping here
2770
2771 if (locked) mServiceLock.unlock();
2772 return NO_ERROR;
2773 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002774
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002775 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2776 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2777 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2778 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
Ruben Brunkcc776712015-02-17 20:18:47 -08002779 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
Eino-Ville Talvala49c97052016-01-12 14:29:40 -08002780 result.appendFormat("Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Ruben Brunkcc776712015-02-17 20:18:47 -08002781 String8 activeClientString = mActiveClientManager.toString();
2782 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
Ruben Brunk6267b532015-04-30 17:44:07 -07002783 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002784
Ruben Brunkf81648e2014-04-17 16:14:57 -07002785 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2786 if (desc == NULL) {
2787 result.appendFormat("Vendor tags left unimplemented.\n");
2788 } else {
2789 result.appendFormat("Vendor tag definitions:\n");
2790 }
2791
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002792 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07002793
2794 if (desc != NULL) {
2795 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
2796 }
2797
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002798 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002799
2800 bool stateLocked = tryLock(mCameraStatesLock);
2801 if (!stateLocked) {
2802 result = String8::format("CameraStates in use, may be deadlocked\n");
2803 write(fd, result.string(), result.size());
2804 }
2805
2806 for (auto& state : mCameraStates) {
2807 String8 cameraId = state.first;
2808 result = String8::format("Camera %s information:\n", cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002809 camera_info info;
2810
Ruben Brunkcc776712015-02-17 20:18:47 -08002811 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002812 if (rc != OK) {
2813 result.appendFormat(" Error reading static information!\n");
2814 write(fd, result.string(), result.size());
2815 } else {
2816 result.appendFormat(" Facing: %s\n",
Yin-Chia Yeh13cd59a2016-11-29 12:13:15 -08002817 info.facing == CAMERA_FACING_BACK ? "BACK" :
2818 info.facing == CAMERA_FACING_FRONT ? "FRONT" : "EXTERNAL");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002819 result.appendFormat(" Orientation: %d\n", info.orientation);
2820 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002821 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002822 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2823 } else {
2824 deviceVersion = info.device_version;
2825 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002826
2827 auto conflicting = state.second->getConflicting();
2828 result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
2829 result.appendFormat(" Conflicting Devices:");
2830 for (auto& id : conflicting) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002831 result.appendFormat(" %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002832 }
2833 if (conflicting.size() == 0) {
2834 result.appendFormat(" NONE");
2835 }
2836 result.appendFormat("\n");
2837
2838 result.appendFormat(" Device version: %#x\n", deviceVersion);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002839 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002840 result.appendFormat(" Device static metadata:\n");
2841 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07002842 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07002843 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002844 } else {
2845 write(fd, result.string(), result.size());
2846 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002847
2848 CameraParameters p = state.second->getShimParams();
2849 if (!p.isEmpty()) {
2850 result = String8::format(" Camera1 API shim is using parameters:\n ");
2851 write(fd, result.string(), result.size());
2852 p.dump(fd, args);
2853 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002854 }
2855
Ruben Brunkcc776712015-02-17 20:18:47 -08002856 auto clientDescriptor = mActiveClientManager.get(cameraId);
2857 if (clientDescriptor == nullptr) {
2858 result = String8::format(" Device %s is closed, no client instance\n",
2859 cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002860 write(fd, result.string(), result.size());
2861 continue;
2862 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002863 hasClient = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002864 result = String8::format(" Device %s is open. Client instance dump:\n\n",
2865 cameraId.string());
2866 result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
2867 result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
2868
2869 auto client = clientDescriptor->getValue();
2870 result.appendFormat("Client package: %s\n",
2871 String8(client->getPackageName()).string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002872 write(fd, result.string(), result.size());
Ruben Brunkcc776712015-02-17 20:18:47 -08002873
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002874 client->dumpClient(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002875 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002876
2877 if (stateLocked) mCameraStatesLock.unlock();
2878
Mathias Agopian65ab4712010-07-14 17:59:35 -07002879 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002880 result = String8::format("\nNo active camera clients yet.\n");
2881 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002882 }
2883
2884 if (locked) mServiceLock.unlock();
2885
Igor Murashkinff3e31d2013-10-23 16:40:06 -07002886 // Dump camera traces if there were any
2887 write(fd, "\n", 1);
2888 camera3::CameraTraces::dump(fd, args);
2889
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002890 // Process dump arguments, if any
Mathias Agopian65ab4712010-07-14 17:59:35 -07002891 int n = args.size();
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002892 String16 verboseOption("-v");
2893 String16 unreachableOption("--unreachable");
2894 for (int i = 0; i < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002895 if (args[i] == verboseOption) {
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002896 // change logging level
2897 if (i + 1 >= n) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002898 String8 levelStr(args[i+1]);
2899 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002900 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002901 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002902 write(fd, result.string(), result.size());
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002903 } else if (args[i] == unreachableOption) {
2904 // Dump memory analysis
2905 // TODO - should limit be an argument parameter?
2906 UnreachableMemoryInfo info;
2907 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2908 if (!success) {
2909 dprintf(fd, "\nUnable to dump unreachable memory. "
2910 "Try disabling SELinux enforcement.\n");
2911 } else {
2912 dprintf(fd, "\nDumping unreachable memory:\n");
2913 std::string s = info.ToString(/*log_contents*/ true);
2914 write(fd, s.c_str(), s.size());
2915 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002916 }
2917 }
2918 }
2919 return NO_ERROR;
2920}
2921
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002922void CameraService::dumpEventLog(int fd) {
2923 String8 result = String8("\nPrior client events (most recent at top):\n");
2924
2925 Mutex::Autolock l(mLogLock);
2926 for (const auto& msg : mEventLog) {
2927 result.appendFormat(" %s\n", msg.string());
2928 }
2929
2930 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
2931 result.append(" ...\n");
2932 } else if (mEventLog.size() == 0) {
2933 result.append(" [no events yet]\n");
2934 }
2935 result.append("\n");
2936
2937 write(fd, result.string(), result.size());
2938}
2939
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002940void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002941 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002942 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2943 if (mTorchClientMap[i] == who) {
2944 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002945 String8 cameraId = mTorchClientMap.keyAt(i);
2946 status_t res = mFlashlight->setTorchMode(cameraId, false);
2947 if (res) {
2948 ALOGE("%s: torch client died but couldn't turn off torch: "
2949 "%s (%d)", __FUNCTION__, strerror(-res), res);
2950 return;
2951 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002952 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002953 break;
2954 }
2955 }
2956}
2957
Ruben Brunkcc776712015-02-17 20:18:47 -08002958/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002959
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002960 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002961 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2962 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002963 */
2964
Ruben Brunka8ca9152015-04-07 14:23:40 -07002965 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2966
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002967 // check torch client
2968 handleTorchClientBinderDied(who);
2969
2970 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002971 if(!evictClientIdByRemote(who)) {
2972 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002973 return;
2974 }
2975
Ruben Brunkcc776712015-02-17 20:18:47 -08002976 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2977 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002978}
2979
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002980void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002981 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002982}
2983
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002984void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2985 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002986 // Do not lock mServiceLock here or can get into a deadlock from
2987 // connect() -> disconnect -> updateStatus
2988
2989 auto state = getCameraState(cameraId);
2990
2991 if (state == nullptr) {
2992 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2993 cameraId.string());
2994 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002995 }
2996
Ruben Brunkcc776712015-02-17 20:18:47 -08002997 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2998 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2999 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003000 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08003001
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003002 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003003 // Update torch status if it has a flash unit.
3004 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003005 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003006 if (getTorchStatusLocked(cameraId, &torchStatus) !=
3007 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003008 TorchModeStatus newTorchStatus =
3009 status == StatusInternal::PRESENT ?
3010 TorchModeStatus::AVAILABLE_OFF :
3011 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003012 if (torchStatus != newTorchStatus) {
3013 onTorchStatusChangedLocked(cameraId, newTorchStatus);
3014 }
3015 }
Ruben Brunkcc776712015-02-17 20:18:47 -08003016 }
3017
3018 Mutex::Autolock lock(mStatusListenerLock);
3019
3020 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003021 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08003022 }
3023 });
Igor Murashkincba2c162013-03-20 15:56:31 -07003024}
3025
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003026template<class Func>
3027void CameraService::CameraState::updateStatus(StatusInternal status,
3028 const String8& cameraId,
3029 std::initializer_list<StatusInternal> rejectSourceStates,
3030 Func onStatusUpdatedLocked) {
3031 Mutex::Autolock lock(mStatusLock);
3032 StatusInternal oldStatus = mStatus;
3033 mStatus = status;
3034
3035 if (oldStatus == status) {
3036 return;
3037 }
3038
3039 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
3040 cameraId.string(), oldStatus, status);
3041
3042 if (oldStatus == StatusInternal::NOT_PRESENT &&
3043 (status != StatusInternal::PRESENT &&
3044 status != StatusInternal::ENUMERATING)) {
3045
3046 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
3047 __FUNCTION__);
3048 mStatus = oldStatus;
3049 return;
3050 }
3051
3052 /**
3053 * Sometimes we want to conditionally do a transition.
3054 * For example if a client disconnects, we want to go to PRESENT
3055 * only if we weren't already in NOT_PRESENT or ENUMERATING.
3056 */
3057 for (auto& rejectStatus : rejectSourceStates) {
3058 if (oldStatus == rejectStatus) {
3059 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
3060 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
3061 mStatus = oldStatus;
3062 return;
3063 }
3064 }
3065
3066 onStatusUpdatedLocked(cameraId, status);
3067}
3068
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07003069void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
3070 const String8& cameraId) {
3071 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
3072 if (proxyBinder == nullptr) return;
3073 String16 id(cameraId);
3074 proxyBinder->notifyCameraState(id, newState);
3075}
3076
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003077status_t CameraService::getTorchStatusLocked(
3078 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003079 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003080 if (!status) {
3081 return BAD_VALUE;
3082 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003083 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3084 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003085 // invalid camera ID or the camera doesn't have a flash unit
3086 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003087 }
3088
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003089 *status = mTorchStatusMap.valueAt(index);
3090 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003091}
3092
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003093status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003094 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003095 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3096 if (index == NAME_NOT_FOUND) {
3097 return BAD_VALUE;
3098 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003099 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003100
3101 return OK;
3102}
3103
Mathias Agopian65ab4712010-07-14 17:59:35 -07003104}; // namespace android