blob: c284a0d7a64ebda8436a6549ea69bcdc8b48629d [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"
Igor Murashkine7ee7632013-06-11 18:10:18 -070044#include "photography/CameraDeviceClient.h"
Igor Murashkin98e24722013-06-19 19:51:04 -070045#include "CameraDeviceFactory.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070046
47namespace android {
48
49// ----------------------------------------------------------------------------
50// Logging support -- this is for debugging only
51// Use "adb shell dumpsys media.camera -v 1" to change it.
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070052volatile int32_t gLogLevel = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -070053
Steve Blockb8a80522011-12-20 16:23:08 +000054#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
55#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070056
57static void setLogLevel(int level) {
58 android_atomic_write(level, &gLogLevel);
59}
60
61// ----------------------------------------------------------------------------
62
63static int getCallingPid() {
64 return IPCThreadState::self()->getCallingPid();
65}
66
67static int getCallingUid() {
68 return IPCThreadState::self()->getCallingUid();
69}
70
Igor Murashkincba2c162013-03-20 15:56:31 -070071extern "C" {
72static void camera_device_status_change(
73 const struct camera_module_callbacks* callbacks,
74 int camera_id,
75 int new_status) {
76 sp<CameraService> cs = const_cast<CameraService*>(
77 static_cast<const CameraService*>(callbacks));
78
79 cs->onDeviceStatusChanged(
80 camera_id,
81 new_status);
82}
83} // extern "C"
84
Mathias Agopian65ab4712010-07-14 17:59:35 -070085// ----------------------------------------------------------------------------
86
87// This is ugly and only safe if we never re-create the CameraService, but
88// should be ok for now.
89static CameraService *gCameraService;
90
91CameraService::CameraService()
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080092 :mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070093{
Steve Blockdf64d152012-01-04 20:05:49 +000094 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070095 gCameraService = this;
Igor Murashkinbfc99152013-02-27 12:55:20 -080096
97 for (size_t i = 0; i < MAX_CAMERAS; ++i) {
Igor Murashkincba2c162013-03-20 15:56:31 -070098 mStatusList[i] = ICameraServiceListener::STATUS_PRESENT;
Igor Murashkinbfc99152013-02-27 12:55:20 -080099 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700100
101 this->camera_device_status_change = android::camera_device_status_change;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102}
103
Iliyan Malchev8951a972011-04-14 16:55:59 -0700104void CameraService::onFirstRef()
105{
Igor Murashkin634a5152013-02-20 17:15:11 -0800106 LOG1("CameraService::onFirstRef");
107
Iliyan Malchev8951a972011-04-14 16:55:59 -0700108 BnCameraService::onFirstRef();
109
110 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
111 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000112 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700113 mNumberOfCameras = 0;
114 }
115 else {
Alex Rayc0dd54f2013-02-20 13:39:37 -0800116 ALOGI("Loaded \"%s\" camera module", mModule->common.name);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700117 mNumberOfCameras = mModule->get_number_of_cameras();
118 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +0000119 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -0700120 mNumberOfCameras, MAX_CAMERAS);
121 mNumberOfCameras = MAX_CAMERAS;
122 }
123 for (int i = 0; i < mNumberOfCameras; i++) {
124 setCameraFree(i);
125 }
Igor Murashkincba2c162013-03-20 15:56:31 -0700126
127 if (mModule->common.module_api_version >=
128 CAMERA_MODULE_API_VERSION_2_1) {
129 mModule->set_callbacks(this);
130 }
Igor Murashkin98e24722013-06-19 19:51:04 -0700131
132 CameraDeviceFactory::registerService(this);
Iliyan Malchev8951a972011-04-14 16:55:59 -0700133 }
134}
135
Mathias Agopian65ab4712010-07-14 17:59:35 -0700136CameraService::~CameraService() {
137 for (int i = 0; i < mNumberOfCameras; i++) {
138 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000139 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700140 }
141 }
142
143 gCameraService = NULL;
144}
145
Igor Murashkincba2c162013-03-20 15:56:31 -0700146void CameraService::onDeviceStatusChanged(int cameraId,
147 int newStatus)
148{
149 ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__,
150 cameraId, newStatus);
151
152 if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
153 ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId);
154 return;
155 }
156
157 if ((int)getStatus(cameraId) == newStatus) {
158 ALOGE("%s: State transition to the same status 0x%x not allowed",
159 __FUNCTION__, (uint32_t)newStatus);
160 return;
161 }
162
163 /* don't do this in updateStatus
164 since it is also called from connect and we could get into a deadlock */
165 if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
166 Vector<sp<BasicClient> > clientsToDisconnect;
167 {
168 Mutex::Autolock al(mServiceLock);
169
170 /* Find all clients that we need to disconnect */
Igor Murashkine7ee7632013-06-11 18:10:18 -0700171 sp<BasicClient> client = mClient[cameraId].promote();
Igor Murashkincba2c162013-03-20 15:56:31 -0700172 if (client.get() != NULL) {
173 clientsToDisconnect.push_back(client);
174 }
175
176 int i = cameraId;
177 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
178 sp<ProClient> cl = mProClientList[i][j].promote();
179 if (cl != NULL) {
180 clientsToDisconnect.push_back(cl);
181 }
182 }
183 }
184
185 /* now disconnect them. don't hold the lock
186 or we can get into a deadlock */
187
188 for (size_t i = 0; i < clientsToDisconnect.size(); ++i) {
189 sp<BasicClient> client = clientsToDisconnect[i];
190
191 client->disconnect();
192 /**
193 * The remote app will no longer be able to call methods on the
194 * client since the client PID will be reset to 0
195 */
196 }
197
198 ALOGV("%s: After unplug, disconnected %d clients",
199 __FUNCTION__, clientsToDisconnect.size());
200 }
201
202 updateStatus(
203 static_cast<ICameraServiceListener::Status>(newStatus), cameraId);
204
205}
206
Mathias Agopian65ab4712010-07-14 17:59:35 -0700207int32_t CameraService::getNumberOfCameras() {
208 return mNumberOfCameras;
209}
210
211status_t CameraService::getCameraInfo(int cameraId,
212 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700213 if (!mModule) {
214 return NO_INIT;
215 }
216
Mathias Agopian65ab4712010-07-14 17:59:35 -0700217 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
218 return BAD_VALUE;
219 }
220
Iliyan Malchev8951a972011-04-14 16:55:59 -0700221 struct camera_info info;
222 status_t rc = mModule->get_camera_info(cameraId, &info);
223 cameraInfo->facing = info.facing;
224 cameraInfo->orientation = info.orientation;
225 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226}
227
Igor Murashkin634a5152013-02-20 17:15:11 -0800228int CameraService::getDeviceVersion(int cameraId, int* facing) {
229 struct camera_info info;
230 if (mModule->get_camera_info(cameraId, &info) != OK) {
231 return -1;
232 }
233
234 int deviceVersion;
235 if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) {
236 deviceVersion = info.device_version;
237 } else {
238 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
239 }
240
241 if (facing) {
242 *facing = info.facing;
243 }
244
245 return deviceVersion;
246}
247
Igor Murashkinbfc99152013-02-27 12:55:20 -0800248bool CameraService::isValidCameraId(int cameraId) {
249 int facing;
250 int deviceVersion = getDeviceVersion(cameraId, &facing);
251
252 switch(deviceVersion) {
253 case CAMERA_DEVICE_API_VERSION_1_0:
254 case CAMERA_DEVICE_API_VERSION_2_0:
255 case CAMERA_DEVICE_API_VERSION_2_1:
256 case CAMERA_DEVICE_API_VERSION_3_0:
257 return true;
258 default:
259 return false;
260 }
261
262 return false;
263}
264
Igor Murashkine6800ce2013-03-04 17:25:57 -0800265bool CameraService::validateConnect(int cameraId,
266 /*inout*/
267 int& clientUid) const {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800268
Mathias Agopian65ab4712010-07-14 17:59:35 -0700269 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500270
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800271 if (clientUid == USE_CALLING_UID) {
272 clientUid = getCallingUid();
273 } else {
274 // We only trust our own process to forward client UIDs
275 if (callingPid != getpid()) {
276 ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)",
277 callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800278 return false;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800279 }
280 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281
Iliyan Malchev8951a972011-04-14 16:55:59 -0700282 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000283 ALOGE("Camera HAL module not loaded");
Igor Murashkine6800ce2013-03-04 17:25:57 -0800284 return false;
Iliyan Malchev8951a972011-04-14 16:55:59 -0700285 }
286
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000288 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289 callingPid, cameraId);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800290 return false;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 }
292
Wu-cheng Lia3355432011-05-20 14:54:25 +0800293 char value[PROPERTY_VALUE_MAX];
294 property_get("sys.secpolicy.camera.disabled", value, "0");
295 if (strcmp(value, "1") == 0) {
296 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000297 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800298 return false;
Wu-cheng Lia3355432011-05-20 14:54:25 +0800299 }
300
Igor Murashkincba2c162013-03-20 15:56:31 -0700301 ICameraServiceListener::Status currentStatus = getStatus(cameraId);
302 if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) {
303 ALOGI("Camera is not plugged in,"
304 " connect X (pid %d) rejected", callingPid);
305 return false;
306 } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) {
307 ALOGI("Camera is enumerating,"
308 " connect X (pid %d) rejected", callingPid);
309 return false;
310 }
311 // Else don't check for STATUS_NOT_AVAILABLE.
312 // -- It's done implicitly in canConnectUnsafe /w the mBusy array
313
Igor Murashkine6800ce2013-03-04 17:25:57 -0800314 return true;
315}
316
317bool CameraService::canConnectUnsafe(int cameraId,
318 const String16& clientPackageName,
319 const sp<IBinder>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700320 sp<BasicClient> &client) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800321 String8 clientName8(clientPackageName);
322 int callingPid = getCallingPid();
323
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800324 if (mClient[cameraId] != 0) {
325 client = mClient[cameraId].promote();
326 if (client != 0) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700327 if (remoteCallback == client->getRemote()) {
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800328 LOG1("CameraService::connect X (pid %d) (the same client)",
329 callingPid);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800330 return true;
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800331 } else {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800332 // TODOSC: need to support 1 regular client,
333 // multiple shared clients here
334 ALOGW("CameraService::connect X (pid %d) rejected"
335 " (existing client).", callingPid);
336 return false;
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800337 }
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800338 }
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800339 mClient[cameraId].clear();
340 }
341
Igor Murashkin634a5152013-02-20 17:15:11 -0800342 /*
343 mBusy is set to false as the last step of the Client destructor,
344 after which it is guaranteed that the Client destructor has finished (
345 including any inherited destructors)
346
347 We only need this for a Client subclasses since we don't allow
348 multiple Clents to be opened concurrently, but multiple BasicClient
349 would be fine
350 */
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800351 if (mBusy[cameraId]) {
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800352 ALOGW("CameraService::connect X (pid %d, \"%s\") rejected"
353 " (camera %d is still busy).", callingPid,
354 clientName8.string(), cameraId);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800355 return false;
356 }
357
358 return true;
359}
360
361sp<ICamera> CameraService::connect(
362 const sp<ICameraClient>& cameraClient,
363 int cameraId,
364 const String16& clientPackageName,
365 int clientUid) {
366
367 String8 clientName8(clientPackageName);
368 int callingPid = getCallingPid();
369
370 LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid,
371 clientName8.string(), cameraId);
372
373 if (!validateConnect(cameraId, /*inout*/clientUid)) {
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800374 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375 }
376
Igor Murashkine6800ce2013-03-04 17:25:57 -0800377
Igor Murashkine7ee7632013-06-11 18:10:18 -0700378 sp<Client> client;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700379 {
380 Mutex::Autolock lock(mServiceLock);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700381 sp<BasicClient> clientTmp;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700382 if (!canConnectUnsafe(cameraId, clientPackageName,
383 cameraClient->asBinder(),
Igor Murashkine7ee7632013-06-11 18:10:18 -0700384 /*out*/clientTmp)) {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700385 return NULL;
386 } else if (client.get() != NULL) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700387 return static_cast<Client*>(clientTmp.get());
Igor Murashkinacd695c2013-03-13 17:23:00 -0700388 }
389
390 int facing = -1;
391 int deviceVersion = getDeviceVersion(cameraId, &facing);
392
393 // If there are other non-exclusive users of the camera,
394 // this will tear them down before we can reuse the camera
395 if (isValidCameraId(cameraId)) {
Igor Murashkincba2c162013-03-20 15:56:31 -0700396 // transition from PRESENT -> NOT_AVAILABLE
Igor Murashkinacd695c2013-03-13 17:23:00 -0700397 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
398 cameraId);
399 }
400
401 switch(deviceVersion) {
402 case CAMERA_DEVICE_API_VERSION_1_0:
403 client = new CameraClient(this, cameraClient,
404 clientPackageName, cameraId,
405 facing, callingPid, clientUid, getpid());
406 break;
407 case CAMERA_DEVICE_API_VERSION_2_0:
408 case CAMERA_DEVICE_API_VERSION_2_1:
409 case CAMERA_DEVICE_API_VERSION_3_0:
410 client = new Camera2Client(this, cameraClient,
411 clientPackageName, cameraId,
412 facing, callingPid, clientUid, getpid(),
413 deviceVersion);
414 break;
415 case -1:
416 ALOGE("Invalid camera id %d", cameraId);
417 return NULL;
418 default:
419 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
420 return NULL;
421 }
422
Igor Murashkine7ee7632013-06-11 18:10:18 -0700423 if (!connectFinishUnsafe(client,
424 client->getRemote())) {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700425 // this is probably not recoverable.. maybe the client can try again
Igor Murashkincba2c162013-03-20 15:56:31 -0700426 // OK: we can only get here if we were originally in PRESENT state
427 updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId);
Igor Murashkinacd695c2013-03-13 17:23:00 -0700428
429 return NULL;
430 }
431
432 mClient[cameraId] = client;
433 LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId,
434 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800435 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700436 // important: release the mutex here so the client can call back
437 // into the service from its destructor (can be at the end of the call)
Igor Murashkinbfc99152013-02-27 12:55:20 -0800438
Mathias Agopian65ab4712010-07-14 17:59:35 -0700439 return client;
440}
441
Igor Murashkine6800ce2013-03-04 17:25:57 -0800442bool CameraService::connectFinishUnsafe(const sp<BasicClient>& client,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700443 const sp<IBinder>& remoteCallback) {
Igor Murashkine6800ce2013-03-04 17:25:57 -0800444 if (client->initialize(mModule) != OK) {
445 return false;
446 }
447
Igor Murashkine7ee7632013-06-11 18:10:18 -0700448 remoteCallback->linkToDeath(this);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800449
450 return true;
451}
452
Igor Murashkin634a5152013-02-20 17:15:11 -0800453sp<IProCameraUser> CameraService::connect(
454 const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -0800455 int cameraId,
456 const String16& clientPackageName,
457 int clientUid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800458{
Igor Murashkinbfc99152013-02-27 12:55:20 -0800459 String8 clientName8(clientPackageName);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700460 int callingPid = getCallingPid();
Igor Murashkin634a5152013-02-20 17:15:11 -0800461
Igor Murashkine6800ce2013-03-04 17:25:57 -0800462 LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid,
463 clientName8.string(), cameraId);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800464
Igor Murashkine6800ce2013-03-04 17:25:57 -0800465 if (!validateConnect(cameraId, /*inout*/clientUid)) {
Igor Murashkin634a5152013-02-20 17:15:11 -0800466 return NULL;
467 }
468
Igor Murashkinacd695c2013-03-13 17:23:00 -0700469 sp<ProClient> client;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800470 {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700471 Mutex::Autolock lock(mServiceLock);
472 {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700473 sp<BasicClient> client;
Igor Murashkinacd695c2013-03-13 17:23:00 -0700474 if (!canConnectUnsafe(cameraId, clientPackageName,
475 cameraCb->asBinder(),
476 /*out*/client)) {
477 return NULL;
478 }
479 }
480
481 int facing = -1;
482 int deviceVersion = getDeviceVersion(cameraId, &facing);
483
484 switch(deviceVersion) {
485 case CAMERA_DEVICE_API_VERSION_1_0:
486 ALOGE("Camera id %d uses HALv1, doesn't support ProCamera",
487 cameraId);
488 return NULL;
489 break;
490 case CAMERA_DEVICE_API_VERSION_2_0:
491 case CAMERA_DEVICE_API_VERSION_2_1:
492 client = new ProCamera2Client(this, cameraCb, String16(),
493 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
494 break;
495 case -1:
496 ALOGE("Invalid camera id %d", cameraId);
497 return NULL;
498 default:
499 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800500 return NULL;
501 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700502
Igor Murashkine7ee7632013-06-11 18:10:18 -0700503 if (!connectFinishUnsafe(client, client->getRemote())) {
Igor Murashkinacd695c2013-03-13 17:23:00 -0700504 return NULL;
505 }
506
507 mProClientList[cameraId].push(client);
508
509 LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId,
510 getpid());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800511 }
Igor Murashkinacd695c2013-03-13 17:23:00 -0700512 // important: release the mutex here so the client can call back
513 // into the service from its destructor (can be at the end of the call)
Igor Murashkine6800ce2013-03-04 17:25:57 -0800514
Igor Murashkin634a5152013-02-20 17:15:11 -0800515 return client;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800516}
Igor Murashkin634a5152013-02-20 17:15:11 -0800517
Igor Murashkine7ee7632013-06-11 18:10:18 -0700518sp<ICameraDeviceUser> CameraService::connect(
519 const sp<ICameraDeviceCallbacks>& cameraCb,
520 int cameraId,
521 const String16& clientPackageName,
522 int clientUid)
523{
524 // TODO: this function needs to return status_t
525 // so that we have an error code when things go wrong and the client is NULL
526
527 String8 clientName8(clientPackageName);
528 int callingPid = getCallingPid();
529
530 LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid,
531 clientName8.string(), cameraId);
532
533 if (!validateConnect(cameraId, /*inout*/clientUid)) {
534 return NULL;
535 }
536
537 sp<CameraDeviceClient> client;
538 {
539 Mutex::Autolock lock(mServiceLock);
540 {
541 sp<BasicClient> client;
542 if (!canConnectUnsafe(cameraId, clientPackageName,
543 cameraCb->asBinder(),
544 /*out*/client)) {
545 return NULL;
546 }
547 }
548
549 int facing = -1;
550 int deviceVersion = getDeviceVersion(cameraId, &facing);
551
552 // If there are other non-exclusive users of the camera,
553 // this will tear them down before we can reuse the camera
554 if (isValidCameraId(cameraId)) {
555 // transition from PRESENT -> NOT_AVAILABLE
556 updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE,
557 cameraId);
558 }
559
560 switch(deviceVersion) {
561 case CAMERA_DEVICE_API_VERSION_1_0:
562 ALOGE("Camera id %d uses old HAL, doesn't support CameraDevice",
563 cameraId);
564 return NULL;
565 break;
566 // TODO: don't allow 2.0 Only allow 2.1 and higher
567 case CAMERA_DEVICE_API_VERSION_2_0:
568 case CAMERA_DEVICE_API_VERSION_2_1:
569 case CAMERA_DEVICE_API_VERSION_3_0:
570 client = new CameraDeviceClient(this, cameraCb, String16(),
571 cameraId, facing, callingPid, USE_CALLING_UID, getpid());
572 break;
573 case -1:
574 ALOGE("Invalid camera id %d", cameraId);
575 return NULL;
576 default:
577 ALOGE("Unknown camera device HAL version: %d", deviceVersion);
578 return NULL;
579 }
580
581 if (!connectFinishUnsafe(client, client->getRemote())) {
582 // this is probably not recoverable.. maybe the client can try again
583 // OK: we can only get here if we were originally in PRESENT state
584 updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId);
585 return NULL;
586 }
587
588 LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId,
589 getpid());
590
591 mClient[cameraId] = client;
592 }
593 // important: release the mutex here so the client can call back
594 // into the service from its destructor (can be at the end of the call)
595
596 return client;
597}
598
599
Igor Murashkinbfc99152013-02-27 12:55:20 -0800600status_t CameraService::addListener(
601 const sp<ICameraServiceListener>& listener) {
602 ALOGV("%s: Add listener %p", __FUNCTION__, listener.get());
Igor Murashkin634a5152013-02-20 17:15:11 -0800603
Igor Murashkinbfc99152013-02-27 12:55:20 -0800604 Mutex::Autolock lock(mServiceLock);
605
606 Vector<sp<ICameraServiceListener> >::iterator it, end;
607 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
608 if ((*it)->asBinder() == listener->asBinder()) {
609 ALOGW("%s: Tried to add listener %p which was already subscribed",
610 __FUNCTION__, listener.get());
611 return ALREADY_EXISTS;
612 }
613 }
614
615 mListenerList.push_back(listener);
616
Igor Murashkincba2c162013-03-20 15:56:31 -0700617 /* Immediately signal current status to this listener only */
618 {
619 Mutex::Autolock m(mStatusMutex) ;
620 int numCams = getNumberOfCameras();
621 for (int i = 0; i < numCams; ++i) {
622 listener->onStatusChanged(mStatusList[i], i);
623 }
624 }
625
Igor Murashkinbfc99152013-02-27 12:55:20 -0800626 return OK;
627}
628status_t CameraService::removeListener(
629 const sp<ICameraServiceListener>& listener) {
630 ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get());
631
632 Mutex::Autolock lock(mServiceLock);
633
634 Vector<sp<ICameraServiceListener> >::iterator it;
635 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
636 if ((*it)->asBinder() == listener->asBinder()) {
637 mListenerList.erase(it);
638 return OK;
639 }
640 }
641
642 ALOGW("%s: Tried to remove a listener %p which was not subscribed",
643 __FUNCTION__, listener.get());
644
645 return BAD_VALUE;
Igor Murashkin634a5152013-02-20 17:15:11 -0800646}
647
648void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) {
649 int callingPid = getCallingPid();
650 LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700651
Igor Murashkinecf17e82012-10-02 16:05:11 -0700652 // Declare this before the lock to make absolutely sure the
653 // destructor won't be called with the lock held.
654 Mutex::Autolock lock(mServiceLock);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700655
Igor Murashkinecf17e82012-10-02 16:05:11 -0700656 int outIndex;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700657 sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700658
659 if (client != 0) {
660 // Found our camera, clear and leave.
661 LOG1("removeClient: clear camera %d", outIndex);
662 mClient[outIndex].clear();
663
Igor Murashkine7ee7632013-06-11 18:10:18 -0700664 client->getRemote()->unlinkToDeath(this);
Igor Murashkin634a5152013-02-20 17:15:11 -0800665 } else {
666
667 sp<ProClient> clientPro = findProClientUnsafe(remoteBinder);
668
669 if (clientPro != NULL) {
670 // Found our camera, clear and leave.
671 LOG1("removeClient: clear pro %p", clientPro.get());
672
673 clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this);
674 }
Igor Murashkinecf17e82012-10-02 16:05:11 -0700675 }
676
Igor Murashkin634a5152013-02-20 17:15:11 -0800677 LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid);
678}
679
680sp<CameraService::ProClient> CameraService::findProClientUnsafe(
681 const wp<IBinder>& cameraCallbacksRemote)
682{
683 sp<ProClient> clientPro;
684
685 for (int i = 0; i < mNumberOfCameras; ++i) {
686 Vector<size_t> removeIdx;
687
688 for (size_t j = 0; j < mProClientList[i].size(); ++j) {
689 wp<ProClient> cl = mProClientList[i][j];
690
691 sp<ProClient> clStrong = cl.promote();
692 if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) {
693 clientPro = clStrong;
694 break;
695 } else if (clStrong == NULL) {
696 // mark to clean up dead ptr
697 removeIdx.push(j);
698 }
699 }
700
701 // remove stale ptrs (in reverse so the indices dont change)
702 for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) {
703 mProClientList[i].removeAt(removeIdx[j]);
704 }
705
706 }
707
708 return clientPro;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700709}
710
Igor Murashkine7ee7632013-06-11 18:10:18 -0700711sp<CameraService::BasicClient> CameraService::findClientUnsafe(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700712 const wp<IBinder>& cameraClient, int& outIndex) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700713 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700714
715 for (int i = 0; i < mNumberOfCameras; i++) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700716
717 // This happens when we have already disconnected (or this is
718 // just another unused camera).
719 if (mClient[i] == 0) continue;
720
721 // Promote mClient. It can fail if we are called from this path:
Igor Murashkin634a5152013-02-20 17:15:11 -0800722 // Client::~Client() -> disconnect() -> removeClientByRemote().
Mathias Agopian65ab4712010-07-14 17:59:35 -0700723 client = mClient[i].promote();
724
Igor Murashkinecf17e82012-10-02 16:05:11 -0700725 // Clean up stale client entry
726 if (client == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727 mClient[i].clear();
728 continue;
729 }
730
Igor Murashkine7ee7632013-06-11 18:10:18 -0700731 if (cameraClient == client->getRemote()) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700732 // Found our camera
733 outIndex = i;
734 return client;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700735 }
736 }
737
Igor Murashkinecf17e82012-10-02 16:05:11 -0700738 outIndex = -1;
739 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700740}
741
Igor Murashkine7ee7632013-06-11 18:10:18 -0700742CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Keun young Parkd8973a72012-03-28 14:13:09 -0700744 return mClient[cameraId].unsafe_get();
745}
746
747Mutex* CameraService::getClientLockById(int cameraId) {
748 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
749 return &mClientLock[cameraId];
Mathias Agopian65ab4712010-07-14 17:59:35 -0700750}
751
Igor Murashkin634a5152013-02-20 17:15:11 -0800752sp<CameraService::BasicClient> CameraService::getClientByRemote(
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700753 const wp<IBinder>& cameraClient) {
Igor Murashkinecf17e82012-10-02 16:05:11 -0700754
755 // Declare this before the lock to make absolutely sure the
756 // destructor won't be called with the lock held.
Igor Murashkin634a5152013-02-20 17:15:11 -0800757 sp<BasicClient> client;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700758
759 Mutex::Autolock lock(mServiceLock);
760
761 int outIndex;
762 client = findClientUnsafe(cameraClient, outIndex);
763
764 return client;
765}
766
Mathias Agopian65ab4712010-07-14 17:59:35 -0700767status_t CameraService::onTransact(
768 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
769 // Permission checks
770 switch (code) {
771 case BnCameraService::CONNECT:
Igor Murashkin634a5152013-02-20 17:15:11 -0800772 case BnCameraService::CONNECT_PRO:
Mathias Agopian65ab4712010-07-14 17:59:35 -0700773 const int pid = getCallingPid();
774 const int self_pid = getpid();
775 if (pid != self_pid) {
776 // we're called from a different process, do the real check
777 if (!checkCallingPermission(
778 String16("android.permission.CAMERA"))) {
779 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000780 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700781 "can't use the camera pid=%d, uid=%d", pid, uid);
782 return PERMISSION_DENIED;
783 }
784 }
785 break;
786 }
787
788 return BnCameraService::onTransact(code, data, reply, flags);
789}
790
791// The reason we need this busy bit is a new CameraService::connect() request
792// may come in while the previous Client's destructor has not been run or is
793// still running. If the last strong reference of the previous Client is gone
794// but the destructor has not been finished, we should not allow the new Client
795// to be created because we need to wait for the previous Client to tear down
796// the hardware first.
797void CameraService::setCameraBusy(int cameraId) {
798 android_atomic_write(1, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700799
800 ALOGV("setCameraBusy cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700801}
802
803void CameraService::setCameraFree(int cameraId) {
804 android_atomic_write(0, &mBusy[cameraId]);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700805
806 ALOGV("setCameraFree cameraId=%d", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700807}
808
809// We share the media players for shutter and recording sound for all clients.
810// A reference count is kept to determine when we will actually release the
811// media players.
812
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800813MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700814 MediaPlayer* mp = new MediaPlayer();
815 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800816 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700817 mp->prepare();
818 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000819 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700820 return NULL;
821 }
822 return mp;
823}
824
825void CameraService::loadSound() {
826 Mutex::Autolock lock(mSoundLock);
827 LOG1("CameraService::loadSound ref=%d", mSoundRef);
828 if (mSoundRef++) return;
829
830 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
831 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
832}
833
834void CameraService::releaseSound() {
835 Mutex::Autolock lock(mSoundLock);
836 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
837 if (--mSoundRef) return;
838
839 for (int i = 0; i < NUM_SOUNDS; i++) {
840 if (mSoundPlayer[i] != 0) {
841 mSoundPlayer[i]->disconnect();
842 mSoundPlayer[i].clear();
843 }
844 }
845}
846
847void CameraService::playSound(sound_kind kind) {
848 LOG1("playSound(%d)", kind);
849 Mutex::Autolock lock(mSoundLock);
850 sp<MediaPlayer> player = mSoundPlayer[kind];
851 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800852 player->seekTo(0);
853 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700854 }
855}
856
857// ----------------------------------------------------------------------------
858
859CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700860 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800861 const String16& clientPackageName,
862 int cameraId, int cameraFacing,
863 int clientPid, uid_t clientUid,
864 int servicePid) :
Igor Murashkin634a5152013-02-20 17:15:11 -0800865 CameraService::BasicClient(cameraService, cameraClient->asBinder(),
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800866 clientPackageName,
867 cameraId, cameraFacing,
868 clientPid, clientUid,
869 servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -0800870{
Mathias Agopian65ab4712010-07-14 17:59:35 -0700871 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800872 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700873
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800874 mRemoteCallback = cameraClient;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700875
Mathias Agopian65ab4712010-07-14 17:59:35 -0700876 cameraService->setCameraBusy(cameraId);
877 cameraService->loadSound();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800878
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800879 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700880}
881
Mathias Agopian65ab4712010-07-14 17:59:35 -0700882// tear down the client
883CameraService::Client::~Client() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -0700884 ALOGV("~Client");
Igor Murashkin634a5152013-02-20 17:15:11 -0800885 mDestructionStarted = true;
886
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700887 mCameraService->releaseSound();
Igor Murashkin036bc3e2012-10-08 15:09:46 -0700888 // unconditionally disconnect. function is idempotent
889 Client::disconnect();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700890}
891
Igor Murashkin634a5152013-02-20 17:15:11 -0800892CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800893 const sp<IBinder>& remoteCallback,
894 const String16& clientPackageName,
895 int cameraId, int cameraFacing,
896 int clientPid, uid_t clientUid,
897 int servicePid):
898 mClientPackageName(clientPackageName)
Igor Murashkin634a5152013-02-20 17:15:11 -0800899{
900 mCameraService = cameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800901 mRemoteBinder = remoteCallback;
Igor Murashkin634a5152013-02-20 17:15:11 -0800902 mCameraId = cameraId;
903 mCameraFacing = cameraFacing;
904 mClientPid = clientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800905 mClientUid = clientUid;
Igor Murashkin634a5152013-02-20 17:15:11 -0800906 mServicePid = servicePid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800907 mOpsActive = false;
Igor Murashkin634a5152013-02-20 17:15:11 -0800908 mDestructionStarted = false;
909}
910
911CameraService::BasicClient::~BasicClient() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -0700912 ALOGV("~BasicClient");
Igor Murashkin634a5152013-02-20 17:15:11 -0800913 mDestructionStarted = true;
914}
915
916void CameraService::BasicClient::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -0700917 ALOGV("BasicClient::disconnect");
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800918 mCameraService->removeClientByRemote(mRemoteBinder);
Igor Murashkincba2c162013-03-20 15:56:31 -0700919 // client shouldn't be able to call into us anymore
920 mClientPid = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800921}
922
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800923status_t CameraService::BasicClient::startCameraOps() {
924 int32_t res;
925
926 mOpsCallback = new OpsCallback(this);
927
Igor Murashkine6800ce2013-03-04 17:25:57 -0800928 {
929 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
930 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
931 }
932
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800933 mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA,
934 mClientPackageName, mOpsCallback);
935 res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA,
936 mClientUid, mClientPackageName);
937
938 if (res != AppOpsManager::MODE_ALLOWED) {
939 ALOGI("Camera %d: Access for \"%s\" has been revoked",
940 mCameraId, String8(mClientPackageName).string());
941 return PERMISSION_DENIED;
942 }
943 mOpsActive = true;
944 return OK;
945}
946
947status_t CameraService::BasicClient::finishCameraOps() {
948 if (mOpsActive) {
949 mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid,
950 mClientPackageName);
951 mOpsActive = false;
952 }
953 mAppOpsManager.stopWatchingMode(mOpsCallback);
954 mOpsCallback.clear();
955
956 return OK;
957}
958
959void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) {
960 String8 name(packageName);
961 String8 myName(mClientPackageName);
962
963 if (op != AppOpsManager::OP_CAMERA) {
964 ALOGW("Unexpected app ops notification received: %d", op);
965 return;
966 }
967
968 int32_t res;
969 res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA,
970 mClientUid, mClientPackageName);
971 ALOGV("checkOp returns: %d, %s ", res,
972 res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" :
973 res == AppOpsManager::MODE_IGNORED ? "IGNORED" :
974 res == AppOpsManager::MODE_ERRORED ? "ERRORED" :
975 "UNKNOWN");
976
977 if (res != AppOpsManager::MODE_ALLOWED) {
978 ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId,
979 myName.string());
980 // Reset the client PID to allow server-initiated disconnect,
981 // and to prevent further calls by client.
982 mClientPid = getCallingPid();
983 notifyError();
984 disconnect();
985 }
986}
987
Mathias Agopian65ab4712010-07-14 17:59:35 -0700988// ----------------------------------------------------------------------------
989
Keun young Parkd8973a72012-03-28 14:13:09 -0700990Mutex* CameraService::Client::getClientLockFromCookie(void* user) {
991 return gCameraService->getClientLockById((int) user);
992}
993
994// Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should
995// be acquired for this to be safe
996CameraService::Client* CameraService::Client::getClientFromCookie(void* user) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700997 BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int) user);
998 // OK: only CameraClient calls this, and they already cast anyway.
999 Client* client = static_cast<Client*>(basicClient);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000
1001 // This could happen if the Client is in the process of shutting down (the
1002 // last strong reference is gone, but the destructor hasn't finished
1003 // stopping the hardware).
Keun young Parkd8973a72012-03-28 14:13:09 -07001004 if (client == NULL) return NULL;
1005
1006 // destruction already started, so should not be accessed
1007 if (client->mDestructionStarted) return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008
Mathias Agopian65ab4712010-07-14 17:59:35 -07001009 return client;
1010}
1011
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001012void CameraService::Client::notifyError() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001013 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001014}
1015
Igor Murashkin036bc3e2012-10-08 15:09:46 -07001016// NOTE: function is idempotent
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001017void CameraService::Client::disconnect() {
Eino-Ville Talvalad09801b2013-04-23 15:16:57 -07001018 ALOGV("Client::disconnect");
Igor Murashkin634a5152013-02-20 17:15:11 -08001019 BasicClient::disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001020 mCameraService->setCameraFree(mCameraId);
Igor Murashkin93747b92013-05-01 15:42:20 -07001021
1022 StatusVector rejectSourceStates;
1023 rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT);
1024 rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING);
1025
1026 // Transition to PRESENT if the camera is not in either of above 2 states
Igor Murashkincba2c162013-03-20 15:56:31 -07001027 mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT,
Igor Murashkin93747b92013-05-01 15:42:20 -07001028 mCameraId,
1029 &rejectSourceStates);
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001030}
1031
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001032CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client):
1033 mClient(client) {
1034}
1035
1036void CameraService::Client::OpsCallback::opChanged(int32_t op,
1037 const String16& packageName) {
1038 sp<BasicClient> client = mClient.promote();
1039 if (client != NULL) {
1040 client->opChanged(op, packageName);
1041 }
1042}
1043
Mathias Agopian65ab4712010-07-14 17:59:35 -07001044// ----------------------------------------------------------------------------
Igor Murashkin634a5152013-02-20 17:15:11 -08001045// IProCamera
1046// ----------------------------------------------------------------------------
1047
1048CameraService::ProClient::ProClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001049 const sp<IProCameraCallbacks>& remoteCallback,
1050 const String16& clientPackageName,
1051 int cameraId,
1052 int cameraFacing,
1053 int clientPid,
1054 uid_t clientUid,
1055 int servicePid)
1056 : CameraService::BasicClient(cameraService, remoteCallback->asBinder(),
1057 clientPackageName, cameraId, cameraFacing,
1058 clientPid, clientUid, servicePid)
Igor Murashkin634a5152013-02-20 17:15:11 -08001059{
1060 mRemoteCallback = remoteCallback;
1061}
1062
1063CameraService::ProClient::~ProClient() {
Igor Murashkin634a5152013-02-20 17:15:11 -08001064}
1065
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001066void CameraService::ProClient::notifyError() {
Igor Murashkine6800ce2013-03-04 17:25:57 -08001067 mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -08001068}
1069
Igor Murashkin634a5152013-02-20 17:15:11 -08001070// ----------------------------------------------------------------------------
Mathias Agopian65ab4712010-07-14 17:59:35 -07001071
1072static const int kDumpLockRetries = 50;
1073static const int kDumpLockSleep = 60000;
1074
1075static bool tryLock(Mutex& mutex)
1076{
1077 bool locked = false;
1078 for (int i = 0; i < kDumpLockRetries; ++i) {
1079 if (mutex.tryLock() == NO_ERROR) {
1080 locked = true;
1081 break;
1082 }
1083 usleep(kDumpLockSleep);
1084 }
1085 return locked;
1086}
1087
1088status_t CameraService::dump(int fd, const Vector<String16>& args) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 String8 result;
1090 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001091 result.appendFormat("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -07001092 "can't dump CameraService from pid=%d, uid=%d\n",
1093 getCallingPid(),
1094 getCallingUid());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 write(fd, result.string(), result.size());
1096 } else {
1097 bool locked = tryLock(mServiceLock);
1098 // failed to lock - CameraService is probably deadlocked
1099 if (!locked) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001100 result.append("CameraService may be deadlocked\n");
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001101 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001102 }
1103
1104 bool hasClient = false;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001105 if (!mModule) {
1106 result = String8::format("No camera module available!\n");
1107 write(fd, result.string(), result.size());
1108 return NO_ERROR;
1109 }
1110
1111 result = String8::format("Camera module HAL API version: 0x%x\n",
1112 mModule->common.hal_api_version);
1113 result.appendFormat("Camera module API version: 0x%x\n",
1114 mModule->common.module_api_version);
1115 result.appendFormat("Camera module name: %s\n",
1116 mModule->common.name);
1117 result.appendFormat("Camera module author: %s\n",
1118 mModule->common.author);
1119 result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras);
1120 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001121 for (int i = 0; i < mNumberOfCameras; i++) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001122 result = String8::format("Camera %d static information:\n", i);
1123 camera_info info;
1124
1125 status_t rc = mModule->get_camera_info(i, &info);
1126 if (rc != OK) {
1127 result.appendFormat(" Error reading static information!\n");
1128 write(fd, result.string(), result.size());
1129 } else {
1130 result.appendFormat(" Facing: %s\n",
1131 info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT");
1132 result.appendFormat(" Orientation: %d\n", info.orientation);
1133 int deviceVersion;
1134 if (mModule->common.module_api_version <
1135 CAMERA_MODULE_API_VERSION_2_0) {
1136 deviceVersion = CAMERA_DEVICE_API_VERSION_1_0;
1137 } else {
1138 deviceVersion = info.device_version;
1139 }
1140 result.appendFormat(" Device version: 0x%x\n", deviceVersion);
1141 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) {
1142 result.appendFormat(" Device static metadata:\n");
1143 write(fd, result.string(), result.size());
Eino-Ville Talvala428b77a2012-07-30 09:55:30 -07001144 dump_indented_camera_metadata(info.static_camera_characteristics,
1145 fd, 2, 4);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001146 } else {
1147 write(fd, result.string(), result.size());
1148 }
1149 }
1150
Igor Murashkine7ee7632013-06-11 18:10:18 -07001151 sp<BasicClient> client = mClient[i].promote();
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001152 if (client == 0) {
1153 result = String8::format(" Device is closed, no client instance\n");
1154 write(fd, result.string(), result.size());
1155 continue;
1156 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001157 hasClient = true;
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001158 result = String8::format(" Device is open. Client instance dump:\n");
1159 write(fd, result.string(), result.size());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001160 client->dump(fd, args);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161 }
1162 if (!hasClient) {
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001163 result = String8::format("\nNo active camera clients yet.\n");
1164 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 }
1166
1167 if (locked) mServiceLock.unlock();
1168
1169 // change logging level
1170 int n = args.size();
1171 for (int i = 0; i + 1 < n; i++) {
Eino-Ville Talvala611f6192012-05-31 12:28:23 -07001172 String16 verboseOption("-v");
1173 if (args[i] == verboseOption) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001174 String8 levelStr(args[i+1]);
1175 int level = atoi(levelStr.string());
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001176 result = String8::format("\nSetting log level to %d.\n", level);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001177 setLogLevel(level);
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001178 write(fd, result.string(), result.size());
Mathias Agopian65ab4712010-07-14 17:59:35 -07001179 }
1180 }
Eino-Ville Talvalaf5926132012-07-17 13:54:20 -07001181
Mathias Agopian65ab4712010-07-14 17:59:35 -07001182 }
1183 return NO_ERROR;
1184}
1185
Igor Murashkinecf17e82012-10-02 16:05:11 -07001186/*virtual*/void CameraService::binderDied(
1187 const wp<IBinder> &who) {
1188
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001189 /**
1190 * While tempting to promote the wp<IBinder> into a sp,
1191 * it's actually not supported by the binder driver
1192 */
1193
Igor Murashkinecf17e82012-10-02 16:05:11 -07001194 ALOGV("java clients' binder died");
1195
Igor Murashkin634a5152013-02-20 17:15:11 -08001196 sp<BasicClient> cameraClient = getClientByRemote(who);
Igor Murashkinecf17e82012-10-02 16:05:11 -07001197
Igor Murashkin294d0ec2012-10-05 10:44:57 -07001198 if (cameraClient == 0) {
Igor Murashkinecf17e82012-10-02 16:05:11 -07001199 ALOGV("java clients' binder death already cleaned up (normal case)");
1200 return;
1201 }
1202
Igor Murashkinecf17e82012-10-02 16:05:11 -07001203 ALOGW("Disconnecting camera client %p since the binder for it "
1204 "died (this pid %d)", cameraClient.get(), getCallingPid());
1205
1206 cameraClient->disconnect();
1207
1208}
1209
Igor Murashkinbfc99152013-02-27 12:55:20 -08001210void CameraService::updateStatus(ICameraServiceListener::Status status,
Igor Murashkin93747b92013-05-01 15:42:20 -07001211 int32_t cameraId,
1212 const StatusVector *rejectSourceStates) {
Igor Murashkinbfc99152013-02-27 12:55:20 -08001213 // do not lock mServiceLock here or can get into a deadlock from
1214 // connect() -> ProClient::disconnect -> updateStatus
1215 Mutex::Autolock lock(mStatusMutex);
Igor Murashkinbfc99152013-02-27 12:55:20 -08001216
1217 ICameraServiceListener::Status oldStatus = mStatusList[cameraId];
1218
1219 mStatusList[cameraId] = status;
1220
1221 if (oldStatus != status) {
1222 ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x",
1223 __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status);
1224
Igor Murashkincba2c162013-03-20 15:56:31 -07001225 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
1226 (status != ICameraServiceListener::STATUS_PRESENT &&
1227 status != ICameraServiceListener::STATUS_ENUMERATING)) {
1228
1229 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT"
1230 " or ENUMERATING", __FUNCTION__);
1231 mStatusList[cameraId] = oldStatus;
1232 return;
1233 }
1234
Igor Murashkin93747b92013-05-01 15:42:20 -07001235 if (rejectSourceStates != NULL) {
1236 const StatusVector &rejectList = *rejectSourceStates;
1237 StatusVector::const_iterator it = rejectList.begin();
1238
1239 /**
1240 * Sometimes we want to conditionally do a transition.
1241 * For example if a client disconnects, we want to go to PRESENT
1242 * only if we weren't already in NOT_PRESENT or ENUMERATING.
1243 */
1244 for (; it != rejectList.end(); ++it) {
1245 if (oldStatus == *it) {
1246 ALOGV("%s: Rejecting status transition for Camera ID %d, "
1247 " since the source state was was in one of the bad "
1248 " states.", __FUNCTION__, cameraId);
1249 mStatusList[cameraId] = oldStatus;
1250 return;
1251 }
1252 }
1253 }
1254
Igor Murashkinbfc99152013-02-27 12:55:20 -08001255 /**
1256 * ProClients lose their exclusive lock.
1257 * - Done before the CameraClient can initialize the HAL device,
1258 * since we want to be able to close it before they get to initialize
1259 */
1260 if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) {
1261 Vector<wp<ProClient> > proClients(mProClientList[cameraId]);
1262 Vector<wp<ProClient> >::const_iterator it;
1263
1264 for (it = proClients.begin(); it != proClients.end(); ++it) {
1265 sp<ProClient> proCl = it->promote();
1266 if (proCl.get() != NULL) {
1267 proCl->onExclusiveLockStolen();
1268 }
1269 }
1270 }
1271
1272 Vector<sp<ICameraServiceListener> >::const_iterator it;
1273 for (it = mListenerList.begin(); it != mListenerList.end(); ++it) {
1274 (*it)->onStatusChanged(status, cameraId);
1275 }
1276 }
1277}
1278
Igor Murashkincba2c162013-03-20 15:56:31 -07001279ICameraServiceListener::Status CameraService::getStatus(int cameraId) const {
1280 if (cameraId < 0 || cameraId >= MAX_CAMERAS) {
1281 ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
1282 return ICameraServiceListener::STATUS_UNKNOWN;
1283 }
1284
1285 Mutex::Autolock al(mStatusMutex);
1286 return mStatusList[cameraId];
1287}
1288
Mathias Agopian65ab4712010-07-14 17:59:35 -07001289}; // namespace android