blob: 76361434ba45c6f8b8eb6851bf85fdd7d0631964 [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
280 Mutex::Autolock lock(mServiceLock);
281 if (!canConnectUnsafe(cameraId, clientPackageName,
282 cameraClient->asBinder(),
283 /*out*/client)) {
284 return NULL;
285 } else if (client.get() != NULL) {
286 return client;
287 }
288
Igor Murashkin634a5152013-02-20 17:15:11 -0800289 int facing = -1;
290 int deviceVersion = getDeviceVersion(cameraId, &facing);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700291
Igor Murashkine6800ce2013-03-04 17:25:57 -0800292 // If there are other non-exclusive users of the camera,
293 // this will tear them down before we can reuse the camera
Igor Murashkinbfc99152013-02-27 12:55:20 -0800294 if (isValidCameraId(cameraId)) {
295 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, cameraId);
296 }
297
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700298 switch(deviceVersion) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700299 case CAMERA_DEVICE_API_VERSION_1_0:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800300 client = new CameraClient(this, cameraClient,
301 clientPackageName, cameraId,
302 facing, callingPid, clientUid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700303 break;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700304 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkin634a5152013-02-20 17:15:11 -0800305 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800306 case CAMERA_DEVICE_API_VERSION_3_0:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800307 client = new Camera2Client(this, cameraClient,
308 clientPackageName, cameraId,
Eino-Ville Talvalab99c5b82013-02-06 17:20:07 -0800309 facing, callingPid, clientUid, getpid(),
310 deviceVersion);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700311 break;
Igor Murashkin634a5152013-02-20 17:15:11 -0800312 case -1:
313 ALOGE("Invalid camera id %d", cameraId);
314 return NULL;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700315 default:
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700316 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500317 return NULL;
318 }
319
Igor Murashkine6800ce2013-03-04 17:25:57 -0800320 if (!connectFinishUnsafe(client, client->asBinder())) {
Igor Murashkinbfc99152013-02-27 12:55:20 -0800321 // this is probably not recoverable.. but maybe the client can try again
322 updateStatus(ICameraServiceListener::STATUS_AVAILABLE, cameraId);
323
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700324 return NULL;
325 }
326
Mathias Agopian65ab4712010-07-14 17:59:35 -0700327 mClient[cameraId] = client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700328 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid());
Igor Murashkinbfc99152013-02-27 12:55:20 -0800329
Mathias Agopian65ab4712010-07-14 17:59:35 -0700330 return client;
331}
332
Igor Murashkine6800ce2013-03-04 17:25:57 -0800333bool CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
334 const sp<IBinder>& clientBinder) {
335 if (client->initialize(mModule) != OK) {
336 return false;
337 }
338
339 clientBinder->linkToDeath(this);
340
341 return true;
342}
343
Igor Murashkin634a5152013-02-20 17:15:11 -0800344sp<IProCameraUser> CameraService::connect(
345 const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -0800346 int cameraId,
347 const String16& clientPackageName,
348 int clientUid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800349{
Igor Murashkinbfc99152013-02-27 12:55:20 -0800350 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 int callingPid = getCallingPid();
Igor Murashkin634a5152013-02-20 17:15:11 -0800352
Igor Murashkine6800ce2013-03-04 17:25:57 -0800353 LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid,
354 clientName8.string(), cameraId);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800355
Igor Murashkine6800ce2013-03-04 17:25:57 -0800356 if (!validateConnect(cameraId, /*inout*/clientUid)) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800357 return NULL;
358 }
359
Igor Murashkine6800ce2013-03-04 17:25:57 -0800360 Mutex::Autolock lock(mServiceLock);
361 {
362 sp<Client> client;
363 if (!canConnectUnsafe(cameraId, clientPackageName,
364 cameraCb->asBinder(),
365 /*out*/client)) {
366 return NULL;
367 }
368 }
369
Igor Murashkin634a5152013-02-20 17:15:11 -0800370 sp<ProClient> client;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800371
Igor Murashkin634a5152013-02-20 17:15:11 -0800372 int facing = -1;
373 int deviceVersion = getDeviceVersion(cameraId, &facing);
374
375 switch(deviceVersion) {
376 case CAMERA_DEVICE_API_VERSION_1_0:
377 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", cameraId);
378 return NULL;
379 break;
380 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkin985fd302013-02-20 18:24:43 -0800381 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800382 client = new ProCamera2Client(this, cameraCb, String16(),
383 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
Igor Murashkin634a5152013-02-20 17:15:11 -0800384 break;
385 case -1:
386 ALOGE("Invalid camera id %d", cameraId);
387 return NULL;
388 default:
389 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
390 return NULL;
391 }
392
Igor Murashkine6800ce2013-03-04 17:25:57 -0800393 if (!connectFinishUnsafe(client, client->asBinder())) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800394 return NULL;
395 }
396
397 mProClientList[cameraId].push(client);
398
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800399 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
400 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800401
Igor Murashkin634a5152013-02-20 17:15:11 -0800402 return client;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800403}
Igor Murashkin634a5152013-02-20 17:15:11 -0800404
Igor Murashkinbfc99152013-02-27 12:55:20 -0800405status_t CameraService::addListener(
406 const sp<ICameraServiceListener>& listener) {
407 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -0800408
Igor Murashkinbfc99152013-02-27 12:55:20 -0800409 Mutex::Autolock lock(mServiceLock);
410
411 Vector<sp<ICameraServiceListener> >::iterator it, end;
412 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
413 if ((*it)->asBinder() == listener->asBinder()) {
414 ALOGW("%s: Tried to add listener %p which was already subscribed",
415 __FUNCTION__, listener.get());
416 return ALREADY_EXISTS;
417 }
418 }
419
420 mListenerList.push_back(listener);
421
422 return OK;
423}
424status_t CameraService::removeListener(
425 const sp<ICameraServiceListener>& listener) {
426 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
427
428 Mutex::Autolock lock(mServiceLock);
429
430 Vector<sp<ICameraServiceListener> >::iterator it;
431 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
432 if ((*it)->asBinder() == listener->asBinder()) {
433 mListenerList.erase(it);
434 return OK;
435 }
436 }
437
438 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
439 __FUNCTION__, listener.get());
440
441 return BAD_VALUE;
Igor Murashkin634a5152013-02-20 17:15:11 -0800442}
443
444void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
445 int callingPid = getCallingPid();
446 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700447
Igor Murashkinecf17e82012-10-02 16:05:11 -0700448 // Declare this before the lock to make absolutely sure the
449 // destructor won't be called with the lock held.
450 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700451
Igor Murashkinecf17e82012-10-02 16:05:11 -0700452 int outIndex;
Igor Murashkin634a5152013-02-20 17:15:11 -0800453 sp<Client> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700454
455 if (client != 0) {
456 // Found our camera, clear and leave.
457 LOG1("removeClient: clear camera %d", outIndex);
458 mClient[outIndex].clear();
459
460 client->unlinkToDeath(this);
Igor Murashkin634a5152013-02-20 17:15:11 -0800461 } else {
462
463 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
464
465 if (clientPro != NULL) {
466 // Found our camera, clear and leave.
467 LOG1("removeClient: clear pro %p", clientPro.get());
468
469 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
470 }
Igor Murashkinecf17e82012-10-02 16:05:11 -0700471 }
472
Igor Murashkin634a5152013-02-20 17:15:11 -0800473 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
474}
475
476sp<CameraService::ProClient> CameraService::findProClientUnsafe(
477 const wp<IBinder>& cameraCallbacksRemote)
478{
479 sp<ProClient> clientPro;
480
481 for (int i = 0; i < mNumberOfCameras; ++i) {
482 Vector<size_t> removeIdx;
483
484 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
485 wp<ProClient> cl = mProClientList[i][j];
486
487 sp<ProClient> clStrong = cl.promote();
488 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
489 clientPro = clStrong;
490 break;
491 } else if (clStrong == NULL) {
492 // mark to clean up dead ptr
493 removeIdx.push(j);
494 }
495 }
496
497 // remove stale ptrs (in reverse so the indices dont change)
498 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
499 mProClientList[i].removeAt(removeIdx[j]);
500 }
501
502 }
503
504 return clientPro;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700505}
506
507sp<CameraService::Client> CameraService::findClientUnsafe(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700508 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700509 sp<Client> client;
510
511 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700512
513 // This happens when we have already disconnected (or this is
514 // just another unused camera).
515 if (mClient[i] == 0) continue;
516
517 // Promote mClient. It can fail if we are called from this path:
Igor Murashkin634a5152013-02-20 17:15:11 -0800518 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -0700519 client = mClient[i].promote();
520
Igor Murashkinecf17e82012-10-02 16:05:11 -0700521 // Clean up stale client entry
522 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700523 mClient[i].clear();
524 continue;
525 }
526
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800527 if (cameraClient == client->getRemoteCallback()->asBinder()) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700528 // Found our camera
529 outIndex = i;
530 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 }
532 }
533
Igor Murashkinecf17e82012-10-02 16:05:11 -0700534 outIndex = -1;
535 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536}
537
Keun young Parkd8973a72012-03-28 14:13:09 -0700538CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700540 return mClient[cameraId].unsafe_get();
541}
542
543Mutex* CameraService::getClientLockById(int cameraId) {
544 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
545 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546}
547
Igor Murashkin634a5152013-02-20 17:15:11 -0800548sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700549 const wp<IBinder>& cameraClient) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700550
551 // Declare this before the lock to make absolutely sure the
552 // destructor won't be called with the lock held.
Igor Murashkin634a5152013-02-20 17:15:11 -0800553 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700554
555 Mutex::Autolock lock(mServiceLock);
556
557 int outIndex;
558 client = findClientUnsafe(cameraClient, outIndex);
559
560 return client;
561}
562
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563status_t CameraService::onTransact(
564 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
565 // Permission checks
566 switch (code) {
567 case BnCameraService::CONNECT:
Igor Murashkin634a5152013-02-20 17:15:11 -0800568 case BnCameraService::CONNECT_PRO:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569 const int pid = getCallingPid();
570 const int self_pid = getpid();
571 if (pid != self_pid) {
572 // we're called from a different process, do the real check
573 if (!checkCallingPermission(
574 String16("android.permission.CAMERA"))) {
575 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000576 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577 "can't use the camera pid=%d, uid=%d", pid, uid);
578 return PERMISSION_DENIED;
579 }
580 }
581 break;
582 }
583
584 return BnCameraService::onTransact(code, data, reply, flags);
585}
586
587// The reason we need this busy bit is a new CameraService::connect() request
588// may come in while the previous Client's destructor has not been run or is
589// still running. If the last strong reference of the previous Client is gone
590// but the destructor has not been finished, we should not allow the new Client
591// to be created because we need to wait for the previous Client to tear down
592// the hardware first.
593void CameraService::setCameraBusy(int cameraId) {
594 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700595
596 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700597}
598
599void CameraService::setCameraFree(int cameraId) {
600 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700601
602 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700603}
604
605// We share the media players for shutter and recording sound for all clients.
606// A reference count is kept to determine when we will actually release the
607// media players.
608
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800609MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610 MediaPlayer* mp = new MediaPlayer();
611 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800612 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700613 mp->prepare();
614 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000615 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700616 return NULL;
617 }
618 return mp;
619}
620
621void CameraService::loadSound() {
622 Mutex::Autolock lock(mSoundLock);
623 LOG1("CameraService::loadSound ref=%d", mSoundRef);
624 if (mSoundRef++) return;
625
626 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
627 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
628}
629
630void CameraService::releaseSound() {
631 Mutex::Autolock lock(mSoundLock);
632 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
633 if (--mSoundRef) return;
634
635 for (int i = 0; i < NUM_SOUNDS; i++) {
636 if (mSoundPlayer[i] != 0) {
637 mSoundPlayer[i]->disconnect();
638 mSoundPlayer[i].clear();
639 }
640 }
641}
642
643void CameraService::playSound(sound_kind kind) {
644 LOG1("playSound(%d)", kind);
645 Mutex::Autolock lock(mSoundLock);
646 sp<MediaPlayer> player = mSoundPlayer[kind];
647 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800648 player->seekTo(0);
649 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 }
651}
652
653// ----------------------------------------------------------------------------
654
655CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700656 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800657 const String16& clientPackageName,
658 int cameraId, int cameraFacing,
659 int clientPid, uid_t clientUid,
660 int servicePid) :
Igor Murashkin634a5152013-02-20 17:15:11 -0800661 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800662 clientPackageName,
663 cameraId, cameraFacing,
664 clientPid, clientUid,
665 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800666{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700667 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800668 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700669
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800670 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700671
Mathias Agopian65ab4712010-07-14 17:59:35 -0700672 cameraService->setCameraBusy(cameraId);
673 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800674
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800675 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676}
677
Mathias Agopian65ab4712010-07-14 17:59:35 -0700678// tear down the client
679CameraService::Client::~Client() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800680 mDestructionStarted = true;
681
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700682 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700683 // unconditionally disconnect. function is idempotent
684 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685}
686
Igor Murashkin634a5152013-02-20 17:15:11 -0800687CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800688 const sp<IBinder>& remoteCallback,
689 const String16& clientPackageName,
690 int cameraId, int cameraFacing,
691 int clientPid, uid_t clientUid,
692 int servicePid):
693 mClientPackageName(clientPackageName)
Igor Murashkin634a5152013-02-20 17:15:11 -0800694{
695 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800696 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -0800697 mCameraId = cameraId;
698 mCameraFacing = cameraFacing;
699 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800700 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -0800701 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800702 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -0800703 mDestructionStarted = false;
704}
705
706CameraService::BasicClient::~BasicClient() {
707 mDestructionStarted = true;
708}
709
710void CameraService::BasicClient::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800711 mCameraService->removeClientByRemote(mRemoteBinder);
Igor Murashkin634a5152013-02-20 17:15:11 -0800712}
713
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800714status_t CameraService::BasicClient::startCameraOps() {
715 int32_t res;
716
717 mOpsCallback = new OpsCallback(this);
718
Igor Murashkine6800ce2013-03-04 17:25:57 -0800719 {
720 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
721 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
722 }
723
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800724 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
725 mClientPackageName, mOpsCallback);
726 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
727 mClientUid, mClientPackageName);
728
729 if (res != AppOpsManager::MODE_ALLOWED) {
730 ALOGI("Camera %d: Access for \"%s\" has been revoked",
731 mCameraId, String8(mClientPackageName).string());
732 return PERMISSION_DENIED;
733 }
734 mOpsActive = true;
735 return OK;
736}
737
738status_t CameraService::BasicClient::finishCameraOps() {
739 if (mOpsActive) {
740 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
741 mClientPackageName);
742 mOpsActive = false;
743 }
744 mAppOpsManager.stopWatchingMode(mOpsCallback);
745 mOpsCallback.clear();
746
747 return OK;
748}
749
750void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
751 String8 name(packageName);
752 String8 myName(mClientPackageName);
753
754 if (op != AppOpsManager::OP_CAMERA) {
755 ALOGW("Unexpected app ops notification received: %d", op);
756 return;
757 }
758
759 int32_t res;
760 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
761 mClientUid, mClientPackageName);
762 ALOGV("checkOp returns: %d, %s ", res,
763 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
764 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
765 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
766 "UNKNOWN");
767
768 if (res != AppOpsManager::MODE_ALLOWED) {
769 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
770 myName.string());
771 // Reset the client PID to allow server-initiated disconnect,
772 // and to prevent further calls by client.
773 mClientPid = getCallingPid();
774 notifyError();
775 disconnect();
776 }
777}
778
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779// ----------------------------------------------------------------------------
780
Keun young Parkd8973a72012-03-28 14:13:09 -0700781Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
782 return gCameraService->getClientLockById((int) user);
783}
784
785// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
786// be acquired for this to be safe
787CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
788 Client* client = gCameraService->getClientByIdUnsafe((int) user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700789
790 // This could happen if the Client is in the process of shutting down (the
791 // last strong reference is gone, but the destructor hasn't finished
792 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -0700793 if (client == NULL) return NULL;
794
795 // destruction already started, so should not be accessed
796 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700797
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798 return client;
799}
800
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800801void CameraService::Client::notifyError() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800802 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800803}
804
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700805// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700806void CameraService::Client::disconnect() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800807 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700808 mCameraService->setCameraFree(mCameraId);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800809 mCameraService->updateStatus(ICameraServiceListener::STATUS_AVAILABLE,
810 mCameraId);
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800811}
812
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800813CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
814 mClient(client) {
815}
816
817void CameraService::Client::OpsCallback::opChanged(int32_t op,
818 const String16& packageName) {
819 sp<BasicClient> client = mClient.promote();
820 if (client != NULL) {
821 client->opChanged(op, packageName);
822 }
823}
824
Mathias Agopian65ab4712010-07-14 17:59:35 -0700825// ----------------------------------------------------------------------------
Igor Murashkin634a5152013-02-20 17:15:11 -0800826// IProCamera
827// ----------------------------------------------------------------------------
828
829CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800830 const sp<IProCameraCallbacks>& remoteCallback,
831 const String16& clientPackageName,
832 int cameraId,
833 int cameraFacing,
834 int clientPid,
835 uid_t clientUid,
836 int servicePid)
837 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
838 clientPackageName, cameraId, cameraFacing,
839 clientPid, clientUid, servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800840{
841 mRemoteCallback = remoteCallback;
842}
843
844CameraService::ProClient::~ProClient() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800845}
846
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800847void CameraService::ProClient::notifyError() {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800848 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800849}
850
Igor Murashkin634a5152013-02-20 17:15:11 -0800851// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852
853static const int kDumpLockRetries = 50;
854static const int kDumpLockSleep = 60000;
855
856static bool tryLock(Mutex& mutex)
857{
858 bool locked = false;
859 for (int i = 0; i < kDumpLockRetries; ++i) {
860 if (mutex.tryLock() == NO_ERROR) {
861 locked = true;
862 break;
863 }
864 usleep(kDumpLockSleep);
865 }
866 return locked;
867}
868
869status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700870 String8 result;
871 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700872 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873 "can't dump CameraService from pid=%d, uid=%d\n",
874 getCallingPid(),
875 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 write(fd, result.string(), result.size());
877 } else {
878 bool locked = tryLock(mServiceLock);
879 // failed to lock - CameraService is probably deadlocked
880 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700881 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700882 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700883 }
884
885 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700886 if (!mModule) {
887 result = String8::format("No camera module available!\n");
888 write(fd, result.string(), result.size());
889 return NO_ERROR;
890 }
891
892 result = String8::format("Camera module HAL API version: 0x%x\n",
893 mModule->common.hal_api_version);
894 result.appendFormat("Camera module API version: 0x%x\n",
895 mModule->common.module_api_version);
896 result.appendFormat("Camera module name: %s\n",
897 mModule->common.name);
898 result.appendFormat("Camera module author: %s\n",
899 mModule->common.author);
900 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
901 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700902 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700903 result = String8::format("Camera %d static information:\n", i);
904 camera_info info;
905
906 status_t rc = mModule->get_camera_info(i, &info);
907 if (rc != OK) {
908 result.appendFormat(" Error reading static information!\n");
909 write(fd, result.string(), result.size());
910 } else {
911 result.appendFormat(" Facing: %s\n",
912 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
913 result.appendFormat(" Orientation: %d\n", info.orientation);
914 int deviceVersion;
915 if (mModule->common.module_api_version <
916 CAMERA_MODULE_API_VERSION_2_0) {
917 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
918 } else {
919 deviceVersion = info.device_version;
920 }
921 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
922 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
923 result.appendFormat(" Device static metadata:\n");
924 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700925 dump_indented_camera_metadata(info.static_camera_characteristics,
926 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700927 } else {
928 write(fd, result.string(), result.size());
929 }
930 }
931
Mathias Agopian65ab4712010-07-14 17:59:35 -0700932 sp<Client> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700933 if (client == 0) {
934 result = String8::format(" Device is closed, no client instance\n");
935 write(fd, result.string(), result.size());
936 continue;
937 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700938 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700939 result = String8::format(" Device is open. Client instance dump:\n");
940 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700941 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700942 }
943 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700944 result = String8::format("\nNo active camera clients yet.\n");
945 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700946 }
947
948 if (locked) mServiceLock.unlock();
949
950 // change logging level
951 int n = args.size();
952 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700953 String16 verboseOption("-v");
954 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700955 String8 levelStr(args[i+1]);
956 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700957 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700959 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700960 }
961 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700962
Mathias Agopian65ab4712010-07-14 17:59:35 -0700963 }
964 return NO_ERROR;
965}
966
Igor Murashkinecf17e82012-10-02 16:05:11 -0700967/*virtual*/void CameraService::binderDied(
968 const wp<IBinder> &who) {
969
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700970 /**
971 * While tempting to promote the wp<IBinder> into a sp,
972 * it's actually not supported by the binder driver
973 */
974
Igor Murashkinecf17e82012-10-02 16:05:11 -0700975 ALOGV("java clients' binder died");
976
Igor Murashkin634a5152013-02-20 17:15:11 -0800977 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700978
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700979 if (cameraClient == 0) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700980 ALOGV("java clients' binder death already cleaned up (normal case)");
981 return;
982 }
983
Igor Murashkinecf17e82012-10-02 16:05:11 -0700984 ALOGW("Disconnecting camera client %p since the binder for it "
985 "died (this pid %d)", cameraClient.get(), getCallingPid());
986
987 cameraClient->disconnect();
988
989}
990
Igor Murashkinbfc99152013-02-27 12:55:20 -0800991void CameraService::updateStatus(ICameraServiceListener::Status status,
992 int32_t cameraId) {
993 // do not lock mServiceLock here or can get into a deadlock from
994 // connect() -> ProClient::disconnect -> updateStatus
995 Mutex::Autolock lock(mStatusMutex);
996 updateStatusUnsafe(status, cameraId);
997}
998
999void CameraService::updateStatusUnsafe(ICameraServiceListener::Status status,
1000 int32_t cameraId) {
1001
1002 ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1003
1004 mStatusList[cameraId] = status;
1005
1006 if (oldStatus != status) {
1007 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1008 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1009
1010 /**
1011 * ProClients lose their exclusive lock.
1012 * - Done before the CameraClient can initialize the HAL device,
1013 * since we want to be able to close it before they get to initialize
1014 */
1015 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1016 Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1017 Vector<wp<ProClient> >::const_iterator it;
1018
1019 for (it = proClients.begin(); it != proClients.end(); ++it) {
1020 sp<ProClient> proCl = it->promote();
1021 if (proCl.get() != NULL) {
1022 proCl->onExclusiveLockStolen();
1023 }
1024 }
1025 }
1026
1027 Vector<sp<ICameraServiceListener> >::const_iterator it;
1028 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1029 (*it)->onStatusChanged(status, cameraId);
1030 }
1031 }
1032}
1033
Mathias Agopian65ab4712010-07-14 17:59:35 -07001034}; // namespace android