blob: 5a6a3c87146a90ada922a2058881c601e9ffe202 [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 Talvala5e08d602012-05-16 14:59:25 -070041#include "CameraClient.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070042#include "Camera2Client.h"
Igor Murashkin985fd302013-02-20 18:24:43 -080043#include "ProCamera2Client.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45namespace android {
46
47// ----------------------------------------------------------------------------
48// Logging support -- this is for debugging only
49// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070050volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Steve Blockb8a80522011-12-20 16:23:08 +000052#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
53#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
55static void setLogLevel(int level) {
56 android_atomic_write(level, &gLogLevel);
57}
58
59// ----------------------------------------------------------------------------
60
61static int getCallingPid() {
62 return IPCThreadState::self()->getCallingPid();
63}
64
65static int getCallingUid() {
66 return IPCThreadState::self()->getCallingUid();
67}
68
69// ----------------------------------------------------------------------------
70
71// This is ugly and only safe if we never re-create the CameraService, but
72// should be ok for now.
73static CameraService *gCameraService;
74
75CameraService::CameraService()
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080076 :mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070077{
Steve Blockdf64d152012-01-04 20:05:49 +000078 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070079 gCameraService = this;
Igor Murashkinbfc99152013-02-27 12:55:20 -080080
81 for (size_t i = 0; i < MAX_CAMERAS; ++i) {
82 mStatusList[i] = ICameraServiceListener::STATUS_AVAILABLE;
83 }
Mathias Agopian65ab4712010-07-14 17:59:35 -070084}
85
Iliyan Malchev8951a972011-04-14 16:55:59 -070086void CameraService::onFirstRef()
87{
Igor Murashkin634a5152013-02-20 17:15:11 -080088 LOG1("CameraService::onFirstRef");
89
Iliyan Malchev8951a972011-04-14 16:55:59 -070090 BnCameraService::onFirstRef();
91
92 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
93 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +000094 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -070095 mNumberOfCameras = 0;
96 }
97 else {
Alex Rayc0dd54f2013-02-20 13:39:37 -080098 ALOGI("Loaded \"%s\" camera module", mModule->common.name);
Iliyan Malchev8951a972011-04-14 16:55:59 -070099 mNumberOfCameras = mModule->get_number_of_cameras();
100 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +0000101 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -0700102 mNumberOfCameras, MAX_CAMERAS);
103 mNumberOfCameras = MAX_CAMERAS;
104 }
105 for (int i = 0; i < mNumberOfCameras; i++) {
106 setCameraFree(i);
107 }
108 }
109}
110
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111CameraService::~CameraService() {
112 for (int i = 0; i < mNumberOfCameras; i++) {
113 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000114 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700115 }
116 }
117
118 gCameraService = NULL;
119}
120
121int32_t CameraService::getNumberOfCameras() {
122 return mNumberOfCameras;
123}
124
125status_t CameraService::getCameraInfo(int cameraId,
126 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700127 if (!mModule) {
128 return NO_INIT;
129 }
130
Mathias Agopian65ab4712010-07-14 17:59:35 -0700131 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
132 return BAD_VALUE;
133 }
134
Iliyan Malchev8951a972011-04-14 16:55:59 -0700135 struct camera_info info;
136 status_t rc = mModule->get_camera_info(cameraId, &info);
137 cameraInfo->facing = info.facing;
138 cameraInfo->orientation = info.orientation;
139 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140}
141
Igor Murashkin634a5152013-02-20 17:15:11 -0800142int CameraService::getDeviceVersion(int cameraId, int* facing) {
143 struct camera_info info;
144 if (mModule->get_camera_info(cameraId, &info) != OK) {
145 return -1;
146 }
147
148 int deviceVersion;
149 if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
150 deviceVersion = info.device_version;
151 } else {
152 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
153 }
154
155 if (facing) {
156 *facing = info.facing;
157 }
158
159 return deviceVersion;
160}
161
Igor Murashkinbfc99152013-02-27 12:55:20 -0800162bool CameraService::isValidCameraId(int cameraId) {
163 int facing;
164 int deviceVersion = getDeviceVersion(cameraId, &facing);
165
166 switch(deviceVersion) {
167 case CAMERA_DEVICE_API_VERSION_1_0:
168 case CAMERA_DEVICE_API_VERSION_2_0:
169 case CAMERA_DEVICE_API_VERSION_2_1:
170 case CAMERA_DEVICE_API_VERSION_3_0:
171 return true;
172 default:
173 return false;
174 }
175
176 return false;
177}
178
Igor Murashkine6800ce2013-03-04 17:25:57 -0800179bool CameraService::validateConnect(int cameraId,
180 /*inout*/
181 int& clientUid) const {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800182
Mathias Agopian65ab4712010-07-14 17:59:35 -0700183 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500184
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800185 if (clientUid == USE_CALLING_UID) {
186 clientUid = getCallingUid();
187 } else {
188 // We only trust our own process to forward client UIDs
189 if (callingPid != getpid()) {
190 ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
191 callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800192 return false;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800193 }
194 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700195
Iliyan Malchev8951a972011-04-14 16:55:59 -0700196 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000197 ALOGE("Camera HAL module not loaded");
Igor Murashkine6800ce2013-03-04 17:25:57 -0800198 return false;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700199 }
200
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000202 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700203 callingPid, cameraId);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800204 return false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700205 }
206
Wu-cheng Lia3355432011-05-20 14:54:25 +0800207 char value[PROPERTY_VALUE_MAX];
208 property_get("sys.secpolicy.camera.disabled", value, "0");
209 if (strcmp(value, "1") == 0) {
210 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000211 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800212 return false;
Wu-cheng Lia3355432011-05-20 14:54:25 +0800213 }
214
Igor Murashkine6800ce2013-03-04 17:25:57 -0800215 return true;
216}
217
218bool CameraService::canConnectUnsafe(int cameraId,
219 const String16& clientPackageName,
220 const sp<IBinder>& remoteCallback,
221 sp<Client> &client) {
222 String8 clientName8(clientPackageName);
223 int callingPid = getCallingPid();
224
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800225 if (mClient[cameraId] != 0) {
226 client = mClient[cameraId].promote();
227 if (client != 0) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800228 if (remoteCallback == client->getRemoteCallback()->asBinder()) {
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800229 LOG1("CameraService::connect X (pid %d) (the same client)",
230 callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800231 return true;
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800232 } else {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800233 // TODOSC: need to support 1 regular client,
234 // multiple shared clients here
235 ALOGW("CameraService::connect X (pid %d) rejected"
236 " (existing client).", callingPid);
237 return false;
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800238 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800239 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800240 mClient[cameraId].clear();
241 }
242
Igor Murashkin634a5152013-02-20 17:15:11 -0800243 /*
244 mBusy is set to false as the last step of the Client destructor,
245 after which it is guaranteed that the Client destructor has finished (
246 including any inherited destructors)
247
248 We only need this for a Client subclasses since we don't allow
249 multiple Clents to be opened concurrently, but multiple BasicClient
250 would be fine
251 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800252 if (mBusy[cameraId]) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800253 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
254 " (camera %d is still busy).", callingPid,
255 clientName8.string(), cameraId);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800256 return false;
257 }
258
259 return true;
260}
261
262sp<ICamera> CameraService::connect(
263 const sp<ICameraClient>& cameraClient,
264 int cameraId,
265 const String16& clientPackageName,
266 int clientUid) {
267
268 String8 clientName8(clientPackageName);
269 int callingPid = getCallingPid();
270
271 LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
272 clientName8.string(), cameraId);
273
274 if (!validateConnect(cameraId, /*inout*/clientUid)) {
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800275 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276 }
277
Igor Murashkine6800ce2013-03-04 17:25:57 -0800278 sp<Client> client;
279
Igor Murashkinacd695c2013-03-13 17:23:00 -0700280 {
281 Mutex::Autolock lock(mServiceLock);
282 if (!canConnectUnsafe(cameraId, clientPackageName,
283 cameraClient->asBinder(),
284 /*out*/client)) {
285 return NULL;
286 } else if (client.get() != NULL) {
287 return client;
288 }
289
290 int facing = -1;
291 int deviceVersion = getDeviceVersion(cameraId, &facing);
292
293 // If there are other non-exclusive users of the camera,
294 // this will tear them down before we can reuse the camera
295 if (isValidCameraId(cameraId)) {
296 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
297 cameraId);
298 }
299
300 switch(deviceVersion) {
301 case CAMERA_DEVICE_API_VERSION_1_0:
302 client = new CameraClient(this, cameraClient,
303 clientPackageName, cameraId,
304 facing, callingPid, clientUid, getpid());
305 break;
306 case CAMERA_DEVICE_API_VERSION_2_0:
307 case CAMERA_DEVICE_API_VERSION_2_1:
308 case CAMERA_DEVICE_API_VERSION_3_0:
309 client = new Camera2Client(this, cameraClient,
310 clientPackageName, cameraId,
311 facing, callingPid, clientUid, getpid(),
312 deviceVersion);
313 break;
314 case -1:
315 ALOGE("Invalid camera id %d", cameraId);
316 return NULL;
317 default:
318 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
319 return NULL;
320 }
321
322 if (!connectFinishUnsafe(client, client->asBinder())) {
323 // this is probably not recoverable.. maybe the client can try again
324 updateStatus(ICameraServiceListener::STATUS_AVAILABLE, cameraId);
325
326 return NULL;
327 }
328
329 mClient[cameraId] = client;
330 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId,
331 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800332 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700333 // important: release the mutex here so the client can call back
334 // into the service from its destructor (can be at the end of the call)
Igor Murashkinbfc99152013-02-27 12:55:20 -0800335
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 return client;
337}
338
Igor Murashkine6800ce2013-03-04 17:25:57 -0800339bool CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
340 const sp<IBinder>& clientBinder) {
341 if (client->initialize(mModule) != OK) {
342 return false;
343 }
344
345 clientBinder->linkToDeath(this);
346
347 return true;
348}
349
Igor Murashkin634a5152013-02-20 17:15:11 -0800350sp<IProCameraUser> CameraService::connect(
351 const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -0800352 int cameraId,
353 const String16& clientPackageName,
354 int clientUid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800355{
Igor Murashkinbfc99152013-02-27 12:55:20 -0800356 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 int callingPid = getCallingPid();
Igor Murashkin634a5152013-02-20 17:15:11 -0800358
Igor Murashkine6800ce2013-03-04 17:25:57 -0800359 LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid,
360 clientName8.string(), cameraId);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800361
Igor Murashkine6800ce2013-03-04 17:25:57 -0800362 if (!validateConnect(cameraId, /*inout*/clientUid)) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800363 return NULL;
364 }
365
Igor Murashkinacd695c2013-03-13 17:23:00 -0700366 sp<ProClient> client;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800367 {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700368 Mutex::Autolock lock(mServiceLock);
369 {
370 sp<Client> client;
371 if (!canConnectUnsafe(cameraId, clientPackageName,
372 cameraCb->asBinder(),
373 /*out*/client)) {
374 return NULL;
375 }
376 }
377
378 int facing = -1;
379 int deviceVersion = getDeviceVersion(cameraId, &facing);
380
381 switch(deviceVersion) {
382 case CAMERA_DEVICE_API_VERSION_1_0:
383 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera",
384 cameraId);
385 return NULL;
386 break;
387 case CAMERA_DEVICE_API_VERSION_2_0:
388 case CAMERA_DEVICE_API_VERSION_2_1:
389 client = new ProCamera2Client(this, cameraCb, String16(),
390 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
391 break;
392 case -1:
393 ALOGE("Invalid camera id %d", cameraId);
394 return NULL;
395 default:
396 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800397 return NULL;
398 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700399
400 if (!connectFinishUnsafe(client, client->asBinder())) {
401 return NULL;
402 }
403
404 mProClientList[cameraId].push(client);
405
406 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
407 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800408 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700409 // important: release the mutex here so the client can call back
410 // into the service from its destructor (can be at the end of the call)
Igor Murashkine6800ce2013-03-04 17:25:57 -0800411
Igor Murashkin634a5152013-02-20 17:15:11 -0800412 return client;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800413}
Igor Murashkin634a5152013-02-20 17:15:11 -0800414
Igor Murashkinbfc99152013-02-27 12:55:20 -0800415status_t CameraService::addListener(
416 const sp<ICameraServiceListener>& listener) {
417 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -0800418
Igor Murashkinbfc99152013-02-27 12:55:20 -0800419 Mutex::Autolock lock(mServiceLock);
420
421 Vector<sp<ICameraServiceListener> >::iterator it, end;
422 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
423 if ((*it)->asBinder() == listener->asBinder()) {
424 ALOGW("%s: Tried to add listener %p which was already subscribed",
425 __FUNCTION__, listener.get());
426 return ALREADY_EXISTS;
427 }
428 }
429
430 mListenerList.push_back(listener);
431
432 return OK;
433}
434status_t CameraService::removeListener(
435 const sp<ICameraServiceListener>& listener) {
436 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
437
438 Mutex::Autolock lock(mServiceLock);
439
440 Vector<sp<ICameraServiceListener> >::iterator it;
441 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
442 if ((*it)->asBinder() == listener->asBinder()) {
443 mListenerList.erase(it);
444 return OK;
445 }
446 }
447
448 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
449 __FUNCTION__, listener.get());
450
451 return BAD_VALUE;
Igor Murashkin634a5152013-02-20 17:15:11 -0800452}
453
454void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
455 int callingPid = getCallingPid();
456 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457
Igor Murashkinecf17e82012-10-02 16:05:11 -0700458 // Declare this before the lock to make absolutely sure the
459 // destructor won't be called with the lock held.
460 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700461
Igor Murashkinecf17e82012-10-02 16:05:11 -0700462 int outIndex;
Igor Murashkin634a5152013-02-20 17:15:11 -0800463 sp<Client> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700464
465 if (client != 0) {
466 // Found our camera, clear and leave.
467 LOG1("removeClient: clear camera %d", outIndex);
468 mClient[outIndex].clear();
469
470 client->unlinkToDeath(this);
Igor Murashkin634a5152013-02-20 17:15:11 -0800471 } else {
472
473 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
474
475 if (clientPro != NULL) {
476 // Found our camera, clear and leave.
477 LOG1("removeClient: clear pro %p", clientPro.get());
478
479 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
480 }
Igor Murashkinecf17e82012-10-02 16:05:11 -0700481 }
482
Igor Murashkin634a5152013-02-20 17:15:11 -0800483 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
484}
485
486sp<CameraService::ProClient> CameraService::findProClientUnsafe(
487 const wp<IBinder>& cameraCallbacksRemote)
488{
489 sp<ProClient> clientPro;
490
491 for (int i = 0; i < mNumberOfCameras; ++i) {
492 Vector<size_t> removeIdx;
493
494 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
495 wp<ProClient> cl = mProClientList[i][j];
496
497 sp<ProClient> clStrong = cl.promote();
498 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
499 clientPro = clStrong;
500 break;
501 } else if (clStrong == NULL) {
502 // mark to clean up dead ptr
503 removeIdx.push(j);
504 }
505 }
506
507 // remove stale ptrs (in reverse so the indices dont change)
508 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
509 mProClientList[i].removeAt(removeIdx[j]);
510 }
511
512 }
513
514 return clientPro;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700515}
516
517sp<CameraService::Client> CameraService::findClientUnsafe(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700518 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700519 sp<Client> client;
520
521 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522
523 // This happens when we have already disconnected (or this is
524 // just another unused camera).
525 if (mClient[i] == 0) continue;
526
527 // Promote mClient. It can fail if we are called from this path:
Igor Murashkin634a5152013-02-20 17:15:11 -0800528 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -0700529 client = mClient[i].promote();
530
Igor Murashkinecf17e82012-10-02 16:05:11 -0700531 // Clean up stale client entry
532 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533 mClient[i].clear();
534 continue;
535 }
536
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800537 if (cameraClient == client->getRemoteCallback()->asBinder()) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700538 // Found our camera
539 outIndex = i;
540 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 }
542 }
543
Igor Murashkinecf17e82012-10-02 16:05:11 -0700544 outIndex = -1;
545 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546}
547
Keun young Parkd8973a72012-03-28 14:13:09 -0700548CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700550 return mClient[cameraId].unsafe_get();
551}
552
553Mutex* CameraService::getClientLockById(int cameraId) {
554 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
555 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700556}
557
Igor Murashkin634a5152013-02-20 17:15:11 -0800558sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700559 const wp<IBinder>& cameraClient) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700560
561 // Declare this before the lock to make absolutely sure the
562 // destructor won't be called with the lock held.
Igor Murashkin634a5152013-02-20 17:15:11 -0800563 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700564
565 Mutex::Autolock lock(mServiceLock);
566
567 int outIndex;
568 client = findClientUnsafe(cameraClient, outIndex);
569
570 return client;
571}
572
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573status_t CameraService::onTransact(
574 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
575 // Permission checks
576 switch (code) {
577 case BnCameraService::CONNECT:
Igor Murashkin634a5152013-02-20 17:15:11 -0800578 case BnCameraService::CONNECT_PRO:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700579 const int pid = getCallingPid();
580 const int self_pid = getpid();
581 if (pid != self_pid) {
582 // we're called from a different process, do the real check
583 if (!checkCallingPermission(
584 String16("android.permission.CAMERA"))) {
585 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000586 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 "can't use the camera pid=%d, uid=%d", pid, uid);
588 return PERMISSION_DENIED;
589 }
590 }
591 break;
592 }
593
594 return BnCameraService::onTransact(code, data, reply, flags);
595}
596
597// The reason we need this busy bit is a new CameraService::connect() request
598// may come in while the previous Client's destructor has not been run or is
599// still running. If the last strong reference of the previous Client is gone
600// but the destructor has not been finished, we should not allow the new Client
601// to be created because we need to wait for the previous Client to tear down
602// the hardware first.
603void CameraService::setCameraBusy(int cameraId) {
604 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700605
606 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700607}
608
609void CameraService::setCameraFree(int cameraId) {
610 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700611
612 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613}
614
615// We share the media players for shutter and recording sound for all clients.
616// A reference count is kept to determine when we will actually release the
617// media players.
618
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800619MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700620 MediaPlayer* mp = new MediaPlayer();
621 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800622 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700623 mp->prepare();
624 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000625 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700626 return NULL;
627 }
628 return mp;
629}
630
631void CameraService::loadSound() {
632 Mutex::Autolock lock(mSoundLock);
633 LOG1("CameraService::loadSound ref=%d", mSoundRef);
634 if (mSoundRef++) return;
635
636 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
637 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
638}
639
640void CameraService::releaseSound() {
641 Mutex::Autolock lock(mSoundLock);
642 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
643 if (--mSoundRef) return;
644
645 for (int i = 0; i < NUM_SOUNDS; i++) {
646 if (mSoundPlayer[i] != 0) {
647 mSoundPlayer[i]->disconnect();
648 mSoundPlayer[i].clear();
649 }
650 }
651}
652
653void CameraService::playSound(sound_kind kind) {
654 LOG1("playSound(%d)", kind);
655 Mutex::Autolock lock(mSoundLock);
656 sp<MediaPlayer> player = mSoundPlayer[kind];
657 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800658 player->seekTo(0);
659 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 }
661}
662
663// ----------------------------------------------------------------------------
664
665CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700666 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800667 const String16& clientPackageName,
668 int cameraId, int cameraFacing,
669 int clientPid, uid_t clientUid,
670 int servicePid) :
Igor Murashkin634a5152013-02-20 17:15:11 -0800671 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800672 clientPackageName,
673 cameraId, cameraFacing,
674 clientPid, clientUid,
675 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800676{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700677 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800678 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700679
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800680 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700681
Mathias Agopian65ab4712010-07-14 17:59:35 -0700682 cameraService->setCameraBusy(cameraId);
683 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800684
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800685 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700686}
687
Mathias Agopian65ab4712010-07-14 17:59:35 -0700688// tear down the client
689CameraService::Client::~Client() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800690 mDestructionStarted = true;
691
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700692 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700693 // unconditionally disconnect. function is idempotent
694 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700695}
696
Igor Murashkin634a5152013-02-20 17:15:11 -0800697CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800698 const sp<IBinder>& remoteCallback,
699 const String16& clientPackageName,
700 int cameraId, int cameraFacing,
701 int clientPid, uid_t clientUid,
702 int servicePid):
703 mClientPackageName(clientPackageName)
Igor Murashkin634a5152013-02-20 17:15:11 -0800704{
705 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800706 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -0800707 mCameraId = cameraId;
708 mCameraFacing = cameraFacing;
709 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800710 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -0800711 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800712 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -0800713 mDestructionStarted = false;
714}
715
716CameraService::BasicClient::~BasicClient() {
717 mDestructionStarted = true;
718}
719
720void CameraService::BasicClient::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800721 mCameraService->removeClientByRemote(mRemoteBinder);
Igor Murashkin634a5152013-02-20 17:15:11 -0800722}
723
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800724status_t CameraService::BasicClient::startCameraOps() {
725 int32_t res;
726
727 mOpsCallback = new OpsCallback(this);
728
Igor Murashkine6800ce2013-03-04 17:25:57 -0800729 {
730 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
731 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
732 }
733
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800734 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
735 mClientPackageName, mOpsCallback);
736 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
737 mClientUid, mClientPackageName);
738
739 if (res != AppOpsManager::MODE_ALLOWED) {
740 ALOGI("Camera %d: Access for \"%s\" has been revoked",
741 mCameraId, String8(mClientPackageName).string());
742 return PERMISSION_DENIED;
743 }
744 mOpsActive = true;
745 return OK;
746}
747
748status_t CameraService::BasicClient::finishCameraOps() {
749 if (mOpsActive) {
750 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
751 mClientPackageName);
752 mOpsActive = false;
753 }
754 mAppOpsManager.stopWatchingMode(mOpsCallback);
755 mOpsCallback.clear();
756
757 return OK;
758}
759
760void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
761 String8 name(packageName);
762 String8 myName(mClientPackageName);
763
764 if (op != AppOpsManager::OP_CAMERA) {
765 ALOGW("Unexpected app ops notification received: %d", op);
766 return;
767 }
768
769 int32_t res;
770 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
771 mClientUid, mClientPackageName);
772 ALOGV("checkOp returns: %d, %s ", res,
773 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
774 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
775 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
776 "UNKNOWN");
777
778 if (res != AppOpsManager::MODE_ALLOWED) {
779 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
780 myName.string());
781 // Reset the client PID to allow server-initiated disconnect,
782 // and to prevent further calls by client.
783 mClientPid = getCallingPid();
784 notifyError();
785 disconnect();
786 }
787}
788
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789// ----------------------------------------------------------------------------
790
Keun young Parkd8973a72012-03-28 14:13:09 -0700791Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
792 return gCameraService->getClientLockById((int) user);
793}
794
795// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
796// be acquired for this to be safe
797CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
798 Client* client = gCameraService->getClientByIdUnsafe((int) user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700799
800 // This could happen if the Client is in the process of shutting down (the
801 // last strong reference is gone, but the destructor hasn't finished
802 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -0700803 if (client == NULL) return NULL;
804
805 // destruction already started, so should not be accessed
806 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807
Mathias Agopian65ab4712010-07-14 17:59:35 -0700808 return client;
809}
810
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800811void CameraService::Client::notifyError() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800812 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800813}
814
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700815// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700816void CameraService::Client::disconnect() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800817 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700818 mCameraService->setCameraFree(mCameraId);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800819 mCameraService->updateStatus(ICameraServiceListener::STATUS_AVAILABLE,
820 mCameraId);
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800821}
822
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800823CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
824 mClient(client) {
825}
826
827void CameraService::Client::OpsCallback::opChanged(int32_t op,
828 const String16& packageName) {
829 sp<BasicClient> client = mClient.promote();
830 if (client != NULL) {
831 client->opChanged(op, packageName);
832 }
833}
834
Mathias Agopian65ab4712010-07-14 17:59:35 -0700835// ----------------------------------------------------------------------------
Igor Murashkin634a5152013-02-20 17:15:11 -0800836// IProCamera
837// ----------------------------------------------------------------------------
838
839CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800840 const sp<IProCameraCallbacks>& remoteCallback,
841 const String16& clientPackageName,
842 int cameraId,
843 int cameraFacing,
844 int clientPid,
845 uid_t clientUid,
846 int servicePid)
847 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
848 clientPackageName, cameraId, cameraFacing,
849 clientPid, clientUid, servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800850{
851 mRemoteCallback = remoteCallback;
852}
853
854CameraService::ProClient::~ProClient() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800855}
856
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800857void CameraService::ProClient::notifyError() {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800858 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800859}
860
Igor Murashkin634a5152013-02-20 17:15:11 -0800861// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862
863static const int kDumpLockRetries = 50;
864static const int kDumpLockSleep = 60000;
865
866static bool tryLock(Mutex& mutex)
867{
868 bool locked = false;
869 for (int i = 0; i < kDumpLockRetries; ++i) {
870 if (mutex.tryLock() == NO_ERROR) {
871 locked = true;
872 break;
873 }
874 usleep(kDumpLockSleep);
875 }
876 return locked;
877}
878
879status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880 String8 result;
881 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700882 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883 "can't dump CameraService from pid=%d, uid=%d\n",
884 getCallingPid(),
885 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700886 write(fd, result.string(), result.size());
887 } else {
888 bool locked = tryLock(mServiceLock);
889 // failed to lock - CameraService is probably deadlocked
890 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700891 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700892 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893 }
894
895 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700896 if (!mModule) {
897 result = String8::format("No camera module available!\n");
898 write(fd, result.string(), result.size());
899 return NO_ERROR;
900 }
901
902 result = String8::format("Camera module HAL API version: 0x%x\n",
903 mModule->common.hal_api_version);
904 result.appendFormat("Camera module API version: 0x%x\n",
905 mModule->common.module_api_version);
906 result.appendFormat("Camera module name: %s\n",
907 mModule->common.name);
908 result.appendFormat("Camera module author: %s\n",
909 mModule->common.author);
910 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
911 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700913 result = String8::format("Camera %d static information:\n", i);
914 camera_info info;
915
916 status_t rc = mModule->get_camera_info(i, &info);
917 if (rc != OK) {
918 result.appendFormat(" Error reading static information!\n");
919 write(fd, result.string(), result.size());
920 } else {
921 result.appendFormat(" Facing: %s\n",
922 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
923 result.appendFormat(" Orientation: %d\n", info.orientation);
924 int deviceVersion;
925 if (mModule->common.module_api_version <
926 CAMERA_MODULE_API_VERSION_2_0) {
927 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
928 } else {
929 deviceVersion = info.device_version;
930 }
931 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
932 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
933 result.appendFormat(" Device static metadata:\n");
934 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700935 dump_indented_camera_metadata(info.static_camera_characteristics,
936 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700937 } else {
938 write(fd, result.string(), result.size());
939 }
940 }
941
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 sp<Client> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700943 if (client == 0) {
944 result = String8::format(" Device is closed, no client instance\n");
945 write(fd, result.string(), result.size());
946 continue;
947 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700948 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700949 result = String8::format(" Device is open. Client instance dump:\n");
950 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700951 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700952 }
953 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700954 result = String8::format("\nNo active camera clients yet.\n");
955 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700956 }
957
958 if (locked) mServiceLock.unlock();
959
960 // change logging level
961 int n = args.size();
962 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700963 String16 verboseOption("-v");
964 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700965 String8 levelStr(args[i+1]);
966 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700967 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700968 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700969 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700970 }
971 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700972
Mathias Agopian65ab4712010-07-14 17:59:35 -0700973 }
974 return NO_ERROR;
975}
976
Igor Murashkinecf17e82012-10-02 16:05:11 -0700977/*virtual*/void CameraService::binderDied(
978 const wp<IBinder> &who) {
979
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700980 /**
981 * While tempting to promote the wp<IBinder> into a sp,
982 * it's actually not supported by the binder driver
983 */
984
Igor Murashkinecf17e82012-10-02 16:05:11 -0700985 ALOGV("java clients' binder died");
986
Igor Murashkin634a5152013-02-20 17:15:11 -0800987 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700988
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700989 if (cameraClient == 0) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700990 ALOGV("java clients' binder death already cleaned up (normal case)");
991 return;
992 }
993
Igor Murashkinecf17e82012-10-02 16:05:11 -0700994 ALOGW("Disconnecting camera client %p since the binder for it "
995 "died (this pid %d)", cameraClient.get(), getCallingPid());
996
997 cameraClient->disconnect();
998
999}
1000
Igor Murashkinbfc99152013-02-27 12:55:20 -08001001void CameraService::updateStatus(ICameraServiceListener::Status status,
1002 int32_t cameraId) {
1003 // do not lock mServiceLock here or can get into a deadlock from
1004 // connect() -> ProClient::disconnect -> updateStatus
1005 Mutex::Autolock lock(mStatusMutex);
1006 updateStatusUnsafe(status, cameraId);
1007}
1008
1009void CameraService::updateStatusUnsafe(ICameraServiceListener::Status status,
1010 int32_t cameraId) {
1011
1012 ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1013
1014 mStatusList[cameraId] = status;
1015
1016 if (oldStatus != status) {
1017 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1018 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1019
1020 /**
1021 * ProClients lose their exclusive lock.
1022 * - Done before the CameraClient can initialize the HAL device,
1023 * since we want to be able to close it before they get to initialize
1024 */
1025 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1026 Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1027 Vector<wp<ProClient> >::const_iterator it;
1028
1029 for (it = proClients.begin(); it != proClients.end(); ++it) {
1030 sp<ProClient> proCl = it->promote();
1031 if (proCl.get() != NULL) {
1032 proCl->onExclusiveLockStolen();
1033 }
1034 }
1035 }
1036
1037 Vector<sp<ICameraServiceListener> >::const_iterator it;
1038 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1039 (*it)->onStatusChanged(status, cameraId);
1040 }
1041 }
1042}
1043
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044}; // namespace android