blob: 6fbd6edb6a8e234eabd704e5408b6ca6452e623e [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
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27#include <binder/MemoryBase.h>
28#include <binder/MemoryHeapBase.h>
29#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070030#include <cutils/properties.h>
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080031#include <gui/SurfaceTextureClient.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"
Mathias Agopian65ab4712010-07-14 17:59:35 -070043
44namespace android {
45
46// ----------------------------------------------------------------------------
47// Logging support -- this is for debugging only
48// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070049volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
Steve Blockb8a80522011-12-20 16:23:08 +000051#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
52#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
54static void setLogLevel(int level) {
55 android_atomic_write(level, &gLogLevel);
56}
57
58// ----------------------------------------------------------------------------
59
60static int getCallingPid() {
61 return IPCThreadState::self()->getCallingPid();
62}
63
64static int getCallingUid() {
65 return IPCThreadState::self()->getCallingUid();
66}
67
68// ----------------------------------------------------------------------------
69
70// This is ugly and only safe if we never re-create the CameraService, but
71// should be ok for now.
72static CameraService *gCameraService;
73
74CameraService::CameraService()
Iliyan Malchev8951a972011-04-14 16:55:59 -070075:mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070076{
Steve Blockdf64d152012-01-04 20:05:49 +000077 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070078 gCameraService = this;
79}
80
Iliyan Malchev8951a972011-04-14 16:55:59 -070081void CameraService::onFirstRef()
82{
83 BnCameraService::onFirstRef();
84
85 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
86 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +000087 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -070088 mNumberOfCameras = 0;
89 }
90 else {
91 mNumberOfCameras = mModule->get_number_of_cameras();
92 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +000093 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -070094 mNumberOfCameras, MAX_CAMERAS);
95 mNumberOfCameras = MAX_CAMERAS;
96 }
97 for (int i = 0; i < mNumberOfCameras; i++) {
98 setCameraFree(i);
99 }
100 }
101}
102
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103CameraService::~CameraService() {
104 for (int i = 0; i < mNumberOfCameras; i++) {
105 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000106 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107 }
108 }
109
110 gCameraService = NULL;
111}
112
113int32_t CameraService::getNumberOfCameras() {
114 return mNumberOfCameras;
115}
116
117status_t CameraService::getCameraInfo(int cameraId,
118 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700119 if (!mModule) {
120 return NO_INIT;
121 }
122
Mathias Agopian65ab4712010-07-14 17:59:35 -0700123 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
124 return BAD_VALUE;
125 }
126
Iliyan Malchev8951a972011-04-14 16:55:59 -0700127 struct camera_info info;
128 status_t rc = mModule->get_camera_info(cameraId, &info);
129 cameraInfo->facing = info.facing;
130 cameraInfo->orientation = info.orientation;
131 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132}
133
134sp<ICamera> CameraService::connect(
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800135 const sp<ICameraClient>& cameraClient, int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500137
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
139
Iliyan Malchev8951a972011-04-14 16:55:59 -0700140 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000141 ALOGE("Camera HAL module not loaded");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700142 return NULL;
143 }
144
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145 sp<Client> client;
146 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000147 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148 callingPid, cameraId);
149 return NULL;
150 }
151
Wu-cheng Lia3355432011-05-20 14:54:25 +0800152 char value[PROPERTY_VALUE_MAX];
153 property_get("sys.secpolicy.camera.disabled", value, "0");
154 if (strcmp(value, "1") == 0) {
155 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000156 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800157 return NULL;
158 }
159
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800160 Mutex::Autolock lock(mServiceLock);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800161 if (mClient[cameraId] != 0) {
162 client = mClient[cameraId].promote();
163 if (client != 0) {
164 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
165 LOG1("CameraService::connect X (pid %d) (the same client)",
166 callingPid);
167 return client;
168 } else {
169 ALOGW("CameraService::connect X (pid %d) rejected (existing client).",
170 callingPid);
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800171 return NULL;
172 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800173 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800174 mClient[cameraId].clear();
175 }
176
177 if (mBusy[cameraId]) {
178 ALOGW("CameraService::connect X (pid %d) rejected"
179 " (camera %d is still busy).", callingPid, cameraId);
180 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700181 }
182
Iliyan Malchev8951a972011-04-14 16:55:59 -0700183 struct camera_info info;
184 if (mModule->get_camera_info(cameraId, &info) != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000185 ALOGE("Invalid camera id %d", cameraId);
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700186 return NULL;
187 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700188
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700189 int deviceVersion;
190 if (mModule->common.module_api_version == CAMERA_MODULE_API_VERSION_2_0) {
191 deviceVersion = info.device_version;
192 } else {
193 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
194 }
195
196 switch(deviceVersion) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700197 case CAMERA_DEVICE_API_VERSION_1_0:
198 client = new CameraClient(this, cameraClient, cameraId,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700199 info.facing, callingPid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700200 break;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700201 case CAMERA_DEVICE_API_VERSION_2_0:
202 client = new Camera2Client(this, cameraClient, cameraId,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700203 info.facing, callingPid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700204 break;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700205 default:
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700206 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500207 return NULL;
208 }
209
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700210 if (client->initialize(mModule) != OK) {
211 return NULL;
212 }
213
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700214 cameraClient->asBinder()->linkToDeath(this);
215
Mathias Agopian65ab4712010-07-14 17:59:35 -0700216 mClient[cameraId] = client;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700217 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700218 return client;
219}
220
221void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
222 int callingPid = getCallingPid();
223 LOG1("CameraService::removeClient E (pid %d)", callingPid);
224
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700225 // Declare this before the lock to make absolutely sure the
226 // destructor won't be called with the lock held.
227 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700228
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700229 int outIndex;
Igor Murashkin507994d2012-10-05 10:44:57 -0700230 sp<Client> client = findClientUnsafe(cameraClient->asBinder(), outIndex);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700231
232 if (client != 0) {
233 // Found our camera, clear and leave.
234 LOG1("removeClient: clear camera %d", outIndex);
235 mClient[outIndex].clear();
236
237 client->unlinkToDeath(this);
238 }
239
240 LOG1("CameraService::removeClient X (pid %d)", callingPid);
241}
242
243sp<CameraService::Client> CameraService::findClientUnsafe(
Igor Murashkin507994d2012-10-05 10:44:57 -0700244 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700245 sp<Client> client;
246
247 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700248
249 // This happens when we have already disconnected (or this is
250 // just another unused camera).
251 if (mClient[i] == 0) continue;
252
253 // Promote mClient. It can fail if we are called from this path:
254 // Client::~Client() -> disconnect() -> removeClient().
255 client = mClient[i].promote();
256
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700257 // Clean up stale client entry
258 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700259 mClient[i].clear();
260 continue;
261 }
262
Igor Murashkin507994d2012-10-05 10:44:57 -0700263 if (cameraClient == client->getCameraClient()->asBinder()) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700264 // Found our camera
265 outIndex = i;
266 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267 }
268 }
269
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700270 outIndex = -1;
271 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272}
273
Keun young Parkd8973a72012-03-28 14:13:09 -0700274CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700275 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700276 return mClient[cameraId].unsafe_get();
277}
278
279Mutex* CameraService::getClientLockById(int cameraId) {
280 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
281 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282}
283
Igor Murashkin507994d2012-10-05 10:44:57 -0700284sp<CameraService::Client> CameraService::getClientByRemote(
285 const wp<IBinder>& cameraClient) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700286
287 // Declare this before the lock to make absolutely sure the
288 // destructor won't be called with the lock held.
289 sp<Client> client;
290
291 Mutex::Autolock lock(mServiceLock);
292
293 int outIndex;
294 client = findClientUnsafe(cameraClient, outIndex);
295
296 return client;
297}
298
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299status_t CameraService::onTransact(
300 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
301 // Permission checks
302 switch (code) {
303 case BnCameraService::CONNECT:
304 const int pid = getCallingPid();
305 const int self_pid = getpid();
306 if (pid != self_pid) {
307 // we're called from a different process, do the real check
308 if (!checkCallingPermission(
309 String16("android.permission.CAMERA"))) {
310 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000311 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 "can't use the camera pid=%d, uid=%d", pid, uid);
313 return PERMISSION_DENIED;
314 }
315 }
316 break;
317 }
318
319 return BnCameraService::onTransact(code, data, reply, flags);
320}
321
322// The reason we need this busy bit is a new CameraService::connect() request
323// may come in while the previous Client's destructor has not been run or is
324// still running. If the last strong reference of the previous Client is gone
325// but the destructor has not been finished, we should not allow the new Client
326// to be created because we need to wait for the previous Client to tear down
327// the hardware first.
328void CameraService::setCameraBusy(int cameraId) {
329 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700330
331 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700332}
333
334void CameraService::setCameraFree(int cameraId) {
335 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700336
337 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338}
339
340// We share the media players for shutter and recording sound for all clients.
341// A reference count is kept to determine when we will actually release the
342// media players.
343
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800344MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 MediaPlayer* mp = new MediaPlayer();
346 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800347 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700348 mp->prepare();
349 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000350 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 return NULL;
352 }
353 return mp;
354}
355
356void CameraService::loadSound() {
357 Mutex::Autolock lock(mSoundLock);
358 LOG1("CameraService::loadSound ref=%d", mSoundRef);
359 if (mSoundRef++) return;
360
361 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
362 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
363}
364
365void CameraService::releaseSound() {
366 Mutex::Autolock lock(mSoundLock);
367 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
368 if (--mSoundRef) return;
369
370 for (int i = 0; i < NUM_SOUNDS; i++) {
371 if (mSoundPlayer[i] != 0) {
372 mSoundPlayer[i]->disconnect();
373 mSoundPlayer[i].clear();
374 }
375 }
376}
377
378void CameraService::playSound(sound_kind kind) {
379 LOG1("playSound(%d)", kind);
380 Mutex::Autolock lock(mSoundLock);
381 sp<MediaPlayer> player = mSoundPlayer[kind];
382 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800383 player->seekTo(0);
384 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385 }
386}
387
388// ----------------------------------------------------------------------------
389
390CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700391 const sp<ICameraClient>& cameraClient,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700392 int cameraId, int cameraFacing, int clientPid, int servicePid) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800394 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700395
396 mCameraService = cameraService;
397 mCameraClient = cameraClient;
398 mCameraId = cameraId;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800399 mCameraFacing = cameraFacing;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400 mClientPid = clientPid;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700401 mServicePid = servicePid;
Keun young Parkd8973a72012-03-28 14:13:09 -0700402 mDestructionStarted = false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 cameraService->setCameraBusy(cameraId);
405 cameraService->loadSound();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800406 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700407}
408
Mathias Agopian65ab4712010-07-14 17:59:35 -0700409// tear down the client
410CameraService::Client::~Client() {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700411 mCameraService->releaseSound();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412}
413
414// ----------------------------------------------------------------------------
415
Keun young Parkd8973a72012-03-28 14:13:09 -0700416Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
417 return gCameraService->getClientLockById((int) user);
418}
419
420// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
421// be acquired for this to be safe
422CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
423 Client* client = gCameraService->getClientByIdUnsafe((int) user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424
425 // This could happen if the Client is in the process of shutting down (the
426 // last strong reference is gone, but the destructor hasn't finished
427 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -0700428 if (client == NULL) return NULL;
429
430 // destruction already started, so should not be accessed
431 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700432
Mathias Agopian65ab4712010-07-14 17:59:35 -0700433 return client;
434}
435
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700436void CameraService::Client::disconnect() {
437 mCameraService->removeClient(mCameraClient);
438 mCameraService->setCameraFree(mCameraId);
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800439}
440
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441// ----------------------------------------------------------------------------
442
443static const int kDumpLockRetries = 50;
444static const int kDumpLockSleep = 60000;
445
446static bool tryLock(Mutex& mutex)
447{
448 bool locked = false;
449 for (int i = 0; i < kDumpLockRetries; ++i) {
450 if (mutex.tryLock() == NO_ERROR) {
451 locked = true;
452 break;
453 }
454 usleep(kDumpLockSleep);
455 }
456 return locked;
457}
458
459status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 String8 result;
461 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700462 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463 "can't dump CameraService from pid=%d, uid=%d\n",
464 getCallingPid(),
465 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700466 write(fd, result.string(), result.size());
467 } else {
468 bool locked = tryLock(mServiceLock);
469 // failed to lock - CameraService is probably deadlocked
470 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700471 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700472 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700473 }
474
475 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700476 if (!mModule) {
477 result = String8::format("No camera module available!\n");
478 write(fd, result.string(), result.size());
479 return NO_ERROR;
480 }
481
482 result = String8::format("Camera module HAL API version: 0x%x\n",
483 mModule->common.hal_api_version);
484 result.appendFormat("Camera module API version: 0x%x\n",
485 mModule->common.module_api_version);
486 result.appendFormat("Camera module name: %s\n",
487 mModule->common.name);
488 result.appendFormat("Camera module author: %s\n",
489 mModule->common.author);
490 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
491 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700492 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700493 result = String8::format("Camera %d static information:\n", i);
494 camera_info info;
495
496 status_t rc = mModule->get_camera_info(i, &info);
497 if (rc != OK) {
498 result.appendFormat(" Error reading static information!\n");
499 write(fd, result.string(), result.size());
500 } else {
501 result.appendFormat(" Facing: %s\n",
502 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
503 result.appendFormat(" Orientation: %d\n", info.orientation);
504 int deviceVersion;
505 if (mModule->common.module_api_version <
506 CAMERA_MODULE_API_VERSION_2_0) {
507 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
508 } else {
509 deviceVersion = info.device_version;
510 }
511 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
512 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
513 result.appendFormat(" Device static metadata:\n");
514 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700515 dump_indented_camera_metadata(info.static_camera_characteristics,
516 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700517 } else {
518 write(fd, result.string(), result.size());
519 }
520 }
521
Mathias Agopian65ab4712010-07-14 17:59:35 -0700522 sp<Client> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700523 if (client == 0) {
524 result = String8::format(" Device is closed, no client instance\n");
525 write(fd, result.string(), result.size());
526 continue;
527 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700528 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700529 result = String8::format(" Device is open. Client instance dump:\n");
530 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700531 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700532 }
533 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700534 result = String8::format("\nNo active camera clients yet.\n");
535 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536 }
537
538 if (locked) mServiceLock.unlock();
539
540 // change logging level
541 int n = args.size();
542 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700543 String16 verboseOption("-v");
544 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700545 String8 levelStr(args[i+1]);
546 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700547 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700549 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700550 }
551 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700552
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 }
554 return NO_ERROR;
555}
556
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700557/*virtual*/void CameraService::binderDied(
558 const wp<IBinder> &who) {
559
Igor Murashkin507994d2012-10-05 10:44:57 -0700560 /**
561 * While tempting to promote the wp<IBinder> into a sp,
562 * it's actually not supported by the binder driver
563 */
564
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700565 ALOGV("java clients' binder died");
566
Igor Murashkin507994d2012-10-05 10:44:57 -0700567 sp<Client> cameraClient = getClientByRemote(who);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700568
Igor Murashkin507994d2012-10-05 10:44:57 -0700569 if (cameraClient == 0) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700570 ALOGV("java clients' binder death already cleaned up (normal case)");
571 return;
572 }
573
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700574 ALOGW("Disconnecting camera client %p since the binder for it "
575 "died (this pid %d)", cameraClient.get(), getCallingPid());
576
577 cameraClient->disconnect();
578
579}
580
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581}; // namespace android