blob: 713370906004422596100b1712f7d0776dd457b9 [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
Ruben Brunkcc776712015-02-17 20:18:47 -080021#include <algorithm>
22#include <climits>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <stdio.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080024#include <cstring>
25#include <ctime>
26#include <string>
Mathias Agopian65ab4712010-07-14 17:59:35 -070027#include <sys/types.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080028#include <inttypes.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <pthread.h>
30
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080031#include <android/hardware/ICamera.h>
32#include <android/hardware/ICameraClient.h>
33
Alex Deymo9c2a2c22016-08-25 11:59:14 -070034#include <android-base/macros.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080035#include <android-base/parseint.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080036#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryBase.h>
40#include <binder/MemoryHeapBase.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080041#include <binder/ProcessInfoService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070043#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080044#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include <hardware/hardware.h>
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -070046#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070047#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080048#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070050#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070051#include <utils/Errors.h>
52#include <utils/Log.h>
53#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080054#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080055#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080056#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070057#include <system/camera_metadata.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080058
Ruben Brunkb2119af2014-05-09 19:57:56 -070059#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070060
61#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070062#include "api1/CameraClient.h"
63#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070064#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070065#include "utils/CameraTraces.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070066
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080067namespace {
68 const char* kPermissionServiceName = "permission";
69}; // namespace anonymous
70
Mathias Agopian65ab4712010-07-14 17:59:35 -070071namespace android {
72
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080073using binder::Status;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080074using hardware::ICamera;
75using hardware::ICameraClient;
76using hardware::ICameraServiceListener;
77using hardware::camera::common::V1_0::CameraDeviceStatus;
78using hardware::camera::common::V1_0::TorchModeStatus;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080079
Mathias Agopian65ab4712010-07-14 17:59:35 -070080// ----------------------------------------------------------------------------
81// Logging support -- this is for debugging only
82// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070083volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070084
Steve Blockb8a80522011-12-20 16:23:08 +000085#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
86#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070087
88static void setLogLevel(int level) {
89 android_atomic_write(level, &gLogLevel);
90}
91
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080092// Convenience methods for constructing binder::Status objects for error returns
93
94#define STATUS_ERROR(errorCode, errorString) \
95 binder::Status::fromServiceSpecificError(errorCode, \
96 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
97
98#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
99 binder::Status::fromServiceSpecificError(errorCode, \
100 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
101 __VA_ARGS__))
102
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103// ----------------------------------------------------------------------------
104
Igor Murashkincba2c162013-03-20 15:56:31 -0700105extern "C" {
106static void camera_device_status_change(
107 const struct camera_module_callbacks* callbacks,
108 int camera_id,
109 int new_status) {
110 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -0800111 static_cast<const CameraService*>(callbacks));
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800112 String8 id = String8::format("%d", camera_id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700113
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800114 CameraDeviceStatus newStatus{CameraDeviceStatus::NOT_PRESENT};
115 switch (new_status) {
116 case CAMERA_DEVICE_STATUS_NOT_PRESENT:
117 newStatus = CameraDeviceStatus::NOT_PRESENT;
118 break;
119 case CAMERA_DEVICE_STATUS_PRESENT:
120 newStatus = CameraDeviceStatus::PRESENT;
121 break;
122 case CAMERA_DEVICE_STATUS_ENUMERATING:
123 newStatus = CameraDeviceStatus::ENUMERATING;
124 break;
125 default:
126 ALOGW("Unknown device status change to %d", new_status);
127 break;
128 }
129 cs->onDeviceStatusChanged(id, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700130}
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800131
132static void torch_mode_status_change(
133 const struct camera_module_callbacks* callbacks,
134 const char* camera_id,
135 int new_status) {
136 if (!callbacks || !camera_id) {
137 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
138 callbacks, camera_id);
139 }
140 sp<CameraService> cs = const_cast<CameraService*>(
141 static_cast<const CameraService*>(callbacks));
142
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800143 TorchModeStatus status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800144 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800145 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800146 status = TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800147 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800148 case TORCH_MODE_STATUS_AVAILABLE_OFF:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800149 status = TorchModeStatus::AVAILABLE_OFF;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800150 break;
151 case TORCH_MODE_STATUS_AVAILABLE_ON:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800152 status = TorchModeStatus::AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800153 break;
154 default:
155 ALOGE("Unknown torch status %d", new_status);
156 return;
157 }
158
159 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800160 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800161 status);
162}
Igor Murashkincba2c162013-03-20 15:56:31 -0700163} // extern "C"
164
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165// ----------------------------------------------------------------------------
166
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800167CameraService::CameraService() :
168 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800169 mNumberOfCameras(0), mNumberOfNormalCameras(0),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800170 mSoundRef(0), mInitialized(false), mModule(nullptr) {
Steve Blockdf64d152012-01-04 20:05:49 +0000171 ALOGI("CameraService started (pid=%d)", getpid());
Igor Murashkinbfc99152013-02-27 12:55:20 -0800172
Igor Murashkincba2c162013-03-20 15:56:31 -0700173 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800174 this->torch_mode_status_change = android::torch_mode_status_change;
175
Ruben Brunkcc776712015-02-17 20:18:47 -0800176 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177}
178
Iliyan Malchev8951a972011-04-14 16:55:59 -0700179void CameraService::onFirstRef()
180{
Ruben Brunkcc776712015-02-17 20:18:47 -0800181 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800182
Iliyan Malchev8951a972011-04-14 16:55:59 -0700183 BnCameraService::onFirstRef();
184
Ruben Brunk99e69712015-05-26 17:25:07 -0700185 // Update battery life tracking if service is restarting
186 BatteryNotifier& notifier(BatteryNotifier::getInstance());
187 notifier.noteResetCamera();
188 notifier.noteResetFlashlight();
189
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800190 status_t res = INVALID_OPERATION;
Eino-Ville Talvala9cbbc832017-01-23 15:39:53 -0800191
192 bool disableTreble = property_get_bool("camera.disable_treble", false);
193 if (disableTreble) {
194 ALOGI("Treble disabled - using legacy path");
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800195 res = loadLegacyHalModule();
Eino-Ville Talvala9cbbc832017-01-23 15:39:53 -0800196 } else {
197 res = enumerateProviders();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800198 }
199 if (res == OK) {
200 mInitialized = true;
201 }
202
203 CameraService::pingCameraServiceProxy();
204}
205
206status_t CameraService::loadLegacyHalModule() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800207 camera_module_t *rawModule;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700208 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
209 (const hw_module_t **)&rawModule);
210 if (err < 0) {
211 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
212 logServiceError("Could not load camera HAL module", err);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800213 return INVALID_OPERATION;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700214 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800215
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700216 mModule = new CameraModule(rawModule);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700217 err = mModule->init();
218 if (err != OK) {
219 ALOGE("Could not initialize camera HAL module: %d (%s)", err,
220 strerror(-err));
221 logServiceError("Could not initialize camera HAL module", err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800222
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700223 delete mModule;
224 mModule = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800225 return INVALID_OPERATION;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700226 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700227 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700228
229 mNumberOfCameras = mModule->getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700230 mNumberOfNormalCameras = mNumberOfCameras;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700231
Yin-Chia Yehd4a653a2015-10-14 11:05:44 -0700232 // Setup vendor tags before we call get_camera_info the first time
233 // because HAL might need to setup static vendor keys in get_camera_info
234 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
235 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
236 setUpVendorTags();
237 }
238
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800239 mFlashlight = new CameraFlashlight(mModule, this);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700240 status_t res = mFlashlight->findFlashUnits();
241 if (res) {
242 // impossible because we haven't open any camera devices.
243 ALOGE("Failed to find flash units.");
244 }
245
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700246 int latestStrangeCameraId = INT_MAX;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700247 for (int i = 0; i < mNumberOfCameras; i++) {
248 String8 cameraId = String8::format("%d", i);
249
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700250 // Get camera info
251
252 struct camera_info info;
253 bool haveInfo = true;
254 status_t rc = mModule->getCameraInfo(i, &info);
255 if (rc != NO_ERROR) {
256 ALOGE("%s: Received error loading camera info for device %d, cost and"
257 " conflicting devices fields set to defaults for this device.",
258 __FUNCTION__, i);
259 haveInfo = false;
260 }
261
262 // Check for backwards-compatibility support
263 if (haveInfo) {
264 if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) {
265 delete mModule;
266 mModule = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800267 return INVALID_OPERATION;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700268 }
269 }
270
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700271 // Defaults to use for cost and conflicting devices
272 int cost = 100;
273 char** conflicting_devices = nullptr;
274 size_t conflicting_devices_length = 0;
275
276 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700277 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
278 cost = info.resource_cost;
279 conflicting_devices = info.conflicting_devices;
280 conflicting_devices_length = info.conflicting_devices_length;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700281 }
282
283 std::set<String8> conflicting;
284 for (size_t i = 0; i < conflicting_devices_length; i++) {
285 conflicting.emplace(String8(conflicting_devices[i]));
286 }
287
288 // Initialize state for each camera device
289 {
290 Mutex::Autolock lock(mCameraStatesLock);
291 mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost,
292 conflicting));
293 }
294
295 if (mFlashlight->hasFlashUnit(cameraId)) {
296 mTorchStatusMap.add(cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800297 TorchModeStatus::AVAILABLE_OFF);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700298 }
299 }
300
301 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
302 mModule->setCallbacks(this);
303 }
304
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800305 return OK;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700306}
307
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800308status_t CameraService::enumerateProviders() {
309 mCameraProviderManager = new CameraProviderManager();
310 status_t res;
311 res = mCameraProviderManager->initialize(this);
312 if (res != OK) {
313 ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
314 __FUNCTION__, strerror(-res), res);
315 return res;
316 }
317
318 mNumberOfCameras = mCameraProviderManager->getCameraCount();
319 mNumberOfNormalCameras = mCameraProviderManager->getStandardCameraCount();
320
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800321 // Setup vendor tags before we call get_camera_info the first time
322 // because HAL might need to setup static vendor keys in get_camera_info
323 // TODO: maybe put this into CameraProviderManager::initialize()?
324 mCameraProviderManager->setUpVendorTags();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800325
326 mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
327 res = mFlashlight->findFlashUnits();
328 if (res != OK) {
329 ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
330 }
331
332 // TODO: Verify device versions are in support
333
334 for (auto& cameraId : mCameraProviderManager->getCameraDeviceIds()) {
335 hardware::camera::common::V1_0::CameraResourceCost cost;
336 res = mCameraProviderManager->getResourceCost(cameraId, &cost);
337 if (res != OK) {
338 ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res);
339 continue;
340 }
341 std::set<String8> conflicting;
342 for (size_t i = 0; i < cost.conflictingDevices.size(); i++) {
343 conflicting.emplace(String8(cost.conflictingDevices[i].c_str()));
344 }
345 String8 id8 = String8(cameraId.c_str());
346
347 Mutex::Autolock lock(mCameraStatesLock);
348 mCameraStates.emplace(id8,
349 std::make_shared<CameraState>(id8, cost.resourceCost, conflicting));
350
351 if (mFlashlight->hasFlashUnit(id8)) {
352 mTorchStatusMap.add(id8,
353 TorchModeStatus::AVAILABLE_OFF);
354 }
355 }
356
357 return OK;
358}
359
360
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700361sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800362 sp<ICameraServiceProxy> proxyBinder = nullptr;
363#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700364 sp<IServiceManager> sm = defaultServiceManager();
Eino-Ville Talvalaf4db13d2016-12-08 11:20:51 -0800365 // Use checkService because cameraserver normally starts before the
366 // system server and the proxy service. So the long timeout that getService
367 // has before giving up is inappropriate.
368 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800369 if (binder != nullptr) {
370 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700371 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800372#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700373 return proxyBinder;
374}
375
376void CameraService::pingCameraServiceProxy() {
377 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
378 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700379 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700380}
381
Mathias Agopian65ab4712010-07-14 17:59:35 -0700382CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800383 if (mModule) {
384 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800385 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800386 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800387 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388}
389
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800390void CameraService::onDeviceStatusChanged(const String8& id,
391 CameraDeviceStatus newHalStatus) {
392 ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__,
393 id.string(), newHalStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700394
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800395 StatusInternal newStatus = mapToInternal(newHalStatus);
396
Ruben Brunkcc776712015-02-17 20:18:47 -0800397 std::shared_ptr<CameraState> state = getCameraState(id);
398
399 if (state == nullptr) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800400 ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700401 return;
402 }
403
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800404 StatusInternal oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800405
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800406 if (oldStatus == newStatus) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800407 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700408 return;
409 }
410
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800411 if (newStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700412 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
413 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800414 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700415 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800416 // Don't do this in updateStatus to avoid deadlock over mServiceLock
417 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700418
Ruben Brunkcc776712015-02-17 20:18:47 -0800419 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
420 // to this device until the status changes
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800421 updateStatus(StatusInternal::NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700422
Ruben Brunkcc776712015-02-17 20:18:47 -0800423 // Remove cached shim parameters
424 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700425
Ruben Brunkcc776712015-02-17 20:18:47 -0800426 // Remove the client from the list of active clients
427 clientToDisconnect = removeClientLocked(id);
428
429 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800430 clientToDisconnect->notifyError(
431 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800432 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700433 }
434
Ruben Brunkcc776712015-02-17 20:18:47 -0800435 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
436 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700437
Ruben Brunkcc776712015-02-17 20:18:47 -0800438 // Disconnect client
439 if (clientToDisconnect.get() != nullptr) {
440 // Ensure not in binder RPC so client disconnect PID checks work correctly
441 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
442 "onDeviceStatusChanged must be called from the camera service process!");
443 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700444 }
445
Ruben Brunkcc776712015-02-17 20:18:47 -0800446 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800447 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700448 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
449 newStatus));
450 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800451 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700452 }
453
Igor Murashkincba2c162013-03-20 15:56:31 -0700454}
455
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800456void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800457 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800458 Mutex::Autolock al(mTorchStatusMutex);
459 onTorchStatusChangedLocked(cameraId, newStatus);
460}
461
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800462void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800463 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800464 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
465 __FUNCTION__, cameraId.string(), newStatus);
466
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800467 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800468 status_t res = getTorchStatusLocked(cameraId, &status);
469 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700470 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
471 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800472 return;
473 }
474 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800475 return;
476 }
477
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800478 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800479 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800480 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
481 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800482 return;
483 }
484
Ruben Brunkcc776712015-02-17 20:18:47 -0800485 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700486 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700487 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700488 auto iter = mTorchUidMap.find(cameraId);
489 if (iter != mTorchUidMap.end()) {
490 int oldUid = iter->second.second;
491 int newUid = iter->second.first;
492 BatteryNotifier& notifier(BatteryNotifier::getInstance());
493 if (oldUid != newUid) {
494 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800495 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700496 notifier.noteFlashlightOff(cameraId, oldUid);
497 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800498 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700499 notifier.noteFlashlightOn(cameraId, newUid);
500 }
501 iter->second.second = newUid;
502 } else {
503 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800504 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700505 notifier.noteFlashlightOn(cameraId, oldUid);
506 } else {
507 notifier.noteFlashlightOff(cameraId, oldUid);
508 }
509 }
510 }
511 }
512
513 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800514 Mutex::Autolock lock(mStatusListenerLock);
515 for (auto& i : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800516 i->onTorchStatusChanged(mapToInterface(newStatus), String16{cameraId});
Ruben Brunkcc776712015-02-17 20:18:47 -0800517 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800518 }
519}
520
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800521Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700522 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700523 switch (type) {
524 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800525 *numCameras = mNumberOfNormalCameras;
526 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700527 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800528 *numCameras = mNumberOfCameras;
529 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700530 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700532 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800533 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
534 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700535 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800536 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537}
538
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800539Status CameraService::getCameraInfo(int cameraId,
540 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700541 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800542 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800543 return STATUS_ERROR(ERROR_DISCONNECTED,
544 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700545 }
546
Mathias Agopian65ab4712010-07-14 17:59:35 -0700547 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800548 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
549 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 }
551
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800552 Status ret = Status::ok();
553 if (mModule != nullptr) {
554 struct camera_info info;
555 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800556
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800557 if (ret.isOk()) {
558 cameraInfo->facing = info.facing;
559 cameraInfo->orientation = info.orientation;
560 // CameraInfo is for android.hardware.Camera which does not
561 // support external camera facing. The closest approximation would be
562 // front camera.
563 if (cameraInfo->facing == CAMERA_FACING_EXTERNAL) {
564 cameraInfo->facing = hardware::CAMERA_FACING_FRONT;
565 }
Yin-Chia Yeh9b5a6e92016-04-22 13:24:05 -0700566 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800567 } else {
568 status_t err = mCameraProviderManager->getCameraInfo(std::to_string(cameraId), cameraInfo);
569 if (err != OK) {
570 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
571 "Error retrieving camera info from device %d: %s (%d)", cameraId,
572 strerror(-err), err);
573 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800574 }
575 return ret;
576}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700577
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800578int CameraService::cameraIdToInt(const String8& cameraId) {
579 int id;
580 bool success = base::ParseInt(cameraId.string(), &id, 0);
581 if (!success) {
582 return -1;
583 }
584 return id;
585}
586
587Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800588 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700589 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700590 if (!cameraInfo) {
591 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800592 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700593 }
594
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800595 if (!mInitialized) {
596 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597 return STATUS_ERROR(ERROR_DISCONNECTED,
598 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700599 }
600
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800601 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800602
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800603 if (mModule != nullptr) {
604 int id = cameraIdToInt(String8(cameraId));
Zhijun He2b59be82013-09-25 10:14:30 -0700605
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800606 if (id < 0 || id >= mNumberOfCameras) {
607 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, id);
608 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
609 "Invalid camera id: %d", id);
610 }
611
612 int version = getDeviceVersion(String8(cameraId));
613 if (version < CAMERA_DEVICE_API_VERSION_3_0) {
614 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Can't get camera characteristics"
615 " for devices with HAL version < 3.0, %d is version %x", id, version);
616 }
617
618 struct camera_info info;
619 ret = filterGetInfoErrorCode(mModule->getCameraInfo(id, &info));
620 if (ret.isOk()) {
621 *cameraInfo = info.static_camera_characteristics;
622 }
623 } else {
624 status_t res = mCameraProviderManager->getCameraCharacteristics(
625 String16::std_string(cameraId), cameraInfo);
626 if (res != OK) {
627 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
628 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
629 strerror(-res), res);
630 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700631 }
Zhijun He2b59be82013-09-25 10:14:30 -0700632
633 return ret;
634}
635
Ruben Brunkcc776712015-02-17 20:18:47 -0800636int CameraService::getCallingPid() {
637 return IPCThreadState::self()->getCallingPid();
638}
639
640int CameraService::getCallingUid() {
641 return IPCThreadState::self()->getCallingUid();
642}
643
644String8 CameraService::getFormattedCurrentTime() {
645 time_t now = time(nullptr);
646 char formattedTime[64];
647 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
648 return String8(formattedTime);
649}
650
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800651Status CameraService::getCameraVendorTagDescriptor(
652 /*out*/
653 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700654 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800655 if (!mInitialized) {
656 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800657 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800658 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800659 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
660 if (globalDescriptor != nullptr) {
661 *desc = *(globalDescriptor.get());
662 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800663 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800664}
665
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800666int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700667 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800668
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800669 int deviceVersion = 0;
670
671 if (mModule != nullptr) {
672 int id = cameraIdToInt(cameraId);
673 if (id < 0) return -1;
674
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800675 struct camera_info info;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800676 if (mModule->getCameraInfo(id, &info) != OK) {
677 return -1;
678 }
679
680 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
681 deviceVersion = info.device_version;
682 } else {
683 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
684 }
685
686 if (facing) {
687 *facing = info.facing;
688 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800689 } else {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800690 status_t res;
691 hardware::hidl_version maxVersion{0,0};
692 res = mCameraProviderManager->getHighestSupportedVersion(String8::std_string(cameraId),
693 &maxVersion);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800694 if (res != OK) return -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800695 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800696
697 hardware::CameraInfo info;
698 if (facing) {
699 res = mCameraProviderManager->getCameraInfo(String8::std_string(cameraId), &info);
700 if (res != OK) return -1;
701 *facing = info.facing;
702 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800703 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800704 return deviceVersion;
705}
706
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800707Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700708 switch(err) {
709 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800710 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800711 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800712 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
713 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800714 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800715 return STATUS_ERROR(ERROR_DISCONNECTED,
716 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700717 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800718 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
719 "Camera HAL encountered error %d: %s",
720 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700721 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800722}
723
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800724bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700725 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800726 if (mModule == nullptr) return false;
727
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800728 vendor_tag_ops_t vOps = vendor_tag_ops_t();
729
730 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800731 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800732 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
733 return false;
734 }
735
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800736 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800737
738 // Ensure all vendor operations are present
739 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
740 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
741 vOps.get_tag_type == NULL) {
742 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
743 , __FUNCTION__);
744 return false;
745 }
746
747 // Read all vendor tag definitions into a descriptor
748 sp<VendorTagDescriptor> desc;
749 status_t res;
750 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
751 != OK) {
752 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
753 "received error %s (%d). Camera clients will not be able to use"
754 "vendor tags", __FUNCTION__, strerror(res), res);
755 return false;
756 }
757
758 // Set the global descriptor to use with camera metadata
759 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
760 return true;
761}
762
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800763Status CameraService::makeClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800764 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800765 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
766 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
767 /*out*/sp<BasicClient>* client) {
768
Ruben Brunkcc776712015-02-17 20:18:47 -0800769 if (halVersion < 0 || halVersion == deviceVersion) {
770 // Default path: HAL version is unspecified by caller, create CameraClient
771 // based on device version reported by the HAL.
772 switch(deviceVersion) {
773 case CAMERA_DEVICE_API_VERSION_1_0:
774 if (effectiveApiLevel == API_1) { // Camera1 API route
775 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800776 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
777 facing, clientPid, clientUid, getpid(), legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800778 } else { // Camera2 API route
779 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800780 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800781 "Camera device \"%s\" HAL version %d does not support camera2 API",
782 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800783 }
784 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800785 case CAMERA_DEVICE_API_VERSION_3_0:
786 case CAMERA_DEVICE_API_VERSION_3_1:
787 case CAMERA_DEVICE_API_VERSION_3_2:
788 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700789 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800790 if (effectiveApiLevel == API_1) { // Camera1 API route
791 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800792 *client = new Camera2Client(cameraService, tmp, packageName, cameraIdToInt(cameraId),
793 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800794 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800795 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
796 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
797 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800798 facing, clientPid, clientUid, servicePid);
799 }
800 break;
801 default:
802 // Should not be reachable
803 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800804 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800805 "Camera device \"%s\" has unknown HAL version %d",
806 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800807 }
808 } else {
809 // A particular HAL version is requested by caller. Create CameraClient
810 // based on the requested HAL version.
811 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
812 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
813 // Only support higher HAL version device opened as HAL1.0 device.
814 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800815 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
816 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800817 } else {
818 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
819 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
820 " opened as HAL %x device", halVersion, deviceVersion,
821 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800822 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800823 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
824 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800825 }
826 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800827 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800828}
829
Ruben Brunk6267b532015-04-30 17:44:07 -0700830String8 CameraService::toString(std::set<userid_t> intSet) {
831 String8 s("");
832 bool first = true;
833 for (userid_t i : intSet) {
834 if (first) {
835 s.appendFormat("%d", i);
836 first = false;
837 } else {
838 s.appendFormat(", %d", i);
839 }
840 }
841 return s;
842}
843
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800844int32_t CameraService::mapToInterface(TorchModeStatus status) {
845 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
846 switch (status) {
847 case TorchModeStatus::NOT_AVAILABLE:
848 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
849 break;
850 case TorchModeStatus::AVAILABLE_OFF:
851 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
852 break;
853 case TorchModeStatus::AVAILABLE_ON:
854 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
855 break;
856 default:
857 ALOGW("Unknown new flash status: %d", status);
858 }
859 return serviceStatus;
860}
861
862CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
863 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
864 switch (status) {
865 case CameraDeviceStatus::NOT_PRESENT:
866 serviceStatus = StatusInternal::NOT_PRESENT;
867 break;
868 case CameraDeviceStatus::PRESENT:
869 serviceStatus = StatusInternal::PRESENT;
870 break;
871 case CameraDeviceStatus::ENUMERATING:
872 serviceStatus = StatusInternal::ENUMERATING;
873 break;
874 default:
875 ALOGW("Unknown new HAL device status: %d", status);
876 }
877 return serviceStatus;
878}
879
880int32_t CameraService::mapToInterface(StatusInternal status) {
881 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
882 switch (status) {
883 case StatusInternal::NOT_PRESENT:
884 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
885 break;
886 case StatusInternal::PRESENT:
887 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
888 break;
889 case StatusInternal::ENUMERATING:
890 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
891 break;
892 case StatusInternal::NOT_AVAILABLE:
893 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
894 break;
895 case StatusInternal::UNKNOWN:
896 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
897 break;
898 default:
899 ALOGW("Unknown new internal device status: %d", status);
900 }
901 return serviceStatus;
902}
903
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800904Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800905 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700906
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800907 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800908 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800909 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800910 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800911 if (!(ret = connectHelper<ICameraClient,Client>(
912 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
913 internalPackageName, uid, USE_CALLING_PID,
914 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
915 /*out*/ tmp)
916 ).isOk()) {
917 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700918 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800919 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700920}
921
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800922Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700923 /*out*/
924 CameraParameters* parameters) {
925
926 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
927
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800928 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700929
930 if (parameters == NULL) {
931 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800932 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700933 }
934
Ruben Brunkcc776712015-02-17 20:18:47 -0800935 String8 id = String8::format("%d", cameraId);
936
937 // Check if we already have parameters
938 {
939 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700940 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800941 auto cameraState = getCameraState(id);
942 if (cameraState == nullptr) {
943 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800944 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
945 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800946 }
947 CameraParameters p = cameraState->getShimParams();
948 if (!p.isEmpty()) {
949 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800950 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700951 }
952 }
953
Ruben Brunkcc776712015-02-17 20:18:47 -0800954 int64_t token = IPCThreadState::self()->clearCallingIdentity();
955 ret = initializeShimMetadata(cameraId);
956 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800957 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800958 // Error already logged by callee
959 return ret;
960 }
961
962 // Check for parameters again
963 {
964 // Scope for service lock
965 Mutex::Autolock lock(mServiceLock);
966 auto cameraState = getCameraState(id);
967 if (cameraState == nullptr) {
968 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800969 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
970 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700971 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800972 CameraParameters p = cameraState->getShimParams();
973 if (!p.isEmpty()) {
974 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800975 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700976 }
977 }
978
Ruben Brunkcc776712015-02-17 20:18:47 -0800979 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
980 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800981 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700982}
983
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800984// Can camera service trust the caller based on the calling UID?
985static bool isTrustedCallingUid(uid_t uid) {
986 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700987 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800988 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700989 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800990 return true;
991 default:
992 return false;
993 }
994}
995
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800996Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700997 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
998 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500999
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001000#ifdef __BRILLO__
1001 UNUSED(clientName8);
1002 UNUSED(clientUid);
1003 UNUSED(clientPid);
1004 UNUSED(originalClientPid);
1005#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001006 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
1007 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001008 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -08001009 return allowed;
1010 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001011#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -08001012
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001013 int callingPid = getCallingPid();
1014
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001015 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001016 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
1017 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001018 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1019 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -07001020 }
1021
Ruben Brunkcc776712015-02-17 20:18:47 -08001022 if (getCameraState(cameraId) == nullptr) {
1023 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1024 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001025 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1026 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001027 }
1028
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001029 status_t err = checkIfDeviceIsUsable(cameraId);
1030 if (err != NO_ERROR) {
1031 switch(err) {
1032 case -ENODEV:
1033 case -EBUSY:
1034 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1035 "No camera device with ID \"%s\" currently available", cameraId.string());
1036 default:
1037 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1038 "Unknown error connecting to ID \"%s\"", cameraId.string());
1039 }
1040 }
1041 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001042}
1043
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001044Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001045 const String8& clientName8, int& clientUid, int& clientPid,
1046 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001047 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001048 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001050 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -07001051 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001052 clientUid = callingUid;
1053 } else if (!isTrustedCallingUid(callingUid)) {
1054 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1055 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001056 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1057 "Untrusted caller (calling PID %d, UID %d) trying to "
1058 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1059 callingPid, callingUid, cameraId.string(),
1060 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001061 }
1062
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001063 // Check if we can trust clientPid
1064 if (clientPid == USE_CALLING_PID) {
1065 clientPid = callingPid;
1066 } else if (!isTrustedCallingUid(callingUid)) {
1067 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1068 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001069 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1070 "Untrusted caller (calling PID %d, UID %d) trying to "
1071 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1072 callingPid, callingUid, cameraId.string(),
1073 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001074 }
1075
1076 // If it's not calling from cameraserver, check the permission.
1077 if (callingPid != getpid() &&
1078 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
1079 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001080 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1081 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1082 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001083 }
1084
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001085 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1086 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001087 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001088 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089
Ruben Brunk6267b532015-04-30 17:44:07 -07001090 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001091
Ruben Brunka8ca9152015-04-07 14:23:40 -07001092 // Only allow clients who are being used by the current foreground device user, unless calling
1093 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001094 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1095 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1096 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1097 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001098 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1099 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1100 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001101 }
1102
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001103 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001104}
1105
1106status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1107 auto cameraState = getCameraState(cameraId);
1108 int callingPid = getCallingPid();
1109 if (cameraState == nullptr) {
1110 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1111 cameraId.string());
1112 return -ENODEV;
1113 }
1114
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001115 StatusInternal currentStatus = cameraState->getStatus();
1116 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001117 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1118 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001119 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001120 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001121 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1122 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001123 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001124 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001125
Ruben Brunkcc776712015-02-17 20:18:47 -08001126 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001127}
1128
Ruben Brunkcc776712015-02-17 20:18:47 -08001129void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1130 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001131
Ruben Brunkcc776712015-02-17 20:18:47 -08001132 // Make a descriptor for the incoming client
1133 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1134 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1135
1136 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1137 String8(client->getPackageName()));
1138
1139 if (evicted.size() > 0) {
1140 // This should never happen - clients should already have been removed in disconnect
1141 for (auto& i : evicted) {
1142 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1143 __FUNCTION__, i->getKey().string());
1144 }
1145
1146 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1147 __FUNCTION__);
1148 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001149
1150 // And register a death notification for the client callback. Do
1151 // this last to avoid Binder policy where a nested Binder
1152 // transaction might be pre-empted to service the client death
1153 // notification if the client process dies before linkToDeath is
1154 // invoked.
1155 sp<IBinder> remoteCallback = client->getRemote();
1156 if (remoteCallback != nullptr) {
1157 remoteCallback->linkToDeath(this);
1158 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001159}
1160
1161status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1162 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1163 /*out*/
1164 sp<BasicClient>* client,
1165 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001166 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001167 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001168 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001169 DescriptorPtr clientDescriptor;
1170 {
1171 if (effectiveApiLevel == API_1) {
1172 // If we are using API1, any existing client for this camera ID with the same remote
1173 // should be returned rather than evicted to allow MediaRecorder to work properly.
1174
1175 auto current = mActiveClientManager.get(cameraId);
1176 if (current != nullptr) {
1177 auto clientSp = current->getValue();
1178 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001179 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1180 ALOGW("CameraService connect called from same client, but with a different"
1181 " API level, evicting prior client...");
1182 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001183 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001184 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001185 *client = clientSp;
1186 return NO_ERROR;
1187 }
1188 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001189 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001190 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001191
Ruben Brunkcc776712015-02-17 20:18:47 -08001192 // Get current active client PIDs
1193 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1194 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001195
Emilian Peev8131a262017-02-01 12:33:43 +00001196 std::vector<int> priorityScores(ownerPids.size());
1197 std::vector<int> states(ownerPids.size());
Igor Murashkine6800ce2013-03-04 17:25:57 -08001198
Emilian Peev8131a262017-02-01 12:33:43 +00001199 // Get priority scores of all active PIDs
1200 status_t err = ProcessInfoService::getProcessStatesScoresFromPids(
1201 ownerPids.size(), &ownerPids[0], /*out*/&states[0],
1202 /*out*/&priorityScores[0]);
1203 if (err != OK) {
1204 ALOGE("%s: Priority score query failed: %d",
1205 __FUNCTION__, err);
1206 return err;
1207 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001208
Ruben Brunkcc776712015-02-17 20:18:47 -08001209 // Update all active clients' priorities
Emilian Peev8131a262017-02-01 12:33:43 +00001210 std::map<int,resource_policy::ClientPriority> pidToPriorityMap;
Ruben Brunkcc776712015-02-17 20:18:47 -08001211 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
Emilian Peev8131a262017-02-01 12:33:43 +00001212 pidToPriorityMap.emplace(ownerPids[i],
1213 resource_policy::ClientPriority(priorityScores[i], states[i]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001214 }
1215 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001216
Ruben Brunkcc776712015-02-17 20:18:47 -08001217 // Get state for the given cameraId
1218 auto state = getCameraState(cameraId);
1219 if (state == nullptr) {
1220 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1221 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001222 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001223 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001224 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001225
1226 // Make descriptor for incoming client
1227 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1228 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1229 state->getConflicting(),
Emilian Peev8131a262017-02-01 12:33:43 +00001230 priorityScores[priorityScores.size() - 1],
1231 clientPid,
1232 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001233
1234 // Find clients that would be evicted
1235 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1236
1237 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1238 // background, so we cannot do evictions
1239 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1240 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1241 " priority).", clientPid);
1242
1243 sp<BasicClient> clientSp = clientDescriptor->getValue();
1244 String8 curTime = getFormattedCurrentTime();
1245 auto incompatibleClients =
1246 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1247
1248 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Emilian Peev8131a262017-02-01 12:33:43 +00001249 "(PID %d, score %d state %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001250 cameraId.string(), packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001251 priorityScores[priorityScores.size() - 1],
1252 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001253
1254 for (auto& i : incompatibleClients) {
1255 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
Emilian Peev8131a262017-02-01 12:33:43 +00001256 "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")",
1257 i->getKey().string(),
1258 String8{i->getValue()->getPackageName()}.string(),
1259 i->getOwnerId(), i->getPriority().getScore(),
1260 i->getPriority().getState());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001261 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001262 PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(),
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001263 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
Emilian Peev8131a262017-02-01 12:33:43 +00001264 i->getPriority().getScore(), i->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08001265 }
1266
1267 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001268 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001269 mEventLog.add(msg);
1270
1271 return -EBUSY;
1272 }
1273
1274 for (auto& i : evicted) {
1275 sp<BasicClient> clientSp = i->getValue();
1276 if (clientSp.get() == nullptr) {
1277 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1278
1279 // TODO: Remove this
1280 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1281 __FUNCTION__);
1282 mActiveClientManager.remove(i);
1283 continue;
1284 }
1285
1286 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1287 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001288 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001289
Ruben Brunkcc776712015-02-17 20:18:47 -08001290 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001291 logEvent(String8::format("EVICT device %s client held by package %s (PID"
Emilian Peev8131a262017-02-01 12:33:43 +00001292 " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for"
1293 " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001294 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
Emilian Peev8131a262017-02-01 12:33:43 +00001295 i->getOwnerId(), i->getPriority().getScore(),
1296 i->getPriority().getState(), cameraId.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001297 packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001298 priorityScores[priorityScores.size() - 1],
1299 states[states.size() - 1]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001300
1301 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001302 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001303 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001304 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001305 }
1306
Ruben Brunkcc776712015-02-17 20:18:47 -08001307 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1308 // other clients from connecting in mServiceLockWrapper if held
1309 mServiceLock.unlock();
1310
1311 // Clear caller identity temporarily so client disconnect PID checks work correctly
1312 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1313
1314 // Destroy evicted clients
1315 for (auto& i : evictedClients) {
1316 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001317 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001318 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001319
1320 IPCThreadState::self()->restoreCallingIdentity(token);
1321
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001322 for (const auto& i : evictedClients) {
1323 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1324 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1325 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1326 if (ret == TIMED_OUT) {
1327 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1328 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1329 mActiveClientManager.toString().string());
1330 return -EBUSY;
1331 }
1332 if (ret != NO_ERROR) {
1333 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1334 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1335 ret, mActiveClientManager.toString().string());
1336 return ret;
1337 }
1338 }
1339
1340 evictedClients.clear();
1341
Ruben Brunkcc776712015-02-17 20:18:47 -08001342 // Once clients have been disconnected, relock
1343 mServiceLock.lock();
1344
1345 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1346 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1347 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001348 }
1349
Ruben Brunkcc776712015-02-17 20:18:47 -08001350 *partial = clientDescriptor;
1351 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001352}
1353
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001354Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001355 const sp<ICameraClient>& cameraClient,
1356 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001357 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001358 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001359 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001360 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001361 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001362
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001363 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001364 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001365 String8 id = String8::format("%d", cameraId);
1366 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001367 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1368 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1369 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1370 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001371
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001372 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001373 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001374 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001375 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001376 }
1377
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001378 *device = client;
1379 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380}
1381
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001382Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001383 const sp<ICameraClient>& cameraClient,
1384 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001385 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001386 int clientUid,
1387 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001388 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001389
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001390 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001391 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001392 if (mModule != nullptr) {
1393 int apiVersion = mModule->getModuleApiVersion();
1394 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
1395 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
1396 /*
1397 * Either the HAL version is unspecified in which case this just creates
1398 * a camera client selected by the latest device version, or
1399 * it's a particular version in which case the HAL must supported
1400 * the open_legacy call
1401 */
1402 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1403 apiVersion);
1404 ALOGE("%s: %s",
1405 __FUNCTION__, msg.string());
1406 logRejected(id, getCallingPid(), String8(clientPackageName),
1407 msg);
1408 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
1409 }
Zhijun Heb10cdad2014-06-16 16:38:35 -07001410 }
1411
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001412 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001413 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001414 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1415 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1416 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1417 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001418
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001419 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001420 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001421 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001422 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001423 }
1424
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001425 *device = client;
1426 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001427}
1428
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001429Status CameraService::connectDevice(
1430 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001431 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001432 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001433 int clientUid,
1434 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001435 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001436
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001437 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001438 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001439 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001440 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001441 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1442 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1443 clientUid, USE_CALLING_PID, API_2,
1444 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1445 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001446
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001447 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001448 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001449 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001450 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001451 }
1452
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001453 *device = client;
1454 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001455}
1456
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001457template<class CALLBACK, class CLIENT>
1458Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
1459 int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
1460 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
1461 /*out*/sp<CLIENT>& device) {
1462 binder::Status ret = binder::Status::ok();
1463
1464 String8 clientName8(clientPackageName);
1465
1466 int originalClientPid = 0;
1467
1468 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1469 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1470 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1471 static_cast<int>(effectiveApiLevel));
1472
1473 sp<CLIENT> client = nullptr;
1474 {
1475 // Acquire mServiceLock and prevent other clients from connecting
1476 std::unique_ptr<AutoConditionLock> lock =
1477 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1478
1479 if (lock == nullptr) {
1480 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1481 , clientPid);
1482 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1483 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1484 cameraId.string(), clientName8.string(), clientPid);
1485 }
1486
1487 // Enforce client permissions and do basic sanity checks
1488 if(!(ret = validateConnectLocked(cameraId, clientName8,
1489 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1490 return ret;
1491 }
1492
1493 // Check the shim parameters after acquiring lock, if they have already been updated and
1494 // we were doing a shim update, return immediately
1495 if (shimUpdateOnly) {
1496 auto cameraState = getCameraState(cameraId);
1497 if (cameraState != nullptr) {
1498 if (!cameraState->getShimParams().isEmpty()) return ret;
1499 }
1500 }
1501
1502 status_t err;
1503
1504 sp<BasicClient> clientTmp = nullptr;
1505 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1506 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1507 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1508 /*out*/&partial)) != NO_ERROR) {
1509 switch (err) {
1510 case -ENODEV:
1511 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1512 "No camera device with ID \"%s\" currently available",
1513 cameraId.string());
1514 case -EBUSY:
1515 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1516 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1517 cameraId.string());
1518 default:
1519 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1520 "Unexpected error %s (%d) opening camera \"%s\"",
1521 strerror(-err), err, cameraId.string());
1522 }
1523 }
1524
1525 if (clientTmp.get() != nullptr) {
1526 // Handle special case for API1 MediaRecorder where the existing client is returned
1527 device = static_cast<CLIENT*>(clientTmp.get());
1528 return ret;
1529 }
1530
1531 // give flashlight a chance to close devices if necessary.
1532 mFlashlight->prepareDeviceOpen(cameraId);
1533
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001534 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001535 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -08001536 if (facing == -1) {
1537 ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string());
1538 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1539 "Unable to get camera device \"%s\" facing", cameraId.string());
1540 }
1541
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001542 sp<BasicClient> tmp = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001543 if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001544 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
1545 /*out*/&tmp)).isOk()) {
1546 return ret;
1547 }
1548 client = static_cast<CLIENT*>(tmp.get());
1549
1550 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1551 __FUNCTION__);
1552
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001553 if (mModule != nullptr) {
1554 err = client->initialize(mModule);
1555 } else {
1556 err = client->initialize(mCameraProviderManager);
1557 }
1558
1559 if (err != OK) {
1560 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001561 // Errors could be from the HAL module open call or from AppOpsManager
1562 switch(err) {
1563 case BAD_VALUE:
1564 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1565 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1566 case -EBUSY:
1567 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1568 "Camera \"%s\" is already open", cameraId.string());
1569 case -EUSERS:
1570 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1571 "Too many cameras already open, cannot open camera \"%s\"",
1572 cameraId.string());
1573 case PERMISSION_DENIED:
1574 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1575 "No permission to open camera \"%s\"", cameraId.string());
1576 case -EACCES:
1577 return STATUS_ERROR_FMT(ERROR_DISABLED,
1578 "Camera \"%s\" disabled by policy", cameraId.string());
1579 case -ENODEV:
1580 default:
1581 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1582 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1583 strerror(-err), err);
1584 }
1585 }
1586
1587 // Update shim paremeters for legacy clients
1588 if (effectiveApiLevel == API_1) {
1589 // Assume we have always received a Client subclass for API1
1590 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1591 String8 rawParams = shimClient->getParameters();
1592 CameraParameters params(rawParams);
1593
1594 auto cameraState = getCameraState(cameraId);
1595 if (cameraState != nullptr) {
1596 cameraState->setShimParams(params);
1597 } else {
1598 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1599 __FUNCTION__, cameraId.string());
1600 }
1601 }
1602
1603 if (shimUpdateOnly) {
1604 // If only updating legacy shim parameters, immediately disconnect client
1605 mServiceLock.unlock();
1606 client->disconnect();
1607 mServiceLock.lock();
1608 } else {
1609 // Otherwise, add client to active clients list
1610 finishConnectLocked(client, partial);
1611 }
1612 } // lock is destroyed, allow further connect calls
1613
1614 // Important: release the mutex here so the client can call back into the service from its
1615 // destructor (can be at the end of the call)
1616 device = client;
1617 return ret;
1618}
1619
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001620Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001621 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001622 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001623
1624 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001625 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001626 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001627 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1628 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001629 }
1630
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001631 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001632 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001633
1634 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001635 auto state = getCameraState(id);
1636 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001637 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001638 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1639 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001640 }
1641
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001642 StatusInternal cameraStatus = state->getStatus();
1643 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001644 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1645 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001646 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1647 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001648 }
1649
1650 {
1651 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001652 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001653 status_t err = getTorchStatusLocked(id, &status);
1654 if (err != OK) {
1655 if (err == NAME_NOT_FOUND) {
1656 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1657 "Camera \"%s\" does not have a flash unit", id.string());
1658 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001659 ALOGE("%s: getting current torch status failed for camera %s",
1660 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001661 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1662 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1663 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001664 }
1665
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001666 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001667 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001668 ALOGE("%s: torch mode of camera %s is not available because "
1669 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001670 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1671 "Torch for camera \"%s\" is not available due to an existing camera user",
1672 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001673 } else {
1674 ALOGE("%s: torch mode of camera %s is not available due to "
1675 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001676 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1677 "Torch for camera \"%s\" is not available due to insufficient resources",
1678 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001679 }
1680 }
1681 }
1682
Ruben Brunk99e69712015-05-26 17:25:07 -07001683 {
1684 // Update UID map - this is used in the torch status changed callbacks, so must be done
1685 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001686 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001687 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1688 mTorchUidMap[id].first = uid;
1689 mTorchUidMap[id].second = uid;
1690 } else {
1691 // Set the pending UID
1692 mTorchUidMap[id].first = uid;
1693 }
1694 }
1695
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001696 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001697
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001698 if (err != OK) {
1699 int32_t errorCode;
1700 String8 msg;
1701 switch (err) {
1702 case -ENOSYS:
1703 msg = String8::format("Camera \"%s\" has no flashlight",
1704 id.string());
1705 errorCode = ERROR_ILLEGAL_ARGUMENT;
1706 break;
1707 default:
1708 msg = String8::format(
1709 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1710 id.string(), enabled, strerror(-err), err);
1711 errorCode = ERROR_INVALID_OPERATION;
1712 }
1713 ALOGE("%s: %s", __FUNCTION__, msg.string());
1714 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001715 }
1716
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001717 {
1718 // update the link to client's death
1719 Mutex::Autolock al(mTorchClientMapMutex);
1720 ssize_t index = mTorchClientMap.indexOfKey(id);
1721 if (enabled) {
1722 if (index == NAME_NOT_FOUND) {
1723 mTorchClientMap.add(id, clientBinder);
1724 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001725 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001726 mTorchClientMap.replaceValueAt(index, clientBinder);
1727 }
1728 clientBinder->linkToDeath(this);
1729 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001730 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001731 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001732 }
1733
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001734 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001735}
1736
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001737Status CameraService::notifySystemEvent(int32_t eventId,
1738 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001739 ATRACE_CALL();
1740
Ruben Brunk36597b22015-03-20 22:15:57 -07001741 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001742 case ICameraService::EVENT_USER_SWITCHED: {
1743 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001744 break;
1745 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001746 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001747 default: {
1748 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1749 eventId);
1750 break;
1751 }
1752 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001753 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001754}
1755
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001756Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1757 /*out*/
1758 std::vector<hardware::CameraStatus> *cameraStatuses) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001759 ATRACE_CALL();
1760
Igor Murashkinbfc99152013-02-27 12:55:20 -08001761 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001762
Ruben Brunk3450ba72015-06-16 11:00:37 -07001763 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001764 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001765 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001766 }
1767
Igor Murashkinbfc99152013-02-27 12:55:20 -08001768 Mutex::Autolock lock(mServiceLock);
1769
Ruben Brunkcc776712015-02-17 20:18:47 -08001770 {
1771 Mutex::Autolock lock(mStatusListenerLock);
1772 for (auto& it : mListenerList) {
1773 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1774 ALOGW("%s: Tried to add listener %p which was already subscribed",
1775 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001776 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001777 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001778 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001779
1780 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001781 }
1782
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001783 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001784 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001785 Mutex::Autolock lock(mCameraStatesLock);
1786 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001787 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001788 }
1789 }
1790
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001791 /*
1792 * Immediately signal current torch status to this listener only
1793 * This may be a subset of all the devices, so don't include it in the response directly
1794 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001795 {
1796 Mutex::Autolock al(mTorchStatusMutex);
1797 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001798 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001799 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001800 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001801 }
1802
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001803 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001804}
Ruben Brunkcc776712015-02-17 20:18:47 -08001805
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001806Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001807 ATRACE_CALL();
1808
Igor Murashkinbfc99152013-02-27 12:55:20 -08001809 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1810
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001811 if (listener == 0) {
1812 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001813 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001814 }
1815
Igor Murashkinbfc99152013-02-27 12:55:20 -08001816 Mutex::Autolock lock(mServiceLock);
1817
Ruben Brunkcc776712015-02-17 20:18:47 -08001818 {
1819 Mutex::Autolock lock(mStatusListenerLock);
1820 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1821 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1822 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001823 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001824 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001825 }
1826 }
1827
1828 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1829 __FUNCTION__, listener.get());
1830
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001831 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001832}
1833
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001834Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001835
1836 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001837 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1838
1839 if (parameters == NULL) {
1840 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001841 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001842 }
1843
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001844 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001845
1846 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001847 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001848 // Error logged by caller
1849 return ret;
1850 }
1851
1852 String8 shimParamsString8 = shimParams.flatten();
1853 String16 shimParamsString16 = String16(shimParamsString8);
1854
1855 *parameters = shimParamsString16;
1856
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001857 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001858}
1859
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001860Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
1861 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001862 ATRACE_CALL();
1863
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001864 const String8 id = String8(cameraId);
1865
1866 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001867
1868 switch (apiVersion) {
1869 case API_VERSION_1:
1870 case API_VERSION_2:
1871 break;
1872 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001873 String8 msg = String8::format("Unknown API version %d", apiVersion);
1874 ALOGE("%s: %s", __FUNCTION__, msg.string());
1875 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001876 }
1877
Emilian Peev28ad2ea2017-02-07 16:14:32 +00001878 int deviceVersion = getDeviceVersion(id);
Igor Murashkin65d14b92014-06-17 12:03:20 -07001879 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001880 case CAMERA_DEVICE_API_VERSION_1_0:
1881 case CAMERA_DEVICE_API_VERSION_3_0:
1882 case CAMERA_DEVICE_API_VERSION_3_1:
1883 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001884 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
1885 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001886 *isSupported = false;
1887 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001888 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
1889 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001890 *isSupported = true;
1891 }
1892 break;
1893 case CAMERA_DEVICE_API_VERSION_3_2:
1894 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001895 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001896 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
1897 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001898 *isSupported = true;
1899 break;
1900 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001901 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001902 ALOGE("%s: %s", __FUNCTION__, msg.string());
1903 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001904 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001905 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001906 String8 msg = String8::format("Unknown device version %x for device %s",
1907 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001908 ALOGE("%s: %s", __FUNCTION__, msg.string());
1909 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1910 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001911 }
1912
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001913 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001914}
1915
Ruben Brunkcc776712015-02-17 20:18:47 -08001916void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001917 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001918 for (auto& i : mActiveClientManager.getAll()) {
1919 auto clientSp = i->getValue();
1920 if (clientSp.get() == client) {
1921 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001922 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001923 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001924}
1925
Ruben Brunkcc776712015-02-17 20:18:47 -08001926bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1927 const int callingPid = getCallingPid();
1928 const int servicePid = getpid();
1929 bool ret = false;
1930 {
1931 // Acquire mServiceLock and prevent other clients from connecting
1932 std::unique_ptr<AutoConditionLock> lock =
1933 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001934
Igor Murashkin634a5152013-02-20 17:15:11 -08001935
Ruben Brunkcc776712015-02-17 20:18:47 -08001936 std::vector<sp<BasicClient>> evicted;
1937 for (auto& i : mActiveClientManager.getAll()) {
1938 auto clientSp = i->getValue();
1939 if (clientSp.get() == nullptr) {
1940 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1941 mActiveClientManager.remove(i);
1942 continue;
1943 }
1944 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1945 callingPid == clientSp->getClientPid())) {
1946 mActiveClientManager.remove(i);
1947 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001948
Ruben Brunkcc776712015-02-17 20:18:47 -08001949 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001950 clientSp->notifyError(
1951 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001952 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001953 }
1954 }
1955
Ruben Brunkcc776712015-02-17 20:18:47 -08001956 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1957 // other clients from connecting in mServiceLockWrapper if held
1958 mServiceLock.unlock();
1959
Ruben Brunk36597b22015-03-20 22:15:57 -07001960 // Do not clear caller identity, remote caller should be client proccess
1961
Ruben Brunkcc776712015-02-17 20:18:47 -08001962 for (auto& i : evicted) {
1963 if (i.get() != nullptr) {
1964 i->disconnect();
1965 ret = true;
1966 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001967 }
1968
Ruben Brunkcc776712015-02-17 20:18:47 -08001969 // Reacquire mServiceLock
1970 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001971
Ruben Brunkcc776712015-02-17 20:18:47 -08001972 } // lock is destroyed, allow further connect calls
1973
1974 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001975}
1976
Igor Murashkinecf17e82012-10-02 16:05:11 -07001977
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001978/**
1979 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001980 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001981 */
1982int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001983 if (mModule == nullptr) return NO_INIT;
1984
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001985 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1986 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1987 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1988 // Verify the device version is in the supported range
1989 switch (info.device_version) {
1990 case CAMERA_DEVICE_API_VERSION_1_0:
1991 case CAMERA_DEVICE_API_VERSION_3_0:
1992 case CAMERA_DEVICE_API_VERSION_3_1:
1993 case CAMERA_DEVICE_API_VERSION_3_2:
1994 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001995 case CAMERA_DEVICE_API_VERSION_3_4:
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001996 // in support
1997 break;
1998 case CAMERA_DEVICE_API_VERSION_2_0:
1999 case CAMERA_DEVICE_API_VERSION_2_1:
2000 // no longer supported
2001 default:
2002 ALOGE("%s: Device %d has HAL version %x, which is not supported",
2003 __FUNCTION__, id, info.device_version);
2004 String8 msg = String8::format(
2005 "Unsupported device HAL version %x for device %d",
2006 info.device_version, id);
2007 logServiceError(msg.string(), NO_INIT);
2008 return NO_INIT;
2009 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002010 }
2011
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002012 // Assume all devices pre-v3.3 are backward-compatible
2013 bool isBackwardCompatible = true;
2014 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
2015 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
2016 isBackwardCompatible = false;
2017 status_t res;
2018 camera_metadata_ro_entry_t caps;
2019 res = find_camera_metadata_ro_entry(
2020 info.static_camera_characteristics,
2021 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
2022 &caps);
2023 if (res != 0) {
2024 ALOGW("%s: Unable to find camera capabilities for camera device %d",
2025 __FUNCTION__, id);
2026 caps.count = 0;
2027 }
2028 for (size_t i = 0; i < caps.count; i++) {
2029 if (caps.data.u8[i] ==
2030 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
2031 isBackwardCompatible = true;
2032 break;
2033 }
2034 }
2035 }
2036
2037 if (!isBackwardCompatible) {
2038 mNumberOfNormalCameras--;
2039 *latestStrangeCameraId = id;
2040 } else {
2041 if (id > *latestStrangeCameraId) {
2042 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
2043 "This is not allowed due backward-compatibility requirements",
2044 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002045 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002046 mNumberOfCameras = 0;
2047 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002048 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002049 }
2050 }
2051 return OK;
2052}
2053
Ruben Brunkcc776712015-02-17 20:18:47 -08002054std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
2055 const String8& cameraId) const {
2056 std::shared_ptr<CameraState> state;
2057 {
2058 Mutex::Autolock lock(mCameraStatesLock);
2059 auto iter = mCameraStates.find(cameraId);
2060 if (iter != mCameraStates.end()) {
2061 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062 }
2063 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002064 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002065}
2066
Ruben Brunkcc776712015-02-17 20:18:47 -08002067sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
2068 // Remove from active clients list
2069 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
2070 if (clientDescriptorPtr == nullptr) {
2071 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
2072 cameraId.string());
2073 return sp<BasicClient>{nullptr};
2074 }
2075
2076 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07002077}
2078
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002079void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07002080 // Acquire mServiceLock and prevent other clients from connecting
2081 std::unique_ptr<AutoConditionLock> lock =
2082 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
2083
Ruben Brunk6267b532015-04-30 17:44:07 -07002084 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002085 for (size_t i = 0; i < newUserIds.size(); i++) {
2086 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07002087 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002088 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07002089 return;
2090 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002091 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07002092 }
2093
Ruben Brunka8ca9152015-04-07 14:23:40 -07002094
Ruben Brunk6267b532015-04-30 17:44:07 -07002095 if (newAllowedUsers == mAllowedUsers) {
2096 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
2097 return;
2098 }
2099
2100 logUserSwitch(mAllowedUsers, newAllowedUsers);
2101
2102 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07002103
2104 // Current user has switched, evict all current clients.
2105 std::vector<sp<BasicClient>> evicted;
2106 for (auto& i : mActiveClientManager.getAll()) {
2107 auto clientSp = i->getValue();
2108
2109 if (clientSp.get() == nullptr) {
2110 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
2111 continue;
2112 }
2113
Ruben Brunk6267b532015-04-30 17:44:07 -07002114 // Don't evict clients that are still allowed.
2115 uid_t clientUid = clientSp->getClientUid();
2116 userid_t clientUserId = multiuser_get_user_id(clientUid);
2117 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
2118 continue;
2119 }
2120
Ruben Brunk36597b22015-03-20 22:15:57 -07002121 evicted.push_back(clientSp);
2122
2123 String8 curTime = getFormattedCurrentTime();
2124
2125 ALOGE("Evicting conflicting client for camera ID %s due to user change",
2126 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07002127
Ruben Brunk36597b22015-03-20 22:15:57 -07002128 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002129 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00002130 PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due"
2131 " to user switch.", i->getKey().string(),
2132 String8{clientSp->getPackageName()}.string(),
2133 i->getOwnerId(), i->getPriority().getScore(),
2134 i->getPriority().getState()));
Ruben Brunk36597b22015-03-20 22:15:57 -07002135
2136 }
2137
2138 // Do not hold mServiceLock while disconnecting clients, but retain the condition
2139 // blocking other clients from connecting in mServiceLockWrapper if held.
2140 mServiceLock.unlock();
2141
2142 // Clear caller identity temporarily so client disconnect PID checks work correctly
2143 int64_t token = IPCThreadState::self()->clearCallingIdentity();
2144
2145 for (auto& i : evicted) {
2146 i->disconnect();
2147 }
2148
2149 IPCThreadState::self()->restoreCallingIdentity(token);
2150
2151 // Reacquire mServiceLock
2152 mServiceLock.lock();
2153}
Ruben Brunkcc776712015-02-17 20:18:47 -08002154
Ruben Brunka8ca9152015-04-07 14:23:40 -07002155void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002156 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07002157 Mutex::Autolock l(mLogLock);
2158 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07002159}
2160
Ruben Brunka8ca9152015-04-07 14:23:40 -07002161void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002162 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002163 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002164 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002165 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002166}
2167
2168void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002169 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002170 // Log the clients evicted
2171 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002172 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002173}
2174
2175void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002176 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002177 // Log the client rejected
2178 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002179 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002180}
2181
Ruben Brunk6267b532015-04-30 17:44:07 -07002182void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
2183 const std::set<userid_t>& newUserIds) {
2184 String8 newUsers = toString(newUserIds);
2185 String8 oldUsers = toString(oldUserIds);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002186 if (oldUsers.size() == 0) {
2187 oldUsers = "<None>";
2188 }
Ruben Brunka8ca9152015-04-07 14:23:40 -07002189 // Log the new and old users
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002190 logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s",
Ruben Brunk6267b532015-04-30 17:44:07 -07002191 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002192}
2193
2194void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
2195 // Log the device removal
2196 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
2197}
2198
2199void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
2200 // Log the device removal
2201 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
2202}
2203
2204void CameraService::logClientDied(int clientPid, const char* reason) {
2205 // Log the device removal
2206 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07002207}
2208
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002209void CameraService::logServiceError(const char* msg, int errorCode) {
2210 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002211 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002212}
2213
Ruben Brunk36597b22015-03-20 22:15:57 -07002214status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
2215 uint32_t flags) {
2216
2217 const int pid = getCallingPid();
2218 const int selfPid = getpid();
2219
Mathias Agopian65ab4712010-07-14 17:59:35 -07002220 // Permission checks
2221 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002222 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07002223 if (pid != selfPid) {
2224 // Ensure we're being called by system_server, or similar process with
2225 // permissions to notify the camera service about system events
2226 if (!checkCallingPermission(
2227 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
2228 const int uid = getCallingUid();
2229 ALOGE("Permission Denial: cannot send updates to camera service about system"
2230 " events from pid=%d, uid=%d", pid, uid);
2231 return PERMISSION_DENIED;
2232 }
2233 }
2234 break;
2235 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002236 }
2237
2238 return BnCameraService::onTransact(code, data, reply, flags);
2239}
2240
Mathias Agopian65ab4712010-07-14 17:59:35 -07002241// We share the media players for shutter and recording sound for all clients.
2242// A reference count is kept to determine when we will actually release the
2243// media players.
2244
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08002245MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002246 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08002247 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08002248 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002249 mp->prepare();
2250 } else {
Steve Block29357bc2012-01-06 19:20:56 +00002251 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002252 return NULL;
2253 }
2254 return mp;
2255}
2256
2257void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002258 ATRACE_CALL();
2259
Mathias Agopian65ab4712010-07-14 17:59:35 -07002260 Mutex::Autolock lock(mSoundLock);
2261 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2262 if (mSoundRef++) return;
2263
2264 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002265 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2266 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002267}
2268
2269void CameraService::releaseSound() {
2270 Mutex::Autolock lock(mSoundLock);
2271 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2272 if (--mSoundRef) return;
2273
2274 for (int i = 0; i < NUM_SOUNDS; i++) {
2275 if (mSoundPlayer[i] != 0) {
2276 mSoundPlayer[i]->disconnect();
2277 mSoundPlayer[i].clear();
2278 }
2279 }
2280}
2281
2282void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002283 ATRACE_CALL();
2284
Mathias Agopian65ab4712010-07-14 17:59:35 -07002285 LOG1("playSound(%d)", kind);
2286 Mutex::Autolock lock(mSoundLock);
2287 sp<MediaPlayer> player = mSoundPlayer[kind];
2288 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002289 player->seekTo(0);
2290 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002291 }
2292}
2293
2294// ----------------------------------------------------------------------------
2295
2296CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002297 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002298 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002299 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002300 int clientPid, uid_t clientUid,
2301 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002302 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002303 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002304 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002305 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002306 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002307 servicePid),
2308 mCameraId(CameraService::cameraIdToInt(cameraIdStr))
Igor Murashkin634a5152013-02-20 17:15:11 -08002309{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002310 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002311 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002312
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002313 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002314
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002316
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002317 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318}
2319
Mathias Agopian65ab4712010-07-14 17:59:35 -07002320// tear down the client
2321CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002322 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002323 mDestructionStarted = true;
2324
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002325 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002326 // unconditionally disconnect. function is idempotent
2327 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002328}
2329
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002330sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2331
Igor Murashkin634a5152013-02-20 17:15:11 -08002332CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002333 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002334 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002335 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002336 int clientPid, uid_t clientUid,
2337 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002338 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2339 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2340 mServicePid(servicePid),
2341 mDisconnected(false),
2342 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002343{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002344 if (sCameraService == nullptr) {
2345 sCameraService = cameraService;
2346 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002347 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002348 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002349
2350 // In some cases the calling code has no access to the package it runs under.
2351 // For example, NDK camera API.
2352 // In this case we will get the packages for the calling UID and pick the first one
2353 // for attributing the app op. This will work correctly for runtime permissions
2354 // as for legacy apps we will toggle the app op for all packages in the UID.
2355 // The caveat is that the operation may be attributed to the wrong package and
2356 // stats based on app ops may be slightly off.
2357 if (mClientPackageName.size() <= 0) {
2358 sp<IServiceManager> sm = defaultServiceManager();
2359 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2360 if (binder == 0) {
2361 ALOGE("Cannot get permission service");
2362 // Leave mClientPackageName unchanged (empty) and the further interaction
2363 // with camera will fail in BasicClient::startCameraOps
2364 return;
2365 }
2366
2367 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2368 Vector<String16> packages;
2369
2370 permCtrl->getPackagesForUid(mClientUid, packages);
2371
2372 if (packages.isEmpty()) {
2373 ALOGE("No packages for calling UID");
2374 // Leave mClientPackageName unchanged (empty) and the further interaction
2375 // with camera will fail in BasicClient::startCameraOps
2376 return;
2377 }
2378 mClientPackageName = packages[0];
2379 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002380}
2381
2382CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002383 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002384 mDestructionStarted = true;
2385}
2386
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002387binder::Status CameraService::BasicClient::disconnect() {
2388 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002389 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002390 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002391 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002392 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002393
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002394 sCameraService->removeByClient(this);
2395 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002396 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002397
2398 sp<IBinder> remote = getRemote();
2399 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002400 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002401 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002402
2403 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002404 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002405 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2406 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2407 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002408
Igor Murashkincba2c162013-03-20 15:56:31 -07002409 // client shouldn't be able to call into us anymore
2410 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002411
2412 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002413}
2414
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002415status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2416 // No dumping of clients directly over Binder,
2417 // must go through CameraService::dump
2418 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2419 IPCThreadState::self()->getCallingUid(), NULL, 0);
2420 return OK;
2421}
2422
Ruben Brunkcc776712015-02-17 20:18:47 -08002423String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002424 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002425}
2426
2427
2428int CameraService::BasicClient::getClientPid() const {
2429 return mClientPid;
2430}
2431
Ruben Brunk6267b532015-04-30 17:44:07 -07002432uid_t CameraService::BasicClient::getClientUid() const {
2433 return mClientUid;
2434}
2435
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002436bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2437 // Defaults to API2.
2438 return level == API_2;
2439}
2440
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002441status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002442 ATRACE_CALL();
2443
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002444 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002445 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002446 mOpsCallback = new OpsCallback(this);
2447
Igor Murashkine6800ce2013-03-04 17:25:57 -08002448 {
2449 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002450 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002451 }
2452
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002453 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002454 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002455 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002456 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002457
Svetoslav28e8ef72015-05-11 19:21:31 -07002458 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002459 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2460 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002461 return PERMISSION_DENIED;
2462 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002463
Svetoslav28e8ef72015-05-11 19:21:31 -07002464 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002465 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2466 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002467 // Return the same error as for device policy manager rejection
2468 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002469 }
2470
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002471 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002472
2473 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002474 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002475
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002476 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002477 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2478 mCameraIdStr);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002479
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002480 return OK;
2481}
2482
2483status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002484 ATRACE_CALL();
2485
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002486 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002487 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002488 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002489 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002490 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002491 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002492
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002493 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
2494 StatusInternal::ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002495
Ruben Brunkcc776712015-02-17 20:18:47 -08002496 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002497 sCameraService->updateStatus(StatusInternal::PRESENT,
2498 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002499
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002500 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002501 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2502 mCameraIdStr);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002503 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002504 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002505 if (mOpsCallback != NULL) {
2506 mAppOpsManager.stopWatchingMode(mOpsCallback);
2507 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002508 mOpsCallback.clear();
2509
2510 return OK;
2511}
2512
2513void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002514 ATRACE_CALL();
2515
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002516 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002517 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002518
2519 if (op != AppOpsManager::OP_CAMERA) {
2520 ALOGW("Unexpected app ops notification received: %d", op);
2521 return;
2522 }
2523
2524 int32_t res;
2525 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002526 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002527 ALOGV("checkOp returns: %d, %s ", res,
2528 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2529 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2530 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2531 "UNKNOWN");
2532
2533 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002534 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002535 myName.string());
2536 // Reset the client PID to allow server-initiated disconnect,
2537 // and to prevent further calls by client.
2538 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002539 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002540 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002541 disconnect();
2542 }
2543}
2544
Mathias Agopian65ab4712010-07-14 17:59:35 -07002545// ----------------------------------------------------------------------------
2546
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002547void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002548 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002549 (void) errorCode;
2550 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002551 if (mRemoteCallback != NULL) {
2552 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2553 } else {
2554 ALOGE("mRemoteCallback is NULL!!");
2555 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002556}
2557
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002558// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002559binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002560 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002561 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002562}
2563
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002564bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2565 return level == API_1;
2566}
2567
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002568CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2569 mClient(client) {
2570}
2571
2572void CameraService::Client::OpsCallback::opChanged(int32_t op,
2573 const String16& packageName) {
2574 sp<BasicClient> client = mClient.promote();
2575 if (client != NULL) {
2576 client->opChanged(op, packageName);
2577 }
2578}
2579
Mathias Agopian65ab4712010-07-14 17:59:35 -07002580// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002581// CameraState
2582// ----------------------------------------------------------------------------
2583
2584CameraService::CameraState::CameraState(const String8& id, int cost,
2585 const std::set<String8>& conflicting) : mId(id),
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002586 mStatus(StatusInternal::PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002587
2588CameraService::CameraState::~CameraState() {}
2589
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002590CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002591 Mutex::Autolock lock(mStatusLock);
2592 return mStatus;
2593}
2594
2595CameraParameters CameraService::CameraState::getShimParams() const {
2596 return mShimParams;
2597}
2598
2599void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2600 mShimParams = params;
2601}
2602
2603int CameraService::CameraState::getCost() const {
2604 return mCost;
2605}
2606
2607std::set<String8> CameraService::CameraState::getConflicting() const {
2608 return mConflicting;
2609}
2610
2611String8 CameraService::CameraState::getId() const {
2612 return mId;
2613}
2614
2615// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002616// ClientEventListener
2617// ----------------------------------------------------------------------------
2618
2619void CameraService::ClientEventListener::onClientAdded(
2620 const resource_policy::ClientDescriptor<String8,
2621 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002622 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002623 if (basicClient.get() != nullptr) {
2624 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2625 notifier.noteStartCamera(descriptor.getKey(),
2626 static_cast<int>(basicClient->getClientUid()));
2627 }
2628}
2629
2630void CameraService::ClientEventListener::onClientRemoved(
2631 const resource_policy::ClientDescriptor<String8,
2632 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002633 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002634 if (basicClient.get() != nullptr) {
2635 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2636 notifier.noteStopCamera(descriptor.getKey(),
2637 static_cast<int>(basicClient->getClientUid()));
2638 }
2639}
2640
2641
2642// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002643// CameraClientManager
2644// ----------------------------------------------------------------------------
2645
Ruben Brunk99e69712015-05-26 17:25:07 -07002646CameraService::CameraClientManager::CameraClientManager() {
2647 setListener(std::make_shared<ClientEventListener>());
2648}
2649
Ruben Brunkcc776712015-02-17 20:18:47 -08002650CameraService::CameraClientManager::~CameraClientManager() {}
2651
2652sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2653 const String8& id) const {
2654 auto descriptor = get(id);
2655 if (descriptor == nullptr) {
2656 return sp<BasicClient>{nullptr};
2657 }
2658 return descriptor->getValue();
2659}
2660
2661String8 CameraService::CameraClientManager::toString() const {
2662 auto all = getAll();
2663 String8 ret("[");
2664 bool hasAny = false;
2665 for (auto& i : all) {
2666 hasAny = true;
2667 String8 key = i->getKey();
2668 int32_t cost = i->getCost();
2669 int32_t pid = i->getOwnerId();
Emilian Peev8131a262017-02-01 12:33:43 +00002670 int32_t score = i->getPriority().getScore();
2671 int32_t state = i->getPriority().getState();
Ruben Brunkcc776712015-02-17 20:18:47 -08002672 auto conflicting = i->getConflicting();
2673 auto clientSp = i->getValue();
2674 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002675 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002676 if (clientSp.get() != nullptr) {
2677 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002678 uid_t clientUid = clientSp->getClientUid();
2679 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002680 }
Emilian Peev8131a262017-02-01 12:33:43 +00002681 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %"
2682 PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002683
Ruben Brunk6267b532015-04-30 17:44:07 -07002684 if (clientSp.get() != nullptr) {
2685 ret.appendFormat("User Id: %d, ", clientUserId);
2686 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002687 if (packageName.size() != 0) {
2688 ret.appendFormat("Client Package Name: %s", packageName.string());
2689 }
2690
2691 ret.append(", Conflicting Client Devices: {");
2692 for (auto& j : conflicting) {
2693 ret.appendFormat("%s, ", j.string());
2694 }
2695 ret.append("})");
2696 }
2697 if (hasAny) ret.append("\n");
2698 ret.append("]\n");
2699 return ret;
2700}
2701
2702CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2703 const String8& key, const sp<BasicClient>& value, int32_t cost,
Emilian Peev8131a262017-02-01 12:33:43 +00002704 const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId,
2705 int32_t state) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002706
2707 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
Emilian Peev8131a262017-02-01 12:33:43 +00002708 key, value, cost, conflictingKeys, score, ownerId, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002709}
2710
2711CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2712 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2713 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
Emilian Peev8131a262017-02-01 12:33:43 +00002714 partial->getConflicting(), partial->getPriority().getScore(),
2715 partial->getOwnerId(), partial->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08002716}
2717
2718// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002719
2720static const int kDumpLockRetries = 50;
2721static const int kDumpLockSleep = 60000;
2722
2723static bool tryLock(Mutex& mutex)
2724{
2725 bool locked = false;
2726 for (int i = 0; i < kDumpLockRetries; ++i) {
2727 if (mutex.tryLock() == NO_ERROR) {
2728 locked = true;
2729 break;
2730 }
2731 usleep(kDumpLockSleep);
2732 }
2733 return locked;
2734}
2735
2736status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002737 ATRACE_CALL();
2738
Mathias Agopian65ab4712010-07-14 17:59:35 -07002739 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002740 dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07002741 getCallingPid(),
2742 getCallingUid());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002743 return NO_ERROR;
2744 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002745 bool locked = tryLock(mServiceLock);
2746 // failed to lock - CameraService is probably deadlocked
2747 if (!locked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002748 dprintf(fd, "!! CameraService may be deadlocked !!\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002749 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002750
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002751 if (!mInitialized) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002752 dprintf(fd, "!! No camera HAL available !!\n");
Ruben Brunkf81648e2014-04-17 16:14:57 -07002753
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002754 // Dump event log for error information
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002755 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002756
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002757 if (locked) mServiceLock.unlock();
2758 return NO_ERROR;
2759 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002760 dprintf(fd, "\n== Service global info: ==\n\n");
2761 dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras);
2762 dprintf(fd, "Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002763 String8 activeClientString = mActiveClientManager.toString();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002764 dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string());
2765 dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002766
2767 dumpEventLog(fd);
2768
2769 bool stateLocked = tryLock(mCameraStatesLock);
2770 if (!stateLocked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002771 dprintf(fd, "CameraStates in use, may be deadlocked\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002772 }
2773
2774 for (auto& state : mCameraStates) {
2775 String8 cameraId = state.first;
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002776
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002777 dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002778
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002779 CameraParameters p = state.second->getShimParams();
2780 if (!p.isEmpty()) {
2781 dprintf(fd, " Camera1 API shim is using parameters:\n ");
2782 p.dump(fd, args);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002783 }
2784
2785 auto clientDescriptor = mActiveClientManager.get(cameraId);
Zhijun He2f140ed2017-02-08 09:57:23 -08002786 if (clientDescriptor != nullptr) {
2787 dprintf(fd, " Device %s is open. Client instance dump:\n",
2788 cameraId.string());
2789 dprintf(fd, " Client priority score: %d state: %d\n",
2790 clientDescriptor->getPriority().getScore(),
2791 clientDescriptor->getPriority().getState());
2792 dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId());
2793
2794 auto client = clientDescriptor->getValue();
2795 dprintf(fd, " Client package: %s\n",
2796 String8(client->getPackageName()).string());
2797
2798 client->dumpClient(fd, args);
2799 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002800 dprintf(fd, " Device %s is closed, no client instance\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002801 cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002802 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002803
2804 if (mModule != nullptr) {
2805 dprintf(fd, "== Camera HAL device %s static information: ==\n", cameraId.string());
2806
2807 camera_info info;
2808 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
2809 int deviceVersion = -1;
2810 if (rc != OK) {
2811 dprintf(fd, " Error reading static information!\n");
2812 } else {
2813 dprintf(fd, " Facing: %s\n",
2814 info.facing == CAMERA_FACING_BACK ? "BACK" :
2815 info.facing == CAMERA_FACING_FRONT ? "FRONT" : "EXTERNAL");
2816 dprintf(fd, " Orientation: %d\n", info.orientation);
2817
2818 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
2819 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2820 } else {
2821 deviceVersion = info.device_version;
2822 }
2823 }
2824
2825 auto conflicting = state.second->getConflicting();
2826 dprintf(fd, " Resource Cost: %d\n", state.second->getCost());
2827 dprintf(fd, " Conflicting Devices:");
2828 for (auto& id : conflicting) {
2829 dprintf(fd, " %s", id.string());
2830 }
2831 if (conflicting.size() == 0) {
2832 dprintf(fd, " NONE");
2833 }
2834 dprintf(fd, "\n");
2835
2836 dprintf(fd, " Device version: %#x\n", deviceVersion);
2837 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
2838 dprintf(fd, " Device static metadata:\n");
2839 dump_indented_camera_metadata(info.static_camera_characteristics,
2840 fd, /*verbosity*/2, /*indentation*/4);
2841 }
2842 }
2843
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002844 }
2845
2846 if (stateLocked) mCameraStatesLock.unlock();
2847
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002848 if (locked) mServiceLock.unlock();
2849
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002850 if (mModule == nullptr) {
2851 mCameraProviderManager->dump(fd, args);
2852 } else {
2853 dprintf(fd, "\n== Camera Module HAL static info: ==\n");
2854 dprintf(fd, "Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2855 dprintf(fd, "Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2856 dprintf(fd, "Camera module name: %s\n", mModule->getModuleName());
2857 dprintf(fd, "Camera module author: %s\n", mModule->getModuleAuthor());
2858 }
2859
2860 dprintf(fd, "\n== Vendor tags: ==\n\n");
2861
2862 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2863 if (desc == NULL) {
2864 dprintf(fd, "No vendor tags.\n");
2865 } else {
2866 desc->dump(fd, /*verbosity*/2, /*indentation*/2);
2867 }
2868
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002869 // Dump camera traces if there were any
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002870 dprintf(fd, "\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002871 camera3::CameraTraces::dump(fd, args);
2872
2873 // Process dump arguments, if any
2874 int n = args.size();
2875 String16 verboseOption("-v");
2876 String16 unreachableOption("--unreachable");
2877 for (int i = 0; i < n; i++) {
2878 if (args[i] == verboseOption) {
2879 // change logging level
2880 if (i + 1 >= n) continue;
2881 String8 levelStr(args[i+1]);
2882 int level = atoi(levelStr.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002883 dprintf(fd, "\nSetting log level to %d.\n", level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002884 setLogLevel(level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002885 } else if (args[i] == unreachableOption) {
2886 // Dump memory analysis
2887 // TODO - should limit be an argument parameter?
2888 UnreachableMemoryInfo info;
2889 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2890 if (!success) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002891 dprintf(fd, "\n== Unable to dump unreachable memory. "
2892 "Try disabling SELinux enforcement. ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002893 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002894 dprintf(fd, "\n== Dumping unreachable memory: ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002895 std::string s = info.ToString(/*log_contents*/ true);
2896 write(fd, s.c_str(), s.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002897 }
2898 }
2899 }
2900 return NO_ERROR;
2901}
2902
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002903void CameraService::dumpEventLog(int fd) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002904 dprintf(fd, "\n== Camera service events log (most recent at top): ==\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002905
2906 Mutex::Autolock l(mLogLock);
2907 for (const auto& msg : mEventLog) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002908 dprintf(fd, " %s\n", msg.string());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002909 }
2910
2911 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002912 dprintf(fd, " ...\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002913 } else if (mEventLog.size() == 0) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002914 dprintf(fd, " [no events yet]\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002915 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002916 dprintf(fd, "\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002917}
2918
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002919void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002920 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002921 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2922 if (mTorchClientMap[i] == who) {
2923 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002924 String8 cameraId = mTorchClientMap.keyAt(i);
2925 status_t res = mFlashlight->setTorchMode(cameraId, false);
2926 if (res) {
2927 ALOGE("%s: torch client died but couldn't turn off torch: "
2928 "%s (%d)", __FUNCTION__, strerror(-res), res);
2929 return;
2930 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002931 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002932 break;
2933 }
2934 }
2935}
2936
Ruben Brunkcc776712015-02-17 20:18:47 -08002937/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002938
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002939 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002940 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2941 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002942 */
2943
Ruben Brunka8ca9152015-04-07 14:23:40 -07002944 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2945
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002946 // check torch client
2947 handleTorchClientBinderDied(who);
2948
2949 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002950 if(!evictClientIdByRemote(who)) {
2951 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002952 return;
2953 }
2954
Ruben Brunkcc776712015-02-17 20:18:47 -08002955 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2956 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002957}
2958
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002959void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002960 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002961}
2962
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002963void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2964 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002965 // Do not lock mServiceLock here or can get into a deadlock from
2966 // connect() -> disconnect -> updateStatus
2967
2968 auto state = getCameraState(cameraId);
2969
2970 if (state == nullptr) {
2971 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2972 cameraId.string());
2973 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002974 }
2975
Ruben Brunkcc776712015-02-17 20:18:47 -08002976 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2977 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2978 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002979 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002980
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002981 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002982 // Update torch status if it has a flash unit.
2983 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002984 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002985 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2986 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002987 TorchModeStatus newTorchStatus =
2988 status == StatusInternal::PRESENT ?
2989 TorchModeStatus::AVAILABLE_OFF :
2990 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002991 if (torchStatus != newTorchStatus) {
2992 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2993 }
2994 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002995 }
2996
2997 Mutex::Autolock lock(mStatusListenerLock);
2998
2999 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003000 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08003001 }
3002 });
Igor Murashkincba2c162013-03-20 15:56:31 -07003003}
3004
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003005template<class Func>
3006void CameraService::CameraState::updateStatus(StatusInternal status,
3007 const String8& cameraId,
3008 std::initializer_list<StatusInternal> rejectSourceStates,
3009 Func onStatusUpdatedLocked) {
3010 Mutex::Autolock lock(mStatusLock);
3011 StatusInternal oldStatus = mStatus;
3012 mStatus = status;
3013
3014 if (oldStatus == status) {
3015 return;
3016 }
3017
3018 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
3019 cameraId.string(), oldStatus, status);
3020
3021 if (oldStatus == StatusInternal::NOT_PRESENT &&
3022 (status != StatusInternal::PRESENT &&
3023 status != StatusInternal::ENUMERATING)) {
3024
3025 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
3026 __FUNCTION__);
3027 mStatus = oldStatus;
3028 return;
3029 }
3030
3031 /**
3032 * Sometimes we want to conditionally do a transition.
3033 * For example if a client disconnects, we want to go to PRESENT
3034 * only if we weren't already in NOT_PRESENT or ENUMERATING.
3035 */
3036 for (auto& rejectStatus : rejectSourceStates) {
3037 if (oldStatus == rejectStatus) {
3038 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
3039 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
3040 mStatus = oldStatus;
3041 return;
3042 }
3043 }
3044
3045 onStatusUpdatedLocked(cameraId, status);
3046}
3047
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07003048void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
3049 const String8& cameraId) {
3050 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
3051 if (proxyBinder == nullptr) return;
3052 String16 id(cameraId);
3053 proxyBinder->notifyCameraState(id, newState);
3054}
3055
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003056status_t CameraService::getTorchStatusLocked(
3057 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003058 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003059 if (!status) {
3060 return BAD_VALUE;
3061 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003062 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3063 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003064 // invalid camera ID or the camera doesn't have a flash unit
3065 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003066 }
3067
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003068 *status = mTorchStatusMap.valueAt(index);
3069 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003070}
3071
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003072status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003073 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003074 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3075 if (index == NAME_NOT_FOUND) {
3076 return BAD_VALUE;
3077 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003078 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003079
3080 return OK;
3081}
3082
Mathias Agopian65ab4712010-07-14 17:59:35 -07003083}; // namespace android