blob: f429df80ff9e1de548347e4aa87d1b3a6d7800c3 [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
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080061#include <android/hidl/manager/1.0/IServiceManager.h>
62#include <hidl/ServiceManagement.h>
63
Mathias Agopian65ab4712010-07-14 17:59:35 -070064#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070065#include "api1/CameraClient.h"
66#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070067#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070068#include "utils/CameraTraces.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080070namespace {
71 const char* kPermissionServiceName = "permission";
72}; // namespace anonymous
73
Mathias Agopian65ab4712010-07-14 17:59:35 -070074namespace android {
75
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080076using binder::Status;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080077using hardware::ICamera;
78using hardware::ICameraClient;
79using hardware::ICameraServiceListener;
80using hardware::camera::common::V1_0::CameraDeviceStatus;
81using hardware::camera::common::V1_0::TorchModeStatus;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080082
Mathias Agopian65ab4712010-07-14 17:59:35 -070083// ----------------------------------------------------------------------------
84// Logging support -- this is for debugging only
85// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070086volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070087
Steve Blockb8a80522011-12-20 16:23:08 +000088#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
89#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070090
91static void setLogLevel(int level) {
92 android_atomic_write(level, &gLogLevel);
93}
94
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080095// Convenience methods for constructing binder::Status objects for error returns
96
97#define STATUS_ERROR(errorCode, errorString) \
98 binder::Status::fromServiceSpecificError(errorCode, \
99 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
100
101#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
102 binder::Status::fromServiceSpecificError(errorCode, \
103 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
104 __VA_ARGS__))
105
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106// ----------------------------------------------------------------------------
107
Igor Murashkincba2c162013-03-20 15:56:31 -0700108extern "C" {
109static void camera_device_status_change(
110 const struct camera_module_callbacks* callbacks,
111 int camera_id,
112 int new_status) {
113 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -0800114 static_cast<const CameraService*>(callbacks));
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800115 String8 id = String8::format("%d", camera_id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700116
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800117 CameraDeviceStatus newStatus{CameraDeviceStatus::NOT_PRESENT};
118 switch (new_status) {
119 case CAMERA_DEVICE_STATUS_NOT_PRESENT:
120 newStatus = CameraDeviceStatus::NOT_PRESENT;
121 break;
122 case CAMERA_DEVICE_STATUS_PRESENT:
123 newStatus = CameraDeviceStatus::PRESENT;
124 break;
125 case CAMERA_DEVICE_STATUS_ENUMERATING:
126 newStatus = CameraDeviceStatus::ENUMERATING;
127 break;
128 default:
129 ALOGW("Unknown device status change to %d", new_status);
130 break;
131 }
132 cs->onDeviceStatusChanged(id, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700133}
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800134
135static void torch_mode_status_change(
136 const struct camera_module_callbacks* callbacks,
137 const char* camera_id,
138 int new_status) {
139 if (!callbacks || !camera_id) {
140 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
141 callbacks, camera_id);
142 }
143 sp<CameraService> cs = const_cast<CameraService*>(
144 static_cast<const CameraService*>(callbacks));
145
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800146 TorchModeStatus status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800147 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800148 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800149 status = TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800150 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800151 case TORCH_MODE_STATUS_AVAILABLE_OFF:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800152 status = TorchModeStatus::AVAILABLE_OFF;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800153 break;
154 case TORCH_MODE_STATUS_AVAILABLE_ON:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800155 status = TorchModeStatus::AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800156 break;
157 default:
158 ALOGE("Unknown torch status %d", new_status);
159 return;
160 }
161
162 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800163 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800164 status);
165}
Igor Murashkincba2c162013-03-20 15:56:31 -0700166} // extern "C"
167
Mathias Agopian65ab4712010-07-14 17:59:35 -0700168// ----------------------------------------------------------------------------
169
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800170CameraService::CameraService() :
171 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800172 mNumberOfCameras(0), mNumberOfNormalCameras(0),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800173 mSoundRef(0), mInitialized(false), mModule(nullptr) {
Steve Blockdf64d152012-01-04 20:05:49 +0000174 ALOGI("CameraService started (pid=%d)", getpid());
Igor Murashkinbfc99152013-02-27 12:55:20 -0800175
Igor Murashkincba2c162013-03-20 15:56:31 -0700176 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800177 this->torch_mode_status_change = android::torch_mode_status_change;
178
Ruben Brunkcc776712015-02-17 20:18:47 -0800179 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700180}
181
Iliyan Malchev8951a972011-04-14 16:55:59 -0700182void CameraService::onFirstRef()
183{
Ruben Brunkcc776712015-02-17 20:18:47 -0800184 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800185
Iliyan Malchev8951a972011-04-14 16:55:59 -0700186 BnCameraService::onFirstRef();
187
Ruben Brunk99e69712015-05-26 17:25:07 -0700188 // Update battery life tracking if service is restarting
189 BatteryNotifier& notifier(BatteryNotifier::getInstance());
190 notifier.noteResetCamera();
191 notifier.noteResetFlashlight();
192
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800193 status_t res = INVALID_OPERATION;
Eino-Ville Talvala9cbbc832017-01-23 15:39:53 -0800194
195 bool disableTreble = property_get_bool("camera.disable_treble", false);
196 if (disableTreble) {
197 ALOGI("Treble disabled - using legacy path");
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800198 res = loadLegacyHalModule();
Eino-Ville Talvala9cbbc832017-01-23 15:39:53 -0800199 } else {
200 res = enumerateProviders();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800201 }
202 if (res == OK) {
203 mInitialized = true;
204 }
205
206 CameraService::pingCameraServiceProxy();
207}
208
209status_t CameraService::loadLegacyHalModule() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800210 camera_module_t *rawModule;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700211 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
212 (const hw_module_t **)&rawModule);
213 if (err < 0) {
214 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
215 logServiceError("Could not load camera HAL module", err);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800216 return INVALID_OPERATION;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700217 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800218
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700219 mModule = new CameraModule(rawModule);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700220 err = mModule->init();
221 if (err != OK) {
222 ALOGE("Could not initialize camera HAL module: %d (%s)", err,
223 strerror(-err));
224 logServiceError("Could not initialize camera HAL module", err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800225
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700226 delete mModule;
227 mModule = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800228 return INVALID_OPERATION;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700229 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700230 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700231
232 mNumberOfCameras = mModule->getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700233 mNumberOfNormalCameras = mNumberOfCameras;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700234
Yin-Chia Yehd4a653a2015-10-14 11:05:44 -0700235 // Setup vendor tags before we call get_camera_info the first time
236 // because HAL might need to setup static vendor keys in get_camera_info
237 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
238 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
239 setUpVendorTags();
240 }
241
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800242 mFlashlight = new CameraFlashlight(mModule, this);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700243 status_t res = mFlashlight->findFlashUnits();
244 if (res) {
245 // impossible because we haven't open any camera devices.
246 ALOGE("Failed to find flash units.");
247 }
248
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700249 int latestStrangeCameraId = INT_MAX;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700250 for (int i = 0; i < mNumberOfCameras; i++) {
251 String8 cameraId = String8::format("%d", i);
252
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700253 // Get camera info
254
255 struct camera_info info;
256 bool haveInfo = true;
257 status_t rc = mModule->getCameraInfo(i, &info);
258 if (rc != NO_ERROR) {
259 ALOGE("%s: Received error loading camera info for device %d, cost and"
260 " conflicting devices fields set to defaults for this device.",
261 __FUNCTION__, i);
262 haveInfo = false;
263 }
264
265 // Check for backwards-compatibility support
266 if (haveInfo) {
267 if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) {
268 delete mModule;
269 mModule = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800270 return INVALID_OPERATION;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700271 }
272 }
273
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700274 // Defaults to use for cost and conflicting devices
275 int cost = 100;
276 char** conflicting_devices = nullptr;
277 size_t conflicting_devices_length = 0;
278
279 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700280 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
281 cost = info.resource_cost;
282 conflicting_devices = info.conflicting_devices;
283 conflicting_devices_length = info.conflicting_devices_length;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700284 }
285
286 std::set<String8> conflicting;
287 for (size_t i = 0; i < conflicting_devices_length; i++) {
288 conflicting.emplace(String8(conflicting_devices[i]));
289 }
290
291 // Initialize state for each camera device
292 {
293 Mutex::Autolock lock(mCameraStatesLock);
294 mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost,
295 conflicting));
296 }
297
298 if (mFlashlight->hasFlashUnit(cameraId)) {
299 mTorchStatusMap.add(cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800300 TorchModeStatus::AVAILABLE_OFF);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700301 }
302 }
303
304 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
305 mModule->setCallbacks(this);
306 }
307
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800308 return OK;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700309}
310
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800311status_t CameraService::enumerateProviders() {
312 mCameraProviderManager = new CameraProviderManager();
313 status_t res;
314 res = mCameraProviderManager->initialize(this);
315 if (res != OK) {
316 ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
317 __FUNCTION__, strerror(-res), res);
318 return res;
319 }
320
321 mNumberOfCameras = mCameraProviderManager->getCameraCount();
322 mNumberOfNormalCameras = mCameraProviderManager->getStandardCameraCount();
323
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800324 // Setup vendor tags before we call get_camera_info the first time
325 // because HAL might need to setup static vendor keys in get_camera_info
326 // TODO: maybe put this into CameraProviderManager::initialize()?
327 mCameraProviderManager->setUpVendorTags();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800328
329 mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
330 res = mFlashlight->findFlashUnits();
331 if (res != OK) {
332 ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
333 }
334
335 // TODO: Verify device versions are in support
336
337 for (auto& cameraId : mCameraProviderManager->getCameraDeviceIds()) {
338 hardware::camera::common::V1_0::CameraResourceCost cost;
339 res = mCameraProviderManager->getResourceCost(cameraId, &cost);
340 if (res != OK) {
341 ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res);
342 continue;
343 }
344 std::set<String8> conflicting;
345 for (size_t i = 0; i < cost.conflictingDevices.size(); i++) {
346 conflicting.emplace(String8(cost.conflictingDevices[i].c_str()));
347 }
348 String8 id8 = String8(cameraId.c_str());
349
350 Mutex::Autolock lock(mCameraStatesLock);
351 mCameraStates.emplace(id8,
352 std::make_shared<CameraState>(id8, cost.resourceCost, conflicting));
353
354 if (mFlashlight->hasFlashUnit(id8)) {
355 mTorchStatusMap.add(id8,
356 TorchModeStatus::AVAILABLE_OFF);
357 }
358 }
359
360 return OK;
361}
362
363
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700364sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800365 sp<ICameraServiceProxy> proxyBinder = nullptr;
366#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700367 sp<IServiceManager> sm = defaultServiceManager();
Eino-Ville Talvalaf4db13d2016-12-08 11:20:51 -0800368 // Use checkService because cameraserver normally starts before the
369 // system server and the proxy service. So the long timeout that getService
370 // has before giving up is inappropriate.
371 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800372 if (binder != nullptr) {
373 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700374 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800375#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700376 return proxyBinder;
377}
378
379void CameraService::pingCameraServiceProxy() {
380 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
381 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700382 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700383}
384
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800386 if (mModule) {
387 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800388 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800389 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800390 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700391}
392
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800393void CameraService::onDeviceStatusChanged(const String8& id,
394 CameraDeviceStatus newHalStatus) {
395 ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__,
396 id.string(), newHalStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700397
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800398 StatusInternal newStatus = mapToInternal(newHalStatus);
399
Ruben Brunkcc776712015-02-17 20:18:47 -0800400 std::shared_ptr<CameraState> state = getCameraState(id);
401
402 if (state == nullptr) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800403 ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700404 return;
405 }
406
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800407 StatusInternal oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800408
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800409 if (oldStatus == newStatus) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800410 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700411 return;
412 }
413
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800414 if (newStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700415 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
416 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800417 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700418 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800419 // Don't do this in updateStatus to avoid deadlock over mServiceLock
420 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700421
Ruben Brunkcc776712015-02-17 20:18:47 -0800422 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
423 // to this device until the status changes
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800424 updateStatus(StatusInternal::NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700425
Ruben Brunkcc776712015-02-17 20:18:47 -0800426 // Remove cached shim parameters
427 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700428
Ruben Brunkcc776712015-02-17 20:18:47 -0800429 // Remove the client from the list of active clients
430 clientToDisconnect = removeClientLocked(id);
431
432 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800433 clientToDisconnect->notifyError(
434 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800435 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700436 }
437
Ruben Brunkcc776712015-02-17 20:18:47 -0800438 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
439 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700440
Ruben Brunkcc776712015-02-17 20:18:47 -0800441 // Disconnect client
442 if (clientToDisconnect.get() != nullptr) {
443 // Ensure not in binder RPC so client disconnect PID checks work correctly
444 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
445 "onDeviceStatusChanged must be called from the camera service process!");
446 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700447 }
448
Ruben Brunkcc776712015-02-17 20:18:47 -0800449 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800450 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700451 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
452 newStatus));
453 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800454 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700455 }
456
Igor Murashkincba2c162013-03-20 15:56:31 -0700457}
458
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800459void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800460 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800461 Mutex::Autolock al(mTorchStatusMutex);
462 onTorchStatusChangedLocked(cameraId, newStatus);
463}
464
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800465void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800466 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800467 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
468 __FUNCTION__, cameraId.string(), newStatus);
469
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800470 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800471 status_t res = getTorchStatusLocked(cameraId, &status);
472 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700473 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
474 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800475 return;
476 }
477 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800478 return;
479 }
480
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800481 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800482 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800483 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
484 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800485 return;
486 }
487
Ruben Brunkcc776712015-02-17 20:18:47 -0800488 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700489 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700490 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700491 auto iter = mTorchUidMap.find(cameraId);
492 if (iter != mTorchUidMap.end()) {
493 int oldUid = iter->second.second;
494 int newUid = iter->second.first;
495 BatteryNotifier& notifier(BatteryNotifier::getInstance());
496 if (oldUid != newUid) {
497 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800498 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700499 notifier.noteFlashlightOff(cameraId, oldUid);
500 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800501 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700502 notifier.noteFlashlightOn(cameraId, newUid);
503 }
504 iter->second.second = newUid;
505 } else {
506 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800507 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700508 notifier.noteFlashlightOn(cameraId, oldUid);
509 } else {
510 notifier.noteFlashlightOff(cameraId, oldUid);
511 }
512 }
513 }
514 }
515
516 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800517 Mutex::Autolock lock(mStatusListenerLock);
518 for (auto& i : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800519 i->onTorchStatusChanged(mapToInterface(newStatus), String16{cameraId});
Ruben Brunkcc776712015-02-17 20:18:47 -0800520 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800521 }
522}
523
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800524Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700525 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700526 switch (type) {
527 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800528 *numCameras = mNumberOfNormalCameras;
529 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700530 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 *numCameras = mNumberOfCameras;
532 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700533 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800534 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700535 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800536 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
537 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700538 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800539 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540}
541
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800542Status CameraService::getCameraInfo(int cameraId,
543 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700544 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800545 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800546 return STATUS_ERROR(ERROR_DISCONNECTED,
547 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700548 }
549
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
552 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 }
554
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 Status ret = Status::ok();
556 if (mModule != nullptr) {
557 struct camera_info info;
558 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800559
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800560 if (ret.isOk()) {
561 cameraInfo->facing = info.facing;
562 cameraInfo->orientation = info.orientation;
563 // CameraInfo is for android.hardware.Camera which does not
564 // support external camera facing. The closest approximation would be
565 // front camera.
566 if (cameraInfo->facing == CAMERA_FACING_EXTERNAL) {
567 cameraInfo->facing = hardware::CAMERA_FACING_FRONT;
568 }
Yin-Chia Yeh9b5a6e92016-04-22 13:24:05 -0700569 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800570 } else {
571 status_t err = mCameraProviderManager->getCameraInfo(std::to_string(cameraId), cameraInfo);
572 if (err != OK) {
573 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
574 "Error retrieving camera info from device %d: %s (%d)", cameraId,
575 strerror(-err), err);
576 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800577 }
578 return ret;
579}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700580
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800581int CameraService::cameraIdToInt(const String8& cameraId) {
582 int id;
583 bool success = base::ParseInt(cameraId.string(), &id, 0);
584 if (!success) {
585 return -1;
586 }
587 return id;
588}
589
590Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800591 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700592 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700593 if (!cameraInfo) {
594 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800595 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700596 }
597
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800598 if (!mInitialized) {
599 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600 return STATUS_ERROR(ERROR_DISCONNECTED,
601 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700602 }
603
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800604 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800605
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800606 if (mModule != nullptr) {
607 int id = cameraIdToInt(String8(cameraId));
Zhijun He2b59be82013-09-25 10:14:30 -0700608
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800609 if (id < 0 || id >= mNumberOfCameras) {
610 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, id);
611 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
612 "Invalid camera id: %d", id);
613 }
614
615 int version = getDeviceVersion(String8(cameraId));
616 if (version < CAMERA_DEVICE_API_VERSION_3_0) {
617 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "Can't get camera characteristics"
618 " for devices with HAL version < 3.0, %d is version %x", id, version);
619 }
620
621 struct camera_info info;
622 ret = filterGetInfoErrorCode(mModule->getCameraInfo(id, &info));
623 if (ret.isOk()) {
624 *cameraInfo = info.static_camera_characteristics;
625 }
626 } else {
627 status_t res = mCameraProviderManager->getCameraCharacteristics(
628 String16::std_string(cameraId), cameraInfo);
629 if (res != OK) {
630 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
631 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
632 strerror(-res), res);
633 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700634 }
Zhijun He2b59be82013-09-25 10:14:30 -0700635
636 return ret;
637}
638
Ruben Brunkcc776712015-02-17 20:18:47 -0800639int CameraService::getCallingPid() {
640 return IPCThreadState::self()->getCallingPid();
641}
642
643int CameraService::getCallingUid() {
644 return IPCThreadState::self()->getCallingUid();
645}
646
647String8 CameraService::getFormattedCurrentTime() {
648 time_t now = time(nullptr);
649 char formattedTime[64];
650 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
651 return String8(formattedTime);
652}
653
654int CameraService::getCameraPriorityFromProcState(int procState) {
655 // Find the priority for the camera usage based on the process state. Higher priority clients
656 // win for evictions.
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700657 if (procState < 0) {
658 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
659 procState);
660 return -1;
Ruben Brunkcc776712015-02-17 20:18:47 -0800661 }
Eino-Ville Talvala52aad852015-09-03 12:24:24 -0700662 // Treat sleeping TOP processes the same as regular TOP processes, for
663 // access priority. This is important for lock-screen camera launch scenarios
664 if (procState == PROCESS_STATE_TOP_SLEEPING) {
665 procState = PROCESS_STATE_TOP;
666 }
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700667 return INT_MAX - procState;
Ruben Brunkcc776712015-02-17 20:18:47 -0800668}
669
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800670Status CameraService::getCameraVendorTagDescriptor(
671 /*out*/
672 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700673 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800674 if (!mInitialized) {
675 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800676 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800677 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800678 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
679 if (globalDescriptor != nullptr) {
680 *desc = *(globalDescriptor.get());
681 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800682 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800683}
684
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800685int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700686 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800687
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800688 int deviceVersion = 0;
689
690 if (mModule != nullptr) {
691 int id = cameraIdToInt(cameraId);
692 if (id < 0) return -1;
693
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800694 struct camera_info info;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800695 if (mModule->getCameraInfo(id, &info) != OK) {
696 return -1;
697 }
698
699 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
700 deviceVersion = info.device_version;
701 } else {
702 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
703 }
704
705 if (facing) {
706 *facing = info.facing;
707 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800708 } else {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800709 status_t res;
710 hardware::hidl_version maxVersion{0,0};
711 res = mCameraProviderManager->getHighestSupportedVersion(String8::std_string(cameraId),
712 &maxVersion);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800713 if (res != OK) return -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800714 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800715
716 hardware::CameraInfo info;
717 if (facing) {
718 res = mCameraProviderManager->getCameraInfo(String8::std_string(cameraId), &info);
719 if (res != OK) return -1;
720 *facing = info.facing;
721 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800722 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800723 return deviceVersion;
724}
725
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800726Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700727 switch(err) {
728 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800729 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800730 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800731 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
732 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800733 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800734 return STATUS_ERROR(ERROR_DISCONNECTED,
735 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700736 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800737 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
738 "Camera HAL encountered error %d: %s",
739 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700740 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800741}
742
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800743bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700744 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800745 if (mModule == nullptr) return false;
746
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800747 vendor_tag_ops_t vOps = vendor_tag_ops_t();
748
749 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800750 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800751 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
752 return false;
753 }
754
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800755 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800756
757 // Ensure all vendor operations are present
758 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
759 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
760 vOps.get_tag_type == NULL) {
761 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
762 , __FUNCTION__);
763 return false;
764 }
765
766 // Read all vendor tag definitions into a descriptor
767 sp<VendorTagDescriptor> desc;
768 status_t res;
769 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
770 != OK) {
771 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
772 "received error %s (%d). Camera clients will not be able to use"
773 "vendor tags", __FUNCTION__, strerror(res), res);
774 return false;
775 }
776
777 // Set the global descriptor to use with camera metadata
778 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
779 return true;
780}
781
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800782Status CameraService::makeClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800783 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800784 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
785 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
786 /*out*/sp<BasicClient>* client) {
787
Ruben Brunkcc776712015-02-17 20:18:47 -0800788 if (halVersion < 0 || halVersion == deviceVersion) {
789 // Default path: HAL version is unspecified by caller, create CameraClient
790 // based on device version reported by the HAL.
791 switch(deviceVersion) {
792 case CAMERA_DEVICE_API_VERSION_1_0:
793 if (effectiveApiLevel == API_1) { // Camera1 API route
794 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800795 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
796 facing, clientPid, clientUid, getpid(), legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800797 } else { // Camera2 API route
798 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800799 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800800 "Camera device \"%s\" HAL version %d does not support camera2 API",
801 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800802 }
803 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800804 case CAMERA_DEVICE_API_VERSION_3_0:
805 case CAMERA_DEVICE_API_VERSION_3_1:
806 case CAMERA_DEVICE_API_VERSION_3_2:
807 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700808 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800809 if (effectiveApiLevel == API_1) { // Camera1 API route
810 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800811 *client = new Camera2Client(cameraService, tmp, packageName, cameraIdToInt(cameraId),
812 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800813 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800814 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
815 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
816 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800817 facing, clientPid, clientUid, servicePid);
818 }
819 break;
820 default:
821 // Should not be reachable
822 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800823 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800824 "Camera device \"%s\" has unknown HAL version %d",
825 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800826 }
827 } else {
828 // A particular HAL version is requested by caller. Create CameraClient
829 // based on the requested HAL version.
830 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
831 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
832 // Only support higher HAL version device opened as HAL1.0 device.
833 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800834 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
835 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800836 } else {
837 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
838 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
839 " opened as HAL %x device", halVersion, deviceVersion,
840 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800841 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800842 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
843 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800844 }
845 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800846 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800847}
848
Ruben Brunk6267b532015-04-30 17:44:07 -0700849String8 CameraService::toString(std::set<userid_t> intSet) {
850 String8 s("");
851 bool first = true;
852 for (userid_t i : intSet) {
853 if (first) {
854 s.appendFormat("%d", i);
855 first = false;
856 } else {
857 s.appendFormat(", %d", i);
858 }
859 }
860 return s;
861}
862
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800863int32_t CameraService::mapToInterface(TorchModeStatus status) {
864 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
865 switch (status) {
866 case TorchModeStatus::NOT_AVAILABLE:
867 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
868 break;
869 case TorchModeStatus::AVAILABLE_OFF:
870 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
871 break;
872 case TorchModeStatus::AVAILABLE_ON:
873 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
874 break;
875 default:
876 ALOGW("Unknown new flash status: %d", status);
877 }
878 return serviceStatus;
879}
880
881CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
882 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
883 switch (status) {
884 case CameraDeviceStatus::NOT_PRESENT:
885 serviceStatus = StatusInternal::NOT_PRESENT;
886 break;
887 case CameraDeviceStatus::PRESENT:
888 serviceStatus = StatusInternal::PRESENT;
889 break;
890 case CameraDeviceStatus::ENUMERATING:
891 serviceStatus = StatusInternal::ENUMERATING;
892 break;
893 default:
894 ALOGW("Unknown new HAL device status: %d", status);
895 }
896 return serviceStatus;
897}
898
899int32_t CameraService::mapToInterface(StatusInternal status) {
900 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
901 switch (status) {
902 case StatusInternal::NOT_PRESENT:
903 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
904 break;
905 case StatusInternal::PRESENT:
906 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
907 break;
908 case StatusInternal::ENUMERATING:
909 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
910 break;
911 case StatusInternal::NOT_AVAILABLE:
912 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
913 break;
914 case StatusInternal::UNKNOWN:
915 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
916 break;
917 default:
918 ALOGW("Unknown new internal device status: %d", status);
919 }
920 return serviceStatus;
921}
922
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800923Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800924 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700925
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800926 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800927 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800928 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800929 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800930 if (!(ret = connectHelper<ICameraClient,Client>(
931 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
932 internalPackageName, uid, USE_CALLING_PID,
933 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
934 /*out*/ tmp)
935 ).isOk()) {
936 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700937 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800938 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700939}
940
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800941Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700942 /*out*/
943 CameraParameters* parameters) {
944
945 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
946
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800947 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700948
949 if (parameters == NULL) {
950 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800951 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700952 }
953
Ruben Brunkcc776712015-02-17 20:18:47 -0800954 String8 id = String8::format("%d", cameraId);
955
956 // Check if we already have parameters
957 {
958 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700959 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800960 auto cameraState = getCameraState(id);
961 if (cameraState == nullptr) {
962 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800963 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
964 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800965 }
966 CameraParameters p = cameraState->getShimParams();
967 if (!p.isEmpty()) {
968 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800969 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700970 }
971 }
972
Ruben Brunkcc776712015-02-17 20:18:47 -0800973 int64_t token = IPCThreadState::self()->clearCallingIdentity();
974 ret = initializeShimMetadata(cameraId);
975 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800976 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800977 // Error already logged by callee
978 return ret;
979 }
980
981 // Check for parameters again
982 {
983 // Scope for service lock
984 Mutex::Autolock lock(mServiceLock);
985 auto cameraState = getCameraState(id);
986 if (cameraState == nullptr) {
987 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800988 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
989 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700990 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800991 CameraParameters p = cameraState->getShimParams();
992 if (!p.isEmpty()) {
993 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800994 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700995 }
996 }
997
Ruben Brunkcc776712015-02-17 20:18:47 -0800998 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
999 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001000 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001001}
1002
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001003// Can camera service trust the caller based on the calling UID?
1004static bool isTrustedCallingUid(uid_t uid) {
1005 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -07001006 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001007 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -07001008 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001009 return true;
1010 default:
1011 return false;
1012 }
1013}
1014
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001015Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001016 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
1017 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -05001018
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001019#ifdef __BRILLO__
1020 UNUSED(clientName8);
1021 UNUSED(clientUid);
1022 UNUSED(clientPid);
1023 UNUSED(originalClientPid);
1024#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001025 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
1026 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001027 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -08001028 return allowed;
1029 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001030#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -08001031
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001032 int callingPid = getCallingPid();
1033
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001034 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001035 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
1036 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001037 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1038 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -07001039 }
1040
Ruben Brunkcc776712015-02-17 20:18:47 -08001041 if (getCameraState(cameraId) == nullptr) {
1042 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1043 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001044 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1045 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001046 }
1047
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001048 status_t err = checkIfDeviceIsUsable(cameraId);
1049 if (err != NO_ERROR) {
1050 switch(err) {
1051 case -ENODEV:
1052 case -EBUSY:
1053 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1054 "No camera device with ID \"%s\" currently available", cameraId.string());
1055 default:
1056 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1057 "Unknown error connecting to ID \"%s\"", cameraId.string());
1058 }
1059 }
1060 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001061}
1062
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001063Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001064 const String8& clientName8, int& clientUid, int& clientPid,
1065 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001066 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001067 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001069 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -07001070 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001071 clientUid = callingUid;
1072 } else if (!isTrustedCallingUid(callingUid)) {
1073 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1074 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001075 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1076 "Untrusted caller (calling PID %d, UID %d) trying to "
1077 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1078 callingPid, callingUid, cameraId.string(),
1079 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001080 }
1081
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001082 // Check if we can trust clientPid
1083 if (clientPid == USE_CALLING_PID) {
1084 clientPid = callingPid;
1085 } else if (!isTrustedCallingUid(callingUid)) {
1086 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1087 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001088 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1089 "Untrusted caller (calling PID %d, UID %d) trying to "
1090 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1091 callingPid, callingUid, cameraId.string(),
1092 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001093 }
1094
1095 // If it's not calling from cameraserver, check the permission.
1096 if (callingPid != getpid() &&
1097 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
1098 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001099 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1100 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1101 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001102 }
1103
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001104 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1105 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001106 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001107 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001108
Ruben Brunk6267b532015-04-30 17:44:07 -07001109 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001110
Ruben Brunka8ca9152015-04-07 14:23:40 -07001111 // Only allow clients who are being used by the current foreground device user, unless calling
1112 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001113 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1114 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1115 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1116 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001117 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1118 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1119 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001120 }
1121
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001122 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001123}
1124
1125status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1126 auto cameraState = getCameraState(cameraId);
1127 int callingPid = getCallingPid();
1128 if (cameraState == nullptr) {
1129 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1130 cameraId.string());
1131 return -ENODEV;
1132 }
1133
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001134 StatusInternal currentStatus = cameraState->getStatus();
1135 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001136 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1137 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001138 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001139 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001140 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1141 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001142 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001143 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001144
Ruben Brunkcc776712015-02-17 20:18:47 -08001145 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001146}
1147
Ruben Brunkcc776712015-02-17 20:18:47 -08001148void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1149 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001150
Ruben Brunkcc776712015-02-17 20:18:47 -08001151 // Make a descriptor for the incoming client
1152 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1153 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1154
1155 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1156 String8(client->getPackageName()));
1157
1158 if (evicted.size() > 0) {
1159 // This should never happen - clients should already have been removed in disconnect
1160 for (auto& i : evicted) {
1161 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1162 __FUNCTION__, i->getKey().string());
1163 }
1164
1165 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1166 __FUNCTION__);
1167 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001168
1169 // And register a death notification for the client callback. Do
1170 // this last to avoid Binder policy where a nested Binder
1171 // transaction might be pre-empted to service the client death
1172 // notification if the client process dies before linkToDeath is
1173 // invoked.
1174 sp<IBinder> remoteCallback = client->getRemote();
1175 if (remoteCallback != nullptr) {
1176 remoteCallback->linkToDeath(this);
1177 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001178}
1179
1180status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1181 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1182 /*out*/
1183 sp<BasicClient>* client,
1184 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001185 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001186 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001187 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001188 DescriptorPtr clientDescriptor;
1189 {
1190 if (effectiveApiLevel == API_1) {
1191 // If we are using API1, any existing client for this camera ID with the same remote
1192 // should be returned rather than evicted to allow MediaRecorder to work properly.
1193
1194 auto current = mActiveClientManager.get(cameraId);
1195 if (current != nullptr) {
1196 auto clientSp = current->getValue();
1197 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001198 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1199 ALOGW("CameraService connect called from same client, but with a different"
1200 " API level, evicting prior client...");
1201 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001202 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001203 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001204 *client = clientSp;
1205 return NO_ERROR;
1206 }
1207 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001208 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001209 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001210
Ruben Brunkcc776712015-02-17 20:18:47 -08001211 // Get current active client PIDs
1212 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1213 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001214
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001215 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1216 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1217 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1218 // not have an out-of-class definition.
1219 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001220
Ruben Brunkcc776712015-02-17 20:18:47 -08001221 // Get priorites of all active PIDs
1222 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1223 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001224
Ruben Brunkcc776712015-02-17 20:18:47 -08001225 // Update all active clients' priorities
1226 std::map<int,int> pidToPriorityMap;
1227 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1228 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1229 }
1230 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001231
Ruben Brunkcc776712015-02-17 20:18:47 -08001232 // Get state for the given cameraId
1233 auto state = getCameraState(cameraId);
1234 if (state == nullptr) {
1235 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1236 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001237 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001238 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001239 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001240
1241 // Make descriptor for incoming client
1242 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1243 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1244 state->getConflicting(),
1245 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1246
1247 // Find clients that would be evicted
1248 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1249
1250 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1251 // background, so we cannot do evictions
1252 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1253 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1254 " priority).", clientPid);
1255
1256 sp<BasicClient> clientSp = clientDescriptor->getValue();
1257 String8 curTime = getFormattedCurrentTime();
1258 auto incompatibleClients =
1259 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1260
1261 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001262 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001263 cameraId.string(), packageName.string(), clientPid,
1264 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1265
1266 for (auto& i : incompatibleClients) {
1267 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1268 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1269 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1270 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001271 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1272 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1273 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1274 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001275 }
1276
1277 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001278 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001279 mEventLog.add(msg);
1280
1281 return -EBUSY;
1282 }
1283
1284 for (auto& i : evicted) {
1285 sp<BasicClient> clientSp = i->getValue();
1286 if (clientSp.get() == nullptr) {
1287 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1288
1289 // TODO: Remove this
1290 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1291 __FUNCTION__);
1292 mActiveClientManager.remove(i);
1293 continue;
1294 }
1295
1296 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1297 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001298 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001299
Ruben Brunkcc776712015-02-17 20:18:47 -08001300 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001301 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1302 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1303 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001304 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1305 i->getOwnerId(), i->getPriority(), cameraId.string(),
1306 packageName.string(), clientPid,
1307 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1308
1309 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001310 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001311 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001312 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001313 }
1314
Ruben Brunkcc776712015-02-17 20:18:47 -08001315 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1316 // other clients from connecting in mServiceLockWrapper if held
1317 mServiceLock.unlock();
1318
1319 // Clear caller identity temporarily so client disconnect PID checks work correctly
1320 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1321
1322 // Destroy evicted clients
1323 for (auto& i : evictedClients) {
1324 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001325 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001326 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001327
1328 IPCThreadState::self()->restoreCallingIdentity(token);
1329
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001330 for (const auto& i : evictedClients) {
1331 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1332 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1333 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1334 if (ret == TIMED_OUT) {
1335 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1336 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1337 mActiveClientManager.toString().string());
1338 return -EBUSY;
1339 }
1340 if (ret != NO_ERROR) {
1341 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1342 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1343 ret, mActiveClientManager.toString().string());
1344 return ret;
1345 }
1346 }
1347
1348 evictedClients.clear();
1349
Ruben Brunkcc776712015-02-17 20:18:47 -08001350 // Once clients have been disconnected, relock
1351 mServiceLock.lock();
1352
1353 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1354 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1355 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001356 }
1357
Ruben Brunkcc776712015-02-17 20:18:47 -08001358 *partial = clientDescriptor;
1359 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001360}
1361
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001362Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001363 const sp<ICameraClient>& cameraClient,
1364 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001365 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001366 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001367 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001368 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001369 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001370
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001371 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001372 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001373 String8 id = String8::format("%d", cameraId);
1374 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001375 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1376 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1377 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1378 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001379
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001381 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001382 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001383 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001384 }
1385
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001386 *device = client;
1387 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001388}
1389
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001390Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001391 const sp<ICameraClient>& cameraClient,
1392 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001393 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001394 int clientUid,
1395 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001396 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001397
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001398 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001399 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001400 if (mModule != nullptr) {
1401 int apiVersion = mModule->getModuleApiVersion();
1402 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
1403 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
1404 /*
1405 * Either the HAL version is unspecified in which case this just creates
1406 * a camera client selected by the latest device version, or
1407 * it's a particular version in which case the HAL must supported
1408 * the open_legacy call
1409 */
1410 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1411 apiVersion);
1412 ALOGE("%s: %s",
1413 __FUNCTION__, msg.string());
1414 logRejected(id, getCallingPid(), String8(clientPackageName),
1415 msg);
1416 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
1417 }
Zhijun Heb10cdad2014-06-16 16:38:35 -07001418 }
1419
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001420 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001421 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001422 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1423 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1424 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1425 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001426
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001427 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001428 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001429 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001430 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001431 }
1432
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001433 *device = client;
1434 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001435}
1436
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001437Status CameraService::connectDevice(
1438 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001439 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001440 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001441 int clientUid,
1442 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001443 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001444
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001445 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001446 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001447 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001448 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001449 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1450 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1451 clientUid, USE_CALLING_PID, API_2,
1452 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1453 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001454
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001455 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001456 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001457 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001458 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001459 }
1460
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001461 *device = client;
1462 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001463}
1464
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001465template<class CALLBACK, class CLIENT>
1466Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
1467 int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
1468 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
1469 /*out*/sp<CLIENT>& device) {
1470 binder::Status ret = binder::Status::ok();
1471
1472 String8 clientName8(clientPackageName);
1473
1474 int originalClientPid = 0;
1475
1476 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1477 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1478 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1479 static_cast<int>(effectiveApiLevel));
1480
1481 sp<CLIENT> client = nullptr;
1482 {
1483 // Acquire mServiceLock and prevent other clients from connecting
1484 std::unique_ptr<AutoConditionLock> lock =
1485 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1486
1487 if (lock == nullptr) {
1488 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1489 , clientPid);
1490 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1491 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1492 cameraId.string(), clientName8.string(), clientPid);
1493 }
1494
1495 // Enforce client permissions and do basic sanity checks
1496 if(!(ret = validateConnectLocked(cameraId, clientName8,
1497 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1498 return ret;
1499 }
1500
1501 // Check the shim parameters after acquiring lock, if they have already been updated and
1502 // we were doing a shim update, return immediately
1503 if (shimUpdateOnly) {
1504 auto cameraState = getCameraState(cameraId);
1505 if (cameraState != nullptr) {
1506 if (!cameraState->getShimParams().isEmpty()) return ret;
1507 }
1508 }
1509
1510 status_t err;
1511
1512 sp<BasicClient> clientTmp = nullptr;
1513 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1514 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1515 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1516 /*out*/&partial)) != NO_ERROR) {
1517 switch (err) {
1518 case -ENODEV:
1519 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1520 "No camera device with ID \"%s\" currently available",
1521 cameraId.string());
1522 case -EBUSY:
1523 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1524 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1525 cameraId.string());
1526 default:
1527 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1528 "Unexpected error %s (%d) opening camera \"%s\"",
1529 strerror(-err), err, cameraId.string());
1530 }
1531 }
1532
1533 if (clientTmp.get() != nullptr) {
1534 // Handle special case for API1 MediaRecorder where the existing client is returned
1535 device = static_cast<CLIENT*>(clientTmp.get());
1536 return ret;
1537 }
1538
1539 // give flashlight a chance to close devices if necessary.
1540 mFlashlight->prepareDeviceOpen(cameraId);
1541
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001542 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001543 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -08001544 if (facing == -1) {
1545 ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string());
1546 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1547 "Unable to get camera device \"%s\" facing", cameraId.string());
1548 }
1549
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001550 sp<BasicClient> tmp = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001551 if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001552 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
1553 /*out*/&tmp)).isOk()) {
1554 return ret;
1555 }
1556 client = static_cast<CLIENT*>(tmp.get());
1557
1558 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1559 __FUNCTION__);
1560
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001561 if (mModule != nullptr) {
1562 err = client->initialize(mModule);
1563 } else {
1564 err = client->initialize(mCameraProviderManager);
1565 }
1566
1567 if (err != OK) {
1568 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001569 // Errors could be from the HAL module open call or from AppOpsManager
1570 switch(err) {
1571 case BAD_VALUE:
1572 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1573 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1574 case -EBUSY:
1575 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1576 "Camera \"%s\" is already open", cameraId.string());
1577 case -EUSERS:
1578 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1579 "Too many cameras already open, cannot open camera \"%s\"",
1580 cameraId.string());
1581 case PERMISSION_DENIED:
1582 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1583 "No permission to open camera \"%s\"", cameraId.string());
1584 case -EACCES:
1585 return STATUS_ERROR_FMT(ERROR_DISABLED,
1586 "Camera \"%s\" disabled by policy", cameraId.string());
1587 case -ENODEV:
1588 default:
1589 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1590 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1591 strerror(-err), err);
1592 }
1593 }
1594
1595 // Update shim paremeters for legacy clients
1596 if (effectiveApiLevel == API_1) {
1597 // Assume we have always received a Client subclass for API1
1598 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1599 String8 rawParams = shimClient->getParameters();
1600 CameraParameters params(rawParams);
1601
1602 auto cameraState = getCameraState(cameraId);
1603 if (cameraState != nullptr) {
1604 cameraState->setShimParams(params);
1605 } else {
1606 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1607 __FUNCTION__, cameraId.string());
1608 }
1609 }
1610
1611 if (shimUpdateOnly) {
1612 // If only updating legacy shim parameters, immediately disconnect client
1613 mServiceLock.unlock();
1614 client->disconnect();
1615 mServiceLock.lock();
1616 } else {
1617 // Otherwise, add client to active clients list
1618 finishConnectLocked(client, partial);
1619 }
1620 } // lock is destroyed, allow further connect calls
1621
1622 // Important: release the mutex here so the client can call back into the service from its
1623 // destructor (can be at the end of the call)
1624 device = client;
1625 return ret;
1626}
1627
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001628Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001629 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001630 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001631
1632 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001633 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001634 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001635 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1636 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001637 }
1638
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001639 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001640 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001641
1642 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001643 auto state = getCameraState(id);
1644 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001645 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001646 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1647 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001648 }
1649
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001650 StatusInternal cameraStatus = state->getStatus();
1651 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001652 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1653 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001654 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1655 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001656 }
1657
1658 {
1659 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001660 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001661 status_t err = getTorchStatusLocked(id, &status);
1662 if (err != OK) {
1663 if (err == NAME_NOT_FOUND) {
1664 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1665 "Camera \"%s\" does not have a flash unit", id.string());
1666 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001667 ALOGE("%s: getting current torch status failed for camera %s",
1668 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001669 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1670 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1671 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001672 }
1673
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001674 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001675 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001676 ALOGE("%s: torch mode of camera %s is not available because "
1677 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001678 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1679 "Torch for camera \"%s\" is not available due to an existing camera user",
1680 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001681 } else {
1682 ALOGE("%s: torch mode of camera %s is not available due to "
1683 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001684 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1685 "Torch for camera \"%s\" is not available due to insufficient resources",
1686 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001687 }
1688 }
1689 }
1690
Ruben Brunk99e69712015-05-26 17:25:07 -07001691 {
1692 // Update UID map - this is used in the torch status changed callbacks, so must be done
1693 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001694 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001695 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1696 mTorchUidMap[id].first = uid;
1697 mTorchUidMap[id].second = uid;
1698 } else {
1699 // Set the pending UID
1700 mTorchUidMap[id].first = uid;
1701 }
1702 }
1703
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001704 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001705
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001706 if (err != OK) {
1707 int32_t errorCode;
1708 String8 msg;
1709 switch (err) {
1710 case -ENOSYS:
1711 msg = String8::format("Camera \"%s\" has no flashlight",
1712 id.string());
1713 errorCode = ERROR_ILLEGAL_ARGUMENT;
1714 break;
1715 default:
1716 msg = String8::format(
1717 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1718 id.string(), enabled, strerror(-err), err);
1719 errorCode = ERROR_INVALID_OPERATION;
1720 }
1721 ALOGE("%s: %s", __FUNCTION__, msg.string());
1722 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001723 }
1724
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001725 {
1726 // update the link to client's death
1727 Mutex::Autolock al(mTorchClientMapMutex);
1728 ssize_t index = mTorchClientMap.indexOfKey(id);
1729 if (enabled) {
1730 if (index == NAME_NOT_FOUND) {
1731 mTorchClientMap.add(id, clientBinder);
1732 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001733 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001734 mTorchClientMap.replaceValueAt(index, clientBinder);
1735 }
1736 clientBinder->linkToDeath(this);
1737 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001738 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001739 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001740 }
1741
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001742 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001743}
1744
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001745Status CameraService::notifySystemEvent(int32_t eventId,
1746 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001747 ATRACE_CALL();
1748
Ruben Brunk36597b22015-03-20 22:15:57 -07001749 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001750 case ICameraService::EVENT_USER_SWITCHED: {
1751 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001752 break;
1753 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001754 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001755 default: {
1756 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1757 eventId);
1758 break;
1759 }
1760 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001761 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001762}
1763
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001764Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1765 /*out*/
1766 std::vector<hardware::CameraStatus> *cameraStatuses) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001767 ATRACE_CALL();
1768
Igor Murashkinbfc99152013-02-27 12:55:20 -08001769 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001770
Ruben Brunk3450ba72015-06-16 11:00:37 -07001771 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001772 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001773 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001774 }
1775
Igor Murashkinbfc99152013-02-27 12:55:20 -08001776 Mutex::Autolock lock(mServiceLock);
1777
Ruben Brunkcc776712015-02-17 20:18:47 -08001778 {
1779 Mutex::Autolock lock(mStatusListenerLock);
1780 for (auto& it : mListenerList) {
1781 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1782 ALOGW("%s: Tried to add listener %p which was already subscribed",
1783 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001784 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001785 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001786 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001787
1788 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001789 }
1790
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001791 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001792 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001793 Mutex::Autolock lock(mCameraStatesLock);
1794 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001795 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001796 }
1797 }
1798
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001799 /*
1800 * Immediately signal current torch status to this listener only
1801 * This may be a subset of all the devices, so don't include it in the response directly
1802 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001803 {
1804 Mutex::Autolock al(mTorchStatusMutex);
1805 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001806 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001807 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001808 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001809 }
1810
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001811 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001812}
Ruben Brunkcc776712015-02-17 20:18:47 -08001813
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001814Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001815 ATRACE_CALL();
1816
Igor Murashkinbfc99152013-02-27 12:55:20 -08001817 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1818
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001819 if (listener == 0) {
1820 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001821 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001822 }
1823
Igor Murashkinbfc99152013-02-27 12:55:20 -08001824 Mutex::Autolock lock(mServiceLock);
1825
Ruben Brunkcc776712015-02-17 20:18:47 -08001826 {
1827 Mutex::Autolock lock(mStatusListenerLock);
1828 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1829 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1830 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001831 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001832 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001833 }
1834 }
1835
1836 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1837 __FUNCTION__, listener.get());
1838
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001839 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001840}
1841
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001842Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001843
1844 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001845 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1846
1847 if (parameters == NULL) {
1848 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001849 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001850 }
1851
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001852 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001853
1854 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001855 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001856 // Error logged by caller
1857 return ret;
1858 }
1859
1860 String8 shimParamsString8 = shimParams.flatten();
1861 String16 shimParamsString16 = String16(shimParamsString8);
1862
1863 *parameters = shimParamsString16;
1864
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001865 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001866}
1867
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001868Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
1869 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001870 ATRACE_CALL();
1871
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001872 const String8 id = String8(cameraId);
1873
1874 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001875
1876 switch (apiVersion) {
1877 case API_VERSION_1:
1878 case API_VERSION_2:
1879 break;
1880 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001881 String8 msg = String8::format("Unknown API version %d", apiVersion);
1882 ALOGE("%s: %s", __FUNCTION__, msg.string());
1883 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001884 }
1885
1886 int facing = -1;
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -08001887 int deviceVersion = getDeviceVersion(id, /*out*/ &facing);
1888 if (facing == -1) {
1889 String8 msg = String8::format("Unable to get facing for camera device %s",
1890 id.string());
1891 ALOGE("%s: %s", __FUNCTION__, msg.string());
1892 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1893 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001894 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001895 case CAMERA_DEVICE_API_VERSION_1_0:
1896 case CAMERA_DEVICE_API_VERSION_3_0:
1897 case CAMERA_DEVICE_API_VERSION_3_1:
1898 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001899 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
1900 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001901 *isSupported = false;
1902 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001903 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
1904 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001905 *isSupported = true;
1906 }
1907 break;
1908 case CAMERA_DEVICE_API_VERSION_3_2:
1909 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001910 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001911 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
1912 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001913 *isSupported = true;
1914 break;
1915 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001916 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001917 ALOGE("%s: %s", __FUNCTION__, msg.string());
1918 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001919 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001920 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001921 String8 msg = String8::format("Unknown device version %x for device %s",
1922 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001923 ALOGE("%s: %s", __FUNCTION__, msg.string());
1924 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1925 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001926 }
1927
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001928 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001929}
1930
Ruben Brunkcc776712015-02-17 20:18:47 -08001931void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001932 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001933 for (auto& i : mActiveClientManager.getAll()) {
1934 auto clientSp = i->getValue();
1935 if (clientSp.get() == client) {
1936 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001937 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001938 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001939}
1940
Ruben Brunkcc776712015-02-17 20:18:47 -08001941bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1942 const int callingPid = getCallingPid();
1943 const int servicePid = getpid();
1944 bool ret = false;
1945 {
1946 // Acquire mServiceLock and prevent other clients from connecting
1947 std::unique_ptr<AutoConditionLock> lock =
1948 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001949
Igor Murashkin634a5152013-02-20 17:15:11 -08001950
Ruben Brunkcc776712015-02-17 20:18:47 -08001951 std::vector<sp<BasicClient>> evicted;
1952 for (auto& i : mActiveClientManager.getAll()) {
1953 auto clientSp = i->getValue();
1954 if (clientSp.get() == nullptr) {
1955 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1956 mActiveClientManager.remove(i);
1957 continue;
1958 }
1959 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1960 callingPid == clientSp->getClientPid())) {
1961 mActiveClientManager.remove(i);
1962 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001963
Ruben Brunkcc776712015-02-17 20:18:47 -08001964 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001965 clientSp->notifyError(
1966 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001967 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001968 }
1969 }
1970
Ruben Brunkcc776712015-02-17 20:18:47 -08001971 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1972 // other clients from connecting in mServiceLockWrapper if held
1973 mServiceLock.unlock();
1974
Ruben Brunk36597b22015-03-20 22:15:57 -07001975 // Do not clear caller identity, remote caller should be client proccess
1976
Ruben Brunkcc776712015-02-17 20:18:47 -08001977 for (auto& i : evicted) {
1978 if (i.get() != nullptr) {
1979 i->disconnect();
1980 ret = true;
1981 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001982 }
1983
Ruben Brunkcc776712015-02-17 20:18:47 -08001984 // Reacquire mServiceLock
1985 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001986
Ruben Brunkcc776712015-02-17 20:18:47 -08001987 } // lock is destroyed, allow further connect calls
1988
1989 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001990}
1991
Igor Murashkinecf17e82012-10-02 16:05:11 -07001992
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001993/**
1994 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001995 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001996 */
1997int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001998 if (mModule == nullptr) return NO_INIT;
1999
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08002000 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
2001 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
2002 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
2003 // Verify the device version is in the supported range
2004 switch (info.device_version) {
2005 case CAMERA_DEVICE_API_VERSION_1_0:
2006 case CAMERA_DEVICE_API_VERSION_3_0:
2007 case CAMERA_DEVICE_API_VERSION_3_1:
2008 case CAMERA_DEVICE_API_VERSION_3_2:
2009 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07002010 case CAMERA_DEVICE_API_VERSION_3_4:
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08002011 // in support
2012 break;
2013 case CAMERA_DEVICE_API_VERSION_2_0:
2014 case CAMERA_DEVICE_API_VERSION_2_1:
2015 // no longer supported
2016 default:
2017 ALOGE("%s: Device %d has HAL version %x, which is not supported",
2018 __FUNCTION__, id, info.device_version);
2019 String8 msg = String8::format(
2020 "Unsupported device HAL version %x for device %d",
2021 info.device_version, id);
2022 logServiceError(msg.string(), NO_INIT);
2023 return NO_INIT;
2024 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002025 }
2026
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002027 // Assume all devices pre-v3.3 are backward-compatible
2028 bool isBackwardCompatible = true;
2029 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
2030 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
2031 isBackwardCompatible = false;
2032 status_t res;
2033 camera_metadata_ro_entry_t caps;
2034 res = find_camera_metadata_ro_entry(
2035 info.static_camera_characteristics,
2036 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
2037 &caps);
2038 if (res != 0) {
2039 ALOGW("%s: Unable to find camera capabilities for camera device %d",
2040 __FUNCTION__, id);
2041 caps.count = 0;
2042 }
2043 for (size_t i = 0; i < caps.count; i++) {
2044 if (caps.data.u8[i] ==
2045 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
2046 isBackwardCompatible = true;
2047 break;
2048 }
2049 }
2050 }
2051
2052 if (!isBackwardCompatible) {
2053 mNumberOfNormalCameras--;
2054 *latestStrangeCameraId = id;
2055 } else {
2056 if (id > *latestStrangeCameraId) {
2057 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
2058 "This is not allowed due backward-compatibility requirements",
2059 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002060 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002061 mNumberOfCameras = 0;
2062 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002063 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07002064 }
2065 }
2066 return OK;
2067}
2068
Ruben Brunkcc776712015-02-17 20:18:47 -08002069std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
2070 const String8& cameraId) const {
2071 std::shared_ptr<CameraState> state;
2072 {
2073 Mutex::Autolock lock(mCameraStatesLock);
2074 auto iter = mCameraStates.find(cameraId);
2075 if (iter != mCameraStates.end()) {
2076 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002077 }
2078 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002079 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002080}
2081
Ruben Brunkcc776712015-02-17 20:18:47 -08002082sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
2083 // Remove from active clients list
2084 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
2085 if (clientDescriptorPtr == nullptr) {
2086 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
2087 cameraId.string());
2088 return sp<BasicClient>{nullptr};
2089 }
2090
2091 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07002092}
2093
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002094void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07002095 // Acquire mServiceLock and prevent other clients from connecting
2096 std::unique_ptr<AutoConditionLock> lock =
2097 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
2098
Ruben Brunk6267b532015-04-30 17:44:07 -07002099 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002100 for (size_t i = 0; i < newUserIds.size(); i++) {
2101 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07002102 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002103 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07002104 return;
2105 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002106 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07002107 }
2108
Ruben Brunka8ca9152015-04-07 14:23:40 -07002109
Ruben Brunk6267b532015-04-30 17:44:07 -07002110 if (newAllowedUsers == mAllowedUsers) {
2111 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
2112 return;
2113 }
2114
2115 logUserSwitch(mAllowedUsers, newAllowedUsers);
2116
2117 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07002118
2119 // Current user has switched, evict all current clients.
2120 std::vector<sp<BasicClient>> evicted;
2121 for (auto& i : mActiveClientManager.getAll()) {
2122 auto clientSp = i->getValue();
2123
2124 if (clientSp.get() == nullptr) {
2125 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
2126 continue;
2127 }
2128
Ruben Brunk6267b532015-04-30 17:44:07 -07002129 // Don't evict clients that are still allowed.
2130 uid_t clientUid = clientSp->getClientUid();
2131 userid_t clientUserId = multiuser_get_user_id(clientUid);
2132 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
2133 continue;
2134 }
2135
Ruben Brunk36597b22015-03-20 22:15:57 -07002136 evicted.push_back(clientSp);
2137
2138 String8 curTime = getFormattedCurrentTime();
2139
2140 ALOGE("Evicting conflicting client for camera ID %s due to user change",
2141 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07002142
Ruben Brunk36597b22015-03-20 22:15:57 -07002143 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002144 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07002145 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07002146 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
2147 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07002148
2149 }
2150
2151 // Do not hold mServiceLock while disconnecting clients, but retain the condition
2152 // blocking other clients from connecting in mServiceLockWrapper if held.
2153 mServiceLock.unlock();
2154
2155 // Clear caller identity temporarily so client disconnect PID checks work correctly
2156 int64_t token = IPCThreadState::self()->clearCallingIdentity();
2157
2158 for (auto& i : evicted) {
2159 i->disconnect();
2160 }
2161
2162 IPCThreadState::self()->restoreCallingIdentity(token);
2163
2164 // Reacquire mServiceLock
2165 mServiceLock.lock();
2166}
Ruben Brunkcc776712015-02-17 20:18:47 -08002167
Ruben Brunka8ca9152015-04-07 14:23:40 -07002168void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002169 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07002170 Mutex::Autolock l(mLogLock);
2171 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07002172}
2173
Ruben Brunka8ca9152015-04-07 14:23:40 -07002174void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002175 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002176 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002177 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002178 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002179}
2180
2181void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002182 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002183 // Log the clients evicted
2184 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002185 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002186}
2187
2188void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002189 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002190 // Log the client rejected
2191 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002192 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002193}
2194
Ruben Brunk6267b532015-04-30 17:44:07 -07002195void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
2196 const std::set<userid_t>& newUserIds) {
2197 String8 newUsers = toString(newUserIds);
2198 String8 oldUsers = toString(oldUserIds);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002199 if (oldUsers.size() == 0) {
2200 oldUsers = "<None>";
2201 }
Ruben Brunka8ca9152015-04-07 14:23:40 -07002202 // Log the new and old users
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002203 logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s",
Ruben Brunk6267b532015-04-30 17:44:07 -07002204 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002205}
2206
2207void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
2208 // Log the device removal
2209 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
2210}
2211
2212void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
2213 // Log the device removal
2214 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
2215}
2216
2217void CameraService::logClientDied(int clientPid, const char* reason) {
2218 // Log the device removal
2219 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07002220}
2221
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002222void CameraService::logServiceError(const char* msg, int errorCode) {
2223 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002224 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002225}
2226
Ruben Brunk36597b22015-03-20 22:15:57 -07002227status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
2228 uint32_t flags) {
2229
2230 const int pid = getCallingPid();
2231 const int selfPid = getpid();
2232
Mathias Agopian65ab4712010-07-14 17:59:35 -07002233 // Permission checks
2234 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002235 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07002236 if (pid != selfPid) {
2237 // Ensure we're being called by system_server, or similar process with
2238 // permissions to notify the camera service about system events
2239 if (!checkCallingPermission(
2240 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
2241 const int uid = getCallingUid();
2242 ALOGE("Permission Denial: cannot send updates to camera service about system"
2243 " events from pid=%d, uid=%d", pid, uid);
2244 return PERMISSION_DENIED;
2245 }
2246 }
2247 break;
2248 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002249 }
2250
2251 return BnCameraService::onTransact(code, data, reply, flags);
2252}
2253
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254// We share the media players for shutter and recording sound for all clients.
2255// A reference count is kept to determine when we will actually release the
2256// media players.
2257
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08002258MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002259 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08002260 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08002261 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002262 mp->prepare();
2263 } else {
Steve Block29357bc2012-01-06 19:20:56 +00002264 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265 return NULL;
2266 }
2267 return mp;
2268}
2269
2270void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002271 ATRACE_CALL();
2272
Mathias Agopian65ab4712010-07-14 17:59:35 -07002273 Mutex::Autolock lock(mSoundLock);
2274 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2275 if (mSoundRef++) return;
2276
2277 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002278 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2279 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002280}
2281
2282void CameraService::releaseSound() {
2283 Mutex::Autolock lock(mSoundLock);
2284 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2285 if (--mSoundRef) return;
2286
2287 for (int i = 0; i < NUM_SOUNDS; i++) {
2288 if (mSoundPlayer[i] != 0) {
2289 mSoundPlayer[i]->disconnect();
2290 mSoundPlayer[i].clear();
2291 }
2292 }
2293}
2294
2295void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002296 ATRACE_CALL();
2297
Mathias Agopian65ab4712010-07-14 17:59:35 -07002298 LOG1("playSound(%d)", kind);
2299 Mutex::Autolock lock(mSoundLock);
2300 sp<MediaPlayer> player = mSoundPlayer[kind];
2301 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002302 player->seekTo(0);
2303 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002304 }
2305}
2306
2307// ----------------------------------------------------------------------------
2308
2309CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002310 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002311 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002312 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002313 int clientPid, uid_t clientUid,
2314 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002315 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002316 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002317 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002318 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002319 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002320 servicePid),
2321 mCameraId(CameraService::cameraIdToInt(cameraIdStr))
Igor Murashkin634a5152013-02-20 17:15:11 -08002322{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002323 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002324 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002325
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002326 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002327
Mathias Agopian65ab4712010-07-14 17:59:35 -07002328 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002329
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002330 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002331}
2332
Mathias Agopian65ab4712010-07-14 17:59:35 -07002333// tear down the client
2334CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002335 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002336 mDestructionStarted = true;
2337
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002338 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002339 // unconditionally disconnect. function is idempotent
2340 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002341}
2342
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002343sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2344
Igor Murashkin634a5152013-02-20 17:15:11 -08002345CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002346 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002347 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002348 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002349 int clientPid, uid_t clientUid,
2350 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002351 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2352 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2353 mServicePid(servicePid),
2354 mDisconnected(false),
2355 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002356{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002357 if (sCameraService == nullptr) {
2358 sCameraService = cameraService;
2359 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002360 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002361 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002362
2363 // In some cases the calling code has no access to the package it runs under.
2364 // For example, NDK camera API.
2365 // In this case we will get the packages for the calling UID and pick the first one
2366 // for attributing the app op. This will work correctly for runtime permissions
2367 // as for legacy apps we will toggle the app op for all packages in the UID.
2368 // The caveat is that the operation may be attributed to the wrong package and
2369 // stats based on app ops may be slightly off.
2370 if (mClientPackageName.size() <= 0) {
2371 sp<IServiceManager> sm = defaultServiceManager();
2372 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2373 if (binder == 0) {
2374 ALOGE("Cannot get permission service");
2375 // Leave mClientPackageName unchanged (empty) and the further interaction
2376 // with camera will fail in BasicClient::startCameraOps
2377 return;
2378 }
2379
2380 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2381 Vector<String16> packages;
2382
2383 permCtrl->getPackagesForUid(mClientUid, packages);
2384
2385 if (packages.isEmpty()) {
2386 ALOGE("No packages for calling UID");
2387 // Leave mClientPackageName unchanged (empty) and the further interaction
2388 // with camera will fail in BasicClient::startCameraOps
2389 return;
2390 }
2391 mClientPackageName = packages[0];
2392 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002393}
2394
2395CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002396 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002397 mDestructionStarted = true;
2398}
2399
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002400binder::Status CameraService::BasicClient::disconnect() {
2401 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002402 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002403 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002404 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002405 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002406
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002407 sCameraService->removeByClient(this);
2408 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002409 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002410
2411 sp<IBinder> remote = getRemote();
2412 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002413 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002414 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002415
2416 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002417 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002418 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2419 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2420 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002421
Igor Murashkincba2c162013-03-20 15:56:31 -07002422 // client shouldn't be able to call into us anymore
2423 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002424
2425 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002426}
2427
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002428status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2429 // No dumping of clients directly over Binder,
2430 // must go through CameraService::dump
2431 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2432 IPCThreadState::self()->getCallingUid(), NULL, 0);
2433 return OK;
2434}
2435
Ruben Brunkcc776712015-02-17 20:18:47 -08002436String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002437 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002438}
2439
2440
2441int CameraService::BasicClient::getClientPid() const {
2442 return mClientPid;
2443}
2444
Ruben Brunk6267b532015-04-30 17:44:07 -07002445uid_t CameraService::BasicClient::getClientUid() const {
2446 return mClientUid;
2447}
2448
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002449bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2450 // Defaults to API2.
2451 return level == API_2;
2452}
2453
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002454status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002455 ATRACE_CALL();
2456
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002457 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002458 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002459 mOpsCallback = new OpsCallback(this);
2460
Igor Murashkine6800ce2013-03-04 17:25:57 -08002461 {
2462 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002463 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002464 }
2465
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002466 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002467 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002468 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002469 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002470
Svetoslav28e8ef72015-05-11 19:21:31 -07002471 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002472 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2473 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002474 return PERMISSION_DENIED;
2475 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002476
Svetoslav28e8ef72015-05-11 19:21:31 -07002477 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002478 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2479 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002480 // Return the same error as for device policy manager rejection
2481 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002482 }
2483
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002484 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002485
2486 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002487 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002488
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002489 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002490 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2491 mCameraIdStr);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002492
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002493 return OK;
2494}
2495
2496status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002497 ATRACE_CALL();
2498
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002499 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002500 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002501 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002502 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002503 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002504 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002505
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002506 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
2507 StatusInternal::ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002508
Ruben Brunkcc776712015-02-17 20:18:47 -08002509 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002510 sCameraService->updateStatus(StatusInternal::PRESENT,
2511 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002512
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002513 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002514 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2515 mCameraIdStr);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002516 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002517 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002518 if (mOpsCallback != NULL) {
2519 mAppOpsManager.stopWatchingMode(mOpsCallback);
2520 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002521 mOpsCallback.clear();
2522
2523 return OK;
2524}
2525
2526void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002527 ATRACE_CALL();
2528
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002529 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002530 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002531
2532 if (op != AppOpsManager::OP_CAMERA) {
2533 ALOGW("Unexpected app ops notification received: %d", op);
2534 return;
2535 }
2536
2537 int32_t res;
2538 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002539 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002540 ALOGV("checkOp returns: %d, %s ", res,
2541 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2542 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2543 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2544 "UNKNOWN");
2545
2546 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002547 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002548 myName.string());
2549 // Reset the client PID to allow server-initiated disconnect,
2550 // and to prevent further calls by client.
2551 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002552 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002553 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002554 disconnect();
2555 }
2556}
2557
Mathias Agopian65ab4712010-07-14 17:59:35 -07002558// ----------------------------------------------------------------------------
2559
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002560void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002561 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002562 (void) errorCode;
2563 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002564 if (mRemoteCallback != NULL) {
2565 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2566 } else {
2567 ALOGE("mRemoteCallback is NULL!!");
2568 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002569}
2570
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002571// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002572binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002573 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002574 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002575}
2576
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002577bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2578 return level == API_1;
2579}
2580
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002581CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2582 mClient(client) {
2583}
2584
2585void CameraService::Client::OpsCallback::opChanged(int32_t op,
2586 const String16& packageName) {
2587 sp<BasicClient> client = mClient.promote();
2588 if (client != NULL) {
2589 client->opChanged(op, packageName);
2590 }
2591}
2592
Mathias Agopian65ab4712010-07-14 17:59:35 -07002593// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002594// CameraState
2595// ----------------------------------------------------------------------------
2596
2597CameraService::CameraState::CameraState(const String8& id, int cost,
2598 const std::set<String8>& conflicting) : mId(id),
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002599 mStatus(StatusInternal::PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002600
2601CameraService::CameraState::~CameraState() {}
2602
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002603CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002604 Mutex::Autolock lock(mStatusLock);
2605 return mStatus;
2606}
2607
2608CameraParameters CameraService::CameraState::getShimParams() const {
2609 return mShimParams;
2610}
2611
2612void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2613 mShimParams = params;
2614}
2615
2616int CameraService::CameraState::getCost() const {
2617 return mCost;
2618}
2619
2620std::set<String8> CameraService::CameraState::getConflicting() const {
2621 return mConflicting;
2622}
2623
2624String8 CameraService::CameraState::getId() const {
2625 return mId;
2626}
2627
2628// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002629// ClientEventListener
2630// ----------------------------------------------------------------------------
2631
2632void CameraService::ClientEventListener::onClientAdded(
2633 const resource_policy::ClientDescriptor<String8,
2634 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002635 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002636 if (basicClient.get() != nullptr) {
2637 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2638 notifier.noteStartCamera(descriptor.getKey(),
2639 static_cast<int>(basicClient->getClientUid()));
2640 }
2641}
2642
2643void CameraService::ClientEventListener::onClientRemoved(
2644 const resource_policy::ClientDescriptor<String8,
2645 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002646 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002647 if (basicClient.get() != nullptr) {
2648 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2649 notifier.noteStopCamera(descriptor.getKey(),
2650 static_cast<int>(basicClient->getClientUid()));
2651 }
2652}
2653
2654
2655// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002656// CameraClientManager
2657// ----------------------------------------------------------------------------
2658
Ruben Brunk99e69712015-05-26 17:25:07 -07002659CameraService::CameraClientManager::CameraClientManager() {
2660 setListener(std::make_shared<ClientEventListener>());
2661}
2662
Ruben Brunkcc776712015-02-17 20:18:47 -08002663CameraService::CameraClientManager::~CameraClientManager() {}
2664
2665sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2666 const String8& id) const {
2667 auto descriptor = get(id);
2668 if (descriptor == nullptr) {
2669 return sp<BasicClient>{nullptr};
2670 }
2671 return descriptor->getValue();
2672}
2673
2674String8 CameraService::CameraClientManager::toString() const {
2675 auto all = getAll();
2676 String8 ret("[");
2677 bool hasAny = false;
2678 for (auto& i : all) {
2679 hasAny = true;
2680 String8 key = i->getKey();
2681 int32_t cost = i->getCost();
2682 int32_t pid = i->getOwnerId();
2683 int32_t priority = i->getPriority();
2684 auto conflicting = i->getConflicting();
2685 auto clientSp = i->getValue();
2686 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002687 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002688 if (clientSp.get() != nullptr) {
2689 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002690 uid_t clientUid = clientSp->getClientUid();
2691 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002692 }
2693 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2694 PRId32 ", ", key.string(), cost, pid, priority);
2695
Ruben Brunk6267b532015-04-30 17:44:07 -07002696 if (clientSp.get() != nullptr) {
2697 ret.appendFormat("User Id: %d, ", clientUserId);
2698 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002699 if (packageName.size() != 0) {
2700 ret.appendFormat("Client Package Name: %s", packageName.string());
2701 }
2702
2703 ret.append(", Conflicting Client Devices: {");
2704 for (auto& j : conflicting) {
2705 ret.appendFormat("%s, ", j.string());
2706 }
2707 ret.append("})");
2708 }
2709 if (hasAny) ret.append("\n");
2710 ret.append("]\n");
2711 return ret;
2712}
2713
2714CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2715 const String8& key, const sp<BasicClient>& value, int32_t cost,
2716 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2717
2718 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2719 key, value, cost, conflictingKeys, priority, ownerId);
2720}
2721
2722CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2723 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2724 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2725 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2726}
2727
2728// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002729
2730static const int kDumpLockRetries = 50;
2731static const int kDumpLockSleep = 60000;
2732
2733static bool tryLock(Mutex& mutex)
2734{
2735 bool locked = false;
2736 for (int i = 0; i < kDumpLockRetries; ++i) {
2737 if (mutex.tryLock() == NO_ERROR) {
2738 locked = true;
2739 break;
2740 }
2741 usleep(kDumpLockSleep);
2742 }
2743 return locked;
2744}
2745
2746status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002747 ATRACE_CALL();
2748
Mathias Agopian65ab4712010-07-14 17:59:35 -07002749 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002750 dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07002751 getCallingPid(),
2752 getCallingUid());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002753 return NO_ERROR;
2754 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002755 bool locked = tryLock(mServiceLock);
2756 // failed to lock - CameraService is probably deadlocked
2757 if (!locked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002758 dprintf(fd, "!! CameraService may be deadlocked !!\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002759 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002760
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002761 if (!mInitialized) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002762 dprintf(fd, "!! No camera HAL available !!\n");
Ruben Brunkf81648e2014-04-17 16:14:57 -07002763
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002764 // Dump event log for error information
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002765 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002766
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002767 if (locked) mServiceLock.unlock();
2768 return NO_ERROR;
2769 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002770 dprintf(fd, "\n== Service global info: ==\n\n");
2771 dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras);
2772 dprintf(fd, "Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002773 String8 activeClientString = mActiveClientManager.toString();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002774 dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string());
2775 dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002776
2777 dumpEventLog(fd);
2778
2779 bool stateLocked = tryLock(mCameraStatesLock);
2780 if (!stateLocked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002781 dprintf(fd, "CameraStates in use, may be deadlocked\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002782 }
2783
2784 for (auto& state : mCameraStates) {
2785 String8 cameraId = state.first;
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002786
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002787 dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002788
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002789 CameraParameters p = state.second->getShimParams();
2790 if (!p.isEmpty()) {
2791 dprintf(fd, " Camera1 API shim is using parameters:\n ");
2792 p.dump(fd, args);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002793 }
2794
2795 auto clientDescriptor = mActiveClientManager.get(cameraId);
2796 if (clientDescriptor == nullptr) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002797 dprintf(fd, " Device %s is closed, no client instance\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002798 cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002799 continue;
2800 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002801 dprintf(fd, " Device %s is open. Client instance dump:\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002802 cameraId.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002803 dprintf(fd, " Client priority level: %d\n", clientDescriptor->getPriority());
2804 dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002805
2806 auto client = clientDescriptor->getValue();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002807 dprintf(fd, " Client package: %s\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002808 String8(client->getPackageName()).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002809
2810 client->dumpClient(fd, args);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002811
2812 if (mModule != nullptr) {
2813 dprintf(fd, "== Camera HAL device %s static information: ==\n", cameraId.string());
2814
2815 camera_info info;
2816 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
2817 int deviceVersion = -1;
2818 if (rc != OK) {
2819 dprintf(fd, " Error reading static information!\n");
2820 } else {
2821 dprintf(fd, " Facing: %s\n",
2822 info.facing == CAMERA_FACING_BACK ? "BACK" :
2823 info.facing == CAMERA_FACING_FRONT ? "FRONT" : "EXTERNAL");
2824 dprintf(fd, " Orientation: %d\n", info.orientation);
2825
2826 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
2827 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2828 } else {
2829 deviceVersion = info.device_version;
2830 }
2831 }
2832
2833 auto conflicting = state.second->getConflicting();
2834 dprintf(fd, " Resource Cost: %d\n", state.second->getCost());
2835 dprintf(fd, " Conflicting Devices:");
2836 for (auto& id : conflicting) {
2837 dprintf(fd, " %s", id.string());
2838 }
2839 if (conflicting.size() == 0) {
2840 dprintf(fd, " NONE");
2841 }
2842 dprintf(fd, "\n");
2843
2844 dprintf(fd, " Device version: %#x\n", deviceVersion);
2845 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
2846 dprintf(fd, " Device static metadata:\n");
2847 dump_indented_camera_metadata(info.static_camera_characteristics,
2848 fd, /*verbosity*/2, /*indentation*/4);
2849 }
2850 }
2851
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002852 }
2853
2854 if (stateLocked) mCameraStatesLock.unlock();
2855
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002856 if (locked) mServiceLock.unlock();
2857
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002858 if (mModule == nullptr) {
2859 mCameraProviderManager->dump(fd, args);
2860 } else {
2861 dprintf(fd, "\n== Camera Module HAL static info: ==\n");
2862 dprintf(fd, "Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2863 dprintf(fd, "Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2864 dprintf(fd, "Camera module name: %s\n", mModule->getModuleName());
2865 dprintf(fd, "Camera module author: %s\n", mModule->getModuleAuthor());
2866 }
2867
2868 dprintf(fd, "\n== Vendor tags: ==\n\n");
2869
2870 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2871 if (desc == NULL) {
2872 dprintf(fd, "No vendor tags.\n");
2873 } else {
2874 desc->dump(fd, /*verbosity*/2, /*indentation*/2);
2875 }
2876
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002877 // Dump camera traces if there were any
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002878 dprintf(fd, "\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002879 camera3::CameraTraces::dump(fd, args);
2880
2881 // Process dump arguments, if any
2882 int n = args.size();
2883 String16 verboseOption("-v");
2884 String16 unreachableOption("--unreachable");
2885 for (int i = 0; i < n; i++) {
2886 if (args[i] == verboseOption) {
2887 // change logging level
2888 if (i + 1 >= n) continue;
2889 String8 levelStr(args[i+1]);
2890 int level = atoi(levelStr.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002891 dprintf(fd, "\nSetting log level to %d.\n", level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002892 setLogLevel(level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002893 } else if (args[i] == unreachableOption) {
2894 // Dump memory analysis
2895 // TODO - should limit be an argument parameter?
2896 UnreachableMemoryInfo info;
2897 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2898 if (!success) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002899 dprintf(fd, "\n== Unable to dump unreachable memory. "
2900 "Try disabling SELinux enforcement. ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002901 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002902 dprintf(fd, "\n== Dumping unreachable memory: ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002903 std::string s = info.ToString(/*log_contents*/ true);
2904 write(fd, s.c_str(), s.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002905 }
2906 }
2907 }
2908 return NO_ERROR;
2909}
2910
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002911void CameraService::dumpEventLog(int fd) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002912 dprintf(fd, "\n== Camera service events log (most recent at top): ==\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002913
2914 Mutex::Autolock l(mLogLock);
2915 for (const auto& msg : mEventLog) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002916 dprintf(fd, " %s\n", msg.string());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002917 }
2918
2919 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002920 dprintf(fd, " ...\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002921 } else if (mEventLog.size() == 0) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002922 dprintf(fd, " [no events yet]\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002923 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002924 dprintf(fd, "\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002925}
2926
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002927void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002928 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002929 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2930 if (mTorchClientMap[i] == who) {
2931 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002932 String8 cameraId = mTorchClientMap.keyAt(i);
2933 status_t res = mFlashlight->setTorchMode(cameraId, false);
2934 if (res) {
2935 ALOGE("%s: torch client died but couldn't turn off torch: "
2936 "%s (%d)", __FUNCTION__, strerror(-res), res);
2937 return;
2938 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002939 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002940 break;
2941 }
2942 }
2943}
2944
Ruben Brunkcc776712015-02-17 20:18:47 -08002945/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002946
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002947 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002948 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2949 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002950 */
2951
Ruben Brunka8ca9152015-04-07 14:23:40 -07002952 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2953
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002954 // check torch client
2955 handleTorchClientBinderDied(who);
2956
2957 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002958 if(!evictClientIdByRemote(who)) {
2959 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002960 return;
2961 }
2962
Ruben Brunkcc776712015-02-17 20:18:47 -08002963 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2964 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002965}
2966
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002967void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002968 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002969}
2970
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002971void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2972 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002973 // Do not lock mServiceLock here or can get into a deadlock from
2974 // connect() -> disconnect -> updateStatus
2975
2976 auto state = getCameraState(cameraId);
2977
2978 if (state == nullptr) {
2979 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2980 cameraId.string());
2981 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002982 }
2983
Ruben Brunkcc776712015-02-17 20:18:47 -08002984 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2985 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2986 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002987 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002988
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002989 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002990 // Update torch status if it has a flash unit.
2991 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002992 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002993 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2994 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002995 TorchModeStatus newTorchStatus =
2996 status == StatusInternal::PRESENT ?
2997 TorchModeStatus::AVAILABLE_OFF :
2998 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002999 if (torchStatus != newTorchStatus) {
3000 onTorchStatusChangedLocked(cameraId, newTorchStatus);
3001 }
3002 }
Ruben Brunkcc776712015-02-17 20:18:47 -08003003 }
3004
3005 Mutex::Autolock lock(mStatusListenerLock);
3006
3007 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003008 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08003009 }
3010 });
Igor Murashkincba2c162013-03-20 15:56:31 -07003011}
3012
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003013template<class Func>
3014void CameraService::CameraState::updateStatus(StatusInternal status,
3015 const String8& cameraId,
3016 std::initializer_list<StatusInternal> rejectSourceStates,
3017 Func onStatusUpdatedLocked) {
3018 Mutex::Autolock lock(mStatusLock);
3019 StatusInternal oldStatus = mStatus;
3020 mStatus = status;
3021
3022 if (oldStatus == status) {
3023 return;
3024 }
3025
3026 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
3027 cameraId.string(), oldStatus, status);
3028
3029 if (oldStatus == StatusInternal::NOT_PRESENT &&
3030 (status != StatusInternal::PRESENT &&
3031 status != StatusInternal::ENUMERATING)) {
3032
3033 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
3034 __FUNCTION__);
3035 mStatus = oldStatus;
3036 return;
3037 }
3038
3039 /**
3040 * Sometimes we want to conditionally do a transition.
3041 * For example if a client disconnects, we want to go to PRESENT
3042 * only if we weren't already in NOT_PRESENT or ENUMERATING.
3043 */
3044 for (auto& rejectStatus : rejectSourceStates) {
3045 if (oldStatus == rejectStatus) {
3046 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
3047 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
3048 mStatus = oldStatus;
3049 return;
3050 }
3051 }
3052
3053 onStatusUpdatedLocked(cameraId, status);
3054}
3055
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07003056void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
3057 const String8& cameraId) {
3058 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
3059 if (proxyBinder == nullptr) return;
3060 String16 id(cameraId);
3061 proxyBinder->notifyCameraState(id, newState);
3062}
3063
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003064status_t CameraService::getTorchStatusLocked(
3065 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003066 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003067 if (!status) {
3068 return BAD_VALUE;
3069 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003070 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3071 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003072 // invalid camera ID or the camera doesn't have a flash unit
3073 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003074 }
3075
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003076 *status = mTorchStatusMap.valueAt(index);
3077 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003078}
3079
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003080status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003081 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003082 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3083 if (index == NAME_NOT_FOUND) {
3084 return BAD_VALUE;
3085 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003086 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003087
3088 return OK;
3089}
3090
Mathias Agopian65ab4712010-07-14 17:59:35 -07003091}; // namespace android