blob: 7597054f8da51ae62d22fc6001af565461b4a646 [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>
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -070044#include <memunreachable/memunreachable.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070045#include <media/AudioSystem.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080046#include <media/IMediaHTTPService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070047#include <media/mediaplayer.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070048#include <mediautils/BatteryNotifier.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070049#include <utils/Errors.h>
50#include <utils/Log.h>
51#include <utils/String16.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080052#include <utils/Trace.h>
Chien-Yu Chen98a668f2015-12-18 14:10:33 -080053#include <private/android_filesystem_config.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080054#include <system/camera_vendor_tags.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070055#include <system/camera_metadata.h>
56#include <system/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
58#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070059#include "api1/CameraClient.h"
60#include "api1/Camera2Client.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070061#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070062#include "utils/CameraTraces.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070063
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080064namespace {
65 const char* kPermissionServiceName = "permission";
66}; // namespace anonymous
67
Mathias Agopian65ab4712010-07-14 17:59:35 -070068namespace android {
69
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080070using binder::Status;
71using namespace hardware;
72
Mathias Agopian65ab4712010-07-14 17:59:35 -070073// ----------------------------------------------------------------------------
74// Logging support -- this is for debugging only
75// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070076volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070077
Steve Blockb8a80522011-12-20 16:23:08 +000078#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
79#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070080
81static void setLogLevel(int level) {
82 android_atomic_write(level, &gLogLevel);
83}
84
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080085// Convenience methods for constructing binder::Status objects for error returns
86
87#define STATUS_ERROR(errorCode, errorString) \
88 binder::Status::fromServiceSpecificError(errorCode, \
89 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
90
91#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
92 binder::Status::fromServiceSpecificError(errorCode, \
93 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
94 __VA_ARGS__))
95
Mathias Agopian65ab4712010-07-14 17:59:35 -070096// ----------------------------------------------------------------------------
97
Igor Murashkincba2c162013-03-20 15:56:31 -070098extern "C" {
99static void camera_device_status_change(
100 const struct camera_module_callbacks* callbacks,
101 int camera_id,
102 int new_status) {
103 sp<CameraService> cs = const_cast<CameraService*>(
Ruben Brunkcc776712015-02-17 20:18:47 -0800104 static_cast<const CameraService*>(callbacks));
Igor Murashkincba2c162013-03-20 15:56:31 -0700105
Bin Chene16d1162016-02-22 18:19:58 +1100106 cs->onDeviceStatusChanged(camera_id,
Ruben Brunkcc776712015-02-17 20:18:47 -0800107 static_cast<camera_device_status_t>(new_status));
Igor Murashkincba2c162013-03-20 15:56:31 -0700108}
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800109
110static void torch_mode_status_change(
111 const struct camera_module_callbacks* callbacks,
112 const char* camera_id,
113 int new_status) {
114 if (!callbacks || !camera_id) {
115 ALOGE("%s invalid parameters. callbacks %p, camera_id %p", __FUNCTION__,
116 callbacks, camera_id);
117 }
118 sp<CameraService> cs = const_cast<CameraService*>(
119 static_cast<const CameraService*>(callbacks));
120
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800121 int32_t status;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800122 switch (new_status) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800123 case TORCH_MODE_STATUS_NOT_AVAILABLE:
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800124 status = ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
125 break;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800126 case TORCH_MODE_STATUS_AVAILABLE_OFF:
127 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF;
128 break;
129 case TORCH_MODE_STATUS_AVAILABLE_ON:
130 status = ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800131 break;
132 default:
133 ALOGE("Unknown torch status %d", new_status);
134 return;
135 }
136
137 cs->onTorchStatusChanged(
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800138 String8(camera_id),
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800139 status);
140}
Igor Murashkincba2c162013-03-20 15:56:31 -0700141} // extern "C"
142
Mathias Agopian65ab4712010-07-14 17:59:35 -0700143// ----------------------------------------------------------------------------
144
145// This is ugly and only safe if we never re-create the CameraService, but
146// should be ok for now.
147static CameraService *gCameraService;
148
Eino-Ville Talvala49c97052016-01-12 14:29:40 -0800149CameraService::CameraService() :
150 mEventLog(DEFAULT_EVENT_LOG_LENGTH),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800151 mNumberOfCameras(0), mNumberOfNormalCameras(0),
152 mSoundRef(0), mModule(nullptr) {
Steve Blockdf64d152012-01-04 20:05:49 +0000153 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700154 gCameraService = this;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800155
Igor Murashkincba2c162013-03-20 15:56:31 -0700156 this->camera_device_status_change = android::camera_device_status_change;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800157 this->torch_mode_status_change = android::torch_mode_status_change;
158
Ruben Brunkcc776712015-02-17 20:18:47 -0800159 mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160}
161
Iliyan Malchev8951a972011-04-14 16:55:59 -0700162void CameraService::onFirstRef()
163{
Ruben Brunkcc776712015-02-17 20:18:47 -0800164 ALOGI("CameraService process starting");
Igor Murashkin634a5152013-02-20 17:15:11 -0800165
Iliyan Malchev8951a972011-04-14 16:55:59 -0700166 BnCameraService::onFirstRef();
167
Ruben Brunk99e69712015-05-26 17:25:07 -0700168 // Update battery life tracking if service is restarting
169 BatteryNotifier& notifier(BatteryNotifier::getInstance());
170 notifier.noteResetCamera();
171 notifier.noteResetFlashlight();
172
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800173 camera_module_t *rawModule;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700174 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
175 (const hw_module_t **)&rawModule);
176 if (err < 0) {
177 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
178 logServiceError("Could not load camera HAL module", err);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700179 return;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700180 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800181
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700182 mModule = new CameraModule(rawModule);
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700183 err = mModule->init();
184 if (err != OK) {
185 ALOGE("Could not initialize camera HAL module: %d (%s)", err,
186 strerror(-err));
187 logServiceError("Could not initialize camera HAL module", err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800188
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700189 delete mModule;
190 mModule = nullptr;
191 return;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700192 }
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700193 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700194
195 mNumberOfCameras = mModule->getNumberOfCameras();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700196 mNumberOfNormalCameras = mNumberOfCameras;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700197
Yin-Chia Yehd4a653a2015-10-14 11:05:44 -0700198 // Setup vendor tags before we call get_camera_info the first time
199 // because HAL might need to setup static vendor keys in get_camera_info
200 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
201 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_2) {
202 setUpVendorTags();
203 }
204
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700205 mFlashlight = new CameraFlashlight(*mModule, *this);
206 status_t res = mFlashlight->findFlashUnits();
207 if (res) {
208 // impossible because we haven't open any camera devices.
209 ALOGE("Failed to find flash units.");
210 }
211
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700212 int latestStrangeCameraId = INT_MAX;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700213 for (int i = 0; i < mNumberOfCameras; i++) {
214 String8 cameraId = String8::format("%d", i);
215
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700216 // Get camera info
217
218 struct camera_info info;
219 bool haveInfo = true;
220 status_t rc = mModule->getCameraInfo(i, &info);
221 if (rc != NO_ERROR) {
222 ALOGE("%s: Received error loading camera info for device %d, cost and"
223 " conflicting devices fields set to defaults for this device.",
224 __FUNCTION__, i);
225 haveInfo = false;
226 }
227
228 // Check for backwards-compatibility support
229 if (haveInfo) {
230 if (checkCameraCapabilities(i, info, &latestStrangeCameraId) != OK) {
231 delete mModule;
232 mModule = nullptr;
233 return;
234 }
235 }
236
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700237 // Defaults to use for cost and conflicting devices
238 int cost = 100;
239 char** conflicting_devices = nullptr;
240 size_t conflicting_devices_length = 0;
241
242 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700243 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 && haveInfo) {
244 cost = info.resource_cost;
245 conflicting_devices = info.conflicting_devices;
246 conflicting_devices_length = info.conflicting_devices_length;
Eino-Ville Talvala1527f072015-04-07 15:55:31 -0700247 }
248
249 std::set<String8> conflicting;
250 for (size_t i = 0; i < conflicting_devices_length; i++) {
251 conflicting.emplace(String8(conflicting_devices[i]));
252 }
253
254 // Initialize state for each camera device
255 {
256 Mutex::Autolock lock(mCameraStatesLock);
257 mCameraStates.emplace(cameraId, std::make_shared<CameraState>(cameraId, cost,
258 conflicting));
259 }
260
261 if (mFlashlight->hasFlashUnit(cameraId)) {
262 mTorchStatusMap.add(cameraId,
263 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF);
264 }
265 }
266
267 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_1) {
268 mModule->setCallbacks(this);
269 }
270
Ruben Brunk2823ce02015-05-19 17:25:13 -0700271 CameraService::pingCameraServiceProxy();
272}
273
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700274sp<ICameraServiceProxy> CameraService::getCameraServiceProxy() {
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800275 sp<ICameraServiceProxy> proxyBinder = nullptr;
276#ifndef __BRILLO__
Ruben Brunk2823ce02015-05-19 17:25:13 -0700277 sp<IServiceManager> sm = defaultServiceManager();
278 sp<IBinder> binder = sm->getService(String16("media.camera.proxy"));
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800279 if (binder != nullptr) {
280 proxyBinder = interface_cast<ICameraServiceProxy>(binder);
Ruben Brunk2823ce02015-05-19 17:25:13 -0700281 }
Christopher Wiley92c06fc2016-02-11 15:40:05 -0800282#endif
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700283 return proxyBinder;
284}
285
286void CameraService::pingCameraServiceProxy() {
287 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
288 if (proxyBinder == nullptr) return;
Ruben Brunk2823ce02015-05-19 17:25:13 -0700289 proxyBinder->pingForUserUpdate();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700290}
291
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292CameraService::~CameraService() {
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800293 if (mModule) {
294 delete mModule;
Ruben Brunkcc776712015-02-17 20:18:47 -0800295 mModule = nullptr;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800296 }
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800297 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
Ruben Brunkcc776712015-02-17 20:18:47 -0800298 gCameraService = nullptr;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299}
300
Bin Chene16d1162016-02-22 18:19:58 +1100301void CameraService::onDeviceStatusChanged(int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800302 camera_device_status_t newStatus) {
303 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
Igor Murashkincba2c162013-03-20 15:56:31 -0700304 cameraId, newStatus);
305
Ruben Brunkcc776712015-02-17 20:18:47 -0800306 String8 id = String8::format("%d", cameraId);
307 std::shared_ptr<CameraState> state = getCameraState(id);
308
309 if (state == nullptr) {
Igor Murashkincba2c162013-03-20 15:56:31 -0700310 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
311 return;
312 }
313
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800314 int32_t oldStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -0800315
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800316 if (oldStatus == static_cast<int32_t>(newStatus)) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800317 ALOGE("%s: State transition to the same status %#x not allowed", __FUNCTION__, newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700318 return;
319 }
320
Igor Murashkincba2c162013-03-20 15:56:31 -0700321 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700322 logDeviceRemoved(id, String8::format("Device status changed from %d to %d", oldStatus,
323 newStatus));
Ruben Brunkcc776712015-02-17 20:18:47 -0800324 sp<BasicClient> clientToDisconnect;
Igor Murashkincba2c162013-03-20 15:56:31 -0700325 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800326 // Don't do this in updateStatus to avoid deadlock over mServiceLock
327 Mutex::Autolock lock(mServiceLock);
Igor Murashkincba2c162013-03-20 15:56:31 -0700328
Ruben Brunkcc776712015-02-17 20:18:47 -0800329 // Set the device status to NOT_PRESENT, clients will no longer be able to connect
330 // to this device until the status changes
331 updateStatus(ICameraServiceListener::STATUS_NOT_PRESENT, id);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700332
Ruben Brunkcc776712015-02-17 20:18:47 -0800333 // Remove cached shim parameters
334 state->setShimParams(CameraParameters());
Igor Murashkincba2c162013-03-20 15:56:31 -0700335
Ruben Brunkcc776712015-02-17 20:18:47 -0800336 // Remove the client from the list of active clients
337 clientToDisconnect = removeClientLocked(id);
338
339 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800340 clientToDisconnect->notifyError(
341 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -0800342 CaptureResultExtras{});
Igor Murashkincba2c162013-03-20 15:56:31 -0700343 }
344
Ruben Brunkcc776712015-02-17 20:18:47 -0800345 ALOGI("%s: Client for camera ID %s evicted due to device status change from HAL",
346 __FUNCTION__, id.string());
Igor Murashkincba2c162013-03-20 15:56:31 -0700347
Ruben Brunkcc776712015-02-17 20:18:47 -0800348 // Disconnect client
349 if (clientToDisconnect.get() != nullptr) {
350 // Ensure not in binder RPC so client disconnect PID checks work correctly
351 LOG_ALWAYS_FATAL_IF(getCallingPid() != getpid(),
352 "onDeviceStatusChanged must be called from the camera service process!");
353 clientToDisconnect->disconnect();
Igor Murashkincba2c162013-03-20 15:56:31 -0700354 }
355
Ruben Brunkcc776712015-02-17 20:18:47 -0800356 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800357 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunka8ca9152015-04-07 14:23:40 -0700358 logDeviceAdded(id, String8::format("Device status changed from %d to %d", oldStatus,
359 newStatus));
360 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800361 updateStatus(static_cast<int32_t>(newStatus), id);
Igor Murashkincba2c162013-03-20 15:56:31 -0700362 }
363
Igor Murashkincba2c162013-03-20 15:56:31 -0700364}
365
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800366void CameraService::onTorchStatusChanged(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800367 int32_t newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800368 Mutex::Autolock al(mTorchStatusMutex);
369 onTorchStatusChangedLocked(cameraId, newStatus);
370}
371
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800372void CameraService::onTorchStatusChangedLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800373 int32_t newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800374 ALOGI("%s: Torch status changed for cameraId=%s, newStatus=%d",
375 __FUNCTION__, cameraId.string(), newStatus);
376
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800377 int32_t status;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800378 status_t res = getTorchStatusLocked(cameraId, &status);
379 if (res) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -0700380 ALOGE("%s: cannot get torch status of camera %s: %s (%d)",
381 __FUNCTION__, cameraId.string(), strerror(-res), res);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800382 return;
383 }
384 if (status == newStatus) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800385 return;
386 }
387
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800388 res = setTorchStatusLocked(cameraId, newStatus);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800389 if (res) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800390 ALOGE("%s: Failed to set the torch status to %d: %s (%d)", __FUNCTION__,
391 (uint32_t)newStatus, strerror(-res), res);
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800392 return;
393 }
394
Ruben Brunkcc776712015-02-17 20:18:47 -0800395 {
Ruben Brunk99e69712015-05-26 17:25:07 -0700396 // Update battery life logging for flashlight
Chien-Yu Chenfe751be2015-09-01 14:16:44 -0700397 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -0700398 auto iter = mTorchUidMap.find(cameraId);
399 if (iter != mTorchUidMap.end()) {
400 int oldUid = iter->second.second;
401 int newUid = iter->second.first;
402 BatteryNotifier& notifier(BatteryNotifier::getInstance());
403 if (oldUid != newUid) {
404 // If the UID has changed, log the status and update current UID in mTorchUidMap
405 if (status == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
406 notifier.noteFlashlightOff(cameraId, oldUid);
407 }
408 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
409 notifier.noteFlashlightOn(cameraId, newUid);
410 }
411 iter->second.second = newUid;
412 } else {
413 // If the UID has not changed, log the status
414 if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) {
415 notifier.noteFlashlightOn(cameraId, oldUid);
416 } else {
417 notifier.noteFlashlightOff(cameraId, oldUid);
418 }
419 }
420 }
421 }
422
423 {
Ruben Brunkcc776712015-02-17 20:18:47 -0800424 Mutex::Autolock lock(mStatusListenerLock);
425 for (auto& i : mListenerList) {
426 i->onTorchStatusChanged(newStatus, String16{cameraId});
427 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800428 }
429}
430
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800431Status CameraService::getNumberOfCameras(int32_t type, int32_t* numCameras) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700432 ATRACE_CALL();
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700433 switch (type) {
434 case CAMERA_TYPE_BACKWARD_COMPATIBLE:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800435 *numCameras = mNumberOfNormalCameras;
436 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700437 case CAMERA_TYPE_ALL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800438 *numCameras = mNumberOfCameras;
439 break;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700440 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800441 ALOGW("%s: Unknown camera type %d",
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700442 __FUNCTION__, type);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800443 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
444 "Unknown camera type %d", type);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -0700445 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800446 return Status::ok();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700447}
448
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800449Status CameraService::getCameraInfo(int cameraId,
450 CameraInfo* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700451 ATRACE_CALL();
Iliyan Malchev8951a972011-04-14 16:55:59 -0700452 if (!mModule) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800453 return STATUS_ERROR(ERROR_DISCONNECTED,
454 "Camera subsystem is not available");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700455 }
456
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800458 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
459 "CameraId is not valid");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 }
461
Iliyan Malchev8951a972011-04-14 16:55:59 -0700462 struct camera_info info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800463 Status rc = filterGetInfoErrorCode(
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800464 mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800465
466 if (rc.isOk()) {
467 cameraInfo->facing = info.facing;
468 cameraInfo->orientation = info.orientation;
Yin-Chia Yeh9b5a6e92016-04-22 13:24:05 -0700469 // CameraInfo is for android.hardware.Camera which does not
470 // support external camera facing. The closest approximation would be
471 // front camera.
472 if (cameraInfo->orientation == CAMERA_FACING_EXTERNAL) {
473 cameraInfo->orientation = CAMERA_FACING_FRONT;
474 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800475 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700476 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477}
478
Ruben Brunkcc776712015-02-17 20:18:47 -0800479int CameraService::cameraIdToInt(const String8& cameraId) {
480 errno = 0;
481 size_t pos = 0;
482 int ret = stoi(std::string{cameraId.string()}, &pos);
483 if (errno != 0 || pos != cameraId.size()) {
484 return -1;
485 }
486 return ret;
487}
Ruben Brunkb2119af2014-05-09 19:57:56 -0700488
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800489Status CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700490 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800491
492 Status ret = Status::ok();
493
Ruben Brunkb2119af2014-05-09 19:57:56 -0700494 struct CameraInfo info;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800495 if (!(ret = getCameraInfo(cameraId, &info)).isOk()) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700496 return ret;
497 }
498
499 CameraMetadata shimInfo;
500 int32_t orientation = static_cast<int32_t>(info.orientation);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800501 status_t rc;
502 if ((rc = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) {
503 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
504 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700505 }
506
507 uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ?
508 ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800509 if ((rc = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) {
510 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
511 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700512 }
513
Igor Murashkin65d14b92014-06-17 12:03:20 -0700514 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800515 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -0700516 // Error logged by callee
517 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700518 }
519
520 Vector<Size> sizes;
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700521 Vector<Size> jpegSizes;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700522 Vector<int32_t> formats;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700523 {
524 shimParams.getSupportedPreviewSizes(/*out*/sizes);
525 shimParams.getSupportedPreviewFormats(/*out*/formats);
526 shimParams.getSupportedPictureSizes(/*out*/jpegSizes);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700527 }
528
529 // Always include IMPLEMENTATION_DEFINED
530 formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
531
532 const size_t INTS_PER_CONFIG = 4;
533
534 // Build available stream configurations metadata
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700535 size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG;
536
537 Vector<int32_t> streamConfigs;
538 streamConfigs.setCapacity(streamConfigSize);
539
Ruben Brunkb2119af2014-05-09 19:57:56 -0700540 for (size_t i = 0; i < formats.size(); ++i) {
541 for (size_t j = 0; j < sizes.size(); ++j) {
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700542 streamConfigs.add(formats[i]);
543 streamConfigs.add(sizes[j].width);
544 streamConfigs.add(sizes[j].height);
545 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700546 }
547 }
548
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700549 for (size_t i = 0; i < jpegSizes.size(); ++i) {
550 streamConfigs.add(HAL_PIXEL_FORMAT_BLOB);
551 streamConfigs.add(jpegSizes[i].width);
552 streamConfigs.add(jpegSizes[i].height);
553 streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
554 }
555
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800556 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
Ruben Brunk152dbcf2014-06-12 19:11:45 -0700557 streamConfigs.array(), streamConfigSize)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800558 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
559 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700560 }
561
562 int64_t fakeMinFrames[0];
563 // TODO: Fixme, don't fake min frame durations.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800564 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
Ruben Brunkb2119af2014-05-09 19:57:56 -0700565 fakeMinFrames, 0)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800566 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
567 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700568 }
569
570 int64_t fakeStalls[0];
571 // TODO: Fixme, don't fake stall durations.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800572 if ((rc = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS,
Ruben Brunkb2119af2014-05-09 19:57:56 -0700573 fakeStalls, 0)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800574 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
575 "Error updating metadata: %d (%s)", rc, strerror(-rc));
Ruben Brunkb2119af2014-05-09 19:57:56 -0700576 }
577
578 *cameraInfo = shimInfo;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800579 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700580}
581
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800582Status CameraService::getCameraCharacteristics(int cameraId,
Zhijun He2b59be82013-09-25 10:14:30 -0700583 CameraMetadata* cameraInfo) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700584 ATRACE_CALL();
Zhijun He2b59be82013-09-25 10:14:30 -0700585 if (!cameraInfo) {
586 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800587 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "cameraInfo is NULL");
Zhijun He2b59be82013-09-25 10:14:30 -0700588 }
589
590 if (!mModule) {
591 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800592 return STATUS_ERROR(ERROR_DISCONNECTED,
593 "Camera subsystem is not available");;
Zhijun He2b59be82013-09-25 10:14:30 -0700594 }
595
Zhijun He2b59be82013-09-25 10:14:30 -0700596 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
597 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800598 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
599 "Invalid camera id: %d", cameraId);
Zhijun He2b59be82013-09-25 10:14:30 -0700600 }
601
602 int facing;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800603 Status ret;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800604 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0 ||
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800605 getDeviceVersion(cameraId, &facing) < CAMERA_DEVICE_API_VERSION_3_0) {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700606 /**
607 * Backwards compatibility mode for old HALs:
608 * - Convert CameraInfo into static CameraMetadata properties.
609 * - Retrieve cached CameraParameters for this camera. If none exist,
610 * attempt to open CameraClient and retrieve the CameraParameters.
611 * - Convert cached CameraParameters into static CameraMetadata
612 * properties.
613 */
614 ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__);
Zhijun He2b59be82013-09-25 10:14:30 -0700615
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800616 ret = generateShimMetadata(cameraId, cameraInfo);
Ruben Brunkb2119af2014-05-09 19:57:56 -0700617 } else {
618 /**
619 * Normal HAL 2.1+ codepath.
620 */
621 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800622 ret = filterGetInfoErrorCode(mModule->getCameraInfo(cameraId, &info));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800623 if (ret.isOk()) {
624 *cameraInfo = info.static_camera_characteristics;
625 }
Ruben Brunkb2119af2014-05-09 19:57:56 -0700626 }
Zhijun He2b59be82013-09-25 10:14:30 -0700627
628 return ret;
629}
630
Ruben Brunkcc776712015-02-17 20:18:47 -0800631int CameraService::getCallingPid() {
632 return IPCThreadState::self()->getCallingPid();
633}
634
635int CameraService::getCallingUid() {
636 return IPCThreadState::self()->getCallingUid();
637}
638
639String8 CameraService::getFormattedCurrentTime() {
640 time_t now = time(nullptr);
641 char formattedTime[64];
642 strftime(formattedTime, sizeof(formattedTime), "%m-%d %H:%M:%S", localtime(&now));
643 return String8(formattedTime);
644}
645
646int CameraService::getCameraPriorityFromProcState(int procState) {
647 // Find the priority for the camera usage based on the process state. Higher priority clients
648 // win for evictions.
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700649 if (procState < 0) {
650 ALOGE("%s: Received invalid process state %d from ActivityManagerService!", __FUNCTION__,
651 procState);
652 return -1;
Ruben Brunkcc776712015-02-17 20:18:47 -0800653 }
Eino-Ville Talvala52aad852015-09-03 12:24:24 -0700654 // Treat sleeping TOP processes the same as regular TOP processes, for
655 // access priority. This is important for lock-screen camera launch scenarios
656 if (procState == PROCESS_STATE_TOP_SLEEPING) {
657 procState = PROCESS_STATE_TOP;
658 }
Ruben Brunkbe0b6b42015-05-12 16:10:52 -0700659 return INT_MAX - procState;
Ruben Brunkcc776712015-02-17 20:18:47 -0800660}
661
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800662Status CameraService::getCameraVendorTagDescriptor(
663 /*out*/
664 hardware::camera2::params::VendorTagDescriptor* desc) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700665 ATRACE_CALL();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800666 if (!mModule) {
667 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800668 return STATUS_ERROR(ERROR_DISCONNECTED, "Camera subsystem not available");
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800669 }
Eino-Ville Talvala1e74e242016-03-03 11:24:28 -0800670 sp<VendorTagDescriptor> globalDescriptor = VendorTagDescriptor::getGlobalVendorTagDescriptor();
671 if (globalDescriptor != nullptr) {
672 *desc = *(globalDescriptor.get());
673 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800674 return Status::ok();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800675}
676
Igor Murashkin634a5152013-02-20 17:15:11 -0800677int CameraService::getDeviceVersion(int cameraId, int* facing) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700678 ATRACE_CALL();
Igor Murashkin634a5152013-02-20 17:15:11 -0800679 struct camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800680 if (mModule->getCameraInfo(cameraId, &info) != OK) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800681 return -1;
682 }
683
684 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -0800685 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800686 deviceVersion = info.device_version;
687 } else {
688 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
689 }
690
691 if (facing) {
692 *facing = info.facing;
693 }
694
695 return deviceVersion;
696}
697
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800698Status CameraService::filterGetInfoErrorCode(status_t err) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700699 switch(err) {
700 case NO_ERROR:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800701 return Status::ok();
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700702 case -EINVAL:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800703 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
704 "CameraId is not valid for HAL module");
705 case -ENODEV:
706 return STATUS_ERROR(ERROR_DISCONNECTED,
707 "Camera device not available");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700708 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800709 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
710 "Camera HAL encountered error %d: %s",
711 err, strerror(-err));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700712 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800713}
714
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800715bool CameraService::setUpVendorTags() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -0700716 ATRACE_CALL();
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800717 vendor_tag_ops_t vOps = vendor_tag_ops_t();
718
719 // Check if vendor operations have been implemented
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800720 if (!mModule->isVendorTagDefined()) {
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800721 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
722 return false;
723 }
724
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800725 mModule->getVendorTagOps(&vOps);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800726
727 // Ensure all vendor operations are present
728 if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL ||
729 vOps.get_section_name == NULL || vOps.get_tag_name == NULL ||
730 vOps.get_tag_type == NULL) {
731 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
732 , __FUNCTION__);
733 return false;
734 }
735
736 // Read all vendor tag definitions into a descriptor
737 sp<VendorTagDescriptor> desc;
738 status_t res;
739 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
740 != OK) {
741 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
742 "received error %s (%d). Camera clients will not be able to use"
743 "vendor tags", __FUNCTION__, strerror(res), res);
744 return false;
745 }
746
747 // Set the global descriptor to use with camera metadata
748 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
749 return true;
750}
751
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800752Status CameraService::makeClient(const sp<CameraService>& cameraService,
753 const sp<IInterface>& cameraCb, const String16& packageName, int cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800754 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
755 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
756 /*out*/sp<BasicClient>* client) {
757
Ruben Brunkcc776712015-02-17 20:18:47 -0800758 if (halVersion < 0 || halVersion == deviceVersion) {
759 // Default path: HAL version is unspecified by caller, create CameraClient
760 // based on device version reported by the HAL.
761 switch(deviceVersion) {
762 case CAMERA_DEVICE_API_VERSION_1_0:
763 if (effectiveApiLevel == API_1) { // Camera1 API route
764 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800765 *client = new CameraClient(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800766 clientPid, clientUid, getpid(), legacyMode);
767 } else { // Camera2 API route
768 ALOGW("Camera using old HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800769 return STATUS_ERROR_FMT(ERROR_DEPRECATED_HAL,
770 "Camera device \"%d\" HAL version %d does not support camera2 API",
771 cameraId, deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800772 }
773 break;
Ruben Brunkcc776712015-02-17 20:18:47 -0800774 case CAMERA_DEVICE_API_VERSION_3_0:
775 case CAMERA_DEVICE_API_VERSION_3_1:
776 case CAMERA_DEVICE_API_VERSION_3_2:
777 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -0700778 case CAMERA_DEVICE_API_VERSION_3_4:
Ruben Brunkcc776712015-02-17 20:18:47 -0800779 if (effectiveApiLevel == API_1) { // Camera1 API route
780 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800781 *client = new Camera2Client(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800782 clientPid, clientUid, servicePid, legacyMode);
783 } else { // Camera2 API route
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800784 sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
785 static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
786 *client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId,
Ruben Brunkcc776712015-02-17 20:18:47 -0800787 facing, clientPid, clientUid, servicePid);
788 }
789 break;
790 default:
791 // Should not be reachable
792 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800793 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
794 "Camera device \"%d\" has unknown HAL version %d",
795 cameraId, deviceVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800796 }
797 } else {
798 // A particular HAL version is requested by caller. Create CameraClient
799 // based on the requested HAL version.
800 if (deviceVersion > CAMERA_DEVICE_API_VERSION_1_0 &&
801 halVersion == CAMERA_DEVICE_API_VERSION_1_0) {
802 // Only support higher HAL version device opened as HAL1.0 device.
803 sp<ICameraClient> tmp = static_cast<ICameraClient*>(cameraCb.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800804 *client = new CameraClient(cameraService, tmp, packageName, cameraId, facing,
Ruben Brunkcc776712015-02-17 20:18:47 -0800805 clientPid, clientUid, servicePid, legacyMode);
806 } else {
807 // Other combinations (e.g. HAL3.x open as HAL2.x) are not supported yet.
808 ALOGE("Invalid camera HAL version %x: HAL %x device can only be"
809 " opened as HAL %x device", halVersion, deviceVersion,
810 CAMERA_DEVICE_API_VERSION_1_0);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800811 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
812 "Camera device \"%d\" (HAL version %d) cannot be opened as HAL version %d",
813 cameraId, deviceVersion, halVersion);
Ruben Brunkcc776712015-02-17 20:18:47 -0800814 }
815 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800816 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800817}
818
Ruben Brunk6267b532015-04-30 17:44:07 -0700819String8 CameraService::toString(std::set<userid_t> intSet) {
820 String8 s("");
821 bool first = true;
822 for (userid_t i : intSet) {
823 if (first) {
824 s.appendFormat("%d", i);
825 first = false;
826 } else {
827 s.appendFormat(", %d", i);
828 }
829 }
830 return s;
831}
832
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800833Status CameraService::initializeShimMetadata(int cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800834 int uid = getCallingUid();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700835
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800836 String16 internalPackageName("cameraserver");
Ruben Brunkcc776712015-02-17 20:18:47 -0800837 String8 id = String8::format("%d", cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800838 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -0800839 sp<Client> tmp = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800840 if (!(ret = connectHelper<ICameraClient,Client>(
841 sp<ICameraClient>{nullptr}, id, static_cast<int>(CAMERA_HAL_API_VERSION_UNSPECIFIED),
842 internalPackageName, uid, USE_CALLING_PID,
843 API_1, /*legacyMode*/ false, /*shimUpdateOnly*/ true,
844 /*out*/ tmp)
845 ).isOk()) {
846 ALOGE("%s: Error initializing shim metadata: %s", __FUNCTION__, ret.toString8().string());
Ruben Brunkb2119af2014-05-09 19:57:56 -0700847 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800848 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700849}
850
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800851Status CameraService::getLegacyParametersLazy(int cameraId,
Igor Murashkin65d14b92014-06-17 12:03:20 -0700852 /*out*/
853 CameraParameters* parameters) {
854
855 ALOGV("%s: for cameraId: %d", __FUNCTION__, cameraId);
856
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800857 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -0700858
859 if (parameters == NULL) {
860 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800861 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700862 }
863
Ruben Brunkcc776712015-02-17 20:18:47 -0800864 String8 id = String8::format("%d", cameraId);
865
866 // Check if we already have parameters
867 {
868 // Scope for service lock
Igor Murashkin65d14b92014-06-17 12:03:20 -0700869 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -0800870 auto cameraState = getCameraState(id);
871 if (cameraState == nullptr) {
872 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800873 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
874 "Invalid camera ID: %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -0800875 }
876 CameraParameters p = cameraState->getShimParams();
877 if (!p.isEmpty()) {
878 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800879 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700880 }
881 }
882
Ruben Brunkcc776712015-02-17 20:18:47 -0800883 int64_t token = IPCThreadState::self()->clearCallingIdentity();
884 ret = initializeShimMetadata(cameraId);
885 IPCThreadState::self()->restoreCallingIdentity(token);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800886 if (!ret.isOk()) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800887 // Error already logged by callee
888 return ret;
889 }
890
891 // Check for parameters again
892 {
893 // Scope for service lock
894 Mutex::Autolock lock(mServiceLock);
895 auto cameraState = getCameraState(id);
896 if (cameraState == nullptr) {
897 ALOGE("%s: Invalid camera ID: %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800898 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
899 "Invalid camera ID: %s", id.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -0700900 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800901 CameraParameters p = cameraState->getShimParams();
902 if (!p.isEmpty()) {
903 *parameters = p;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800904 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700905 }
906 }
907
Ruben Brunkcc776712015-02-17 20:18:47 -0800908 ALOGE("%s: Parameters were not initialized, or were empty. Device may not be present.",
909 __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800910 return STATUS_ERROR(ERROR_INVALID_OPERATION, "Unable to initialize legacy parameters");
Igor Murashkin65d14b92014-06-17 12:03:20 -0700911}
912
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800913// Can camera service trust the caller based on the calling UID?
914static bool isTrustedCallingUid(uid_t uid) {
915 switch (uid) {
916 case AID_MEDIA: // mediaserver
917 case AID_CAMERASERVER: // cameraserver
918 return true;
919 default:
920 return false;
921 }
922}
923
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800924Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700925 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
926 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500927
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700928 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
929 originalClientPid);
Alex Deymoeb4c32d2016-08-24 20:19:39 -0700930#if !defined(__BRILLO__)
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800931 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -0800932 return allowed;
933 }
934#endif // defined(__BRILLO__)
935
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800936 int callingPid = getCallingPid();
937
Iliyan Malchev8951a972011-04-14 16:55:59 -0700938 if (!mModule) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800939 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
940 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800941 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
942 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700943 }
944
Ruben Brunkcc776712015-02-17 20:18:47 -0800945 if (getCameraState(cameraId) == nullptr) {
946 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
947 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800948 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
949 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700950 }
951
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800952 status_t err = checkIfDeviceIsUsable(cameraId);
953 if (err != NO_ERROR) {
954 switch(err) {
955 case -ENODEV:
956 case -EBUSY:
957 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
958 "No camera device with ID \"%s\" currently available", cameraId.string());
959 default:
960 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
961 "Unknown error connecting to ID \"%s\"", cameraId.string());
962 }
963 }
964 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800965}
966
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800967Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700968 const String8& clientName8, int& clientUid, int& clientPid,
969 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800970 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800971 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700972
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800973 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -0700974 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800975 clientUid = callingUid;
976 } else if (!isTrustedCallingUid(callingUid)) {
977 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
978 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800979 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
980 "Untrusted caller (calling PID %d, UID %d) trying to "
981 "forward camera access to camera %s for client %s (PID %d, UID %d)",
982 callingPid, callingUid, cameraId.string(),
983 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700984 }
985
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800986 // Check if we can trust clientPid
987 if (clientPid == USE_CALLING_PID) {
988 clientPid = callingPid;
989 } else if (!isTrustedCallingUid(callingUid)) {
990 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
991 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800992 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
993 "Untrusted caller (calling PID %d, UID %d) trying to "
994 "forward camera access to camera %s for client %s (PID %d, UID %d)",
995 callingPid, callingUid, cameraId.string(),
996 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800997 }
998
999 // If it's not calling from cameraserver, check the permission.
1000 if (callingPid != getpid() &&
1001 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
1002 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001003 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1004 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1005 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001006 }
1007
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001008 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1009 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001010 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001011 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012
Ruben Brunk6267b532015-04-30 17:44:07 -07001013 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001014
Ruben Brunka8ca9152015-04-07 14:23:40 -07001015 // Only allow clients who are being used by the current foreground device user, unless calling
1016 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001017 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1018 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1019 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1020 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001021 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1022 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1023 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001024 }
1025
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001026 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001027}
1028
1029status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1030 auto cameraState = getCameraState(cameraId);
1031 int callingPid = getCallingPid();
1032 if (cameraState == nullptr) {
1033 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1034 cameraId.string());
1035 return -ENODEV;
1036 }
1037
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001038 int32_t currentStatus = cameraState->getStatus();
Igor Murashkincba2c162013-03-20 15:56:31 -07001039 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001040 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1041 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001042 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -07001043 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001044 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1045 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001046 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001047 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001048
Ruben Brunkcc776712015-02-17 20:18:47 -08001049 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001050}
1051
Ruben Brunkcc776712015-02-17 20:18:47 -08001052void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1053 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001054
Ruben Brunkcc776712015-02-17 20:18:47 -08001055 // Make a descriptor for the incoming client
1056 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1057 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1058
1059 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1060 String8(client->getPackageName()));
1061
1062 if (evicted.size() > 0) {
1063 // This should never happen - clients should already have been removed in disconnect
1064 for (auto& i : evicted) {
1065 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1066 __FUNCTION__, i->getKey().string());
1067 }
1068
1069 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1070 __FUNCTION__);
1071 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001072
1073 // And register a death notification for the client callback. Do
1074 // this last to avoid Binder policy where a nested Binder
1075 // transaction might be pre-empted to service the client death
1076 // notification if the client process dies before linkToDeath is
1077 // invoked.
1078 sp<IBinder> remoteCallback = client->getRemote();
1079 if (remoteCallback != nullptr) {
1080 remoteCallback->linkToDeath(this);
1081 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001082}
1083
1084status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1085 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1086 /*out*/
1087 sp<BasicClient>* client,
1088 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001089 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001090 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001091 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001092 DescriptorPtr clientDescriptor;
1093 {
1094 if (effectiveApiLevel == API_1) {
1095 // If we are using API1, any existing client for this camera ID with the same remote
1096 // should be returned rather than evicted to allow MediaRecorder to work properly.
1097
1098 auto current = mActiveClientManager.get(cameraId);
1099 if (current != nullptr) {
1100 auto clientSp = current->getValue();
1101 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001102 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1103 ALOGW("CameraService connect called from same client, but with a different"
1104 " API level, evicting prior client...");
1105 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001106 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001107 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001108 *client = clientSp;
1109 return NO_ERROR;
1110 }
1111 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001112 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001113 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001114
Ruben Brunkcc776712015-02-17 20:18:47 -08001115 // Get current active client PIDs
1116 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1117 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001118
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001119 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1120 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1121 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1122 // not have an out-of-class definition.
1123 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001124
Ruben Brunkcc776712015-02-17 20:18:47 -08001125 // Get priorites of all active PIDs
1126 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1127 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001128
Ruben Brunkcc776712015-02-17 20:18:47 -08001129 // Update all active clients' priorities
1130 std::map<int,int> pidToPriorityMap;
1131 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1132 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1133 }
1134 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001135
Ruben Brunkcc776712015-02-17 20:18:47 -08001136 // Get state for the given cameraId
1137 auto state = getCameraState(cameraId);
1138 if (state == nullptr) {
1139 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1140 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001141 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001142 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001143 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001144
1145 // Make descriptor for incoming client
1146 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1147 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1148 state->getConflicting(),
1149 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1150
1151 // Find clients that would be evicted
1152 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1153
1154 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1155 // background, so we cannot do evictions
1156 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1157 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1158 " priority).", clientPid);
1159
1160 sp<BasicClient> clientSp = clientDescriptor->getValue();
1161 String8 curTime = getFormattedCurrentTime();
1162 auto incompatibleClients =
1163 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1164
1165 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001166 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001167 cameraId.string(), packageName.string(), clientPid,
1168 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1169
1170 for (auto& i : incompatibleClients) {
1171 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1172 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1173 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1174 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001175 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1176 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1177 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1178 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001179 }
1180
1181 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001182 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001183 mEventLog.add(msg);
1184
1185 return -EBUSY;
1186 }
1187
1188 for (auto& i : evicted) {
1189 sp<BasicClient> clientSp = i->getValue();
1190 if (clientSp.get() == nullptr) {
1191 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1192
1193 // TODO: Remove this
1194 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1195 __FUNCTION__);
1196 mActiveClientManager.remove(i);
1197 continue;
1198 }
1199
1200 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1201 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001202 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001203
Ruben Brunkcc776712015-02-17 20:18:47 -08001204 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001205 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1206 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1207 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001208 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1209 i->getOwnerId(), i->getPriority(), cameraId.string(),
1210 packageName.string(), clientPid,
1211 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1212
1213 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001214 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001215 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001216 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001217 }
1218
Ruben Brunkcc776712015-02-17 20:18:47 -08001219 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1220 // other clients from connecting in mServiceLockWrapper if held
1221 mServiceLock.unlock();
1222
1223 // Clear caller identity temporarily so client disconnect PID checks work correctly
1224 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1225
1226 // Destroy evicted clients
1227 for (auto& i : evictedClients) {
1228 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001229 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001230 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001231
1232 IPCThreadState::self()->restoreCallingIdentity(token);
1233
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001234 for (const auto& i : evictedClients) {
1235 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1236 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1237 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1238 if (ret == TIMED_OUT) {
1239 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1240 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1241 mActiveClientManager.toString().string());
1242 return -EBUSY;
1243 }
1244 if (ret != NO_ERROR) {
1245 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1246 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1247 ret, mActiveClientManager.toString().string());
1248 return ret;
1249 }
1250 }
1251
1252 evictedClients.clear();
1253
Ruben Brunkcc776712015-02-17 20:18:47 -08001254 // Once clients have been disconnected, relock
1255 mServiceLock.lock();
1256
1257 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1258 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1259 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001260 }
1261
Ruben Brunkcc776712015-02-17 20:18:47 -08001262 *partial = clientDescriptor;
1263 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001264}
1265
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001266Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001267 const sp<ICameraClient>& cameraClient,
1268 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001269 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001270 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001271 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001272 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001273 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001274
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001275 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001276 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001277 String8 id = String8::format("%d", cameraId);
1278 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001279 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1280 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1281 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1282 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001283
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001284 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001285 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001286 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001287 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001288 }
1289
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001290 *device = client;
1291 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001292}
1293
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001294Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001295 const sp<ICameraClient>& cameraClient,
1296 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001297 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001298 int clientUid,
1299 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001300 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001301
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001302 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001303 String8 id = String8::format("%d", cameraId);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08001304 int apiVersion = mModule->getModuleApiVersion();
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001305 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001306 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001307 /*
1308 * Either the HAL version is unspecified in which case this just creates
1309 * a camera client selected by the latest device version, or
1310 * it's a particular version in which case the HAL must supported
1311 * the open_legacy call
1312 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001313 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1314 apiVersion);
1315 ALOGE("%s: %s",
1316 __FUNCTION__, msg.string());
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001317 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001318 msg);
1319 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001320 }
1321
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001322 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001323 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001324 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1325 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1326 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1327 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001328
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001329 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001330 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001331 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001332 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001333 }
1334
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001335 *device = client;
1336 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001337}
1338
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001339Status CameraService::connectDevice(
1340 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Ruben Brunkcc776712015-02-17 20:18:47 -08001341 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001342 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001343 int clientUid,
1344 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001345 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001346
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001347 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001348 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001349 String8 id = String8::format("%d", cameraId);
1350 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001351 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1352 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1353 clientUid, USE_CALLING_PID, API_2,
1354 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1355 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001356
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001357 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001358 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001359 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001360 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001361 }
1362
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001363 *device = client;
1364 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001365}
1366
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001367Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001368 const sp<IBinder>& clientBinder) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001369
1370 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001371 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001372 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001373 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1374 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001375 }
1376
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001377 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001378 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001379
1380 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001381 auto state = getCameraState(id);
1382 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001383 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001384 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1385 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001386 }
1387
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001388 int32_t cameraStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -08001389 if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
1390 cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001391 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001392 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1393 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001394 }
1395
1396 {
1397 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001398 int32_t status;
1399 status_t err = getTorchStatusLocked(id, &status);
1400 if (err != OK) {
1401 if (err == NAME_NOT_FOUND) {
1402 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1403 "Camera \"%s\" does not have a flash unit", id.string());
1404 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001405 ALOGE("%s: getting current torch status failed for camera %s",
1406 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001407 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1408 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1409 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001410 }
1411
1412 if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001413 if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001414 ALOGE("%s: torch mode of camera %s is not available because "
1415 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001416 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1417 "Torch for camera \"%s\" is not available due to an existing camera user",
1418 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001419 } else {
1420 ALOGE("%s: torch mode of camera %s is not available due to "
1421 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001422 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1423 "Torch for camera \"%s\" is not available due to insufficient resources",
1424 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001425 }
1426 }
1427 }
1428
Ruben Brunk99e69712015-05-26 17:25:07 -07001429 {
1430 // Update UID map - this is used in the torch status changed callbacks, so must be done
1431 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001432 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001433 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1434 mTorchUidMap[id].first = uid;
1435 mTorchUidMap[id].second = uid;
1436 } else {
1437 // Set the pending UID
1438 mTorchUidMap[id].first = uid;
1439 }
1440 }
1441
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001443
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001444 if (err != OK) {
1445 int32_t errorCode;
1446 String8 msg;
1447 switch (err) {
1448 case -ENOSYS:
1449 msg = String8::format("Camera \"%s\" has no flashlight",
1450 id.string());
1451 errorCode = ERROR_ILLEGAL_ARGUMENT;
1452 break;
1453 default:
1454 msg = String8::format(
1455 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1456 id.string(), enabled, strerror(-err), err);
1457 errorCode = ERROR_INVALID_OPERATION;
1458 }
1459 ALOGE("%s: %s", __FUNCTION__, msg.string());
1460 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001461 }
1462
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001463 {
1464 // update the link to client's death
1465 Mutex::Autolock al(mTorchClientMapMutex);
1466 ssize_t index = mTorchClientMap.indexOfKey(id);
1467 if (enabled) {
1468 if (index == NAME_NOT_FOUND) {
1469 mTorchClientMap.add(id, clientBinder);
1470 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001471 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001472 mTorchClientMap.replaceValueAt(index, clientBinder);
1473 }
1474 clientBinder->linkToDeath(this);
1475 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001476 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001477 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001478 }
1479
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001480 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001481}
1482
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001483Status CameraService::notifySystemEvent(int32_t eventId,
1484 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001485 ATRACE_CALL();
1486
Ruben Brunk36597b22015-03-20 22:15:57 -07001487 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001488 case ICameraService::EVENT_USER_SWITCHED: {
1489 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001490 break;
1491 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001492 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001493 default: {
1494 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1495 eventId);
1496 break;
1497 }
1498 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001499 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001500}
1501
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001502Status CameraService::addListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001503 ATRACE_CALL();
1504
Igor Murashkinbfc99152013-02-27 12:55:20 -08001505 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001506
Ruben Brunk3450ba72015-06-16 11:00:37 -07001507 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001508 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001509 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001510 }
1511
Igor Murashkinbfc99152013-02-27 12:55:20 -08001512 Mutex::Autolock lock(mServiceLock);
1513
Ruben Brunkcc776712015-02-17 20:18:47 -08001514 {
1515 Mutex::Autolock lock(mStatusListenerLock);
1516 for (auto& it : mListenerList) {
1517 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1518 ALOGW("%s: Tried to add listener %p which was already subscribed",
1519 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001520 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001521 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001522 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001523
1524 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001525 }
1526
Igor Murashkinbfc99152013-02-27 12:55:20 -08001527
Igor Murashkincba2c162013-03-20 15:56:31 -07001528 /* Immediately signal current status to this listener only */
1529 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001530 Mutex::Autolock lock(mCameraStatesLock);
1531 for (auto& i : mCameraStates) {
1532 // TODO: Update binder to use String16 for camera IDs and remove;
1533 int id = cameraIdToInt(i.first);
1534 if (id == -1) continue;
1535
1536 listener->onStatusChanged(i.second->getStatus(), id);
Igor Murashkincba2c162013-03-20 15:56:31 -07001537 }
1538 }
1539
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001540 /* Immediately signal current torch status to this listener only */
1541 {
1542 Mutex::Autolock al(mTorchStatusMutex);
1543 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001544 String16 id = String16(mTorchStatusMap.keyAt(i).string());
1545 listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001546 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001547 }
1548
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001549 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001550}
Ruben Brunkcc776712015-02-17 20:18:47 -08001551
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001552Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001553 ATRACE_CALL();
1554
Igor Murashkinbfc99152013-02-27 12:55:20 -08001555 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1556
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001557 if (listener == 0) {
1558 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001559 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001560 }
1561
Igor Murashkinbfc99152013-02-27 12:55:20 -08001562 Mutex::Autolock lock(mServiceLock);
1563
Ruben Brunkcc776712015-02-17 20:18:47 -08001564 {
1565 Mutex::Autolock lock(mStatusListenerLock);
1566 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1567 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1568 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001569 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001570 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001571 }
1572 }
1573
1574 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1575 __FUNCTION__, listener.get());
1576
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001577 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001578}
1579
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001580Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001581
1582 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001583 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1584
1585 if (parameters == NULL) {
1586 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001587 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001588 }
1589
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001590 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001591
1592 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001593 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001594 // Error logged by caller
1595 return ret;
1596 }
1597
1598 String8 shimParamsString8 = shimParams.flatten();
1599 String16 shimParamsString16 = String16(shimParamsString8);
1600
1601 *parameters = shimParamsString16;
1602
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001603 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001604}
1605
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001606Status CameraService::supportsCameraApi(int cameraId, int apiVersion, bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001607 ATRACE_CALL();
1608
Igor Murashkin65d14b92014-06-17 12:03:20 -07001609 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1610
1611 switch (apiVersion) {
1612 case API_VERSION_1:
1613 case API_VERSION_2:
1614 break;
1615 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001616 String8 msg = String8::format("Unknown API version %d", apiVersion);
1617 ALOGE("%s: %s", __FUNCTION__, msg.string());
1618 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001619 }
1620
1621 int facing = -1;
1622 int deviceVersion = getDeviceVersion(cameraId, &facing);
1623
1624 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001625 case CAMERA_DEVICE_API_VERSION_1_0:
1626 case CAMERA_DEVICE_API_VERSION_3_0:
1627 case CAMERA_DEVICE_API_VERSION_3_1:
1628 if (apiVersion == API_VERSION_2) {
1629 ALOGV("%s: Camera id %d uses HAL version %d <3.2, doesn't support api2 without shim",
1630 __FUNCTION__, cameraId, deviceVersion);
1631 *isSupported = false;
1632 } else { // if (apiVersion == API_VERSION_1) {
1633 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported",
1634 __FUNCTION__, cameraId);
1635 *isSupported = true;
1636 }
1637 break;
1638 case CAMERA_DEVICE_API_VERSION_3_2:
1639 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001640 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001641 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
Igor Murashkin65d14b92014-06-17 12:03:20 -07001642 __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001643 *isSupported = true;
1644 break;
1645 case -1: {
1646 String8 msg = String8::format("Unknown camera ID %d", cameraId);
1647 ALOGE("%s: %s", __FUNCTION__, msg.string());
1648 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001649 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001650 default: {
1651 String8 msg = String8::format("Unknown device version %d for device %d",
1652 deviceVersion, cameraId);
1653 ALOGE("%s: %s", __FUNCTION__, msg.string());
1654 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1655 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001656 }
1657
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001658 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001659}
1660
Ruben Brunkcc776712015-02-17 20:18:47 -08001661void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001662 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001663 for (auto& i : mActiveClientManager.getAll()) {
1664 auto clientSp = i->getValue();
1665 if (clientSp.get() == client) {
1666 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001667 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001668 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001669}
1670
Ruben Brunkcc776712015-02-17 20:18:47 -08001671bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1672 const int callingPid = getCallingPid();
1673 const int servicePid = getpid();
1674 bool ret = false;
1675 {
1676 // Acquire mServiceLock and prevent other clients from connecting
1677 std::unique_ptr<AutoConditionLock> lock =
1678 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001679
Igor Murashkin634a5152013-02-20 17:15:11 -08001680
Ruben Brunkcc776712015-02-17 20:18:47 -08001681 std::vector<sp<BasicClient>> evicted;
1682 for (auto& i : mActiveClientManager.getAll()) {
1683 auto clientSp = i->getValue();
1684 if (clientSp.get() == nullptr) {
1685 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1686 mActiveClientManager.remove(i);
1687 continue;
1688 }
1689 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1690 callingPid == clientSp->getClientPid())) {
1691 mActiveClientManager.remove(i);
1692 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001693
Ruben Brunkcc776712015-02-17 20:18:47 -08001694 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001695 clientSp->notifyError(
1696 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001697 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001698 }
1699 }
1700
Ruben Brunkcc776712015-02-17 20:18:47 -08001701 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1702 // other clients from connecting in mServiceLockWrapper if held
1703 mServiceLock.unlock();
1704
Ruben Brunk36597b22015-03-20 22:15:57 -07001705 // Do not clear caller identity, remote caller should be client proccess
1706
Ruben Brunkcc776712015-02-17 20:18:47 -08001707 for (auto& i : evicted) {
1708 if (i.get() != nullptr) {
1709 i->disconnect();
1710 ret = true;
1711 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001712 }
1713
Ruben Brunkcc776712015-02-17 20:18:47 -08001714 // Reacquire mServiceLock
1715 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001716
Ruben Brunkcc776712015-02-17 20:18:47 -08001717 } // lock is destroyed, allow further connect calls
1718
1719 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001720}
1721
Igor Murashkinecf17e82012-10-02 16:05:11 -07001722
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001723/**
1724 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001725 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001726 */
1727int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001728 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1729 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1730 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1731 // Verify the device version is in the supported range
1732 switch (info.device_version) {
1733 case CAMERA_DEVICE_API_VERSION_1_0:
1734 case CAMERA_DEVICE_API_VERSION_3_0:
1735 case CAMERA_DEVICE_API_VERSION_3_1:
1736 case CAMERA_DEVICE_API_VERSION_3_2:
1737 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001738 case CAMERA_DEVICE_API_VERSION_3_4:
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001739 // in support
1740 break;
1741 case CAMERA_DEVICE_API_VERSION_2_0:
1742 case CAMERA_DEVICE_API_VERSION_2_1:
1743 // no longer supported
1744 default:
1745 ALOGE("%s: Device %d has HAL version %x, which is not supported",
1746 __FUNCTION__, id, info.device_version);
1747 String8 msg = String8::format(
1748 "Unsupported device HAL version %x for device %d",
1749 info.device_version, id);
1750 logServiceError(msg.string(), NO_INIT);
1751 return NO_INIT;
1752 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001753 }
1754
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001755 // Assume all devices pre-v3.3 are backward-compatible
1756 bool isBackwardCompatible = true;
1757 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
1758 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
1759 isBackwardCompatible = false;
1760 status_t res;
1761 camera_metadata_ro_entry_t caps;
1762 res = find_camera_metadata_ro_entry(
1763 info.static_camera_characteristics,
1764 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1765 &caps);
1766 if (res != 0) {
1767 ALOGW("%s: Unable to find camera capabilities for camera device %d",
1768 __FUNCTION__, id);
1769 caps.count = 0;
1770 }
1771 for (size_t i = 0; i < caps.count; i++) {
1772 if (caps.data.u8[i] ==
1773 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
1774 isBackwardCompatible = true;
1775 break;
1776 }
1777 }
1778 }
1779
1780 if (!isBackwardCompatible) {
1781 mNumberOfNormalCameras--;
1782 *latestStrangeCameraId = id;
1783 } else {
1784 if (id > *latestStrangeCameraId) {
1785 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
1786 "This is not allowed due backward-compatibility requirements",
1787 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001788 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001789 mNumberOfCameras = 0;
1790 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001791 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001792 }
1793 }
1794 return OK;
1795}
1796
Ruben Brunkcc776712015-02-17 20:18:47 -08001797std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1798 const String8& cameraId) const {
1799 std::shared_ptr<CameraState> state;
1800 {
1801 Mutex::Autolock lock(mCameraStatesLock);
1802 auto iter = mCameraStates.find(cameraId);
1803 if (iter != mCameraStates.end()) {
1804 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001805 }
1806 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001807 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001808}
1809
Ruben Brunkcc776712015-02-17 20:18:47 -08001810sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1811 // Remove from active clients list
1812 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1813 if (clientDescriptorPtr == nullptr) {
1814 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1815 cameraId.string());
1816 return sp<BasicClient>{nullptr};
1817 }
1818
1819 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001820}
1821
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001822void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001823 // Acquire mServiceLock and prevent other clients from connecting
1824 std::unique_ptr<AutoConditionLock> lock =
1825 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1826
Ruben Brunk6267b532015-04-30 17:44:07 -07001827 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001828 for (size_t i = 0; i < newUserIds.size(); i++) {
1829 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001830 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001831 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07001832 return;
1833 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001834 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001835 }
1836
Ruben Brunka8ca9152015-04-07 14:23:40 -07001837
Ruben Brunk6267b532015-04-30 17:44:07 -07001838 if (newAllowedUsers == mAllowedUsers) {
1839 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1840 return;
1841 }
1842
1843 logUserSwitch(mAllowedUsers, newAllowedUsers);
1844
1845 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001846
1847 // Current user has switched, evict all current clients.
1848 std::vector<sp<BasicClient>> evicted;
1849 for (auto& i : mActiveClientManager.getAll()) {
1850 auto clientSp = i->getValue();
1851
1852 if (clientSp.get() == nullptr) {
1853 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1854 continue;
1855 }
1856
Ruben Brunk6267b532015-04-30 17:44:07 -07001857 // Don't evict clients that are still allowed.
1858 uid_t clientUid = clientSp->getClientUid();
1859 userid_t clientUserId = multiuser_get_user_id(clientUid);
1860 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1861 continue;
1862 }
1863
Ruben Brunk36597b22015-03-20 22:15:57 -07001864 evicted.push_back(clientSp);
1865
1866 String8 curTime = getFormattedCurrentTime();
1867
1868 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1869 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001870
Ruben Brunk36597b22015-03-20 22:15:57 -07001871 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001872 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07001873 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07001874 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1875 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001876
1877 }
1878
1879 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1880 // blocking other clients from connecting in mServiceLockWrapper if held.
1881 mServiceLock.unlock();
1882
1883 // Clear caller identity temporarily so client disconnect PID checks work correctly
1884 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1885
1886 for (auto& i : evicted) {
1887 i->disconnect();
1888 }
1889
1890 IPCThreadState::self()->restoreCallingIdentity(token);
1891
1892 // Reacquire mServiceLock
1893 mServiceLock.lock();
1894}
Ruben Brunkcc776712015-02-17 20:18:47 -08001895
Ruben Brunka8ca9152015-04-07 14:23:40 -07001896void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001897 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001898 Mutex::Autolock l(mLogLock);
1899 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001900}
1901
Ruben Brunka8ca9152015-04-07 14:23:40 -07001902void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001903 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001904 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001905 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001906 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001907}
1908
1909void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001910 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001911 // Log the clients evicted
1912 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001913 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001914}
1915
1916void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001917 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001918 // Log the client rejected
1919 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001920 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001921}
1922
Ruben Brunk6267b532015-04-30 17:44:07 -07001923void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1924 const std::set<userid_t>& newUserIds) {
1925 String8 newUsers = toString(newUserIds);
1926 String8 oldUsers = toString(oldUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001927 // Log the new and old users
Ruben Brunk6267b532015-04-30 17:44:07 -07001928 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
1929 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001930}
1931
1932void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1933 // Log the device removal
1934 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1935}
1936
1937void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1938 // Log the device removal
1939 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1940}
1941
1942void CameraService::logClientDied(int clientPid, const char* reason) {
1943 // Log the device removal
1944 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001945}
1946
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001947void CameraService::logServiceError(const char* msg, int errorCode) {
1948 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001949 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001950}
1951
Ruben Brunk36597b22015-03-20 22:15:57 -07001952status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1953 uint32_t flags) {
1954
1955 const int pid = getCallingPid();
1956 const int selfPid = getpid();
1957
Mathias Agopian65ab4712010-07-14 17:59:35 -07001958 // Permission checks
1959 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001960 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07001961 if (pid != selfPid) {
1962 // Ensure we're being called by system_server, or similar process with
1963 // permissions to notify the camera service about system events
1964 if (!checkCallingPermission(
1965 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1966 const int uid = getCallingUid();
1967 ALOGE("Permission Denial: cannot send updates to camera service about system"
1968 " events from pid=%d, uid=%d", pid, uid);
1969 return PERMISSION_DENIED;
1970 }
1971 }
1972 break;
1973 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001974 }
1975
1976 return BnCameraService::onTransact(code, data, reply, flags);
1977}
1978
Mathias Agopian65ab4712010-07-14 17:59:35 -07001979// We share the media players for shutter and recording sound for all clients.
1980// A reference count is kept to determine when we will actually release the
1981// media players.
1982
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001983MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001984 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001985 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001986 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001987 mp->prepare();
1988 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001989 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001990 return NULL;
1991 }
1992 return mp;
1993}
1994
1995void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001996 ATRACE_CALL();
1997
Mathias Agopian65ab4712010-07-14 17:59:35 -07001998 Mutex::Autolock lock(mSoundLock);
1999 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2000 if (mSoundRef++) return;
2001
2002 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002003 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2004 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002005}
2006
2007void CameraService::releaseSound() {
2008 Mutex::Autolock lock(mSoundLock);
2009 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2010 if (--mSoundRef) return;
2011
2012 for (int i = 0; i < NUM_SOUNDS; i++) {
2013 if (mSoundPlayer[i] != 0) {
2014 mSoundPlayer[i]->disconnect();
2015 mSoundPlayer[i].clear();
2016 }
2017 }
2018}
2019
2020void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002021 ATRACE_CALL();
2022
Mathias Agopian65ab4712010-07-14 17:59:35 -07002023 LOG1("playSound(%d)", kind);
2024 Mutex::Autolock lock(mSoundLock);
2025 sp<MediaPlayer> player = mSoundPlayer[kind];
2026 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002027 player->seekTo(0);
2028 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002029 }
2030}
2031
2032// ----------------------------------------------------------------------------
2033
2034CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002035 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002036 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002037 int cameraId, int cameraFacing,
2038 int clientPid, uid_t clientUid,
2039 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002040 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002041 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002042 clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002043 cameraId, cameraFacing,
2044 clientPid, clientUid,
2045 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08002046{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002047 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002048 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002049
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002050 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002051
Mathias Agopian65ab4712010-07-14 17:59:35 -07002052 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002053
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002054 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002055}
2056
Mathias Agopian65ab4712010-07-14 17:59:35 -07002057// tear down the client
2058CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002059 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002060 mDestructionStarted = true;
2061
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -07002062 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002063 // unconditionally disconnect. function is idempotent
2064 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002065}
2066
Igor Murashkin634a5152013-02-20 17:15:11 -08002067CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002068 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002069 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002070 int cameraId, int cameraFacing,
2071 int clientPid, uid_t clientUid,
2072 int servicePid):
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002073 mClientPackageName(clientPackageName), mDisconnected(false)
Igor Murashkin634a5152013-02-20 17:15:11 -08002074{
2075 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002076 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -08002077 mCameraId = cameraId;
2078 mCameraFacing = cameraFacing;
2079 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002080 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -08002081 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002082 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002083 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002084
2085 // In some cases the calling code has no access to the package it runs under.
2086 // For example, NDK camera API.
2087 // In this case we will get the packages for the calling UID and pick the first one
2088 // for attributing the app op. This will work correctly for runtime permissions
2089 // as for legacy apps we will toggle the app op for all packages in the UID.
2090 // The caveat is that the operation may be attributed to the wrong package and
2091 // stats based on app ops may be slightly off.
2092 if (mClientPackageName.size() <= 0) {
2093 sp<IServiceManager> sm = defaultServiceManager();
2094 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2095 if (binder == 0) {
2096 ALOGE("Cannot get permission service");
2097 // Leave mClientPackageName unchanged (empty) and the further interaction
2098 // with camera will fail in BasicClient::startCameraOps
2099 return;
2100 }
2101
2102 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2103 Vector<String16> packages;
2104
2105 permCtrl->getPackagesForUid(mClientUid, packages);
2106
2107 if (packages.isEmpty()) {
2108 ALOGE("No packages for calling UID");
2109 // Leave mClientPackageName unchanged (empty) and the further interaction
2110 // with camera will fail in BasicClient::startCameraOps
2111 return;
2112 }
2113 mClientPackageName = packages[0];
2114 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002115}
2116
2117CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002118 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002119 mDestructionStarted = true;
2120}
2121
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002122binder::Status CameraService::BasicClient::disconnect() {
2123 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002124 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002125 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002126 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002127 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002128
2129 mCameraService->removeByClient(this);
2130 mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002131 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002132
2133 sp<IBinder> remote = getRemote();
2134 if (remote != nullptr) {
2135 remote->unlinkToDeath(mCameraService);
2136 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002137
2138 finishCameraOps();
Ruben Brunkcc776712015-02-17 20:18:47 -08002139 ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid);
2140
Igor Murashkincba2c162013-03-20 15:56:31 -07002141 // client shouldn't be able to call into us anymore
2142 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002143
2144 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002145}
2146
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002147status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2148 // No dumping of clients directly over Binder,
2149 // must go through CameraService::dump
2150 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2151 IPCThreadState::self()->getCallingUid(), NULL, 0);
2152 return OK;
2153}
2154
Ruben Brunkcc776712015-02-17 20:18:47 -08002155String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002156 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002157}
2158
2159
2160int CameraService::BasicClient::getClientPid() const {
2161 return mClientPid;
2162}
2163
Ruben Brunk6267b532015-04-30 17:44:07 -07002164uid_t CameraService::BasicClient::getClientUid() const {
2165 return mClientUid;
2166}
2167
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002168bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2169 // Defaults to API2.
2170 return level == API_2;
2171}
2172
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002173status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002174 ATRACE_CALL();
2175
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002176 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002177 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002178 mOpsCallback = new OpsCallback(this);
2179
Igor Murashkine6800ce2013-03-04 17:25:57 -08002180 {
2181 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002182 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002183 }
2184
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002185 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002186 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002187 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002188 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002189
Svetoslav28e8ef72015-05-11 19:21:31 -07002190 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002191 ALOGI("Camera %d: Access for \"%s\" has been revoked",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002192 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002193 return PERMISSION_DENIED;
2194 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002195
Svetoslav28e8ef72015-05-11 19:21:31 -07002196 if (res == AppOpsManager::MODE_IGNORED) {
2197 ALOGI("Camera %d: Access for \"%s\" has been restricted",
2198 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002199 // Return the same error as for device policy manager rejection
2200 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002201 }
2202
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002203 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002204
2205 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
2206 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
Ruben Brunkcc776712015-02-17 20:18:47 -08002207 String8::format("%d", mCameraId));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002208
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002209 // Transition device state to OPEN
2210 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2211 String8::format("%d", mCameraId));
2212
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002213 return OK;
2214}
2215
2216status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002217 ATRACE_CALL();
2218
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002219 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002220 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002221 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002222 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002223 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002224 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002225
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002226 std::initializer_list<int32_t> rejected = {ICameraServiceListener::STATUS_NOT_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002227 ICameraServiceListener::STATUS_ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002228
Ruben Brunkcc776712015-02-17 20:18:47 -08002229 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002230 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002231 String8::format("%d", mCameraId), rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002232
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002233 // Transition device state to CLOSED
2234 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2235 String8::format("%d", mCameraId));
2236
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002237 // Notify flashlight that a camera device is closed.
2238 mCameraService->mFlashlight->deviceClosed(
2239 String8::format("%d", mCameraId));
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002240 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002241 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002242 if (mOpsCallback != NULL) {
2243 mAppOpsManager.stopWatchingMode(mOpsCallback);
2244 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002245 mOpsCallback.clear();
2246
2247 return OK;
2248}
2249
2250void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002251 ATRACE_CALL();
2252
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002253 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002254 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002255
2256 if (op != AppOpsManager::OP_CAMERA) {
2257 ALOGW("Unexpected app ops notification received: %d", op);
2258 return;
2259 }
2260
2261 int32_t res;
2262 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002263 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002264 ALOGV("checkOp returns: %d, %s ", res,
2265 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2266 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2267 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2268 "UNKNOWN");
2269
2270 if (res != AppOpsManager::MODE_ALLOWED) {
2271 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
2272 myName.string());
2273 // Reset the client PID to allow server-initiated disconnect,
2274 // and to prevent further calls by client.
2275 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002276 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002277 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002278 disconnect();
2279 }
2280}
2281
Mathias Agopian65ab4712010-07-14 17:59:35 -07002282// ----------------------------------------------------------------------------
2283
Ruben Brunkcc776712015-02-17 20:18:47 -08002284// Provide client strong pointer for callbacks.
2285sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
2286 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
2287 auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId);
2288 if (clientDescriptor != nullptr) {
2289 return sp<Client>{
2290 static_cast<Client*>(clientDescriptor->getValue().get())};
2291 }
2292 return sp<Client>{nullptr};
Mathias Agopian65ab4712010-07-14 17:59:35 -07002293}
2294
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002295void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002296 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002297 (void) errorCode;
2298 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002299 if (mRemoteCallback != NULL) {
2300 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2301 } else {
2302 ALOGE("mRemoteCallback is NULL!!");
2303 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002304}
2305
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002306// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002307binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002308 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002309 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002310}
2311
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002312bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2313 return level == API_1;
2314}
2315
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002316CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2317 mClient(client) {
2318}
2319
2320void CameraService::Client::OpsCallback::opChanged(int32_t op,
2321 const String16& packageName) {
2322 sp<BasicClient> client = mClient.promote();
2323 if (client != NULL) {
2324 client->opChanged(op, packageName);
2325 }
2326}
2327
Mathias Agopian65ab4712010-07-14 17:59:35 -07002328// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002329// CameraState
2330// ----------------------------------------------------------------------------
2331
2332CameraService::CameraState::CameraState(const String8& id, int cost,
2333 const std::set<String8>& conflicting) : mId(id),
2334 mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {}
2335
2336CameraService::CameraState::~CameraState() {}
2337
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002338int32_t CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002339 Mutex::Autolock lock(mStatusLock);
2340 return mStatus;
2341}
2342
2343CameraParameters CameraService::CameraState::getShimParams() const {
2344 return mShimParams;
2345}
2346
2347void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2348 mShimParams = params;
2349}
2350
2351int CameraService::CameraState::getCost() const {
2352 return mCost;
2353}
2354
2355std::set<String8> CameraService::CameraState::getConflicting() const {
2356 return mConflicting;
2357}
2358
2359String8 CameraService::CameraState::getId() const {
2360 return mId;
2361}
2362
2363// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002364// ClientEventListener
2365// ----------------------------------------------------------------------------
2366
2367void CameraService::ClientEventListener::onClientAdded(
2368 const resource_policy::ClientDescriptor<String8,
2369 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002370 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002371 if (basicClient.get() != nullptr) {
2372 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2373 notifier.noteStartCamera(descriptor.getKey(),
2374 static_cast<int>(basicClient->getClientUid()));
2375 }
2376}
2377
2378void CameraService::ClientEventListener::onClientRemoved(
2379 const resource_policy::ClientDescriptor<String8,
2380 sp<CameraService::BasicClient>>& descriptor) {
Chih-Hung Hsieh5404ee12016-08-09 14:25:53 -07002381 const auto& basicClient = descriptor.getValue();
Ruben Brunk99e69712015-05-26 17:25:07 -07002382 if (basicClient.get() != nullptr) {
2383 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2384 notifier.noteStopCamera(descriptor.getKey(),
2385 static_cast<int>(basicClient->getClientUid()));
2386 }
2387}
2388
2389
2390// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002391// CameraClientManager
2392// ----------------------------------------------------------------------------
2393
Ruben Brunk99e69712015-05-26 17:25:07 -07002394CameraService::CameraClientManager::CameraClientManager() {
2395 setListener(std::make_shared<ClientEventListener>());
2396}
2397
Ruben Brunkcc776712015-02-17 20:18:47 -08002398CameraService::CameraClientManager::~CameraClientManager() {}
2399
2400sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2401 const String8& id) const {
2402 auto descriptor = get(id);
2403 if (descriptor == nullptr) {
2404 return sp<BasicClient>{nullptr};
2405 }
2406 return descriptor->getValue();
2407}
2408
2409String8 CameraService::CameraClientManager::toString() const {
2410 auto all = getAll();
2411 String8 ret("[");
2412 bool hasAny = false;
2413 for (auto& i : all) {
2414 hasAny = true;
2415 String8 key = i->getKey();
2416 int32_t cost = i->getCost();
2417 int32_t pid = i->getOwnerId();
2418 int32_t priority = i->getPriority();
2419 auto conflicting = i->getConflicting();
2420 auto clientSp = i->getValue();
2421 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002422 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002423 if (clientSp.get() != nullptr) {
2424 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002425 uid_t clientUid = clientSp->getClientUid();
2426 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002427 }
2428 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2429 PRId32 ", ", key.string(), cost, pid, priority);
2430
Ruben Brunk6267b532015-04-30 17:44:07 -07002431 if (clientSp.get() != nullptr) {
2432 ret.appendFormat("User Id: %d, ", clientUserId);
2433 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002434 if (packageName.size() != 0) {
2435 ret.appendFormat("Client Package Name: %s", packageName.string());
2436 }
2437
2438 ret.append(", Conflicting Client Devices: {");
2439 for (auto& j : conflicting) {
2440 ret.appendFormat("%s, ", j.string());
2441 }
2442 ret.append("})");
2443 }
2444 if (hasAny) ret.append("\n");
2445 ret.append("]\n");
2446 return ret;
2447}
2448
2449CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2450 const String8& key, const sp<BasicClient>& value, int32_t cost,
2451 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2452
2453 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2454 key, value, cost, conflictingKeys, priority, ownerId);
2455}
2456
2457CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2458 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2459 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2460 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2461}
2462
2463// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002464
2465static const int kDumpLockRetries = 50;
2466static const int kDumpLockSleep = 60000;
2467
2468static bool tryLock(Mutex& mutex)
2469{
2470 bool locked = false;
2471 for (int i = 0; i < kDumpLockRetries; ++i) {
2472 if (mutex.tryLock() == NO_ERROR) {
2473 locked = true;
2474 break;
2475 }
2476 usleep(kDumpLockSleep);
2477 }
2478 return locked;
2479}
2480
2481status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002482 ATRACE_CALL();
2483
Ruben Brunka8ca9152015-04-07 14:23:40 -07002484 String8 result("Dump of the Camera Service:\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002485 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
dcashmanfefa6142015-09-09 13:43:01 -07002486 result = result.format("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07002487 "can't dump CameraService from pid=%d, uid=%d\n",
2488 getCallingPid(),
2489 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002490 write(fd, result.string(), result.size());
2491 } else {
2492 bool locked = tryLock(mServiceLock);
2493 // failed to lock - CameraService is probably deadlocked
2494 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002495 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002496 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002497 }
2498
2499 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002500 if (!mModule) {
2501 result = String8::format("No camera module available!\n");
2502 write(fd, result.string(), result.size());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002503
2504 // Dump event log for error information
2505 dumpEventLog(fd);
2506
Kalle Lampila6ec3a152013-04-30 15:27:19 +03002507 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002508 return NO_ERROR;
2509 }
2510
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002511 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2512 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2513 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2514 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
Ruben Brunkcc776712015-02-17 20:18:47 -08002515 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
Eino-Ville Talvala49c97052016-01-12 14:29:40 -08002516 result.appendFormat("Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Ruben Brunkcc776712015-02-17 20:18:47 -08002517 String8 activeClientString = mActiveClientManager.toString();
2518 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
Ruben Brunk6267b532015-04-30 17:44:07 -07002519 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002520
Ruben Brunkf81648e2014-04-17 16:14:57 -07002521 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2522 if (desc == NULL) {
2523 result.appendFormat("Vendor tags left unimplemented.\n");
2524 } else {
2525 result.appendFormat("Vendor tag definitions:\n");
2526 }
2527
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002528 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07002529
2530 if (desc != NULL) {
2531 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
2532 }
2533
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002534 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002535
2536 bool stateLocked = tryLock(mCameraStatesLock);
2537 if (!stateLocked) {
2538 result = String8::format("CameraStates in use, may be deadlocked\n");
2539 write(fd, result.string(), result.size());
2540 }
2541
2542 for (auto& state : mCameraStates) {
2543 String8 cameraId = state.first;
2544 result = String8::format("Camera %s information:\n", cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002545 camera_info info;
2546
Ruben Brunkcc776712015-02-17 20:18:47 -08002547 // TODO: Change getCameraInfo + HAL to use String cameraIds
2548 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002549 if (rc != OK) {
2550 result.appendFormat(" Error reading static information!\n");
2551 write(fd, result.string(), result.size());
2552 } else {
2553 result.appendFormat(" Facing: %s\n",
2554 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
2555 result.appendFormat(" Orientation: %d\n", info.orientation);
2556 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002557 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002558 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2559 } else {
2560 deviceVersion = info.device_version;
2561 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002562
2563 auto conflicting = state.second->getConflicting();
2564 result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
2565 result.appendFormat(" Conflicting Devices:");
2566 for (auto& id : conflicting) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002567 result.appendFormat(" %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002568 }
2569 if (conflicting.size() == 0) {
2570 result.appendFormat(" NONE");
2571 }
2572 result.appendFormat("\n");
2573
2574 result.appendFormat(" Device version: %#x\n", deviceVersion);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002575 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002576 result.appendFormat(" Device static metadata:\n");
2577 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07002578 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07002579 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002580 } else {
2581 write(fd, result.string(), result.size());
2582 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002583
2584 CameraParameters p = state.second->getShimParams();
2585 if (!p.isEmpty()) {
2586 result = String8::format(" Camera1 API shim is using parameters:\n ");
2587 write(fd, result.string(), result.size());
2588 p.dump(fd, args);
2589 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002590 }
2591
Ruben Brunkcc776712015-02-17 20:18:47 -08002592 auto clientDescriptor = mActiveClientManager.get(cameraId);
2593 if (clientDescriptor == nullptr) {
2594 result = String8::format(" Device %s is closed, no client instance\n",
2595 cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002596 write(fd, result.string(), result.size());
2597 continue;
2598 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002599 hasClient = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002600 result = String8::format(" Device %s is open. Client instance dump:\n\n",
2601 cameraId.string());
2602 result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
2603 result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
2604
2605 auto client = clientDescriptor->getValue();
2606 result.appendFormat("Client package: %s\n",
2607 String8(client->getPackageName()).string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002608 write(fd, result.string(), result.size());
Ruben Brunkcc776712015-02-17 20:18:47 -08002609
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002610 client->dumpClient(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002611 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002612
2613 if (stateLocked) mCameraStatesLock.unlock();
2614
Mathias Agopian65ab4712010-07-14 17:59:35 -07002615 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002616 result = String8::format("\nNo active camera clients yet.\n");
2617 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002618 }
2619
2620 if (locked) mServiceLock.unlock();
2621
Igor Murashkinff3e31d2013-10-23 16:40:06 -07002622 // Dump camera traces if there were any
2623 write(fd, "\n", 1);
2624 camera3::CameraTraces::dump(fd, args);
2625
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002626 // Process dump arguments, if any
Mathias Agopian65ab4712010-07-14 17:59:35 -07002627 int n = args.size();
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002628 String16 verboseOption("-v");
2629 String16 unreachableOption("--unreachable");
2630 for (int i = 0; i < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002631 if (args[i] == verboseOption) {
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002632 // change logging level
2633 if (i + 1 >= n) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002634 String8 levelStr(args[i+1]);
2635 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002636 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002637 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002638 write(fd, result.string(), result.size());
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002639 } else if (args[i] == unreachableOption) {
2640 // Dump memory analysis
2641 // TODO - should limit be an argument parameter?
2642 UnreachableMemoryInfo info;
2643 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2644 if (!success) {
2645 dprintf(fd, "\nUnable to dump unreachable memory. "
2646 "Try disabling SELinux enforcement.\n");
2647 } else {
2648 dprintf(fd, "\nDumping unreachable memory:\n");
2649 std::string s = info.ToString(/*log_contents*/ true);
2650 write(fd, s.c_str(), s.size());
2651 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002652 }
2653 }
2654 }
2655 return NO_ERROR;
2656}
2657
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002658void CameraService::dumpEventLog(int fd) {
2659 String8 result = String8("\nPrior client events (most recent at top):\n");
2660
2661 Mutex::Autolock l(mLogLock);
2662 for (const auto& msg : mEventLog) {
2663 result.appendFormat(" %s\n", msg.string());
2664 }
2665
2666 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
2667 result.append(" ...\n");
2668 } else if (mEventLog.size() == 0) {
2669 result.append(" [no events yet]\n");
2670 }
2671 result.append("\n");
2672
2673 write(fd, result.string(), result.size());
2674}
2675
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002676void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002677 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002678 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2679 if (mTorchClientMap[i] == who) {
2680 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002681 String8 cameraId = mTorchClientMap.keyAt(i);
2682 status_t res = mFlashlight->setTorchMode(cameraId, false);
2683 if (res) {
2684 ALOGE("%s: torch client died but couldn't turn off torch: "
2685 "%s (%d)", __FUNCTION__, strerror(-res), res);
2686 return;
2687 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002688 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002689 break;
2690 }
2691 }
2692}
2693
Ruben Brunkcc776712015-02-17 20:18:47 -08002694/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002695
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002696 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002697 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2698 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002699 */
2700
Ruben Brunka8ca9152015-04-07 14:23:40 -07002701 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2702
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002703 // check torch client
2704 handleTorchClientBinderDied(who);
2705
2706 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002707 if(!evictClientIdByRemote(who)) {
2708 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002709 return;
2710 }
2711
Ruben Brunkcc776712015-02-17 20:18:47 -08002712 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2713 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002714}
2715
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002716void CameraService::updateStatus(int32_t status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002717 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002718}
2719
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002720void CameraService::updateStatus(int32_t status, const String8& cameraId,
2721 std::initializer_list<int32_t> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002722 // Do not lock mServiceLock here or can get into a deadlock from
2723 // connect() -> disconnect -> updateStatus
2724
2725 auto state = getCameraState(cameraId);
2726
2727 if (state == nullptr) {
2728 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2729 cameraId.string());
2730 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002731 }
2732
Ruben Brunkcc776712015-02-17 20:18:47 -08002733 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2734 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2735 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002736 (const String8& cameraId, int32_t status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002737
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002738 if (status != ICameraServiceListener::STATUS_ENUMERATING) {
2739 // Update torch status if it has a flash unit.
2740 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002741 int32_t torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002742 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2743 NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002744 int32_t newTorchStatus =
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002745 status == ICameraServiceListener::STATUS_PRESENT ?
2746 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF :
2747 ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
2748 if (torchStatus != newTorchStatus) {
2749 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2750 }
2751 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002752 }
2753
2754 Mutex::Autolock lock(mStatusListenerLock);
2755
2756 for (auto& listener : mListenerList) {
2757 // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
2758 int id = cameraIdToInt(cameraId);
2759 if (id != -1) listener->onStatusChanged(status, id);
2760 }
2761 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002762}
2763
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002764void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
2765 const String8& cameraId) {
2766 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2767 if (proxyBinder == nullptr) return;
2768 String16 id(cameraId);
2769 proxyBinder->notifyCameraState(id, newState);
2770}
2771
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002772status_t CameraService::getTorchStatusLocked(
2773 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002774 int32_t *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002775 if (!status) {
2776 return BAD_VALUE;
2777 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002778 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2779 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002780 // invalid camera ID or the camera doesn't have a flash unit
2781 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002782 }
2783
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002784 *status = mTorchStatusMap.valueAt(index);
2785 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002786}
2787
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002788status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002789 int32_t status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002790 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2791 if (index == NAME_NOT_FOUND) {
2792 return BAD_VALUE;
2793 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002794 int32_t& item =
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002795 mTorchStatusMap.editValueAt(index);
2796 item = status;
2797
2798 return OK;
2799}
2800
Mathias Agopian65ab4712010-07-14 17:59:35 -07002801}; // namespace android