blob: be78f6953bbca5f5b8e5337a231c133fc18a0e15 [file] [log] [blame]
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraClient"
18//#define LOG_NDEBUG 0
19
20#include <cutils/properties.h>
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070021#include <gui/Surface.h>
22
23#include "CameraClient.h"
24#include "CameraHardwareInterface.h"
25#include "CameraService.h"
26
27namespace android {
28
29#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
30#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
31
32static int getCallingPid() {
33 return IPCThreadState::self()->getCallingPid();
34}
35
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070036CameraClient::CameraClient(const sp<CameraService>& cameraService,
37 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080038 const String16& clientPackageName,
39 int cameraId, int cameraFacing,
40 int clientPid, int clientUid,
41 int servicePid):
42 Client(cameraService, cameraClient, clientPackageName,
43 cameraId, cameraFacing, clientPid, clientUid, servicePid)
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070044{
45 int callingPid = getCallingPid();
46 LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
47
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070048 mHardware = NULL;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070049 mMsgEnabled = 0;
50 mSurface = 0;
51 mPreviewWindow = 0;
52 mDestructionStarted = false;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070053
54 // Callback is disabled by default
55 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
56 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
57 mPlayShutterSound = true;
58 LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
59}
60
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070061status_t CameraClient::initialize(camera_module_t *module) {
62 int callingPid = getCallingPid();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080063 status_t res;
64
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070065 LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
66
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080067 // Verify ops permissions
68 res = startCameraOps();
69 if (res != OK) {
70 return res;
71 }
72
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070073 char camera_device_name[10];
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070074 snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
75
76 mHardware = new CameraHardwareInterface(camera_device_name);
77 res = mHardware->initialize(&module->common);
78 if (res != OK) {
79 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
80 __FUNCTION__, mCameraId, strerror(-res), res);
Igor Murashkin44f120f2012-10-09 14:45:37 -070081 mHardware.clear();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070082 return NO_INIT;
83 }
84
85 mHardware->setCallbacks(notifyCallback,
86 dataCallback,
87 dataCallbackTimestamp,
88 (void *)mCameraId);
89
90 // Enable zoom, error, focus, and metadata messages by default
91 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
92 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
93
94 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
95 return OK;
96}
97
98
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070099// tear down the client
100CameraClient::~CameraClient() {
101 // this lock should never be NULL
102 Mutex* lock = mCameraService->getClientLockById(mCameraId);
103 lock->lock();
104 mDestructionStarted = true;
105 // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect
106 lock->unlock();
107 int callingPid = getCallingPid();
108 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
109
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700110 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700111 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
112}
113
114status_t CameraClient::dump(int fd, const Vector<String16>& args) {
115 const size_t SIZE = 256;
116 char buffer[SIZE];
117
118 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
119 mCameraId,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800120 getRemoteCallback()->asBinder().get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700121 mClientPid);
122 len = (len > SIZE - 1) ? SIZE - 1 : len;
123 write(fd, buffer, len);
124 return mHardware->dump(fd, args);
125}
126
127// ----------------------------------------------------------------------------
128
129status_t CameraClient::checkPid() const {
130 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700131 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700132
133 ALOGW("attempt to use a locked camera from a different process"
134 " (old pid %d, new pid %d)", mClientPid, callingPid);
135 return EBUSY;
136}
137
138status_t CameraClient::checkPidAndHardware() const {
139 status_t result = checkPid();
140 if (result != NO_ERROR) return result;
141 if (mHardware == 0) {
142 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
143 return INVALID_OPERATION;
144 }
145 return NO_ERROR;
146}
147
148status_t CameraClient::lock() {
149 int callingPid = getCallingPid();
150 LOG1("lock (pid %d)", callingPid);
151 Mutex::Autolock lock(mLock);
152
153 // lock camera to this client if the the camera is unlocked
154 if (mClientPid == 0) {
155 mClientPid = callingPid;
156 return NO_ERROR;
157 }
158
159 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
160 return checkPid();
161}
162
163status_t CameraClient::unlock() {
164 int callingPid = getCallingPid();
165 LOG1("unlock (pid %d)", callingPid);
166 Mutex::Autolock lock(mLock);
167
168 // allow anyone to use camera (after they lock the camera)
169 status_t result = checkPid();
170 if (result == NO_ERROR) {
171 if (mHardware->recordingEnabled()) {
172 ALOGE("Not allowed to unlock camera during recording.");
173 return INVALID_OPERATION;
174 }
175 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800176 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700177 // we need to remove the reference to ICameraClient so that when the app
178 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800179 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700180 }
181 return result;
182}
183
184// connect a new client to the camera
185status_t CameraClient::connect(const sp<ICameraClient>& client) {
186 int callingPid = getCallingPid();
187 LOG1("connect E (pid %d)", callingPid);
188 Mutex::Autolock lock(mLock);
189
190 if (mClientPid != 0 && checkPid() != NO_ERROR) {
191 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
192 mClientPid, callingPid);
193 return EBUSY;
194 }
195
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800196 if (mRemoteCallback != 0 &&
197 (client->asBinder() == mRemoteCallback->asBinder())) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700198 LOG1("Connect to the same client");
199 return NO_ERROR;
200 }
201
202 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
203 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800204 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700205
206 LOG1("connect X (pid %d)", callingPid);
207 return NO_ERROR;
208}
209
210static void disconnectWindow(const sp<ANativeWindow>& window) {
211 if (window != 0) {
212 status_t result = native_window_api_disconnect(window.get(),
213 NATIVE_WINDOW_API_CAMERA);
214 if (result != NO_ERROR) {
215 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
216 result);
217 }
218 }
219}
220
221void CameraClient::disconnect() {
222 int callingPid = getCallingPid();
223 LOG1("disconnect E (pid %d)", callingPid);
224 Mutex::Autolock lock(mLock);
225
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700226 // Allow both client and the media server to disconnect at all times
227 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700228 ALOGW("different client - don't disconnect");
229 return;
230 }
231
232 if (mClientPid <= 0) {
233 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
234 return;
235 }
236
237 // Make sure disconnect() is done once and once only, whether it is called
238 // from the user directly, or called by the destructor.
239 if (mHardware == 0) return;
240
241 LOG1("hardware teardown");
242 // Before destroying mHardware, we must make sure it's in the
243 // idle state.
244 // Turn off all messages.
245 disableMsgType(CAMERA_MSG_ALL_MSGS);
246 mHardware->stopPreview();
247 mHardware->cancelPicture();
248 // Release the hardware resources.
249 mHardware->release();
250
251 // Release the held ANativeWindow resources.
252 if (mPreviewWindow != 0) {
253 disconnectWindow(mPreviewWindow);
254 mPreviewWindow = 0;
255 mHardware->setPreviewWindow(mPreviewWindow);
256 }
257 mHardware.clear();
258
259 CameraService::Client::disconnect();
260
261 LOG1("disconnect X (pid %d)", callingPid);
262}
263
264// ----------------------------------------------------------------------------
265
266status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
267 const sp<ANativeWindow>& window) {
268 Mutex::Autolock lock(mLock);
269 status_t result = checkPidAndHardware();
270 if (result != NO_ERROR) return result;
271
272 // return if no change in surface.
273 if (binder == mSurface) {
274 return NO_ERROR;
275 }
276
277 if (window != 0) {
278 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
279 if (result != NO_ERROR) {
280 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
281 result);
282 return result;
283 }
284 }
285
286 // If preview has been already started, register preview buffers now.
287 if (mHardware->previewEnabled()) {
288 if (window != 0) {
289 native_window_set_scaling_mode(window.get(),
290 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
291 native_window_set_buffers_transform(window.get(), mOrientation);
292 result = mHardware->setPreviewWindow(window);
293 }
294 }
295
296 if (result == NO_ERROR) {
297 // Everything has succeeded. Disconnect the old window and remember the
298 // new window.
299 disconnectWindow(mPreviewWindow);
300 mSurface = binder;
301 mPreviewWindow = window;
302 } else {
303 // Something went wrong after we connected to the new window, so
304 // disconnect here.
305 disconnectWindow(window);
306 }
307
308 return result;
309}
310
311// set the Surface that the preview will use
312status_t CameraClient::setPreviewDisplay(const sp<Surface>& surface) {
313 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
314
Mathias Agopian9e1cdea2013-02-19 18:25:33 -0800315 sp<IBinder> binder(surface != 0 ? surface->getIGraphicBufferProducer()->asBinder() : 0);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700316 sp<ANativeWindow> window(surface);
317 return setPreviewWindow(binder, window);
318}
319
Andy McFadden8ba01022012-12-18 09:46:54 -0800320// set the SurfaceTextureClient that the preview will use
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700321status_t CameraClient::setPreviewTexture(
Andy McFadden8ba01022012-12-18 09:46:54 -0800322 const sp<IGraphicBufferProducer>& bufferProducer) {
323 LOG1("setPreviewTexture(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700324 getCallingPid());
325
326 sp<IBinder> binder;
327 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800328 if (bufferProducer != 0) {
329 binder = bufferProducer->asBinder();
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800330 window = new Surface(bufferProducer);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700331 }
332 return setPreviewWindow(binder, window);
333}
334
335// set the preview callback flag to affect how the received frames from
336// preview are handled.
337void CameraClient::setPreviewCallbackFlag(int callback_flag) {
338 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
339 Mutex::Autolock lock(mLock);
340 if (checkPidAndHardware() != NO_ERROR) return;
341
342 mPreviewCallbackFlag = callback_flag;
343 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
344 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
345 } else {
346 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
347 }
348}
349
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700350status_t CameraClient::setPreviewCallbackTarget(
351 const sp<IGraphicBufferProducer>& callbackProducer) {
352 ALOGE("%s: Unimplemented!", __FUNCTION__);
353 return INVALID_OPERATION;
354}
355
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700356// start preview mode
357status_t CameraClient::startPreview() {
358 LOG1("startPreview (pid %d)", getCallingPid());
359 return startCameraMode(CAMERA_PREVIEW_MODE);
360}
361
362// start recording mode
363status_t CameraClient::startRecording() {
364 LOG1("startRecording (pid %d)", getCallingPid());
365 return startCameraMode(CAMERA_RECORDING_MODE);
366}
367
368// start preview or recording
369status_t CameraClient::startCameraMode(camera_mode mode) {
370 LOG1("startCameraMode(%d)", mode);
371 Mutex::Autolock lock(mLock);
372 status_t result = checkPidAndHardware();
373 if (result != NO_ERROR) return result;
374
375 switch(mode) {
376 case CAMERA_PREVIEW_MODE:
377 if (mSurface == 0 && mPreviewWindow == 0) {
378 LOG1("mSurface is not set yet.");
379 // still able to start preview in this case.
380 }
381 return startPreviewMode();
382 case CAMERA_RECORDING_MODE:
383 if (mSurface == 0 && mPreviewWindow == 0) {
384 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
385 return INVALID_OPERATION;
386 }
387 return startRecordingMode();
388 default:
389 return UNKNOWN_ERROR;
390 }
391}
392
393status_t CameraClient::startPreviewMode() {
394 LOG1("startPreviewMode");
395 status_t result = NO_ERROR;
396
397 // if preview has been enabled, nothing needs to be done
398 if (mHardware->previewEnabled()) {
399 return NO_ERROR;
400 }
401
402 if (mPreviewWindow != 0) {
403 native_window_set_scaling_mode(mPreviewWindow.get(),
404 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
405 native_window_set_buffers_transform(mPreviewWindow.get(),
406 mOrientation);
407 }
408 mHardware->setPreviewWindow(mPreviewWindow);
409 result = mHardware->startPreview();
410
411 return result;
412}
413
414status_t CameraClient::startRecordingMode() {
415 LOG1("startRecordingMode");
416 status_t result = NO_ERROR;
417
418 // if recording has been enabled, nothing needs to be done
419 if (mHardware->recordingEnabled()) {
420 return NO_ERROR;
421 }
422
423 // if preview has not been started, start preview first
424 if (!mHardware->previewEnabled()) {
425 result = startPreviewMode();
426 if (result != NO_ERROR) {
427 return result;
428 }
429 }
430
431 // start recording mode
432 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
433 mCameraService->playSound(CameraService::SOUND_RECORDING);
434 result = mHardware->startRecording();
435 if (result != NO_ERROR) {
436 ALOGE("mHardware->startRecording() failed with status %d", result);
437 }
438 return result;
439}
440
441// stop preview mode
442void CameraClient::stopPreview() {
443 LOG1("stopPreview (pid %d)", getCallingPid());
444 Mutex::Autolock lock(mLock);
445 if (checkPidAndHardware() != NO_ERROR) return;
446
447
448 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
449 mHardware->stopPreview();
450
451 mPreviewBuffer.clear();
452}
453
454// stop recording mode
455void CameraClient::stopRecording() {
456 LOG1("stopRecording (pid %d)", getCallingPid());
457 Mutex::Autolock lock(mLock);
458 if (checkPidAndHardware() != NO_ERROR) return;
459
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700460 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
461 mHardware->stopRecording();
Patric Frederiksen35f859b2012-07-10 13:38:35 +0200462 mCameraService->playSound(CameraService::SOUND_RECORDING);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700463
464 mPreviewBuffer.clear();
465}
466
467// release a recording frame
468void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
469 Mutex::Autolock lock(mLock);
470 if (checkPidAndHardware() != NO_ERROR) return;
471 mHardware->releaseRecordingFrame(mem);
472}
473
474status_t CameraClient::storeMetaDataInBuffers(bool enabled)
475{
476 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
477 Mutex::Autolock lock(mLock);
478 if (checkPidAndHardware() != NO_ERROR) {
479 return UNKNOWN_ERROR;
480 }
481 return mHardware->storeMetaDataInBuffers(enabled);
482}
483
484bool CameraClient::previewEnabled() {
485 LOG1("previewEnabled (pid %d)", getCallingPid());
486
487 Mutex::Autolock lock(mLock);
488 if (checkPidAndHardware() != NO_ERROR) return false;
489 return mHardware->previewEnabled();
490}
491
492bool CameraClient::recordingEnabled() {
493 LOG1("recordingEnabled (pid %d)", getCallingPid());
494
495 Mutex::Autolock lock(mLock);
496 if (checkPidAndHardware() != NO_ERROR) return false;
497 return mHardware->recordingEnabled();
498}
499
500status_t CameraClient::autoFocus() {
501 LOG1("autoFocus (pid %d)", getCallingPid());
502
503 Mutex::Autolock lock(mLock);
504 status_t result = checkPidAndHardware();
505 if (result != NO_ERROR) return result;
506
507 return mHardware->autoFocus();
508}
509
510status_t CameraClient::cancelAutoFocus() {
511 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
512
513 Mutex::Autolock lock(mLock);
514 status_t result = checkPidAndHardware();
515 if (result != NO_ERROR) return result;
516
517 return mHardware->cancelAutoFocus();
518}
519
520// take a picture - image is returned in callback
521status_t CameraClient::takePicture(int msgType) {
522 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
523
524 Mutex::Autolock lock(mLock);
525 status_t result = checkPidAndHardware();
526 if (result != NO_ERROR) return result;
527
528 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
529 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
530 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
531 " cannot be both enabled");
532 return BAD_VALUE;
533 }
534
535 // We only accept picture related message types
536 // and ignore other types of messages for takePicture().
537 int picMsgType = msgType
538 & (CAMERA_MSG_SHUTTER |
539 CAMERA_MSG_POSTVIEW_FRAME |
540 CAMERA_MSG_RAW_IMAGE |
541 CAMERA_MSG_RAW_IMAGE_NOTIFY |
542 CAMERA_MSG_COMPRESSED_IMAGE);
543
544 enableMsgType(picMsgType);
545
546 return mHardware->takePicture();
547}
548
549// set preview/capture parameters - key/value pairs
550status_t CameraClient::setParameters(const String8& params) {
551 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
552
553 Mutex::Autolock lock(mLock);
554 status_t result = checkPidAndHardware();
555 if (result != NO_ERROR) return result;
556
557 CameraParameters p(params);
558 return mHardware->setParameters(p);
559}
560
561// get preview/capture parameters - key/value pairs
562String8 CameraClient::getParameters() const {
563 Mutex::Autolock lock(mLock);
564 if (checkPidAndHardware() != NO_ERROR) return String8();
565
566 String8 params(mHardware->getParameters().flatten());
567 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
568 return params;
569}
570
571// enable shutter sound
572status_t CameraClient::enableShutterSound(bool enable) {
573 LOG1("enableShutterSound (pid %d)", getCallingPid());
574
575 status_t result = checkPidAndHardware();
576 if (result != NO_ERROR) return result;
577
578 if (enable) {
579 mPlayShutterSound = true;
580 return OK;
581 }
582
583 // Disabling shutter sound may not be allowed. In that case only
584 // allow the mediaserver process to disable the sound.
585 char value[PROPERTY_VALUE_MAX];
586 property_get("ro.camera.sound.forced", value, "0");
587 if (strcmp(value, "0") != 0) {
588 // Disabling shutter sound is not allowed. Deny if the current
589 // process is not mediaserver.
590 if (getCallingPid() != getpid()) {
591 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
592 return PERMISSION_DENIED;
593 }
594 }
595
596 mPlayShutterSound = false;
597 return OK;
598}
599
600status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
601 LOG1("sendCommand (pid %d)", getCallingPid());
602 int orientation;
603 Mutex::Autolock lock(mLock);
604 status_t result = checkPidAndHardware();
605 if (result != NO_ERROR) return result;
606
607 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
608 // Mirror the preview if the camera is front-facing.
609 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
610 if (orientation == -1) return BAD_VALUE;
611
612 if (mOrientation != orientation) {
613 mOrientation = orientation;
614 if (mPreviewWindow != 0) {
615 native_window_set_buffers_transform(mPreviewWindow.get(),
616 mOrientation);
617 }
618 }
619 return OK;
620 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
621 switch (arg1) {
622 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700623 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700624 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700625 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700626 default:
627 return BAD_VALUE;
628 }
629 return OK;
630 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
631 mCameraService->playSound(CameraService::SOUND_RECORDING);
James Dong983cf232012-08-01 16:39:55 -0700632 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
633 // Silently ignore this command
634 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700635 } else if (cmd == CAMERA_CMD_PING) {
636 // If mHardware is 0, checkPidAndHardware will return error.
637 return OK;
638 }
639
640 return mHardware->sendCommand(cmd, arg1, arg2);
641}
642
643// ----------------------------------------------------------------------------
644
645void CameraClient::enableMsgType(int32_t msgType) {
646 android_atomic_or(msgType, &mMsgEnabled);
647 mHardware->enableMsgType(msgType);
648}
649
650void CameraClient::disableMsgType(int32_t msgType) {
651 android_atomic_and(~msgType, &mMsgEnabled);
652 mHardware->disableMsgType(msgType);
653}
654
655#define CHECK_MESSAGE_INTERVAL 10 // 10ms
656bool CameraClient::lockIfMessageWanted(int32_t msgType) {
657 int sleepCount = 0;
658 while (mMsgEnabled & msgType) {
659 if (mLock.tryLock() == NO_ERROR) {
660 if (sleepCount > 0) {
661 LOG1("lockIfMessageWanted(%d): waited for %d ms",
662 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
663 }
664 return true;
665 }
666 if (sleepCount++ == 0) {
667 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
668 }
669 usleep(CHECK_MESSAGE_INTERVAL * 1000);
670 }
671 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
672 return false;
673}
674
675// Callback messages can be dispatched to internal handlers or pass to our
676// client's callback functions, depending on the message type.
677//
678// notifyCallback:
679// CAMERA_MSG_SHUTTER handleShutter
680// (others) c->notifyCallback
681// dataCallback:
682// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
683// CAMERA_MSG_POSTVIEW_FRAME handlePostview
684// CAMERA_MSG_RAW_IMAGE handleRawPicture
685// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
686// (others) c->dataCallback
687// dataCallbackTimestamp
688// (others) c->dataCallbackTimestamp
689//
690// NOTE: the *Callback functions grab mLock of the client before passing
691// control to handle* functions. So the handle* functions must release the
692// lock before calling the ICameraClient's callbacks, so those callbacks can
693// invoke methods in the Client class again (For example, the preview frame
694// callback may want to releaseRecordingFrame). The handle* functions must
695// release the lock after all accesses to member variables, so it must be
696// handled very carefully.
697
698void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
699 int32_t ext2, void* user) {
700 LOG2("notifyCallback(%d)", msgType);
701
702 Mutex* lock = getClientLockFromCookie(user);
703 if (lock == NULL) return;
704 Mutex::Autolock alock(*lock);
705
706 CameraClient* client =
707 static_cast<CameraClient*>(getClientFromCookie(user));
708 if (client == NULL) return;
709
710 if (!client->lockIfMessageWanted(msgType)) return;
711
712 switch (msgType) {
713 case CAMERA_MSG_SHUTTER:
714 // ext1 is the dimension of the yuv picture.
715 client->handleShutter();
716 break;
717 default:
718 client->handleGenericNotify(msgType, ext1, ext2);
719 break;
720 }
721}
722
723void CameraClient::dataCallback(int32_t msgType,
724 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
725 LOG2("dataCallback(%d)", msgType);
726
727 Mutex* lock = getClientLockFromCookie(user);
728 if (lock == NULL) return;
729 Mutex::Autolock alock(*lock);
730
731 CameraClient* client =
732 static_cast<CameraClient*>(getClientFromCookie(user));
733 if (client == NULL) return;
734
735 if (!client->lockIfMessageWanted(msgType)) return;
736 if (dataPtr == 0 && metadata == NULL) {
737 ALOGE("Null data returned in data callback");
738 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
739 return;
740 }
741
742 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
743 case CAMERA_MSG_PREVIEW_FRAME:
744 client->handlePreviewData(msgType, dataPtr, metadata);
745 break;
746 case CAMERA_MSG_POSTVIEW_FRAME:
747 client->handlePostview(dataPtr);
748 break;
749 case CAMERA_MSG_RAW_IMAGE:
750 client->handleRawPicture(dataPtr);
751 break;
752 case CAMERA_MSG_COMPRESSED_IMAGE:
753 client->handleCompressedPicture(dataPtr);
754 break;
755 default:
756 client->handleGenericData(msgType, dataPtr, metadata);
757 break;
758 }
759}
760
761void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
762 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
763 LOG2("dataCallbackTimestamp(%d)", msgType);
764
765 Mutex* lock = getClientLockFromCookie(user);
766 if (lock == NULL) return;
767 Mutex::Autolock alock(*lock);
768
769 CameraClient* client =
770 static_cast<CameraClient*>(getClientFromCookie(user));
771 if (client == NULL) return;
772
773 if (!client->lockIfMessageWanted(msgType)) return;
774
775 if (dataPtr == 0) {
776 ALOGE("Null data returned in data with timestamp callback");
777 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
778 return;
779 }
780
781 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
782}
783
784// snapshot taken callback
785void CameraClient::handleShutter(void) {
786 if (mPlayShutterSound) {
787 mCameraService->playSound(CameraService::SOUND_SHUTTER);
788 }
789
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800790 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700791 if (c != 0) {
792 mLock.unlock();
793 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
794 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
795 }
796 disableMsgType(CAMERA_MSG_SHUTTER);
797
798 mLock.unlock();
799}
800
801// preview callback - frame buffer update
802void CameraClient::handlePreviewData(int32_t msgType,
803 const sp<IMemory>& mem,
804 camera_frame_metadata_t *metadata) {
805 ssize_t offset;
806 size_t size;
807 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
808
809 // local copy of the callback flags
810 int flags = mPreviewCallbackFlag;
811
812 // is callback enabled?
813 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
814 // If the enable bit is off, the copy-out and one-shot bits are ignored
815 LOG2("frame callback is disabled");
816 mLock.unlock();
817 return;
818 }
819
820 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800821 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700822
823 // clear callback flags if no client or one-shot mode
824 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
825 LOG2("Disable preview callback");
826 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
827 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
828 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
829 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
830 }
831
832 if (c != 0) {
833 // Is the received frame copied out or not?
834 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
835 LOG2("frame is copied");
836 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
837 } else {
838 LOG2("frame is forwarded");
839 mLock.unlock();
840 c->dataCallback(msgType, mem, metadata);
841 }
842 } else {
843 mLock.unlock();
844 }
845}
846
847// picture callback - postview image ready
848void CameraClient::handlePostview(const sp<IMemory>& mem) {
849 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
850
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800851 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700852 mLock.unlock();
853 if (c != 0) {
854 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
855 }
856}
857
858// picture callback - raw image ready
859void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
860 disableMsgType(CAMERA_MSG_RAW_IMAGE);
861
862 ssize_t offset;
863 size_t size;
864 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
865
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800866 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700867 mLock.unlock();
868 if (c != 0) {
869 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
870 }
871}
872
873// picture callback - compressed picture ready
874void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
875 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
876
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800877 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700878 mLock.unlock();
879 if (c != 0) {
880 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
881 }
882}
883
884
885void CameraClient::handleGenericNotify(int32_t msgType,
886 int32_t ext1, int32_t ext2) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800887 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700888 mLock.unlock();
889 if (c != 0) {
890 c->notifyCallback(msgType, ext1, ext2);
891 }
892}
893
894void CameraClient::handleGenericData(int32_t msgType,
895 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800896 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700897 mLock.unlock();
898 if (c != 0) {
899 c->dataCallback(msgType, dataPtr, metadata);
900 }
901}
902
903void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
904 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800905 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700906 mLock.unlock();
907 if (c != 0) {
908 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
909 }
910}
911
912void CameraClient::copyFrameAndPostCopiedFrame(
913 int32_t msgType, const sp<ICameraClient>& client,
914 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
915 camera_frame_metadata_t *metadata) {
916 LOG2("copyFrameAndPostCopiedFrame");
917 // It is necessary to copy out of pmem before sending this to
918 // the callback. For efficiency, reuse the same MemoryHeapBase
919 // provided it's big enough. Don't allocate the memory or
920 // perform the copy if there's no callback.
921 // hold the preview lock while we grab a reference to the preview buffer
922 sp<MemoryHeapBase> previewBuffer;
923
924 if (mPreviewBuffer == 0) {
925 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
926 } else if (size > mPreviewBuffer->virtualSize()) {
927 mPreviewBuffer.clear();
928 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
929 }
930 if (mPreviewBuffer == 0) {
931 ALOGE("failed to allocate space for preview buffer");
932 mLock.unlock();
933 return;
934 }
935 previewBuffer = mPreviewBuffer;
936
937 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
938
939 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
940 if (frame == 0) {
941 ALOGE("failed to allocate space for frame callback");
942 mLock.unlock();
943 return;
944 }
945
946 mLock.unlock();
947 client->dataCallback(msgType, frame, metadata);
948}
949
950int CameraClient::getOrientation(int degrees, bool mirror) {
951 if (!mirror) {
952 if (degrees == 0) return 0;
953 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
954 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
955 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
956 } else { // Do mirror (horizontal flip)
957 if (degrees == 0) { // FLIP_H and ROT_0
958 return HAL_TRANSFORM_FLIP_H;
959 } else if (degrees == 90) { // FLIP_H and ROT_90
960 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
961 } else if (degrees == 180) { // FLIP_H and ROT_180
962 return HAL_TRANSFORM_FLIP_V;
963 } else if (degrees == 270) { // FLIP_H and ROT_270
964 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
965 }
966 }
967 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
968 return -1;
969}
970
971}; // namespace android