blob: 007629d2d89b4e47cf0c5fd672f090aad041c88f [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
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080034#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <binder/IPCThreadState.h>
36#include <binder/IServiceManager.h>
37#include <binder/MemoryBase.h>
38#include <binder/MemoryHeapBase.h>
Ruben Brunkcc776712015-02-17 20:18:47 -080039#include <binder/ProcessInfoService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070040#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070041#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080042#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070043#include <hardware/hardware.h>
44#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080045#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070046#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070047#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070048#include <utils/Errors.h>
49#include <utils/Log.h>
50#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080051#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080052#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080053#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070054#include <system/camera_metadata.h>
55#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
57#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070058#include "api1/CameraClient.h"
59#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070060#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070061#include "utils/CameraTraces.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070062
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080063namespace {
64 const char* kPermissionServiceName = "permission";
65}; // namespace anonymous
66
Mathias Agopian65ab4712010-07-14 17:59:35 -070067namespace android {
68
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080069using binder::Status;
70using namespace hardware;
71
Mathias Agopian65ab4712010-07-14 17:59:35 -070072// ----------------------------------------------------------------------------
73// Logging support -- this is for debugging only
74// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070075volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070076
Steve Blockb8a80522011-12-20 16:23:08 +000077#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
78#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070079
80static void setLogLevel(int level) {
81 android_atomic_write(level, &gLogLevel);
82}
83
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080084// Convenience methods for constructing binder::Status objects for error returns
85
86#define STATUS_ERROR(errorCode, errorString) \
87 binder::Status::fromServiceSpecificError(errorCode, \
88 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
89
90#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
91 binder::Status::fromServiceSpecificError(errorCode, \
92 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
93 __VA_ARGS__))
94
Mathias Agopian65ab4712010-07-14 17:59:35 -070095// ----------------------------------------------------------------------------
96
Igor Murashkincba2c162013-03-20 15:56:31 -070097extern "C" {
98static void camera_device_status_change(
99 const struct camera_module_callbacks* callbacks,
100 int camera_id,
101 int new_status) {
102 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -0800103 static_cast<const CameraService*>(callbacks));
Igor Murashkincba2c162013-03-20 15:56:31 -0700104
Bin Chene16d1162016-02-22 18:19:58 +1100105 cs->onDeviceStatusChanged(camera_id,
Ruben Brunkcc776712015-02-17 20:18:47 -0800106 static_cast<camera_device_status_t>(new_status));
Igor Murashkincba2c162013-03-20 15:56:31 -0700107}
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800108
109static void torch_mode_status_change(
110 const struct camera_module_callbacks* callbacks,
111 const char* camera_id,
112 int new_status) {
113 if (!callbacks || !camera_id) {
114 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
115 callbacks, camera_id);
116 }
117 sp<CameraService> cs = const_cast<CameraService*>(
118 static_cast<const CameraService*>(callbacks));
119
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800120 int32_t status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800121 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800122 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800123 status = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
124 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800125 case TORCH_MODE_STATUS_AVAILABLE_OFF:
126 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
127 break;
128 case TORCH_MODE_STATUS_AVAILABLE_ON:
129 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800130 break;
131 default:
132 ALOGE("Unknown torch status %d", new_status);
133 return;
134 }
135
136 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800137 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800138 status);
139}
Igor Murashkincba2c162013-03-20 15:56:31 -0700140} // extern "C"
141
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142// ----------------------------------------------------------------------------
143
144// This is ugly and only safe if we never re-create the CameraService, but
145// should be ok for now.
146static CameraService *gCameraService;
147
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800148CameraService::CameraService() :
149 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800150 mNumberOfCameras(0), mNumberOfNormalCameras(0),
151 mSoundRef(0), mModule(nullptr) {
Steve Blockdf64d152012-01-04 20:05:49 +0000152 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700153 gCameraService = this;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800154
Igor Murashkincba2c162013-03-20 15:56:31 -0700155 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800156 this->torch_mode_status_change = android::torch_mode_status_change;
157
Ruben Brunkcc776712015-02-17 20:18:47 -0800158 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159}
160
Iliyan Malchev8951a972011-04-14 16:55:59 -0700161void CameraService::onFirstRef()
162{
Ruben Brunkcc776712015-02-17 20:18:47 -0800163 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800164
Iliyan Malchev8951a972011-04-14 16:55:59 -0700165 BnCameraService::onFirstRef();
166
Ruben Brunk99e69712015-05-26 17:25:07 -0700167 // Update battery life tracking if service is restarting
168 BatteryNotifier& notifier(BatteryNotifier::getInstance());
169 notifier.noteResetCamera();
170 notifier.noteResetFlashlight();
171
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800172 camera_module_t *rawModule;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700173 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
174 (const hw_module_t **)&rawModule);
175 if (err < 0) {
176 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
177 logServiceError("Could not load camera HAL module", err);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700178 return;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700179 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800180
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700181 mModule = new CameraModule(rawModule);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700182 err = mModule->init();
183 if (err != OK) {
184 ALOGE("Could not initialize camera HAL module: %d (%s)", err,
185 strerror(-err));
186 logServiceError("Could not initialize camera HAL module", err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800187
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700188 delete mModule;
189 mModule = nullptr;
190 return;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700191 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700192 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700193
194 mNumberOfCameras = mModule->getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700195 mNumberOfNormalCameras = mNumberOfCameras;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700196
Yin-Chia Yehd4a653a2015-10-14 11:05:44 -0700197 // Setup vendor tags before we call get_camera_info the first time
198 // because HAL might need to setup static vendor keys in get_camera_info
199 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
200 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
201 setUpVendorTags();
202 }
203
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700204 mFlashlight = new CameraFlashlight(*mModule, *this);
205 status_t res = mFlashlight->findFlashUnits();
206 if (res) {
207 // impossible because we haven't open any camera devices.
208 ALOGE("Failed to find flash units.");
209 }
210
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700211 int latestStrangeCameraId = INT_MAX;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700212 for (int i = 0; i < mNumberOfCameras; i++) {
213 String8 cameraId = String8::format("%d", i);
214
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700215 // Get camera info
216
217 struct camera_info info;
218 bool haveInfo = true;
219 status_t rc = mModule->getCameraInfo(i, &info);
220 if (rc != NO_ERROR) {
221 ALOGE("%s: Received error loading camera info for device %d, cost and"
222 " conflicting devices fields set to defaults for this device.",
223 __FUNCTION__, i);
224 haveInfo = false;
225 }
226
227 // Check for backwards-compatibility support
228 if (haveInfo) {
229 if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) {
230 delete mModule;
231 mModule = nullptr;
232 return;
233 }
234 }
235
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700236 // Defaults to use for cost and conflicting devices
237 int cost = 100;
238 char** conflicting_devices = nullptr;
239 size_t conflicting_devices_length = 0;
240
241 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700242 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
243 cost = info.resource_cost;
244 conflicting_devices = info.conflicting_devices;
245 conflicting_devices_length = info.conflicting_devices_length;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700246 }
247
248 std::set<String8> conflicting;
249 for (size_t i = 0; i < conflicting_devices_length; i++) {
250 conflicting.emplace(String8(conflicting_devices[i]));
251 }
252
253 // Initialize state for each camera device
254 {
255 Mutex::Autolock lock(mCameraStatesLock);
256 mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost,
257 conflicting));
258 }
259
260 if (mFlashlight->hasFlashUnit(cameraId)) {
261 mTorchStatusMap.add(cameraId,
262 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF);
263 }
264 }
265
266 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
267 mModule->setCallbacks(this);
268 }
269
Ruben Brunk2823ce02015-05-19 17:25:13 -0700270 CameraService::pingCameraServiceProxy();
271}
272
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700273sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800274 sp<ICameraServiceProxy> proxyBinder = nullptr;
275#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700276 sp<IServiceManager> sm = defaultServiceManager();
277 sp<IBinder> binder = sm->getService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800278 if (binder != nullptr) {
279 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700280 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800281#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700282 return proxyBinder;
283}
284
285void CameraService::pingCameraServiceProxy() {
286 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
287 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700288 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700289}
290
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800292 if (mModule) {
293 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800294 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800295 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800296 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Ruben Brunkcc776712015-02-17 20:18:47 -0800297 gCameraService = nullptr;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298}
299
Bin Chene16d1162016-02-22 18:19:58 +1100300void CameraService::onDeviceStatusChanged(int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800301 camera_device_status_t newStatus) {
302 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
Igor Murashkincba2c162013-03-20 15:56:31 -0700303 cameraId, newStatus);
304
Ruben Brunkcc776712015-02-17 20:18:47 -0800305 String8 id = String8::format("%d", cameraId);
306 std::shared_ptr<CameraState> state = getCameraState(id);
307
308 if (state == nullptr) {
Igor Murashkincba2c162013-03-20 15:56:31 -0700309 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
310 return;
311 }
312
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800313 int32_t oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800314
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800315 if (oldStatus == static_cast<int32_t>(newStatus)) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800316 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700317 return;
318 }
319
Igor Murashkincba2c162013-03-20 15:56:31 -0700320 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700321 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
322 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800323 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700324 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800325 // Don't do this in updateStatus to avoid deadlock over mServiceLock
326 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700327
Ruben Brunkcc776712015-02-17 20:18:47 -0800328 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
329 // to this device until the status changes
330 updateStatus(ICameraServiceListener::STATUS_NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700331
Ruben Brunkcc776712015-02-17 20:18:47 -0800332 // Remove cached shim parameters
333 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700334
Ruben Brunkcc776712015-02-17 20:18:47 -0800335 // Remove the client from the list of active clients
336 clientToDisconnect = removeClientLocked(id);
337
338 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800339 clientToDisconnect->notifyError(
340 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800341 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700342 }
343
Ruben Brunkcc776712015-02-17 20:18:47 -0800344 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
345 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700346
Ruben Brunkcc776712015-02-17 20:18:47 -0800347 // Disconnect client
348 if (clientToDisconnect.get() != nullptr) {
349 // Ensure not in binder RPC so client disconnect PID checks work correctly
350 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
351 "onDeviceStatusChanged must be called from the camera service process!");
352 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700353 }
354
Ruben Brunkcc776712015-02-17 20:18:47 -0800355 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800356 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700357 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
358 newStatus));
359 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800360 updateStatus(static_cast<int32_t>(newStatus), id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700361 }
362
Igor Murashkincba2c162013-03-20 15:56:31 -0700363}
364
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800365void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800366 int32_t newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800367 Mutex::Autolock al(mTorchStatusMutex);
368 onTorchStatusChangedLocked(cameraId, newStatus);
369}
370
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800371void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800372 int32_t newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800373 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
374 __FUNCTION__, cameraId.string(), newStatus);
375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800376 int32_t status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800377 status_t res = getTorchStatusLocked(cameraId, &status);
378 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700379 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
380 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800381 return;
382 }
383 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800384 return;
385 }
386
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800387 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800388 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800389 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
390 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800391 return;
392 }
393
Ruben Brunkcc776712015-02-17 20:18:47 -0800394 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700395 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700396 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700397 auto iter = mTorchUidMap.find(cameraId);
398 if (iter != mTorchUidMap.end()) {
399 int oldUid = iter->second.second;
400 int newUid = iter->second.first;
401 BatteryNotifier& notifier(BatteryNotifier::getInstance());
402 if (oldUid != newUid) {
403 // If the UID has changed, log the status and update current UID in mTorchUidMap
404 if (status == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
405 notifier.noteFlashlightOff(cameraId, oldUid);
406 }
407 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
408 notifier.noteFlashlightOn(cameraId, newUid);
409 }
410 iter->second.second = newUid;
411 } else {
412 // If the UID has not changed, log the status
413 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
414 notifier.noteFlashlightOn(cameraId, oldUid);
415 } else {
416 notifier.noteFlashlightOff(cameraId, oldUid);
417 }
418 }
419 }
420 }
421
422 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800423 Mutex::Autolock lock(mStatusListenerLock);
424 for (auto& i : mListenerList) {
425 i->onTorchStatusChanged(newStatus, String16{cameraId});
426 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800427 }
428}
429
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800430Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700431 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700432 switch (type) {
433 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800434 *numCameras = mNumberOfNormalCameras;
435 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700436 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800437 *numCameras = mNumberOfCameras;
438 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700439 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800440 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700441 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800442 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
443 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700444 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800445 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700446}
447
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800448Status CameraService::getCameraInfo(int cameraId,
449 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700450 ATRACE_CALL();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700451 if (!mModule) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800452 return STATUS_ERROR(ERROR_DISCONNECTED,
453 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700454 }
455
Mathias Agopian65ab4712010-07-14 17:59:35 -0700456 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800457 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
458 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700459 }
460
Iliyan Malchev8951a972011-04-14 16:55:59 -0700461 struct camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800462 Status rc = filterGetInfoErrorCode(
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800463 mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800464
465 if (rc.isOk()) {
466 cameraInfo->facing = info.facing;
467 cameraInfo->orientation = info.orientation;
468 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700469 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470}
471
Ruben Brunkcc776712015-02-17 20:18:47 -0800472int CameraService::cameraIdToInt(const String8& cameraId) {
473 errno = 0;
474 size_t pos = 0;
475 int ret = stoi(std::string{cameraId.string()}, &pos);
476 if (errno != 0 || pos != cameraId.size()) {
477 return -1;
478 }
479 return ret;
480}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700481
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800482Status CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700483 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800484
485 Status ret = Status::ok();
486
Ruben Brunkb2119af2014-05-09 19:57:56 -0700487 struct CameraInfo info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800488 if (!(ret = getCameraInfo(cameraId, &info)).isOk()) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700489 return ret;
490 }
491
492 CameraMetadata shimInfo;
493 int32_t orientation = static_cast<int32_t>(info.orientation);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800494 status_t rc;
495 if ((rc = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) {
496 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
497 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700498 }
499
500 uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ?
501 ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800502 if ((rc = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) {
503 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
504 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700505 }
506
Igor Murashkin65d14b92014-06-17 12:03:20 -0700507 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800508 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -0700509 // Error logged by callee
510 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700511 }
512
513 Vector<Size> sizes;
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700514 Vector<Size> jpegSizes;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700515 Vector<int32_t> formats;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700516 {
517 shimParams.getSupportedPreviewSizes(/*out*/sizes);
518 shimParams.getSupportedPreviewFormats(/*out*/formats);
519 shimParams.getSupportedPictureSizes(/*out*/jpegSizes);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700520 }
521
522 // Always include IMPLEMENTATION_DEFINED
523 formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
524
525 const size_t INTS_PER_CONFIG = 4;
526
527 // Build available stream configurations metadata
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700528 size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG;
529
530 Vector<int32_t> streamConfigs;
531 streamConfigs.setCapacity(streamConfigSize);
532
Ruben Brunkb2119af2014-05-09 19:57:56 -0700533 for (size_t i = 0; i < formats.size(); ++i) {
534 for (size_t j = 0; j < sizes.size(); ++j) {
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700535 streamConfigs.add(formats[i]);
536 streamConfigs.add(sizes[j].width);
537 streamConfigs.add(sizes[j].height);
538 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700539 }
540 }
541
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700542 for (size_t i = 0; i < jpegSizes.size(); ++i) {
543 streamConfigs.add(HAL_PIXEL_FORMAT_BLOB);
544 streamConfigs.add(jpegSizes[i].width);
545 streamConfigs.add(jpegSizes[i].height);
546 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
547 }
548
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800549 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700550 streamConfigs.array(), streamConfigSize)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
552 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700553 }
554
555 int64_t fakeMinFrames[0];
556 // TODO: Fixme, don't fake min frame durations.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800557 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
Ruben Brunkb2119af2014-05-09 19:57:56 -0700558 fakeMinFrames, 0)) != 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 fakeStalls[0];
564 // TODO: Fixme, don't fake stall durations.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800565 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
Ruben Brunkb2119af2014-05-09 19:57:56 -0700566 fakeStalls, 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 *cameraInfo = shimInfo;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800572 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700573}
574
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800575Status CameraService::getCameraCharacteristics(int cameraId,
Zhijun He2b59be82013-09-25 10:14:30 -0700576 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700577 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700578 if (!cameraInfo) {
579 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800580 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700581 }
582
583 if (!mModule) {
584 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800585 return STATUS_ERROR(ERROR_DISCONNECTED,
586 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700587 }
588
Zhijun He2b59be82013-09-25 10:14:30 -0700589 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
590 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800591 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
592 "Invalid camera id: %d", cameraId);
Zhijun He2b59be82013-09-25 10:14:30 -0700593 }
594
595 int facing;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800596 Status ret;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800597 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0 ||
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800598 getDeviceVersion(cameraId, &facing) < CAMERA_DEVICE_API_VERSION_3_0) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700599 /**
600 * Backwards compatibility mode for old HALs:
601 * - Convert CameraInfo into static CameraMetadata properties.
602 * - Retrieve cached CameraParameters for this camera. If none exist,
603 * attempt to open CameraClient and retrieve the CameraParameters.
604 * - Convert cached CameraParameters into static CameraMetadata
605 * properties.
606 */
607 ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__);
Zhijun He2b59be82013-09-25 10:14:30 -0700608
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800609 ret = generateShimMetadata(cameraId, cameraInfo);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700610 } else {
611 /**
612 * Normal HAL 2.1+ codepath.
613 */
614 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800615 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800616 if (ret.isOk()) {
617 *cameraInfo = info.static_camera_characteristics;
618 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700619 }
Zhijun He2b59be82013-09-25 10:14:30 -0700620
621 return ret;
622}
623
Ruben Brunkcc776712015-02-17 20:18:47 -0800624int CameraService::getCallingPid() {
625 return IPCThreadState::self()->getCallingPid();
626}
627
628int CameraService::getCallingUid() {
629 return IPCThreadState::self()->getCallingUid();
630}
631
632String8 CameraService::getFormattedCurrentTime() {
633 time_t now = time(nullptr);
634 char formattedTime[64];
635 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
636 return String8(formattedTime);
637}
638
639int CameraService::getCameraPriorityFromProcState(int procState) {
640 // Find the priority for the camera usage based on the process state. Higher priority clients
641 // win for evictions.
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700642 if (procState < 0) {
643 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
644 procState);
645 return -1;
Ruben Brunkcc776712015-02-17 20:18:47 -0800646 }
Eino-Ville Talvala52aad852015-09-03 12:24:24 -0700647 // Treat sleeping TOP processes the same as regular TOP processes, for
648 // access priority. This is important for lock-screen camera launch scenarios
649 if (procState == PROCESS_STATE_TOP_SLEEPING) {
650 procState = PROCESS_STATE_TOP;
651 }
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700652 return INT_MAX - procState;
Ruben Brunkcc776712015-02-17 20:18:47 -0800653}
654
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800655Status CameraService::getCameraVendorTagDescriptor(
656 /*out*/
657 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700658 ATRACE_CALL();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800659 if (!mModule) {
660 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800661 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800662 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800663 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
664 if (globalDescriptor != nullptr) {
665 *desc = *(globalDescriptor.get());
666 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800667 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800668}
669
Igor Murashkin634a5152013-02-20 17:15:11 -0800670int CameraService::getDeviceVersion(int cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700671 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800672 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800673 if (mModule->getCameraInfo(cameraId, &info) != OK) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800674 return -1;
675 }
676
677 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800678 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800679 deviceVersion = info.device_version;
680 } else {
681 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
682 }
683
684 if (facing) {
685 *facing = info.facing;
686 }
687
688 return deviceVersion;
689}
690
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800691Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700692 switch(err) {
693 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800694 return Status::ok();
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700695 case -EINVAL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800696 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
697 "CameraId is not valid for HAL module");
698 case -ENODEV:
699 return STATUS_ERROR(ERROR_DISCONNECTED,
700 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700701 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800702 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
703 "Camera HAL encountered error %d: %s",
704 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700705 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800706}
707
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800708bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700709 ATRACE_CALL();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800710 vendor_tag_ops_t vOps = vendor_tag_ops_t();
711
712 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800713 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800714 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
715 return false;
716 }
717
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800718 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800719
720 // Ensure all vendor operations are present
721 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
722 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
723 vOps.get_tag_type == NULL) {
724 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
725 , __FUNCTION__);
726 return false;
727 }
728
729 // Read all vendor tag definitions into a descriptor
730 sp<VendorTagDescriptor> desc;
731 status_t res;
732 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
733 != OK) {
734 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
735 "received error %s (%d). Camera clients will not be able to use"
736 "vendor tags", __FUNCTION__, strerror(res), res);
737 return false;
738 }
739
740 // Set the global descriptor to use with camera metadata
741 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
742 return true;
743}
744
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800745Status CameraService::makeClient(const sp<CameraService>& cameraService,
746 const sp<IInterface>& cameraCb, const String16& packageName, int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800747 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
748 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
749 /*out*/sp<BasicClient>* client) {
750
Ruben Brunkcc776712015-02-17 20:18:47 -0800751 if (halVersion < 0 || halVersion == deviceVersion) {
752 // Default path: HAL version is unspecified by caller, create CameraClient
753 // based on device version reported by the HAL.
754 switch(deviceVersion) {
755 case CAMERA_DEVICE_API_VERSION_1_0:
756 if (effectiveApiLevel == API_1) { // Camera1 API route
757 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800758 *client = new CameraClient(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800759 clientPid, clientUid, getpid(), legacyMode);
760 } else { // Camera2 API route
761 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800762 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
763 "Camera device \"%d\" HAL version %d does not support camera2 API",
764 cameraId, deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800765 }
766 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800767 case CAMERA_DEVICE_API_VERSION_3_0:
768 case CAMERA_DEVICE_API_VERSION_3_1:
769 case CAMERA_DEVICE_API_VERSION_3_2:
770 case CAMERA_DEVICE_API_VERSION_3_3:
771 if (effectiveApiLevel == API_1) { // Camera1 API route
772 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800773 *client = new Camera2Client(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800774 clientPid, clientUid, servicePid, legacyMode);
775 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800776 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
777 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
778 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800779 facing, clientPid, clientUid, servicePid);
780 }
781 break;
782 default:
783 // Should not be reachable
784 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800785 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
786 "Camera device \"%d\" has unknown HAL version %d",
787 cameraId, deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800788 }
789 } else {
790 // A particular HAL version is requested by caller. Create CameraClient
791 // based on the requested HAL version.
792 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
793 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
794 // Only support higher HAL version device opened as HAL1.0 device.
795 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800796 *client = new CameraClient(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800797 clientPid, clientUid, servicePid, legacyMode);
798 } else {
799 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
800 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
801 " opened as HAL %x device", halVersion, deviceVersion,
802 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800803 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
804 "Camera device \"%d\" (HAL version %d) cannot be opened as HAL version %d",
805 cameraId, deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800806 }
807 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800808 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800809}
810
Ruben Brunk6267b532015-04-30 17:44:07 -0700811String8 CameraService::toString(std::set<userid_t> intSet) {
812 String8 s("");
813 bool first = true;
814 for (userid_t i : intSet) {
815 if (first) {
816 s.appendFormat("%d", i);
817 first = false;
818 } else {
819 s.appendFormat(", %d", i);
820 }
821 }
822 return s;
823}
824
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800825Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800826 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700827
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800828 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800829 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800830 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800831 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800832 if (!(ret = connectHelper<ICameraClient,Client>(
833 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
834 internalPackageName, uid, USE_CALLING_PID,
835 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
836 /*out*/ tmp)
837 ).isOk()) {
838 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700839 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800840 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700841}
842
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800843Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700844 /*out*/
845 CameraParameters* parameters) {
846
847 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
848
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800849 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700850
851 if (parameters == NULL) {
852 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800853 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700854 }
855
Ruben Brunkcc776712015-02-17 20:18:47 -0800856 String8 id = String8::format("%d", cameraId);
857
858 // Check if we already have parameters
859 {
860 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700861 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800862 auto cameraState = getCameraState(id);
863 if (cameraState == nullptr) {
864 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800865 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
866 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800867 }
868 CameraParameters p = cameraState->getShimParams();
869 if (!p.isEmpty()) {
870 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800871 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700872 }
873 }
874
Ruben Brunkcc776712015-02-17 20:18:47 -0800875 int64_t token = IPCThreadState::self()->clearCallingIdentity();
876 ret = initializeShimMetadata(cameraId);
877 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800878 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800879 // Error already logged by callee
880 return ret;
881 }
882
883 // Check for parameters again
884 {
885 // Scope for service lock
886 Mutex::Autolock lock(mServiceLock);
887 auto cameraState = getCameraState(id);
888 if (cameraState == nullptr) {
889 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800890 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
891 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700892 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800893 CameraParameters p = cameraState->getShimParams();
894 if (!p.isEmpty()) {
895 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800896 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700897 }
898 }
899
Ruben Brunkcc776712015-02-17 20:18:47 -0800900 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
901 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800902 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700903}
904
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800905// Can camera service trust the caller based on the calling UID?
906static bool isTrustedCallingUid(uid_t uid) {
907 switch (uid) {
908 case AID_MEDIA: // mediaserver
909 case AID_CAMERASERVER: // cameraserver
910 return true;
911 default:
912 return false;
913 }
914}
915
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800916Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700917 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
918 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500919
Christopher Wileyce761d12016-02-16 10:15:00 -0800920#if !defined(__BRILLO__)
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700921 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
922 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800923 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -0800924 return allowed;
925 }
926#endif // defined(__BRILLO__)
927
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800928 int callingPid = getCallingPid();
929
Iliyan Malchev8951a972011-04-14 16:55:59 -0700930 if (!mModule) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800931 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
932 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800933 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
934 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700935 }
936
Ruben Brunkcc776712015-02-17 20:18:47 -0800937 if (getCameraState(cameraId) == nullptr) {
938 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
939 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800940 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
941 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 }
943
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800944 status_t err = checkIfDeviceIsUsable(cameraId);
945 if (err != NO_ERROR) {
946 switch(err) {
947 case -ENODEV:
948 case -EBUSY:
949 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
950 "No camera device with ID \"%s\" currently available", cameraId.string());
951 default:
952 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
953 "Unknown error connecting to ID \"%s\"", cameraId.string());
954 }
955 }
956 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800957}
958
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800959Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700960 const String8& clientName8, int& clientUid, int& clientPid,
961 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800962 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800963 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700964
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800965 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -0700966 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800967 clientUid = callingUid;
968 } else if (!isTrustedCallingUid(callingUid)) {
969 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
970 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800971 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
972 "Untrusted caller (calling PID %d, UID %d) trying to "
973 "forward camera access to camera %s for client %s (PID %d, UID %d)",
974 callingPid, callingUid, cameraId.string(),
975 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 }
977
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800978 // Check if we can trust clientPid
979 if (clientPid == USE_CALLING_PID) {
980 clientPid = callingPid;
981 } else if (!isTrustedCallingUid(callingUid)) {
982 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
983 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800984 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
985 "Untrusted caller (calling PID %d, UID %d) trying to "
986 "forward camera access to camera %s for client %s (PID %d, UID %d)",
987 callingPid, callingUid, cameraId.string(),
988 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800989 }
990
991 // If it's not calling from cameraserver, check the permission.
992 if (callingPid != getpid() &&
993 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
994 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800995 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
996 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
997 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800998 }
999
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001000 originalClientPid = clientPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001
Ruben Brunk6267b532015-04-30 17:44:07 -07001002 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001003
Ruben Brunka8ca9152015-04-07 14:23:40 -07001004 // Only allow clients who are being used by the current foreground device user, unless calling
1005 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001006 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1007 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1008 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1009 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001010 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1011 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1012 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001013 }
1014
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001015 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001016}
1017
1018status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1019 auto cameraState = getCameraState(cameraId);
1020 int callingPid = getCallingPid();
1021 if (cameraState == nullptr) {
1022 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1023 cameraId.string());
1024 return -ENODEV;
1025 }
1026
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001027 int32_t currentStatus = cameraState->getStatus();
Igor Murashkincba2c162013-03-20 15:56:31 -07001028 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001029 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1030 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001031 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -07001032 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001033 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1034 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001035 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001036 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001037
Ruben Brunkcc776712015-02-17 20:18:47 -08001038 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001039}
1040
Ruben Brunkcc776712015-02-17 20:18:47 -08001041void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1042 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001043
Ruben Brunkcc776712015-02-17 20:18:47 -08001044 // Make a descriptor for the incoming client
1045 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1046 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1047
1048 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1049 String8(client->getPackageName()));
1050
1051 if (evicted.size() > 0) {
1052 // This should never happen - clients should already have been removed in disconnect
1053 for (auto& i : evicted) {
1054 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1055 __FUNCTION__, i->getKey().string());
1056 }
1057
1058 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1059 __FUNCTION__);
1060 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001061
1062 // And register a death notification for the client callback. Do
1063 // this last to avoid Binder policy where a nested Binder
1064 // transaction might be pre-empted to service the client death
1065 // notification if the client process dies before linkToDeath is
1066 // invoked.
1067 sp<IBinder> remoteCallback = client->getRemote();
1068 if (remoteCallback != nullptr) {
1069 remoteCallback->linkToDeath(this);
1070 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001071}
1072
1073status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1074 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1075 /*out*/
1076 sp<BasicClient>* client,
1077 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001078 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001079 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001080 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001081 DescriptorPtr clientDescriptor;
1082 {
1083 if (effectiveApiLevel == API_1) {
1084 // If we are using API1, any existing client for this camera ID with the same remote
1085 // should be returned rather than evicted to allow MediaRecorder to work properly.
1086
1087 auto current = mActiveClientManager.get(cameraId);
1088 if (current != nullptr) {
1089 auto clientSp = current->getValue();
1090 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001091 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1092 ALOGW("CameraService connect called from same client, but with a different"
1093 " API level, evicting prior client...");
1094 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001095 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001096 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001097 *client = clientSp;
1098 return NO_ERROR;
1099 }
1100 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001101 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001102 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001103
Ruben Brunkcc776712015-02-17 20:18:47 -08001104 // Get current active client PIDs
1105 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1106 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001107
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001108 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1109 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1110 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1111 // not have an out-of-class definition.
1112 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001113
Ruben Brunkcc776712015-02-17 20:18:47 -08001114 // Get priorites of all active PIDs
1115 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1116 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001117
Ruben Brunkcc776712015-02-17 20:18:47 -08001118 // Update all active clients' priorities
1119 std::map<int,int> pidToPriorityMap;
1120 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1121 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1122 }
1123 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001124
Ruben Brunkcc776712015-02-17 20:18:47 -08001125 // Get state for the given cameraId
1126 auto state = getCameraState(cameraId);
1127 if (state == nullptr) {
1128 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1129 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001130 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001131 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001132 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001133
1134 // Make descriptor for incoming client
1135 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1136 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1137 state->getConflicting(),
1138 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1139
1140 // Find clients that would be evicted
1141 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1142
1143 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1144 // background, so we cannot do evictions
1145 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1146 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1147 " priority).", clientPid);
1148
1149 sp<BasicClient> clientSp = clientDescriptor->getValue();
1150 String8 curTime = getFormattedCurrentTime();
1151 auto incompatibleClients =
1152 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1153
1154 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001155 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001156 cameraId.string(), packageName.string(), clientPid,
1157 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1158
1159 for (auto& i : incompatibleClients) {
1160 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1161 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1162 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1163 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001164 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1165 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1166 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1167 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001168 }
1169
1170 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001171 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001172 mEventLog.add(msg);
1173
1174 return -EBUSY;
1175 }
1176
1177 for (auto& i : evicted) {
1178 sp<BasicClient> clientSp = i->getValue();
1179 if (clientSp.get() == nullptr) {
1180 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1181
1182 // TODO: Remove this
1183 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1184 __FUNCTION__);
1185 mActiveClientManager.remove(i);
1186 continue;
1187 }
1188
1189 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1190 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001191 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001192
Ruben Brunkcc776712015-02-17 20:18:47 -08001193 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001194 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1195 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1196 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001197 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1198 i->getOwnerId(), i->getPriority(), cameraId.string(),
1199 packageName.string(), clientPid,
1200 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1201
1202 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001204 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001205 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001206 }
1207
Ruben Brunkcc776712015-02-17 20:18:47 -08001208 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1209 // other clients from connecting in mServiceLockWrapper if held
1210 mServiceLock.unlock();
1211
1212 // Clear caller identity temporarily so client disconnect PID checks work correctly
1213 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1214
1215 // Destroy evicted clients
1216 for (auto& i : evictedClients) {
1217 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001218 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001219 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001220
1221 IPCThreadState::self()->restoreCallingIdentity(token);
1222
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001223 for (const auto& i : evictedClients) {
1224 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1225 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1226 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1227 if (ret == TIMED_OUT) {
1228 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1229 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1230 mActiveClientManager.toString().string());
1231 return -EBUSY;
1232 }
1233 if (ret != NO_ERROR) {
1234 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1235 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1236 ret, mActiveClientManager.toString().string());
1237 return ret;
1238 }
1239 }
1240
1241 evictedClients.clear();
1242
Ruben Brunkcc776712015-02-17 20:18:47 -08001243 // Once clients have been disconnected, relock
1244 mServiceLock.lock();
1245
1246 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1247 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1248 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001249 }
1250
Ruben Brunkcc776712015-02-17 20:18:47 -08001251 *partial = clientDescriptor;
1252 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001253}
1254
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001255Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001256 const sp<ICameraClient>& cameraClient,
1257 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001258 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001259 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001260 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001261 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001262 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001263
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001264 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001265 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001266 String8 id = String8::format("%d", cameraId);
1267 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001268 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1269 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1270 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1271 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001272
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001273 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001274 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001275 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001276 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001277 }
1278
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001279 *device = client;
1280 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001281}
1282
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001283Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001284 const sp<ICameraClient>& cameraClient,
1285 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001286 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001287 int clientUid,
1288 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001289 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001290
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001291 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001292 String8 id = String8::format("%d", cameraId);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08001293 int apiVersion = mModule->getModuleApiVersion();
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001294 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001295 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001296 /*
1297 * Either the HAL version is unspecified in which case this just creates
1298 * a camera client selected by the latest device version, or
1299 * it's a particular version in which case the HAL must supported
1300 * the open_legacy call
1301 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001302 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1303 apiVersion);
1304 ALOGE("%s: %s",
1305 __FUNCTION__, msg.string());
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001306 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001307 msg);
1308 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001309 }
1310
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001311 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001312 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001313 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1314 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1315 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1316 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001317
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001318 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001319 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001320 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001321 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001322 }
1323
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001324 *device = client;
1325 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001326}
1327
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001328Status CameraService::connectDevice(
1329 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Ruben Brunkcc776712015-02-17 20:18:47 -08001330 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001331 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001332 int clientUid,
1333 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001334 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001335
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001336 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001337 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001338 String8 id = String8::format("%d", cameraId);
1339 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001340 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1341 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1342 clientUid, USE_CALLING_PID, API_2,
1343 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1344 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001345
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001346 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001347 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001348 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001349 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001350 }
1351
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001352 *device = client;
1353 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001354}
1355
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001356Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001357 const sp<IBinder>& clientBinder) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001358
1359 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001360 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001361 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001362 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1363 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001364 }
1365
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001366 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001367 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001368
1369 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001370 auto state = getCameraState(id);
1371 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001372 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001373 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1374 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001375 }
1376
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001377 int32_t cameraStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -08001378 if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
1379 cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001380 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001381 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1382 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001383 }
1384
1385 {
1386 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001387 int32_t status;
1388 status_t err = getTorchStatusLocked(id, &status);
1389 if (err != OK) {
1390 if (err == NAME_NOT_FOUND) {
1391 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1392 "Camera \"%s\" does not have a flash unit", id.string());
1393 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001394 ALOGE("%s: getting current torch status failed for camera %s",
1395 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001396 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1397 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1398 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001399 }
1400
1401 if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001402 if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001403 ALOGE("%s: torch mode of camera %s is not available because "
1404 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001405 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1406 "Torch for camera \"%s\" is not available due to an existing camera user",
1407 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001408 } else {
1409 ALOGE("%s: torch mode of camera %s is not available due to "
1410 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001411 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1412 "Torch for camera \"%s\" is not available due to insufficient resources",
1413 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001414 }
1415 }
1416 }
1417
Ruben Brunk99e69712015-05-26 17:25:07 -07001418 {
1419 // Update UID map - this is used in the torch status changed callbacks, so must be done
1420 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001421 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001422 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1423 mTorchUidMap[id].first = uid;
1424 mTorchUidMap[id].second = uid;
1425 } else {
1426 // Set the pending UID
1427 mTorchUidMap[id].first = uid;
1428 }
1429 }
1430
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001431 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001432
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001433 if (err != OK) {
1434 int32_t errorCode;
1435 String8 msg;
1436 switch (err) {
1437 case -ENOSYS:
1438 msg = String8::format("Camera \"%s\" has no flashlight",
1439 id.string());
1440 errorCode = ERROR_ILLEGAL_ARGUMENT;
1441 break;
1442 default:
1443 msg = String8::format(
1444 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1445 id.string(), enabled, strerror(-err), err);
1446 errorCode = ERROR_INVALID_OPERATION;
1447 }
1448 ALOGE("%s: %s", __FUNCTION__, msg.string());
1449 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001450 }
1451
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001452 {
1453 // update the link to client's death
1454 Mutex::Autolock al(mTorchClientMapMutex);
1455 ssize_t index = mTorchClientMap.indexOfKey(id);
1456 if (enabled) {
1457 if (index == NAME_NOT_FOUND) {
1458 mTorchClientMap.add(id, clientBinder);
1459 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001460 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001461 mTorchClientMap.replaceValueAt(index, clientBinder);
1462 }
1463 clientBinder->linkToDeath(this);
1464 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001465 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001466 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001467 }
1468
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001469 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001470}
1471
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001472Status CameraService::notifySystemEvent(int32_t eventId,
1473 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001474 ATRACE_CALL();
1475
Ruben Brunk36597b22015-03-20 22:15:57 -07001476 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001477 case ICameraService::EVENT_USER_SWITCHED: {
1478 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001479 break;
1480 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001481 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001482 default: {
1483 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1484 eventId);
1485 break;
1486 }
1487 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001488 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001489}
1490
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001491Status CameraService::addListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001492 ATRACE_CALL();
1493
Igor Murashkinbfc99152013-02-27 12:55:20 -08001494 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001495
Ruben Brunk3450ba72015-06-16 11:00:37 -07001496 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001497 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001498 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001499 }
1500
Igor Murashkinbfc99152013-02-27 12:55:20 -08001501 Mutex::Autolock lock(mServiceLock);
1502
Ruben Brunkcc776712015-02-17 20:18:47 -08001503 {
1504 Mutex::Autolock lock(mStatusListenerLock);
1505 for (auto& it : mListenerList) {
1506 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1507 ALOGW("%s: Tried to add listener %p which was already subscribed",
1508 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001509 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001510 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001511 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001512
1513 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001514 }
1515
Igor Murashkinbfc99152013-02-27 12:55:20 -08001516
Igor Murashkincba2c162013-03-20 15:56:31 -07001517 /* Immediately signal current status to this listener only */
1518 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001519 Mutex::Autolock lock(mCameraStatesLock);
1520 for (auto& i : mCameraStates) {
1521 // TODO: Update binder to use String16 for camera IDs and remove;
1522 int id = cameraIdToInt(i.first);
1523 if (id == -1) continue;
1524
1525 listener->onStatusChanged(i.second->getStatus(), id);
Igor Murashkincba2c162013-03-20 15:56:31 -07001526 }
1527 }
1528
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001529 /* Immediately signal current torch status to this listener only */
1530 {
1531 Mutex::Autolock al(mTorchStatusMutex);
1532 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001533 String16 id = String16(mTorchStatusMap.keyAt(i).string());
1534 listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001535 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001536 }
1537
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001538 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001539}
Ruben Brunkcc776712015-02-17 20:18:47 -08001540
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001541Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001542 ATRACE_CALL();
1543
Igor Murashkinbfc99152013-02-27 12:55:20 -08001544 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1545
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001546 if (listener == 0) {
1547 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001548 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001549 }
1550
Igor Murashkinbfc99152013-02-27 12:55:20 -08001551 Mutex::Autolock lock(mServiceLock);
1552
Ruben Brunkcc776712015-02-17 20:18:47 -08001553 {
1554 Mutex::Autolock lock(mStatusListenerLock);
1555 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1556 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1557 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001558 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001559 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001560 }
1561 }
1562
1563 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1564 __FUNCTION__, listener.get());
1565
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001566 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001567}
1568
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001569Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001570
1571 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001572 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1573
1574 if (parameters == NULL) {
1575 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001576 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001577 }
1578
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001579 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001580
1581 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001582 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001583 // Error logged by caller
1584 return ret;
1585 }
1586
1587 String8 shimParamsString8 = shimParams.flatten();
1588 String16 shimParamsString16 = String16(shimParamsString8);
1589
1590 *parameters = shimParamsString16;
1591
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001592 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001593}
1594
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001595Status CameraService::supportsCameraApi(int cameraId, int apiVersion, bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001596 ATRACE_CALL();
1597
Igor Murashkin65d14b92014-06-17 12:03:20 -07001598 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1599
1600 switch (apiVersion) {
1601 case API_VERSION_1:
1602 case API_VERSION_2:
1603 break;
1604 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001605 String8 msg = String8::format("Unknown API version %d", apiVersion);
1606 ALOGE("%s: %s", __FUNCTION__, msg.string());
1607 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001608 }
1609
1610 int facing = -1;
1611 int deviceVersion = getDeviceVersion(cameraId, &facing);
1612
1613 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001614 case CAMERA_DEVICE_API_VERSION_1_0:
1615 case CAMERA_DEVICE_API_VERSION_3_0:
1616 case CAMERA_DEVICE_API_VERSION_3_1:
1617 if (apiVersion == API_VERSION_2) {
1618 ALOGV("%s: Camera id %d uses HAL version %d <3.2, doesn't support api2 without shim",
1619 __FUNCTION__, cameraId, deviceVersion);
1620 *isSupported = false;
1621 } else { // if (apiVersion == API_VERSION_1) {
1622 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported",
1623 __FUNCTION__, cameraId);
1624 *isSupported = true;
1625 }
1626 break;
1627 case CAMERA_DEVICE_API_VERSION_3_2:
1628 case CAMERA_DEVICE_API_VERSION_3_3:
1629 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
Igor Murashkin65d14b92014-06-17 12:03:20 -07001630 __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001631 *isSupported = true;
1632 break;
1633 case -1: {
1634 String8 msg = String8::format("Unknown camera ID %d", cameraId);
1635 ALOGE("%s: %s", __FUNCTION__, msg.string());
1636 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001637 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001638 default: {
1639 String8 msg = String8::format("Unknown device version %d for device %d",
1640 deviceVersion, cameraId);
1641 ALOGE("%s: %s", __FUNCTION__, msg.string());
1642 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1643 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001644 }
1645
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001646 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001647}
1648
Ruben Brunkcc776712015-02-17 20:18:47 -08001649void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001650 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001651 for (auto& i : mActiveClientManager.getAll()) {
1652 auto clientSp = i->getValue();
1653 if (clientSp.get() == client) {
1654 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001655 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001656 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001657}
1658
Ruben Brunkcc776712015-02-17 20:18:47 -08001659bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1660 const int callingPid = getCallingPid();
1661 const int servicePid = getpid();
1662 bool ret = false;
1663 {
1664 // Acquire mServiceLock and prevent other clients from connecting
1665 std::unique_ptr<AutoConditionLock> lock =
1666 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001667
Igor Murashkin634a5152013-02-20 17:15:11 -08001668
Ruben Brunkcc776712015-02-17 20:18:47 -08001669 std::vector<sp<BasicClient>> evicted;
1670 for (auto& i : mActiveClientManager.getAll()) {
1671 auto clientSp = i->getValue();
1672 if (clientSp.get() == nullptr) {
1673 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1674 mActiveClientManager.remove(i);
1675 continue;
1676 }
1677 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1678 callingPid == clientSp->getClientPid())) {
1679 mActiveClientManager.remove(i);
1680 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001681
Ruben Brunkcc776712015-02-17 20:18:47 -08001682 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001683 clientSp->notifyError(
1684 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001685 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001686 }
1687 }
1688
Ruben Brunkcc776712015-02-17 20:18:47 -08001689 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1690 // other clients from connecting in mServiceLockWrapper if held
1691 mServiceLock.unlock();
1692
Ruben Brunk36597b22015-03-20 22:15:57 -07001693 // Do not clear caller identity, remote caller should be client proccess
1694
Ruben Brunkcc776712015-02-17 20:18:47 -08001695 for (auto& i : evicted) {
1696 if (i.get() != nullptr) {
1697 i->disconnect();
1698 ret = true;
1699 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001700 }
1701
Ruben Brunkcc776712015-02-17 20:18:47 -08001702 // Reacquire mServiceLock
1703 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001704
Ruben Brunkcc776712015-02-17 20:18:47 -08001705 } // lock is destroyed, allow further connect calls
1706
1707 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001708}
1709
Igor Murashkinecf17e82012-10-02 16:05:11 -07001710
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001711/**
1712 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001713 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001714 */
1715int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001716 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1717 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1718 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1719 // Verify the device version is in the supported range
1720 switch (info.device_version) {
1721 case CAMERA_DEVICE_API_VERSION_1_0:
1722 case CAMERA_DEVICE_API_VERSION_3_0:
1723 case CAMERA_DEVICE_API_VERSION_3_1:
1724 case CAMERA_DEVICE_API_VERSION_3_2:
1725 case CAMERA_DEVICE_API_VERSION_3_3:
1726 // in support
1727 break;
1728 case CAMERA_DEVICE_API_VERSION_2_0:
1729 case CAMERA_DEVICE_API_VERSION_2_1:
1730 // no longer supported
1731 default:
1732 ALOGE("%s: Device %d has HAL version %x, which is not supported",
1733 __FUNCTION__, id, info.device_version);
1734 String8 msg = String8::format(
1735 "Unsupported device HAL version %x for device %d",
1736 info.device_version, id);
1737 logServiceError(msg.string(), NO_INIT);
1738 return NO_INIT;
1739 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001740 }
1741
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001742 // Assume all devices pre-v3.3 are backward-compatible
1743 bool isBackwardCompatible = true;
1744 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
1745 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
1746 isBackwardCompatible = false;
1747 status_t res;
1748 camera_metadata_ro_entry_t caps;
1749 res = find_camera_metadata_ro_entry(
1750 info.static_camera_characteristics,
1751 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1752 &caps);
1753 if (res != 0) {
1754 ALOGW("%s: Unable to find camera capabilities for camera device %d",
1755 __FUNCTION__, id);
1756 caps.count = 0;
1757 }
1758 for (size_t i = 0; i < caps.count; i++) {
1759 if (caps.data.u8[i] ==
1760 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
1761 isBackwardCompatible = true;
1762 break;
1763 }
1764 }
1765 }
1766
1767 if (!isBackwardCompatible) {
1768 mNumberOfNormalCameras--;
1769 *latestStrangeCameraId = id;
1770 } else {
1771 if (id > *latestStrangeCameraId) {
1772 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
1773 "This is not allowed due backward-compatibility requirements",
1774 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001775 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001776 mNumberOfCameras = 0;
1777 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001778 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001779 }
1780 }
1781 return OK;
1782}
1783
Ruben Brunkcc776712015-02-17 20:18:47 -08001784std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1785 const String8& cameraId) const {
1786 std::shared_ptr<CameraState> state;
1787 {
1788 Mutex::Autolock lock(mCameraStatesLock);
1789 auto iter = mCameraStates.find(cameraId);
1790 if (iter != mCameraStates.end()) {
1791 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001792 }
1793 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001794 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001795}
1796
Ruben Brunkcc776712015-02-17 20:18:47 -08001797sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1798 // Remove from active clients list
1799 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1800 if (clientDescriptorPtr == nullptr) {
1801 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1802 cameraId.string());
1803 return sp<BasicClient>{nullptr};
1804 }
1805
1806 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001807}
1808
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001809void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001810 // Acquire mServiceLock and prevent other clients from connecting
1811 std::unique_ptr<AutoConditionLock> lock =
1812 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1813
Ruben Brunk6267b532015-04-30 17:44:07 -07001814 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001815 for (size_t i = 0; i < newUserIds.size(); i++) {
1816 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001817 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001818 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07001819 return;
1820 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001821 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001822 }
1823
Ruben Brunka8ca9152015-04-07 14:23:40 -07001824
Ruben Brunk6267b532015-04-30 17:44:07 -07001825 if (newAllowedUsers == mAllowedUsers) {
1826 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1827 return;
1828 }
1829
1830 logUserSwitch(mAllowedUsers, newAllowedUsers);
1831
1832 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001833
1834 // Current user has switched, evict all current clients.
1835 std::vector<sp<BasicClient>> evicted;
1836 for (auto& i : mActiveClientManager.getAll()) {
1837 auto clientSp = i->getValue();
1838
1839 if (clientSp.get() == nullptr) {
1840 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1841 continue;
1842 }
1843
Ruben Brunk6267b532015-04-30 17:44:07 -07001844 // Don't evict clients that are still allowed.
1845 uid_t clientUid = clientSp->getClientUid();
1846 userid_t clientUserId = multiuser_get_user_id(clientUid);
1847 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1848 continue;
1849 }
1850
Ruben Brunk36597b22015-03-20 22:15:57 -07001851 evicted.push_back(clientSp);
1852
1853 String8 curTime = getFormattedCurrentTime();
1854
1855 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1856 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001857
Ruben Brunk36597b22015-03-20 22:15:57 -07001858 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001859 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07001860 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07001861 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1862 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001863
1864 }
1865
1866 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1867 // blocking other clients from connecting in mServiceLockWrapper if held.
1868 mServiceLock.unlock();
1869
1870 // Clear caller identity temporarily so client disconnect PID checks work correctly
1871 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1872
1873 for (auto& i : evicted) {
1874 i->disconnect();
1875 }
1876
1877 IPCThreadState::self()->restoreCallingIdentity(token);
1878
1879 // Reacquire mServiceLock
1880 mServiceLock.lock();
1881}
Ruben Brunkcc776712015-02-17 20:18:47 -08001882
Ruben Brunka8ca9152015-04-07 14:23:40 -07001883void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001884 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001885 Mutex::Autolock l(mLogLock);
1886 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001887}
1888
Ruben Brunka8ca9152015-04-07 14:23:40 -07001889void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001890 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001891 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001892 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001893 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001894}
1895
1896void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001897 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001898 // Log the clients evicted
1899 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001900 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001901}
1902
1903void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001904 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001905 // Log the client rejected
1906 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001907 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001908}
1909
Ruben Brunk6267b532015-04-30 17:44:07 -07001910void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1911 const std::set<userid_t>& newUserIds) {
1912 String8 newUsers = toString(newUserIds);
1913 String8 oldUsers = toString(oldUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001914 // Log the new and old users
Ruben Brunk6267b532015-04-30 17:44:07 -07001915 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
1916 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001917}
1918
1919void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1920 // Log the device removal
1921 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1922}
1923
1924void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1925 // Log the device removal
1926 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1927}
1928
1929void CameraService::logClientDied(int clientPid, const char* reason) {
1930 // Log the device removal
1931 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001932}
1933
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001934void CameraService::logServiceError(const char* msg, int errorCode) {
1935 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001936 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001937}
1938
Ruben Brunk36597b22015-03-20 22:15:57 -07001939status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1940 uint32_t flags) {
1941
1942 const int pid = getCallingPid();
1943 const int selfPid = getpid();
1944
Mathias Agopian65ab4712010-07-14 17:59:35 -07001945 // Permission checks
1946 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001947 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07001948 if (pid != selfPid) {
1949 // Ensure we're being called by system_server, or similar process with
1950 // permissions to notify the camera service about system events
1951 if (!checkCallingPermission(
1952 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1953 const int uid = getCallingUid();
1954 ALOGE("Permission Denial: cannot send updates to camera service about system"
1955 " events from pid=%d, uid=%d", pid, uid);
1956 return PERMISSION_DENIED;
1957 }
1958 }
1959 break;
1960 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001961 }
1962
1963 return BnCameraService::onTransact(code, data, reply, flags);
1964}
1965
Mathias Agopian65ab4712010-07-14 17:59:35 -07001966// We share the media players for shutter and recording sound for all clients.
1967// A reference count is kept to determine when we will actually release the
1968// media players.
1969
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001970MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001971 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001972 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001973 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974 mp->prepare();
1975 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001976 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001977 return NULL;
1978 }
1979 return mp;
1980}
1981
1982void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001983 ATRACE_CALL();
1984
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985 Mutex::Autolock lock(mSoundLock);
1986 LOG1("CameraService::loadSound ref=%d", mSoundRef);
1987 if (mSoundRef++) return;
1988
1989 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07001990 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
1991 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001992}
1993
1994void CameraService::releaseSound() {
1995 Mutex::Autolock lock(mSoundLock);
1996 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
1997 if (--mSoundRef) return;
1998
1999 for (int i = 0; i < NUM_SOUNDS; i++) {
2000 if (mSoundPlayer[i] != 0) {
2001 mSoundPlayer[i]->disconnect();
2002 mSoundPlayer[i].clear();
2003 }
2004 }
2005}
2006
2007void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002008 ATRACE_CALL();
2009
Mathias Agopian65ab4712010-07-14 17:59:35 -07002010 LOG1("playSound(%d)", kind);
2011 Mutex::Autolock lock(mSoundLock);
2012 sp<MediaPlayer> player = mSoundPlayer[kind];
2013 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002014 player->seekTo(0);
2015 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002016 }
2017}
2018
2019// ----------------------------------------------------------------------------
2020
2021CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002022 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002023 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002024 int cameraId, int cameraFacing,
2025 int clientPid, uid_t clientUid,
2026 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002027 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002028 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002029 clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002030 cameraId, cameraFacing,
2031 clientPid, clientUid,
2032 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08002033{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002034 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002035 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002036
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002037 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002038
Mathias Agopian65ab4712010-07-14 17:59:35 -07002039 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002040
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002041 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002042}
2043
Mathias Agopian65ab4712010-07-14 17:59:35 -07002044// tear down the client
2045CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002046 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002047 mDestructionStarted = true;
2048
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -07002049 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002050 // unconditionally disconnect. function is idempotent
2051 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002052}
2053
Igor Murashkin634a5152013-02-20 17:15:11 -08002054CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002055 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002056 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002057 int cameraId, int cameraFacing,
2058 int clientPid, uid_t clientUid,
2059 int servicePid):
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002060 mClientPackageName(clientPackageName), mDisconnected(false)
Igor Murashkin634a5152013-02-20 17:15:11 -08002061{
2062 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002063 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -08002064 mCameraId = cameraId;
2065 mCameraFacing = cameraFacing;
2066 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002067 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -08002068 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002069 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002070 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002071
2072 // In some cases the calling code has no access to the package it runs under.
2073 // For example, NDK camera API.
2074 // In this case we will get the packages for the calling UID and pick the first one
2075 // for attributing the app op. This will work correctly for runtime permissions
2076 // as for legacy apps we will toggle the app op for all packages in the UID.
2077 // The caveat is that the operation may be attributed to the wrong package and
2078 // stats based on app ops may be slightly off.
2079 if (mClientPackageName.size() <= 0) {
2080 sp<IServiceManager> sm = defaultServiceManager();
2081 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2082 if (binder == 0) {
2083 ALOGE("Cannot get permission service");
2084 // Leave mClientPackageName unchanged (empty) and the further interaction
2085 // with camera will fail in BasicClient::startCameraOps
2086 return;
2087 }
2088
2089 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2090 Vector<String16> packages;
2091
2092 permCtrl->getPackagesForUid(mClientUid, packages);
2093
2094 if (packages.isEmpty()) {
2095 ALOGE("No packages for calling UID");
2096 // Leave mClientPackageName unchanged (empty) and the further interaction
2097 // with camera will fail in BasicClient::startCameraOps
2098 return;
2099 }
2100 mClientPackageName = packages[0];
2101 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002102}
2103
2104CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002105 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002106 mDestructionStarted = true;
2107}
2108
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002109binder::Status CameraService::BasicClient::disconnect() {
2110 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002111 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002112 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002113 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002114 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002115
2116 mCameraService->removeByClient(this);
2117 mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002118 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002119
2120 sp<IBinder> remote = getRemote();
2121 if (remote != nullptr) {
2122 remote->unlinkToDeath(mCameraService);
2123 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002124
2125 finishCameraOps();
Ruben Brunkcc776712015-02-17 20:18:47 -08002126 ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid);
2127
Igor Murashkincba2c162013-03-20 15:56:31 -07002128 // client shouldn't be able to call into us anymore
2129 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002130
2131 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002132}
2133
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002134status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2135 // No dumping of clients directly over Binder,
2136 // must go through CameraService::dump
2137 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2138 IPCThreadState::self()->getCallingUid(), NULL, 0);
2139 return OK;
2140}
2141
Ruben Brunkcc776712015-02-17 20:18:47 -08002142String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002143 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002144}
2145
2146
2147int CameraService::BasicClient::getClientPid() const {
2148 return mClientPid;
2149}
2150
Ruben Brunk6267b532015-04-30 17:44:07 -07002151uid_t CameraService::BasicClient::getClientUid() const {
2152 return mClientUid;
2153}
2154
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002155bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2156 // Defaults to API2.
2157 return level == API_2;
2158}
2159
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002160status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002161 ATRACE_CALL();
2162
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002163 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002164 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002165 mOpsCallback = new OpsCallback(this);
2166
Igor Murashkine6800ce2013-03-04 17:25:57 -08002167 {
2168 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002169 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002170 }
2171
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002172 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002173 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002174 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002175 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002176
Svetoslav28e8ef72015-05-11 19:21:31 -07002177 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002178 ALOGI("Camera %d: Access for \"%s\" has been revoked",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002179 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002180 return PERMISSION_DENIED;
2181 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002182
Svetoslav28e8ef72015-05-11 19:21:31 -07002183 if (res == AppOpsManager::MODE_IGNORED) {
2184 ALOGI("Camera %d: Access for \"%s\" has been restricted",
2185 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002186 // Return the same error as for device policy manager rejection
2187 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002188 }
2189
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002190 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002191
2192 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
2193 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
Ruben Brunkcc776712015-02-17 20:18:47 -08002194 String8::format("%d", mCameraId));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002195
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002196 // Transition device state to OPEN
2197 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2198 String8::format("%d", mCameraId));
2199
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002200 return OK;
2201}
2202
2203status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002204 ATRACE_CALL();
2205
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002206 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002207 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002208 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002209 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002210 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002211 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002212
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002213 std::initializer_list<int32_t> rejected = {ICameraServiceListener::STATUS_NOT_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002214 ICameraServiceListener::STATUS_ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002215
Ruben Brunkcc776712015-02-17 20:18:47 -08002216 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002217 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002218 String8::format("%d", mCameraId), rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002219
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002220 // Transition device state to CLOSED
2221 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2222 String8::format("%d", mCameraId));
2223
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002224 // Notify flashlight that a camera device is closed.
2225 mCameraService->mFlashlight->deviceClosed(
2226 String8::format("%d", mCameraId));
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002227 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002228 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002229 if (mOpsCallback != NULL) {
2230 mAppOpsManager.stopWatchingMode(mOpsCallback);
2231 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002232 mOpsCallback.clear();
2233
2234 return OK;
2235}
2236
2237void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002238 ATRACE_CALL();
2239
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002240 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002241 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002242
2243 if (op != AppOpsManager::OP_CAMERA) {
2244 ALOGW("Unexpected app ops notification received: %d", op);
2245 return;
2246 }
2247
2248 int32_t res;
2249 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002250 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002251 ALOGV("checkOp returns: %d, %s ", res,
2252 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2253 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2254 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2255 "UNKNOWN");
2256
2257 if (res != AppOpsManager::MODE_ALLOWED) {
2258 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
2259 myName.string());
2260 // Reset the client PID to allow server-initiated disconnect,
2261 // and to prevent further calls by client.
2262 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002263 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002264 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002265 disconnect();
2266 }
2267}
2268
Mathias Agopian65ab4712010-07-14 17:59:35 -07002269// ----------------------------------------------------------------------------
2270
Ruben Brunkcc776712015-02-17 20:18:47 -08002271// Provide client strong pointer for callbacks.
2272sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
2273 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
2274 auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId);
2275 if (clientDescriptor != nullptr) {
2276 return sp<Client>{
2277 static_cast<Client*>(clientDescriptor->getValue().get())};
2278 }
2279 return sp<Client>{nullptr};
Mathias Agopian65ab4712010-07-14 17:59:35 -07002280}
2281
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002282void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002283 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002284 (void) errorCode;
2285 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002286 if (mRemoteCallback != NULL) {
2287 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2288 } else {
2289 ALOGE("mRemoteCallback is NULL!!");
2290 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002291}
2292
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002293// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002294binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002295 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002296 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002297}
2298
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002299bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2300 return level == API_1;
2301}
2302
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002303CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2304 mClient(client) {
2305}
2306
2307void CameraService::Client::OpsCallback::opChanged(int32_t op,
2308 const String16& packageName) {
2309 sp<BasicClient> client = mClient.promote();
2310 if (client != NULL) {
2311 client->opChanged(op, packageName);
2312 }
2313}
2314
Mathias Agopian65ab4712010-07-14 17:59:35 -07002315// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002316// CameraState
2317// ----------------------------------------------------------------------------
2318
2319CameraService::CameraState::CameraState(const String8& id, int cost,
2320 const std::set<String8>& conflicting) : mId(id),
2321 mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {}
2322
2323CameraService::CameraState::~CameraState() {}
2324
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002325int32_t CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002326 Mutex::Autolock lock(mStatusLock);
2327 return mStatus;
2328}
2329
2330CameraParameters CameraService::CameraState::getShimParams() const {
2331 return mShimParams;
2332}
2333
2334void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2335 mShimParams = params;
2336}
2337
2338int CameraService::CameraState::getCost() const {
2339 return mCost;
2340}
2341
2342std::set<String8> CameraService::CameraState::getConflicting() const {
2343 return mConflicting;
2344}
2345
2346String8 CameraService::CameraState::getId() const {
2347 return mId;
2348}
2349
2350// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002351// ClientEventListener
2352// ----------------------------------------------------------------------------
2353
2354void CameraService::ClientEventListener::onClientAdded(
2355 const resource_policy::ClientDescriptor<String8,
2356 sp<CameraService::BasicClient>>& descriptor) {
2357 auto basicClient = descriptor.getValue();
2358 if (basicClient.get() != nullptr) {
2359 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2360 notifier.noteStartCamera(descriptor.getKey(),
2361 static_cast<int>(basicClient->getClientUid()));
2362 }
2363}
2364
2365void CameraService::ClientEventListener::onClientRemoved(
2366 const resource_policy::ClientDescriptor<String8,
2367 sp<CameraService::BasicClient>>& descriptor) {
2368 auto basicClient = descriptor.getValue();
2369 if (basicClient.get() != nullptr) {
2370 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2371 notifier.noteStopCamera(descriptor.getKey(),
2372 static_cast<int>(basicClient->getClientUid()));
2373 }
2374}
2375
2376
2377// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002378// CameraClientManager
2379// ----------------------------------------------------------------------------
2380
Ruben Brunk99e69712015-05-26 17:25:07 -07002381CameraService::CameraClientManager::CameraClientManager() {
2382 setListener(std::make_shared<ClientEventListener>());
2383}
2384
Ruben Brunkcc776712015-02-17 20:18:47 -08002385CameraService::CameraClientManager::~CameraClientManager() {}
2386
2387sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2388 const String8& id) const {
2389 auto descriptor = get(id);
2390 if (descriptor == nullptr) {
2391 return sp<BasicClient>{nullptr};
2392 }
2393 return descriptor->getValue();
2394}
2395
2396String8 CameraService::CameraClientManager::toString() const {
2397 auto all = getAll();
2398 String8 ret("[");
2399 bool hasAny = false;
2400 for (auto& i : all) {
2401 hasAny = true;
2402 String8 key = i->getKey();
2403 int32_t cost = i->getCost();
2404 int32_t pid = i->getOwnerId();
2405 int32_t priority = i->getPriority();
2406 auto conflicting = i->getConflicting();
2407 auto clientSp = i->getValue();
2408 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002409 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002410 if (clientSp.get() != nullptr) {
2411 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002412 uid_t clientUid = clientSp->getClientUid();
2413 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002414 }
2415 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2416 PRId32 ", ", key.string(), cost, pid, priority);
2417
Ruben Brunk6267b532015-04-30 17:44:07 -07002418 if (clientSp.get() != nullptr) {
2419 ret.appendFormat("User Id: %d, ", clientUserId);
2420 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002421 if (packageName.size() != 0) {
2422 ret.appendFormat("Client Package Name: %s", packageName.string());
2423 }
2424
2425 ret.append(", Conflicting Client Devices: {");
2426 for (auto& j : conflicting) {
2427 ret.appendFormat("%s, ", j.string());
2428 }
2429 ret.append("})");
2430 }
2431 if (hasAny) ret.append("\n");
2432 ret.append("]\n");
2433 return ret;
2434}
2435
2436CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2437 const String8& key, const sp<BasicClient>& value, int32_t cost,
2438 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2439
2440 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2441 key, value, cost, conflictingKeys, priority, ownerId);
2442}
2443
2444CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2445 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2446 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2447 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2448}
2449
2450// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002451
2452static const int kDumpLockRetries = 50;
2453static const int kDumpLockSleep = 60000;
2454
2455static bool tryLock(Mutex& mutex)
2456{
2457 bool locked = false;
2458 for (int i = 0; i < kDumpLockRetries; ++i) {
2459 if (mutex.tryLock() == NO_ERROR) {
2460 locked = true;
2461 break;
2462 }
2463 usleep(kDumpLockSleep);
2464 }
2465 return locked;
2466}
2467
2468status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002469 ATRACE_CALL();
2470
Ruben Brunka8ca9152015-04-07 14:23:40 -07002471 String8 result("Dump of the Camera Service:\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002472 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
dcashmanfefa6142015-09-09 13:43:01 -07002473 result = result.format("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07002474 "can't dump CameraService from pid=%d, uid=%d\n",
2475 getCallingPid(),
2476 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002477 write(fd, result.string(), result.size());
2478 } else {
2479 bool locked = tryLock(mServiceLock);
2480 // failed to lock - CameraService is probably deadlocked
2481 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002482 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002483 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002484 }
2485
2486 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002487 if (!mModule) {
2488 result = String8::format("No camera module available!\n");
2489 write(fd, result.string(), result.size());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002490
2491 // Dump event log for error information
2492 dumpEventLog(fd);
2493
Kalle Lampila6ec3a152013-04-30 15:27:19 +03002494 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002495 return NO_ERROR;
2496 }
2497
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002498 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2499 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2500 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2501 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
Ruben Brunkcc776712015-02-17 20:18:47 -08002502 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
Eino-Ville Talvala49c97052016-01-12 14:29:40 -08002503 result.appendFormat("Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Ruben Brunkcc776712015-02-17 20:18:47 -08002504 String8 activeClientString = mActiveClientManager.toString();
2505 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
Ruben Brunk6267b532015-04-30 17:44:07 -07002506 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002507
Ruben Brunkf81648e2014-04-17 16:14:57 -07002508 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2509 if (desc == NULL) {
2510 result.appendFormat("Vendor tags left unimplemented.\n");
2511 } else {
2512 result.appendFormat("Vendor tag definitions:\n");
2513 }
2514
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002515 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07002516
2517 if (desc != NULL) {
2518 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
2519 }
2520
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002521 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002522
2523 bool stateLocked = tryLock(mCameraStatesLock);
2524 if (!stateLocked) {
2525 result = String8::format("CameraStates in use, may be deadlocked\n");
2526 write(fd, result.string(), result.size());
2527 }
2528
2529 for (auto& state : mCameraStates) {
2530 String8 cameraId = state.first;
2531 result = String8::format("Camera %s information:\n", cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002532 camera_info info;
2533
Ruben Brunkcc776712015-02-17 20:18:47 -08002534 // TODO: Change getCameraInfo + HAL to use String cameraIds
2535 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002536 if (rc != OK) {
2537 result.appendFormat(" Error reading static information!\n");
2538 write(fd, result.string(), result.size());
2539 } else {
2540 result.appendFormat(" Facing: %s\n",
2541 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
2542 result.appendFormat(" Orientation: %d\n", info.orientation);
2543 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002544 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002545 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2546 } else {
2547 deviceVersion = info.device_version;
2548 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002549
2550 auto conflicting = state.second->getConflicting();
2551 result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
2552 result.appendFormat(" Conflicting Devices:");
2553 for (auto& id : conflicting) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002554 result.appendFormat(" %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002555 }
2556 if (conflicting.size() == 0) {
2557 result.appendFormat(" NONE");
2558 }
2559 result.appendFormat("\n");
2560
2561 result.appendFormat(" Device version: %#x\n", deviceVersion);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002562 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002563 result.appendFormat(" Device static metadata:\n");
2564 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07002565 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07002566 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002567 } else {
2568 write(fd, result.string(), result.size());
2569 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002570
2571 CameraParameters p = state.second->getShimParams();
2572 if (!p.isEmpty()) {
2573 result = String8::format(" Camera1 API shim is using parameters:\n ");
2574 write(fd, result.string(), result.size());
2575 p.dump(fd, args);
2576 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002577 }
2578
Ruben Brunkcc776712015-02-17 20:18:47 -08002579 auto clientDescriptor = mActiveClientManager.get(cameraId);
2580 if (clientDescriptor == nullptr) {
2581 result = String8::format(" Device %s is closed, no client instance\n",
2582 cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002583 write(fd, result.string(), result.size());
2584 continue;
2585 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002586 hasClient = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002587 result = String8::format(" Device %s is open. Client instance dump:\n\n",
2588 cameraId.string());
2589 result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
2590 result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
2591
2592 auto client = clientDescriptor->getValue();
2593 result.appendFormat("Client package: %s\n",
2594 String8(client->getPackageName()).string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002595 write(fd, result.string(), result.size());
Ruben Brunkcc776712015-02-17 20:18:47 -08002596
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002597 client->dumpClient(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002598 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002599
2600 if (stateLocked) mCameraStatesLock.unlock();
2601
Mathias Agopian65ab4712010-07-14 17:59:35 -07002602 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002603 result = String8::format("\nNo active camera clients yet.\n");
2604 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002605 }
2606
2607 if (locked) mServiceLock.unlock();
2608
Igor Murashkinff3e31d2013-10-23 16:40:06 -07002609 // Dump camera traces if there were any
2610 write(fd, "\n", 1);
2611 camera3::CameraTraces::dump(fd, args);
2612
Mathias Agopian65ab4712010-07-14 17:59:35 -07002613 // change logging level
2614 int n = args.size();
2615 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002616 String16 verboseOption("-v");
2617 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002618 String8 levelStr(args[i+1]);
2619 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002620 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002621 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002622 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002623 }
2624 }
2625 }
2626 return NO_ERROR;
2627}
2628
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002629void CameraService::dumpEventLog(int fd) {
2630 String8 result = String8("\nPrior client events (most recent at top):\n");
2631
2632 Mutex::Autolock l(mLogLock);
2633 for (const auto& msg : mEventLog) {
2634 result.appendFormat(" %s\n", msg.string());
2635 }
2636
2637 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
2638 result.append(" ...\n");
2639 } else if (mEventLog.size() == 0) {
2640 result.append(" [no events yet]\n");
2641 }
2642 result.append("\n");
2643
2644 write(fd, result.string(), result.size());
2645}
2646
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002647void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002648 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002649 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2650 if (mTorchClientMap[i] == who) {
2651 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002652 String8 cameraId = mTorchClientMap.keyAt(i);
2653 status_t res = mFlashlight->setTorchMode(cameraId, false);
2654 if (res) {
2655 ALOGE("%s: torch client died but couldn't turn off torch: "
2656 "%s (%d)", __FUNCTION__, strerror(-res), res);
2657 return;
2658 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002659 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002660 break;
2661 }
2662 }
2663}
2664
Ruben Brunkcc776712015-02-17 20:18:47 -08002665/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002666
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002667 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002668 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2669 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002670 */
2671
Ruben Brunka8ca9152015-04-07 14:23:40 -07002672 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2673
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002674 // check torch client
2675 handleTorchClientBinderDied(who);
2676
2677 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002678 if(!evictClientIdByRemote(who)) {
2679 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002680 return;
2681 }
2682
Ruben Brunkcc776712015-02-17 20:18:47 -08002683 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2684 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002685}
2686
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002687void CameraService::updateStatus(int32_t status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002688 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002689}
2690
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002691void CameraService::updateStatus(int32_t status, const String8& cameraId,
2692 std::initializer_list<int32_t> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002693 // Do not lock mServiceLock here or can get into a deadlock from
2694 // connect() -> disconnect -> updateStatus
2695
2696 auto state = getCameraState(cameraId);
2697
2698 if (state == nullptr) {
2699 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2700 cameraId.string());
2701 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002702 }
2703
Ruben Brunkcc776712015-02-17 20:18:47 -08002704 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2705 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2706 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002707 (const String8& cameraId, int32_t status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002708
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002709 if (status != ICameraServiceListener::STATUS_ENUMERATING) {
2710 // Update torch status if it has a flash unit.
2711 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002712 int32_t torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002713 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2714 NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002715 int32_t newTorchStatus =
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002716 status == ICameraServiceListener::STATUS_PRESENT ?
2717 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF :
2718 ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
2719 if (torchStatus != newTorchStatus) {
2720 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2721 }
2722 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002723 }
2724
2725 Mutex::Autolock lock(mStatusListenerLock);
2726
2727 for (auto& listener : mListenerList) {
2728 // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
2729 int id = cameraIdToInt(cameraId);
2730 if (id != -1) listener->onStatusChanged(status, id);
2731 }
2732 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002733}
2734
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002735void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
2736 const String8& cameraId) {
2737 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2738 if (proxyBinder == nullptr) return;
2739 String16 id(cameraId);
2740 proxyBinder->notifyCameraState(id, newState);
2741}
2742
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002743status_t CameraService::getTorchStatusLocked(
2744 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002745 int32_t *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002746 if (!status) {
2747 return BAD_VALUE;
2748 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002749 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2750 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002751 // invalid camera ID or the camera doesn't have a flash unit
2752 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002753 }
2754
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002755 *status = mTorchStatusMap.valueAt(index);
2756 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002757}
2758
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002759status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002760 int32_t status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002761 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2762 if (index == NAME_NOT_FOUND) {
2763 return BAD_VALUE;
2764 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002765 int32_t& item =
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002766 mTorchStatusMap.editValueAt(index);
2767 item = status;
2768
2769 return OK;
2770}
2771
Mathias Agopian65ab4712010-07-14 17:59:35 -07002772}; // namespace android