blob: b704573a32221889942530f330931c126ad614b5 [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>
Jayant Chowdharyb61526c2019-05-13 19:37:42 -070036#include <android-base/stringprintf.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080037#include <binder/ActivityManager.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080038#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070039#include <binder/IPCThreadState.h>
40#include <binder/IServiceManager.h>
41#include <binder/MemoryBase.h>
42#include <binder/MemoryHeapBase.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080043#include <binder/PermissionController.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080044#include <binder/ProcessInfoService.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080045#include <binder/IResultReceiver.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070046#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070047#include <cutils/properties.h>
Svet Ganova453d0d2018-01-11 15:37:58 -080048#include <cutils/misc.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080049#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include <hardware/hardware.h>
Jayant Chowdharybe543d42018-08-15 13:16:14 -070051#include "hidl/HidlCameraService.h"
52#include <hidl/HidlTransportSupport.h>
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -080053#include <hwbinder/IPCThreadState.h>
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -070054#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070055#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080056#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070057#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070058#include <mediautils/BatteryNotifier.h>
Michael Grooverd1d435a2018-12-18 17:39:42 -080059#include <sensorprivacy/SensorPrivacyManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070060#include <utils/Errors.h>
61#include <utils/Log.h>
62#include <utils/String16.h>
Svet Ganov94ec46f2018-06-08 15:03:46 -070063#include <utils/SystemClock.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080064#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080065#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080066#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070067#include <system/camera_metadata.h>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080068
Ruben Brunkb2119af2014-05-09 19:57:56 -070069#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070070
71#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070072#include "api1/CameraClient.h"
73#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070074#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070075#include "utils/CameraTraces.h"
Emilian Peevbd8c5032018-02-14 23:05:40 +000076#include "utils/TagMonitor.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070077#include "utils/CameraThreadState.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070078
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080079namespace {
80 const char* kPermissionServiceName = "permission";
81}; // namespace anonymous
82
Mathias Agopian65ab4712010-07-14 17:59:35 -070083namespace android {
84
Jayant Chowdharyb61526c2019-05-13 19:37:42 -070085using base::StringPrintf;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080086using binder::Status;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070087using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080088using hardware::ICamera;
89using hardware::ICameraClient;
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -070090using hardware::ICameraServiceProxy;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080091using hardware::ICameraServiceListener;
92using hardware::camera::common::V1_0::CameraDeviceStatus;
93using hardware::camera::common::V1_0::TorchModeStatus;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080094
Mathias Agopian65ab4712010-07-14 17:59:35 -070095// ----------------------------------------------------------------------------
96// Logging support -- this is for debugging only
97// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070098volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070099
Steve Blockb8a80522011-12-20 16:23:08 +0000100#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
101#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102
103static void setLogLevel(int level) {
104 android_atomic_write(level, &gLogLevel);
105}
106
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800107// Convenience methods for constructing binder::Status objects for error returns
108
109#define STATUS_ERROR(errorCode, errorString) \
110 binder::Status::fromServiceSpecificError(errorCode, \
111 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
112
113#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
114 binder::Status::fromServiceSpecificError(errorCode, \
115 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
116 __VA_ARGS__))
117
Mathias Agopian65ab4712010-07-14 17:59:35 -0700118// ----------------------------------------------------------------------------
119
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700120static const String16 sDumpPermission("android.permission.DUMP");
Svet Ganova453d0d2018-01-11 15:37:58 -0800121static const String16 sManageCameraPermission("android.permission.MANAGE_CAMERA");
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700122static const String16 sCameraPermission("android.permission.CAMERA");
123static const String16 sSystemCameraPermission("android.permission.SYSTEM_CAMERA");
124static const String16
125 sCameraSendSystemEventsPermission("android.permission.CAMERA_SEND_SYSTEM_EVENTS");
Svet Ganova453d0d2018-01-11 15:37:58 -0800126
Jayant Chowdharyc578a502019-05-08 10:57:54 -0700127// Matches with PERCEPTIBLE_APP_ADJ in ProcessList.java
128static constexpr int32_t kVendorClientScore = 200;
129// Matches with PROCESS_STATE_PERSISTENT_UI in ActivityManager.java
130static constexpr int32_t kVendorClientState = 1;
131
Eino-Ville Talvala7d5569f2019-04-08 13:57:54 -0700132Mutex CameraService::sProxyMutex;
133sp<hardware::ICameraServiceProxy> CameraService::sCameraServiceProxy;
134
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800135CameraService::CameraService() :
136 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800137 mNumberOfCameras(0),
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700138 mNumberOfCamerasWithoutSystemCamera(0),
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700139 mSoundRef(0), mInitialized(false),
140 mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE) {
Steve Blockdf64d152012-01-04 20:05:49 +0000141 ALOGI("CameraService started (pid=%d)", getpid());
Ruben Brunkcc776712015-02-17 20:18:47 -0800142 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143}
144
Iliyan Malchev8951a972011-04-14 16:55:59 -0700145void CameraService::onFirstRef()
146{
Ruben Brunkcc776712015-02-17 20:18:47 -0800147 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800148
Iliyan Malchev8951a972011-04-14 16:55:59 -0700149 BnCameraService::onFirstRef();
150
Ruben Brunk99e69712015-05-26 17:25:07 -0700151 // Update battery life tracking if service is restarting
152 BatteryNotifier& notifier(BatteryNotifier::getInstance());
153 notifier.noteResetCamera();
154 notifier.noteResetFlashlight();
155
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800156 status_t res = INVALID_OPERATION;
Eino-Ville Talvala9cbbc832017-01-23 15:39:53 -0800157
Emilian Peevf53f66e2017-04-11 14:29:43 +0100158 res = enumerateProviders();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800159 if (res == OK) {
160 mInitialized = true;
161 }
162
Svet Ganova453d0d2018-01-11 15:37:58 -0800163 mUidPolicy = new UidPolicy(this);
164 mUidPolicy->registerSelf();
Michael Grooverd1d435a2018-12-18 17:39:42 -0800165 mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
166 mSensorPrivacyPolicy->registerSelf();
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700167 mAppOps.setCameraAudioRestriction(mAudioRestriction);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700168 sp<HidlCameraService> hcs = HidlCameraService::getInstance(this);
169 if (hcs->registerAsService() != android::OK) {
170 ALOGE("%s: Failed to register default android.frameworks.cameraservice.service@1.0",
171 __FUNCTION__);
172 }
Shuzhen Wang24b44152019-09-20 10:38:11 -0700173
174 // This needs to be last call in this function, so that it's as close to
175 // ServiceManager::addService() as possible.
176 CameraService::pingCameraServiceProxy();
177 ALOGI("CameraService pinged cameraservice proxy");
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800178}
179
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800180status_t CameraService::enumerateProviders() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800181 status_t res;
Emilian Peevaee727d2017-05-04 16:35:48 +0100182
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800183 std::vector<std::string> deviceIds;
184 {
185 Mutex::Autolock l(mServiceLock);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800186
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800187 if (nullptr == mCameraProviderManager.get()) {
188 mCameraProviderManager = new CameraProviderManager();
189 res = mCameraProviderManager->initialize(this);
190 if (res != OK) {
191 ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
192 __FUNCTION__, strerror(-res), res);
193 return res;
Emilian Peevaee727d2017-05-04 16:35:48 +0100194 }
195 }
196
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800197
198 // Setup vendor tags before we call get_camera_info the first time
199 // because HAL might need to setup static vendor keys in get_camera_info
200 // TODO: maybe put this into CameraProviderManager::initialize()?
201 mCameraProviderManager->setUpVendorTags();
202
203 if (nullptr == mFlashlight.get()) {
204 mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700205 }
206
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800207 res = mFlashlight->findFlashUnits();
208 if (res != OK) {
209 ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
210 }
211
212 deviceIds = mCameraProviderManager->getCameraDeviceIds();
213 }
214
215
216 for (auto& cameraId : deviceIds) {
217 String8 id8 = String8(cameraId.c_str());
Shuzhen Wang6ba3f5e2018-11-20 10:04:08 -0800218 if (getCameraState(id8) == nullptr) {
219 onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT);
220 }
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800221 }
222
223 return OK;
224}
225
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700226sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800227#ifndef __BRILLO__
Eino-Ville Talvala7d5569f2019-04-08 13:57:54 -0700228 Mutex::Autolock al(sProxyMutex);
229 if (sCameraServiceProxy == nullptr) {
230 sp<IServiceManager> sm = defaultServiceManager();
231 // Use checkService because cameraserver normally starts before the
232 // system server and the proxy service. So the long timeout that getService
233 // has before giving up is inappropriate.
234 sp<IBinder> binder = sm->checkService(String16("media.camera.proxy"));
235 if (binder != nullptr) {
236 sCameraServiceProxy = interface_cast<ICameraServiceProxy>(binder);
237 }
Ruben Brunk2823ce02015-05-19 17:25:13 -0700238 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800239#endif
Eino-Ville Talvala7d5569f2019-04-08 13:57:54 -0700240 return sCameraServiceProxy;
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700241}
242
243void CameraService::pingCameraServiceProxy() {
244 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
245 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700246 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700247}
248
Shuzhen Wang7d859d42018-11-06 15:33:23 -0800249void CameraService::broadcastTorchModeStatus(const String8& cameraId, TorchModeStatus status) {
250 Mutex::Autolock lock(mStatusListenerLock);
251
252 for (auto& i : mListenerList) {
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700253 i->getListener()->onTorchStatusChanged(mapToInterface(status), String16{cameraId});
Shuzhen Wang7d859d42018-11-06 15:33:23 -0800254 }
255}
256
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257CameraService::~CameraService() {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800258 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Svet Ganova453d0d2018-01-11 15:37:58 -0800259 mUidPolicy->unregisterSelf();
Michael Grooverd1d435a2018-12-18 17:39:42 -0800260 mSensorPrivacyPolicy->unregisterSelf();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700261}
262
Emilian Peevaee727d2017-05-04 16:35:48 +0100263void CameraService::onNewProviderRegistered() {
264 enumerateProviders();
265}
266
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700267void CameraService::filterAPI1SystemCameraLocked(
268 const std::vector<std::string> &normalDeviceIds) {
269 mNormalDeviceIdsWithoutSystemCamera.clear();
270 for (auto &deviceId : normalDeviceIds) {
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700271 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
272 if (getSystemCameraKind(String8(deviceId.c_str()), &deviceKind) != OK) {
273 ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, deviceId.c_str());
274 continue;
275 }
276 if (deviceKind == SystemCameraKind::SYSTEM_ONLY_CAMERA) {
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700277 // All system camera ids will necessarily come after public camera
278 // device ids as per the HAL interface contract.
279 break;
280 }
281 mNormalDeviceIdsWithoutSystemCamera.push_back(deviceId);
282 }
283 ALOGV("%s: number of API1 compatible public cameras is %zu", __FUNCTION__,
284 mNormalDeviceIdsWithoutSystemCamera.size());
285}
286
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700287status_t CameraService::getSystemCameraKind(const String8& cameraId, SystemCameraKind *kind) const {
288 auto state = getCameraState(cameraId);
289 if (state != nullptr) {
290 *kind = state->getSystemCameraKind();
291 return OK;
292 }
293 // Hidden physical camera ids won't have CameraState
294 return mCameraProviderManager->getSystemCameraKind(cameraId.c_str(), kind);
295}
296
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800297void CameraService::updateCameraNumAndIds() {
298 Mutex::Autolock l(mServiceLock);
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700299 std::pair<int, int> systemAndNonSystemCameras = mCameraProviderManager->getCameraCount();
300 // Excludes hidden secure cameras
301 mNumberOfCameras =
302 systemAndNonSystemCameras.first + systemAndNonSystemCameras.second;
303 mNumberOfCamerasWithoutSystemCamera = systemAndNonSystemCameras.second;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800304 mNormalDeviceIds =
305 mCameraProviderManager->getAPI1CompatibleCameraDeviceIds();
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700306 filterAPI1SystemCameraLocked(mNormalDeviceIds);
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800307}
308
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100309void CameraService::addStates(const String8 id) {
310 std::string cameraId(id.c_str());
311 hardware::camera::common::V1_0::CameraResourceCost cost;
312 status_t res = mCameraProviderManager->getResourceCost(cameraId, &cost);
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700313 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100314 if (res != OK) {
315 ALOGE("Failed to query device resource cost: %s (%d)", strerror(-res), res);
316 return;
317 }
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700318 res = mCameraProviderManager->getSystemCameraKind(cameraId, &deviceKind);
319 if (res != OK) {
320 ALOGE("Failed to query device kind: %s (%d)", strerror(-res), res);
321 return;
322 }
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100323 std::set<String8> conflicting;
324 for (size_t i = 0; i < cost.conflictingDevices.size(); i++) {
325 conflicting.emplace(String8(cost.conflictingDevices[i].c_str()));
326 }
327
328 {
329 Mutex::Autolock lock(mCameraStatesLock);
330 mCameraStates.emplace(id, std::make_shared<CameraState>(id, cost.resourceCost,
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700331 conflicting, deviceKind));
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100332 }
333
334 if (mFlashlight->hasFlashUnit(id)) {
Emilian Peev7f25e5f2018-04-11 16:50:34 +0100335 Mutex::Autolock al(mTorchStatusMutex);
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100336 mTorchStatusMap.add(id, TorchModeStatus::AVAILABLE_OFF);
Shuzhen Wang7d859d42018-11-06 15:33:23 -0800337
338 broadcastTorchModeStatus(id, TorchModeStatus::AVAILABLE_OFF);
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100339 }
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800340
341 updateCameraNumAndIds();
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100342 logDeviceAdded(id, "Device added");
343}
344
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100345void CameraService::removeStates(const String8 id) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800346 updateCameraNumAndIds();
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100347 if (mFlashlight->hasFlashUnit(id)) {
Emilian Peev7f25e5f2018-04-11 16:50:34 +0100348 Mutex::Autolock al(mTorchStatusMutex);
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100349 mTorchStatusMap.removeItem(id);
350 }
351
352 {
353 Mutex::Autolock lock(mCameraStatesLock);
354 mCameraStates.erase(id);
355 }
356}
357
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800358void CameraService::onDeviceStatusChanged(const String8& id,
359 CameraDeviceStatus newHalStatus) {
360 ALOGI("%s: Status changed for cameraId=%s, newStatus=%d", __FUNCTION__,
361 id.string(), newHalStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700362
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800363 StatusInternal newStatus = mapToInternal(newHalStatus);
364
Ruben Brunkcc776712015-02-17 20:18:47 -0800365 std::shared_ptr<CameraState> state = getCameraState(id);
366
367 if (state == nullptr) {
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700368 if (newStatus == StatusInternal::PRESENT) {
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100369 ALOGI("%s: Unknown camera ID %s, a new camera is added",
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700370 __FUNCTION__, id.string());
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +0100371
372 // First add as absent to make sure clients are notified below
373 addStates(id);
374
375 updateStatus(newStatus, id);
Yin-Chia Yeh92e33212017-05-24 15:54:15 -0700376 } else {
377 ALOGE("%s: Bad camera ID %s", __FUNCTION__, id.string());
378 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700379 return;
380 }
381
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800382 StatusInternal oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800383
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800384 if (oldStatus == newStatus) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800385 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700386 return;
387 }
388
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800389 if (newStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700390 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
391 newStatus));
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800392
393 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
394 // to this device until the status changes
395 updateStatus(StatusInternal::NOT_PRESENT, id);
396
Ruben Brunkcc776712015-02-17 20:18:47 -0800397 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700398 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800399 // Don't do this in updateStatus to avoid deadlock over mServiceLock
400 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700401
Ruben Brunkcc776712015-02-17 20:18:47 -0800402 // Remove cached shim parameters
403 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700404
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700405 // Remove the client from the list of active clients, if there is one
Ruben Brunkcc776712015-02-17 20:18:47 -0800406 clientToDisconnect = removeClientLocked(id);
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700407 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800408
Eino-Ville Talvala8d942f92017-03-13 10:09:51 -0700409 // Disconnect client
410 if (clientToDisconnect.get() != nullptr) {
411 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
412 __FUNCTION__, id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800413 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800414 clientToDisconnect->notifyError(
415 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800416 CaptureResultExtras{});
Ruben Brunkcc776712015-02-17 20:18:47 -0800417 // Ensure not in binder RPC so client disconnect PID checks work correctly
Jayant Chowdhary12361932018-08-27 14:46:13 -0700418 LOG_ALWAYS_FATAL_IF(CameraThreadState::getCallingPid() != getpid(),
Ruben Brunkcc776712015-02-17 20:18:47 -0800419 "onDeviceStatusChanged must be called from the camera service process!");
420 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700421 }
422
Guennadi Liakhovetski6034bf52017-12-07 10:28:29 +0100423 removeStates(id);
Ruben Brunkcc776712015-02-17 20:18:47 -0800424 } else {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800425 if (oldStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700426 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
427 newStatus));
428 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800429 updateStatus(newStatus, id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700430 }
431
Igor Murashkincba2c162013-03-20 15:56:31 -0700432}
433
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800434void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800435 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800436 Mutex::Autolock al(mTorchStatusMutex);
437 onTorchStatusChangedLocked(cameraId, newStatus);
438}
439
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800440void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800441 TorchModeStatus newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800442 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
443 __FUNCTION__, cameraId.string(), newStatus);
444
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800445 TorchModeStatus status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800446 status_t res = getTorchStatusLocked(cameraId, &status);
447 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700448 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
449 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800450 return;
451 }
452 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800453 return;
454 }
455
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800456 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800457 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800458 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
459 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800460 return;
461 }
462
Ruben Brunkcc776712015-02-17 20:18:47 -0800463 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700464 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700465 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700466 auto iter = mTorchUidMap.find(cameraId);
467 if (iter != mTorchUidMap.end()) {
468 int oldUid = iter->second.second;
469 int newUid = iter->second.first;
470 BatteryNotifier& notifier(BatteryNotifier::getInstance());
471 if (oldUid != newUid) {
472 // If the UID has changed, log the status and update current UID in mTorchUidMap
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800473 if (status == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700474 notifier.noteFlashlightOff(cameraId, oldUid);
475 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800476 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700477 notifier.noteFlashlightOn(cameraId, newUid);
478 }
479 iter->second.second = newUid;
480 } else {
481 // If the UID has not changed, log the status
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800482 if (newStatus == TorchModeStatus::AVAILABLE_ON) {
Ruben Brunk99e69712015-05-26 17:25:07 -0700483 notifier.noteFlashlightOn(cameraId, oldUid);
484 } else {
485 notifier.noteFlashlightOff(cameraId, oldUid);
486 }
487 }
488 }
489 }
490
Shuzhen Wang7d859d42018-11-06 15:33:23 -0800491 broadcastTorchModeStatus(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800492}
493
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700494static bool hasPermissionsForSystemCamera(int callingPid, int callingUid) {
495 return checkPermission(sSystemCameraPermission, callingPid, callingUid) &&
496 checkPermission(sCameraPermission, callingPid, callingUid);
497}
498
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800499Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700500 ATRACE_CALL();
Emilian Peevaee727d2017-05-04 16:35:48 +0100501 Mutex::Autolock l(mServiceLock);
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700502 bool hasSystemCameraPermissions =
503 hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(),
504 CameraThreadState::getCallingUid());
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700505 switch (type) {
506 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700507 if (hasSystemCameraPermissions) {
508 *numCameras = static_cast<int>(mNormalDeviceIds.size());
509 } else {
510 *numCameras = static_cast<int>(mNormalDeviceIdsWithoutSystemCamera.size());
511 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800512 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700513 case CAMERA_TYPE_ALL:
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700514 if (hasSystemCameraPermissions) {
515 *numCameras = mNumberOfCameras;
516 } else {
517 *numCameras = mNumberOfCamerasWithoutSystemCamera;
518 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800519 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700520 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800521 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700522 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800523 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
524 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700525 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800526 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527}
528
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800529Status CameraService::getCameraInfo(int cameraId,
530 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700531 ATRACE_CALL();
Emilian Peevaee727d2017-05-04 16:35:48 +0100532 Mutex::Autolock l(mServiceLock);
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700533 std::string cameraIdStr = cameraIdIntToStrLocked(cameraId);
534 if (shouldRejectSystemCameraConnection(String8(cameraIdStr.c_str()))) {
535 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera"
536 "characteristics for system only device %s: ", cameraIdStr.c_str());
537 }
Emilian Peevaee727d2017-05-04 16:35:48 +0100538
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800539 if (!mInitialized) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800540 return STATUS_ERROR(ERROR_DISCONNECTED,
541 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700542 }
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700543 bool hasSystemCameraPermissions =
544 hasPermissionsForSystemCamera(CameraThreadState::getCallingPid(),
545 CameraThreadState::getCallingUid());
546 int cameraIdBound = mNumberOfCamerasWithoutSystemCamera;
547 if (hasSystemCameraPermissions) {
548 cameraIdBound = mNumberOfCameras;
549 }
550 if (cameraId < 0 || cameraId >= cameraIdBound) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
552 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 }
554
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 Status ret = Status::ok();
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800556 status_t err = mCameraProviderManager->getCameraInfo(
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700557 cameraIdStr.c_str(), cameraInfo);
Emilian Peevf53f66e2017-04-11 14:29:43 +0100558 if (err != OK) {
559 ret = STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
560 "Error retrieving camera info from device %d: %s (%d)", cameraId,
561 strerror(-err), err);
Ruben Brunkcc776712015-02-17 20:18:47 -0800562 }
Emilian Peevf53f66e2017-04-11 14:29:43 +0100563
Ruben Brunkcc776712015-02-17 20:18:47 -0800564 return ret;
565}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700566
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800567std::string CameraService::cameraIdIntToStrLocked(int cameraIdInt) {
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700568 const std::vector<std::string> *deviceIds = &mNormalDeviceIdsWithoutSystemCamera;
569 auto callingPid = CameraThreadState::getCallingPid();
570 auto callingUid = CameraThreadState::getCallingUid();
571 if (checkPermission(sSystemCameraPermission, callingPid, callingUid) ||
572 getpid() == callingPid) {
573 deviceIds = &mNormalDeviceIds;
574 }
575 if (cameraIdInt < 0 || cameraIdInt >= static_cast<int>(deviceIds->size())) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800576 ALOGE("%s: input id %d invalid: valid range (0, %zu)",
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700577 __FUNCTION__, cameraIdInt, deviceIds->size());
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800578 return std::string{};
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800579 }
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800580
Jayant Chowdhary847947d2019-08-30 18:02:59 -0700581 return (*deviceIds)[cameraIdInt];
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800582}
583
584String8 CameraService::cameraIdIntToStr(int cameraIdInt) {
585 Mutex::Autolock lock(mServiceLock);
586 return String8(cameraIdIntToStrLocked(cameraIdInt).c_str());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800587}
588
589Status CameraService::getCameraCharacteristics(const String16& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800590 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700591 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700592 if (!cameraInfo) {
593 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800594 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700595 }
596
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800597 if (!mInitialized) {
598 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800599 return STATUS_ERROR(ERROR_DISCONNECTED,
600 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700601 }
602
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700603 if (shouldRejectSystemCameraConnection(String8(cameraId))) {
604 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera"
605 "characteristics for system only device %s: ", String8(cameraId).string());
606 }
607
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800608 Status ret{};
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800609
Emilian Peevf53f66e2017-04-11 14:29:43 +0100610 status_t res = mCameraProviderManager->getCameraCharacteristics(
611 String8(cameraId).string(), cameraInfo);
612 if (res != OK) {
613 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera "
614 "characteristics for device %s: %s (%d)", String8(cameraId).string(),
615 strerror(-res), res);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700616 }
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700617 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
618 if (getSystemCameraKind(String8(cameraId), &deviceKind) != OK) {
619 ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, String8(cameraId).string());
620 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Unable to retrieve camera kind "
621 "for device %s", String8(cameraId).string());
622 }
Jayant Chowdhary12361932018-08-27 14:46:13 -0700623 int callingPid = CameraThreadState::getCallingPid();
624 int callingUid = CameraThreadState::getCallingUid();
Emilian Peeve20c6372018-08-14 18:45:53 +0100625 std::vector<int32_t> tagsRemoved;
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700626 // If it's not calling from cameraserver, check the permission only if
627 // android.permission.CAMERA is required. If android.permission.SYSTEM_CAMERA was needed,
628 // it would've already been checked in shouldRejectSystemCameraConnection.
Emilian Peeve20c6372018-08-14 18:45:53 +0100629 if ((callingPid != getpid()) &&
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -0700630 (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) &&
Jayant Chowdhary5216b212019-07-17 09:26:23 -0700631 !checkPermission(sCameraPermission, callingPid, callingUid)) {
Emilian Peeve20c6372018-08-14 18:45:53 +0100632 res = cameraInfo->removePermissionEntries(
633 mCameraProviderManager->getProviderTagIdLocked(String8(cameraId).string()),
634 &tagsRemoved);
635 if (res != OK) {
636 cameraInfo->clear();
637 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to remove camera"
638 " characteristics needing camera permission for device %s: %s (%d)",
639 String8(cameraId).string(), strerror(-res), res);
640 }
641 }
642
643 if (!tagsRemoved.empty()) {
644 res = cameraInfo->update(ANDROID_REQUEST_CHARACTERISTIC_KEYS_NEEDING_PERMISSION,
645 tagsRemoved.data(), tagsRemoved.size());
646 if (res != OK) {
647 cameraInfo->clear();
648 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION, "Failed to insert camera "
649 "keys needing permission for device %s: %s (%d)", String8(cameraId).string(),
650 strerror(-res), res);
651 }
652 }
653
Zhijun He2b59be82013-09-25 10:14:30 -0700654 return ret;
655}
656
Ruben Brunkcc776712015-02-17 20:18:47 -0800657String8 CameraService::getFormattedCurrentTime() {
658 time_t now = time(nullptr);
659 char formattedTime[64];
660 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
661 return String8(formattedTime);
662}
663
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800664Status CameraService::getCameraVendorTagDescriptor(
665 /*out*/
666 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700667 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800668 if (!mInitialized) {
669 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800670 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800671 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800672 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
673 if (globalDescriptor != nullptr) {
674 *desc = *(globalDescriptor.get());
675 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800676 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800677}
678
Emilian Peev71c73a22017-03-21 16:35:51 +0000679Status CameraService::getCameraVendorTagCache(
680 /*out*/ hardware::camera2::params::VendorTagDescriptorCache* cache) {
681 ATRACE_CALL();
682 if (!mInitialized) {
683 ALOGE("%s: Camera HAL couldn't be initialized", __FUNCTION__);
684 return STATUS_ERROR(ERROR_DISCONNECTED,
685 "Camera subsystem not available");
686 }
687 sp<VendorTagDescriptorCache> globalCache =
688 VendorTagDescriptorCache::getGlobalVendorTagCache();
689 if (globalCache != nullptr) {
690 *cache = *(globalCache.get());
691 }
692 return Status::ok();
693}
694
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800695int CameraService::getDeviceVersion(const String8& cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700696 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800697
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800698 int deviceVersion = 0;
699
Emilian Peevf53f66e2017-04-11 14:29:43 +0100700 status_t res;
701 hardware::hidl_version maxVersion{0,0};
702 res = mCameraProviderManager->getHighestSupportedVersion(cameraId.string(),
703 &maxVersion);
704 if (res != OK) return -1;
705 deviceVersion = HARDWARE_DEVICE_API_VERSION(maxVersion.get_major(), maxVersion.get_minor());
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800706
Emilian Peevf53f66e2017-04-11 14:29:43 +0100707 hardware::CameraInfo info;
708 if (facing) {
709 res = mCameraProviderManager->getCameraInfo(cameraId.string(), &info);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -0800710 if (res != OK) return -1;
Emilian Peevf53f66e2017-04-11 14:29:43 +0100711 *facing = info.facing;
Igor Murashkin634a5152013-02-20 17:15:11 -0800712 }
Emilian Peevf53f66e2017-04-11 14:29:43 +0100713
Igor Murashkin634a5152013-02-20 17:15:11 -0800714 return deviceVersion;
715}
716
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800717Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700718 switch(err) {
719 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800720 return Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800721 case BAD_VALUE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800722 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
723 "CameraId is not valid for HAL module");
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800724 case NO_INIT:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800725 return STATUS_ERROR(ERROR_DISCONNECTED,
726 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700727 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800728 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
729 "Camera HAL encountered error %d: %s",
730 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700731 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800732}
733
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800734Status CameraService::makeClient(const sp<CameraService>& cameraService,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -0800735 const sp<IInterface>& cameraCb, const String16& packageName,
736 const std::unique_ptr<String16>& featureId, const String8& cameraId, int api1CameraId,
737 int facing, int clientPid, uid_t clientUid, int servicePid, int halVersion,
738 int deviceVersion, apiLevel effectiveApiLevel,
Ruben Brunkcc776712015-02-17 20:18:47 -0800739 /*out*/sp<BasicClient>* client) {
740
Ruben Brunkcc776712015-02-17 20:18:47 -0800741 if (halVersion < 0 || halVersion == deviceVersion) {
742 // Default path: HAL version is unspecified by caller, create CameraClient
743 // based on device version reported by the HAL.
744 switch(deviceVersion) {
745 case CAMERA_DEVICE_API_VERSION_1_0:
746 if (effectiveApiLevel == API_1) { // Camera1 API route
747 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Philip P. Moltmann9e648f62019-11-04 12:52:45 -0800748 *client = new CameraClient(cameraService, tmp, packageName, featureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800749 api1CameraId, facing, clientPid, clientUid,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -0700750 getpid());
Ruben Brunkcc776712015-02-17 20:18:47 -0800751 } else { // Camera2 API route
752 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800753 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800754 "Camera device \"%s\" HAL version %d does not support camera2 API",
755 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800756 }
757 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800758 case CAMERA_DEVICE_API_VERSION_3_0:
759 case CAMERA_DEVICE_API_VERSION_3_1:
760 case CAMERA_DEVICE_API_VERSION_3_2:
761 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700762 case CAMERA_DEVICE_API_VERSION_3_4:
Shuzhen Wangf9d2c022018-08-21 12:07:35 -0700763 case CAMERA_DEVICE_API_VERSION_3_5:
Ruben Brunkcc776712015-02-17 20:18:47 -0800764 if (effectiveApiLevel == API_1) { // Camera1 API route
765 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Philip P. Moltmann9e648f62019-11-04 12:52:45 -0800766 *client = new Camera2Client(cameraService, tmp, packageName, featureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800767 cameraId, api1CameraId,
768 facing, clientPid, clientUid,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -0700769 servicePid);
Ruben Brunkcc776712015-02-17 20:18:47 -0800770 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800771 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
772 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
Philip P. Moltmann9e648f62019-11-04 12:52:45 -0800773 *client = new CameraDeviceClient(cameraService, tmp, packageName, featureId,
774 cameraId, facing, clientPid, clientUid, servicePid);
Ruben Brunkcc776712015-02-17 20:18:47 -0800775 }
776 break;
777 default:
778 // Should not be reachable
779 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800780 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800781 "Camera device \"%s\" has unknown HAL version %d",
782 cameraId.string(), deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800783 }
784 } else {
785 // A particular HAL version is requested by caller. Create CameraClient
786 // based on the requested HAL version.
787 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
788 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
789 // Only support higher HAL version device opened as HAL1.0 device.
790 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Philip P. Moltmann9e648f62019-11-04 12:52:45 -0800791 *client = new CameraClient(cameraService, tmp, packageName, featureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800792 api1CameraId, facing, clientPid, clientUid,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -0700793 servicePid);
Ruben Brunkcc776712015-02-17 20:18:47 -0800794 } else {
795 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
796 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
797 " opened as HAL %x device", halVersion, deviceVersion,
798 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800799 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800800 "Camera device \"%s\" (HAL version %d) cannot be opened as HAL version %d",
801 cameraId.string(), deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800802 }
803 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800804 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800805}
806
Ruben Brunk6267b532015-04-30 17:44:07 -0700807String8 CameraService::toString(std::set<userid_t> intSet) {
808 String8 s("");
809 bool first = true;
810 for (userid_t i : intSet) {
811 if (first) {
812 s.appendFormat("%d", i);
813 first = false;
814 } else {
815 s.appendFormat(", %d", i);
816 }
817 }
818 return s;
819}
820
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800821int32_t CameraService::mapToInterface(TorchModeStatus status) {
822 int32_t serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
823 switch (status) {
824 case TorchModeStatus::NOT_AVAILABLE:
825 serviceStatus = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
826 break;
827 case TorchModeStatus::AVAILABLE_OFF:
828 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
829 break;
830 case TorchModeStatus::AVAILABLE_ON:
831 serviceStatus = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
832 break;
833 default:
834 ALOGW("Unknown new flash status: %d", status);
835 }
836 return serviceStatus;
837}
838
839CameraService::StatusInternal CameraService::mapToInternal(CameraDeviceStatus status) {
840 StatusInternal serviceStatus = StatusInternal::NOT_PRESENT;
841 switch (status) {
842 case CameraDeviceStatus::NOT_PRESENT:
843 serviceStatus = StatusInternal::NOT_PRESENT;
844 break;
845 case CameraDeviceStatus::PRESENT:
846 serviceStatus = StatusInternal::PRESENT;
847 break;
848 case CameraDeviceStatus::ENUMERATING:
849 serviceStatus = StatusInternal::ENUMERATING;
850 break;
851 default:
852 ALOGW("Unknown new HAL device status: %d", status);
853 }
854 return serviceStatus;
855}
856
857int32_t CameraService::mapToInterface(StatusInternal status) {
858 int32_t serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
859 switch (status) {
860 case StatusInternal::NOT_PRESENT:
861 serviceStatus = ICameraServiceListener::STATUS_NOT_PRESENT;
862 break;
863 case StatusInternal::PRESENT:
864 serviceStatus = ICameraServiceListener::STATUS_PRESENT;
865 break;
866 case StatusInternal::ENUMERATING:
867 serviceStatus = ICameraServiceListener::STATUS_ENUMERATING;
868 break;
869 case StatusInternal::NOT_AVAILABLE:
870 serviceStatus = ICameraServiceListener::STATUS_NOT_AVAILABLE;
871 break;
872 case StatusInternal::UNKNOWN:
873 serviceStatus = ICameraServiceListener::STATUS_UNKNOWN;
874 break;
875 default:
876 ALOGW("Unknown new internal device status: %d", status);
877 }
878 return serviceStatus;
879}
880
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800881Status CameraService::initializeShimMetadata(int cameraId) {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700882 int uid = CameraThreadState::getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700883
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800884 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800885 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800886 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800887 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800888 if (!(ret = connectHelper<ICameraClient,Client>(
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800889 sp<ICameraClient>{nullptr}, id, cameraId,
890 static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
Philip P. Moltmann9e648f62019-11-04 12:52:45 -0800891 internalPackageName, std::unique_ptr<String16>(), uid, USE_CALLING_PID,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -0700892 API_1, /*shimUpdateOnly*/ true, /*out*/ tmp)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800893 ).isOk()) {
894 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700895 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800896 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700897}
898
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800899Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700900 /*out*/
901 CameraParameters* parameters) {
902
903 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
904
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800905 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700906
907 if (parameters == NULL) {
908 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800909 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700910 }
911
Ruben Brunkcc776712015-02-17 20:18:47 -0800912 String8 id = String8::format("%d", cameraId);
913
914 // Check if we already have parameters
915 {
916 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700917 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800918 auto cameraState = getCameraState(id);
919 if (cameraState == nullptr) {
920 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800921 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
922 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800923 }
924 CameraParameters p = cameraState->getShimParams();
925 if (!p.isEmpty()) {
926 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800927 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700928 }
929 }
930
Jayant Chowdhary12361932018-08-27 14:46:13 -0700931 int64_t token = CameraThreadState::clearCallingIdentity();
Ruben Brunkcc776712015-02-17 20:18:47 -0800932 ret = initializeShimMetadata(cameraId);
Jayant Chowdhary12361932018-08-27 14:46:13 -0700933 CameraThreadState::restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800934 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800935 // Error already logged by callee
936 return ret;
937 }
938
939 // Check for parameters again
940 {
941 // Scope for service lock
942 Mutex::Autolock lock(mServiceLock);
943 auto cameraState = getCameraState(id);
944 if (cameraState == nullptr) {
945 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800946 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
947 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700948 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800949 CameraParameters p = cameraState->getShimParams();
950 if (!p.isEmpty()) {
951 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800952 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700953 }
954 }
955
Ruben Brunkcc776712015-02-17 20:18:47 -0800956 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
957 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800958 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700959}
960
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800961// Can camera service trust the caller based on the calling UID?
962static bool isTrustedCallingUid(uid_t uid) {
963 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700964 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800965 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700966 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800967 return true;
968 default:
969 return false;
970 }
971}
972
Nicholas Sauera3620332019-04-03 14:05:17 -0700973static status_t getUidForPackage(String16 packageName, int userId, /*inout*/uid_t& uid, int err) {
974 PermissionController pc;
975 uid = pc.getPackageUid(packageName, 0);
976 if (uid <= 0) {
977 ALOGE("Unknown package: '%s'", String8(packageName).string());
978 dprintf(err, "Unknown package: '%s'\n", String8(packageName).string());
979 return BAD_VALUE;
980 }
981
982 if (userId < 0) {
983 ALOGE("Invalid user: %d", userId);
984 dprintf(err, "Invalid user: %d\n", userId);
985 return BAD_VALUE;
986 }
987
988 uid = multiuser_get_uid(userId, uid);
989 return NO_ERROR;
990}
991
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800992Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700993 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
994 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500995
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700996#ifdef __BRILLO__
997 UNUSED(clientName8);
998 UNUSED(clientUid);
999 UNUSED(clientPid);
1000 UNUSED(originalClientPid);
1001#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001002 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
1003 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001004 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -08001005 return allowed;
1006 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -07001007#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -08001008
Jayant Chowdhary12361932018-08-27 14:46:13 -07001009 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001010
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001011 if (!mInitialized) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001012 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
1013 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001014 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1015 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -07001016 }
1017
Ruben Brunkcc776712015-02-17 20:18:47 -08001018 if (getCameraState(cameraId) == nullptr) {
1019 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1020 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001021 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1022 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023 }
1024
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001025 status_t err = checkIfDeviceIsUsable(cameraId);
1026 if (err != NO_ERROR) {
1027 switch(err) {
1028 case -ENODEV:
1029 case -EBUSY:
1030 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1031 "No camera device with ID \"%s\" currently available", cameraId.string());
1032 default:
1033 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1034 "Unknown error connecting to ID \"%s\"", cameraId.string());
1035 }
1036 }
1037 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -08001038}
1039
Eino-Ville Talvala04926862016-03-02 15:42:53 -08001040Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -07001041 const String8& clientName8, int& clientUid, int& clientPid,
1042 /*out*/int& originalClientPid) const {
Jayant Chowdhary12361932018-08-27 14:46:13 -07001043 int callingPid = CameraThreadState::getCallingPid();
1044 int callingUid = CameraThreadState::getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001045
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001046 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001048 clientUid = callingUid;
1049 } else if (!isTrustedCallingUid(callingUid)) {
1050 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1051 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001052 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1053 "Untrusted caller (calling PID %d, UID %d) trying to "
1054 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1055 callingPid, callingUid, cameraId.string(),
1056 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057 }
1058
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001059 // Check if we can trust clientPid
1060 if (clientPid == USE_CALLING_PID) {
1061 clientPid = callingPid;
1062 } else if (!isTrustedCallingUid(callingUid)) {
1063 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
1064 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001065 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1066 "Untrusted caller (calling PID %d, UID %d) trying to "
1067 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1068 callingPid, callingUid, cameraId.string(),
1069 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001070 }
1071
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001072 if (shouldRejectSystemCameraConnection(cameraId)) {
1073 ALOGW("Attempting to connect to system-only camera id %s, connection rejected",
1074 cameraId.c_str());
1075 return STATUS_ERROR_FMT(ERROR_DISCONNECTED, "No camera device with ID \"%s\" is"
1076 "available", cameraId.string());
1077 }
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -07001078 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
1079 if (getSystemCameraKind(cameraId, &deviceKind) != OK) {
1080 ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string());
1081 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT, "No camera device with ID \"%s\""
1082 "found while trying to query device kind", cameraId.string());
1083
1084 }
1085
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001086 // If it's not calling from cameraserver, check the permission if the
1087 // device isn't a system only camera (shouldRejectSystemCameraConnection already checks for
1088 // android.permission.SYSTEM_CAMERA for system only camera devices).
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001089 if (callingPid != getpid() &&
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -07001090 (deviceKind != SystemCameraKind::SYSTEM_ONLY_CAMERA) &&
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001091 !checkPermission(sCameraPermission, clientPid, clientUid)) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001092 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001093 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1094 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1095 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001096 }
1097
Svet Ganova453d0d2018-01-11 15:37:58 -08001098 // Make sure the UID is in an active state to use the camera
Svet Ganov7b4ab782018-03-25 12:48:10 -07001099 if (!mUidPolicy->isUidActive(callingUid, String16(clientName8))) {
Varun Shahb42f1eb2019-04-16 14:45:13 -07001100 int32_t procState = mUidPolicy->getProcState(callingUid);
Svet Ganova453d0d2018-01-11 15:37:58 -08001101 ALOGE("Access Denial: can't use the camera from an idle UID pid=%d, uid=%d",
1102 clientPid, clientUid);
1103 return STATUS_ERROR_FMT(ERROR_DISABLED,
Varun Shahb42f1eb2019-04-16 14:45:13 -07001104 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" from background ("
1105 "calling UID %d proc state %" PRId32 ")",
1106 clientName8.string(), clientUid, clientPid, cameraId.string(),
1107 callingUid, procState);
Svet Ganova453d0d2018-01-11 15:37:58 -08001108 }
1109
Michael Grooverd1d435a2018-12-18 17:39:42 -08001110 // If sensor privacy is enabled then prevent access to the camera
1111 if (mSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
1112 ALOGE("Access Denial: cannot use the camera when sensor privacy is enabled");
1113 return STATUS_ERROR_FMT(ERROR_DISABLED,
1114 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" when sensor privacy "
1115 "is enabled", clientName8.string(), clientUid, clientPid, cameraId.string());
1116 }
1117
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001118 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1119 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001120 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001121 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001122
Ruben Brunk6267b532015-04-30 17:44:07 -07001123 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001124
Ruben Brunka8ca9152015-04-07 14:23:40 -07001125 // Only allow clients who are being used by the current foreground device user, unless calling
Jayant Chowdhary8ec41c12019-02-21 20:17:22 -08001126 // from our own process OR the caller is using the cameraserver's HIDL interface.
1127 if (!hardware::IPCThreadState::self()->isServingCall() && callingPid != getpid() &&
1128 (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001129 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1130 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1131 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001132 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1133 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1134 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001135 }
1136
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001137 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001138}
1139
1140status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1141 auto cameraState = getCameraState(cameraId);
Jayant Chowdhary12361932018-08-27 14:46:13 -07001142 int callingPid = CameraThreadState::getCallingPid();
Ruben Brunkcc776712015-02-17 20:18:47 -08001143 if (cameraState == nullptr) {
1144 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1145 cameraId.string());
1146 return -ENODEV;
1147 }
1148
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001149 StatusInternal currentStatus = cameraState->getStatus();
1150 if (currentStatus == StatusInternal::NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001151 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1152 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001153 return -ENODEV;
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001154 } else if (currentStatus == StatusInternal::ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001155 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1156 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001157 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001158 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001159
Ruben Brunkcc776712015-02-17 20:18:47 -08001160 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001161}
1162
Ruben Brunkcc776712015-02-17 20:18:47 -08001163void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1164 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001165
Ruben Brunkcc776712015-02-17 20:18:47 -08001166 // Make a descriptor for the incoming client
1167 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1168 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1169
1170 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1171 String8(client->getPackageName()));
1172
1173 if (evicted.size() > 0) {
1174 // This should never happen - clients should already have been removed in disconnect
1175 for (auto& i : evicted) {
1176 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1177 __FUNCTION__, i->getKey().string());
1178 }
1179
1180 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1181 __FUNCTION__);
1182 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001183
1184 // And register a death notification for the client callback. Do
1185 // this last to avoid Binder policy where a nested Binder
1186 // transaction might be pre-empted to service the client death
1187 // notification if the client process dies before linkToDeath is
1188 // invoked.
1189 sp<IBinder> remoteCallback = client->getRemote();
1190 if (remoteCallback != nullptr) {
1191 remoteCallback->linkToDeath(this);
1192 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001193}
1194
1195status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1196 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1197 /*out*/
1198 sp<BasicClient>* client,
1199 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001200 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001201 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001202 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001203 DescriptorPtr clientDescriptor;
1204 {
1205 if (effectiveApiLevel == API_1) {
1206 // If we are using API1, any existing client for this camera ID with the same remote
1207 // should be returned rather than evicted to allow MediaRecorder to work properly.
1208
1209 auto current = mActiveClientManager.get(cameraId);
1210 if (current != nullptr) {
1211 auto clientSp = current->getValue();
1212 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001213 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1214 ALOGW("CameraService connect called from same client, but with a different"
1215 " API level, evicting prior client...");
1216 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001217 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001218 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001219 *client = clientSp;
1220 return NO_ERROR;
1221 }
1222 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001223 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001224 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001225
Ruben Brunkcc776712015-02-17 20:18:47 -08001226 // Get current active client PIDs
1227 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1228 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001229
Emilian Peev8131a262017-02-01 12:33:43 +00001230 std::vector<int> priorityScores(ownerPids.size());
1231 std::vector<int> states(ownerPids.size());
Igor Murashkine6800ce2013-03-04 17:25:57 -08001232
Emilian Peev8131a262017-02-01 12:33:43 +00001233 // Get priority scores of all active PIDs
1234 status_t err = ProcessInfoService::getProcessStatesScoresFromPids(
1235 ownerPids.size(), &ownerPids[0], /*out*/&states[0],
1236 /*out*/&priorityScores[0]);
1237 if (err != OK) {
1238 ALOGE("%s: Priority score query failed: %d",
1239 __FUNCTION__, err);
1240 return err;
1241 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001242
Ruben Brunkcc776712015-02-17 20:18:47 -08001243 // Update all active clients' priorities
Emilian Peev8131a262017-02-01 12:33:43 +00001244 std::map<int,resource_policy::ClientPriority> pidToPriorityMap;
Ruben Brunkcc776712015-02-17 20:18:47 -08001245 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
Emilian Peev8131a262017-02-01 12:33:43 +00001246 pidToPriorityMap.emplace(ownerPids[i],
Jayant Chowdharyc578a502019-05-08 10:57:54 -07001247 resource_policy::ClientPriority(priorityScores[i], states[i],
1248 /* isVendorClient won't get copied over*/ false));
Ruben Brunkcc776712015-02-17 20:18:47 -08001249 }
1250 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001251
Ruben Brunkcc776712015-02-17 20:18:47 -08001252 // Get state for the given cameraId
1253 auto state = getCameraState(cameraId);
1254 if (state == nullptr) {
1255 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1256 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001257 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001258 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001259 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001260
1261 // Make descriptor for incoming client
Shuzhen Wang2db86ff2018-04-25 01:11:17 +00001262 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -08001263 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1264 state->getConflicting(),
Emilian Peev8131a262017-02-01 12:33:43 +00001265 priorityScores[priorityScores.size() - 1],
1266 clientPid,
1267 states[states.size() - 1]);
Ruben Brunkcc776712015-02-17 20:18:47 -08001268
Jayant Chowdhary5bf11bf2019-06-24 19:42:56 -07001269 resource_policy::ClientPriority clientPriority = clientDescriptor->getPriority();
1270
Ruben Brunkcc776712015-02-17 20:18:47 -08001271 // Find clients that would be evicted
1272 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1273
1274 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1275 // background, so we cannot do evictions
1276 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1277 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1278 " priority).", clientPid);
1279
1280 sp<BasicClient> clientSp = clientDescriptor->getValue();
1281 String8 curTime = getFormattedCurrentTime();
1282 auto incompatibleClients =
1283 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1284
1285 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Emilian Peev8131a262017-02-01 12:33:43 +00001286 "(PID %d, score %d state %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001287 cameraId.string(), packageName.string(), clientPid,
Jayant Chowdhary5bf11bf2019-06-24 19:42:56 -07001288 clientPriority.getScore(), clientPriority.getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08001289
1290 for (auto& i : incompatibleClients) {
1291 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
Emilian Peev8131a262017-02-01 12:33:43 +00001292 "(PID %" PRId32 ", score %" PRId32 ", state %" PRId32 ")",
1293 i->getKey().string(),
1294 String8{i->getValue()->getPackageName()}.string(),
1295 i->getOwnerId(), i->getPriority().getScore(),
1296 i->getPriority().getState());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001297 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00001298 PRId32 ", score %" PRId32 ", state %" PRId32 ")", i->getKey().string(),
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001299 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
Emilian Peev8131a262017-02-01 12:33:43 +00001300 i->getPriority().getScore(), i->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08001301 }
1302
1303 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001304 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001305 mEventLog.add(msg);
1306
1307 return -EBUSY;
1308 }
1309
1310 for (auto& i : evicted) {
1311 sp<BasicClient> clientSp = i->getValue();
1312 if (clientSp.get() == nullptr) {
1313 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1314
1315 // TODO: Remove this
1316 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1317 __FUNCTION__);
1318 mActiveClientManager.remove(i);
1319 continue;
1320 }
1321
1322 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1323 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001324 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001325
Ruben Brunkcc776712015-02-17 20:18:47 -08001326 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001327 logEvent(String8::format("EVICT device %s client held by package %s (PID"
Emilian Peev8131a262017-02-01 12:33:43 +00001328 " %" PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted by device %s client for"
1329 " package %s (PID %d, score %" PRId32 ", state %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001330 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
Emilian Peev8131a262017-02-01 12:33:43 +00001331 i->getOwnerId(), i->getPriority().getScore(),
1332 i->getPriority().getState(), cameraId.string(),
Jayant Chowdhary5bf11bf2019-06-24 19:42:56 -07001333 packageName.string(), clientPid, clientPriority.getScore(),
1334 clientPriority.getState()));
Ruben Brunkcc776712015-02-17 20:18:47 -08001335
1336 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001337 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001338 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001339 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001340 }
1341
Ruben Brunkcc776712015-02-17 20:18:47 -08001342 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1343 // other clients from connecting in mServiceLockWrapper if held
1344 mServiceLock.unlock();
1345
1346 // Clear caller identity temporarily so client disconnect PID checks work correctly
Jayant Chowdhary12361932018-08-27 14:46:13 -07001347 int64_t token = CameraThreadState::clearCallingIdentity();
Ruben Brunkcc776712015-02-17 20:18:47 -08001348
1349 // Destroy evicted clients
1350 for (auto& i : evictedClients) {
1351 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001352 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001353 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001354
Jayant Chowdhary12361932018-08-27 14:46:13 -07001355 CameraThreadState::restoreCallingIdentity(token);
Ruben Brunkcc776712015-02-17 20:18:47 -08001356
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001357 for (const auto& i : evictedClients) {
1358 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1359 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1360 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1361 if (ret == TIMED_OUT) {
1362 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1363 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1364 mActiveClientManager.toString().string());
1365 return -EBUSY;
1366 }
1367 if (ret != NO_ERROR) {
1368 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1369 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1370 ret, mActiveClientManager.toString().string());
1371 return ret;
1372 }
1373 }
1374
1375 evictedClients.clear();
1376
Ruben Brunkcc776712015-02-17 20:18:47 -08001377 // Once clients have been disconnected, relock
1378 mServiceLock.lock();
1379
1380 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1381 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1382 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001383 }
1384
Ruben Brunkcc776712015-02-17 20:18:47 -08001385 *partial = clientDescriptor;
1386 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001387}
1388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001390 const sp<ICameraClient>& cameraClient,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001391 int api1CameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001392 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001393 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001394 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001395 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001396 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001397
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001398 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001399 Status ret = Status::ok();
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001400
1401 String8 id = cameraIdIntToStr(api1CameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001402 sp<Client> client = nullptr;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001403 ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08001404 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, std::unique_ptr<String16>(),
1405 clientUid, clientPid, API_1, /*shimUpdateOnly*/ false, /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001406
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001407 if(!ret.isOk()) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07001408 logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001409 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001410 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001411 }
1412
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001413 *device = client;
1414 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001415}
1416
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001417Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001418 const sp<ICameraClient>& cameraClient,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001419 int api1CameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001420 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001421 int clientUid,
1422 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001424
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001425 ATRACE_CALL();
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001426 String8 id = cameraIdIntToStr(api1CameraId);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001427
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001428 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001429 sp<Client> client = nullptr;
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001430 ret = connectHelper<ICameraClient,Client>(cameraClient, id, api1CameraId, halVersion,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08001431 clientPackageName, std::unique_ptr<String16>(), clientUid, USE_CALLING_PID, API_1,
1432 /*shimUpdateOnly*/ false, /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001433
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001434 if(!ret.isOk()) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07001435 logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001436 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001437 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001438 }
1439
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001440 *device = client;
1441 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001442}
1443
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -07001444bool CameraService::shouldSkipStatusUpdates(SystemCameraKind systemCameraKind,
1445 bool isVendorListener, int clientPid, int clientUid) {
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001446 // If the client is not a vendor client, don't add listener if
1447 // a) the camera is a publicly hidden secure camera OR
1448 // b) the camera is a system only camera and the client doesn't
1449 // have android.permission.SYSTEM_CAMERA permissions.
1450 if (!isVendorListener && (systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA ||
1451 (systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA &&
1452 !hasPermissionsForSystemCamera(clientPid, clientUid)))) {
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -08001453 return true;
1454 }
1455 return false;
1456}
1457
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001458bool CameraService::shouldRejectSystemCameraConnection(const String8& cameraId) const {
1459 // Rules for rejection:
1460 // 1) If cameraserver tries to access this camera device, accept the
1461 // connection.
1462 // 2) The camera device is a publicly hidden secure camera device AND some
1463 // component is trying to access it on a non-hwbinder thread (generally a non HAL client),
1464 // reject it.
1465 // 3) if the camera device is advertised by the camera HAL as SYSTEM_ONLY
1466 // and the serving thread is a non hwbinder thread, the client must have
1467 // android.permission.SYSTEM_CAMERA permissions to connect.
1468
1469 int cPid = CameraThreadState::getCallingPid();
1470 int cUid = CameraThreadState::getCallingUid();
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -07001471 SystemCameraKind systemCameraKind = SystemCameraKind::PUBLIC;
1472 if (getSystemCameraKind(cameraId, &systemCameraKind) != OK) {
1473 ALOGE("%s: Invalid camera id %s, ", __FUNCTION__, cameraId.c_str());
1474 return true;
1475 }
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001476
1477 // (1) Cameraserver trying to connect, accept.
1478 if (CameraThreadState::getCallingPid() == getpid()) {
1479 return false;
1480 }
1481 // (2)
1482 if (!hardware::IPCThreadState::self()->isServingCall() &&
1483 systemCameraKind == SystemCameraKind::HIDDEN_SECURE_CAMERA) {
1484 ALOGW("Rejecting access to secure hidden camera %s", cameraId.c_str());
1485 return true;
1486 }
1487 // (3) Here we only check for permissions if it is a system only camera device. This is since
1488 // getCameraCharacteristics() allows for calls to succeed (albeit after hiding some
1489 // characteristics) even if clients don't have android.permission.CAMERA. We do not want the
1490 // same behavior for system camera devices.
1491 if (!hardware::IPCThreadState::self()->isServingCall() &&
1492 systemCameraKind == SystemCameraKind::SYSTEM_ONLY_CAMERA &&
1493 !hasPermissionsForSystemCamera(cPid, cUid)) {
1494 ALOGW("Rejecting access to system only camera %s, inadequete permissions",
1495 cameraId.c_str());
1496 return true;
1497 }
1498
1499 return false;
1500}
1501
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001502Status CameraService::connectDevice(
1503 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001504 const String16& cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001505 const String16& clientPackageName,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08001506 const std::unique_ptr<String16>& clientFeatureId,
Ruben Brunkcc776712015-02-17 20:18:47 -08001507 int clientUid,
1508 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001509 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001510
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001511 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001512 Status ret = Status::ok();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001513 String8 id = String8(cameraId);
Ruben Brunkcc776712015-02-17 20:18:47 -08001514 sp<CameraDeviceClient> client = nullptr;
Jayant Chowdhary5bf11bf2019-06-24 19:42:56 -07001515 String16 clientPackageNameAdj = clientPackageName;
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08001516
Jayant Chowdhary5bf11bf2019-06-24 19:42:56 -07001517 if (hardware::IPCThreadState::self()->isServingCall()) {
1518 std::string vendorClient =
1519 StringPrintf("vendor.client.pid<%d>", CameraThreadState::getCallingPid());
1520 clientPackageNameAdj = String16(vendorClient.c_str());
1521 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001522 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001523 /*api1CameraId*/-1,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08001524 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageNameAdj, clientFeatureId,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -07001525 clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001526
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001527 if(!ret.isOk()) {
Jayant Chowdhary5bf11bf2019-06-24 19:42:56 -07001528 logRejected(id, CameraThreadState::getCallingPid(), String8(clientPackageNameAdj),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001529 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001530 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001531 }
1532
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001533 *device = client;
1534 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001535}
1536
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001537template<class CALLBACK, class CLIENT>
1538Status CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08001539 int api1CameraId, int halVersion, const String16& clientPackageName,
1540 const std::unique_ptr<String16>& clientFeatureId, int clientUid, int clientPid,
1541 apiLevel effectiveApiLevel, bool shimUpdateOnly,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001542 /*out*/sp<CLIENT>& device) {
1543 binder::Status ret = binder::Status::ok();
1544
1545 String8 clientName8(clientPackageName);
1546
1547 int originalClientPid = 0;
1548
1549 ALOGI("CameraService::connect call (PID %d \"%s\", camera ID %s) for HAL version %s and "
1550 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
1551 (halVersion == -1) ? "default" : std::to_string(halVersion).c_str(),
1552 static_cast<int>(effectiveApiLevel));
1553
1554 sp<CLIENT> client = nullptr;
1555 {
1556 // Acquire mServiceLock and prevent other clients from connecting
1557 std::unique_ptr<AutoConditionLock> lock =
1558 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
1559
1560 if (lock == nullptr) {
1561 ALOGE("CameraService::connect (PID %d) rejected (too many other clients connecting)."
1562 , clientPid);
1563 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1564 "Cannot open camera %s for \"%s\" (PID %d): Too many other clients connecting",
1565 cameraId.string(), clientName8.string(), clientPid);
1566 }
1567
1568 // Enforce client permissions and do basic sanity checks
1569 if(!(ret = validateConnectLocked(cameraId, clientName8,
1570 /*inout*/clientUid, /*inout*/clientPid, /*out*/originalClientPid)).isOk()) {
1571 return ret;
1572 }
1573
1574 // Check the shim parameters after acquiring lock, if they have already been updated and
1575 // we were doing a shim update, return immediately
1576 if (shimUpdateOnly) {
1577 auto cameraState = getCameraState(cameraId);
1578 if (cameraState != nullptr) {
1579 if (!cameraState->getShimParams().isEmpty()) return ret;
1580 }
1581 }
1582
1583 status_t err;
1584
1585 sp<BasicClient> clientTmp = nullptr;
1586 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
1587 if ((err = handleEvictionsLocked(cameraId, originalClientPid, effectiveApiLevel,
1588 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
1589 /*out*/&partial)) != NO_ERROR) {
1590 switch (err) {
1591 case -ENODEV:
1592 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
1593 "No camera device with ID \"%s\" currently available",
1594 cameraId.string());
1595 case -EBUSY:
1596 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1597 "Higher-priority client using camera, ID \"%s\" currently unavailable",
1598 cameraId.string());
1599 default:
1600 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1601 "Unexpected error %s (%d) opening camera \"%s\"",
1602 strerror(-err), err, cameraId.string());
1603 }
1604 }
1605
1606 if (clientTmp.get() != nullptr) {
1607 // Handle special case for API1 MediaRecorder where the existing client is returned
1608 device = static_cast<CLIENT*>(clientTmp.get());
1609 return ret;
1610 }
1611
1612 // give flashlight a chance to close devices if necessary.
1613 mFlashlight->prepareDeviceOpen(cameraId);
1614
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001615 int facing = -1;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001616 int deviceVersion = getDeviceVersion(cameraId, /*out*/&facing);
Eino-Ville Talvala6963d0a2017-01-31 13:00:34 -08001617 if (facing == -1) {
1618 ALOGE("%s: Unable to get camera device \"%s\" facing", __FUNCTION__, cameraId.string());
1619 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1620 "Unable to get camera device \"%s\" facing", cameraId.string());
1621 }
1622
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001623 sp<BasicClient> tmp = nullptr;
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08001624 if(!(ret = makeClient(this, cameraCb, clientPackageName, clientFeatureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001625 cameraId, api1CameraId, facing,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -07001626 clientPid, clientUid, getpid(),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08001627 halVersion, deviceVersion, effectiveApiLevel,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001628 /*out*/&tmp)).isOk()) {
1629 return ret;
1630 }
1631 client = static_cast<CLIENT*>(tmp.get());
1632
1633 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
1634 __FUNCTION__);
1635
Emilian Peevbd8c5032018-02-14 23:05:40 +00001636 err = client->initialize(mCameraProviderManager, mMonitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001637 if (err != OK) {
1638 ALOGE("%s: Could not initialize client from HAL.", __FUNCTION__);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001639 // Errors could be from the HAL module open call or from AppOpsManager
1640 switch(err) {
1641 case BAD_VALUE:
1642 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1643 "Illegal argument to HAL module for camera \"%s\"", cameraId.string());
1644 case -EBUSY:
1645 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1646 "Camera \"%s\" is already open", cameraId.string());
1647 case -EUSERS:
1648 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1649 "Too many cameras already open, cannot open camera \"%s\"",
1650 cameraId.string());
1651 case PERMISSION_DENIED:
1652 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1653 "No permission to open camera \"%s\"", cameraId.string());
1654 case -EACCES:
1655 return STATUS_ERROR_FMT(ERROR_DISABLED,
1656 "Camera \"%s\" disabled by policy", cameraId.string());
1657 case -ENODEV:
1658 default:
1659 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1660 "Failed to initialize camera \"%s\": %s (%d)", cameraId.string(),
1661 strerror(-err), err);
1662 }
1663 }
1664
1665 // Update shim paremeters for legacy clients
1666 if (effectiveApiLevel == API_1) {
1667 // Assume we have always received a Client subclass for API1
1668 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
1669 String8 rawParams = shimClient->getParameters();
1670 CameraParameters params(rawParams);
1671
1672 auto cameraState = getCameraState(cameraId);
1673 if (cameraState != nullptr) {
1674 cameraState->setShimParams(params);
1675 } else {
1676 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
1677 __FUNCTION__, cameraId.string());
1678 }
1679 }
1680
1681 if (shimUpdateOnly) {
1682 // If only updating legacy shim parameters, immediately disconnect client
1683 mServiceLock.unlock();
1684 client->disconnect();
1685 mServiceLock.lock();
1686 } else {
1687 // Otherwise, add client to active clients list
1688 finishConnectLocked(client, partial);
1689 }
1690 } // lock is destroyed, allow further connect calls
1691
1692 // Important: release the mutex here so the client can call back into the service from its
1693 // destructor (can be at the end of the call)
1694 device = client;
1695 return ret;
1696}
1697
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001698Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001699 const sp<IBinder>& clientBinder) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001700 Mutex::Autolock lock(mServiceLock);
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001701
1702 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001703 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001704 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001705 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1706 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001707 }
1708
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001709 String8 id = String8(cameraId.string());
Jayant Chowdhary12361932018-08-27 14:46:13 -07001710 int uid = CameraThreadState::getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001711
1712 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001713 auto state = getCameraState(id);
1714 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001715 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001716 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1717 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001718 }
1719
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001720 StatusInternal cameraStatus = state->getStatus();
1721 if (cameraStatus != StatusInternal::PRESENT &&
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001722 cameraStatus != StatusInternal::NOT_AVAILABLE) {
1723 ALOGE("%s: camera id is invalid %s, status %d", __FUNCTION__, id.string(), (int)cameraStatus);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001724 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1725 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001726 }
1727
1728 {
1729 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001730 TorchModeStatus status;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001731 status_t err = getTorchStatusLocked(id, &status);
1732 if (err != OK) {
1733 if (err == NAME_NOT_FOUND) {
1734 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1735 "Camera \"%s\" does not have a flash unit", id.string());
1736 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001737 ALOGE("%s: getting current torch status failed for camera %s",
1738 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001739 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1740 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1741 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001742 }
1743
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001744 if (status == TorchModeStatus::NOT_AVAILABLE) {
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001745 if (cameraStatus == StatusInternal::NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001746 ALOGE("%s: torch mode of camera %s is not available because "
1747 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001748 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1749 "Torch for camera \"%s\" is not available due to an existing camera user",
1750 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001751 } else {
1752 ALOGE("%s: torch mode of camera %s is not available due to "
1753 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001754 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1755 "Torch for camera \"%s\" is not available due to insufficient resources",
1756 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001757 }
1758 }
1759 }
1760
Ruben Brunk99e69712015-05-26 17:25:07 -07001761 {
1762 // Update UID map - this is used in the torch status changed callbacks, so must be done
1763 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001764 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001765 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1766 mTorchUidMap[id].first = uid;
1767 mTorchUidMap[id].second = uid;
1768 } else {
1769 // Set the pending UID
1770 mTorchUidMap[id].first = uid;
1771 }
1772 }
1773
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001774 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001775
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001776 if (err != OK) {
1777 int32_t errorCode;
1778 String8 msg;
1779 switch (err) {
1780 case -ENOSYS:
1781 msg = String8::format("Camera \"%s\" has no flashlight",
1782 id.string());
1783 errorCode = ERROR_ILLEGAL_ARGUMENT;
1784 break;
1785 default:
1786 msg = String8::format(
1787 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1788 id.string(), enabled, strerror(-err), err);
1789 errorCode = ERROR_INVALID_OPERATION;
1790 }
1791 ALOGE("%s: %s", __FUNCTION__, msg.string());
1792 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001793 }
1794
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001795 {
1796 // update the link to client's death
1797 Mutex::Autolock al(mTorchClientMapMutex);
1798 ssize_t index = mTorchClientMap.indexOfKey(id);
1799 if (enabled) {
1800 if (index == NAME_NOT_FOUND) {
1801 mTorchClientMap.add(id, clientBinder);
1802 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001803 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001804 mTorchClientMap.replaceValueAt(index, clientBinder);
1805 }
1806 clientBinder->linkToDeath(this);
1807 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001808 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001809 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001810 }
1811
Jayant Chowdhary0e2eefd2019-04-18 14:05:43 -07001812 int clientPid = CameraThreadState::getCallingPid();
1813 const char *id_cstr = id.c_str();
1814 const char *torchState = enabled ? "on" : "off";
1815 ALOGI("Torch for camera id %s turned %s for client PID %d", id_cstr, torchState, clientPid);
1816 logTorchEvent(id_cstr, torchState , clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001817 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001818}
1819
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001820Status CameraService::notifySystemEvent(int32_t eventId,
1821 const std::vector<int32_t>& args) {
Jeongik Chaaa5e64c2018-11-17 05:08:04 +09001822 const int pid = CameraThreadState::getCallingPid();
1823 const int selfPid = getpid();
1824
1825 // Permission checks
1826 if (pid != selfPid) {
1827 // Ensure we're being called by system_server, or similar process with
1828 // permissions to notify the camera service about system events
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001829 if (!checkCallingPermission(sCameraSendSystemEventsPermission)) {
Jeongik Chaaa5e64c2018-11-17 05:08:04 +09001830 const int uid = CameraThreadState::getCallingUid();
1831 ALOGE("Permission Denial: cannot send updates to camera service about system"
1832 " events from pid=%d, uid=%d", pid, uid);
1833 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
Jeongik Chaaa1b8152018-11-21 10:02:25 +09001834 "No permission to send updates to camera service about system events"
1835 " from pid=%d, uid=%d", pid, uid);
Jeongik Chaaa5e64c2018-11-17 05:08:04 +09001836 }
1837 }
1838
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001839 ATRACE_CALL();
1840
Ruben Brunk36597b22015-03-20 22:15:57 -07001841 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001842 case ICameraService::EVENT_USER_SWITCHED: {
Michael Grooverd1d435a2018-12-18 17:39:42 -08001843 // Try to register for UID and sensor privacy policy updates, in case we're recovering
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07001844 // from a system server crash
1845 mUidPolicy->registerSelf();
Michael Grooverd1d435a2018-12-18 17:39:42 -08001846 mSensorPrivacyPolicy->registerSelf();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001847 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001848 break;
1849 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001850 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001851 default: {
1852 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1853 eventId);
1854 break;
1855 }
1856 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001857 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001858}
1859
Emilian Peev53722fa2019-02-22 17:47:20 -08001860void CameraService::notifyMonitoredUids() {
1861 Mutex::Autolock lock(mStatusListenerLock);
1862
1863 for (const auto& it : mListenerList) {
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001864 auto ret = it->getListener()->onCameraAccessPrioritiesChanged();
Emilian Peev53722fa2019-02-22 17:47:20 -08001865 if (!ret.isOk()) {
1866 ALOGE("%s: Failed to trigger permission callback: %d", __FUNCTION__,
1867 ret.exceptionCode());
1868 }
1869 }
1870}
1871
Eino-Ville Talvala63f36112018-12-06 14:57:03 -08001872Status CameraService::notifyDeviceStateChange(int64_t newState) {
1873 const int pid = CameraThreadState::getCallingPid();
1874 const int selfPid = getpid();
1875
1876 // Permission checks
1877 if (pid != selfPid) {
1878 // Ensure we're being called by system_server, or similar process with
1879 // permissions to notify the camera service about system events
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001880 if (!checkCallingPermission(sCameraSendSystemEventsPermission)) {
Eino-Ville Talvala63f36112018-12-06 14:57:03 -08001881 const int uid = CameraThreadState::getCallingUid();
1882 ALOGE("Permission Denial: cannot send updates to camera service about device"
1883 " state changes from pid=%d, uid=%d", pid, uid);
1884 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1885 "No permission to send updates to camera service about device state"
1886 " changes from pid=%d, uid=%d", pid, uid);
1887 }
1888 }
1889
1890 ATRACE_CALL();
1891
1892 using hardware::camera::provider::V2_5::DeviceState;
1893 hardware::hidl_bitfield<DeviceState> newDeviceState{};
1894 if (newState & ICameraService::DEVICE_STATE_BACK_COVERED) {
1895 newDeviceState |= DeviceState::BACK_COVERED;
1896 }
1897 if (newState & ICameraService::DEVICE_STATE_FRONT_COVERED) {
1898 newDeviceState |= DeviceState::FRONT_COVERED;
1899 }
1900 if (newState & ICameraService::DEVICE_STATE_FOLDED) {
1901 newDeviceState |= DeviceState::FOLDED;
1902 }
1903 // Only map vendor bits directly
1904 uint64_t vendorBits = static_cast<uint64_t>(newState) & 0xFFFFFFFF00000000l;
1905 newDeviceState |= vendorBits;
1906
1907 ALOGV("%s: New device state 0x%" PRIx64, __FUNCTION__, newDeviceState);
1908 Mutex::Autolock l(mServiceLock);
1909 mCameraProviderManager->notifyDeviceStateChange(newDeviceState);
1910
1911 return Status::ok();
1912}
1913
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001914Status CameraService::addListener(const sp<ICameraServiceListener>& listener,
1915 /*out*/
1916 std::vector<hardware::CameraStatus> *cameraStatuses) {
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -08001917 return addListenerHelper(listener, cameraStatuses);
1918}
1919
1920Status CameraService::addListenerHelper(const sp<ICameraServiceListener>& listener,
1921 /*out*/
1922 std::vector<hardware::CameraStatus> *cameraStatuses,
1923 bool isVendorListener) {
1924
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001925 ATRACE_CALL();
1926
Igor Murashkinbfc99152013-02-27 12:55:20 -08001927 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001928
Ruben Brunk3450ba72015-06-16 11:00:37 -07001929 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001930 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001931 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001932 }
1933
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001934 auto clientUid = CameraThreadState::getCallingUid();
1935 auto clientPid = CameraThreadState::getCallingPid();
1936
Igor Murashkinbfc99152013-02-27 12:55:20 -08001937 Mutex::Autolock lock(mServiceLock);
1938
Ruben Brunkcc776712015-02-17 20:18:47 -08001939 {
1940 Mutex::Autolock lock(mStatusListenerLock);
Emilian Peev53722fa2019-02-22 17:47:20 -08001941 for (const auto &it : mListenerList) {
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001942 if (IInterface::asBinder(it->getListener()) == IInterface::asBinder(listener)) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001943 ALOGW("%s: Tried to add listener %p which was already subscribed",
1944 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001945 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001946 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001947 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001948
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001949 sp<ServiceListener> serviceListener =
1950 new ServiceListener(this, listener, clientUid, clientPid, isVendorListener);
Emilian Peev53722fa2019-02-22 17:47:20 -08001951 auto ret = serviceListener->initialize();
1952 if (ret != NO_ERROR) {
1953 String8 msg = String8::format("Failed to initialize service listener: %s (%d)",
1954 strerror(-ret), ret);
1955 ALOGE("%s: %s", __FUNCTION__, msg.string());
1956 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
1957 }
Jayant Chowdhary5216b212019-07-17 09:26:23 -07001958 // The listener still needs to be added to the list of listeners, regardless of what
1959 // permissions the listener process has / whether it is a vendor listener. Since it might be
1960 // eligible to listen to other camera ids.
1961 mListenerList.emplace_back(serviceListener);
Emilian Peev53722fa2019-02-22 17:47:20 -08001962 mUidPolicy->registerMonitorUid(clientUid);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001963 }
1964
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001965 /* Collect current devices and status */
Igor Murashkincba2c162013-03-20 15:56:31 -07001966 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001967 Mutex::Autolock lock(mCameraStatesLock);
1968 for (auto& i : mCameraStates) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001969 cameraStatuses->emplace_back(i.first, mapToInterface(i.second->getStatus()));
Igor Murashkincba2c162013-03-20 15:56:31 -07001970 }
1971 }
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -07001972 // Remove the camera statuses that should be hidden from the client, we do
1973 // this after collecting the states in order to avoid holding
1974 // mCameraStatesLock and mInterfaceLock (held in getSystemCameraKind()) at
1975 // the same time.
1976 cameraStatuses->erase(std::remove_if(cameraStatuses->begin(), cameraStatuses->end(),
1977 [this, &isVendorListener, &clientPid, &clientUid](const hardware::CameraStatus& s) {
1978 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
1979 if (getSystemCameraKind(s.cameraId, &deviceKind) != OK) {
1980 ALOGE("%s: Invalid camera id %s, skipping status update",
1981 __FUNCTION__, s.cameraId.c_str());
1982 return true;
1983 }
1984 return shouldSkipStatusUpdates(deviceKind, isVendorListener, clientPid,
1985 clientUid);}), cameraStatuses->end());
1986
Igor Murashkincba2c162013-03-20 15:56:31 -07001987
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001988 /*
1989 * Immediately signal current torch status to this listener only
1990 * This may be a subset of all the devices, so don't include it in the response directly
1991 */
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001992 {
1993 Mutex::Autolock al(mTorchStatusMutex);
1994 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001995 String16 id = String16(mTorchStatusMap.keyAt(i).string());
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08001996 listener->onTorchStatusChanged(mapToInterface(mTorchStatusMap.valueAt(i)), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001997 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001998 }
1999
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002000 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08002001}
Ruben Brunkcc776712015-02-17 20:18:47 -08002002
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002003Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002004 ATRACE_CALL();
2005
Igor Murashkinbfc99152013-02-27 12:55:20 -08002006 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
2007
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07002008 if (listener == 0) {
2009 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002010 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07002011 }
2012
Igor Murashkinbfc99152013-02-27 12:55:20 -08002013 Mutex::Autolock lock(mServiceLock);
2014
Ruben Brunkcc776712015-02-17 20:18:47 -08002015 {
2016 Mutex::Autolock lock(mStatusListenerLock);
2017 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
Jayant Chowdhary5216b212019-07-17 09:26:23 -07002018 if (IInterface::asBinder((*it)->getListener()) == IInterface::asBinder(listener)) {
2019 mUidPolicy->unregisterMonitorUid((*it)->getListenerUid());
2020 IInterface::asBinder(listener)->unlinkToDeath(*it);
Ruben Brunkcc776712015-02-17 20:18:47 -08002021 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002022 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08002023 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08002024 }
2025 }
2026
2027 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
2028 __FUNCTION__, listener.get());
2029
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002030 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08002031}
2032
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002033Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002034
2035 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07002036 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
2037
2038 if (parameters == NULL) {
2039 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002040 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07002041 }
2042
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002043 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07002044
2045 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002046 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07002047 // Error logged by caller
2048 return ret;
2049 }
2050
2051 String8 shimParamsString8 = shimParams.flatten();
2052 String16 shimParamsString16 = String16(shimParamsString8);
2053
2054 *parameters = shimParamsString16;
2055
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002056 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07002057}
2058
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002059Status CameraService::supportsCameraApi(const String16& cameraId, int apiVersion,
2060 /*out*/ bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002061 ATRACE_CALL();
2062
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002063 const String8 id = String8(cameraId);
2064
2065 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07002066
2067 switch (apiVersion) {
2068 case API_VERSION_1:
2069 case API_VERSION_2:
2070 break;
2071 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002072 String8 msg = String8::format("Unknown API version %d", apiVersion);
2073 ALOGE("%s: %s", __FUNCTION__, msg.string());
2074 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07002075 }
2076
Emilian Peev28ad2ea2017-02-07 16:14:32 +00002077 int deviceVersion = getDeviceVersion(id);
Shuzhen Wangf9d2c022018-08-21 12:07:35 -07002078 switch (deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002079 case CAMERA_DEVICE_API_VERSION_1_0:
2080 case CAMERA_DEVICE_API_VERSION_3_0:
2081 case CAMERA_DEVICE_API_VERSION_3_1:
2082 if (apiVersion == API_VERSION_2) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002083 ALOGV("%s: Camera id %s uses HAL version %d <3.2, doesn't support api2 without shim",
2084 __FUNCTION__, id.string(), deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002085 *isSupported = false;
2086 } else { // if (apiVersion == API_VERSION_1) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002087 ALOGV("%s: Camera id %s uses older HAL before 3.2, but api1 is always supported",
2088 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002089 *isSupported = true;
2090 }
2091 break;
2092 case CAMERA_DEVICE_API_VERSION_3_2:
2093 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07002094 case CAMERA_DEVICE_API_VERSION_3_4:
Shuzhen Wangf9d2c022018-08-21 12:07:35 -07002095 case CAMERA_DEVICE_API_VERSION_3_5:
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002096 ALOGV("%s: Camera id %s uses HAL3.2 or newer, supports api1/api2 directly",
2097 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002098 *isSupported = true;
2099 break;
2100 case -1: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002101 String8 msg = String8::format("Unknown camera ID %s", id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002102 ALOGE("%s: %s", __FUNCTION__, msg.string());
2103 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07002104 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002105 default: {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002106 String8 msg = String8::format("Unknown device version %x for device %s",
2107 deviceVersion, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002108 ALOGE("%s: %s", __FUNCTION__, msg.string());
2109 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
2110 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07002111 }
2112
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002113 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07002114}
2115
Shuzhen Wangf9d2c022018-08-21 12:07:35 -07002116Status CameraService::isHiddenPhysicalCamera(const String16& cameraId,
2117 /*out*/ bool *isSupported) {
2118 ATRACE_CALL();
2119
2120 const String8 id = String8(cameraId);
2121
2122 ALOGV("%s: for camera ID = %s", __FUNCTION__, id.string());
2123 *isSupported = mCameraProviderManager->isHiddenPhysicalCamera(id.string());
2124
2125 return Status::ok();
2126}
2127
Ruben Brunkcc776712015-02-17 20:18:47 -08002128void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002129 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08002130 for (auto& i : mActiveClientManager.getAll()) {
2131 auto clientSp = i->getValue();
2132 if (clientSp.get() == client) {
2133 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08002134 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07002135 }
Yin-Chia Yehdba03232019-08-19 15:54:28 -07002136 updateAudioRestrictionLocked();
Igor Murashkin634a5152013-02-20 17:15:11 -08002137}
2138
Ruben Brunkcc776712015-02-17 20:18:47 -08002139bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002140 bool ret = false;
2141 {
2142 // Acquire mServiceLock and prevent other clients from connecting
2143 std::unique_ptr<AutoConditionLock> lock =
2144 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08002145
Igor Murashkin634a5152013-02-20 17:15:11 -08002146
Ruben Brunkcc776712015-02-17 20:18:47 -08002147 std::vector<sp<BasicClient>> evicted;
2148 for (auto& i : mActiveClientManager.getAll()) {
2149 auto clientSp = i->getValue();
2150 if (clientSp.get() == nullptr) {
2151 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
2152 mActiveClientManager.remove(i);
2153 continue;
2154 }
Yin-Chia Yehdbfcb382018-02-16 11:17:36 -08002155 if (remote == clientSp->getRemote()) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002156 mActiveClientManager.remove(i);
2157 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08002158
Ruben Brunkcc776712015-02-17 20:18:47 -08002159 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002160 clientSp->notifyError(
2161 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08002162 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08002163 }
2164 }
2165
Ruben Brunkcc776712015-02-17 20:18:47 -08002166 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
2167 // other clients from connecting in mServiceLockWrapper if held
2168 mServiceLock.unlock();
2169
Ruben Brunk36597b22015-03-20 22:15:57 -07002170 // Do not clear caller identity, remote caller should be client proccess
2171
Ruben Brunkcc776712015-02-17 20:18:47 -08002172 for (auto& i : evicted) {
2173 if (i.get() != nullptr) {
2174 i->disconnect();
2175 ret = true;
2176 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002177 }
2178
Ruben Brunkcc776712015-02-17 20:18:47 -08002179 // Reacquire mServiceLock
2180 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08002181
Ruben Brunkcc776712015-02-17 20:18:47 -08002182 } // lock is destroyed, allow further connect calls
2183
2184 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07002185}
2186
Ruben Brunkcc776712015-02-17 20:18:47 -08002187std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
2188 const String8& cameraId) const {
2189 std::shared_ptr<CameraState> state;
2190 {
2191 Mutex::Autolock lock(mCameraStatesLock);
2192 auto iter = mCameraStates.find(cameraId);
2193 if (iter != mCameraStates.end()) {
2194 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002195 }
2196 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002197 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002198}
2199
Ruben Brunkcc776712015-02-17 20:18:47 -08002200sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
2201 // Remove from active clients list
2202 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
2203 if (clientDescriptorPtr == nullptr) {
2204 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
2205 cameraId.string());
2206 return sp<BasicClient>{nullptr};
2207 }
2208
2209 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07002210}
2211
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002212void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07002213 // Acquire mServiceLock and prevent other clients from connecting
2214 std::unique_ptr<AutoConditionLock> lock =
2215 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
2216
Ruben Brunk6267b532015-04-30 17:44:07 -07002217 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002218 for (size_t i = 0; i < newUserIds.size(); i++) {
2219 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07002220 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002221 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07002222 return;
2223 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002224 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07002225 }
2226
Ruben Brunka8ca9152015-04-07 14:23:40 -07002227
Ruben Brunk6267b532015-04-30 17:44:07 -07002228 if (newAllowedUsers == mAllowedUsers) {
2229 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
2230 return;
2231 }
2232
2233 logUserSwitch(mAllowedUsers, newAllowedUsers);
2234
2235 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07002236
2237 // Current user has switched, evict all current clients.
2238 std::vector<sp<BasicClient>> evicted;
2239 for (auto& i : mActiveClientManager.getAll()) {
2240 auto clientSp = i->getValue();
2241
2242 if (clientSp.get() == nullptr) {
2243 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
2244 continue;
2245 }
2246
Ruben Brunk6267b532015-04-30 17:44:07 -07002247 // Don't evict clients that are still allowed.
2248 uid_t clientUid = clientSp->getClientUid();
2249 userid_t clientUserId = multiuser_get_user_id(clientUid);
2250 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
2251 continue;
2252 }
2253
Ruben Brunk36597b22015-03-20 22:15:57 -07002254 evicted.push_back(clientSp);
2255
2256 String8 curTime = getFormattedCurrentTime();
2257
2258 ALOGE("Evicting conflicting client for camera ID %s due to user change",
2259 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07002260
Ruben Brunk36597b22015-03-20 22:15:57 -07002261 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002262 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Emilian Peev8131a262017-02-01 12:33:43 +00002263 PRId32 ", score %" PRId32 ", state %" PRId32 ")\n - Evicted due"
2264 " to user switch.", i->getKey().string(),
2265 String8{clientSp->getPackageName()}.string(),
2266 i->getOwnerId(), i->getPriority().getScore(),
2267 i->getPriority().getState()));
Ruben Brunk36597b22015-03-20 22:15:57 -07002268
2269 }
2270
2271 // Do not hold mServiceLock while disconnecting clients, but retain the condition
2272 // blocking other clients from connecting in mServiceLockWrapper if held.
2273 mServiceLock.unlock();
2274
2275 // Clear caller identity temporarily so client disconnect PID checks work correctly
Jayant Chowdhary12361932018-08-27 14:46:13 -07002276 int64_t token = CameraThreadState::clearCallingIdentity();
Ruben Brunk36597b22015-03-20 22:15:57 -07002277
2278 for (auto& i : evicted) {
2279 i->disconnect();
2280 }
2281
Jayant Chowdhary12361932018-08-27 14:46:13 -07002282 CameraThreadState::restoreCallingIdentity(token);
Ruben Brunk36597b22015-03-20 22:15:57 -07002283
2284 // Reacquire mServiceLock
2285 mServiceLock.lock();
2286}
Ruben Brunkcc776712015-02-17 20:18:47 -08002287
Ruben Brunka8ca9152015-04-07 14:23:40 -07002288void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002289 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07002290 Mutex::Autolock l(mLogLock);
2291 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07002292}
2293
Ruben Brunka8ca9152015-04-07 14:23:40 -07002294void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002295 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002296 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07002297 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002298 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002299}
2300
2301void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002302 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002303 // Log the clients evicted
2304 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002305 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002306}
2307
2308void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002309 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07002310 // Log the client rejected
2311 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002312 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002313}
2314
Jayant Chowdhary0e2eefd2019-04-18 14:05:43 -07002315void CameraService::logTorchEvent(const char* cameraId, const char *torchState, int clientPid) {
2316 // Log torch event
2317 logEvent(String8::format("Torch for camera id %s turned %s for client PID %d", cameraId,
2318 torchState, clientPid));
2319}
2320
Ruben Brunk6267b532015-04-30 17:44:07 -07002321void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
2322 const std::set<userid_t>& newUserIds) {
2323 String8 newUsers = toString(newUserIds);
2324 String8 oldUsers = toString(oldUserIds);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002325 if (oldUsers.size() == 0) {
2326 oldUsers = "<None>";
2327 }
Ruben Brunka8ca9152015-04-07 14:23:40 -07002328 // Log the new and old users
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08002329 logEvent(String8::format("USER_SWITCH previous allowed user IDs: %s, current allowed user IDs: %s",
Ruben Brunk6267b532015-04-30 17:44:07 -07002330 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07002331}
2332
2333void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
2334 // Log the device removal
2335 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
2336}
2337
2338void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
2339 // Log the device removal
2340 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
2341}
2342
2343void CameraService::logClientDied(int clientPid, const char* reason) {
2344 // Log the device removal
2345 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07002346}
2347
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002348void CameraService::logServiceError(const char* msg, int errorCode) {
2349 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002350 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002351}
2352
Ruben Brunk36597b22015-03-20 22:15:57 -07002353status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
2354 uint32_t flags) {
2355
Mathias Agopian65ab4712010-07-14 17:59:35 -07002356 // Permission checks
2357 switch (code) {
Svet Ganova453d0d2018-01-11 15:37:58 -08002358 case SHELL_COMMAND_TRANSACTION: {
2359 int in = data.readFileDescriptor();
2360 int out = data.readFileDescriptor();
2361 int err = data.readFileDescriptor();
2362 int argc = data.readInt32();
2363 Vector<String16> args;
2364 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
2365 args.add(data.readString16());
2366 }
2367 sp<IBinder> unusedCallback;
2368 sp<IResultReceiver> resultReceiver;
2369 status_t status;
2370 if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
2371 return status;
2372 }
2373 if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
2374 return status;
2375 }
2376 status = shellCommand(in, out, err, args);
2377 if (resultReceiver != nullptr) {
2378 resultReceiver->send(status);
2379 }
2380 return NO_ERROR;
2381 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002382 }
2383
2384 return BnCameraService::onTransact(code, data, reply, flags);
2385}
2386
Mathias Agopian65ab4712010-07-14 17:59:35 -07002387// We share the media players for shutter and recording sound for all clients.
2388// A reference count is kept to determine when we will actually release the
2389// media players.
2390
Jaekyun Seokef498052018-03-23 13:09:44 +09002391sp<MediaPlayer> CameraService::newMediaPlayer(const char *file) {
2392 sp<MediaPlayer> mp = new MediaPlayer();
2393 status_t error;
2394 if ((error = mp->setDataSource(NULL /* httpService */, file, NULL)) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08002395 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Jaekyun Seokef498052018-03-23 13:09:44 +09002396 error = mp->prepare();
2397 }
2398 if (error != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +00002399 ALOGE("Failed to load CameraService sounds: %s", file);
Jaekyun Seokef498052018-03-23 13:09:44 +09002400 mp->disconnect();
2401 mp.clear();
Jaekyun Seok59a8ef02018-01-15 14:49:05 +09002402 return nullptr;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002403 }
2404 return mp;
2405}
2406
username5755fea2018-12-27 09:48:08 +08002407void CameraService::increaseSoundRef() {
2408 Mutex::Autolock lock(mSoundLock);
2409 mSoundRef++;
2410}
2411
2412void CameraService::loadSoundLocked(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002413 ATRACE_CALL();
2414
username5755fea2018-12-27 09:48:08 +08002415 LOG1("CameraService::loadSoundLocked ref=%d", mSoundRef);
2416 if (SOUND_SHUTTER == kind && mSoundPlayer[SOUND_SHUTTER] == NULL) {
2417 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/product/media/audio/ui/camera_click.ogg");
2418 if (mSoundPlayer[SOUND_SHUTTER] == nullptr) {
2419 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
2420 }
2421 } else if (SOUND_RECORDING_START == kind && mSoundPlayer[SOUND_RECORDING_START] == NULL) {
2422 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/product/media/audio/ui/VideoRecord.ogg");
2423 if (mSoundPlayer[SOUND_RECORDING_START] == nullptr) {
2424 mSoundPlayer[SOUND_RECORDING_START] =
Jaekyun Seok59a8ef02018-01-15 14:49:05 +09002425 newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
username5755fea2018-12-27 09:48:08 +08002426 }
2427 } else if (SOUND_RECORDING_STOP == kind && mSoundPlayer[SOUND_RECORDING_STOP] == NULL) {
2428 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/product/media/audio/ui/VideoStop.ogg");
2429 if (mSoundPlayer[SOUND_RECORDING_STOP] == nullptr) {
2430 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
2431 }
Jaekyun Seok59a8ef02018-01-15 14:49:05 +09002432 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002433}
2434
username5755fea2018-12-27 09:48:08 +08002435void CameraService::decreaseSoundRef() {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002436 Mutex::Autolock lock(mSoundLock);
username5755fea2018-12-27 09:48:08 +08002437 LOG1("CameraService::decreaseSoundRef ref=%d", mSoundRef);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002438 if (--mSoundRef) return;
2439
2440 for (int i = 0; i < NUM_SOUNDS; i++) {
2441 if (mSoundPlayer[i] != 0) {
2442 mSoundPlayer[i]->disconnect();
2443 mSoundPlayer[i].clear();
2444 }
2445 }
2446}
2447
2448void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002449 ATRACE_CALL();
2450
Mathias Agopian65ab4712010-07-14 17:59:35 -07002451 LOG1("playSound(%d)", kind);
2452 Mutex::Autolock lock(mSoundLock);
username5755fea2018-12-27 09:48:08 +08002453 loadSoundLocked(kind);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002454 sp<MediaPlayer> player = mSoundPlayer[kind];
2455 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002456 player->seekTo(0);
2457 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002458 }
2459}
2460
2461// ----------------------------------------------------------------------------
2462
2463CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002464 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002465 const String16& clientPackageName,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08002466 const std::unique_ptr<String16>& clientFeatureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08002467 const String8& cameraIdStr,
2468 int api1CameraId, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002469 int clientPid, uid_t clientUid,
2470 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002471 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002472 IInterface::asBinder(cameraClient),
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08002473 clientPackageName, clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002474 cameraIdStr, cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002475 clientPid, clientUid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002476 servicePid),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08002477 mCameraId(api1CameraId)
Igor Murashkin634a5152013-02-20 17:15:11 -08002478{
Jayant Chowdhary12361932018-08-27 14:46:13 -07002479 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002480 LOG1("Client::Client E (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002481
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002482 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002483
username5755fea2018-12-27 09:48:08 +08002484 cameraService->increaseSoundRef();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002485
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002486 LOG1("Client::Client X (pid %d, id %d)", callingPid, mCameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002487}
2488
Mathias Agopian65ab4712010-07-14 17:59:35 -07002489// tear down the client
2490CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002491 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002492 mDestructionStarted = true;
2493
username5755fea2018-12-27 09:48:08 +08002494 sCameraService->decreaseSoundRef();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002495 // unconditionally disconnect. function is idempotent
2496 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002497}
2498
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002499sp<CameraService> CameraService::BasicClient::BasicClient::sCameraService;
2500
Igor Murashkin634a5152013-02-20 17:15:11 -08002501CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002502 const sp<IBinder>& remoteCallback,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08002503 const String16& clientPackageName, const std::unique_ptr<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002504 const String8& cameraIdStr, int cameraFacing,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002505 int clientPid, uid_t clientUid,
2506 int servicePid):
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002507 mCameraIdStr(cameraIdStr), mCameraFacing(cameraFacing),
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08002508 mClientPackageName(clientPackageName),
2509 mClientPid(clientPid), mClientUid(clientUid),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002510 mServicePid(servicePid),
2511 mDisconnected(false),
Yin-Chia Yehdba03232019-08-19 15:54:28 -07002512 mAudioRestriction(hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002513 mRemoteBinder(remoteCallback)
Igor Murashkin634a5152013-02-20 17:15:11 -08002514{
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08002515 if (clientFeatureId) {
2516 mClientFeatureId = std::unique_ptr<String16>(new String16(*clientFeatureId));
2517 } else {
2518 mClientFeatureId = std::unique_ptr<String16>();
2519 }
2520
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002521 if (sCameraService == nullptr) {
2522 sCameraService = cameraService;
2523 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002524 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002525 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002526
2527 // In some cases the calling code has no access to the package it runs under.
2528 // For example, NDK camera API.
2529 // In this case we will get the packages for the calling UID and pick the first one
2530 // for attributing the app op. This will work correctly for runtime permissions
2531 // as for legacy apps we will toggle the app op for all packages in the UID.
2532 // The caveat is that the operation may be attributed to the wrong package and
2533 // stats based on app ops may be slightly off.
2534 if (mClientPackageName.size() <= 0) {
2535 sp<IServiceManager> sm = defaultServiceManager();
2536 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2537 if (binder == 0) {
2538 ALOGE("Cannot get permission service");
2539 // Leave mClientPackageName unchanged (empty) and the further interaction
2540 // with camera will fail in BasicClient::startCameraOps
2541 return;
2542 }
2543
2544 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2545 Vector<String16> packages;
2546
2547 permCtrl->getPackagesForUid(mClientUid, packages);
2548
2549 if (packages.isEmpty()) {
2550 ALOGE("No packages for calling UID");
2551 // Leave mClientPackageName unchanged (empty) and the further interaction
2552 // with camera will fail in BasicClient::startCameraOps
2553 return;
2554 }
2555 mClientPackageName = packages[0];
2556 }
Jayant Chowdhary5bf11bf2019-06-24 19:42:56 -07002557 if (!hardware::IPCThreadState::self()->isServingCall()) {
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002558 mAppOpsManager = std::make_unique<AppOpsManager>();
2559 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002560}
2561
2562CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002563 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002564 mDestructionStarted = true;
2565}
2566
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002567binder::Status CameraService::BasicClient::disconnect() {
2568 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002569 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002570 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002571 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002572 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002573
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002574 sCameraService->removeByClient(this);
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002575 sCameraService->logDisconnected(mCameraIdStr, mClientPid, String8(mClientPackageName));
Peter Kalauskasa29c1352018-10-10 12:05:42 -07002576 sCameraService->mCameraProviderManager->removeRef(CameraProviderManager::DeviceMode::CAMERA,
2577 mCameraIdStr.c_str());
Ruben Brunkcc776712015-02-17 20:18:47 -08002578
2579 sp<IBinder> remote = getRemote();
2580 if (remote != nullptr) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002581 remote->unlinkToDeath(sCameraService);
Ruben Brunkcc776712015-02-17 20:18:47 -08002582 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002583
2584 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002585 // Notify flashlight that a camera device is closed.
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002586 sCameraService->mFlashlight->deviceClosed(mCameraIdStr);
2587 ALOGI("%s: Disconnected client for camera %s for PID %d", __FUNCTION__, mCameraIdStr.string(),
2588 mClientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002589
Igor Murashkincba2c162013-03-20 15:56:31 -07002590 // client shouldn't be able to call into us anymore
2591 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002592
2593 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002594}
2595
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002596status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2597 // No dumping of clients directly over Binder,
2598 // must go through CameraService::dump
2599 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
Jayant Chowdhary12361932018-08-27 14:46:13 -07002600 CameraThreadState::getCallingUid(), NULL, 0);
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002601 return OK;
2602}
2603
Ruben Brunkcc776712015-02-17 20:18:47 -08002604String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002605 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002606}
2607
2608
2609int CameraService::BasicClient::getClientPid() const {
2610 return mClientPid;
2611}
2612
Ruben Brunk6267b532015-04-30 17:44:07 -07002613uid_t CameraService::BasicClient::getClientUid() const {
2614 return mClientUid;
2615}
2616
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002617bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2618 // Defaults to API2.
2619 return level == API_2;
2620}
2621
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07002622status_t CameraService::BasicClient::setAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07002623 {
2624 Mutex::Autolock l(mAudioRestrictionLock);
2625 mAudioRestriction = mode;
2626 }
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07002627 sCameraService->updateAudioRestriction();
2628 return OK;
2629}
2630
2631int32_t CameraService::BasicClient::getServiceAudioRestriction() const {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07002632 return sCameraService->updateAudioRestriction();
2633}
2634
2635int32_t CameraService::BasicClient::getAudioRestriction() const {
2636 Mutex::Autolock l(mAudioRestrictionLock);
2637 return mAudioRestriction;
2638}
2639
2640bool CameraService::BasicClient::isValidAudioRestriction(int32_t mode) {
2641 switch (mode) {
2642 case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_NONE:
2643 case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION:
2644 case hardware::camera2::ICameraDeviceUser::AUDIO_RESTRICTION_VIBRATION_SOUND:
2645 return true;
2646 default:
2647 return false;
2648 }
2649}
2650
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002651status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002652 ATRACE_CALL();
2653
Igor Murashkine6800ce2013-03-04 17:25:57 -08002654 {
2655 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002656 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002657 }
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002658 if (mAppOpsManager != nullptr) {
2659 // Notify app ops that the camera is not available
2660 mOpsCallback = new OpsCallback(this);
2661 int32_t res;
2662 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
2663 mClientPackageName, mOpsCallback);
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08002664 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, mClientUid,
2665 mClientPackageName, /*startIfModeDefault*/ false, mClientFeatureId,
2666 String16("start camera ") + String16(mCameraIdStr));
Igor Murashkine6800ce2013-03-04 17:25:57 -08002667
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002668 if (res == AppOpsManager::MODE_ERRORED) {
2669 ALOGI("Camera %s: Access for \"%s\" has been revoked",
2670 mCameraIdStr.string(), String8(mClientPackageName).string());
2671 return PERMISSION_DENIED;
2672 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002673
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002674 if (res == AppOpsManager::MODE_IGNORED) {
2675 ALOGI("Camera %s: Access for \"%s\" has been restricted",
2676 mCameraIdStr.string(), String8(mClientPackageName).string());
2677 // Return the same error as for device policy manager rejection
2678 return -EACCES;
2679 }
Svetoslav28e8ef72015-05-11 19:21:31 -07002680 }
2681
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002682 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002683
2684 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002685 sCameraService->updateStatus(StatusInternal::NOT_AVAILABLE, mCameraIdStr);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002686
Emilian Peev573291c2018-02-10 02:10:56 +00002687 int apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1;
2688 if (canCastToApiClient(API_2)) {
2689 apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2;
2690 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002691 // Transition device state to OPEN
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002692 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
Emilian Peev573291c2018-02-10 02:10:56 +00002693 mCameraIdStr, mCameraFacing, mClientPackageName, apiLevel);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002694
Emilian Peev53722fa2019-02-22 17:47:20 -08002695 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
2696
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002697 return OK;
2698}
2699
2700status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002701 ATRACE_CALL();
2702
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002703 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002704 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002705 // Notify app ops that the camera is available again
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002706 if (mAppOpsManager != nullptr) {
2707 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -08002708 mClientPackageName, mClientFeatureId);
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002709 mOpsActive = false;
2710 }
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002711 // This function is called when a client disconnects. This should
2712 // release the camera, but actually only if it was in a proper
2713 // functional state, i.e. with status NOT_AVAILABLE
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08002714 std::initializer_list<StatusInternal> rejected = {StatusInternal::PRESENT,
Guennadi Liakhovetski151e3be2017-11-28 16:34:18 +01002715 StatusInternal::ENUMERATING, StatusInternal::NOT_PRESENT};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002716
Ruben Brunkcc776712015-02-17 20:18:47 -08002717 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002718 sCameraService->updateStatus(StatusInternal::PRESENT,
2719 mCameraIdStr, rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002720
Emilian Peev573291c2018-02-10 02:10:56 +00002721 int apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1;
2722 if (canCastToApiClient(API_2)) {
2723 apiLevel = hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2;
2724 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002725 // Transition device state to CLOSED
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002726 sCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
Emilian Peev573291c2018-02-10 02:10:56 +00002727 mCameraIdStr, mCameraFacing, mClientPackageName, apiLevel);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002728 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002729 // Always stop watching, even if no camera op is active
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002730 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
2731 mAppOpsManager->stopWatchingMode(mOpsCallback);
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002732 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002733 mOpsCallback.clear();
2734
Emilian Peev53722fa2019-02-22 17:47:20 -08002735 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
2736
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002737 return OK;
2738}
2739
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002740void CameraService::BasicClient::opChanged(int32_t op, const String16&) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002741 ATRACE_CALL();
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002742 if (mAppOpsManager == nullptr) {
2743 return;
2744 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002745 if (op != AppOpsManager::OP_CAMERA) {
2746 ALOGW("Unexpected app ops notification received: %d", op);
2747 return;
2748 }
2749
2750 int32_t res;
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002751 res = mAppOpsManager->checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002752 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002753 ALOGV("checkOp returns: %d, %s ", res,
2754 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2755 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2756 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2757 "UNKNOWN");
2758
2759 if (res != AppOpsManager::MODE_ALLOWED) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002760 ALOGI("Camera %s: Access for \"%s\" revoked", mCameraIdStr.string(),
Jayant Chowdharyb61526c2019-05-13 19:37:42 -07002761 String8(mClientPackageName).string());
Svet Ganova453d0d2018-01-11 15:37:58 -08002762 block();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002763 }
2764}
2765
Svet Ganova453d0d2018-01-11 15:37:58 -08002766void CameraService::BasicClient::block() {
2767 ATRACE_CALL();
2768
2769 // Reset the client PID to allow server-initiated disconnect,
2770 // and to prevent further calls by client.
Jayant Chowdhary12361932018-08-27 14:46:13 -07002771 mClientPid = CameraThreadState::getCallingPid();
Svet Ganova453d0d2018-01-11 15:37:58 -08002772 CaptureResultExtras resultExtras; // a dummy result (invalid)
2773 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED, resultExtras);
2774 disconnect();
2775}
2776
Mathias Agopian65ab4712010-07-14 17:59:35 -07002777// ----------------------------------------------------------------------------
2778
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002779void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002780 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002781 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002782 if (mRemoteCallback != NULL) {
Yin-Chia Yehf13bda52018-05-31 12:12:59 -07002783 int32_t api1ErrorCode = CAMERA_ERROR_RELEASED;
2784 if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED) {
2785 api1ErrorCode = CAMERA_ERROR_DISABLED;
2786 }
2787 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, api1ErrorCode, 0);
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002788 } else {
2789 ALOGE("mRemoteCallback is NULL!!");
2790 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002791}
2792
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002793// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002794binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002795 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002796 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002797}
2798
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002799bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2800 return level == API_1;
2801}
2802
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002803CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2804 mClient(client) {
2805}
2806
2807void CameraService::Client::OpsCallback::opChanged(int32_t op,
2808 const String16& packageName) {
2809 sp<BasicClient> client = mClient.promote();
2810 if (client != NULL) {
2811 client->opChanged(op, packageName);
2812 }
2813}
2814
Mathias Agopian65ab4712010-07-14 17:59:35 -07002815// ----------------------------------------------------------------------------
Svet Ganova453d0d2018-01-11 15:37:58 -08002816// UidPolicy
2817// ----------------------------------------------------------------------------
2818
2819void CameraService::UidPolicy::registerSelf() {
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002820 Mutex::Autolock _l(mUidLock);
2821
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002822 if (mRegistered) return;
Steven Moreland2f348142019-07-02 15:59:07 -07002823 status_t res = mAm.linkToDeath(this);
2824 mAm.registerUidObserver(this, ActivityManager::UID_OBSERVER_GONE
Svet Ganova453d0d2018-01-11 15:37:58 -08002825 | ActivityManager::UID_OBSERVER_IDLE
Emilian Peev53722fa2019-02-22 17:47:20 -08002826 | ActivityManager::UID_OBSERVER_ACTIVE | ActivityManager::UID_OBSERVER_PROCSTATE,
Svet Ganova453d0d2018-01-11 15:37:58 -08002827 ActivityManager::PROCESS_STATE_UNKNOWN,
2828 String16("cameraserver"));
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002829 if (res == OK) {
2830 mRegistered = true;
2831 ALOGV("UidPolicy: Registered with ActivityManager");
2832 }
Svet Ganova453d0d2018-01-11 15:37:58 -08002833}
2834
2835void CameraService::UidPolicy::unregisterSelf() {
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002836 Mutex::Autolock _l(mUidLock);
2837
Steven Moreland2f348142019-07-02 15:59:07 -07002838 mAm.unregisterUidObserver(this);
2839 mAm.unlinkToDeath(this);
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002840 mRegistered = false;
2841 mActiveUids.clear();
2842 ALOGV("UidPolicy: Unregistered with ActivityManager");
Svet Ganova453d0d2018-01-11 15:37:58 -08002843}
2844
2845void CameraService::UidPolicy::onUidGone(uid_t uid, bool disabled) {
2846 onUidIdle(uid, disabled);
2847}
2848
2849void CameraService::UidPolicy::onUidActive(uid_t uid) {
2850 Mutex::Autolock _l(mUidLock);
2851 mActiveUids.insert(uid);
2852}
2853
2854void CameraService::UidPolicy::onUidIdle(uid_t uid, bool /* disabled */) {
2855 bool deleted = false;
2856 {
2857 Mutex::Autolock _l(mUidLock);
2858 if (mActiveUids.erase(uid) > 0) {
2859 deleted = true;
2860 }
2861 }
2862 if (deleted) {
2863 sp<CameraService> service = mService.promote();
2864 if (service != nullptr) {
2865 service->blockClientsForUid(uid);
2866 }
2867 }
2868}
2869
Emilian Peev53722fa2019-02-22 17:47:20 -08002870void CameraService::UidPolicy::onUidStateChanged(uid_t uid, int32_t procState,
Hui Yu13ad0eb2019-09-09 10:27:07 -07002871 int64_t procStateSeq __unused, int32_t capability __unused) {
Emilian Peev53722fa2019-02-22 17:47:20 -08002872 bool procStateChange = false;
2873 {
2874 Mutex::Autolock _l(mUidLock);
2875 if ((mMonitoredUids.find(uid) != mMonitoredUids.end()) &&
2876 (mMonitoredUids[uid].first != procState)) {
2877 mMonitoredUids[uid].first = procState;
2878 procStateChange = true;
2879 }
2880 }
2881
2882 if (procStateChange) {
2883 sp<CameraService> service = mService.promote();
2884 if (service != nullptr) {
2885 service->notifyMonitoredUids();
2886 }
2887 }
2888}
2889
2890void CameraService::UidPolicy::registerMonitorUid(uid_t uid) {
2891 Mutex::Autolock _l(mUidLock);
2892 auto it = mMonitoredUids.find(uid);
2893 if (it != mMonitoredUids.end()) {
2894 it->second.second++;
2895 } else {
2896 mMonitoredUids.emplace(
2897 std::pair<uid_t, std::pair<int32_t, size_t>> (uid,
2898 std::pair<int32_t, size_t> (ActivityManager::PROCESS_STATE_NONEXISTENT, 1)));
2899 }
2900}
2901
2902void CameraService::UidPolicy::unregisterMonitorUid(uid_t uid) {
2903 Mutex::Autolock _l(mUidLock);
2904 auto it = mMonitoredUids.find(uid);
2905 if (it != mMonitoredUids.end()) {
2906 it->second.second--;
2907 if (it->second.second == 0) {
2908 mMonitoredUids.erase(it);
2909 }
2910 } else {
2911 ALOGE("%s: Trying to unregister uid: %d which is not monitored!", __FUNCTION__, uid);
2912 }
2913}
2914
Svet Ganov7b4ab782018-03-25 12:48:10 -07002915bool CameraService::UidPolicy::isUidActive(uid_t uid, String16 callingPackage) {
Svet Ganova453d0d2018-01-11 15:37:58 -08002916 Mutex::Autolock _l(mUidLock);
Svet Ganov7b4ab782018-03-25 12:48:10 -07002917 return isUidActiveLocked(uid, callingPackage);
Svet Ganova453d0d2018-01-11 15:37:58 -08002918}
2919
Eino-Ville Talvala32b8c202018-08-20 10:27:58 -07002920static const int64_t kPollUidActiveTimeoutTotalMillis = 300;
2921static const int64_t kPollUidActiveTimeoutMillis = 50;
Svet Ganov94ec46f2018-06-08 15:03:46 -07002922
Svet Ganov7b4ab782018-03-25 12:48:10 -07002923bool CameraService::UidPolicy::isUidActiveLocked(uid_t uid, String16 callingPackage) {
Svet Ganova453d0d2018-01-11 15:37:58 -08002924 // Non-app UIDs are considered always active
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002925 // If activity manager is unreachable, assume everything is active
2926 if (uid < FIRST_APPLICATION_UID || !mRegistered) {
Svet Ganova453d0d2018-01-11 15:37:58 -08002927 return true;
2928 }
2929 auto it = mOverrideUids.find(uid);
2930 if (it != mOverrideUids.end()) {
2931 return it->second;
2932 }
Svet Ganov7b4ab782018-03-25 12:48:10 -07002933 bool active = mActiveUids.find(uid) != mActiveUids.end();
2934 if (!active) {
2935 // We want active UIDs to always access camera with their first attempt since
2936 // there is no guarantee the app is robustly written and would retry getting
2937 // the camera on failure. The inverse case is not a problem as we would take
2938 // camera away soon once we get the callback that the uid is no longer active.
2939 ActivityManager am;
2940 // Okay to access with a lock held as UID changes are dispatched without
2941 // a lock and we are a higher level component.
Svet Ganov94ec46f2018-06-08 15:03:46 -07002942 int64_t startTimeMillis = 0;
2943 do {
2944 // TODO: Fix this b/109950150!
2945 // Okay this is a hack. There is a race between the UID turning active and
2946 // activity being resumed. The proper fix is very risky, so we temporary add
2947 // some polling which should happen pretty rarely anyway as the race is hard
2948 // to hit.
Eino-Ville Talvala32b8c202018-08-20 10:27:58 -07002949 active = mActiveUids.find(uid) != mActiveUids.end();
2950 if (!active) active = am.isUidActive(uid, callingPackage);
Svet Ganov94ec46f2018-06-08 15:03:46 -07002951 if (active) {
2952 break;
2953 }
2954 if (startTimeMillis <= 0) {
2955 startTimeMillis = uptimeMillis();
2956 }
2957 int64_t ellapsedTimeMillis = uptimeMillis() - startTimeMillis;
Eino-Ville Talvala32b8c202018-08-20 10:27:58 -07002958 int64_t remainingTimeMillis = kPollUidActiveTimeoutTotalMillis - ellapsedTimeMillis;
Svet Ganov94ec46f2018-06-08 15:03:46 -07002959 if (remainingTimeMillis <= 0) {
2960 break;
2961 }
Eino-Ville Talvala32b8c202018-08-20 10:27:58 -07002962 remainingTimeMillis = std::min(kPollUidActiveTimeoutMillis, remainingTimeMillis);
2963
2964 mUidLock.unlock();
Svet Ganov94ec46f2018-06-08 15:03:46 -07002965 usleep(remainingTimeMillis * 1000);
Eino-Ville Talvala32b8c202018-08-20 10:27:58 -07002966 mUidLock.lock();
Svet Ganov94ec46f2018-06-08 15:03:46 -07002967 } while (true);
2968
Svet Ganov7b4ab782018-03-25 12:48:10 -07002969 if (active) {
2970 // Now that we found out the UID is actually active, cache that
2971 mActiveUids.insert(uid);
2972 }
2973 }
2974 return active;
Svet Ganova453d0d2018-01-11 15:37:58 -08002975}
2976
Varun Shahb42f1eb2019-04-16 14:45:13 -07002977int32_t CameraService::UidPolicy::getProcState(uid_t uid) {
2978 Mutex::Autolock _l(mUidLock);
2979 return getProcStateLocked(uid);
2980}
2981
2982int32_t CameraService::UidPolicy::getProcStateLocked(uid_t uid) {
2983 int32_t procState = ActivityManager::PROCESS_STATE_UNKNOWN;
2984 if (mMonitoredUids.find(uid) != mMonitoredUids.end()) {
2985 procState = mMonitoredUids[uid].first;
2986 }
2987 return procState;
2988}
2989
Svet Ganov7b4ab782018-03-25 12:48:10 -07002990void CameraService::UidPolicy::UidPolicy::addOverrideUid(uid_t uid,
2991 String16 callingPackage, bool active) {
2992 updateOverrideUid(uid, callingPackage, active, true);
Svet Ganova453d0d2018-01-11 15:37:58 -08002993}
2994
Svet Ganov7b4ab782018-03-25 12:48:10 -07002995void CameraService::UidPolicy::removeOverrideUid(uid_t uid, String16 callingPackage) {
2996 updateOverrideUid(uid, callingPackage, false, false);
Svet Ganova453d0d2018-01-11 15:37:58 -08002997}
2998
Eino-Ville Talvala8abec3f2018-03-20 11:07:00 -07002999void CameraService::UidPolicy::binderDied(const wp<IBinder>& /*who*/) {
3000 Mutex::Autolock _l(mUidLock);
3001 ALOGV("UidPolicy: ActivityManager has died");
3002 mRegistered = false;
3003 mActiveUids.clear();
3004}
3005
Svet Ganov7b4ab782018-03-25 12:48:10 -07003006void CameraService::UidPolicy::updateOverrideUid(uid_t uid, String16 callingPackage,
3007 bool active, bool insert) {
Svet Ganova453d0d2018-01-11 15:37:58 -08003008 bool wasActive = false;
3009 bool isActive = false;
3010 {
3011 Mutex::Autolock _l(mUidLock);
Svet Ganov7b4ab782018-03-25 12:48:10 -07003012 wasActive = isUidActiveLocked(uid, callingPackage);
Svet Ganova453d0d2018-01-11 15:37:58 -08003013 mOverrideUids.erase(uid);
3014 if (insert) {
3015 mOverrideUids.insert(std::pair<uid_t, bool>(uid, active));
3016 }
Svet Ganov7b4ab782018-03-25 12:48:10 -07003017 isActive = isUidActiveLocked(uid, callingPackage);
Svet Ganova453d0d2018-01-11 15:37:58 -08003018 }
3019 if (wasActive != isActive && !isActive) {
3020 sp<CameraService> service = mService.promote();
3021 if (service != nullptr) {
3022 service->blockClientsForUid(uid);
3023 }
3024 }
3025}
3026
3027// ----------------------------------------------------------------------------
Michael Grooverd1d435a2018-12-18 17:39:42 -08003028// SensorPrivacyPolicy
3029// ----------------------------------------------------------------------------
3030void CameraService::SensorPrivacyPolicy::registerSelf() {
3031 Mutex::Autolock _l(mSensorPrivacyLock);
3032 if (mRegistered) {
3033 return;
3034 }
3035 SensorPrivacyManager spm;
3036 spm.addSensorPrivacyListener(this);
3037 mSensorPrivacyEnabled = spm.isSensorPrivacyEnabled();
3038 status_t res = spm.linkToDeath(this);
3039 if (res == OK) {
3040 mRegistered = true;
3041 ALOGV("SensorPrivacyPolicy: Registered with SensorPrivacyManager");
3042 }
3043}
3044
3045void CameraService::SensorPrivacyPolicy::unregisterSelf() {
3046 Mutex::Autolock _l(mSensorPrivacyLock);
3047 SensorPrivacyManager spm;
3048 spm.removeSensorPrivacyListener(this);
3049 spm.unlinkToDeath(this);
3050 mRegistered = false;
3051 ALOGV("SensorPrivacyPolicy: Unregistered with SensorPrivacyManager");
3052}
3053
3054bool CameraService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
3055 Mutex::Autolock _l(mSensorPrivacyLock);
3056 return mSensorPrivacyEnabled;
3057}
3058
3059binder::Status CameraService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
3060 {
3061 Mutex::Autolock _l(mSensorPrivacyLock);
3062 mSensorPrivacyEnabled = enabled;
3063 }
3064 // if sensor privacy is enabled then block all clients from accessing the camera
3065 if (enabled) {
3066 sp<CameraService> service = mService.promote();
3067 if (service != nullptr) {
3068 service->blockAllClients();
3069 }
3070 }
3071 return binder::Status::ok();
3072}
3073
3074void CameraService::SensorPrivacyPolicy::binderDied(const wp<IBinder>& /*who*/) {
3075 Mutex::Autolock _l(mSensorPrivacyLock);
3076 ALOGV("SensorPrivacyPolicy: SensorPrivacyManager has died");
3077 mRegistered = false;
3078}
3079
3080// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08003081// CameraState
3082// ----------------------------------------------------------------------------
3083
3084CameraService::CameraState::CameraState(const String8& id, int cost,
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -07003085 const std::set<String8>& conflicting, SystemCameraKind systemCameraKind) : mId(id),
3086 mStatus(StatusInternal::NOT_PRESENT), mCost(cost), mConflicting(conflicting),
3087 mSystemCameraKind(systemCameraKind) {}
Ruben Brunkcc776712015-02-17 20:18:47 -08003088
3089CameraService::CameraState::~CameraState() {}
3090
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003091CameraService::StatusInternal CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08003092 Mutex::Autolock lock(mStatusLock);
3093 return mStatus;
3094}
3095
3096CameraParameters CameraService::CameraState::getShimParams() const {
3097 return mShimParams;
3098}
3099
3100void CameraService::CameraState::setShimParams(const CameraParameters& params) {
3101 mShimParams = params;
3102}
3103
3104int CameraService::CameraState::getCost() const {
3105 return mCost;
3106}
3107
3108std::set<String8> CameraService::CameraState::getConflicting() const {
3109 return mConflicting;
3110}
3111
3112String8 CameraService::CameraState::getId() const {
3113 return mId;
3114}
3115
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -07003116SystemCameraKind CameraService::CameraState::getSystemCameraKind() const {
3117 return mSystemCameraKind;
3118}
3119
Ruben Brunkcc776712015-02-17 20:18:47 -08003120// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07003121// ClientEventListener
3122// ----------------------------------------------------------------------------
3123
3124void CameraService::ClientEventListener::onClientAdded(
3125 const resource_policy::ClientDescriptor<String8,
3126 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07003127 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07003128 if (basicClient.get() != nullptr) {
3129 BatteryNotifier& notifier(BatteryNotifier::getInstance());
3130 notifier.noteStartCamera(descriptor.getKey(),
3131 static_cast<int>(basicClient->getClientUid()));
3132 }
3133}
3134
3135void CameraService::ClientEventListener::onClientRemoved(
3136 const resource_policy::ClientDescriptor<String8,
3137 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07003138 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07003139 if (basicClient.get() != nullptr) {
3140 BatteryNotifier& notifier(BatteryNotifier::getInstance());
3141 notifier.noteStopCamera(descriptor.getKey(),
3142 static_cast<int>(basicClient->getClientUid()));
3143 }
3144}
3145
3146
3147// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08003148// CameraClientManager
3149// ----------------------------------------------------------------------------
3150
Ruben Brunk99e69712015-05-26 17:25:07 -07003151CameraService::CameraClientManager::CameraClientManager() {
3152 setListener(std::make_shared<ClientEventListener>());
3153}
3154
Ruben Brunkcc776712015-02-17 20:18:47 -08003155CameraService::CameraClientManager::~CameraClientManager() {}
3156
3157sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
3158 const String8& id) const {
3159 auto descriptor = get(id);
3160 if (descriptor == nullptr) {
3161 return sp<BasicClient>{nullptr};
3162 }
3163 return descriptor->getValue();
3164}
3165
3166String8 CameraService::CameraClientManager::toString() const {
3167 auto all = getAll();
3168 String8 ret("[");
3169 bool hasAny = false;
3170 for (auto& i : all) {
3171 hasAny = true;
3172 String8 key = i->getKey();
3173 int32_t cost = i->getCost();
3174 int32_t pid = i->getOwnerId();
Emilian Peev8131a262017-02-01 12:33:43 +00003175 int32_t score = i->getPriority().getScore();
3176 int32_t state = i->getPriority().getState();
Ruben Brunkcc776712015-02-17 20:18:47 -08003177 auto conflicting = i->getConflicting();
3178 auto clientSp = i->getValue();
3179 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07003180 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08003181 if (clientSp.get() != nullptr) {
3182 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07003183 uid_t clientUid = clientSp->getClientUid();
3184 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08003185 }
Emilian Peev8131a262017-02-01 12:33:43 +00003186 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Score: %"
3187 PRId32 ", State: %" PRId32, key.string(), cost, pid, score, state);
Ruben Brunkcc776712015-02-17 20:18:47 -08003188
Ruben Brunk6267b532015-04-30 17:44:07 -07003189 if (clientSp.get() != nullptr) {
3190 ret.appendFormat("User Id: %d, ", clientUserId);
3191 }
Ruben Brunkcc776712015-02-17 20:18:47 -08003192 if (packageName.size() != 0) {
3193 ret.appendFormat("Client Package Name: %s", packageName.string());
3194 }
3195
3196 ret.append(", Conflicting Client Devices: {");
3197 for (auto& j : conflicting) {
3198 ret.appendFormat("%s, ", j.string());
3199 }
3200 ret.append("})");
3201 }
3202 if (hasAny) ret.append("\n");
3203 ret.append("]\n");
3204 return ret;
3205}
3206
3207CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
3208 const String8& key, const sp<BasicClient>& value, int32_t cost,
Emilian Peev8131a262017-02-01 12:33:43 +00003209 const std::set<String8>& conflictingKeys, int32_t score, int32_t ownerId,
3210 int32_t state) {
Ruben Brunkcc776712015-02-17 20:18:47 -08003211
Jayant Chowdharyc578a502019-05-08 10:57:54 -07003212 bool isVendorClient = hardware::IPCThreadState::self()->isServingCall();
3213 int32_t score_adj = isVendorClient ? kVendorClientScore : score;
3214 int32_t state_adj = isVendorClient ? kVendorClientState: state;
3215
Ruben Brunkcc776712015-02-17 20:18:47 -08003216 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
Jayant Chowdharyc578a502019-05-08 10:57:54 -07003217 key, value, cost, conflictingKeys, score_adj, ownerId, state_adj, isVendorClient);
Ruben Brunkcc776712015-02-17 20:18:47 -08003218}
3219
3220CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
3221 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
3222 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
Emilian Peev8131a262017-02-01 12:33:43 +00003223 partial->getConflicting(), partial->getPriority().getScore(),
3224 partial->getOwnerId(), partial->getPriority().getState());
Ruben Brunkcc776712015-02-17 20:18:47 -08003225}
3226
3227// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07003228
3229static const int kDumpLockRetries = 50;
3230static const int kDumpLockSleep = 60000;
3231
3232static bool tryLock(Mutex& mutex)
3233{
3234 bool locked = false;
3235 for (int i = 0; i < kDumpLockRetries; ++i) {
3236 if (mutex.tryLock() == NO_ERROR) {
3237 locked = true;
3238 break;
3239 }
3240 usleep(kDumpLockSleep);
3241 }
3242 return locked;
3243}
3244
3245status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07003246 ATRACE_CALL();
3247
Jayant Chowdhary5216b212019-07-17 09:26:23 -07003248 if (checkCallingPermission(sDumpPermission) == false) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003249 dprintf(fd, "Permission Denial: can't dump CameraService from pid=%d, uid=%d\n",
Jayant Chowdhary12361932018-08-27 14:46:13 -07003250 CameraThreadState::getCallingPid(),
3251 CameraThreadState::getCallingUid());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003252 return NO_ERROR;
3253 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003254 bool locked = tryLock(mServiceLock);
3255 // failed to lock - CameraService is probably deadlocked
3256 if (!locked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003257 dprintf(fd, "!! CameraService may be deadlocked !!\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003258 }
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07003259
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003260 if (!mInitialized) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003261 dprintf(fd, "!! No camera HAL available !!\n");
Ruben Brunkf81648e2014-04-17 16:14:57 -07003262
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003263 // Dump event log for error information
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07003264 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08003265
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003266 if (locked) mServiceLock.unlock();
3267 return NO_ERROR;
3268 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003269 dprintf(fd, "\n== Service global info: ==\n\n");
3270 dprintf(fd, "Number of camera devices: %d\n", mNumberOfCameras);
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08003271 dprintf(fd, "Number of normal camera devices: %zu\n", mNormalDeviceIds.size());
Jayant Chowdhary847947d2019-08-30 18:02:59 -07003272 dprintf(fd, "Number of public camera devices visible to API1: %zu\n",
3273 mNormalDeviceIdsWithoutSystemCamera.size());
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -08003274 for (size_t i = 0; i < mNormalDeviceIds.size(); i++) {
3275 dprintf(fd, " Device %zu maps to \"%s\"\n", i, mNormalDeviceIds[i].c_str());
3276 }
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003277 String8 activeClientString = mActiveClientManager.toString();
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003278 dprintf(fd, "Active Camera Clients:\n%s", activeClientString.string());
3279 dprintf(fd, "Allowed user IDs: %s\n", toString(mAllowedUsers).string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003280
3281 dumpEventLog(fd);
3282
3283 bool stateLocked = tryLock(mCameraStatesLock);
3284 if (!stateLocked) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003285 dprintf(fd, "CameraStates in use, may be deadlocked\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003286 }
3287
Emilian Peevbd8c5032018-02-14 23:05:40 +00003288 int argSize = args.size();
3289 for (int i = 0; i < argSize; i++) {
3290 if (args[i] == TagMonitor::kMonitorOption) {
3291 if (i + 1 < argSize) {
3292 mMonitorTags = String8(args[i + 1]);
3293 }
3294 break;
3295 }
3296 }
3297
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003298 for (auto& state : mCameraStates) {
3299 String8 cameraId = state.first;
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003300
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003301 dprintf(fd, "== Camera device %s dynamic info: ==\n", cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003302
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003303 CameraParameters p = state.second->getShimParams();
3304 if (!p.isEmpty()) {
3305 dprintf(fd, " Camera1 API shim is using parameters:\n ");
3306 p.dump(fd, args);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003307 }
3308
3309 auto clientDescriptor = mActiveClientManager.get(cameraId);
Zhijun He2f140ed2017-02-08 09:57:23 -08003310 if (clientDescriptor != nullptr) {
3311 dprintf(fd, " Device %s is open. Client instance dump:\n",
3312 cameraId.string());
3313 dprintf(fd, " Client priority score: %d state: %d\n",
3314 clientDescriptor->getPriority().getScore(),
3315 clientDescriptor->getPriority().getState());
3316 dprintf(fd, " Client PID: %d\n", clientDescriptor->getOwnerId());
3317
3318 auto client = clientDescriptor->getValue();
3319 dprintf(fd, " Client package: %s\n",
3320 String8(client->getPackageName()).string());
3321
3322 client->dumpClient(fd, args);
3323 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003324 dprintf(fd, " Device %s is closed, no client instance\n",
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003325 cameraId.string());
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003326 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003327
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003328 }
3329
3330 if (stateLocked) mCameraStatesLock.unlock();
3331
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003332 if (locked) mServiceLock.unlock();
3333
Emilian Peevf53f66e2017-04-11 14:29:43 +01003334 mCameraProviderManager->dump(fd, args);
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003335
3336 dprintf(fd, "\n== Vendor tags: ==\n\n");
3337
3338 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
3339 if (desc == NULL) {
Emilian Peev71c73a22017-03-21 16:35:51 +00003340 sp<VendorTagDescriptorCache> cache =
3341 VendorTagDescriptorCache::getGlobalVendorTagCache();
3342 if (cache == NULL) {
3343 dprintf(fd, "No vendor tags.\n");
3344 } else {
3345 cache->dump(fd, /*verbosity*/2, /*indentation*/2);
3346 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003347 } else {
3348 desc->dump(fd, /*verbosity*/2, /*indentation*/2);
3349 }
3350
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003351 // Dump camera traces if there were any
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003352 dprintf(fd, "\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003353 camera3::CameraTraces::dump(fd, args);
3354
3355 // Process dump arguments, if any
3356 int n = args.size();
3357 String16 verboseOption("-v");
3358 String16 unreachableOption("--unreachable");
3359 for (int i = 0; i < n; i++) {
3360 if (args[i] == verboseOption) {
3361 // change logging level
3362 if (i + 1 >= n) continue;
3363 String8 levelStr(args[i+1]);
3364 int level = atoi(levelStr.string());
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003365 dprintf(fd, "\nSetting log level to %d.\n", level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003366 setLogLevel(level);
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003367 } else if (args[i] == unreachableOption) {
3368 // Dump memory analysis
3369 // TODO - should limit be an argument parameter?
3370 UnreachableMemoryInfo info;
3371 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
3372 if (!success) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003373 dprintf(fd, "\n== Unable to dump unreachable memory. "
3374 "Try disabling SELinux enforcement. ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003375 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003376 dprintf(fd, "\n== Dumping unreachable memory: ==\n");
Eino-Ville Talvala81314182017-01-30 16:13:45 -08003377 std::string s = info.ToString(/*log_contents*/ true);
3378 write(fd, s.c_str(), s.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07003379 }
3380 }
3381 }
3382 return NO_ERROR;
3383}
3384
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07003385void CameraService::dumpEventLog(int fd) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003386 dprintf(fd, "\n== Camera service events log (most recent at top): ==\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07003387
3388 Mutex::Autolock l(mLogLock);
3389 for (const auto& msg : mEventLog) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003390 dprintf(fd, " %s\n", msg.string());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07003391 }
3392
3393 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003394 dprintf(fd, " ...\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07003395 } else if (mEventLog.size() == 0) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003396 dprintf(fd, " [no events yet]\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07003397 }
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08003398 dprintf(fd, "\n");
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07003399}
3400
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003401void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003402 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003403 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
3404 if (mTorchClientMap[i] == who) {
3405 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003406 String8 cameraId = mTorchClientMap.keyAt(i);
3407 status_t res = mFlashlight->setTorchMode(cameraId, false);
3408 if (res) {
3409 ALOGE("%s: torch client died but couldn't turn off torch: "
3410 "%s (%d)", __FUNCTION__, strerror(-res), res);
3411 return;
3412 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003413 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003414 break;
3415 }
3416 }
3417}
3418
Ruben Brunkcc776712015-02-17 20:18:47 -08003419/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07003420
Igor Murashkin294d0ec2012-10-05 10:44:57 -07003421 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07003422 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
3423 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07003424 */
Yin-Chia Yehdbfcb382018-02-16 11:17:36 -08003425 // PID here is approximate and can be wrong.
Jayant Chowdhary12361932018-08-27 14:46:13 -07003426 logClientDied(CameraThreadState::getCallingPid(), String8("Binder died unexpectedly"));
Ruben Brunka8ca9152015-04-07 14:23:40 -07003427
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003428 // check torch client
3429 handleTorchClientBinderDied(who);
3430
3431 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08003432 if(!evictClientIdByRemote(who)) {
3433 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07003434 return;
3435 }
3436
Ruben Brunkcc776712015-02-17 20:18:47 -08003437 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
3438 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07003439}
3440
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003441void CameraService::updateStatus(StatusInternal status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08003442 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08003443}
3444
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003445void CameraService::updateStatus(StatusInternal status, const String8& cameraId,
3446 std::initializer_list<StatusInternal> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08003447 // Do not lock mServiceLock here or can get into a deadlock from
3448 // connect() -> disconnect -> updateStatus
3449
3450 auto state = getCameraState(cameraId);
3451
3452 if (state == nullptr) {
3453 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
3454 cameraId.string());
3455 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07003456 }
3457
Jayant Chowdhary33e8ef82019-09-27 09:20:42 -07003458 // Avoid calling getSystemCameraKind() with mStatusListenerLock held (b/141756275)
3459 SystemCameraKind deviceKind = SystemCameraKind::PUBLIC;
3460 if (getSystemCameraKind(cameraId, &deviceKind) != OK) {
3461 ALOGE("%s: Invalid camera id %s, skipping", __FUNCTION__, cameraId.string());
3462 return;
3463 }
Jayant Chowdhary90e63692019-10-25 14:13:01 -07003464 bool supportsHAL3 = false;
3465 // supportsCameraApi also holds mInterfaceMutex, we can't call it in the
3466 // HIDL onStatusChanged wrapper call (we'll hold mStatusListenerLock and
3467 // mInterfaceMutex together, which can lead to deadlocks)
3468 binder::Status sRet =
3469 supportsCameraApi(String16(cameraId), hardware::ICameraService::API_VERSION_2,
3470 &supportsHAL3);
3471 if (!sRet.isOk()) {
3472 ALOGW("%s: Failed to determine if device supports HAL3 %s, supportsCameraApi call failed",
3473 __FUNCTION__, cameraId.string());
3474 return;
3475 }
Ruben Brunkcc776712015-02-17 20:18:47 -08003476 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
3477 // of the listeners with both the mStatusStatus and mStatusListenerLock held
Jayant Chowdhary90e63692019-10-25 14:13:01 -07003478 state->updateStatus(status, cameraId, rejectSourceStates, [this, &deviceKind, &supportsHAL3]
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003479 (const String8& cameraId, StatusInternal status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08003480
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003481 if (status != StatusInternal::ENUMERATING) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003482 // Update torch status if it has a flash unit.
3483 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003484 TorchModeStatus torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003485 if (getTorchStatusLocked(cameraId, &torchStatus) !=
3486 NAME_NOT_FOUND) {
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003487 TorchModeStatus newTorchStatus =
3488 status == StatusInternal::PRESENT ?
3489 TorchModeStatus::AVAILABLE_OFF :
3490 TorchModeStatus::NOT_AVAILABLE;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07003491 if (torchStatus != newTorchStatus) {
3492 onTorchStatusChangedLocked(cameraId, newTorchStatus);
3493 }
3494 }
Ruben Brunkcc776712015-02-17 20:18:47 -08003495 }
3496
3497 Mutex::Autolock lock(mStatusListenerLock);
3498
3499 for (auto& listener : mListenerList) {
Jayant Chowdhary90e63692019-10-25 14:13:01 -07003500 bool isVendorListener = listener->isVendorListener();
3501 if (shouldSkipStatusUpdates(deviceKind, isVendorListener,
3502 listener->getListenerPid(), listener->getListenerUid()) ||
3503 (isVendorListener && !supportsHAL3)) {
3504 ALOGV("Skipping discovery callback for system-only camera/HAL1 device %s",
Jayant Chowdhary5216b212019-07-17 09:26:23 -07003505 cameraId.c_str());
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -08003506 continue;
3507 }
Jayant Chowdhary5216b212019-07-17 09:26:23 -07003508 listener->getListener()->onStatusChanged(mapToInterface(status),
Emilian Peev53722fa2019-02-22 17:47:20 -08003509 String16(cameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08003510 }
3511 });
Igor Murashkincba2c162013-03-20 15:56:31 -07003512}
3513
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003514template<class Func>
3515void CameraService::CameraState::updateStatus(StatusInternal status,
3516 const String8& cameraId,
3517 std::initializer_list<StatusInternal> rejectSourceStates,
3518 Func onStatusUpdatedLocked) {
3519 Mutex::Autolock lock(mStatusLock);
3520 StatusInternal oldStatus = mStatus;
3521 mStatus = status;
3522
3523 if (oldStatus == status) {
3524 return;
3525 }
3526
3527 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
3528 cameraId.string(), oldStatus, status);
3529
3530 if (oldStatus == StatusInternal::NOT_PRESENT &&
3531 (status != StatusInternal::PRESENT &&
3532 status != StatusInternal::ENUMERATING)) {
3533
3534 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
3535 __FUNCTION__);
3536 mStatus = oldStatus;
3537 return;
3538 }
3539
3540 /**
3541 * Sometimes we want to conditionally do a transition.
3542 * For example if a client disconnects, we want to go to PRESENT
3543 * only if we weren't already in NOT_PRESENT or ENUMERATING.
3544 */
3545 for (auto& rejectStatus : rejectSourceStates) {
3546 if (oldStatus == rejectStatus) {
3547 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
3548 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
3549 mStatus = oldStatus;
3550 return;
3551 }
3552 }
3553
3554 onStatusUpdatedLocked(cameraId, status);
3555}
3556
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -07003557void CameraService::updateProxyDeviceState(int newState,
Emilian Peev573291c2018-02-10 02:10:56 +00003558 const String8& cameraId, int facing, const String16& clientName, int apiLevel) {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07003559 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
3560 if (proxyBinder == nullptr) return;
3561 String16 id(cameraId);
Emilian Peev573291c2018-02-10 02:10:56 +00003562 proxyBinder->notifyCameraState(id, newState, facing, clientName, apiLevel);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07003563}
3564
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003565status_t CameraService::getTorchStatusLocked(
3566 const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003567 TorchModeStatus *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003568 if (!status) {
3569 return BAD_VALUE;
3570 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003571 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3572 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003573 // invalid camera ID or the camera doesn't have a flash unit
3574 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003575 }
3576
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003577 *status = mTorchStatusMap.valueAt(index);
3578 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003579}
3580
Chien-Yu Chen88da5262015-02-17 13:56:46 -08003581status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003582 TorchModeStatus status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003583 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
3584 if (index == NAME_NOT_FOUND) {
3585 return BAD_VALUE;
3586 }
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -08003587 mTorchStatusMap.editValueAt(index) = status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08003588
3589 return OK;
3590}
3591
Svet Ganova453d0d2018-01-11 15:37:58 -08003592void CameraService::blockClientsForUid(uid_t uid) {
3593 const auto clients = mActiveClientManager.getAll();
3594 for (auto& current : clients) {
3595 if (current != nullptr) {
3596 const auto basicClient = current->getValue();
3597 if (basicClient.get() != nullptr && basicClient->getClientUid() == uid) {
3598 basicClient->block();
3599 }
3600 }
3601 }
3602}
3603
Michael Grooverd1d435a2018-12-18 17:39:42 -08003604void CameraService::blockAllClients() {
3605 const auto clients = mActiveClientManager.getAll();
3606 for (auto& current : clients) {
3607 if (current != nullptr) {
3608 const auto basicClient = current->getValue();
3609 if (basicClient.get() != nullptr) {
3610 basicClient->block();
3611 }
3612 }
3613 }
3614}
3615
Svet Ganova453d0d2018-01-11 15:37:58 -08003616// NOTE: This is a remote API - make sure all args are validated
3617status_t CameraService::shellCommand(int in, int out, int err, const Vector<String16>& args) {
3618 if (!checkCallingPermission(sManageCameraPermission, nullptr, nullptr)) {
3619 return PERMISSION_DENIED;
3620 }
3621 if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) {
3622 return BAD_VALUE;
3623 }
Nicholas Sauera3620332019-04-03 14:05:17 -07003624 if (args.size() >= 3 && args[0] == String16("set-uid-state")) {
Svet Ganova453d0d2018-01-11 15:37:58 -08003625 return handleSetUidState(args, err);
Nicholas Sauera3620332019-04-03 14:05:17 -07003626 } else if (args.size() >= 2 && args[0] == String16("reset-uid-state")) {
Svet Ganova453d0d2018-01-11 15:37:58 -08003627 return handleResetUidState(args, err);
Nicholas Sauera3620332019-04-03 14:05:17 -07003628 } else if (args.size() >= 2 && args[0] == String16("get-uid-state")) {
Svet Ganova453d0d2018-01-11 15:37:58 -08003629 return handleGetUidState(args, out, err);
3630 } else if (args.size() == 1 && args[0] == String16("help")) {
3631 printHelp(out);
3632 return NO_ERROR;
3633 }
3634 printHelp(err);
3635 return BAD_VALUE;
3636}
3637
3638status_t CameraService::handleSetUidState(const Vector<String16>& args, int err) {
Nicholas Sauera3620332019-04-03 14:05:17 -07003639 String16 packageName = args[1];
3640
Svet Ganova453d0d2018-01-11 15:37:58 -08003641 bool active = false;
3642 if (args[2] == String16("active")) {
3643 active = true;
3644 } else if ((args[2] != String16("idle"))) {
3645 ALOGE("Expected active or idle but got: '%s'", String8(args[2]).string());
3646 return BAD_VALUE;
3647 }
Nicholas Sauera3620332019-04-03 14:05:17 -07003648
3649 int userId = 0;
3650 if (args.size() >= 5 && args[3] == String16("--user")) {
3651 userId = atoi(String8(args[4]));
3652 }
3653
3654 uid_t uid;
3655 if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) {
3656 return BAD_VALUE;
3657 }
3658
3659 mUidPolicy->addOverrideUid(uid, packageName, active);
Svet Ganova453d0d2018-01-11 15:37:58 -08003660 return NO_ERROR;
3661}
3662
3663status_t CameraService::handleResetUidState(const Vector<String16>& args, int err) {
Nicholas Sauera3620332019-04-03 14:05:17 -07003664 String16 packageName = args[1];
3665
3666 int userId = 0;
3667 if (args.size() >= 4 && args[2] == String16("--user")) {
3668 userId = atoi(String8(args[3]));
3669 }
3670
3671 uid_t uid;
3672 if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) {
Svet Ganova453d0d2018-01-11 15:37:58 -08003673 return BAD_VALUE;
3674 }
Nicholas Sauera3620332019-04-03 14:05:17 -07003675
3676 mUidPolicy->removeOverrideUid(uid, packageName);
Svet Ganova453d0d2018-01-11 15:37:58 -08003677 return NO_ERROR;
3678}
3679
3680status_t CameraService::handleGetUidState(const Vector<String16>& args, int out, int err) {
Nicholas Sauera3620332019-04-03 14:05:17 -07003681 String16 packageName = args[1];
3682
3683 int userId = 0;
3684 if (args.size() >= 4 && args[2] == String16("--user")) {
3685 userId = atoi(String8(args[3]));
3686 }
3687
3688 uid_t uid;
3689 if (getUidForPackage(packageName, userId, uid, err) == BAD_VALUE) {
Svet Ganova453d0d2018-01-11 15:37:58 -08003690 return BAD_VALUE;
3691 }
Nicholas Sauera3620332019-04-03 14:05:17 -07003692
3693 if (mUidPolicy->isUidActive(uid, packageName)) {
Svet Ganova453d0d2018-01-11 15:37:58 -08003694 return dprintf(out, "active\n");
3695 } else {
3696 return dprintf(out, "idle\n");
3697 }
3698}
3699
3700status_t CameraService::printHelp(int out) {
3701 return dprintf(out, "Camera service commands:\n"
Nicholas Sauera3620332019-04-03 14:05:17 -07003702 " get-uid-state <PACKAGE> [--user USER_ID] gets the uid state\n"
3703 " set-uid-state <PACKAGE> <active|idle> [--user USER_ID] overrides the uid state\n"
3704 " reset-uid-state <PACKAGE> [--user USER_ID] clears the uid state override\n"
Svet Ganova453d0d2018-01-11 15:37:58 -08003705 " help print this message\n");
3706}
3707
Yin-Chia Yehdba03232019-08-19 15:54:28 -07003708int32_t CameraService::updateAudioRestriction() {
3709 Mutex::Autolock lock(mServiceLock);
3710 return updateAudioRestrictionLocked();
3711}
3712
3713int32_t CameraService::updateAudioRestrictionLocked() {
3714 int32_t mode = 0;
3715 // iterate through all active client
3716 for (const auto& i : mActiveClientManager.getAll()) {
3717 const auto clientSp = i->getValue();
3718 mode |= clientSp->getAudioRestriction();
3719 }
3720
3721 bool modeChanged = (mAudioRestriction != mode);
3722 mAudioRestriction = mode;
3723 if (modeChanged) {
3724 mAppOps.setCameraAudioRestriction(mode);
3725 }
3726 return mode;
3727}
3728
Mathias Agopian65ab4712010-07-14 17:59:35 -07003729}; // namespace android