blob: ec1c3f04a3d232b4f5a49c665e00bb5e730986ae [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopian65ab4712010-07-14 17:59:35 -07004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "CameraService"
Iliyan Malchev8951a972011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
21#include <stdio.h>
22#include <sys/types.h>
23#include <pthread.h>
24
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080025#include <binder/AppOpsManager.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026#include <binder/IPCThreadState.h>
27#include <binder/IServiceManager.h>
28#include <binder/MemoryBase.h>
29#include <binder/MemoryHeapBase.h>
30#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070031#include <cutils/properties.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080032#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include <hardware/hardware.h>
34#include <media/AudioSystem.h>
35#include <media/mediaplayer.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070036#include <utils/Errors.h>
37#include <utils/Log.h>
38#include <utils/String16.h>
39
40#include "CameraService.h"
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070041#include "CameraClient.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070042#include "Camera2Client.h"
Igor Murashkin985fd302013-02-20 18:24:43 -080043#include "ProCamera2Client.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45namespace android {
46
47// ----------------------------------------------------------------------------
48// Logging support -- this is for debugging only
49// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070050volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051
Steve Blockb8a80522011-12-20 16:23:08 +000052#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
53#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070054
55static void setLogLevel(int level) {
56 android_atomic_write(level, &gLogLevel);
57}
58
59// ----------------------------------------------------------------------------
60
61static int getCallingPid() {
62 return IPCThreadState::self()->getCallingPid();
63}
64
65static int getCallingUid() {
66 return IPCThreadState::self()->getCallingUid();
67}
68
69// ----------------------------------------------------------------------------
70
71// This is ugly and only safe if we never re-create the CameraService, but
72// should be ok for now.
73static CameraService *gCameraService;
74
75CameraService::CameraService()
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080076 :mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070077{
Steve Blockdf64d152012-01-04 20:05:49 +000078 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070079 gCameraService = this;
80}
81
Iliyan Malchev8951a972011-04-14 16:55:59 -070082void CameraService::onFirstRef()
83{
Igor Murashkin634a5152013-02-20 17:15:11 -080084 LOG1("CameraService::onFirstRef");
85
Iliyan Malchev8951a972011-04-14 16:55:59 -070086 BnCameraService::onFirstRef();
87
88 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
89 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +000090 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -070091 mNumberOfCameras = 0;
92 }
93 else {
Alex Rayc0dd54f2013-02-20 13:39:37 -080094 ALOGI("Loaded \"%s\" camera module", mModule->common.name);
Iliyan Malchev8951a972011-04-14 16:55:59 -070095 mNumberOfCameras = mModule->get_number_of_cameras();
96 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +000097 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -070098 mNumberOfCameras, MAX_CAMERAS);
99 mNumberOfCameras = MAX_CAMERAS;
100 }
101 for (int i = 0; i < mNumberOfCameras; i++) {
102 setCameraFree(i);
103 }
104 }
105}
106
Mathias Agopian65ab4712010-07-14 17:59:35 -0700107CameraService::~CameraService() {
108 for (int i = 0; i < mNumberOfCameras; i++) {
109 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000110 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111 }
112 }
113
114 gCameraService = NULL;
115}
116
117int32_t CameraService::getNumberOfCameras() {
118 return mNumberOfCameras;
119}
120
121status_t CameraService::getCameraInfo(int cameraId,
122 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700123 if (!mModule) {
124 return NO_INIT;
125 }
126
Mathias Agopian65ab4712010-07-14 17:59:35 -0700127 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
128 return BAD_VALUE;
129 }
130
Iliyan Malchev8951a972011-04-14 16:55:59 -0700131 struct camera_info info;
132 status_t rc = mModule->get_camera_info(cameraId, &info);
133 cameraInfo->facing = info.facing;
134 cameraInfo->orientation = info.orientation;
135 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136}
137
Igor Murashkin634a5152013-02-20 17:15:11 -0800138int CameraService::getDeviceVersion(int cameraId, int* facing) {
139 struct camera_info info;
140 if (mModule->get_camera_info(cameraId, &info) != OK) {
141 return -1;
142 }
143
144 int deviceVersion;
145 if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
146 deviceVersion = info.device_version;
147 } else {
148 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
149 }
150
151 if (facing) {
152 *facing = info.facing;
153 }
154
155 return deviceVersion;
156}
157
Mathias Agopian65ab4712010-07-14 17:59:35 -0700158sp<ICamera> CameraService::connect(
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800159 const sp<ICameraClient>& cameraClient,
160 int cameraId,
161 const String16& clientPackageName,
162 int clientUid) {
163
164 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500166
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800167 LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
168 clientName8.string(), cameraId);
169
170 if (clientUid == USE_CALLING_UID) {
171 clientUid = getCallingUid();
172 } else {
173 // We only trust our own process to forward client UIDs
174 if (callingPid != getpid()) {
175 ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
176 callingPid);
177 return NULL;
178 }
179 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700180
Iliyan Malchev8951a972011-04-14 16:55:59 -0700181 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000182 ALOGE("Camera HAL module not loaded");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700183 return NULL;
184 }
185
Mathias Agopian65ab4712010-07-14 17:59:35 -0700186 sp<Client> client;
187 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000188 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700189 callingPid, cameraId);
190 return NULL;
191 }
192
Wu-cheng Lia3355432011-05-20 14:54:25 +0800193 char value[PROPERTY_VALUE_MAX];
194 property_get("sys.secpolicy.camera.disabled", value, "0");
195 if (strcmp(value, "1") == 0) {
196 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000197 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800198 return NULL;
199 }
200
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800201 Mutex::Autolock lock(mServiceLock);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800202 if (mClient[cameraId] != 0) {
203 client = mClient[cameraId].promote();
204 if (client != 0) {
205 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
206 LOG1("CameraService::connect X (pid %d) (the same client)",
207 callingPid);
208 return client;
209 } else {
Igor Murashkin1d880232013-02-20 16:50:13 -0800210 // TODOSC: need to support 1 regular client, multiple shared clients here
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800211 ALOGW("CameraService::connect X (pid %d) rejected (existing client).",
212 callingPid);
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800213 return NULL;
214 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800215 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800216 mClient[cameraId].clear();
217 }
218
Igor Murashkin634a5152013-02-20 17:15:11 -0800219 /*
220 mBusy is set to false as the last step of the Client destructor,
221 after which it is guaranteed that the Client destructor has finished (
222 including any inherited destructors)
223
224 We only need this for a Client subclasses since we don't allow
225 multiple Clents to be opened concurrently, but multiple BasicClient
226 would be fine
227 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800228 if (mBusy[cameraId]) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800229
230 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
231 " (camera %d is still busy).", callingPid,
232 clientName8.string(), cameraId);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800233 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234 }
235
Igor Murashkin634a5152013-02-20 17:15:11 -0800236 int facing = -1;
237 int deviceVersion = getDeviceVersion(cameraId, &facing);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700238
239 switch(deviceVersion) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700240 case CAMERA_DEVICE_API_VERSION_1_0:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800241 client = new CameraClient(this, cameraClient,
242 clientPackageName, cameraId,
243 facing, callingPid, clientUid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700244 break;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700245 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkin634a5152013-02-20 17:15:11 -0800246 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800247 client = new Camera2Client(this, cameraClient,
248 clientPackageName, cameraId,
249 facing, callingPid, clientUid, getpid());
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700250 break;
Igor Murashkin634a5152013-02-20 17:15:11 -0800251 case -1:
252 ALOGE("Invalid camera id %d", cameraId);
253 return NULL;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700254 default:
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700255 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Tyler Luu5861a9a2011-10-06 00:00:03 -0500256 return NULL;
257 }
258
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700259 if (client->initialize(mModule) != OK) {
260 return NULL;
261 }
262
Igor Murashkinecf17e82012-10-02 16:05:11 -0700263 cameraClient->asBinder()->linkToDeath(this);
264
Mathias Agopian65ab4712010-07-14 17:59:35 -0700265 mClient[cameraId] = client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700266 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267 return client;
268}
269
Igor Murashkin634a5152013-02-20 17:15:11 -0800270sp<IProCameraUser> CameraService::connect(
271 const sp<IProCameraCallbacks>& cameraCb,
272 int cameraId)
273{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274 int callingPid = getCallingPid();
Igor Murashkin634a5152013-02-20 17:15:11 -0800275
276 LOG1("CameraService::connectPro E (pid %d, id %d)", callingPid, cameraId);
277
278 if (!mModule) {
279 ALOGE("Camera HAL module not loaded");
280 return NULL;
281 }
282
283 sp<ProClient> client;
284 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
285 ALOGE("CameraService::connectPro X (pid %d) rejected (invalid cameraId %d).",
286 callingPid, cameraId);
287 return NULL;
288 }
289
290 char value[PROPERTY_VALUE_MAX];
291 property_get("sys.secpolicy.camera.disabled", value, "0");
292 if (strcmp(value, "1") == 0) {
293 // Camera is disabled by DevicePolicyManager.
294 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
295 return NULL;
296 }
297
298 int facing = -1;
299 int deviceVersion = getDeviceVersion(cameraId, &facing);
300
301 switch(deviceVersion) {
302 case CAMERA_DEVICE_API_VERSION_1_0:
303 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", cameraId);
304 return NULL;
305 break;
306 case CAMERA_DEVICE_API_VERSION_2_0:
Igor Murashkin985fd302013-02-20 18:24:43 -0800307 case CAMERA_DEVICE_API_VERSION_2_1:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800308 client = new ProCamera2Client(this, cameraCb, String16(),
309 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
Igor Murashkin634a5152013-02-20 17:15:11 -0800310 break;
311 case -1:
312 ALOGE("Invalid camera id %d", cameraId);
313 return NULL;
314 default:
315 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
316 return NULL;
317 }
318
319 if (client->initialize(mModule) != OK) {
320 return NULL;
321 }
322
323 mProClientList[cameraId].push(client);
324
325 cameraCb->asBinder()->linkToDeath(this);
326
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800327 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
328 getpid());
Igor Murashkin634a5152013-02-20 17:15:11 -0800329 return client;
330
331
332 return NULL;
333}
334
335void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
336 int callingPid = getCallingPid();
337 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338
Igor Murashkinecf17e82012-10-02 16:05:11 -0700339 // Declare this before the lock to make absolutely sure the
340 // destructor won't be called with the lock held.
341 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342
Igor Murashkinecf17e82012-10-02 16:05:11 -0700343 int outIndex;
Igor Murashkin634a5152013-02-20 17:15:11 -0800344 sp<Client> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700345
346 if (client != 0) {
347 // Found our camera, clear and leave.
348 LOG1("removeClient: clear camera %d", outIndex);
349 mClient[outIndex].clear();
350
351 client->unlinkToDeath(this);
Igor Murashkin634a5152013-02-20 17:15:11 -0800352 } else {
353
354 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
355
356 if (clientPro != NULL) {
357 // Found our camera, clear and leave.
358 LOG1("removeClient: clear pro %p", clientPro.get());
359
360 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
361 }
Igor Murashkinecf17e82012-10-02 16:05:11 -0700362 }
363
Igor Murashkin634a5152013-02-20 17:15:11 -0800364 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
365}
366
367sp<CameraService::ProClient> CameraService::findProClientUnsafe(
368 const wp<IBinder>& cameraCallbacksRemote)
369{
370 sp<ProClient> clientPro;
371
372 for (int i = 0; i < mNumberOfCameras; ++i) {
373 Vector<size_t> removeIdx;
374
375 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
376 wp<ProClient> cl = mProClientList[i][j];
377
378 sp<ProClient> clStrong = cl.promote();
379 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
380 clientPro = clStrong;
381 break;
382 } else if (clStrong == NULL) {
383 // mark to clean up dead ptr
384 removeIdx.push(j);
385 }
386 }
387
388 // remove stale ptrs (in reverse so the indices dont change)
389 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
390 mProClientList[i].removeAt(removeIdx[j]);
391 }
392
393 }
394
395 return clientPro;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700396}
397
398sp<CameraService::Client> CameraService::findClientUnsafe(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700399 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700400 sp<Client> client;
401
402 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700403
404 // This happens when we have already disconnected (or this is
405 // just another unused camera).
406 if (mClient[i] == 0) continue;
407
408 // Promote mClient. It can fail if we are called from this path:
Igor Murashkin634a5152013-02-20 17:15:11 -0800409 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -0700410 client = mClient[i].promote();
411
Igor Murashkinecf17e82012-10-02 16:05:11 -0700412 // Clean up stale client entry
413 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700414 mClient[i].clear();
415 continue;
416 }
417
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700418 if (cameraClient == client->getCameraClient()->asBinder()) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700419 // Found our camera
420 outIndex = i;
421 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700422 }
423 }
424
Igor Murashkinecf17e82012-10-02 16:05:11 -0700425 outIndex = -1;
426 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427}
428
Keun young Parkd8973a72012-03-28 14:13:09 -0700429CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700430 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700431 return mClient[cameraId].unsafe_get();
432}
433
434Mutex* CameraService::getClientLockById(int cameraId) {
435 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
436 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700437}
438
Igor Murashkin634a5152013-02-20 17:15:11 -0800439sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700440 const wp<IBinder>& cameraClient) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700441
442 // Declare this before the lock to make absolutely sure the
443 // destructor won't be called with the lock held.
Igor Murashkin634a5152013-02-20 17:15:11 -0800444 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700445
446 Mutex::Autolock lock(mServiceLock);
447
448 int outIndex;
449 client = findClientUnsafe(cameraClient, outIndex);
450
451 return client;
452}
453
Mathias Agopian65ab4712010-07-14 17:59:35 -0700454status_t CameraService::onTransact(
455 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
456 // Permission checks
457 switch (code) {
458 case BnCameraService::CONNECT:
Igor Murashkin634a5152013-02-20 17:15:11 -0800459 case BnCameraService::CONNECT_PRO:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 const int pid = getCallingPid();
461 const int self_pid = getpid();
462 if (pid != self_pid) {
463 // we're called from a different process, do the real check
464 if (!checkCallingPermission(
465 String16("android.permission.CAMERA"))) {
466 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000467 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 "can't use the camera pid=%d, uid=%d", pid, uid);
469 return PERMISSION_DENIED;
470 }
471 }
472 break;
473 }
474
475 return BnCameraService::onTransact(code, data, reply, flags);
476}
477
478// The reason we need this busy bit is a new CameraService::connect() request
479// may come in while the previous Client's destructor has not been run or is
480// still running. If the last strong reference of the previous Client is gone
481// but the destructor has not been finished, we should not allow the new Client
482// to be created because we need to wait for the previous Client to tear down
483// the hardware first.
484void CameraService::setCameraBusy(int cameraId) {
485 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700486
487 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700488}
489
490void CameraService::setCameraFree(int cameraId) {
491 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700492
493 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700494}
495
496// We share the media players for shutter and recording sound for all clients.
497// A reference count is kept to determine when we will actually release the
498// media players.
499
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800500MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700501 MediaPlayer* mp = new MediaPlayer();
502 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800503 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700504 mp->prepare();
505 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000506 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700507 return NULL;
508 }
509 return mp;
510}
511
512void CameraService::loadSound() {
513 Mutex::Autolock lock(mSoundLock);
514 LOG1("CameraService::loadSound ref=%d", mSoundRef);
515 if (mSoundRef++) return;
516
517 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
518 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
519}
520
521void CameraService::releaseSound() {
522 Mutex::Autolock lock(mSoundLock);
523 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
524 if (--mSoundRef) return;
525
526 for (int i = 0; i < NUM_SOUNDS; i++) {
527 if (mSoundPlayer[i] != 0) {
528 mSoundPlayer[i]->disconnect();
529 mSoundPlayer[i].clear();
530 }
531 }
532}
533
534void CameraService::playSound(sound_kind kind) {
535 LOG1("playSound(%d)", kind);
536 Mutex::Autolock lock(mSoundLock);
537 sp<MediaPlayer> player = mSoundPlayer[kind];
538 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800539 player->seekTo(0);
540 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 }
542}
543
544// ----------------------------------------------------------------------------
545
546CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700547 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800548 const String16& clientPackageName,
549 int cameraId, int cameraFacing,
550 int clientPid, uid_t clientUid,
551 int servicePid) :
Igor Murashkin634a5152013-02-20 17:15:11 -0800552 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800553 clientPackageName,
554 cameraId, cameraFacing,
555 clientPid, clientUid,
556 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800557{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700558 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800559 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700560
Mathias Agopian65ab4712010-07-14 17:59:35 -0700561 mCameraClient = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700562
Mathias Agopian65ab4712010-07-14 17:59:35 -0700563 cameraService->setCameraBusy(cameraId);
564 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800565
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800566 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567}
568
Mathias Agopian65ab4712010-07-14 17:59:35 -0700569// tear down the client
570CameraService::Client::~Client() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800571 mDestructionStarted = true;
572
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700573 mCameraService->releaseSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800574 finishCameraOps();
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700575 // unconditionally disconnect. function is idempotent
576 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700577}
578
Igor Murashkin634a5152013-02-20 17:15:11 -0800579CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800580 const sp<IBinder>& remoteCallback,
581 const String16& clientPackageName,
582 int cameraId, int cameraFacing,
583 int clientPid, uid_t clientUid,
584 int servicePid):
585 mClientPackageName(clientPackageName)
Igor Murashkin634a5152013-02-20 17:15:11 -0800586{
587 mCameraService = cameraService;
588 mRemoteCallback = remoteCallback;
589 mCameraId = cameraId;
590 mCameraFacing = cameraFacing;
591 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800592 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -0800593 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800594 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -0800595 mDestructionStarted = false;
596}
597
598CameraService::BasicClient::~BasicClient() {
599 mDestructionStarted = true;
600}
601
602void CameraService::BasicClient::disconnect() {
603 mCameraService->removeClientByRemote(mRemoteCallback);
604}
605
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800606status_t CameraService::BasicClient::startCameraOps() {
607 int32_t res;
608
609 mOpsCallback = new OpsCallback(this);
610
611 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
612 mClientPackageName, mOpsCallback);
613 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
614 mClientUid, mClientPackageName);
615
616 if (res != AppOpsManager::MODE_ALLOWED) {
617 ALOGI("Camera %d: Access for \"%s\" has been revoked",
618 mCameraId, String8(mClientPackageName).string());
619 return PERMISSION_DENIED;
620 }
621 mOpsActive = true;
622 return OK;
623}
624
625status_t CameraService::BasicClient::finishCameraOps() {
626 if (mOpsActive) {
627 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
628 mClientPackageName);
629 mOpsActive = false;
630 }
631 mAppOpsManager.stopWatchingMode(mOpsCallback);
632 mOpsCallback.clear();
633
634 return OK;
635}
636
637void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
638 String8 name(packageName);
639 String8 myName(mClientPackageName);
640
641 if (op != AppOpsManager::OP_CAMERA) {
642 ALOGW("Unexpected app ops notification received: %d", op);
643 return;
644 }
645
646 int32_t res;
647 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
648 mClientUid, mClientPackageName);
649 ALOGV("checkOp returns: %d, %s ", res,
650 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
651 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
652 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
653 "UNKNOWN");
654
655 if (res != AppOpsManager::MODE_ALLOWED) {
656 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
657 myName.string());
658 // Reset the client PID to allow server-initiated disconnect,
659 // and to prevent further calls by client.
660 mClientPid = getCallingPid();
661 notifyError();
662 disconnect();
663 }
664}
665
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666// ----------------------------------------------------------------------------
667
Keun young Parkd8973a72012-03-28 14:13:09 -0700668Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
669 return gCameraService->getClientLockById((int) user);
670}
671
672// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
673// be acquired for this to be safe
674CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
675 Client* client = gCameraService->getClientByIdUnsafe((int) user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700676
677 // This could happen if the Client is in the process of shutting down (the
678 // last strong reference is gone, but the destructor hasn't finished
679 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -0700680 if (client == NULL) return NULL;
681
682 // destruction already started, so should not be accessed
683 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700684
Mathias Agopian65ab4712010-07-14 17:59:35 -0700685 return client;
686}
687
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800688void CameraService::Client::notifyError() {
689 mCameraClient->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
690}
691
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700692// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700693void CameraService::Client::disconnect() {
Igor Murashkin634a5152013-02-20 17:15:11 -0800694 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700695 mCameraService->setCameraFree(mCameraId);
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800696}
697
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800698CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
699 mClient(client) {
700}
701
702void CameraService::Client::OpsCallback::opChanged(int32_t op,
703 const String16& packageName) {
704 sp<BasicClient> client = mClient.promote();
705 if (client != NULL) {
706 client->opChanged(op, packageName);
707 }
708}
709
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710// ----------------------------------------------------------------------------
Igor Murashkin634a5152013-02-20 17:15:11 -0800711// IProCamera
712// ----------------------------------------------------------------------------
713
714CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800715 const sp<IProCameraCallbacks>& remoteCallback,
716 const String16& clientPackageName,
717 int cameraId,
718 int cameraFacing,
719 int clientPid,
720 uid_t clientUid,
721 int servicePid)
722 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
723 clientPackageName, cameraId, cameraFacing,
724 clientPid, clientUid, servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800725{
726 mRemoteCallback = remoteCallback;
727}
728
729CameraService::ProClient::~ProClient() {
730 mDestructionStarted = true;
731
732 ProClient::disconnect();
733}
734
735status_t CameraService::ProClient::connect(const sp<IProCameraCallbacks>& callbacks) {
736 ALOGE("%s: not implemented yet", __FUNCTION__);
737
738 return INVALID_OPERATION;
739}
740
741void CameraService::ProClient::disconnect() {
742 BasicClient::disconnect();
743}
744
745status_t CameraService::ProClient::initialize(camera_module_t* module)
746{
747 ALOGW("%s: not implemented yet", __FUNCTION__);
748 return OK;
749}
750
751status_t CameraService::ProClient::exclusiveTryLock() {
752 ALOGE("%s: not implemented yet", __FUNCTION__);
753 return INVALID_OPERATION;
754}
755
756status_t CameraService::ProClient::exclusiveLock() {
757 ALOGE("%s: not implemented yet", __FUNCTION__);
758 return INVALID_OPERATION;
759}
760
761status_t CameraService::ProClient::exclusiveUnlock() {
762 ALOGE("%s: not implemented yet", __FUNCTION__);
763 return INVALID_OPERATION;
764}
765
766bool CameraService::ProClient::hasExclusiveLock() {
767 ALOGE("%s: not implemented yet", __FUNCTION__);
768 return false;
769}
770
771status_t CameraService::ProClient::submitRequest(camera_metadata_t* request, bool streaming) {
772 ALOGE("%s: not implemented yet", __FUNCTION__);
773
774 free_camera_metadata(request);
775
776 return INVALID_OPERATION;
777}
778
779status_t CameraService::ProClient::cancelRequest(int requestId) {
780 ALOGE("%s: not implemented yet", __FUNCTION__);
781
782 return INVALID_OPERATION;
783}
784
785status_t CameraService::ProClient::requestStream(int streamId) {
786 ALOGE("%s: not implemented yet", __FUNCTION__);
787
788 return INVALID_OPERATION;
789}
790
791status_t CameraService::ProClient::cancelStream(int streamId) {
792 ALOGE("%s: not implemented yet", __FUNCTION__);
793
794 return INVALID_OPERATION;
795}
796
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800797void CameraService::ProClient::notifyError() {
798 ALOGE("%s: not implemented yet", __FUNCTION__);
799}
800
Igor Murashkin634a5152013-02-20 17:15:11 -0800801// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -0700802
803static const int kDumpLockRetries = 50;
804static const int kDumpLockSleep = 60000;
805
806static bool tryLock(Mutex& mutex)
807{
808 bool locked = false;
809 for (int i = 0; i < kDumpLockRetries; ++i) {
810 if (mutex.tryLock() == NO_ERROR) {
811 locked = true;
812 break;
813 }
814 usleep(kDumpLockSleep);
815 }
816 return locked;
817}
818
819status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 String8 result;
821 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700822 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700823 "can't dump CameraService from pid=%d, uid=%d\n",
824 getCallingPid(),
825 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700826 write(fd, result.string(), result.size());
827 } else {
828 bool locked = tryLock(mServiceLock);
829 // failed to lock - CameraService is probably deadlocked
830 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700831 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700832 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700833 }
834
835 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700836 if (!mModule) {
837 result = String8::format("No camera module available!\n");
838 write(fd, result.string(), result.size());
839 return NO_ERROR;
840 }
841
842 result = String8::format("Camera module HAL API version: 0x%x\n",
843 mModule->common.hal_api_version);
844 result.appendFormat("Camera module API version: 0x%x\n",
845 mModule->common.module_api_version);
846 result.appendFormat("Camera module name: %s\n",
847 mModule->common.name);
848 result.appendFormat("Camera module author: %s\n",
849 mModule->common.author);
850 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
851 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700852 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700853 result = String8::format("Camera %d static information:\n", i);
854 camera_info info;
855
856 status_t rc = mModule->get_camera_info(i, &info);
857 if (rc != OK) {
858 result.appendFormat(" Error reading static information!\n");
859 write(fd, result.string(), result.size());
860 } else {
861 result.appendFormat(" Facing: %s\n",
862 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
863 result.appendFormat(" Orientation: %d\n", info.orientation);
864 int deviceVersion;
865 if (mModule->common.module_api_version <
866 CAMERA_MODULE_API_VERSION_2_0) {
867 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
868 } else {
869 deviceVersion = info.device_version;
870 }
871 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
872 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
873 result.appendFormat(" Device static metadata:\n");
874 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -0700875 dump_indented_camera_metadata(info.static_camera_characteristics,
876 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700877 } else {
878 write(fd, result.string(), result.size());
879 }
880 }
881
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882 sp<Client> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700883 if (client == 0) {
884 result = String8::format(" Device is closed, no client instance\n");
885 write(fd, result.string(), result.size());
886 continue;
887 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700888 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700889 result = String8::format(" Device is open. Client instance dump:\n");
890 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700891 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700892 }
893 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700894 result = String8::format("\nNo active camera clients yet.\n");
895 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700896 }
897
898 if (locked) mServiceLock.unlock();
899
900 // change logging level
901 int n = args.size();
902 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -0700903 String16 verboseOption("-v");
904 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700905 String8 levelStr(args[i+1]);
906 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700907 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700908 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700909 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700910 }
911 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -0700912
Mathias Agopian65ab4712010-07-14 17:59:35 -0700913 }
914 return NO_ERROR;
915}
916
Igor Murashkinecf17e82012-10-02 16:05:11 -0700917/*virtual*/void CameraService::binderDied(
918 const wp<IBinder> &who) {
919
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700920 /**
921 * While tempting to promote the wp<IBinder> into a sp,
922 * it's actually not supported by the binder driver
923 */
924
Igor Murashkinecf17e82012-10-02 16:05:11 -0700925 ALOGV("java clients' binder died");
926
Igor Murashkin634a5152013-02-20 17:15:11 -0800927 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700928
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700929 if (cameraClient == 0) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700930 ALOGV("java clients' binder death already cleaned up (normal case)");
931 return;
932 }
933
Igor Murashkinecf17e82012-10-02 16:05:11 -0700934 ALOGW("Disconnecting camera client %p since the binder for it "
935 "died (this pid %d)", cameraClient.get(), getCallingPid());
936
937 cameraClient->disconnect();
938
939}
940
Mathias Agopian65ab4712010-07-14 17:59:35 -0700941}; // namespace android