blob: 8c4f619eb4e42a3448740e16effc9542f1e9bb7f [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 Talvala48af7e82013-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 Murashkin69e22432013-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 Talvala48af7e82013-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 Murashkin8fdfbe22013-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 Murashkinbfb5d5e2013-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 Ray53c8ad32013-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 Murashkinbfb5d5e2013-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 Murashkin8fdfbe22013-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 Talvala48af7e82013-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 Talvala48af7e82013-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) {
226 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
227 LOG1("CameraService::connect X (pid %d) (the same client)",
228 callingPid);
229 return client;
230 } else {
Igor Murashkine4e5b2f2013-02-20 16:50:13 -0800231 // TODOSC: need to support 1 regular client, multiple shared clients here
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800232 ALOGW("CameraService::connect X (pid %d) rejected (existing client).",
233 callingPid);
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800234 return NULL;
235 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800236 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800237 mClient[cameraId].clear();
238 }
239
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800240 /*
241 mBusy is set to false as the last step of the Client destructor,
242 after which it is guaranteed that the Client destructor has finished (
243 including any inherited destructors)
244
245 We only need this for a Client subclasses since we don't allow
246 multiple Clents to be opened concurrently, but multiple BasicClient
247 would be fine
248 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800249 if (mBusy[cameraId]) {
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800250
251 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
252 " (camera %d is still busy).", callingPid,
253 clientName8.string(), cameraId);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800254 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700255 }
256
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800257 int facing = -1;
258 int deviceVersion = getDeviceVersion(cameraId, &facing);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700259
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800260 if (isValidCameraId(cameraId)) {
261 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, cameraId);
262 }
263
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700264 switch(deviceVersion) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700265 case CAMERA_DEVICE_API_VERSION_1_0:
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800266 client = new CameraClient(this, cameraClient,
267 clientPackageName, cameraId,
268 facing, callingPid, clientUid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700269 break;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700270 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800271 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800272 case CAMERA_DEVICE_API_VERSION_3_0:
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800273 client = new Camera2Client(this, cameraClient,
274 clientPackageName, cameraId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800275 facing, callingPid, clientUid, getpid(),
276 deviceVersion);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700277 break;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800278 case -1:
279 ALOGE("Invalid camera id %d", cameraId);
280 return NULL;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700281 default:
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700282 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500283 return NULL;
284 }
285
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700286 if (client->initialize(mModule) != OK) {
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800287 // this is probably not recoverable.. but maybe the client can try again
288 updateStatus(ICameraServiceListener::STATUS_AVAILABLE, cameraId);
289
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700290 return NULL;
291 }
292
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700293 cameraClient->asBinder()->linkToDeath(this);
294
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 mClient[cameraId] = client;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700296 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid());
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800297
Mathias Agopian65ab4712010-07-14 17:59:35 -0700298 return client;
299}
300
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800301sp<IProCameraUser> CameraService::connect(
302 const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinb84d9352013-02-26 14:32:34 -0800303 int cameraId,
304 const String16& clientPackageName,
305 int clientUid)
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800306{
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800307 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700308 int callingPid = getCallingPid();
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800309
Igor Murashkinb84d9352013-02-26 14:32:34 -0800310 // TODO: use clientPackageName and clientUid with appOpsMangr
311
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800312 LOG1("CameraService::connectPro E (pid %d, id %d)", callingPid, cameraId);
313
314 if (!mModule) {
315 ALOGE("Camera HAL module not loaded");
316 return NULL;
317 }
318
319 sp<ProClient> client;
320 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
321 ALOGE("CameraService::connectPro X (pid %d) rejected (invalid cameraId %d).",
322 callingPid, cameraId);
323 return NULL;
324 }
325
326 char value[PROPERTY_VALUE_MAX];
327 property_get("sys.secpolicy.camera.disabled", value, "0");
328 if (strcmp(value, "1") == 0) {
329 // Camera is disabled by DevicePolicyManager.
330 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
331 return NULL;
332 }
333
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800334 // TODO: allow concurrent connections with a ProCamera
335 if (mBusy[cameraId]) {
336
337 ALOGW("CameraService::connectPro X (pid %d, \"%s\") rejected"
338 " (camera %d is still busy).", callingPid,
339 clientName8.string(), cameraId);
340 return NULL;
341 }
342
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800343 int facing = -1;
344 int deviceVersion = getDeviceVersion(cameraId, &facing);
345
346 switch(deviceVersion) {
347 case CAMERA_DEVICE_API_VERSION_1_0:
348 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", cameraId);
349 return NULL;
350 break;
351 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkin69e22432013-02-20 18:24:43 -0800352 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800353 client = new ProCamera2Client(this, cameraCb, String16(),
354 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800355 break;
356 case -1:
357 ALOGE("Invalid camera id %d", cameraId);
358 return NULL;
359 default:
360 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
361 return NULL;
362 }
363
364 if (client->initialize(mModule) != OK) {
365 return NULL;
366 }
367
368 mProClientList[cameraId].push(client);
369
370 cameraCb->asBinder()->linkToDeath(this);
371
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800372 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
373 getpid());
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800374 return client;
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800375}
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800376
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800377status_t CameraService::addListener(
378 const sp<ICameraServiceListener>& listener) {
379 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800380
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800381 Mutex::Autolock lock(mServiceLock);
382
383 Vector<sp<ICameraServiceListener> >::iterator it, end;
384 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
385 if ((*it)->asBinder() == listener->asBinder()) {
386 ALOGW("%s: Tried to add listener %p which was already subscribed",
387 __FUNCTION__, listener.get());
388 return ALREADY_EXISTS;
389 }
390 }
391
392 mListenerList.push_back(listener);
393
394 return OK;
395}
396status_t CameraService::removeListener(
397 const sp<ICameraServiceListener>& listener) {
398 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
399
400 Mutex::Autolock lock(mServiceLock);
401
402 Vector<sp<ICameraServiceListener> >::iterator it;
403 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
404 if ((*it)->asBinder() == listener->asBinder()) {
405 mListenerList.erase(it);
406 return OK;
407 }
408 }
409
410 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
411 __FUNCTION__, listener.get());
412
413 return BAD_VALUE;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800414}
415
416void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
417 int callingPid = getCallingPid();
418 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700420 // Declare this before the lock to make absolutely sure the
421 // destructor won't be called with the lock held.
422 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700423
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700424 int outIndex;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800425 sp<Client> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700426
427 if (client != 0) {
428 // Found our camera, clear and leave.
429 LOG1("removeClient: clear camera %d", outIndex);
430 mClient[outIndex].clear();
431
432 client->unlinkToDeath(this);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800433 } else {
434
435 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
436
437 if (clientPro != NULL) {
438 // Found our camera, clear and leave.
439 LOG1("removeClient: clear pro %p", clientPro.get());
440
441 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
442 }
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700443 }
444
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800445 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
446}
447
448sp<CameraService::ProClient> CameraService::findProClientUnsafe(
449 const wp<IBinder>& cameraCallbacksRemote)
450{
451 sp<ProClient> clientPro;
452
453 for (int i = 0; i < mNumberOfCameras; ++i) {
454 Vector<size_t> removeIdx;
455
456 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
457 wp<ProClient> cl = mProClientList[i][j];
458
459 sp<ProClient> clStrong = cl.promote();
460 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
461 clientPro = clStrong;
462 break;
463 } else if (clStrong == NULL) {
464 // mark to clean up dead ptr
465 removeIdx.push(j);
466 }
467 }
468
469 // remove stale ptrs (in reverse so the indices dont change)
470 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
471 mProClientList[i].removeAt(removeIdx[j]);
472 }
473
474 }
475
476 return clientPro;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700477}
478
479sp<CameraService::Client> CameraService::findClientUnsafe(
Igor Murashkin507994d2012-10-05 10:44:57 -0700480 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700481 sp<Client> client;
482
483 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700484
485 // This happens when we have already disconnected (or this is
486 // just another unused camera).
487 if (mClient[i] == 0) continue;
488
489 // Promote mClient. It can fail if we are called from this path:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800490 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -0700491 client = mClient[i].promote();
492
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700493 // Clean up stale client entry
494 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700495 mClient[i].clear();
496 continue;
497 }
498
Igor Murashkin507994d2012-10-05 10:44:57 -0700499 if (cameraClient == client->getCameraClient()->asBinder()) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700500 // Found our camera
501 outIndex = i;
502 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700503 }
504 }
505
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700506 outIndex = -1;
507 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700508}
509
Keun young Parkd8973a72012-03-28 14:13:09 -0700510CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700511 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700512 return mClient[cameraId].unsafe_get();
513}
514
515Mutex* CameraService::getClientLockById(int cameraId) {
516 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
517 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700518}
519
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800520sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin507994d2012-10-05 10:44:57 -0700521 const wp<IBinder>& cameraClient) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700522
523 // Declare this before the lock to make absolutely sure the
524 // destructor won't be called with the lock held.
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800525 sp<BasicClient> client;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700526
527 Mutex::Autolock lock(mServiceLock);
528
529 int outIndex;
530 client = findClientUnsafe(cameraClient, outIndex);
531
532 return client;
533}
534
Mathias Agopian65ab4712010-07-14 17:59:35 -0700535status_t CameraService::onTransact(
536 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
537 // Permission checks
538 switch (code) {
539 case BnCameraService::CONNECT:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800540 case BnCameraService::CONNECT_PRO:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 const int pid = getCallingPid();
542 const int self_pid = getpid();
543 if (pid != self_pid) {
544 // we're called from a different process, do the real check
545 if (!checkCallingPermission(
546 String16("android.permission.CAMERA"))) {
547 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000548 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700549 "can't use the camera pid=%d, uid=%d", pid, uid);
550 return PERMISSION_DENIED;
551 }
552 }
553 break;
554 }
555
556 return BnCameraService::onTransact(code, data, reply, flags);
557}
558
559// The reason we need this busy bit is a new CameraService::connect() request
560// may come in while the previous Client's destructor has not been run or is
561// still running. If the last strong reference of the previous Client is gone
562// but the destructor has not been finished, we should not allow the new Client
563// to be created because we need to wait for the previous Client to tear down
564// the hardware first.
565void CameraService::setCameraBusy(int cameraId) {
566 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700567
568 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569}
570
571void CameraService::setCameraFree(int cameraId) {
572 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700573
574 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575}
576
577// We share the media players for shutter and recording sound for all clients.
578// A reference count is kept to determine when we will actually release the
579// media players.
580
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800581MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700582 MediaPlayer* mp = new MediaPlayer();
583 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800584 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700585 mp->prepare();
586 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000587 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700588 return NULL;
589 }
590 return mp;
591}
592
593void CameraService::loadSound() {
594 Mutex::Autolock lock(mSoundLock);
595 LOG1("CameraService::loadSound ref=%d", mSoundRef);
596 if (mSoundRef++) return;
597
598 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
599 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
600}
601
602void CameraService::releaseSound() {
603 Mutex::Autolock lock(mSoundLock);
604 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
605 if (--mSoundRef) return;
606
607 for (int i = 0; i < NUM_SOUNDS; i++) {
608 if (mSoundPlayer[i] != 0) {
609 mSoundPlayer[i]->disconnect();
610 mSoundPlayer[i].clear();
611 }
612 }
613}
614
615void CameraService::playSound(sound_kind kind) {
616 LOG1("playSound(%d)", kind);
617 Mutex::Autolock lock(mSoundLock);
618 sp<MediaPlayer> player = mSoundPlayer[kind];
619 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800620 player->seekTo(0);
621 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700622 }
623}
624
625// ----------------------------------------------------------------------------
626
627CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700628 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800629 const String16& clientPackageName,
630 int cameraId, int cameraFacing,
631 int clientPid, uid_t clientUid,
632 int servicePid) :
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800633 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800634 clientPackageName,
635 cameraId, cameraFacing,
636 clientPid, clientUid,
637 servicePid)
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800638{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700639 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800640 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700641
Mathias Agopian65ab4712010-07-14 17:59:35 -0700642 mCameraClient = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700643
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 cameraService->setCameraBusy(cameraId);
645 cameraService->loadSound();
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800646
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800647 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700648}
649
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650// tear down the client
651CameraService::Client::~Client() {
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800652 mDestructionStarted = true;
653
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700654 mCameraService->releaseSound();
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800655 finishCameraOps();
Igor Murashkinbe8d28a2012-10-08 15:09:46 -0700656 // unconditionally disconnect. function is idempotent
657 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700658}
659
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800660CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800661 const sp<IBinder>& remoteCallback,
662 const String16& clientPackageName,
663 int cameraId, int cameraFacing,
664 int clientPid, uid_t clientUid,
665 int servicePid):
666 mClientPackageName(clientPackageName)
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800667{
668 mCameraService = cameraService;
669 mRemoteCallback = remoteCallback;
670 mCameraId = cameraId;
671 mCameraFacing = cameraFacing;
672 mClientPid = clientPid;
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800673 mClientUid = clientUid;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800674 mServicePid = servicePid;
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800675 mOpsActive = false;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800676 mDestructionStarted = false;
677}
678
679CameraService::BasicClient::~BasicClient() {
680 mDestructionStarted = true;
681}
682
683void CameraService::BasicClient::disconnect() {
684 mCameraService->removeClientByRemote(mRemoteCallback);
685}
686
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800687status_t CameraService::BasicClient::startCameraOps() {
688 int32_t res;
689
690 mOpsCallback = new OpsCallback(this);
691
692 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
693 mClientPackageName, mOpsCallback);
694 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
695 mClientUid, mClientPackageName);
696
697 if (res != AppOpsManager::MODE_ALLOWED) {
698 ALOGI("Camera %d: Access for \"%s\" has been revoked",
699 mCameraId, String8(mClientPackageName).string());
700 return PERMISSION_DENIED;
701 }
702 mOpsActive = true;
703 return OK;
704}
705
706status_t CameraService::BasicClient::finishCameraOps() {
707 if (mOpsActive) {
708 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
709 mClientPackageName);
710 mOpsActive = false;
711 }
712 mAppOpsManager.stopWatchingMode(mOpsCallback);
713 mOpsCallback.clear();
714
715 return OK;
716}
717
718void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
719 String8 name(packageName);
720 String8 myName(mClientPackageName);
721
722 if (op != AppOpsManager::OP_CAMERA) {
723 ALOGW("Unexpected app ops notification received: %d", op);
724 return;
725 }
726
727 int32_t res;
728 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
729 mClientUid, mClientPackageName);
730 ALOGV("checkOp returns: %d, %s ", res,
731 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
732 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
733 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
734 "UNKNOWN");
735
736 if (res != AppOpsManager::MODE_ALLOWED) {
737 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
738 myName.string());
739 // Reset the client PID to allow server-initiated disconnect,
740 // and to prevent further calls by client.
741 mClientPid = getCallingPid();
742 notifyError();
743 disconnect();
744 }
745}
746
Mathias Agopian65ab4712010-07-14 17:59:35 -0700747// ----------------------------------------------------------------------------
748
Keun young Parkd8973a72012-03-28 14:13:09 -0700749Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
750 return gCameraService->getClientLockById((int) user);
751}
752
753// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
754// be acquired for this to be safe
755CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
756 Client* client = gCameraService->getClientByIdUnsafe((int) user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700757
758 // This could happen if the Client is in the process of shutting down (the
759 // last strong reference is gone, but the destructor hasn't finished
760 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -0700761 if (client == NULL) return NULL;
762
763 // destruction already started, so should not be accessed
764 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766 return client;
767}
768
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800769void CameraService::Client::notifyError() {
770 mCameraClient->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
771}
772
Igor Murashkinbe8d28a2012-10-08 15:09:46 -0700773// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700774void CameraService::Client::disconnect() {
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800775 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700776 mCameraService->setCameraFree(mCameraId);
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800777 mCameraService->updateStatus(ICameraServiceListener::STATUS_AVAILABLE,
778 mCameraId);
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800779}
780
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800781CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
782 mClient(client) {
783}
784
785void CameraService::Client::OpsCallback::opChanged(int32_t op,
786 const String16& packageName) {
787 sp<BasicClient> client = mClient.promote();
788 if (client != NULL) {
789 client->opChanged(op, packageName);
790 }
791}
792
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793// ----------------------------------------------------------------------------
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800794// IProCamera
795// ----------------------------------------------------------------------------
796
797CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800798 const sp<IProCameraCallbacks>& remoteCallback,
799 const String16& clientPackageName,
800 int cameraId,
801 int cameraFacing,
802 int clientPid,
803 uid_t clientUid,
804 int servicePid)
805 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
806 clientPackageName, cameraId, cameraFacing,
807 clientPid, clientUid, servicePid)
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800808{
809 mRemoteCallback = remoteCallback;
810}
811
812CameraService::ProClient::~ProClient() {
813 mDestructionStarted = true;
814
815 ProClient::disconnect();
816}
817
818status_t CameraService::ProClient::connect(const sp<IProCameraCallbacks>& callbacks) {
819 ALOGE("%s: not implemented yet", __FUNCTION__);
820
821 return INVALID_OPERATION;
822}
823
824void CameraService::ProClient::disconnect() {
825 BasicClient::disconnect();
826}
827
828status_t CameraService::ProClient::initialize(camera_module_t* module)
829{
830 ALOGW("%s: not implemented yet", __FUNCTION__);
831 return OK;
832}
833
834status_t CameraService::ProClient::exclusiveTryLock() {
835 ALOGE("%s: not implemented yet", __FUNCTION__);
836 return INVALID_OPERATION;
837}
838
839status_t CameraService::ProClient::exclusiveLock() {
840 ALOGE("%s: not implemented yet", __FUNCTION__);
841 return INVALID_OPERATION;
842}
843
844status_t CameraService::ProClient::exclusiveUnlock() {
845 ALOGE("%s: not implemented yet", __FUNCTION__);
846 return INVALID_OPERATION;
847}
848
849bool CameraService::ProClient::hasExclusiveLock() {
850 ALOGE("%s: not implemented yet", __FUNCTION__);
851 return false;
852}
853
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800854void CameraService::ProClient::onExclusiveLockStolen() {
855 ALOGE("%s: not implemented yet", __FUNCTION__);
856}
857
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800858status_t CameraService::ProClient::submitRequest(camera_metadata_t* request, bool streaming) {
859 ALOGE("%s: not implemented yet", __FUNCTION__);
860
861 free_camera_metadata(request);
862
863 return INVALID_OPERATION;
864}
865
866status_t CameraService::ProClient::cancelRequest(int requestId) {
867 ALOGE("%s: not implemented yet", __FUNCTION__);
868
869 return INVALID_OPERATION;
870}
871
872status_t CameraService::ProClient::requestStream(int streamId) {
873 ALOGE("%s: not implemented yet", __FUNCTION__);
874
875 return INVALID_OPERATION;
876}
877
878status_t CameraService::ProClient::cancelStream(int streamId) {
879 ALOGE("%s: not implemented yet", __FUNCTION__);
880
881 return INVALID_OPERATION;
882}
883
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800884void CameraService::ProClient::notifyError() {
885 ALOGE("%s: not implemented yet", __FUNCTION__);
886}
887
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800888// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700889
890static const int kDumpLockRetries = 50;
891static const int kDumpLockSleep = 60000;
892
893static bool tryLock(Mutex& mutex)
894{
895 bool locked = false;
896 for (int i = 0; i < kDumpLockRetries; ++i) {
897 if (mutex.tryLock() == NO_ERROR) {
898 locked = true;
899 break;
900 }
901 usleep(kDumpLockSleep);
902 }
903 return locked;
904}
905
906status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700907 String8 result;
908 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700909 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 "can't dump CameraService from pid=%d, uid=%d\n",
911 getCallingPid(),
912 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913 write(fd, result.string(), result.size());
914 } else {
915 bool locked = tryLock(mServiceLock);
916 // failed to lock - CameraService is probably deadlocked
917 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700918 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700919 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700920 }
921
922 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700923 if (!mModule) {
924 result = String8::format("No camera module available!\n");
925 write(fd, result.string(), result.size());
926 return NO_ERROR;
927 }
928
929 result = String8::format("Camera module HAL API version: 0x%x\n",
930 mModule->common.hal_api_version);
931 result.appendFormat("Camera module API version: 0x%x\n",
932 mModule->common.module_api_version);
933 result.appendFormat("Camera module name: %s\n",
934 mModule->common.name);
935 result.appendFormat("Camera module author: %s\n",
936 mModule->common.author);
937 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
938 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700939 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700940 result = String8::format("Camera %d static information:\n", i);
941 camera_info info;
942
943 status_t rc = mModule->get_camera_info(i, &info);
944 if (rc != OK) {
945 result.appendFormat(" Error reading static information!\n");
946 write(fd, result.string(), result.size());
947 } else {
948 result.appendFormat(" Facing: %s\n",
949 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
950 result.appendFormat(" Orientation: %d\n", info.orientation);
951 int deviceVersion;
952 if (mModule->common.module_api_version <
953 CAMERA_MODULE_API_VERSION_2_0) {
954 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
955 } else {
956 deviceVersion = info.device_version;
957 }
958 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
959 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
960 result.appendFormat(" Device static metadata:\n");
961 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700962 dump_indented_camera_metadata(info.static_camera_characteristics,
963 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700964 } else {
965 write(fd, result.string(), result.size());
966 }
967 }
968
Mathias Agopian65ab4712010-07-14 17:59:35 -0700969 sp<Client> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700970 if (client == 0) {
971 result = String8::format(" Device is closed, no client instance\n");
972 write(fd, result.string(), result.size());
973 continue;
974 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700975 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700976 result = String8::format(" Device is open. Client instance dump:\n");
977 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700978 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700979 }
980 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700981 result = String8::format("\nNo active camera clients yet.\n");
982 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700983 }
984
985 if (locked) mServiceLock.unlock();
986
987 // change logging level
988 int n = args.size();
989 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700990 String16 verboseOption("-v");
991 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992 String8 levelStr(args[i+1]);
993 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700994 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700996 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700997 }
998 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700999
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 }
1001 return NO_ERROR;
1002}
1003
Igor Murashkin8dcdb952012-10-02 16:05:11 -07001004/*virtual*/void CameraService::binderDied(
1005 const wp<IBinder> &who) {
1006
Igor Murashkin507994d2012-10-05 10:44:57 -07001007 /**
1008 * While tempting to promote the wp<IBinder> into a sp,
1009 * it's actually not supported by the binder driver
1010 */
1011
Igor Murashkin8dcdb952012-10-02 16:05:11 -07001012 ALOGV("java clients' binder died");
1013
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -08001014 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkin8dcdb952012-10-02 16:05:11 -07001015
Igor Murashkin507994d2012-10-05 10:44:57 -07001016 if (cameraClient == 0) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -07001017 ALOGV("java clients' binder death already cleaned up (normal case)");
1018 return;
1019 }
1020
Igor Murashkin8dcdb952012-10-02 16:05:11 -07001021 ALOGW("Disconnecting camera client %p since the binder for it "
1022 "died (this pid %d)", cameraClient.get(), getCallingPid());
1023
1024 cameraClient->disconnect();
1025
1026}
1027
Igor Murashkin8fdfbe22013-02-27 12:55:20 -08001028void CameraService::updateStatus(ICameraServiceListener::Status status,
1029 int32_t cameraId) {
1030 // do not lock mServiceLock here or can get into a deadlock from
1031 // connect() -> ProClient::disconnect -> updateStatus
1032 Mutex::Autolock lock(mStatusMutex);
1033 updateStatusUnsafe(status, cameraId);
1034}
1035
1036void CameraService::updateStatusUnsafe(ICameraServiceListener::Status status,
1037 int32_t cameraId) {
1038
1039 ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1040
1041 mStatusList[cameraId] = status;
1042
1043 if (oldStatus != status) {
1044 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1045 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1046
1047 /**
1048 * ProClients lose their exclusive lock.
1049 * - Done before the CameraClient can initialize the HAL device,
1050 * since we want to be able to close it before they get to initialize
1051 */
1052 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1053 Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1054 Vector<wp<ProClient> >::const_iterator it;
1055
1056 for (it = proClients.begin(); it != proClients.end(); ++it) {
1057 sp<ProClient> proCl = it->promote();
1058 if (proCl.get() != NULL) {
1059 proCl->onExclusiveLockStolen();
1060 }
1061 }
1062 }
1063
1064 Vector<sp<ICameraServiceListener> >::const_iterator it;
1065 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1066 (*it)->onStatusChanged(status, cameraId);
1067 }
1068 }
1069}
1070
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071}; // namespace android