blob: 06fc708e053d4a78d3d7460ecc170e876aa204ee [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopian65ab4712010-07-14 17:59:35 -07004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "CameraService"
Iliyan Malchev8951a972011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
Mathias Agopian65ab4712010-07-14 17:59:35 -070020
21#include <stdio.h>
22#include <sys/types.h>
23#include <pthread.h>
24
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27#include <binder/MemoryBase.h>
28#include <binder/MemoryHeapBase.h>
29#include <cutils/atomic.h>
Nipun Kwatrab5ca4612010-09-11 19:31:10 -070030#include <cutils/properties.h>
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080031#include <gui/SurfaceTextureClient.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070032#include <hardware/hardware.h>
33#include <media/AudioSystem.h>
34#include <media/mediaplayer.h>
35#include <surfaceflinger/ISurface.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"
Iliyan Malchev8951a972011-04-14 16:55:59 -070041#include "CameraHardwareInterface.h"
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
43namespace android {
44
45// ----------------------------------------------------------------------------
46// Logging support -- this is for debugging only
47// Use "adb shell dumpsys media.camera -v 1" to change it.
48static volatile int32_t gLogLevel = 0;
49
Steve Blockb8a80522011-12-20 16:23:08 +000050#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
51#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Mathias Agopian65ab4712010-07-14 17:59:35 -070052
53static void setLogLevel(int level) {
54 android_atomic_write(level, &gLogLevel);
55}
56
57// ----------------------------------------------------------------------------
58
59static int getCallingPid() {
60 return IPCThreadState::self()->getCallingPid();
61}
62
63static int getCallingUid() {
64 return IPCThreadState::self()->getCallingUid();
65}
66
67// ----------------------------------------------------------------------------
68
69// This is ugly and only safe if we never re-create the CameraService, but
70// should be ok for now.
71static CameraService *gCameraService;
72
73CameraService::CameraService()
Iliyan Malchev8951a972011-04-14 16:55:59 -070074:mSoundRef(0), mModule(0)
Mathias Agopian65ab4712010-07-14 17:59:35 -070075{
Steve Blockdf64d152012-01-04 20:05:49 +000076 ALOGI("CameraService started (pid=%d)", getpid());
Mathias Agopian65ab4712010-07-14 17:59:35 -070077 gCameraService = this;
78}
79
Iliyan Malchev8951a972011-04-14 16:55:59 -070080void CameraService::onFirstRef()
81{
82 BnCameraService::onFirstRef();
83
84 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
85 (const hw_module_t **)&mModule) < 0) {
Steve Block29357bc2012-01-06 19:20:56 +000086 ALOGE("Could not load camera HAL module");
Iliyan Malchev8951a972011-04-14 16:55:59 -070087 mNumberOfCameras = 0;
88 }
89 else {
90 mNumberOfCameras = mModule->get_number_of_cameras();
91 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block29357bc2012-01-06 19:20:56 +000092 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malchev8951a972011-04-14 16:55:59 -070093 mNumberOfCameras, MAX_CAMERAS);
94 mNumberOfCameras = MAX_CAMERAS;
95 }
96 for (int i = 0; i < mNumberOfCameras; i++) {
97 setCameraFree(i);
98 }
99 }
100}
101
Mathias Agopian65ab4712010-07-14 17:59:35 -0700102CameraService::~CameraService() {
103 for (int i = 0; i < mNumberOfCameras; i++) {
104 if (mBusy[i]) {
Steve Block29357bc2012-01-06 19:20:56 +0000105 ALOGE("camera %d is still in use in destructor!", i);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106 }
107 }
108
109 gCameraService = NULL;
110}
111
112int32_t CameraService::getNumberOfCameras() {
113 return mNumberOfCameras;
114}
115
116status_t CameraService::getCameraInfo(int cameraId,
117 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700118 if (!mModule) {
119 return NO_INIT;
120 }
121
Mathias Agopian65ab4712010-07-14 17:59:35 -0700122 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
123 return BAD_VALUE;
124 }
125
Iliyan Malchev8951a972011-04-14 16:55:59 -0700126 struct camera_info info;
127 status_t rc = mModule->get_camera_info(cameraId, &info);
128 cameraInfo->facing = info.facing;
129 cameraInfo->orientation = info.orientation;
130 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700131}
132
133sp<ICamera> CameraService::connect(
134 const sp<ICameraClient>& cameraClient, int cameraId) {
135 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500136 sp<CameraHardwareInterface> hardware = NULL;
137
Mathias Agopian65ab4712010-07-14 17:59:35 -0700138 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
139
Iliyan Malchev8951a972011-04-14 16:55:59 -0700140 if (!mModule) {
Steve Block29357bc2012-01-06 19:20:56 +0000141 ALOGE("Camera HAL module not loaded");
Iliyan Malchev8951a972011-04-14 16:55:59 -0700142 return NULL;
143 }
144
Mathias Agopian65ab4712010-07-14 17:59:35 -0700145 sp<Client> client;
146 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block29357bc2012-01-06 19:20:56 +0000147 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148 callingPid, cameraId);
149 return NULL;
150 }
151
Wu-cheng Lia3355432011-05-20 14:54:25 +0800152 char value[PROPERTY_VALUE_MAX];
153 property_get("sys.secpolicy.camera.disabled", value, "0");
154 if (strcmp(value, "1") == 0) {
155 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000156 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800157 return NULL;
158 }
159
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 Mutex::Autolock lock(mServiceLock);
161 if (mClient[cameraId] != 0) {
162 client = mClient[cameraId].promote();
163 if (client != 0) {
164 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
165 LOG1("CameraService::connect X (pid %d) (the same client)",
166 callingPid);
167 return client;
168 } else {
Steve Block5ff1dd52012-01-05 23:22:43 +0000169 ALOGW("CameraService::connect X (pid %d) rejected (existing client).",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 callingPid);
171 return NULL;
172 }
173 }
174 mClient[cameraId].clear();
175 }
176
177 if (mBusy[cameraId]) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000178 ALOGW("CameraService::connect X (pid %d) rejected"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700179 " (camera %d is still busy).", callingPid, cameraId);
180 return NULL;
181 }
182
Iliyan Malchev8951a972011-04-14 16:55:59 -0700183 struct camera_info info;
184 if (mModule->get_camera_info(cameraId, &info) != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000185 ALOGE("Invalid camera id %d", cameraId);
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700186 return NULL;
187 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700188
189 char camera_device_name[10];
190 snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
191
Tyler Luu5861a9a2011-10-06 00:00:03 -0500192 hardware = new CameraHardwareInterface(camera_device_name);
193 if (hardware->initialize(&mModule->common) != OK) {
194 hardware.clear();
195 return NULL;
196 }
197
198 client = new Client(this, cameraClient, hardware, cameraId, info.facing, callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700199 mClient[cameraId] = client;
200 LOG1("CameraService::connect X");
201 return client;
202}
203
204void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
205 int callingPid = getCallingPid();
206 LOG1("CameraService::removeClient E (pid %d)", callingPid);
207
208 for (int i = 0; i < mNumberOfCameras; i++) {
209 // Declare this before the lock to make absolutely sure the
210 // destructor won't be called with the lock held.
211 sp<Client> client;
212
213 Mutex::Autolock lock(mServiceLock);
214
215 // This happens when we have already disconnected (or this is
216 // just another unused camera).
217 if (mClient[i] == 0) continue;
218
219 // Promote mClient. It can fail if we are called from this path:
220 // Client::~Client() -> disconnect() -> removeClient().
221 client = mClient[i].promote();
222
223 if (client == 0) {
224 mClient[i].clear();
225 continue;
226 }
227
228 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
229 // Found our camera, clear and leave.
230 LOG1("removeClient: clear camera %d", i);
231 mClient[i].clear();
232 break;
233 }
234 }
235
236 LOG1("CameraService::removeClient X (pid %d)", callingPid);
237}
238
239sp<CameraService::Client> CameraService::getClientById(int cameraId) {
240 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
241 return mClient[cameraId].promote();
242}
243
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244status_t CameraService::onTransact(
245 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
246 // Permission checks
247 switch (code) {
248 case BnCameraService::CONNECT:
249 const int pid = getCallingPid();
250 const int self_pid = getpid();
251 if (pid != self_pid) {
252 // we're called from a different process, do the real check
253 if (!checkCallingPermission(
254 String16("android.permission.CAMERA"))) {
255 const int uid = getCallingUid();
Steve Block29357bc2012-01-06 19:20:56 +0000256 ALOGE("Permission Denial: "
Mathias Agopian65ab4712010-07-14 17:59:35 -0700257 "can't use the camera pid=%d, uid=%d", pid, uid);
258 return PERMISSION_DENIED;
259 }
260 }
261 break;
262 }
263
264 return BnCameraService::onTransact(code, data, reply, flags);
265}
266
267// The reason we need this busy bit is a new CameraService::connect() request
268// may come in while the previous Client's destructor has not been run or is
269// still running. If the last strong reference of the previous Client is gone
270// but the destructor has not been finished, we should not allow the new Client
271// to be created because we need to wait for the previous Client to tear down
272// the hardware first.
273void CameraService::setCameraBusy(int cameraId) {
274 android_atomic_write(1, &mBusy[cameraId]);
275}
276
277void CameraService::setCameraFree(int cameraId) {
278 android_atomic_write(0, &mBusy[cameraId]);
279}
280
281// We share the media players for shutter and recording sound for all clients.
282// A reference count is kept to determine when we will actually release the
283// media players.
284
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800285MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700286 MediaPlayer* mp = new MediaPlayer();
287 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvala60a78ac2012-01-05 15:34:53 -0800288 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700289 mp->prepare();
290 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000291 ALOGE("Failed to load CameraService sounds: %s", file);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292 return NULL;
293 }
294 return mp;
295}
296
297void CameraService::loadSound() {
298 Mutex::Autolock lock(mSoundLock);
299 LOG1("CameraService::loadSound ref=%d", mSoundRef);
300 if (mSoundRef++) return;
301
302 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
303 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
304}
305
306void CameraService::releaseSound() {
307 Mutex::Autolock lock(mSoundLock);
308 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
309 if (--mSoundRef) return;
310
311 for (int i = 0; i < NUM_SOUNDS; i++) {
312 if (mSoundPlayer[i] != 0) {
313 mSoundPlayer[i]->disconnect();
314 mSoundPlayer[i].clear();
315 }
316 }
317}
318
319void CameraService::playSound(sound_kind kind) {
320 LOG1("playSound(%d)", kind);
321 Mutex::Autolock lock(mSoundLock);
322 sp<MediaPlayer> player = mSoundPlayer[kind];
323 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800324 player->seekTo(0);
325 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700326 }
327}
328
329// ----------------------------------------------------------------------------
330
331CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700332 const sp<ICameraClient>& cameraClient,
333 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800334 int cameraId, int cameraFacing, int clientPid) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 int callingPid = getCallingPid();
336 LOG1("Client::Client E (pid %d)", callingPid);
337
338 mCameraService = cameraService;
339 mCameraClient = cameraClient;
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700340 mHardware = hardware;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700341 mCameraId = cameraId;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800342 mCameraFacing = cameraFacing;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343 mClientPid = clientPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700344 mMsgEnabled = 0;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800345 mSurface = 0;
346 mPreviewWindow = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 mHardware->setCallbacks(notifyCallback,
348 dataCallback,
349 dataCallbackTimestamp,
350 (void *)cameraId);
351
Wu-cheng Li57c86182011-07-30 05:00:37 +0800352 // Enable zoom, error, focus, and metadata messages by default
353 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
Wu-cheng Lid6205062011-11-14 20:30:14 +0800354 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700355
356 // Callback is disabled by default
Iliyan Malchev9e626522011-04-14 16:51:21 -0700357 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800358 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700359 mPlayShutterSound = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 cameraService->setCameraBusy(cameraId);
361 cameraService->loadSound();
362 LOG1("Client::Client X (pid %d)", callingPid);
363}
364
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365// tear down the client
366CameraService::Client::~Client() {
367 int callingPid = getCallingPid();
368 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
369
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 // set mClientPid to let disconnet() tear down the hardware
371 mClientPid = callingPid;
372 disconnect();
373 mCameraService->releaseSound();
374 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
375}
376
377// ----------------------------------------------------------------------------
378
379status_t CameraService::Client::checkPid() const {
380 int callingPid = getCallingPid();
381 if (callingPid == mClientPid) return NO_ERROR;
382
Steve Block5ff1dd52012-01-05 23:22:43 +0000383 ALOGW("attempt to use a locked camera from a different process"
Mathias Agopian65ab4712010-07-14 17:59:35 -0700384 " (old pid %d, new pid %d)", mClientPid, callingPid);
385 return EBUSY;
386}
387
388status_t CameraService::Client::checkPidAndHardware() const {
389 status_t result = checkPid();
390 if (result != NO_ERROR) return result;
391 if (mHardware == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000392 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393 return INVALID_OPERATION;
394 }
395 return NO_ERROR;
396}
397
398status_t CameraService::Client::lock() {
399 int callingPid = getCallingPid();
400 LOG1("lock (pid %d)", callingPid);
401 Mutex::Autolock lock(mLock);
402
403 // lock camera to this client if the the camera is unlocked
404 if (mClientPid == 0) {
405 mClientPid = callingPid;
406 return NO_ERROR;
407 }
408
409 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
410 return checkPid();
411}
412
413status_t CameraService::Client::unlock() {
414 int callingPid = getCallingPid();
415 LOG1("unlock (pid %d)", callingPid);
416 Mutex::Autolock lock(mLock);
417
418 // allow anyone to use camera (after they lock the camera)
419 status_t result = checkPid();
420 if (result == NO_ERROR) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800421 if (mHardware->recordingEnabled()) {
Steve Block29357bc2012-01-06 19:20:56 +0000422 ALOGE("Not allowed to unlock camera during recording.");
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800423 return INVALID_OPERATION;
424 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700425 mClientPid = 0;
426 LOG1("clear mCameraClient (pid %d)", callingPid);
427 // we need to remove the reference to ICameraClient so that when the app
428 // goes away, the reference count goes to 0.
429 mCameraClient.clear();
430 }
431 return result;
432}
433
434// connect a new client to the camera
435status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
436 int callingPid = getCallingPid();
437 LOG1("connect E (pid %d)", callingPid);
438 Mutex::Autolock lock(mLock);
439
440 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000441 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
Mathias Agopian65ab4712010-07-14 17:59:35 -0700442 mClientPid, callingPid);
443 return EBUSY;
444 }
445
446 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
447 LOG1("Connect to the same client");
448 return NO_ERROR;
449 }
450
Iliyan Malchev9e626522011-04-14 16:51:21 -0700451 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700452 mClientPid = callingPid;
453 mCameraClient = client;
454
455 LOG1("connect X (pid %d)", callingPid);
456 return NO_ERROR;
457}
458
Wu-cheng Li7574da52011-07-20 05:35:02 +0800459static void disconnectWindow(const sp<ANativeWindow>& window) {
460 if (window != 0) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700461 status_t result = native_window_api_disconnect(window.get(),
Wu-cheng Li7574da52011-07-20 05:35:02 +0800462 NATIVE_WINDOW_API_CAMERA);
463 if (result != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000464 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
Wu-cheng Li7574da52011-07-20 05:35:02 +0800465 result);
466 }
467 }
468}
469
Mathias Agopian65ab4712010-07-14 17:59:35 -0700470void CameraService::Client::disconnect() {
471 int callingPid = getCallingPid();
472 LOG1("disconnect E (pid %d)", callingPid);
473 Mutex::Autolock lock(mLock);
474
475 if (checkPid() != NO_ERROR) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000476 ALOGW("different client - don't disconnect");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700477 return;
478 }
479
480 if (mClientPid <= 0) {
481 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
482 return;
483 }
484
485 // Make sure disconnect() is done once and once only, whether it is called
486 // from the user directly, or called by the destructor.
487 if (mHardware == 0) return;
488
489 LOG1("hardware teardown");
490 // Before destroying mHardware, we must make sure it's in the
491 // idle state.
492 // Turn off all messages.
493 disableMsgType(CAMERA_MSG_ALL_MSGS);
494 mHardware->stopPreview();
495 mHardware->cancelPicture();
496 // Release the hardware resources.
497 mHardware->release();
Mathias Agopian03dfce92010-12-07 19:38:17 -0800498
Jamie Gennis4b791682010-08-10 16:37:53 -0700499 // Release the held ANativeWindow resources.
500 if (mPreviewWindow != 0) {
Wu-cheng Li7574da52011-07-20 05:35:02 +0800501 disconnectWindow(mPreviewWindow);
Jamie Gennis4b791682010-08-10 16:37:53 -0700502 mPreviewWindow = 0;
503 mHardware->setPreviewWindow(mPreviewWindow);
504 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700505 mHardware.clear();
506
507 mCameraService->removeClient(mCameraClient);
508 mCameraService->setCameraFree(mCameraId);
509
510 LOG1("disconnect X (pid %d)", callingPid);
511}
512
513// ----------------------------------------------------------------------------
514
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700515status_t CameraService::Client::setPreviewWindow(const sp<IBinder>& binder,
516 const sp<ANativeWindow>& window) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700517 Mutex::Autolock lock(mLock);
518 status_t result = checkPidAndHardware();
519 if (result != NO_ERROR) return result;
520
Mathias Agopian65ab4712010-07-14 17:59:35 -0700521 // return if no change in surface.
Mathias Agopian8b1027d2011-04-05 15:44:20 -0700522 if (binder == mSurface) {
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700523 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700524 }
525
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700526 if (window != 0) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700527 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700528 if (result != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000529 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700530 result);
531 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700532 }
533 }
534
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700535 // If preview has been already started, register preview buffers now.
536 if (mHardware->previewEnabled()) {
537 if (window != 0) {
Mathias Agopian9bc7af12011-07-18 16:15:08 -0700538 native_window_set_scaling_mode(window.get(),
539 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700540 native_window_set_buffers_transform(window.get(), mOrientation);
541 result = mHardware->setPreviewWindow(window);
542 }
543 }
544
545 if (result == NO_ERROR) {
546 // Everything has succeeded. Disconnect the old window and remember the
547 // new window.
548 disconnectWindow(mPreviewWindow);
549 mSurface = binder;
550 mPreviewWindow = window;
551 } else {
552 // Something went wrong after we connected to the new window, so
553 // disconnect here.
554 disconnectWindow(window);
555 }
556
Mathias Agopian65ab4712010-07-14 17:59:35 -0700557 return result;
558}
559
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700560// set the Surface that the preview will use
561status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
562 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
563
564 sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
565 sp<ANativeWindow> window(surface);
566 return setPreviewWindow(binder, window);
567}
568
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800569// set the SurfaceTexture that the preview will use
570status_t CameraService::Client::setPreviewTexture(
571 const sp<ISurfaceTexture>& surfaceTexture) {
572 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
573 getCallingPid());
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800574
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700575 sp<IBinder> binder;
576 sp<ANativeWindow> window;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800577 if (surfaceTexture != 0) {
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700578 binder = surfaceTexture->asBinder();
579 window = new SurfaceTextureClient(surfaceTexture);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800580 }
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700581 return setPreviewWindow(binder, window);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800582}
583
Mathias Agopian65ab4712010-07-14 17:59:35 -0700584// set the preview callback flag to affect how the received frames from
585// preview are handled.
586void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
587 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
588 Mutex::Autolock lock(mLock);
589 if (checkPidAndHardware() != NO_ERROR) return;
590
591 mPreviewCallbackFlag = callback_flag;
Iliyan Malchev9e626522011-04-14 16:51:21 -0700592 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
Wu-cheng Li0667de72010-09-03 16:40:32 -0700593 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
594 } else {
595 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 }
597}
598
599// start preview mode
600status_t CameraService::Client::startPreview() {
601 LOG1("startPreview (pid %d)", getCallingPid());
602 return startCameraMode(CAMERA_PREVIEW_MODE);
603}
604
605// start recording mode
606status_t CameraService::Client::startRecording() {
607 LOG1("startRecording (pid %d)", getCallingPid());
608 return startCameraMode(CAMERA_RECORDING_MODE);
609}
610
611// start preview or recording
612status_t CameraService::Client::startCameraMode(camera_mode mode) {
613 LOG1("startCameraMode(%d)", mode);
614 Mutex::Autolock lock(mLock);
615 status_t result = checkPidAndHardware();
616 if (result != NO_ERROR) return result;
617
618 switch(mode) {
619 case CAMERA_PREVIEW_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700620 if (mSurface == 0 && mPreviewWindow == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700621 LOG1("mSurface is not set yet.");
622 // still able to start preview in this case.
623 }
624 return startPreviewMode();
625 case CAMERA_RECORDING_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700626 if (mSurface == 0 && mPreviewWindow == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000627 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700628 return INVALID_OPERATION;
629 }
630 return startRecordingMode();
631 default:
632 return UNKNOWN_ERROR;
633 }
634}
635
636status_t CameraService::Client::startPreviewMode() {
637 LOG1("startPreviewMode");
638 status_t result = NO_ERROR;
639
640 // if preview has been enabled, nothing needs to be done
641 if (mHardware->previewEnabled()) {
642 return NO_ERROR;
643 }
644
Mathias Agopian03dfce92010-12-07 19:38:17 -0800645 if (mPreviewWindow != 0) {
Mathias Agopian9bc7af12011-07-18 16:15:08 -0700646 native_window_set_scaling_mode(mPreviewWindow.get(),
647 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian03dfce92010-12-07 19:38:17 -0800648 native_window_set_buffers_transform(mPreviewWindow.get(),
649 mOrientation);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700650 }
Mathias Agopian03dfce92010-12-07 19:38:17 -0800651 mHardware->setPreviewWindow(mPreviewWindow);
652 result = mHardware->startPreview();
653
Mathias Agopian65ab4712010-07-14 17:59:35 -0700654 return result;
655}
656
657status_t CameraService::Client::startRecordingMode() {
658 LOG1("startRecordingMode");
659 status_t result = NO_ERROR;
660
661 // if recording has been enabled, nothing needs to be done
662 if (mHardware->recordingEnabled()) {
663 return NO_ERROR;
664 }
665
666 // if preview has not been started, start preview first
667 if (!mHardware->previewEnabled()) {
668 result = startPreviewMode();
669 if (result != NO_ERROR) {
670 return result;
671 }
672 }
673
674 // start recording mode
675 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
676 mCameraService->playSound(SOUND_RECORDING);
677 result = mHardware->startRecording();
678 if (result != NO_ERROR) {
Steve Block29357bc2012-01-06 19:20:56 +0000679 ALOGE("mHardware->startRecording() failed with status %d", result);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700680 }
681 return result;
682}
683
684// stop preview mode
685void CameraService::Client::stopPreview() {
686 LOG1("stopPreview (pid %d)", getCallingPid());
687 Mutex::Autolock lock(mLock);
688 if (checkPidAndHardware() != NO_ERROR) return;
689
Jamie Gennis4b791682010-08-10 16:37:53 -0700690
Mathias Agopian65ab4712010-07-14 17:59:35 -0700691 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
692 mHardware->stopPreview();
693
Mathias Agopian65ab4712010-07-14 17:59:35 -0700694 mPreviewBuffer.clear();
695}
696
697// stop recording mode
698void CameraService::Client::stopRecording() {
699 LOG1("stopRecording (pid %d)", getCallingPid());
700 Mutex::Autolock lock(mLock);
701 if (checkPidAndHardware() != NO_ERROR) return;
702
703 mCameraService->playSound(SOUND_RECORDING);
704 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
705 mHardware->stopRecording();
706
707 mPreviewBuffer.clear();
708}
709
710// release a recording frame
711void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
712 Mutex::Autolock lock(mLock);
713 if (checkPidAndHardware() != NO_ERROR) return;
714 mHardware->releaseRecordingFrame(mem);
715}
716
James Donge2ad6732010-10-18 20:42:51 -0700717status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
718{
719 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
720 Mutex::Autolock lock(mLock);
721 if (checkPidAndHardware() != NO_ERROR) {
722 return UNKNOWN_ERROR;
723 }
724 return mHardware->storeMetaDataInBuffers(enabled);
725}
726
Mathias Agopian65ab4712010-07-14 17:59:35 -0700727bool CameraService::Client::previewEnabled() {
728 LOG1("previewEnabled (pid %d)", getCallingPid());
729
730 Mutex::Autolock lock(mLock);
731 if (checkPidAndHardware() != NO_ERROR) return false;
732 return mHardware->previewEnabled();
733}
734
735bool CameraService::Client::recordingEnabled() {
736 LOG1("recordingEnabled (pid %d)", getCallingPid());
737
738 Mutex::Autolock lock(mLock);
739 if (checkPidAndHardware() != NO_ERROR) return false;
740 return mHardware->recordingEnabled();
741}
742
743status_t CameraService::Client::autoFocus() {
744 LOG1("autoFocus (pid %d)", getCallingPid());
745
746 Mutex::Autolock lock(mLock);
747 status_t result = checkPidAndHardware();
748 if (result != NO_ERROR) return result;
749
750 return mHardware->autoFocus();
751}
752
753status_t CameraService::Client::cancelAutoFocus() {
754 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
755
756 Mutex::Autolock lock(mLock);
757 status_t result = checkPidAndHardware();
758 if (result != NO_ERROR) return result;
759
760 return mHardware->cancelAutoFocus();
761}
762
763// take a picture - image is returned in callback
James Donge468ac52011-02-17 16:38:06 -0800764status_t CameraService::Client::takePicture(int msgType) {
765 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700766
767 Mutex::Autolock lock(mLock);
768 status_t result = checkPidAndHardware();
769 if (result != NO_ERROR) return result;
770
James Donge468ac52011-02-17 16:38:06 -0800771 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
772 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
Steve Block29357bc2012-01-06 19:20:56 +0000773 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
James Donge468ac52011-02-17 16:38:06 -0800774 " cannot be both enabled");
775 return BAD_VALUE;
776 }
777
778 // We only accept picture related message types
779 // and ignore other types of messages for takePicture().
780 int picMsgType = msgType
781 & (CAMERA_MSG_SHUTTER |
782 CAMERA_MSG_POSTVIEW_FRAME |
783 CAMERA_MSG_RAW_IMAGE |
784 CAMERA_MSG_RAW_IMAGE_NOTIFY |
785 CAMERA_MSG_COMPRESSED_IMAGE);
786
787 enableMsgType(picMsgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700788
789 return mHardware->takePicture();
790}
791
792// set preview/capture parameters - key/value pairs
793status_t CameraService::Client::setParameters(const String8& params) {
794 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
795
796 Mutex::Autolock lock(mLock);
797 status_t result = checkPidAndHardware();
798 if (result != NO_ERROR) return result;
799
800 CameraParameters p(params);
801 return mHardware->setParameters(p);
802}
803
804// get preview/capture parameters - key/value pairs
805String8 CameraService::Client::getParameters() const {
806 Mutex::Autolock lock(mLock);
807 if (checkPidAndHardware() != NO_ERROR) return String8();
808
809 String8 params(mHardware->getParameters().flatten());
810 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
811 return params;
812}
813
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700814// enable shutter sound
815status_t CameraService::Client::enableShutterSound(bool enable) {
816 LOG1("enableShutterSound (pid %d)", getCallingPid());
817
818 status_t result = checkPidAndHardware();
819 if (result != NO_ERROR) return result;
820
821 if (enable) {
822 mPlayShutterSound = true;
823 return OK;
824 }
825
826 // Disabling shutter sound may not be allowed. In that case only
827 // allow the mediaserver process to disable the sound.
828 char value[PROPERTY_VALUE_MAX];
829 property_get("ro.camera.sound.forced", value, "0");
830 if (strcmp(value, "0") != 0) {
831 // Disabling shutter sound is not allowed. Deny if the current
832 // process is not mediaserver.
833 if (getCallingPid() != getpid()) {
Steve Block29357bc2012-01-06 19:20:56 +0000834 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700835 return PERMISSION_DENIED;
836 }
837 }
838
839 mPlayShutterSound = false;
840 return OK;
841}
842
Mathias Agopian65ab4712010-07-14 17:59:35 -0700843status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
844 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700845 int orientation;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700846 Mutex::Autolock lock(mLock);
847 status_t result = checkPidAndHardware();
848 if (result != NO_ERROR) return result;
849
850 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800851 // Mirror the preview if the camera is front-facing.
852 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
853 if (orientation == -1) return BAD_VALUE;
854
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700855 if (mOrientation != orientation) {
856 mOrientation = orientation;
Wu-cheng Lib9f58862011-10-07 13:13:54 +0800857 if (mPreviewWindow != 0) {
858 native_window_set_buffers_transform(mPreviewWindow.get(),
859 mOrientation);
860 }
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700861 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 return OK;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700863 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
864 switch (arg1) {
865 case 0:
866 enableShutterSound(false);
867 break;
868 case 1:
869 enableShutterSound(true);
870 break;
871 default:
872 return BAD_VALUE;
873 }
874 return OK;
Nipun Kwatra3b7b3582010-09-14 16:49:08 -0700875 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
876 mCameraService->playSound(SOUND_RECORDING);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700877 }
878
879 return mHardware->sendCommand(cmd, arg1, arg2);
880}
881
882// ----------------------------------------------------------------------------
883
884void CameraService::Client::enableMsgType(int32_t msgType) {
885 android_atomic_or(msgType, &mMsgEnabled);
886 mHardware->enableMsgType(msgType);
887}
888
889void CameraService::Client::disableMsgType(int32_t msgType) {
890 android_atomic_and(~msgType, &mMsgEnabled);
891 mHardware->disableMsgType(msgType);
892}
893
894#define CHECK_MESSAGE_INTERVAL 10 // 10ms
895bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
896 int sleepCount = 0;
897 while (mMsgEnabled & msgType) {
898 if (mLock.tryLock() == NO_ERROR) {
899 if (sleepCount > 0) {
900 LOG1("lockIfMessageWanted(%d): waited for %d ms",
901 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
902 }
903 return true;
904 }
905 if (sleepCount++ == 0) {
906 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
907 }
908 usleep(CHECK_MESSAGE_INTERVAL * 1000);
909 }
Steve Block5ff1dd52012-01-05 23:22:43 +0000910 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700911 return false;
912}
913
914// ----------------------------------------------------------------------------
915
916// Converts from a raw pointer to the client to a strong pointer during a
917// hardware callback. This requires the callbacks only happen when the client
918// is still alive.
919sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
920 sp<Client> client = gCameraService->getClientById((int) user);
921
922 // This could happen if the Client is in the process of shutting down (the
923 // last strong reference is gone, but the destructor hasn't finished
924 // stopping the hardware).
925 if (client == 0) return NULL;
926
927 // The checks below are not necessary and are for debugging only.
928 if (client->mCameraService.get() != gCameraService) {
Steve Block29357bc2012-01-06 19:20:56 +0000929 ALOGE("mismatch service!");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700930 return NULL;
931 }
932
933 if (client->mHardware == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000934 ALOGE("mHardware == 0: callback after disconnect()?");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700935 return NULL;
936 }
937
938 return client;
939}
940
941// Callback messages can be dispatched to internal handlers or pass to our
942// client's callback functions, depending on the message type.
943//
944// notifyCallback:
945// CAMERA_MSG_SHUTTER handleShutter
946// (others) c->notifyCallback
947// dataCallback:
948// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
949// CAMERA_MSG_POSTVIEW_FRAME handlePostview
950// CAMERA_MSG_RAW_IMAGE handleRawPicture
951// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
952// (others) c->dataCallback
953// dataCallbackTimestamp
954// (others) c->dataCallbackTimestamp
955//
956// NOTE: the *Callback functions grab mLock of the client before passing
957// control to handle* functions. So the handle* functions must release the
958// lock before calling the ICameraClient's callbacks, so those callbacks can
959// invoke methods in the Client class again (For example, the preview frame
960// callback may want to releaseRecordingFrame). The handle* functions must
961// release the lock after all accesses to member variables, so it must be
962// handled very carefully.
963
964void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
965 int32_t ext2, void* user) {
966 LOG2("notifyCallback(%d)", msgType);
967
968 sp<Client> client = getClientFromCookie(user);
969 if (client == 0) return;
970 if (!client->lockIfMessageWanted(msgType)) return;
971
972 switch (msgType) {
973 case CAMERA_MSG_SHUTTER:
974 // ext1 is the dimension of the yuv picture.
Iliyan Malchev108dddf2011-03-28 16:10:12 -0700975 client->handleShutter();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700976 break;
977 default:
978 client->handleGenericNotify(msgType, ext1, ext2);
979 break;
980 }
981}
982
983void CameraService::Client::dataCallback(int32_t msgType,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800984 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700985 LOG2("dataCallback(%d)", msgType);
986
987 sp<Client> client = getClientFromCookie(user);
988 if (client == 0) return;
989 if (!client->lockIfMessageWanted(msgType)) return;
990
Wu-cheng Li57c86182011-07-30 05:00:37 +0800991 if (dataPtr == 0 && metadata == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000992 ALOGE("Null data returned in data callback");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700993 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
994 return;
995 }
996
Wu-cheng Li57c86182011-07-30 05:00:37 +0800997 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700998 case CAMERA_MSG_PREVIEW_FRAME:
Wu-cheng Li57c86182011-07-30 05:00:37 +0800999 client->handlePreviewData(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001000 break;
1001 case CAMERA_MSG_POSTVIEW_FRAME:
1002 client->handlePostview(dataPtr);
1003 break;
1004 case CAMERA_MSG_RAW_IMAGE:
1005 client->handleRawPicture(dataPtr);
1006 break;
1007 case CAMERA_MSG_COMPRESSED_IMAGE:
1008 client->handleCompressedPicture(dataPtr);
1009 break;
1010 default:
Wu-cheng Li57c86182011-07-30 05:00:37 +08001011 client->handleGenericData(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001012 break;
1013 }
1014}
1015
1016void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
1017 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
1018 LOG2("dataCallbackTimestamp(%d)", msgType);
1019
1020 sp<Client> client = getClientFromCookie(user);
1021 if (client == 0) return;
James Dong986ef2a2010-12-09 11:08:14 -08001022 if (!client->lockIfMessageWanted(msgType)) return;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001023
1024 if (dataPtr == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001025 ALOGE("Null data returned in data with timestamp callback");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001026 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1027 return;
1028 }
1029
1030 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1031}
1032
1033// snapshot taken callback
Iliyan Malchev108dddf2011-03-28 16:10:12 -07001034void CameraService::Client::handleShutter(void) {
Nipun Kwatrab5ca4612010-09-11 19:31:10 -07001035 if (mPlayShutterSound) {
1036 mCameraService->playSound(SOUND_SHUTTER);
1037 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001038
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039 sp<ICameraClient> c = mCameraClient;
1040 if (c != 0) {
1041 mLock.unlock();
1042 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1043 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1044 }
1045 disableMsgType(CAMERA_MSG_SHUTTER);
1046
Mathias Agopian65ab4712010-07-14 17:59:35 -07001047 mLock.unlock();
1048}
1049
1050// preview callback - frame buffer update
Wu-cheng Li57c86182011-07-30 05:00:37 +08001051void CameraService::Client::handlePreviewData(int32_t msgType,
1052 const sp<IMemory>& mem,
1053 camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054 ssize_t offset;
1055 size_t size;
1056 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1057
Mathias Agopian65ab4712010-07-14 17:59:35 -07001058 // local copy of the callback flags
1059 int flags = mPreviewCallbackFlag;
1060
1061 // is callback enabled?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001062 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 // If the enable bit is off, the copy-out and one-shot bits are ignored
1064 LOG2("frame callback is disabled");
1065 mLock.unlock();
1066 return;
1067 }
1068
1069 // hold a strong pointer to the client
1070 sp<ICameraClient> c = mCameraClient;
1071
1072 // clear callback flags if no client or one-shot mode
Iliyan Malchev9e626522011-04-14 16:51:21 -07001073 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 LOG2("Disable preview callback");
Iliyan Malchev9e626522011-04-14 16:51:21 -07001075 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1076 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1077 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Li0667de72010-09-03 16:40:32 -07001078 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 }
1080
1081 if (c != 0) {
1082 // Is the received frame copied out or not?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001083 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001084 LOG2("frame is copied");
Wu-cheng Li57c86182011-07-30 05:00:37 +08001085 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001086 } else {
1087 LOG2("frame is forwarded");
1088 mLock.unlock();
Wu-cheng Li57c86182011-07-30 05:00:37 +08001089 c->dataCallback(msgType, mem, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090 }
1091 } else {
1092 mLock.unlock();
1093 }
1094}
1095
1096// picture callback - postview image ready
1097void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1098 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1099
1100 sp<ICameraClient> c = mCameraClient;
1101 mLock.unlock();
1102 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001103 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001104 }
1105}
1106
1107// picture callback - raw image ready
1108void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1109 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1110
1111 ssize_t offset;
1112 size_t size;
1113 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1114
Mathias Agopian65ab4712010-07-14 17:59:35 -07001115 sp<ICameraClient> c = mCameraClient;
1116 mLock.unlock();
1117 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001118 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001119 }
1120}
1121
1122// picture callback - compressed picture ready
1123void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1124 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1125
1126 sp<ICameraClient> c = mCameraClient;
1127 mLock.unlock();
1128 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001129 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001130 }
1131}
1132
1133
1134void CameraService::Client::handleGenericNotify(int32_t msgType,
1135 int32_t ext1, int32_t ext2) {
1136 sp<ICameraClient> c = mCameraClient;
1137 mLock.unlock();
1138 if (c != 0) {
1139 c->notifyCallback(msgType, ext1, ext2);
1140 }
1141}
1142
1143void CameraService::Client::handleGenericData(int32_t msgType,
Wu-cheng Li57c86182011-07-30 05:00:37 +08001144 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001145 sp<ICameraClient> c = mCameraClient;
1146 mLock.unlock();
1147 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001148 c->dataCallback(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001149 }
1150}
1151
1152void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1153 int32_t msgType, const sp<IMemory>& dataPtr) {
1154 sp<ICameraClient> c = mCameraClient;
1155 mLock.unlock();
1156 if (c != 0) {
1157 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1158 }
1159}
1160
1161void CameraService::Client::copyFrameAndPostCopiedFrame(
Wu-cheng Li57c86182011-07-30 05:00:37 +08001162 int32_t msgType, const sp<ICameraClient>& client,
1163 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1164 camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 LOG2("copyFrameAndPostCopiedFrame");
1166 // It is necessary to copy out of pmem before sending this to
1167 // the callback. For efficiency, reuse the same MemoryHeapBase
1168 // provided it's big enough. Don't allocate the memory or
1169 // perform the copy if there's no callback.
1170 // hold the preview lock while we grab a reference to the preview buffer
1171 sp<MemoryHeapBase> previewBuffer;
1172
1173 if (mPreviewBuffer == 0) {
1174 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1175 } else if (size > mPreviewBuffer->virtualSize()) {
1176 mPreviewBuffer.clear();
1177 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1178 }
1179 if (mPreviewBuffer == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001180 ALOGE("failed to allocate space for preview buffer");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181 mLock.unlock();
1182 return;
1183 }
1184 previewBuffer = mPreviewBuffer;
1185
1186 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1187
1188 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1189 if (frame == 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001190 ALOGE("failed to allocate space for frame callback");
Mathias Agopian65ab4712010-07-14 17:59:35 -07001191 mLock.unlock();
1192 return;
1193 }
1194
1195 mLock.unlock();
Wu-cheng Li57c86182011-07-30 05:00:37 +08001196 client->dataCallback(msgType, frame, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001197}
1198
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001199int CameraService::Client::getOrientation(int degrees, bool mirror) {
1200 if (!mirror) {
1201 if (degrees == 0) return 0;
1202 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1203 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1204 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1205 } else { // Do mirror (horizontal flip)
1206 if (degrees == 0) { // FLIP_H and ROT_0
1207 return HAL_TRANSFORM_FLIP_H;
1208 } else if (degrees == 90) { // FLIP_H and ROT_90
1209 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1210 } else if (degrees == 180) { // FLIP_H and ROT_180
1211 return HAL_TRANSFORM_FLIP_V;
1212 } else if (degrees == 270) { // FLIP_H and ROT_270
1213 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1214 }
1215 }
Steve Block29357bc2012-01-06 19:20:56 +00001216 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001217 return -1;
1218}
1219
1220
Mathias Agopian65ab4712010-07-14 17:59:35 -07001221// ----------------------------------------------------------------------------
1222
1223static const int kDumpLockRetries = 50;
1224static const int kDumpLockSleep = 60000;
1225
1226static bool tryLock(Mutex& mutex)
1227{
1228 bool locked = false;
1229 for (int i = 0; i < kDumpLockRetries; ++i) {
1230 if (mutex.tryLock() == NO_ERROR) {
1231 locked = true;
1232 break;
1233 }
1234 usleep(kDumpLockSleep);
1235 }
1236 return locked;
1237}
1238
1239status_t CameraService::dump(int fd, const Vector<String16>& args) {
1240 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1241
1242 const size_t SIZE = 256;
1243 char buffer[SIZE];
1244 String8 result;
1245 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1246 snprintf(buffer, SIZE, "Permission Denial: "
1247 "can't dump CameraService from pid=%d, uid=%d\n",
1248 getCallingPid(),
1249 getCallingUid());
1250 result.append(buffer);
1251 write(fd, result.string(), result.size());
1252 } else {
1253 bool locked = tryLock(mServiceLock);
1254 // failed to lock - CameraService is probably deadlocked
1255 if (!locked) {
1256 String8 result(kDeadlockedString);
1257 write(fd, result.string(), result.size());
1258 }
1259
1260 bool hasClient = false;
1261 for (int i = 0; i < mNumberOfCameras; i++) {
1262 sp<Client> client = mClient[i].promote();
1263 if (client == 0) continue;
1264 hasClient = true;
1265 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1266 i,
1267 client->getCameraClient()->asBinder().get(),
1268 client->mClientPid);
1269 result.append(buffer);
1270 write(fd, result.string(), result.size());
1271 client->mHardware->dump(fd, args);
1272 }
1273 if (!hasClient) {
1274 result.append("No camera client yet.\n");
1275 write(fd, result.string(), result.size());
1276 }
1277
1278 if (locked) mServiceLock.unlock();
1279
1280 // change logging level
1281 int n = args.size();
1282 for (int i = 0; i + 1 < n; i++) {
1283 if (args[i] == String16("-v")) {
1284 String8 levelStr(args[i+1]);
1285 int level = atoi(levelStr.string());
1286 sprintf(buffer, "Set Log Level to %d", level);
1287 result.append(buffer);
1288 setLogLevel(level);
1289 }
1290 }
1291 }
1292 return NO_ERROR;
1293}
1294
Mathias Agopian65ab4712010-07-14 17:59:35 -07001295}; // namespace android