blob: 141b1b3b3d94b3b375057c32f9cf5869f0ece153 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopian65ab4712010-07-14 17:59:35 -07004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "CameraService"
Iliyan Malchev8951a972011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
21#include <stdio.h>
22#include <sys/types.h>
23#include <pthread.h>
24
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080025#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
28#include <binder/MemoryBase.h>
29#include <binder/MemoryHeapBase.h>
30#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070031#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080032#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <hardware/hardware.h>
34#include <media/AudioSystem.h>
35#include <media/mediaplayer.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <utils/Errors.h>
37#include <utils/Log.h>
38#include <utils/String16.h>
39
40#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070041#include "api1/CameraClient.h"
42#include "api1/Camera2Client.h"
43#include "api_pro/ProCamera2Client.h"
44#include "api2/CameraDeviceClient.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070045#include "utils/CameraTraces.h"
Igor Murashkin98e24722013-06-19 19:51:04 -070046#include "CameraDeviceFactory.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070047
48namespace android {
49
50// ----------------------------------------------------------------------------
51// Logging support -- this is for debugging only
52// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070053volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
Steve Blockb8a80522011-12-20 16:23:08 +000055#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
56#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
58static void setLogLevel(int level) {
59 android_atomic_write(level, &gLogLevel);
60}
61
62// ----------------------------------------------------------------------------
63
64static int getCallingPid() {
65 return IPCThreadState::self()->getCallingPid();
66}
67
68static int getCallingUid() {
69 return IPCThreadState::self()->getCallingUid();
70}
71
Igor Murashkincba2c162013-03-20 15:56:31 -070072extern "C" {
73static void camera_device_status_change(
74 const struct camera_module_callbacks* callbacks,
75 int camera_id,
76 int new_status) {
77 sp<CameraService> cs = const_cast<CameraService*>(
78 static_cast<const CameraService*>(callbacks));
79
80 cs->onDeviceStatusChanged(
81 camera_id,
82 new_status);
83}
84} // extern "C"
85
Mathias Agopian65ab4712010-07-14 17:59:35 -070086// ----------------------------------------------------------------------------
87
88// This is ugly and only safe if we never re-create the CameraService, but
89// should be ok for now.
90static CameraService *gCameraService;
91
92CameraService::CameraService()
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080093 :mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070094{
Steve Blockdf64d152012-01-04 20:05:49 +000095 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070096 gCameraService = this;
Igor Murashkinbfc99152013-02-27 12:55:20 -080097
98 for (size_t i = 0; i < MAX_CAMERAS; ++i) {
Igor Murashkincba2c162013-03-20 15:56:31 -070099 mStatusList[i] = ICameraServiceListener::STATUS_PRESENT;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800100 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700101
102 this->camera_device_status_change = android::camera_device_status_change;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103}
104
Iliyan Malchev8951a972011-04-14 16:55:59 -0700105void CameraService::onFirstRef()
106{
Igor Murashkin634a5152013-02-20 17:15:11 -0800107 LOG1("CameraService::onFirstRef");
108
Iliyan Malchev8951a972011-04-14 16:55:59 -0700109 BnCameraService::onFirstRef();
110
111 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
112 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000113 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700114 mNumberOfCameras = 0;
115 }
116 else {
Alex Rayc0dd54f2013-02-20 13:39:37 -0800117 ALOGI("Loaded \"%s\" camera module", mModule->common.name);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700118 mNumberOfCameras = mModule->get_number_of_cameras();
119 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +0000120 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -0700121 mNumberOfCameras, MAX_CAMERAS);
122 mNumberOfCameras = MAX_CAMERAS;
123 }
124 for (int i = 0; i < mNumberOfCameras; i++) {
125 setCameraFree(i);
126 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700127
128 if (mModule->common.module_api_version >=
129 CAMERA_MODULE_API_VERSION_2_1) {
130 mModule->set_callbacks(this);
131 }
Igor Murashkin98e24722013-06-19 19:51:04 -0700132
133 CameraDeviceFactory::registerService(this);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700134 }
135}
136
Mathias Agopian65ab4712010-07-14 17:59:35 -0700137CameraService::~CameraService() {
138 for (int i = 0; i < mNumberOfCameras; i++) {
139 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000140 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141 }
142 }
143
144 gCameraService = NULL;
145}
146
Igor Murashkincba2c162013-03-20 15:56:31 -0700147void CameraService::onDeviceStatusChanged(int cameraId,
148 int newStatus)
149{
150 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
151 cameraId, newStatus);
152
153 if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
154 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
155 return;
156 }
157
158 if ((int)getStatus(cameraId) == newStatus) {
159 ALOGE("%s: State transition to the same status 0x%x not allowed",
160 __FUNCTION__, (uint32_t)newStatus);
161 return;
162 }
163
164 /* don't do this in updateStatus
165 since it is also called from connect and we could get into a deadlock */
166 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
167 Vector<sp<BasicClient> > clientsToDisconnect;
168 {
169 Mutex::Autolock al(mServiceLock);
170
171 /* Find all clients that we need to disconnect */
Igor Murashkine7ee7632013-06-11 18:10:18 -0700172 sp<BasicClient> client = mClient[cameraId].promote();
Igor Murashkincba2c162013-03-20 15:56:31 -0700173 if (client.get() != NULL) {
174 clientsToDisconnect.push_back(client);
175 }
176
177 int i = cameraId;
178 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
179 sp<ProClient> cl = mProClientList[i][j].promote();
180 if (cl != NULL) {
181 clientsToDisconnect.push_back(cl);
182 }
183 }
184 }
185
186 /* now disconnect them. don't hold the lock
187 or we can get into a deadlock */
188
189 for (size_t i = 0; i < clientsToDisconnect.size(); ++i) {
190 sp<BasicClient> client = clientsToDisconnect[i];
191
192 client->disconnect();
193 /**
194 * The remote app will no longer be able to call methods on the
195 * client since the client PID will be reset to 0
196 */
197 }
198
Colin Crosse5729fa2014-03-21 15:04:25 -0700199 ALOGV("%s: After unplug, disconnected %zu clients",
Igor Murashkincba2c162013-03-20 15:56:31 -0700200 __FUNCTION__, clientsToDisconnect.size());
201 }
202
203 updateStatus(
204 static_cast<ICameraServiceListener::Status>(newStatus), cameraId);
205
206}
207
Mathias Agopian65ab4712010-07-14 17:59:35 -0700208int32_t CameraService::getNumberOfCameras() {
209 return mNumberOfCameras;
210}
211
212status_t CameraService::getCameraInfo(int cameraId,
213 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700214 if (!mModule) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700215 return -ENODEV;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700216 }
217
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
219 return BAD_VALUE;
220 }
221
Iliyan Malchev8951a972011-04-14 16:55:59 -0700222 struct camera_info info;
223 status_t rc = mModule->get_camera_info(cameraId, &info);
224 cameraInfo->facing = info.facing;
225 cameraInfo->orientation = info.orientation;
226 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700227}
228
Zhijun He2b59be82013-09-25 10:14:30 -0700229status_t CameraService::getCameraCharacteristics(int cameraId,
230 CameraMetadata* cameraInfo) {
231 if (!cameraInfo) {
232 ALOGE("%s: cameraInfo is NULL", __FUNCTION__);
233 return BAD_VALUE;
234 }
235
236 if (!mModule) {
237 ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__);
238 return -ENODEV;
239 }
240
241 if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_0) {
242 // TODO: Remove this check once HAL1 shim is in place.
243 ALOGE("%s: Only HAL module version V2 or higher supports static metadata", __FUNCTION__);
244 return BAD_VALUE;
245 }
246
247 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
248 ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId);
249 return BAD_VALUE;
250 }
251
252 int facing;
253 if (getDeviceVersion(cameraId, &facing) == CAMERA_DEVICE_API_VERSION_1_0) {
254 // TODO: Remove this check once HAL1 shim is in place.
255 ALOGE("%s: HAL1 doesn't support static metadata yet", __FUNCTION__);
256 return BAD_VALUE;
257 }
258
Zhijun Hef05e50e2013-10-01 11:05:33 -0700259 if (getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1) {
260 // Disable HAL2.x support for camera2 API for now.
261 ALOGW("%s: HAL2.x doesn't support getCameraCharacteristics for now", __FUNCTION__);
262 return BAD_VALUE;
263 }
264
Zhijun He2b59be82013-09-25 10:14:30 -0700265 struct camera_info info;
266 status_t ret = mModule->get_camera_info(cameraId, &info);
267 *cameraInfo = info.static_camera_characteristics;
268
269 return ret;
270}
271
Igor Murashkin634a5152013-02-20 17:15:11 -0800272int CameraService::getDeviceVersion(int cameraId, int* facing) {
273 struct camera_info info;
274 if (mModule->get_camera_info(cameraId, &info) != OK) {
275 return -1;
276 }
277
278 int deviceVersion;
279 if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
280 deviceVersion = info.device_version;
281 } else {
282 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
283 }
284
285 if (facing) {
286 *facing = info.facing;
287 }
288
289 return deviceVersion;
290}
291
Igor Murashkinbfc99152013-02-27 12:55:20 -0800292bool CameraService::isValidCameraId(int cameraId) {
293 int facing;
294 int deviceVersion = getDeviceVersion(cameraId, &facing);
295
296 switch(deviceVersion) {
297 case CAMERA_DEVICE_API_VERSION_1_0:
298 case CAMERA_DEVICE_API_VERSION_2_0:
299 case CAMERA_DEVICE_API_VERSION_2_1:
300 case CAMERA_DEVICE_API_VERSION_3_0:
301 return true;
302 default:
303 return false;
304 }
305
306 return false;
307}
308
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700309status_t CameraService::validateConnect(int cameraId,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800310 /*inout*/
311 int& clientUid) const {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800312
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500314
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800315 if (clientUid == USE_CALLING_UID) {
316 clientUid = getCallingUid();
317 } else {
318 // We only trust our own process to forward client UIDs
319 if (callingPid != getpid()) {
320 ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
321 callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700322 return PERMISSION_DENIED;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800323 }
324 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700325
Iliyan Malchev8951a972011-04-14 16:55:59 -0700326 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000327 ALOGE("Camera HAL module not loaded");
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700328 return -ENODEV;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700329 }
330
Mathias Agopian65ab4712010-07-14 17:59:35 -0700331 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000332 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700333 callingPid, cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700334 return -ENODEV;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 }
336
Wu-cheng Lia3355432011-05-20 14:54:25 +0800337 char value[PROPERTY_VALUE_MAX];
338 property_get("sys.secpolicy.camera.disabled", value, "0");
339 if (strcmp(value, "1") == 0) {
340 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000341 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700342 return -EACCES;
Wu-cheng Lia3355432011-05-20 14:54:25 +0800343 }
344
Igor Murashkincba2c162013-03-20 15:56:31 -0700345 ICameraServiceListener::Status currentStatus = getStatus(cameraId);
346 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
347 ALOGI("Camera is not plugged in,"
348 " connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700349 return -ENODEV;
Igor Murashkincba2c162013-03-20 15:56:31 -0700350 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
351 ALOGI("Camera is enumerating,"
352 " connect X (pid %d) rejected", callingPid);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700353 return -EBUSY;
Igor Murashkincba2c162013-03-20 15:56:31 -0700354 }
355 // Else don't check for STATUS_NOT_AVAILABLE.
356 // -- It's done implicitly in canConnectUnsafe /w the mBusy array
357
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700358 return OK;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800359}
360
361bool CameraService::canConnectUnsafe(int cameraId,
362 const String16& clientPackageName,
363 const sp<IBinder>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700364 sp<BasicClient> &client) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800365 String8 clientName8(clientPackageName);
366 int callingPid = getCallingPid();
367
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800368 if (mClient[cameraId] != 0) {
369 client = mClient[cameraId].promote();
370 if (client != 0) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700371 if (remoteCallback == client->getRemote()) {
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800372 LOG1("CameraService::connect X (pid %d) (the same client)",
373 callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800374 return true;
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800375 } else {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800376 // TODOSC: need to support 1 regular client,
377 // multiple shared clients here
378 ALOGW("CameraService::connect X (pid %d) rejected"
379 " (existing client).", callingPid);
380 return false;
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800381 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800382 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800383 mClient[cameraId].clear();
384 }
385
Igor Murashkin634a5152013-02-20 17:15:11 -0800386 /*
387 mBusy is set to false as the last step of the Client destructor,
388 after which it is guaranteed that the Client destructor has finished (
389 including any inherited destructors)
390
391 We only need this for a Client subclasses since we don't allow
392 multiple Clents to be opened concurrently, but multiple BasicClient
393 would be fine
394 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800395 if (mBusy[cameraId]) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800396 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
397 " (camera %d is still busy).", callingPid,
398 clientName8.string(), cameraId);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800399 return false;
400 }
401
402 return true;
403}
404
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700405status_t CameraService::connect(
Igor Murashkine6800ce2013-03-04 17:25:57 -0800406 const sp<ICameraClient>& cameraClient,
407 int cameraId,
408 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700409 int clientUid,
410 /*out*/
411 sp<ICamera>& device) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800412
413 String8 clientName8(clientPackageName);
414 int callingPid = getCallingPid();
415
416 LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
417 clientName8.string(), cameraId);
418
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700419 status_t status = validateConnect(cameraId, /*inout*/clientUid);
420 if (status != OK) {
421 return status;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422 }
423
Igor Murashkine6800ce2013-03-04 17:25:57 -0800424
Igor Murashkine7ee7632013-06-11 18:10:18 -0700425 sp<Client> client;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700426 {
427 Mutex::Autolock lock(mServiceLock);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700428 sp<BasicClient> clientTmp;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700429 if (!canConnectUnsafe(cameraId, clientPackageName,
430 cameraClient->asBinder(),
Igor Murashkine7ee7632013-06-11 18:10:18 -0700431 /*out*/clientTmp)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700432 return -EBUSY;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700433 } else if (client.get() != NULL) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700434 device = static_cast<Client*>(clientTmp.get());
435 return OK;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700436 }
437
438 int facing = -1;
439 int deviceVersion = getDeviceVersion(cameraId, &facing);
440
441 // If there are other non-exclusive users of the camera,
442 // this will tear them down before we can reuse the camera
443 if (isValidCameraId(cameraId)) {
Igor Murashkincba2c162013-03-20 15:56:31 -0700444 // transition from PRESENT -> NOT_AVAILABLE
Igor Murashkinacd695c2013-03-13 17:23:00 -0700445 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
446 cameraId);
447 }
448
449 switch(deviceVersion) {
450 case CAMERA_DEVICE_API_VERSION_1_0:
451 client = new CameraClient(this, cameraClient,
452 clientPackageName, cameraId,
453 facing, callingPid, clientUid, getpid());
454 break;
455 case CAMERA_DEVICE_API_VERSION_2_0:
456 case CAMERA_DEVICE_API_VERSION_2_1:
457 case CAMERA_DEVICE_API_VERSION_3_0:
458 client = new Camera2Client(this, cameraClient,
459 clientPackageName, cameraId,
460 facing, callingPid, clientUid, getpid(),
461 deviceVersion);
462 break;
463 case -1:
464 ALOGE("Invalid camera id %d", cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700465 return BAD_VALUE;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700466 default:
467 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700468 return INVALID_OPERATION;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700469 }
470
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700471 status_t status = connectFinishUnsafe(client, client->getRemote());
472 if (status != OK) {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700473 // this is probably not recoverable.. maybe the client can try again
Igor Murashkincba2c162013-03-20 15:56:31 -0700474 // OK: we can only get here if we were originally in PRESENT state
475 updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700476 return status;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700477 }
478
479 mClient[cameraId] = client;
480 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId,
481 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800482 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700483 // important: release the mutex here so the client can call back
484 // into the service from its destructor (can be at the end of the call)
Igor Murashkinbfc99152013-02-27 12:55:20 -0800485
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700486 device = client;
487 return OK;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700488}
489
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700490status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
491 const sp<IBinder>& remoteCallback) {
492 status_t status = client->initialize(mModule);
493 if (status != OK) {
494 return status;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800495 }
496
Igor Murashkine7ee7632013-06-11 18:10:18 -0700497 remoteCallback->linkToDeath(this);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800498
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700499 return OK;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800500}
501
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700502status_t CameraService::connectPro(
Igor Murashkin634a5152013-02-20 17:15:11 -0800503 const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -0800504 int cameraId,
505 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700506 int clientUid,
507 /*out*/
508 sp<IProCameraUser>& device)
Igor Murashkin634a5152013-02-20 17:15:11 -0800509{
Igor Murashkinbfc99152013-02-27 12:55:20 -0800510 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 int callingPid = getCallingPid();
Igor Murashkin634a5152013-02-20 17:15:11 -0800512
Igor Murashkine6800ce2013-03-04 17:25:57 -0800513 LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid,
514 clientName8.string(), cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700515 status_t status = validateConnect(cameraId, /*inout*/clientUid);
516 if (status != OK) {
517 return status;
Igor Murashkin634a5152013-02-20 17:15:11 -0800518 }
519
Igor Murashkinacd695c2013-03-13 17:23:00 -0700520 sp<ProClient> client;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800521 {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700522 Mutex::Autolock lock(mServiceLock);
523 {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700524 sp<BasicClient> client;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700525 if (!canConnectUnsafe(cameraId, clientPackageName,
526 cameraCb->asBinder(),
527 /*out*/client)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700528 return -EBUSY;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700529 }
530 }
531
532 int facing = -1;
533 int deviceVersion = getDeviceVersion(cameraId, &facing);
534
535 switch(deviceVersion) {
536 case CAMERA_DEVICE_API_VERSION_1_0:
537 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera",
538 cameraId);
Ruben Brunk17963d12013-08-19 15:21:19 -0700539 return -EOPNOTSUPP;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700540 break;
541 case CAMERA_DEVICE_API_VERSION_2_0:
542 case CAMERA_DEVICE_API_VERSION_2_1:
Zhijun He47110052013-07-22 17:34:34 -0700543 case CAMERA_DEVICE_API_VERSION_3_0:
Igor Murashkinacd695c2013-03-13 17:23:00 -0700544 client = new ProCamera2Client(this, cameraCb, String16(),
545 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
546 break;
547 case -1:
548 ALOGE("Invalid camera id %d", cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700549 return BAD_VALUE;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700550 default:
551 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700552 return INVALID_OPERATION;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800553 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700554
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700555 status_t status = connectFinishUnsafe(client, client->getRemote());
556 if (status != OK) {
557 return status;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700558 }
559
560 mProClientList[cameraId].push(client);
561
562 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
563 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800564 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700565 // important: release the mutex here so the client can call back
566 // into the service from its destructor (can be at the end of the call)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700567 device = client;
568 return OK;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800569}
Igor Murashkin634a5152013-02-20 17:15:11 -0800570
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700571status_t CameraService::connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700572 const sp<ICameraDeviceCallbacks>& cameraCb,
573 int cameraId,
574 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700575 int clientUid,
576 /*out*/
577 sp<ICameraDeviceUser>& device)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700578{
Igor Murashkine7ee7632013-06-11 18:10:18 -0700579
580 String8 clientName8(clientPackageName);
581 int callingPid = getCallingPid();
582
583 LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid,
584 clientName8.string(), cameraId);
585
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700586 status_t status = validateConnect(cameraId, /*inout*/clientUid);
587 if (status != OK) {
588 return status;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700589 }
590
591 sp<CameraDeviceClient> client;
592 {
593 Mutex::Autolock lock(mServiceLock);
594 {
595 sp<BasicClient> client;
596 if (!canConnectUnsafe(cameraId, clientPackageName,
597 cameraCb->asBinder(),
598 /*out*/client)) {
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700599 return -EBUSY;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700600 }
601 }
602
603 int facing = -1;
604 int deviceVersion = getDeviceVersion(cameraId, &facing);
605
606 // If there are other non-exclusive users of the camera,
607 // this will tear them down before we can reuse the camera
608 if (isValidCameraId(cameraId)) {
609 // transition from PRESENT -> NOT_AVAILABLE
610 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
611 cameraId);
612 }
613
614 switch(deviceVersion) {
615 case CAMERA_DEVICE_API_VERSION_1_0:
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700616 ALOGW("Camera using old HAL version: %d", deviceVersion);
Ruben Brunk17963d12013-08-19 15:21:19 -0700617 return -EOPNOTSUPP;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700618 // TODO: don't allow 2.0 Only allow 2.1 and higher
619 case CAMERA_DEVICE_API_VERSION_2_0:
620 case CAMERA_DEVICE_API_VERSION_2_1:
621 case CAMERA_DEVICE_API_VERSION_3_0:
622 client = new CameraDeviceClient(this, cameraCb, String16(),
623 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
624 break;
625 case -1:
626 ALOGE("Invalid camera id %d", cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700627 return BAD_VALUE;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700628 default:
629 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700630 return INVALID_OPERATION;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700631 }
632
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700633 status_t status = connectFinishUnsafe(client, client->getRemote());
634 if (status != OK) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700635 // this is probably not recoverable.. maybe the client can try again
636 // OK: we can only get here if we were originally in PRESENT state
637 updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId);
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700638 return status;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700639 }
640
641 LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId,
642 getpid());
643
644 mClient[cameraId] = client;
645 }
646 // important: release the mutex here so the client can call back
647 // into the service from its destructor (can be at the end of the call)
648
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700649 device = client;
650 return OK;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700651}
652
653
Igor Murashkinbfc99152013-02-27 12:55:20 -0800654status_t CameraService::addListener(
655 const sp<ICameraServiceListener>& listener) {
656 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -0800657
Igor Murashkinbfc99152013-02-27 12:55:20 -0800658 Mutex::Autolock lock(mServiceLock);
659
660 Vector<sp<ICameraServiceListener> >::iterator it, end;
661 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
662 if ((*it)->asBinder() == listener->asBinder()) {
663 ALOGW("%s: Tried to add listener %p which was already subscribed",
664 __FUNCTION__, listener.get());
665 return ALREADY_EXISTS;
666 }
667 }
668
669 mListenerList.push_back(listener);
670
Igor Murashkincba2c162013-03-20 15:56:31 -0700671 /* Immediately signal current status to this listener only */
672 {
673 Mutex::Autolock m(mStatusMutex) ;
674 int numCams = getNumberOfCameras();
675 for (int i = 0; i < numCams; ++i) {
676 listener->onStatusChanged(mStatusList[i], i);
677 }
678 }
679
Igor Murashkinbfc99152013-02-27 12:55:20 -0800680 return OK;
681}
682status_t CameraService::removeListener(
683 const sp<ICameraServiceListener>& listener) {
684 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
685
686 Mutex::Autolock lock(mServiceLock);
687
688 Vector<sp<ICameraServiceListener> >::iterator it;
689 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
690 if ((*it)->asBinder() == listener->asBinder()) {
691 mListenerList.erase(it);
692 return OK;
693 }
694 }
695
696 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
697 __FUNCTION__, listener.get());
698
699 return BAD_VALUE;
Igor Murashkin634a5152013-02-20 17:15:11 -0800700}
701
702void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
703 int callingPid = getCallingPid();
704 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700705
Igor Murashkinecf17e82012-10-02 16:05:11 -0700706 // Declare this before the lock to make absolutely sure the
707 // destructor won't be called with the lock held.
708 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709
Igor Murashkinecf17e82012-10-02 16:05:11 -0700710 int outIndex;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700711 sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700712
713 if (client != 0) {
714 // Found our camera, clear and leave.
715 LOG1("removeClient: clear camera %d", outIndex);
716 mClient[outIndex].clear();
717
Igor Murashkine7ee7632013-06-11 18:10:18 -0700718 client->getRemote()->unlinkToDeath(this);
Igor Murashkin634a5152013-02-20 17:15:11 -0800719 } else {
720
721 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
722
723 if (clientPro != NULL) {
724 // Found our camera, clear and leave.
725 LOG1("removeClient: clear pro %p", clientPro.get());
726
727 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
728 }
Igor Murashkinecf17e82012-10-02 16:05:11 -0700729 }
730
Igor Murashkin634a5152013-02-20 17:15:11 -0800731 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
732}
733
734sp<CameraService::ProClient> CameraService::findProClientUnsafe(
735 const wp<IBinder>& cameraCallbacksRemote)
736{
737 sp<ProClient> clientPro;
738
739 for (int i = 0; i < mNumberOfCameras; ++i) {
740 Vector<size_t> removeIdx;
741
742 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
743 wp<ProClient> cl = mProClientList[i][j];
744
745 sp<ProClient> clStrong = cl.promote();
746 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
747 clientPro = clStrong;
748 break;
749 } else if (clStrong == NULL) {
750 // mark to clean up dead ptr
751 removeIdx.push(j);
752 }
753 }
754
755 // remove stale ptrs (in reverse so the indices dont change)
756 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
757 mProClientList[i].removeAt(removeIdx[j]);
758 }
759
760 }
761
762 return clientPro;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700763}
764
Igor Murashkine7ee7632013-06-11 18:10:18 -0700765sp<CameraService::BasicClient> CameraService::findClientUnsafe(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700766 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700767 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700768
769 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700770
771 // This happens when we have already disconnected (or this is
772 // just another unused camera).
773 if (mClient[i] == 0) continue;
774
775 // Promote mClient. It can fail if we are called from this path:
Igor Murashkin634a5152013-02-20 17:15:11 -0800776 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -0700777 client = mClient[i].promote();
778
Igor Murashkinecf17e82012-10-02 16:05:11 -0700779 // Clean up stale client entry
780 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 mClient[i].clear();
782 continue;
783 }
784
Igor Murashkine7ee7632013-06-11 18:10:18 -0700785 if (cameraClient == client->getRemote()) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700786 // Found our camera
787 outIndex = i;
788 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789 }
790 }
791
Igor Murashkinecf17e82012-10-02 16:05:11 -0700792 outIndex = -1;
793 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700794}
795
Igor Murashkine7ee7632013-06-11 18:10:18 -0700796CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700798 return mClient[cameraId].unsafe_get();
799}
800
801Mutex* CameraService::getClientLockById(int cameraId) {
802 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
803 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804}
805
Igor Murashkin634a5152013-02-20 17:15:11 -0800806sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700807 const wp<IBinder>& cameraClient) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700808
809 // Declare this before the lock to make absolutely sure the
810 // destructor won't be called with the lock held.
Igor Murashkin634a5152013-02-20 17:15:11 -0800811 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700812
813 Mutex::Autolock lock(mServiceLock);
814
815 int outIndex;
816 client = findClientUnsafe(cameraClient, outIndex);
817
818 return client;
819}
820
Mathias Agopian65ab4712010-07-14 17:59:35 -0700821status_t CameraService::onTransact(
822 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
823 // Permission checks
824 switch (code) {
825 case BnCameraService::CONNECT:
Igor Murashkin634a5152013-02-20 17:15:11 -0800826 case BnCameraService::CONNECT_PRO:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700827 const int pid = getCallingPid();
828 const int self_pid = getpid();
829 if (pid != self_pid) {
830 // we're called from a different process, do the real check
831 if (!checkCallingPermission(
832 String16("android.permission.CAMERA"))) {
833 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000834 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835 "can't use the camera pid=%d, uid=%d", pid, uid);
836 return PERMISSION_DENIED;
837 }
838 }
839 break;
840 }
841
842 return BnCameraService::onTransact(code, data, reply, flags);
843}
844
845// The reason we need this busy bit is a new CameraService::connect() request
846// may come in while the previous Client's destructor has not been run or is
847// still running. If the last strong reference of the previous Client is gone
848// but the destructor has not been finished, we should not allow the new Client
849// to be created because we need to wait for the previous Client to tear down
850// the hardware first.
851void CameraService::setCameraBusy(int cameraId) {
852 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700853
854 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700855}
856
857void CameraService::setCameraFree(int cameraId) {
858 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700859
860 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700861}
862
863// We share the media players for shutter and recording sound for all clients.
864// A reference count is kept to determine when we will actually release the
865// media players.
866
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800867MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700868 MediaPlayer* mp = new MediaPlayer();
869 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800870 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 mp->prepare();
872 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000873 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700874 return NULL;
875 }
876 return mp;
877}
878
879void CameraService::loadSound() {
880 Mutex::Autolock lock(mSoundLock);
881 LOG1("CameraService::loadSound ref=%d", mSoundRef);
882 if (mSoundRef++) return;
883
884 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
885 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
886}
887
888void CameraService::releaseSound() {
889 Mutex::Autolock lock(mSoundLock);
890 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
891 if (--mSoundRef) return;
892
893 for (int i = 0; i < NUM_SOUNDS; i++) {
894 if (mSoundPlayer[i] != 0) {
895 mSoundPlayer[i]->disconnect();
896 mSoundPlayer[i].clear();
897 }
898 }
899}
900
901void CameraService::playSound(sound_kind kind) {
902 LOG1("playSound(%d)", kind);
903 Mutex::Autolock lock(mSoundLock);
904 sp<MediaPlayer> player = mSoundPlayer[kind];
905 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800906 player->seekTo(0);
907 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 }
909}
910
911// ----------------------------------------------------------------------------
912
913CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700914 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800915 const String16& clientPackageName,
916 int cameraId, int cameraFacing,
917 int clientPid, uid_t clientUid,
918 int servicePid) :
Igor Murashkin634a5152013-02-20 17:15:11 -0800919 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800920 clientPackageName,
921 cameraId, cameraFacing,
922 clientPid, clientUid,
923 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800924{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700925 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800926 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700927
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800928 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700929
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 cameraService->setCameraBusy(cameraId);
931 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800932
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800933 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700934}
935
Mathias Agopian65ab4712010-07-14 17:59:35 -0700936// tear down the client
937CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -0700938 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -0800939 mDestructionStarted = true;
940
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700941 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700942 // unconditionally disconnect. function is idempotent
943 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700944}
945
Igor Murashkin634a5152013-02-20 17:15:11 -0800946CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800947 const sp<IBinder>& remoteCallback,
948 const String16& clientPackageName,
949 int cameraId, int cameraFacing,
950 int clientPid, uid_t clientUid,
951 int servicePid):
952 mClientPackageName(clientPackageName)
Igor Murashkin634a5152013-02-20 17:15:11 -0800953{
954 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800955 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -0800956 mCameraId = cameraId;
957 mCameraFacing = cameraFacing;
958 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800959 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -0800960 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800961 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -0800962 mDestructionStarted = false;
963}
964
965CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -0700966 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -0800967 mDestructionStarted = true;
968}
969
970void CameraService::BasicClient::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -0700971 ALOGV("BasicClient::disconnect");
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800972 mCameraService->removeClientByRemote(mRemoteBinder);
Igor Murashkincba2c162013-03-20 15:56:31 -0700973 // client shouldn't be able to call into us anymore
974 mClientPid = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800975}
976
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800977status_t CameraService::BasicClient::startCameraOps() {
978 int32_t res;
979
980 mOpsCallback = new OpsCallback(this);
981
Igor Murashkine6800ce2013-03-04 17:25:57 -0800982 {
983 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
984 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
985 }
986
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800987 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
988 mClientPackageName, mOpsCallback);
989 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
990 mClientUid, mClientPackageName);
991
992 if (res != AppOpsManager::MODE_ALLOWED) {
993 ALOGI("Camera %d: Access for \"%s\" has been revoked",
994 mCameraId, String8(mClientPackageName).string());
995 return PERMISSION_DENIED;
996 }
997 mOpsActive = true;
998 return OK;
999}
1000
1001status_t CameraService::BasicClient::finishCameraOps() {
1002 if (mOpsActive) {
1003 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
1004 mClientPackageName);
1005 mOpsActive = false;
1006 }
1007 mAppOpsManager.stopWatchingMode(mOpsCallback);
1008 mOpsCallback.clear();
1009
1010 return OK;
1011}
1012
1013void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
1014 String8 name(packageName);
1015 String8 myName(mClientPackageName);
1016
1017 if (op != AppOpsManager::OP_CAMERA) {
1018 ALOGW("Unexpected app ops notification received: %d", op);
1019 return;
1020 }
1021
1022 int32_t res;
1023 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
1024 mClientUid, mClientPackageName);
1025 ALOGV("checkOp returns: %d, %s ", res,
1026 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
1027 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
1028 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
1029 "UNKNOWN");
1030
1031 if (res != AppOpsManager::MODE_ALLOWED) {
1032 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
1033 myName.string());
1034 // Reset the client PID to allow server-initiated disconnect,
1035 // and to prevent further calls by client.
1036 mClientPid = getCallingPid();
1037 notifyError();
1038 disconnect();
1039 }
1040}
1041
Mathias Agopian65ab4712010-07-14 17:59:35 -07001042// ----------------------------------------------------------------------------
1043
Keun young Parkd8973a72012-03-28 14:13:09 -07001044Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001045 return gCameraService->getClientLockById((int)(intptr_t) user);
Keun young Parkd8973a72012-03-28 14:13:09 -07001046}
1047
1048// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
1049// be acquired for this to be safe
1050CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +00001051 BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001052 // OK: only CameraClient calls this, and they already cast anyway.
1053 Client* client = static_cast<Client*>(basicClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054
1055 // This could happen if the Client is in the process of shutting down (the
1056 // last strong reference is gone, but the destructor hasn't finished
1057 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -07001058 if (client == NULL) return NULL;
1059
1060 // destruction already started, so should not be accessed
1061 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001062
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 return client;
1064}
1065
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001066void CameraService::Client::notifyError() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001067 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001068}
1069
Igor Murashkin036bc3e2012-10-08 15:09:46 -07001070// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001071void CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001072 ALOGV("Client::disconnect");
Igor Murashkin634a5152013-02-20 17:15:11 -08001073 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001074 mCameraService->setCameraFree(mCameraId);
Igor Murashkin93747b92013-05-01 15:42:20 -07001075
1076 StatusVector rejectSourceStates;
1077 rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT);
1078 rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING);
1079
1080 // Transition to PRESENT if the camera is not in either of above 2 states
Igor Murashkincba2c162013-03-20 15:56:31 -07001081 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Igor Murashkin93747b92013-05-01 15:42:20 -07001082 mCameraId,
1083 &rejectSourceStates);
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001084}
1085
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001086CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
1087 mClient(client) {
1088}
1089
1090void CameraService::Client::OpsCallback::opChanged(int32_t op,
1091 const String16& packageName) {
1092 sp<BasicClient> client = mClient.promote();
1093 if (client != NULL) {
1094 client->opChanged(op, packageName);
1095 }
1096}
1097
Mathias Agopian65ab4712010-07-14 17:59:35 -07001098// ----------------------------------------------------------------------------
Igor Murashkin634a5152013-02-20 17:15:11 -08001099// IProCamera
1100// ----------------------------------------------------------------------------
1101
1102CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001103 const sp<IProCameraCallbacks>& remoteCallback,
1104 const String16& clientPackageName,
1105 int cameraId,
1106 int cameraFacing,
1107 int clientPid,
1108 uid_t clientUid,
1109 int servicePid)
1110 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
1111 clientPackageName, cameraId, cameraFacing,
1112 clientPid, clientUid, servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08001113{
1114 mRemoteCallback = remoteCallback;
1115}
1116
1117CameraService::ProClient::~ProClient() {
Igor Murashkin634a5152013-02-20 17:15:11 -08001118}
1119
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001120void CameraService::ProClient::notifyError() {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001121 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001122}
1123
Igor Murashkin634a5152013-02-20 17:15:11 -08001124// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125
1126static const int kDumpLockRetries = 50;
1127static const int kDumpLockSleep = 60000;
1128
1129static bool tryLock(Mutex& mutex)
1130{
1131 bool locked = false;
1132 for (int i = 0; i < kDumpLockRetries; ++i) {
1133 if (mutex.tryLock() == NO_ERROR) {
1134 locked = true;
1135 break;
1136 }
1137 usleep(kDumpLockSleep);
1138 }
1139 return locked;
1140}
1141
1142status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001143 String8 result;
1144 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001145 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 "can't dump CameraService from pid=%d, uid=%d\n",
1147 getCallingPid(),
1148 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 write(fd, result.string(), result.size());
1150 } else {
1151 bool locked = tryLock(mServiceLock);
1152 // failed to lock - CameraService is probably deadlocked
1153 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001154 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001155 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001156 }
1157
1158 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001159 if (!mModule) {
1160 result = String8::format("No camera module available!\n");
1161 write(fd, result.string(), result.size());
Kalle Lampila6ec3a152013-04-30 15:27:19 +03001162 if (locked) mServiceLock.unlock();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001163 return NO_ERROR;
1164 }
1165
1166 result = String8::format("Camera module HAL API version: 0x%x\n",
1167 mModule->common.hal_api_version);
1168 result.appendFormat("Camera module API version: 0x%x\n",
1169 mModule->common.module_api_version);
1170 result.appendFormat("Camera module name: %s\n",
1171 mModule->common.name);
1172 result.appendFormat("Camera module author: %s\n",
1173 mModule->common.author);
1174 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
1175 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001176 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001177 result = String8::format("Camera %d static information:\n", i);
1178 camera_info info;
1179
1180 status_t rc = mModule->get_camera_info(i, &info);
1181 if (rc != OK) {
1182 result.appendFormat(" Error reading static information!\n");
1183 write(fd, result.string(), result.size());
1184 } else {
1185 result.appendFormat(" Facing: %s\n",
1186 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
1187 result.appendFormat(" Orientation: %d\n", info.orientation);
1188 int deviceVersion;
1189 if (mModule->common.module_api_version <
1190 CAMERA_MODULE_API_VERSION_2_0) {
1191 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
1192 } else {
1193 deviceVersion = info.device_version;
1194 }
1195 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
1196 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
1197 result.appendFormat(" Device static metadata:\n");
1198 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07001199 dump_indented_camera_metadata(info.static_camera_characteristics,
1200 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001201 } else {
1202 write(fd, result.string(), result.size());
1203 }
1204 }
1205
Igor Murashkine7ee7632013-06-11 18:10:18 -07001206 sp<BasicClient> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001207 if (client == 0) {
1208 result = String8::format(" Device is closed, no client instance\n");
1209 write(fd, result.string(), result.size());
1210 continue;
1211 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001212 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001213 result = String8::format(" Device is open. Client instance dump:\n");
1214 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001215 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001216 }
1217 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001218 result = String8::format("\nNo active camera clients yet.\n");
1219 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001220 }
1221
1222 if (locked) mServiceLock.unlock();
1223
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001224 // Dump camera traces if there were any
1225 write(fd, "\n", 1);
1226 camera3::CameraTraces::dump(fd, args);
1227
Mathias Agopian65ab4712010-07-14 17:59:35 -07001228 // change logging level
1229 int n = args.size();
1230 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001231 String16 verboseOption("-v");
1232 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001233 String8 levelStr(args[i+1]);
1234 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001235 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001236 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001237 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001238 }
1239 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001240
Mathias Agopian65ab4712010-07-14 17:59:35 -07001241 }
1242 return NO_ERROR;
1243}
1244
Igor Murashkinecf17e82012-10-02 16:05:11 -07001245/*virtual*/void CameraService::binderDied(
1246 const wp<IBinder> &who) {
1247
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001248 /**
1249 * While tempting to promote the wp<IBinder> into a sp,
1250 * it's actually not supported by the binder driver
1251 */
1252
Igor Murashkinecf17e82012-10-02 16:05:11 -07001253 ALOGV("java clients' binder died");
1254
Igor Murashkin634a5152013-02-20 17:15:11 -08001255 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001256
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001257 if (cameraClient == 0) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001258 ALOGV("java clients' binder death already cleaned up (normal case)");
1259 return;
1260 }
1261
Igor Murashkinecf17e82012-10-02 16:05:11 -07001262 ALOGW("Disconnecting camera client %p since the binder for it "
1263 "died (this pid %d)", cameraClient.get(), getCallingPid());
1264
1265 cameraClient->disconnect();
1266
1267}
1268
Igor Murashkinbfc99152013-02-27 12:55:20 -08001269void CameraService::updateStatus(ICameraServiceListener::Status status,
Igor Murashkin93747b92013-05-01 15:42:20 -07001270 int32_t cameraId,
1271 const StatusVector *rejectSourceStates) {
Igor Murashkinbfc99152013-02-27 12:55:20 -08001272 // do not lock mServiceLock here or can get into a deadlock from
1273 // connect() -> ProClient::disconnect -> updateStatus
1274 Mutex::Autolock lock(mStatusMutex);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001275
1276 ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1277
1278 mStatusList[cameraId] = status;
1279
1280 if (oldStatus != status) {
1281 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1282 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1283
Igor Murashkincba2c162013-03-20 15:56:31 -07001284 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
1285 (status != ICameraServiceListener::STATUS_PRESENT &&
1286 status != ICameraServiceListener::STATUS_ENUMERATING)) {
1287
1288 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT"
1289 " or ENUMERATING", __FUNCTION__);
1290 mStatusList[cameraId] = oldStatus;
1291 return;
1292 }
1293
Igor Murashkin93747b92013-05-01 15:42:20 -07001294 if (rejectSourceStates != NULL) {
1295 const StatusVector &rejectList = *rejectSourceStates;
1296 StatusVector::const_iterator it = rejectList.begin();
1297
1298 /**
1299 * Sometimes we want to conditionally do a transition.
1300 * For example if a client disconnects, we want to go to PRESENT
1301 * only if we weren't already in NOT_PRESENT or ENUMERATING.
1302 */
1303 for (; it != rejectList.end(); ++it) {
1304 if (oldStatus == *it) {
1305 ALOGV("%s: Rejecting status transition for Camera ID %d, "
1306 " since the source state was was in one of the bad "
1307 " states.", __FUNCTION__, cameraId);
1308 mStatusList[cameraId] = oldStatus;
1309 return;
1310 }
1311 }
1312 }
1313
Igor Murashkinbfc99152013-02-27 12:55:20 -08001314 /**
1315 * ProClients lose their exclusive lock.
1316 * - Done before the CameraClient can initialize the HAL device,
1317 * since we want to be able to close it before they get to initialize
1318 */
1319 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1320 Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1321 Vector<wp<ProClient> >::const_iterator it;
1322
1323 for (it = proClients.begin(); it != proClients.end(); ++it) {
1324 sp<ProClient> proCl = it->promote();
1325 if (proCl.get() != NULL) {
1326 proCl->onExclusiveLockStolen();
1327 }
1328 }
1329 }
1330
1331 Vector<sp<ICameraServiceListener> >::const_iterator it;
1332 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1333 (*it)->onStatusChanged(status, cameraId);
1334 }
1335 }
1336}
1337
Igor Murashkincba2c162013-03-20 15:56:31 -07001338ICameraServiceListener::Status CameraService::getStatus(int cameraId) const {
1339 if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
1340 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
1341 return ICameraServiceListener::STATUS_UNKNOWN;
1342 }
1343
1344 Mutex::Autolock al(mStatusMutex);
1345 return mStatusList[cameraId];
1346}
1347
Mathias Agopian65ab4712010-07-14 17:59:35 -07001348}; // namespace android