blob: c17525922c9efb5a1faf4dedb24b930bb614cf83 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
Ruben Brunkd1176ef2014-02-21 10:51:38 -08002 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mathias Agopian65ab4712010-07-14 17:59:35 -070016
17#define LOG_TAG "CameraService"
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -070018#define ATRACE_TAG ATRACE_TAG_CAMERA
Iliyan Malchev8951a972011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
Ruben Brunkcc776712015-02-17 20:18:47 -080021#include <algorithm>
22#include <climits>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <stdio.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080024#include <cstring>
25#include <ctime>
26#include <string>
Mathias Agopian65ab4712010-07-14 17:59:35 -070027#include <sys/types.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080028#include <inttypes.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070029#include <pthread.h>
30
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080031#include <android/hardware/ICamera.h>
32#include <android/hardware/ICameraClient.h>
33
Alex Deymo9c2a2c22016-08-25 11:59:14 -070034#include <android-base/macros.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080035#include <android-base/parseint.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080036#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryBase.h>
40#include <binder/MemoryHeapBase.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080041#include <binder/ProcessInfoService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070042#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070043#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080044#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include <hardware/hardware.h>
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -070046#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070047#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080048#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070050#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070051#include <utils/Errors.h>
52#include <utils/Log.h>
53#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080054#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080055#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080056#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070057#include <system/camera_metadata.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080058
Ruben Brunkb2119af2014-05-09 19:57:56 -070059#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070060
61#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070062#include "api1/CameraClient.h"
63#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070064#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070065#include "utils/CameraTraces.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070066
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080067namespace {
68 const char* kPermissionServiceName = "permission";
69}; // namespace anonymous
70
Mathias Agopian65ab4712010-07-14 17:59:35 -070071namespace android {
72
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080073using binder::Status;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080074using hardware::ICamera;
75using hardware::ICameraClient;
76using hardware::ICameraServiceListener;
77using hardware::camera::common::V1_0::CameraDeviceStatus;
78using hardware::camera::common::V1_0::TorchModeStatus;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080079
Mathias Agopian65ab4712010-07-14 17:59:35 -070080// ----------------------------------------------------------------------------
81// Logging support -- this is for debugging only
82// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070083volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070084
Steve Blockb8a80522011-12-20 16:23:08 +000085#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
86#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070087
88static void setLogLevel(int level) {
89 android_atomic_write(level, &gLogLevel);
90}
91
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080092// Convenience methods for constructing binder::Status objects for error returns
93
94#define STATUS_ERROR(errorCode, errorString) \
95 binder::Status::fromServiceSpecificError(errorCode, \
96 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
97
98#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
99 binder::Status::fromServiceSpecificError(errorCode, \
100 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
101 __VA_ARGS__))
102
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103// ----------------------------------------------------------------------------
104
Igor Murashkincba2c162013-03-20 15:56:31 -0700105extern "C" {
106static void camera_device_status_change(
107 const struct camera_module_callbacks* callbacks,
108 int camera_id,
109 int new_status) {
110 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -0800111 static_cast<const CameraService*>(callbacks));
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800112 String8 id = String8::format("%d", camera_id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700113
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800114 CameraDeviceStatus newStatus{CameraDeviceStatus::NOT_PRESENT};
115 switch (new_status) {
116 case CAMERA_DEVICE_STATUS_NOT_PRESENT:
117 newStatus = CameraDeviceStatus::NOT_PRESENT;
118 break;
119 case CAMERA_DEVICE_STATUS_PRESENT:
120 newStatus = CameraDeviceStatus::PRESENT;
121 break;
122 case CAMERA_DEVICE_STATUS_ENUMERATING:
123 newStatus = CameraDeviceStatus::ENUMERATING;
124 break;
125 default:
126 ALOGW("Unknown device status change to %d", new_status);
127 break;
128 }
129 cs->onDeviceStatusChanged(id, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700130}
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800131
132static void torch_mode_status_change(
133 const struct camera_module_callbacks* callbacks,
134 const char* camera_id,
135 int new_status) {
136 if (!callbacks || !camera_id) {
137 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
138 callbacks, camera_id);
139 }
140 sp<CameraService> cs = const_cast<CameraService*>(
141 static_cast<const CameraService*>(callbacks));
142
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800143 TorchModeStatus status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800144 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800145 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800146 status = TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800147 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800148 case TORCH_MODE_STATUS_AVAILABLE_OFF:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800149 status = TorchModeStatus::AVAILABLE_OFF;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800150 break;
151 case TORCH_MODE_STATUS_AVAILABLE_ON:
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800152 status = TorchModeStatus::AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800153 break;
154 default:
155 ALOGE("Unknown torch status %d", new_status);
156 return;
157 }
158
159 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800160 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800161 status);
162}
Igor Murashkincba2c162013-03-20 15:56:31 -0700163} // extern "C"
164
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165// ----------------------------------------------------------------------------
166
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800167CameraService::CameraService() :
168 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800169 mNumberOfCameras(0), mNumberOfNormalCameras(0),
Emilian Peevf53f66e2017-04-11 14:29:43 +0100170 mSoundRef(0), mInitialized(false) {
Steve Blockdf64d152012-01-04 20:05:49 +0000171 ALOGI("CameraService started (pid=%d)", getpid());
Igor Murashkinbfc99152013-02-27 12:55:20 -0800172
Igor Murashkincba2c162013-03-20 15:56:31 -0700173 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800174 this->torch_mode_status_change = android::torch_mode_status_change;
175
Ruben Brunkcc776712015-02-17 20:18:47 -0800176 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700177}
178
Iliyan Malchev8951a972011-04-14 16:55:59 -0700179void CameraService::onFirstRef()
180{
Ruben Brunkcc776712015-02-17 20:18:47 -0800181 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800182
Iliyan Malchev8951a972011-04-14 16:55:59 -0700183 BnCameraService::onFirstRef();
184
Ruben Brunk99e69712015-05-26 17:25:07 -0700185 // Update battery life tracking if service is restarting
186 BatteryNotifier& notifier(BatteryNotifier::getInstance());
187 notifier.noteResetCamera();
188 notifier.noteResetFlashlight();
189
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800190 status_t res = INVALID_OPERATION;
Eino-Ville Talvala9cbbc832017-01-23 15:39:53 -0800191
Emilian Peevf53f66e2017-04-11 14:29:43 +0100192 res = enumerateProviders();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800193 if (res == OK) {
194 mInitialized = true;
195 }
196
197 CameraService::pingCameraServiceProxy();
198}
199
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800200status_t CameraService::enumerateProviders() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800201 status_t res;
Emilian Peevaee727d2017-05-04 16:35:48 +0100202 Mutex::Autolock l(mServiceLock);
203
204 if (nullptr == mCameraProviderManager.get()) {
205 mCameraProviderManager = new CameraProviderManager();
206 res = mCameraProviderManager->initialize(this);
207 if (res != OK) {
208 ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
209 __FUNCTION__, strerror(-res), res);
210 return res;
211 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800212 }
213
214 mNumberOfCameras = mCameraProviderManager->getCameraCount();
Emilian Peevf53f66e2017-04-11 14:29:43 +0100215 mNumberOfNormalCameras =
216 mCameraProviderManager->getAPI1CompatibleCameraCount();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800217
Yin-Chia Yeh067428c2017-01-13 15:19:24 -0800218 // Setup vendor tags before we call get_camera_info the first time
219 // because HAL might need to setup static vendor keys in get_camera_info
220 // TODO: maybe put this into CameraProviderManager::initialize()?
221 mCameraProviderManager->setUpVendorTags();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800222
Emilian Peevaee727d2017-05-04 16:35:48 +0100223 if (nullptr == mFlashlight.get()) {
224 mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
225 }
226
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800227 res = mFlashlight->findFlashUnits();
228 if (res != OK) {
229 ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
230 }
231
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800232 for (auto& cameraId : mCameraProviderManager->getCameraDeviceIds()) {
Emilian Peevaee727d2017-05-04 16:35:48 +0100233 String8 id8 = String8(cameraId.c_str());
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700234 bool cameraFound = false;
Emilian Peevaee727d2017-05-04 16:35:48 +0100235 {
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700236
Emilian Peevaee727d2017-05-04 16:35:48 +0100237 Mutex::Autolock lock(mCameraStatesLock);
238 auto iter = mCameraStates.find(id8);
239 if (iter != mCameraStates.end()) {
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700240 cameraFound = true;
Emilian Peevaee727d2017-05-04 16:35:48 +0100241 }
242 }
243
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700244 if (!cameraFound) {
245 hardware::camera::common::V1_0::CameraResourceCost cost;
246 res = mCameraProviderManager->getResourceCost(cameraId, &cost);
247 if (res != OK) {
248 ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res);
249 continue;
250 }
251 std::set<String8> conflicting;
252 for (size_t i = 0; i < cost.conflictingDevices.size(); i++) {
253 conflicting.emplace(String8(cost.conflictingDevices[i].c_str()));
254 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800255
Yin-Chia Yehfad32282017-06-27 19:04:48 -0700256 {
257 Mutex::Autolock lock(mCameraStatesLock);
258 mCameraStates.emplace(id8,
259 std::make_shared<CameraState>(id8, cost.resourceCost, conflicting));
260 }
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700261 }
262
263 onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800264
265 if (mFlashlight->hasFlashUnit(id8)) {
Emilian Peevaee727d2017-05-04 16:35:48 +0100266 mTorchStatusMap.add(id8, TorchModeStatus::AVAILABLE_OFF);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800267 }
268 }
269
270 return OK;
271}
272
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700273sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800274 sp<ICameraServiceProxy> proxyBinder = nullptr;
275#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700276 sp<IServiceManager> sm = defaultServiceManager();
Eino-Ville Talvalaf4db13d2016-12-08 11:20:51 -0800277 // Use checkService because cameraserver normally starts before the
278 // system server and the proxy service. So the long timeout that getService
279 // has before giving up is inappropriate.
280 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800281 if (binder != nullptr) {
282 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700283 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800284#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700285 return proxyBinder;
286}
287
288void CameraService::pingCameraServiceProxy() {
289 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
290 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700291 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700292}
293
Mathias Agopian65ab4712010-07-14 17:59:35 -0700294CameraService::~CameraService() {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800295 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296}
297
Emilian Peevaee727d2017-05-04 16:35:48 +0100298void CameraService::onNewProviderRegistered() {
299 enumerateProviders();
300}
301
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800302void CameraService::onDeviceStatusChanged(const String8& id,
303 CameraDeviceStatus newHalStatus) {
304 ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__,
305 id.string(), newHalStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700306
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800307 StatusInternal newStatus = mapToInternal(newHalStatus);
308
Ruben Brunkcc776712015-02-17 20:18:47 -0800309 std::shared_ptr<CameraState> state = getCameraState(id);
310
311 if (state == nullptr) {
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700312 if (newStatus == StatusInternal::PRESENT) {
313 ALOGW("%s: Unknown camera ID %s, probably newly registered?",
314 __FUNCTION__, id.string());
315 } else {
316 ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string());
317 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700318 return;
319 }
320
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800321 StatusInternal oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800322
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800323 if (oldStatus == newStatus) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800324 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700325 return;
326 }
327
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800328 if (newStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700329 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
330 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800331 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700332 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800333 // Don't do this in updateStatus to avoid deadlock over mServiceLock
334 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700335
Ruben Brunkcc776712015-02-17 20:18:47 -0800336 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
337 // to this device until the status changes
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800338 updateStatus(StatusInternal::NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700339
Ruben Brunkcc776712015-02-17 20:18:47 -0800340 // Remove cached shim parameters
341 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700342
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700343 // Remove the client from the list of active clients, if there is one
Ruben Brunkcc776712015-02-17 20:18:47 -0800344 clientToDisconnect = removeClientLocked(id);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700345 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800346
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700347 // Disconnect client
348 if (clientToDisconnect.get() != nullptr) {
349 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
350 __FUNCTION__, id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800351 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800352 clientToDisconnect->notifyError(
353 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800354 CaptureResultExtras{});
Ruben Brunkcc776712015-02-17 20:18:47 -0800355 // Ensure not in binder RPC so client disconnect PID checks work correctly
356 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
357 "onDeviceStatusChanged must be called from the camera service process!");
358 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700359 }
360
Ruben Brunkcc776712015-02-17 20:18:47 -0800361 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800362 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700363 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
364 newStatus));
365 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800366 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700367 }
368
Igor Murashkincba2c162013-03-20 15:56:31 -0700369}
370
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800371void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800372 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800373 Mutex::Autolock al(mTorchStatusMutex);
374 onTorchStatusChangedLocked(cameraId, newStatus);
375}
376
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800377void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800378 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800379 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
380 __FUNCTION__, cameraId.string(), newStatus);
381
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800382 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800383 status_t res = getTorchStatusLocked(cameraId, &status);
384 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700385 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
386 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800387 return;
388 }
389 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800390 return;
391 }
392
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800393 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800394 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800395 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
396 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800397 return;
398 }
399
Ruben Brunkcc776712015-02-17 20:18:47 -0800400 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700401 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700402 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700403 auto iter = mTorchUidMap.find(cameraId);
404 if (iter != mTorchUidMap.end()) {
405 int oldUid = iter->second.second;
406 int newUid = iter->second.first;
407 BatteryNotifier& notifier(BatteryNotifier::getInstance());
408 if (oldUid != newUid) {
409 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800410 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700411 notifier.noteFlashlightOff(cameraId, oldUid);
412 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800413 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700414 notifier.noteFlashlightOn(cameraId, newUid);
415 }
416 iter->second.second = newUid;
417 } else {
418 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800419 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700420 notifier.noteFlashlightOn(cameraId, oldUid);
421 } else {
422 notifier.noteFlashlightOff(cameraId, oldUid);
423 }
424 }
425 }
426 }
427
428 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800429 Mutex::Autolock lock(mStatusListenerLock);
430 for (auto& i : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800431 i->onTorchStatusChanged(mapToInterface(newStatus), String16{cameraId});
Ruben Brunkcc776712015-02-17 20:18:47 -0800432 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800433 }
434}
435
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800436Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700437 ATRACE_CALL();
Emilian Peevaee727d2017-05-04 16:35:48 +0100438 Mutex::Autolock l(mServiceLock);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700439 switch (type) {
440 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800441 *numCameras = mNumberOfNormalCameras;
442 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700443 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800444 *numCameras = mNumberOfCameras;
445 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700446 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800447 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700448 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800449 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
450 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700451 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800452 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700453}
454
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800455Status CameraService::getCameraInfo(int cameraId,
456 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700457 ATRACE_CALL();
Emilian Peevaee727d2017-05-04 16:35:48 +0100458 Mutex::Autolock l(mServiceLock);
459
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800460 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800461 return STATUS_ERROR(ERROR_DISCONNECTED,
462 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700463 }
464
Mathias Agopian65ab4712010-07-14 17:59:35 -0700465 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800466 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
467 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 }
469
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800470 Status ret = Status::ok();
Emilian Peevf53f66e2017-04-11 14:29:43 +0100471 status_t err = mCameraProviderManager->getCameraInfo(std::to_string(cameraId), cameraInfo);
472 if (err != OK) {
473 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
474 "Error retrieving camera info from device %d: %s (%d)", cameraId,
475 strerror(-err), err);
Ruben Brunkcc776712015-02-17 20:18:47 -0800476 }
Emilian Peevf53f66e2017-04-11 14:29:43 +0100477
Ruben Brunkcc776712015-02-17 20:18:47 -0800478 return ret;
479}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700480
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800481int CameraService::cameraIdToInt(const String8& cameraId) {
482 int id;
483 bool success = base::ParseInt(cameraId.string(), &id, 0);
484 if (!success) {
485 return -1;
486 }
487 return id;
488}
489
490Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800491 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700492 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700493 if (!cameraInfo) {
494 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800495 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700496 }
497
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800498 if (!mInitialized) {
499 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800500 return STATUS_ERROR(ERROR_DISCONNECTED,
501 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700502 }
503
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800504 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800505
Emilian Peevf53f66e2017-04-11 14:29:43 +0100506 status_t res = mCameraProviderManager->getCameraCharacteristics(
507 String8(cameraId).string(), cameraInfo);
508 if (res != OK) {
509 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
510 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
511 strerror(-res), res);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700512 }
Zhijun He2b59be82013-09-25 10:14:30 -0700513
514 return ret;
515}
516
Ruben Brunkcc776712015-02-17 20:18:47 -0800517int CameraService::getCallingPid() {
518 return IPCThreadState::self()->getCallingPid();
519}
520
521int CameraService::getCallingUid() {
522 return IPCThreadState::self()->getCallingUid();
523}
524
525String8 CameraService::getFormattedCurrentTime() {
526 time_t now = time(nullptr);
527 char formattedTime[64];
528 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
529 return String8(formattedTime);
530}
531
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800532Status CameraService::getCameraVendorTagDescriptor(
533 /*out*/
534 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700535 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800536 if (!mInitialized) {
537 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800538 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800539 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800540 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
541 if (globalDescriptor != nullptr) {
542 *desc = *(globalDescriptor.get());
543 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800544 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800545}
546
Emilian Peev71c73a22017-03-21 16:35:51 +0000547Status CameraService::getCameraVendorTagCache(
548 /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) {
549 ATRACE_CALL();
550 if (!mInitialized) {
551 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
552 return STATUS_ERROR(ERROR_DISCONNECTED,
553 "Camera subsystem not available");
554 }
555 sp<VendorTagDescriptorCache> globalCache =
556 VendorTagDescriptorCache::getGlobalVendorTagCache();
557 if (globalCache != nullptr) {
558 *cache = *(globalCache.get());
559 }
560 return Status::ok();
561}
562
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800563int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700564 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800565
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800566 int deviceVersion = 0;
567
Emilian Peevf53f66e2017-04-11 14:29:43 +0100568 status_t res;
569 hardware::hidl_version maxVersion{0,0};
570 res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(),
571 &maxVersion);
572 if (res != OK) return -1;
573 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800574
Emilian Peevf53f66e2017-04-11 14:29:43 +0100575 hardware::CameraInfo info;
576 if (facing) {
577 res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800578 if (res != OK) return -1;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100579 *facing = info.facing;
Igor Murashkin634a5152013-02-20 17:15:11 -0800580 }
Emilian Peevf53f66e2017-04-11 14:29:43 +0100581
Igor Murashkin634a5152013-02-20 17:15:11 -0800582 return deviceVersion;
583}
584
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800585Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700586 switch(err) {
587 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800589 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800590 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
591 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800592 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800593 return STATUS_ERROR(ERROR_DISCONNECTED,
594 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700595 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800596 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
597 "Camera HAL encountered error %d: %s",
598 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700599 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800600}
601
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800602Status CameraService::makeClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800603 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800604 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
605 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
606 /*out*/sp<BasicClient>* client) {
607
Ruben Brunkcc776712015-02-17 20:18:47 -0800608 if (halVersion < 0 || halVersion == deviceVersion) {
609 // Default path: HAL version is unspecified by caller, create CameraClient
610 // based on device version reported by the HAL.
611 switch(deviceVersion) {
612 case CAMERA_DEVICE_API_VERSION_1_0:
613 if (effectiveApiLevel == API_1) { // Camera1 API route
614 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800615 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
616 facing, clientPid, clientUid, getpid(), legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800617 } else { // Camera2 API route
618 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800619 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800620 "Camera device \"%s\" HAL version %d does not support camera2 API",
621 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800622 }
623 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800624 case CAMERA_DEVICE_API_VERSION_3_0:
625 case CAMERA_DEVICE_API_VERSION_3_1:
626 case CAMERA_DEVICE_API_VERSION_3_2:
627 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700628 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800629 if (effectiveApiLevel == API_1) { // Camera1 API route
630 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800631 *client = new Camera2Client(cameraService, tmp, packageName, cameraIdToInt(cameraId),
632 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800633 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800634 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
635 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
636 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800637 facing, clientPid, clientUid, servicePid);
638 }
639 break;
640 default:
641 // Should not be reachable
642 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800643 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800644 "Camera device \"%s\" has unknown HAL version %d",
645 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800646 }
647 } else {
648 // A particular HAL version is requested by caller. Create CameraClient
649 // based on the requested HAL version.
650 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
651 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
652 // Only support higher HAL version device opened as HAL1.0 device.
653 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800654 *client = new CameraClient(cameraService, tmp, packageName, cameraIdToInt(cameraId),
655 facing, clientPid, clientUid, servicePid, legacyMode);
Ruben Brunkcc776712015-02-17 20:18:47 -0800656 } else {
657 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
658 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
659 " opened as HAL %x device", halVersion, deviceVersion,
660 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800661 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800662 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
663 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800664 }
665 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800666 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800667}
668
Ruben Brunk6267b532015-04-30 17:44:07 -0700669String8 CameraService::toString(std::set<userid_t> intSet) {
670 String8 s("");
671 bool first = true;
672 for (userid_t i : intSet) {
673 if (first) {
674 s.appendFormat("%d", i);
675 first = false;
676 } else {
677 s.appendFormat(", %d", i);
678 }
679 }
680 return s;
681}
682
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800683int32_t CameraService::mapToInterface(TorchModeStatus status) {
684 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
685 switch (status) {
686 case TorchModeStatus::NOT_AVAILABLE:
687 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
688 break;
689 case TorchModeStatus::AVAILABLE_OFF:
690 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
691 break;
692 case TorchModeStatus::AVAILABLE_ON:
693 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
694 break;
695 default:
696 ALOGW("Unknown new flash status: %d", status);
697 }
698 return serviceStatus;
699}
700
701CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
702 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
703 switch (status) {
704 case CameraDeviceStatus::NOT_PRESENT:
705 serviceStatus = StatusInternal::NOT_PRESENT;
706 break;
707 case CameraDeviceStatus::PRESENT:
708 serviceStatus = StatusInternal::PRESENT;
709 break;
710 case CameraDeviceStatus::ENUMERATING:
711 serviceStatus = StatusInternal::ENUMERATING;
712 break;
713 default:
714 ALOGW("Unknown new HAL device status: %d", status);
715 }
716 return serviceStatus;
717}
718
719int32_t CameraService::mapToInterface(StatusInternal status) {
720 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
721 switch (status) {
722 case StatusInternal::NOT_PRESENT:
723 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
724 break;
725 case StatusInternal::PRESENT:
726 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
727 break;
728 case StatusInternal::ENUMERATING:
729 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
730 break;
731 case StatusInternal::NOT_AVAILABLE:
732 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
733 break;
734 case StatusInternal::UNKNOWN:
735 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
736 break;
737 default:
738 ALOGW("Unknown new internal device status: %d", status);
739 }
740 return serviceStatus;
741}
742
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800743Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800744 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700745
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800746 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800747 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800748 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800749 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800750 if (!(ret = connectHelper<ICameraClient,Client>(
751 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
752 internalPackageName, uid, USE_CALLING_PID,
753 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
754 /*out*/ tmp)
755 ).isOk()) {
756 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700757 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800758 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700759}
760
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800761Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700762 /*out*/
763 CameraParameters* parameters) {
764
765 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
766
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800767 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700768
769 if (parameters == NULL) {
770 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800771 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700772 }
773
Ruben Brunkcc776712015-02-17 20:18:47 -0800774 String8 id = String8::format("%d", cameraId);
775
776 // Check if we already have parameters
777 {
778 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700779 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800780 auto cameraState = getCameraState(id);
781 if (cameraState == nullptr) {
782 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800783 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
784 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800785 }
786 CameraParameters p = cameraState->getShimParams();
787 if (!p.isEmpty()) {
788 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800789 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700790 }
791 }
792
Ruben Brunkcc776712015-02-17 20:18:47 -0800793 int64_t token = IPCThreadState::self()->clearCallingIdentity();
794 ret = initializeShimMetadata(cameraId);
795 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800796 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800797 // Error already logged by callee
798 return ret;
799 }
800
801 // Check for parameters again
802 {
803 // Scope for service lock
804 Mutex::Autolock lock(mServiceLock);
805 auto cameraState = getCameraState(id);
806 if (cameraState == nullptr) {
807 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800808 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
809 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700810 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800811 CameraParameters p = cameraState->getShimParams();
812 if (!p.isEmpty()) {
813 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800814 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700815 }
816 }
817
Ruben Brunkcc776712015-02-17 20:18:47 -0800818 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
819 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800820 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700821}
822
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800823// Can camera service trust the caller based on the calling UID?
824static bool isTrustedCallingUid(uid_t uid) {
825 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700826 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800827 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700828 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800829 return true;
830 default:
831 return false;
832 }
833}
834
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800835Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700836 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
837 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500838
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700839#ifdef __BRILLO__
840 UNUSED(clientName8);
841 UNUSED(clientUid);
842 UNUSED(clientPid);
843 UNUSED(originalClientPid);
844#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700845 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
846 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800847 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -0800848 return allowed;
849 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700850#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -0800851
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800852 int callingPid = getCallingPid();
853
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800854 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800855 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
856 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800857 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
858 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700859 }
860
Ruben Brunkcc776712015-02-17 20:18:47 -0800861 if (getCameraState(cameraId) == nullptr) {
862 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
863 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800864 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
865 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866 }
867
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800868 status_t err = checkIfDeviceIsUsable(cameraId);
869 if (err != NO_ERROR) {
870 switch(err) {
871 case -ENODEV:
872 case -EBUSY:
873 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
874 "No camera device with ID \"%s\" currently available", cameraId.string());
875 default:
876 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
877 "Unknown error connecting to ID \"%s\"", cameraId.string());
878 }
879 }
880 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800881}
882
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800883Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700884 const String8& clientName8, int& clientUid, int& clientPid,
885 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800886 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800887 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800889 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800891 clientUid = callingUid;
892 } else if (!isTrustedCallingUid(callingUid)) {
893 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
894 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800895 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
896 "Untrusted caller (calling PID %d, UID %d) trying to "
897 "forward camera access to camera %s for client %s (PID %d, UID %d)",
898 callingPid, callingUid, cameraId.string(),
899 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700900 }
901
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800902 // Check if we can trust clientPid
903 if (clientPid == USE_CALLING_PID) {
904 clientPid = callingPid;
905 } else if (!isTrustedCallingUid(callingUid)) {
906 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
907 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800908 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
909 "Untrusted caller (calling PID %d, UID %d) trying to "
910 "forward camera access to camera %s for client %s (PID %d, UID %d)",
911 callingPid, callingUid, cameraId.string(),
912 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800913 }
914
915 // If it's not calling from cameraserver, check the permission.
916 if (callingPid != getpid() &&
917 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
918 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800919 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
920 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
921 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800922 }
923
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -0700924 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
925 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700926 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -0700927 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700928
Ruben Brunk6267b532015-04-30 17:44:07 -0700929 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800930
Ruben Brunka8ca9152015-04-07 14:23:40 -0700931 // Only allow clients who are being used by the current foreground device user, unless calling
932 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -0700933 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
934 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
935 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
936 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800937 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
938 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
939 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -0700940 }
941
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800942 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800943}
944
945status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
946 auto cameraState = getCameraState(cameraId);
947 int callingPid = getCallingPid();
948 if (cameraState == nullptr) {
949 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
950 cameraId.string());
951 return -ENODEV;
952 }
953
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800954 StatusInternal currentStatus = cameraState->getStatus();
955 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800956 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
957 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700958 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800959 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800960 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
961 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700962 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -0700963 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700964
Ruben Brunkcc776712015-02-17 20:18:47 -0800965 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800966}
967
Ruben Brunkcc776712015-02-17 20:18:47 -0800968void CameraService::finishConnectLocked(const sp<BasicClient>& client,
969 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800970
Ruben Brunkcc776712015-02-17 20:18:47 -0800971 // Make a descriptor for the incoming client
972 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
973 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
974
975 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
976 String8(client->getPackageName()));
977
978 if (evicted.size() > 0) {
979 // This should never happen - clients should already have been removed in disconnect
980 for (auto& i : evicted) {
981 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
982 __FUNCTION__, i->getKey().string());
983 }
984
985 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
986 __FUNCTION__);
987 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -0700988
989 // And register a death notification for the client callback. Do
990 // this last to avoid Binder policy where a nested Binder
991 // transaction might be pre-empted to service the client death
992 // notification if the client process dies before linkToDeath is
993 // invoked.
994 sp<IBinder> remoteCallback = client->getRemote();
995 if (remoteCallback != nullptr) {
996 remoteCallback->linkToDeath(this);
997 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800998}
999
1000status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1001 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1002 /*out*/
1003 sp<BasicClient>* client,
1004 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001005 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001006 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001007 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001008 DescriptorPtr clientDescriptor;
1009 {
1010 if (effectiveApiLevel == API_1) {
1011 // If we are using API1, any existing client for this camera ID with the same remote
1012 // should be returned rather than evicted to allow MediaRecorder to work properly.
1013
1014 auto current = mActiveClientManager.get(cameraId);
1015 if (current != nullptr) {
1016 auto clientSp = current->getValue();
1017 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001018 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1019 ALOGW("CameraService connect called from same client, but with a different"
1020 " API level, evicting prior client...");
1021 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001022 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001023 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001024 *client = clientSp;
1025 return NO_ERROR;
1026 }
1027 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001028 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001029 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001030
Ruben Brunkcc776712015-02-17 20:18:47 -08001031 // Get current active client PIDs
1032 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1033 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001034
Emilian Peev8131a262017-02-01 12:33:43 +00001035 std::vector<int> priorityScores(ownerPids.size());
1036 std::vector<int> states(ownerPids.size());
Igor Murashkine6800ce2013-03-04 17:25:57 -08001037
Emilian Peev8131a262017-02-01 12:33:43 +00001038 // Get priority scores of all active PIDs
1039 status_t err = ProcessInfoService::getProcessStatesScoresFromPids(
1040 ownerPids.size(), &ownerPids[0], /*out*/&states[0],
1041 /*out*/&priorityScores[0]);
1042 if (err != OK) {
1043 ALOGE("%s: Priority score query failed: %d",
1044 __FUNCTION__, err);
1045 return err;
1046 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001047
Ruben Brunkcc776712015-02-17 20:18:47 -08001048 // Update all active clients' priorities
Emilian Peev8131a262017-02-01 12:33:43 +00001049 std::map<int,resource_policy::ClientPriority> pidToPriorityMap;
Ruben Brunkcc776712015-02-17 20:18:47 -08001050 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
Emilian Peev8131a262017-02-01 12:33:43 +00001051 pidToPriorityMap.emplace(ownerPids[i],
1052 resource_policy::ClientPriority(priorityScores[i], states[i]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001053 }
1054 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001055
Ruben Brunkcc776712015-02-17 20:18:47 -08001056 // Get state for the given cameraId
1057 auto state = getCameraState(cameraId);
1058 if (state == nullptr) {
1059 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1060 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001061 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001062 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001063 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001064
1065 // Make descriptor for incoming client
1066 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1067 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1068 state->getConflicting(),
Emilian Peev8131a262017-02-01 12:33:43 +00001069 priorityScores[priorityScores.size() - 1],
1070 clientPid,
1071 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001072
1073 // Find clients that would be evicted
1074 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1075
1076 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1077 // background, so we cannot do evictions
1078 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1079 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1080 " priority).", clientPid);
1081
1082 sp<BasicClient> clientSp = clientDescriptor->getValue();
1083 String8 curTime = getFormattedCurrentTime();
1084 auto incompatibleClients =
1085 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1086
1087 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Emilian Peev8131a262017-02-01 12:33:43 +00001088 "(PID %d, score %d state %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001089 cameraId.string(), packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001090 priorityScores[priorityScores.size() - 1],
1091 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001092
1093 for (auto& i : incompatibleClients) {
1094 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
Emilian Peev8131a262017-02-01 12:33:43 +00001095 "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")",
1096 i->getKey().string(),
1097 String8{i->getValue()->getPackageName()}.string(),
1098 i->getOwnerId(), i->getPriority().getScore(),
1099 i->getPriority().getState());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001100 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001101 PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(),
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001102 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
Emilian Peev8131a262017-02-01 12:33:43 +00001103 i->getPriority().getScore(), i->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08001104 }
1105
1106 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001107 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001108 mEventLog.add(msg);
1109
1110 return -EBUSY;
1111 }
1112
1113 for (auto& i : evicted) {
1114 sp<BasicClient> clientSp = i->getValue();
1115 if (clientSp.get() == nullptr) {
1116 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1117
1118 // TODO: Remove this
1119 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1120 __FUNCTION__);
1121 mActiveClientManager.remove(i);
1122 continue;
1123 }
1124
1125 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1126 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001127 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001128
Ruben Brunkcc776712015-02-17 20:18:47 -08001129 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001130 logEvent(String8::format("EVICT device %s client held by package %s (PID"
Emilian Peev8131a262017-02-01 12:33:43 +00001131 " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for"
1132 " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001133 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
Emilian Peev8131a262017-02-01 12:33:43 +00001134 i->getOwnerId(), i->getPriority().getScore(),
1135 i->getPriority().getState(), cameraId.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001136 packageName.string(), clientPid,
Emilian Peev8131a262017-02-01 12:33:43 +00001137 priorityScores[priorityScores.size() - 1],
1138 states[states.size() - 1]));
Ruben Brunkcc776712015-02-17 20:18:47 -08001139
1140 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001141 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001142 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001143 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001144 }
1145
Ruben Brunkcc776712015-02-17 20:18:47 -08001146 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1147 // other clients from connecting in mServiceLockWrapper if held
1148 mServiceLock.unlock();
1149
1150 // Clear caller identity temporarily so client disconnect PID checks work correctly
1151 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1152
1153 // Destroy evicted clients
1154 for (auto& i : evictedClients) {
1155 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001156 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001157 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001158
1159 IPCThreadState::self()->restoreCallingIdentity(token);
1160
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001161 for (const auto& i : evictedClients) {
1162 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1163 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1164 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1165 if (ret == TIMED_OUT) {
1166 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1167 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1168 mActiveClientManager.toString().string());
1169 return -EBUSY;
1170 }
1171 if (ret != NO_ERROR) {
1172 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1173 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1174 ret, mActiveClientManager.toString().string());
1175 return ret;
1176 }
1177 }
1178
1179 evictedClients.clear();
1180
Ruben Brunkcc776712015-02-17 20:18:47 -08001181 // Once clients have been disconnected, relock
1182 mServiceLock.lock();
1183
1184 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1185 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1186 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001187 }
1188
Ruben Brunkcc776712015-02-17 20:18:47 -08001189 *partial = clientDescriptor;
1190 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001191}
1192
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001193Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001194 const sp<ICameraClient>& cameraClient,
1195 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001196 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001197 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001198 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001199 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001200 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001201
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001202 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001204 String8 id = String8::format("%d", cameraId);
1205 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001206 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1207 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1208 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1209 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001210
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001211 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001212 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001213 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001214 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001215 }
1216
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001217 *device = client;
1218 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001219}
1220
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001221Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001222 const sp<ICameraClient>& cameraClient,
1223 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001224 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001225 int clientUid,
1226 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001227 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001228
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001229 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001230 String8 id = String8::format("%d", cameraId);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001231
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001232 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001233 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001234 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1235 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1236 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1237 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001238
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001239 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001240 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001241 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001242 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001243 }
1244
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001245 *device = client;
1246 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001247}
1248
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001249Status CameraService::connectDevice(
1250 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001251 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001252 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001253 int clientUid,
1254 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001255 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001256
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001257 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001258 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001259 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001260 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001261 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1262 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1263 clientUid, USE_CALLING_PID, API_2,
1264 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1265 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001266
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001267 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001268 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001269 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001270 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001271 }
1272
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001273 *device = client;
1274 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001275}
1276
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001277template<class CALLBACK, class CLIENT>
1278Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
1279 int halVersion, const String16& clientPackageName, int clientUid, int clientPid,
1280 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
1281 /*out*/sp<CLIENT>& device) {
1282 binder::Status ret = binder::Status::ok();
1283
1284 String8 clientName8(clientPackageName);
1285
1286 int originalClientPid = 0;
1287
1288 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1289 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1290 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1291 static_cast<int>(effectiveApiLevel));
1292
1293 sp<CLIENT> client = nullptr;
1294 {
1295 // Acquire mServiceLock and prevent other clients from connecting
1296 std::unique_ptr<AutoConditionLock> lock =
1297 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1298
1299 if (lock == nullptr) {
1300 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1301 , clientPid);
1302 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1303 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1304 cameraId.string(), clientName8.string(), clientPid);
1305 }
1306
1307 // Enforce client permissions and do basic sanity checks
1308 if(!(ret = validateConnectLocked(cameraId, clientName8,
1309 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1310 return ret;
1311 }
1312
1313 // Check the shim parameters after acquiring lock, if they have already been updated and
1314 // we were doing a shim update, return immediately
1315 if (shimUpdateOnly) {
1316 auto cameraState = getCameraState(cameraId);
1317 if (cameraState != nullptr) {
1318 if (!cameraState->getShimParams().isEmpty()) return ret;
1319 }
1320 }
1321
1322 status_t err;
1323
1324 sp<BasicClient> clientTmp = nullptr;
1325 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1326 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1327 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1328 /*out*/&partial)) != NO_ERROR) {
1329 switch (err) {
1330 case -ENODEV:
1331 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1332 "No camera device with ID \"%s\" currently available",
1333 cameraId.string());
1334 case -EBUSY:
1335 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1336 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1337 cameraId.string());
1338 default:
1339 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1340 "Unexpected error %s (%d) opening camera \"%s\"",
1341 strerror(-err), err, cameraId.string());
1342 }
1343 }
1344
1345 if (clientTmp.get() != nullptr) {
1346 // Handle special case for API1 MediaRecorder where the existing client is returned
1347 device = static_cast<CLIENT*>(clientTmp.get());
1348 return ret;
1349 }
1350
1351 // give flashlight a chance to close devices if necessary.
1352 mFlashlight->prepareDeviceOpen(cameraId);
1353
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001354 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001355 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -08001356 if (facing == -1) {
1357 ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string());
1358 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1359 "Unable to get camera device \"%s\" facing", cameraId.string());
1360 }
1361
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001362 sp<BasicClient> tmp = nullptr;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001363 if(!(ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001364 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
1365 /*out*/&tmp)).isOk()) {
1366 return ret;
1367 }
1368 client = static_cast<CLIENT*>(tmp.get());
1369
1370 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1371 __FUNCTION__);
1372
Emilian Peevf53f66e2017-04-11 14:29:43 +01001373 err = client->initialize(mCameraProviderManager);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001374 if (err != OK) {
1375 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001376 // Errors could be from the HAL module open call or from AppOpsManager
1377 switch(err) {
1378 case BAD_VALUE:
1379 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1380 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1381 case -EBUSY:
1382 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1383 "Camera \"%s\" is already open", cameraId.string());
1384 case -EUSERS:
1385 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1386 "Too many cameras already open, cannot open camera \"%s\"",
1387 cameraId.string());
1388 case PERMISSION_DENIED:
1389 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1390 "No permission to open camera \"%s\"", cameraId.string());
1391 case -EACCES:
1392 return STATUS_ERROR_FMT(ERROR_DISABLED,
1393 "Camera \"%s\" disabled by policy", cameraId.string());
1394 case -ENODEV:
1395 default:
1396 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1397 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1398 strerror(-err), err);
1399 }
1400 }
1401
1402 // Update shim paremeters for legacy clients
1403 if (effectiveApiLevel == API_1) {
1404 // Assume we have always received a Client subclass for API1
1405 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1406 String8 rawParams = shimClient->getParameters();
1407 CameraParameters params(rawParams);
1408
1409 auto cameraState = getCameraState(cameraId);
1410 if (cameraState != nullptr) {
1411 cameraState->setShimParams(params);
1412 } else {
1413 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1414 __FUNCTION__, cameraId.string());
1415 }
1416 }
1417
1418 if (shimUpdateOnly) {
1419 // If only updating legacy shim parameters, immediately disconnect client
1420 mServiceLock.unlock();
1421 client->disconnect();
1422 mServiceLock.lock();
1423 } else {
1424 // Otherwise, add client to active clients list
1425 finishConnectLocked(client, partial);
1426 }
1427 } // lock is destroyed, allow further connect calls
1428
1429 // Important: release the mutex here so the client can call back into the service from its
1430 // destructor (can be at the end of the call)
1431 device = client;
1432 return ret;
1433}
1434
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001435Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001436 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001437 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001438
1439 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001440 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001441 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1443 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001444 }
1445
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001446 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001447 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001448
1449 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001450 auto state = getCameraState(id);
1451 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001452 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001453 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1454 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001455 }
1456
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001457 StatusInternal cameraStatus = state->getStatus();
1458 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001459 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1460 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001461 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1462 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001463 }
1464
1465 {
1466 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001467 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001468 status_t err = getTorchStatusLocked(id, &status);
1469 if (err != OK) {
1470 if (err == NAME_NOT_FOUND) {
1471 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1472 "Camera \"%s\" does not have a flash unit", id.string());
1473 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001474 ALOGE("%s: getting current torch status failed for camera %s",
1475 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001476 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1477 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1478 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001479 }
1480
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001481 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001482 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001483 ALOGE("%s: torch mode of camera %s is not available because "
1484 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001485 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1486 "Torch for camera \"%s\" is not available due to an existing camera user",
1487 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001488 } else {
1489 ALOGE("%s: torch mode of camera %s is not available due to "
1490 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001491 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1492 "Torch for camera \"%s\" is not available due to insufficient resources",
1493 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001494 }
1495 }
1496 }
1497
Ruben Brunk99e69712015-05-26 17:25:07 -07001498 {
1499 // Update UID map - this is used in the torch status changed callbacks, so must be done
1500 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001501 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001502 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1503 mTorchUidMap[id].first = uid;
1504 mTorchUidMap[id].second = uid;
1505 } else {
1506 // Set the pending UID
1507 mTorchUidMap[id].first = uid;
1508 }
1509 }
1510
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001511 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001512
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001513 if (err != OK) {
1514 int32_t errorCode;
1515 String8 msg;
1516 switch (err) {
1517 case -ENOSYS:
1518 msg = String8::format("Camera \"%s\" has no flashlight",
1519 id.string());
1520 errorCode = ERROR_ILLEGAL_ARGUMENT;
1521 break;
1522 default:
1523 msg = String8::format(
1524 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1525 id.string(), enabled, strerror(-err), err);
1526 errorCode = ERROR_INVALID_OPERATION;
1527 }
1528 ALOGE("%s: %s", __FUNCTION__, msg.string());
1529 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001530 }
1531
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001532 {
1533 // update the link to client's death
1534 Mutex::Autolock al(mTorchClientMapMutex);
1535 ssize_t index = mTorchClientMap.indexOfKey(id);
1536 if (enabled) {
1537 if (index == NAME_NOT_FOUND) {
1538 mTorchClientMap.add(id, clientBinder);
1539 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001540 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001541 mTorchClientMap.replaceValueAt(index, clientBinder);
1542 }
1543 clientBinder->linkToDeath(this);
1544 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001545 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001546 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001547 }
1548
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001549 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001550}
1551
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001552Status CameraService::notifySystemEvent(int32_t eventId,
1553 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001554 ATRACE_CALL();
1555
Ruben Brunk36597b22015-03-20 22:15:57 -07001556 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001557 case ICameraService::EVENT_USER_SWITCHED: {
1558 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001559 break;
1560 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001561 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001562 default: {
1563 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1564 eventId);
1565 break;
1566 }
1567 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001568 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001569}
1570
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001571Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1572 /*out*/
1573 std::vector<hardware::CameraStatus> *cameraStatuses) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001574 ATRACE_CALL();
1575
Igor Murashkinbfc99152013-02-27 12:55:20 -08001576 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001577
Ruben Brunk3450ba72015-06-16 11:00:37 -07001578 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001579 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001580 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001581 }
1582
Igor Murashkinbfc99152013-02-27 12:55:20 -08001583 Mutex::Autolock lock(mServiceLock);
1584
Ruben Brunkcc776712015-02-17 20:18:47 -08001585 {
1586 Mutex::Autolock lock(mStatusListenerLock);
1587 for (auto& it : mListenerList) {
1588 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1589 ALOGW("%s: Tried to add listener %p which was already subscribed",
1590 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001591 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001592 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001593 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001594
1595 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001596 }
1597
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001598 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001599 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001600 Mutex::Autolock lock(mCameraStatesLock);
1601 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001602 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001603 }
1604 }
1605
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001606 /*
1607 * Immediately signal current torch status to this listener only
1608 * This may be a subset of all the devices, so don't include it in the response directly
1609 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001610 {
1611 Mutex::Autolock al(mTorchStatusMutex);
1612 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001613 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001614 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001615 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001616 }
1617
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001618 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001619}
Ruben Brunkcc776712015-02-17 20:18:47 -08001620
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001621Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001622 ATRACE_CALL();
1623
Igor Murashkinbfc99152013-02-27 12:55:20 -08001624 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1625
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001626 if (listener == 0) {
1627 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001628 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001629 }
1630
Igor Murashkinbfc99152013-02-27 12:55:20 -08001631 Mutex::Autolock lock(mServiceLock);
1632
Ruben Brunkcc776712015-02-17 20:18:47 -08001633 {
1634 Mutex::Autolock lock(mStatusListenerLock);
1635 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1636 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1637 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001638 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001639 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001640 }
1641 }
1642
1643 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1644 __FUNCTION__, listener.get());
1645
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001646 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001647}
1648
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001649Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001650
1651 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001652 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1653
1654 if (parameters == NULL) {
1655 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001656 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001657 }
1658
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001659 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001660
1661 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001662 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001663 // Error logged by caller
1664 return ret;
1665 }
1666
1667 String8 shimParamsString8 = shimParams.flatten();
1668 String16 shimParamsString16 = String16(shimParamsString8);
1669
1670 *parameters = shimParamsString16;
1671
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001672 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001673}
1674
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001675Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
1676 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001677 ATRACE_CALL();
1678
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001679 const String8 id = String8(cameraId);
1680
1681 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001682
1683 switch (apiVersion) {
1684 case API_VERSION_1:
1685 case API_VERSION_2:
1686 break;
1687 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001688 String8 msg = String8::format("Unknown API version %d", apiVersion);
1689 ALOGE("%s: %s", __FUNCTION__, msg.string());
1690 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001691 }
1692
Emilian Peev28ad2ea2017-02-07 16:14:32 +00001693 int deviceVersion = getDeviceVersion(id);
Igor Murashkin65d14b92014-06-17 12:03:20 -07001694 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001695 case CAMERA_DEVICE_API_VERSION_1_0:
1696 case CAMERA_DEVICE_API_VERSION_3_0:
1697 case CAMERA_DEVICE_API_VERSION_3_1:
1698 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001699 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
1700 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001701 *isSupported = false;
1702 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001703 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
1704 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001705 *isSupported = true;
1706 }
1707 break;
1708 case CAMERA_DEVICE_API_VERSION_3_2:
1709 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001710 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001711 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
1712 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001713 *isSupported = true;
1714 break;
1715 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001716 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001717 ALOGE("%s: %s", __FUNCTION__, msg.string());
1718 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001719 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001720 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001721 String8 msg = String8::format("Unknown device version %x for device %s",
1722 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001723 ALOGE("%s: %s", __FUNCTION__, msg.string());
1724 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1725 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001726 }
1727
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001728 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001729}
1730
Ruben Brunkcc776712015-02-17 20:18:47 -08001731void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001732 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001733 for (auto& i : mActiveClientManager.getAll()) {
1734 auto clientSp = i->getValue();
1735 if (clientSp.get() == client) {
1736 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001737 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001738 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001739}
1740
Ruben Brunkcc776712015-02-17 20:18:47 -08001741bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1742 const int callingPid = getCallingPid();
1743 const int servicePid = getpid();
1744 bool ret = false;
1745 {
1746 // Acquire mServiceLock and prevent other clients from connecting
1747 std::unique_ptr<AutoConditionLock> lock =
1748 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001749
Igor Murashkin634a5152013-02-20 17:15:11 -08001750
Ruben Brunkcc776712015-02-17 20:18:47 -08001751 std::vector<sp<BasicClient>> evicted;
1752 for (auto& i : mActiveClientManager.getAll()) {
1753 auto clientSp = i->getValue();
1754 if (clientSp.get() == nullptr) {
1755 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1756 mActiveClientManager.remove(i);
1757 continue;
1758 }
1759 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1760 callingPid == clientSp->getClientPid())) {
1761 mActiveClientManager.remove(i);
1762 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001763
Ruben Brunkcc776712015-02-17 20:18:47 -08001764 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001765 clientSp->notifyError(
1766 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001767 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001768 }
1769 }
1770
Ruben Brunkcc776712015-02-17 20:18:47 -08001771 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1772 // other clients from connecting in mServiceLockWrapper if held
1773 mServiceLock.unlock();
1774
Ruben Brunk36597b22015-03-20 22:15:57 -07001775 // Do not clear caller identity, remote caller should be client proccess
1776
Ruben Brunkcc776712015-02-17 20:18:47 -08001777 for (auto& i : evicted) {
1778 if (i.get() != nullptr) {
1779 i->disconnect();
1780 ret = true;
1781 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001782 }
1783
Ruben Brunkcc776712015-02-17 20:18:47 -08001784 // Reacquire mServiceLock
1785 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001786
Ruben Brunkcc776712015-02-17 20:18:47 -08001787 } // lock is destroyed, allow further connect calls
1788
1789 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001790}
1791
Ruben Brunkcc776712015-02-17 20:18:47 -08001792std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1793 const String8& cameraId) const {
1794 std::shared_ptr<CameraState> state;
1795 {
1796 Mutex::Autolock lock(mCameraStatesLock);
1797 auto iter = mCameraStates.find(cameraId);
1798 if (iter != mCameraStates.end()) {
1799 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001800 }
1801 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001802 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001803}
1804
Ruben Brunkcc776712015-02-17 20:18:47 -08001805sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1806 // Remove from active clients list
1807 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1808 if (clientDescriptorPtr == nullptr) {
1809 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1810 cameraId.string());
1811 return sp<BasicClient>{nullptr};
1812 }
1813
1814 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001815}
1816
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001817void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001818 // Acquire mServiceLock and prevent other clients from connecting
1819 std::unique_ptr<AutoConditionLock> lock =
1820 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1821
Ruben Brunk6267b532015-04-30 17:44:07 -07001822 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001823 for (size_t i = 0; i < newUserIds.size(); i++) {
1824 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001825 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001826 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07001827 return;
1828 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001829 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001830 }
1831
Ruben Brunka8ca9152015-04-07 14:23:40 -07001832
Ruben Brunk6267b532015-04-30 17:44:07 -07001833 if (newAllowedUsers == mAllowedUsers) {
1834 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1835 return;
1836 }
1837
1838 logUserSwitch(mAllowedUsers, newAllowedUsers);
1839
1840 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001841
1842 // Current user has switched, evict all current clients.
1843 std::vector<sp<BasicClient>> evicted;
1844 for (auto& i : mActiveClientManager.getAll()) {
1845 auto clientSp = i->getValue();
1846
1847 if (clientSp.get() == nullptr) {
1848 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1849 continue;
1850 }
1851
Ruben Brunk6267b532015-04-30 17:44:07 -07001852 // Don't evict clients that are still allowed.
1853 uid_t clientUid = clientSp->getClientUid();
1854 userid_t clientUserId = multiuser_get_user_id(clientUid);
1855 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1856 continue;
1857 }
1858
Ruben Brunk36597b22015-03-20 22:15:57 -07001859 evicted.push_back(clientSp);
1860
1861 String8 curTime = getFormattedCurrentTime();
1862
1863 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1864 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001865
Ruben Brunk36597b22015-03-20 22:15:57 -07001866 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001867 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001868 PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due"
1869 " to user switch.", i->getKey().string(),
1870 String8{clientSp->getPackageName()}.string(),
1871 i->getOwnerId(), i->getPriority().getScore(),
1872 i->getPriority().getState()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001873
1874 }
1875
1876 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1877 // blocking other clients from connecting in mServiceLockWrapper if held.
1878 mServiceLock.unlock();
1879
1880 // Clear caller identity temporarily so client disconnect PID checks work correctly
1881 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1882
1883 for (auto& i : evicted) {
1884 i->disconnect();
1885 }
1886
1887 IPCThreadState::self()->restoreCallingIdentity(token);
1888
1889 // Reacquire mServiceLock
1890 mServiceLock.lock();
1891}
Ruben Brunkcc776712015-02-17 20:18:47 -08001892
Ruben Brunka8ca9152015-04-07 14:23:40 -07001893void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001894 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001895 Mutex::Autolock l(mLogLock);
1896 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001897}
1898
Ruben Brunka8ca9152015-04-07 14:23:40 -07001899void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001900 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001901 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001902 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001903 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001904}
1905
1906void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001907 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001908 // Log the clients evicted
1909 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001910 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001911}
1912
1913void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001914 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001915 // Log the client rejected
1916 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001917 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001918}
1919
Ruben Brunk6267b532015-04-30 17:44:07 -07001920void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1921 const std::set<userid_t>& newUserIds) {
1922 String8 newUsers = toString(newUserIds);
1923 String8 oldUsers = toString(oldUserIds);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001924 if (oldUsers.size() == 0) {
1925 oldUsers = "<None>";
1926 }
Ruben Brunka8ca9152015-04-07 14:23:40 -07001927 // Log the new and old users
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001928 logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s",
Ruben Brunk6267b532015-04-30 17:44:07 -07001929 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001930}
1931
1932void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1933 // Log the device removal
1934 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1935}
1936
1937void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1938 // Log the device removal
1939 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1940}
1941
1942void CameraService::logClientDied(int clientPid, const char* reason) {
1943 // Log the device removal
1944 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001945}
1946
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001947void CameraService::logServiceError(const char* msg, int errorCode) {
1948 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001949 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001950}
1951
Ruben Brunk36597b22015-03-20 22:15:57 -07001952status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1953 uint32_t flags) {
1954
1955 const int pid = getCallingPid();
1956 const int selfPid = getpid();
1957
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 // Permission checks
1959 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001960 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07001961 if (pid != selfPid) {
1962 // Ensure we're being called by system_server, or similar process with
1963 // permissions to notify the camera service about system events
1964 if (!checkCallingPermission(
1965 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1966 const int uid = getCallingUid();
1967 ALOGE("Permission Denial: cannot send updates to camera service about system"
1968 " events from pid=%d, uid=%d", pid, uid);
1969 return PERMISSION_DENIED;
1970 }
1971 }
1972 break;
1973 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974 }
1975
1976 return BnCameraService::onTransact(code, data, reply, flags);
1977}
1978
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979// We share the media players for shutter and recording sound for all clients.
1980// A reference count is kept to determine when we will actually release the
1981// media players.
1982
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001983MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001984 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001985 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001986 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987 mp->prepare();
1988 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001989 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990 return NULL;
1991 }
1992 return mp;
1993}
1994
1995void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001996 ATRACE_CALL();
1997
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998 Mutex::Autolock lock(mSoundLock);
1999 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2000 if (mSoundRef++) return;
2001
2002 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002003 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2004 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005}
2006
2007void CameraService::releaseSound() {
2008 Mutex::Autolock lock(mSoundLock);
2009 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2010 if (--mSoundRef) return;
2011
2012 for (int i = 0; i < NUM_SOUNDS; i++) {
2013 if (mSoundPlayer[i] != 0) {
2014 mSoundPlayer[i]->disconnect();
2015 mSoundPlayer[i].clear();
2016 }
2017 }
2018}
2019
2020void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002021 ATRACE_CALL();
2022
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023 LOG1("playSound(%d)", kind);
2024 Mutex::Autolock lock(mSoundLock);
2025 sp<MediaPlayer> player = mSoundPlayer[kind];
2026 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002027 player->seekTo(0);
2028 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029 }
2030}
2031
2032// ----------------------------------------------------------------------------
2033
2034CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002035 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002036 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002037 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002038 int clientPid, uid_t clientUid,
2039 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002040 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002041 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002042 clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002043 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002044 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002045 servicePid),
2046 mCameraId(CameraService::cameraIdToInt(cameraIdStr))
Igor Murashkin634a5152013-02-20 17:15:11 -08002047{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002048 int callingPid = getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002049 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002051 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002052
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002054
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002055 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056}
2057
Mathias Agopian65ab4712010-07-14 17:59:35 -07002058// tear down the client
2059CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002060 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002061 mDestructionStarted = true;
2062
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002063 sCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002064 // unconditionally disconnect. function is idempotent
2065 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066}
2067
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002068sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2069
Igor Murashkin634a5152013-02-20 17:15:11 -08002070CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002071 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002072 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002073 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002074 int clientPid, uid_t clientUid,
2075 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002076 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
2077 mClientPackageName(clientPackageName), mClientPid(clientPid), mClientUid(clientUid),
2078 mServicePid(servicePid),
2079 mDisconnected(false),
2080 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002081{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002082 if (sCameraService == nullptr) {
2083 sCameraService = cameraService;
2084 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002085 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002086 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002087
2088 // In some cases the calling code has no access to the package it runs under.
2089 // For example, NDK camera API.
2090 // In this case we will get the packages for the calling UID and pick the first one
2091 // for attributing the app op. This will work correctly for runtime permissions
2092 // as for legacy apps we will toggle the app op for all packages in the UID.
2093 // The caveat is that the operation may be attributed to the wrong package and
2094 // stats based on app ops may be slightly off.
2095 if (mClientPackageName.size() <= 0) {
2096 sp<IServiceManager> sm = defaultServiceManager();
2097 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2098 if (binder == 0) {
2099 ALOGE("Cannot get permission service");
2100 // Leave mClientPackageName unchanged (empty) and the further interaction
2101 // with camera will fail in BasicClient::startCameraOps
2102 return;
2103 }
2104
2105 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2106 Vector<String16> packages;
2107
2108 permCtrl->getPackagesForUid(mClientUid, packages);
2109
2110 if (packages.isEmpty()) {
2111 ALOGE("No packages for calling UID");
2112 // Leave mClientPackageName unchanged (empty) and the further interaction
2113 // with camera will fail in BasicClient::startCameraOps
2114 return;
2115 }
2116 mClientPackageName = packages[0];
2117 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002118}
2119
2120CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002121 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002122 mDestructionStarted = true;
2123}
2124
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002125binder::Status CameraService::BasicClient::disconnect() {
2126 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002127 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002128 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002129 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002130 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002131
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002132 sCameraService->removeByClient(this);
2133 sCameraService->logDisconnected(mCameraIdStr, mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002134 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002135
2136 sp<IBinder> remote = getRemote();
2137 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002138 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002139 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002140
2141 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002142 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002143 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2144 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2145 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002146
Igor Murashkincba2c162013-03-20 15:56:31 -07002147 // client shouldn't be able to call into us anymore
2148 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002149
2150 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002151}
2152
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002153status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2154 // No dumping of clients directly over Binder,
2155 // must go through CameraService::dump
2156 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2157 IPCThreadState::self()->getCallingUid(), NULL, 0);
2158 return OK;
2159}
2160
Ruben Brunkcc776712015-02-17 20:18:47 -08002161String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002162 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002163}
2164
2165
2166int CameraService::BasicClient::getClientPid() const {
2167 return mClientPid;
2168}
2169
Ruben Brunk6267b532015-04-30 17:44:07 -07002170uid_t CameraService::BasicClient::getClientUid() const {
2171 return mClientUid;
2172}
2173
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002174bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2175 // Defaults to API2.
2176 return level == API_2;
2177}
2178
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002179status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002180 ATRACE_CALL();
2181
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002182 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002183 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002184 mOpsCallback = new OpsCallback(this);
2185
Igor Murashkine6800ce2013-03-04 17:25:57 -08002186 {
2187 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002188 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002189 }
2190
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002191 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002192 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002193 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002194 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002195
Svetoslav28e8ef72015-05-11 19:21:31 -07002196 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002197 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2198 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002199 return PERMISSION_DENIED;
2200 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002201
Svetoslav28e8ef72015-05-11 19:21:31 -07002202 if (res == AppOpsManager::MODE_IGNORED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002203 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2204 mCameraIdStr.string(), String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002205 // Return the same error as for device policy manager rejection
2206 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002207 }
2208
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002209 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002210
2211 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002212 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002213
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002214 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002215 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2216 mCameraIdStr);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002217
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002218 return OK;
2219}
2220
2221status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002222 ATRACE_CALL();
2223
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002224 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002225 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002226 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002227 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002228 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002229 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002230
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002231 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
2232 StatusInternal::ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002233
Ruben Brunkcc776712015-02-17 20:18:47 -08002234 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002235 sCameraService->updateStatus(StatusInternal::PRESENT,
2236 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002237
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002238 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002239 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2240 mCameraIdStr);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002241 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002242 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002243 if (mOpsCallback != NULL) {
2244 mAppOpsManager.stopWatchingMode(mOpsCallback);
2245 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002246 mOpsCallback.clear();
2247
2248 return OK;
2249}
2250
2251void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002252 ATRACE_CALL();
2253
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002254 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002255 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002256
2257 if (op != AppOpsManager::OP_CAMERA) {
2258 ALOGW("Unexpected app ops notification received: %d", op);
2259 return;
2260 }
2261
2262 int32_t res;
2263 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002264 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002265 ALOGV("checkOp returns: %d, %s ", res,
2266 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2267 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2268 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2269 "UNKNOWN");
2270
2271 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002272 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002273 myName.string());
2274 // Reset the client PID to allow server-initiated disconnect,
2275 // and to prevent further calls by client.
2276 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002277 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002278 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002279 disconnect();
2280 }
2281}
2282
Mathias Agopian65ab4712010-07-14 17:59:35 -07002283// ----------------------------------------------------------------------------
2284
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002285void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002286 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002287 (void) errorCode;
2288 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002289 if (mRemoteCallback != NULL) {
2290 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2291 } else {
2292 ALOGE("mRemoteCallback is NULL!!");
2293 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002294}
2295
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002296// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002297binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002298 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002299 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002300}
2301
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002302bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2303 return level == API_1;
2304}
2305
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002306CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2307 mClient(client) {
2308}
2309
2310void CameraService::Client::OpsCallback::opChanged(int32_t op,
2311 const String16& packageName) {
2312 sp<BasicClient> client = mClient.promote();
2313 if (client != NULL) {
2314 client->opChanged(op, packageName);
2315 }
2316}
2317
Mathias Agopian65ab4712010-07-14 17:59:35 -07002318// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002319// CameraState
2320// ----------------------------------------------------------------------------
2321
2322CameraService::CameraState::CameraState(const String8& id, int cost,
2323 const std::set<String8>& conflicting) : mId(id),
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002324 mStatus(StatusInternal::PRESENT), mCost(cost), mConflicting(conflicting) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08002325
2326CameraService::CameraState::~CameraState() {}
2327
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002328CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002329 Mutex::Autolock lock(mStatusLock);
2330 return mStatus;
2331}
2332
2333CameraParameters CameraService::CameraState::getShimParams() const {
2334 return mShimParams;
2335}
2336
2337void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2338 mShimParams = params;
2339}
2340
2341int CameraService::CameraState::getCost() const {
2342 return mCost;
2343}
2344
2345std::set<String8> CameraService::CameraState::getConflicting() const {
2346 return mConflicting;
2347}
2348
2349String8 CameraService::CameraState::getId() const {
2350 return mId;
2351}
2352
2353// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002354// ClientEventListener
2355// ----------------------------------------------------------------------------
2356
2357void CameraService::ClientEventListener::onClientAdded(
2358 const resource_policy::ClientDescriptor<String8,
2359 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002360 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002361 if (basicClient.get() != nullptr) {
2362 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2363 notifier.noteStartCamera(descriptor.getKey(),
2364 static_cast<int>(basicClient->getClientUid()));
2365 }
2366}
2367
2368void CameraService::ClientEventListener::onClientRemoved(
2369 const resource_policy::ClientDescriptor<String8,
2370 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002371 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002372 if (basicClient.get() != nullptr) {
2373 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2374 notifier.noteStopCamera(descriptor.getKey(),
2375 static_cast<int>(basicClient->getClientUid()));
2376 }
2377}
2378
2379
2380// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002381// CameraClientManager
2382// ----------------------------------------------------------------------------
2383
Ruben Brunk99e69712015-05-26 17:25:07 -07002384CameraService::CameraClientManager::CameraClientManager() {
2385 setListener(std::make_shared<ClientEventListener>());
2386}
2387
Ruben Brunkcc776712015-02-17 20:18:47 -08002388CameraService::CameraClientManager::~CameraClientManager() {}
2389
2390sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2391 const String8& id) const {
2392 auto descriptor = get(id);
2393 if (descriptor == nullptr) {
2394 return sp<BasicClient>{nullptr};
2395 }
2396 return descriptor->getValue();
2397}
2398
2399String8 CameraService::CameraClientManager::toString() const {
2400 auto all = getAll();
2401 String8 ret("[");
2402 bool hasAny = false;
2403 for (auto& i : all) {
2404 hasAny = true;
2405 String8 key = i->getKey();
2406 int32_t cost = i->getCost();
2407 int32_t pid = i->getOwnerId();
Emilian Peev8131a262017-02-01 12:33:43 +00002408 int32_t score = i->getPriority().getScore();
2409 int32_t state = i->getPriority().getState();
Ruben Brunkcc776712015-02-17 20:18:47 -08002410 auto conflicting = i->getConflicting();
2411 auto clientSp = i->getValue();
2412 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002413 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002414 if (clientSp.get() != nullptr) {
2415 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002416 uid_t clientUid = clientSp->getClientUid();
2417 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002418 }
Emilian Peev8131a262017-02-01 12:33:43 +00002419 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %"
2420 PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002421
Ruben Brunk6267b532015-04-30 17:44:07 -07002422 if (clientSp.get() != nullptr) {
2423 ret.appendFormat("User Id: %d, ", clientUserId);
2424 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002425 if (packageName.size() != 0) {
2426 ret.appendFormat("Client Package Name: %s", packageName.string());
2427 }
2428
2429 ret.append(", Conflicting Client Devices: {");
2430 for (auto& j : conflicting) {
2431 ret.appendFormat("%s, ", j.string());
2432 }
2433 ret.append("})");
2434 }
2435 if (hasAny) ret.append("\n");
2436 ret.append("]\n");
2437 return ret;
2438}
2439
2440CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2441 const String8& key, const sp<BasicClient>& value, int32_t cost,
Emilian Peev8131a262017-02-01 12:33:43 +00002442 const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId,
2443 int32_t state) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002444
2445 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
Emilian Peev8131a262017-02-01 12:33:43 +00002446 key, value, cost, conflictingKeys, score, ownerId, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08002447}
2448
2449CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2450 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2451 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
Emilian Peev8131a262017-02-01 12:33:43 +00002452 partial->getConflicting(), partial->getPriority().getScore(),
2453 partial->getOwnerId(), partial->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08002454}
2455
2456// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002457
2458static const int kDumpLockRetries = 50;
2459static const int kDumpLockSleep = 60000;
2460
2461static bool tryLock(Mutex& mutex)
2462{
2463 bool locked = false;
2464 for (int i = 0; i < kDumpLockRetries; ++i) {
2465 if (mutex.tryLock() == NO_ERROR) {
2466 locked = true;
2467 break;
2468 }
2469 usleep(kDumpLockSleep);
2470 }
2471 return locked;
2472}
2473
2474status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002475 ATRACE_CALL();
2476
Mathias Agopian65ab4712010-07-14 17:59:35 -07002477 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002478 dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n",
Mathias Agopian65ab4712010-07-14 17:59:35 -07002479 getCallingPid(),
2480 getCallingUid());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002481 return NO_ERROR;
2482 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002483 bool locked = tryLock(mServiceLock);
2484 // failed to lock - CameraService is probably deadlocked
2485 if (!locked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002486 dprintf(fd, "!! CameraService may be deadlocked !!\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002487 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002488
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002489 if (!mInitialized) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002490 dprintf(fd, "!! No camera HAL available !!\n");
Ruben Brunkf81648e2014-04-17 16:14:57 -07002491
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002492 // Dump event log for error information
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002493 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002494
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002495 if (locked) mServiceLock.unlock();
2496 return NO_ERROR;
2497 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002498 dprintf(fd, "\n== Service global info: ==\n\n");
2499 dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras);
2500 dprintf(fd, "Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002501 String8 activeClientString = mActiveClientManager.toString();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002502 dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string());
2503 dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002504
2505 dumpEventLog(fd);
2506
2507 bool stateLocked = tryLock(mCameraStatesLock);
2508 if (!stateLocked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002509 dprintf(fd, "CameraStates in use, may be deadlocked\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002510 }
2511
2512 for (auto& state : mCameraStates) {
2513 String8 cameraId = state.first;
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002514
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002515 dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002516
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002517 CameraParameters p = state.second->getShimParams();
2518 if (!p.isEmpty()) {
2519 dprintf(fd, " Camera1 API shim is using parameters:\n ");
2520 p.dump(fd, args);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002521 }
2522
2523 auto clientDescriptor = mActiveClientManager.get(cameraId);
Zhijun He2f140ed2017-02-08 09:57:23 -08002524 if (clientDescriptor != nullptr) {
2525 dprintf(fd, " Device %s is open. Client instance dump:\n",
2526 cameraId.string());
2527 dprintf(fd, " Client priority score: %d state: %d\n",
2528 clientDescriptor->getPriority().getScore(),
2529 clientDescriptor->getPriority().getState());
2530 dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId());
2531
2532 auto client = clientDescriptor->getValue();
2533 dprintf(fd, " Client package: %s\n",
2534 String8(client->getPackageName()).string());
2535
2536 client->dumpClient(fd, args);
2537 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002538 dprintf(fd, " Device %s is closed, no client instance\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002539 cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002540 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002541
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002542 }
2543
2544 if (stateLocked) mCameraStatesLock.unlock();
2545
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002546 if (locked) mServiceLock.unlock();
2547
Emilian Peevf53f66e2017-04-11 14:29:43 +01002548 mCameraProviderManager->dump(fd, args);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002549
2550 dprintf(fd, "\n== Vendor tags: ==\n\n");
2551
2552 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2553 if (desc == NULL) {
Emilian Peev71c73a22017-03-21 16:35:51 +00002554 sp<VendorTagDescriptorCache> cache =
2555 VendorTagDescriptorCache::getGlobalVendorTagCache();
2556 if (cache == NULL) {
2557 dprintf(fd, "No vendor tags.\n");
2558 } else {
2559 cache->dump(fd, /*verbosity*/2, /*indentation*/2);
2560 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002561 } else {
2562 desc->dump(fd, /*verbosity*/2, /*indentation*/2);
2563 }
2564
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002565 // Dump camera traces if there were any
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002566 dprintf(fd, "\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002567 camera3::CameraTraces::dump(fd, args);
2568
2569 // Process dump arguments, if any
2570 int n = args.size();
2571 String16 verboseOption("-v");
2572 String16 unreachableOption("--unreachable");
2573 for (int i = 0; i < n; i++) {
2574 if (args[i] == verboseOption) {
2575 // change logging level
2576 if (i + 1 >= n) continue;
2577 String8 levelStr(args[i+1]);
2578 int level = atoi(levelStr.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002579 dprintf(fd, "\nSetting log level to %d.\n", level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002580 setLogLevel(level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002581 } else if (args[i] == unreachableOption) {
2582 // Dump memory analysis
2583 // TODO - should limit be an argument parameter?
2584 UnreachableMemoryInfo info;
2585 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2586 if (!success) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002587 dprintf(fd, "\n== Unable to dump unreachable memory. "
2588 "Try disabling SELinux enforcement. ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002589 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002590 dprintf(fd, "\n== Dumping unreachable memory: ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08002591 std::string s = info.ToString(/*log_contents*/ true);
2592 write(fd, s.c_str(), s.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002593 }
2594 }
2595 }
2596 return NO_ERROR;
2597}
2598
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002599void CameraService::dumpEventLog(int fd) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002600 dprintf(fd, "\n== Camera service events log (most recent at top): ==\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002601
2602 Mutex::Autolock l(mLogLock);
2603 for (const auto& msg : mEventLog) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002604 dprintf(fd, " %s\n", msg.string());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002605 }
2606
2607 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002608 dprintf(fd, " ...\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002609 } else if (mEventLog.size() == 0) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002610 dprintf(fd, " [no events yet]\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002611 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002612 dprintf(fd, "\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002613}
2614
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002615void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002616 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002617 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2618 if (mTorchClientMap[i] == who) {
2619 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002620 String8 cameraId = mTorchClientMap.keyAt(i);
2621 status_t res = mFlashlight->setTorchMode(cameraId, false);
2622 if (res) {
2623 ALOGE("%s: torch client died but couldn't turn off torch: "
2624 "%s (%d)", __FUNCTION__, strerror(-res), res);
2625 return;
2626 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002627 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002628 break;
2629 }
2630 }
2631}
2632
Ruben Brunkcc776712015-02-17 20:18:47 -08002633/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002634
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002635 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002636 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2637 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002638 */
2639
Ruben Brunka8ca9152015-04-07 14:23:40 -07002640 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2641
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002642 // check torch client
2643 handleTorchClientBinderDied(who);
2644
2645 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002646 if(!evictClientIdByRemote(who)) {
2647 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002648 return;
2649 }
2650
Ruben Brunkcc776712015-02-17 20:18:47 -08002651 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2652 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002653}
2654
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002655void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002656 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002657}
2658
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002659void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
2660 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002661 // Do not lock mServiceLock here or can get into a deadlock from
2662 // connect() -> disconnect -> updateStatus
2663
2664 auto state = getCameraState(cameraId);
2665
2666 if (state == nullptr) {
2667 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2668 cameraId.string());
2669 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002670 }
2671
Ruben Brunkcc776712015-02-17 20:18:47 -08002672 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2673 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2674 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002675 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002676
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002677 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002678 // Update torch status if it has a flash unit.
2679 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002680 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002681 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2682 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002683 TorchModeStatus newTorchStatus =
2684 status == StatusInternal::PRESENT ?
2685 TorchModeStatus::AVAILABLE_OFF :
2686 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002687 if (torchStatus != newTorchStatus) {
2688 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2689 }
2690 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002691 }
2692
2693 Mutex::Autolock lock(mStatusListenerLock);
2694
2695 for (auto& listener : mListenerList) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002696 listener->onStatusChanged(mapToInterface(status), String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08002697 }
2698 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002699}
2700
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002701template<class Func>
2702void CameraService::CameraState::updateStatus(StatusInternal status,
2703 const String8& cameraId,
2704 std::initializer_list<StatusInternal> rejectSourceStates,
2705 Func onStatusUpdatedLocked) {
2706 Mutex::Autolock lock(mStatusLock);
2707 StatusInternal oldStatus = mStatus;
2708 mStatus = status;
2709
2710 if (oldStatus == status) {
2711 return;
2712 }
2713
2714 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
2715 cameraId.string(), oldStatus, status);
2716
2717 if (oldStatus == StatusInternal::NOT_PRESENT &&
2718 (status != StatusInternal::PRESENT &&
2719 status != StatusInternal::ENUMERATING)) {
2720
2721 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
2722 __FUNCTION__);
2723 mStatus = oldStatus;
2724 return;
2725 }
2726
2727 /**
2728 * Sometimes we want to conditionally do a transition.
2729 * For example if a client disconnects, we want to go to PRESENT
2730 * only if we weren't already in NOT_PRESENT or ENUMERATING.
2731 */
2732 for (auto& rejectStatus : rejectSourceStates) {
2733 if (oldStatus == rejectStatus) {
2734 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
2735 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
2736 mStatus = oldStatus;
2737 return;
2738 }
2739 }
2740
2741 onStatusUpdatedLocked(cameraId, status);
2742}
2743
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002744void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
2745 const String8& cameraId) {
2746 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2747 if (proxyBinder == nullptr) return;
2748 String16 id(cameraId);
2749 proxyBinder->notifyCameraState(id, newState);
2750}
2751
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002752status_t CameraService::getTorchStatusLocked(
2753 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002754 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002755 if (!status) {
2756 return BAD_VALUE;
2757 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002758 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2759 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002760 // invalid camera ID or the camera doesn't have a flash unit
2761 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002762 }
2763
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002764 *status = mTorchStatusMap.valueAt(index);
2765 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002766}
2767
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002768status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002769 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002770 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2771 if (index == NAME_NOT_FOUND) {
2772 return BAD_VALUE;
2773 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002774 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002775
2776 return OK;
2777}
2778
Mathias Agopian65ab4712010-07-14 17:59:35 -07002779}; // namespace android