blob: 171710a13c09be0ec6206abcb10d2ca6840163c8 [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
50#define LOG1(...) LOGD_IF(gLogLevel >= 1, __VA_ARGS__);
51#define LOG2(...) LOGD_IF(gLogLevel >= 2, __VA_ARGS__);
52
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{
76 LOGI("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.
166 LOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
167 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) {
334 // do not play the sound if stream volume is 0
335 // (typically because ringer mode is silent).
336 int index;
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800337 AudioSystem::getStreamVolumeIndex(mAudioStreamType, &index);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 if (index != 0) {
339 player->seekTo(0);
340 player->start();
341 }
342 }
343}
344
345// ----------------------------------------------------------------------------
346
347CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700348 const sp<ICameraClient>& cameraClient,
349 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800350 int cameraId, int cameraFacing, int clientPid) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351 int callingPid = getCallingPid();
352 LOG1("Client::Client E (pid %d)", callingPid);
353
354 mCameraService = cameraService;
355 mCameraClient = cameraClient;
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700356 mHardware = hardware;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 mCameraId = cameraId;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800358 mCameraFacing = cameraFacing;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700359 mClientPid = clientPid;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 mMsgEnabled = 0;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800361 mSurface = 0;
362 mPreviewWindow = 0;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700363 mHardware->setCallbacks(notifyCallback,
364 dataCallback,
365 dataCallbackTimestamp,
366 (void *)cameraId);
367
Wu-cheng Li57c86182011-07-30 05:00:37 +0800368 // Enable zoom, error, focus, and metadata messages by default
369 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
370 CAMERA_MSG_PREVIEW_METADATA);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700371
372 // Callback is disabled by default
Iliyan Malchev9e626522011-04-14 16:51:21 -0700373 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800374 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700375 mPlayShutterSound = true;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700376 cameraService->setCameraBusy(cameraId);
377 cameraService->loadSound();
378 LOG1("Client::Client X (pid %d)", callingPid);
379}
380
Mathias Agopian65ab4712010-07-14 17:59:35 -0700381// tear down the client
382CameraService::Client::~Client() {
383 int callingPid = getCallingPid();
384 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
385
Mathias Agopian65ab4712010-07-14 17:59:35 -0700386 // set mClientPid to let disconnet() tear down the hardware
387 mClientPid = callingPid;
388 disconnect();
389 mCameraService->releaseSound();
390 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
391}
392
393// ----------------------------------------------------------------------------
394
395status_t CameraService::Client::checkPid() const {
396 int callingPid = getCallingPid();
397 if (callingPid == mClientPid) return NO_ERROR;
398
399 LOGW("attempt to use a locked camera from a different process"
400 " (old pid %d, new pid %d)", mClientPid, callingPid);
401 return EBUSY;
402}
403
404status_t CameraService::Client::checkPidAndHardware() const {
405 status_t result = checkPid();
406 if (result != NO_ERROR) return result;
407 if (mHardware == 0) {
408 LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
409 return INVALID_OPERATION;
410 }
411 return NO_ERROR;
412}
413
414status_t CameraService::Client::lock() {
415 int callingPid = getCallingPid();
416 LOG1("lock (pid %d)", callingPid);
417 Mutex::Autolock lock(mLock);
418
419 // lock camera to this client if the the camera is unlocked
420 if (mClientPid == 0) {
421 mClientPid = callingPid;
422 return NO_ERROR;
423 }
424
425 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
426 return checkPid();
427}
428
429status_t CameraService::Client::unlock() {
430 int callingPid = getCallingPid();
431 LOG1("unlock (pid %d)", callingPid);
432 Mutex::Autolock lock(mLock);
433
434 // allow anyone to use camera (after they lock the camera)
435 status_t result = checkPid();
436 if (result == NO_ERROR) {
Wu-cheng Li4ca2c7c2011-06-01 17:22:24 +0800437 if (mHardware->recordingEnabled()) {
438 LOGE("Not allowed to unlock camera during recording.");
439 return INVALID_OPERATION;
440 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700441 mClientPid = 0;
442 LOG1("clear mCameraClient (pid %d)", callingPid);
443 // we need to remove the reference to ICameraClient so that when the app
444 // goes away, the reference count goes to 0.
445 mCameraClient.clear();
446 }
447 return result;
448}
449
450// connect a new client to the camera
451status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
452 int callingPid = getCallingPid();
453 LOG1("connect E (pid %d)", callingPid);
454 Mutex::Autolock lock(mLock);
455
456 if (mClientPid != 0 && checkPid() != NO_ERROR) {
457 LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
458 mClientPid, callingPid);
459 return EBUSY;
460 }
461
462 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
463 LOG1("Connect to the same client");
464 return NO_ERROR;
465 }
466
Iliyan Malchev9e626522011-04-14 16:51:21 -0700467 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700468 mClientPid = callingPid;
469 mCameraClient = client;
470
471 LOG1("connect X (pid %d)", callingPid);
472 return NO_ERROR;
473}
474
Wu-cheng Li7574da52011-07-20 05:35:02 +0800475static void disconnectWindow(const sp<ANativeWindow>& window) {
476 if (window != 0) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700477 status_t result = native_window_api_disconnect(window.get(),
Wu-cheng Li7574da52011-07-20 05:35:02 +0800478 NATIVE_WINDOW_API_CAMERA);
479 if (result != NO_ERROR) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700480 LOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
Wu-cheng Li7574da52011-07-20 05:35:02 +0800481 result);
482 }
483 }
484}
485
Mathias Agopian65ab4712010-07-14 17:59:35 -0700486void CameraService::Client::disconnect() {
487 int callingPid = getCallingPid();
488 LOG1("disconnect E (pid %d)", callingPid);
489 Mutex::Autolock lock(mLock);
490
491 if (checkPid() != NO_ERROR) {
492 LOGW("different client - don't disconnect");
493 return;
494 }
495
496 if (mClientPid <= 0) {
497 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
498 return;
499 }
500
501 // Make sure disconnect() is done once and once only, whether it is called
502 // from the user directly, or called by the destructor.
503 if (mHardware == 0) return;
504
505 LOG1("hardware teardown");
506 // Before destroying mHardware, we must make sure it's in the
507 // idle state.
508 // Turn off all messages.
509 disableMsgType(CAMERA_MSG_ALL_MSGS);
510 mHardware->stopPreview();
511 mHardware->cancelPicture();
512 // Release the hardware resources.
513 mHardware->release();
Mathias Agopian03dfce92010-12-07 19:38:17 -0800514
Jamie Gennis4b791682010-08-10 16:37:53 -0700515 // Release the held ANativeWindow resources.
516 if (mPreviewWindow != 0) {
Wu-cheng Li7574da52011-07-20 05:35:02 +0800517 disconnectWindow(mPreviewWindow);
Jamie Gennis4b791682010-08-10 16:37:53 -0700518 mPreviewWindow = 0;
519 mHardware->setPreviewWindow(mPreviewWindow);
520 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700521 mHardware.clear();
522
523 mCameraService->removeClient(mCameraClient);
524 mCameraService->setCameraFree(mCameraId);
525
526 LOG1("disconnect X (pid %d)", callingPid);
527}
528
529// ----------------------------------------------------------------------------
530
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700531status_t CameraService::Client::setPreviewWindow(const sp<IBinder>& binder,
532 const sp<ANativeWindow>& window) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700533 Mutex::Autolock lock(mLock);
534 status_t result = checkPidAndHardware();
535 if (result != NO_ERROR) return result;
536
Mathias Agopian65ab4712010-07-14 17:59:35 -0700537 // return if no change in surface.
Mathias Agopian8b1027d2011-04-05 15:44:20 -0700538 if (binder == mSurface) {
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700539 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700540 }
541
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700542 if (window != 0) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700543 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700544 if (result != NO_ERROR) {
Mathias Agopianc3da3432011-07-29 17:55:48 -0700545 LOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700546 result);
547 return result;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700548 }
549 }
550
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700551 // If preview has been already started, register preview buffers now.
552 if (mHardware->previewEnabled()) {
553 if (window != 0) {
Mathias Agopian9bc7af12011-07-18 16:15:08 -0700554 native_window_set_scaling_mode(window.get(),
555 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700556 native_window_set_buffers_transform(window.get(), mOrientation);
557 result = mHardware->setPreviewWindow(window);
558 }
559 }
560
561 if (result == NO_ERROR) {
562 // Everything has succeeded. Disconnect the old window and remember the
563 // new window.
564 disconnectWindow(mPreviewWindow);
565 mSurface = binder;
566 mPreviewWindow = window;
567 } else {
568 // Something went wrong after we connected to the new window, so
569 // disconnect here.
570 disconnectWindow(window);
571 }
572
Mathias Agopian65ab4712010-07-14 17:59:35 -0700573 return result;
574}
575
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700576// set the Surface that the preview will use
577status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
578 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
579
580 sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
581 sp<ANativeWindow> window(surface);
582 return setPreviewWindow(binder, window);
583}
584
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800585// set the SurfaceTexture that the preview will use
586status_t CameraService::Client::setPreviewTexture(
587 const sp<ISurfaceTexture>& surfaceTexture) {
588 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
589 getCallingPid());
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800590
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700591 sp<IBinder> binder;
592 sp<ANativeWindow> window;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800593 if (surfaceTexture != 0) {
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700594 binder = surfaceTexture->asBinder();
595 window = new SurfaceTextureClient(surfaceTexture);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800596 }
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700597 return setPreviewWindow(binder, window);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800598}
599
Mathias Agopian65ab4712010-07-14 17:59:35 -0700600// set the preview callback flag to affect how the received frames from
601// preview are handled.
602void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
603 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
604 Mutex::Autolock lock(mLock);
605 if (checkPidAndHardware() != NO_ERROR) return;
606
607 mPreviewCallbackFlag = callback_flag;
Iliyan Malchev9e626522011-04-14 16:51:21 -0700608 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
Wu-cheng Li0667de72010-09-03 16:40:32 -0700609 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
610 } else {
611 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700612 }
613}
614
615// start preview mode
616status_t CameraService::Client::startPreview() {
617 LOG1("startPreview (pid %d)", getCallingPid());
618 return startCameraMode(CAMERA_PREVIEW_MODE);
619}
620
621// start recording mode
622status_t CameraService::Client::startRecording() {
623 LOG1("startRecording (pid %d)", getCallingPid());
624 return startCameraMode(CAMERA_RECORDING_MODE);
625}
626
627// start preview or recording
628status_t CameraService::Client::startCameraMode(camera_mode mode) {
629 LOG1("startCameraMode(%d)", mode);
630 Mutex::Autolock lock(mLock);
631 status_t result = checkPidAndHardware();
632 if (result != NO_ERROR) return result;
633
634 switch(mode) {
635 case CAMERA_PREVIEW_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700636 if (mSurface == 0 && mPreviewWindow == 0) {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700637 LOG1("mSurface is not set yet.");
638 // still able to start preview in this case.
639 }
640 return startPreviewMode();
641 case CAMERA_RECORDING_MODE:
Jamie Gennis4b791682010-08-10 16:37:53 -0700642 if (mSurface == 0 && mPreviewWindow == 0) {
643 LOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Mathias Agopian65ab4712010-07-14 17:59:35 -0700644 return INVALID_OPERATION;
645 }
646 return startRecordingMode();
647 default:
648 return UNKNOWN_ERROR;
649 }
650}
651
652status_t CameraService::Client::startPreviewMode() {
653 LOG1("startPreviewMode");
654 status_t result = NO_ERROR;
655
656 // if preview has been enabled, nothing needs to be done
657 if (mHardware->previewEnabled()) {
658 return NO_ERROR;
659 }
660
Mathias Agopian03dfce92010-12-07 19:38:17 -0800661 if (mPreviewWindow != 0) {
Mathias Agopian9bc7af12011-07-18 16:15:08 -0700662 native_window_set_scaling_mode(mPreviewWindow.get(),
663 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian03dfce92010-12-07 19:38:17 -0800664 native_window_set_buffers_transform(mPreviewWindow.get(),
665 mOrientation);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700666 }
Mathias Agopian03dfce92010-12-07 19:38:17 -0800667 mHardware->setPreviewWindow(mPreviewWindow);
668 result = mHardware->startPreview();
669
Mathias Agopian65ab4712010-07-14 17:59:35 -0700670 return result;
671}
672
673status_t CameraService::Client::startRecordingMode() {
674 LOG1("startRecordingMode");
675 status_t result = NO_ERROR;
676
677 // if recording has been enabled, nothing needs to be done
678 if (mHardware->recordingEnabled()) {
679 return NO_ERROR;
680 }
681
682 // if preview has not been started, start preview first
683 if (!mHardware->previewEnabled()) {
684 result = startPreviewMode();
685 if (result != NO_ERROR) {
686 return result;
687 }
688 }
689
690 // start recording mode
691 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
692 mCameraService->playSound(SOUND_RECORDING);
693 result = mHardware->startRecording();
694 if (result != NO_ERROR) {
695 LOGE("mHardware->startRecording() failed with status %d", result);
696 }
697 return result;
698}
699
700// stop preview mode
701void CameraService::Client::stopPreview() {
702 LOG1("stopPreview (pid %d)", getCallingPid());
703 Mutex::Autolock lock(mLock);
704 if (checkPidAndHardware() != NO_ERROR) return;
705
Jamie Gennis4b791682010-08-10 16:37:53 -0700706
Mathias Agopian65ab4712010-07-14 17:59:35 -0700707 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
708 mHardware->stopPreview();
709
Mathias Agopian65ab4712010-07-14 17:59:35 -0700710 mPreviewBuffer.clear();
711}
712
713// stop recording mode
714void CameraService::Client::stopRecording() {
715 LOG1("stopRecording (pid %d)", getCallingPid());
716 Mutex::Autolock lock(mLock);
717 if (checkPidAndHardware() != NO_ERROR) return;
718
719 mCameraService->playSound(SOUND_RECORDING);
720 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
721 mHardware->stopRecording();
722
723 mPreviewBuffer.clear();
724}
725
726// release a recording frame
727void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
728 Mutex::Autolock lock(mLock);
729 if (checkPidAndHardware() != NO_ERROR) return;
730 mHardware->releaseRecordingFrame(mem);
731}
732
James Donge2ad6732010-10-18 20:42:51 -0700733status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
734{
735 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
736 Mutex::Autolock lock(mLock);
737 if (checkPidAndHardware() != NO_ERROR) {
738 return UNKNOWN_ERROR;
739 }
740 return mHardware->storeMetaDataInBuffers(enabled);
741}
742
Mathias Agopian65ab4712010-07-14 17:59:35 -0700743bool CameraService::Client::previewEnabled() {
744 LOG1("previewEnabled (pid %d)", getCallingPid());
745
746 Mutex::Autolock lock(mLock);
747 if (checkPidAndHardware() != NO_ERROR) return false;
748 return mHardware->previewEnabled();
749}
750
751bool CameraService::Client::recordingEnabled() {
752 LOG1("recordingEnabled (pid %d)", getCallingPid());
753
754 Mutex::Autolock lock(mLock);
755 if (checkPidAndHardware() != NO_ERROR) return false;
756 return mHardware->recordingEnabled();
757}
758
759status_t CameraService::Client::autoFocus() {
760 LOG1("autoFocus (pid %d)", getCallingPid());
761
762 Mutex::Autolock lock(mLock);
763 status_t result = checkPidAndHardware();
764 if (result != NO_ERROR) return result;
765
766 return mHardware->autoFocus();
767}
768
769status_t CameraService::Client::cancelAutoFocus() {
770 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
771
772 Mutex::Autolock lock(mLock);
773 status_t result = checkPidAndHardware();
774 if (result != NO_ERROR) return result;
775
776 return mHardware->cancelAutoFocus();
777}
778
779// take a picture - image is returned in callback
James Donge468ac52011-02-17 16:38:06 -0800780status_t CameraService::Client::takePicture(int msgType) {
781 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700782
783 Mutex::Autolock lock(mLock);
784 status_t result = checkPidAndHardware();
785 if (result != NO_ERROR) return result;
786
James Donge468ac52011-02-17 16:38:06 -0800787 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
788 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
789 LOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
790 " cannot be both enabled");
791 return BAD_VALUE;
792 }
793
794 // We only accept picture related message types
795 // and ignore other types of messages for takePicture().
796 int picMsgType = msgType
797 & (CAMERA_MSG_SHUTTER |
798 CAMERA_MSG_POSTVIEW_FRAME |
799 CAMERA_MSG_RAW_IMAGE |
800 CAMERA_MSG_RAW_IMAGE_NOTIFY |
801 CAMERA_MSG_COMPRESSED_IMAGE);
802
803 enableMsgType(picMsgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700804
805 return mHardware->takePicture();
806}
807
808// set preview/capture parameters - key/value pairs
809status_t CameraService::Client::setParameters(const String8& params) {
810 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
811
812 Mutex::Autolock lock(mLock);
813 status_t result = checkPidAndHardware();
814 if (result != NO_ERROR) return result;
815
816 CameraParameters p(params);
817 return mHardware->setParameters(p);
818}
819
820// get preview/capture parameters - key/value pairs
821String8 CameraService::Client::getParameters() const {
822 Mutex::Autolock lock(mLock);
823 if (checkPidAndHardware() != NO_ERROR) return String8();
824
825 String8 params(mHardware->getParameters().flatten());
826 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
827 return params;
828}
829
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700830// enable shutter sound
831status_t CameraService::Client::enableShutterSound(bool enable) {
832 LOG1("enableShutterSound (pid %d)", getCallingPid());
833
834 status_t result = checkPidAndHardware();
835 if (result != NO_ERROR) return result;
836
837 if (enable) {
838 mPlayShutterSound = true;
839 return OK;
840 }
841
842 // Disabling shutter sound may not be allowed. In that case only
843 // allow the mediaserver process to disable the sound.
844 char value[PROPERTY_VALUE_MAX];
845 property_get("ro.camera.sound.forced", value, "0");
846 if (strcmp(value, "0") != 0) {
847 // Disabling shutter sound is not allowed. Deny if the current
848 // process is not mediaserver.
849 if (getCallingPid() != getpid()) {
850 LOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
851 return PERMISSION_DENIED;
852 }
853 }
854
855 mPlayShutterSound = false;
856 return OK;
857}
858
Mathias Agopian65ab4712010-07-14 17:59:35 -0700859status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
860 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700861 int orientation;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700862 Mutex::Autolock lock(mLock);
863 status_t result = checkPidAndHardware();
864 if (result != NO_ERROR) return result;
865
866 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800867 // Mirror the preview if the camera is front-facing.
868 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
869 if (orientation == -1) return BAD_VALUE;
870
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700871 if (mOrientation != orientation) {
872 mOrientation = orientation;
Wu-cheng Lib9f58862011-10-07 13:13:54 +0800873 if (mPreviewWindow != 0) {
874 native_window_set_buffers_transform(mPreviewWindow.get(),
875 mOrientation);
876 }
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700877 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700878 return OK;
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700879 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
880 switch (arg1) {
881 case 0:
882 enableShutterSound(false);
883 break;
884 case 1:
885 enableShutterSound(true);
886 break;
887 default:
888 return BAD_VALUE;
889 }
890 return OK;
Nipun Kwatra3b7b3582010-09-14 16:49:08 -0700891 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
892 mCameraService->playSound(SOUND_RECORDING);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700893 }
894
895 return mHardware->sendCommand(cmd, arg1, arg2);
896}
897
898// ----------------------------------------------------------------------------
899
900void CameraService::Client::enableMsgType(int32_t msgType) {
901 android_atomic_or(msgType, &mMsgEnabled);
902 mHardware->enableMsgType(msgType);
903}
904
905void CameraService::Client::disableMsgType(int32_t msgType) {
906 android_atomic_and(~msgType, &mMsgEnabled);
907 mHardware->disableMsgType(msgType);
908}
909
910#define CHECK_MESSAGE_INTERVAL 10 // 10ms
911bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
912 int sleepCount = 0;
913 while (mMsgEnabled & msgType) {
914 if (mLock.tryLock() == NO_ERROR) {
915 if (sleepCount > 0) {
916 LOG1("lockIfMessageWanted(%d): waited for %d ms",
917 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
918 }
919 return true;
920 }
921 if (sleepCount++ == 0) {
922 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
923 }
924 usleep(CHECK_MESSAGE_INTERVAL * 1000);
925 }
926 LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
927 return false;
928}
929
930// ----------------------------------------------------------------------------
931
932// Converts from a raw pointer to the client to a strong pointer during a
933// hardware callback. This requires the callbacks only happen when the client
934// is still alive.
935sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
936 sp<Client> client = gCameraService->getClientById((int) user);
937
938 // This could happen if the Client is in the process of shutting down (the
939 // last strong reference is gone, but the destructor hasn't finished
940 // stopping the hardware).
941 if (client == 0) return NULL;
942
943 // The checks below are not necessary and are for debugging only.
944 if (client->mCameraService.get() != gCameraService) {
945 LOGE("mismatch service!");
946 return NULL;
947 }
948
949 if (client->mHardware == 0) {
950 LOGE("mHardware == 0: callback after disconnect()?");
951 return NULL;
952 }
953
954 return client;
955}
956
957// Callback messages can be dispatched to internal handlers or pass to our
958// client's callback functions, depending on the message type.
959//
960// notifyCallback:
961// CAMERA_MSG_SHUTTER handleShutter
962// (others) c->notifyCallback
963// dataCallback:
964// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
965// CAMERA_MSG_POSTVIEW_FRAME handlePostview
966// CAMERA_MSG_RAW_IMAGE handleRawPicture
967// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
968// (others) c->dataCallback
969// dataCallbackTimestamp
970// (others) c->dataCallbackTimestamp
971//
972// NOTE: the *Callback functions grab mLock of the client before passing
973// control to handle* functions. So the handle* functions must release the
974// lock before calling the ICameraClient's callbacks, so those callbacks can
975// invoke methods in the Client class again (For example, the preview frame
976// callback may want to releaseRecordingFrame). The handle* functions must
977// release the lock after all accesses to member variables, so it must be
978// handled very carefully.
979
980void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
981 int32_t ext2, void* user) {
982 LOG2("notifyCallback(%d)", msgType);
983
984 sp<Client> client = getClientFromCookie(user);
985 if (client == 0) return;
986 if (!client->lockIfMessageWanted(msgType)) return;
987
988 switch (msgType) {
989 case CAMERA_MSG_SHUTTER:
990 // ext1 is the dimension of the yuv picture.
Iliyan Malchev108dddf2011-03-28 16:10:12 -0700991 client->handleShutter();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700992 break;
993 default:
994 client->handleGenericNotify(msgType, ext1, ext2);
995 break;
996 }
997}
998
999void CameraService::Client::dataCallback(int32_t msgType,
Wu-cheng Liff09ef82011-07-28 05:30:59 +08001000 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001001 LOG2("dataCallback(%d)", msgType);
1002
1003 sp<Client> client = getClientFromCookie(user);
1004 if (client == 0) return;
1005 if (!client->lockIfMessageWanted(msgType)) return;
1006
Wu-cheng Li57c86182011-07-30 05:00:37 +08001007 if (dataPtr == 0 && metadata == NULL) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001008 LOGE("Null data returned in data callback");
1009 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1010 return;
1011 }
1012
Wu-cheng Li57c86182011-07-30 05:00:37 +08001013 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001014 case CAMERA_MSG_PREVIEW_FRAME:
Wu-cheng Li57c86182011-07-30 05:00:37 +08001015 client->handlePreviewData(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001016 break;
1017 case CAMERA_MSG_POSTVIEW_FRAME:
1018 client->handlePostview(dataPtr);
1019 break;
1020 case CAMERA_MSG_RAW_IMAGE:
1021 client->handleRawPicture(dataPtr);
1022 break;
1023 case CAMERA_MSG_COMPRESSED_IMAGE:
1024 client->handleCompressedPicture(dataPtr);
1025 break;
1026 default:
Wu-cheng Li57c86182011-07-30 05:00:37 +08001027 client->handleGenericData(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001028 break;
1029 }
1030}
1031
1032void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
1033 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
1034 LOG2("dataCallbackTimestamp(%d)", msgType);
1035
1036 sp<Client> client = getClientFromCookie(user);
1037 if (client == 0) return;
James Dong986ef2a2010-12-09 11:08:14 -08001038 if (!client->lockIfMessageWanted(msgType)) return;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001039
1040 if (dataPtr == 0) {
1041 LOGE("Null data returned in data with timestamp callback");
1042 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1043 return;
1044 }
1045
1046 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1047}
1048
1049// snapshot taken callback
Iliyan Malchev108dddf2011-03-28 16:10:12 -07001050void CameraService::Client::handleShutter(void) {
Nipun Kwatrab5ca4612010-09-11 19:31:10 -07001051 if (mPlayShutterSound) {
1052 mCameraService->playSound(SOUND_SHUTTER);
1053 }
Mathias Agopian65ab4712010-07-14 17:59:35 -07001054
Mathias Agopian65ab4712010-07-14 17:59:35 -07001055 sp<ICameraClient> c = mCameraClient;
1056 if (c != 0) {
1057 mLock.unlock();
1058 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1059 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1060 }
1061 disableMsgType(CAMERA_MSG_SHUTTER);
1062
Mathias Agopian65ab4712010-07-14 17:59:35 -07001063 mLock.unlock();
1064}
1065
1066// preview callback - frame buffer update
Wu-cheng Li57c86182011-07-30 05:00:37 +08001067void CameraService::Client::handlePreviewData(int32_t msgType,
1068 const sp<IMemory>& mem,
1069 camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001070 ssize_t offset;
1071 size_t size;
1072 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1073
Mathias Agopian65ab4712010-07-14 17:59:35 -07001074 // local copy of the callback flags
1075 int flags = mPreviewCallbackFlag;
1076
1077 // is callback enabled?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001078 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001079 // If the enable bit is off, the copy-out and one-shot bits are ignored
1080 LOG2("frame callback is disabled");
1081 mLock.unlock();
1082 return;
1083 }
1084
1085 // hold a strong pointer to the client
1086 sp<ICameraClient> c = mCameraClient;
1087
1088 // clear callback flags if no client or one-shot mode
Iliyan Malchev9e626522011-04-14 16:51:21 -07001089 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001090 LOG2("Disable preview callback");
Iliyan Malchev9e626522011-04-14 16:51:21 -07001091 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1092 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1093 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Li0667de72010-09-03 16:40:32 -07001094 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001095 }
1096
1097 if (c != 0) {
1098 // Is the received frame copied out or not?
Iliyan Malchev9e626522011-04-14 16:51:21 -07001099 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001100 LOG2("frame is copied");
Wu-cheng Li57c86182011-07-30 05:00:37 +08001101 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001102 } else {
1103 LOG2("frame is forwarded");
1104 mLock.unlock();
Wu-cheng Li57c86182011-07-30 05:00:37 +08001105 c->dataCallback(msgType, mem, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001106 }
1107 } else {
1108 mLock.unlock();
1109 }
1110}
1111
1112// picture callback - postview image ready
1113void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1114 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1115
1116 sp<ICameraClient> c = mCameraClient;
1117 mLock.unlock();
1118 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001119 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001120 }
1121}
1122
1123// picture callback - raw image ready
1124void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1125 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1126
1127 ssize_t offset;
1128 size_t size;
1129 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1130
Mathias Agopian65ab4712010-07-14 17:59:35 -07001131 sp<ICameraClient> c = mCameraClient;
1132 mLock.unlock();
1133 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001134 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001135 }
1136}
1137
1138// picture callback - compressed picture ready
1139void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1140 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1141
1142 sp<ICameraClient> c = mCameraClient;
1143 mLock.unlock();
1144 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001145 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001146 }
1147}
1148
1149
1150void CameraService::Client::handleGenericNotify(int32_t msgType,
1151 int32_t ext1, int32_t ext2) {
1152 sp<ICameraClient> c = mCameraClient;
1153 mLock.unlock();
1154 if (c != 0) {
1155 c->notifyCallback(msgType, ext1, ext2);
1156 }
1157}
1158
1159void CameraService::Client::handleGenericData(int32_t msgType,
Wu-cheng Li57c86182011-07-30 05:00:37 +08001160 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001161 sp<ICameraClient> c = mCameraClient;
1162 mLock.unlock();
1163 if (c != 0) {
Wu-cheng Li57c86182011-07-30 05:00:37 +08001164 c->dataCallback(msgType, dataPtr, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001165 }
1166}
1167
1168void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1169 int32_t msgType, const sp<IMemory>& dataPtr) {
1170 sp<ICameraClient> c = mCameraClient;
1171 mLock.unlock();
1172 if (c != 0) {
1173 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1174 }
1175}
1176
1177void CameraService::Client::copyFrameAndPostCopiedFrame(
Wu-cheng Li57c86182011-07-30 05:00:37 +08001178 int32_t msgType, const sp<ICameraClient>& client,
1179 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1180 camera_frame_metadata_t *metadata) {
Mathias Agopian65ab4712010-07-14 17:59:35 -07001181 LOG2("copyFrameAndPostCopiedFrame");
1182 // It is necessary to copy out of pmem before sending this to
1183 // the callback. For efficiency, reuse the same MemoryHeapBase
1184 // provided it's big enough. Don't allocate the memory or
1185 // perform the copy if there's no callback.
1186 // hold the preview lock while we grab a reference to the preview buffer
1187 sp<MemoryHeapBase> previewBuffer;
1188
1189 if (mPreviewBuffer == 0) {
1190 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1191 } else if (size > mPreviewBuffer->virtualSize()) {
1192 mPreviewBuffer.clear();
1193 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1194 }
1195 if (mPreviewBuffer == 0) {
1196 LOGE("failed to allocate space for preview buffer");
1197 mLock.unlock();
1198 return;
1199 }
1200 previewBuffer = mPreviewBuffer;
1201
1202 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
1203
1204 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1205 if (frame == 0) {
1206 LOGE("failed to allocate space for frame callback");
1207 mLock.unlock();
1208 return;
1209 }
1210
1211 mLock.unlock();
Wu-cheng Li57c86182011-07-30 05:00:37 +08001212 client->dataCallback(msgType, frame, metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001213}
1214
Wu-cheng Lie09591e2010-10-14 20:17:44 +08001215int CameraService::Client::getOrientation(int degrees, bool mirror) {
1216 if (!mirror) {
1217 if (degrees == 0) return 0;
1218 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1219 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1220 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1221 } else { // Do mirror (horizontal flip)
1222 if (degrees == 0) { // FLIP_H and ROT_0
1223 return HAL_TRANSFORM_FLIP_H;
1224 } else if (degrees == 90) { // FLIP_H and ROT_90
1225 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1226 } else if (degrees == 180) { // FLIP_H and ROT_180
1227 return HAL_TRANSFORM_FLIP_V;
1228 } else if (degrees == 270) { // FLIP_H and ROT_270
1229 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1230 }
1231 }
1232 LOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1233 return -1;
1234}
1235
1236
Mathias Agopian65ab4712010-07-14 17:59:35 -07001237// ----------------------------------------------------------------------------
1238
1239static const int kDumpLockRetries = 50;
1240static const int kDumpLockSleep = 60000;
1241
1242static bool tryLock(Mutex& mutex)
1243{
1244 bool locked = false;
1245 for (int i = 0; i < kDumpLockRetries; ++i) {
1246 if (mutex.tryLock() == NO_ERROR) {
1247 locked = true;
1248 break;
1249 }
1250 usleep(kDumpLockSleep);
1251 }
1252 return locked;
1253}
1254
1255status_t CameraService::dump(int fd, const Vector<String16>& args) {
1256 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1257
1258 const size_t SIZE = 256;
1259 char buffer[SIZE];
1260 String8 result;
1261 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1262 snprintf(buffer, SIZE, "Permission Denial: "
1263 "can't dump CameraService from pid=%d, uid=%d\n",
1264 getCallingPid(),
1265 getCallingUid());
1266 result.append(buffer);
1267 write(fd, result.string(), result.size());
1268 } else {
1269 bool locked = tryLock(mServiceLock);
1270 // failed to lock - CameraService is probably deadlocked
1271 if (!locked) {
1272 String8 result(kDeadlockedString);
1273 write(fd, result.string(), result.size());
1274 }
1275
1276 bool hasClient = false;
1277 for (int i = 0; i < mNumberOfCameras; i++) {
1278 sp<Client> client = mClient[i].promote();
1279 if (client == 0) continue;
1280 hasClient = true;
1281 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1282 i,
1283 client->getCameraClient()->asBinder().get(),
1284 client->mClientPid);
1285 result.append(buffer);
1286 write(fd, result.string(), result.size());
1287 client->mHardware->dump(fd, args);
1288 }
1289 if (!hasClient) {
1290 result.append("No camera client yet.\n");
1291 write(fd, result.string(), result.size());
1292 }
1293
1294 if (locked) mServiceLock.unlock();
1295
1296 // change logging level
1297 int n = args.size();
1298 for (int i = 0; i + 1 < n; i++) {
1299 if (args[i] == String16("-v")) {
1300 String8 levelStr(args[i+1]);
1301 int level = atoi(levelStr.string());
1302 sprintf(buffer, "Set Log Level to %d", level);
1303 result.append(buffer);
1304 setLogLevel(level);
1305 }
1306 }
1307 }
1308 return NO_ERROR;
1309}
1310
Mathias Agopian65ab4712010-07-14 17:59:35 -07001311}; // namespace android