blob: fb6b67893a69575a7469e343dd628aa0ddbf8016 [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
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070023#include "api1/CameraClient.h"
24#include "device1/CameraHardwareInterface.h"
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070025#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();
Zhijun Heb10cdad2014-06-16 16:38:35 -070082 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070083 }
84
85 mHardware->setCallbacks(notifyCallback,
86 dataCallback,
87 dataCallbackTimestamp,
Kévin PETIT377b2ec2014-02-03 12:35:36 +000088 (void *)(uintptr_t)mCameraId);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070089
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
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700311// set the buffer consumer that the preview will use
312status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800313 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700314 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700315 getCallingPid());
316
317 sp<IBinder> binder;
318 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800319 if (bufferProducer != 0) {
320 binder = bufferProducer->asBinder();
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700321 // Using controlledByApp flag to ensure that the buffer queue remains in
322 // async mode for the old camera API, where many applications depend
323 // on that behavior.
324 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700325 }
326 return setPreviewWindow(binder, window);
327}
328
329// set the preview callback flag to affect how the received frames from
330// preview are handled.
331void CameraClient::setPreviewCallbackFlag(int callback_flag) {
332 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
333 Mutex::Autolock lock(mLock);
334 if (checkPidAndHardware() != NO_ERROR) return;
335
336 mPreviewCallbackFlag = callback_flag;
337 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
338 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
339 } else {
340 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
341 }
342}
343
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700344status_t CameraClient::setPreviewCallbackTarget(
345 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700346 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700347 ALOGE("%s: Unimplemented!", __FUNCTION__);
348 return INVALID_OPERATION;
349}
350
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700351// start preview mode
352status_t CameraClient::startPreview() {
353 LOG1("startPreview (pid %d)", getCallingPid());
354 return startCameraMode(CAMERA_PREVIEW_MODE);
355}
356
357// start recording mode
358status_t CameraClient::startRecording() {
359 LOG1("startRecording (pid %d)", getCallingPid());
360 return startCameraMode(CAMERA_RECORDING_MODE);
361}
362
363// start preview or recording
364status_t CameraClient::startCameraMode(camera_mode mode) {
365 LOG1("startCameraMode(%d)", mode);
366 Mutex::Autolock lock(mLock);
367 status_t result = checkPidAndHardware();
368 if (result != NO_ERROR) return result;
369
370 switch(mode) {
371 case CAMERA_PREVIEW_MODE:
372 if (mSurface == 0 && mPreviewWindow == 0) {
373 LOG1("mSurface is not set yet.");
374 // still able to start preview in this case.
375 }
376 return startPreviewMode();
377 case CAMERA_RECORDING_MODE:
378 if (mSurface == 0 && mPreviewWindow == 0) {
379 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
380 return INVALID_OPERATION;
381 }
382 return startRecordingMode();
383 default:
384 return UNKNOWN_ERROR;
385 }
386}
387
388status_t CameraClient::startPreviewMode() {
389 LOG1("startPreviewMode");
390 status_t result = NO_ERROR;
391
392 // if preview has been enabled, nothing needs to be done
393 if (mHardware->previewEnabled()) {
394 return NO_ERROR;
395 }
396
397 if (mPreviewWindow != 0) {
398 native_window_set_scaling_mode(mPreviewWindow.get(),
399 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
400 native_window_set_buffers_transform(mPreviewWindow.get(),
401 mOrientation);
402 }
403 mHardware->setPreviewWindow(mPreviewWindow);
404 result = mHardware->startPreview();
405
406 return result;
407}
408
409status_t CameraClient::startRecordingMode() {
410 LOG1("startRecordingMode");
411 status_t result = NO_ERROR;
412
413 // if recording has been enabled, nothing needs to be done
414 if (mHardware->recordingEnabled()) {
415 return NO_ERROR;
416 }
417
418 // if preview has not been started, start preview first
419 if (!mHardware->previewEnabled()) {
420 result = startPreviewMode();
421 if (result != NO_ERROR) {
422 return result;
423 }
424 }
425
426 // start recording mode
427 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
428 mCameraService->playSound(CameraService::SOUND_RECORDING);
429 result = mHardware->startRecording();
430 if (result != NO_ERROR) {
431 ALOGE("mHardware->startRecording() failed with status %d", result);
432 }
433 return result;
434}
435
436// stop preview mode
437void CameraClient::stopPreview() {
438 LOG1("stopPreview (pid %d)", getCallingPid());
439 Mutex::Autolock lock(mLock);
440 if (checkPidAndHardware() != NO_ERROR) return;
441
442
443 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
444 mHardware->stopPreview();
445
446 mPreviewBuffer.clear();
447}
448
449// stop recording mode
450void CameraClient::stopRecording() {
451 LOG1("stopRecording (pid %d)", getCallingPid());
452 Mutex::Autolock lock(mLock);
453 if (checkPidAndHardware() != NO_ERROR) return;
454
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700455 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
456 mHardware->stopRecording();
Patric Frederiksen35f859b2012-07-10 13:38:35 +0200457 mCameraService->playSound(CameraService::SOUND_RECORDING);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700458
459 mPreviewBuffer.clear();
460}
461
462// release a recording frame
463void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
464 Mutex::Autolock lock(mLock);
465 if (checkPidAndHardware() != NO_ERROR) return;
466 mHardware->releaseRecordingFrame(mem);
467}
468
469status_t CameraClient::storeMetaDataInBuffers(bool enabled)
470{
471 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
472 Mutex::Autolock lock(mLock);
473 if (checkPidAndHardware() != NO_ERROR) {
474 return UNKNOWN_ERROR;
475 }
476 return mHardware->storeMetaDataInBuffers(enabled);
477}
478
479bool CameraClient::previewEnabled() {
480 LOG1("previewEnabled (pid %d)", getCallingPid());
481
482 Mutex::Autolock lock(mLock);
483 if (checkPidAndHardware() != NO_ERROR) return false;
484 return mHardware->previewEnabled();
485}
486
487bool CameraClient::recordingEnabled() {
488 LOG1("recordingEnabled (pid %d)", getCallingPid());
489
490 Mutex::Autolock lock(mLock);
491 if (checkPidAndHardware() != NO_ERROR) return false;
492 return mHardware->recordingEnabled();
493}
494
495status_t CameraClient::autoFocus() {
496 LOG1("autoFocus (pid %d)", getCallingPid());
497
498 Mutex::Autolock lock(mLock);
499 status_t result = checkPidAndHardware();
500 if (result != NO_ERROR) return result;
501
502 return mHardware->autoFocus();
503}
504
505status_t CameraClient::cancelAutoFocus() {
506 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
507
508 Mutex::Autolock lock(mLock);
509 status_t result = checkPidAndHardware();
510 if (result != NO_ERROR) return result;
511
512 return mHardware->cancelAutoFocus();
513}
514
515// take a picture - image is returned in callback
516status_t CameraClient::takePicture(int msgType) {
517 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
518
519 Mutex::Autolock lock(mLock);
520 status_t result = checkPidAndHardware();
521 if (result != NO_ERROR) return result;
522
523 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
524 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
525 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
526 " cannot be both enabled");
527 return BAD_VALUE;
528 }
529
530 // We only accept picture related message types
531 // and ignore other types of messages for takePicture().
532 int picMsgType = msgType
533 & (CAMERA_MSG_SHUTTER |
534 CAMERA_MSG_POSTVIEW_FRAME |
535 CAMERA_MSG_RAW_IMAGE |
536 CAMERA_MSG_RAW_IMAGE_NOTIFY |
537 CAMERA_MSG_COMPRESSED_IMAGE);
538
539 enableMsgType(picMsgType);
540
541 return mHardware->takePicture();
542}
543
544// set preview/capture parameters - key/value pairs
545status_t CameraClient::setParameters(const String8& params) {
546 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
547
548 Mutex::Autolock lock(mLock);
549 status_t result = checkPidAndHardware();
550 if (result != NO_ERROR) return result;
551
552 CameraParameters p(params);
553 return mHardware->setParameters(p);
554}
555
556// get preview/capture parameters - key/value pairs
557String8 CameraClient::getParameters() const {
558 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700559 // The camera service can unconditionally get the parameters at all times
560 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700561
562 String8 params(mHardware->getParameters().flatten());
563 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
564 return params;
565}
566
567// enable shutter sound
568status_t CameraClient::enableShutterSound(bool enable) {
569 LOG1("enableShutterSound (pid %d)", getCallingPid());
570
571 status_t result = checkPidAndHardware();
572 if (result != NO_ERROR) return result;
573
574 if (enable) {
575 mPlayShutterSound = true;
576 return OK;
577 }
578
579 // Disabling shutter sound may not be allowed. In that case only
580 // allow the mediaserver process to disable the sound.
581 char value[PROPERTY_VALUE_MAX];
582 property_get("ro.camera.sound.forced", value, "0");
583 if (strcmp(value, "0") != 0) {
584 // Disabling shutter sound is not allowed. Deny if the current
585 // process is not mediaserver.
586 if (getCallingPid() != getpid()) {
587 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
588 return PERMISSION_DENIED;
589 }
590 }
591
592 mPlayShutterSound = false;
593 return OK;
594}
595
596status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
597 LOG1("sendCommand (pid %d)", getCallingPid());
598 int orientation;
599 Mutex::Autolock lock(mLock);
600 status_t result = checkPidAndHardware();
601 if (result != NO_ERROR) return result;
602
603 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
604 // Mirror the preview if the camera is front-facing.
605 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
606 if (orientation == -1) return BAD_VALUE;
607
608 if (mOrientation != orientation) {
609 mOrientation = orientation;
610 if (mPreviewWindow != 0) {
611 native_window_set_buffers_transform(mPreviewWindow.get(),
612 mOrientation);
613 }
614 }
615 return OK;
616 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
617 switch (arg1) {
618 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700619 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700620 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700621 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700622 default:
623 return BAD_VALUE;
624 }
625 return OK;
626 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
627 mCameraService->playSound(CameraService::SOUND_RECORDING);
James Dong983cf232012-08-01 16:39:55 -0700628 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
629 // Silently ignore this command
630 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700631 } else if (cmd == CAMERA_CMD_PING) {
632 // If mHardware is 0, checkPidAndHardware will return error.
633 return OK;
634 }
635
636 return mHardware->sendCommand(cmd, arg1, arg2);
637}
638
639// ----------------------------------------------------------------------------
640
641void CameraClient::enableMsgType(int32_t msgType) {
642 android_atomic_or(msgType, &mMsgEnabled);
643 mHardware->enableMsgType(msgType);
644}
645
646void CameraClient::disableMsgType(int32_t msgType) {
647 android_atomic_and(~msgType, &mMsgEnabled);
648 mHardware->disableMsgType(msgType);
649}
650
651#define CHECK_MESSAGE_INTERVAL 10 // 10ms
652bool CameraClient::lockIfMessageWanted(int32_t msgType) {
653 int sleepCount = 0;
654 while (mMsgEnabled & msgType) {
655 if (mLock.tryLock() == NO_ERROR) {
656 if (sleepCount > 0) {
657 LOG1("lockIfMessageWanted(%d): waited for %d ms",
658 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
659 }
660 return true;
661 }
662 if (sleepCount++ == 0) {
663 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
664 }
665 usleep(CHECK_MESSAGE_INTERVAL * 1000);
666 }
667 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
668 return false;
669}
670
671// Callback messages can be dispatched to internal handlers or pass to our
672// client's callback functions, depending on the message type.
673//
674// notifyCallback:
675// CAMERA_MSG_SHUTTER handleShutter
676// (others) c->notifyCallback
677// dataCallback:
678// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
679// CAMERA_MSG_POSTVIEW_FRAME handlePostview
680// CAMERA_MSG_RAW_IMAGE handleRawPicture
681// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
682// (others) c->dataCallback
683// dataCallbackTimestamp
684// (others) c->dataCallbackTimestamp
685//
686// NOTE: the *Callback functions grab mLock of the client before passing
687// control to handle* functions. So the handle* functions must release the
688// lock before calling the ICameraClient's callbacks, so those callbacks can
689// invoke methods in the Client class again (For example, the preview frame
690// callback may want to releaseRecordingFrame). The handle* functions must
691// release the lock after all accesses to member variables, so it must be
692// handled very carefully.
693
694void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
695 int32_t ext2, void* user) {
696 LOG2("notifyCallback(%d)", msgType);
697
698 Mutex* lock = getClientLockFromCookie(user);
699 if (lock == NULL) return;
700 Mutex::Autolock alock(*lock);
701
702 CameraClient* client =
703 static_cast<CameraClient*>(getClientFromCookie(user));
704 if (client == NULL) return;
705
706 if (!client->lockIfMessageWanted(msgType)) return;
707
708 switch (msgType) {
709 case CAMERA_MSG_SHUTTER:
710 // ext1 is the dimension of the yuv picture.
711 client->handleShutter();
712 break;
713 default:
714 client->handleGenericNotify(msgType, ext1, ext2);
715 break;
716 }
717}
718
719void CameraClient::dataCallback(int32_t msgType,
720 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
721 LOG2("dataCallback(%d)", msgType);
722
723 Mutex* lock = getClientLockFromCookie(user);
724 if (lock == NULL) return;
725 Mutex::Autolock alock(*lock);
726
727 CameraClient* client =
728 static_cast<CameraClient*>(getClientFromCookie(user));
729 if (client == NULL) return;
730
731 if (!client->lockIfMessageWanted(msgType)) return;
732 if (dataPtr == 0 && metadata == NULL) {
733 ALOGE("Null data returned in data callback");
734 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
735 return;
736 }
737
738 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
739 case CAMERA_MSG_PREVIEW_FRAME:
740 client->handlePreviewData(msgType, dataPtr, metadata);
741 break;
742 case CAMERA_MSG_POSTVIEW_FRAME:
743 client->handlePostview(dataPtr);
744 break;
745 case CAMERA_MSG_RAW_IMAGE:
746 client->handleRawPicture(dataPtr);
747 break;
748 case CAMERA_MSG_COMPRESSED_IMAGE:
749 client->handleCompressedPicture(dataPtr);
750 break;
751 default:
752 client->handleGenericData(msgType, dataPtr, metadata);
753 break;
754 }
755}
756
757void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
758 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
759 LOG2("dataCallbackTimestamp(%d)", msgType);
760
761 Mutex* lock = getClientLockFromCookie(user);
762 if (lock == NULL) return;
763 Mutex::Autolock alock(*lock);
764
765 CameraClient* client =
766 static_cast<CameraClient*>(getClientFromCookie(user));
767 if (client == NULL) return;
768
769 if (!client->lockIfMessageWanted(msgType)) return;
770
771 if (dataPtr == 0) {
772 ALOGE("Null data returned in data with timestamp callback");
773 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
774 return;
775 }
776
777 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
778}
779
780// snapshot taken callback
781void CameraClient::handleShutter(void) {
782 if (mPlayShutterSound) {
783 mCameraService->playSound(CameraService::SOUND_SHUTTER);
784 }
785
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800786 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700787 if (c != 0) {
788 mLock.unlock();
789 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
790 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
791 }
792 disableMsgType(CAMERA_MSG_SHUTTER);
793
794 mLock.unlock();
795}
796
797// preview callback - frame buffer update
798void CameraClient::handlePreviewData(int32_t msgType,
799 const sp<IMemory>& mem,
800 camera_frame_metadata_t *metadata) {
801 ssize_t offset;
802 size_t size;
803 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
804
805 // local copy of the callback flags
806 int flags = mPreviewCallbackFlag;
807
808 // is callback enabled?
809 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
810 // If the enable bit is off, the copy-out and one-shot bits are ignored
811 LOG2("frame callback is disabled");
812 mLock.unlock();
813 return;
814 }
815
816 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800817 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700818
819 // clear callback flags if no client or one-shot mode
820 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
821 LOG2("Disable preview callback");
822 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
823 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
824 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
825 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
826 }
827
828 if (c != 0) {
829 // Is the received frame copied out or not?
830 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
831 LOG2("frame is copied");
832 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
833 } else {
834 LOG2("frame is forwarded");
835 mLock.unlock();
836 c->dataCallback(msgType, mem, metadata);
837 }
838 } else {
839 mLock.unlock();
840 }
841}
842
843// picture callback - postview image ready
844void CameraClient::handlePostview(const sp<IMemory>& mem) {
845 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
846
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800847 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700848 mLock.unlock();
849 if (c != 0) {
850 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
851 }
852}
853
854// picture callback - raw image ready
855void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
856 disableMsgType(CAMERA_MSG_RAW_IMAGE);
857
858 ssize_t offset;
859 size_t size;
860 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
861
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800862 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700863 mLock.unlock();
864 if (c != 0) {
865 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
866 }
867}
868
869// picture callback - compressed picture ready
870void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
871 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
872
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800873 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700874 mLock.unlock();
875 if (c != 0) {
876 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
877 }
878}
879
880
881void CameraClient::handleGenericNotify(int32_t msgType,
882 int32_t ext1, int32_t ext2) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800883 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700884 mLock.unlock();
885 if (c != 0) {
886 c->notifyCallback(msgType, ext1, ext2);
887 }
888}
889
890void CameraClient::handleGenericData(int32_t msgType,
891 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800892 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700893 mLock.unlock();
894 if (c != 0) {
895 c->dataCallback(msgType, dataPtr, metadata);
896 }
897}
898
899void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
900 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800901 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700902 mLock.unlock();
903 if (c != 0) {
904 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
905 }
906}
907
908void CameraClient::copyFrameAndPostCopiedFrame(
909 int32_t msgType, const sp<ICameraClient>& client,
910 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
911 camera_frame_metadata_t *metadata) {
912 LOG2("copyFrameAndPostCopiedFrame");
913 // It is necessary to copy out of pmem before sending this to
914 // the callback. For efficiency, reuse the same MemoryHeapBase
915 // provided it's big enough. Don't allocate the memory or
916 // perform the copy if there's no callback.
917 // hold the preview lock while we grab a reference to the preview buffer
918 sp<MemoryHeapBase> previewBuffer;
919
920 if (mPreviewBuffer == 0) {
921 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
922 } else if (size > mPreviewBuffer->virtualSize()) {
923 mPreviewBuffer.clear();
924 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
925 }
926 if (mPreviewBuffer == 0) {
927 ALOGE("failed to allocate space for preview buffer");
928 mLock.unlock();
929 return;
930 }
931 previewBuffer = mPreviewBuffer;
932
933 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
934
935 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
936 if (frame == 0) {
937 ALOGE("failed to allocate space for frame callback");
938 mLock.unlock();
939 return;
940 }
941
942 mLock.unlock();
943 client->dataCallback(msgType, frame, metadata);
944}
945
946int CameraClient::getOrientation(int degrees, bool mirror) {
947 if (!mirror) {
948 if (degrees == 0) return 0;
949 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
950 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
951 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
952 } else { // Do mirror (horizontal flip)
953 if (degrees == 0) { // FLIP_H and ROT_0
954 return HAL_TRANSFORM_FLIP_H;
955 } else if (degrees == 90) { // FLIP_H and ROT_90
956 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
957 } else if (degrees == 180) { // FLIP_H and ROT_180
958 return HAL_TRANSFORM_FLIP_V;
959 } else if (degrees == 270) { // FLIP_H and ROT_270
960 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
961 }
962 }
963 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
964 return -1;
965}
966
967}; // namespace android