blob: 0c88dad24960feda49c357df3bd58dde314fae31 [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
Ruben Brunkcc776712015-02-17 20:18:47 -0800105 cs->onDeviceStatusChanged(static_cast<camera_device_status_t>(camera_id),
106 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() {
Ruben Brunk2823ce02015-05-19 17:25:13 -0700274 sp<IServiceManager> sm = defaultServiceManager();
275 sp<IBinder> binder = sm->getService(String16("media.camera.proxy"));
276 if (binder == nullptr) {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700277 return nullptr;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700278 }
279 sp<ICameraServiceProxy> proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700280 return proxyBinder;
281}
282
283void CameraService::pingCameraServiceProxy() {
284 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
285 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700286 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700287}
288
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800290 if (mModule) {
291 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800292 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800293 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800294 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Ruben Brunkcc776712015-02-17 20:18:47 -0800295 gCameraService = nullptr;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296}
297
Ruben Brunkcc776712015-02-17 20:18:47 -0800298void CameraService::onDeviceStatusChanged(camera_device_status_t cameraId,
299 camera_device_status_t newStatus) {
300 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
Igor Murashkincba2c162013-03-20 15:56:31 -0700301 cameraId, newStatus);
302
Ruben Brunkcc776712015-02-17 20:18:47 -0800303 String8 id = String8::format("%d", cameraId);
304 std::shared_ptr<CameraState> state = getCameraState(id);
305
306 if (state == nullptr) {
Igor Murashkincba2c162013-03-20 15:56:31 -0700307 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
308 return;
309 }
310
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800311 int32_t oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800312
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800313 if (oldStatus == static_cast<int32_t>(newStatus)) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800314 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700315 return;
316 }
317
Igor Murashkincba2c162013-03-20 15:56:31 -0700318 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700319 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
320 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800321 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700322 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800323 // Don't do this in updateStatus to avoid deadlock over mServiceLock
324 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700325
Ruben Brunkcc776712015-02-17 20:18:47 -0800326 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
327 // to this device until the status changes
328 updateStatus(ICameraServiceListener::STATUS_NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700329
Ruben Brunkcc776712015-02-17 20:18:47 -0800330 // Remove cached shim parameters
331 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700332
Ruben Brunkcc776712015-02-17 20:18:47 -0800333 // Remove the client from the list of active clients
334 clientToDisconnect = removeClientLocked(id);
335
336 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800337 clientToDisconnect->notifyError(
338 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800339 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700340 }
341
Ruben Brunkcc776712015-02-17 20:18:47 -0800342 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
343 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700344
Ruben Brunkcc776712015-02-17 20:18:47 -0800345 // Disconnect client
346 if (clientToDisconnect.get() != nullptr) {
347 // Ensure not in binder RPC so client disconnect PID checks work correctly
348 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
349 "onDeviceStatusChanged must be called from the camera service process!");
350 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700351 }
352
Ruben Brunkcc776712015-02-17 20:18:47 -0800353 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800354 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700355 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
356 newStatus));
357 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800358 updateStatus(static_cast<int32_t>(newStatus), id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700359 }
360
Igor Murashkincba2c162013-03-20 15:56:31 -0700361}
362
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800363void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800364 int32_t newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800365 Mutex::Autolock al(mTorchStatusMutex);
366 onTorchStatusChangedLocked(cameraId, newStatus);
367}
368
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800369void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800370 int32_t newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800371 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
372 __FUNCTION__, cameraId.string(), newStatus);
373
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800374 int32_t status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800375 status_t res = getTorchStatusLocked(cameraId, &status);
376 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700377 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
378 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800379 return;
380 }
381 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800382 return;
383 }
384
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800385 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800386 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800387 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
388 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800389 return;
390 }
391
Ruben Brunkcc776712015-02-17 20:18:47 -0800392 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700393 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700394 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700395 auto iter = mTorchUidMap.find(cameraId);
396 if (iter != mTorchUidMap.end()) {
397 int oldUid = iter->second.second;
398 int newUid = iter->second.first;
399 BatteryNotifier& notifier(BatteryNotifier::getInstance());
400 if (oldUid != newUid) {
401 // If the UID has changed, log the status and update current UID in mTorchUidMap
402 if (status == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
403 notifier.noteFlashlightOff(cameraId, oldUid);
404 }
405 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
406 notifier.noteFlashlightOn(cameraId, newUid);
407 }
408 iter->second.second = newUid;
409 } else {
410 // If the UID has not changed, log the status
411 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
412 notifier.noteFlashlightOn(cameraId, oldUid);
413 } else {
414 notifier.noteFlashlightOff(cameraId, oldUid);
415 }
416 }
417 }
418 }
419
420 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800421 Mutex::Autolock lock(mStatusListenerLock);
422 for (auto& i : mListenerList) {
423 i->onTorchStatusChanged(newStatus, String16{cameraId});
424 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800425 }
426}
427
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800428Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700429 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700430 switch (type) {
431 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800432 *numCameras = mNumberOfNormalCameras;
433 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700434 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800435 *numCameras = mNumberOfCameras;
436 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700437 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800438 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700439 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800440 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
441 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700442 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800443 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700444}
445
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800446Status CameraService::getCameraInfo(int cameraId,
447 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700448 ATRACE_CALL();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700449 if (!mModule) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800450 return STATUS_ERROR(ERROR_DISCONNECTED,
451 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700452 }
453
Mathias Agopian65ab4712010-07-14 17:59:35 -0700454 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800455 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
456 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 }
458
Iliyan Malchev8951a972011-04-14 16:55:59 -0700459 struct camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800460 Status rc = filterGetInfoErrorCode(
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800461 mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800462
463 if (rc.isOk()) {
464 cameraInfo->facing = info.facing;
465 cameraInfo->orientation = info.orientation;
466 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700467 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468}
469
Ruben Brunkcc776712015-02-17 20:18:47 -0800470int CameraService::cameraIdToInt(const String8& cameraId) {
471 errno = 0;
472 size_t pos = 0;
473 int ret = stoi(std::string{cameraId.string()}, &pos);
474 if (errno != 0 || pos != cameraId.size()) {
475 return -1;
476 }
477 return ret;
478}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700479
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800480Status CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700481 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800482
483 Status ret = Status::ok();
484
Ruben Brunkb2119af2014-05-09 19:57:56 -0700485 struct CameraInfo info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800486 if (!(ret = getCameraInfo(cameraId, &info)).isOk()) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700487 return ret;
488 }
489
490 CameraMetadata shimInfo;
491 int32_t orientation = static_cast<int32_t>(info.orientation);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800492 status_t rc;
493 if ((rc = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) {
494 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
495 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700496 }
497
498 uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ?
499 ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800500 if ((rc = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) {
501 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
502 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700503 }
504
Igor Murashkin65d14b92014-06-17 12:03:20 -0700505 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800506 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -0700507 // Error logged by callee
508 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700509 }
510
511 Vector<Size> sizes;
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700512 Vector<Size> jpegSizes;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700513 Vector<int32_t> formats;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700514 {
515 shimParams.getSupportedPreviewSizes(/*out*/sizes);
516 shimParams.getSupportedPreviewFormats(/*out*/formats);
517 shimParams.getSupportedPictureSizes(/*out*/jpegSizes);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700518 }
519
520 // Always include IMPLEMENTATION_DEFINED
521 formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
522
523 const size_t INTS_PER_CONFIG = 4;
524
525 // Build available stream configurations metadata
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700526 size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG;
527
528 Vector<int32_t> streamConfigs;
529 streamConfigs.setCapacity(streamConfigSize);
530
Ruben Brunkb2119af2014-05-09 19:57:56 -0700531 for (size_t i = 0; i < formats.size(); ++i) {
532 for (size_t j = 0; j < sizes.size(); ++j) {
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700533 streamConfigs.add(formats[i]);
534 streamConfigs.add(sizes[j].width);
535 streamConfigs.add(sizes[j].height);
536 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700537 }
538 }
539
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700540 for (size_t i = 0; i < jpegSizes.size(); ++i) {
541 streamConfigs.add(HAL_PIXEL_FORMAT_BLOB);
542 streamConfigs.add(jpegSizes[i].width);
543 streamConfigs.add(jpegSizes[i].height);
544 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
545 }
546
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800547 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700548 streamConfigs.array(), streamConfigSize)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800549 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
550 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700551 }
552
553 int64_t fakeMinFrames[0];
554 // TODO: Fixme, don't fake min frame durations.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800555 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
Ruben Brunkb2119af2014-05-09 19:57:56 -0700556 fakeMinFrames, 0)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800557 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
558 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700559 }
560
561 int64_t fakeStalls[0];
562 // TODO: Fixme, don't fake stall durations.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800563 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
Ruben Brunkb2119af2014-05-09 19:57:56 -0700564 fakeStalls, 0)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800565 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
566 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700567 }
568
569 *cameraInfo = shimInfo;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800570 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700571}
572
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800573Status CameraService::getCameraCharacteristics(int cameraId,
Zhijun He2b59be82013-09-25 10:14:30 -0700574 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700575 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700576 if (!cameraInfo) {
577 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800578 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700579 }
580
581 if (!mModule) {
582 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800583 return STATUS_ERROR(ERROR_DISCONNECTED,
584 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700585 }
586
Zhijun He2b59be82013-09-25 10:14:30 -0700587 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
588 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800589 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
590 "Invalid camera id: %d", cameraId);
Zhijun He2b59be82013-09-25 10:14:30 -0700591 }
592
593 int facing;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800594 Status ret;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800595 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0 ||
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800596 getDeviceVersion(cameraId, &facing) < CAMERA_DEVICE_API_VERSION_3_0) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700597 /**
598 * Backwards compatibility mode for old HALs:
599 * - Convert CameraInfo into static CameraMetadata properties.
600 * - Retrieve cached CameraParameters for this camera. If none exist,
601 * attempt to open CameraClient and retrieve the CameraParameters.
602 * - Convert cached CameraParameters into static CameraMetadata
603 * properties.
604 */
605 ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__);
Zhijun He2b59be82013-09-25 10:14:30 -0700606
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800607 ret = generateShimMetadata(cameraId, cameraInfo);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700608 } else {
609 /**
610 * Normal HAL 2.1+ codepath.
611 */
612 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800613 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800614 if (ret.isOk()) {
615 *cameraInfo = info.static_camera_characteristics;
616 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700617 }
Zhijun He2b59be82013-09-25 10:14:30 -0700618
619 return ret;
620}
621
Ruben Brunkcc776712015-02-17 20:18:47 -0800622int CameraService::getCallingPid() {
623 return IPCThreadState::self()->getCallingPid();
624}
625
626int CameraService::getCallingUid() {
627 return IPCThreadState::self()->getCallingUid();
628}
629
630String8 CameraService::getFormattedCurrentTime() {
631 time_t now = time(nullptr);
632 char formattedTime[64];
633 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
634 return String8(formattedTime);
635}
636
637int CameraService::getCameraPriorityFromProcState(int procState) {
638 // Find the priority for the camera usage based on the process state. Higher priority clients
639 // win for evictions.
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700640 if (procState < 0) {
641 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
642 procState);
643 return -1;
Ruben Brunkcc776712015-02-17 20:18:47 -0800644 }
Eino-Ville Talvala52aad852015-09-03 12:24:24 -0700645 // Treat sleeping TOP processes the same as regular TOP processes, for
646 // access priority. This is important for lock-screen camera launch scenarios
647 if (procState == PROCESS_STATE_TOP_SLEEPING) {
648 procState = PROCESS_STATE_TOP;
649 }
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700650 return INT_MAX - procState;
Ruben Brunkcc776712015-02-17 20:18:47 -0800651}
652
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800653Status CameraService::getCameraVendorTagDescriptor(
654 /*out*/
655 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700656 ATRACE_CALL();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800657 if (!mModule) {
658 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800659 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800660 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800661 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
662 if (globalDescriptor != nullptr) {
663 *desc = *(globalDescriptor.get());
664 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800665 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800666}
667
Igor Murashkin634a5152013-02-20 17:15:11 -0800668int CameraService::getDeviceVersion(int cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700669 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800670 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800671 if (mModule->getCameraInfo(cameraId, &info) != OK) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800672 return -1;
673 }
674
675 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800676 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800677 deviceVersion = info.device_version;
678 } else {
679 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
680 }
681
682 if (facing) {
683 *facing = info.facing;
684 }
685
686 return deviceVersion;
687}
688
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800689Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700690 switch(err) {
691 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800692 return Status::ok();
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700693 case -EINVAL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800694 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
695 "CameraId is not valid for HAL module");
696 case -ENODEV:
697 return STATUS_ERROR(ERROR_DISCONNECTED,
698 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700699 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800700 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
701 "Camera HAL encountered error %d: %s",
702 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700703 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800704}
705
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800706bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700707 ATRACE_CALL();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800708 vendor_tag_ops_t vOps = vendor_tag_ops_t();
709
710 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800711 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800712 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
713 return false;
714 }
715
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800716 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800717
718 // Ensure all vendor operations are present
719 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
720 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
721 vOps.get_tag_type == NULL) {
722 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
723 , __FUNCTION__);
724 return false;
725 }
726
727 // Read all vendor tag definitions into a descriptor
728 sp<VendorTagDescriptor> desc;
729 status_t res;
730 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
731 != OK) {
732 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
733 "received error %s (%d). Camera clients will not be able to use"
734 "vendor tags", __FUNCTION__, strerror(res), res);
735 return false;
736 }
737
738 // Set the global descriptor to use with camera metadata
739 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
740 return true;
741}
742
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800743Status CameraService::makeClient(const sp<CameraService>& cameraService,
744 const sp<IInterface>& cameraCb, const String16& packageName, int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800745 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
746 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
747 /*out*/sp<BasicClient>* client) {
748
Ruben Brunkcc776712015-02-17 20:18:47 -0800749 if (halVersion < 0 || halVersion == deviceVersion) {
750 // Default path: HAL version is unspecified by caller, create CameraClient
751 // based on device version reported by the HAL.
752 switch(deviceVersion) {
753 case CAMERA_DEVICE_API_VERSION_1_0:
754 if (effectiveApiLevel == API_1) { // Camera1 API route
755 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800756 *client = new CameraClient(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800757 clientPid, clientUid, getpid(), legacyMode);
758 } else { // Camera2 API route
759 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800760 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
761 "Camera device \"%d\" HAL version %d does not support camera2 API",
762 cameraId, deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800763 }
764 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800765 case CAMERA_DEVICE_API_VERSION_3_0:
766 case CAMERA_DEVICE_API_VERSION_3_1:
767 case CAMERA_DEVICE_API_VERSION_3_2:
768 case CAMERA_DEVICE_API_VERSION_3_3:
769 if (effectiveApiLevel == API_1) { // Camera1 API route
770 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800771 *client = new Camera2Client(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800772 clientPid, clientUid, servicePid, legacyMode);
773 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800774 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
775 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
776 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800777 facing, clientPid, clientUid, servicePid);
778 }
779 break;
780 default:
781 // Should not be reachable
782 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800783 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
784 "Camera device \"%d\" has unknown HAL version %d",
785 cameraId, deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800786 }
787 } else {
788 // A particular HAL version is requested by caller. Create CameraClient
789 // based on the requested HAL version.
790 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
791 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
792 // Only support higher HAL version device opened as HAL1.0 device.
793 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800794 *client = new CameraClient(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800795 clientPid, clientUid, servicePid, legacyMode);
796 } else {
797 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
798 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
799 " opened as HAL %x device", halVersion, deviceVersion,
800 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800801 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
802 "Camera device \"%d\" (HAL version %d) cannot be opened as HAL version %d",
803 cameraId, deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800804 }
805 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800806 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800807}
808
Ruben Brunk6267b532015-04-30 17:44:07 -0700809String8 CameraService::toString(std::set<userid_t> intSet) {
810 String8 s("");
811 bool first = true;
812 for (userid_t i : intSet) {
813 if (first) {
814 s.appendFormat("%d", i);
815 first = false;
816 } else {
817 s.appendFormat(", %d", i);
818 }
819 }
820 return s;
821}
822
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800823Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800824 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700825
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800826 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800827 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800828 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800829 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800830 if (!(ret = connectHelper<ICameraClient,Client>(
831 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
832 internalPackageName, uid, USE_CALLING_PID,
833 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
834 /*out*/ tmp)
835 ).isOk()) {
836 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700837 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800838 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700839}
840
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800841Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700842 /*out*/
843 CameraParameters* parameters) {
844
845 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
846
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800847 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700848
849 if (parameters == NULL) {
850 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800851 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700852 }
853
Ruben Brunkcc776712015-02-17 20:18:47 -0800854 String8 id = String8::format("%d", cameraId);
855
856 // Check if we already have parameters
857 {
858 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700859 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800860 auto cameraState = getCameraState(id);
861 if (cameraState == nullptr) {
862 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800863 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
864 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800865 }
866 CameraParameters p = cameraState->getShimParams();
867 if (!p.isEmpty()) {
868 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800869 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700870 }
871 }
872
Ruben Brunkcc776712015-02-17 20:18:47 -0800873 int64_t token = IPCThreadState::self()->clearCallingIdentity();
874 ret = initializeShimMetadata(cameraId);
875 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800876 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800877 // Error already logged by callee
878 return ret;
879 }
880
881 // Check for parameters again
882 {
883 // Scope for service lock
884 Mutex::Autolock lock(mServiceLock);
885 auto cameraState = getCameraState(id);
886 if (cameraState == nullptr) {
887 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800888 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
889 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700890 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800891 CameraParameters p = cameraState->getShimParams();
892 if (!p.isEmpty()) {
893 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800894 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700895 }
896 }
897
Ruben Brunkcc776712015-02-17 20:18:47 -0800898 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
899 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800900 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700901}
902
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800903// Can camera service trust the caller based on the calling UID?
904static bool isTrustedCallingUid(uid_t uid) {
905 switch (uid) {
906 case AID_MEDIA: // mediaserver
907 case AID_CAMERASERVER: // cameraserver
908 return true;
909 default:
910 return false;
911 }
912}
913
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800914Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700915 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
916 /*out*/int& originalClientPid) const {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800917
Mathias Agopian65ab4712010-07-14 17:59:35 -0700918 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800919 int callingUid = getCallingUid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500920
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800921 // Check if we can trust clientUid
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800922 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800923 clientUid = callingUid;
924 } else if (!isTrustedCallingUid(callingUid)) {
925 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
926 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800927 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
928 "Untrusted caller (calling PID %d, UID %d) trying to "
929 "forward camera access to camera %s for client %s (PID %d, UID %d)",
930 callingPid, callingUid, cameraId.string(),
931 clientName8.string(), clientUid, clientPid);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800932 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700933
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800934 // Check if we can trust clientPid
935 if (clientPid == USE_CALLING_PID) {
936 clientPid = callingPid;
937 } else if (!isTrustedCallingUid(callingUid)) {
938 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
939 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800940 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
941 "Untrusted caller (calling PID %d, UID %d) trying to "
942 "forward camera access to camera %s for client %s (PID %d, UID %d)",
943 callingPid, callingUid, cameraId.string(),
944 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800945 }
946
947 // If it's not calling from cameraserver, check the permission.
948 if (callingPid != getpid() &&
949 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
950 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800951 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
952 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
953 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800954 }
955
956 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
957 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700958 originalClientPid = clientPid;
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800959 clientPid = callingPid;
960
Iliyan Malchev8951a972011-04-14 16:55:59 -0700961 if (!mModule) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800962 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
963 callingPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800964 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
965 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700966 }
967
Ruben Brunkcc776712015-02-17 20:18:47 -0800968 if (getCameraState(cameraId) == nullptr) {
969 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
970 cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800971 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
972 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 }
974
Ruben Brunk6267b532015-04-30 17:44:07 -0700975 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800976
Ruben Brunka8ca9152015-04-07 14:23:40 -0700977 // Only allow clients who are being used by the current foreground device user, unless calling
978 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -0700979 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
980 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
981 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
982 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800983 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
984 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
985 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -0700986 }
987
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800988 status_t err = checkIfDeviceIsUsable(cameraId);
989 if (err != NO_ERROR) {
990 switch(err) {
991 case -ENODEV:
992 case -EBUSY:
993 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
994 "No camera device with ID \"%s\" currently available", cameraId.string());
995 default:
996 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
997 "Unknown error connecting to ID \"%s\"", cameraId.string());
998 }
999 }
1000 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001001}
1002
1003status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1004 auto cameraState = getCameraState(cameraId);
1005 int callingPid = getCallingPid();
1006 if (cameraState == nullptr) {
1007 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1008 cameraId.string());
1009 return -ENODEV;
1010 }
1011
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001012 int32_t currentStatus = cameraState->getStatus();
Igor Murashkincba2c162013-03-20 15:56:31 -07001013 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001014 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1015 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001016 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -07001017 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001018 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1019 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001020 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001021 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001022
Ruben Brunkcc776712015-02-17 20:18:47 -08001023 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001024}
1025
Ruben Brunkcc776712015-02-17 20:18:47 -08001026void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1027 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001028
Ruben Brunkcc776712015-02-17 20:18:47 -08001029 // Make a descriptor for the incoming client
1030 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1031 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1032
1033 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1034 String8(client->getPackageName()));
1035
1036 if (evicted.size() > 0) {
1037 // This should never happen - clients should already have been removed in disconnect
1038 for (auto& i : evicted) {
1039 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1040 __FUNCTION__, i->getKey().string());
1041 }
1042
1043 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1044 __FUNCTION__);
1045 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001046
1047 // And register a death notification for the client callback. Do
1048 // this last to avoid Binder policy where a nested Binder
1049 // transaction might be pre-empted to service the client death
1050 // notification if the client process dies before linkToDeath is
1051 // invoked.
1052 sp<IBinder> remoteCallback = client->getRemote();
1053 if (remoteCallback != nullptr) {
1054 remoteCallback->linkToDeath(this);
1055 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001056}
1057
1058status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1059 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1060 /*out*/
1061 sp<BasicClient>* client,
1062 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001063 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001064 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001065 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001066 DescriptorPtr clientDescriptor;
1067 {
1068 if (effectiveApiLevel == API_1) {
1069 // If we are using API1, any existing client for this camera ID with the same remote
1070 // should be returned rather than evicted to allow MediaRecorder to work properly.
1071
1072 auto current = mActiveClientManager.get(cameraId);
1073 if (current != nullptr) {
1074 auto clientSp = current->getValue();
1075 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001076 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1077 ALOGW("CameraService connect called from same client, but with a different"
1078 " API level, evicting prior client...");
1079 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001080 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001081 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001082 *client = clientSp;
1083 return NO_ERROR;
1084 }
1085 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001086 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001087 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001088
Ruben Brunkcc776712015-02-17 20:18:47 -08001089 // Get current active client PIDs
1090 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1091 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001092
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001093 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1094 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1095 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1096 // not have an out-of-class definition.
1097 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001098
Ruben Brunkcc776712015-02-17 20:18:47 -08001099 // Get priorites of all active PIDs
1100 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1101 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001102
Ruben Brunkcc776712015-02-17 20:18:47 -08001103 // Update all active clients' priorities
1104 std::map<int,int> pidToPriorityMap;
1105 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1106 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1107 }
1108 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001109
Ruben Brunkcc776712015-02-17 20:18:47 -08001110 // Get state for the given cameraId
1111 auto state = getCameraState(cameraId);
1112 if (state == nullptr) {
1113 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1114 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001115 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001116 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001117 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001118
1119 // Make descriptor for incoming client
1120 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1121 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1122 state->getConflicting(),
1123 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1124
1125 // Find clients that would be evicted
1126 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1127
1128 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1129 // background, so we cannot do evictions
1130 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1131 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1132 " priority).", clientPid);
1133
1134 sp<BasicClient> clientSp = clientDescriptor->getValue();
1135 String8 curTime = getFormattedCurrentTime();
1136 auto incompatibleClients =
1137 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1138
1139 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001140 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001141 cameraId.string(), packageName.string(), clientPid,
1142 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1143
1144 for (auto& i : incompatibleClients) {
1145 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1146 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1147 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1148 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001149 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1150 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1151 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1152 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001153 }
1154
1155 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001156 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001157 mEventLog.add(msg);
1158
1159 return -EBUSY;
1160 }
1161
1162 for (auto& i : evicted) {
1163 sp<BasicClient> clientSp = i->getValue();
1164 if (clientSp.get() == nullptr) {
1165 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1166
1167 // TODO: Remove this
1168 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1169 __FUNCTION__);
1170 mActiveClientManager.remove(i);
1171 continue;
1172 }
1173
1174 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1175 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001176 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001177
Ruben Brunkcc776712015-02-17 20:18:47 -08001178 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001179 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1180 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1181 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001182 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1183 i->getOwnerId(), i->getPriority(), cameraId.string(),
1184 packageName.string(), clientPid,
1185 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1186
1187 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001188 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001189 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001190 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001191 }
1192
Ruben Brunkcc776712015-02-17 20:18:47 -08001193 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1194 // other clients from connecting in mServiceLockWrapper if held
1195 mServiceLock.unlock();
1196
1197 // Clear caller identity temporarily so client disconnect PID checks work correctly
1198 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1199
1200 // Destroy evicted clients
1201 for (auto& i : evictedClients) {
1202 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001203 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001204 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001205
1206 IPCThreadState::self()->restoreCallingIdentity(token);
1207
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001208 for (const auto& i : evictedClients) {
1209 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1210 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1211 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1212 if (ret == TIMED_OUT) {
1213 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1214 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1215 mActiveClientManager.toString().string());
1216 return -EBUSY;
1217 }
1218 if (ret != NO_ERROR) {
1219 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1220 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1221 ret, mActiveClientManager.toString().string());
1222 return ret;
1223 }
1224 }
1225
1226 evictedClients.clear();
1227
Ruben Brunkcc776712015-02-17 20:18:47 -08001228 // Once clients have been disconnected, relock
1229 mServiceLock.lock();
1230
1231 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1232 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1233 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001234 }
1235
Ruben Brunkcc776712015-02-17 20:18:47 -08001236 *partial = clientDescriptor;
1237 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001238}
1239
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001240Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001241 const sp<ICameraClient>& cameraClient,
1242 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001243 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001244 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001245 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001246 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001247 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001248
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001249 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001250 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001251 String8 id = String8::format("%d", cameraId);
1252 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001253 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1254 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1255 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1256 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001257
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001258 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001259 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001260 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001261 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 }
1263
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001264 *device = client;
1265 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001266}
1267
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001268Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001269 const sp<ICameraClient>& cameraClient,
1270 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001271 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001272 int clientUid,
1273 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001274 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001275
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001276 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001277 String8 id = String8::format("%d", cameraId);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08001278 int apiVersion = mModule->getModuleApiVersion();
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001279 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001280 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001281 /*
1282 * Either the HAL version is unspecified in which case this just creates
1283 * a camera client selected by the latest device version, or
1284 * it's a particular version in which case the HAL must supported
1285 * the open_legacy call
1286 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001287 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1288 apiVersion);
1289 ALOGE("%s: %s",
1290 __FUNCTION__, msg.string());
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001291 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001292 msg);
1293 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001294 }
1295
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001296 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001297 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001298 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1299 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1300 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1301 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001302
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001303 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001304 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001305 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001306 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001307 }
1308
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001309 *device = client;
1310 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001311}
1312
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001313Status CameraService::connectDevice(
1314 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Ruben Brunkcc776712015-02-17 20:18:47 -08001315 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001316 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001317 int clientUid,
1318 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001319 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001320
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001321 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001322 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001323 String8 id = String8::format("%d", cameraId);
1324 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001325 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1326 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1327 clientUid, USE_CALLING_PID, API_2,
1328 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1329 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001330
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001331 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001332 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001333 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001334 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001335 }
1336
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001337 *device = client;
1338 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001339}
1340
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001341Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001342 const sp<IBinder>& clientBinder) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001343
1344 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001345 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001346 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001347 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1348 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001349 }
1350
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001351 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001352 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001353
1354 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001355 auto state = getCameraState(id);
1356 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001357 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001358 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1359 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001360 }
1361
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001362 int32_t cameraStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -08001363 if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
1364 cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001365 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001366 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1367 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001368 }
1369
1370 {
1371 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001372 int32_t status;
1373 status_t err = getTorchStatusLocked(id, &status);
1374 if (err != OK) {
1375 if (err == NAME_NOT_FOUND) {
1376 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1377 "Camera \"%s\" does not have a flash unit", id.string());
1378 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001379 ALOGE("%s: getting current torch status failed for camera %s",
1380 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001381 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1382 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1383 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001384 }
1385
1386 if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001387 if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001388 ALOGE("%s: torch mode of camera %s is not available because "
1389 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001390 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1391 "Torch for camera \"%s\" is not available due to an existing camera user",
1392 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001393 } else {
1394 ALOGE("%s: torch mode of camera %s is not available due to "
1395 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001396 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1397 "Torch for camera \"%s\" is not available due to insufficient resources",
1398 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001399 }
1400 }
1401 }
1402
Ruben Brunk99e69712015-05-26 17:25:07 -07001403 {
1404 // Update UID map - this is used in the torch status changed callbacks, so must be done
1405 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001406 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001407 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1408 mTorchUidMap[id].first = uid;
1409 mTorchUidMap[id].second = uid;
1410 } else {
1411 // Set the pending UID
1412 mTorchUidMap[id].first = uid;
1413 }
1414 }
1415
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001416 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001417
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001418 if (err != OK) {
1419 int32_t errorCode;
1420 String8 msg;
1421 switch (err) {
1422 case -ENOSYS:
1423 msg = String8::format("Camera \"%s\" has no flashlight",
1424 id.string());
1425 errorCode = ERROR_ILLEGAL_ARGUMENT;
1426 break;
1427 default:
1428 msg = String8::format(
1429 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1430 id.string(), enabled, strerror(-err), err);
1431 errorCode = ERROR_INVALID_OPERATION;
1432 }
1433 ALOGE("%s: %s", __FUNCTION__, msg.string());
1434 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001435 }
1436
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001437 {
1438 // update the link to client's death
1439 Mutex::Autolock al(mTorchClientMapMutex);
1440 ssize_t index = mTorchClientMap.indexOfKey(id);
1441 if (enabled) {
1442 if (index == NAME_NOT_FOUND) {
1443 mTorchClientMap.add(id, clientBinder);
1444 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001445 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001446 mTorchClientMap.replaceValueAt(index, clientBinder);
1447 }
1448 clientBinder->linkToDeath(this);
1449 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001450 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001451 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001452 }
1453
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001454 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001455}
1456
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001457Status CameraService::notifySystemEvent(int32_t eventId,
1458 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001459 ATRACE_CALL();
1460
Ruben Brunk36597b22015-03-20 22:15:57 -07001461 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001462 case ICameraService::EVENT_USER_SWITCHED: {
1463 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001464 break;
1465 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001466 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001467 default: {
1468 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1469 eventId);
1470 break;
1471 }
1472 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001473 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001474}
1475
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001476Status CameraService::addListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001477 ATRACE_CALL();
1478
Igor Murashkinbfc99152013-02-27 12:55:20 -08001479 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001480
Ruben Brunk3450ba72015-06-16 11:00:37 -07001481 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001482 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001483 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001484 }
1485
Igor Murashkinbfc99152013-02-27 12:55:20 -08001486 Mutex::Autolock lock(mServiceLock);
1487
Ruben Brunkcc776712015-02-17 20:18:47 -08001488 {
1489 Mutex::Autolock lock(mStatusListenerLock);
1490 for (auto& it : mListenerList) {
1491 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1492 ALOGW("%s: Tried to add listener %p which was already subscribed",
1493 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001494 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001495 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001496 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001497
1498 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001499 }
1500
Igor Murashkinbfc99152013-02-27 12:55:20 -08001501
Igor Murashkincba2c162013-03-20 15:56:31 -07001502 /* Immediately signal current status to this listener only */
1503 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001504 Mutex::Autolock lock(mCameraStatesLock);
1505 for (auto& i : mCameraStates) {
1506 // TODO: Update binder to use String16 for camera IDs and remove;
1507 int id = cameraIdToInt(i.first);
1508 if (id == -1) continue;
1509
1510 listener->onStatusChanged(i.second->getStatus(), id);
Igor Murashkincba2c162013-03-20 15:56:31 -07001511 }
1512 }
1513
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001514 /* Immediately signal current torch status to this listener only */
1515 {
1516 Mutex::Autolock al(mTorchStatusMutex);
1517 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001518 String16 id = String16(mTorchStatusMap.keyAt(i).string());
1519 listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001520 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001521 }
1522
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001523 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001524}
Ruben Brunkcc776712015-02-17 20:18:47 -08001525
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001526Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001527 ATRACE_CALL();
1528
Igor Murashkinbfc99152013-02-27 12:55:20 -08001529 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1530
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001531 if (listener == 0) {
1532 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001533 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001534 }
1535
Igor Murashkinbfc99152013-02-27 12:55:20 -08001536 Mutex::Autolock lock(mServiceLock);
1537
Ruben Brunkcc776712015-02-17 20:18:47 -08001538 {
1539 Mutex::Autolock lock(mStatusListenerLock);
1540 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1541 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1542 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001543 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001544 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001545 }
1546 }
1547
1548 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1549 __FUNCTION__, listener.get());
1550
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001551 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001552}
1553
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001554Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001555
1556 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001557 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1558
1559 if (parameters == NULL) {
1560 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001561 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001562 }
1563
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001564 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001565
1566 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001567 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001568 // Error logged by caller
1569 return ret;
1570 }
1571
1572 String8 shimParamsString8 = shimParams.flatten();
1573 String16 shimParamsString16 = String16(shimParamsString8);
1574
1575 *parameters = shimParamsString16;
1576
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001577 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001578}
1579
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001580Status CameraService::supportsCameraApi(int cameraId, int apiVersion, bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001581 ATRACE_CALL();
1582
Igor Murashkin65d14b92014-06-17 12:03:20 -07001583 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1584
1585 switch (apiVersion) {
1586 case API_VERSION_1:
1587 case API_VERSION_2:
1588 break;
1589 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001590 String8 msg = String8::format("Unknown API version %d", apiVersion);
1591 ALOGE("%s: %s", __FUNCTION__, msg.string());
1592 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001593 }
1594
1595 int facing = -1;
1596 int deviceVersion = getDeviceVersion(cameraId, &facing);
1597
1598 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001599 case CAMERA_DEVICE_API_VERSION_1_0:
1600 case CAMERA_DEVICE_API_VERSION_3_0:
1601 case CAMERA_DEVICE_API_VERSION_3_1:
1602 if (apiVersion == API_VERSION_2) {
1603 ALOGV("%s: Camera id %d uses HAL version %d <3.2, doesn't support api2 without shim",
1604 __FUNCTION__, cameraId, deviceVersion);
1605 *isSupported = false;
1606 } else { // if (apiVersion == API_VERSION_1) {
1607 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported",
1608 __FUNCTION__, cameraId);
1609 *isSupported = true;
1610 }
1611 break;
1612 case CAMERA_DEVICE_API_VERSION_3_2:
1613 case CAMERA_DEVICE_API_VERSION_3_3:
1614 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
Igor Murashkin65d14b92014-06-17 12:03:20 -07001615 __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001616 *isSupported = true;
1617 break;
1618 case -1: {
1619 String8 msg = String8::format("Unknown camera ID %d", cameraId);
1620 ALOGE("%s: %s", __FUNCTION__, msg.string());
1621 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001622 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001623 default: {
1624 String8 msg = String8::format("Unknown device version %d for device %d",
1625 deviceVersion, cameraId);
1626 ALOGE("%s: %s", __FUNCTION__, msg.string());
1627 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1628 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001629 }
1630
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001631 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001632}
1633
Ruben Brunkcc776712015-02-17 20:18:47 -08001634void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001635 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001636 for (auto& i : mActiveClientManager.getAll()) {
1637 auto clientSp = i->getValue();
1638 if (clientSp.get() == client) {
1639 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001640 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001641 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001642}
1643
Ruben Brunkcc776712015-02-17 20:18:47 -08001644bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1645 const int callingPid = getCallingPid();
1646 const int servicePid = getpid();
1647 bool ret = false;
1648 {
1649 // Acquire mServiceLock and prevent other clients from connecting
1650 std::unique_ptr<AutoConditionLock> lock =
1651 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001652
Igor Murashkin634a5152013-02-20 17:15:11 -08001653
Ruben Brunkcc776712015-02-17 20:18:47 -08001654 std::vector<sp<BasicClient>> evicted;
1655 for (auto& i : mActiveClientManager.getAll()) {
1656 auto clientSp = i->getValue();
1657 if (clientSp.get() == nullptr) {
1658 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1659 mActiveClientManager.remove(i);
1660 continue;
1661 }
1662 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1663 callingPid == clientSp->getClientPid())) {
1664 mActiveClientManager.remove(i);
1665 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001666
Ruben Brunkcc776712015-02-17 20:18:47 -08001667 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001668 clientSp->notifyError(
1669 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001670 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001671 }
1672 }
1673
Ruben Brunkcc776712015-02-17 20:18:47 -08001674 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1675 // other clients from connecting in mServiceLockWrapper if held
1676 mServiceLock.unlock();
1677
Ruben Brunk36597b22015-03-20 22:15:57 -07001678 // Do not clear caller identity, remote caller should be client proccess
1679
Ruben Brunkcc776712015-02-17 20:18:47 -08001680 for (auto& i : evicted) {
1681 if (i.get() != nullptr) {
1682 i->disconnect();
1683 ret = true;
1684 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001685 }
1686
Ruben Brunkcc776712015-02-17 20:18:47 -08001687 // Reacquire mServiceLock
1688 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001689
Ruben Brunkcc776712015-02-17 20:18:47 -08001690 } // lock is destroyed, allow further connect calls
1691
1692 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001693}
1694
Igor Murashkinecf17e82012-10-02 16:05:11 -07001695
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001696/**
1697 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001698 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001699 */
1700int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001701 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1702 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1703 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1704 // Verify the device version is in the supported range
1705 switch (info.device_version) {
1706 case CAMERA_DEVICE_API_VERSION_1_0:
1707 case CAMERA_DEVICE_API_VERSION_3_0:
1708 case CAMERA_DEVICE_API_VERSION_3_1:
1709 case CAMERA_DEVICE_API_VERSION_3_2:
1710 case CAMERA_DEVICE_API_VERSION_3_3:
1711 // in support
1712 break;
1713 case CAMERA_DEVICE_API_VERSION_2_0:
1714 case CAMERA_DEVICE_API_VERSION_2_1:
1715 // no longer supported
1716 default:
1717 ALOGE("%s: Device %d has HAL version %x, which is not supported",
1718 __FUNCTION__, id, info.device_version);
1719 String8 msg = String8::format(
1720 "Unsupported device HAL version %x for device %d",
1721 info.device_version, id);
1722 logServiceError(msg.string(), NO_INIT);
1723 return NO_INIT;
1724 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001725 }
1726
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001727 // Assume all devices pre-v3.3 are backward-compatible
1728 bool isBackwardCompatible = true;
1729 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
1730 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
1731 isBackwardCompatible = false;
1732 status_t res;
1733 camera_metadata_ro_entry_t caps;
1734 res = find_camera_metadata_ro_entry(
1735 info.static_camera_characteristics,
1736 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1737 &caps);
1738 if (res != 0) {
1739 ALOGW("%s: Unable to find camera capabilities for camera device %d",
1740 __FUNCTION__, id);
1741 caps.count = 0;
1742 }
1743 for (size_t i = 0; i < caps.count; i++) {
1744 if (caps.data.u8[i] ==
1745 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
1746 isBackwardCompatible = true;
1747 break;
1748 }
1749 }
1750 }
1751
1752 if (!isBackwardCompatible) {
1753 mNumberOfNormalCameras--;
1754 *latestStrangeCameraId = id;
1755 } else {
1756 if (id > *latestStrangeCameraId) {
1757 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
1758 "This is not allowed due backward-compatibility requirements",
1759 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001760 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001761 mNumberOfCameras = 0;
1762 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001763 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001764 }
1765 }
1766 return OK;
1767}
1768
Ruben Brunkcc776712015-02-17 20:18:47 -08001769std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1770 const String8& cameraId) const {
1771 std::shared_ptr<CameraState> state;
1772 {
1773 Mutex::Autolock lock(mCameraStatesLock);
1774 auto iter = mCameraStates.find(cameraId);
1775 if (iter != mCameraStates.end()) {
1776 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001777 }
1778 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001779 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001780}
1781
Ruben Brunkcc776712015-02-17 20:18:47 -08001782sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1783 // Remove from active clients list
1784 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1785 if (clientDescriptorPtr == nullptr) {
1786 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1787 cameraId.string());
1788 return sp<BasicClient>{nullptr};
1789 }
1790
1791 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001792}
1793
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001794void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001795 // Acquire mServiceLock and prevent other clients from connecting
1796 std::unique_ptr<AutoConditionLock> lock =
1797 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1798
Ruben Brunk6267b532015-04-30 17:44:07 -07001799 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001800 for (size_t i = 0; i < newUserIds.size(); i++) {
1801 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001802 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001803 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07001804 return;
1805 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001806 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001807 }
1808
Ruben Brunka8ca9152015-04-07 14:23:40 -07001809
Ruben Brunk6267b532015-04-30 17:44:07 -07001810 if (newAllowedUsers == mAllowedUsers) {
1811 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1812 return;
1813 }
1814
1815 logUserSwitch(mAllowedUsers, newAllowedUsers);
1816
1817 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001818
1819 // Current user has switched, evict all current clients.
1820 std::vector<sp<BasicClient>> evicted;
1821 for (auto& i : mActiveClientManager.getAll()) {
1822 auto clientSp = i->getValue();
1823
1824 if (clientSp.get() == nullptr) {
1825 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1826 continue;
1827 }
1828
Ruben Brunk6267b532015-04-30 17:44:07 -07001829 // Don't evict clients that are still allowed.
1830 uid_t clientUid = clientSp->getClientUid();
1831 userid_t clientUserId = multiuser_get_user_id(clientUid);
1832 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1833 continue;
1834 }
1835
Ruben Brunk36597b22015-03-20 22:15:57 -07001836 evicted.push_back(clientSp);
1837
1838 String8 curTime = getFormattedCurrentTime();
1839
1840 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1841 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001842
Ruben Brunk36597b22015-03-20 22:15:57 -07001843 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001844 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07001845 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07001846 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1847 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001848
1849 }
1850
1851 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1852 // blocking other clients from connecting in mServiceLockWrapper if held.
1853 mServiceLock.unlock();
1854
1855 // Clear caller identity temporarily so client disconnect PID checks work correctly
1856 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1857
1858 for (auto& i : evicted) {
1859 i->disconnect();
1860 }
1861
1862 IPCThreadState::self()->restoreCallingIdentity(token);
1863
1864 // Reacquire mServiceLock
1865 mServiceLock.lock();
1866}
Ruben Brunkcc776712015-02-17 20:18:47 -08001867
Ruben Brunka8ca9152015-04-07 14:23:40 -07001868void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001869 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001870 Mutex::Autolock l(mLogLock);
1871 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001872}
1873
Ruben Brunka8ca9152015-04-07 14:23:40 -07001874void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001875 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001876 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001877 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001878 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001879}
1880
1881void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001882 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001883 // Log the clients evicted
1884 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001885 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001886}
1887
1888void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001889 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001890 // Log the client rejected
1891 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001892 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001893}
1894
Ruben Brunk6267b532015-04-30 17:44:07 -07001895void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1896 const std::set<userid_t>& newUserIds) {
1897 String8 newUsers = toString(newUserIds);
1898 String8 oldUsers = toString(oldUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001899 // Log the new and old users
Ruben Brunk6267b532015-04-30 17:44:07 -07001900 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
1901 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001902}
1903
1904void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1905 // Log the device removal
1906 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1907}
1908
1909void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1910 // Log the device removal
1911 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1912}
1913
1914void CameraService::logClientDied(int clientPid, const char* reason) {
1915 // Log the device removal
1916 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001917}
1918
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001919void CameraService::logServiceError(const char* msg, int errorCode) {
1920 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001921 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001922}
1923
Ruben Brunk36597b22015-03-20 22:15:57 -07001924status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1925 uint32_t flags) {
1926
1927 const int pid = getCallingPid();
1928 const int selfPid = getpid();
1929
Mathias Agopian65ab4712010-07-14 17:59:35 -07001930 // Permission checks
1931 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001932 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07001933 if (pid != selfPid) {
1934 // Ensure we're being called by system_server, or similar process with
1935 // permissions to notify the camera service about system events
1936 if (!checkCallingPermission(
1937 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1938 const int uid = getCallingUid();
1939 ALOGE("Permission Denial: cannot send updates to camera service about system"
1940 " events from pid=%d, uid=%d", pid, uid);
1941 return PERMISSION_DENIED;
1942 }
1943 }
1944 break;
1945 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001946 }
1947
1948 return BnCameraService::onTransact(code, data, reply, flags);
1949}
1950
Mathias Agopian65ab4712010-07-14 17:59:35 -07001951// We share the media players for shutter and recording sound for all clients.
1952// A reference count is kept to determine when we will actually release the
1953// media players.
1954
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001955MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001956 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001957 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001958 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959 mp->prepare();
1960 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001961 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001962 return NULL;
1963 }
1964 return mp;
1965}
1966
1967void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001968 ATRACE_CALL();
1969
Mathias Agopian65ab4712010-07-14 17:59:35 -07001970 Mutex::Autolock lock(mSoundLock);
1971 LOG1("CameraService::loadSound ref=%d", mSoundRef);
1972 if (mSoundRef++) return;
1973
1974 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07001975 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
1976 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001977}
1978
1979void CameraService::releaseSound() {
1980 Mutex::Autolock lock(mSoundLock);
1981 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
1982 if (--mSoundRef) return;
1983
1984 for (int i = 0; i < NUM_SOUNDS; i++) {
1985 if (mSoundPlayer[i] != 0) {
1986 mSoundPlayer[i]->disconnect();
1987 mSoundPlayer[i].clear();
1988 }
1989 }
1990}
1991
1992void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001993 ATRACE_CALL();
1994
Mathias Agopian65ab4712010-07-14 17:59:35 -07001995 LOG1("playSound(%d)", kind);
1996 Mutex::Autolock lock(mSoundLock);
1997 sp<MediaPlayer> player = mSoundPlayer[kind];
1998 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08001999 player->seekTo(0);
2000 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002001 }
2002}
2003
2004// ----------------------------------------------------------------------------
2005
2006CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002007 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002008 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002009 int cameraId, int cameraFacing,
2010 int clientPid, uid_t clientUid,
2011 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002012 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002013 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002014 clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002015 cameraId, cameraFacing,
2016 clientPid, clientUid,
2017 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08002018{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002019 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002020 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002021
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002022 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002025
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002026 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002027}
2028
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029// tear down the client
2030CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002031 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002032 mDestructionStarted = true;
2033
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -07002034 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002035 // unconditionally disconnect. function is idempotent
2036 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002037}
2038
Igor Murashkin634a5152013-02-20 17:15:11 -08002039CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002040 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002041 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002042 int cameraId, int cameraFacing,
2043 int clientPid, uid_t clientUid,
2044 int servicePid):
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002045 mClientPackageName(clientPackageName), mDisconnected(false)
Igor Murashkin634a5152013-02-20 17:15:11 -08002046{
2047 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002048 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -08002049 mCameraId = cameraId;
2050 mCameraFacing = cameraFacing;
2051 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002052 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -08002053 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002054 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002055 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002056
2057 // In some cases the calling code has no access to the package it runs under.
2058 // For example, NDK camera API.
2059 // In this case we will get the packages for the calling UID and pick the first one
2060 // for attributing the app op. This will work correctly for runtime permissions
2061 // as for legacy apps we will toggle the app op for all packages in the UID.
2062 // The caveat is that the operation may be attributed to the wrong package and
2063 // stats based on app ops may be slightly off.
2064 if (mClientPackageName.size() <= 0) {
2065 sp<IServiceManager> sm = defaultServiceManager();
2066 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2067 if (binder == 0) {
2068 ALOGE("Cannot get permission service");
2069 // Leave mClientPackageName unchanged (empty) and the further interaction
2070 // with camera will fail in BasicClient::startCameraOps
2071 return;
2072 }
2073
2074 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2075 Vector<String16> packages;
2076
2077 permCtrl->getPackagesForUid(mClientUid, packages);
2078
2079 if (packages.isEmpty()) {
2080 ALOGE("No packages for calling UID");
2081 // Leave mClientPackageName unchanged (empty) and the further interaction
2082 // with camera will fail in BasicClient::startCameraOps
2083 return;
2084 }
2085 mClientPackageName = packages[0];
2086 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002087}
2088
2089CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002090 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002091 mDestructionStarted = true;
2092}
2093
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002094binder::Status CameraService::BasicClient::disconnect() {
2095 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002096 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002097 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002098 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002099 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002100
2101 mCameraService->removeByClient(this);
2102 mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002103 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002104
2105 sp<IBinder> remote = getRemote();
2106 if (remote != nullptr) {
2107 remote->unlinkToDeath(mCameraService);
2108 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002109
2110 finishCameraOps();
Ruben Brunkcc776712015-02-17 20:18:47 -08002111 ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid);
2112
Igor Murashkincba2c162013-03-20 15:56:31 -07002113 // client shouldn't be able to call into us anymore
2114 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002115
2116 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002117}
2118
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002119status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2120 // No dumping of clients directly over Binder,
2121 // must go through CameraService::dump
2122 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2123 IPCThreadState::self()->getCallingUid(), NULL, 0);
2124 return OK;
2125}
2126
Ruben Brunkcc776712015-02-17 20:18:47 -08002127String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002128 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002129}
2130
2131
2132int CameraService::BasicClient::getClientPid() const {
2133 return mClientPid;
2134}
2135
Ruben Brunk6267b532015-04-30 17:44:07 -07002136uid_t CameraService::BasicClient::getClientUid() const {
2137 return mClientUid;
2138}
2139
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002140bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2141 // Defaults to API2.
2142 return level == API_2;
2143}
2144
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002145status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002146 ATRACE_CALL();
2147
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002148 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002149 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002150 mOpsCallback = new OpsCallback(this);
2151
Igor Murashkine6800ce2013-03-04 17:25:57 -08002152 {
2153 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002154 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002155 }
2156
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002157 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002158 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002159 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002160 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002161
Svetoslav28e8ef72015-05-11 19:21:31 -07002162 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002163 ALOGI("Camera %d: Access for \"%s\" has been revoked",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002164 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002165 return PERMISSION_DENIED;
2166 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002167
Svetoslav28e8ef72015-05-11 19:21:31 -07002168 if (res == AppOpsManager::MODE_IGNORED) {
2169 ALOGI("Camera %d: Access for \"%s\" has been restricted",
2170 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002171 // Return the same error as for device policy manager rejection
2172 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002173 }
2174
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002175 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002176
2177 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
2178 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
Ruben Brunkcc776712015-02-17 20:18:47 -08002179 String8::format("%d", mCameraId));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002180
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002181 // Transition device state to OPEN
2182 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2183 String8::format("%d", mCameraId));
2184
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002185 return OK;
2186}
2187
2188status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002189 ATRACE_CALL();
2190
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002191 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002192 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002193 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002194 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002195 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002196 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002197
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002198 std::initializer_list<int32_t> rejected = {ICameraServiceListener::STATUS_NOT_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002199 ICameraServiceListener::STATUS_ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002200
Ruben Brunkcc776712015-02-17 20:18:47 -08002201 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002202 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002203 String8::format("%d", mCameraId), rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002204
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002205 // Transition device state to CLOSED
2206 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2207 String8::format("%d", mCameraId));
2208
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002209 // Notify flashlight that a camera device is closed.
2210 mCameraService->mFlashlight->deviceClosed(
2211 String8::format("%d", mCameraId));
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002212 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002213 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002214 if (mOpsCallback != NULL) {
2215 mAppOpsManager.stopWatchingMode(mOpsCallback);
2216 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002217 mOpsCallback.clear();
2218
2219 return OK;
2220}
2221
2222void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002223 ATRACE_CALL();
2224
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002225 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002226 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002227
2228 if (op != AppOpsManager::OP_CAMERA) {
2229 ALOGW("Unexpected app ops notification received: %d", op);
2230 return;
2231 }
2232
2233 int32_t res;
2234 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002235 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002236 ALOGV("checkOp returns: %d, %s ", res,
2237 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2238 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2239 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2240 "UNKNOWN");
2241
2242 if (res != AppOpsManager::MODE_ALLOWED) {
2243 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
2244 myName.string());
2245 // Reset the client PID to allow server-initiated disconnect,
2246 // and to prevent further calls by client.
2247 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002248 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002249 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002250 disconnect();
2251 }
2252}
2253
Mathias Agopian65ab4712010-07-14 17:59:35 -07002254// ----------------------------------------------------------------------------
2255
Ruben Brunkcc776712015-02-17 20:18:47 -08002256// Provide client strong pointer for callbacks.
2257sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
2258 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
2259 auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId);
2260 if (clientDescriptor != nullptr) {
2261 return sp<Client>{
2262 static_cast<Client*>(clientDescriptor->getValue().get())};
2263 }
2264 return sp<Client>{nullptr};
Mathias Agopian65ab4712010-07-14 17:59:35 -07002265}
2266
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002267void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002268 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002269 (void) errorCode;
2270 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002271 if (mRemoteCallback != NULL) {
2272 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2273 } else {
2274 ALOGE("mRemoteCallback is NULL!!");
2275 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002276}
2277
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002278// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002279binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002280 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002281 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002282}
2283
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002284bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2285 return level == API_1;
2286}
2287
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002288CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2289 mClient(client) {
2290}
2291
2292void CameraService::Client::OpsCallback::opChanged(int32_t op,
2293 const String16& packageName) {
2294 sp<BasicClient> client = mClient.promote();
2295 if (client != NULL) {
2296 client->opChanged(op, packageName);
2297 }
2298}
2299
Mathias Agopian65ab4712010-07-14 17:59:35 -07002300// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002301// CameraState
2302// ----------------------------------------------------------------------------
2303
2304CameraService::CameraState::CameraState(const String8& id, int cost,
2305 const std::set<String8>& conflicting) : mId(id),
2306 mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {}
2307
2308CameraService::CameraState::~CameraState() {}
2309
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002310int32_t CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002311 Mutex::Autolock lock(mStatusLock);
2312 return mStatus;
2313}
2314
2315CameraParameters CameraService::CameraState::getShimParams() const {
2316 return mShimParams;
2317}
2318
2319void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2320 mShimParams = params;
2321}
2322
2323int CameraService::CameraState::getCost() const {
2324 return mCost;
2325}
2326
2327std::set<String8> CameraService::CameraState::getConflicting() const {
2328 return mConflicting;
2329}
2330
2331String8 CameraService::CameraState::getId() const {
2332 return mId;
2333}
2334
2335// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002336// ClientEventListener
2337// ----------------------------------------------------------------------------
2338
2339void CameraService::ClientEventListener::onClientAdded(
2340 const resource_policy::ClientDescriptor<String8,
2341 sp<CameraService::BasicClient>>& descriptor) {
2342 auto basicClient = descriptor.getValue();
2343 if (basicClient.get() != nullptr) {
2344 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2345 notifier.noteStartCamera(descriptor.getKey(),
2346 static_cast<int>(basicClient->getClientUid()));
2347 }
2348}
2349
2350void CameraService::ClientEventListener::onClientRemoved(
2351 const resource_policy::ClientDescriptor<String8,
2352 sp<CameraService::BasicClient>>& descriptor) {
2353 auto basicClient = descriptor.getValue();
2354 if (basicClient.get() != nullptr) {
2355 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2356 notifier.noteStopCamera(descriptor.getKey(),
2357 static_cast<int>(basicClient->getClientUid()));
2358 }
2359}
2360
2361
2362// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002363// CameraClientManager
2364// ----------------------------------------------------------------------------
2365
Ruben Brunk99e69712015-05-26 17:25:07 -07002366CameraService::CameraClientManager::CameraClientManager() {
2367 setListener(std::make_shared<ClientEventListener>());
2368}
2369
Ruben Brunkcc776712015-02-17 20:18:47 -08002370CameraService::CameraClientManager::~CameraClientManager() {}
2371
2372sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2373 const String8& id) const {
2374 auto descriptor = get(id);
2375 if (descriptor == nullptr) {
2376 return sp<BasicClient>{nullptr};
2377 }
2378 return descriptor->getValue();
2379}
2380
2381String8 CameraService::CameraClientManager::toString() const {
2382 auto all = getAll();
2383 String8 ret("[");
2384 bool hasAny = false;
2385 for (auto& i : all) {
2386 hasAny = true;
2387 String8 key = i->getKey();
2388 int32_t cost = i->getCost();
2389 int32_t pid = i->getOwnerId();
2390 int32_t priority = i->getPriority();
2391 auto conflicting = i->getConflicting();
2392 auto clientSp = i->getValue();
2393 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002394 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002395 if (clientSp.get() != nullptr) {
2396 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002397 uid_t clientUid = clientSp->getClientUid();
2398 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002399 }
2400 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2401 PRId32 ", ", key.string(), cost, pid, priority);
2402
Ruben Brunk6267b532015-04-30 17:44:07 -07002403 if (clientSp.get() != nullptr) {
2404 ret.appendFormat("User Id: %d, ", clientUserId);
2405 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002406 if (packageName.size() != 0) {
2407 ret.appendFormat("Client Package Name: %s", packageName.string());
2408 }
2409
2410 ret.append(", Conflicting Client Devices: {");
2411 for (auto& j : conflicting) {
2412 ret.appendFormat("%s, ", j.string());
2413 }
2414 ret.append("})");
2415 }
2416 if (hasAny) ret.append("\n");
2417 ret.append("]\n");
2418 return ret;
2419}
2420
2421CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2422 const String8& key, const sp<BasicClient>& value, int32_t cost,
2423 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2424
2425 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2426 key, value, cost, conflictingKeys, priority, ownerId);
2427}
2428
2429CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2430 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2431 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2432 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2433}
2434
2435// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002436
2437static const int kDumpLockRetries = 50;
2438static const int kDumpLockSleep = 60000;
2439
2440static bool tryLock(Mutex& mutex)
2441{
2442 bool locked = false;
2443 for (int i = 0; i < kDumpLockRetries; ++i) {
2444 if (mutex.tryLock() == NO_ERROR) {
2445 locked = true;
2446 break;
2447 }
2448 usleep(kDumpLockSleep);
2449 }
2450 return locked;
2451}
2452
2453status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002454 ATRACE_CALL();
2455
Ruben Brunka8ca9152015-04-07 14:23:40 -07002456 String8 result("Dump of the Camera Service:\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002457 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
dcashmanfefa6142015-09-09 13:43:01 -07002458 result = result.format("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07002459 "can't dump CameraService from pid=%d, uid=%d\n",
2460 getCallingPid(),
2461 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002462 write(fd, result.string(), result.size());
2463 } else {
2464 bool locked = tryLock(mServiceLock);
2465 // failed to lock - CameraService is probably deadlocked
2466 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002467 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002468 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002469 }
2470
2471 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002472 if (!mModule) {
2473 result = String8::format("No camera module available!\n");
2474 write(fd, result.string(), result.size());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002475
2476 // Dump event log for error information
2477 dumpEventLog(fd);
2478
Kalle Lampila6ec3a152013-04-30 15:27:19 +03002479 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002480 return NO_ERROR;
2481 }
2482
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002483 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2484 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2485 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2486 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
Ruben Brunkcc776712015-02-17 20:18:47 -08002487 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
Eino-Ville Talvala49c97052016-01-12 14:29:40 -08002488 result.appendFormat("Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Ruben Brunkcc776712015-02-17 20:18:47 -08002489 String8 activeClientString = mActiveClientManager.toString();
2490 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
Ruben Brunk6267b532015-04-30 17:44:07 -07002491 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002492
Ruben Brunkf81648e2014-04-17 16:14:57 -07002493 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2494 if (desc == NULL) {
2495 result.appendFormat("Vendor tags left unimplemented.\n");
2496 } else {
2497 result.appendFormat("Vendor tag definitions:\n");
2498 }
2499
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002500 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07002501
2502 if (desc != NULL) {
2503 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
2504 }
2505
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002506 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002507
2508 bool stateLocked = tryLock(mCameraStatesLock);
2509 if (!stateLocked) {
2510 result = String8::format("CameraStates in use, may be deadlocked\n");
2511 write(fd, result.string(), result.size());
2512 }
2513
2514 for (auto& state : mCameraStates) {
2515 String8 cameraId = state.first;
2516 result = String8::format("Camera %s information:\n", cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002517 camera_info info;
2518
Ruben Brunkcc776712015-02-17 20:18:47 -08002519 // TODO: Change getCameraInfo + HAL to use String cameraIds
2520 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002521 if (rc != OK) {
2522 result.appendFormat(" Error reading static information!\n");
2523 write(fd, result.string(), result.size());
2524 } else {
2525 result.appendFormat(" Facing: %s\n",
2526 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
2527 result.appendFormat(" Orientation: %d\n", info.orientation);
2528 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002529 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002530 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2531 } else {
2532 deviceVersion = info.device_version;
2533 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002534
2535 auto conflicting = state.second->getConflicting();
2536 result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
2537 result.appendFormat(" Conflicting Devices:");
2538 for (auto& id : conflicting) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002539 result.appendFormat(" %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002540 }
2541 if (conflicting.size() == 0) {
2542 result.appendFormat(" NONE");
2543 }
2544 result.appendFormat("\n");
2545
2546 result.appendFormat(" Device version: %#x\n", deviceVersion);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002547 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002548 result.appendFormat(" Device static metadata:\n");
2549 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07002550 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07002551 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002552 } else {
2553 write(fd, result.string(), result.size());
2554 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002555
2556 CameraParameters p = state.second->getShimParams();
2557 if (!p.isEmpty()) {
2558 result = String8::format(" Camera1 API shim is using parameters:\n ");
2559 write(fd, result.string(), result.size());
2560 p.dump(fd, args);
2561 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002562 }
2563
Ruben Brunkcc776712015-02-17 20:18:47 -08002564 auto clientDescriptor = mActiveClientManager.get(cameraId);
2565 if (clientDescriptor == nullptr) {
2566 result = String8::format(" Device %s is closed, no client instance\n",
2567 cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002568 write(fd, result.string(), result.size());
2569 continue;
2570 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002571 hasClient = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002572 result = String8::format(" Device %s is open. Client instance dump:\n\n",
2573 cameraId.string());
2574 result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
2575 result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
2576
2577 auto client = clientDescriptor->getValue();
2578 result.appendFormat("Client package: %s\n",
2579 String8(client->getPackageName()).string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002580 write(fd, result.string(), result.size());
Ruben Brunkcc776712015-02-17 20:18:47 -08002581
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002582 client->dumpClient(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002583 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002584
2585 if (stateLocked) mCameraStatesLock.unlock();
2586
Mathias Agopian65ab4712010-07-14 17:59:35 -07002587 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002588 result = String8::format("\nNo active camera clients yet.\n");
2589 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002590 }
2591
2592 if (locked) mServiceLock.unlock();
2593
Igor Murashkinff3e31d2013-10-23 16:40:06 -07002594 // Dump camera traces if there were any
2595 write(fd, "\n", 1);
2596 camera3::CameraTraces::dump(fd, args);
2597
Mathias Agopian65ab4712010-07-14 17:59:35 -07002598 // change logging level
2599 int n = args.size();
2600 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002601 String16 verboseOption("-v");
2602 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07002603 String8 levelStr(args[i+1]);
2604 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002605 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002606 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002607 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002608 }
2609 }
2610 }
2611 return NO_ERROR;
2612}
2613
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002614void CameraService::dumpEventLog(int fd) {
2615 String8 result = String8("\nPrior client events (most recent at top):\n");
2616
2617 Mutex::Autolock l(mLogLock);
2618 for (const auto& msg : mEventLog) {
2619 result.appendFormat(" %s\n", msg.string());
2620 }
2621
2622 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
2623 result.append(" ...\n");
2624 } else if (mEventLog.size() == 0) {
2625 result.append(" [no events yet]\n");
2626 }
2627 result.append("\n");
2628
2629 write(fd, result.string(), result.size());
2630}
2631
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002632void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002633 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002634 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2635 if (mTorchClientMap[i] == who) {
2636 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002637 String8 cameraId = mTorchClientMap.keyAt(i);
2638 status_t res = mFlashlight->setTorchMode(cameraId, false);
2639 if (res) {
2640 ALOGE("%s: torch client died but couldn't turn off torch: "
2641 "%s (%d)", __FUNCTION__, strerror(-res), res);
2642 return;
2643 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002644 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002645 break;
2646 }
2647 }
2648}
2649
Ruben Brunkcc776712015-02-17 20:18:47 -08002650/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002651
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002652 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002653 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2654 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002655 */
2656
Ruben Brunka8ca9152015-04-07 14:23:40 -07002657 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2658
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002659 // check torch client
2660 handleTorchClientBinderDied(who);
2661
2662 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002663 if(!evictClientIdByRemote(who)) {
2664 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002665 return;
2666 }
2667
Ruben Brunkcc776712015-02-17 20:18:47 -08002668 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2669 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002670}
2671
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002672void CameraService::updateStatus(int32_t status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002673 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002674}
2675
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002676void CameraService::updateStatus(int32_t status, const String8& cameraId,
2677 std::initializer_list<int32_t> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002678 // Do not lock mServiceLock here or can get into a deadlock from
2679 // connect() -> disconnect -> updateStatus
2680
2681 auto state = getCameraState(cameraId);
2682
2683 if (state == nullptr) {
2684 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2685 cameraId.string());
2686 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002687 }
2688
Ruben Brunkcc776712015-02-17 20:18:47 -08002689 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2690 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2691 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002692 (const String8& cameraId, int32_t status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002693
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002694 if (status != ICameraServiceListener::STATUS_ENUMERATING) {
2695 // Update torch status if it has a flash unit.
2696 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002697 int32_t torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002698 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2699 NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002700 int32_t newTorchStatus =
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002701 status == ICameraServiceListener::STATUS_PRESENT ?
2702 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF :
2703 ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
2704 if (torchStatus != newTorchStatus) {
2705 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2706 }
2707 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002708 }
2709
2710 Mutex::Autolock lock(mStatusListenerLock);
2711
2712 for (auto& listener : mListenerList) {
2713 // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
2714 int id = cameraIdToInt(cameraId);
2715 if (id != -1) listener->onStatusChanged(status, id);
2716 }
2717 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002718}
2719
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002720void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
2721 const String8& cameraId) {
2722 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2723 if (proxyBinder == nullptr) return;
2724 String16 id(cameraId);
2725 proxyBinder->notifyCameraState(id, newState);
2726}
2727
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002728status_t CameraService::getTorchStatusLocked(
2729 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002730 int32_t *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002731 if (!status) {
2732 return BAD_VALUE;
2733 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002734 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2735 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002736 // invalid camera ID or the camera doesn't have a flash unit
2737 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002738 }
2739
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002740 *status = mTorchStatusMap.valueAt(index);
2741 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002742}
2743
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002744status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002745 int32_t status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002746 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2747 if (index == NAME_NOT_FOUND) {
2748 return BAD_VALUE;
2749 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002750 int32_t& item =
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002751 mTorchStatusMap.editValueAt(index);
2752 item = status;
2753
2754 return OK;
2755}
2756
Mathias Agopian65ab4712010-07-14 17:59:35 -07002757}; // namespace android