blob: 6abfa81e7301e43fa5786c00351dc6ed4ee64808 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
Ruben Brunkd1176ef2014-02-21 10:51:38 -08002 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mathias Agopian65ab4712010-07-14 17:59:35 -070016
17#define LOG_TAG "CameraService"
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -070018#define ATRACE_TAG ATRACE_TAG_CAMERA
Iliyan Malchev8951a972011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
Ruben Brunkcc776712015-02-17 20:18:47 -080021#include <algorithm>
22#include <climits>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <stdio.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080024#include <cstring>
25#include <ctime>
26#include <string>
Mathias Agopian65ab4712010-07-14 17:59:35 -070027#include <sys/types.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080028#include <inttypes.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <pthread.h>
30
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080031#include <android/hardware/ICamera.h>
32#include <android/hardware/ICameraClient.h>
33
Alex Deymo9c2a2c22016-08-25 11:59:14 -070034#include <android-base/macros.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080035#include <android-base/parseint.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080036#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryBase.h>
40#include <binder/MemoryHeapBase.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080041#include <binder/ProcessInfoService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070043#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080044#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include <hardware/hardware.h>
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -070046#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070047#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080048#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070050#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070051#include <utils/Errors.h>
52#include <utils/Log.h>
53#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080054#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080055#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080056#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070057#include <system/camera_metadata.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080058
Ruben Brunkb2119af2014-05-09 19:57:56 -070059#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070060
61#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070062#include "api1/CameraClient.h"
63#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070064#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070065#include "utils/CameraTraces.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070066
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080067namespace {
68 const char* kPermissionServiceName = "permission";
69}; // namespace anonymous
70
Mathias Agopian65ab4712010-07-14 17:59:35 -070071namespace android {
72
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080073using binder::Status;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080074using hardware::ICamera;
75using hardware::ICameraClient;
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -070076using hardware::ICameraServiceProxy;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080077using hardware::ICameraServiceListener;
78using hardware::camera::common::V1_0::CameraDeviceStatus;
79using hardware::camera::common::V1_0::TorchModeStatus;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080080
Mathias Agopian65ab4712010-07-14 17:59:35 -070081// ----------------------------------------------------------------------------
82// Logging support -- this is for debugging only
83// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070084volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
Steve Blockb8a80522011-12-20 16:23:08 +000086#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
87#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070088
89static void setLogLevel(int level) {
90 android_atomic_write(level, &gLogLevel);
91}
92
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080093// Convenience methods for constructing binder::Status objects for error returns
94
95#define STATUS_ERROR(errorCode, errorString) \
96 binder::Status::fromServiceSpecificError(errorCode, \
97 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
98
99#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
100 binder::Status::fromServiceSpecificError(errorCode, \
101 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
102 __VA_ARGS__))
103
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104// ----------------------------------------------------------------------------
105
Igor Murashkincba2c162013-03-20 15:56:31 -0700106extern "C" {
107static void camera_device_status_change(
108 const struct camera_module_callbacks* callbacks,
109 int camera_id,
110 int new_status) {
111 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -0800112 static_cast<const CameraService*>(callbacks));
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800113 String8 id = String8::format("%d", camera_id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700114
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800115 CameraDeviceStatus newStatus{CameraDeviceStatus::NOT_PRESENT};
116 switch (new_status) {
117 case CAMERA_DEVICE_STATUS_NOT_PRESENT:
118 newStatus = CameraDeviceStatus::NOT_PRESENT;
119 break;
120 case CAMERA_DEVICE_STATUS_PRESENT:
121 newStatus = CameraDeviceStatus::PRESENT;
122 break;
123 case CAMERA_DEVICE_STATUS_ENUMERATING:
124 newStatus = CameraDeviceStatus::ENUMERATING;
125 break;
126 default:
127 ALOGW("Unknown device status change to %d", new_status);
128 break;
129 }
130 cs->onDeviceStatusChanged(id, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700131}
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800132
133static void torch_mode_status_change(
134 const struct camera_module_callbacks* callbacks,
135 const char* camera_id,
136 int new_status) {
137 if (!callbacks || !camera_id) {
138 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
139 callbacks, camera_id);
140 }
141 sp<CameraService> cs = const_cast<CameraService*>(
142 static_cast<const CameraService*>(callbacks));
143
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800144 TorchModeStatus status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800145 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800146 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800147 status = TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800148 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800149 case TORCH_MODE_STATUS_AVAILABLE_OFF:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800150 status = TorchModeStatus::AVAILABLE_OFF;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800151 break;
152 case TORCH_MODE_STATUS_AVAILABLE_ON:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800153 status = TorchModeStatus::AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800154 break;
155 default:
156 ALOGE("Unknown torch status %d", new_status);
157 return;
158 }
159
160 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800161 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800162 status);
163}
Igor Murashkincba2c162013-03-20 15:56:31 -0700164} // extern "C"
165
Mathias Agopian65ab4712010-07-14 17:59:35 -0700166// ----------------------------------------------------------------------------
167
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800168CameraService::CameraService() :
169 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800170 mNumberOfCameras(0), mNumberOfNormalCameras(0),
Emilian Peevf53f66e2017-04-11 14:29:43 +0100171 mSoundRef(0), mInitialized(false) {
Steve Blockdf64d152012-01-04 20:05:49 +0000172 ALOGI("CameraService started (pid=%d)", getpid());
Igor Murashkinbfc99152013-02-27 12:55:20 -0800173
Igor Murashkincba2c162013-03-20 15:56:31 -0700174 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800175 this->torch_mode_status_change = android::torch_mode_status_change;
176
Ruben Brunkcc776712015-02-17 20:18:47 -0800177 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700178}
179
Iliyan Malchev8951a972011-04-14 16:55:59 -0700180void CameraService::onFirstRef()
181{
Ruben Brunkcc776712015-02-17 20:18:47 -0800182 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800183
Iliyan Malchev8951a972011-04-14 16:55:59 -0700184 BnCameraService::onFirstRef();
185
Ruben Brunk99e69712015-05-26 17:25:07 -0700186 // Update battery life tracking if service is restarting
187 BatteryNotifier& notifier(BatteryNotifier::getInstance());
188 notifier.noteResetCamera();
189 notifier.noteResetFlashlight();
190
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800191 status_t res = INVALID_OPERATION;
Eino-Ville Talvala9cbbc832017-01-23 15:39:53 -0800192
Emilian Peevf53f66e2017-04-11 14:29:43 +0100193 res = enumerateProviders();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800194 if (res == OK) {
195 mInitialized = true;
196 }
197
198 CameraService::pingCameraServiceProxy();
199}
200
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800201status_t CameraService::enumerateProviders() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800202 status_t res;
Emilian Peevaee727d2017-05-04 16:35:48 +0100203 Mutex::Autolock l(mServiceLock);
204
205 if (nullptr == mCameraProviderManager.get()) {
206 mCameraProviderManager = new CameraProviderManager();
207 res = mCameraProviderManager->initialize(this);
208 if (res != OK) {
209 ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
210 __FUNCTION__, strerror(-res), res);
211 return res;
212 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800213 }
214
215 mNumberOfCameras = mCameraProviderManager->getCameraCount();
Emilian Peevf53f66e2017-04-11 14:29:43 +0100216 mNumberOfNormalCameras =
217 mCameraProviderManager->getAPI1CompatibleCameraCount();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800218
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800219 // Setup vendor tags before we call get_camera_info the first time
220 // because HAL might need to setup static vendor keys in get_camera_info
221 // TODO: maybe put this into CameraProviderManager::initialize()?
222 mCameraProviderManager->setUpVendorTags();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800223
Emilian Peevaee727d2017-05-04 16:35:48 +0100224 if (nullptr == mFlashlight.get()) {
225 mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
226 }
227
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800228 res = mFlashlight->findFlashUnits();
229 if (res != OK) {
230 ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
231 }
232
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800233 for (auto& cameraId : mCameraProviderManager->getCameraDeviceIds()) {
Emilian Peevaee727d2017-05-04 16:35:48 +0100234 String8 id8 = String8(cameraId.c_str());
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700235 bool cameraFound = false;
Emilian Peevaee727d2017-05-04 16:35:48 +0100236 {
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700237
Emilian Peevaee727d2017-05-04 16:35:48 +0100238 Mutex::Autolock lock(mCameraStatesLock);
239 auto iter = mCameraStates.find(id8);
240 if (iter != mCameraStates.end()) {
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700241 cameraFound = true;
Emilian Peevaee727d2017-05-04 16:35:48 +0100242 }
243 }
244
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700245 if (!cameraFound) {
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100246 addStates(id8);
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700247 }
248
249 onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800250 }
251
252 return OK;
253}
254
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700255sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800256 sp<ICameraServiceProxy> proxyBinder = nullptr;
257#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700258 sp<IServiceManager> sm = defaultServiceManager();
Eino-Ville Talvalaf4db13d2016-12-08 11:20:51 -0800259 // Use checkService because cameraserver normally starts before the
260 // system server and the proxy service. So the long timeout that getService
261 // has before giving up is inappropriate.
262 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800263 if (binder != nullptr) {
264 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700265 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800266#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700267 return proxyBinder;
268}
269
270void CameraService::pingCameraServiceProxy() {
271 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
272 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700273 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700274}
275
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276CameraService::~CameraService() {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800277 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700278}
279
Emilian Peevaee727d2017-05-04 16:35:48 +0100280void CameraService::onNewProviderRegistered() {
281 enumerateProviders();
282}
283
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100284void CameraService::addStates(const String8 id) {
285 std::string cameraId(id.c_str());
286 hardware::camera::common::V1_0::CameraResourceCost cost;
287 status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost);
288 if (res != OK) {
289 ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res);
290 return;
291 }
292 std::set<String8> conflicting;
293 for (size_t i = 0; i < cost.conflictingDevices.size(); i++) {
294 conflicting.emplace(String8(cost.conflictingDevices[i].c_str()));
295 }
296
297 {
298 Mutex::Autolock lock(mCameraStatesLock);
299 mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost,
300 conflicting));
301 }
302
303 if (mFlashlight->hasFlashUnit(id)) {
304 mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF);
305 }
306 logDeviceAdded(id, "Device added");
307}
308
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800309void CameraService::onDeviceStatusChanged(const String8& id,
310 CameraDeviceStatus newHalStatus) {
311 ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__,
312 id.string(), newHalStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700313
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800314 StatusInternal newStatus = mapToInternal(newHalStatus);
315
Ruben Brunkcc776712015-02-17 20:18:47 -0800316 std::shared_ptr<CameraState> state = getCameraState(id);
317
318 if (state == nullptr) {
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700319 if (newStatus == StatusInternal::PRESENT) {
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100320 ALOGI("%s: Unknown camera ID %s, a new camera is added",
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700321 __FUNCTION__, id.string());
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100322
323 // First add as absent to make sure clients are notified below
324 addStates(id);
325
326 updateStatus(newStatus, id);
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700327 } else {
328 ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string());
329 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700330 return;
331 }
332
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800333 StatusInternal oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800334
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800335 if (oldStatus == newStatus) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800336 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700337 return;
338 }
339
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800340 if (newStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700341 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
342 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800343 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700344 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800345 // Don't do this in updateStatus to avoid deadlock over mServiceLock
346 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700347
Ruben Brunkcc776712015-02-17 20:18:47 -0800348 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
349 // to this device until the status changes
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800350 updateStatus(StatusInternal::NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700351
Ruben Brunkcc776712015-02-17 20:18:47 -0800352 // Remove cached shim parameters
353 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700354
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700355 // Remove the client from the list of active clients, if there is one
Ruben Brunkcc776712015-02-17 20:18:47 -0800356 clientToDisconnect = removeClientLocked(id);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700357 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800358
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700359 // Disconnect client
360 if (clientToDisconnect.get() != nullptr) {
361 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
362 __FUNCTION__, id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800363 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800364 clientToDisconnect->notifyError(
365 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800366 CaptureResultExtras{});
Ruben Brunkcc776712015-02-17 20:18:47 -0800367 // Ensure not in binder RPC so client disconnect PID checks work correctly
368 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
369 "onDeviceStatusChanged must be called from the camera service process!");
370 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700371 }
372
Ruben Brunkcc776712015-02-17 20:18:47 -0800373 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800374 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700375 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
376 newStatus));
377 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800378 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700379 }
380
Igor Murashkincba2c162013-03-20 15:56:31 -0700381}
382
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800383void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800384 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800385 Mutex::Autolock al(mTorchStatusMutex);
386 onTorchStatusChangedLocked(cameraId, newStatus);
387}
388
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800389void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800390 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800391 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
392 __FUNCTION__, cameraId.string(), newStatus);
393
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800394 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800395 status_t res = getTorchStatusLocked(cameraId, &status);
396 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700397 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
398 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800399 return;
400 }
401 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800402 return;
403 }
404
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800405 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800406 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800407 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
408 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800409 return;
410 }
411
Ruben Brunkcc776712015-02-17 20:18:47 -0800412 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700413 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700414 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700415 auto iter = mTorchUidMap.find(cameraId);
416 if (iter != mTorchUidMap.end()) {
417 int oldUid = iter->second.second;
418 int newUid = iter->second.first;
419 BatteryNotifier& notifier(BatteryNotifier::getInstance());
420 if (oldUid != newUid) {
421 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800422 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700423 notifier.noteFlashlightOff(cameraId, oldUid);
424 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800425 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700426 notifier.noteFlashlightOn(cameraId, newUid);
427 }
428 iter->second.second = newUid;
429 } else {
430 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800431 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700432 notifier.noteFlashlightOn(cameraId, oldUid);
433 } else {
434 notifier.noteFlashlightOff(cameraId, oldUid);
435 }
436 }
437 }
438 }
439
440 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800441 Mutex::Autolock lock(mStatusListenerLock);
442 for (auto& i : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800443 i->onTorchStatusChanged(mapToInterface(newStatus), String16{cameraId});
Ruben Brunkcc776712015-02-17 20:18:47 -0800444 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800445 }
446}
447
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800448Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700449 ATRACE_CALL();
Emilian Peevaee727d2017-05-04 16:35:48 +0100450 Mutex::Autolock l(mServiceLock);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700451 switch (type) {
452 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800453 *numCameras = mNumberOfNormalCameras;
454 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700455 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800456 *numCameras = mNumberOfCameras;
457 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700458 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800459 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700460 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800461 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
462 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700463 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800464 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465}
466
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800467Status CameraService::getCameraInfo(int cameraId,
468 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700469 ATRACE_CALL();
Emilian Peevaee727d2017-05-04 16:35:48 +0100470 Mutex::Autolock l(mServiceLock);
471
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800472 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800473 return STATUS_ERROR(ERROR_DISCONNECTED,
474 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700475 }
476
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800478 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
479 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480 }
481
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800482 Status ret = Status::ok();
Emilian Peevf53f66e2017-04-11 14:29:43 +0100483 status_t err = mCameraProviderManager->getCameraInfo(std::to_string(cameraId), cameraInfo);
484 if (err != OK) {
485 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
486 "Error retrieving camera info from device %d: %s (%d)", cameraId,
487 strerror(-err), err);
Ruben Brunkcc776712015-02-17 20:18:47 -0800488 }
Emilian Peevf53f66e2017-04-11 14:29:43 +0100489
Ruben Brunkcc776712015-02-17 20:18:47 -0800490 return ret;
491}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700492
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800493int CameraService::cameraIdToInt(const String8& cameraId) {
494 int id;
495 bool success = base::ParseInt(cameraId.string(), &id, 0);
496 if (!success) {
497 return -1;
498 }
499 return id;
500}
501
502Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800503 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700504 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700505 if (!cameraInfo) {
506 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800507 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700508 }
509
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800510 if (!mInitialized) {
511 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800512 return STATUS_ERROR(ERROR_DISCONNECTED,
513 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700514 }
515
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800516 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800517
Emilian Peevf53f66e2017-04-11 14:29:43 +0100518 status_t res = mCameraProviderManager->getCameraCharacteristics(
519 String8(cameraId).string(), cameraInfo);
520 if (res != OK) {
521 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
522 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
523 strerror(-res), res);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700524 }
Zhijun He2b59be82013-09-25 10:14:30 -0700525
526 return ret;
527}
528
Ruben Brunkcc776712015-02-17 20:18:47 -0800529int CameraService::getCallingPid() {
530 return IPCThreadState::self()->getCallingPid();
531}
532
533int CameraService::getCallingUid() {
534 return IPCThreadState::self()->getCallingUid();
535}
536
537String8 CameraService::getFormattedCurrentTime() {
538 time_t now = time(nullptr);
539 char formattedTime[64];
540 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
541 return String8(formattedTime);
542}
543
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800544Status CameraService::getCameraVendorTagDescriptor(
545 /*out*/
546 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700547 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800548 if (!mInitialized) {
549 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800550 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800551 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800552 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
553 if (globalDescriptor != nullptr) {
554 *desc = *(globalDescriptor.get());
555 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800556 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800557}
558
Emilian Peev71c73a22017-03-21 16:35:51 +0000559Status CameraService::getCameraVendorTagCache(
560 /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) {
561 ATRACE_CALL();
562 if (!mInitialized) {
563 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
564 return STATUS_ERROR(ERROR_DISCONNECTED,
565 "Camera subsystem not available");
566 }
567 sp<VendorTagDescriptorCache> globalCache =
568 VendorTagDescriptorCache::getGlobalVendorTagCache();
569 if (globalCache != nullptr) {
570 *cache = *(globalCache.get());
571 }
572 return Status::ok();
573}
574
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800575int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700576 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800577
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800578 int deviceVersion = 0;
579
Emilian Peevf53f66e2017-04-11 14:29:43 +0100580 status_t res;
581 hardware::hidl_version maxVersion{0,0};
582 res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(),
583 &maxVersion);
584 if (res != OK) return -1;
585 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800586
Emilian Peevf53f66e2017-04-11 14:29:43 +0100587 hardware::CameraInfo info;
588 if (facing) {
589 res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800590 if (res != OK) return -1;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100591 *facing = info.facing;
Igor Murashkin634a5152013-02-20 17:15:11 -0800592 }
Emilian Peevf53f66e2017-04-11 14:29:43 +0100593
Igor Murashkin634a5152013-02-20 17:15:11 -0800594 return deviceVersion;
595}
596
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700598 switch(err) {
599 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800601 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800602 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
603 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800604 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800605 return STATUS_ERROR(ERROR_DISCONNECTED,
606 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700607 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800608 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
609 "Camera HAL encountered error %d: %s",
610 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700611 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800612}
613
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800614Status CameraService::makeClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800615 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800616 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
617 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
618 /*out*/sp<BasicClient>* client) {
619
Ruben Brunkcc776712015-02-17 20:18:47 -0800620 if (halVersion < 0 || halVersion == deviceVersion) {
621 // Default path: HAL version is unspecified by caller, create CameraClient
622 // based on device version reported by the HAL.
623 switch(deviceVersion) {
624 case CAMERA_DEVICE_API_VERSION_1_0:
625 if (effectiveApiLevel == API_1) { // Camera1 API route
626 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800627 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
628 facing, clientPid, clientUid, getpid(), legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800629 } else { // Camera2 API route
630 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800631 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800632 "Camera device \"%s\" HAL version %d does not support camera2 API",
633 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800634 }
635 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800636 case CAMERA_DEVICE_API_VERSION_3_0:
637 case CAMERA_DEVICE_API_VERSION_3_1:
638 case CAMERA_DEVICE_API_VERSION_3_2:
639 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700640 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800641 if (effectiveApiLevel == API_1) { // Camera1 API route
642 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800643 *client = new Camera2Client(cameraService, tmp, packageName, cameraIdToInt(cameraId),
644 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800645 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800646 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
647 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
648 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800649 facing, clientPid, clientUid, servicePid);
650 }
651 break;
652 default:
653 // Should not be reachable
654 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800655 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800656 "Camera device \"%s\" has unknown HAL version %d",
657 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800658 }
659 } else {
660 // A particular HAL version is requested by caller. Create CameraClient
661 // based on the requested HAL version.
662 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
663 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
664 // Only support higher HAL version device opened as HAL1.0 device.
665 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800666 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
667 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800668 } else {
669 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
670 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
671 " opened as HAL %x device", halVersion, deviceVersion,
672 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800673 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800674 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
675 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800676 }
677 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800678 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800679}
680
Ruben Brunk6267b532015-04-30 17:44:07 -0700681String8 CameraService::toString(std::set<userid_t> intSet) {
682 String8 s("");
683 bool first = true;
684 for (userid_t i : intSet) {
685 if (first) {
686 s.appendFormat("%d", i);
687 first = false;
688 } else {
689 s.appendFormat(", %d", i);
690 }
691 }
692 return s;
693}
694
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800695int32_t CameraService::mapToInterface(TorchModeStatus status) {
696 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
697 switch (status) {
698 case TorchModeStatus::NOT_AVAILABLE:
699 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
700 break;
701 case TorchModeStatus::AVAILABLE_OFF:
702 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
703 break;
704 case TorchModeStatus::AVAILABLE_ON:
705 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
706 break;
707 default:
708 ALOGW("Unknown new flash status: %d", status);
709 }
710 return serviceStatus;
711}
712
713CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
714 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
715 switch (status) {
716 case CameraDeviceStatus::NOT_PRESENT:
717 serviceStatus = StatusInternal::NOT_PRESENT;
718 break;
719 case CameraDeviceStatus::PRESENT:
720 serviceStatus = StatusInternal::PRESENT;
721 break;
722 case CameraDeviceStatus::ENUMERATING:
723 serviceStatus = StatusInternal::ENUMERATING;
724 break;
725 default:
726 ALOGW("Unknown new HAL device status: %d", status);
727 }
728 return serviceStatus;
729}
730
731int32_t CameraService::mapToInterface(StatusInternal status) {
732 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
733 switch (status) {
734 case StatusInternal::NOT_PRESENT:
735 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
736 break;
737 case StatusInternal::PRESENT:
738 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
739 break;
740 case StatusInternal::ENUMERATING:
741 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
742 break;
743 case StatusInternal::NOT_AVAILABLE:
744 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
745 break;
746 case StatusInternal::UNKNOWN:
747 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
748 break;
749 default:
750 ALOGW("Unknown new internal device status: %d", status);
751 }
752 return serviceStatus;
753}
754
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800755Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800756 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700757
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800758 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800759 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800760 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800761 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800762 if (!(ret = connectHelper<ICameraClient,Client>(
763 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
764 internalPackageName, uid, USE_CALLING_PID,
765 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
766 /*out*/ tmp)
767 ).isOk()) {
768 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700769 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800770 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700771}
772
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800773Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700774 /*out*/
775 CameraParameters* parameters) {
776
777 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
778
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800779 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700780
781 if (parameters == NULL) {
782 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800783 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700784 }
785
Ruben Brunkcc776712015-02-17 20:18:47 -0800786 String8 id = String8::format("%d", cameraId);
787
788 // Check if we already have parameters
789 {
790 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700791 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800792 auto cameraState = getCameraState(id);
793 if (cameraState == nullptr) {
794 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800795 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
796 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800797 }
798 CameraParameters p = cameraState->getShimParams();
799 if (!p.isEmpty()) {
800 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800801 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700802 }
803 }
804
Ruben Brunkcc776712015-02-17 20:18:47 -0800805 int64_t token = IPCThreadState::self()->clearCallingIdentity();
806 ret = initializeShimMetadata(cameraId);
807 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800808 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800809 // Error already logged by callee
810 return ret;
811 }
812
813 // Check for parameters again
814 {
815 // Scope for service lock
816 Mutex::Autolock lock(mServiceLock);
817 auto cameraState = getCameraState(id);
818 if (cameraState == nullptr) {
819 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800820 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
821 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700822 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800823 CameraParameters p = cameraState->getShimParams();
824 if (!p.isEmpty()) {
825 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800826 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700827 }
828 }
829
Ruben Brunkcc776712015-02-17 20:18:47 -0800830 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
831 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800832 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700833}
834
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800835// Can camera service trust the caller based on the calling UID?
836static bool isTrustedCallingUid(uid_t uid) {
837 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700838 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800839 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700840 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800841 return true;
842 default:
843 return false;
844 }
845}
846
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800847Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700848 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
849 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500850
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700851#ifdef __BRILLO__
852 UNUSED(clientName8);
853 UNUSED(clientUid);
854 UNUSED(clientPid);
855 UNUSED(originalClientPid);
856#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700857 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
858 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800859 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -0800860 return allowed;
861 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700862#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -0800863
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800864 int callingPid = getCallingPid();
865
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800866 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800867 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
868 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800869 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
870 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700871 }
872
Ruben Brunkcc776712015-02-17 20:18:47 -0800873 if (getCameraState(cameraId) == nullptr) {
874 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
875 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800876 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
877 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 }
879
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800880 status_t err = checkIfDeviceIsUsable(cameraId);
881 if (err != NO_ERROR) {
882 switch(err) {
883 case -ENODEV:
884 case -EBUSY:
885 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
886 "No camera device with ID \"%s\" currently available", cameraId.string());
887 default:
888 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
889 "Unknown error connecting to ID \"%s\"", cameraId.string());
890 }
891 }
892 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800893}
894
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800895Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700896 const String8& clientName8, int& clientUid, int& clientPid,
897 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800898 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800899 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800901 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800903 clientUid = callingUid;
904 } else if (!isTrustedCallingUid(callingUid)) {
905 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
906 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800907 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
908 "Untrusted caller (calling PID %d, UID %d) trying to "
909 "forward camera access to camera %s for client %s (PID %d, UID %d)",
910 callingPid, callingUid, cameraId.string(),
911 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 }
913
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800914 // Check if we can trust clientPid
915 if (clientPid == USE_CALLING_PID) {
916 clientPid = callingPid;
917 } else if (!isTrustedCallingUid(callingUid)) {
918 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
919 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800920 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
921 "Untrusted caller (calling PID %d, UID %d) trying to "
922 "forward camera access to camera %s for client %s (PID %d, UID %d)",
923 callingPid, callingUid, cameraId.string(),
924 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800925 }
926
927 // If it's not calling from cameraserver, check the permission.
928 if (callingPid != getpid() &&
929 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
930 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800931 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
932 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
933 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800934 }
935
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -0700936 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
937 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700938 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -0700939 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940
Ruben Brunk6267b532015-04-30 17:44:07 -0700941 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800942
Ruben Brunka8ca9152015-04-07 14:23:40 -0700943 // Only allow clients who are being used by the current foreground device user, unless calling
944 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -0700945 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
946 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
947 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
948 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800949 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
950 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
951 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -0700952 }
953
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800954 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800955}
956
957status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
958 auto cameraState = getCameraState(cameraId);
959 int callingPid = getCallingPid();
960 if (cameraState == nullptr) {
961 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
962 cameraId.string());
963 return -ENODEV;
964 }
965
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800966 StatusInternal currentStatus = cameraState->getStatus();
967 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800968 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
969 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700970 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800971 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800972 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
973 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700974 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -0700975 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700976
Ruben Brunkcc776712015-02-17 20:18:47 -0800977 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800978}
979
Ruben Brunkcc776712015-02-17 20:18:47 -0800980void CameraService::finishConnectLocked(const sp<BasicClient>& client,
981 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800982
Ruben Brunkcc776712015-02-17 20:18:47 -0800983 // Make a descriptor for the incoming client
984 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
985 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
986
987 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
988 String8(client->getPackageName()));
989
990 if (evicted.size() > 0) {
991 // This should never happen - clients should already have been removed in disconnect
992 for (auto& i : evicted) {
993 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
994 __FUNCTION__, i->getKey().string());
995 }
996
997 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
998 __FUNCTION__);
999 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001000
1001 // And register a death notification for the client callback. Do
1002 // this last to avoid Binder policy where a nested Binder
1003 // transaction might be pre-empted to service the client death
1004 // notification if the client process dies before linkToDeath is
1005 // invoked.
1006 sp<IBinder> remoteCallback = client->getRemote();
1007 if (remoteCallback != nullptr) {
1008 remoteCallback->linkToDeath(this);
1009 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001010}
1011
1012status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1013 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1014 /*out*/
1015 sp<BasicClient>* client,
1016 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001017 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001018 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001019 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001020 DescriptorPtr clientDescriptor;
1021 {
1022 if (effectiveApiLevel == API_1) {
1023 // If we are using API1, any existing client for this camera ID with the same remote
1024 // should be returned rather than evicted to allow MediaRecorder to work properly.
1025
1026 auto current = mActiveClientManager.get(cameraId);
1027 if (current != nullptr) {
1028 auto clientSp = current->getValue();
1029 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001030 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1031 ALOGW("CameraService connect called from same client, but with a different"
1032 " API level, evicting prior client...");
1033 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001034 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001035 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001036 *client = clientSp;
1037 return NO_ERROR;
1038 }
1039 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001040 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001041 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001042
Ruben Brunkcc776712015-02-17 20:18:47 -08001043 // Get current active client PIDs
1044 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1045 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001046
Emilian Peev8131a262017-02-01 12:33:43 +00001047 std::vector<int> priorityScores(ownerPids.size());
1048 std::vector<int> states(ownerPids.size());
Igor Murashkine6800ce2013-03-04 17:25:57 -08001049
Emilian Peev8131a262017-02-01 12:33:43 +00001050 // Get priority scores of all active PIDs
1051 status_t err = ProcessInfoService::getProcessStatesScoresFromPids(
1052 ownerPids.size(), &ownerPids[0], /*out*/&states[0],
1053 /*out*/&priorityScores[0]);
1054 if (err != OK) {
1055 ALOGE("%s: Priority score query failed: %d",
1056 __FUNCTION__, err);
1057 return err;
1058 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001059
Ruben Brunkcc776712015-02-17 20:18:47 -08001060 // Update all active clients' priorities
Emilian Peev8131a262017-02-01 12:33:43 +00001061 std::map<int,resource_policy::ClientPriority> pidToPriorityMap;
Ruben Brunkcc776712015-02-17 20:18:47 -08001062 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
Emilian Peev8131a262017-02-01 12:33:43 +00001063 pidToPriorityMap.emplace(ownerPids[i],
1064 resource_policy::ClientPriority(priorityScores[i], states[i]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001065 }
1066 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001067
Ruben Brunkcc776712015-02-17 20:18:47 -08001068 // Get state for the given cameraId
1069 auto state = getCameraState(cameraId);
1070 if (state == nullptr) {
1071 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1072 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001073 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001074 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001075 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001076
1077 // Make descriptor for incoming client
1078 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1079 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1080 state->getConflicting(),
Emilian Peev8131a262017-02-01 12:33:43 +00001081 priorityScores[priorityScores.size() - 1],
1082 clientPid,
1083 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001084
1085 // Find clients that would be evicted
1086 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1087
1088 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1089 // background, so we cannot do evictions
1090 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1091 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1092 " priority).", clientPid);
1093
1094 sp<BasicClient> clientSp = clientDescriptor->getValue();
1095 String8 curTime = getFormattedCurrentTime();
1096 auto incompatibleClients =
1097 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1098
1099 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Emilian Peev8131a262017-02-01 12:33:43 +00001100 "(PID %d, score %d state %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001101 cameraId.string(), packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001102 priorityScores[priorityScores.size() - 1],
1103 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001104
1105 for (auto& i : incompatibleClients) {
1106 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
Emilian Peev8131a262017-02-01 12:33:43 +00001107 "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")",
1108 i->getKey().string(),
1109 String8{i->getValue()->getPackageName()}.string(),
1110 i->getOwnerId(), i->getPriority().getScore(),
1111 i->getPriority().getState());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001112 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001113 PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(),
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001114 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
Emilian Peev8131a262017-02-01 12:33:43 +00001115 i->getPriority().getScore(), i->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08001116 }
1117
1118 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001119 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001120 mEventLog.add(msg);
1121
1122 return -EBUSY;
1123 }
1124
1125 for (auto& i : evicted) {
1126 sp<BasicClient> clientSp = i->getValue();
1127 if (clientSp.get() == nullptr) {
1128 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1129
1130 // TODO: Remove this
1131 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1132 __FUNCTION__);
1133 mActiveClientManager.remove(i);
1134 continue;
1135 }
1136
1137 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1138 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001139 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001140
Ruben Brunkcc776712015-02-17 20:18:47 -08001141 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001142 logEvent(String8::format("EVICT device %s client held by package %s (PID"
Emilian Peev8131a262017-02-01 12:33:43 +00001143 " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for"
1144 " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001145 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
Emilian Peev8131a262017-02-01 12:33:43 +00001146 i->getOwnerId(), i->getPriority().getScore(),
1147 i->getPriority().getState(), cameraId.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001148 packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001149 priorityScores[priorityScores.size() - 1],
1150 states[states.size() - 1]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001151
1152 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001153 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001154 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001155 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001156 }
1157
Ruben Brunkcc776712015-02-17 20:18:47 -08001158 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1159 // other clients from connecting in mServiceLockWrapper if held
1160 mServiceLock.unlock();
1161
1162 // Clear caller identity temporarily so client disconnect PID checks work correctly
1163 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1164
1165 // Destroy evicted clients
1166 for (auto& i : evictedClients) {
1167 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001168 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001169 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001170
1171 IPCThreadState::self()->restoreCallingIdentity(token);
1172
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001173 for (const auto& i : evictedClients) {
1174 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1175 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1176 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1177 if (ret == TIMED_OUT) {
1178 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1179 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1180 mActiveClientManager.toString().string());
1181 return -EBUSY;
1182 }
1183 if (ret != NO_ERROR) {
1184 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1185 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1186 ret, mActiveClientManager.toString().string());
1187 return ret;
1188 }
1189 }
1190
1191 evictedClients.clear();
1192
Ruben Brunkcc776712015-02-17 20:18:47 -08001193 // Once clients have been disconnected, relock
1194 mServiceLock.lock();
1195
1196 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1197 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1198 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001199 }
1200
Ruben Brunkcc776712015-02-17 20:18:47 -08001201 *partial = clientDescriptor;
1202 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001203}
1204
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001205Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001206 const sp<ICameraClient>& cameraClient,
1207 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001208 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001209 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001210 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001211 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001212 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001213
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001214 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001215 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001216 String8 id = String8::format("%d", cameraId);
1217 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001218 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1219 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1220 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1221 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001222
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001223 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001224 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001225 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001226 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001227 }
1228
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001229 *device = client;
1230 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231}
1232
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001233Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001234 const sp<ICameraClient>& cameraClient,
1235 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001236 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001237 int clientUid,
1238 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001239 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001240
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001241 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001242 String8 id = String8::format("%d", cameraId);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001243
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001244 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001245 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001246 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1247 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1248 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1249 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001250
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001251 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001252 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001253 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001254 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001255 }
1256
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001257 *device = client;
1258 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001259}
1260
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001261Status CameraService::connectDevice(
1262 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001263 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001264 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001265 int clientUid,
1266 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001267 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001268
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001269 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001270 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001271 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001272 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001273 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1274 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1275 clientUid, USE_CALLING_PID, API_2,
1276 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1277 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001278
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001279 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001280 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001281 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001282 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001283 }
1284
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001285 *device = client;
1286 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001287}
1288
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001289template<class CALLBACK, class CLIENT>
1290Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
1291 int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
1292 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
1293 /*out*/sp<CLIENT>& device) {
1294 binder::Status ret = binder::Status::ok();
1295
1296 String8 clientName8(clientPackageName);
1297
1298 int originalClientPid = 0;
1299
1300 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1301 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1302 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1303 static_cast<int>(effectiveApiLevel));
1304
1305 sp<CLIENT> client = nullptr;
1306 {
1307 // Acquire mServiceLock and prevent other clients from connecting
1308 std::unique_ptr<AutoConditionLock> lock =
1309 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1310
1311 if (lock == nullptr) {
1312 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1313 , clientPid);
1314 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1315 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1316 cameraId.string(), clientName8.string(), clientPid);
1317 }
1318
1319 // Enforce client permissions and do basic sanity checks
1320 if(!(ret = validateConnectLocked(cameraId, clientName8,
1321 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1322 return ret;
1323 }
1324
1325 // Check the shim parameters after acquiring lock, if they have already been updated and
1326 // we were doing a shim update, return immediately
1327 if (shimUpdateOnly) {
1328 auto cameraState = getCameraState(cameraId);
1329 if (cameraState != nullptr) {
1330 if (!cameraState->getShimParams().isEmpty()) return ret;
1331 }
1332 }
1333
1334 status_t err;
1335
1336 sp<BasicClient> clientTmp = nullptr;
1337 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1338 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1339 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1340 /*out*/&partial)) != NO_ERROR) {
1341 switch (err) {
1342 case -ENODEV:
1343 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1344 "No camera device with ID \"%s\" currently available",
1345 cameraId.string());
1346 case -EBUSY:
1347 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1348 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1349 cameraId.string());
1350 default:
1351 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1352 "Unexpected error %s (%d) opening camera \"%s\"",
1353 strerror(-err), err, cameraId.string());
1354 }
1355 }
1356
1357 if (clientTmp.get() != nullptr) {
1358 // Handle special case for API1 MediaRecorder where the existing client is returned
1359 device = static_cast<CLIENT*>(clientTmp.get());
1360 return ret;
1361 }
1362
1363 // give flashlight a chance to close devices if necessary.
1364 mFlashlight->prepareDeviceOpen(cameraId);
1365
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001366 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001367 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -08001368 if (facing == -1) {
1369 ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string());
1370 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1371 "Unable to get camera device \"%s\" facing", cameraId.string());
1372 }
1373
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001374 sp<BasicClient> tmp = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001375 if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001376 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
1377 /*out*/&tmp)).isOk()) {
1378 return ret;
1379 }
1380 client = static_cast<CLIENT*>(tmp.get());
1381
1382 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1383 __FUNCTION__);
1384
Emilian Peevf53f66e2017-04-11 14:29:43 +01001385 err = client->initialize(mCameraProviderManager);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001386 if (err != OK) {
1387 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001388 // Errors could be from the HAL module open call or from AppOpsManager
1389 switch(err) {
1390 case BAD_VALUE:
1391 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1392 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1393 case -EBUSY:
1394 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1395 "Camera \"%s\" is already open", cameraId.string());
1396 case -EUSERS:
1397 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1398 "Too many cameras already open, cannot open camera \"%s\"",
1399 cameraId.string());
1400 case PERMISSION_DENIED:
1401 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1402 "No permission to open camera \"%s\"", cameraId.string());
1403 case -EACCES:
1404 return STATUS_ERROR_FMT(ERROR_DISABLED,
1405 "Camera \"%s\" disabled by policy", cameraId.string());
1406 case -ENODEV:
1407 default:
1408 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1409 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1410 strerror(-err), err);
1411 }
1412 }
1413
1414 // Update shim paremeters for legacy clients
1415 if (effectiveApiLevel == API_1) {
1416 // Assume we have always received a Client subclass for API1
1417 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1418 String8 rawParams = shimClient->getParameters();
1419 CameraParameters params(rawParams);
1420
1421 auto cameraState = getCameraState(cameraId);
1422 if (cameraState != nullptr) {
1423 cameraState->setShimParams(params);
1424 } else {
1425 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1426 __FUNCTION__, cameraId.string());
1427 }
1428 }
1429
1430 if (shimUpdateOnly) {
1431 // If only updating legacy shim parameters, immediately disconnect client
1432 mServiceLock.unlock();
1433 client->disconnect();
1434 mServiceLock.lock();
1435 } else {
1436 // Otherwise, add client to active clients list
1437 finishConnectLocked(client, partial);
1438 }
1439 } // lock is destroyed, allow further connect calls
1440
1441 // Important: release the mutex here so the client can call back into the service from its
1442 // destructor (can be at the end of the call)
1443 device = client;
1444 return ret;
1445}
1446
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001447Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001448 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001449 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001450
1451 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001452 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001453 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001454 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1455 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001456 }
1457
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001458 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001459 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001460
1461 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001462 auto state = getCameraState(id);
1463 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001464 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001465 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1466 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001467 }
1468
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001469 StatusInternal cameraStatus = state->getStatus();
1470 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001471 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1472 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001473 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1474 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001475 }
1476
1477 {
1478 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001479 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001480 status_t err = getTorchStatusLocked(id, &status);
1481 if (err != OK) {
1482 if (err == NAME_NOT_FOUND) {
1483 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1484 "Camera \"%s\" does not have a flash unit", id.string());
1485 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001486 ALOGE("%s: getting current torch status failed for camera %s",
1487 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001488 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1489 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1490 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001491 }
1492
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001493 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001494 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001495 ALOGE("%s: torch mode of camera %s is not available because "
1496 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001497 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1498 "Torch for camera \"%s\" is not available due to an existing camera user",
1499 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001500 } else {
1501 ALOGE("%s: torch mode of camera %s is not available due to "
1502 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001503 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1504 "Torch for camera \"%s\" is not available due to insufficient resources",
1505 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001506 }
1507 }
1508 }
1509
Ruben Brunk99e69712015-05-26 17:25:07 -07001510 {
1511 // Update UID map - this is used in the torch status changed callbacks, so must be done
1512 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001513 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001514 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1515 mTorchUidMap[id].first = uid;
1516 mTorchUidMap[id].second = uid;
1517 } else {
1518 // Set the pending UID
1519 mTorchUidMap[id].first = uid;
1520 }
1521 }
1522
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001523 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001524
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001525 if (err != OK) {
1526 int32_t errorCode;
1527 String8 msg;
1528 switch (err) {
1529 case -ENOSYS:
1530 msg = String8::format("Camera \"%s\" has no flashlight",
1531 id.string());
1532 errorCode = ERROR_ILLEGAL_ARGUMENT;
1533 break;
1534 default:
1535 msg = String8::format(
1536 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1537 id.string(), enabled, strerror(-err), err);
1538 errorCode = ERROR_INVALID_OPERATION;
1539 }
1540 ALOGE("%s: %s", __FUNCTION__, msg.string());
1541 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001542 }
1543
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001544 {
1545 // update the link to client's death
1546 Mutex::Autolock al(mTorchClientMapMutex);
1547 ssize_t index = mTorchClientMap.indexOfKey(id);
1548 if (enabled) {
1549 if (index == NAME_NOT_FOUND) {
1550 mTorchClientMap.add(id, clientBinder);
1551 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001552 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001553 mTorchClientMap.replaceValueAt(index, clientBinder);
1554 }
1555 clientBinder->linkToDeath(this);
1556 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001557 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001558 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001559 }
1560
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001561 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001562}
1563
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001564Status CameraService::notifySystemEvent(int32_t eventId,
1565 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001566 ATRACE_CALL();
1567
Ruben Brunk36597b22015-03-20 22:15:57 -07001568 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001569 case ICameraService::EVENT_USER_SWITCHED: {
1570 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001571 break;
1572 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001573 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001574 default: {
1575 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1576 eventId);
1577 break;
1578 }
1579 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001580 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001581}
1582
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001583Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1584 /*out*/
1585 std::vector<hardware::CameraStatus> *cameraStatuses) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001586 ATRACE_CALL();
1587
Igor Murashkinbfc99152013-02-27 12:55:20 -08001588 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001589
Ruben Brunk3450ba72015-06-16 11:00:37 -07001590 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001591 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001592 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001593 }
1594
Igor Murashkinbfc99152013-02-27 12:55:20 -08001595 Mutex::Autolock lock(mServiceLock);
1596
Ruben Brunkcc776712015-02-17 20:18:47 -08001597 {
1598 Mutex::Autolock lock(mStatusListenerLock);
1599 for (auto& it : mListenerList) {
1600 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1601 ALOGW("%s: Tried to add listener %p which was already subscribed",
1602 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001603 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001604 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001605 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001606
1607 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001608 }
1609
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001610 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001611 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001612 Mutex::Autolock lock(mCameraStatesLock);
1613 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001614 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001615 }
1616 }
1617
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001618 /*
1619 * Immediately signal current torch status to this listener only
1620 * This may be a subset of all the devices, so don't include it in the response directly
1621 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001622 {
1623 Mutex::Autolock al(mTorchStatusMutex);
1624 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001625 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001626 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001627 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001628 }
1629
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001630 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001631}
Ruben Brunkcc776712015-02-17 20:18:47 -08001632
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001633Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001634 ATRACE_CALL();
1635
Igor Murashkinbfc99152013-02-27 12:55:20 -08001636 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1637
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001638 if (listener == 0) {
1639 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001640 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001641 }
1642
Igor Murashkinbfc99152013-02-27 12:55:20 -08001643 Mutex::Autolock lock(mServiceLock);
1644
Ruben Brunkcc776712015-02-17 20:18:47 -08001645 {
1646 Mutex::Autolock lock(mStatusListenerLock);
1647 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1648 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1649 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001650 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001651 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001652 }
1653 }
1654
1655 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1656 __FUNCTION__, listener.get());
1657
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001658 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001659}
1660
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001661Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001662
1663 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001664 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1665
1666 if (parameters == NULL) {
1667 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001668 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001669 }
1670
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001671 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001672
1673 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001674 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001675 // Error logged by caller
1676 return ret;
1677 }
1678
1679 String8 shimParamsString8 = shimParams.flatten();
1680 String16 shimParamsString16 = String16(shimParamsString8);
1681
1682 *parameters = shimParamsString16;
1683
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001684 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001685}
1686
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001687Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
1688 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001689 ATRACE_CALL();
1690
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001691 const String8 id = String8(cameraId);
1692
1693 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001694
1695 switch (apiVersion) {
1696 case API_VERSION_1:
1697 case API_VERSION_2:
1698 break;
1699 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001700 String8 msg = String8::format("Unknown API version %d", apiVersion);
1701 ALOGE("%s: %s", __FUNCTION__, msg.string());
1702 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001703 }
1704
Emilian Peev28ad2ea2017-02-07 16:14:32 +00001705 int deviceVersion = getDeviceVersion(id);
Igor Murashkin65d14b92014-06-17 12:03:20 -07001706 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001707 case CAMERA_DEVICE_API_VERSION_1_0:
1708 case CAMERA_DEVICE_API_VERSION_3_0:
1709 case CAMERA_DEVICE_API_VERSION_3_1:
1710 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001711 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
1712 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001713 *isSupported = false;
1714 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001715 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
1716 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001717 *isSupported = true;
1718 }
1719 break;
1720 case CAMERA_DEVICE_API_VERSION_3_2:
1721 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001722 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001723 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
1724 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001725 *isSupported = true;
1726 break;
1727 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001728 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001729 ALOGE("%s: %s", __FUNCTION__, msg.string());
1730 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001731 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001732 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001733 String8 msg = String8::format("Unknown device version %x for device %s",
1734 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001735 ALOGE("%s: %s", __FUNCTION__, msg.string());
1736 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1737 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001738 }
1739
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001740 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001741}
1742
Ruben Brunkcc776712015-02-17 20:18:47 -08001743void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001744 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001745 for (auto& i : mActiveClientManager.getAll()) {
1746 auto clientSp = i->getValue();
1747 if (clientSp.get() == client) {
1748 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001749 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001750 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001751}
1752
Ruben Brunkcc776712015-02-17 20:18:47 -08001753bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1754 const int callingPid = getCallingPid();
1755 const int servicePid = getpid();
1756 bool ret = false;
1757 {
1758 // Acquire mServiceLock and prevent other clients from connecting
1759 std::unique_ptr<AutoConditionLock> lock =
1760 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001761
Igor Murashkin634a5152013-02-20 17:15:11 -08001762
Ruben Brunkcc776712015-02-17 20:18:47 -08001763 std::vector<sp<BasicClient>> evicted;
1764 for (auto& i : mActiveClientManager.getAll()) {
1765 auto clientSp = i->getValue();
1766 if (clientSp.get() == nullptr) {
1767 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1768 mActiveClientManager.remove(i);
1769 continue;
1770 }
1771 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1772 callingPid == clientSp->getClientPid())) {
1773 mActiveClientManager.remove(i);
1774 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001775
Ruben Brunkcc776712015-02-17 20:18:47 -08001776 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001777 clientSp->notifyError(
1778 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001779 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001780 }
1781 }
1782
Ruben Brunkcc776712015-02-17 20:18:47 -08001783 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1784 // other clients from connecting in mServiceLockWrapper if held
1785 mServiceLock.unlock();
1786
Ruben Brunk36597b22015-03-20 22:15:57 -07001787 // Do not clear caller identity, remote caller should be client proccess
1788
Ruben Brunkcc776712015-02-17 20:18:47 -08001789 for (auto& i : evicted) {
1790 if (i.get() != nullptr) {
1791 i->disconnect();
1792 ret = true;
1793 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001794 }
1795
Ruben Brunkcc776712015-02-17 20:18:47 -08001796 // Reacquire mServiceLock
1797 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001798
Ruben Brunkcc776712015-02-17 20:18:47 -08001799 } // lock is destroyed, allow further connect calls
1800
1801 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001802}
1803
Ruben Brunkcc776712015-02-17 20:18:47 -08001804std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1805 const String8& cameraId) const {
1806 std::shared_ptr<CameraState> state;
1807 {
1808 Mutex::Autolock lock(mCameraStatesLock);
1809 auto iter = mCameraStates.find(cameraId);
1810 if (iter != mCameraStates.end()) {
1811 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812 }
1813 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001814 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001815}
1816
Ruben Brunkcc776712015-02-17 20:18:47 -08001817sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1818 // Remove from active clients list
1819 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1820 if (clientDescriptorPtr == nullptr) {
1821 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1822 cameraId.string());
1823 return sp<BasicClient>{nullptr};
1824 }
1825
1826 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001827}
1828
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001829void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001830 // Acquire mServiceLock and prevent other clients from connecting
1831 std::unique_ptr<AutoConditionLock> lock =
1832 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1833
Ruben Brunk6267b532015-04-30 17:44:07 -07001834 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001835 for (size_t i = 0; i < newUserIds.size(); i++) {
1836 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001837 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001838 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07001839 return;
1840 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001841 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001842 }
1843
Ruben Brunka8ca9152015-04-07 14:23:40 -07001844
Ruben Brunk6267b532015-04-30 17:44:07 -07001845 if (newAllowedUsers == mAllowedUsers) {
1846 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1847 return;
1848 }
1849
1850 logUserSwitch(mAllowedUsers, newAllowedUsers);
1851
1852 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001853
1854 // Current user has switched, evict all current clients.
1855 std::vector<sp<BasicClient>> evicted;
1856 for (auto& i : mActiveClientManager.getAll()) {
1857 auto clientSp = i->getValue();
1858
1859 if (clientSp.get() == nullptr) {
1860 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1861 continue;
1862 }
1863
Ruben Brunk6267b532015-04-30 17:44:07 -07001864 // Don't evict clients that are still allowed.
1865 uid_t clientUid = clientSp->getClientUid();
1866 userid_t clientUserId = multiuser_get_user_id(clientUid);
1867 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1868 continue;
1869 }
1870
Ruben Brunk36597b22015-03-20 22:15:57 -07001871 evicted.push_back(clientSp);
1872
1873 String8 curTime = getFormattedCurrentTime();
1874
1875 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1876 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001877
Ruben Brunk36597b22015-03-20 22:15:57 -07001878 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001879 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001880 PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due"
1881 " to user switch.", i->getKey().string(),
1882 String8{clientSp->getPackageName()}.string(),
1883 i->getOwnerId(), i->getPriority().getScore(),
1884 i->getPriority().getState()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001885
1886 }
1887
1888 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1889 // blocking other clients from connecting in mServiceLockWrapper if held.
1890 mServiceLock.unlock();
1891
1892 // Clear caller identity temporarily so client disconnect PID checks work correctly
1893 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1894
1895 for (auto& i : evicted) {
1896 i->disconnect();
1897 }
1898
1899 IPCThreadState::self()->restoreCallingIdentity(token);
1900
1901 // Reacquire mServiceLock
1902 mServiceLock.lock();
1903}
Ruben Brunkcc776712015-02-17 20:18:47 -08001904
Ruben Brunka8ca9152015-04-07 14:23:40 -07001905void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001906 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001907 Mutex::Autolock l(mLogLock);
1908 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001909}
1910
Ruben Brunka8ca9152015-04-07 14:23:40 -07001911void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001912 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001913 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001914 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001915 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001916}
1917
1918void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001919 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001920 // Log the clients evicted
1921 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001922 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001923}
1924
1925void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001926 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001927 // Log the client rejected
1928 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001929 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001930}
1931
Ruben Brunk6267b532015-04-30 17:44:07 -07001932void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1933 const std::set<userid_t>& newUserIds) {
1934 String8 newUsers = toString(newUserIds);
1935 String8 oldUsers = toString(oldUserIds);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001936 if (oldUsers.size() == 0) {
1937 oldUsers = "<None>";
1938 }
Ruben Brunka8ca9152015-04-07 14:23:40 -07001939 // Log the new and old users
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001940 logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s",
Ruben Brunk6267b532015-04-30 17:44:07 -07001941 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001942}
1943
1944void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1945 // Log the device removal
1946 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1947}
1948
1949void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1950 // Log the device removal
1951 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1952}
1953
1954void CameraService::logClientDied(int clientPid, const char* reason) {
1955 // Log the device removal
1956 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001957}
1958
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001959void CameraService::logServiceError(const char* msg, int errorCode) {
1960 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001961 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001962}
1963
Ruben Brunk36597b22015-03-20 22:15:57 -07001964status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1965 uint32_t flags) {
1966
1967 const int pid = getCallingPid();
1968 const int selfPid = getpid();
1969
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970 // Permission checks
1971 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001972 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07001973 if (pid != selfPid) {
1974 // Ensure we're being called by system_server, or similar process with
1975 // permissions to notify the camera service about system events
1976 if (!checkCallingPermission(
1977 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1978 const int uid = getCallingUid();
1979 ALOGE("Permission Denial: cannot send updates to camera service about system"
1980 " events from pid=%d, uid=%d", pid, uid);
1981 return PERMISSION_DENIED;
1982 }
1983 }
1984 break;
1985 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986 }
1987
1988 return BnCameraService::onTransact(code, data, reply, flags);
1989}
1990
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991// We share the media players for shutter and recording sound for all clients.
1992// A reference count is kept to determine when we will actually release the
1993// media players.
1994
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001995MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001996 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001997 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001998 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999 mp->prepare();
2000 } else {
Steve Block29357bc2012-01-06 19:20:56 +00002001 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002002 return NULL;
2003 }
2004 return mp;
2005}
2006
2007void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002008 ATRACE_CALL();
2009
Mathias Agopian65ab4712010-07-14 17:59:35 -07002010 Mutex::Autolock lock(mSoundLock);
2011 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2012 if (mSoundRef++) return;
2013
2014 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002015 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2016 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002017}
2018
2019void CameraService::releaseSound() {
2020 Mutex::Autolock lock(mSoundLock);
2021 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2022 if (--mSoundRef) return;
2023
2024 for (int i = 0; i < NUM_SOUNDS; i++) {
2025 if (mSoundPlayer[i] != 0) {
2026 mSoundPlayer[i]->disconnect();
2027 mSoundPlayer[i].clear();
2028 }
2029 }
2030}
2031
2032void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002033 ATRACE_CALL();
2034
Mathias Agopian65ab4712010-07-14 17:59:35 -07002035 LOG1("playSound(%d)", kind);
2036 Mutex::Autolock lock(mSoundLock);
2037 sp<MediaPlayer> player = mSoundPlayer[kind];
2038 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002039 player->seekTo(0);
2040 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002041 }
2042}
2043
2044// ----------------------------------------------------------------------------
2045
2046CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002047 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002048 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002049 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002050 int clientPid, uid_t clientUid,
2051 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002052 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002053 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002054 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002055 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002056 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002057 servicePid),
2058 mCameraId(CameraService::cameraIdToInt(cameraIdStr))
Igor Murashkin634a5152013-02-20 17:15:11 -08002059{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002060 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002061 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002063 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002064
Mathias Agopian65ab4712010-07-14 17:59:35 -07002065 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002066
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002067 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002068}
2069
Mathias Agopian65ab4712010-07-14 17:59:35 -07002070// tear down the client
2071CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002072 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002073 mDestructionStarted = true;
2074
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002075 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002076 // unconditionally disconnect. function is idempotent
2077 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002078}
2079
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002080sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2081
Igor Murashkin634a5152013-02-20 17:15:11 -08002082CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002083 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002084 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002085 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002086 int clientPid, uid_t clientUid,
2087 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002088 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2089 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2090 mServicePid(servicePid),
2091 mDisconnected(false),
2092 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002093{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002094 if (sCameraService == nullptr) {
2095 sCameraService = cameraService;
2096 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002097 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002098 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002099
2100 // In some cases the calling code has no access to the package it runs under.
2101 // For example, NDK camera API.
2102 // In this case we will get the packages for the calling UID and pick the first one
2103 // for attributing the app op. This will work correctly for runtime permissions
2104 // as for legacy apps we will toggle the app op for all packages in the UID.
2105 // The caveat is that the operation may be attributed to the wrong package and
2106 // stats based on app ops may be slightly off.
2107 if (mClientPackageName.size() <= 0) {
2108 sp<IServiceManager> sm = defaultServiceManager();
2109 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2110 if (binder == 0) {
2111 ALOGE("Cannot get permission service");
2112 // Leave mClientPackageName unchanged (empty) and the further interaction
2113 // with camera will fail in BasicClient::startCameraOps
2114 return;
2115 }
2116
2117 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2118 Vector<String16> packages;
2119
2120 permCtrl->getPackagesForUid(mClientUid, packages);
2121
2122 if (packages.isEmpty()) {
2123 ALOGE("No packages for calling UID");
2124 // Leave mClientPackageName unchanged (empty) and the further interaction
2125 // with camera will fail in BasicClient::startCameraOps
2126 return;
2127 }
2128 mClientPackageName = packages[0];
2129 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002130}
2131
2132CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002133 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002134 mDestructionStarted = true;
2135}
2136
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002137binder::Status CameraService::BasicClient::disconnect() {
2138 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002139 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002140 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002141 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002142 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002143
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002144 sCameraService->removeByClient(this);
2145 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002146 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002147
2148 sp<IBinder> remote = getRemote();
2149 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002150 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002151 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002152
2153 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002154 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002155 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2156 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2157 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002158
Igor Murashkincba2c162013-03-20 15:56:31 -07002159 // client shouldn't be able to call into us anymore
2160 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002161
2162 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002163}
2164
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002165status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2166 // No dumping of clients directly over Binder,
2167 // must go through CameraService::dump
2168 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2169 IPCThreadState::self()->getCallingUid(), NULL, 0);
2170 return OK;
2171}
2172
Ruben Brunkcc776712015-02-17 20:18:47 -08002173String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002174 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002175}
2176
2177
2178int CameraService::BasicClient::getClientPid() const {
2179 return mClientPid;
2180}
2181
Ruben Brunk6267b532015-04-30 17:44:07 -07002182uid_t CameraService::BasicClient::getClientUid() const {
2183 return mClientUid;
2184}
2185
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002186bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2187 // Defaults to API2.
2188 return level == API_2;
2189}
2190
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002191status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002192 ATRACE_CALL();
2193
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002194 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002195 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002196 mOpsCallback = new OpsCallback(this);
2197
Igor Murashkine6800ce2013-03-04 17:25:57 -08002198 {
2199 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002200 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002201 }
2202
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002203 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002204 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002205 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002206 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002207
Svetoslav28e8ef72015-05-11 19:21:31 -07002208 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002209 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2210 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002211 return PERMISSION_DENIED;
2212 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002213
Svetoslav28e8ef72015-05-11 19:21:31 -07002214 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002215 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2216 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002217 // Return the same error as for device policy manager rejection
2218 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002219 }
2220
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002221 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002222
2223 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002224 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002225
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002226 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002227 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -07002228 mCameraIdStr, mCameraFacing, mClientPackageName);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002229
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002230 return OK;
2231}
2232
2233status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002234 ATRACE_CALL();
2235
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002236 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002237 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002238 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002239 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002240 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002241 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002242
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002243 // This function is called when a client disconnects. This should
2244 // release the camera, but actually only if it was in a proper
2245 // functional state, i.e. with status NOT_AVAILABLE
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002246 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002247 StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002248
Ruben Brunkcc776712015-02-17 20:18:47 -08002249 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002250 sCameraService->updateStatus(StatusInternal::PRESENT,
2251 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002252
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002253 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002254 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -07002255 mCameraIdStr, mCameraFacing, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002256 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002257 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002258 if (mOpsCallback != NULL) {
2259 mAppOpsManager.stopWatchingMode(mOpsCallback);
2260 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002261 mOpsCallback.clear();
2262
2263 return OK;
2264}
2265
2266void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002267 ATRACE_CALL();
2268
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002269 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002270 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002271
2272 if (op != AppOpsManager::OP_CAMERA) {
2273 ALOGW("Unexpected app ops notification received: %d", op);
2274 return;
2275 }
2276
2277 int32_t res;
2278 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002279 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002280 ALOGV("checkOp returns: %d, %s ", res,
2281 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2282 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2283 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2284 "UNKNOWN");
2285
2286 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002287 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002288 myName.string());
2289 // Reset the client PID to allow server-initiated disconnect,
2290 // and to prevent further calls by client.
2291 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002292 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002293 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002294 disconnect();
2295 }
2296}
2297
Mathias Agopian65ab4712010-07-14 17:59:35 -07002298// ----------------------------------------------------------------------------
2299
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002300void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002301 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002302 (void) errorCode;
2303 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002304 if (mRemoteCallback != NULL) {
2305 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2306 } else {
2307 ALOGE("mRemoteCallback is NULL!!");
2308 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002309}
2310
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002311// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002312binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002313 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002314 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002315}
2316
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002317bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2318 return level == API_1;
2319}
2320
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002321CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2322 mClient(client) {
2323}
2324
2325void CameraService::Client::OpsCallback::opChanged(int32_t op,
2326 const String16& packageName) {
2327 sp<BasicClient> client = mClient.promote();
2328 if (client != NULL) {
2329 client->opChanged(op, packageName);
2330 }
2331}
2332
Mathias Agopian65ab4712010-07-14 17:59:35 -07002333// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002334// CameraState
2335// ----------------------------------------------------------------------------
2336
2337CameraService::CameraState::CameraState(const String8& id, int cost,
2338 const std::set<String8>& conflicting) : mId(id),
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002339 mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002340
2341CameraService::CameraState::~CameraState() {}
2342
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002343CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002344 Mutex::Autolock lock(mStatusLock);
2345 return mStatus;
2346}
2347
2348CameraParameters CameraService::CameraState::getShimParams() const {
2349 return mShimParams;
2350}
2351
2352void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2353 mShimParams = params;
2354}
2355
2356int CameraService::CameraState::getCost() const {
2357 return mCost;
2358}
2359
2360std::set<String8> CameraService::CameraState::getConflicting() const {
2361 return mConflicting;
2362}
2363
2364String8 CameraService::CameraState::getId() const {
2365 return mId;
2366}
2367
2368// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002369// ClientEventListener
2370// ----------------------------------------------------------------------------
2371
2372void CameraService::ClientEventListener::onClientAdded(
2373 const resource_policy::ClientDescriptor<String8,
2374 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002375 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002376 if (basicClient.get() != nullptr) {
2377 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2378 notifier.noteStartCamera(descriptor.getKey(),
2379 static_cast<int>(basicClient->getClientUid()));
2380 }
2381}
2382
2383void CameraService::ClientEventListener::onClientRemoved(
2384 const resource_policy::ClientDescriptor<String8,
2385 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002386 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002387 if (basicClient.get() != nullptr) {
2388 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2389 notifier.noteStopCamera(descriptor.getKey(),
2390 static_cast<int>(basicClient->getClientUid()));
2391 }
2392}
2393
2394
2395// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002396// CameraClientManager
2397// ----------------------------------------------------------------------------
2398
Ruben Brunk99e69712015-05-26 17:25:07 -07002399CameraService::CameraClientManager::CameraClientManager() {
2400 setListener(std::make_shared<ClientEventListener>());
2401}
2402
Ruben Brunkcc776712015-02-17 20:18:47 -08002403CameraService::CameraClientManager::~CameraClientManager() {}
2404
2405sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2406 const String8& id) const {
2407 auto descriptor = get(id);
2408 if (descriptor == nullptr) {
2409 return sp<BasicClient>{nullptr};
2410 }
2411 return descriptor->getValue();
2412}
2413
2414String8 CameraService::CameraClientManager::toString() const {
2415 auto all = getAll();
2416 String8 ret("[");
2417 bool hasAny = false;
2418 for (auto& i : all) {
2419 hasAny = true;
2420 String8 key = i->getKey();
2421 int32_t cost = i->getCost();
2422 int32_t pid = i->getOwnerId();
Emilian Peev8131a262017-02-01 12:33:43 +00002423 int32_t score = i->getPriority().getScore();
2424 int32_t state = i->getPriority().getState();
Ruben Brunkcc776712015-02-17 20:18:47 -08002425 auto conflicting = i->getConflicting();
2426 auto clientSp = i->getValue();
2427 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002428 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002429 if (clientSp.get() != nullptr) {
2430 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002431 uid_t clientUid = clientSp->getClientUid();
2432 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002433 }
Emilian Peev8131a262017-02-01 12:33:43 +00002434 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %"
2435 PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002436
Ruben Brunk6267b532015-04-30 17:44:07 -07002437 if (clientSp.get() != nullptr) {
2438 ret.appendFormat("User Id: %d, ", clientUserId);
2439 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002440 if (packageName.size() != 0) {
2441 ret.appendFormat("Client Package Name: %s", packageName.string());
2442 }
2443
2444 ret.append(", Conflicting Client Devices: {");
2445 for (auto& j : conflicting) {
2446 ret.appendFormat("%s, ", j.string());
2447 }
2448 ret.append("})");
2449 }
2450 if (hasAny) ret.append("\n");
2451 ret.append("]\n");
2452 return ret;
2453}
2454
2455CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2456 const String8& key, const sp<BasicClient>& value, int32_t cost,
Emilian Peev8131a262017-02-01 12:33:43 +00002457 const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId,
2458 int32_t state) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002459
2460 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
Emilian Peev8131a262017-02-01 12:33:43 +00002461 key, value, cost, conflictingKeys, score, ownerId, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002462}
2463
2464CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2465 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2466 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
Emilian Peev8131a262017-02-01 12:33:43 +00002467 partial->getConflicting(), partial->getPriority().getScore(),
2468 partial->getOwnerId(), partial->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08002469}
2470
2471// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472
2473static const int kDumpLockRetries = 50;
2474static const int kDumpLockSleep = 60000;
2475
2476static bool tryLock(Mutex& mutex)
2477{
2478 bool locked = false;
2479 for (int i = 0; i < kDumpLockRetries; ++i) {
2480 if (mutex.tryLock() == NO_ERROR) {
2481 locked = true;
2482 break;
2483 }
2484 usleep(kDumpLockSleep);
2485 }
2486 return locked;
2487}
2488
2489status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002490 ATRACE_CALL();
2491
Mathias Agopian65ab4712010-07-14 17:59:35 -07002492 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002493 dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07002494 getCallingPid(),
2495 getCallingUid());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002496 return NO_ERROR;
2497 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002498 bool locked = tryLock(mServiceLock);
2499 // failed to lock - CameraService is probably deadlocked
2500 if (!locked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002501 dprintf(fd, "!! CameraService may be deadlocked !!\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002502 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002503
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002504 if (!mInitialized) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002505 dprintf(fd, "!! No camera HAL available !!\n");
Ruben Brunkf81648e2014-04-17 16:14:57 -07002506
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002507 // Dump event log for error information
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002508 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002509
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002510 if (locked) mServiceLock.unlock();
2511 return NO_ERROR;
2512 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002513 dprintf(fd, "\n== Service global info: ==\n\n");
2514 dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras);
2515 dprintf(fd, "Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002516 String8 activeClientString = mActiveClientManager.toString();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002517 dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string());
2518 dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002519
2520 dumpEventLog(fd);
2521
2522 bool stateLocked = tryLock(mCameraStatesLock);
2523 if (!stateLocked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002524 dprintf(fd, "CameraStates in use, may be deadlocked\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002525 }
2526
2527 for (auto& state : mCameraStates) {
2528 String8 cameraId = state.first;
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002529
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002530 dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002531
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002532 CameraParameters p = state.second->getShimParams();
2533 if (!p.isEmpty()) {
2534 dprintf(fd, " Camera1 API shim is using parameters:\n ");
2535 p.dump(fd, args);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002536 }
2537
2538 auto clientDescriptor = mActiveClientManager.get(cameraId);
Zhijun He2f140ed2017-02-08 09:57:23 -08002539 if (clientDescriptor != nullptr) {
2540 dprintf(fd, " Device %s is open. Client instance dump:\n",
2541 cameraId.string());
2542 dprintf(fd, " Client priority score: %d state: %d\n",
2543 clientDescriptor->getPriority().getScore(),
2544 clientDescriptor->getPriority().getState());
2545 dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId());
2546
2547 auto client = clientDescriptor->getValue();
2548 dprintf(fd, " Client package: %s\n",
2549 String8(client->getPackageName()).string());
2550
2551 client->dumpClient(fd, args);
2552 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002553 dprintf(fd, " Device %s is closed, no client instance\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002554 cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002555 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002556
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002557 }
2558
2559 if (stateLocked) mCameraStatesLock.unlock();
2560
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002561 if (locked) mServiceLock.unlock();
2562
Emilian Peevf53f66e2017-04-11 14:29:43 +01002563 mCameraProviderManager->dump(fd, args);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002564
2565 dprintf(fd, "\n== Vendor tags: ==\n\n");
2566
2567 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2568 if (desc == NULL) {
Emilian Peev71c73a22017-03-21 16:35:51 +00002569 sp<VendorTagDescriptorCache> cache =
2570 VendorTagDescriptorCache::getGlobalVendorTagCache();
2571 if (cache == NULL) {
2572 dprintf(fd, "No vendor tags.\n");
2573 } else {
2574 cache->dump(fd, /*verbosity*/2, /*indentation*/2);
2575 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002576 } else {
2577 desc->dump(fd, /*verbosity*/2, /*indentation*/2);
2578 }
2579
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002580 // Dump camera traces if there were any
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002581 dprintf(fd, "\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002582 camera3::CameraTraces::dump(fd, args);
2583
2584 // Process dump arguments, if any
2585 int n = args.size();
2586 String16 verboseOption("-v");
2587 String16 unreachableOption("--unreachable");
2588 for (int i = 0; i < n; i++) {
2589 if (args[i] == verboseOption) {
2590 // change logging level
2591 if (i + 1 >= n) continue;
2592 String8 levelStr(args[i+1]);
2593 int level = atoi(levelStr.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002594 dprintf(fd, "\nSetting log level to %d.\n", level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002595 setLogLevel(level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002596 } else if (args[i] == unreachableOption) {
2597 // Dump memory analysis
2598 // TODO - should limit be an argument parameter?
2599 UnreachableMemoryInfo info;
2600 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2601 if (!success) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002602 dprintf(fd, "\n== Unable to dump unreachable memory. "
2603 "Try disabling SELinux enforcement. ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002604 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002605 dprintf(fd, "\n== Dumping unreachable memory: ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002606 std::string s = info.ToString(/*log_contents*/ true);
2607 write(fd, s.c_str(), s.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002608 }
2609 }
2610 }
2611 return NO_ERROR;
2612}
2613
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002614void CameraService::dumpEventLog(int fd) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002615 dprintf(fd, "\n== Camera service events log (most recent at top): ==\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002616
2617 Mutex::Autolock l(mLogLock);
2618 for (const auto& msg : mEventLog) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002619 dprintf(fd, " %s\n", msg.string());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002620 }
2621
2622 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002623 dprintf(fd, " ...\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002624 } else if (mEventLog.size() == 0) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002625 dprintf(fd, " [no events yet]\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002626 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002627 dprintf(fd, "\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002628}
2629
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002630void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002631 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002632 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2633 if (mTorchClientMap[i] == who) {
2634 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002635 String8 cameraId = mTorchClientMap.keyAt(i);
2636 status_t res = mFlashlight->setTorchMode(cameraId, false);
2637 if (res) {
2638 ALOGE("%s: torch client died but couldn't turn off torch: "
2639 "%s (%d)", __FUNCTION__, strerror(-res), res);
2640 return;
2641 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002642 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002643 break;
2644 }
2645 }
2646}
2647
Ruben Brunkcc776712015-02-17 20:18:47 -08002648/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002649
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002650 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002651 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2652 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002653 */
2654
Ruben Brunka8ca9152015-04-07 14:23:40 -07002655 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2656
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002657 // check torch client
2658 handleTorchClientBinderDied(who);
2659
2660 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002661 if(!evictClientIdByRemote(who)) {
2662 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002663 return;
2664 }
2665
Ruben Brunkcc776712015-02-17 20:18:47 -08002666 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2667 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002668}
2669
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002670void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002671 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002672}
2673
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002674void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2675 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002676 // Do not lock mServiceLock here or can get into a deadlock from
2677 // connect() -> disconnect -> updateStatus
2678
2679 auto state = getCameraState(cameraId);
2680
2681 if (state == nullptr) {
2682 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2683 cameraId.string());
2684 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002685 }
2686
Ruben Brunkcc776712015-02-17 20:18:47 -08002687 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2688 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2689 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002690 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002691
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002692 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002693 // Update torch status if it has a flash unit.
2694 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002695 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002696 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2697 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002698 TorchModeStatus newTorchStatus =
2699 status == StatusInternal::PRESENT ?
2700 TorchModeStatus::AVAILABLE_OFF :
2701 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002702 if (torchStatus != newTorchStatus) {
2703 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2704 }
2705 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002706 }
2707
2708 Mutex::Autolock lock(mStatusListenerLock);
2709
2710 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002711 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08002712 }
2713 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002714}
2715
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002716template<class Func>
2717void CameraService::CameraState::updateStatus(StatusInternal status,
2718 const String8& cameraId,
2719 std::initializer_list<StatusInternal> rejectSourceStates,
2720 Func onStatusUpdatedLocked) {
2721 Mutex::Autolock lock(mStatusLock);
2722 StatusInternal oldStatus = mStatus;
2723 mStatus = status;
2724
2725 if (oldStatus == status) {
2726 return;
2727 }
2728
2729 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
2730 cameraId.string(), oldStatus, status);
2731
2732 if (oldStatus == StatusInternal::NOT_PRESENT &&
2733 (status != StatusInternal::PRESENT &&
2734 status != StatusInternal::ENUMERATING)) {
2735
2736 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
2737 __FUNCTION__);
2738 mStatus = oldStatus;
2739 return;
2740 }
2741
2742 /**
2743 * Sometimes we want to conditionally do a transition.
2744 * For example if a client disconnects, we want to go to PRESENT
2745 * only if we weren't already in NOT_PRESENT or ENUMERATING.
2746 */
2747 for (auto& rejectStatus : rejectSourceStates) {
2748 if (oldStatus == rejectStatus) {
2749 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
2750 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
2751 mStatus = oldStatus;
2752 return;
2753 }
2754 }
2755
2756 onStatusUpdatedLocked(cameraId, status);
2757}
2758
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -07002759void CameraService::updateProxyDeviceState(int newState,
2760 const String8& cameraId, int facing, const String16& clientName) {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002761 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2762 if (proxyBinder == nullptr) return;
2763 String16 id(cameraId);
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -07002764 proxyBinder->notifyCameraState(id, newState, facing, clientName);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002765}
2766
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002767status_t CameraService::getTorchStatusLocked(
2768 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002769 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002770 if (!status) {
2771 return BAD_VALUE;
2772 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002773 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2774 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002775 // invalid camera ID or the camera doesn't have a flash unit
2776 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002777 }
2778
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002779 *status = mTorchStatusMap.valueAt(index);
2780 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002781}
2782
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002783status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002784 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002785 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2786 if (index == NAME_NOT_FOUND) {
2787 return BAD_VALUE;
2788 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002789 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002790
2791 return OK;
2792}
2793
Mathias Agopian65ab4712010-07-14 17:59:35 -07002794}; // namespace android