blob: 22836e313fad3dbc5338b9cd170b506cbd0e76ee [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>
Wu-cheng Li2fd24402012-02-23 19:01:00 -080024#include <time.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025
26#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>
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080032#include <gui/SurfaceTextureClient.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080033#include <gui/Surface.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070034#include <hardware/hardware.h>
35#include <media/AudioSystem.h>
36#include <media/mediaplayer.h>
Wu-cheng Li2fd24402012-02-23 19:01:00 -080037#include <utils/Condition.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070038#include <utils/Errors.h>
39#include <utils/Log.h>
40#include <utils/String16.h>
41
42#include "CameraService.h"
Iliyan Malchev8951a972011-04-14 16:55:59 -070043#include "CameraHardwareInterface.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070044
45namespace android {
46
Wu-cheng Li2fd24402012-02-23 19:01:00 -080047#define WAIT_RELEASE_TIMEOUT 250 // 250ms
48
Mathias Agopian65ab4712010-07-14 17:59:35 -070049// ----------------------------------------------------------------------------
50// Logging support -- this is for debugging only
51// Use "adb shell dumpsys media.camera -v 1" to change it.
52static volatile int32_t gLogLevel = 0;
53
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
Wu-cheng Li2fd24402012-02-23 19:01:00 -080071static long long getTimeInMs() {
72 struct timeval t;
73 t.tv_sec = t.tv_usec = 0;
74 gettimeofday(&t, NULL);
75 return t.tv_sec * 1000LL + t.tv_usec / 1000;
76}
77
Mathias Agopian65ab4712010-07-14 17:59:35 -070078// ----------------------------------------------------------------------------
79
80// This is ugly and only safe if we never re-create the CameraService, but
81// should be ok for now.
82static CameraService *gCameraService;
83
84CameraService::CameraService()
Iliyan Malchev8951a972011-04-14 16:55:59 -070085:mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070086{
Steve Blockdf64d152012-01-04 20:05:49 +000087 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070088 gCameraService = this;
89}
90
Iliyan Malchev8951a972011-04-14 16:55:59 -070091void CameraService::onFirstRef()
92{
93 BnCameraService::onFirstRef();
94
95 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
96 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +000097 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -070098 mNumberOfCameras = 0;
99 }
100 else {
101 mNumberOfCameras = mModule->get_number_of_cameras();
102 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +0000103 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -0700104 mNumberOfCameras, MAX_CAMERAS);
105 mNumberOfCameras = MAX_CAMERAS;
106 }
107 for (int i = 0; i < mNumberOfCameras; i++) {
108 setCameraFree(i);
109 }
110 }
111}
112
Mathias Agopian65ab4712010-07-14 17:59:35 -0700113CameraService::~CameraService() {
114 for (int i = 0; i < mNumberOfCameras; i++) {
115 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000116 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700117 }
118 }
119
120 gCameraService = NULL;
121}
122
123int32_t CameraService::getNumberOfCameras() {
124 return mNumberOfCameras;
125}
126
127status_t CameraService::getCameraInfo(int cameraId,
128 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700129 if (!mModule) {
130 return NO_INIT;
131 }
132
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
134 return BAD_VALUE;
135 }
136
Iliyan Malchev8951a972011-04-14 16:55:59 -0700137 struct camera_info info;
138 status_t rc = mModule->get_camera_info(cameraId, &info);
139 cameraInfo->facing = info.facing;
140 cameraInfo->orientation = info.orientation;
141 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142}
143
144sp<ICamera> CameraService::connect(
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800145 const sp<ICameraClient>& cameraClient, int cameraId, bool force, bool keep) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700146 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500147 sp<CameraHardwareInterface> hardware = NULL;
148
Mathias Agopian65ab4712010-07-14 17:59:35 -0700149 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
150
Iliyan Malchev8951a972011-04-14 16:55:59 -0700151 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000152 ALOGE("Camera HAL module not loaded");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700153 return NULL;
154 }
155
Mathias Agopian65ab4712010-07-14 17:59:35 -0700156 sp<Client> client;
157 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000158 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159 callingPid, cameraId);
160 return NULL;
161 }
162
Wu-cheng Lia3355432011-05-20 14:54:25 +0800163 char value[PROPERTY_VALUE_MAX];
164 property_get("sys.secpolicy.camera.disabled", value, "0");
165 if (strcmp(value, "1") == 0) {
166 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000167 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800168 return NULL;
169 }
170
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800171 if (keep && !checkCallingPermission(String16("android.permission.KEEP_CAMERA"))) {
172 ALOGE("connect X (pid %d) rejected (no KEEP_CAMERA permission).", callingPid);
173 return NULL;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700174 }
175
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800176 Mutex::Autolock lock(mServiceLock);
177 // Check if there is an existing client.
178 client = mClient[cameraId].promote();
179 if (client != 0 &&
180 cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
181 LOG1("connect X (pid %d) (the same client)", callingPid);
182 return client;
183 }
184
185 if (!force) {
186 if (mClient[cameraId].promote() != 0) {
187 ALOGW("connect X (pid %d) rejected (existing client).", callingPid);
188 return NULL;
189 }
190 mClient[cameraId].clear();
191 if (mBusy[cameraId]) {
192 ALOGW("connect X (pid %d) rejected (camera %d is still busy).",
193 callingPid, cameraId);
194 return NULL;
195 }
196 } else { // force == true
197 int i = 0;
198 long long start_time = getTimeInMs();
199 while (i < mNumberOfCameras) {
200 if (getTimeInMs() - start_time >= 3000LL) {
201 ALOGE("connect X (pid %d) rejected (timeout 3s)", callingPid);
202 return NULL;
203 }
204
205 client = mClient[i].promote();
206 if (client != 0) {
207 if (client->keep()) {
208 ALOGW("connect X (pid %d) rejected (existing client wants to keeps the camera)",
209 callingPid);
210 return NULL;
211 } else {
212 ALOGW("New client (pid %d, id=%d). Disconnect the existing client (id=%d).",
213 callingPid, cameraId, i);
214 // Do not hold mServiceLock because disconnect will try to get it.
215 mServiceLock.unlock();
216 client->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0, &i);
217 client->waitRelease(WAIT_RELEASE_TIMEOUT);
218 client->disconnectInternal(false);
219 mServiceLock.lock();
220 // Restart from the first client because a new client may have connected
221 // when mServiceLock is unlocked.
222 i = 0;
223 continue;
224 }
225 }
226
227 if (mBusy[i]) {
228 // Give the client a chance to release the hardware.
229 mServiceLock.unlock();
230 usleep(10 * 1000);
231 mServiceLock.lock();
232 i = 0; // Restart from the first client
233 continue;
234 }
235
236 i++;
237 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700238 }
239
Iliyan Malchev8951a972011-04-14 16:55:59 -0700240 struct camera_info info;
241 if (mModule->get_camera_info(cameraId, &info) != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000242 ALOGE("Invalid camera id %d", cameraId);
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700243 return NULL;
244 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700245
246 char camera_device_name[10];
247 snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
248
Tyler Luu5861a9a2011-10-06 00:00:03 -0500249 hardware = new CameraHardwareInterface(camera_device_name);
250 if (hardware->initialize(&mModule->common) != OK) {
251 hardware.clear();
252 return NULL;
253 }
254
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800255 client = new Client(this, cameraClient, hardware, cameraId, info.facing,
256 callingPid, keep);
257 // We need to clear the hardware here. After the destructor of mServiceLock
258 // finishes, a new client may connect and disconnect this client. If this
259 // reference is not cleared, the destructor of CameraHardwareInterface
260 // cannot run. The new client will not be able to connect.
261 hardware.clear();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700262 mClient[cameraId] = client;
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800263 LOG1("CameraService::connect X (id %d)", cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700264 return client;
265}
266
267void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
268 int callingPid = getCallingPid();
269 LOG1("CameraService::removeClient E (pid %d)", callingPid);
270
271 for (int i = 0; i < mNumberOfCameras; i++) {
272 // Declare this before the lock to make absolutely sure the
273 // destructor won't be called with the lock held.
274 sp<Client> client;
275
276 Mutex::Autolock lock(mServiceLock);
277
278 // This happens when we have already disconnected (or this is
279 // just another unused camera).
280 if (mClient[i] == 0) continue;
281
282 // Promote mClient. It can fail if we are called from this path:
283 // Client::~Client() -> disconnect() -> removeClient().
284 client = mClient[i].promote();
285
286 if (client == 0) {
287 mClient[i].clear();
288 continue;
289 }
290
291 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
292 // Found our camera, clear and leave.
293 LOG1("removeClient: clear camera %d", i);
294 mClient[i].clear();
295 break;
296 }
297 }
298
299 LOG1("CameraService::removeClient X (pid %d)", callingPid);
300}
301
302sp<CameraService::Client> CameraService::getClientById(int cameraId) {
303 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
304 return mClient[cameraId].promote();
305}
306
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307status_t CameraService::onTransact(
308 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
309 // Permission checks
310 switch (code) {
311 case BnCameraService::CONNECT:
312 const int pid = getCallingPid();
313 const int self_pid = getpid();
314 if (pid != self_pid) {
315 // we're called from a different process, do the real check
316 if (!checkCallingPermission(
317 String16("android.permission.CAMERA"))) {
318 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000319 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 "can't use the camera pid=%d, uid=%d", pid, uid);
321 return PERMISSION_DENIED;
322 }
323 }
324 break;
325 }
326
327 return BnCameraService::onTransact(code, data, reply, flags);
328}
329
330// The reason we need this busy bit is a new CameraService::connect() request
331// may come in while the previous Client's destructor has not been run or is
332// still running. If the last strong reference of the previous Client is gone
333// but the destructor has not been finished, we should not allow the new Client
334// to be created because we need to wait for the previous Client to tear down
335// the hardware first.
336void CameraService::setCameraBusy(int cameraId) {
337 android_atomic_write(1, &mBusy[cameraId]);
338}
339
340void CameraService::setCameraFree(int cameraId) {
341 android_atomic_write(0, &mBusy[cameraId]);
342}
343
344// We share the media players for shutter and recording sound for all clients.
345// A reference count is kept to determine when we will actually release the
346// media players.
347
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800348MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349 MediaPlayer* mp = new MediaPlayer();
350 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800351 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352 mp->prepare();
353 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000354 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355 return NULL;
356 }
357 return mp;
358}
359
360void CameraService::loadSound() {
361 Mutex::Autolock lock(mSoundLock);
362 LOG1("CameraService::loadSound ref=%d", mSoundRef);
363 if (mSoundRef++) return;
364
365 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
366 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
367}
368
369void CameraService::releaseSound() {
370 Mutex::Autolock lock(mSoundLock);
371 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
372 if (--mSoundRef) return;
373
374 for (int i = 0; i < NUM_SOUNDS; i++) {
375 if (mSoundPlayer[i] != 0) {
376 mSoundPlayer[i]->disconnect();
377 mSoundPlayer[i].clear();
378 }
379 }
380}
381
382void CameraService::playSound(sound_kind kind) {
383 LOG1("playSound(%d)", kind);
384 Mutex::Autolock lock(mSoundLock);
385 sp<MediaPlayer> player = mSoundPlayer[kind];
386 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800387 player->seekTo(0);
388 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700389 }
390}
391
392// ----------------------------------------------------------------------------
393
394CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700395 const sp<ICameraClient>& cameraClient,
396 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800397 int cameraId, int cameraFacing, int clientPid, bool keep) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700398 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800399 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700400
401 mCameraService = cameraService;
402 mCameraClient = cameraClient;
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700403 mHardware = hardware;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700404 mCameraId = cameraId;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800405 mCameraFacing = cameraFacing;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700406 mClientPid = clientPid;
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800407 mKeep = keep;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700408 mMsgEnabled = 0;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800409 mSurface = 0;
410 mPreviewWindow = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700411 mHardware->setCallbacks(notifyCallback,
412 dataCallback,
413 dataCallbackTimestamp,
414 (void *)cameraId);
415
Wu-cheng Li57c86182011-07-30 05:00:37 +0800416 // Enable zoom, error, focus, and metadata messages by default
417 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
Wu-cheng Lid6205062011-11-14 20:30:14 +0800418 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700419
420 // Callback is disabled by default
Iliyan Malchev9e626522011-04-14 16:51:21 -0700421 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800422 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700423 mPlayShutterSound = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700424 cameraService->setCameraBusy(cameraId);
425 cameraService->loadSound();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800426 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700427}
428
Mathias Agopian65ab4712010-07-14 17:59:35 -0700429// tear down the client
430CameraService::Client::~Client() {
431 int callingPid = getCallingPid();
432 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
433
Mathias Agopian65ab4712010-07-14 17:59:35 -0700434 // set mClientPid to let disconnet() tear down the hardware
435 mClientPid = callingPid;
436 disconnect();
437 mCameraService->releaseSound();
438 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
439}
440
441// ----------------------------------------------------------------------------
442
443status_t CameraService::Client::checkPid() const {
444 int callingPid = getCallingPid();
445 if (callingPid == mClientPid) return NO_ERROR;
446
Steve Block5ff1dd52012-01-05 23:22:43 +0000447 ALOGW("attempt to use a locked camera from a different process"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700448 " (old pid %d, new pid %d)", mClientPid, callingPid);
449 return EBUSY;
450}
451
452status_t CameraService::Client::checkPidAndHardware() const {
453 status_t result = checkPid();
454 if (result != NO_ERROR) return result;
455 if (mHardware == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000456 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700457 return INVALID_OPERATION;
458 }
459 return NO_ERROR;
460}
461
462status_t CameraService::Client::lock() {
463 int callingPid = getCallingPid();
464 LOG1("lock (pid %d)", callingPid);
465 Mutex::Autolock lock(mLock);
466
467 // lock camera to this client if the the camera is unlocked
468 if (mClientPid == 0) {
469 mClientPid = callingPid;
470 return NO_ERROR;
471 }
472
473 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
474 return checkPid();
475}
476
477status_t CameraService::Client::unlock() {
478 int callingPid = getCallingPid();
479 LOG1("unlock (pid %d)", callingPid);
480 Mutex::Autolock lock(mLock);
481
482 // allow anyone to use camera (after they lock the camera)
483 status_t result = checkPid();
484 if (result == NO_ERROR) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800485 if (mHardware->recordingEnabled()) {
Steve Block29357bc2012-01-06 19:20:56 +0000486 ALOGE("Not allowed to unlock camera during recording.");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800487 return INVALID_OPERATION;
488 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700489 mClientPid = 0;
490 LOG1("clear mCameraClient (pid %d)", callingPid);
491 // we need to remove the reference to ICameraClient so that when the app
492 // goes away, the reference count goes to 0.
493 mCameraClient.clear();
494 }
495 return result;
496}
497
498// connect a new client to the camera
499status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
500 int callingPid = getCallingPid();
501 LOG1("connect E (pid %d)", callingPid);
502 Mutex::Autolock lock(mLock);
503
504 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000505 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700506 mClientPid, callingPid);
507 return EBUSY;
508 }
509
510 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
511 LOG1("Connect to the same client");
512 return NO_ERROR;
513 }
514
Iliyan Malchev9e626522011-04-14 16:51:21 -0700515 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700516 mClientPid = callingPid;
517 mCameraClient = client;
518
519 LOG1("connect X (pid %d)", callingPid);
520 return NO_ERROR;
521}
522
Wu-cheng Li7574da52011-07-20 05:35:02 +0800523static void disconnectWindow(const sp<ANativeWindow>& window) {
524 if (window != 0) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700525 status_t result = native_window_api_disconnect(window.get(),
Wu-cheng Li7574da52011-07-20 05:35:02 +0800526 NATIVE_WINDOW_API_CAMERA);
527 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000528 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
Wu-cheng Li7574da52011-07-20 05:35:02 +0800529 result);
530 }
531 }
532}
533
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534void CameraService::Client::disconnect() {
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800535 disconnectInternal(true);
536}
537
538void CameraService::Client::disconnectInternal(bool needCheckPid) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700539 int callingPid = getCallingPid();
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800540 LOG1("disconnectInternal E (pid %d)", callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700541 Mutex::Autolock lock(mLock);
542
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800543 if (needCheckPid) {
544 if (checkPid() != NO_ERROR) {
545 ALOGW("different client - don't disconnect");
546 return;
547 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800549 if (mClientPid <= 0) {
550 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
551 return;
552 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700553 }
554
555 // Make sure disconnect() is done once and once only, whether it is called
556 // from the user directly, or called by the destructor.
557 if (mHardware == 0) return;
558
559 LOG1("hardware teardown");
560 // Before destroying mHardware, we must make sure it's in the
561 // idle state.
562 // Turn off all messages.
563 disableMsgType(CAMERA_MSG_ALL_MSGS);
564 mHardware->stopPreview();
565 mHardware->cancelPicture();
566 // Release the hardware resources.
567 mHardware->release();
Mathias Agopian03dfce92010-12-07 19:38:17 -0800568
Jamie Gennis4b791682010-08-10 16:37:53 -0700569 // Release the held ANativeWindow resources.
570 if (mPreviewWindow != 0) {
Wu-cheng Li7574da52011-07-20 05:35:02 +0800571 disconnectWindow(mPreviewWindow);
Jamie Gennis4b791682010-08-10 16:37:53 -0700572 mPreviewWindow = 0;
573 mHardware->setPreviewWindow(mPreviewWindow);
574 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700575 mHardware.clear();
576
577 mCameraService->removeClient(mCameraClient);
578 mCameraService->setCameraFree(mCameraId);
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800579 mReleaseCondition.signal();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700580
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800581 LOG1("disconnectInternal X (pid %d)", callingPid);
582}
583
584void CameraService::Client::waitRelease(int ms) {
585 Mutex::Autolock lock(mLock);
586 if (mHardware != 0) {
587 mReleaseCondition.waitRelative(mLock, ms * 1000000);
588 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700589}
590
591// ----------------------------------------------------------------------------
592
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700593status_t CameraService::Client::setPreviewWindow(const sp<IBinder>& binder,
594 const sp<ANativeWindow>& window) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700595 Mutex::Autolock lock(mLock);
596 status_t result = checkPidAndHardware();
597 if (result != NO_ERROR) return result;
598
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 // return if no change in surface.
Mathias Agopian8b1027d2011-04-05 15:44:20 -0700600 if (binder == mSurface) {
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700601 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700602 }
603
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700604 if (window != 0) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700605 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700606 if (result != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000607 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700608 result);
609 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700610 }
611 }
612
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700613 // If preview has been already started, register preview buffers now.
614 if (mHardware->previewEnabled()) {
615 if (window != 0) {
Mathias Agopian9bc7af12011-07-18 16:15:08 -0700616 native_window_set_scaling_mode(window.get(),
617 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700618 native_window_set_buffers_transform(window.get(), mOrientation);
619 result = mHardware->setPreviewWindow(window);
620 }
621 }
622
623 if (result == NO_ERROR) {
624 // Everything has succeeded. Disconnect the old window and remember the
625 // new window.
626 disconnectWindow(mPreviewWindow);
627 mSurface = binder;
628 mPreviewWindow = window;
629 } else {
630 // Something went wrong after we connected to the new window, so
631 // disconnect here.
632 disconnectWindow(window);
633 }
634
Mathias Agopian65ab4712010-07-14 17:59:35 -0700635 return result;
636}
637
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700638// set the Surface that the preview will use
639status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
640 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
641
642 sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
643 sp<ANativeWindow> window(surface);
644 return setPreviewWindow(binder, window);
645}
646
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800647// set the SurfaceTexture that the preview will use
648status_t CameraService::Client::setPreviewTexture(
649 const sp<ISurfaceTexture>& surfaceTexture) {
650 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
651 getCallingPid());
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800652
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700653 sp<IBinder> binder;
654 sp<ANativeWindow> window;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800655 if (surfaceTexture != 0) {
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700656 binder = surfaceTexture->asBinder();
657 window = new SurfaceTextureClient(surfaceTexture);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800658 }
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700659 return setPreviewWindow(binder, window);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800660}
661
Mathias Agopian65ab4712010-07-14 17:59:35 -0700662// set the preview callback flag to affect how the received frames from
663// preview are handled.
664void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
665 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
666 Mutex::Autolock lock(mLock);
667 if (checkPidAndHardware() != NO_ERROR) return;
668
669 mPreviewCallbackFlag = callback_flag;
Iliyan Malchev9e626522011-04-14 16:51:21 -0700670 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
Wu-cheng Li0667de72010-09-03 16:40:32 -0700671 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
672 } else {
673 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700674 }
675}
676
677// start preview mode
678status_t CameraService::Client::startPreview() {
679 LOG1("startPreview (pid %d)", getCallingPid());
680 return startCameraMode(CAMERA_PREVIEW_MODE);
681}
682
683// start recording mode
684status_t CameraService::Client::startRecording() {
685 LOG1("startRecording (pid %d)", getCallingPid());
686 return startCameraMode(CAMERA_RECORDING_MODE);
687}
688
689// start preview or recording
690status_t CameraService::Client::startCameraMode(camera_mode mode) {
691 LOG1("startCameraMode(%d)", mode);
692 Mutex::Autolock lock(mLock);
693 status_t result = checkPidAndHardware();
694 if (result != NO_ERROR) return result;
695
696 switch(mode) {
697 case CAMERA_PREVIEW_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700698 if (mSurface == 0 && mPreviewWindow == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700699 LOG1("mSurface is not set yet.");
700 // still able to start preview in this case.
701 }
702 return startPreviewMode();
703 case CAMERA_RECORDING_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700704 if (mSurface == 0 && mPreviewWindow == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000705 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700706 return INVALID_OPERATION;
707 }
708 return startRecordingMode();
709 default:
710 return UNKNOWN_ERROR;
711 }
712}
713
714status_t CameraService::Client::startPreviewMode() {
715 LOG1("startPreviewMode");
716 status_t result = NO_ERROR;
717
718 // if preview has been enabled, nothing needs to be done
719 if (mHardware->previewEnabled()) {
720 return NO_ERROR;
721 }
722
Mathias Agopian03dfce92010-12-07 19:38:17 -0800723 if (mPreviewWindow != 0) {
Mathias Agopian9bc7af12011-07-18 16:15:08 -0700724 native_window_set_scaling_mode(mPreviewWindow.get(),
725 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian03dfce92010-12-07 19:38:17 -0800726 native_window_set_buffers_transform(mPreviewWindow.get(),
727 mOrientation);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700728 }
Mathias Agopian03dfce92010-12-07 19:38:17 -0800729 mHardware->setPreviewWindow(mPreviewWindow);
730 result = mHardware->startPreview();
731
Mathias Agopian65ab4712010-07-14 17:59:35 -0700732 return result;
733}
734
735status_t CameraService::Client::startRecordingMode() {
736 LOG1("startRecordingMode");
737 status_t result = NO_ERROR;
738
739 // if recording has been enabled, nothing needs to be done
740 if (mHardware->recordingEnabled()) {
741 return NO_ERROR;
742 }
743
744 // if preview has not been started, start preview first
745 if (!mHardware->previewEnabled()) {
746 result = startPreviewMode();
747 if (result != NO_ERROR) {
748 return result;
749 }
750 }
751
752 // start recording mode
753 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
754 mCameraService->playSound(SOUND_RECORDING);
755 result = mHardware->startRecording();
756 if (result != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000757 ALOGE("mHardware->startRecording() failed with status %d", result);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700758 }
759 return result;
760}
761
762// stop preview mode
763void CameraService::Client::stopPreview() {
764 LOG1("stopPreview (pid %d)", getCallingPid());
765 Mutex::Autolock lock(mLock);
766 if (checkPidAndHardware() != NO_ERROR) return;
767
Jamie Gennis4b791682010-08-10 16:37:53 -0700768
Mathias Agopian65ab4712010-07-14 17:59:35 -0700769 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
770 mHardware->stopPreview();
771
Mathias Agopian65ab4712010-07-14 17:59:35 -0700772 mPreviewBuffer.clear();
773}
774
775// stop recording mode
776void CameraService::Client::stopRecording() {
777 LOG1("stopRecording (pid %d)", getCallingPid());
778 Mutex::Autolock lock(mLock);
779 if (checkPidAndHardware() != NO_ERROR) return;
780
781 mCameraService->playSound(SOUND_RECORDING);
782 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
783 mHardware->stopRecording();
784
785 mPreviewBuffer.clear();
786}
787
788// release a recording frame
789void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
790 Mutex::Autolock lock(mLock);
791 if (checkPidAndHardware() != NO_ERROR) return;
792 mHardware->releaseRecordingFrame(mem);
793}
794
James Donge2ad6732010-10-18 20:42:51 -0700795status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
796{
797 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
798 Mutex::Autolock lock(mLock);
799 if (checkPidAndHardware() != NO_ERROR) {
800 return UNKNOWN_ERROR;
801 }
802 return mHardware->storeMetaDataInBuffers(enabled);
803}
804
Mathias Agopian65ab4712010-07-14 17:59:35 -0700805bool CameraService::Client::previewEnabled() {
806 LOG1("previewEnabled (pid %d)", getCallingPid());
807
808 Mutex::Autolock lock(mLock);
809 if (checkPidAndHardware() != NO_ERROR) return false;
810 return mHardware->previewEnabled();
811}
812
813bool CameraService::Client::recordingEnabled() {
814 LOG1("recordingEnabled (pid %d)", getCallingPid());
815
816 Mutex::Autolock lock(mLock);
817 if (checkPidAndHardware() != NO_ERROR) return false;
818 return mHardware->recordingEnabled();
819}
820
821status_t CameraService::Client::autoFocus() {
822 LOG1("autoFocus (pid %d)", getCallingPid());
823
824 Mutex::Autolock lock(mLock);
825 status_t result = checkPidAndHardware();
826 if (result != NO_ERROR) return result;
827
828 return mHardware->autoFocus();
829}
830
831status_t CameraService::Client::cancelAutoFocus() {
832 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
833
834 Mutex::Autolock lock(mLock);
835 status_t result = checkPidAndHardware();
836 if (result != NO_ERROR) return result;
837
838 return mHardware->cancelAutoFocus();
839}
840
841// take a picture - image is returned in callback
James Donge468ac52011-02-17 16:38:06 -0800842status_t CameraService::Client::takePicture(int msgType) {
843 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700844
845 Mutex::Autolock lock(mLock);
846 status_t result = checkPidAndHardware();
847 if (result != NO_ERROR) return result;
848
James Donge468ac52011-02-17 16:38:06 -0800849 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
850 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
Steve Block29357bc2012-01-06 19:20:56 +0000851 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
James Donge468ac52011-02-17 16:38:06 -0800852 " cannot be both enabled");
853 return BAD_VALUE;
854 }
855
856 // We only accept picture related message types
857 // and ignore other types of messages for takePicture().
858 int picMsgType = msgType
859 & (CAMERA_MSG_SHUTTER |
860 CAMERA_MSG_POSTVIEW_FRAME |
861 CAMERA_MSG_RAW_IMAGE |
862 CAMERA_MSG_RAW_IMAGE_NOTIFY |
863 CAMERA_MSG_COMPRESSED_IMAGE);
864
865 enableMsgType(picMsgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700866
867 return mHardware->takePicture();
868}
869
870// set preview/capture parameters - key/value pairs
871status_t CameraService::Client::setParameters(const String8& params) {
872 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
873
874 Mutex::Autolock lock(mLock);
875 status_t result = checkPidAndHardware();
876 if (result != NO_ERROR) return result;
877
878 CameraParameters p(params);
879 return mHardware->setParameters(p);
880}
881
882// get preview/capture parameters - key/value pairs
883String8 CameraService::Client::getParameters() const {
884 Mutex::Autolock lock(mLock);
885 if (checkPidAndHardware() != NO_ERROR) return String8();
886
887 String8 params(mHardware->getParameters().flatten());
888 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
889 return params;
890}
891
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700892// enable shutter sound
893status_t CameraService::Client::enableShutterSound(bool enable) {
894 LOG1("enableShutterSound (pid %d)", getCallingPid());
895
896 status_t result = checkPidAndHardware();
897 if (result != NO_ERROR) return result;
898
899 if (enable) {
900 mPlayShutterSound = true;
901 return OK;
902 }
903
904 // Disabling shutter sound may not be allowed. In that case only
905 // allow the mediaserver process to disable the sound.
906 char value[PROPERTY_VALUE_MAX];
907 property_get("ro.camera.sound.forced", value, "0");
908 if (strcmp(value, "0") != 0) {
909 // Disabling shutter sound is not allowed. Deny if the current
910 // process is not mediaserver.
911 if (getCallingPid() != getpid()) {
Steve Block29357bc2012-01-06 19:20:56 +0000912 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700913 return PERMISSION_DENIED;
914 }
915 }
916
917 mPlayShutterSound = false;
918 return OK;
919}
920
Mathias Agopian65ab4712010-07-14 17:59:35 -0700921status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
922 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700923 int orientation;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700924 Mutex::Autolock lock(mLock);
925 status_t result = checkPidAndHardware();
926 if (result != NO_ERROR) return result;
927
928 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800929 // Mirror the preview if the camera is front-facing.
930 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
931 if (orientation == -1) return BAD_VALUE;
932
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700933 if (mOrientation != orientation) {
934 mOrientation = orientation;
Wu-cheng Lib9f58862011-10-07 13:13:54 +0800935 if (mPreviewWindow != 0) {
936 native_window_set_buffers_transform(mPreviewWindow.get(),
937 mOrientation);
938 }
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700939 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700940 return OK;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700941 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
942 switch (arg1) {
943 case 0:
944 enableShutterSound(false);
945 break;
946 case 1:
947 enableShutterSound(true);
948 break;
949 default:
950 return BAD_VALUE;
951 }
952 return OK;
Nipun Kwatra3b7b3582010-09-14 16:49:08 -0700953 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
954 mCameraService->playSound(SOUND_RECORDING);
Wu-cheng Li2fd24402012-02-23 19:01:00 -0800955 } else if (cmd == CAMERA_CMD_PING) {
956 // If mHardware is 0, checkPidAndHardware will return error.
957 return OK;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700958 }
959
960 return mHardware->sendCommand(cmd, arg1, arg2);
961}
962
963// ----------------------------------------------------------------------------
964
965void CameraService::Client::enableMsgType(int32_t msgType) {
966 android_atomic_or(msgType, &mMsgEnabled);
967 mHardware->enableMsgType(msgType);
968}
969
970void CameraService::Client::disableMsgType(int32_t msgType) {
971 android_atomic_and(~msgType, &mMsgEnabled);
972 mHardware->disableMsgType(msgType);
973}
974
975#define CHECK_MESSAGE_INTERVAL 10 // 10ms
976bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
977 int sleepCount = 0;
978 while (mMsgEnabled & msgType) {
979 if (mLock.tryLock() == NO_ERROR) {
980 if (sleepCount > 0) {
981 LOG1("lockIfMessageWanted(%d): waited for %d ms",
982 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
983 }
984 return true;
985 }
986 if (sleepCount++ == 0) {
987 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
988 }
989 usleep(CHECK_MESSAGE_INTERVAL * 1000);
990 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000991 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992 return false;
993}
994
995// ----------------------------------------------------------------------------
996
997// Converts from a raw pointer to the client to a strong pointer during a
998// hardware callback. This requires the callbacks only happen when the client
999// is still alive.
1000sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
1001 sp<Client> client = gCameraService->getClientById((int) user);
1002
1003 // This could happen if the Client is in the process of shutting down (the
1004 // last strong reference is gone, but the destructor hasn't finished
1005 // stopping the hardware).
1006 if (client == 0) return NULL;
1007
1008 // The checks below are not necessary and are for debugging only.
1009 if (client->mCameraService.get() != gCameraService) {
Steve Block29357bc2012-01-06 19:20:56 +00001010 ALOGE("mismatch service!");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001011 return NULL;
1012 }
1013
1014 if (client->mHardware == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001015 ALOGE("mHardware == 0: callback after disconnect()?");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016 return NULL;
1017 }
1018
1019 return client;
1020}
1021
1022// Callback messages can be dispatched to internal handlers or pass to our
1023// client's callback functions, depending on the message type.
1024//
1025// notifyCallback:
1026// CAMERA_MSG_SHUTTER handleShutter
1027// (others) c->notifyCallback
1028// dataCallback:
1029// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
1030// CAMERA_MSG_POSTVIEW_FRAME handlePostview
1031// CAMERA_MSG_RAW_IMAGE handleRawPicture
1032// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
1033// (others) c->dataCallback
1034// dataCallbackTimestamp
1035// (others) c->dataCallbackTimestamp
1036//
1037// NOTE: the *Callback functions grab mLock of the client before passing
1038// control to handle* functions. So the handle* functions must release the
1039// lock before calling the ICameraClient's callbacks, so those callbacks can
1040// invoke methods in the Client class again (For example, the preview frame
1041// callback may want to releaseRecordingFrame). The handle* functions must
1042// release the lock after all accesses to member variables, so it must be
1043// handled very carefully.
1044
1045void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
1046 int32_t ext2, void* user) {
1047 LOG2("notifyCallback(%d)", msgType);
1048
1049 sp<Client> client = getClientFromCookie(user);
1050 if (client == 0) return;
1051 if (!client->lockIfMessageWanted(msgType)) return;
1052
1053 switch (msgType) {
1054 case CAMERA_MSG_SHUTTER:
1055 // ext1 is the dimension of the yuv picture.
Iliyan Malchev108dddf2011-03-28 16:10:12 -07001056 client->handleShutter();
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057 break;
1058 default:
1059 client->handleGenericNotify(msgType, ext1, ext2);
1060 break;
1061 }
1062}
1063
1064void CameraService::Client::dataCallback(int32_t msgType,
Wu-cheng Liff09ef82011-07-28 05:30:59 +08001065 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001066 LOG2("dataCallback(%d)", msgType);
1067
1068 sp<Client> client = getClientFromCookie(user);
1069 if (client == 0) return;
1070 if (!client->lockIfMessageWanted(msgType)) return;
1071
Wu-cheng Li57c86182011-07-30 05:00:37 +08001072 if (dataPtr == 0 && metadata == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +00001073 ALOGE("Null data returned in data callback");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1075 return;
1076 }
1077
Wu-cheng Li57c86182011-07-30 05:00:37 +08001078 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 case CAMERA_MSG_PREVIEW_FRAME:
Wu-cheng Li57c86182011-07-30 05:00:37 +08001080 client->handlePreviewData(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001081 break;
1082 case CAMERA_MSG_POSTVIEW_FRAME:
1083 client->handlePostview(dataPtr);
1084 break;
1085 case CAMERA_MSG_RAW_IMAGE:
1086 client->handleRawPicture(dataPtr);
1087 break;
1088 case CAMERA_MSG_COMPRESSED_IMAGE:
1089 client->handleCompressedPicture(dataPtr);
1090 break;
1091 default:
Wu-cheng Li57c86182011-07-30 05:00:37 +08001092 client->handleGenericData(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001093 break;
1094 }
1095}
1096
1097void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
1098 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
1099 LOG2("dataCallbackTimestamp(%d)", msgType);
1100
1101 sp<Client> client = getClientFromCookie(user);
1102 if (client == 0) return;
James Dong986ef2a2010-12-09 11:08:14 -08001103 if (!client->lockIfMessageWanted(msgType)) return;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104
1105 if (dataPtr == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001106 ALOGE("Null data returned in data with timestamp callback");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001107 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1108 return;
1109 }
1110
1111 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1112}
1113
1114// snapshot taken callback
Iliyan Malchev108dddf2011-03-28 16:10:12 -07001115void CameraService::Client::handleShutter(void) {
Nipun Kwatrab5ca4612010-09-11 19:31:10 -07001116 if (mPlayShutterSound) {
1117 mCameraService->playSound(SOUND_SHUTTER);
1118 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120 sp<ICameraClient> c = mCameraClient;
1121 if (c != 0) {
1122 mLock.unlock();
1123 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1124 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1125 }
1126 disableMsgType(CAMERA_MSG_SHUTTER);
1127
Mathias Agopian65ab4712010-07-14 17:59:35 -07001128 mLock.unlock();
1129}
1130
1131// preview callback - frame buffer update
Wu-cheng Li57c86182011-07-30 05:00:37 +08001132void CameraService::Client::handlePreviewData(int32_t msgType,
1133 const sp<IMemory>& mem,
1134 camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001135 ssize_t offset;
1136 size_t size;
1137 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1138
Mathias Agopian65ab4712010-07-14 17:59:35 -07001139 // local copy of the callback flags
1140 int flags = mPreviewCallbackFlag;
1141
1142 // is callback enabled?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001143 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001144 // If the enable bit is off, the copy-out and one-shot bits are ignored
1145 LOG2("frame callback is disabled");
1146 mLock.unlock();
1147 return;
1148 }
1149
1150 // hold a strong pointer to the client
1151 sp<ICameraClient> c = mCameraClient;
1152
1153 // clear callback flags if no client or one-shot mode
Iliyan Malchev9e626522011-04-14 16:51:21 -07001154 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 LOG2("Disable preview callback");
Iliyan Malchev9e626522011-04-14 16:51:21 -07001156 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1157 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1158 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Li0667de72010-09-03 16:40:32 -07001159 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001160 }
1161
1162 if (c != 0) {
1163 // Is the received frame copied out or not?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001164 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 LOG2("frame is copied");
Wu-cheng Li57c86182011-07-30 05:00:37 +08001166 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001167 } else {
1168 LOG2("frame is forwarded");
1169 mLock.unlock();
Wu-cheng Li57c86182011-07-30 05:00:37 +08001170 c->dataCallback(msgType, mem, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001171 }
1172 } else {
1173 mLock.unlock();
1174 }
1175}
1176
1177// picture callback - postview image ready
1178void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1179 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1180
1181 sp<ICameraClient> c = mCameraClient;
1182 mLock.unlock();
1183 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001184 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001185 }
1186}
1187
1188// picture callback - raw image ready
1189void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1190 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1191
1192 ssize_t offset;
1193 size_t size;
1194 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1195
Mathias Agopian65ab4712010-07-14 17:59:35 -07001196 sp<ICameraClient> c = mCameraClient;
1197 mLock.unlock();
1198 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001199 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001200 }
1201}
1202
1203// picture callback - compressed picture ready
1204void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1205 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1206
1207 sp<ICameraClient> c = mCameraClient;
1208 mLock.unlock();
1209 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001210 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001211 }
1212}
1213
1214
1215void CameraService::Client::handleGenericNotify(int32_t msgType,
1216 int32_t ext1, int32_t ext2) {
1217 sp<ICameraClient> c = mCameraClient;
1218 mLock.unlock();
1219 if (c != 0) {
1220 c->notifyCallback(msgType, ext1, ext2);
1221 }
1222}
1223
1224void CameraService::Client::handleGenericData(int32_t msgType,
Wu-cheng Li57c86182011-07-30 05:00:37 +08001225 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001226 sp<ICameraClient> c = mCameraClient;
1227 mLock.unlock();
1228 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001229 c->dataCallback(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001230 }
1231}
1232
1233void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1234 int32_t msgType, const sp<IMemory>& dataPtr) {
1235 sp<ICameraClient> c = mCameraClient;
1236 mLock.unlock();
1237 if (c != 0) {
1238 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1239 }
1240}
1241
1242void CameraService::Client::copyFrameAndPostCopiedFrame(
Wu-cheng Li57c86182011-07-30 05:00:37 +08001243 int32_t msgType, const sp<ICameraClient>& client,
1244 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1245 camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001246 LOG2("copyFrameAndPostCopiedFrame");
1247 // It is necessary to copy out of pmem before sending this to
1248 // the callback. For efficiency, reuse the same MemoryHeapBase
1249 // provided it's big enough. Don't allocate the memory or
1250 // perform the copy if there's no callback.
1251 // hold the preview lock while we grab a reference to the preview buffer
1252 sp<MemoryHeapBase> previewBuffer;
1253
1254 if (mPreviewBuffer == 0) {
1255 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1256 } else if (size > mPreviewBuffer->virtualSize()) {
1257 mPreviewBuffer.clear();
1258 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1259 }
1260 if (mPreviewBuffer == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001261 ALOGE("failed to allocate space for preview buffer");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001262 mLock.unlock();
1263 return;
1264 }
1265 previewBuffer = mPreviewBuffer;
1266
1267 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1268
1269 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1270 if (frame == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001271 ALOGE("failed to allocate space for frame callback");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001272 mLock.unlock();
1273 return;
1274 }
1275
1276 mLock.unlock();
Wu-cheng Li57c86182011-07-30 05:00:37 +08001277 client->dataCallback(msgType, frame, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001278}
1279
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001280int CameraService::Client::getOrientation(int degrees, bool mirror) {
1281 if (!mirror) {
1282 if (degrees == 0) return 0;
1283 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1284 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1285 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1286 } else { // Do mirror (horizontal flip)
1287 if (degrees == 0) { // FLIP_H and ROT_0
1288 return HAL_TRANSFORM_FLIP_H;
1289 } else if (degrees == 90) { // FLIP_H and ROT_90
1290 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1291 } else if (degrees == 180) { // FLIP_H and ROT_180
1292 return HAL_TRANSFORM_FLIP_V;
1293 } else if (degrees == 270) { // FLIP_H and ROT_270
1294 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1295 }
1296 }
Steve Block29357bc2012-01-06 19:20:56 +00001297 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001298 return -1;
1299}
1300
Wu-cheng Li2fd24402012-02-23 19:01:00 -08001301// Whether the client wants to keep the camera from taking
1302bool CameraService::Client::keep() const {
1303 return mKeep;
1304}
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001305
Mathias Agopian65ab4712010-07-14 17:59:35 -07001306// ----------------------------------------------------------------------------
1307
1308static const int kDumpLockRetries = 50;
1309static const int kDumpLockSleep = 60000;
1310
1311static bool tryLock(Mutex& mutex)
1312{
1313 bool locked = false;
1314 for (int i = 0; i < kDumpLockRetries; ++i) {
1315 if (mutex.tryLock() == NO_ERROR) {
1316 locked = true;
1317 break;
1318 }
1319 usleep(kDumpLockSleep);
1320 }
1321 return locked;
1322}
1323
1324status_t CameraService::dump(int fd, const Vector<String16>& args) {
1325 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1326
1327 const size_t SIZE = 256;
1328 char buffer[SIZE];
1329 String8 result;
1330 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1331 snprintf(buffer, SIZE, "Permission Denial: "
1332 "can't dump CameraService from pid=%d, uid=%d\n",
1333 getCallingPid(),
1334 getCallingUid());
1335 result.append(buffer);
1336 write(fd, result.string(), result.size());
1337 } else {
1338 bool locked = tryLock(mServiceLock);
1339 // failed to lock - CameraService is probably deadlocked
1340 if (!locked) {
1341 String8 result(kDeadlockedString);
1342 write(fd, result.string(), result.size());
1343 }
1344
1345 bool hasClient = false;
1346 for (int i = 0; i < mNumberOfCameras; i++) {
1347 sp<Client> client = mClient[i].promote();
1348 if (client == 0) continue;
1349 hasClient = true;
1350 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1351 i,
1352 client->getCameraClient()->asBinder().get(),
1353 client->mClientPid);
1354 result.append(buffer);
1355 write(fd, result.string(), result.size());
1356 client->mHardware->dump(fd, args);
1357 }
1358 if (!hasClient) {
1359 result.append("No camera client yet.\n");
1360 write(fd, result.string(), result.size());
1361 }
1362
1363 if (locked) mServiceLock.unlock();
1364
1365 // change logging level
1366 int n = args.size();
1367 for (int i = 0; i + 1 < n; i++) {
1368 if (args[i] == String16("-v")) {
1369 String8 levelStr(args[i+1]);
1370 int level = atoi(levelStr.string());
1371 sprintf(buffer, "Set Log Level to %d", level);
1372 result.append(buffer);
1373 setLogLevel(level);
1374 }
1375 }
1376 }
1377 return NO_ERROR;
1378}
1379
Mathias Agopian65ab4712010-07-14 17:59:35 -07001380}; // namespace android