blob: d46ca88c1b633f0696b8dd73ac1516e24c867763 [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
Mathias Agopian65ab4712010-07-14 17:59:35 -0700179sp<ICamera> CameraService::connect(
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800180 const sp<ICameraClient>& cameraClient,
181 int cameraId,
182 const String16& clientPackageName,
183 int clientUid) {
184
185 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500187
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800188 LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
189 clientName8.string(), cameraId);
190
191 if (clientUid == USE_CALLING_UID) {
192 clientUid = getCallingUid();
193 } else {
194 // We only trust our own process to forward client UIDs
195 if (callingPid != getpid()) {
196 ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
197 callingPid);
198 return NULL;
199 }
200 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700201
Iliyan Malchev8951a972011-04-14 16:55:59 -0700202 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000203 ALOGE("Camera HAL module not loaded");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700204 return NULL;
205 }
206
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207 sp<Client> client;
208 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000209 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700210 callingPid, cameraId);
211 return NULL;
212 }
213
Wu-cheng Lia3355432011-05-20 14:54:25 +0800214 char value[PROPERTY_VALUE_MAX];
215 property_get("sys.secpolicy.camera.disabled", value, "0");
216 if (strcmp(value, "1") == 0) {
217 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000218 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800219 return NULL;
220 }
221
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800222 Mutex::Autolock lock(mServiceLock);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800223 if (mClient[cameraId] != 0) {
224 client = mClient[cameraId].promote();
225 if (client != 0) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800226 if (cameraClient->asBinder() ==
227 client->getRemoteCallback()->asBinder()) {
228
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800229 LOG1("CameraService::connect X (pid %d) (the same client)",
230 callingPid);
231 return client;
232 } else {
Igor Murashkin1d880232013-02-20 16:50:13 -0800233 // TODOSC: need to support 1 regular client, multiple shared clients here
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800234 ALOGW("CameraService::connect X (pid %d) rejected (existing client).",
235 callingPid);
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800236 return NULL;
237 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800238 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800239 mClient[cameraId].clear();
240 }
241
Igor Murashkin634a5152013-02-20 17:15:11 -0800242 /*
243 mBusy is set to false as the last step of the Client destructor,
244 after which it is guaranteed that the Client destructor has finished (
245 including any inherited destructors)
246
247 We only need this for a Client subclasses since we don't allow
248 multiple Clents to be opened concurrently, but multiple BasicClient
249 would be fine
250 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800251 if (mBusy[cameraId]) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800252
253 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
254 " (camera %d is still busy).", callingPid,
255 clientName8.string(), cameraId);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800256 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257 }
258
Igor Murashkin634a5152013-02-20 17:15:11 -0800259 int facing = -1;
260 int deviceVersion = getDeviceVersion(cameraId, &facing);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700261
Igor Murashkinbfc99152013-02-27 12:55:20 -0800262 if (isValidCameraId(cameraId)) {
263 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, cameraId);
264 }
265
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700266 switch(deviceVersion) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700267 case CAMERA_DEVICE_API_VERSION_1_0:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800268 client = new CameraClient(this, cameraClient,
269 clientPackageName, cameraId,
270 facing, callingPid, clientUid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700271 break;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700272 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkin634a5152013-02-20 17:15:11 -0800273 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800274 case CAMERA_DEVICE_API_VERSION_3_0:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800275 client = new Camera2Client(this, cameraClient,
276 clientPackageName, cameraId,
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800277 facing, callingPid, clientUid, getpid(),
278 deviceVersion);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700279 break;
Igor Murashkin634a5152013-02-20 17:15:11 -0800280 case -1:
281 ALOGE("Invalid camera id %d", cameraId);
282 return NULL;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700283 default:
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700284 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500285 return NULL;
286 }
287
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700288 if (client->initialize(mModule) != OK) {
Igor Murashkinbfc99152013-02-27 12:55:20 -0800289 // this is probably not recoverable.. but maybe the client can try again
290 updateStatus(ICameraServiceListener::STATUS_AVAILABLE, cameraId);
291
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700292 return NULL;
293 }
294
Igor Murashkinecf17e82012-10-02 16:05:11 -0700295 cameraClient->asBinder()->linkToDeath(this);
296
Mathias Agopian65ab4712010-07-14 17:59:35 -0700297 mClient[cameraId] = client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700298 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid());
Igor Murashkinbfc99152013-02-27 12:55:20 -0800299
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 return client;
301}
302
Igor Murashkin634a5152013-02-20 17:15:11 -0800303sp<IProCameraUser> CameraService::connect(
304 const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -0800305 int cameraId,
306 const String16& clientPackageName,
307 int clientUid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800308{
Igor Murashkinbfc99152013-02-27 12:55:20 -0800309 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 int callingPid = getCallingPid();
Igor Murashkin634a5152013-02-20 17:15:11 -0800311
Igor Murashkinc073ba52013-02-26 14:32:34 -0800312 // TODO: use clientPackageName and clientUid with appOpsMangr
313
Igor Murashkin634a5152013-02-20 17:15:11 -0800314 LOG1("CameraService::connectPro E (pid %d, id %d)", callingPid, cameraId);
315
316 if (!mModule) {
317 ALOGE("Camera HAL module not loaded");
318 return NULL;
319 }
320
321 sp<ProClient> client;
322 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
323 ALOGE("CameraService::connectPro X (pid %d) rejected (invalid cameraId %d).",
324 callingPid, cameraId);
325 return NULL;
326 }
327
328 char value[PROPERTY_VALUE_MAX];
329 property_get("sys.secpolicy.camera.disabled", value, "0");
330 if (strcmp(value, "1") == 0) {
331 // Camera is disabled by DevicePolicyManager.
332 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
333 return NULL;
334 }
335
Igor Murashkinbfc99152013-02-27 12:55:20 -0800336 // TODO: allow concurrent connections with a ProCamera
337 if (mBusy[cameraId]) {
338
339 ALOGW("CameraService::connectPro X (pid %d, \"%s\") rejected"
340 " (camera %d is still busy).", callingPid,
341 clientName8.string(), cameraId);
342 return NULL;
343 }
344
Igor Murashkin634a5152013-02-20 17:15:11 -0800345 int facing = -1;
346 int deviceVersion = getDeviceVersion(cameraId, &facing);
347
348 switch(deviceVersion) {
349 case CAMERA_DEVICE_API_VERSION_1_0:
350 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", cameraId);
351 return NULL;
352 break;
353 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkin985fd302013-02-20 18:24:43 -0800354 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800355 client = new ProCamera2Client(this, cameraCb, String16(),
356 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
Igor Murashkin634a5152013-02-20 17:15:11 -0800357 break;
358 case -1:
359 ALOGE("Invalid camera id %d", cameraId);
360 return NULL;
361 default:
362 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
363 return NULL;
364 }
365
366 if (client->initialize(mModule) != OK) {
367 return NULL;
368 }
369
370 mProClientList[cameraId].push(client);
371
372 cameraCb->asBinder()->linkToDeath(this);
373
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800374 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
375 getpid());
Igor Murashkin634a5152013-02-20 17:15:11 -0800376 return client;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800377}
Igor Murashkin634a5152013-02-20 17:15:11 -0800378
Igor Murashkinbfc99152013-02-27 12:55:20 -0800379status_t CameraService::addListener(
380 const sp<ICameraServiceListener>& listener) {
381 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -0800382
Igor Murashkinbfc99152013-02-27 12:55:20 -0800383 Mutex::Autolock lock(mServiceLock);
384
385 Vector<sp<ICameraServiceListener> >::iterator it, end;
386 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
387 if ((*it)->asBinder() == listener->asBinder()) {
388 ALOGW("%s: Tried to add listener %p which was already subscribed",
389 __FUNCTION__, listener.get());
390 return ALREADY_EXISTS;
391 }
392 }
393
394 mListenerList.push_back(listener);
395
396 return OK;
397}
398status_t CameraService::removeListener(
399 const sp<ICameraServiceListener>& listener) {
400 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
401
402 Mutex::Autolock lock(mServiceLock);
403
404 Vector<sp<ICameraServiceListener> >::iterator it;
405 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
406 if ((*it)->asBinder() == listener->asBinder()) {
407 mListenerList.erase(it);
408 return OK;
409 }
410 }
411
412 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
413 __FUNCTION__, listener.get());
414
415 return BAD_VALUE;
Igor Murashkin634a5152013-02-20 17:15:11 -0800416}
417
418void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
419 int callingPid = getCallingPid();
420 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421
Igor Murashkinecf17e82012-10-02 16:05:11 -0700422 // Declare this before the lock to make absolutely sure the
423 // destructor won't be called with the lock held.
424 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425
Igor Murashkinecf17e82012-10-02 16:05:11 -0700426 int outIndex;
Igor Murashkin634a5152013-02-20 17:15:11 -0800427 sp<Client> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700428
429 if (client != 0) {
430 // Found our camera, clear and leave.
431 LOG1("removeClient: clear camera %d", outIndex);
432 mClient[outIndex].clear();
433
434 client->unlinkToDeath(this);
Igor Murashkin634a5152013-02-20 17:15:11 -0800435 } else {
436
437 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
438
439 if (clientPro != NULL) {
440 // Found our camera, clear and leave.
441 LOG1("removeClient: clear pro %p", clientPro.get());
442
443 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
444 }
Igor Murashkinecf17e82012-10-02 16:05:11 -0700445 }
446
Igor Murashkin634a5152013-02-20 17:15:11 -0800447 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
448}
449
450sp<CameraService::ProClient> CameraService::findProClientUnsafe(
451 const wp<IBinder>& cameraCallbacksRemote)
452{
453 sp<ProClient> clientPro;
454
455 for (int i = 0; i < mNumberOfCameras; ++i) {
456 Vector<size_t> removeIdx;
457
458 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
459 wp<ProClient> cl = mProClientList[i][j];
460
461 sp<ProClient> clStrong = cl.promote();
462 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
463 clientPro = clStrong;
464 break;
465 } else if (clStrong == NULL) {
466 // mark to clean up dead ptr
467 removeIdx.push(j);
468 }
469 }
470
471 // remove stale ptrs (in reverse so the indices dont change)
472 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
473 mProClientList[i].removeAt(removeIdx[j]);
474 }
475
476 }
477
478 return clientPro;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700479}
480
481sp<CameraService::Client> CameraService::findClientUnsafe(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700482 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700483 sp<Client> client;
484
485 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486
487 // This happens when we have already disconnected (or this is
488 // just another unused camera).
489 if (mClient[i] == 0) continue;
490
491 // Promote mClient. It can fail if we are called from this path:
Igor Murashkin634a5152013-02-20 17:15:11 -0800492 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -0700493 client = mClient[i].promote();
494
Igor Murashkinecf17e82012-10-02 16:05:11 -0700495 // Clean up stale client entry
496 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700497 mClient[i].clear();
498 continue;
499 }
500
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800501 if (cameraClient == client->getRemoteCallback()->asBinder()) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700502 // Found our camera
503 outIndex = i;
504 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 }
506 }
507
Igor Murashkinecf17e82012-10-02 16:05:11 -0700508 outIndex = -1;
509 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700510}
511
Keun young Parkd8973a72012-03-28 14:13:09 -0700512CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700513 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700514 return mClient[cameraId].unsafe_get();
515}
516
517Mutex* CameraService::getClientLockById(int cameraId) {
518 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
519 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520}
521
Igor Murashkin634a5152013-02-20 17:15:11 -0800522sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700523 const wp<IBinder>& cameraClient) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700524
525 // Declare this before the lock to make absolutely sure the
526 // destructor won't be called with the lock held.
Igor Murashkin634a5152013-02-20 17:15:11 -0800527 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700528
529 Mutex::Autolock lock(mServiceLock);
530
531 int outIndex;
532 client = findClientUnsafe(cameraClient, outIndex);
533
534 return client;
535}
536
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537status_t CameraService::onTransact(
538 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
539 // Permission checks
540 switch (code) {
541 case BnCameraService::CONNECT:
Igor Murashkin634a5152013-02-20 17:15:11 -0800542 case BnCameraService::CONNECT_PRO:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700543 const int pid = getCallingPid();
544 const int self_pid = getpid();
545 if (pid != self_pid) {
546 // we're called from a different process, do the real check
547 if (!checkCallingPermission(
548 String16("android.permission.CAMERA"))) {
549 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000550 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700551 "can't use the camera pid=%d, uid=%d", pid, uid);
552 return PERMISSION_DENIED;
553 }
554 }
555 break;
556 }
557
558 return BnCameraService::onTransact(code, data, reply, flags);
559}
560
561// The reason we need this busy bit is a new CameraService::connect() request
562// may come in while the previous Client's destructor has not been run or is
563// still running. If the last strong reference of the previous Client is gone
564// but the destructor has not been finished, we should not allow the new Client
565// to be created because we need to wait for the previous Client to tear down
566// the hardware first.
567void CameraService::setCameraBusy(int cameraId) {
568 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700569
570 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571}
572
573void CameraService::setCameraFree(int cameraId) {
574 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700575
576 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577}
578
579// We share the media players for shutter and recording sound for all clients.
580// A reference count is kept to determine when we will actually release the
581// media players.
582
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800583MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584 MediaPlayer* mp = new MediaPlayer();
585 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800586 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700587 mp->prepare();
588 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000589 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 return NULL;
591 }
592 return mp;
593}
594
595void CameraService::loadSound() {
596 Mutex::Autolock lock(mSoundLock);
597 LOG1("CameraService::loadSound ref=%d", mSoundRef);
598 if (mSoundRef++) return;
599
600 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
601 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
602}
603
604void CameraService::releaseSound() {
605 Mutex::Autolock lock(mSoundLock);
606 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
607 if (--mSoundRef) return;
608
609 for (int i = 0; i < NUM_SOUNDS; i++) {
610 if (mSoundPlayer[i] != 0) {
611 mSoundPlayer[i]->disconnect();
612 mSoundPlayer[i].clear();
613 }
614 }
615}
616
617void CameraService::playSound(sound_kind kind) {
618 LOG1("playSound(%d)", kind);
619 Mutex::Autolock lock(mSoundLock);
620 sp<MediaPlayer> player = mSoundPlayer[kind];
621 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800622 player->seekTo(0);
623 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700624 }
625}
626
627// ----------------------------------------------------------------------------
628
629CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700630 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800631 const String16& clientPackageName,
632 int cameraId, int cameraFacing,
633 int clientPid, uid_t clientUid,
634 int servicePid) :
Igor Murashkin634a5152013-02-20 17:15:11 -0800635 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800636 clientPackageName,
637 cameraId, cameraFacing,
638 clientPid, clientUid,
639 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800640{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800642 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800644 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700645
Mathias Agopian65ab4712010-07-14 17:59:35 -0700646 cameraService->setCameraBusy(cameraId);
647 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800648
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800649 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650}
651
Mathias Agopian65ab4712010-07-14 17:59:35 -0700652// tear down the client
653CameraService::Client::~Client() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800654 mDestructionStarted = true;
655
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700656 mCameraService->releaseSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800657 finishCameraOps();
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700658 // unconditionally disconnect. function is idempotent
659 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660}
661
Igor Murashkin634a5152013-02-20 17:15:11 -0800662CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800663 const sp<IBinder>& remoteCallback,
664 const String16& clientPackageName,
665 int cameraId, int cameraFacing,
666 int clientPid, uid_t clientUid,
667 int servicePid):
668 mClientPackageName(clientPackageName)
Igor Murashkin634a5152013-02-20 17:15:11 -0800669{
670 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800671 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -0800672 mCameraId = cameraId;
673 mCameraFacing = cameraFacing;
674 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800675 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -0800676 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800677 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -0800678 mDestructionStarted = false;
679}
680
681CameraService::BasicClient::~BasicClient() {
682 mDestructionStarted = true;
683}
684
685void CameraService::BasicClient::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800686 mCameraService->removeClientByRemote(mRemoteBinder);
Igor Murashkin634a5152013-02-20 17:15:11 -0800687}
688
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800689status_t CameraService::BasicClient::startCameraOps() {
690 int32_t res;
691
692 mOpsCallback = new OpsCallback(this);
693
694 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
695 mClientPackageName, mOpsCallback);
696 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
697 mClientUid, mClientPackageName);
698
699 if (res != AppOpsManager::MODE_ALLOWED) {
700 ALOGI("Camera %d: Access for \"%s\" has been revoked",
701 mCameraId, String8(mClientPackageName).string());
702 return PERMISSION_DENIED;
703 }
704 mOpsActive = true;
705 return OK;
706}
707
708status_t CameraService::BasicClient::finishCameraOps() {
709 if (mOpsActive) {
710 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
711 mClientPackageName);
712 mOpsActive = false;
713 }
714 mAppOpsManager.stopWatchingMode(mOpsCallback);
715 mOpsCallback.clear();
716
717 return OK;
718}
719
720void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
721 String8 name(packageName);
722 String8 myName(mClientPackageName);
723
724 if (op != AppOpsManager::OP_CAMERA) {
725 ALOGW("Unexpected app ops notification received: %d", op);
726 return;
727 }
728
729 int32_t res;
730 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
731 mClientUid, mClientPackageName);
732 ALOGV("checkOp returns: %d, %s ", res,
733 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
734 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
735 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
736 "UNKNOWN");
737
738 if (res != AppOpsManager::MODE_ALLOWED) {
739 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
740 myName.string());
741 // Reset the client PID to allow server-initiated disconnect,
742 // and to prevent further calls by client.
743 mClientPid = getCallingPid();
744 notifyError();
745 disconnect();
746 }
747}
748
Mathias Agopian65ab4712010-07-14 17:59:35 -0700749// ----------------------------------------------------------------------------
750
Keun young Parkd8973a72012-03-28 14:13:09 -0700751Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
752 return gCameraService->getClientLockById((int) user);
753}
754
755// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
756// be acquired for this to be safe
757CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
758 Client* client = gCameraService->getClientByIdUnsafe((int) user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700759
760 // This could happen if the Client is in the process of shutting down (the
761 // last strong reference is gone, but the destructor hasn't finished
762 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -0700763 if (client == NULL) return NULL;
764
765 // destruction already started, so should not be accessed
766 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767
Mathias Agopian65ab4712010-07-14 17:59:35 -0700768 return client;
769}
770
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800771void CameraService::Client::notifyError() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800772 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800773}
774
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700775// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700776void CameraService::Client::disconnect() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800777 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700778 mCameraService->setCameraFree(mCameraId);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800779 mCameraService->updateStatus(ICameraServiceListener::STATUS_AVAILABLE,
780 mCameraId);
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800781}
782
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800783CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
784 mClient(client) {
785}
786
787void CameraService::Client::OpsCallback::opChanged(int32_t op,
788 const String16& packageName) {
789 sp<BasicClient> client = mClient.promote();
790 if (client != NULL) {
791 client->opChanged(op, packageName);
792 }
793}
794
Mathias Agopian65ab4712010-07-14 17:59:35 -0700795// ----------------------------------------------------------------------------
Igor Murashkin634a5152013-02-20 17:15:11 -0800796// IProCamera
797// ----------------------------------------------------------------------------
798
799CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800800 const sp<IProCameraCallbacks>& remoteCallback,
801 const String16& clientPackageName,
802 int cameraId,
803 int cameraFacing,
804 int clientPid,
805 uid_t clientUid,
806 int servicePid)
807 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
808 clientPackageName, cameraId, cameraFacing,
809 clientPid, clientUid, servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800810{
811 mRemoteCallback = remoteCallback;
812}
813
814CameraService::ProClient::~ProClient() {
815 mDestructionStarted = true;
816
817 ProClient::disconnect();
818}
819
820status_t CameraService::ProClient::connect(const sp<IProCameraCallbacks>& callbacks) {
821 ALOGE("%s: not implemented yet", __FUNCTION__);
822
823 return INVALID_OPERATION;
824}
825
826void CameraService::ProClient::disconnect() {
827 BasicClient::disconnect();
828}
829
830status_t CameraService::ProClient::initialize(camera_module_t* module)
831{
832 ALOGW("%s: not implemented yet", __FUNCTION__);
833 return OK;
834}
835
836status_t CameraService::ProClient::exclusiveTryLock() {
837 ALOGE("%s: not implemented yet", __FUNCTION__);
838 return INVALID_OPERATION;
839}
840
841status_t CameraService::ProClient::exclusiveLock() {
842 ALOGE("%s: not implemented yet", __FUNCTION__);
843 return INVALID_OPERATION;
844}
845
846status_t CameraService::ProClient::exclusiveUnlock() {
847 ALOGE("%s: not implemented yet", __FUNCTION__);
848 return INVALID_OPERATION;
849}
850
851bool CameraService::ProClient::hasExclusiveLock() {
852 ALOGE("%s: not implemented yet", __FUNCTION__);
853 return false;
854}
855
Igor Murashkinbfc99152013-02-27 12:55:20 -0800856void CameraService::ProClient::onExclusiveLockStolen() {
857 ALOGE("%s: not implemented yet", __FUNCTION__);
858}
859
Igor Murashkin634a5152013-02-20 17:15:11 -0800860status_t CameraService::ProClient::submitRequest(camera_metadata_t* request, bool streaming) {
861 ALOGE("%s: not implemented yet", __FUNCTION__);
862
863 free_camera_metadata(request);
864
865 return INVALID_OPERATION;
866}
867
868status_t CameraService::ProClient::cancelRequest(int requestId) {
869 ALOGE("%s: not implemented yet", __FUNCTION__);
870
871 return INVALID_OPERATION;
872}
873
874status_t CameraService::ProClient::requestStream(int streamId) {
875 ALOGE("%s: not implemented yet", __FUNCTION__);
876
877 return INVALID_OPERATION;
878}
879
880status_t CameraService::ProClient::cancelStream(int streamId) {
881 ALOGE("%s: not implemented yet", __FUNCTION__);
882
883 return INVALID_OPERATION;
884}
885
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800886void CameraService::ProClient::notifyError() {
887 ALOGE("%s: not implemented yet", __FUNCTION__);
888}
889
Igor Murashkin634a5152013-02-20 17:15:11 -0800890// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700891
892static const int kDumpLockRetries = 50;
893static const int kDumpLockSleep = 60000;
894
895static bool tryLock(Mutex& mutex)
896{
897 bool locked = false;
898 for (int i = 0; i < kDumpLockRetries; ++i) {
899 if (mutex.tryLock() == NO_ERROR) {
900 locked = true;
901 break;
902 }
903 usleep(kDumpLockSleep);
904 }
905 return locked;
906}
907
908status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700909 String8 result;
910 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700911 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700912 "can't dump CameraService from pid=%d, uid=%d\n",
913 getCallingPid(),
914 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700915 write(fd, result.string(), result.size());
916 } else {
917 bool locked = tryLock(mServiceLock);
918 // failed to lock - CameraService is probably deadlocked
919 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700920 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700921 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700922 }
923
924 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700925 if (!mModule) {
926 result = String8::format("No camera module available!\n");
927 write(fd, result.string(), result.size());
928 return NO_ERROR;
929 }
930
931 result = String8::format("Camera module HAL API version: 0x%x\n",
932 mModule->common.hal_api_version);
933 result.appendFormat("Camera module API version: 0x%x\n",
934 mModule->common.module_api_version);
935 result.appendFormat("Camera module name: %s\n",
936 mModule->common.name);
937 result.appendFormat("Camera module author: %s\n",
938 mModule->common.author);
939 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
940 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700942 result = String8::format("Camera %d static information:\n", i);
943 camera_info info;
944
945 status_t rc = mModule->get_camera_info(i, &info);
946 if (rc != OK) {
947 result.appendFormat(" Error reading static information!\n");
948 write(fd, result.string(), result.size());
949 } else {
950 result.appendFormat(" Facing: %s\n",
951 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
952 result.appendFormat(" Orientation: %d\n", info.orientation);
953 int deviceVersion;
954 if (mModule->common.module_api_version <
955 CAMERA_MODULE_API_VERSION_2_0) {
956 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
957 } else {
958 deviceVersion = info.device_version;
959 }
960 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
961 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
962 result.appendFormat(" Device static metadata:\n");
963 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700964 dump_indented_camera_metadata(info.static_camera_characteristics,
965 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700966 } else {
967 write(fd, result.string(), result.size());
968 }
969 }
970
Mathias Agopian65ab4712010-07-14 17:59:35 -0700971 sp<Client> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700972 if (client == 0) {
973 result = String8::format(" Device is closed, no client instance\n");
974 write(fd, result.string(), result.size());
975 continue;
976 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700977 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700978 result = String8::format(" Device is open. Client instance dump:\n");
979 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700980 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700981 }
982 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700983 result = String8::format("\nNo active camera clients yet.\n");
984 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 }
986
987 if (locked) mServiceLock.unlock();
988
989 // change logging level
990 int n = args.size();
991 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700992 String16 verboseOption("-v");
993 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700994 String8 levelStr(args[i+1]);
995 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700996 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700998 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700999 }
1000 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001001
Mathias Agopian65ab4712010-07-14 17:59:35 -07001002 }
1003 return NO_ERROR;
1004}
1005
Igor Murashkinecf17e82012-10-02 16:05:11 -07001006/*virtual*/void CameraService::binderDied(
1007 const wp<IBinder> &who) {
1008
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001009 /**
1010 * While tempting to promote the wp<IBinder> into a sp,
1011 * it's actually not supported by the binder driver
1012 */
1013
Igor Murashkinecf17e82012-10-02 16:05:11 -07001014 ALOGV("java clients' binder died");
1015
Igor Murashkin634a5152013-02-20 17:15:11 -08001016 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001017
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001018 if (cameraClient == 0) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001019 ALOGV("java clients' binder death already cleaned up (normal case)");
1020 return;
1021 }
1022
Igor Murashkinecf17e82012-10-02 16:05:11 -07001023 ALOGW("Disconnecting camera client %p since the binder for it "
1024 "died (this pid %d)", cameraClient.get(), getCallingPid());
1025
1026 cameraClient->disconnect();
1027
1028}
1029
Igor Murashkinbfc99152013-02-27 12:55:20 -08001030void CameraService::updateStatus(ICameraServiceListener::Status status,
1031 int32_t cameraId) {
1032 // do not lock mServiceLock here or can get into a deadlock from
1033 // connect() -> ProClient::disconnect -> updateStatus
1034 Mutex::Autolock lock(mStatusMutex);
1035 updateStatusUnsafe(status, cameraId);
1036}
1037
1038void CameraService::updateStatusUnsafe(ICameraServiceListener::Status status,
1039 int32_t cameraId) {
1040
1041 ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1042
1043 mStatusList[cameraId] = status;
1044
1045 if (oldStatus != status) {
1046 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1047 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1048
1049 /**
1050 * ProClients lose their exclusive lock.
1051 * - Done before the CameraClient can initialize the HAL device,
1052 * since we want to be able to close it before they get to initialize
1053 */
1054 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1055 Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1056 Vector<wp<ProClient> >::const_iterator it;
1057
1058 for (it = proClients.begin(); it != proClients.end(); ++it) {
1059 sp<ProClient> proCl = it->promote();
1060 if (proCl.get() != NULL) {
1061 proCl->onExclusiveLockStolen();
1062 }
1063 }
1064 }
1065
1066 Vector<sp<ICameraServiceListener> >::const_iterator it;
1067 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1068 (*it)->onStatusChanged(status, cameraId);
1069 }
1070 }
1071}
1072
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073}; // namespace android