blob: 7b4b4ace6088ec0b0c178c78f1461521381aeee3 [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) {
86 LOGE("Could not load camera HAL module");
87 mNumberOfCameras = 0;
88 }
89 else {
90 mNumberOfCameras = mModule->get_number_of_cameras();
91 if (mNumberOfCameras > MAX_CAMERAS) {
92 LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
93 mNumberOfCameras, MAX_CAMERAS);
94 mNumberOfCameras = MAX_CAMERAS;
95 }
96 for (int i = 0; i < mNumberOfCameras; i++) {
97 setCameraFree(i);
98 }
99 }
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800100
101 // Read the system property to determine if we have to use the
102 // AUDIO_STREAM_ENFORCED_AUDIBLE type.
103 char value[PROPERTY_VALUE_MAX];
104 property_get("ro.camera.sound.forced", value, "0");
105 if (strcmp(value, "0") != 0) {
106 mAudioStreamType = AUDIO_STREAM_ENFORCED_AUDIBLE;
107 } else {
108 mAudioStreamType = AUDIO_STREAM_MUSIC;
109 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700110}
111
Mathias Agopian65ab4712010-07-14 17:59:35 -0700112CameraService::~CameraService() {
113 for (int i = 0; i < mNumberOfCameras; i++) {
114 if (mBusy[i]) {
115 LOGE("camera %d is still in use in destructor!", i);
116 }
117 }
118
119 gCameraService = NULL;
120}
121
122int32_t CameraService::getNumberOfCameras() {
123 return mNumberOfCameras;
124}
125
126status_t CameraService::getCameraInfo(int cameraId,
127 struct CameraInfo* cameraInfo) {
Iliyan Malchev8951a972011-04-14 16:55:59 -0700128 if (!mModule) {
129 return NO_INIT;
130 }
131
Mathias Agopian65ab4712010-07-14 17:59:35 -0700132 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
133 return BAD_VALUE;
134 }
135
Iliyan Malchev8951a972011-04-14 16:55:59 -0700136 struct camera_info info;
137 status_t rc = mModule->get_camera_info(cameraId, &info);
138 cameraInfo->facing = info.facing;
139 cameraInfo->orientation = info.orientation;
140 return rc;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700141}
142
143sp<ICamera> CameraService::connect(
144 const sp<ICameraClient>& cameraClient, int cameraId) {
145 int callingPid = getCallingPid();
Tyler Luu5861a9a2011-10-06 00:00:03 -0500146 sp<CameraHardwareInterface> hardware = NULL;
147
Mathias Agopian65ab4712010-07-14 17:59:35 -0700148 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
149
Iliyan Malchev8951a972011-04-14 16:55:59 -0700150 if (!mModule) {
151 LOGE("Camera HAL module not loaded");
152 return NULL;
153 }
154
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155 sp<Client> client;
156 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
157 LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
158 callingPid, cameraId);
159 return NULL;
160 }
161
Wu-cheng Lia3355432011-05-20 14:54:25 +0800162 char value[PROPERTY_VALUE_MAX];
163 property_get("sys.secpolicy.camera.disabled", value, "0");
164 if (strcmp(value, "1") == 0) {
165 // Camera is disabled by DevicePolicyManager.
Steve Blockdf64d152012-01-04 20:05:49 +0000166 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Wu-cheng Lia3355432011-05-20 14:54:25 +0800167 return NULL;
168 }
169
Mathias Agopian65ab4712010-07-14 17:59:35 -0700170 Mutex::Autolock lock(mServiceLock);
171 if (mClient[cameraId] != 0) {
172 client = mClient[cameraId].promote();
173 if (client != 0) {
174 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
175 LOG1("CameraService::connect X (pid %d) (the same client)",
176 callingPid);
177 return client;
178 } else {
179 LOGW("CameraService::connect X (pid %d) rejected (existing client).",
180 callingPid);
181 return NULL;
182 }
183 }
184 mClient[cameraId].clear();
185 }
186
187 if (mBusy[cameraId]) {
188 LOGW("CameraService::connect X (pid %d) rejected"
189 " (camera %d is still busy).", callingPid, cameraId);
190 return NULL;
191 }
192
Iliyan Malchev8951a972011-04-14 16:55:59 -0700193 struct camera_info info;
194 if (mModule->get_camera_info(cameraId, &info) != OK) {
195 LOGE("Invalid camera id %d", cameraId);
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700196 return NULL;
197 }
Iliyan Malchev8951a972011-04-14 16:55:59 -0700198
199 char camera_device_name[10];
200 snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
201
Tyler Luu5861a9a2011-10-06 00:00:03 -0500202 hardware = new CameraHardwareInterface(camera_device_name);
203 if (hardware->initialize(&mModule->common) != OK) {
204 hardware.clear();
205 return NULL;
206 }
207
208 client = new Client(this, cameraClient, hardware, cameraId, info.facing, callingPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700209 mClient[cameraId] = client;
210 LOG1("CameraService::connect X");
211 return client;
212}
213
214void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
215 int callingPid = getCallingPid();
216 LOG1("CameraService::removeClient E (pid %d)", callingPid);
217
218 for (int i = 0; i < mNumberOfCameras; i++) {
219 // Declare this before the lock to make absolutely sure the
220 // destructor won't be called with the lock held.
221 sp<Client> client;
222
223 Mutex::Autolock lock(mServiceLock);
224
225 // This happens when we have already disconnected (or this is
226 // just another unused camera).
227 if (mClient[i] == 0) continue;
228
229 // Promote mClient. It can fail if we are called from this path:
230 // Client::~Client() -> disconnect() -> removeClient().
231 client = mClient[i].promote();
232
233 if (client == 0) {
234 mClient[i].clear();
235 continue;
236 }
237
238 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
239 // Found our camera, clear and leave.
240 LOG1("removeClient: clear camera %d", i);
241 mClient[i].clear();
242 break;
243 }
244 }
245
246 LOG1("CameraService::removeClient X (pid %d)", callingPid);
247}
248
249sp<CameraService::Client> CameraService::getClientById(int cameraId) {
250 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
251 return mClient[cameraId].promote();
252}
253
Mathias Agopian65ab4712010-07-14 17:59:35 -0700254status_t CameraService::onTransact(
255 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
256 // Permission checks
257 switch (code) {
258 case BnCameraService::CONNECT:
259 const int pid = getCallingPid();
260 const int self_pid = getpid();
261 if (pid != self_pid) {
262 // we're called from a different process, do the real check
263 if (!checkCallingPermission(
264 String16("android.permission.CAMERA"))) {
265 const int uid = getCallingUid();
266 LOGE("Permission Denial: "
267 "can't use the camera pid=%d, uid=%d", pid, uid);
268 return PERMISSION_DENIED;
269 }
270 }
271 break;
272 }
273
274 return BnCameraService::onTransact(code, data, reply, flags);
275}
276
277// The reason we need this busy bit is a new CameraService::connect() request
278// may come in while the previous Client's destructor has not been run or is
279// still running. If the last strong reference of the previous Client is gone
280// but the destructor has not been finished, we should not allow the new Client
281// to be created because we need to wait for the previous Client to tear down
282// the hardware first.
283void CameraService::setCameraBusy(int cameraId) {
284 android_atomic_write(1, &mBusy[cameraId]);
285}
286
287void CameraService::setCameraFree(int cameraId) {
288 android_atomic_write(0, &mBusy[cameraId]);
289}
290
291// We share the media players for shutter and recording sound for all clients.
292// A reference count is kept to determine when we will actually release the
293// media players.
294
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800295MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700296 MediaPlayer* mp = new MediaPlayer();
297 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800298 mp->setAudioStreamType(mAudioStreamType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299 mp->prepare();
300 } else {
301 LOGE("Failed to load CameraService sounds: %s", file);
302 return NULL;
303 }
304 return mp;
305}
306
307void CameraService::loadSound() {
308 Mutex::Autolock lock(mSoundLock);
309 LOG1("CameraService::loadSound ref=%d", mSoundRef);
310 if (mSoundRef++) return;
311
312 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
313 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
314}
315
316void CameraService::releaseSound() {
317 Mutex::Autolock lock(mSoundLock);
318 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
319 if (--mSoundRef) return;
320
321 for (int i = 0; i < NUM_SOUNDS; i++) {
322 if (mSoundPlayer[i] != 0) {
323 mSoundPlayer[i]->disconnect();
324 mSoundPlayer[i].clear();
325 }
326 }
327}
328
329void CameraService::playSound(sound_kind kind) {
330 LOG1("playSound(%d)", kind);
331 Mutex::Autolock lock(mSoundLock);
332 sp<MediaPlayer> player = mSoundPlayer[kind];
333 if (player != 0) {
Chih-Chung Chang8888a752011-10-20 10:47:26 +0800334 player->seekTo(0);
335 player->start();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700336 }
337}
338
339// ----------------------------------------------------------------------------
340
341CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700342 const sp<ICameraClient>& cameraClient,
343 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800344 int cameraId, int cameraFacing, int clientPid) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700345 int callingPid = getCallingPid();
346 LOG1("Client::Client E (pid %d)", callingPid);
347
348 mCameraService = cameraService;
349 mCameraClient = cameraClient;
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700350 mHardware = hardware;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 mCameraId = cameraId;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800352 mCameraFacing = cameraFacing;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700353 mClientPid = clientPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700354 mMsgEnabled = 0;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800355 mSurface = 0;
356 mPreviewWindow = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 mHardware->setCallbacks(notifyCallback,
358 dataCallback,
359 dataCallbackTimestamp,
360 (void *)cameraId);
361
Wu-cheng Li57c86182011-07-30 05:00:37 +0800362 // Enable zoom, error, focus, and metadata messages by default
363 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
Wu-cheng Lid6205062011-11-14 20:30:14 +0800364 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365
366 // Callback is disabled by default
Iliyan Malchev9e626522011-04-14 16:51:21 -0700367 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800368 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700369 mPlayShutterSound = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700370 cameraService->setCameraBusy(cameraId);
371 cameraService->loadSound();
372 LOG1("Client::Client X (pid %d)", callingPid);
373}
374
Mathias Agopian65ab4712010-07-14 17:59:35 -0700375// tear down the client
376CameraService::Client::~Client() {
377 int callingPid = getCallingPid();
378 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
379
Mathias Agopian65ab4712010-07-14 17:59:35 -0700380 // set mClientPid to let disconnet() tear down the hardware
381 mClientPid = callingPid;
382 disconnect();
383 mCameraService->releaseSound();
384 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
385}
386
387// ----------------------------------------------------------------------------
388
389status_t CameraService::Client::checkPid() const {
390 int callingPid = getCallingPid();
391 if (callingPid == mClientPid) return NO_ERROR;
392
393 LOGW("attempt to use a locked camera from a different process"
394 " (old pid %d, new pid %d)", mClientPid, callingPid);
395 return EBUSY;
396}
397
398status_t CameraService::Client::checkPidAndHardware() const {
399 status_t result = checkPid();
400 if (result != NO_ERROR) return result;
401 if (mHardware == 0) {
402 LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
403 return INVALID_OPERATION;
404 }
405 return NO_ERROR;
406}
407
408status_t CameraService::Client::lock() {
409 int callingPid = getCallingPid();
410 LOG1("lock (pid %d)", callingPid);
411 Mutex::Autolock lock(mLock);
412
413 // lock camera to this client if the the camera is unlocked
414 if (mClientPid == 0) {
415 mClientPid = callingPid;
416 return NO_ERROR;
417 }
418
419 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
420 return checkPid();
421}
422
423status_t CameraService::Client::unlock() {
424 int callingPid = getCallingPid();
425 LOG1("unlock (pid %d)", callingPid);
426 Mutex::Autolock lock(mLock);
427
428 // allow anyone to use camera (after they lock the camera)
429 status_t result = checkPid();
430 if (result == NO_ERROR) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800431 if (mHardware->recordingEnabled()) {
432 LOGE("Not allowed to unlock camera during recording.");
433 return INVALID_OPERATION;
434 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700435 mClientPid = 0;
436 LOG1("clear mCameraClient (pid %d)", callingPid);
437 // we need to remove the reference to ICameraClient so that when the app
438 // goes away, the reference count goes to 0.
439 mCameraClient.clear();
440 }
441 return result;
442}
443
444// connect a new client to the camera
445status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
446 int callingPid = getCallingPid();
447 LOG1("connect E (pid %d)", callingPid);
448 Mutex::Autolock lock(mLock);
449
450 if (mClientPid != 0 && checkPid() != NO_ERROR) {
451 LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
452 mClientPid, callingPid);
453 return EBUSY;
454 }
455
456 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
457 LOG1("Connect to the same client");
458 return NO_ERROR;
459 }
460
Iliyan Malchev9e626522011-04-14 16:51:21 -0700461 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700462 mClientPid = callingPid;
463 mCameraClient = client;
464
465 LOG1("connect X (pid %d)", callingPid);
466 return NO_ERROR;
467}
468
Wu-cheng Li7574da52011-07-20 05:35:02 +0800469static void disconnectWindow(const sp<ANativeWindow>& window) {
470 if (window != 0) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700471 status_t result = native_window_api_disconnect(window.get(),
Wu-cheng Li7574da52011-07-20 05:35:02 +0800472 NATIVE_WINDOW_API_CAMERA);
473 if (result != NO_ERROR) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700474 LOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
Wu-cheng Li7574da52011-07-20 05:35:02 +0800475 result);
476 }
477 }
478}
479
Mathias Agopian65ab4712010-07-14 17:59:35 -0700480void CameraService::Client::disconnect() {
481 int callingPid = getCallingPid();
482 LOG1("disconnect E (pid %d)", callingPid);
483 Mutex::Autolock lock(mLock);
484
485 if (checkPid() != NO_ERROR) {
486 LOGW("different client - don't disconnect");
487 return;
488 }
489
490 if (mClientPid <= 0) {
491 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
492 return;
493 }
494
495 // Make sure disconnect() is done once and once only, whether it is called
496 // from the user directly, or called by the destructor.
497 if (mHardware == 0) return;
498
499 LOG1("hardware teardown");
500 // Before destroying mHardware, we must make sure it's in the
501 // idle state.
502 // Turn off all messages.
503 disableMsgType(CAMERA_MSG_ALL_MSGS);
504 mHardware->stopPreview();
505 mHardware->cancelPicture();
506 // Release the hardware resources.
507 mHardware->release();
Mathias Agopian03dfce92010-12-07 19:38:17 -0800508
Jamie Gennis4b791682010-08-10 16:37:53 -0700509 // Release the held ANativeWindow resources.
510 if (mPreviewWindow != 0) {
Wu-cheng Li7574da52011-07-20 05:35:02 +0800511 disconnectWindow(mPreviewWindow);
Jamie Gennis4b791682010-08-10 16:37:53 -0700512 mPreviewWindow = 0;
513 mHardware->setPreviewWindow(mPreviewWindow);
514 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700515 mHardware.clear();
516
517 mCameraService->removeClient(mCameraClient);
518 mCameraService->setCameraFree(mCameraId);
519
520 LOG1("disconnect X (pid %d)", callingPid);
521}
522
523// ----------------------------------------------------------------------------
524
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700525status_t CameraService::Client::setPreviewWindow(const sp<IBinder>& binder,
526 const sp<ANativeWindow>& window) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700527 Mutex::Autolock lock(mLock);
528 status_t result = checkPidAndHardware();
529 if (result != NO_ERROR) return result;
530
Mathias Agopian65ab4712010-07-14 17:59:35 -0700531 // return if no change in surface.
Mathias Agopian8b1027d2011-04-05 15:44:20 -0700532 if (binder == mSurface) {
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700533 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700534 }
535
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700536 if (window != 0) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700537 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700538 if (result != NO_ERROR) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700539 LOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700540 result);
541 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700542 }
543 }
544
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700545 // If preview has been already started, register preview buffers now.
546 if (mHardware->previewEnabled()) {
547 if (window != 0) {
Mathias Agopian9bc7af12011-07-18 16:15:08 -0700548 native_window_set_scaling_mode(window.get(),
549 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700550 native_window_set_buffers_transform(window.get(), mOrientation);
551 result = mHardware->setPreviewWindow(window);
552 }
553 }
554
555 if (result == NO_ERROR) {
556 // Everything has succeeded. Disconnect the old window and remember the
557 // new window.
558 disconnectWindow(mPreviewWindow);
559 mSurface = binder;
560 mPreviewWindow = window;
561 } else {
562 // Something went wrong after we connected to the new window, so
563 // disconnect here.
564 disconnectWindow(window);
565 }
566
Mathias Agopian65ab4712010-07-14 17:59:35 -0700567 return result;
568}
569
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700570// set the Surface that the preview will use
571status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
572 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
573
574 sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
575 sp<ANativeWindow> window(surface);
576 return setPreviewWindow(binder, window);
577}
578
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800579// set the SurfaceTexture that the preview will use
580status_t CameraService::Client::setPreviewTexture(
581 const sp<ISurfaceTexture>& surfaceTexture) {
582 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
583 getCallingPid());
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800584
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700585 sp<IBinder> binder;
586 sp<ANativeWindow> window;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800587 if (surfaceTexture != 0) {
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700588 binder = surfaceTexture->asBinder();
589 window = new SurfaceTextureClient(surfaceTexture);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800590 }
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700591 return setPreviewWindow(binder, window);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800592}
593
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594// set the preview callback flag to affect how the received frames from
595// preview are handled.
596void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
597 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
598 Mutex::Autolock lock(mLock);
599 if (checkPidAndHardware() != NO_ERROR) return;
600
601 mPreviewCallbackFlag = callback_flag;
Iliyan Malchev9e626522011-04-14 16:51:21 -0700602 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
Wu-cheng Li0667de72010-09-03 16:40:32 -0700603 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
604 } else {
605 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700606 }
607}
608
609// start preview mode
610status_t CameraService::Client::startPreview() {
611 LOG1("startPreview (pid %d)", getCallingPid());
612 return startCameraMode(CAMERA_PREVIEW_MODE);
613}
614
615// start recording mode
616status_t CameraService::Client::startRecording() {
617 LOG1("startRecording (pid %d)", getCallingPid());
618 return startCameraMode(CAMERA_RECORDING_MODE);
619}
620
621// start preview or recording
622status_t CameraService::Client::startCameraMode(camera_mode mode) {
623 LOG1("startCameraMode(%d)", mode);
624 Mutex::Autolock lock(mLock);
625 status_t result = checkPidAndHardware();
626 if (result != NO_ERROR) return result;
627
628 switch(mode) {
629 case CAMERA_PREVIEW_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700630 if (mSurface == 0 && mPreviewWindow == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700631 LOG1("mSurface is not set yet.");
632 // still able to start preview in this case.
633 }
634 return startPreviewMode();
635 case CAMERA_RECORDING_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700636 if (mSurface == 0 && mPreviewWindow == 0) {
637 LOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700638 return INVALID_OPERATION;
639 }
640 return startRecordingMode();
641 default:
642 return UNKNOWN_ERROR;
643 }
644}
645
646status_t CameraService::Client::startPreviewMode() {
647 LOG1("startPreviewMode");
648 status_t result = NO_ERROR;
649
650 // if preview has been enabled, nothing needs to be done
651 if (mHardware->previewEnabled()) {
652 return NO_ERROR;
653 }
654
Mathias Agopian03dfce92010-12-07 19:38:17 -0800655 if (mPreviewWindow != 0) {
Mathias Agopian9bc7af12011-07-18 16:15:08 -0700656 native_window_set_scaling_mode(mPreviewWindow.get(),
657 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian03dfce92010-12-07 19:38:17 -0800658 native_window_set_buffers_transform(mPreviewWindow.get(),
659 mOrientation);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700660 }
Mathias Agopian03dfce92010-12-07 19:38:17 -0800661 mHardware->setPreviewWindow(mPreviewWindow);
662 result = mHardware->startPreview();
663
Mathias Agopian65ab4712010-07-14 17:59:35 -0700664 return result;
665}
666
667status_t CameraService::Client::startRecordingMode() {
668 LOG1("startRecordingMode");
669 status_t result = NO_ERROR;
670
671 // if recording has been enabled, nothing needs to be done
672 if (mHardware->recordingEnabled()) {
673 return NO_ERROR;
674 }
675
676 // if preview has not been started, start preview first
677 if (!mHardware->previewEnabled()) {
678 result = startPreviewMode();
679 if (result != NO_ERROR) {
680 return result;
681 }
682 }
683
684 // start recording mode
685 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
686 mCameraService->playSound(SOUND_RECORDING);
687 result = mHardware->startRecording();
688 if (result != NO_ERROR) {
689 LOGE("mHardware->startRecording() failed with status %d", result);
690 }
691 return result;
692}
693
694// stop preview mode
695void CameraService::Client::stopPreview() {
696 LOG1("stopPreview (pid %d)", getCallingPid());
697 Mutex::Autolock lock(mLock);
698 if (checkPidAndHardware() != NO_ERROR) return;
699
Jamie Gennis4b791682010-08-10 16:37:53 -0700700
Mathias Agopian65ab4712010-07-14 17:59:35 -0700701 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
702 mHardware->stopPreview();
703
Mathias Agopian65ab4712010-07-14 17:59:35 -0700704 mPreviewBuffer.clear();
705}
706
707// stop recording mode
708void CameraService::Client::stopRecording() {
709 LOG1("stopRecording (pid %d)", getCallingPid());
710 Mutex::Autolock lock(mLock);
711 if (checkPidAndHardware() != NO_ERROR) return;
712
713 mCameraService->playSound(SOUND_RECORDING);
714 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
715 mHardware->stopRecording();
716
717 mPreviewBuffer.clear();
718}
719
720// release a recording frame
721void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
722 Mutex::Autolock lock(mLock);
723 if (checkPidAndHardware() != NO_ERROR) return;
724 mHardware->releaseRecordingFrame(mem);
725}
726
James Donge2ad6732010-10-18 20:42:51 -0700727status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
728{
729 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
730 Mutex::Autolock lock(mLock);
731 if (checkPidAndHardware() != NO_ERROR) {
732 return UNKNOWN_ERROR;
733 }
734 return mHardware->storeMetaDataInBuffers(enabled);
735}
736
Mathias Agopian65ab4712010-07-14 17:59:35 -0700737bool CameraService::Client::previewEnabled() {
738 LOG1("previewEnabled (pid %d)", getCallingPid());
739
740 Mutex::Autolock lock(mLock);
741 if (checkPidAndHardware() != NO_ERROR) return false;
742 return mHardware->previewEnabled();
743}
744
745bool CameraService::Client::recordingEnabled() {
746 LOG1("recordingEnabled (pid %d)", getCallingPid());
747
748 Mutex::Autolock lock(mLock);
749 if (checkPidAndHardware() != NO_ERROR) return false;
750 return mHardware->recordingEnabled();
751}
752
753status_t CameraService::Client::autoFocus() {
754 LOG1("autoFocus (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->autoFocus();
761}
762
763status_t CameraService::Client::cancelAutoFocus() {
764 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
765
766 Mutex::Autolock lock(mLock);
767 status_t result = checkPidAndHardware();
768 if (result != NO_ERROR) return result;
769
770 return mHardware->cancelAutoFocus();
771}
772
773// take a picture - image is returned in callback
James Donge468ac52011-02-17 16:38:06 -0800774status_t CameraService::Client::takePicture(int msgType) {
775 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700776
777 Mutex::Autolock lock(mLock);
778 status_t result = checkPidAndHardware();
779 if (result != NO_ERROR) return result;
780
James Donge468ac52011-02-17 16:38:06 -0800781 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
782 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
783 LOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
784 " cannot be both enabled");
785 return BAD_VALUE;
786 }
787
788 // We only accept picture related message types
789 // and ignore other types of messages for takePicture().
790 int picMsgType = msgType
791 & (CAMERA_MSG_SHUTTER |
792 CAMERA_MSG_POSTVIEW_FRAME |
793 CAMERA_MSG_RAW_IMAGE |
794 CAMERA_MSG_RAW_IMAGE_NOTIFY |
795 CAMERA_MSG_COMPRESSED_IMAGE);
796
797 enableMsgType(picMsgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700798
799 return mHardware->takePicture();
800}
801
802// set preview/capture parameters - key/value pairs
803status_t CameraService::Client::setParameters(const String8& params) {
804 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
805
806 Mutex::Autolock lock(mLock);
807 status_t result = checkPidAndHardware();
808 if (result != NO_ERROR) return result;
809
810 CameraParameters p(params);
811 return mHardware->setParameters(p);
812}
813
814// get preview/capture parameters - key/value pairs
815String8 CameraService::Client::getParameters() const {
816 Mutex::Autolock lock(mLock);
817 if (checkPidAndHardware() != NO_ERROR) return String8();
818
819 String8 params(mHardware->getParameters().flatten());
820 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
821 return params;
822}
823
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700824// enable shutter sound
825status_t CameraService::Client::enableShutterSound(bool enable) {
826 LOG1("enableShutterSound (pid %d)", getCallingPid());
827
828 status_t result = checkPidAndHardware();
829 if (result != NO_ERROR) return result;
830
831 if (enable) {
832 mPlayShutterSound = true;
833 return OK;
834 }
835
836 // Disabling shutter sound may not be allowed. In that case only
837 // allow the mediaserver process to disable the sound.
838 char value[PROPERTY_VALUE_MAX];
839 property_get("ro.camera.sound.forced", value, "0");
840 if (strcmp(value, "0") != 0) {
841 // Disabling shutter sound is not allowed. Deny if the current
842 // process is not mediaserver.
843 if (getCallingPid() != getpid()) {
844 LOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
845 return PERMISSION_DENIED;
846 }
847 }
848
849 mPlayShutterSound = false;
850 return OK;
851}
852
Mathias Agopian65ab4712010-07-14 17:59:35 -0700853status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
854 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700855 int orientation;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700856 Mutex::Autolock lock(mLock);
857 status_t result = checkPidAndHardware();
858 if (result != NO_ERROR) return result;
859
860 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800861 // Mirror the preview if the camera is front-facing.
862 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
863 if (orientation == -1) return BAD_VALUE;
864
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700865 if (mOrientation != orientation) {
866 mOrientation = orientation;
Wu-cheng Lib9f58862011-10-07 13:13:54 +0800867 if (mPreviewWindow != 0) {
868 native_window_set_buffers_transform(mPreviewWindow.get(),
869 mOrientation);
870 }
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700871 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700872 return OK;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700873 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
874 switch (arg1) {
875 case 0:
876 enableShutterSound(false);
877 break;
878 case 1:
879 enableShutterSound(true);
880 break;
881 default:
882 return BAD_VALUE;
883 }
884 return OK;
Nipun Kwatra3b7b3582010-09-14 16:49:08 -0700885 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
886 mCameraService->playSound(SOUND_RECORDING);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700887 }
888
889 return mHardware->sendCommand(cmd, arg1, arg2);
890}
891
892// ----------------------------------------------------------------------------
893
894void CameraService::Client::enableMsgType(int32_t msgType) {
895 android_atomic_or(msgType, &mMsgEnabled);
896 mHardware->enableMsgType(msgType);
897}
898
899void CameraService::Client::disableMsgType(int32_t msgType) {
900 android_atomic_and(~msgType, &mMsgEnabled);
901 mHardware->disableMsgType(msgType);
902}
903
904#define CHECK_MESSAGE_INTERVAL 10 // 10ms
905bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
906 int sleepCount = 0;
907 while (mMsgEnabled & msgType) {
908 if (mLock.tryLock() == NO_ERROR) {
909 if (sleepCount > 0) {
910 LOG1("lockIfMessageWanted(%d): waited for %d ms",
911 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
912 }
913 return true;
914 }
915 if (sleepCount++ == 0) {
916 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
917 }
918 usleep(CHECK_MESSAGE_INTERVAL * 1000);
919 }
920 LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
921 return false;
922}
923
924// ----------------------------------------------------------------------------
925
926// Converts from a raw pointer to the client to a strong pointer during a
927// hardware callback. This requires the callbacks only happen when the client
928// is still alive.
929sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
930 sp<Client> client = gCameraService->getClientById((int) user);
931
932 // This could happen if the Client is in the process of shutting down (the
933 // last strong reference is gone, but the destructor hasn't finished
934 // stopping the hardware).
935 if (client == 0) return NULL;
936
937 // The checks below are not necessary and are for debugging only.
938 if (client->mCameraService.get() != gCameraService) {
939 LOGE("mismatch service!");
940 return NULL;
941 }
942
943 if (client->mHardware == 0) {
944 LOGE("mHardware == 0: callback after disconnect()?");
945 return NULL;
946 }
947
948 return client;
949}
950
951// Callback messages can be dispatched to internal handlers or pass to our
952// client's callback functions, depending on the message type.
953//
954// notifyCallback:
955// CAMERA_MSG_SHUTTER handleShutter
956// (others) c->notifyCallback
957// dataCallback:
958// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
959// CAMERA_MSG_POSTVIEW_FRAME handlePostview
960// CAMERA_MSG_RAW_IMAGE handleRawPicture
961// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
962// (others) c->dataCallback
963// dataCallbackTimestamp
964// (others) c->dataCallbackTimestamp
965//
966// NOTE: the *Callback functions grab mLock of the client before passing
967// control to handle* functions. So the handle* functions must release the
968// lock before calling the ICameraClient's callbacks, so those callbacks can
969// invoke methods in the Client class again (For example, the preview frame
970// callback may want to releaseRecordingFrame). The handle* functions must
971// release the lock after all accesses to member variables, so it must be
972// handled very carefully.
973
974void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
975 int32_t ext2, void* user) {
976 LOG2("notifyCallback(%d)", msgType);
977
978 sp<Client> client = getClientFromCookie(user);
979 if (client == 0) return;
980 if (!client->lockIfMessageWanted(msgType)) return;
981
982 switch (msgType) {
983 case CAMERA_MSG_SHUTTER:
984 // ext1 is the dimension of the yuv picture.
Iliyan Malchev108dddf2011-03-28 16:10:12 -0700985 client->handleShutter();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700986 break;
987 default:
988 client->handleGenericNotify(msgType, ext1, ext2);
989 break;
990 }
991}
992
993void CameraService::Client::dataCallback(int32_t msgType,
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800994 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700995 LOG2("dataCallback(%d)", msgType);
996
997 sp<Client> client = getClientFromCookie(user);
998 if (client == 0) return;
999 if (!client->lockIfMessageWanted(msgType)) return;
1000
Wu-cheng Li57c86182011-07-30 05:00:37 +08001001 if (dataPtr == 0 && metadata == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001002 LOGE("Null data returned in data callback");
1003 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1004 return;
1005 }
1006
Wu-cheng Li57c86182011-07-30 05:00:37 +08001007 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008 case CAMERA_MSG_PREVIEW_FRAME:
Wu-cheng Li57c86182011-07-30 05:00:37 +08001009 client->handlePreviewData(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001010 break;
1011 case CAMERA_MSG_POSTVIEW_FRAME:
1012 client->handlePostview(dataPtr);
1013 break;
1014 case CAMERA_MSG_RAW_IMAGE:
1015 client->handleRawPicture(dataPtr);
1016 break;
1017 case CAMERA_MSG_COMPRESSED_IMAGE:
1018 client->handleCompressedPicture(dataPtr);
1019 break;
1020 default:
Wu-cheng Li57c86182011-07-30 05:00:37 +08001021 client->handleGenericData(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001022 break;
1023 }
1024}
1025
1026void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
1027 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
1028 LOG2("dataCallbackTimestamp(%d)", msgType);
1029
1030 sp<Client> client = getClientFromCookie(user);
1031 if (client == 0) return;
James Dong986ef2a2010-12-09 11:08:14 -08001032 if (!client->lockIfMessageWanted(msgType)) return;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001033
1034 if (dataPtr == 0) {
1035 LOGE("Null data returned in data with timestamp callback");
1036 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1037 return;
1038 }
1039
1040 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1041}
1042
1043// snapshot taken callback
Iliyan Malchev108dddf2011-03-28 16:10:12 -07001044void CameraService::Client::handleShutter(void) {
Nipun Kwatrab5ca4612010-09-11 19:31:10 -07001045 if (mPlayShutterSound) {
1046 mCameraService->playSound(SOUND_SHUTTER);
1047 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001048
Mathias Agopian65ab4712010-07-14 17:59:35 -07001049 sp<ICameraClient> c = mCameraClient;
1050 if (c != 0) {
1051 mLock.unlock();
1052 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1053 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1054 }
1055 disableMsgType(CAMERA_MSG_SHUTTER);
1056
Mathias Agopian65ab4712010-07-14 17:59:35 -07001057 mLock.unlock();
1058}
1059
1060// preview callback - frame buffer update
Wu-cheng Li57c86182011-07-30 05:00:37 +08001061void CameraService::Client::handlePreviewData(int32_t msgType,
1062 const sp<IMemory>& mem,
1063 camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001064 ssize_t offset;
1065 size_t size;
1066 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1067
Mathias Agopian65ab4712010-07-14 17:59:35 -07001068 // local copy of the callback flags
1069 int flags = mPreviewCallbackFlag;
1070
1071 // is callback enabled?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001072 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001073 // If the enable bit is off, the copy-out and one-shot bits are ignored
1074 LOG2("frame callback is disabled");
1075 mLock.unlock();
1076 return;
1077 }
1078
1079 // hold a strong pointer to the client
1080 sp<ICameraClient> c = mCameraClient;
1081
1082 // clear callback flags if no client or one-shot mode
Iliyan Malchev9e626522011-04-14 16:51:21 -07001083 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001084 LOG2("Disable preview callback");
Iliyan Malchev9e626522011-04-14 16:51:21 -07001085 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1086 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1087 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Li0667de72010-09-03 16:40:32 -07001088 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001089 }
1090
1091 if (c != 0) {
1092 // Is the received frame copied out or not?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001093 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001094 LOG2("frame is copied");
Wu-cheng Li57c86182011-07-30 05:00:37 +08001095 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001096 } else {
1097 LOG2("frame is forwarded");
1098 mLock.unlock();
Wu-cheng Li57c86182011-07-30 05:00:37 +08001099 c->dataCallback(msgType, mem, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100 }
1101 } else {
1102 mLock.unlock();
1103 }
1104}
1105
1106// picture callback - postview image ready
1107void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1108 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1109
1110 sp<ICameraClient> c = mCameraClient;
1111 mLock.unlock();
1112 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001113 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001114 }
1115}
1116
1117// picture callback - raw image ready
1118void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1119 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1120
1121 ssize_t offset;
1122 size_t size;
1123 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1124
Mathias Agopian65ab4712010-07-14 17:59:35 -07001125 sp<ICameraClient> c = mCameraClient;
1126 mLock.unlock();
1127 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001128 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001129 }
1130}
1131
1132// picture callback - compressed picture ready
1133void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1134 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1135
1136 sp<ICameraClient> c = mCameraClient;
1137 mLock.unlock();
1138 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001139 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001140 }
1141}
1142
1143
1144void CameraService::Client::handleGenericNotify(int32_t msgType,
1145 int32_t ext1, int32_t ext2) {
1146 sp<ICameraClient> c = mCameraClient;
1147 mLock.unlock();
1148 if (c != 0) {
1149 c->notifyCallback(msgType, ext1, ext2);
1150 }
1151}
1152
1153void CameraService::Client::handleGenericData(int32_t msgType,
Wu-cheng Li57c86182011-07-30 05:00:37 +08001154 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001155 sp<ICameraClient> c = mCameraClient;
1156 mLock.unlock();
1157 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001158 c->dataCallback(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001159 }
1160}
1161
1162void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1163 int32_t msgType, const sp<IMemory>& dataPtr) {
1164 sp<ICameraClient> c = mCameraClient;
1165 mLock.unlock();
1166 if (c != 0) {
1167 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1168 }
1169}
1170
1171void CameraService::Client::copyFrameAndPostCopiedFrame(
Wu-cheng Li57c86182011-07-30 05:00:37 +08001172 int32_t msgType, const sp<ICameraClient>& client,
1173 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1174 camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001175 LOG2("copyFrameAndPostCopiedFrame");
1176 // It is necessary to copy out of pmem before sending this to
1177 // the callback. For efficiency, reuse the same MemoryHeapBase
1178 // provided it's big enough. Don't allocate the memory or
1179 // perform the copy if there's no callback.
1180 // hold the preview lock while we grab a reference to the preview buffer
1181 sp<MemoryHeapBase> previewBuffer;
1182
1183 if (mPreviewBuffer == 0) {
1184 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1185 } else if (size > mPreviewBuffer->virtualSize()) {
1186 mPreviewBuffer.clear();
1187 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1188 }
1189 if (mPreviewBuffer == 0) {
1190 LOGE("failed to allocate space for preview buffer");
1191 mLock.unlock();
1192 return;
1193 }
1194 previewBuffer = mPreviewBuffer;
1195
1196 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1197
1198 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1199 if (frame == 0) {
1200 LOGE("failed to allocate space for frame callback");
1201 mLock.unlock();
1202 return;
1203 }
1204
1205 mLock.unlock();
Wu-cheng Li57c86182011-07-30 05:00:37 +08001206 client->dataCallback(msgType, frame, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001207}
1208
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001209int CameraService::Client::getOrientation(int degrees, bool mirror) {
1210 if (!mirror) {
1211 if (degrees == 0) return 0;
1212 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1213 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1214 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1215 } else { // Do mirror (horizontal flip)
1216 if (degrees == 0) { // FLIP_H and ROT_0
1217 return HAL_TRANSFORM_FLIP_H;
1218 } else if (degrees == 90) { // FLIP_H and ROT_90
1219 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1220 } else if (degrees == 180) { // FLIP_H and ROT_180
1221 return HAL_TRANSFORM_FLIP_V;
1222 } else if (degrees == 270) { // FLIP_H and ROT_270
1223 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1224 }
1225 }
1226 LOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1227 return -1;
1228}
1229
1230
Mathias Agopian65ab4712010-07-14 17:59:35 -07001231// ----------------------------------------------------------------------------
1232
1233static const int kDumpLockRetries = 50;
1234static const int kDumpLockSleep = 60000;
1235
1236static bool tryLock(Mutex& mutex)
1237{
1238 bool locked = false;
1239 for (int i = 0; i < kDumpLockRetries; ++i) {
1240 if (mutex.tryLock() == NO_ERROR) {
1241 locked = true;
1242 break;
1243 }
1244 usleep(kDumpLockSleep);
1245 }
1246 return locked;
1247}
1248
1249status_t CameraService::dump(int fd, const Vector<String16>& args) {
1250 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1251
1252 const size_t SIZE = 256;
1253 char buffer[SIZE];
1254 String8 result;
1255 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1256 snprintf(buffer, SIZE, "Permission Denial: "
1257 "can't dump CameraService from pid=%d, uid=%d\n",
1258 getCallingPid(),
1259 getCallingUid());
1260 result.append(buffer);
1261 write(fd, result.string(), result.size());
1262 } else {
1263 bool locked = tryLock(mServiceLock);
1264 // failed to lock - CameraService is probably deadlocked
1265 if (!locked) {
1266 String8 result(kDeadlockedString);
1267 write(fd, result.string(), result.size());
1268 }
1269
1270 bool hasClient = false;
1271 for (int i = 0; i < mNumberOfCameras; i++) {
1272 sp<Client> client = mClient[i].promote();
1273 if (client == 0) continue;
1274 hasClient = true;
1275 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1276 i,
1277 client->getCameraClient()->asBinder().get(),
1278 client->mClientPid);
1279 result.append(buffer);
1280 write(fd, result.string(), result.size());
1281 client->mHardware->dump(fd, args);
1282 }
1283 if (!hasClient) {
1284 result.append("No camera client yet.\n");
1285 write(fd, result.string(), result.size());
1286 }
1287
1288 if (locked) mServiceLock.unlock();
1289
1290 // change logging level
1291 int n = args.size();
1292 for (int i = 0; i + 1 < n; i++) {
1293 if (args[i] == String16("-v")) {
1294 String8 levelStr(args[i+1]);
1295 int level = atoi(levelStr.string());
1296 sprintf(buffer, "Set Log Level to %d", level);
1297 result.append(buffer);
1298 setLogLevel(level);
1299 }
1300 }
1301 }
1302 return NO_ERROR;
1303}
1304
Mathias Agopian65ab4712010-07-14 17:59:35 -07001305}; // namespace android