blob: aec77948d0d2041ca880deb0dd9c1b940bc65cec [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
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700426 // Remove the client from the list of active clients, if there is one
Ruben Brunkcc776712015-02-17 20:18:47 -0800427 clientToDisconnect = removeClientLocked(id);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700428 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800429
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700430 // Disconnect client
431 if (clientToDisconnect.get() != nullptr) {
432 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
433 __FUNCTION__, id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800434 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800435 clientToDisconnect->notifyError(
436 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800437 CaptureResultExtras{});
Ruben Brunkcc776712015-02-17 20:18:47 -0800438 // Ensure not in binder RPC so client disconnect PID checks work correctly
439 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
440 "onDeviceStatusChanged must be called from the camera service process!");
441 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700442 }
443
Ruben Brunkcc776712015-02-17 20:18:47 -0800444 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800445 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700446 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
447 newStatus));
448 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800449 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700450 }
451
Igor Murashkincba2c162013-03-20 15:56:31 -0700452}
453
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800454void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800455 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800456 Mutex::Autolock al(mTorchStatusMutex);
457 onTorchStatusChangedLocked(cameraId, newStatus);
458}
459
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800460void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800461 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800462 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
463 __FUNCTION__, cameraId.string(), newStatus);
464
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800465 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800466 status_t res = getTorchStatusLocked(cameraId, &status);
467 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700468 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
469 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800470 return;
471 }
472 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800473 return;
474 }
475
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800476 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800477 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800478 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
479 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800480 return;
481 }
482
Ruben Brunkcc776712015-02-17 20:18:47 -0800483 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700484 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700485 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700486 auto iter = mTorchUidMap.find(cameraId);
487 if (iter != mTorchUidMap.end()) {
488 int oldUid = iter->second.second;
489 int newUid = iter->second.first;
490 BatteryNotifier& notifier(BatteryNotifier::getInstance());
491 if (oldUid != newUid) {
492 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800493 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700494 notifier.noteFlashlightOff(cameraId, oldUid);
495 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800496 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700497 notifier.noteFlashlightOn(cameraId, newUid);
498 }
499 iter->second.second = newUid;
500 } else {
501 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800502 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700503 notifier.noteFlashlightOn(cameraId, oldUid);
504 } else {
505 notifier.noteFlashlightOff(cameraId, oldUid);
506 }
507 }
508 }
509 }
510
511 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800512 Mutex::Autolock lock(mStatusListenerLock);
513 for (auto& i : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800514 i->onTorchStatusChanged(mapToInterface(newStatus), String16{cameraId});
Ruben Brunkcc776712015-02-17 20:18:47 -0800515 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800516 }
517}
518
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800519Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700520 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700521 switch (type) {
522 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800523 *numCameras = mNumberOfNormalCameras;
524 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700525 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800526 *numCameras = mNumberOfCameras;
527 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700528 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800529 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700530 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
532 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700533 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800534 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535}
536
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800537Status CameraService::getCameraInfo(int cameraId,
538 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700539 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800540 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800541 return STATUS_ERROR(ERROR_DISCONNECTED,
542 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700543 }
544
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800546 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
547 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 }
549
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800550 Status ret = Status::ok();
551 if (mModule != nullptr) {
552 struct camera_info info;
553 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800554
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 if (ret.isOk()) {
556 cameraInfo->facing = info.facing;
557 cameraInfo->orientation = info.orientation;
558 // CameraInfo is for android.hardware.Camera which does not
559 // support external camera facing. The closest approximation would be
560 // front camera.
561 if (cameraInfo->facing == CAMERA_FACING_EXTERNAL) {
562 cameraInfo->facing = hardware::CAMERA_FACING_FRONT;
563 }
Yin-Chia Yeh9b5a6e92016-04-22 13:24:05 -0700564 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800565 } else {
566 status_t err = mCameraProviderManager->getCameraInfo(std::to_string(cameraId), cameraInfo);
567 if (err != OK) {
568 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
569 "Error retrieving camera info from device %d: %s (%d)", cameraId,
570 strerror(-err), err);
571 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800572 }
573 return ret;
574}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700575
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800576int CameraService::cameraIdToInt(const String8& cameraId) {
577 int id;
578 bool success = base::ParseInt(cameraId.string(), &id, 0);
579 if (!success) {
580 return -1;
581 }
582 return id;
583}
584
585Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800586 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700587 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700588 if (!cameraInfo) {
589 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800590 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700591 }
592
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800593 if (!mInitialized) {
594 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800595 return STATUS_ERROR(ERROR_DISCONNECTED,
596 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700597 }
598
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800599 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800600
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800601 if (mModule != nullptr) {
602 int id = cameraIdToInt(String8(cameraId));
Zhijun He2b59be82013-09-25 10:14:30 -0700603
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800604 if (id < 0 || id >= mNumberOfCameras) {
605 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, id);
606 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
607 "Invalid camera id: %d", id);
608 }
609
610 int version = getDeviceVersion(String8(cameraId));
611 if (version < CAMERA_DEVICE_API_VERSION_3_0) {
612 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Can't get camera characteristics"
613 " for devices with HAL version < 3.0, %d is version %x", id, version);
614 }
615
616 struct camera_info info;
617 ret = filterGetInfoErrorCode(mModule->getCameraInfo(id, &info));
618 if (ret.isOk()) {
619 *cameraInfo = info.static_camera_characteristics;
620 }
621 } else {
622 status_t res = mCameraProviderManager->getCameraCharacteristics(
623 String16::std_string(cameraId), cameraInfo);
624 if (res != OK) {
625 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
626 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
627 strerror(-res), res);
628 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700629 }
Zhijun He2b59be82013-09-25 10:14:30 -0700630
631 return ret;
632}
633
Ruben Brunkcc776712015-02-17 20:18:47 -0800634int CameraService::getCallingPid() {
635 return IPCThreadState::self()->getCallingPid();
636}
637
638int CameraService::getCallingUid() {
639 return IPCThreadState::self()->getCallingUid();
640}
641
642String8 CameraService::getFormattedCurrentTime() {
643 time_t now = time(nullptr);
644 char formattedTime[64];
645 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
646 return String8(formattedTime);
647}
648
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800649Status CameraService::getCameraVendorTagDescriptor(
650 /*out*/
651 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700652 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800653 if (!mInitialized) {
654 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800655 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800656 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800657 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
658 if (globalDescriptor != nullptr) {
659 *desc = *(globalDescriptor.get());
660 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800661 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800662}
663
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800664int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700665 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800666
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800667 int deviceVersion = 0;
668
669 if (mModule != nullptr) {
670 int id = cameraIdToInt(cameraId);
671 if (id < 0) return -1;
672
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800673 struct camera_info info;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800674 if (mModule->getCameraInfo(id, &info) != OK) {
675 return -1;
676 }
677
678 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
679 deviceVersion = info.device_version;
680 } else {
681 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
682 }
683
684 if (facing) {
685 *facing = info.facing;
686 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800687 } else {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800688 status_t res;
689 hardware::hidl_version maxVersion{0,0};
690 res = mCameraProviderManager->getHighestSupportedVersion(String8::std_string(cameraId),
691 &maxVersion);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800692 if (res != OK) return -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800693 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800694
695 hardware::CameraInfo info;
696 if (facing) {
697 res = mCameraProviderManager->getCameraInfo(String8::std_string(cameraId), &info);
698 if (res != OK) return -1;
699 *facing = info.facing;
700 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800701 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800702 return deviceVersion;
703}
704
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800705Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700706 switch(err) {
707 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800708 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800709 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800710 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
711 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800712 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800713 return STATUS_ERROR(ERROR_DISCONNECTED,
714 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700715 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800716 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
717 "Camera HAL encountered error %d: %s",
718 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700719 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800720}
721
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800722bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700723 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800724 if (mModule == nullptr) return false;
725
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800726 vendor_tag_ops_t vOps = vendor_tag_ops_t();
727
728 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800729 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800730 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
731 return false;
732 }
733
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800734 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800735
736 // Ensure all vendor operations are present
737 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
738 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
739 vOps.get_tag_type == NULL) {
740 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
741 , __FUNCTION__);
742 return false;
743 }
744
745 // Read all vendor tag definitions into a descriptor
746 sp<VendorTagDescriptor> desc;
747 status_t res;
748 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
749 != OK) {
750 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
751 "received error %s (%d). Camera clients will not be able to use"
752 "vendor tags", __FUNCTION__, strerror(res), res);
753 return false;
754 }
755
756 // Set the global descriptor to use with camera metadata
757 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
758 return true;
759}
760
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800761Status CameraService::makeClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800762 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800763 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
764 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
765 /*out*/sp<BasicClient>* client) {
766
Ruben Brunkcc776712015-02-17 20:18:47 -0800767 if (halVersion < 0 || halVersion == deviceVersion) {
768 // Default path: HAL version is unspecified by caller, create CameraClient
769 // based on device version reported by the HAL.
770 switch(deviceVersion) {
771 case CAMERA_DEVICE_API_VERSION_1_0:
772 if (effectiveApiLevel == API_1) { // Camera1 API route
773 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800774 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
775 facing, clientPid, clientUid, getpid(), legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800776 } else { // Camera2 API route
777 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800778 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800779 "Camera device \"%s\" HAL version %d does not support camera2 API",
780 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800781 }
782 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800783 case CAMERA_DEVICE_API_VERSION_3_0:
784 case CAMERA_DEVICE_API_VERSION_3_1:
785 case CAMERA_DEVICE_API_VERSION_3_2:
786 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700787 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800788 if (effectiveApiLevel == API_1) { // Camera1 API route
789 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800790 *client = new Camera2Client(cameraService, tmp, packageName, cameraIdToInt(cameraId),
791 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800792 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800793 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
794 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
795 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800796 facing, clientPid, clientUid, servicePid);
797 }
798 break;
799 default:
800 // Should not be reachable
801 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800802 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800803 "Camera device \"%s\" has unknown HAL version %d",
804 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800805 }
806 } else {
807 // A particular HAL version is requested by caller. Create CameraClient
808 // based on the requested HAL version.
809 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
810 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
811 // Only support higher HAL version device opened as HAL1.0 device.
812 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800813 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
814 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800815 } else {
816 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
817 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
818 " opened as HAL %x device", halVersion, deviceVersion,
819 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800820 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800821 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
822 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800823 }
824 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800825 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800826}
827
Ruben Brunk6267b532015-04-30 17:44:07 -0700828String8 CameraService::toString(std::set<userid_t> intSet) {
829 String8 s("");
830 bool first = true;
831 for (userid_t i : intSet) {
832 if (first) {
833 s.appendFormat("%d", i);
834 first = false;
835 } else {
836 s.appendFormat(", %d", i);
837 }
838 }
839 return s;
840}
841
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800842int32_t CameraService::mapToInterface(TorchModeStatus status) {
843 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
844 switch (status) {
845 case TorchModeStatus::NOT_AVAILABLE:
846 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
847 break;
848 case TorchModeStatus::AVAILABLE_OFF:
849 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
850 break;
851 case TorchModeStatus::AVAILABLE_ON:
852 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
853 break;
854 default:
855 ALOGW("Unknown new flash status: %d", status);
856 }
857 return serviceStatus;
858}
859
860CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
861 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
862 switch (status) {
863 case CameraDeviceStatus::NOT_PRESENT:
864 serviceStatus = StatusInternal::NOT_PRESENT;
865 break;
866 case CameraDeviceStatus::PRESENT:
867 serviceStatus = StatusInternal::PRESENT;
868 break;
869 case CameraDeviceStatus::ENUMERATING:
870 serviceStatus = StatusInternal::ENUMERATING;
871 break;
872 default:
873 ALOGW("Unknown new HAL device status: %d", status);
874 }
875 return serviceStatus;
876}
877
878int32_t CameraService::mapToInterface(StatusInternal status) {
879 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
880 switch (status) {
881 case StatusInternal::NOT_PRESENT:
882 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
883 break;
884 case StatusInternal::PRESENT:
885 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
886 break;
887 case StatusInternal::ENUMERATING:
888 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
889 break;
890 case StatusInternal::NOT_AVAILABLE:
891 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
892 break;
893 case StatusInternal::UNKNOWN:
894 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
895 break;
896 default:
897 ALOGW("Unknown new internal device status: %d", status);
898 }
899 return serviceStatus;
900}
901
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800902Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800903 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700904
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800905 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800906 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800907 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800908 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800909 if (!(ret = connectHelper<ICameraClient,Client>(
910 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
911 internalPackageName, uid, USE_CALLING_PID,
912 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
913 /*out*/ tmp)
914 ).isOk()) {
915 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700916 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800917 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700918}
919
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800920Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700921 /*out*/
922 CameraParameters* parameters) {
923
924 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
925
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800926 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700927
928 if (parameters == NULL) {
929 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800930 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700931 }
932
Ruben Brunkcc776712015-02-17 20:18:47 -0800933 String8 id = String8::format("%d", cameraId);
934
935 // Check if we already have parameters
936 {
937 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700938 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800939 auto cameraState = getCameraState(id);
940 if (cameraState == nullptr) {
941 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800942 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
943 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800944 }
945 CameraParameters p = cameraState->getShimParams();
946 if (!p.isEmpty()) {
947 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800948 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700949 }
950 }
951
Ruben Brunkcc776712015-02-17 20:18:47 -0800952 int64_t token = IPCThreadState::self()->clearCallingIdentity();
953 ret = initializeShimMetadata(cameraId);
954 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800955 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800956 // Error already logged by callee
957 return ret;
958 }
959
960 // Check for parameters again
961 {
962 // Scope for service lock
963 Mutex::Autolock lock(mServiceLock);
964 auto cameraState = getCameraState(id);
965 if (cameraState == nullptr) {
966 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800967 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
968 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700969 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800970 CameraParameters p = cameraState->getShimParams();
971 if (!p.isEmpty()) {
972 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800973 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700974 }
975 }
976
Ruben Brunkcc776712015-02-17 20:18:47 -0800977 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
978 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800979 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700980}
981
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800982// Can camera service trust the caller based on the calling UID?
983static bool isTrustedCallingUid(uid_t uid) {
984 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700985 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800986 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700987 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800988 return true;
989 default:
990 return false;
991 }
992}
993
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800994Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700995 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
996 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500997
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700998#ifdef __BRILLO__
999 UNUSED(clientName8);
1000 UNUSED(clientUid);
1001 UNUSED(clientPid);
1002 UNUSED(originalClientPid);
1003#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001004 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
1005 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001006 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -08001007 return allowed;
1008 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001009#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -08001010
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001011 int callingPid = getCallingPid();
1012
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001013 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001014 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
1015 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001016 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1017 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -07001018 }
1019
Ruben Brunkcc776712015-02-17 20:18:47 -08001020 if (getCameraState(cameraId) == nullptr) {
1021 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1022 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001023 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1024 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001025 }
1026
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001027 status_t err = checkIfDeviceIsUsable(cameraId);
1028 if (err != NO_ERROR) {
1029 switch(err) {
1030 case -ENODEV:
1031 case -EBUSY:
1032 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1033 "No camera device with ID \"%s\" currently available", cameraId.string());
1034 default:
1035 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1036 "Unknown error connecting to ID \"%s\"", cameraId.string());
1037 }
1038 }
1039 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001040}
1041
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001042Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001043 const String8& clientName8, int& clientUid, int& clientPid,
1044 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001045 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001046 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001048 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001050 clientUid = callingUid;
1051 } else if (!isTrustedCallingUid(callingUid)) {
1052 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1053 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001054 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1055 "Untrusted caller (calling PID %d, UID %d) trying to "
1056 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1057 callingPid, callingUid, cameraId.string(),
1058 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001059 }
1060
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001061 // Check if we can trust clientPid
1062 if (clientPid == USE_CALLING_PID) {
1063 clientPid = callingPid;
1064 } else if (!isTrustedCallingUid(callingUid)) {
1065 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1066 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001067 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1068 "Untrusted caller (calling PID %d, UID %d) trying to "
1069 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1070 callingPid, callingUid, cameraId.string(),
1071 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001072 }
1073
1074 // If it's not calling from cameraserver, check the permission.
1075 if (callingPid != getpid() &&
1076 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
1077 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001078 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1079 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1080 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001081 }
1082
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001083 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1084 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001085 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001086 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001087
Ruben Brunk6267b532015-04-30 17:44:07 -07001088 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001089
Ruben Brunka8ca9152015-04-07 14:23:40 -07001090 // Only allow clients who are being used by the current foreground device user, unless calling
1091 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001092 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1093 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1094 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1095 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001096 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1097 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1098 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001099 }
1100
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001101 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001102}
1103
1104status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1105 auto cameraState = getCameraState(cameraId);
1106 int callingPid = getCallingPid();
1107 if (cameraState == nullptr) {
1108 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1109 cameraId.string());
1110 return -ENODEV;
1111 }
1112
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001113 StatusInternal currentStatus = cameraState->getStatus();
1114 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001115 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1116 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001117 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001118 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001119 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1120 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001121 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001122 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001123
Ruben Brunkcc776712015-02-17 20:18:47 -08001124 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001125}
1126
Ruben Brunkcc776712015-02-17 20:18:47 -08001127void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1128 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001129
Ruben Brunkcc776712015-02-17 20:18:47 -08001130 // Make a descriptor for the incoming client
1131 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1132 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1133
1134 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1135 String8(client->getPackageName()));
1136
1137 if (evicted.size() > 0) {
1138 // This should never happen - clients should already have been removed in disconnect
1139 for (auto& i : evicted) {
1140 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1141 __FUNCTION__, i->getKey().string());
1142 }
1143
1144 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1145 __FUNCTION__);
1146 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001147
1148 // And register a death notification for the client callback. Do
1149 // this last to avoid Binder policy where a nested Binder
1150 // transaction might be pre-empted to service the client death
1151 // notification if the client process dies before linkToDeath is
1152 // invoked.
1153 sp<IBinder> remoteCallback = client->getRemote();
1154 if (remoteCallback != nullptr) {
1155 remoteCallback->linkToDeath(this);
1156 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001157}
1158
1159status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1160 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1161 /*out*/
1162 sp<BasicClient>* client,
1163 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001164 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001165 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001166 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001167 DescriptorPtr clientDescriptor;
1168 {
1169 if (effectiveApiLevel == API_1) {
1170 // If we are using API1, any existing client for this camera ID with the same remote
1171 // should be returned rather than evicted to allow MediaRecorder to work properly.
1172
1173 auto current = mActiveClientManager.get(cameraId);
1174 if (current != nullptr) {
1175 auto clientSp = current->getValue();
1176 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001177 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1178 ALOGW("CameraService connect called from same client, but with a different"
1179 " API level, evicting prior client...");
1180 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001181 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001182 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001183 *client = clientSp;
1184 return NO_ERROR;
1185 }
1186 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001187 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001188 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001189
Ruben Brunkcc776712015-02-17 20:18:47 -08001190 // Get current active client PIDs
1191 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1192 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001193
Emilian Peev8131a262017-02-01 12:33:43 +00001194 std::vector<int> priorityScores(ownerPids.size());
1195 std::vector<int> states(ownerPids.size());
Igor Murashkine6800ce2013-03-04 17:25:57 -08001196
Emilian Peev8131a262017-02-01 12:33:43 +00001197 // Get priority scores of all active PIDs
1198 status_t err = ProcessInfoService::getProcessStatesScoresFromPids(
1199 ownerPids.size(), &ownerPids[0], /*out*/&states[0],
1200 /*out*/&priorityScores[0]);
1201 if (err != OK) {
1202 ALOGE("%s: Priority score query failed: %d",
1203 __FUNCTION__, err);
1204 return err;
1205 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001206
Ruben Brunkcc776712015-02-17 20:18:47 -08001207 // Update all active clients' priorities
Emilian Peev8131a262017-02-01 12:33:43 +00001208 std::map<int,resource_policy::ClientPriority> pidToPriorityMap;
Ruben Brunkcc776712015-02-17 20:18:47 -08001209 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
Emilian Peev8131a262017-02-01 12:33:43 +00001210 pidToPriorityMap.emplace(ownerPids[i],
1211 resource_policy::ClientPriority(priorityScores[i], states[i]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001212 }
1213 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001214
Ruben Brunkcc776712015-02-17 20:18:47 -08001215 // Get state for the given cameraId
1216 auto state = getCameraState(cameraId);
1217 if (state == nullptr) {
1218 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1219 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001220 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001221 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001222 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001223
1224 // Make descriptor for incoming client
1225 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1226 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1227 state->getConflicting(),
Emilian Peev8131a262017-02-01 12:33:43 +00001228 priorityScores[priorityScores.size() - 1],
1229 clientPid,
1230 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001231
1232 // Find clients that would be evicted
1233 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1234
1235 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1236 // background, so we cannot do evictions
1237 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1238 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1239 " priority).", clientPid);
1240
1241 sp<BasicClient> clientSp = clientDescriptor->getValue();
1242 String8 curTime = getFormattedCurrentTime();
1243 auto incompatibleClients =
1244 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1245
1246 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Emilian Peev8131a262017-02-01 12:33:43 +00001247 "(PID %d, score %d state %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001248 cameraId.string(), packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001249 priorityScores[priorityScores.size() - 1],
1250 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001251
1252 for (auto& i : incompatibleClients) {
1253 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
Emilian Peev8131a262017-02-01 12:33:43 +00001254 "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")",
1255 i->getKey().string(),
1256 String8{i->getValue()->getPackageName()}.string(),
1257 i->getOwnerId(), i->getPriority().getScore(),
1258 i->getPriority().getState());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001259 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001260 PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(),
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001261 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
Emilian Peev8131a262017-02-01 12:33:43 +00001262 i->getPriority().getScore(), i->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08001263 }
1264
1265 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001266 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001267 mEventLog.add(msg);
1268
1269 return -EBUSY;
1270 }
1271
1272 for (auto& i : evicted) {
1273 sp<BasicClient> clientSp = i->getValue();
1274 if (clientSp.get() == nullptr) {
1275 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1276
1277 // TODO: Remove this
1278 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1279 __FUNCTION__);
1280 mActiveClientManager.remove(i);
1281 continue;
1282 }
1283
1284 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1285 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001286 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001287
Ruben Brunkcc776712015-02-17 20:18:47 -08001288 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001289 logEvent(String8::format("EVICT device %s client held by package %s (PID"
Emilian Peev8131a262017-02-01 12:33:43 +00001290 " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for"
1291 " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001292 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
Emilian Peev8131a262017-02-01 12:33:43 +00001293 i->getOwnerId(), i->getPriority().getScore(),
1294 i->getPriority().getState(), cameraId.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001295 packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001296 priorityScores[priorityScores.size() - 1],
1297 states[states.size() - 1]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001298
1299 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001300 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001301 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001302 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001303 }
1304
Ruben Brunkcc776712015-02-17 20:18:47 -08001305 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1306 // other clients from connecting in mServiceLockWrapper if held
1307 mServiceLock.unlock();
1308
1309 // Clear caller identity temporarily so client disconnect PID checks work correctly
1310 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1311
1312 // Destroy evicted clients
1313 for (auto& i : evictedClients) {
1314 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001315 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001316 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001317
1318 IPCThreadState::self()->restoreCallingIdentity(token);
1319
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001320 for (const auto& i : evictedClients) {
1321 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1322 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1323 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1324 if (ret == TIMED_OUT) {
1325 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1326 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1327 mActiveClientManager.toString().string());
1328 return -EBUSY;
1329 }
1330 if (ret != NO_ERROR) {
1331 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1332 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1333 ret, mActiveClientManager.toString().string());
1334 return ret;
1335 }
1336 }
1337
1338 evictedClients.clear();
1339
Ruben Brunkcc776712015-02-17 20:18:47 -08001340 // Once clients have been disconnected, relock
1341 mServiceLock.lock();
1342
1343 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1344 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1345 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001346 }
1347
Ruben Brunkcc776712015-02-17 20:18:47 -08001348 *partial = clientDescriptor;
1349 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001350}
1351
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001352Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001353 const sp<ICameraClient>& cameraClient,
1354 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001355 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001356 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001357 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001358 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001359 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001360
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001361 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001362 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001363 String8 id = String8::format("%d", cameraId);
1364 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001365 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1366 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1367 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1368 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001369
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001370 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001371 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001372 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001373 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001374 }
1375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001376 *device = client;
1377 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001378}
1379
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001381 const sp<ICameraClient>& cameraClient,
1382 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001383 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001384 int clientUid,
1385 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001386 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001387
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001388 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001389 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001390 if (mModule != nullptr) {
1391 int apiVersion = mModule->getModuleApiVersion();
1392 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
1393 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
1394 /*
1395 * Either the HAL version is unspecified in which case this just creates
1396 * a camera client selected by the latest device version, or
1397 * it's a particular version in which case the HAL must supported
1398 * the open_legacy call
1399 */
1400 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1401 apiVersion);
1402 ALOGE("%s: %s",
1403 __FUNCTION__, msg.string());
1404 logRejected(id, getCallingPid(), String8(clientPackageName),
1405 msg);
1406 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
1407 }
Zhijun Heb10cdad2014-06-16 16:38:35 -07001408 }
1409
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001410 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001411 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001412 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1413 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1414 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1415 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001416
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001417 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001418 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001419 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001420 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001421 }
1422
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423 *device = client;
1424 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001425}
1426
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001427Status CameraService::connectDevice(
1428 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001429 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001430 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001431 int clientUid,
1432 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001433 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001434
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001435 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001436 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001437 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001438 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001439 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1440 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1441 clientUid, USE_CALLING_PID, API_2,
1442 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1443 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001444
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001445 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001446 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001447 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001448 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001449 }
1450
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001451 *device = client;
1452 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001453}
1454
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001455template<class CALLBACK, class CLIENT>
1456Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
1457 int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
1458 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
1459 /*out*/sp<CLIENT>& device) {
1460 binder::Status ret = binder::Status::ok();
1461
1462 String8 clientName8(clientPackageName);
1463
1464 int originalClientPid = 0;
1465
1466 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1467 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1468 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1469 static_cast<int>(effectiveApiLevel));
1470
1471 sp<CLIENT> client = nullptr;
1472 {
1473 // Acquire mServiceLock and prevent other clients from connecting
1474 std::unique_ptr<AutoConditionLock> lock =
1475 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1476
1477 if (lock == nullptr) {
1478 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1479 , clientPid);
1480 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1481 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1482 cameraId.string(), clientName8.string(), clientPid);
1483 }
1484
1485 // Enforce client permissions and do basic sanity checks
1486 if(!(ret = validateConnectLocked(cameraId, clientName8,
1487 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1488 return ret;
1489 }
1490
1491 // Check the shim parameters after acquiring lock, if they have already been updated and
1492 // we were doing a shim update, return immediately
1493 if (shimUpdateOnly) {
1494 auto cameraState = getCameraState(cameraId);
1495 if (cameraState != nullptr) {
1496 if (!cameraState->getShimParams().isEmpty()) return ret;
1497 }
1498 }
1499
1500 status_t err;
1501
1502 sp<BasicClient> clientTmp = nullptr;
1503 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1504 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1505 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1506 /*out*/&partial)) != NO_ERROR) {
1507 switch (err) {
1508 case -ENODEV:
1509 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1510 "No camera device with ID \"%s\" currently available",
1511 cameraId.string());
1512 case -EBUSY:
1513 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1514 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1515 cameraId.string());
1516 default:
1517 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1518 "Unexpected error %s (%d) opening camera \"%s\"",
1519 strerror(-err), err, cameraId.string());
1520 }
1521 }
1522
1523 if (clientTmp.get() != nullptr) {
1524 // Handle special case for API1 MediaRecorder where the existing client is returned
1525 device = static_cast<CLIENT*>(clientTmp.get());
1526 return ret;
1527 }
1528
1529 // give flashlight a chance to close devices if necessary.
1530 mFlashlight->prepareDeviceOpen(cameraId);
1531
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001532 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001533 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -08001534 if (facing == -1) {
1535 ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string());
1536 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1537 "Unable to get camera device \"%s\" facing", cameraId.string());
1538 }
1539
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001540 sp<BasicClient> tmp = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001541 if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001542 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
1543 /*out*/&tmp)).isOk()) {
1544 return ret;
1545 }
1546 client = static_cast<CLIENT*>(tmp.get());
1547
1548 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1549 __FUNCTION__);
1550
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001551 if (mModule != nullptr) {
1552 err = client->initialize(mModule);
1553 } else {
1554 err = client->initialize(mCameraProviderManager);
1555 }
1556
1557 if (err != OK) {
1558 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001559 // Errors could be from the HAL module open call or from AppOpsManager
1560 switch(err) {
1561 case BAD_VALUE:
1562 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1563 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1564 case -EBUSY:
1565 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1566 "Camera \"%s\" is already open", cameraId.string());
1567 case -EUSERS:
1568 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1569 "Too many cameras already open, cannot open camera \"%s\"",
1570 cameraId.string());
1571 case PERMISSION_DENIED:
1572 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1573 "No permission to open camera \"%s\"", cameraId.string());
1574 case -EACCES:
1575 return STATUS_ERROR_FMT(ERROR_DISABLED,
1576 "Camera \"%s\" disabled by policy", cameraId.string());
1577 case -ENODEV:
1578 default:
1579 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1580 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1581 strerror(-err), err);
1582 }
1583 }
1584
1585 // Update shim paremeters for legacy clients
1586 if (effectiveApiLevel == API_1) {
1587 // Assume we have always received a Client subclass for API1
1588 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1589 String8 rawParams = shimClient->getParameters();
1590 CameraParameters params(rawParams);
1591
1592 auto cameraState = getCameraState(cameraId);
1593 if (cameraState != nullptr) {
1594 cameraState->setShimParams(params);
1595 } else {
1596 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1597 __FUNCTION__, cameraId.string());
1598 }
1599 }
1600
1601 if (shimUpdateOnly) {
1602 // If only updating legacy shim parameters, immediately disconnect client
1603 mServiceLock.unlock();
1604 client->disconnect();
1605 mServiceLock.lock();
1606 } else {
1607 // Otherwise, add client to active clients list
1608 finishConnectLocked(client, partial);
1609 }
1610 } // lock is destroyed, allow further connect calls
1611
1612 // Important: release the mutex here so the client can call back into the service from its
1613 // destructor (can be at the end of the call)
1614 device = client;
1615 return ret;
1616}
1617
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001618Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001619 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001620 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001621
1622 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001623 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001624 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001625 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1626 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001627 }
1628
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001629 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001630 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001631
1632 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001633 auto state = getCameraState(id);
1634 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001635 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001636 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1637 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001638 }
1639
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001640 StatusInternal cameraStatus = state->getStatus();
1641 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001642 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1643 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001644 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1645 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001646 }
1647
1648 {
1649 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001650 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001651 status_t err = getTorchStatusLocked(id, &status);
1652 if (err != OK) {
1653 if (err == NAME_NOT_FOUND) {
1654 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1655 "Camera \"%s\" does not have a flash unit", id.string());
1656 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001657 ALOGE("%s: getting current torch status failed for camera %s",
1658 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001659 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1660 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1661 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001662 }
1663
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001664 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001665 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001666 ALOGE("%s: torch mode of camera %s is not available because "
1667 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001668 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1669 "Torch for camera \"%s\" is not available due to an existing camera user",
1670 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001671 } else {
1672 ALOGE("%s: torch mode of camera %s is not available due to "
1673 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001674 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1675 "Torch for camera \"%s\" is not available due to insufficient resources",
1676 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001677 }
1678 }
1679 }
1680
Ruben Brunk99e69712015-05-26 17:25:07 -07001681 {
1682 // Update UID map - this is used in the torch status changed callbacks, so must be done
1683 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001684 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001685 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1686 mTorchUidMap[id].first = uid;
1687 mTorchUidMap[id].second = uid;
1688 } else {
1689 // Set the pending UID
1690 mTorchUidMap[id].first = uid;
1691 }
1692 }
1693
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001694 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001695
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001696 if (err != OK) {
1697 int32_t errorCode;
1698 String8 msg;
1699 switch (err) {
1700 case -ENOSYS:
1701 msg = String8::format("Camera \"%s\" has no flashlight",
1702 id.string());
1703 errorCode = ERROR_ILLEGAL_ARGUMENT;
1704 break;
1705 default:
1706 msg = String8::format(
1707 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1708 id.string(), enabled, strerror(-err), err);
1709 errorCode = ERROR_INVALID_OPERATION;
1710 }
1711 ALOGE("%s: %s", __FUNCTION__, msg.string());
1712 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001713 }
1714
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001715 {
1716 // update the link to client's death
1717 Mutex::Autolock al(mTorchClientMapMutex);
1718 ssize_t index = mTorchClientMap.indexOfKey(id);
1719 if (enabled) {
1720 if (index == NAME_NOT_FOUND) {
1721 mTorchClientMap.add(id, clientBinder);
1722 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001723 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001724 mTorchClientMap.replaceValueAt(index, clientBinder);
1725 }
1726 clientBinder->linkToDeath(this);
1727 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001728 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001729 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001730 }
1731
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001732 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001733}
1734
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001735Status CameraService::notifySystemEvent(int32_t eventId,
1736 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001737 ATRACE_CALL();
1738
Ruben Brunk36597b22015-03-20 22:15:57 -07001739 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001740 case ICameraService::EVENT_USER_SWITCHED: {
1741 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001742 break;
1743 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001744 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001745 default: {
1746 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1747 eventId);
1748 break;
1749 }
1750 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001751 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001752}
1753
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001754Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1755 /*out*/
1756 std::vector<hardware::CameraStatus> *cameraStatuses) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001757 ATRACE_CALL();
1758
Igor Murashkinbfc99152013-02-27 12:55:20 -08001759 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001760
Ruben Brunk3450ba72015-06-16 11:00:37 -07001761 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001762 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001763 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001764 }
1765
Igor Murashkinbfc99152013-02-27 12:55:20 -08001766 Mutex::Autolock lock(mServiceLock);
1767
Ruben Brunkcc776712015-02-17 20:18:47 -08001768 {
1769 Mutex::Autolock lock(mStatusListenerLock);
1770 for (auto& it : mListenerList) {
1771 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1772 ALOGW("%s: Tried to add listener %p which was already subscribed",
1773 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001774 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001775 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001776 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001777
1778 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001779 }
1780
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001781 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001782 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001783 Mutex::Autolock lock(mCameraStatesLock);
1784 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001785 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001786 }
1787 }
1788
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001789 /*
1790 * Immediately signal current torch status to this listener only
1791 * This may be a subset of all the devices, so don't include it in the response directly
1792 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001793 {
1794 Mutex::Autolock al(mTorchStatusMutex);
1795 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001796 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001797 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001798 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001799 }
1800
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001801 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001802}
Ruben Brunkcc776712015-02-17 20:18:47 -08001803
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001804Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001805 ATRACE_CALL();
1806
Igor Murashkinbfc99152013-02-27 12:55:20 -08001807 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1808
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001809 if (listener == 0) {
1810 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001811 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001812 }
1813
Igor Murashkinbfc99152013-02-27 12:55:20 -08001814 Mutex::Autolock lock(mServiceLock);
1815
Ruben Brunkcc776712015-02-17 20:18:47 -08001816 {
1817 Mutex::Autolock lock(mStatusListenerLock);
1818 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1819 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1820 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001821 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001822 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001823 }
1824 }
1825
1826 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1827 __FUNCTION__, listener.get());
1828
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001829 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001830}
1831
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001832Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001833
1834 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001835 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1836
1837 if (parameters == NULL) {
1838 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001839 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001840 }
1841
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001842 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001843
1844 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001845 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001846 // Error logged by caller
1847 return ret;
1848 }
1849
1850 String8 shimParamsString8 = shimParams.flatten();
1851 String16 shimParamsString16 = String16(shimParamsString8);
1852
1853 *parameters = shimParamsString16;
1854
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001855 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001856}
1857
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001858Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
1859 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001860 ATRACE_CALL();
1861
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001862 const String8 id = String8(cameraId);
1863
1864 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001865
1866 switch (apiVersion) {
1867 case API_VERSION_1:
1868 case API_VERSION_2:
1869 break;
1870 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001871 String8 msg = String8::format("Unknown API version %d", apiVersion);
1872 ALOGE("%s: %s", __FUNCTION__, msg.string());
1873 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001874 }
1875
Emilian Peev28ad2ea2017-02-07 16:14:32 +00001876 int deviceVersion = getDeviceVersion(id);
Igor Murashkin65d14b92014-06-17 12:03:20 -07001877 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001878 case CAMERA_DEVICE_API_VERSION_1_0:
1879 case CAMERA_DEVICE_API_VERSION_3_0:
1880 case CAMERA_DEVICE_API_VERSION_3_1:
1881 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001882 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
1883 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001884 *isSupported = false;
1885 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001886 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
1887 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001888 *isSupported = true;
1889 }
1890 break;
1891 case CAMERA_DEVICE_API_VERSION_3_2:
1892 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001893 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001894 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
1895 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001896 *isSupported = true;
1897 break;
1898 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001899 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001900 ALOGE("%s: %s", __FUNCTION__, msg.string());
1901 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001902 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001903 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001904 String8 msg = String8::format("Unknown device version %x for device %s",
1905 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001906 ALOGE("%s: %s", __FUNCTION__, msg.string());
1907 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1908 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001909 }
1910
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001911 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001912}
1913
Ruben Brunkcc776712015-02-17 20:18:47 -08001914void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001915 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001916 for (auto& i : mActiveClientManager.getAll()) {
1917 auto clientSp = i->getValue();
1918 if (clientSp.get() == client) {
1919 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001920 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001921 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001922}
1923
Ruben Brunkcc776712015-02-17 20:18:47 -08001924bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1925 const int callingPid = getCallingPid();
1926 const int servicePid = getpid();
1927 bool ret = false;
1928 {
1929 // Acquire mServiceLock and prevent other clients from connecting
1930 std::unique_ptr<AutoConditionLock> lock =
1931 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001932
Igor Murashkin634a5152013-02-20 17:15:11 -08001933
Ruben Brunkcc776712015-02-17 20:18:47 -08001934 std::vector<sp<BasicClient>> evicted;
1935 for (auto& i : mActiveClientManager.getAll()) {
1936 auto clientSp = i->getValue();
1937 if (clientSp.get() == nullptr) {
1938 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1939 mActiveClientManager.remove(i);
1940 continue;
1941 }
1942 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1943 callingPid == clientSp->getClientPid())) {
1944 mActiveClientManager.remove(i);
1945 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001946
Ruben Brunkcc776712015-02-17 20:18:47 -08001947 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001948 clientSp->notifyError(
1949 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001950 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001951 }
1952 }
1953
Ruben Brunkcc776712015-02-17 20:18:47 -08001954 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1955 // other clients from connecting in mServiceLockWrapper if held
1956 mServiceLock.unlock();
1957
Ruben Brunk36597b22015-03-20 22:15:57 -07001958 // Do not clear caller identity, remote caller should be client proccess
1959
Ruben Brunkcc776712015-02-17 20:18:47 -08001960 for (auto& i : evicted) {
1961 if (i.get() != nullptr) {
1962 i->disconnect();
1963 ret = true;
1964 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001965 }
1966
Ruben Brunkcc776712015-02-17 20:18:47 -08001967 // Reacquire mServiceLock
1968 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001969
Ruben Brunkcc776712015-02-17 20:18:47 -08001970 } // lock is destroyed, allow further connect calls
1971
1972 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001973}
1974
Igor Murashkinecf17e82012-10-02 16:05:11 -07001975
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001976/**
1977 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001978 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001979 */
1980int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001981 if (mModule == nullptr) return NO_INIT;
1982
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001983 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1984 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1985 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1986 // Verify the device version is in the supported range
1987 switch (info.device_version) {
1988 case CAMERA_DEVICE_API_VERSION_1_0:
1989 case CAMERA_DEVICE_API_VERSION_3_0:
1990 case CAMERA_DEVICE_API_VERSION_3_1:
1991 case CAMERA_DEVICE_API_VERSION_3_2:
1992 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001993 case CAMERA_DEVICE_API_VERSION_3_4:
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001994 // in support
1995 break;
1996 case CAMERA_DEVICE_API_VERSION_2_0:
1997 case CAMERA_DEVICE_API_VERSION_2_1:
1998 // no longer supported
1999 default:
2000 ALOGE("%s: Device %d has HAL version %x, which is not supported",
2001 __FUNCTION__, id, info.device_version);
2002 String8 msg = String8::format(
2003 "Unsupported device HAL version %x for device %d",
2004 info.device_version, id);
2005 logServiceError(msg.string(), NO_INIT);
2006 return NO_INIT;
2007 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002008 }
2009
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002010 // Assume all devices pre-v3.3 are backward-compatible
2011 bool isBackwardCompatible = true;
2012 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
2013 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
2014 isBackwardCompatible = false;
2015 status_t res;
2016 camera_metadata_ro_entry_t caps;
2017 res = find_camera_metadata_ro_entry(
2018 info.static_camera_characteristics,
2019 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
2020 &caps);
2021 if (res != 0) {
2022 ALOGW("%s: Unable to find camera capabilities for camera device %d",
2023 __FUNCTION__, id);
2024 caps.count = 0;
2025 }
2026 for (size_t i = 0; i < caps.count; i++) {
2027 if (caps.data.u8[i] ==
2028 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
2029 isBackwardCompatible = true;
2030 break;
2031 }
2032 }
2033 }
2034
2035 if (!isBackwardCompatible) {
2036 mNumberOfNormalCameras--;
2037 *latestStrangeCameraId = id;
2038 } else {
2039 if (id > *latestStrangeCameraId) {
2040 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
2041 "This is not allowed due backward-compatibility requirements",
2042 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002043 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002044 mNumberOfCameras = 0;
2045 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002046 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002047 }
2048 }
2049 return OK;
2050}
2051
Ruben Brunkcc776712015-02-17 20:18:47 -08002052std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
2053 const String8& cameraId) const {
2054 std::shared_ptr<CameraState> state;
2055 {
2056 Mutex::Autolock lock(mCameraStatesLock);
2057 auto iter = mCameraStates.find(cameraId);
2058 if (iter != mCameraStates.end()) {
2059 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002060 }
2061 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002062 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002063}
2064
Ruben Brunkcc776712015-02-17 20:18:47 -08002065sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
2066 // Remove from active clients list
2067 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
2068 if (clientDescriptorPtr == nullptr) {
2069 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
2070 cameraId.string());
2071 return sp<BasicClient>{nullptr};
2072 }
2073
2074 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07002075}
2076
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002077void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07002078 // Acquire mServiceLock and prevent other clients from connecting
2079 std::unique_ptr<AutoConditionLock> lock =
2080 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
2081
Ruben Brunk6267b532015-04-30 17:44:07 -07002082 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002083 for (size_t i = 0; i < newUserIds.size(); i++) {
2084 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07002085 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002086 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07002087 return;
2088 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002089 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07002090 }
2091
Ruben Brunka8ca9152015-04-07 14:23:40 -07002092
Ruben Brunk6267b532015-04-30 17:44:07 -07002093 if (newAllowedUsers == mAllowedUsers) {
2094 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
2095 return;
2096 }
2097
2098 logUserSwitch(mAllowedUsers, newAllowedUsers);
2099
2100 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07002101
2102 // Current user has switched, evict all current clients.
2103 std::vector<sp<BasicClient>> evicted;
2104 for (auto& i : mActiveClientManager.getAll()) {
2105 auto clientSp = i->getValue();
2106
2107 if (clientSp.get() == nullptr) {
2108 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
2109 continue;
2110 }
2111
Ruben Brunk6267b532015-04-30 17:44:07 -07002112 // Don't evict clients that are still allowed.
2113 uid_t clientUid = clientSp->getClientUid();
2114 userid_t clientUserId = multiuser_get_user_id(clientUid);
2115 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
2116 continue;
2117 }
2118
Ruben Brunk36597b22015-03-20 22:15:57 -07002119 evicted.push_back(clientSp);
2120
2121 String8 curTime = getFormattedCurrentTime();
2122
2123 ALOGE("Evicting conflicting client for camera ID %s due to user change",
2124 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07002125
Ruben Brunk36597b22015-03-20 22:15:57 -07002126 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002127 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00002128 PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due"
2129 " to user switch.", i->getKey().string(),
2130 String8{clientSp->getPackageName()}.string(),
2131 i->getOwnerId(), i->getPriority().getScore(),
2132 i->getPriority().getState()));
Ruben Brunk36597b22015-03-20 22:15:57 -07002133
2134 }
2135
2136 // Do not hold mServiceLock while disconnecting clients, but retain the condition
2137 // blocking other clients from connecting in mServiceLockWrapper if held.
2138 mServiceLock.unlock();
2139
2140 // Clear caller identity temporarily so client disconnect PID checks work correctly
2141 int64_t token = IPCThreadState::self()->clearCallingIdentity();
2142
2143 for (auto& i : evicted) {
2144 i->disconnect();
2145 }
2146
2147 IPCThreadState::self()->restoreCallingIdentity(token);
2148
2149 // Reacquire mServiceLock
2150 mServiceLock.lock();
2151}
Ruben Brunkcc776712015-02-17 20:18:47 -08002152
Ruben Brunka8ca9152015-04-07 14:23:40 -07002153void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002154 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07002155 Mutex::Autolock l(mLogLock);
2156 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07002157}
2158
Ruben Brunka8ca9152015-04-07 14:23:40 -07002159void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002160 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002161 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002162 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002163 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002164}
2165
2166void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002167 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002168 // Log the clients evicted
2169 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002170 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002171}
2172
2173void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002174 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002175 // Log the client rejected
2176 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002177 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002178}
2179
Ruben Brunk6267b532015-04-30 17:44:07 -07002180void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
2181 const std::set<userid_t>& newUserIds) {
2182 String8 newUsers = toString(newUserIds);
2183 String8 oldUsers = toString(oldUserIds);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002184 if (oldUsers.size() == 0) {
2185 oldUsers = "<None>";
2186 }
Ruben Brunka8ca9152015-04-07 14:23:40 -07002187 // Log the new and old users
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002188 logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s",
Ruben Brunk6267b532015-04-30 17:44:07 -07002189 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002190}
2191
2192void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
2193 // Log the device removal
2194 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
2195}
2196
2197void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
2198 // Log the device removal
2199 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
2200}
2201
2202void CameraService::logClientDied(int clientPid, const char* reason) {
2203 // Log the device removal
2204 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07002205}
2206
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002207void CameraService::logServiceError(const char* msg, int errorCode) {
2208 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002209 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002210}
2211
Ruben Brunk36597b22015-03-20 22:15:57 -07002212status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
2213 uint32_t flags) {
2214
2215 const int pid = getCallingPid();
2216 const int selfPid = getpid();
2217
Mathias Agopian65ab4712010-07-14 17:59:35 -07002218 // Permission checks
2219 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002220 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07002221 if (pid != selfPid) {
2222 // Ensure we're being called by system_server, or similar process with
2223 // permissions to notify the camera service about system events
2224 if (!checkCallingPermission(
2225 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
2226 const int uid = getCallingUid();
2227 ALOGE("Permission Denial: cannot send updates to camera service about system"
2228 " events from pid=%d, uid=%d", pid, uid);
2229 return PERMISSION_DENIED;
2230 }
2231 }
2232 break;
2233 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002234 }
2235
2236 return BnCameraService::onTransact(code, data, reply, flags);
2237}
2238
Mathias Agopian65ab4712010-07-14 17:59:35 -07002239// We share the media players for shutter and recording sound for all clients.
2240// A reference count is kept to determine when we will actually release the
2241// media players.
2242
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08002243MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002244 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08002245 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08002246 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002247 mp->prepare();
2248 } else {
Steve Block29357bc2012-01-06 19:20:56 +00002249 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002250 return NULL;
2251 }
2252 return mp;
2253}
2254
2255void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002256 ATRACE_CALL();
2257
Mathias Agopian65ab4712010-07-14 17:59:35 -07002258 Mutex::Autolock lock(mSoundLock);
2259 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2260 if (mSoundRef++) return;
2261
2262 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002263 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2264 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265}
2266
2267void CameraService::releaseSound() {
2268 Mutex::Autolock lock(mSoundLock);
2269 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2270 if (--mSoundRef) return;
2271
2272 for (int i = 0; i < NUM_SOUNDS; i++) {
2273 if (mSoundPlayer[i] != 0) {
2274 mSoundPlayer[i]->disconnect();
2275 mSoundPlayer[i].clear();
2276 }
2277 }
2278}
2279
2280void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002281 ATRACE_CALL();
2282
Mathias Agopian65ab4712010-07-14 17:59:35 -07002283 LOG1("playSound(%d)", kind);
2284 Mutex::Autolock lock(mSoundLock);
2285 sp<MediaPlayer> player = mSoundPlayer[kind];
2286 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002287 player->seekTo(0);
2288 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002289 }
2290}
2291
2292// ----------------------------------------------------------------------------
2293
2294CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002295 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002296 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002297 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002298 int clientPid, uid_t clientUid,
2299 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002300 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002301 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002302 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002303 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002304 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002305 servicePid),
2306 mCameraId(CameraService::cameraIdToInt(cameraIdStr))
Igor Murashkin634a5152013-02-20 17:15:11 -08002307{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002308 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002309 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002310
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002311 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002312
Mathias Agopian65ab4712010-07-14 17:59:35 -07002313 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002314
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002315 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002316}
2317
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318// tear down the client
2319CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002320 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002321 mDestructionStarted = true;
2322
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002323 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002324 // unconditionally disconnect. function is idempotent
2325 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002326}
2327
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002328sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2329
Igor Murashkin634a5152013-02-20 17:15:11 -08002330CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002331 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002332 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002333 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002334 int clientPid, uid_t clientUid,
2335 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002336 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2337 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2338 mServicePid(servicePid),
2339 mDisconnected(false),
2340 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002341{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002342 if (sCameraService == nullptr) {
2343 sCameraService = cameraService;
2344 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002345 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002346 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002347
2348 // In some cases the calling code has no access to the package it runs under.
2349 // For example, NDK camera API.
2350 // In this case we will get the packages for the calling UID and pick the first one
2351 // for attributing the app op. This will work correctly for runtime permissions
2352 // as for legacy apps we will toggle the app op for all packages in the UID.
2353 // The caveat is that the operation may be attributed to the wrong package and
2354 // stats based on app ops may be slightly off.
2355 if (mClientPackageName.size() <= 0) {
2356 sp<IServiceManager> sm = defaultServiceManager();
2357 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2358 if (binder == 0) {
2359 ALOGE("Cannot get permission service");
2360 // Leave mClientPackageName unchanged (empty) and the further interaction
2361 // with camera will fail in BasicClient::startCameraOps
2362 return;
2363 }
2364
2365 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2366 Vector<String16> packages;
2367
2368 permCtrl->getPackagesForUid(mClientUid, packages);
2369
2370 if (packages.isEmpty()) {
2371 ALOGE("No packages for calling UID");
2372 // Leave mClientPackageName unchanged (empty) and the further interaction
2373 // with camera will fail in BasicClient::startCameraOps
2374 return;
2375 }
2376 mClientPackageName = packages[0];
2377 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002378}
2379
2380CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002381 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002382 mDestructionStarted = true;
2383}
2384
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002385binder::Status CameraService::BasicClient::disconnect() {
2386 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002387 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002388 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002389 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002390 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002391
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002392 sCameraService->removeByClient(this);
2393 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002394 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002395
2396 sp<IBinder> remote = getRemote();
2397 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002398 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002399 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002400
2401 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002402 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002403 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2404 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2405 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002406
Igor Murashkincba2c162013-03-20 15:56:31 -07002407 // client shouldn't be able to call into us anymore
2408 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002409
2410 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002411}
2412
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002413status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2414 // No dumping of clients directly over Binder,
2415 // must go through CameraService::dump
2416 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2417 IPCThreadState::self()->getCallingUid(), NULL, 0);
2418 return OK;
2419}
2420
Ruben Brunkcc776712015-02-17 20:18:47 -08002421String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002422 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002423}
2424
2425
2426int CameraService::BasicClient::getClientPid() const {
2427 return mClientPid;
2428}
2429
Ruben Brunk6267b532015-04-30 17:44:07 -07002430uid_t CameraService::BasicClient::getClientUid() const {
2431 return mClientUid;
2432}
2433
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002434bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2435 // Defaults to API2.
2436 return level == API_2;
2437}
2438
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002439status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002440 ATRACE_CALL();
2441
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002442 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002443 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002444 mOpsCallback = new OpsCallback(this);
2445
Igor Murashkine6800ce2013-03-04 17:25:57 -08002446 {
2447 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002448 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002449 }
2450
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002451 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002452 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002453 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002454 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002455
Svetoslav28e8ef72015-05-11 19:21:31 -07002456 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002457 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2458 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002459 return PERMISSION_DENIED;
2460 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002461
Svetoslav28e8ef72015-05-11 19:21:31 -07002462 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002463 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2464 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002465 // Return the same error as for device policy manager rejection
2466 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002467 }
2468
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002469 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002470
2471 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002472 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002473
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002474 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002475 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2476 mCameraIdStr);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002477
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002478 return OK;
2479}
2480
2481status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002482 ATRACE_CALL();
2483
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002484 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002485 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002486 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002487 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002488 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002489 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002490
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002491 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
2492 StatusInternal::ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002493
Ruben Brunkcc776712015-02-17 20:18:47 -08002494 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002495 sCameraService->updateStatus(StatusInternal::PRESENT,
2496 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002497
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002498 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002499 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2500 mCameraIdStr);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002501 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002502 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002503 if (mOpsCallback != NULL) {
2504 mAppOpsManager.stopWatchingMode(mOpsCallback);
2505 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002506 mOpsCallback.clear();
2507
2508 return OK;
2509}
2510
2511void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002512 ATRACE_CALL();
2513
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002514 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002515 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002516
2517 if (op != AppOpsManager::OP_CAMERA) {
2518 ALOGW("Unexpected app ops notification received: %d", op);
2519 return;
2520 }
2521
2522 int32_t res;
2523 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002524 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002525 ALOGV("checkOp returns: %d, %s ", res,
2526 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2527 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2528 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2529 "UNKNOWN");
2530
2531 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002532 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002533 myName.string());
2534 // Reset the client PID to allow server-initiated disconnect,
2535 // and to prevent further calls by client.
2536 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002537 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002538 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002539 disconnect();
2540 }
2541}
2542
Mathias Agopian65ab4712010-07-14 17:59:35 -07002543// ----------------------------------------------------------------------------
2544
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002545void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002546 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002547 (void) errorCode;
2548 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002549 if (mRemoteCallback != NULL) {
2550 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2551 } else {
2552 ALOGE("mRemoteCallback is NULL!!");
2553 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002554}
2555
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002556// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002557binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002558 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002559 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002560}
2561
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002562bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2563 return level == API_1;
2564}
2565
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002566CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2567 mClient(client) {
2568}
2569
2570void CameraService::Client::OpsCallback::opChanged(int32_t op,
2571 const String16& packageName) {
2572 sp<BasicClient> client = mClient.promote();
2573 if (client != NULL) {
2574 client->opChanged(op, packageName);
2575 }
2576}
2577
Mathias Agopian65ab4712010-07-14 17:59:35 -07002578// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002579// CameraState
2580// ----------------------------------------------------------------------------
2581
2582CameraService::CameraState::CameraState(const String8& id, int cost,
2583 const std::set<String8>& conflicting) : mId(id),
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002584 mStatus(StatusInternal::PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002585
2586CameraService::CameraState::~CameraState() {}
2587
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002588CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002589 Mutex::Autolock lock(mStatusLock);
2590 return mStatus;
2591}
2592
2593CameraParameters CameraService::CameraState::getShimParams() const {
2594 return mShimParams;
2595}
2596
2597void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2598 mShimParams = params;
2599}
2600
2601int CameraService::CameraState::getCost() const {
2602 return mCost;
2603}
2604
2605std::set<String8> CameraService::CameraState::getConflicting() const {
2606 return mConflicting;
2607}
2608
2609String8 CameraService::CameraState::getId() const {
2610 return mId;
2611}
2612
2613// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002614// ClientEventListener
2615// ----------------------------------------------------------------------------
2616
2617void CameraService::ClientEventListener::onClientAdded(
2618 const resource_policy::ClientDescriptor<String8,
2619 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002620 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002621 if (basicClient.get() != nullptr) {
2622 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2623 notifier.noteStartCamera(descriptor.getKey(),
2624 static_cast<int>(basicClient->getClientUid()));
2625 }
2626}
2627
2628void CameraService::ClientEventListener::onClientRemoved(
2629 const resource_policy::ClientDescriptor<String8,
2630 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002631 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002632 if (basicClient.get() != nullptr) {
2633 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2634 notifier.noteStopCamera(descriptor.getKey(),
2635 static_cast<int>(basicClient->getClientUid()));
2636 }
2637}
2638
2639
2640// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002641// CameraClientManager
2642// ----------------------------------------------------------------------------
2643
Ruben Brunk99e69712015-05-26 17:25:07 -07002644CameraService::CameraClientManager::CameraClientManager() {
2645 setListener(std::make_shared<ClientEventListener>());
2646}
2647
Ruben Brunkcc776712015-02-17 20:18:47 -08002648CameraService::CameraClientManager::~CameraClientManager() {}
2649
2650sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2651 const String8& id) const {
2652 auto descriptor = get(id);
2653 if (descriptor == nullptr) {
2654 return sp<BasicClient>{nullptr};
2655 }
2656 return descriptor->getValue();
2657}
2658
2659String8 CameraService::CameraClientManager::toString() const {
2660 auto all = getAll();
2661 String8 ret("[");
2662 bool hasAny = false;
2663 for (auto& i : all) {
2664 hasAny = true;
2665 String8 key = i->getKey();
2666 int32_t cost = i->getCost();
2667 int32_t pid = i->getOwnerId();
Emilian Peev8131a262017-02-01 12:33:43 +00002668 int32_t score = i->getPriority().getScore();
2669 int32_t state = i->getPriority().getState();
Ruben Brunkcc776712015-02-17 20:18:47 -08002670 auto conflicting = i->getConflicting();
2671 auto clientSp = i->getValue();
2672 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002673 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002674 if (clientSp.get() != nullptr) {
2675 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002676 uid_t clientUid = clientSp->getClientUid();
2677 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002678 }
Emilian Peev8131a262017-02-01 12:33:43 +00002679 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %"
2680 PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002681
Ruben Brunk6267b532015-04-30 17:44:07 -07002682 if (clientSp.get() != nullptr) {
2683 ret.appendFormat("User Id: %d, ", clientUserId);
2684 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002685 if (packageName.size() != 0) {
2686 ret.appendFormat("Client Package Name: %s", packageName.string());
2687 }
2688
2689 ret.append(", Conflicting Client Devices: {");
2690 for (auto& j : conflicting) {
2691 ret.appendFormat("%s, ", j.string());
2692 }
2693 ret.append("})");
2694 }
2695 if (hasAny) ret.append("\n");
2696 ret.append("]\n");
2697 return ret;
2698}
2699
2700CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2701 const String8& key, const sp<BasicClient>& value, int32_t cost,
Emilian Peev8131a262017-02-01 12:33:43 +00002702 const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId,
2703 int32_t state) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002704
2705 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
Emilian Peev8131a262017-02-01 12:33:43 +00002706 key, value, cost, conflictingKeys, score, ownerId, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002707}
2708
2709CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2710 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2711 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
Emilian Peev8131a262017-02-01 12:33:43 +00002712 partial->getConflicting(), partial->getPriority().getScore(),
2713 partial->getOwnerId(), partial->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08002714}
2715
2716// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002717
2718static const int kDumpLockRetries = 50;
2719static const int kDumpLockSleep = 60000;
2720
2721static bool tryLock(Mutex& mutex)
2722{
2723 bool locked = false;
2724 for (int i = 0; i < kDumpLockRetries; ++i) {
2725 if (mutex.tryLock() == NO_ERROR) {
2726 locked = true;
2727 break;
2728 }
2729 usleep(kDumpLockSleep);
2730 }
2731 return locked;
2732}
2733
2734status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002735 ATRACE_CALL();
2736
Mathias Agopian65ab4712010-07-14 17:59:35 -07002737 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002738 dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07002739 getCallingPid(),
2740 getCallingUid());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002741 return NO_ERROR;
2742 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002743 bool locked = tryLock(mServiceLock);
2744 // failed to lock - CameraService is probably deadlocked
2745 if (!locked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002746 dprintf(fd, "!! CameraService may be deadlocked !!\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002747 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002748
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002749 if (!mInitialized) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002750 dprintf(fd, "!! No camera HAL available !!\n");
Ruben Brunkf81648e2014-04-17 16:14:57 -07002751
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002752 // Dump event log for error information
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002753 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002754
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002755 if (locked) mServiceLock.unlock();
2756 return NO_ERROR;
2757 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002758 dprintf(fd, "\n== Service global info: ==\n\n");
2759 dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras);
2760 dprintf(fd, "Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002761 String8 activeClientString = mActiveClientManager.toString();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002762 dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string());
2763 dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002764
2765 dumpEventLog(fd);
2766
2767 bool stateLocked = tryLock(mCameraStatesLock);
2768 if (!stateLocked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002769 dprintf(fd, "CameraStates in use, may be deadlocked\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002770 }
2771
2772 for (auto& state : mCameraStates) {
2773 String8 cameraId = state.first;
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002774
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002775 dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002776
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002777 CameraParameters p = state.second->getShimParams();
2778 if (!p.isEmpty()) {
2779 dprintf(fd, " Camera1 API shim is using parameters:\n ");
2780 p.dump(fd, args);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002781 }
2782
2783 auto clientDescriptor = mActiveClientManager.get(cameraId);
Zhijun He2f140ed2017-02-08 09:57:23 -08002784 if (clientDescriptor != nullptr) {
2785 dprintf(fd, " Device %s is open. Client instance dump:\n",
2786 cameraId.string());
2787 dprintf(fd, " Client priority score: %d state: %d\n",
2788 clientDescriptor->getPriority().getScore(),
2789 clientDescriptor->getPriority().getState());
2790 dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId());
2791
2792 auto client = clientDescriptor->getValue();
2793 dprintf(fd, " Client package: %s\n",
2794 String8(client->getPackageName()).string());
2795
2796 client->dumpClient(fd, args);
2797 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002798 dprintf(fd, " Device %s is closed, no client instance\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002799 cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002800 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002801
2802 if (mModule != nullptr) {
2803 dprintf(fd, "== Camera HAL device %s static information: ==\n", cameraId.string());
2804
2805 camera_info info;
2806 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
2807 int deviceVersion = -1;
2808 if (rc != OK) {
2809 dprintf(fd, " Error reading static information!\n");
2810 } else {
2811 dprintf(fd, " Facing: %s\n",
2812 info.facing == CAMERA_FACING_BACK ? "BACK" :
2813 info.facing == CAMERA_FACING_FRONT ? "FRONT" : "EXTERNAL");
2814 dprintf(fd, " Orientation: %d\n", info.orientation);
2815
2816 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
2817 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2818 } else {
2819 deviceVersion = info.device_version;
2820 }
2821 }
2822
2823 auto conflicting = state.second->getConflicting();
2824 dprintf(fd, " Resource Cost: %d\n", state.second->getCost());
2825 dprintf(fd, " Conflicting Devices:");
2826 for (auto& id : conflicting) {
2827 dprintf(fd, " %s", id.string());
2828 }
2829 if (conflicting.size() == 0) {
2830 dprintf(fd, " NONE");
2831 }
2832 dprintf(fd, "\n");
2833
2834 dprintf(fd, " Device version: %#x\n", deviceVersion);
2835 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
2836 dprintf(fd, " Device static metadata:\n");
2837 dump_indented_camera_metadata(info.static_camera_characteristics,
2838 fd, /*verbosity*/2, /*indentation*/4);
2839 }
2840 }
2841
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002842 }
2843
2844 if (stateLocked) mCameraStatesLock.unlock();
2845
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002846 if (locked) mServiceLock.unlock();
2847
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002848 if (mModule == nullptr) {
2849 mCameraProviderManager->dump(fd, args);
2850 } else {
2851 dprintf(fd, "\n== Camera Module HAL static info: ==\n");
2852 dprintf(fd, "Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2853 dprintf(fd, "Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2854 dprintf(fd, "Camera module name: %s\n", mModule->getModuleName());
2855 dprintf(fd, "Camera module author: %s\n", mModule->getModuleAuthor());
2856 }
2857
2858 dprintf(fd, "\n== Vendor tags: ==\n\n");
2859
2860 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2861 if (desc == NULL) {
2862 dprintf(fd, "No vendor tags.\n");
2863 } else {
2864 desc->dump(fd, /*verbosity*/2, /*indentation*/2);
2865 }
2866
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002867 // Dump camera traces if there were any
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002868 dprintf(fd, "\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002869 camera3::CameraTraces::dump(fd, args);
2870
2871 // Process dump arguments, if any
2872 int n = args.size();
2873 String16 verboseOption("-v");
2874 String16 unreachableOption("--unreachable");
2875 for (int i = 0; i < n; i++) {
2876 if (args[i] == verboseOption) {
2877 // change logging level
2878 if (i + 1 >= n) continue;
2879 String8 levelStr(args[i+1]);
2880 int level = atoi(levelStr.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002881 dprintf(fd, "\nSetting log level to %d.\n", level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002882 setLogLevel(level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002883 } else if (args[i] == unreachableOption) {
2884 // Dump memory analysis
2885 // TODO - should limit be an argument parameter?
2886 UnreachableMemoryInfo info;
2887 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2888 if (!success) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002889 dprintf(fd, "\n== Unable to dump unreachable memory. "
2890 "Try disabling SELinux enforcement. ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002891 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002892 dprintf(fd, "\n== Dumping unreachable memory: ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002893 std::string s = info.ToString(/*log_contents*/ true);
2894 write(fd, s.c_str(), s.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002895 }
2896 }
2897 }
2898 return NO_ERROR;
2899}
2900
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002901void CameraService::dumpEventLog(int fd) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002902 dprintf(fd, "\n== Camera service events log (most recent at top): ==\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002903
2904 Mutex::Autolock l(mLogLock);
2905 for (const auto& msg : mEventLog) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002906 dprintf(fd, " %s\n", msg.string());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002907 }
2908
2909 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002910 dprintf(fd, " ...\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002911 } else if (mEventLog.size() == 0) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002912 dprintf(fd, " [no events yet]\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002913 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002914 dprintf(fd, "\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002915}
2916
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002917void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002918 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002919 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2920 if (mTorchClientMap[i] == who) {
2921 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002922 String8 cameraId = mTorchClientMap.keyAt(i);
2923 status_t res = mFlashlight->setTorchMode(cameraId, false);
2924 if (res) {
2925 ALOGE("%s: torch client died but couldn't turn off torch: "
2926 "%s (%d)", __FUNCTION__, strerror(-res), res);
2927 return;
2928 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002929 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002930 break;
2931 }
2932 }
2933}
2934
Ruben Brunkcc776712015-02-17 20:18:47 -08002935/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002936
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002937 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002938 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2939 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002940 */
2941
Ruben Brunka8ca9152015-04-07 14:23:40 -07002942 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2943
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002944 // check torch client
2945 handleTorchClientBinderDied(who);
2946
2947 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002948 if(!evictClientIdByRemote(who)) {
2949 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002950 return;
2951 }
2952
Ruben Brunkcc776712015-02-17 20:18:47 -08002953 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2954 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002955}
2956
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002957void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002958 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002959}
2960
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002961void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2962 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002963 // Do not lock mServiceLock here or can get into a deadlock from
2964 // connect() -> disconnect -> updateStatus
2965
2966 auto state = getCameraState(cameraId);
2967
2968 if (state == nullptr) {
2969 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2970 cameraId.string());
2971 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002972 }
2973
Ruben Brunkcc776712015-02-17 20:18:47 -08002974 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2975 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2976 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002977 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002978
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002979 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002980 // Update torch status if it has a flash unit.
2981 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002982 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002983 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2984 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002985 TorchModeStatus newTorchStatus =
2986 status == StatusInternal::PRESENT ?
2987 TorchModeStatus::AVAILABLE_OFF :
2988 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002989 if (torchStatus != newTorchStatus) {
2990 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2991 }
2992 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002993 }
2994
2995 Mutex::Autolock lock(mStatusListenerLock);
2996
2997 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002998 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08002999 }
3000 });
Igor Murashkincba2c162013-03-20 15:56:31 -07003001}
3002
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003003template<class Func>
3004void CameraService::CameraState::updateStatus(StatusInternal status,
3005 const String8& cameraId,
3006 std::initializer_list<StatusInternal> rejectSourceStates,
3007 Func onStatusUpdatedLocked) {
3008 Mutex::Autolock lock(mStatusLock);
3009 StatusInternal oldStatus = mStatus;
3010 mStatus = status;
3011
3012 if (oldStatus == status) {
3013 return;
3014 }
3015
3016 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
3017 cameraId.string(), oldStatus, status);
3018
3019 if (oldStatus == StatusInternal::NOT_PRESENT &&
3020 (status != StatusInternal::PRESENT &&
3021 status != StatusInternal::ENUMERATING)) {
3022
3023 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
3024 __FUNCTION__);
3025 mStatus = oldStatus;
3026 return;
3027 }
3028
3029 /**
3030 * Sometimes we want to conditionally do a transition.
3031 * For example if a client disconnects, we want to go to PRESENT
3032 * only if we weren't already in NOT_PRESENT or ENUMERATING.
3033 */
3034 for (auto& rejectStatus : rejectSourceStates) {
3035 if (oldStatus == rejectStatus) {
3036 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
3037 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
3038 mStatus = oldStatus;
3039 return;
3040 }
3041 }
3042
3043 onStatusUpdatedLocked(cameraId, status);
3044}
3045
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07003046void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
3047 const String8& cameraId) {
3048 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
3049 if (proxyBinder == nullptr) return;
3050 String16 id(cameraId);
3051 proxyBinder->notifyCameraState(id, newState);
3052}
3053
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003054status_t CameraService::getTorchStatusLocked(
3055 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003056 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003057 if (!status) {
3058 return BAD_VALUE;
3059 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003060 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3061 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003062 // invalid camera ID or the camera doesn't have a flash unit
3063 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003064 }
3065
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003066 *status = mTorchStatusMap.valueAt(index);
3067 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003068}
3069
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003070status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003071 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003072 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3073 if (index == NAME_NOT_FOUND) {
3074 return BAD_VALUE;
3075 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003076 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003077
3078 return OK;
3079}
3080
Mathias Agopian65ab4712010-07-14 17:59:35 -07003081}; // namespace android