blob: 85faac6b7da04cb270ca64a0be9c09093934008f [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 Talvalaceb388d2013-02-19 10:40:14 -080035#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <binder/IPCThreadState.h>
37#include <binder/IServiceManager.h>
38#include <binder/MemoryBase.h>
39#include <binder/MemoryHeapBase.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080040#include <binder/ProcessInfoService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070041#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070042#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080043#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070044#include <hardware/hardware.h>
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -070045#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070046#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080047#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070049#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070050#include <utils/Errors.h>
51#include <utils/Log.h>
52#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080053#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080054#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080055#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070056#include <system/camera_metadata.h>
57#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
59#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070060#include "api1/CameraClient.h"
61#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070062#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070063#include "utils/CameraTraces.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070064
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080065namespace {
66 const char* kPermissionServiceName = "permission";
67}; // namespace anonymous
68
Mathias Agopian65ab4712010-07-14 17:59:35 -070069namespace android {
70
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080071using binder::Status;
72using namespace hardware;
73
Mathias Agopian65ab4712010-07-14 17:59:35 -070074// ----------------------------------------------------------------------------
75// Logging support -- this is for debugging only
76// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070077volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070078
Steve Blockb8a80522011-12-20 16:23:08 +000079#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
80#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070081
82static void setLogLevel(int level) {
83 android_atomic_write(level, &gLogLevel);
84}
85
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080086// Convenience methods for constructing binder::Status objects for error returns
87
88#define STATUS_ERROR(errorCode, errorString) \
89 binder::Status::fromServiceSpecificError(errorCode, \
90 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
91
92#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
93 binder::Status::fromServiceSpecificError(errorCode, \
94 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
95 __VA_ARGS__))
96
Mathias Agopian65ab4712010-07-14 17:59:35 -070097// ----------------------------------------------------------------------------
98
Igor Murashkincba2c162013-03-20 15:56:31 -070099extern "C" {
100static void camera_device_status_change(
101 const struct camera_module_callbacks* callbacks,
102 int camera_id,
103 int new_status) {
104 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -0800105 static_cast<const CameraService*>(callbacks));
Igor Murashkincba2c162013-03-20 15:56:31 -0700106
Bin Chene16d1162016-02-22 18:19:58 +1100107 cs->onDeviceStatusChanged(camera_id,
Ruben Brunkcc776712015-02-17 20:18:47 -0800108 static_cast<camera_device_status_t>(new_status));
Igor Murashkincba2c162013-03-20 15:56:31 -0700109}
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800110
111static void torch_mode_status_change(
112 const struct camera_module_callbacks* callbacks,
113 const char* camera_id,
114 int new_status) {
115 if (!callbacks || !camera_id) {
116 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
117 callbacks, camera_id);
118 }
119 sp<CameraService> cs = const_cast<CameraService*>(
120 static_cast<const CameraService*>(callbacks));
121
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800122 int32_t status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800123 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800124 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800125 status = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
126 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800127 case TORCH_MODE_STATUS_AVAILABLE_OFF:
128 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
129 break;
130 case TORCH_MODE_STATUS_AVAILABLE_ON:
131 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800132 break;
133 default:
134 ALOGE("Unknown torch status %d", new_status);
135 return;
136 }
137
138 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800139 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800140 status);
141}
Igor Murashkincba2c162013-03-20 15:56:31 -0700142} // extern "C"
143
Mathias Agopian65ab4712010-07-14 17:59:35 -0700144// ----------------------------------------------------------------------------
145
146// This is ugly and only safe if we never re-create the CameraService, but
147// should be ok for now.
148static CameraService *gCameraService;
149
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800150CameraService::CameraService() :
151 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800152 mNumberOfCameras(0), mNumberOfNormalCameras(0),
153 mSoundRef(0), mModule(nullptr) {
Steve Blockdf64d152012-01-04 20:05:49 +0000154 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155 gCameraService = this;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800156
Igor Murashkincba2c162013-03-20 15:56:31 -0700157 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800158 this->torch_mode_status_change = android::torch_mode_status_change;
159
Ruben Brunkcc776712015-02-17 20:18:47 -0800160 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700161}
162
Iliyan Malchev8951a972011-04-14 16:55:59 -0700163void CameraService::onFirstRef()
164{
Ruben Brunkcc776712015-02-17 20:18:47 -0800165 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800166
Iliyan Malchev8951a972011-04-14 16:55:59 -0700167 BnCameraService::onFirstRef();
168
Ruben Brunk99e69712015-05-26 17:25:07 -0700169 // Update battery life tracking if service is restarting
170 BatteryNotifier& notifier(BatteryNotifier::getInstance());
171 notifier.noteResetCamera();
172 notifier.noteResetFlashlight();
173
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800174 camera_module_t *rawModule;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700175 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
176 (const hw_module_t **)&rawModule);
177 if (err < 0) {
178 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
179 logServiceError("Could not load camera HAL module", err);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700180 return;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700181 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800182
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700183 mModule = new CameraModule(rawModule);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700184 err = mModule->init();
185 if (err != OK) {
186 ALOGE("Could not initialize camera HAL module: %d (%s)", err,
187 strerror(-err));
188 logServiceError("Could not initialize camera HAL module", err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800189
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700190 delete mModule;
191 mModule = nullptr;
192 return;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700193 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700194 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700195
196 mNumberOfCameras = mModule->getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700197 mNumberOfNormalCameras = mNumberOfCameras;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700198
Yin-Chia Yehd4a653a2015-10-14 11:05:44 -0700199 // Setup vendor tags before we call get_camera_info the first time
200 // because HAL might need to setup static vendor keys in get_camera_info
201 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
202 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
203 setUpVendorTags();
204 }
205
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700206 mFlashlight = new CameraFlashlight(*mModule, *this);
207 status_t res = mFlashlight->findFlashUnits();
208 if (res) {
209 // impossible because we haven't open any camera devices.
210 ALOGE("Failed to find flash units.");
211 }
212
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700213 int latestStrangeCameraId = INT_MAX;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700214 for (int i = 0; i < mNumberOfCameras; i++) {
215 String8 cameraId = String8::format("%d", i);
216
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700217 // Get camera info
218
219 struct camera_info info;
220 bool haveInfo = true;
221 status_t rc = mModule->getCameraInfo(i, &info);
222 if (rc != NO_ERROR) {
223 ALOGE("%s: Received error loading camera info for device %d, cost and"
224 " conflicting devices fields set to defaults for this device.",
225 __FUNCTION__, i);
226 haveInfo = false;
227 }
228
229 // Check for backwards-compatibility support
230 if (haveInfo) {
231 if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) {
232 delete mModule;
233 mModule = nullptr;
234 return;
235 }
236 }
237
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700238 // Defaults to use for cost and conflicting devices
239 int cost = 100;
240 char** conflicting_devices = nullptr;
241 size_t conflicting_devices_length = 0;
242
243 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700244 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
245 cost = info.resource_cost;
246 conflicting_devices = info.conflicting_devices;
247 conflicting_devices_length = info.conflicting_devices_length;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700248 }
249
250 std::set<String8> conflicting;
251 for (size_t i = 0; i < conflicting_devices_length; i++) {
252 conflicting.emplace(String8(conflicting_devices[i]));
253 }
254
255 // Initialize state for each camera device
256 {
257 Mutex::Autolock lock(mCameraStatesLock);
258 mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost,
259 conflicting));
260 }
261
262 if (mFlashlight->hasFlashUnit(cameraId)) {
263 mTorchStatusMap.add(cameraId,
264 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF);
265 }
266 }
267
268 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
269 mModule->setCallbacks(this);
270 }
271
Ruben Brunk2823ce02015-05-19 17:25:13 -0700272 CameraService::pingCameraServiceProxy();
273}
274
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700275sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800276 sp<ICameraServiceProxy> proxyBinder = nullptr;
277#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700278 sp<IServiceManager> sm = defaultServiceManager();
279 sp<IBinder> binder = sm->getService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800280 if (binder != nullptr) {
281 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700282 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800283#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700284 return proxyBinder;
285}
286
287void CameraService::pingCameraServiceProxy() {
288 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
289 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700290 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700291}
292
Mathias Agopian65ab4712010-07-14 17:59:35 -0700293CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800294 if (mModule) {
295 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800296 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800297 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800298 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Ruben Brunkcc776712015-02-17 20:18:47 -0800299 gCameraService = nullptr;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300}
301
Bin Chene16d1162016-02-22 18:19:58 +1100302void CameraService::onDeviceStatusChanged(int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800303 camera_device_status_t newStatus) {
304 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
Igor Murashkincba2c162013-03-20 15:56:31 -0700305 cameraId, newStatus);
306
Ruben Brunkcc776712015-02-17 20:18:47 -0800307 String8 id = String8::format("%d", cameraId);
308 std::shared_ptr<CameraState> state = getCameraState(id);
309
310 if (state == nullptr) {
Igor Murashkincba2c162013-03-20 15:56:31 -0700311 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
312 return;
313 }
314
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800315 int32_t oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800316
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800317 if (oldStatus == static_cast<int32_t>(newStatus)) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800318 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700319 return;
320 }
321
Igor Murashkincba2c162013-03-20 15:56:31 -0700322 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700323 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
324 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800325 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700326 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800327 // Don't do this in updateStatus to avoid deadlock over mServiceLock
328 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700329
Ruben Brunkcc776712015-02-17 20:18:47 -0800330 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
331 // to this device until the status changes
332 updateStatus(ICameraServiceListener::STATUS_NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700333
Ruben Brunkcc776712015-02-17 20:18:47 -0800334 // Remove cached shim parameters
335 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700336
Ruben Brunkcc776712015-02-17 20:18:47 -0800337 // Remove the client from the list of active clients
338 clientToDisconnect = removeClientLocked(id);
339
340 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800341 clientToDisconnect->notifyError(
342 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800343 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700344 }
345
Ruben Brunkcc776712015-02-17 20:18:47 -0800346 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
347 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700348
Ruben Brunkcc776712015-02-17 20:18:47 -0800349 // Disconnect client
350 if (clientToDisconnect.get() != nullptr) {
351 // Ensure not in binder RPC so client disconnect PID checks work correctly
352 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
353 "onDeviceStatusChanged must be called from the camera service process!");
354 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700355 }
356
Ruben Brunkcc776712015-02-17 20:18:47 -0800357 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800358 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700359 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
360 newStatus));
361 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800362 updateStatus(static_cast<int32_t>(newStatus), id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700363 }
364
Igor Murashkincba2c162013-03-20 15:56:31 -0700365}
366
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800367void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800368 int32_t newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800369 Mutex::Autolock al(mTorchStatusMutex);
370 onTorchStatusChangedLocked(cameraId, newStatus);
371}
372
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800373void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800374 int32_t newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800375 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
376 __FUNCTION__, cameraId.string(), newStatus);
377
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800378 int32_t status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800379 status_t res = getTorchStatusLocked(cameraId, &status);
380 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700381 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
382 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800383 return;
384 }
385 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800386 return;
387 }
388
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800389 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800390 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800391 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
392 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800393 return;
394 }
395
Ruben Brunkcc776712015-02-17 20:18:47 -0800396 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700397 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700398 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700399 auto iter = mTorchUidMap.find(cameraId);
400 if (iter != mTorchUidMap.end()) {
401 int oldUid = iter->second.second;
402 int newUid = iter->second.first;
403 BatteryNotifier& notifier(BatteryNotifier::getInstance());
404 if (oldUid != newUid) {
405 // If the UID has changed, log the status and update current UID in mTorchUidMap
406 if (status == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
407 notifier.noteFlashlightOff(cameraId, oldUid);
408 }
409 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
410 notifier.noteFlashlightOn(cameraId, newUid);
411 }
412 iter->second.second = newUid;
413 } else {
414 // If the UID has not changed, log the status
415 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
416 notifier.noteFlashlightOn(cameraId, oldUid);
417 } else {
418 notifier.noteFlashlightOff(cameraId, oldUid);
419 }
420 }
421 }
422 }
423
424 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800425 Mutex::Autolock lock(mStatusListenerLock);
426 for (auto& i : mListenerList) {
427 i->onTorchStatusChanged(newStatus, String16{cameraId});
428 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800429 }
430}
431
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800432Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700433 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700434 switch (type) {
435 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800436 *numCameras = mNumberOfNormalCameras;
437 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700438 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800439 *numCameras = mNumberOfCameras;
440 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700441 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800442 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700443 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800444 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
445 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700446 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800447 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448}
449
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800450Status CameraService::getCameraInfo(int cameraId,
451 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700452 ATRACE_CALL();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700453 if (!mModule) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800454 return STATUS_ERROR(ERROR_DISCONNECTED,
455 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700456 }
457
Mathias Agopian65ab4712010-07-14 17:59:35 -0700458 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800459 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
460 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461 }
462
Iliyan Malchev8951a972011-04-14 16:55:59 -0700463 struct camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800464 Status rc = filterGetInfoErrorCode(
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800465 mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800466
467 if (rc.isOk()) {
468 cameraInfo->facing = info.facing;
469 cameraInfo->orientation = info.orientation;
Yin-Chia Yeh9b5a6e92016-04-22 13:24:05 -0700470 // CameraInfo is for android.hardware.Camera which does not
471 // support external camera facing. The closest approximation would be
472 // front camera.
Yin-Chia Yeh13cd59a2016-11-29 12:13:15 -0800473 if (cameraInfo->facing == CAMERA_FACING_EXTERNAL) {
474 cameraInfo->facing = CAMERA_FACING_FRONT;
Yin-Chia Yeh9b5a6e92016-04-22 13:24:05 -0700475 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800476 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700477 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700478}
479
Ruben Brunkcc776712015-02-17 20:18:47 -0800480int CameraService::cameraIdToInt(const String8& cameraId) {
481 errno = 0;
482 size_t pos = 0;
483 int ret = stoi(std::string{cameraId.string()}, &pos);
484 if (errno != 0 || pos != cameraId.size()) {
485 return -1;
486 }
487 return ret;
488}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700489
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800490Status CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700491 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800492
493 Status ret = Status::ok();
494
Ruben Brunkb2119af2014-05-09 19:57:56 -0700495 struct CameraInfo info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800496 if (!(ret = getCameraInfo(cameraId, &info)).isOk()) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700497 return ret;
498 }
499
500 CameraMetadata shimInfo;
501 int32_t orientation = static_cast<int32_t>(info.orientation);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800502 status_t rc;
503 if ((rc = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) {
504 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
505 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700506 }
507
508 uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ?
509 ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800510 if ((rc = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) {
511 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
512 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700513 }
514
Igor Murashkin65d14b92014-06-17 12:03:20 -0700515 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800516 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -0700517 // Error logged by callee
518 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700519 }
520
521 Vector<Size> sizes;
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700522 Vector<Size> jpegSizes;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700523 Vector<int32_t> formats;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700524 {
525 shimParams.getSupportedPreviewSizes(/*out*/sizes);
526 shimParams.getSupportedPreviewFormats(/*out*/formats);
527 shimParams.getSupportedPictureSizes(/*out*/jpegSizes);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700528 }
529
530 // Always include IMPLEMENTATION_DEFINED
531 formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
532
533 const size_t INTS_PER_CONFIG = 4;
534
535 // Build available stream configurations metadata
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700536 size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG;
537
538 Vector<int32_t> streamConfigs;
539 streamConfigs.setCapacity(streamConfigSize);
540
Ruben Brunkb2119af2014-05-09 19:57:56 -0700541 for (size_t i = 0; i < formats.size(); ++i) {
542 for (size_t j = 0; j < sizes.size(); ++j) {
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700543 streamConfigs.add(formats[i]);
544 streamConfigs.add(sizes[j].width);
545 streamConfigs.add(sizes[j].height);
546 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700547 }
548 }
549
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700550 for (size_t i = 0; i < jpegSizes.size(); ++i) {
551 streamConfigs.add(HAL_PIXEL_FORMAT_BLOB);
552 streamConfigs.add(jpegSizes[i].width);
553 streamConfigs.add(jpegSizes[i].height);
554 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
555 }
556
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800557 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700558 streamConfigs.array(), streamConfigSize)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800559 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
560 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700561 }
562
563 int64_t fakeMinFrames[0];
564 // TODO: Fixme, don't fake min frame durations.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800565 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
Ruben Brunkb2119af2014-05-09 19:57:56 -0700566 fakeMinFrames, 0)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800567 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
568 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700569 }
570
571 int64_t fakeStalls[0];
572 // TODO: Fixme, don't fake stall durations.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800573 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
Ruben Brunkb2119af2014-05-09 19:57:56 -0700574 fakeStalls, 0)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800575 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
576 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700577 }
578
579 *cameraInfo = shimInfo;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800580 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700581}
582
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800583Status CameraService::getCameraCharacteristics(int cameraId,
Zhijun He2b59be82013-09-25 10:14:30 -0700584 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700585 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700586 if (!cameraInfo) {
587 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700589 }
590
591 if (!mModule) {
592 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800593 return STATUS_ERROR(ERROR_DISCONNECTED,
594 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700595 }
596
Zhijun He2b59be82013-09-25 10:14:30 -0700597 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
598 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800599 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
600 "Invalid camera id: %d", cameraId);
Zhijun He2b59be82013-09-25 10:14:30 -0700601 }
602
603 int facing;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800604 Status ret;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800605 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0 ||
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800606 getDeviceVersion(cameraId, &facing) < CAMERA_DEVICE_API_VERSION_3_0) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700607 /**
608 * Backwards compatibility mode for old HALs:
609 * - Convert CameraInfo into static CameraMetadata properties.
610 * - Retrieve cached CameraParameters for this camera. If none exist,
611 * attempt to open CameraClient and retrieve the CameraParameters.
612 * - Convert cached CameraParameters into static CameraMetadata
613 * properties.
614 */
615 ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__);
Zhijun He2b59be82013-09-25 10:14:30 -0700616
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800617 ret = generateShimMetadata(cameraId, cameraInfo);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700618 } else {
619 /**
620 * Normal HAL 2.1+ codepath.
621 */
622 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800623 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800624 if (ret.isOk()) {
625 *cameraInfo = info.static_camera_characteristics;
626 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700627 }
Zhijun He2b59be82013-09-25 10:14:30 -0700628
629 return ret;
630}
631
Ruben Brunkcc776712015-02-17 20:18:47 -0800632int CameraService::getCallingPid() {
633 return IPCThreadState::self()->getCallingPid();
634}
635
636int CameraService::getCallingUid() {
637 return IPCThreadState::self()->getCallingUid();
638}
639
640String8 CameraService::getFormattedCurrentTime() {
641 time_t now = time(nullptr);
642 char formattedTime[64];
643 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
644 return String8(formattedTime);
645}
646
647int CameraService::getCameraPriorityFromProcState(int procState) {
648 // Find the priority for the camera usage based on the process state. Higher priority clients
649 // win for evictions.
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700650 if (procState < 0) {
651 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
652 procState);
653 return -1;
Ruben Brunkcc776712015-02-17 20:18:47 -0800654 }
Eino-Ville Talvala52aad852015-09-03 12:24:24 -0700655 // Treat sleeping TOP processes the same as regular TOP processes, for
656 // access priority. This is important for lock-screen camera launch scenarios
657 if (procState == PROCESS_STATE_TOP_SLEEPING) {
658 procState = PROCESS_STATE_TOP;
659 }
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700660 return INT_MAX - procState;
Ruben Brunkcc776712015-02-17 20:18:47 -0800661}
662
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800663Status CameraService::getCameraVendorTagDescriptor(
664 /*out*/
665 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700666 ATRACE_CALL();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800667 if (!mModule) {
668 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800669 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800670 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800671 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
672 if (globalDescriptor != nullptr) {
673 *desc = *(globalDescriptor.get());
674 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800675 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800676}
677
Igor Murashkin634a5152013-02-20 17:15:11 -0800678int CameraService::getDeviceVersion(int cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700679 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800680 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800681 if (mModule->getCameraInfo(cameraId, &info) != OK) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800682 return -1;
683 }
684
685 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800686 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800687 deviceVersion = info.device_version;
688 } else {
689 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
690 }
691
692 if (facing) {
693 *facing = info.facing;
694 }
695
696 return deviceVersion;
697}
698
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800699Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700700 switch(err) {
701 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800702 return Status::ok();
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700703 case -EINVAL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800704 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
705 "CameraId is not valid for HAL module");
706 case -ENODEV:
707 return STATUS_ERROR(ERROR_DISCONNECTED,
708 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700709 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800710 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
711 "Camera HAL encountered error %d: %s",
712 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700713 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800714}
715
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800716bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700717 ATRACE_CALL();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800718 vendor_tag_ops_t vOps = vendor_tag_ops_t();
719
720 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800721 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800722 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
723 return false;
724 }
725
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800726 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800727
728 // Ensure all vendor operations are present
729 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
730 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
731 vOps.get_tag_type == NULL) {
732 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
733 , __FUNCTION__);
734 return false;
735 }
736
737 // Read all vendor tag definitions into a descriptor
738 sp<VendorTagDescriptor> desc;
739 status_t res;
740 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
741 != OK) {
742 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
743 "received error %s (%d). Camera clients will not be able to use"
744 "vendor tags", __FUNCTION__, strerror(res), res);
745 return false;
746 }
747
748 // Set the global descriptor to use with camera metadata
749 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
750 return true;
751}
752
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800753Status CameraService::makeClient(const sp<CameraService>& cameraService,
754 const sp<IInterface>& cameraCb, const String16& packageName, int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800755 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
756 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
757 /*out*/sp<BasicClient>* client) {
758
Ruben Brunkcc776712015-02-17 20:18:47 -0800759 if (halVersion < 0 || halVersion == deviceVersion) {
760 // Default path: HAL version is unspecified by caller, create CameraClient
761 // based on device version reported by the HAL.
762 switch(deviceVersion) {
763 case CAMERA_DEVICE_API_VERSION_1_0:
764 if (effectiveApiLevel == API_1) { // Camera1 API route
765 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800766 *client = new CameraClient(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800767 clientPid, clientUid, getpid(), legacyMode);
768 } else { // Camera2 API route
769 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800770 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
771 "Camera device \"%d\" HAL version %d does not support camera2 API",
772 cameraId, deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800773 }
774 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800775 case CAMERA_DEVICE_API_VERSION_3_0:
776 case CAMERA_DEVICE_API_VERSION_3_1:
777 case CAMERA_DEVICE_API_VERSION_3_2:
778 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700779 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800780 if (effectiveApiLevel == API_1) { // Camera1 API route
781 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800782 *client = new Camera2Client(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800783 clientPid, clientUid, servicePid, legacyMode);
784 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800785 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
786 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
787 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800788 facing, clientPid, clientUid, servicePid);
789 }
790 break;
791 default:
792 // Should not be reachable
793 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800794 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
795 "Camera device \"%d\" has unknown HAL version %d",
796 cameraId, deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800797 }
798 } else {
799 // A particular HAL version is requested by caller. Create CameraClient
800 // based on the requested HAL version.
801 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
802 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
803 // Only support higher HAL version device opened as HAL1.0 device.
804 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800805 *client = new CameraClient(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800806 clientPid, clientUid, servicePid, legacyMode);
807 } else {
808 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
809 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
810 " opened as HAL %x device", halVersion, deviceVersion,
811 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800812 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
813 "Camera device \"%d\" (HAL version %d) cannot be opened as HAL version %d",
814 cameraId, deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800815 }
816 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800817 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800818}
819
Ruben Brunk6267b532015-04-30 17:44:07 -0700820String8 CameraService::toString(std::set<userid_t> intSet) {
821 String8 s("");
822 bool first = true;
823 for (userid_t i : intSet) {
824 if (first) {
825 s.appendFormat("%d", i);
826 first = false;
827 } else {
828 s.appendFormat(", %d", i);
829 }
830 }
831 return s;
832}
833
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800834Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800835 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700836
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800837 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800838 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800839 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800840 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800841 if (!(ret = connectHelper<ICameraClient,Client>(
842 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
843 internalPackageName, uid, USE_CALLING_PID,
844 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
845 /*out*/ tmp)
846 ).isOk()) {
847 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700848 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800849 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700850}
851
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800852Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700853 /*out*/
854 CameraParameters* parameters) {
855
856 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
857
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800858 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700859
860 if (parameters == NULL) {
861 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800862 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700863 }
864
Ruben Brunkcc776712015-02-17 20:18:47 -0800865 String8 id = String8::format("%d", cameraId);
866
867 // Check if we already have parameters
868 {
869 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700870 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800871 auto cameraState = getCameraState(id);
872 if (cameraState == nullptr) {
873 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800874 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
875 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800876 }
877 CameraParameters p = cameraState->getShimParams();
878 if (!p.isEmpty()) {
879 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800880 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700881 }
882 }
883
Ruben Brunkcc776712015-02-17 20:18:47 -0800884 int64_t token = IPCThreadState::self()->clearCallingIdentity();
885 ret = initializeShimMetadata(cameraId);
886 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800887 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800888 // Error already logged by callee
889 return ret;
890 }
891
892 // Check for parameters again
893 {
894 // Scope for service lock
895 Mutex::Autolock lock(mServiceLock);
896 auto cameraState = getCameraState(id);
897 if (cameraState == nullptr) {
898 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800899 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
900 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700901 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800902 CameraParameters p = cameraState->getShimParams();
903 if (!p.isEmpty()) {
904 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800905 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700906 }
907 }
908
Ruben Brunkcc776712015-02-17 20:18:47 -0800909 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
910 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800911 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700912}
913
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800914// Can camera service trust the caller based on the calling UID?
915static bool isTrustedCallingUid(uid_t uid) {
916 switch (uid) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700917 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800918 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700919 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800920 return true;
921 default:
922 return false;
923 }
924}
925
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800926Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700927 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
928 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500929
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700930#ifdef __BRILLO__
931 UNUSED(clientName8);
932 UNUSED(clientUid);
933 UNUSED(clientPid);
934 UNUSED(originalClientPid);
935#else
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700936 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
937 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800938 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -0800939 return allowed;
940 }
Alex Deymo9c2a2c22016-08-25 11:59:14 -0700941#endif // __BRILLO__
Christopher Wileyce761d12016-02-16 10:15:00 -0800942
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800943 int callingPid = getCallingPid();
944
Iliyan Malchev8951a972011-04-14 16:55:59 -0700945 if (!mModule) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800946 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
947 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800948 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
949 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700950 }
951
Ruben Brunkcc776712015-02-17 20:18:47 -0800952 if (getCameraState(cameraId) == nullptr) {
953 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
954 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800955 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
956 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700957 }
958
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800959 status_t err = checkIfDeviceIsUsable(cameraId);
960 if (err != NO_ERROR) {
961 switch(err) {
962 case -ENODEV:
963 case -EBUSY:
964 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
965 "No camera device with ID \"%s\" currently available", cameraId.string());
966 default:
967 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
968 "Unknown error connecting to ID \"%s\"", cameraId.string());
969 }
970 }
971 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800972}
973
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800974Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700975 const String8& clientName8, int& clientUid, int& clientPid,
976 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800977 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800978 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700979
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800980 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800982 clientUid = callingUid;
983 } else if (!isTrustedCallingUid(callingUid)) {
984 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
985 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800986 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
987 "Untrusted caller (calling PID %d, UID %d) trying to "
988 "forward camera access to camera %s for client %s (PID %d, UID %d)",
989 callingPid, callingUid, cameraId.string(),
990 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700991 }
992
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800993 // Check if we can trust clientPid
994 if (clientPid == USE_CALLING_PID) {
995 clientPid = callingPid;
996 } else if (!isTrustedCallingUid(callingUid)) {
997 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
998 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800999 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1000 "Untrusted caller (calling PID %d, UID %d) trying to "
1001 "forward camera access to camera %s for client %s (PID %d, UID %d)",
1002 callingPid, callingUid, cameraId.string(),
1003 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001004 }
1005
1006 // If it's not calling from cameraserver, check the permission.
1007 if (callingPid != getpid() &&
1008 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
1009 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001010 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1011 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1012 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001013 }
1014
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001015 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1016 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001017 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001018 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001019
Ruben Brunk6267b532015-04-30 17:44:07 -07001020 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001021
Ruben Brunka8ca9152015-04-07 14:23:40 -07001022 // Only allow clients who are being used by the current foreground device user, unless calling
1023 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001024 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1025 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1026 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1027 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001028 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1029 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1030 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001031 }
1032
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001033 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001034}
1035
1036status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1037 auto cameraState = getCameraState(cameraId);
1038 int callingPid = getCallingPid();
1039 if (cameraState == nullptr) {
1040 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1041 cameraId.string());
1042 return -ENODEV;
1043 }
1044
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001045 int32_t currentStatus = cameraState->getStatus();
Igor Murashkincba2c162013-03-20 15:56:31 -07001046 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001047 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1048 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001049 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -07001050 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001051 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1052 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001053 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001054 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001055
Ruben Brunkcc776712015-02-17 20:18:47 -08001056 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001057}
1058
Ruben Brunkcc776712015-02-17 20:18:47 -08001059void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1060 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001061
Ruben Brunkcc776712015-02-17 20:18:47 -08001062 // Make a descriptor for the incoming client
1063 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1064 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1065
1066 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1067 String8(client->getPackageName()));
1068
1069 if (evicted.size() > 0) {
1070 // This should never happen - clients should already have been removed in disconnect
1071 for (auto& i : evicted) {
1072 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1073 __FUNCTION__, i->getKey().string());
1074 }
1075
1076 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1077 __FUNCTION__);
1078 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001079
1080 // And register a death notification for the client callback. Do
1081 // this last to avoid Binder policy where a nested Binder
1082 // transaction might be pre-empted to service the client death
1083 // notification if the client process dies before linkToDeath is
1084 // invoked.
1085 sp<IBinder> remoteCallback = client->getRemote();
1086 if (remoteCallback != nullptr) {
1087 remoteCallback->linkToDeath(this);
1088 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001089}
1090
1091status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1092 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1093 /*out*/
1094 sp<BasicClient>* client,
1095 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001096 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001097 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001098 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001099 DescriptorPtr clientDescriptor;
1100 {
1101 if (effectiveApiLevel == API_1) {
1102 // If we are using API1, any existing client for this camera ID with the same remote
1103 // should be returned rather than evicted to allow MediaRecorder to work properly.
1104
1105 auto current = mActiveClientManager.get(cameraId);
1106 if (current != nullptr) {
1107 auto clientSp = current->getValue();
1108 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001109 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1110 ALOGW("CameraService connect called from same client, but with a different"
1111 " API level, evicting prior client...");
1112 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001113 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001114 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001115 *client = clientSp;
1116 return NO_ERROR;
1117 }
1118 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001119 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001120 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001121
Ruben Brunkcc776712015-02-17 20:18:47 -08001122 // Get current active client PIDs
1123 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1124 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001125
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001126 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1127 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1128 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1129 // not have an out-of-class definition.
1130 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001131
Ruben Brunkcc776712015-02-17 20:18:47 -08001132 // Get priorites of all active PIDs
1133 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1134 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001135
Ruben Brunkcc776712015-02-17 20:18:47 -08001136 // Update all active clients' priorities
1137 std::map<int,int> pidToPriorityMap;
1138 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1139 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1140 }
1141 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001142
Ruben Brunkcc776712015-02-17 20:18:47 -08001143 // Get state for the given cameraId
1144 auto state = getCameraState(cameraId);
1145 if (state == nullptr) {
1146 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1147 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001148 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001149 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001150 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001151
1152 // Make descriptor for incoming client
1153 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1154 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1155 state->getConflicting(),
1156 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1157
1158 // Find clients that would be evicted
1159 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1160
1161 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1162 // background, so we cannot do evictions
1163 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1164 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1165 " priority).", clientPid);
1166
1167 sp<BasicClient> clientSp = clientDescriptor->getValue();
1168 String8 curTime = getFormattedCurrentTime();
1169 auto incompatibleClients =
1170 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1171
1172 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001173 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001174 cameraId.string(), packageName.string(), clientPid,
1175 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1176
1177 for (auto& i : incompatibleClients) {
1178 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1179 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1180 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1181 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001182 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1183 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1184 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1185 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001186 }
1187
1188 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001189 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001190 mEventLog.add(msg);
1191
1192 return -EBUSY;
1193 }
1194
1195 for (auto& i : evicted) {
1196 sp<BasicClient> clientSp = i->getValue();
1197 if (clientSp.get() == nullptr) {
1198 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1199
1200 // TODO: Remove this
1201 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1202 __FUNCTION__);
1203 mActiveClientManager.remove(i);
1204 continue;
1205 }
1206
1207 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1208 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001209 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001210
Ruben Brunkcc776712015-02-17 20:18:47 -08001211 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001212 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1213 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1214 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001215 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1216 i->getOwnerId(), i->getPriority(), cameraId.string(),
1217 packageName.string(), clientPid,
1218 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1219
1220 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001221 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001222 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001223 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001224 }
1225
Ruben Brunkcc776712015-02-17 20:18:47 -08001226 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1227 // other clients from connecting in mServiceLockWrapper if held
1228 mServiceLock.unlock();
1229
1230 // Clear caller identity temporarily so client disconnect PID checks work correctly
1231 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1232
1233 // Destroy evicted clients
1234 for (auto& i : evictedClients) {
1235 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001236 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001237 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001238
1239 IPCThreadState::self()->restoreCallingIdentity(token);
1240
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001241 for (const auto& i : evictedClients) {
1242 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1243 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1244 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1245 if (ret == TIMED_OUT) {
1246 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1247 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1248 mActiveClientManager.toString().string());
1249 return -EBUSY;
1250 }
1251 if (ret != NO_ERROR) {
1252 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1253 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1254 ret, mActiveClientManager.toString().string());
1255 return ret;
1256 }
1257 }
1258
1259 evictedClients.clear();
1260
Ruben Brunkcc776712015-02-17 20:18:47 -08001261 // Once clients have been disconnected, relock
1262 mServiceLock.lock();
1263
1264 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1265 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1266 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001267 }
1268
Ruben Brunkcc776712015-02-17 20:18:47 -08001269 *partial = clientDescriptor;
1270 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001271}
1272
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001273Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001274 const sp<ICameraClient>& cameraClient,
1275 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001276 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001277 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001278 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001279 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001280 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001281
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001282 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001283 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001284 String8 id = String8::format("%d", cameraId);
1285 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001286 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1287 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1288 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1289 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001290
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001291 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001292 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001293 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001294 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295 }
1296
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001297 *device = client;
1298 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001299}
1300
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001301Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001302 const sp<ICameraClient>& cameraClient,
1303 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001304 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001305 int clientUid,
1306 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001307 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001308
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001309 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001310 String8 id = String8::format("%d", cameraId);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08001311 int apiVersion = mModule->getModuleApiVersion();
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001312 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001313 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001314 /*
1315 * Either the HAL version is unspecified in which case this just creates
1316 * a camera client selected by the latest device version, or
1317 * it's a particular version in which case the HAL must supported
1318 * the open_legacy call
1319 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001320 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1321 apiVersion);
1322 ALOGE("%s: %s",
1323 __FUNCTION__, msg.string());
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001324 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001325 msg);
1326 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001327 }
1328
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001329 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001330 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001331 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1332 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1333 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1334 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001335
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001336 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001337 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001338 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001339 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001340 }
1341
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001342 *device = client;
1343 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001344}
1345
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001346Status CameraService::connectDevice(
1347 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Ruben Brunkcc776712015-02-17 20:18:47 -08001348 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001349 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001350 int clientUid,
1351 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001352 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001353
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001354 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001355 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001356 String8 id = String8::format("%d", cameraId);
1357 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001358 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1359 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1360 clientUid, USE_CALLING_PID, API_2,
1361 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1362 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001363
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001364 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001365 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001366 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001367 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001368 }
1369
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001370 *device = client;
1371 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001372}
1373
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001374Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001375 const sp<IBinder>& clientBinder) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001376
1377 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001378 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001379 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1381 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001382 }
1383
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001384 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001385 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001386
1387 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001388 auto state = getCameraState(id);
1389 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001390 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001391 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1392 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001393 }
1394
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001395 int32_t cameraStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -08001396 if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
1397 cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001398 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001399 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1400 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001401 }
1402
1403 {
1404 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001405 int32_t status;
1406 status_t err = getTorchStatusLocked(id, &status);
1407 if (err != OK) {
1408 if (err == NAME_NOT_FOUND) {
1409 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1410 "Camera \"%s\" does not have a flash unit", id.string());
1411 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001412 ALOGE("%s: getting current torch status failed for camera %s",
1413 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001414 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1415 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1416 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001417 }
1418
1419 if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001420 if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001421 ALOGE("%s: torch mode of camera %s is not available because "
1422 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1424 "Torch for camera \"%s\" is not available due to an existing camera user",
1425 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001426 } else {
1427 ALOGE("%s: torch mode of camera %s is not available due to "
1428 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001429 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1430 "Torch for camera \"%s\" is not available due to insufficient resources",
1431 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001432 }
1433 }
1434 }
1435
Ruben Brunk99e69712015-05-26 17:25:07 -07001436 {
1437 // Update UID map - this is used in the torch status changed callbacks, so must be done
1438 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001439 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001440 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1441 mTorchUidMap[id].first = uid;
1442 mTorchUidMap[id].second = uid;
1443 } else {
1444 // Set the pending UID
1445 mTorchUidMap[id].first = uid;
1446 }
1447 }
1448
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001449 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001450
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001451 if (err != OK) {
1452 int32_t errorCode;
1453 String8 msg;
1454 switch (err) {
1455 case -ENOSYS:
1456 msg = String8::format("Camera \"%s\" has no flashlight",
1457 id.string());
1458 errorCode = ERROR_ILLEGAL_ARGUMENT;
1459 break;
1460 default:
1461 msg = String8::format(
1462 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1463 id.string(), enabled, strerror(-err), err);
1464 errorCode = ERROR_INVALID_OPERATION;
1465 }
1466 ALOGE("%s: %s", __FUNCTION__, msg.string());
1467 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001468 }
1469
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001470 {
1471 // update the link to client's death
1472 Mutex::Autolock al(mTorchClientMapMutex);
1473 ssize_t index = mTorchClientMap.indexOfKey(id);
1474 if (enabled) {
1475 if (index == NAME_NOT_FOUND) {
1476 mTorchClientMap.add(id, clientBinder);
1477 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001478 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001479 mTorchClientMap.replaceValueAt(index, clientBinder);
1480 }
1481 clientBinder->linkToDeath(this);
1482 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001483 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001484 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001485 }
1486
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001487 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001488}
1489
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001490Status CameraService::notifySystemEvent(int32_t eventId,
1491 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001492 ATRACE_CALL();
1493
Ruben Brunk36597b22015-03-20 22:15:57 -07001494 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001495 case ICameraService::EVENT_USER_SWITCHED: {
1496 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001497 break;
1498 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001499 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001500 default: {
1501 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1502 eventId);
1503 break;
1504 }
1505 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001506 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001507}
1508
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001509Status CameraService::addListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001510 ATRACE_CALL();
1511
Igor Murashkinbfc99152013-02-27 12:55:20 -08001512 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001513
Ruben Brunk3450ba72015-06-16 11:00:37 -07001514 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001515 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001516 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001517 }
1518
Igor Murashkinbfc99152013-02-27 12:55:20 -08001519 Mutex::Autolock lock(mServiceLock);
1520
Ruben Brunkcc776712015-02-17 20:18:47 -08001521 {
1522 Mutex::Autolock lock(mStatusListenerLock);
1523 for (auto& it : mListenerList) {
1524 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1525 ALOGW("%s: Tried to add listener %p which was already subscribed",
1526 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001527 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001528 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001529 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001530
1531 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001532 }
1533
Igor Murashkinbfc99152013-02-27 12:55:20 -08001534
Igor Murashkincba2c162013-03-20 15:56:31 -07001535 /* Immediately signal current status to this listener only */
1536 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001537 Mutex::Autolock lock(mCameraStatesLock);
1538 for (auto& i : mCameraStates) {
1539 // TODO: Update binder to use String16 for camera IDs and remove;
1540 int id = cameraIdToInt(i.first);
1541 if (id == -1) continue;
1542
1543 listener->onStatusChanged(i.second->getStatus(), id);
Igor Murashkincba2c162013-03-20 15:56:31 -07001544 }
1545 }
1546
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001547 /* Immediately signal current torch status to this listener only */
1548 {
1549 Mutex::Autolock al(mTorchStatusMutex);
1550 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001551 String16 id = String16(mTorchStatusMap.keyAt(i).string());
1552 listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001553 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001554 }
1555
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001556 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001557}
Ruben Brunkcc776712015-02-17 20:18:47 -08001558
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001559Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001560 ATRACE_CALL();
1561
Igor Murashkinbfc99152013-02-27 12:55:20 -08001562 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1563
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001564 if (listener == 0) {
1565 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001566 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001567 }
1568
Igor Murashkinbfc99152013-02-27 12:55:20 -08001569 Mutex::Autolock lock(mServiceLock);
1570
Ruben Brunkcc776712015-02-17 20:18:47 -08001571 {
1572 Mutex::Autolock lock(mStatusListenerLock);
1573 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1574 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1575 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001576 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001577 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001578 }
1579 }
1580
1581 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1582 __FUNCTION__, listener.get());
1583
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001584 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001585}
1586
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001587Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001588
1589 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001590 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1591
1592 if (parameters == NULL) {
1593 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001594 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001595 }
1596
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001597 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001598
1599 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001600 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001601 // Error logged by caller
1602 return ret;
1603 }
1604
1605 String8 shimParamsString8 = shimParams.flatten();
1606 String16 shimParamsString16 = String16(shimParamsString8);
1607
1608 *parameters = shimParamsString16;
1609
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001610 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001611}
1612
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001613Status CameraService::supportsCameraApi(int cameraId, int apiVersion, bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001614 ATRACE_CALL();
1615
Igor Murashkin65d14b92014-06-17 12:03:20 -07001616 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1617
1618 switch (apiVersion) {
1619 case API_VERSION_1:
1620 case API_VERSION_2:
1621 break;
1622 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001623 String8 msg = String8::format("Unknown API version %d", apiVersion);
1624 ALOGE("%s: %s", __FUNCTION__, msg.string());
1625 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001626 }
1627
1628 int facing = -1;
1629 int deviceVersion = getDeviceVersion(cameraId, &facing);
1630
1631 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001632 case CAMERA_DEVICE_API_VERSION_1_0:
1633 case CAMERA_DEVICE_API_VERSION_3_0:
1634 case CAMERA_DEVICE_API_VERSION_3_1:
1635 if (apiVersion == API_VERSION_2) {
1636 ALOGV("%s: Camera id %d uses HAL version %d <3.2, doesn't support api2 without shim",
1637 __FUNCTION__, cameraId, deviceVersion);
1638 *isSupported = false;
1639 } else { // if (apiVersion == API_VERSION_1) {
1640 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported",
1641 __FUNCTION__, cameraId);
1642 *isSupported = true;
1643 }
1644 break;
1645 case CAMERA_DEVICE_API_VERSION_3_2:
1646 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001647 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001648 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
Igor Murashkin65d14b92014-06-17 12:03:20 -07001649 __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001650 *isSupported = true;
1651 break;
1652 case -1: {
1653 String8 msg = String8::format("Unknown camera ID %d", cameraId);
1654 ALOGE("%s: %s", __FUNCTION__, msg.string());
1655 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001656 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001657 default: {
1658 String8 msg = String8::format("Unknown device version %d for device %d",
1659 deviceVersion, cameraId);
1660 ALOGE("%s: %s", __FUNCTION__, msg.string());
1661 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1662 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001663 }
1664
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001665 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001666}
1667
Ruben Brunkcc776712015-02-17 20:18:47 -08001668void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001669 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001670 for (auto& i : mActiveClientManager.getAll()) {
1671 auto clientSp = i->getValue();
1672 if (clientSp.get() == client) {
1673 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001674 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001675 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001676}
1677
Ruben Brunkcc776712015-02-17 20:18:47 -08001678bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1679 const int callingPid = getCallingPid();
1680 const int servicePid = getpid();
1681 bool ret = false;
1682 {
1683 // Acquire mServiceLock and prevent other clients from connecting
1684 std::unique_ptr<AutoConditionLock> lock =
1685 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001686
Igor Murashkin634a5152013-02-20 17:15:11 -08001687
Ruben Brunkcc776712015-02-17 20:18:47 -08001688 std::vector<sp<BasicClient>> evicted;
1689 for (auto& i : mActiveClientManager.getAll()) {
1690 auto clientSp = i->getValue();
1691 if (clientSp.get() == nullptr) {
1692 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1693 mActiveClientManager.remove(i);
1694 continue;
1695 }
1696 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1697 callingPid == clientSp->getClientPid())) {
1698 mActiveClientManager.remove(i);
1699 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001700
Ruben Brunkcc776712015-02-17 20:18:47 -08001701 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001702 clientSp->notifyError(
1703 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001704 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001705 }
1706 }
1707
Ruben Brunkcc776712015-02-17 20:18:47 -08001708 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1709 // other clients from connecting in mServiceLockWrapper if held
1710 mServiceLock.unlock();
1711
Ruben Brunk36597b22015-03-20 22:15:57 -07001712 // Do not clear caller identity, remote caller should be client proccess
1713
Ruben Brunkcc776712015-02-17 20:18:47 -08001714 for (auto& i : evicted) {
1715 if (i.get() != nullptr) {
1716 i->disconnect();
1717 ret = true;
1718 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001719 }
1720
Ruben Brunkcc776712015-02-17 20:18:47 -08001721 // Reacquire mServiceLock
1722 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001723
Ruben Brunkcc776712015-02-17 20:18:47 -08001724 } // lock is destroyed, allow further connect calls
1725
1726 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001727}
1728
Igor Murashkinecf17e82012-10-02 16:05:11 -07001729
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001730/**
1731 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001732 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001733 */
1734int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001735 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1736 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1737 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1738 // Verify the device version is in the supported range
1739 switch (info.device_version) {
1740 case CAMERA_DEVICE_API_VERSION_1_0:
1741 case CAMERA_DEVICE_API_VERSION_3_0:
1742 case CAMERA_DEVICE_API_VERSION_3_1:
1743 case CAMERA_DEVICE_API_VERSION_3_2:
1744 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001745 case CAMERA_DEVICE_API_VERSION_3_4:
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001746 // in support
1747 break;
1748 case CAMERA_DEVICE_API_VERSION_2_0:
1749 case CAMERA_DEVICE_API_VERSION_2_1:
1750 // no longer supported
1751 default:
1752 ALOGE("%s: Device %d has HAL version %x, which is not supported",
1753 __FUNCTION__, id, info.device_version);
1754 String8 msg = String8::format(
1755 "Unsupported device HAL version %x for device %d",
1756 info.device_version, id);
1757 logServiceError(msg.string(), NO_INIT);
1758 return NO_INIT;
1759 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001760 }
1761
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001762 // Assume all devices pre-v3.3 are backward-compatible
1763 bool isBackwardCompatible = true;
1764 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
1765 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
1766 isBackwardCompatible = false;
1767 status_t res;
1768 camera_metadata_ro_entry_t caps;
1769 res = find_camera_metadata_ro_entry(
1770 info.static_camera_characteristics,
1771 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1772 &caps);
1773 if (res != 0) {
1774 ALOGW("%s: Unable to find camera capabilities for camera device %d",
1775 __FUNCTION__, id);
1776 caps.count = 0;
1777 }
1778 for (size_t i = 0; i < caps.count; i++) {
1779 if (caps.data.u8[i] ==
1780 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
1781 isBackwardCompatible = true;
1782 break;
1783 }
1784 }
1785 }
1786
1787 if (!isBackwardCompatible) {
1788 mNumberOfNormalCameras--;
1789 *latestStrangeCameraId = id;
1790 } else {
1791 if (id > *latestStrangeCameraId) {
1792 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
1793 "This is not allowed due backward-compatibility requirements",
1794 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001795 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001796 mNumberOfCameras = 0;
1797 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001798 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001799 }
1800 }
1801 return OK;
1802}
1803
Ruben Brunkcc776712015-02-17 20:18:47 -08001804std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1805 const String8& cameraId) const {
1806 std::shared_ptr<CameraState> state;
1807 {
1808 Mutex::Autolock lock(mCameraStatesLock);
1809 auto iter = mCameraStates.find(cameraId);
1810 if (iter != mCameraStates.end()) {
1811 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001812 }
1813 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001814 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001815}
1816
Ruben Brunkcc776712015-02-17 20:18:47 -08001817sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1818 // Remove from active clients list
1819 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1820 if (clientDescriptorPtr == nullptr) {
1821 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1822 cameraId.string());
1823 return sp<BasicClient>{nullptr};
1824 }
1825
1826 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001827}
1828
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001829void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001830 // Acquire mServiceLock and prevent other clients from connecting
1831 std::unique_ptr<AutoConditionLock> lock =
1832 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1833
Ruben Brunk6267b532015-04-30 17:44:07 -07001834 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001835 for (size_t i = 0; i < newUserIds.size(); i++) {
1836 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001837 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001838 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07001839 return;
1840 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001841 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001842 }
1843
Ruben Brunka8ca9152015-04-07 14:23:40 -07001844
Ruben Brunk6267b532015-04-30 17:44:07 -07001845 if (newAllowedUsers == mAllowedUsers) {
1846 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1847 return;
1848 }
1849
1850 logUserSwitch(mAllowedUsers, newAllowedUsers);
1851
1852 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001853
1854 // Current user has switched, evict all current clients.
1855 std::vector<sp<BasicClient>> evicted;
1856 for (auto& i : mActiveClientManager.getAll()) {
1857 auto clientSp = i->getValue();
1858
1859 if (clientSp.get() == nullptr) {
1860 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1861 continue;
1862 }
1863
Ruben Brunk6267b532015-04-30 17:44:07 -07001864 // Don't evict clients that are still allowed.
1865 uid_t clientUid = clientSp->getClientUid();
1866 userid_t clientUserId = multiuser_get_user_id(clientUid);
1867 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1868 continue;
1869 }
1870
Ruben Brunk36597b22015-03-20 22:15:57 -07001871 evicted.push_back(clientSp);
1872
1873 String8 curTime = getFormattedCurrentTime();
1874
1875 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1876 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001877
Ruben Brunk36597b22015-03-20 22:15:57 -07001878 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001879 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07001880 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07001881 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1882 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001883
1884 }
1885
1886 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1887 // blocking other clients from connecting in mServiceLockWrapper if held.
1888 mServiceLock.unlock();
1889
1890 // Clear caller identity temporarily so client disconnect PID checks work correctly
1891 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1892
1893 for (auto& i : evicted) {
1894 i->disconnect();
1895 }
1896
1897 IPCThreadState::self()->restoreCallingIdentity(token);
1898
1899 // Reacquire mServiceLock
1900 mServiceLock.lock();
1901}
Ruben Brunkcc776712015-02-17 20:18:47 -08001902
Ruben Brunka8ca9152015-04-07 14:23:40 -07001903void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001904 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001905 Mutex::Autolock l(mLogLock);
1906 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001907}
1908
Ruben Brunka8ca9152015-04-07 14:23:40 -07001909void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001910 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001911 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001912 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001913 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001914}
1915
1916void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001917 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001918 // Log the clients evicted
1919 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001920 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001921}
1922
1923void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001924 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001925 // Log the client rejected
1926 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001927 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001928}
1929
Ruben Brunk6267b532015-04-30 17:44:07 -07001930void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1931 const std::set<userid_t>& newUserIds) {
1932 String8 newUsers = toString(newUserIds);
1933 String8 oldUsers = toString(oldUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001934 // Log the new and old users
Ruben Brunk6267b532015-04-30 17:44:07 -07001935 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
1936 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001937}
1938
1939void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1940 // Log the device removal
1941 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1942}
1943
1944void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1945 // Log the device removal
1946 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1947}
1948
1949void CameraService::logClientDied(int clientPid, const char* reason) {
1950 // Log the device removal
1951 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001952}
1953
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001954void CameraService::logServiceError(const char* msg, int errorCode) {
1955 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001956 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001957}
1958
Ruben Brunk36597b22015-03-20 22:15:57 -07001959status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1960 uint32_t flags) {
1961
1962 const int pid = getCallingPid();
1963 const int selfPid = getpid();
1964
Mathias Agopian65ab4712010-07-14 17:59:35 -07001965 // Permission checks
1966 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001967 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07001968 if (pid != selfPid) {
1969 // Ensure we're being called by system_server, or similar process with
1970 // permissions to notify the camera service about system events
1971 if (!checkCallingPermission(
1972 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1973 const int uid = getCallingUid();
1974 ALOGE("Permission Denial: cannot send updates to camera service about system"
1975 " events from pid=%d, uid=%d", pid, uid);
1976 return PERMISSION_DENIED;
1977 }
1978 }
1979 break;
1980 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001981 }
1982
1983 return BnCameraService::onTransact(code, data, reply, flags);
1984}
1985
Mathias Agopian65ab4712010-07-14 17:59:35 -07001986// We share the media players for shutter and recording sound for all clients.
1987// A reference count is kept to determine when we will actually release the
1988// media players.
1989
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001990MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001992 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001993 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001994 mp->prepare();
1995 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001996 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001997 return NULL;
1998 }
1999 return mp;
2000}
2001
2002void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002003 ATRACE_CALL();
2004
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005 Mutex::Autolock lock(mSoundLock);
2006 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2007 if (mSoundRef++) return;
2008
2009 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002010 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2011 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002012}
2013
2014void CameraService::releaseSound() {
2015 Mutex::Autolock lock(mSoundLock);
2016 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2017 if (--mSoundRef) return;
2018
2019 for (int i = 0; i < NUM_SOUNDS; i++) {
2020 if (mSoundPlayer[i] != 0) {
2021 mSoundPlayer[i]->disconnect();
2022 mSoundPlayer[i].clear();
2023 }
2024 }
2025}
2026
2027void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002028 ATRACE_CALL();
2029
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030 LOG1("playSound(%d)", kind);
2031 Mutex::Autolock lock(mSoundLock);
2032 sp<MediaPlayer> player = mSoundPlayer[kind];
2033 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002034 player->seekTo(0);
2035 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002036 }
2037}
2038
2039// ----------------------------------------------------------------------------
2040
2041CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002042 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002043 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002044 int cameraId, int cameraFacing,
2045 int clientPid, uid_t clientUid,
2046 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002047 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002048 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002049 clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002050 cameraId, cameraFacing,
2051 clientPid, clientUid,
2052 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08002053{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002054 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002055 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002057 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002058
Mathias Agopian65ab4712010-07-14 17:59:35 -07002059 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002060
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002061 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002062}
2063
Mathias Agopian65ab4712010-07-14 17:59:35 -07002064// tear down the client
2065CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002066 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002067 mDestructionStarted = true;
2068
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -07002069 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002070 // unconditionally disconnect. function is idempotent
2071 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002072}
2073
Igor Murashkin634a5152013-02-20 17:15:11 -08002074CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002075 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002076 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002077 int cameraId, int cameraFacing,
2078 int clientPid, uid_t clientUid,
2079 int servicePid):
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002080 mClientPackageName(clientPackageName), mDisconnected(false)
Igor Murashkin634a5152013-02-20 17:15:11 -08002081{
2082 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002083 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -08002084 mCameraId = cameraId;
2085 mCameraFacing = cameraFacing;
2086 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002087 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -08002088 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002089 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002090 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002091
2092 // In some cases the calling code has no access to the package it runs under.
2093 // For example, NDK camera API.
2094 // In this case we will get the packages for the calling UID and pick the first one
2095 // for attributing the app op. This will work correctly for runtime permissions
2096 // as for legacy apps we will toggle the app op for all packages in the UID.
2097 // The caveat is that the operation may be attributed to the wrong package and
2098 // stats based on app ops may be slightly off.
2099 if (mClientPackageName.size() <= 0) {
2100 sp<IServiceManager> sm = defaultServiceManager();
2101 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2102 if (binder == 0) {
2103 ALOGE("Cannot get permission service");
2104 // Leave mClientPackageName unchanged (empty) and the further interaction
2105 // with camera will fail in BasicClient::startCameraOps
2106 return;
2107 }
2108
2109 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2110 Vector<String16> packages;
2111
2112 permCtrl->getPackagesForUid(mClientUid, packages);
2113
2114 if (packages.isEmpty()) {
2115 ALOGE("No packages for calling UID");
2116 // Leave mClientPackageName unchanged (empty) and the further interaction
2117 // with camera will fail in BasicClient::startCameraOps
2118 return;
2119 }
2120 mClientPackageName = packages[0];
2121 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002122}
2123
2124CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002125 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002126 mDestructionStarted = true;
2127}
2128
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002129binder::Status CameraService::BasicClient::disconnect() {
2130 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002131 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002132 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002133 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002134 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002135
2136 mCameraService->removeByClient(this);
2137 mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002138 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002139
2140 sp<IBinder> remote = getRemote();
2141 if (remote != nullptr) {
2142 remote->unlinkToDeath(mCameraService);
2143 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002144
2145 finishCameraOps();
Chien-Yu Chene4fe21b2016-08-04 12:42:40 -07002146 // Notify flashlight that a camera device is closed.
2147 mCameraService->mFlashlight->deviceClosed(String8::format("%d", mCameraId));
Ruben Brunkcc776712015-02-17 20:18:47 -08002148 ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid);
2149
Igor Murashkincba2c162013-03-20 15:56:31 -07002150 // client shouldn't be able to call into us anymore
2151 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002152
2153 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002154}
2155
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002156status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2157 // No dumping of clients directly over Binder,
2158 // must go through CameraService::dump
2159 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2160 IPCThreadState::self()->getCallingUid(), NULL, 0);
2161 return OK;
2162}
2163
Ruben Brunkcc776712015-02-17 20:18:47 -08002164String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002165 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002166}
2167
2168
2169int CameraService::BasicClient::getClientPid() const {
2170 return mClientPid;
2171}
2172
Ruben Brunk6267b532015-04-30 17:44:07 -07002173uid_t CameraService::BasicClient::getClientUid() const {
2174 return mClientUid;
2175}
2176
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002177bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2178 // Defaults to API2.
2179 return level == API_2;
2180}
2181
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002182status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002183 ATRACE_CALL();
2184
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002185 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002186 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002187 mOpsCallback = new OpsCallback(this);
2188
Igor Murashkine6800ce2013-03-04 17:25:57 -08002189 {
2190 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002191 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002192 }
2193
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002194 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002195 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002196 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002197 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002198
Svetoslav28e8ef72015-05-11 19:21:31 -07002199 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002200 ALOGI("Camera %d: Access for \"%s\" has been revoked",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002201 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002202 return PERMISSION_DENIED;
2203 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002204
Svetoslav28e8ef72015-05-11 19:21:31 -07002205 if (res == AppOpsManager::MODE_IGNORED) {
2206 ALOGI("Camera %d: Access for \"%s\" has been restricted",
2207 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002208 // Return the same error as for device policy manager rejection
2209 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002210 }
2211
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002212 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002213
2214 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
2215 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
Ruben Brunkcc776712015-02-17 20:18:47 -08002216 String8::format("%d", mCameraId));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002217
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002218 // Transition device state to OPEN
2219 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2220 String8::format("%d", mCameraId));
2221
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002222 return OK;
2223}
2224
2225status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002226 ATRACE_CALL();
2227
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002228 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002229 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002230 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002231 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002232 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002233 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002234
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002235 std::initializer_list<int32_t> rejected = {ICameraServiceListener::STATUS_NOT_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002236 ICameraServiceListener::STATUS_ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002237
Ruben Brunkcc776712015-02-17 20:18:47 -08002238 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002239 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002240 String8::format("%d", mCameraId), rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002241
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002242 // Transition device state to CLOSED
2243 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2244 String8::format("%d", mCameraId));
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002245 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002246 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002247 if (mOpsCallback != NULL) {
2248 mAppOpsManager.stopWatchingMode(mOpsCallback);
2249 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002250 mOpsCallback.clear();
2251
2252 return OK;
2253}
2254
2255void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002256 ATRACE_CALL();
2257
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002258 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002259 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002260
2261 if (op != AppOpsManager::OP_CAMERA) {
2262 ALOGW("Unexpected app ops notification received: %d", op);
2263 return;
2264 }
2265
2266 int32_t res;
2267 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002268 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002269 ALOGV("checkOp returns: %d, %s ", res,
2270 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2271 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2272 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2273 "UNKNOWN");
2274
2275 if (res != AppOpsManager::MODE_ALLOWED) {
2276 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
2277 myName.string());
2278 // Reset the client PID to allow server-initiated disconnect,
2279 // and to prevent further calls by client.
2280 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002281 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002282 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002283 disconnect();
2284 }
2285}
2286
Mathias Agopian65ab4712010-07-14 17:59:35 -07002287// ----------------------------------------------------------------------------
2288
Ruben Brunkcc776712015-02-17 20:18:47 -08002289// Provide client strong pointer for callbacks.
2290sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
2291 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
2292 auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId);
2293 if (clientDescriptor != nullptr) {
2294 return sp<Client>{
2295 static_cast<Client*>(clientDescriptor->getValue().get())};
2296 }
2297 return sp<Client>{nullptr};
Mathias Agopian65ab4712010-07-14 17:59:35 -07002298}
2299
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002300void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002301 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002302 (void) errorCode;
2303 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002304 if (mRemoteCallback != NULL) {
2305 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2306 } else {
2307 ALOGE("mRemoteCallback is NULL!!");
2308 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002309}
2310
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002311// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002312binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002313 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002314 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002315}
2316
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002317bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2318 return level == API_1;
2319}
2320
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002321CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2322 mClient(client) {
2323}
2324
2325void CameraService::Client::OpsCallback::opChanged(int32_t op,
2326 const String16& packageName) {
2327 sp<BasicClient> client = mClient.promote();
2328 if (client != NULL) {
2329 client->opChanged(op, packageName);
2330 }
2331}
2332
Mathias Agopian65ab4712010-07-14 17:59:35 -07002333// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002334// CameraState
2335// ----------------------------------------------------------------------------
2336
2337CameraService::CameraState::CameraState(const String8& id, int cost,
2338 const std::set<String8>& conflicting) : mId(id),
2339 mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {}
2340
2341CameraService::CameraState::~CameraState() {}
2342
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002343int32_t CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002344 Mutex::Autolock lock(mStatusLock);
2345 return mStatus;
2346}
2347
2348CameraParameters CameraService::CameraState::getShimParams() const {
2349 return mShimParams;
2350}
2351
2352void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2353 mShimParams = params;
2354}
2355
2356int CameraService::CameraState::getCost() const {
2357 return mCost;
2358}
2359
2360std::set<String8> CameraService::CameraState::getConflicting() const {
2361 return mConflicting;
2362}
2363
2364String8 CameraService::CameraState::getId() const {
2365 return mId;
2366}
2367
2368// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002369// ClientEventListener
2370// ----------------------------------------------------------------------------
2371
2372void CameraService::ClientEventListener::onClientAdded(
2373 const resource_policy::ClientDescriptor<String8,
2374 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002375 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002376 if (basicClient.get() != nullptr) {
2377 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2378 notifier.noteStartCamera(descriptor.getKey(),
2379 static_cast<int>(basicClient->getClientUid()));
2380 }
2381}
2382
2383void CameraService::ClientEventListener::onClientRemoved(
2384 const resource_policy::ClientDescriptor<String8,
2385 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002386 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002387 if (basicClient.get() != nullptr) {
2388 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2389 notifier.noteStopCamera(descriptor.getKey(),
2390 static_cast<int>(basicClient->getClientUid()));
2391 }
2392}
2393
2394
2395// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002396// CameraClientManager
2397// ----------------------------------------------------------------------------
2398
Ruben Brunk99e69712015-05-26 17:25:07 -07002399CameraService::CameraClientManager::CameraClientManager() {
2400 setListener(std::make_shared<ClientEventListener>());
2401}
2402
Ruben Brunkcc776712015-02-17 20:18:47 -08002403CameraService::CameraClientManager::~CameraClientManager() {}
2404
2405sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2406 const String8& id) const {
2407 auto descriptor = get(id);
2408 if (descriptor == nullptr) {
2409 return sp<BasicClient>{nullptr};
2410 }
2411 return descriptor->getValue();
2412}
2413
2414String8 CameraService::CameraClientManager::toString() const {
2415 auto all = getAll();
2416 String8 ret("[");
2417 bool hasAny = false;
2418 for (auto& i : all) {
2419 hasAny = true;
2420 String8 key = i->getKey();
2421 int32_t cost = i->getCost();
2422 int32_t pid = i->getOwnerId();
2423 int32_t priority = i->getPriority();
2424 auto conflicting = i->getConflicting();
2425 auto clientSp = i->getValue();
2426 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002427 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002428 if (clientSp.get() != nullptr) {
2429 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002430 uid_t clientUid = clientSp->getClientUid();
2431 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002432 }
2433 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2434 PRId32 ", ", key.string(), cost, pid, priority);
2435
Ruben Brunk6267b532015-04-30 17:44:07 -07002436 if (clientSp.get() != nullptr) {
2437 ret.appendFormat("User Id: %d, ", clientUserId);
2438 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002439 if (packageName.size() != 0) {
2440 ret.appendFormat("Client Package Name: %s", packageName.string());
2441 }
2442
2443 ret.append(", Conflicting Client Devices: {");
2444 for (auto& j : conflicting) {
2445 ret.appendFormat("%s, ", j.string());
2446 }
2447 ret.append("})");
2448 }
2449 if (hasAny) ret.append("\n");
2450 ret.append("]\n");
2451 return ret;
2452}
2453
2454CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2455 const String8& key, const sp<BasicClient>& value, int32_t cost,
2456 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2457
2458 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2459 key, value, cost, conflictingKeys, priority, ownerId);
2460}
2461
2462CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2463 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2464 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2465 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2466}
2467
2468// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002469
2470static const int kDumpLockRetries = 50;
2471static const int kDumpLockSleep = 60000;
2472
2473static bool tryLock(Mutex& mutex)
2474{
2475 bool locked = false;
2476 for (int i = 0; i < kDumpLockRetries; ++i) {
2477 if (mutex.tryLock() == NO_ERROR) {
2478 locked = true;
2479 break;
2480 }
2481 usleep(kDumpLockSleep);
2482 }
2483 return locked;
2484}
2485
2486status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002487 ATRACE_CALL();
2488
Ruben Brunka8ca9152015-04-07 14:23:40 -07002489 String8 result("Dump of the Camera Service:\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
dcashmanfefa6142015-09-09 13:43:01 -07002491 result = result.format("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07002492 "can't dump CameraService from pid=%d, uid=%d\n",
2493 getCallingPid(),
2494 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002495 write(fd, result.string(), result.size());
2496 } else {
2497 bool locked = tryLock(mServiceLock);
2498 // failed to lock - CameraService is probably deadlocked
2499 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002500 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002501 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002502 }
2503
2504 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002505 if (!mModule) {
2506 result = String8::format("No camera module available!\n");
2507 write(fd, result.string(), result.size());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002508
2509 // Dump event log for error information
2510 dumpEventLog(fd);
2511
Kalle Lampila6ec3a152013-04-30 15:27:19 +03002512 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002513 return NO_ERROR;
2514 }
2515
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002516 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2517 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2518 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2519 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
Ruben Brunkcc776712015-02-17 20:18:47 -08002520 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
Eino-Ville Talvala49c97052016-01-12 14:29:40 -08002521 result.appendFormat("Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Ruben Brunkcc776712015-02-17 20:18:47 -08002522 String8 activeClientString = mActiveClientManager.toString();
2523 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
Ruben Brunk6267b532015-04-30 17:44:07 -07002524 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002525
Ruben Brunkf81648e2014-04-17 16:14:57 -07002526 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2527 if (desc == NULL) {
2528 result.appendFormat("Vendor tags left unimplemented.\n");
2529 } else {
2530 result.appendFormat("Vendor tag definitions:\n");
2531 }
2532
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002533 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07002534
2535 if (desc != NULL) {
2536 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
2537 }
2538
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002539 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002540
2541 bool stateLocked = tryLock(mCameraStatesLock);
2542 if (!stateLocked) {
2543 result = String8::format("CameraStates in use, may be deadlocked\n");
2544 write(fd, result.string(), result.size());
2545 }
2546
2547 for (auto& state : mCameraStates) {
2548 String8 cameraId = state.first;
2549 result = String8::format("Camera %s information:\n", cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002550 camera_info info;
2551
Ruben Brunkcc776712015-02-17 20:18:47 -08002552 // TODO: Change getCameraInfo + HAL to use String cameraIds
2553 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002554 if (rc != OK) {
2555 result.appendFormat(" Error reading static information!\n");
2556 write(fd, result.string(), result.size());
2557 } else {
2558 result.appendFormat(" Facing: %s\n",
Yin-Chia Yeh13cd59a2016-11-29 12:13:15 -08002559 info.facing == CAMERA_FACING_BACK ? "BACK" :
2560 info.facing == CAMERA_FACING_FRONT ? "FRONT" : "EXTERNAL");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002561 result.appendFormat(" Orientation: %d\n", info.orientation);
2562 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002563 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002564 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2565 } else {
2566 deviceVersion = info.device_version;
2567 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002568
2569 auto conflicting = state.second->getConflicting();
2570 result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
2571 result.appendFormat(" Conflicting Devices:");
2572 for (auto& id : conflicting) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002573 result.appendFormat(" %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002574 }
2575 if (conflicting.size() == 0) {
2576 result.appendFormat(" NONE");
2577 }
2578 result.appendFormat("\n");
2579
2580 result.appendFormat(" Device version: %#x\n", deviceVersion);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002581 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002582 result.appendFormat(" Device static metadata:\n");
2583 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07002584 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07002585 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002586 } else {
2587 write(fd, result.string(), result.size());
2588 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002589
2590 CameraParameters p = state.second->getShimParams();
2591 if (!p.isEmpty()) {
2592 result = String8::format(" Camera1 API shim is using parameters:\n ");
2593 write(fd, result.string(), result.size());
2594 p.dump(fd, args);
2595 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002596 }
2597
Ruben Brunkcc776712015-02-17 20:18:47 -08002598 auto clientDescriptor = mActiveClientManager.get(cameraId);
2599 if (clientDescriptor == nullptr) {
2600 result = String8::format(" Device %s is closed, no client instance\n",
2601 cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002602 write(fd, result.string(), result.size());
2603 continue;
2604 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002605 hasClient = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002606 result = String8::format(" Device %s is open. Client instance dump:\n\n",
2607 cameraId.string());
2608 result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
2609 result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
2610
2611 auto client = clientDescriptor->getValue();
2612 result.appendFormat("Client package: %s\n",
2613 String8(client->getPackageName()).string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002614 write(fd, result.string(), result.size());
Ruben Brunkcc776712015-02-17 20:18:47 -08002615
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002616 client->dumpClient(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002617 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002618
2619 if (stateLocked) mCameraStatesLock.unlock();
2620
Mathias Agopian65ab4712010-07-14 17:59:35 -07002621 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002622 result = String8::format("\nNo active camera clients yet.\n");
2623 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002624 }
2625
2626 if (locked) mServiceLock.unlock();
2627
Igor Murashkinff3e31d2013-10-23 16:40:06 -07002628 // Dump camera traces if there were any
2629 write(fd, "\n", 1);
2630 camera3::CameraTraces::dump(fd, args);
2631
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002632 // Process dump arguments, if any
Mathias Agopian65ab4712010-07-14 17:59:35 -07002633 int n = args.size();
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002634 String16 verboseOption("-v");
2635 String16 unreachableOption("--unreachable");
2636 for (int i = 0; i < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002637 if (args[i] == verboseOption) {
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002638 // change logging level
2639 if (i + 1 >= n) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002640 String8 levelStr(args[i+1]);
2641 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002642 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002643 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002644 write(fd, result.string(), result.size());
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002645 } else if (args[i] == unreachableOption) {
2646 // Dump memory analysis
2647 // TODO - should limit be an argument parameter?
2648 UnreachableMemoryInfo info;
2649 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2650 if (!success) {
2651 dprintf(fd, "\nUnable to dump unreachable memory. "
2652 "Try disabling SELinux enforcement.\n");
2653 } else {
2654 dprintf(fd, "\nDumping unreachable memory:\n");
2655 std::string s = info.ToString(/*log_contents*/ true);
2656 write(fd, s.c_str(), s.size());
2657 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002658 }
2659 }
2660 }
2661 return NO_ERROR;
2662}
2663
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002664void CameraService::dumpEventLog(int fd) {
2665 String8 result = String8("\nPrior client events (most recent at top):\n");
2666
2667 Mutex::Autolock l(mLogLock);
2668 for (const auto& msg : mEventLog) {
2669 result.appendFormat(" %s\n", msg.string());
2670 }
2671
2672 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
2673 result.append(" ...\n");
2674 } else if (mEventLog.size() == 0) {
2675 result.append(" [no events yet]\n");
2676 }
2677 result.append("\n");
2678
2679 write(fd, result.string(), result.size());
2680}
2681
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002682void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002683 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002684 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2685 if (mTorchClientMap[i] == who) {
2686 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002687 String8 cameraId = mTorchClientMap.keyAt(i);
2688 status_t res = mFlashlight->setTorchMode(cameraId, false);
2689 if (res) {
2690 ALOGE("%s: torch client died but couldn't turn off torch: "
2691 "%s (%d)", __FUNCTION__, strerror(-res), res);
2692 return;
2693 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002694 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002695 break;
2696 }
2697 }
2698}
2699
Ruben Brunkcc776712015-02-17 20:18:47 -08002700/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002701
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002702 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002703 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2704 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002705 */
2706
Ruben Brunka8ca9152015-04-07 14:23:40 -07002707 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2708
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002709 // check torch client
2710 handleTorchClientBinderDied(who);
2711
2712 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002713 if(!evictClientIdByRemote(who)) {
2714 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002715 return;
2716 }
2717
Ruben Brunkcc776712015-02-17 20:18:47 -08002718 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2719 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002720}
2721
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002722void CameraService::updateStatus(int32_t status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002723 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002724}
2725
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002726void CameraService::updateStatus(int32_t status, const String8& cameraId,
2727 std::initializer_list<int32_t> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002728 // Do not lock mServiceLock here or can get into a deadlock from
2729 // connect() -> disconnect -> updateStatus
2730
2731 auto state = getCameraState(cameraId);
2732
2733 if (state == nullptr) {
2734 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2735 cameraId.string());
2736 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002737 }
2738
Ruben Brunkcc776712015-02-17 20:18:47 -08002739 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2740 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2741 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002742 (const String8& cameraId, int32_t status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002743
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002744 if (status != ICameraServiceListener::STATUS_ENUMERATING) {
2745 // Update torch status if it has a flash unit.
2746 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002747 int32_t torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002748 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2749 NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002750 int32_t newTorchStatus =
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002751 status == ICameraServiceListener::STATUS_PRESENT ?
2752 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF :
2753 ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
2754 if (torchStatus != newTorchStatus) {
2755 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2756 }
2757 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002758 }
2759
2760 Mutex::Autolock lock(mStatusListenerLock);
2761
2762 for (auto& listener : mListenerList) {
2763 // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
2764 int id = cameraIdToInt(cameraId);
2765 if (id != -1) listener->onStatusChanged(status, id);
2766 }
2767 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002768}
2769
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002770void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
2771 const String8& cameraId) {
2772 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2773 if (proxyBinder == nullptr) return;
2774 String16 id(cameraId);
2775 proxyBinder->notifyCameraState(id, newState);
2776}
2777
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002778status_t CameraService::getTorchStatusLocked(
2779 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002780 int32_t *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002781 if (!status) {
2782 return BAD_VALUE;
2783 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002784 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2785 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002786 // invalid camera ID or the camera doesn't have a flash unit
2787 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002788 }
2789
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002790 *status = mTorchStatusMap.valueAt(index);
2791 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002792}
2793
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002794status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002795 int32_t status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002796 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2797 if (index == NAME_NOT_FOUND) {
2798 return BAD_VALUE;
2799 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002800 int32_t& item =
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002801 mTorchStatusMap.editValueAt(index);
2802 item = status;
2803
2804 return OK;
2805}
2806
Mathias Agopian65ab4712010-07-14 17:59:35 -07002807}; // namespace android