blob: b77bb7b1692b03959988420c2d830f4271f95076 [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) {
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700916 case AID_MEDIA: // mediaserver
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800917 case AID_CAMERASERVER: // cameraserver
Eino-Ville Talvalaaf9d0302016-06-29 14:40:10 -0700918 case AID_RADIO: // telephony
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800919 return true;
920 default:
921 return false;
922 }
923}
924
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800925Status CameraService::validateConnectLocked(const String8& cameraId,
Chien-Yu Chen18df60e2016-03-18 18:18:09 -0700926 const String8& clientName8, /*inout*/int& clientUid, /*inout*/int& clientPid,
927 /*out*/int& originalClientPid) const {
Tyler Luu5861a9a2011-10-06 00:00:03 -0500928
Christopher Wileyce761d12016-02-16 10:15:00 -0800929#if !defined(__BRILLO__)
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700930 Status allowed = validateClientPermissionsLocked(cameraId, clientName8, clientUid, clientPid,
931 originalClientPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800932 if (!allowed.isOk()) {
Christopher Wileyce761d12016-02-16 10:15:00 -0800933 return allowed;
934 }
935#endif // defined(__BRILLO__)
936
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800937 int callingPid = getCallingPid();
938
Iliyan Malchev8951a972011-04-14 16:55:59 -0700939 if (!mModule) {
Ruben Brunkcc776712015-02-17 20:18:47 -0800940 ALOGE("CameraService::connect X (PID %d) rejected (camera HAL module not loaded)",
941 callingPid);
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800942 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
943 "No camera HAL module available to open camera device \"%s\"", cameraId.string());
Iliyan Malchev8951a972011-04-14 16:55:59 -0700944 }
945
Ruben Brunkcc776712015-02-17 20:18:47 -0800946 if (getCameraState(cameraId) == nullptr) {
947 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
948 cameraId.string());
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800949 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
950 "No camera device with ID \"%s\" available", cameraId.string());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700951 }
952
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800953 status_t err = checkIfDeviceIsUsable(cameraId);
954 if (err != NO_ERROR) {
955 switch(err) {
956 case -ENODEV:
957 case -EBUSY:
958 return STATUS_ERROR_FMT(ERROR_DISCONNECTED,
959 "No camera device with ID \"%s\" currently available", cameraId.string());
960 default:
961 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
962 "Unknown error connecting to ID \"%s\"", cameraId.string());
963 }
964 }
965 return Status::ok();
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800966}
967
Eino-Ville Talvala04926862016-03-02 15:42:53 -0800968Status CameraService::validateClientPermissionsLocked(const String8& cameraId,
Chien-Yu Chen7939aee2016-03-21 18:19:33 -0700969 const String8& clientName8, int& clientUid, int& clientPid,
970 /*out*/int& originalClientPid) const {
Christopher Wiley0039bcf2016-02-05 10:29:50 -0800971 int callingPid = getCallingPid();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800972 int callingUid = getCallingUid();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800974 // Check if we can trust clientUid
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 if (clientUid == USE_CALLING_UID) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800976 clientUid = callingUid;
977 } else if (!isTrustedCallingUid(callingUid)) {
978 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
979 "(don't trust clientUid %d)", callingPid, callingUid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800980 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
981 "Untrusted caller (calling PID %d, UID %d) trying to "
982 "forward camera access to camera %s for client %s (PID %d, UID %d)",
983 callingPid, callingUid, cameraId.string(),
984 clientName8.string(), clientUid, clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 }
986
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800987 // Check if we can trust clientPid
988 if (clientPid == USE_CALLING_PID) {
989 clientPid = callingPid;
990 } else if (!isTrustedCallingUid(callingUid)) {
991 ALOGE("CameraService::connect X (calling PID %d, calling UID %d) rejected "
992 "(don't trust clientPid %d)", callingPid, callingUid, clientPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800993 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
994 "Untrusted caller (calling PID %d, UID %d) trying to "
995 "forward camera access to camera %s for client %s (PID %d, UID %d)",
996 callingPid, callingUid, cameraId.string(),
997 clientName8.string(), clientUid, clientPid);
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800998 }
999
1000 // If it's not calling from cameraserver, check the permission.
1001 if (callingPid != getpid() &&
1002 !checkPermission(String16("android.permission.CAMERA"), clientPid, clientUid)) {
1003 ALOGE("Permission Denial: can't use the camera pid=%d, uid=%d", clientPid, clientUid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001004 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1005 "Caller \"%s\" (PID %d, UID %d) cannot open camera \"%s\" without camera permission",
1006 clientName8.string(), clientUid, clientPid, cameraId.string());
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001007 }
1008
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001009 // Only use passed in clientPid to check permission. Use calling PID as the client PID that's
1010 // connected to camera service directly.
Chien-Yu Chen18df60e2016-03-18 18:18:09 -07001011 originalClientPid = clientPid;
Chien-Yu Chen4f3d6202016-03-22 10:50:23 -07001012 clientPid = callingPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001013
Ruben Brunk6267b532015-04-30 17:44:07 -07001014 userid_t clientUserId = multiuser_get_user_id(clientUid);
Wu-cheng Lia3355432011-05-20 14:54:25 +08001015
Ruben Brunka8ca9152015-04-07 14:23:40 -07001016 // Only allow clients who are being used by the current foreground device user, unless calling
1017 // from our own process.
Ruben Brunk6267b532015-04-30 17:44:07 -07001018 if (callingPid != getpid() && (mAllowedUsers.find(clientUserId) == mAllowedUsers.end())) {
1019 ALOGE("CameraService::connect X (PID %d) rejected (cannot connect from "
1020 "device user %d, currently allowed device users: %s)", callingPid, clientUserId,
1021 toString(mAllowedUsers).string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001022 return STATUS_ERROR_FMT(ERROR_PERMISSION_DENIED,
1023 "Callers from device user %d are not currently allowed to connect to camera \"%s\"",
1024 clientUserId, cameraId.string());
Ruben Brunk36597b22015-03-20 22:15:57 -07001025 }
1026
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001027 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001028}
1029
1030status_t CameraService::checkIfDeviceIsUsable(const String8& cameraId) const {
1031 auto cameraState = getCameraState(cameraId);
1032 int callingPid = getCallingPid();
1033 if (cameraState == nullptr) {
1034 ALOGE("CameraService::connect X (PID %d) rejected (invalid camera ID %s)", callingPid,
1035 cameraId.string());
1036 return -ENODEV;
1037 }
1038
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001039 int32_t currentStatus = cameraState->getStatus();
Igor Murashkincba2c162013-03-20 15:56:31 -07001040 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001041 ALOGE("CameraService::connect X (PID %d) rejected (camera %s is not connected)",
1042 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001043 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -07001044 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001045 ALOGE("CameraService::connect X (PID %d) rejected, (camera %s is initializing)",
1046 callingPid, cameraId.string());
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001047 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -07001048 }
Igor Murashkincba2c162013-03-20 15:56:31 -07001049
Ruben Brunkcc776712015-02-17 20:18:47 -08001050 return NO_ERROR;
Igor Murashkine6800ce2013-03-04 17:25:57 -08001051}
1052
Ruben Brunkcc776712015-02-17 20:18:47 -08001053void CameraService::finishConnectLocked(const sp<BasicClient>& client,
1054 const CameraService::DescriptorPtr& desc) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001055
Ruben Brunkcc776712015-02-17 20:18:47 -08001056 // Make a descriptor for the incoming client
1057 auto clientDescriptor = CameraService::CameraClientManager::makeClientDescriptor(client, desc);
1058 auto evicted = mActiveClientManager.addAndEvict(clientDescriptor);
1059
1060 logConnected(desc->getKey(), static_cast<int>(desc->getOwnerId()),
1061 String8(client->getPackageName()));
1062
1063 if (evicted.size() > 0) {
1064 // This should never happen - clients should already have been removed in disconnect
1065 for (auto& i : evicted) {
1066 ALOGE("%s: Invalid state: Client for camera %s was not removed in disconnect",
1067 __FUNCTION__, i->getKey().string());
1068 }
1069
1070 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, clients not evicted properly",
1071 __FUNCTION__);
1072 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07001073
1074 // And register a death notification for the client callback. Do
1075 // this last to avoid Binder policy where a nested Binder
1076 // transaction might be pre-empted to service the client death
1077 // notification if the client process dies before linkToDeath is
1078 // invoked.
1079 sp<IBinder> remoteCallback = client->getRemote();
1080 if (remoteCallback != nullptr) {
1081 remoteCallback->linkToDeath(this);
1082 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001083}
1084
1085status_t CameraService::handleEvictionsLocked(const String8& cameraId, int clientPid,
1086 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
1087 /*out*/
1088 sp<BasicClient>* client,
1089 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001090 ATRACE_CALL();
Ruben Brunkcc776712015-02-17 20:18:47 -08001091 status_t ret = NO_ERROR;
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001092 std::vector<DescriptorPtr> evictedClients;
Ruben Brunkcc776712015-02-17 20:18:47 -08001093 DescriptorPtr clientDescriptor;
1094 {
1095 if (effectiveApiLevel == API_1) {
1096 // If we are using API1, any existing client for this camera ID with the same remote
1097 // should be returned rather than evicted to allow MediaRecorder to work properly.
1098
1099 auto current = mActiveClientManager.get(cameraId);
1100 if (current != nullptr) {
1101 auto clientSp = current->getValue();
1102 if (clientSp.get() != nullptr) { // should never be needed
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001103 if (!clientSp->canCastToApiClient(effectiveApiLevel)) {
1104 ALOGW("CameraService connect called from same client, but with a different"
1105 " API level, evicting prior client...");
1106 } else if (clientSp->getRemote() == remoteCallback) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001107 ALOGI("CameraService::connect X (PID %d) (second call from same"
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07001108 " app binder, returning the same client)", clientPid);
Ruben Brunkcc776712015-02-17 20:18:47 -08001109 *client = clientSp;
1110 return NO_ERROR;
1111 }
1112 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001113 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001114 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +08001115
Ruben Brunkcc776712015-02-17 20:18:47 -08001116 // Get current active client PIDs
1117 std::vector<int> ownerPids(mActiveClientManager.getAllOwners());
1118 ownerPids.push_back(clientPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001119
Chih-Hung Hsieh54b42462015-03-19 12:04:54 -07001120 // Use the value +PROCESS_STATE_NONEXISTENT, to avoid taking
1121 // address of PROCESS_STATE_NONEXISTENT as a reference argument
1122 // for the vector constructor. PROCESS_STATE_NONEXISTENT does
1123 // not have an out-of-class definition.
1124 std::vector<int> priorities(ownerPids.size(), +PROCESS_STATE_NONEXISTENT);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001125
Ruben Brunkcc776712015-02-17 20:18:47 -08001126 // Get priorites of all active PIDs
1127 ProcessInfoService::getProcessStatesFromPids(ownerPids.size(), &ownerPids[0],
1128 /*out*/&priorities[0]);
Ruben Brunkb2119af2014-05-09 19:57:56 -07001129
Ruben Brunkcc776712015-02-17 20:18:47 -08001130 // Update all active clients' priorities
1131 std::map<int,int> pidToPriorityMap;
1132 for (size_t i = 0; i < ownerPids.size() - 1; i++) {
1133 pidToPriorityMap.emplace(ownerPids[i], getCameraPriorityFromProcState(priorities[i]));
1134 }
1135 mActiveClientManager.updatePriorities(pidToPriorityMap);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001136
Ruben Brunkcc776712015-02-17 20:18:47 -08001137 // Get state for the given cameraId
1138 auto state = getCameraState(cameraId);
1139 if (state == nullptr) {
1140 ALOGE("CameraService::connect X (PID %d) rejected (no camera device with ID %s)",
1141 clientPid, cameraId.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001142 // Should never get here because validateConnectLocked should have errored out
Zhijun Heb10cdad2014-06-16 16:38:35 -07001143 return BAD_VALUE;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001144 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001145
1146 // Make descriptor for incoming client
1147 clientDescriptor = CameraClientManager::makeClientDescriptor(cameraId,
1148 sp<BasicClient>{nullptr}, static_cast<int32_t>(state->getCost()),
1149 state->getConflicting(),
1150 getCameraPriorityFromProcState(priorities[priorities.size() - 1]), clientPid);
1151
1152 // Find clients that would be evicted
1153 auto evicted = mActiveClientManager.wouldEvict(clientDescriptor);
1154
1155 // If the incoming client was 'evicted,' higher priority clients have the camera in the
1156 // background, so we cannot do evictions
1157 if (std::find(evicted.begin(), evicted.end(), clientDescriptor) != evicted.end()) {
1158 ALOGE("CameraService::connect X (PID %d) rejected (existing client(s) with higher"
1159 " priority).", clientPid);
1160
1161 sp<BasicClient> clientSp = clientDescriptor->getValue();
1162 String8 curTime = getFormattedCurrentTime();
1163 auto incompatibleClients =
1164 mActiveClientManager.getIncompatibleClients(clientDescriptor);
1165
1166 String8 msg = String8::format("%s : DENIED connect device %s client for package %s "
Ruben Brunka8ca9152015-04-07 14:23:40 -07001167 "(PID %d, priority %d) due to eviction policy", curTime.string(),
Ruben Brunkcc776712015-02-17 20:18:47 -08001168 cameraId.string(), packageName.string(), clientPid,
1169 getCameraPriorityFromProcState(priorities[priorities.size() - 1]));
1170
1171 for (auto& i : incompatibleClients) {
1172 msg.appendFormat("\n - Blocked by existing device %s client for package %s"
1173 "(PID %" PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1174 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1175 i->getPriority());
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07001176 ALOGE(" Conflicts with: Device %s, client package %s (PID %"
1177 PRId32 ", priority %" PRId32 ")", i->getKey().string(),
1178 String8{i->getValue()->getPackageName()}.string(), i->getOwnerId(),
1179 i->getPriority());
Ruben Brunkcc776712015-02-17 20:18:47 -08001180 }
1181
1182 // Log the client's attempt
Ruben Brunka8ca9152015-04-07 14:23:40 -07001183 Mutex::Autolock l(mLogLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001184 mEventLog.add(msg);
1185
1186 return -EBUSY;
1187 }
1188
1189 for (auto& i : evicted) {
1190 sp<BasicClient> clientSp = i->getValue();
1191 if (clientSp.get() == nullptr) {
1192 ALOGE("%s: Invalid state: Null client in active client list.", __FUNCTION__);
1193
1194 // TODO: Remove this
1195 LOG_ALWAYS_FATAL("%s: Invalid state for CameraService, null client in active list",
1196 __FUNCTION__);
1197 mActiveClientManager.remove(i);
1198 continue;
1199 }
1200
1201 ALOGE("CameraService::connect evicting conflicting client for camera ID %s",
1202 i->getKey().string());
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001203 evictedClients.push_back(i);
Ruben Brunkcc776712015-02-17 20:18:47 -08001204
Ruben Brunkcc776712015-02-17 20:18:47 -08001205 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001206 logEvent(String8::format("EVICT device %s client held by package %s (PID"
1207 " %" PRId32 ", priority %" PRId32 ")\n - Evicted by device %s client for"
1208 " package %s (PID %d, priority %" PRId32 ")",
Ruben Brunkcc776712015-02-17 20:18:47 -08001209 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1210 i->getOwnerId(), i->getPriority(), cameraId.string(),
1211 packageName.string(), clientPid,
1212 getCameraPriorityFromProcState(priorities[priorities.size() - 1])));
1213
1214 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001215 clientSp->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001216 CaptureResultExtras());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001217 }
Ruben Brunkb2119af2014-05-09 19:57:56 -07001218 }
1219
Ruben Brunkcc776712015-02-17 20:18:47 -08001220 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1221 // other clients from connecting in mServiceLockWrapper if held
1222 mServiceLock.unlock();
1223
1224 // Clear caller identity temporarily so client disconnect PID checks work correctly
1225 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1226
1227 // Destroy evicted clients
1228 for (auto& i : evictedClients) {
1229 // Disconnect is blocking, and should only have returned when HAL has cleaned up
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001230 i->getValue()->disconnect(); // Clients will remove themselves from the active client list
Ruben Brunkcc776712015-02-17 20:18:47 -08001231 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001232
1233 IPCThreadState::self()->restoreCallingIdentity(token);
1234
Ruben Brunk4f9576b2015-04-10 17:26:56 -07001235 for (const auto& i : evictedClients) {
1236 ALOGV("%s: Waiting for disconnect to complete for client for device %s (PID %" PRId32 ")",
1237 __FUNCTION__, i->getKey().string(), i->getOwnerId());
1238 ret = mActiveClientManager.waitUntilRemoved(i, DEFAULT_DISCONNECT_TIMEOUT_NS);
1239 if (ret == TIMED_OUT) {
1240 ALOGE("%s: Timed out waiting for client for device %s to disconnect, "
1241 "current clients:\n%s", __FUNCTION__, i->getKey().string(),
1242 mActiveClientManager.toString().string());
1243 return -EBUSY;
1244 }
1245 if (ret != NO_ERROR) {
1246 ALOGE("%s: Received error waiting for client for device %s to disconnect: %s (%d), "
1247 "current clients:\n%s", __FUNCTION__, i->getKey().string(), strerror(-ret),
1248 ret, mActiveClientManager.toString().string());
1249 return ret;
1250 }
1251 }
1252
1253 evictedClients.clear();
1254
Ruben Brunkcc776712015-02-17 20:18:47 -08001255 // Once clients have been disconnected, relock
1256 mServiceLock.lock();
1257
1258 // Check again if the device was unplugged or something while we weren't holding mServiceLock
1259 if ((ret = checkIfDeviceIsUsable(cameraId)) != NO_ERROR) {
1260 return ret;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001261 }
1262
Ruben Brunkcc776712015-02-17 20:18:47 -08001263 *partial = clientDescriptor;
1264 return NO_ERROR;
Ruben Brunkb2119af2014-05-09 19:57:56 -07001265}
1266
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001267Status CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -08001268 const sp<ICameraClient>& cameraClient,
1269 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001270 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001271 int clientUid,
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001272 int clientPid,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -07001273 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001274 sp<ICamera>* device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001275
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001276 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001277 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001278 String8 id = String8::format("%d", cameraId);
1279 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001280 ret = connectHelper<ICameraClient,Client>(cameraClient, id,
1281 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, clientPid, API_1,
1282 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1283 /*out*/client);
Igor Murashkine6800ce2013-03-04 17:25:57 -08001284
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001285 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001286 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001287 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001288 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289 }
1290
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001291 *device = client;
1292 return ret;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001293}
1294
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001295Status CameraService::connectLegacy(
Zhijun Heb10cdad2014-06-16 16:38:35 -07001296 const sp<ICameraClient>& cameraClient,
1297 int cameraId, int halVersion,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001298 const String16& clientPackageName,
Zhijun Heb10cdad2014-06-16 16:38:35 -07001299 int clientUid,
1300 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001301 sp<ICamera>* device) {
Zhijun Heb10cdad2014-06-16 16:38:35 -07001302
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001303 ATRACE_CALL();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001304 String8 id = String8::format("%d", cameraId);
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08001305 int apiVersion = mModule->getModuleApiVersion();
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001306 if (halVersion != CAMERA_HAL_API_VERSION_UNSPECIFIED &&
Yin-Chia Yehe074a932015-01-30 10:29:02 -08001307 apiVersion < CAMERA_MODULE_API_VERSION_2_3) {
Igor Murashkin3d07d1a2014-06-20 11:27:03 -07001308 /*
1309 * Either the HAL version is unspecified in which case this just creates
1310 * a camera client selected by the latest device version, or
1311 * it's a particular version in which case the HAL must supported
1312 * the open_legacy call
1313 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001314 String8 msg = String8::format("Camera HAL module version %x too old for connectLegacy!",
1315 apiVersion);
1316 ALOGE("%s: %s",
1317 __FUNCTION__, msg.string());
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001318 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001319 msg);
1320 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun Heb10cdad2014-06-16 16:38:35 -07001321 }
1322
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001323 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001324 sp<Client> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001325 ret = connectHelper<ICameraClient,Client>(cameraClient, id, halVersion,
1326 clientPackageName, clientUid, USE_CALLING_PID, API_1,
1327 /*legacyMode*/ true, /*shimUpdateOnly*/ false,
1328 /*out*/client);
Zhijun Heb10cdad2014-06-16 16:38:35 -07001329
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001330 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001331 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001332 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001333 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001334 }
1335
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001336 *device = client;
1337 return ret;
Zhijun Heb10cdad2014-06-16 16:38:35 -07001338}
1339
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001340Status CameraService::connectDevice(
1341 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Ruben Brunkcc776712015-02-17 20:18:47 -08001342 int cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001343 const String16& clientPackageName,
Ruben Brunkcc776712015-02-17 20:18:47 -08001344 int clientUid,
1345 /*out*/
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001346 sp<hardware::camera2::ICameraDeviceUser>* device) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001347
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001348 ATRACE_CALL();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001349 Status ret = Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001350 String8 id = String8::format("%d", cameraId);
1351 sp<CameraDeviceClient> client = nullptr;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001352 ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
1353 CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName,
1354 clientUid, USE_CALLING_PID, API_2,
1355 /*legacyMode*/ false, /*shimUpdateOnly*/ false,
1356 /*out*/client);
Ruben Brunkcc776712015-02-17 20:18:47 -08001357
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001358 if(!ret.isOk()) {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001359 logRejected(id, getCallingPid(), String8(clientPackageName),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001360 ret.toString8());
Ruben Brunkcc776712015-02-17 20:18:47 -08001361 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001362 }
1363
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001364 *device = client;
1365 return ret;
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001366}
1367
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001368Status CameraService::setTorchMode(const String16& cameraId, bool enabled,
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001369 const sp<IBinder>& clientBinder) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001370
1371 ATRACE_CALL();
Ruben Brunk99e69712015-05-26 17:25:07 -07001372 if (enabled && clientBinder == nullptr) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001373 ALOGE("%s: torch client binder is NULL", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001374 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT,
1375 "Torch client Binder is null");
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001376 }
1377
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001378 String8 id = String8(cameraId.string());
Ruben Brunk99e69712015-05-26 17:25:07 -07001379 int uid = getCallingUid();
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001380
1381 // verify id is valid.
Ruben Brunkcc776712015-02-17 20:18:47 -08001382 auto state = getCameraState(id);
1383 if (state == nullptr) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001384 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001385 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1386 "Camera ID \"%s\" is a not valid camera ID", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08001387 }
1388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389 int32_t cameraStatus = state->getStatus();
Ruben Brunkcc776712015-02-17 20:18:47 -08001390 if (cameraStatus != ICameraServiceListener::STATUS_PRESENT &&
1391 cameraStatus != ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07001392 ALOGE("%s: camera id is invalid %s", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001393 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1394 "Camera ID \"%s\" is a not valid camera ID", id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001395 }
1396
1397 {
1398 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001399 int32_t status;
1400 status_t err = getTorchStatusLocked(id, &status);
1401 if (err != OK) {
1402 if (err == NAME_NOT_FOUND) {
1403 return STATUS_ERROR_FMT(ERROR_ILLEGAL_ARGUMENT,
1404 "Camera \"%s\" does not have a flash unit", id.string());
1405 }
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001406 ALOGE("%s: getting current torch status failed for camera %s",
1407 __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001408 return STATUS_ERROR_FMT(ERROR_INVALID_OPERATION,
1409 "Error updating torch status for camera \"%s\": %s (%d)", id.string(),
1410 strerror(-err), err);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001411 }
1412
1413 if (status == ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001414 if (cameraStatus == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001415 ALOGE("%s: torch mode of camera %s is not available because "
1416 "camera is in use", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001417 return STATUS_ERROR_FMT(ERROR_CAMERA_IN_USE,
1418 "Torch for camera \"%s\" is not available due to an existing camera user",
1419 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001420 } else {
1421 ALOGE("%s: torch mode of camera %s is not available due to "
1422 "insufficient resources", __FUNCTION__, id.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423 return STATUS_ERROR_FMT(ERROR_MAX_CAMERAS_IN_USE,
1424 "Torch for camera \"%s\" is not available due to insufficient resources",
1425 id.string());
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001426 }
1427 }
1428 }
1429
Ruben Brunk99e69712015-05-26 17:25:07 -07001430 {
1431 // Update UID map - this is used in the torch status changed callbacks, so must be done
1432 // before setTorchMode
Chien-Yu Chenfe751be2015-09-01 14:16:44 -07001433 Mutex::Autolock al(mTorchUidMapMutex);
Ruben Brunk99e69712015-05-26 17:25:07 -07001434 if (mTorchUidMap.find(id) == mTorchUidMap.end()) {
1435 mTorchUidMap[id].first = uid;
1436 mTorchUidMap[id].second = uid;
1437 } else {
1438 // Set the pending UID
1439 mTorchUidMap[id].first = uid;
1440 }
1441 }
1442
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001443 status_t err = mFlashlight->setTorchMode(id, enabled);
Ruben Brunk99e69712015-05-26 17:25:07 -07001444
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001445 if (err != OK) {
1446 int32_t errorCode;
1447 String8 msg;
1448 switch (err) {
1449 case -ENOSYS:
1450 msg = String8::format("Camera \"%s\" has no flashlight",
1451 id.string());
1452 errorCode = ERROR_ILLEGAL_ARGUMENT;
1453 break;
1454 default:
1455 msg = String8::format(
1456 "Setting torch mode of camera \"%s\" to %d failed: %s (%d)",
1457 id.string(), enabled, strerror(-err), err);
1458 errorCode = ERROR_INVALID_OPERATION;
1459 }
1460 ALOGE("%s: %s", __FUNCTION__, msg.string());
1461 return STATUS_ERROR(errorCode, msg.string());
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001462 }
1463
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001464 {
1465 // update the link to client's death
1466 Mutex::Autolock al(mTorchClientMapMutex);
1467 ssize_t index = mTorchClientMap.indexOfKey(id);
1468 if (enabled) {
1469 if (index == NAME_NOT_FOUND) {
1470 mTorchClientMap.add(id, clientBinder);
1471 } else {
Ruben Brunk99e69712015-05-26 17:25:07 -07001472 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001473 mTorchClientMap.replaceValueAt(index, clientBinder);
1474 }
1475 clientBinder->linkToDeath(this);
1476 } else if (index != NAME_NOT_FOUND) {
Ruben Brunk99e69712015-05-26 17:25:07 -07001477 mTorchClientMap.valueAt(index)->unlinkToDeath(this);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001478 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001479 }
1480
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001481 return Status::ok();
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001482}
1483
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001484Status CameraService::notifySystemEvent(int32_t eventId,
1485 const std::vector<int32_t>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001486 ATRACE_CALL();
1487
Ruben Brunk36597b22015-03-20 22:15:57 -07001488 switch(eventId) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001489 case ICameraService::EVENT_USER_SWITCHED: {
1490 doUserSwitch(/*newUserIds*/ args);
Ruben Brunk36597b22015-03-20 22:15:57 -07001491 break;
1492 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001493 case ICameraService::EVENT_NONE:
Ruben Brunk36597b22015-03-20 22:15:57 -07001494 default: {
1495 ALOGW("%s: Received invalid system event from system_server: %d", __FUNCTION__,
1496 eventId);
1497 break;
1498 }
1499 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001500 return Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07001501}
1502
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001503Status CameraService::addListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001504 ATRACE_CALL();
1505
Igor Murashkinbfc99152013-02-27 12:55:20 -08001506 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -08001507
Ruben Brunk3450ba72015-06-16 11:00:37 -07001508 if (listener == nullptr) {
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001509 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001510 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to addListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001511 }
1512
Igor Murashkinbfc99152013-02-27 12:55:20 -08001513 Mutex::Autolock lock(mServiceLock);
1514
Ruben Brunkcc776712015-02-17 20:18:47 -08001515 {
1516 Mutex::Autolock lock(mStatusListenerLock);
1517 for (auto& it : mListenerList) {
1518 if (IInterface::asBinder(it) == IInterface::asBinder(listener)) {
1519 ALOGW("%s: Tried to add listener %p which was already subscribed",
1520 __FUNCTION__, listener.get());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001521 return STATUS_ERROR(ERROR_ALREADY_EXISTS, "Listener already registered");
Ruben Brunkcc776712015-02-17 20:18:47 -08001522 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001523 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001524
1525 mListenerList.push_back(listener);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001526 }
1527
Igor Murashkinbfc99152013-02-27 12:55:20 -08001528
Igor Murashkincba2c162013-03-20 15:56:31 -07001529 /* Immediately signal current status to this listener only */
1530 {
Ruben Brunkcc776712015-02-17 20:18:47 -08001531 Mutex::Autolock lock(mCameraStatesLock);
1532 for (auto& i : mCameraStates) {
1533 // TODO: Update binder to use String16 for camera IDs and remove;
1534 int id = cameraIdToInt(i.first);
1535 if (id == -1) continue;
1536
1537 listener->onStatusChanged(i.second->getStatus(), id);
Igor Murashkincba2c162013-03-20 15:56:31 -07001538 }
1539 }
1540
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001541 /* Immediately signal current torch status to this listener only */
1542 {
1543 Mutex::Autolock al(mTorchStatusMutex);
1544 for (size_t i = 0; i < mTorchStatusMap.size(); i++ ) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08001545 String16 id = String16(mTorchStatusMap.keyAt(i).string());
1546 listener->onTorchStatusChanged(mTorchStatusMap.valueAt(i), id);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001547 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08001548 }
1549
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001550 return Status::ok();
Igor Murashkinbfc99152013-02-27 12:55:20 -08001551}
Ruben Brunkcc776712015-02-17 20:18:47 -08001552
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001553Status CameraService::removeListener(const sp<ICameraServiceListener>& listener) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001554 ATRACE_CALL();
1555
Igor Murashkinbfc99152013-02-27 12:55:20 -08001556 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
1557
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001558 if (listener == 0) {
1559 ALOGE("%s: Listener must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001560 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Null listener given to removeListener");
Igor Murashkinbd3e2e02014-03-17 13:01:41 -07001561 }
1562
Igor Murashkinbfc99152013-02-27 12:55:20 -08001563 Mutex::Autolock lock(mServiceLock);
1564
Ruben Brunkcc776712015-02-17 20:18:47 -08001565 {
1566 Mutex::Autolock lock(mStatusListenerLock);
1567 for (auto it = mListenerList.begin(); it != mListenerList.end(); it++) {
1568 if (IInterface::asBinder(*it) == IInterface::asBinder(listener)) {
1569 mListenerList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001570 return Status::ok();
Ruben Brunkcc776712015-02-17 20:18:47 -08001571 }
Igor Murashkinbfc99152013-02-27 12:55:20 -08001572 }
1573 }
1574
1575 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
1576 __FUNCTION__, listener.get());
1577
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001578 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Unregistered listener given to removeListener");
Igor Murashkin634a5152013-02-20 17:15:11 -08001579}
1580
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001581Status CameraService::getLegacyParameters(int cameraId, /*out*/String16* parameters) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001582
1583 ATRACE_CALL();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001584 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1585
1586 if (parameters == NULL) {
1587 ALOGE("%s: parameters must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001588 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Parameters must not be null");
Igor Murashkin65d14b92014-06-17 12:03:20 -07001589 }
1590
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001591 Status ret = Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001592
1593 CameraParameters shimParams;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001594 if (!(ret = getLegacyParametersLazy(cameraId, /*out*/&shimParams)).isOk()) {
Igor Murashkin65d14b92014-06-17 12:03:20 -07001595 // Error logged by caller
1596 return ret;
1597 }
1598
1599 String8 shimParamsString8 = shimParams.flatten();
1600 String16 shimParamsString16 = String16(shimParamsString8);
1601
1602 *parameters = shimParamsString16;
1603
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001604 return ret;
Igor Murashkin65d14b92014-06-17 12:03:20 -07001605}
1606
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001607Status CameraService::supportsCameraApi(int cameraId, int apiVersion, bool *isSupported) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001608 ATRACE_CALL();
1609
Igor Murashkin65d14b92014-06-17 12:03:20 -07001610 ALOGV("%s: for camera ID = %d", __FUNCTION__, cameraId);
1611
1612 switch (apiVersion) {
1613 case API_VERSION_1:
1614 case API_VERSION_2:
1615 break;
1616 default:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001617 String8 msg = String8::format("Unknown API version %d", apiVersion);
1618 ALOGE("%s: %s", __FUNCTION__, msg.string());
1619 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001620 }
1621
1622 int facing = -1;
1623 int deviceVersion = getDeviceVersion(cameraId, &facing);
1624
1625 switch(deviceVersion) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001626 case CAMERA_DEVICE_API_VERSION_1_0:
1627 case CAMERA_DEVICE_API_VERSION_3_0:
1628 case CAMERA_DEVICE_API_VERSION_3_1:
1629 if (apiVersion == API_VERSION_2) {
1630 ALOGV("%s: Camera id %d uses HAL version %d <3.2, doesn't support api2 without shim",
1631 __FUNCTION__, cameraId, deviceVersion);
1632 *isSupported = false;
1633 } else { // if (apiVersion == API_VERSION_1) {
1634 ALOGV("%s: Camera id %d uses older HAL before 3.2, but api1 is always supported",
1635 __FUNCTION__, cameraId);
1636 *isSupported = true;
1637 }
1638 break;
1639 case CAMERA_DEVICE_API_VERSION_3_2:
1640 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001641 case CAMERA_DEVICE_API_VERSION_3_4:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001642 ALOGV("%s: Camera id %d uses HAL3.2 or newer, supports api1/api2 directly",
Igor Murashkin65d14b92014-06-17 12:03:20 -07001643 __FUNCTION__, cameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001644 *isSupported = true;
1645 break;
1646 case -1: {
1647 String8 msg = String8::format("Unknown camera ID %d", cameraId);
1648 ALOGE("%s: %s", __FUNCTION__, msg.string());
1649 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkin65d14b92014-06-17 12:03:20 -07001650 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001651 default: {
1652 String8 msg = String8::format("Unknown device version %d for device %d",
1653 deviceVersion, cameraId);
1654 ALOGE("%s: %s", __FUNCTION__, msg.string());
1655 return STATUS_ERROR(ERROR_INVALID_OPERATION, msg.string());
1656 }
Igor Murashkin65d14b92014-06-17 12:03:20 -07001657 }
1658
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001659 return Status::ok();
Igor Murashkin65d14b92014-06-17 12:03:20 -07001660}
1661
Ruben Brunkcc776712015-02-17 20:18:47 -08001662void CameraService::removeByClient(const BasicClient* client) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001663 Mutex::Autolock lock(mServiceLock);
Ruben Brunkcc776712015-02-17 20:18:47 -08001664 for (auto& i : mActiveClientManager.getAll()) {
1665 auto clientSp = i->getValue();
1666 if (clientSp.get() == client) {
1667 mActiveClientManager.remove(i);
Igor Murashkin634a5152013-02-20 17:15:11 -08001668 }
Igor Murashkinecf17e82012-10-02 16:05:11 -07001669 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001670}
1671
Ruben Brunkcc776712015-02-17 20:18:47 -08001672bool CameraService::evictClientIdByRemote(const wp<IBinder>& remote) {
1673 const int callingPid = getCallingPid();
1674 const int servicePid = getpid();
1675 bool ret = false;
1676 {
1677 // Acquire mServiceLock and prevent other clients from connecting
1678 std::unique_ptr<AutoConditionLock> lock =
1679 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
Igor Murashkin634a5152013-02-20 17:15:11 -08001680
Igor Murashkin634a5152013-02-20 17:15:11 -08001681
Ruben Brunkcc776712015-02-17 20:18:47 -08001682 std::vector<sp<BasicClient>> evicted;
1683 for (auto& i : mActiveClientManager.getAll()) {
1684 auto clientSp = i->getValue();
1685 if (clientSp.get() == nullptr) {
1686 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1687 mActiveClientManager.remove(i);
1688 continue;
1689 }
1690 if (remote == clientSp->getRemote() && (callingPid == servicePid ||
1691 callingPid == clientSp->getClientPid())) {
1692 mActiveClientManager.remove(i);
1693 evicted.push_back(clientSp);
Igor Murashkin634a5152013-02-20 17:15:11 -08001694
Ruben Brunkcc776712015-02-17 20:18:47 -08001695 // Notify the client of disconnection
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001696 clientSp->notifyError(
1697 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED,
Ruben Brunkcc776712015-02-17 20:18:47 -08001698 CaptureResultExtras());
Igor Murashkin634a5152013-02-20 17:15:11 -08001699 }
1700 }
1701
Ruben Brunkcc776712015-02-17 20:18:47 -08001702 // Do not hold mServiceLock while disconnecting clients, but retain the condition blocking
1703 // other clients from connecting in mServiceLockWrapper if held
1704 mServiceLock.unlock();
1705
Ruben Brunk36597b22015-03-20 22:15:57 -07001706 // Do not clear caller identity, remote caller should be client proccess
1707
Ruben Brunkcc776712015-02-17 20:18:47 -08001708 for (auto& i : evicted) {
1709 if (i.get() != nullptr) {
1710 i->disconnect();
1711 ret = true;
1712 }
Igor Murashkin634a5152013-02-20 17:15:11 -08001713 }
1714
Ruben Brunkcc776712015-02-17 20:18:47 -08001715 // Reacquire mServiceLock
1716 mServiceLock.lock();
Igor Murashkin634a5152013-02-20 17:15:11 -08001717
Ruben Brunkcc776712015-02-17 20:18:47 -08001718 } // lock is destroyed, allow further connect calls
1719
1720 return ret;
Igor Murashkinecf17e82012-10-02 16:05:11 -07001721}
1722
Igor Murashkinecf17e82012-10-02 16:05:11 -07001723
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001724/**
1725 * Check camera capabilities, such as support for basic color operation
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001726 * Also check that the device HAL version is still in support
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001727 */
1728int CameraService::checkCameraCapabilities(int id, camera_info info, int *latestStrangeCameraId) {
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001729 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
1730 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
1731 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
1732 // Verify the device version is in the supported range
1733 switch (info.device_version) {
1734 case CAMERA_DEVICE_API_VERSION_1_0:
1735 case CAMERA_DEVICE_API_VERSION_3_0:
1736 case CAMERA_DEVICE_API_VERSION_3_1:
1737 case CAMERA_DEVICE_API_VERSION_3_2:
1738 case CAMERA_DEVICE_API_VERSION_3_3:
Zhijun He4afbdec2016-05-04 15:42:51 -07001739 case CAMERA_DEVICE_API_VERSION_3_4:
Yin-Chia Yeh654b4bf2015-12-08 12:19:31 -08001740 // in support
1741 break;
1742 case CAMERA_DEVICE_API_VERSION_2_0:
1743 case CAMERA_DEVICE_API_VERSION_2_1:
1744 // no longer supported
1745 default:
1746 ALOGE("%s: Device %d has HAL version %x, which is not supported",
1747 __FUNCTION__, id, info.device_version);
1748 String8 msg = String8::format(
1749 "Unsupported device HAL version %x for device %d",
1750 info.device_version, id);
1751 logServiceError(msg.string(), NO_INIT);
1752 return NO_INIT;
1753 }
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001754 }
1755
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001756 // Assume all devices pre-v3.3 are backward-compatible
1757 bool isBackwardCompatible = true;
1758 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0
1759 && info.device_version >= CAMERA_DEVICE_API_VERSION_3_3) {
1760 isBackwardCompatible = false;
1761 status_t res;
1762 camera_metadata_ro_entry_t caps;
1763 res = find_camera_metadata_ro_entry(
1764 info.static_camera_characteristics,
1765 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
1766 &caps);
1767 if (res != 0) {
1768 ALOGW("%s: Unable to find camera capabilities for camera device %d",
1769 __FUNCTION__, id);
1770 caps.count = 0;
1771 }
1772 for (size_t i = 0; i < caps.count; i++) {
1773 if (caps.data.u8[i] ==
1774 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) {
1775 isBackwardCompatible = true;
1776 break;
1777 }
1778 }
1779 }
1780
1781 if (!isBackwardCompatible) {
1782 mNumberOfNormalCameras--;
1783 *latestStrangeCameraId = id;
1784 } else {
1785 if (id > *latestStrangeCameraId) {
1786 ALOGE("%s: Normal camera ID %d higher than strange camera ID %d. "
1787 "This is not allowed due backward-compatibility requirements",
1788 __FUNCTION__, id, *latestStrangeCameraId);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001789 logServiceError("Invalid order of camera devices", NO_INIT);
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001790 mNumberOfCameras = 0;
1791 mNumberOfNormalCameras = 0;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001792 return NO_INIT;
Eino-Ville Talvalabad43582015-08-14 13:12:32 -07001793 }
1794 }
1795 return OK;
1796}
1797
Ruben Brunkcc776712015-02-17 20:18:47 -08001798std::shared_ptr<CameraService::CameraState> CameraService::getCameraState(
1799 const String8& cameraId) const {
1800 std::shared_ptr<CameraState> state;
1801 {
1802 Mutex::Autolock lock(mCameraStatesLock);
1803 auto iter = mCameraStates.find(cameraId);
1804 if (iter != mCameraStates.end()) {
1805 state = iter->second;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001806 }
1807 }
Ruben Brunkcc776712015-02-17 20:18:47 -08001808 return state;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001809}
1810
Ruben Brunkcc776712015-02-17 20:18:47 -08001811sp<CameraService::BasicClient> CameraService::removeClientLocked(const String8& cameraId) {
1812 // Remove from active clients list
1813 auto clientDescriptorPtr = mActiveClientManager.remove(cameraId);
1814 if (clientDescriptorPtr == nullptr) {
1815 ALOGW("%s: Could not evict client, no client for camera ID %s", __FUNCTION__,
1816 cameraId.string());
1817 return sp<BasicClient>{nullptr};
1818 }
1819
1820 return clientDescriptorPtr->getValue();
Keun young Parkd8973a72012-03-28 14:13:09 -07001821}
1822
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001823void CameraService::doUserSwitch(const std::vector<int32_t>& newUserIds) {
Ruben Brunk36597b22015-03-20 22:15:57 -07001824 // Acquire mServiceLock and prevent other clients from connecting
1825 std::unique_ptr<AutoConditionLock> lock =
1826 AutoConditionLock::waitAndAcquire(mServiceLockWrapper);
1827
Ruben Brunk6267b532015-04-30 17:44:07 -07001828 std::set<userid_t> newAllowedUsers;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001829 for (size_t i = 0; i < newUserIds.size(); i++) {
1830 if (newUserIds[i] < 0) {
Ruben Brunk6267b532015-04-30 17:44:07 -07001831 ALOGE("%s: Bad user ID %d given during user switch, ignoring.",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001832 __FUNCTION__, newUserIds[i]);
Ruben Brunk6267b532015-04-30 17:44:07 -07001833 return;
1834 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001835 newAllowedUsers.insert(static_cast<userid_t>(newUserIds[i]));
Ruben Brunk36597b22015-03-20 22:15:57 -07001836 }
1837
Ruben Brunka8ca9152015-04-07 14:23:40 -07001838
Ruben Brunk6267b532015-04-30 17:44:07 -07001839 if (newAllowedUsers == mAllowedUsers) {
1840 ALOGW("%s: Received notification of user switch with no updated user IDs.", __FUNCTION__);
1841 return;
1842 }
1843
1844 logUserSwitch(mAllowedUsers, newAllowedUsers);
1845
1846 mAllowedUsers = std::move(newAllowedUsers);
Ruben Brunk36597b22015-03-20 22:15:57 -07001847
1848 // Current user has switched, evict all current clients.
1849 std::vector<sp<BasicClient>> evicted;
1850 for (auto& i : mActiveClientManager.getAll()) {
1851 auto clientSp = i->getValue();
1852
1853 if (clientSp.get() == nullptr) {
1854 ALOGE("%s: Dead client still in mActiveClientManager.", __FUNCTION__);
1855 continue;
1856 }
1857
Ruben Brunk6267b532015-04-30 17:44:07 -07001858 // Don't evict clients that are still allowed.
1859 uid_t clientUid = clientSp->getClientUid();
1860 userid_t clientUserId = multiuser_get_user_id(clientUid);
1861 if (mAllowedUsers.find(clientUserId) != mAllowedUsers.end()) {
1862 continue;
1863 }
1864
Ruben Brunk36597b22015-03-20 22:15:57 -07001865 evicted.push_back(clientSp);
1866
1867 String8 curTime = getFormattedCurrentTime();
1868
1869 ALOGE("Evicting conflicting client for camera ID %s due to user change",
1870 i->getKey().string());
Ruben Brunka8ca9152015-04-07 14:23:40 -07001871
Ruben Brunk36597b22015-03-20 22:15:57 -07001872 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001873 logEvent(String8::format("EVICT device %s client held by package %s (PID %"
Ruben Brunk36597b22015-03-20 22:15:57 -07001874 PRId32 ", priority %" PRId32 ")\n - Evicted due to user switch.",
Ruben Brunka8ca9152015-04-07 14:23:40 -07001875 i->getKey().string(), String8{clientSp->getPackageName()}.string(),
1876 i->getOwnerId(), i->getPriority()));
Ruben Brunk36597b22015-03-20 22:15:57 -07001877
1878 }
1879
1880 // Do not hold mServiceLock while disconnecting clients, but retain the condition
1881 // blocking other clients from connecting in mServiceLockWrapper if held.
1882 mServiceLock.unlock();
1883
1884 // Clear caller identity temporarily so client disconnect PID checks work correctly
1885 int64_t token = IPCThreadState::self()->clearCallingIdentity();
1886
1887 for (auto& i : evicted) {
1888 i->disconnect();
1889 }
1890
1891 IPCThreadState::self()->restoreCallingIdentity(token);
1892
1893 // Reacquire mServiceLock
1894 mServiceLock.lock();
1895}
Ruben Brunkcc776712015-02-17 20:18:47 -08001896
Ruben Brunka8ca9152015-04-07 14:23:40 -07001897void CameraService::logEvent(const char* event) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001898 String8 curTime = getFormattedCurrentTime();
Ruben Brunka8ca9152015-04-07 14:23:40 -07001899 Mutex::Autolock l(mLogLock);
1900 mEventLog.add(String8::format("%s : %s", curTime.string(), event));
Mathias Agopian65ab4712010-07-14 17:59:35 -07001901}
1902
Ruben Brunka8ca9152015-04-07 14:23:40 -07001903void CameraService::logDisconnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001904 const char* clientPackage) {
Ruben Brunkcc776712015-02-17 20:18:47 -08001905 // Log the clients evicted
Ruben Brunka8ca9152015-04-07 14:23:40 -07001906 logEvent(String8::format("DISCONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001907 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001908}
1909
1910void CameraService::logConnected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001911 const char* clientPackage) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001912 // Log the clients evicted
1913 logEvent(String8::format("CONNECT device %s client for package %s (PID %d)", cameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001914 clientPackage, clientPid));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001915}
1916
1917void CameraService::logRejected(const char* cameraId, int clientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001918 const char* clientPackage, const char* reason) {
Ruben Brunka8ca9152015-04-07 14:23:40 -07001919 // Log the client rejected
1920 logEvent(String8::format("REJECT device %s client for package %s (PID %d), reason: (%s)",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00001921 cameraId, clientPackage, clientPid, reason));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001922}
1923
Ruben Brunk6267b532015-04-30 17:44:07 -07001924void CameraService::logUserSwitch(const std::set<userid_t>& oldUserIds,
1925 const std::set<userid_t>& newUserIds) {
1926 String8 newUsers = toString(newUserIds);
1927 String8 oldUsers = toString(oldUserIds);
Ruben Brunka8ca9152015-04-07 14:23:40 -07001928 // Log the new and old users
Ruben Brunk6267b532015-04-30 17:44:07 -07001929 logEvent(String8::format("USER_SWITCH previous allowed users: %s , current allowed users: %s",
1930 oldUsers.string(), newUsers.string()));
Ruben Brunka8ca9152015-04-07 14:23:40 -07001931}
1932
1933void CameraService::logDeviceRemoved(const char* cameraId, const char* reason) {
1934 // Log the device removal
1935 logEvent(String8::format("REMOVE device %s, reason: (%s)", cameraId, reason));
1936}
1937
1938void CameraService::logDeviceAdded(const char* cameraId, const char* reason) {
1939 // Log the device removal
1940 logEvent(String8::format("ADD device %s, reason: (%s)", cameraId, reason));
1941}
1942
1943void CameraService::logClientDied(int clientPid, const char* reason) {
1944 // Log the device removal
1945 logEvent(String8::format("DIED client(s) with PID %d, reason: (%s)", clientPid, reason));
Igor Murashkinecf17e82012-10-02 16:05:11 -07001946}
1947
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001948void CameraService::logServiceError(const char* msg, int errorCode) {
1949 String8 curTime = getFormattedCurrentTime();
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08001950 logEvent(String8::format("SERVICE ERROR: %s : %d (%s)", msg, errorCode, strerror(-errorCode)));
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07001951}
1952
Ruben Brunk36597b22015-03-20 22:15:57 -07001953status_t CameraService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
1954 uint32_t flags) {
1955
1956 const int pid = getCallingPid();
1957 const int selfPid = getpid();
1958
Mathias Agopian65ab4712010-07-14 17:59:35 -07001959 // Permission checks
1960 switch (code) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001961 case BnCameraService::NOTIFYSYSTEMEVENT: {
Ruben Brunk36597b22015-03-20 22:15:57 -07001962 if (pid != selfPid) {
1963 // Ensure we're being called by system_server, or similar process with
1964 // permissions to notify the camera service about system events
1965 if (!checkCallingPermission(
1966 String16("android.permission.CAMERA_SEND_SYSTEM_EVENTS"))) {
1967 const int uid = getCallingUid();
1968 ALOGE("Permission Denial: cannot send updates to camera service about system"
1969 " events from pid=%d, uid=%d", pid, uid);
1970 return PERMISSION_DENIED;
1971 }
1972 }
1973 break;
1974 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001975 }
1976
1977 return BnCameraService::onTransact(code, data, reply, flags);
1978}
1979
Mathias Agopian65ab4712010-07-14 17:59:35 -07001980// We share the media players for shutter and recording sound for all clients.
1981// A reference count is kept to determine when we will actually release the
1982// media players.
1983
Chih-Chung Changff4f55c2011-10-17 19:03:12 +08001984MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001985 MediaPlayer* mp = new MediaPlayer();
Andreas Huber1b86fe02014-01-29 11:13:26 -08001986 if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -08001987 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001988 mp->prepare();
1989 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001990 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001991 return NULL;
1992 }
1993 return mp;
1994}
1995
1996void CameraService::loadSound() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07001997 ATRACE_CALL();
1998
Mathias Agopian65ab4712010-07-14 17:59:35 -07001999 Mutex::Autolock lock(mSoundLock);
2000 LOG1("CameraService::loadSound ref=%d", mSoundRef);
2001 if (mSoundRef++) return;
2002
2003 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
Chien-Yu Chen82104eb2015-10-14 11:29:31 -07002004 mSoundPlayer[SOUND_RECORDING_START] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
2005 mSoundPlayer[SOUND_RECORDING_STOP] = newMediaPlayer("/system/media/audio/ui/VideoStop.ogg");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002006}
2007
2008void CameraService::releaseSound() {
2009 Mutex::Autolock lock(mSoundLock);
2010 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
2011 if (--mSoundRef) return;
2012
2013 for (int i = 0; i < NUM_SOUNDS; i++) {
2014 if (mSoundPlayer[i] != 0) {
2015 mSoundPlayer[i]->disconnect();
2016 mSoundPlayer[i].clear();
2017 }
2018 }
2019}
2020
2021void CameraService::playSound(sound_kind kind) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002022 ATRACE_CALL();
2023
Mathias Agopian65ab4712010-07-14 17:59:35 -07002024 LOG1("playSound(%d)", kind);
2025 Mutex::Autolock lock(mSoundLock);
2026 sp<MediaPlayer> player = mSoundPlayer[kind];
2027 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +08002028 player->seekTo(0);
2029 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002030 }
2031}
2032
2033// ----------------------------------------------------------------------------
2034
2035CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -07002036 const sp<ICameraClient>& cameraClient,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002037 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002038 int cameraId, int cameraFacing,
2039 int clientPid, uid_t clientUid,
2040 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002041 CameraService::BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -08002042 IInterface::asBinder(cameraClient),
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002043 clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002044 cameraId, cameraFacing,
2045 clientPid, clientUid,
2046 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08002047{
Mathias Agopian65ab4712010-07-14 17:59:35 -07002048 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002049 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002050
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002051 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002052
Mathias Agopian65ab4712010-07-14 17:59:35 -07002053 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002054
Wu-cheng Li2fd24402012-02-23 19:01:00 -08002055 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002056}
2057
Mathias Agopian65ab4712010-07-14 17:59:35 -07002058// tear down the client
2059CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002060 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -08002061 mDestructionStarted = true;
2062
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -07002063 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002064 // unconditionally disconnect. function is idempotent
2065 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -07002066}
2067
Igor Murashkin634a5152013-02-20 17:15:11 -08002068CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002069 const sp<IBinder>& remoteCallback,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002070 const String16& clientPackageName,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002071 int cameraId, int cameraFacing,
2072 int clientPid, uid_t clientUid,
2073 int servicePid):
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002074 mClientPackageName(clientPackageName), mDisconnected(false)
Igor Murashkin634a5152013-02-20 17:15:11 -08002075{
2076 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -08002077 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -08002078 mCameraId = cameraId;
2079 mCameraFacing = cameraFacing;
2080 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002081 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -08002082 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002083 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -08002084 mDestructionStarted = false;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08002085
2086 // In some cases the calling code has no access to the package it runs under.
2087 // For example, NDK camera API.
2088 // In this case we will get the packages for the calling UID and pick the first one
2089 // for attributing the app op. This will work correctly for runtime permissions
2090 // as for legacy apps we will toggle the app op for all packages in the UID.
2091 // The caveat is that the operation may be attributed to the wrong package and
2092 // stats based on app ops may be slightly off.
2093 if (mClientPackageName.size() <= 0) {
2094 sp<IServiceManager> sm = defaultServiceManager();
2095 sp<IBinder> binder = sm->getService(String16(kPermissionServiceName));
2096 if (binder == 0) {
2097 ALOGE("Cannot get permission service");
2098 // Leave mClientPackageName unchanged (empty) and the further interaction
2099 // with camera will fail in BasicClient::startCameraOps
2100 return;
2101 }
2102
2103 sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder);
2104 Vector<String16> packages;
2105
2106 permCtrl->getPackagesForUid(mClientUid, packages);
2107
2108 if (packages.isEmpty()) {
2109 ALOGE("No packages for calling UID");
2110 // Leave mClientPackageName unchanged (empty) and the further interaction
2111 // with camera will fail in BasicClient::startCameraOps
2112 return;
2113 }
2114 mClientPackageName = packages[0];
2115 }
Igor Murashkin634a5152013-02-20 17:15:11 -08002116}
2117
2118CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002119 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -08002120 mDestructionStarted = true;
2121}
2122
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002123binder::Status CameraService::BasicClient::disconnect() {
2124 binder::Status res = Status::ok();
Ruben Brunk36597b22015-03-20 22:15:57 -07002125 if (mDisconnected) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002126 return res;
Ruben Brunk36597b22015-03-20 22:15:57 -07002127 }
Eino-Ville Talvala24901c82015-09-04 14:15:58 -07002128 mDisconnected = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002129
2130 mCameraService->removeByClient(this);
2131 mCameraService->logDisconnected(String8::format("%d", mCameraId), mClientPid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002132 String8(mClientPackageName));
Ruben Brunkcc776712015-02-17 20:18:47 -08002133
2134 sp<IBinder> remote = getRemote();
2135 if (remote != nullptr) {
2136 remote->unlinkToDeath(mCameraService);
2137 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002138
2139 finishCameraOps();
Ruben Brunkcc776712015-02-17 20:18:47 -08002140 ALOGI("%s: Disconnected client for camera %d for PID %d", __FUNCTION__, mCameraId, mClientPid);
2141
Igor Murashkincba2c162013-03-20 15:56:31 -07002142 // client shouldn't be able to call into us anymore
2143 mClientPid = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002144
2145 return res;
Igor Murashkin634a5152013-02-20 17:15:11 -08002146}
2147
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002148status_t CameraService::BasicClient::dump(int, const Vector<String16>&) {
2149 // No dumping of clients directly over Binder,
2150 // must go through CameraService::dump
2151 android_errorWriteWithInfoLog(SN_EVENT_LOG_ID, "26265403",
2152 IPCThreadState::self()->getCallingUid(), NULL, 0);
2153 return OK;
2154}
2155
Ruben Brunkcc776712015-02-17 20:18:47 -08002156String16 CameraService::BasicClient::getPackageName() const {
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002157 return mClientPackageName;
Ruben Brunkcc776712015-02-17 20:18:47 -08002158}
2159
2160
2161int CameraService::BasicClient::getClientPid() const {
2162 return mClientPid;
2163}
2164
Ruben Brunk6267b532015-04-30 17:44:07 -07002165uid_t CameraService::BasicClient::getClientUid() const {
2166 return mClientUid;
2167}
2168
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002169bool CameraService::BasicClient::canCastToApiClient(apiLevel level) const {
2170 // Defaults to API2.
2171 return level == API_2;
2172}
2173
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002174status_t CameraService::BasicClient::startCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002175 ATRACE_CALL();
2176
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002177 int32_t res;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002178 // Notify app ops that the camera is not available
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002179 mOpsCallback = new OpsCallback(this);
2180
Igor Murashkine6800ce2013-03-04 17:25:57 -08002181 {
2182 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002183 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
Igor Murashkine6800ce2013-03-04 17:25:57 -08002184 }
2185
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002186 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002187 mClientPackageName, mOpsCallback);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002188 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002189 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002190
Svetoslav28e8ef72015-05-11 19:21:31 -07002191 if (res == AppOpsManager::MODE_ERRORED) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002192 ALOGI("Camera %d: Access for \"%s\" has been revoked",
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002193 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002194 return PERMISSION_DENIED;
2195 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002196
Svetoslav28e8ef72015-05-11 19:21:31 -07002197 if (res == AppOpsManager::MODE_IGNORED) {
2198 ALOGI("Camera %d: Access for \"%s\" has been restricted",
2199 mCameraId, String8(mClientPackageName).string());
Eino-Ville Talvalae3afb2c2015-06-03 16:03:30 -07002200 // Return the same error as for device policy manager rejection
2201 return -EACCES;
Svetoslav28e8ef72015-05-11 19:21:31 -07002202 }
2203
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002204 mOpsActive = true;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002205
2206 // Transition device availability listeners from PRESENT -> NOT_AVAILABLE
2207 mCameraService->updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
Ruben Brunkcc776712015-02-17 20:18:47 -08002208 String8::format("%d", mCameraId));
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002209
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002210 // Transition device state to OPEN
2211 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_OPEN,
2212 String8::format("%d", mCameraId));
2213
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002214 return OK;
2215}
2216
2217status_t CameraService::BasicClient::finishCameraOps() {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002218 ATRACE_CALL();
2219
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002220 // Check if startCameraOps succeeded, and if so, finish the camera op
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002221 if (mOpsActive) {
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002222 // Notify app ops that the camera is available again
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002223 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002224 mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002225 mOpsActive = false;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002226
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002227 std::initializer_list<int32_t> rejected = {ICameraServiceListener::STATUS_NOT_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002228 ICameraServiceListener::STATUS_ENUMERATING};
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002229
Ruben Brunkcc776712015-02-17 20:18:47 -08002230 // Transition to PRESENT if the camera is not in either of the rejected states
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002231 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Ruben Brunkcc776712015-02-17 20:18:47 -08002232 String8::format("%d", mCameraId), rejected);
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002233
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002234 // Transition device state to CLOSED
2235 mCameraService->updateProxyDeviceState(ICameraServiceProxy::CAMERA_STATE_CLOSED,
2236 String8::format("%d", mCameraId));
2237
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002238 // Notify flashlight that a camera device is closed.
2239 mCameraService->mFlashlight->deviceClosed(
2240 String8::format("%d", mCameraId));
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002241 }
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -07002242 // Always stop watching, even if no camera op is active
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08002243 if (mOpsCallback != NULL) {
2244 mAppOpsManager.stopWatchingMode(mOpsCallback);
2245 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002246 mOpsCallback.clear();
2247
2248 return OK;
2249}
2250
2251void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002252 ATRACE_CALL();
2253
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002254 String8 name(packageName);
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002255 String8 myName(mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002256
2257 if (op != AppOpsManager::OP_CAMERA) {
2258 ALOGW("Unexpected app ops notification received: %d", op);
2259 return;
2260 }
2261
2262 int32_t res;
2263 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
Svetoslav Ganov280405a2015-05-12 02:19:27 +00002264 mClientUid, mClientPackageName);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002265 ALOGV("checkOp returns: %d, %s ", res,
2266 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
2267 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
2268 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
2269 "UNKNOWN");
2270
2271 if (res != AppOpsManager::MODE_ALLOWED) {
2272 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
2273 myName.string());
2274 // Reset the client PID to allow server-initiated disconnect,
2275 // and to prevent further calls by client.
2276 mClientPid = getCallingPid();
Jianing Weicb0652e2014-03-12 18:29:36 -07002277 CaptureResultExtras resultExtras; // a dummy result (invalid)
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002278 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002279 disconnect();
2280 }
2281}
2282
Mathias Agopian65ab4712010-07-14 17:59:35 -07002283// ----------------------------------------------------------------------------
2284
Ruben Brunkcc776712015-02-17 20:18:47 -08002285// Provide client strong pointer for callbacks.
2286sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
2287 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
2288 auto clientDescriptor = gCameraService->mActiveClientManager.get(cameraId);
2289 if (clientDescriptor != nullptr) {
2290 return sp<Client>{
2291 static_cast<Client*>(clientDescriptor->getValue().get())};
2292 }
2293 return sp<Client>{nullptr};
Mathias Agopian65ab4712010-07-14 17:59:35 -07002294}
2295
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002296void CameraService::Client::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07002297 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002298 (void) errorCode;
2299 (void) resultExtras;
Ranjith Kagathi Ananda3e600892015-10-08 16:00:33 -07002300 if (mRemoteCallback != NULL) {
2301 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
2302 } else {
2303 ALOGE("mRemoteCallback is NULL!!");
2304 }
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002305}
2306
Igor Murashkin036bc3e2012-10-08 15:09:46 -07002307// NOTE: function is idempotent
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002308binder::Status CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07002309 ALOGV("Client::disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002310 return BasicClient::disconnect();
Wu-cheng Lie09591e2010-10-14 20:17:44 +08002311}
2312
Ruben Brunk0bbf8b22015-04-30 14:35:42 -07002313bool CameraService::Client::canCastToApiClient(apiLevel level) const {
2314 return level == API_1;
2315}
2316
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08002317CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
2318 mClient(client) {
2319}
2320
2321void CameraService::Client::OpsCallback::opChanged(int32_t op,
2322 const String16& packageName) {
2323 sp<BasicClient> client = mClient.promote();
2324 if (client != NULL) {
2325 client->opChanged(op, packageName);
2326 }
2327}
2328
Mathias Agopian65ab4712010-07-14 17:59:35 -07002329// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002330// CameraState
2331// ----------------------------------------------------------------------------
2332
2333CameraService::CameraState::CameraState(const String8& id, int cost,
2334 const std::set<String8>& conflicting) : mId(id),
2335 mStatus(ICameraServiceListener::STATUS_PRESENT), mCost(cost), mConflicting(conflicting) {}
2336
2337CameraService::CameraState::~CameraState() {}
2338
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002339int32_t CameraService::CameraState::getStatus() const {
Ruben Brunkcc776712015-02-17 20:18:47 -08002340 Mutex::Autolock lock(mStatusLock);
2341 return mStatus;
2342}
2343
2344CameraParameters CameraService::CameraState::getShimParams() const {
2345 return mShimParams;
2346}
2347
2348void CameraService::CameraState::setShimParams(const CameraParameters& params) {
2349 mShimParams = params;
2350}
2351
2352int CameraService::CameraState::getCost() const {
2353 return mCost;
2354}
2355
2356std::set<String8> CameraService::CameraState::getConflicting() const {
2357 return mConflicting;
2358}
2359
2360String8 CameraService::CameraState::getId() const {
2361 return mId;
2362}
2363
2364// ----------------------------------------------------------------------------
Ruben Brunk99e69712015-05-26 17:25:07 -07002365// ClientEventListener
2366// ----------------------------------------------------------------------------
2367
2368void CameraService::ClientEventListener::onClientAdded(
2369 const resource_policy::ClientDescriptor<String8,
2370 sp<CameraService::BasicClient>>& descriptor) {
2371 auto basicClient = descriptor.getValue();
2372 if (basicClient.get() != nullptr) {
2373 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2374 notifier.noteStartCamera(descriptor.getKey(),
2375 static_cast<int>(basicClient->getClientUid()));
2376 }
2377}
2378
2379void CameraService::ClientEventListener::onClientRemoved(
2380 const resource_policy::ClientDescriptor<String8,
2381 sp<CameraService::BasicClient>>& descriptor) {
2382 auto basicClient = descriptor.getValue();
2383 if (basicClient.get() != nullptr) {
2384 BatteryNotifier& notifier(BatteryNotifier::getInstance());
2385 notifier.noteStopCamera(descriptor.getKey(),
2386 static_cast<int>(basicClient->getClientUid()));
2387 }
2388}
2389
2390
2391// ----------------------------------------------------------------------------
Ruben Brunkcc776712015-02-17 20:18:47 -08002392// CameraClientManager
2393// ----------------------------------------------------------------------------
2394
Ruben Brunk99e69712015-05-26 17:25:07 -07002395CameraService::CameraClientManager::CameraClientManager() {
2396 setListener(std::make_shared<ClientEventListener>());
2397}
2398
Ruben Brunkcc776712015-02-17 20:18:47 -08002399CameraService::CameraClientManager::~CameraClientManager() {}
2400
2401sp<CameraService::BasicClient> CameraService::CameraClientManager::getCameraClient(
2402 const String8& id) const {
2403 auto descriptor = get(id);
2404 if (descriptor == nullptr) {
2405 return sp<BasicClient>{nullptr};
2406 }
2407 return descriptor->getValue();
2408}
2409
2410String8 CameraService::CameraClientManager::toString() const {
2411 auto all = getAll();
2412 String8 ret("[");
2413 bool hasAny = false;
2414 for (auto& i : all) {
2415 hasAny = true;
2416 String8 key = i->getKey();
2417 int32_t cost = i->getCost();
2418 int32_t pid = i->getOwnerId();
2419 int32_t priority = i->getPriority();
2420 auto conflicting = i->getConflicting();
2421 auto clientSp = i->getValue();
2422 String8 packageName;
Eino-Ville Talvala022f0cb2015-05-19 16:31:16 -07002423 userid_t clientUserId = 0;
Ruben Brunkcc776712015-02-17 20:18:47 -08002424 if (clientSp.get() != nullptr) {
2425 packageName = String8{clientSp->getPackageName()};
Ruben Brunk6267b532015-04-30 17:44:07 -07002426 uid_t clientUid = clientSp->getClientUid();
2427 clientUserId = multiuser_get_user_id(clientUid);
Ruben Brunkcc776712015-02-17 20:18:47 -08002428 }
2429 ret.appendFormat("\n(Camera ID: %s, Cost: %" PRId32 ", PID: %" PRId32 ", Priority: %"
2430 PRId32 ", ", key.string(), cost, pid, priority);
2431
Ruben Brunk6267b532015-04-30 17:44:07 -07002432 if (clientSp.get() != nullptr) {
2433 ret.appendFormat("User Id: %d, ", clientUserId);
2434 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002435 if (packageName.size() != 0) {
2436 ret.appendFormat("Client Package Name: %s", packageName.string());
2437 }
2438
2439 ret.append(", Conflicting Client Devices: {");
2440 for (auto& j : conflicting) {
2441 ret.appendFormat("%s, ", j.string());
2442 }
2443 ret.append("})");
2444 }
2445 if (hasAny) ret.append("\n");
2446 ret.append("]\n");
2447 return ret;
2448}
2449
2450CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2451 const String8& key, const sp<BasicClient>& value, int32_t cost,
2452 const std::set<String8>& conflictingKeys, int32_t priority, int32_t ownerId) {
2453
2454 return std::make_shared<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>(
2455 key, value, cost, conflictingKeys, priority, ownerId);
2456}
2457
2458CameraService::DescriptorPtr CameraService::CameraClientManager::makeClientDescriptor(
2459 const sp<BasicClient>& value, const CameraService::DescriptorPtr& partial) {
2460 return makeClientDescriptor(partial->getKey(), value, partial->getCost(),
2461 partial->getConflicting(), partial->getPriority(), partial->getOwnerId());
2462}
2463
2464// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07002465
2466static const int kDumpLockRetries = 50;
2467static const int kDumpLockSleep = 60000;
2468
2469static bool tryLock(Mutex& mutex)
2470{
2471 bool locked = false;
2472 for (int i = 0; i < kDumpLockRetries; ++i) {
2473 if (mutex.tryLock() == NO_ERROR) {
2474 locked = true;
2475 break;
2476 }
2477 usleep(kDumpLockSleep);
2478 }
2479 return locked;
2480}
2481
2482status_t CameraService::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalaa84bbe62015-09-08 17:59:17 -07002483 ATRACE_CALL();
2484
Ruben Brunka8ca9152015-04-07 14:23:40 -07002485 String8 result("Dump of the Camera Service:\n");
Mathias Agopian65ab4712010-07-14 17:59:35 -07002486 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
dcashmanfefa6142015-09-09 13:43:01 -07002487 result = result.format("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07002488 "can't dump CameraService from pid=%d, uid=%d\n",
2489 getCallingPid(),
2490 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002491 write(fd, result.string(), result.size());
2492 } else {
2493 bool locked = tryLock(mServiceLock);
2494 // failed to lock - CameraService is probably deadlocked
2495 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002496 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002497 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002498 }
2499
2500 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002501 if (!mModule) {
2502 result = String8::format("No camera module available!\n");
2503 write(fd, result.string(), result.size());
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002504
2505 // Dump event log for error information
2506 dumpEventLog(fd);
2507
Kalle Lampila6ec3a152013-04-30 15:27:19 +03002508 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002509 return NO_ERROR;
2510 }
2511
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002512 result = String8::format("Camera module HAL API version: 0x%x\n", mModule->getHalApiVersion());
2513 result.appendFormat("Camera module API version: 0x%x\n", mModule->getModuleApiVersion());
2514 result.appendFormat("Camera module name: %s\n", mModule->getModuleName());
2515 result.appendFormat("Camera module author: %s\n", mModule->getModuleAuthor());
Ruben Brunkcc776712015-02-17 20:18:47 -08002516 result.appendFormat("Number of camera devices: %d\n", mNumberOfCameras);
Eino-Ville Talvala49c97052016-01-12 14:29:40 -08002517 result.appendFormat("Number of normal camera devices: %d\n", mNumberOfNormalCameras);
Ruben Brunkcc776712015-02-17 20:18:47 -08002518 String8 activeClientString = mActiveClientManager.toString();
2519 result.appendFormat("Active Camera Clients:\n%s", activeClientString.string());
Ruben Brunk6267b532015-04-30 17:44:07 -07002520 result.appendFormat("Allowed users:\n%s\n", toString(mAllowedUsers).string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002521
Ruben Brunkf81648e2014-04-17 16:14:57 -07002522 sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor();
2523 if (desc == NULL) {
2524 result.appendFormat("Vendor tags left unimplemented.\n");
2525 } else {
2526 result.appendFormat("Vendor tag definitions:\n");
2527 }
2528
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002529 write(fd, result.string(), result.size());
Ruben Brunkf81648e2014-04-17 16:14:57 -07002530
2531 if (desc != NULL) {
2532 desc->dump(fd, /*verbosity*/2, /*indentation*/4);
2533 }
2534
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002535 dumpEventLog(fd);
Ruben Brunkcc776712015-02-17 20:18:47 -08002536
2537 bool stateLocked = tryLock(mCameraStatesLock);
2538 if (!stateLocked) {
2539 result = String8::format("CameraStates in use, may be deadlocked\n");
2540 write(fd, result.string(), result.size());
2541 }
2542
2543 for (auto& state : mCameraStates) {
2544 String8 cameraId = state.first;
2545 result = String8::format("Camera %s information:\n", cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002546 camera_info info;
2547
Ruben Brunkcc776712015-02-17 20:18:47 -08002548 // TODO: Change getCameraInfo + HAL to use String cameraIds
2549 status_t rc = mModule->getCameraInfo(cameraIdToInt(cameraId), &info);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002550 if (rc != OK) {
2551 result.appendFormat(" Error reading static information!\n");
2552 write(fd, result.string(), result.size());
2553 } else {
2554 result.appendFormat(" Facing: %s\n",
2555 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
2556 result.appendFormat(" Orientation: %d\n", info.orientation);
2557 int deviceVersion;
Chien-Yu Chen676b21b2015-02-24 10:28:19 -08002558 if (mModule->getModuleApiVersion() < CAMERA_MODULE_API_VERSION_2_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002559 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
2560 } else {
2561 deviceVersion = info.device_version;
2562 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002563
2564 auto conflicting = state.second->getConflicting();
2565 result.appendFormat(" Resource Cost: %d\n", state.second->getCost());
2566 result.appendFormat(" Conflicting Devices:");
2567 for (auto& id : conflicting) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002568 result.appendFormat(" %s", id.string());
Ruben Brunkcc776712015-02-17 20:18:47 -08002569 }
2570 if (conflicting.size() == 0) {
2571 result.appendFormat(" NONE");
2572 }
2573 result.appendFormat("\n");
2574
2575 result.appendFormat(" Device version: %#x\n", deviceVersion);
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08002576 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002577 result.appendFormat(" Device static metadata:\n");
2578 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07002579 dump_indented_camera_metadata(info.static_camera_characteristics,
Ruben Brunkf81648e2014-04-17 16:14:57 -07002580 fd, /*verbosity*/2, /*indentation*/4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002581 } else {
2582 write(fd, result.string(), result.size());
2583 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002584
2585 CameraParameters p = state.second->getShimParams();
2586 if (!p.isEmpty()) {
2587 result = String8::format(" Camera1 API shim is using parameters:\n ");
2588 write(fd, result.string(), result.size());
2589 p.dump(fd, args);
2590 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002591 }
2592
Ruben Brunkcc776712015-02-17 20:18:47 -08002593 auto clientDescriptor = mActiveClientManager.get(cameraId);
2594 if (clientDescriptor == nullptr) {
2595 result = String8::format(" Device %s is closed, no client instance\n",
2596 cameraId.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002597 write(fd, result.string(), result.size());
2598 continue;
2599 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002600 hasClient = true;
Ruben Brunkcc776712015-02-17 20:18:47 -08002601 result = String8::format(" Device %s is open. Client instance dump:\n\n",
2602 cameraId.string());
2603 result.appendFormat("Client priority level: %d\n", clientDescriptor->getPriority());
2604 result.appendFormat("Client PID: %d\n", clientDescriptor->getOwnerId());
2605
2606 auto client = clientDescriptor->getValue();
2607 result.appendFormat("Client package: %s\n",
2608 String8(client->getPackageName()).string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002609 write(fd, result.string(), result.size());
Ruben Brunkcc776712015-02-17 20:18:47 -08002610
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08002611 client->dumpClient(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002612 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002613
2614 if (stateLocked) mCameraStatesLock.unlock();
2615
Mathias Agopian65ab4712010-07-14 17:59:35 -07002616 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002617 result = String8::format("\nNo active camera clients yet.\n");
2618 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07002619 }
2620
2621 if (locked) mServiceLock.unlock();
2622
Igor Murashkinff3e31d2013-10-23 16:40:06 -07002623 // Dump camera traces if there were any
2624 write(fd, "\n", 1);
2625 camera3::CameraTraces::dump(fd, args);
2626
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002627 // Process dump arguments, if any
Mathias Agopian65ab4712010-07-14 17:59:35 -07002628 int n = args.size();
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002629 String16 verboseOption("-v");
2630 String16 unreachableOption("--unreachable");
2631 for (int i = 0; i < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07002632 if (args[i] == verboseOption) {
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002633 // change logging level
2634 if (i + 1 >= n) continue;
Mathias Agopian65ab4712010-07-14 17:59:35 -07002635 String8 levelStr(args[i+1]);
2636 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002637 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07002638 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07002639 write(fd, result.string(), result.size());
Eino-Ville Talvalad89821e2016-04-20 11:23:50 -07002640 } else if (args[i] == unreachableOption) {
2641 // Dump memory analysis
2642 // TODO - should limit be an argument parameter?
2643 UnreachableMemoryInfo info;
2644 bool success = GetUnreachableMemory(info, /*limit*/ 10000);
2645 if (!success) {
2646 dprintf(fd, "\nUnable to dump unreachable memory. "
2647 "Try disabling SELinux enforcement.\n");
2648 } else {
2649 dprintf(fd, "\nDumping unreachable memory:\n");
2650 std::string s = info.ToString(/*log_contents*/ true);
2651 write(fd, s.c_str(), s.size());
2652 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07002653 }
2654 }
2655 }
2656 return NO_ERROR;
2657}
2658
Eino-Ville Talvala1527f072015-04-07 15:55:31 -07002659void CameraService::dumpEventLog(int fd) {
2660 String8 result = String8("\nPrior client events (most recent at top):\n");
2661
2662 Mutex::Autolock l(mLogLock);
2663 for (const auto& msg : mEventLog) {
2664 result.appendFormat(" %s\n", msg.string());
2665 }
2666
2667 if (mEventLog.size() == DEFAULT_EVENT_LOG_LENGTH) {
2668 result.append(" ...\n");
2669 } else if (mEventLog.size() == 0) {
2670 result.append(" [no events yet]\n");
2671 }
2672 result.append("\n");
2673
2674 write(fd, result.string(), result.size());
2675}
2676
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002677void CameraService::handleTorchClientBinderDied(const wp<IBinder> &who) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002678 Mutex::Autolock al(mTorchClientMapMutex);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002679 for (size_t i = 0; i < mTorchClientMap.size(); i++) {
2680 if (mTorchClientMap[i] == who) {
2681 // turn off the torch mode that was turned on by dead client
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002682 String8 cameraId = mTorchClientMap.keyAt(i);
2683 status_t res = mFlashlight->setTorchMode(cameraId, false);
2684 if (res) {
2685 ALOGE("%s: torch client died but couldn't turn off torch: "
2686 "%s (%d)", __FUNCTION__, strerror(-res), res);
2687 return;
2688 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002689 mTorchClientMap.removeItemsAt(i);
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002690 break;
2691 }
2692 }
2693}
2694
Ruben Brunkcc776712015-02-17 20:18:47 -08002695/*virtual*/void CameraService::binderDied(const wp<IBinder> &who) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07002696
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002697 /**
Ruben Brunka8ca9152015-04-07 14:23:40 -07002698 * While tempting to promote the wp<IBinder> into a sp, it's actually not supported by the
2699 * binder driver
Igor Murashkin294d0ec2012-10-05 10:44:57 -07002700 */
2701
Ruben Brunka8ca9152015-04-07 14:23:40 -07002702 logClientDied(getCallingPid(), String8("Binder died unexpectedly"));
2703
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002704 // check torch client
2705 handleTorchClientBinderDied(who);
2706
2707 // check camera device client
Ruben Brunkcc776712015-02-17 20:18:47 -08002708 if(!evictClientIdByRemote(who)) {
2709 ALOGV("%s: Java client's binder death already cleaned up (normal case)", __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002710 return;
2711 }
2712
Ruben Brunkcc776712015-02-17 20:18:47 -08002713 ALOGE("%s: Java client's binder died, removing it from the list of active clients",
2714 __FUNCTION__);
Igor Murashkinecf17e82012-10-02 16:05:11 -07002715}
2716
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002717void CameraService::updateStatus(int32_t status, const String8& cameraId) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002718 updateStatus(status, cameraId, {});
Igor Murashkinbfc99152013-02-27 12:55:20 -08002719}
2720
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002721void CameraService::updateStatus(int32_t status, const String8& cameraId,
2722 std::initializer_list<int32_t> rejectSourceStates) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002723 // Do not lock mServiceLock here or can get into a deadlock from
2724 // connect() -> disconnect -> updateStatus
2725
2726 auto state = getCameraState(cameraId);
2727
2728 if (state == nullptr) {
2729 ALOGW("%s: Could not update the status for %s, no such device exists", __FUNCTION__,
2730 cameraId.string());
2731 return;
Igor Murashkincba2c162013-03-20 15:56:31 -07002732 }
2733
Ruben Brunkcc776712015-02-17 20:18:47 -08002734 // Update the status for this camera state, then send the onStatusChangedCallbacks to each
2735 // of the listeners with both the mStatusStatus and mStatusListenerLock held
2736 state->updateStatus(status, cameraId, rejectSourceStates, [this]
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002737 (const String8& cameraId, int32_t status) {
Ruben Brunkcc776712015-02-17 20:18:47 -08002738
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002739 if (status != ICameraServiceListener::STATUS_ENUMERATING) {
2740 // Update torch status if it has a flash unit.
2741 Mutex::Autolock al(mTorchStatusMutex);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002742 int32_t torchStatus;
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002743 if (getTorchStatusLocked(cameraId, &torchStatus) !=
2744 NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002745 int32_t newTorchStatus =
Chien-Yu Chenf6463fc2015-04-07 15:11:31 -07002746 status == ICameraServiceListener::STATUS_PRESENT ?
2747 ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF :
2748 ICameraServiceListener::TORCH_STATUS_NOT_AVAILABLE;
2749 if (torchStatus != newTorchStatus) {
2750 onTorchStatusChangedLocked(cameraId, newTorchStatus);
2751 }
2752 }
Ruben Brunkcc776712015-02-17 20:18:47 -08002753 }
2754
2755 Mutex::Autolock lock(mStatusListenerLock);
2756
2757 for (auto& listener : mListenerList) {
2758 // TODO: Refactor status listeners to use strings for Camera IDs and remove this.
2759 int id = cameraIdToInt(cameraId);
2760 if (id != -1) listener->onStatusChanged(status, id);
2761 }
2762 });
Igor Murashkincba2c162013-03-20 15:56:31 -07002763}
2764
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002765void CameraService::updateProxyDeviceState(ICameraServiceProxy::CameraState newState,
2766 const String8& cameraId) {
2767 sp<ICameraServiceProxy> proxyBinder = getCameraServiceProxy();
2768 if (proxyBinder == nullptr) return;
2769 String16 id(cameraId);
2770 proxyBinder->notifyCameraState(id, newState);
2771}
2772
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002773status_t CameraService::getTorchStatusLocked(
2774 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002775 int32_t *status) const {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002776 if (!status) {
2777 return BAD_VALUE;
2778 }
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002779 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2780 if (index == NAME_NOT_FOUND) {
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002781 // invalid camera ID or the camera doesn't have a flash unit
2782 return NAME_NOT_FOUND;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002783 }
2784
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002785 *status = mTorchStatusMap.valueAt(index);
2786 return OK;
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002787}
2788
Chien-Yu Chen88da5262015-02-17 13:56:46 -08002789status_t CameraService::setTorchStatusLocked(const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002790 int32_t status) {
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002791 ssize_t index = mTorchStatusMap.indexOfKey(cameraId);
2792 if (index == NAME_NOT_FOUND) {
2793 return BAD_VALUE;
2794 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002795 int32_t& item =
Chien-Yu Chen3068d732015-02-09 13:29:57 -08002796 mTorchStatusMap.editValueAt(index);
2797 item = status;
2798
2799 return OK;
2800}
2801
Mathias Agopian65ab4712010-07-14 17:59:35 -07002802}; // namespace android