blob: 494196535e5145bd6a49fd5b605f846e85ae4e53 [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>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080031#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070032#include <hardware/hardware.h>
33#include <media/AudioSystem.h>
34#include <media/mediaplayer.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070035#include <utils/Errors.h>
36#include <utils/Log.h>
37#include <utils/String16.h>
38
39#include "CameraService.h"
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070040#include "CameraClient.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070041#include "Camera2Client.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
43namespace android {
44
45// ----------------------------------------------------------------------------
46// Logging support -- this is for debugging only
47// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070048volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070049
Steve Blockb8a80522011-12-20 16:23:08 +000050#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
51#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
53static void setLogLevel(int level) {
54 android_atomic_write(level, &gLogLevel);
55}
56
57// ----------------------------------------------------------------------------
58
59static int getCallingPid() {
60 return IPCThreadState::self()->getCallingPid();
61}
62
63static int getCallingUid() {
64 return IPCThreadState::self()->getCallingUid();
65}
66
67// ----------------------------------------------------------------------------
68
69// This is ugly and only safe if we never re-create the CameraService, but
70// should be ok for now.
71static CameraService *gCameraService;
72
73CameraService::CameraService()
Iliyan Malchev8951a972011-04-14 16:55:59 -070074:mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070075{
Steve Blockdf64d152012-01-04 20:05:49 +000076 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070077 gCameraService = this;
78}
79
Iliyan Malchev8951a972011-04-14 16:55:59 -070080void CameraService::onFirstRef()
81{
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080082 LOG1("CameraService::onFirstRef");
83
Iliyan Malchev8951a972011-04-14 16:55:59 -070084 BnCameraService::onFirstRef();
85
86 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
87 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +000088 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -070089 mNumberOfCameras = 0;
90 }
91 else {
Alex Ray53c8ad32013-02-20 13:39:37 -080092 ALOGI("Loaded \"%s\" camera module", mModule->common.name);
Iliyan Malchev8951a972011-04-14 16:55:59 -070093 mNumberOfCameras = mModule->get_number_of_cameras();
94 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +000095 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -070096 mNumberOfCameras, MAX_CAMERAS);
97 mNumberOfCameras = MAX_CAMERAS;
98 }
99 for (int i = 0; i < mNumberOfCameras; i++) {
100 setCameraFree(i);
101 }
102 }
103}
104
Mathias Agopian65ab4712010-07-14 17:59:35 -0700105CameraService::~CameraService() {
106 for (int i = 0; i < mNumberOfCameras; i++) {
107 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000108 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109 }
110 }
111
112 gCameraService = NULL;
113}
114
115int32_t CameraService::getNumberOfCameras() {
116 return mNumberOfCameras;
117}
118
119status_t CameraService::getCameraInfo(int cameraId,
120 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700121 if (!mModule) {
122 return NO_INIT;
123 }
124
Mathias Agopian65ab4712010-07-14 17:59:35 -0700125 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
126 return BAD_VALUE;
127 }
128
Iliyan Malchev8951a972011-04-14 16:55:59 -0700129 struct camera_info info;
130 status_t rc = mModule->get_camera_info(cameraId, &info);
131 cameraInfo->facing = info.facing;
132 cameraInfo->orientation = info.orientation;
133 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700134}
135
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800136int CameraService::getDeviceVersion(int cameraId, int* facing) {
137 struct camera_info info;
138 if (mModule->get_camera_info(cameraId, &info) != OK) {
139 return -1;
140 }
141
142 int deviceVersion;
143 if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
144 deviceVersion = info.device_version;
145 } else {
146 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
147 }
148
149 if (facing) {
150 *facing = info.facing;
151 }
152
153 return deviceVersion;
154}
155
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156sp<ICamera> CameraService::connect(
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800157 const sp<ICameraClient>& cameraClient, int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500159
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
161
Iliyan Malchev8951a972011-04-14 16:55:59 -0700162 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000163 ALOGE("Camera HAL module not loaded");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700164 return NULL;
165 }
166
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167 sp<Client> client;
168 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000169 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 callingPid, cameraId);
171 return NULL;
172 }
173
Wu-cheng Lia3355432011-05-20 14:54:25 +0800174 char value[PROPERTY_VALUE_MAX];
175 property_get("sys.secpolicy.camera.disabled", value, "0");
176 if (strcmp(value, "1") == 0) {
177 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000178 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800179 return NULL;
180 }
181
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800182 Mutex::Autolock lock(mServiceLock);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800183 if (mClient[cameraId] != 0) {
184 client = mClient[cameraId].promote();
185 if (client != 0) {
186 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
187 LOG1("CameraService::connect X (pid %d) (the same client)",
188 callingPid);
189 return client;
190 } else {
Igor Murashkine4e5b2f2013-02-20 16:50:13 -0800191 // TODOSC: need to support 1 regular client, multiple shared clients here
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800192 ALOGW("CameraService::connect X (pid %d) rejected (existing client).",
193 callingPid);
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800194 return NULL;
195 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800196 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800197 mClient[cameraId].clear();
198 }
199
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800200 /*
201 mBusy is set to false as the last step of the Client destructor,
202 after which it is guaranteed that the Client destructor has finished (
203 including any inherited destructors)
204
205 We only need this for a Client subclasses since we don't allow
206 multiple Clents to be opened concurrently, but multiple BasicClient
207 would be fine
208 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800209 if (mBusy[cameraId]) {
210 ALOGW("CameraService::connect X (pid %d) rejected"
211 " (camera %d is still busy).", callingPid, cameraId);
212 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700213 }
214
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800215 int facing = -1;
216 int deviceVersion = getDeviceVersion(cameraId, &facing);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700217
218 switch(deviceVersion) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700219 case CAMERA_DEVICE_API_VERSION_1_0:
220 client = new CameraClient(this, cameraClient, cameraId,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800221 facing, callingPid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700222 break;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700223 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800224 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700225 client = new Camera2Client(this, cameraClient, cameraId,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800226 facing, callingPid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700227 break;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800228 case -1:
229 ALOGE("Invalid camera id %d", cameraId);
230 return NULL;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700231 default:
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700232 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500233 return NULL;
234 }
235
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700236 if (client->initialize(mModule) != OK) {
237 return NULL;
238 }
239
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700240 cameraClient->asBinder()->linkToDeath(this);
241
Mathias Agopian65ab4712010-07-14 17:59:35 -0700242 mClient[cameraId] = client;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700243 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 return client;
245}
246
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800247sp<IProCameraUser> CameraService::connect(
248 const sp<IProCameraCallbacks>& cameraCb,
249 int cameraId)
250{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700251 int callingPid = getCallingPid();
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800252
253 LOG1("CameraService::connectPro E (pid %d, id %d)", callingPid, cameraId);
254
255 if (!mModule) {
256 ALOGE("Camera HAL module not loaded");
257 return NULL;
258 }
259
260 sp<ProClient> client;
261 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
262 ALOGE("CameraService::connectPro X (pid %d) rejected (invalid cameraId %d).",
263 callingPid, cameraId);
264 return NULL;
265 }
266
267 char value[PROPERTY_VALUE_MAX];
268 property_get("sys.secpolicy.camera.disabled", value, "0");
269 if (strcmp(value, "1") == 0) {
270 // Camera is disabled by DevicePolicyManager.
271 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
272 return NULL;
273 }
274
275 int facing = -1;
276 int deviceVersion = getDeviceVersion(cameraId, &facing);
277
278 switch(deviceVersion) {
279 case CAMERA_DEVICE_API_VERSION_1_0:
280 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", cameraId);
281 return NULL;
282 break;
283 case CAMERA_DEVICE_API_VERSION_2_0:
284 client = new ProClient(this, cameraCb, cameraId,
285 facing, callingPid, getpid());
286 break;
287 case -1:
288 ALOGE("Invalid camera id %d", cameraId);
289 return NULL;
290 default:
291 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
292 return NULL;
293 }
294
295 if (client->initialize(mModule) != OK) {
296 return NULL;
297 }
298
299 mProClientList[cameraId].push(client);
300
301 cameraCb->asBinder()->linkToDeath(this);
302
303 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid());
304 return client;
305
306
307 return NULL;
308}
309
310void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
311 int callingPid = getCallingPid();
312 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700314 // Declare this before the lock to make absolutely sure the
315 // destructor won't be called with the lock held.
316 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700317
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700318 int outIndex;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800319 sp<Client> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700320
321 if (client != 0) {
322 // Found our camera, clear and leave.
323 LOG1("removeClient: clear camera %d", outIndex);
324 mClient[outIndex].clear();
325
326 client->unlinkToDeath(this);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800327 } else {
328
329 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
330
331 if (clientPro != NULL) {
332 // Found our camera, clear and leave.
333 LOG1("removeClient: clear pro %p", clientPro.get());
334
335 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
336 }
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700337 }
338
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800339 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
340}
341
342sp<CameraService::ProClient> CameraService::findProClientUnsafe(
343 const wp<IBinder>& cameraCallbacksRemote)
344{
345 sp<ProClient> clientPro;
346
347 for (int i = 0; i < mNumberOfCameras; ++i) {
348 Vector<size_t> removeIdx;
349
350 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
351 wp<ProClient> cl = mProClientList[i][j];
352
353 sp<ProClient> clStrong = cl.promote();
354 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
355 clientPro = clStrong;
356 break;
357 } else if (clStrong == NULL) {
358 // mark to clean up dead ptr
359 removeIdx.push(j);
360 }
361 }
362
363 // remove stale ptrs (in reverse so the indices dont change)
364 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
365 mProClientList[i].removeAt(removeIdx[j]);
366 }
367
368 }
369
370 return clientPro;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700371}
372
373sp<CameraService::Client> CameraService::findClientUnsafe(
Igor Murashkin507994d2012-10-05 10:44:57 -0700374 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700375 sp<Client> client;
376
377 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378
379 // This happens when we have already disconnected (or this is
380 // just another unused camera).
381 if (mClient[i] == 0) continue;
382
383 // Promote mClient. It can fail if we are called from this path:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800384 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -0700385 client = mClient[i].promote();
386
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700387 // Clean up stale client entry
388 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 mClient[i].clear();
390 continue;
391 }
392
Igor Murashkin507994d2012-10-05 10:44:57 -0700393 if (cameraClient == client->getCameraClient()->asBinder()) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700394 // Found our camera
395 outIndex = i;
396 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700397 }
398 }
399
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700400 outIndex = -1;
401 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700402}
403
Keun young Parkd8973a72012-03-28 14:13:09 -0700404CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700405 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700406 return mClient[cameraId].unsafe_get();
407}
408
409Mutex* CameraService::getClientLockById(int cameraId) {
410 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
411 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700412}
413
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800414sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin507994d2012-10-05 10:44:57 -0700415 const wp<IBinder>& cameraClient) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700416
417 // Declare this before the lock to make absolutely sure the
418 // destructor won't be called with the lock held.
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800419 sp<BasicClient> client;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700420
421 Mutex::Autolock lock(mServiceLock);
422
423 int outIndex;
424 client = findClientUnsafe(cameraClient, outIndex);
425
426 return client;
427}
428
Mathias Agopian65ab4712010-07-14 17:59:35 -0700429status_t CameraService::onTransact(
430 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
431 // Permission checks
432 switch (code) {
433 case BnCameraService::CONNECT:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800434 case BnCameraService::CONNECT_PRO:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 const int pid = getCallingPid();
436 const int self_pid = getpid();
437 if (pid != self_pid) {
438 // we're called from a different process, do the real check
439 if (!checkCallingPermission(
440 String16("android.permission.CAMERA"))) {
441 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000442 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700443 "can't use the camera pid=%d, uid=%d", pid, uid);
444 return PERMISSION_DENIED;
445 }
446 }
447 break;
448 }
449
450 return BnCameraService::onTransact(code, data, reply, flags);
451}
452
453// The reason we need this busy bit is a new CameraService::connect() request
454// may come in while the previous Client's destructor has not been run or is
455// still running. If the last strong reference of the previous Client is gone
456// but the destructor has not been finished, we should not allow the new Client
457// to be created because we need to wait for the previous Client to tear down
458// the hardware first.
459void CameraService::setCameraBusy(int cameraId) {
460 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700461
462 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700463}
464
465void CameraService::setCameraFree(int cameraId) {
466 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700467
468 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700469}
470
471// We share the media players for shutter and recording sound for all clients.
472// A reference count is kept to determine when we will actually release the
473// media players.
474
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800475MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700476 MediaPlayer* mp = new MediaPlayer();
477 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800478 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700479 mp->prepare();
480 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000481 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700482 return NULL;
483 }
484 return mp;
485}
486
487void CameraService::loadSound() {
488 Mutex::Autolock lock(mSoundLock);
489 LOG1("CameraService::loadSound ref=%d", mSoundRef);
490 if (mSoundRef++) return;
491
492 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
493 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
494}
495
496void CameraService::releaseSound() {
497 Mutex::Autolock lock(mSoundLock);
498 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
499 if (--mSoundRef) return;
500
501 for (int i = 0; i < NUM_SOUNDS; i++) {
502 if (mSoundPlayer[i] != 0) {
503 mSoundPlayer[i]->disconnect();
504 mSoundPlayer[i].clear();
505 }
506 }
507}
508
509void CameraService::playSound(sound_kind kind) {
510 LOG1("playSound(%d)", kind);
511 Mutex::Autolock lock(mSoundLock);
512 sp<MediaPlayer> player = mSoundPlayer[kind];
513 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800514 player->seekTo(0);
515 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700516 }
517}
518
519// ----------------------------------------------------------------------------
520
521CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700522 const sp<ICameraClient>& cameraClient,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800523 int cameraId, int cameraFacing, int clientPid, int servicePid) :
524 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
525 cameraId, cameraFacing,
526 clientPid, servicePid)
527{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700528 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800529 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700530
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 mCameraClient = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700532
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533 cameraService->setCameraBusy(cameraId);
534 cameraService->loadSound();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800535 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700536}
537
Mathias Agopian65ab4712010-07-14 17:59:35 -0700538// tear down the client
539CameraService::Client::~Client() {
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800540 mDestructionStarted = true;
541
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700542 mCameraService->releaseSound();
Igor Murashkinbe8d28a2012-10-08 15:09:46 -0700543
544 // unconditionally disconnect. function is idempotent
545 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700546}
547
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800548CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
549 const sp<IBinder>& remoteCallback,
550 int cameraId, int cameraFacing,
551 int clientPid, int servicePid)
552{
553 mCameraService = cameraService;
554 mRemoteCallback = remoteCallback;
555 mCameraId = cameraId;
556 mCameraFacing = cameraFacing;
557 mClientPid = clientPid;
558 mServicePid = servicePid;
559
560 mDestructionStarted = false;
561}
562
563CameraService::BasicClient::~BasicClient() {
564 mDestructionStarted = true;
565}
566
567void CameraService::BasicClient::disconnect() {
568 mCameraService->removeClientByRemote(mRemoteCallback);
569}
570
Mathias Agopian65ab4712010-07-14 17:59:35 -0700571// ----------------------------------------------------------------------------
572
Keun young Parkd8973a72012-03-28 14:13:09 -0700573Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
574 return gCameraService->getClientLockById((int) user);
575}
576
577// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
578// be acquired for this to be safe
579CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
580 Client* client = gCameraService->getClientByIdUnsafe((int) user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700581
582 // This could happen if the Client is in the process of shutting down (the
583 // last strong reference is gone, but the destructor hasn't finished
584 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -0700585 if (client == NULL) return NULL;
586
587 // destruction already started, so should not be accessed
588 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589
Mathias Agopian65ab4712010-07-14 17:59:35 -0700590 return client;
591}
592
Igor Murashkinbe8d28a2012-10-08 15:09:46 -0700593// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700594void CameraService::Client::disconnect() {
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800595 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700596 mCameraService->setCameraFree(mCameraId);
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800597}
598
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599// ----------------------------------------------------------------------------
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800600// IProCamera
601// ----------------------------------------------------------------------------
602
603CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
604 const sp<IProCameraCallbacks>& remoteCallback,
605 int cameraId,
606 int cameraFacing,
607 int clientPid,
608 int servicePid)
609 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
610 cameraId, cameraFacing,
611 clientPid, servicePid)
612{
613 mRemoteCallback = remoteCallback;
614}
615
616CameraService::ProClient::~ProClient() {
617 mDestructionStarted = true;
618
619 ProClient::disconnect();
620}
621
622status_t CameraService::ProClient::connect(const sp<IProCameraCallbacks>& callbacks) {
623 ALOGE("%s: not implemented yet", __FUNCTION__);
624
625 return INVALID_OPERATION;
626}
627
628void CameraService::ProClient::disconnect() {
629 BasicClient::disconnect();
630}
631
632status_t CameraService::ProClient::initialize(camera_module_t* module)
633{
634 ALOGW("%s: not implemented yet", __FUNCTION__);
635 return OK;
636}
637
638status_t CameraService::ProClient::exclusiveTryLock() {
639 ALOGE("%s: not implemented yet", __FUNCTION__);
640 return INVALID_OPERATION;
641}
642
643status_t CameraService::ProClient::exclusiveLock() {
644 ALOGE("%s: not implemented yet", __FUNCTION__);
645 return INVALID_OPERATION;
646}
647
648status_t CameraService::ProClient::exclusiveUnlock() {
649 ALOGE("%s: not implemented yet", __FUNCTION__);
650 return INVALID_OPERATION;
651}
652
653bool CameraService::ProClient::hasExclusiveLock() {
654 ALOGE("%s: not implemented yet", __FUNCTION__);
655 return false;
656}
657
658status_t CameraService::ProClient::submitRequest(camera_metadata_t* request, bool streaming) {
659 ALOGE("%s: not implemented yet", __FUNCTION__);
660
661 free_camera_metadata(request);
662
663 return INVALID_OPERATION;
664}
665
666status_t CameraService::ProClient::cancelRequest(int requestId) {
667 ALOGE("%s: not implemented yet", __FUNCTION__);
668
669 return INVALID_OPERATION;
670}
671
672status_t CameraService::ProClient::requestStream(int streamId) {
673 ALOGE("%s: not implemented yet", __FUNCTION__);
674
675 return INVALID_OPERATION;
676}
677
678status_t CameraService::ProClient::cancelStream(int streamId) {
679 ALOGE("%s: not implemented yet", __FUNCTION__);
680
681 return INVALID_OPERATION;
682}
683
684// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685
686static const int kDumpLockRetries = 50;
687static const int kDumpLockSleep = 60000;
688
689static bool tryLock(Mutex& mutex)
690{
691 bool locked = false;
692 for (int i = 0; i < kDumpLockRetries; ++i) {
693 if (mutex.tryLock() == NO_ERROR) {
694 locked = true;
695 break;
696 }
697 usleep(kDumpLockSleep);
698 }
699 return locked;
700}
701
702status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700703 String8 result;
704 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700705 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 "can't dump CameraService from pid=%d, uid=%d\n",
707 getCallingPid(),
708 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700709 write(fd, result.string(), result.size());
710 } else {
711 bool locked = tryLock(mServiceLock);
712 // failed to lock - CameraService is probably deadlocked
713 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700714 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700715 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716 }
717
718 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700719 if (!mModule) {
720 result = String8::format("No camera module available!\n");
721 write(fd, result.string(), result.size());
722 return NO_ERROR;
723 }
724
725 result = String8::format("Camera module HAL API version: 0x%x\n",
726 mModule->common.hal_api_version);
727 result.appendFormat("Camera module API version: 0x%x\n",
728 mModule->common.module_api_version);
729 result.appendFormat("Camera module name: %s\n",
730 mModule->common.name);
731 result.appendFormat("Camera module author: %s\n",
732 mModule->common.author);
733 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
734 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700736 result = String8::format("Camera %d static information:\n", i);
737 camera_info info;
738
739 status_t rc = mModule->get_camera_info(i, &info);
740 if (rc != OK) {
741 result.appendFormat(" Error reading static information!\n");
742 write(fd, result.string(), result.size());
743 } else {
744 result.appendFormat(" Facing: %s\n",
745 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
746 result.appendFormat(" Orientation: %d\n", info.orientation);
747 int deviceVersion;
748 if (mModule->common.module_api_version <
749 CAMERA_MODULE_API_VERSION_2_0) {
750 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
751 } else {
752 deviceVersion = info.device_version;
753 }
754 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
755 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
756 result.appendFormat(" Device static metadata:\n");
757 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700758 dump_indented_camera_metadata(info.static_camera_characteristics,
759 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700760 } else {
761 write(fd, result.string(), result.size());
762 }
763 }
764
Mathias Agopian65ab4712010-07-14 17:59:35 -0700765 sp<Client> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700766 if (client == 0) {
767 result = String8::format(" Device is closed, no client instance\n");
768 write(fd, result.string(), result.size());
769 continue;
770 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700771 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700772 result = String8::format(" Device is open. Client instance dump:\n");
773 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700774 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700775 }
776 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700777 result = String8::format("\nNo active camera clients yet.\n");
778 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700779 }
780
781 if (locked) mServiceLock.unlock();
782
783 // change logging level
784 int n = args.size();
785 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700786 String16 verboseOption("-v");
787 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788 String8 levelStr(args[i+1]);
789 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700790 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700791 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700792 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700793 }
794 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700795
Mathias Agopian65ab4712010-07-14 17:59:35 -0700796 }
797 return NO_ERROR;
798}
799
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700800/*virtual*/void CameraService::binderDied(
801 const wp<IBinder> &who) {
802
Igor Murashkin507994d2012-10-05 10:44:57 -0700803 /**
804 * While tempting to promote the wp<IBinder> into a sp,
805 * it's actually not supported by the binder driver
806 */
807
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700808 ALOGV("java clients' binder died");
809
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800810 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700811
Igor Murashkin507994d2012-10-05 10:44:57 -0700812 if (cameraClient == 0) {
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700813 ALOGV("java clients' binder death already cleaned up (normal case)");
814 return;
815 }
816
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700817 ALOGW("Disconnecting camera client %p since the binder for it "
818 "died (this pid %d)", cameraClient.get(), getCallingPid());
819
820 cameraClient->disconnect();
821
822}
823
Mathias Agopian65ab4712010-07-14 17:59:35 -0700824}; // namespace android