blob: c92092cac271905dc2ac0d9115a4ecf471f8aff2 [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
327 // TODO: Set up vendor tags
328
329 mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
330 res = mFlashlight->findFlashUnits();
331 if (res != OK) {
332 ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
333 }
334
335 // TODO: Verify device versions are in support
336
337 for (auto& cameraId : mCameraProviderManager->getCameraDeviceIds()) {
338 hardware::camera::common::V1_0::CameraResourceCost cost;
339 res = mCameraProviderManager->getResourceCost(cameraId, &cost);
340 if (res != OK) {
341 ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res);
342 continue;
343 }
344 std::set<String8> conflicting;
345 for (size_t i = 0; i < cost.conflictingDevices.size(); i++) {
346 conflicting.emplace(String8(cost.conflictingDevices[i].c_str()));
347 }
348 String8 id8 = String8(cameraId.c_str());
349
350 Mutex::Autolock lock(mCameraStatesLock);
351 mCameraStates.emplace(id8,
352 std::make_shared<CameraState>(id8, cost.resourceCost, conflicting));
353
354 if (mFlashlight->hasFlashUnit(id8)) {
355 mTorchStatusMap.add(id8,
356 TorchModeStatus::AVAILABLE_OFF);
357 }
358 }
359
360 return OK;
361}
362
363
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700364sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800365 sp<ICameraServiceProxy> proxyBinder = nullptr;
366#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700367 sp<IServiceManager> sm = defaultServiceManager();
Eino-Ville Talvalaf4db13d2016-12-08 11:20:51 -0800368 // Use checkService because cameraserver normally starts before the
369 // system server and the proxy service. So the long timeout that getService
370 // has before giving up is inappropriate.
371 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800372 if (binder != nullptr) {
373 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700374 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800375#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700376 return proxyBinder;
377}
378
379void CameraService::pingCameraServiceProxy() {
380 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
381 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700382 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700383}
384
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800386 if (mModule) {
387 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800388 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800389 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800390 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391}
392
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800393void CameraService::onDeviceStatusChanged(const String8& id,
394 CameraDeviceStatus newHalStatus) {
395 ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__,
396 id.string(), newHalStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700397
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800398 StatusInternal newStatus = mapToInternal(newHalStatus);
399
Ruben Brunkcc776712015-02-17 20:18:47 -0800400 std::shared_ptr<CameraState> state = getCameraState(id);
401
402 if (state == nullptr) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800403 ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700404 return;
405 }
406
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800407 StatusInternal oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800408
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800409 if (oldStatus == newStatus) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800410 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700411 return;
412 }
413
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800414 if (newStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700415 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
416 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800417 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700418 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800419 // Don't do this in updateStatus to avoid deadlock over mServiceLock
420 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700421
Ruben Brunkcc776712015-02-17 20:18:47 -0800422 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
423 // to this device until the status changes
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800424 updateStatus(StatusInternal::NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700425
Ruben Brunkcc776712015-02-17 20:18:47 -0800426 // Remove cached shim parameters
427 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700428
Ruben Brunkcc776712015-02-17 20:18:47 -0800429 // Remove the client from the list of active clients
430 clientToDisconnect = removeClientLocked(id);
431
432 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800433 clientToDisconnect->notifyError(
434 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800435 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700436 }
437
Ruben Brunkcc776712015-02-17 20:18:47 -0800438 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
439 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700440
Ruben Brunkcc776712015-02-17 20:18:47 -0800441 // Disconnect client
442 if (clientToDisconnect.get() != nullptr) {
443 // Ensure not in binder RPC so client disconnect PID checks work correctly
444 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
445 "onDeviceStatusChanged must be called from the camera service process!");
446 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700447 }
448
Ruben Brunkcc776712015-02-17 20:18:47 -0800449 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800450 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700451 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
452 newStatus));
453 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800454 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700455 }
456
Igor Murashkincba2c162013-03-20 15:56:31 -0700457}
458
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800459void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800460 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800461 Mutex::Autolock al(mTorchStatusMutex);
462 onTorchStatusChangedLocked(cameraId, newStatus);
463}
464
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800465void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800466 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800467 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
468 __FUNCTION__, cameraId.string(), newStatus);
469
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800470 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800471 status_t res = getTorchStatusLocked(cameraId, &status);
472 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700473 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
474 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800475 return;
476 }
477 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800478 return;
479 }
480
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800481 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800482 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800483 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
484 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800485 return;
486 }
487
Ruben Brunkcc776712015-02-17 20:18:47 -0800488 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700489 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700490 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700491 auto iter = mTorchUidMap.find(cameraId);
492 if (iter != mTorchUidMap.end()) {
493 int oldUid = iter->second.second;
494 int newUid = iter->second.first;
495 BatteryNotifier& notifier(BatteryNotifier::getInstance());
496 if (oldUid != newUid) {
497 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800498 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700499 notifier.noteFlashlightOff(cameraId, oldUid);
500 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800501 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700502 notifier.noteFlashlightOn(cameraId, newUid);
503 }
504 iter->second.second = newUid;
505 } else {
506 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800507 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700508 notifier.noteFlashlightOn(cameraId, oldUid);
509 } else {
510 notifier.noteFlashlightOff(cameraId, oldUid);
511 }
512 }
513 }
514 }
515
516 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800517 Mutex::Autolock lock(mStatusListenerLock);
518 for (auto& i : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800519 i->onTorchStatusChanged(mapToInterface(newStatus), String16{cameraId});
Ruben Brunkcc776712015-02-17 20:18:47 -0800520 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800521 }
522}
523
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800524Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700525 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700526 switch (type) {
527 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800528 *numCameras = mNumberOfNormalCameras;
529 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700530 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 *numCameras = mNumberOfCameras;
532 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700533 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800534 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700535 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800536 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
537 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700538 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800539 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540}
541
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800542Status CameraService::getCameraInfo(int cameraId,
543 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700544 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800545 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800546 return STATUS_ERROR(ERROR_DISCONNECTED,
547 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700548 }
549
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
552 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 }
554
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 Status ret = Status::ok();
556 if (mModule != nullptr) {
557 struct camera_info info;
558 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800559
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800560 if (ret.isOk()) {
561 cameraInfo->facing = info.facing;
562 cameraInfo->orientation = info.orientation;
563 // CameraInfo is for android.hardware.Camera which does not
564 // support external camera facing. The closest approximation would be
565 // front camera.
566 if (cameraInfo->facing == CAMERA_FACING_EXTERNAL) {
567 cameraInfo->facing = hardware::CAMERA_FACING_FRONT;
568 }
Yin-Chia Yeh9b5a6e92016-04-22 13:24:05 -0700569 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800570 } else {
571 status_t err = mCameraProviderManager->getCameraInfo(std::to_string(cameraId), cameraInfo);
572 if (err != OK) {
573 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
574 "Error retrieving camera info from device %d: %s (%d)", cameraId,
575 strerror(-err), err);
576 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800577 }
578 return ret;
579}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700580
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800581int CameraService::cameraIdToInt(const String8& cameraId) {
582 int id;
583 bool success = base::ParseInt(cameraId.string(), &id, 0);
584 if (!success) {
585 return -1;
586 }
587 return id;
588}
589
590Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800591 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700592 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700593 if (!cameraInfo) {
594 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800595 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700596 }
597
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800598 if (!mInitialized) {
599 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600 return STATUS_ERROR(ERROR_DISCONNECTED,
601 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700602 }
603
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800604 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800605
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800606 if (mModule != nullptr) {
607 int id = cameraIdToInt(String8(cameraId));
Zhijun He2b59be82013-09-25 10:14:30 -0700608
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800609 if (id < 0 || id >= mNumberOfCameras) {
610 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, id);
611 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
612 "Invalid camera id: %d", id);
613 }
614
615 int version = getDeviceVersion(String8(cameraId));
616 if (version < CAMERA_DEVICE_API_VERSION_3_0) {
617 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Can't get camera characteristics"
618 " for devices with HAL version < 3.0, %d is version %x", id, version);
619 }
620
621 struct camera_info info;
622 ret = filterGetInfoErrorCode(mModule->getCameraInfo(id, &info));
623 if (ret.isOk()) {
624 *cameraInfo = info.static_camera_characteristics;
625 }
626 } else {
627 status_t res = mCameraProviderManager->getCameraCharacteristics(
628 String16::std_string(cameraId), cameraInfo);
629 if (res != OK) {
630 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
631 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
632 strerror(-res), res);
633 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700634 }
Zhijun He2b59be82013-09-25 10:14:30 -0700635
636 return ret;
637}
638
Ruben Brunkcc776712015-02-17 20:18:47 -0800639int CameraService::getCallingPid() {
640 return IPCThreadState::self()->getCallingPid();
641}
642
643int CameraService::getCallingUid() {
644 return IPCThreadState::self()->getCallingUid();
645}
646
647String8 CameraService::getFormattedCurrentTime() {
648 time_t now = time(nullptr);
649 char formattedTime[64];
650 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
651 return String8(formattedTime);
652}
653
654int CameraService::getCameraPriorityFromProcState(int procState) {
655 // Find the priority for the camera usage based on the process state. Higher priority clients
656 // win for evictions.
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700657 if (procState < 0) {
658 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
659 procState);
660 return -1;
Ruben Brunkcc776712015-02-17 20:18:47 -0800661 }
Eino-Ville Talvala52aad852015-09-03 12:24:24 -0700662 // Treat sleeping TOP processes the same as regular TOP processes, for
663 // access priority. This is important for lock-screen camera launch scenarios
664 if (procState == PROCESS_STATE_TOP_SLEEPING) {
665 procState = PROCESS_STATE_TOP;
666 }
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700667 return INT_MAX - procState;
Ruben Brunkcc776712015-02-17 20:18:47 -0800668}
669
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800670Status CameraService::getCameraVendorTagDescriptor(
671 /*out*/
672 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700673 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800674 if (!mInitialized) {
675 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800676 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800677 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800678 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
679 if (globalDescriptor != nullptr) {
680 *desc = *(globalDescriptor.get());
681 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800682 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800683}
684
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800685int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700686 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800687 struct camera_info info;
Igor Murashkin634a5152013-02-20 17:15:11 -0800688
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800689 int deviceVersion = 0;
690
691 if (mModule != nullptr) {
692 int id = cameraIdToInt(cameraId);
693 if (id < 0) return -1;
694
695 if (mModule->getCameraInfo(id, &info) != OK) {
696 return -1;
697 }
698
699 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
700 deviceVersion = info.device_version;
701 } else {
702 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
703 }
704
705 if (facing) {
706 *facing = info.facing;
707 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800708 } else {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800709 status_t res;
710 hardware::hidl_version maxVersion{0,0};
711 res = mCameraProviderManager->getHighestSupportedVersion(String8::std_string(cameraId),
712 &maxVersion);
713 if (res == NAME_NOT_FOUND) return -1;
714 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Igor Murashkin634a5152013-02-20 17:15:11 -0800715 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800716 return deviceVersion;
717}
718
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800719Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700720 switch(err) {
721 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800722 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800723 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800724 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
725 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800726 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800727 return STATUS_ERROR(ERROR_DISCONNECTED,
728 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700729 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800730 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
731 "Camera HAL encountered error %d: %s",
732 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700733 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800734}
735
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800736bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700737 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800738 if (mModule == nullptr) return false;
739
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800740 vendor_tag_ops_t vOps = vendor_tag_ops_t();
741
742 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800743 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800744 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
745 return false;
746 }
747
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800748 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800749
750 // Ensure all vendor operations are present
751 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
752 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
753 vOps.get_tag_type == NULL) {
754 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
755 , __FUNCTION__);
756 return false;
757 }
758
759 // Read all vendor tag definitions into a descriptor
760 sp<VendorTagDescriptor> desc;
761 status_t res;
762 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
763 != OK) {
764 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
765 "received error %s (%d). Camera clients will not be able to use"
766 "vendor tags", __FUNCTION__, strerror(res), res);
767 return false;
768 }
769
770 // Set the global descriptor to use with camera metadata
771 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
772 return true;
773}
774
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800775Status CameraService::makeClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800776 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800777 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
778 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
779 /*out*/sp<BasicClient>* client) {
780
Ruben Brunkcc776712015-02-17 20:18:47 -0800781 if (halVersion < 0 || halVersion == deviceVersion) {
782 // Default path: HAL version is unspecified by caller, create CameraClient
783 // based on device version reported by the HAL.
784 switch(deviceVersion) {
785 case CAMERA_DEVICE_API_VERSION_1_0:
786 if (effectiveApiLevel == API_1) { // Camera1 API route
787 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800788 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
789 facing, clientPid, clientUid, getpid(), legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800790 } else { // Camera2 API route
791 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800792 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800793 "Camera device \"%s\" HAL version %d does not support camera2 API",
794 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800795 }
796 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800797 case CAMERA_DEVICE_API_VERSION_3_0:
798 case CAMERA_DEVICE_API_VERSION_3_1:
799 case CAMERA_DEVICE_API_VERSION_3_2:
800 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700801 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800802 if (effectiveApiLevel == API_1) { // Camera1 API route
803 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800804 *client = new Camera2Client(cameraService, tmp, packageName, cameraIdToInt(cameraId),
805 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800806 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800807 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
808 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
809 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800810 facing, clientPid, clientUid, servicePid);
811 }
812 break;
813 default:
814 // Should not be reachable
815 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800816 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800817 "Camera device \"%s\" has unknown HAL version %d",
818 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800819 }
820 } else {
821 // A particular HAL version is requested by caller. Create CameraClient
822 // based on the requested HAL version.
823 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
824 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
825 // Only support higher HAL version device opened as HAL1.0 device.
826 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800827 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
828 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800829 } else {
830 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
831 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
832 " opened as HAL %x device", halVersion, deviceVersion,
833 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800834 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800835 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
836 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800837 }
838 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800839 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800840}
841
Ruben Brunk6267b532015-04-30 17:44:07 -0700842String8 CameraService::toString(std::set<userid_t> intSet) {
843 String8 s("");
844 bool first = true;
845 for (userid_t i : intSet) {
846 if (first) {
847 s.appendFormat("%d", i);
848 first = false;
849 } else {
850 s.appendFormat(", %d", i);
851 }
852 }
853 return s;
854}
855
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800856int32_t CameraService::mapToInterface(TorchModeStatus status) {
857 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
858 switch (status) {
859 case TorchModeStatus::NOT_AVAILABLE:
860 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
861 break;
862 case TorchModeStatus::AVAILABLE_OFF:
863 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
864 break;
865 case TorchModeStatus::AVAILABLE_ON:
866 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
867 break;
868 default:
869 ALOGW("Unknown new flash status: %d", status);
870 }
871 return serviceStatus;
872}
873
874CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
875 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
876 switch (status) {
877 case CameraDeviceStatus::NOT_PRESENT:
878 serviceStatus = StatusInternal::NOT_PRESENT;
879 break;
880 case CameraDeviceStatus::PRESENT:
881 serviceStatus = StatusInternal::PRESENT;
882 break;
883 case CameraDeviceStatus::ENUMERATING:
884 serviceStatus = StatusInternal::ENUMERATING;
885 break;
886 default:
887 ALOGW("Unknown new HAL device status: %d", status);
888 }
889 return serviceStatus;
890}
891
892int32_t CameraService::mapToInterface(StatusInternal status) {
893 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
894 switch (status) {
895 case StatusInternal::NOT_PRESENT:
896 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
897 break;
898 case StatusInternal::PRESENT:
899 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
900 break;
901 case StatusInternal::ENUMERATING:
902 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
903 break;
904 case StatusInternal::NOT_AVAILABLE:
905 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
906 break;
907 case StatusInternal::UNKNOWN:
908 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
909 break;
910 default:
911 ALOGW("Unknown new internal device status: %d", status);
912 }
913 return serviceStatus;
914}
915
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800916Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800917 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700918
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800919 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800920 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800921 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800922 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800923 if (!(ret = connectHelper<ICameraClient,Client>(
924 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
925 internalPackageName, uid, USE_CALLING_PID,
926 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
927 /*out*/ tmp)
928 ).isOk()) {
929 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700930 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800931 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700932}
933
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800934Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700935 /*out*/
936 CameraParameters* parameters) {
937
938 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
939
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800940 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700941
942 if (parameters == NULL) {
943 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800944 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700945 }
946
Ruben Brunkcc776712015-02-17 20:18:47 -0800947 String8 id = String8::format("%d", cameraId);
948
949 // Check if we already have parameters
950 {
951 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700952 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800953 auto cameraState = getCameraState(id);
954 if (cameraState == nullptr) {
955 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800956 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
957 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800958 }
959 CameraParameters p = cameraState->getShimParams();
960 if (!p.isEmpty()) {
961 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800962 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700963 }
964 }
965
Ruben Brunkcc776712015-02-17 20:18:47 -0800966 int64_t token = IPCThreadState::self()->clearCallingIdentity();
967 ret = initializeShimMetadata(cameraId);
968 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800969 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800970 // Error already logged by callee
971 return ret;
972 }
973
974 // Check for parameters again
975 {
976 // Scope for service lock
977 Mutex::Autolock lock(mServiceLock);
978 auto cameraState = getCameraState(id);
979 if (cameraState == nullptr) {
980 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800981 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
982 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700983 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800984 CameraParameters p = cameraState->getShimParams();
985 if (!p.isEmpty()) {
986 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800987 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700988 }
989 }
990
Ruben Brunkcc776712015-02-17 20:18:47 -0800991 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
992 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800993 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700994}
995
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800996// Can camera service trust the caller based on the calling UID?
997static bool isTrustedCallingUid(uid_t uid) {
998 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700999 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001000 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -07001001 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001002 return true;
1003 default:
1004 return false;
1005 }
1006}
1007
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001008Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001009 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
1010 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -05001011
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001012#ifdef __BRILLO__
1013 UNUSED(clientName8);
1014 UNUSED(clientUid);
1015 UNUSED(clientPid);
1016 UNUSED(originalClientPid);
1017#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001018 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
1019 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001020 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -08001021 return allowed;
1022 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001023#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -08001024
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001025 int callingPid = getCallingPid();
1026
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001027 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001028 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
1029 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001030 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1031 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -07001032 }
1033
Ruben Brunkcc776712015-02-17 20:18:47 -08001034 if (getCameraState(cameraId) == nullptr) {
1035 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1036 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001037 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1038 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039 }
1040
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001041 status_t err = checkIfDeviceIsUsable(cameraId);
1042 if (err != NO_ERROR) {
1043 switch(err) {
1044 case -ENODEV:
1045 case -EBUSY:
1046 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1047 "No camera device with ID \"%s\" currently available", cameraId.string());
1048 default:
1049 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1050 "Unknown error connecting to ID \"%s\"", cameraId.string());
1051 }
1052 }
1053 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001054}
1055
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001056Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001057 const String8& clientName8, int& clientUid, int& clientPid,
1058 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001059 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001060 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001062 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001064 clientUid = callingUid;
1065 } else if (!isTrustedCallingUid(callingUid)) {
1066 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1067 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001068 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1069 "Untrusted caller (calling PID %d, UID %d) trying to "
1070 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1071 callingPid, callingUid, cameraId.string(),
1072 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 }
1074
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001075 // Check if we can trust clientPid
1076 if (clientPid == USE_CALLING_PID) {
1077 clientPid = callingPid;
1078 } else if (!isTrustedCallingUid(callingUid)) {
1079 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1080 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001081 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1082 "Untrusted caller (calling PID %d, UID %d) trying to "
1083 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1084 callingPid, callingUid, cameraId.string(),
1085 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001086 }
1087
1088 // If it's not calling from cameraserver, check the permission.
1089 if (callingPid != getpid() &&
1090 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
1091 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001092 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1093 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1094 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001095 }
1096
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001097 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1098 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001099 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001100 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001101
Ruben Brunk6267b532015-04-30 17:44:07 -07001102 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001103
Ruben Brunka8ca9152015-04-07 14:23:40 -07001104 // Only allow clients who are being used by the current foreground device user, unless calling
1105 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001106 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1107 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1108 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1109 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001110 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1111 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1112 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001113 }
1114
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001115 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001116}
1117
1118status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1119 auto cameraState = getCameraState(cameraId);
1120 int callingPid = getCallingPid();
1121 if (cameraState == nullptr) {
1122 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1123 cameraId.string());
1124 return -ENODEV;
1125 }
1126
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001127 StatusInternal currentStatus = cameraState->getStatus();
1128 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001129 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1130 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001131 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001132 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001133 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1134 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001135 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001136 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001137
Ruben Brunkcc776712015-02-17 20:18:47 -08001138 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001139}
1140
Ruben Brunkcc776712015-02-17 20:18:47 -08001141void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1142 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001143
Ruben Brunkcc776712015-02-17 20:18:47 -08001144 // Make a descriptor for the incoming client
1145 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1146 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1147
1148 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1149 String8(client->getPackageName()));
1150
1151 if (evicted.size() > 0) {
1152 // This should never happen - clients should already have been removed in disconnect
1153 for (auto& i : evicted) {
1154 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1155 __FUNCTION__, i->getKey().string());
1156 }
1157
1158 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1159 __FUNCTION__);
1160 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001161
1162 // And register a death notification for the client callback. Do
1163 // this last to avoid Binder policy where a nested Binder
1164 // transaction might be pre-empted to service the client death
1165 // notification if the client process dies before linkToDeath is
1166 // invoked.
1167 sp<IBinder> remoteCallback = client->getRemote();
1168 if (remoteCallback != nullptr) {
1169 remoteCallback->linkToDeath(this);
1170 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001171}
1172
1173status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1174 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1175 /*out*/
1176 sp<BasicClient>* client,
1177 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001178 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001179 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001180 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001181 DescriptorPtr clientDescriptor;
1182 {
1183 if (effectiveApiLevel == API_1) {
1184 // If we are using API1, any existing client for this camera ID with the same remote
1185 // should be returned rather than evicted to allow MediaRecorder to work properly.
1186
1187 auto current = mActiveClientManager.get(cameraId);
1188 if (current != nullptr) {
1189 auto clientSp = current->getValue();
1190 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001191 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1192 ALOGW("CameraService connect called from same client, but with a different"
1193 " API level, evicting prior client...");
1194 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001195 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001196 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001197 *client = clientSp;
1198 return NO_ERROR;
1199 }
1200 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001201 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001202 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001203
Ruben Brunkcc776712015-02-17 20:18:47 -08001204 // Get current active client PIDs
1205 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1206 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001207
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001208 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1209 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1210 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1211 // not have an out-of-class definition.
1212 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001213
Ruben Brunkcc776712015-02-17 20:18:47 -08001214 // Get priorites of all active PIDs
1215 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1216 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001217
Ruben Brunkcc776712015-02-17 20:18:47 -08001218 // Update all active clients' priorities
1219 std::map<int,int> pidToPriorityMap;
1220 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1221 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1222 }
1223 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001224
Ruben Brunkcc776712015-02-17 20:18:47 -08001225 // Get state for the given cameraId
1226 auto state = getCameraState(cameraId);
1227 if (state == nullptr) {
1228 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1229 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001230 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001231 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001232 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001233
1234 // Make descriptor for incoming client
1235 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1236 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1237 state->getConflicting(),
1238 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1239
1240 // Find clients that would be evicted
1241 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1242
1243 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1244 // background, so we cannot do evictions
1245 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1246 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1247 " priority).", clientPid);
1248
1249 sp<BasicClient> clientSp = clientDescriptor->getValue();
1250 String8 curTime = getFormattedCurrentTime();
1251 auto incompatibleClients =
1252 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1253
1254 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001255 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001256 cameraId.string(), packageName.string(), clientPid,
1257 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1258
1259 for (auto& i : incompatibleClients) {
1260 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1261 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1262 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1263 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001264 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1265 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1266 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1267 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001268 }
1269
1270 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001271 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001272 mEventLog.add(msg);
1273
1274 return -EBUSY;
1275 }
1276
1277 for (auto& i : evicted) {
1278 sp<BasicClient> clientSp = i->getValue();
1279 if (clientSp.get() == nullptr) {
1280 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1281
1282 // TODO: Remove this
1283 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1284 __FUNCTION__);
1285 mActiveClientManager.remove(i);
1286 continue;
1287 }
1288
1289 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1290 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001291 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001292
Ruben Brunkcc776712015-02-17 20:18:47 -08001293 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001294 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1295 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1296 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001297 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1298 i->getOwnerId(), i->getPriority(), cameraId.string(),
1299 packageName.string(), clientPid,
1300 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1301
1302 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001303 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001304 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001305 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001306 }
1307
Ruben Brunkcc776712015-02-17 20:18:47 -08001308 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1309 // other clients from connecting in mServiceLockWrapper if held
1310 mServiceLock.unlock();
1311
1312 // Clear caller identity temporarily so client disconnect PID checks work correctly
1313 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1314
1315 // Destroy evicted clients
1316 for (auto& i : evictedClients) {
1317 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001318 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001319 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001320
1321 IPCThreadState::self()->restoreCallingIdentity(token);
1322
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001323 for (const auto& i : evictedClients) {
1324 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1325 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1326 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1327 if (ret == TIMED_OUT) {
1328 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1329 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1330 mActiveClientManager.toString().string());
1331 return -EBUSY;
1332 }
1333 if (ret != NO_ERROR) {
1334 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1335 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1336 ret, mActiveClientManager.toString().string());
1337 return ret;
1338 }
1339 }
1340
1341 evictedClients.clear();
1342
Ruben Brunkcc776712015-02-17 20:18:47 -08001343 // Once clients have been disconnected, relock
1344 mServiceLock.lock();
1345
1346 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1347 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1348 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001349 }
1350
Ruben Brunkcc776712015-02-17 20:18:47 -08001351 *partial = clientDescriptor;
1352 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001353}
1354
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001355Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001356 const sp<ICameraClient>& cameraClient,
1357 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001358 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001359 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001360 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001361 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001362 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001363
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001364 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001365 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001366 String8 id = String8::format("%d", cameraId);
1367 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001368 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1369 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1370 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1371 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001372
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001373 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001374 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001375 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001376 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001377 }
1378
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001379 *device = client;
1380 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001381}
1382
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001383Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001384 const sp<ICameraClient>& cameraClient,
1385 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001386 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001387 int clientUid,
1388 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001390
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001391 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001392 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001393 if (mModule != nullptr) {
1394 int apiVersion = mModule->getModuleApiVersion();
1395 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
1396 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
1397 /*
1398 * Either the HAL version is unspecified in which case this just creates
1399 * a camera client selected by the latest device version, or
1400 * it's a particular version in which case the HAL must supported
1401 * the open_legacy call
1402 */
1403 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1404 apiVersion);
1405 ALOGE("%s: %s",
1406 __FUNCTION__, msg.string());
1407 logRejected(id, getCallingPid(), String8(clientPackageName),
1408 msg);
1409 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
1410 }
Zhijun Heb10cdad2014-06-16 16:38:35 -07001411 }
1412
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001413 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001414 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001415 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1416 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1417 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1418 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001419
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001420 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001421 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001422 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001423 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001424 }
1425
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001426 *device = client;
1427 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001428}
1429
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001430Status CameraService::connectDevice(
1431 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001432 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001433 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001434 int clientUid,
1435 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001436 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001437
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001438 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001439 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001440 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001441 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1443 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1444 clientUid, USE_CALLING_PID, API_2,
1445 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1446 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001447
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001448 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001449 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001450 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001451 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001452 }
1453
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001454 *device = client;
1455 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001456}
1457
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001458template<class CALLBACK, class CLIENT>
1459Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
1460 int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
1461 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
1462 /*out*/sp<CLIENT>& device) {
1463 binder::Status ret = binder::Status::ok();
1464
1465 String8 clientName8(clientPackageName);
1466
1467 int originalClientPid = 0;
1468
1469 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1470 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1471 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1472 static_cast<int>(effectiveApiLevel));
1473
1474 sp<CLIENT> client = nullptr;
1475 {
1476 // Acquire mServiceLock and prevent other clients from connecting
1477 std::unique_ptr<AutoConditionLock> lock =
1478 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1479
1480 if (lock == nullptr) {
1481 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1482 , clientPid);
1483 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1484 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1485 cameraId.string(), clientName8.string(), clientPid);
1486 }
1487
1488 // Enforce client permissions and do basic sanity checks
1489 if(!(ret = validateConnectLocked(cameraId, clientName8,
1490 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1491 return ret;
1492 }
1493
1494 // Check the shim parameters after acquiring lock, if they have already been updated and
1495 // we were doing a shim update, return immediately
1496 if (shimUpdateOnly) {
1497 auto cameraState = getCameraState(cameraId);
1498 if (cameraState != nullptr) {
1499 if (!cameraState->getShimParams().isEmpty()) return ret;
1500 }
1501 }
1502
1503 status_t err;
1504
1505 sp<BasicClient> clientTmp = nullptr;
1506 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1507 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1508 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1509 /*out*/&partial)) != NO_ERROR) {
1510 switch (err) {
1511 case -ENODEV:
1512 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1513 "No camera device with ID \"%s\" currently available",
1514 cameraId.string());
1515 case -EBUSY:
1516 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1517 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1518 cameraId.string());
1519 default:
1520 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1521 "Unexpected error %s (%d) opening camera \"%s\"",
1522 strerror(-err), err, cameraId.string());
1523 }
1524 }
1525
1526 if (clientTmp.get() != nullptr) {
1527 // Handle special case for API1 MediaRecorder where the existing client is returned
1528 device = static_cast<CLIENT*>(clientTmp.get());
1529 return ret;
1530 }
1531
1532 // give flashlight a chance to close devices if necessary.
1533 mFlashlight->prepareDeviceOpen(cameraId);
1534
1535 // TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs
1536 int id = cameraIdToInt(cameraId);
1537 if (id == -1) {
1538 ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__,
1539 cameraId.string());
1540 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1541 "Bad camera ID \"%s\" passed to camera open", cameraId.string());
1542 }
1543
1544 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001545 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001546 sp<BasicClient> tmp = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001547 if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001548 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
1549 /*out*/&tmp)).isOk()) {
1550 return ret;
1551 }
1552 client = static_cast<CLIENT*>(tmp.get());
1553
1554 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1555 __FUNCTION__);
1556
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001557 if (mModule != nullptr) {
1558 err = client->initialize(mModule);
1559 } else {
1560 err = client->initialize(mCameraProviderManager);
1561 }
1562
1563 if (err != OK) {
1564 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001565 // Errors could be from the HAL module open call or from AppOpsManager
1566 switch(err) {
1567 case BAD_VALUE:
1568 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1569 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1570 case -EBUSY:
1571 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1572 "Camera \"%s\" is already open", cameraId.string());
1573 case -EUSERS:
1574 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1575 "Too many cameras already open, cannot open camera \"%s\"",
1576 cameraId.string());
1577 case PERMISSION_DENIED:
1578 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1579 "No permission to open camera \"%s\"", cameraId.string());
1580 case -EACCES:
1581 return STATUS_ERROR_FMT(ERROR_DISABLED,
1582 "Camera \"%s\" disabled by policy", cameraId.string());
1583 case -ENODEV:
1584 default:
1585 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1586 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1587 strerror(-err), err);
1588 }
1589 }
1590
1591 // Update shim paremeters for legacy clients
1592 if (effectiveApiLevel == API_1) {
1593 // Assume we have always received a Client subclass for API1
1594 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1595 String8 rawParams = shimClient->getParameters();
1596 CameraParameters params(rawParams);
1597
1598 auto cameraState = getCameraState(cameraId);
1599 if (cameraState != nullptr) {
1600 cameraState->setShimParams(params);
1601 } else {
1602 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1603 __FUNCTION__, cameraId.string());
1604 }
1605 }
1606
1607 if (shimUpdateOnly) {
1608 // If only updating legacy shim parameters, immediately disconnect client
1609 mServiceLock.unlock();
1610 client->disconnect();
1611 mServiceLock.lock();
1612 } else {
1613 // Otherwise, add client to active clients list
1614 finishConnectLocked(client, partial);
1615 }
1616 } // lock is destroyed, allow further connect calls
1617
1618 // Important: release the mutex here so the client can call back into the service from its
1619 // destructor (can be at the end of the call)
1620 device = client;
1621 return ret;
1622}
1623
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001624Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001625 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001626 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001627
1628 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001629 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001630 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001631 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1632 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001633 }
1634
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001635 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001636 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001637
1638 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001639 auto state = getCameraState(id);
1640 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001641 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001642 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1643 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001644 }
1645
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001646 StatusInternal cameraStatus = state->getStatus();
1647 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001648 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1649 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001650 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1651 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001652 }
1653
1654 {
1655 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001656 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001657 status_t err = getTorchStatusLocked(id, &status);
1658 if (err != OK) {
1659 if (err == NAME_NOT_FOUND) {
1660 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1661 "Camera \"%s\" does not have a flash unit", id.string());
1662 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001663 ALOGE("%s: getting current torch status failed for camera %s",
1664 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001665 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1666 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1667 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001668 }
1669
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001670 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001671 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001672 ALOGE("%s: torch mode of camera %s is not available because "
1673 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001674 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1675 "Torch for camera \"%s\" is not available due to an existing camera user",
1676 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001677 } else {
1678 ALOGE("%s: torch mode of camera %s is not available due to "
1679 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001680 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1681 "Torch for camera \"%s\" is not available due to insufficient resources",
1682 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001683 }
1684 }
1685 }
1686
Ruben Brunk99e69712015-05-26 17:25:07 -07001687 {
1688 // Update UID map - this is used in the torch status changed callbacks, so must be done
1689 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001690 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001691 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1692 mTorchUidMap[id].first = uid;
1693 mTorchUidMap[id].second = uid;
1694 } else {
1695 // Set the pending UID
1696 mTorchUidMap[id].first = uid;
1697 }
1698 }
1699
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001700 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001701
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001702 if (err != OK) {
1703 int32_t errorCode;
1704 String8 msg;
1705 switch (err) {
1706 case -ENOSYS:
1707 msg = String8::format("Camera \"%s\" has no flashlight",
1708 id.string());
1709 errorCode = ERROR_ILLEGAL_ARGUMENT;
1710 break;
1711 default:
1712 msg = String8::format(
1713 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1714 id.string(), enabled, strerror(-err), err);
1715 errorCode = ERROR_INVALID_OPERATION;
1716 }
1717 ALOGE("%s: %s", __FUNCTION__, msg.string());
1718 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001719 }
1720
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001721 {
1722 // update the link to client's death
1723 Mutex::Autolock al(mTorchClientMapMutex);
1724 ssize_t index = mTorchClientMap.indexOfKey(id);
1725 if (enabled) {
1726 if (index == NAME_NOT_FOUND) {
1727 mTorchClientMap.add(id, clientBinder);
1728 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001729 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001730 mTorchClientMap.replaceValueAt(index, clientBinder);
1731 }
1732 clientBinder->linkToDeath(this);
1733 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001734 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001735 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001736 }
1737
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001738 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001739}
1740
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001741Status CameraService::notifySystemEvent(int32_t eventId,
1742 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001743 ATRACE_CALL();
1744
Ruben Brunk36597b22015-03-20 22:15:57 -07001745 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001746 case ICameraService::EVENT_USER_SWITCHED: {
1747 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001748 break;
1749 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001750 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001751 default: {
1752 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1753 eventId);
1754 break;
1755 }
1756 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001757 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001758}
1759
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001760Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1761 /*out*/
1762 std::vector<hardware::CameraStatus> *cameraStatuses) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001763 ATRACE_CALL();
1764
Igor Murashkinbfc99152013-02-27 12:55:20 -08001765 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001766
Ruben Brunk3450ba72015-06-16 11:00:37 -07001767 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001768 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001769 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001770 }
1771
Igor Murashkinbfc99152013-02-27 12:55:20 -08001772 Mutex::Autolock lock(mServiceLock);
1773
Ruben Brunkcc776712015-02-17 20:18:47 -08001774 {
1775 Mutex::Autolock lock(mStatusListenerLock);
1776 for (auto& it : mListenerList) {
1777 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1778 ALOGW("%s: Tried to add listener %p which was already subscribed",
1779 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001780 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001781 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001782 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001783
1784 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001785 }
1786
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001787 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001788 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001789 Mutex::Autolock lock(mCameraStatesLock);
1790 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001791 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001792 }
1793 }
1794
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001795 /*
1796 * Immediately signal current torch status to this listener only
1797 * This may be a subset of all the devices, so don't include it in the response directly
1798 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001799 {
1800 Mutex::Autolock al(mTorchStatusMutex);
1801 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001802 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001803 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001804 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001805 }
1806
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001807 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001808}
Ruben Brunkcc776712015-02-17 20:18:47 -08001809
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001810Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001811 ATRACE_CALL();
1812
Igor Murashkinbfc99152013-02-27 12:55:20 -08001813 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1814
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001815 if (listener == 0) {
1816 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001817 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001818 }
1819
Igor Murashkinbfc99152013-02-27 12:55:20 -08001820 Mutex::Autolock lock(mServiceLock);
1821
Ruben Brunkcc776712015-02-17 20:18:47 -08001822 {
1823 Mutex::Autolock lock(mStatusListenerLock);
1824 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1825 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1826 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001827 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001828 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001829 }
1830 }
1831
1832 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1833 __FUNCTION__, listener.get());
1834
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001835 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001836}
1837
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001838Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001839
1840 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001841 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1842
1843 if (parameters == NULL) {
1844 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001845 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001846 }
1847
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001848 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001849
1850 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001851 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001852 // Error logged by caller
1853 return ret;
1854 }
1855
1856 String8 shimParamsString8 = shimParams.flatten();
1857 String16 shimParamsString16 = String16(shimParamsString8);
1858
1859 *parameters = shimParamsString16;
1860
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001861 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001862}
1863
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001864Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
1865 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001866 ATRACE_CALL();
1867
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001868 const String8 id = String8(cameraId);
1869
1870 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001871
1872 switch (apiVersion) {
1873 case API_VERSION_1:
1874 case API_VERSION_2:
1875 break;
1876 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001877 String8 msg = String8::format("Unknown API version %d", apiVersion);
1878 ALOGE("%s: %s", __FUNCTION__, msg.string());
1879 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001880 }
1881
1882 int facing = -1;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001883
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001884 int deviceVersion = getDeviceVersion(id, &facing);
Igor Murashkin65d14b92014-06-17 12:03:20 -07001885
1886 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001887 case CAMERA_DEVICE_API_VERSION_1_0:
1888 case CAMERA_DEVICE_API_VERSION_3_0:
1889 case CAMERA_DEVICE_API_VERSION_3_1:
1890 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001891 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
1892 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001893 *isSupported = false;
1894 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001895 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
1896 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001897 *isSupported = true;
1898 }
1899 break;
1900 case CAMERA_DEVICE_API_VERSION_3_2:
1901 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001902 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001903 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
1904 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001905 *isSupported = true;
1906 break;
1907 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001908 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001909 ALOGE("%s: %s", __FUNCTION__, msg.string());
1910 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001911 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001912 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001913 String8 msg = String8::format("Unknown device version %x for device %s",
1914 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001915 ALOGE("%s: %s", __FUNCTION__, msg.string());
1916 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1917 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001918 }
1919
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001920 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001921}
1922
Ruben Brunkcc776712015-02-17 20:18:47 -08001923void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001924 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001925 for (auto& i : mActiveClientManager.getAll()) {
1926 auto clientSp = i->getValue();
1927 if (clientSp.get() == client) {
1928 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001929 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001930 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001931}
1932
Ruben Brunkcc776712015-02-17 20:18:47 -08001933bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1934 const int callingPid = getCallingPid();
1935 const int servicePid = getpid();
1936 bool ret = false;
1937 {
1938 // Acquire mServiceLock and prevent other clients from connecting
1939 std::unique_ptr<AutoConditionLock> lock =
1940 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001941
Igor Murashkin634a5152013-02-20 17:15:11 -08001942
Ruben Brunkcc776712015-02-17 20:18:47 -08001943 std::vector<sp<BasicClient>> evicted;
1944 for (auto& i : mActiveClientManager.getAll()) {
1945 auto clientSp = i->getValue();
1946 if (clientSp.get() == nullptr) {
1947 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1948 mActiveClientManager.remove(i);
1949 continue;
1950 }
1951 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1952 callingPid == clientSp->getClientPid())) {
1953 mActiveClientManager.remove(i);
1954 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001955
Ruben Brunkcc776712015-02-17 20:18:47 -08001956 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001957 clientSp->notifyError(
1958 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001959 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001960 }
1961 }
1962
Ruben Brunkcc776712015-02-17 20:18:47 -08001963 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1964 // other clients from connecting in mServiceLockWrapper if held
1965 mServiceLock.unlock();
1966
Ruben Brunk36597b22015-03-20 22:15:57 -07001967 // Do not clear caller identity, remote caller should be client proccess
1968
Ruben Brunkcc776712015-02-17 20:18:47 -08001969 for (auto& i : evicted) {
1970 if (i.get() != nullptr) {
1971 i->disconnect();
1972 ret = true;
1973 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001974 }
1975
Ruben Brunkcc776712015-02-17 20:18:47 -08001976 // Reacquire mServiceLock
1977 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001978
Ruben Brunkcc776712015-02-17 20:18:47 -08001979 } // lock is destroyed, allow further connect calls
1980
1981 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001982}
1983
Igor Murashkinecf17e82012-10-02 16:05:11 -07001984
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001985/**
1986 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001987 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001988 */
1989int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001990 if (mModule == nullptr) return NO_INIT;
1991
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001992 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1993 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1994 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1995 // Verify the device version is in the supported range
1996 switch (info.device_version) {
1997 case CAMERA_DEVICE_API_VERSION_1_0:
1998 case CAMERA_DEVICE_API_VERSION_3_0:
1999 case CAMERA_DEVICE_API_VERSION_3_1:
2000 case CAMERA_DEVICE_API_VERSION_3_2:
2001 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07002002 case CAMERA_DEVICE_API_VERSION_3_4:
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08002003 // in support
2004 break;
2005 case CAMERA_DEVICE_API_VERSION_2_0:
2006 case CAMERA_DEVICE_API_VERSION_2_1:
2007 // no longer supported
2008 default:
2009 ALOGE("%s: Device %d has HAL version %x, which is not supported",
2010 __FUNCTION__, id, info.device_version);
2011 String8 msg = String8::format(
2012 "Unsupported device HAL version %x for device %d",
2013 info.device_version, id);
2014 logServiceError(msg.string(), NO_INIT);
2015 return NO_INIT;
2016 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002017 }
2018
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002019 // Assume all devices pre-v3.3 are backward-compatible
2020 bool isBackwardCompatible = true;
2021 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
2022 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
2023 isBackwardCompatible = false;
2024 status_t res;
2025 camera_metadata_ro_entry_t caps;
2026 res = find_camera_metadata_ro_entry(
2027 info.static_camera_characteristics,
2028 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
2029 &caps);
2030 if (res != 0) {
2031 ALOGW("%s: Unable to find camera capabilities for camera device %d",
2032 __FUNCTION__, id);
2033 caps.count = 0;
2034 }
2035 for (size_t i = 0; i < caps.count; i++) {
2036 if (caps.data.u8[i] ==
2037 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
2038 isBackwardCompatible = true;
2039 break;
2040 }
2041 }
2042 }
2043
2044 if (!isBackwardCompatible) {
2045 mNumberOfNormalCameras--;
2046 *latestStrangeCameraId = id;
2047 } else {
2048 if (id > *latestStrangeCameraId) {
2049 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
2050 "This is not allowed due backward-compatibility requirements",
2051 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002052 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002053 mNumberOfCameras = 0;
2054 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002055 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002056 }
2057 }
2058 return OK;
2059}
2060
Ruben Brunkcc776712015-02-17 20:18:47 -08002061std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
2062 const String8& cameraId) const {
2063 std::shared_ptr<CameraState> state;
2064 {
2065 Mutex::Autolock lock(mCameraStatesLock);
2066 auto iter = mCameraStates.find(cameraId);
2067 if (iter != mCameraStates.end()) {
2068 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002069 }
2070 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002071 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002072}
2073
Ruben Brunkcc776712015-02-17 20:18:47 -08002074sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
2075 // Remove from active clients list
2076 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
2077 if (clientDescriptorPtr == nullptr) {
2078 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
2079 cameraId.string());
2080 return sp<BasicClient>{nullptr};
2081 }
2082
2083 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07002084}
2085
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002086void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07002087 // Acquire mServiceLock and prevent other clients from connecting
2088 std::unique_ptr<AutoConditionLock> lock =
2089 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
2090
Ruben Brunk6267b532015-04-30 17:44:07 -07002091 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002092 for (size_t i = 0; i < newUserIds.size(); i++) {
2093 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07002094 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002095 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07002096 return;
2097 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002098 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07002099 }
2100
Ruben Brunka8ca9152015-04-07 14:23:40 -07002101
Ruben Brunk6267b532015-04-30 17:44:07 -07002102 if (newAllowedUsers == mAllowedUsers) {
2103 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
2104 return;
2105 }
2106
2107 logUserSwitch(mAllowedUsers, newAllowedUsers);
2108
2109 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07002110
2111 // Current user has switched, evict all current clients.
2112 std::vector<sp<BasicClient>> evicted;
2113 for (auto& i : mActiveClientManager.getAll()) {
2114 auto clientSp = i->getValue();
2115
2116 if (clientSp.get() == nullptr) {
2117 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
2118 continue;
2119 }
2120
Ruben Brunk6267b532015-04-30 17:44:07 -07002121 // Don't evict clients that are still allowed.
2122 uid_t clientUid = clientSp->getClientUid();
2123 userid_t clientUserId = multiuser_get_user_id(clientUid);
2124 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
2125 continue;
2126 }
2127
Ruben Brunk36597b22015-03-20 22:15:57 -07002128 evicted.push_back(clientSp);
2129
2130 String8 curTime = getFormattedCurrentTime();
2131
2132 ALOGE("Evicting conflicting client for camera ID %s due to user change",
2133 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07002134
Ruben Brunk36597b22015-03-20 22:15:57 -07002135 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002136 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07002137 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07002138 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
2139 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07002140
2141 }
2142
2143 // Do not hold mServiceLock while disconnecting clients, but retain the condition
2144 // blocking other clients from connecting in mServiceLockWrapper if held.
2145 mServiceLock.unlock();
2146
2147 // Clear caller identity temporarily so client disconnect PID checks work correctly
2148 int64_t token = IPCThreadState::self()->clearCallingIdentity();
2149
2150 for (auto& i : evicted) {
2151 i->disconnect();
2152 }
2153
2154 IPCThreadState::self()->restoreCallingIdentity(token);
2155
2156 // Reacquire mServiceLock
2157 mServiceLock.lock();
2158}
Ruben Brunkcc776712015-02-17 20:18:47 -08002159
Ruben Brunka8ca9152015-04-07 14:23:40 -07002160void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002161 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07002162 Mutex::Autolock l(mLogLock);
2163 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07002164}
2165
Ruben Brunka8ca9152015-04-07 14:23:40 -07002166void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002167 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002168 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002169 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002170 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002171}
2172
2173void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002174 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002175 // Log the clients evicted
2176 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002177 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002178}
2179
2180void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002181 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002182 // Log the client rejected
2183 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002184 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002185}
2186
Ruben Brunk6267b532015-04-30 17:44:07 -07002187void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
2188 const std::set<userid_t>& newUserIds) {
2189 String8 newUsers = toString(newUserIds);
2190 String8 oldUsers = toString(oldUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07002191 // Log the new and old users
Ruben Brunk6267b532015-04-30 17:44:07 -07002192 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
2193 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002194}
2195
2196void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
2197 // Log the device removal
2198 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
2199}
2200
2201void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
2202 // Log the device removal
2203 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
2204}
2205
2206void CameraService::logClientDied(int clientPid, const char* reason) {
2207 // Log the device removal
2208 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07002209}
2210
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002211void CameraService::logServiceError(const char* msg, int errorCode) {
2212 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002213 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002214}
2215
Ruben Brunk36597b22015-03-20 22:15:57 -07002216status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
2217 uint32_t flags) {
2218
2219 const int pid = getCallingPid();
2220 const int selfPid = getpid();
2221
Mathias Agopian65ab4712010-07-14 17:59:35 -07002222 // Permission checks
2223 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002224 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07002225 if (pid != selfPid) {
2226 // Ensure we're being called by system_server, or similar process with
2227 // permissions to notify the camera service about system events
2228 if (!checkCallingPermission(
2229 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
2230 const int uid = getCallingUid();
2231 ALOGE("Permission Denial: cannot send updates to camera service about system"
2232 " events from pid=%d, uid=%d", pid, uid);
2233 return PERMISSION_DENIED;
2234 }
2235 }
2236 break;
2237 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002238 }
2239
2240 return BnCameraService::onTransact(code, data, reply, flags);
2241}
2242
Mathias Agopian65ab4712010-07-14 17:59:35 -07002243// We share the media players for shutter and recording sound for all clients.
2244// A reference count is kept to determine when we will actually release the
2245// media players.
2246
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08002247MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002248 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08002249 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08002250 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002251 mp->prepare();
2252 } else {
Steve Block29357bc2012-01-06 19:20:56 +00002253 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254 return NULL;
2255 }
2256 return mp;
2257}
2258
2259void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002260 ATRACE_CALL();
2261
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262 Mutex::Autolock lock(mSoundLock);
2263 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2264 if (mSoundRef++) return;
2265
2266 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002267 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2268 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002269}
2270
2271void CameraService::releaseSound() {
2272 Mutex::Autolock lock(mSoundLock);
2273 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2274 if (--mSoundRef) return;
2275
2276 for (int i = 0; i < NUM_SOUNDS; i++) {
2277 if (mSoundPlayer[i] != 0) {
2278 mSoundPlayer[i]->disconnect();
2279 mSoundPlayer[i].clear();
2280 }
2281 }
2282}
2283
2284void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002285 ATRACE_CALL();
2286
Mathias Agopian65ab4712010-07-14 17:59:35 -07002287 LOG1("playSound(%d)", kind);
2288 Mutex::Autolock lock(mSoundLock);
2289 sp<MediaPlayer> player = mSoundPlayer[kind];
2290 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002291 player->seekTo(0);
2292 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002293 }
2294}
2295
2296// ----------------------------------------------------------------------------
2297
2298CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002299 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002300 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002301 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002302 int clientPid, uid_t clientUid,
2303 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002304 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002305 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002306 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002307 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002308 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002309 servicePid),
2310 mCameraId(CameraService::cameraIdToInt(cameraIdStr))
Igor Murashkin634a5152013-02-20 17:15:11 -08002311{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002312 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002313 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002314
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002315 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002316
Mathias Agopian65ab4712010-07-14 17:59:35 -07002317 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002318
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002319 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320}
2321
Mathias Agopian65ab4712010-07-14 17:59:35 -07002322// tear down the client
2323CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002324 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002325 mDestructionStarted = true;
2326
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002327 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002328 // unconditionally disconnect. function is idempotent
2329 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002330}
2331
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002332sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2333
Igor Murashkin634a5152013-02-20 17:15:11 -08002334CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002335 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002336 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002337 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002338 int clientPid, uid_t clientUid,
2339 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002340 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2341 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2342 mServicePid(servicePid),
2343 mDisconnected(false),
2344 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002345{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002346 if (sCameraService == nullptr) {
2347 sCameraService = cameraService;
2348 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002349 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002350 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002351
2352 // In some cases the calling code has no access to the package it runs under.
2353 // For example, NDK camera API.
2354 // In this case we will get the packages for the calling UID and pick the first one
2355 // for attributing the app op. This will work correctly for runtime permissions
2356 // as for legacy apps we will toggle the app op for all packages in the UID.
2357 // The caveat is that the operation may be attributed to the wrong package and
2358 // stats based on app ops may be slightly off.
2359 if (mClientPackageName.size() <= 0) {
2360 sp<IServiceManager> sm = defaultServiceManager();
2361 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2362 if (binder == 0) {
2363 ALOGE("Cannot get permission service");
2364 // Leave mClientPackageName unchanged (empty) and the further interaction
2365 // with camera will fail in BasicClient::startCameraOps
2366 return;
2367 }
2368
2369 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2370 Vector<String16> packages;
2371
2372 permCtrl->getPackagesForUid(mClientUid, packages);
2373
2374 if (packages.isEmpty()) {
2375 ALOGE("No packages for calling UID");
2376 // Leave mClientPackageName unchanged (empty) and the further interaction
2377 // with camera will fail in BasicClient::startCameraOps
2378 return;
2379 }
2380 mClientPackageName = packages[0];
2381 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002382}
2383
2384CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002385 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002386 mDestructionStarted = true;
2387}
2388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002389binder::Status CameraService::BasicClient::disconnect() {
2390 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002391 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002392 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002393 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002394 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002395
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002396 sCameraService->removeByClient(this);
2397 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002398 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002399
2400 sp<IBinder> remote = getRemote();
2401 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002402 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002403 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002404
2405 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002406 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002407 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2408 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2409 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002410
Igor Murashkincba2c162013-03-20 15:56:31 -07002411 // client shouldn't be able to call into us anymore
2412 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002413
2414 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002415}
2416
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002417status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2418 // No dumping of clients directly over Binder,
2419 // must go through CameraService::dump
2420 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2421 IPCThreadState::self()->getCallingUid(), NULL, 0);
2422 return OK;
2423}
2424
Ruben Brunkcc776712015-02-17 20:18:47 -08002425String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002426 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002427}
2428
2429
2430int CameraService::BasicClient::getClientPid() const {
2431 return mClientPid;
2432}
2433
Ruben Brunk6267b532015-04-30 17:44:07 -07002434uid_t CameraService::BasicClient::getClientUid() const {
2435 return mClientUid;
2436}
2437
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002438bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2439 // Defaults to API2.
2440 return level == API_2;
2441}
2442
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002443status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002444 ATRACE_CALL();
2445
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002446 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002447 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002448 mOpsCallback = new OpsCallback(this);
2449
Igor Murashkine6800ce2013-03-04 17:25:57 -08002450 {
2451 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002452 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002453 }
2454
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002455 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002456 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002457 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002458 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002459
Svetoslav28e8ef72015-05-11 19:21:31 -07002460 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002461 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2462 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002463 return PERMISSION_DENIED;
2464 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002465
Svetoslav28e8ef72015-05-11 19:21:31 -07002466 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002467 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2468 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002469 // Return the same error as for device policy manager rejection
2470 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002471 }
2472
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002473 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002474
2475 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002476 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002477
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002478 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002479 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2480 mCameraIdStr);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002481
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002482 return OK;
2483}
2484
2485status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002486 ATRACE_CALL();
2487
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002488 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002489 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002490 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002491 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002492 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002493 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002494
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002495 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
2496 StatusInternal::ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002497
Ruben Brunkcc776712015-02-17 20:18:47 -08002498 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002499 sCameraService->updateStatus(StatusInternal::PRESENT,
2500 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002501
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002502 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002503 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2504 mCameraIdStr);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002505 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002506 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002507 if (mOpsCallback != NULL) {
2508 mAppOpsManager.stopWatchingMode(mOpsCallback);
2509 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002510 mOpsCallback.clear();
2511
2512 return OK;
2513}
2514
2515void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002516 ATRACE_CALL();
2517
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002518 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002519 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002520
2521 if (op != AppOpsManager::OP_CAMERA) {
2522 ALOGW("Unexpected app ops notification received: %d", op);
2523 return;
2524 }
2525
2526 int32_t res;
2527 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002528 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002529 ALOGV("checkOp returns: %d, %s ", res,
2530 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2531 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2532 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2533 "UNKNOWN");
2534
2535 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002536 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002537 myName.string());
2538 // Reset the client PID to allow server-initiated disconnect,
2539 // and to prevent further calls by client.
2540 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002541 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002542 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002543 disconnect();
2544 }
2545}
2546
Mathias Agopian65ab4712010-07-14 17:59:35 -07002547// ----------------------------------------------------------------------------
2548
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002549void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002550 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002551 (void) errorCode;
2552 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002553 if (mRemoteCallback != NULL) {
2554 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2555 } else {
2556 ALOGE("mRemoteCallback is NULL!!");
2557 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002558}
2559
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002560// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002561binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002562 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002563 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002564}
2565
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002566bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2567 return level == API_1;
2568}
2569
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002570CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2571 mClient(client) {
2572}
2573
2574void CameraService::Client::OpsCallback::opChanged(int32_t op,
2575 const String16& packageName) {
2576 sp<BasicClient> client = mClient.promote();
2577 if (client != NULL) {
2578 client->opChanged(op, packageName);
2579 }
2580}
2581
Mathias Agopian65ab4712010-07-14 17:59:35 -07002582// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002583// CameraState
2584// ----------------------------------------------------------------------------
2585
2586CameraService::CameraState::CameraState(const String8& id, int cost,
2587 const std::set<String8>& conflicting) : mId(id),
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002588 mStatus(StatusInternal::PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002589
2590CameraService::CameraState::~CameraState() {}
2591
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002592CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002593 Mutex::Autolock lock(mStatusLock);
2594 return mStatus;
2595}
2596
2597CameraParameters CameraService::CameraState::getShimParams() const {
2598 return mShimParams;
2599}
2600
2601void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2602 mShimParams = params;
2603}
2604
2605int CameraService::CameraState::getCost() const {
2606 return mCost;
2607}
2608
2609std::set<String8> CameraService::CameraState::getConflicting() const {
2610 return mConflicting;
2611}
2612
2613String8 CameraService::CameraState::getId() const {
2614 return mId;
2615}
2616
2617// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002618// ClientEventListener
2619// ----------------------------------------------------------------------------
2620
2621void CameraService::ClientEventListener::onClientAdded(
2622 const resource_policy::ClientDescriptor<String8,
2623 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002624 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002625 if (basicClient.get() != nullptr) {
2626 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2627 notifier.noteStartCamera(descriptor.getKey(),
2628 static_cast<int>(basicClient->getClientUid()));
2629 }
2630}
2631
2632void CameraService::ClientEventListener::onClientRemoved(
2633 const resource_policy::ClientDescriptor<String8,
2634 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002635 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002636 if (basicClient.get() != nullptr) {
2637 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2638 notifier.noteStopCamera(descriptor.getKey(),
2639 static_cast<int>(basicClient->getClientUid()));
2640 }
2641}
2642
2643
2644// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002645// CameraClientManager
2646// ----------------------------------------------------------------------------
2647
Ruben Brunk99e69712015-05-26 17:25:07 -07002648CameraService::CameraClientManager::CameraClientManager() {
2649 setListener(std::make_shared<ClientEventListener>());
2650}
2651
Ruben Brunkcc776712015-02-17 20:18:47 -08002652CameraService::CameraClientManager::~CameraClientManager() {}
2653
2654sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2655 const String8& id) const {
2656 auto descriptor = get(id);
2657 if (descriptor == nullptr) {
2658 return sp<BasicClient>{nullptr};
2659 }
2660 return descriptor->getValue();
2661}
2662
2663String8 CameraService::CameraClientManager::toString() const {
2664 auto all = getAll();
2665 String8 ret("[");
2666 bool hasAny = false;
2667 for (auto& i : all) {
2668 hasAny = true;
2669 String8 key = i->getKey();
2670 int32_t cost = i->getCost();
2671 int32_t pid = i->getOwnerId();
2672 int32_t priority = i->getPriority();
2673 auto conflicting = i->getConflicting();
2674 auto clientSp = i->getValue();
2675 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002676 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002677 if (clientSp.get() != nullptr) {
2678 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002679 uid_t clientUid = clientSp->getClientUid();
2680 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002681 }
2682 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2683 PRId32 ", ", key.string(), cost, pid, priority);
2684
Ruben Brunk6267b532015-04-30 17:44:07 -07002685 if (clientSp.get() != nullptr) {
2686 ret.appendFormat("User Id: %d, ", clientUserId);
2687 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002688 if (packageName.size() != 0) {
2689 ret.appendFormat("Client Package Name: %s", packageName.string());
2690 }
2691
2692 ret.append(", Conflicting Client Devices: {");
2693 for (auto& j : conflicting) {
2694 ret.appendFormat("%s, ", j.string());
2695 }
2696 ret.append("})");
2697 }
2698 if (hasAny) ret.append("\n");
2699 ret.append("]\n");
2700 return ret;
2701}
2702
2703CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2704 const String8& key, const sp<BasicClient>& value, int32_t cost,
2705 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2706
2707 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2708 key, value, cost, conflictingKeys, priority, ownerId);
2709}
2710
2711CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2712 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2713 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2714 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2715}
2716
2717// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002718
2719static const int kDumpLockRetries = 50;
2720static const int kDumpLockSleep = 60000;
2721
2722static bool tryLock(Mutex& mutex)
2723{
2724 bool locked = false;
2725 for (int i = 0; i < kDumpLockRetries; ++i) {
2726 if (mutex.tryLock() == NO_ERROR) {
2727 locked = true;
2728 break;
2729 }
2730 usleep(kDumpLockSleep);
2731 }
2732 return locked;
2733}
2734
2735status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002736 ATRACE_CALL();
2737
Ruben Brunka8ca9152015-04-07 14:23:40 -07002738 String8 result("Dump of the Camera Service:\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002739 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
dcashmanfefa6142015-09-09 13:43:01 -07002740 result = result.format("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07002741 "can't dump CameraService from pid=%d, uid=%d\n",
2742 getCallingPid(),
2743 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002744 write(fd, result.string(), result.size());
2745 } else {
2746 bool locked = tryLock(mServiceLock);
2747 // failed to lock - CameraService is probably deadlocked
2748 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002749 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002750 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002751 }
2752
2753 bool hasClient = false;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002754 if (!mInitialized) {
2755 result = String8::format("No camera HAL available!\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002756 write(fd, result.string(), result.size());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002757
2758 // Dump event log for error information
2759 dumpEventLog(fd);
2760
Kalle Lampila6ec3a152013-04-30 15:27:19 +03002761 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002762 return NO_ERROR;
2763 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002764 if (mModule == nullptr) {
2765 mCameraProviderManager->dump(fd, args);
2766 // TODO - need way more dumping here
2767
2768 if (locked) mServiceLock.unlock();
2769 return NO_ERROR;
2770 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002771
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002772 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2773 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2774 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2775 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
Ruben Brunkcc776712015-02-17 20:18:47 -08002776 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
Eino-Ville Talvala49c97052016-01-12 14:29:40 -08002777 result.appendFormat("Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Ruben Brunkcc776712015-02-17 20:18:47 -08002778 String8 activeClientString = mActiveClientManager.toString();
2779 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
Ruben Brunk6267b532015-04-30 17:44:07 -07002780 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002781
Ruben Brunkf81648e2014-04-17 16:14:57 -07002782 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2783 if (desc == NULL) {
2784 result.appendFormat("Vendor tags left unimplemented.\n");
2785 } else {
2786 result.appendFormat("Vendor tag definitions:\n");
2787 }
2788
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002789 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07002790
2791 if (desc != NULL) {
2792 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
2793 }
2794
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002795 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002796
2797 bool stateLocked = tryLock(mCameraStatesLock);
2798 if (!stateLocked) {
2799 result = String8::format("CameraStates in use, may be deadlocked\n");
2800 write(fd, result.string(), result.size());
2801 }
2802
2803 for (auto& state : mCameraStates) {
2804 String8 cameraId = state.first;
2805 result = String8::format("Camera %s information:\n", cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002806 camera_info info;
2807
Ruben Brunkcc776712015-02-17 20:18:47 -08002808 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002809 if (rc != OK) {
2810 result.appendFormat(" Error reading static information!\n");
2811 write(fd, result.string(), result.size());
2812 } else {
2813 result.appendFormat(" Facing: %s\n",
Yin-Chia Yeh13cd59a2016-11-29 12:13:15 -08002814 info.facing == CAMERA_FACING_BACK ? "BACK" :
2815 info.facing == CAMERA_FACING_FRONT ? "FRONT" : "EXTERNAL");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002816 result.appendFormat(" Orientation: %d\n", info.orientation);
2817 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002818 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002819 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2820 } else {
2821 deviceVersion = info.device_version;
2822 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002823
2824 auto conflicting = state.second->getConflicting();
2825 result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
2826 result.appendFormat(" Conflicting Devices:");
2827 for (auto& id : conflicting) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002828 result.appendFormat(" %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002829 }
2830 if (conflicting.size() == 0) {
2831 result.appendFormat(" NONE");
2832 }
2833 result.appendFormat("\n");
2834
2835 result.appendFormat(" Device version: %#x\n", deviceVersion);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002836 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002837 result.appendFormat(" Device static metadata:\n");
2838 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07002839 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07002840 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002841 } else {
2842 write(fd, result.string(), result.size());
2843 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002844
2845 CameraParameters p = state.second->getShimParams();
2846 if (!p.isEmpty()) {
2847 result = String8::format(" Camera1 API shim is using parameters:\n ");
2848 write(fd, result.string(), result.size());
2849 p.dump(fd, args);
2850 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002851 }
2852
Ruben Brunkcc776712015-02-17 20:18:47 -08002853 auto clientDescriptor = mActiveClientManager.get(cameraId);
2854 if (clientDescriptor == nullptr) {
2855 result = String8::format(" Device %s is closed, no client instance\n",
2856 cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002857 write(fd, result.string(), result.size());
2858 continue;
2859 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002860 hasClient = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002861 result = String8::format(" Device %s is open. Client instance dump:\n\n",
2862 cameraId.string());
2863 result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
2864 result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
2865
2866 auto client = clientDescriptor->getValue();
2867 result.appendFormat("Client package: %s\n",
2868 String8(client->getPackageName()).string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002869 write(fd, result.string(), result.size());
Ruben Brunkcc776712015-02-17 20:18:47 -08002870
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002871 client->dumpClient(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002872 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002873
2874 if (stateLocked) mCameraStatesLock.unlock();
2875
Mathias Agopian65ab4712010-07-14 17:59:35 -07002876 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002877 result = String8::format("\nNo active camera clients yet.\n");
2878 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002879 }
2880
2881 if (locked) mServiceLock.unlock();
2882
Igor Murashkinff3e31d2013-10-23 16:40:06 -07002883 // Dump camera traces if there were any
2884 write(fd, "\n", 1);
2885 camera3::CameraTraces::dump(fd, args);
2886
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002887 // Process dump arguments, if any
Mathias Agopian65ab4712010-07-14 17:59:35 -07002888 int n = args.size();
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002889 String16 verboseOption("-v");
2890 String16 unreachableOption("--unreachable");
2891 for (int i = 0; i < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002892 if (args[i] == verboseOption) {
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002893 // change logging level
2894 if (i + 1 >= n) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002895 String8 levelStr(args[i+1]);
2896 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002897 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002898 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002899 write(fd, result.string(), result.size());
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002900 } else if (args[i] == unreachableOption) {
2901 // Dump memory analysis
2902 // TODO - should limit be an argument parameter?
2903 UnreachableMemoryInfo info;
2904 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2905 if (!success) {
2906 dprintf(fd, "\nUnable to dump unreachable memory. "
2907 "Try disabling SELinux enforcement.\n");
2908 } else {
2909 dprintf(fd, "\nDumping unreachable memory:\n");
2910 std::string s = info.ToString(/*log_contents*/ true);
2911 write(fd, s.c_str(), s.size());
2912 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002913 }
2914 }
2915 }
2916 return NO_ERROR;
2917}
2918
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002919void CameraService::dumpEventLog(int fd) {
2920 String8 result = String8("\nPrior client events (most recent at top):\n");
2921
2922 Mutex::Autolock l(mLogLock);
2923 for (const auto& msg : mEventLog) {
2924 result.appendFormat(" %s\n", msg.string());
2925 }
2926
2927 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
2928 result.append(" ...\n");
2929 } else if (mEventLog.size() == 0) {
2930 result.append(" [no events yet]\n");
2931 }
2932 result.append("\n");
2933
2934 write(fd, result.string(), result.size());
2935}
2936
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002937void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002938 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002939 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2940 if (mTorchClientMap[i] == who) {
2941 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002942 String8 cameraId = mTorchClientMap.keyAt(i);
2943 status_t res = mFlashlight->setTorchMode(cameraId, false);
2944 if (res) {
2945 ALOGE("%s: torch client died but couldn't turn off torch: "
2946 "%s (%d)", __FUNCTION__, strerror(-res), res);
2947 return;
2948 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002949 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002950 break;
2951 }
2952 }
2953}
2954
Ruben Brunkcc776712015-02-17 20:18:47 -08002955/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002956
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002957 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002958 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2959 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002960 */
2961
Ruben Brunka8ca9152015-04-07 14:23:40 -07002962 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2963
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002964 // check torch client
2965 handleTorchClientBinderDied(who);
2966
2967 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002968 if(!evictClientIdByRemote(who)) {
2969 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002970 return;
2971 }
2972
Ruben Brunkcc776712015-02-17 20:18:47 -08002973 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2974 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002975}
2976
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002977void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002978 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002979}
2980
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002981void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2982 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002983 // Do not lock mServiceLock here or can get into a deadlock from
2984 // connect() -> disconnect -> updateStatus
2985
2986 auto state = getCameraState(cameraId);
2987
2988 if (state == nullptr) {
2989 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2990 cameraId.string());
2991 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002992 }
2993
Ruben Brunkcc776712015-02-17 20:18:47 -08002994 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2995 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2996 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002997 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002998
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002999 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003000 // Update torch status if it has a flash unit.
3001 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003002 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003003 if (getTorchStatusLocked(cameraId, &torchStatus) !=
3004 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003005 TorchModeStatus newTorchStatus =
3006 status == StatusInternal::PRESENT ?
3007 TorchModeStatus::AVAILABLE_OFF :
3008 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003009 if (torchStatus != newTorchStatus) {
3010 onTorchStatusChangedLocked(cameraId, newTorchStatus);
3011 }
3012 }
Ruben Brunkcc776712015-02-17 20:18:47 -08003013 }
3014
3015 Mutex::Autolock lock(mStatusListenerLock);
3016
3017 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003018 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08003019 }
3020 });
Igor Murashkincba2c162013-03-20 15:56:31 -07003021}
3022
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003023template<class Func>
3024void CameraService::CameraState::updateStatus(StatusInternal status,
3025 const String8& cameraId,
3026 std::initializer_list<StatusInternal> rejectSourceStates,
3027 Func onStatusUpdatedLocked) {
3028 Mutex::Autolock lock(mStatusLock);
3029 StatusInternal oldStatus = mStatus;
3030 mStatus = status;
3031
3032 if (oldStatus == status) {
3033 return;
3034 }
3035
3036 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
3037 cameraId.string(), oldStatus, status);
3038
3039 if (oldStatus == StatusInternal::NOT_PRESENT &&
3040 (status != StatusInternal::PRESENT &&
3041 status != StatusInternal::ENUMERATING)) {
3042
3043 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
3044 __FUNCTION__);
3045 mStatus = oldStatus;
3046 return;
3047 }
3048
3049 /**
3050 * Sometimes we want to conditionally do a transition.
3051 * For example if a client disconnects, we want to go to PRESENT
3052 * only if we weren't already in NOT_PRESENT or ENUMERATING.
3053 */
3054 for (auto& rejectStatus : rejectSourceStates) {
3055 if (oldStatus == rejectStatus) {
3056 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
3057 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
3058 mStatus = oldStatus;
3059 return;
3060 }
3061 }
3062
3063 onStatusUpdatedLocked(cameraId, status);
3064}
3065
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07003066void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
3067 const String8& cameraId) {
3068 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
3069 if (proxyBinder == nullptr) return;
3070 String16 id(cameraId);
3071 proxyBinder->notifyCameraState(id, newState);
3072}
3073
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003074status_t CameraService::getTorchStatusLocked(
3075 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003076 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003077 if (!status) {
3078 return BAD_VALUE;
3079 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003080 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3081 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003082 // invalid camera ID or the camera doesn't have a flash unit
3083 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003084 }
3085
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003086 *status = mTorchStatusMap.valueAt(index);
3087 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003088}
3089
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003090status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003091 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003092 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3093 if (index == NAME_NOT_FOUND) {
3094 return BAD_VALUE;
3095 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003096 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003097
3098 return OK;
3099}
3100
Mathias Agopian65ab4712010-07-14 17:59:35 -07003101}; // namespace android