blob: 2e3a2ed03cf20f64f9ae1271b82728bc12b02d04 [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();
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) {
Eino-Ville Talvalac9ab2b02016-01-13 10:07:04 -0800115 return BasicClient::dump(fd, args);
116}
117
118status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700119 const size_t SIZE = 256;
120 char buffer[SIZE];
121
122 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
123 mCameraId,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800124 getRemoteCallback()->asBinder().get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700125 mClientPid);
126 len = (len > SIZE - 1) ? SIZE - 1 : len;
127 write(fd, buffer, len);
128 return mHardware->dump(fd, args);
129}
130
131// ----------------------------------------------------------------------------
132
133status_t CameraClient::checkPid() const {
134 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700135 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700136
137 ALOGW("attempt to use a locked camera from a different process"
138 " (old pid %d, new pid %d)", mClientPid, callingPid);
139 return EBUSY;
140}
141
142status_t CameraClient::checkPidAndHardware() const {
143 status_t result = checkPid();
144 if (result != NO_ERROR) return result;
145 if (mHardware == 0) {
146 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
147 return INVALID_OPERATION;
148 }
149 return NO_ERROR;
150}
151
152status_t CameraClient::lock() {
153 int callingPid = getCallingPid();
154 LOG1("lock (pid %d)", callingPid);
155 Mutex::Autolock lock(mLock);
156
157 // lock camera to this client if the the camera is unlocked
158 if (mClientPid == 0) {
159 mClientPid = callingPid;
160 return NO_ERROR;
161 }
162
163 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
164 return checkPid();
165}
166
167status_t CameraClient::unlock() {
168 int callingPid = getCallingPid();
169 LOG1("unlock (pid %d)", callingPid);
170 Mutex::Autolock lock(mLock);
171
172 // allow anyone to use camera (after they lock the camera)
173 status_t result = checkPid();
174 if (result == NO_ERROR) {
175 if (mHardware->recordingEnabled()) {
176 ALOGE("Not allowed to unlock camera during recording.");
177 return INVALID_OPERATION;
178 }
179 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800180 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700181 // we need to remove the reference to ICameraClient so that when the app
182 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800183 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700184 }
185 return result;
186}
187
188// connect a new client to the camera
189status_t CameraClient::connect(const sp<ICameraClient>& client) {
190 int callingPid = getCallingPid();
191 LOG1("connect E (pid %d)", callingPid);
192 Mutex::Autolock lock(mLock);
193
194 if (mClientPid != 0 && checkPid() != NO_ERROR) {
195 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
196 mClientPid, callingPid);
197 return EBUSY;
198 }
199
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800200 if (mRemoteCallback != 0 &&
201 (client->asBinder() == mRemoteCallback->asBinder())) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700202 LOG1("Connect to the same client");
203 return NO_ERROR;
204 }
205
206 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
207 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800208 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700209
210 LOG1("connect X (pid %d)", callingPid);
211 return NO_ERROR;
212}
213
214static void disconnectWindow(const sp<ANativeWindow>& window) {
215 if (window != 0) {
216 status_t result = native_window_api_disconnect(window.get(),
217 NATIVE_WINDOW_API_CAMERA);
218 if (result != NO_ERROR) {
219 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
220 result);
221 }
222 }
223}
224
225void CameraClient::disconnect() {
226 int callingPid = getCallingPid();
227 LOG1("disconnect E (pid %d)", callingPid);
228 Mutex::Autolock lock(mLock);
229
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700230 // Allow both client and the media server to disconnect at all times
231 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700232 ALOGW("different client - don't disconnect");
233 return;
234 }
235
236 if (mClientPid <= 0) {
237 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
238 return;
239 }
240
241 // Make sure disconnect() is done once and once only, whether it is called
242 // from the user directly, or called by the destructor.
243 if (mHardware == 0) return;
244
245 LOG1("hardware teardown");
246 // Before destroying mHardware, we must make sure it's in the
247 // idle state.
248 // Turn off all messages.
249 disableMsgType(CAMERA_MSG_ALL_MSGS);
250 mHardware->stopPreview();
251 mHardware->cancelPicture();
252 // Release the hardware resources.
253 mHardware->release();
254
255 // Release the held ANativeWindow resources.
256 if (mPreviewWindow != 0) {
257 disconnectWindow(mPreviewWindow);
258 mPreviewWindow = 0;
259 mHardware->setPreviewWindow(mPreviewWindow);
260 }
261 mHardware.clear();
262
263 CameraService::Client::disconnect();
264
265 LOG1("disconnect X (pid %d)", callingPid);
266}
267
268// ----------------------------------------------------------------------------
269
270status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
271 const sp<ANativeWindow>& window) {
272 Mutex::Autolock lock(mLock);
273 status_t result = checkPidAndHardware();
274 if (result != NO_ERROR) return result;
275
276 // return if no change in surface.
277 if (binder == mSurface) {
278 return NO_ERROR;
279 }
280
281 if (window != 0) {
282 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
283 if (result != NO_ERROR) {
284 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
285 result);
286 return result;
287 }
288 }
289
290 // If preview has been already started, register preview buffers now.
291 if (mHardware->previewEnabled()) {
292 if (window != 0) {
293 native_window_set_scaling_mode(window.get(),
294 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
295 native_window_set_buffers_transform(window.get(), mOrientation);
296 result = mHardware->setPreviewWindow(window);
297 }
298 }
299
300 if (result == NO_ERROR) {
301 // Everything has succeeded. Disconnect the old window and remember the
302 // new window.
303 disconnectWindow(mPreviewWindow);
304 mSurface = binder;
305 mPreviewWindow = window;
306 } else {
307 // Something went wrong after we connected to the new window, so
308 // disconnect here.
309 disconnectWindow(window);
310 }
311
312 return result;
313}
314
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700315// set the buffer consumer that the preview will use
316status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800317 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700318 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700319 getCallingPid());
320
321 sp<IBinder> binder;
322 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800323 if (bufferProducer != 0) {
324 binder = bufferProducer->asBinder();
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700325 // Using controlledByApp flag to ensure that the buffer queue remains in
326 // async mode for the old camera API, where many applications depend
327 // on that behavior.
328 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700329 }
330 return setPreviewWindow(binder, window);
331}
332
333// set the preview callback flag to affect how the received frames from
334// preview are handled.
335void CameraClient::setPreviewCallbackFlag(int callback_flag) {
336 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
337 Mutex::Autolock lock(mLock);
338 if (checkPidAndHardware() != NO_ERROR) return;
339
340 mPreviewCallbackFlag = callback_flag;
341 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
342 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
343 } else {
344 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
345 }
346}
347
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700348status_t CameraClient::setPreviewCallbackTarget(
349 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700350 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700351 ALOGE("%s: Unimplemented!", __FUNCTION__);
352 return INVALID_OPERATION;
353}
354
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700355// start preview mode
356status_t CameraClient::startPreview() {
357 LOG1("startPreview (pid %d)", getCallingPid());
358 return startCameraMode(CAMERA_PREVIEW_MODE);
359}
360
361// start recording mode
362status_t CameraClient::startRecording() {
363 LOG1("startRecording (pid %d)", getCallingPid());
364 return startCameraMode(CAMERA_RECORDING_MODE);
365}
366
367// start preview or recording
368status_t CameraClient::startCameraMode(camera_mode mode) {
369 LOG1("startCameraMode(%d)", mode);
370 Mutex::Autolock lock(mLock);
371 status_t result = checkPidAndHardware();
372 if (result != NO_ERROR) return result;
373
374 switch(mode) {
375 case CAMERA_PREVIEW_MODE:
376 if (mSurface == 0 && mPreviewWindow == 0) {
377 LOG1("mSurface is not set yet.");
378 // still able to start preview in this case.
379 }
380 return startPreviewMode();
381 case CAMERA_RECORDING_MODE:
382 if (mSurface == 0 && mPreviewWindow == 0) {
383 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
384 return INVALID_OPERATION;
385 }
386 return startRecordingMode();
387 default:
388 return UNKNOWN_ERROR;
389 }
390}
391
392status_t CameraClient::startPreviewMode() {
393 LOG1("startPreviewMode");
394 status_t result = NO_ERROR;
395
396 // if preview has been enabled, nothing needs to be done
397 if (mHardware->previewEnabled()) {
398 return NO_ERROR;
399 }
400
401 if (mPreviewWindow != 0) {
402 native_window_set_scaling_mode(mPreviewWindow.get(),
403 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
404 native_window_set_buffers_transform(mPreviewWindow.get(),
405 mOrientation);
406 }
407 mHardware->setPreviewWindow(mPreviewWindow);
408 result = mHardware->startPreview();
409
410 return result;
411}
412
413status_t CameraClient::startRecordingMode() {
414 LOG1("startRecordingMode");
415 status_t result = NO_ERROR;
416
417 // if recording has been enabled, nothing needs to be done
418 if (mHardware->recordingEnabled()) {
419 return NO_ERROR;
420 }
421
422 // if preview has not been started, start preview first
423 if (!mHardware->previewEnabled()) {
424 result = startPreviewMode();
425 if (result != NO_ERROR) {
426 return result;
427 }
428 }
429
430 // start recording mode
431 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
432 mCameraService->playSound(CameraService::SOUND_RECORDING);
433 result = mHardware->startRecording();
434 if (result != NO_ERROR) {
435 ALOGE("mHardware->startRecording() failed with status %d", result);
436 }
437 return result;
438}
439
440// stop preview mode
441void CameraClient::stopPreview() {
442 LOG1("stopPreview (pid %d)", getCallingPid());
443 Mutex::Autolock lock(mLock);
444 if (checkPidAndHardware() != NO_ERROR) return;
445
446
447 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
448 mHardware->stopPreview();
449
450 mPreviewBuffer.clear();
451}
452
453// stop recording mode
454void CameraClient::stopRecording() {
455 LOG1("stopRecording (pid %d)", getCallingPid());
456 Mutex::Autolock lock(mLock);
457 if (checkPidAndHardware() != NO_ERROR) return;
458
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700459 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
460 mHardware->stopRecording();
Patric Frederiksen35f859b2012-07-10 13:38:35 +0200461 mCameraService->playSound(CameraService::SOUND_RECORDING);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700462
463 mPreviewBuffer.clear();
464}
465
466// release a recording frame
467void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
468 Mutex::Autolock lock(mLock);
469 if (checkPidAndHardware() != NO_ERROR) return;
470 mHardware->releaseRecordingFrame(mem);
471}
472
473status_t CameraClient::storeMetaDataInBuffers(bool enabled)
474{
475 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
476 Mutex::Autolock lock(mLock);
477 if (checkPidAndHardware() != NO_ERROR) {
478 return UNKNOWN_ERROR;
479 }
480 return mHardware->storeMetaDataInBuffers(enabled);
481}
482
483bool CameraClient::previewEnabled() {
484 LOG1("previewEnabled (pid %d)", getCallingPid());
485
486 Mutex::Autolock lock(mLock);
487 if (checkPidAndHardware() != NO_ERROR) return false;
488 return mHardware->previewEnabled();
489}
490
491bool CameraClient::recordingEnabled() {
492 LOG1("recordingEnabled (pid %d)", getCallingPid());
493
494 Mutex::Autolock lock(mLock);
495 if (checkPidAndHardware() != NO_ERROR) return false;
496 return mHardware->recordingEnabled();
497}
498
499status_t CameraClient::autoFocus() {
500 LOG1("autoFocus (pid %d)", getCallingPid());
501
502 Mutex::Autolock lock(mLock);
503 status_t result = checkPidAndHardware();
504 if (result != NO_ERROR) return result;
505
506 return mHardware->autoFocus();
507}
508
509status_t CameraClient::cancelAutoFocus() {
510 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
511
512 Mutex::Autolock lock(mLock);
513 status_t result = checkPidAndHardware();
514 if (result != NO_ERROR) return result;
515
516 return mHardware->cancelAutoFocus();
517}
518
519// take a picture - image is returned in callback
520status_t CameraClient::takePicture(int msgType) {
521 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
522
523 Mutex::Autolock lock(mLock);
524 status_t result = checkPidAndHardware();
525 if (result != NO_ERROR) return result;
526
527 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
528 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
529 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
530 " cannot be both enabled");
531 return BAD_VALUE;
532 }
533
534 // We only accept picture related message types
535 // and ignore other types of messages for takePicture().
536 int picMsgType = msgType
537 & (CAMERA_MSG_SHUTTER |
538 CAMERA_MSG_POSTVIEW_FRAME |
539 CAMERA_MSG_RAW_IMAGE |
540 CAMERA_MSG_RAW_IMAGE_NOTIFY |
541 CAMERA_MSG_COMPRESSED_IMAGE);
542
543 enableMsgType(picMsgType);
544
545 return mHardware->takePicture();
546}
547
548// set preview/capture parameters - key/value pairs
549status_t CameraClient::setParameters(const String8& params) {
550 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
551
552 Mutex::Autolock lock(mLock);
553 status_t result = checkPidAndHardware();
554 if (result != NO_ERROR) return result;
555
556 CameraParameters p(params);
557 return mHardware->setParameters(p);
558}
559
560// get preview/capture parameters - key/value pairs
561String8 CameraClient::getParameters() const {
562 Mutex::Autolock lock(mLock);
563 if (checkPidAndHardware() != NO_ERROR) return String8();
564
565 String8 params(mHardware->getParameters().flatten());
566 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
567 return params;
568}
569
570// enable shutter sound
571status_t CameraClient::enableShutterSound(bool enable) {
572 LOG1("enableShutterSound (pid %d)", getCallingPid());
573
574 status_t result = checkPidAndHardware();
575 if (result != NO_ERROR) return result;
576
577 if (enable) {
578 mPlayShutterSound = true;
579 return OK;
580 }
581
582 // Disabling shutter sound may not be allowed. In that case only
583 // allow the mediaserver process to disable the sound.
584 char value[PROPERTY_VALUE_MAX];
585 property_get("ro.camera.sound.forced", value, "0");
586 if (strcmp(value, "0") != 0) {
587 // Disabling shutter sound is not allowed. Deny if the current
588 // process is not mediaserver.
589 if (getCallingPid() != getpid()) {
590 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
591 return PERMISSION_DENIED;
592 }
593 }
594
595 mPlayShutterSound = false;
596 return OK;
597}
598
599status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
600 LOG1("sendCommand (pid %d)", getCallingPid());
601 int orientation;
602 Mutex::Autolock lock(mLock);
603 status_t result = checkPidAndHardware();
604 if (result != NO_ERROR) return result;
605
606 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
607 // Mirror the preview if the camera is front-facing.
608 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
609 if (orientation == -1) return BAD_VALUE;
610
611 if (mOrientation != orientation) {
612 mOrientation = orientation;
613 if (mPreviewWindow != 0) {
614 native_window_set_buffers_transform(mPreviewWindow.get(),
615 mOrientation);
616 }
617 }
618 return OK;
619 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
620 switch (arg1) {
621 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700622 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700623 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700624 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700625 default:
626 return BAD_VALUE;
627 }
628 return OK;
629 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
630 mCameraService->playSound(CameraService::SOUND_RECORDING);
James Dong983cf232012-08-01 16:39:55 -0700631 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
632 // Silently ignore this command
633 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700634 } else if (cmd == CAMERA_CMD_PING) {
635 // If mHardware is 0, checkPidAndHardware will return error.
636 return OK;
637 }
638
639 return mHardware->sendCommand(cmd, arg1, arg2);
640}
641
642// ----------------------------------------------------------------------------
643
644void CameraClient::enableMsgType(int32_t msgType) {
645 android_atomic_or(msgType, &mMsgEnabled);
646 mHardware->enableMsgType(msgType);
647}
648
649void CameraClient::disableMsgType(int32_t msgType) {
650 android_atomic_and(~msgType, &mMsgEnabled);
651 mHardware->disableMsgType(msgType);
652}
653
654#define CHECK_MESSAGE_INTERVAL 10 // 10ms
655bool CameraClient::lockIfMessageWanted(int32_t msgType) {
656 int sleepCount = 0;
657 while (mMsgEnabled & msgType) {
658 if (mLock.tryLock() == NO_ERROR) {
659 if (sleepCount > 0) {
660 LOG1("lockIfMessageWanted(%d): waited for %d ms",
661 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
662 }
663 return true;
664 }
665 if (sleepCount++ == 0) {
666 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
667 }
668 usleep(CHECK_MESSAGE_INTERVAL * 1000);
669 }
670 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
671 return false;
672}
673
674// Callback messages can be dispatched to internal handlers or pass to our
675// client's callback functions, depending on the message type.
676//
677// notifyCallback:
678// CAMERA_MSG_SHUTTER handleShutter
679// (others) c->notifyCallback
680// dataCallback:
681// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
682// CAMERA_MSG_POSTVIEW_FRAME handlePostview
683// CAMERA_MSG_RAW_IMAGE handleRawPicture
684// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
685// (others) c->dataCallback
686// dataCallbackTimestamp
687// (others) c->dataCallbackTimestamp
688//
689// NOTE: the *Callback functions grab mLock of the client before passing
690// control to handle* functions. So the handle* functions must release the
691// lock before calling the ICameraClient's callbacks, so those callbacks can
692// invoke methods in the Client class again (For example, the preview frame
693// callback may want to releaseRecordingFrame). The handle* functions must
694// release the lock after all accesses to member variables, so it must be
695// handled very carefully.
696
697void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
698 int32_t ext2, void* user) {
699 LOG2("notifyCallback(%d)", msgType);
700
701 Mutex* lock = getClientLockFromCookie(user);
702 if (lock == NULL) return;
703 Mutex::Autolock alock(*lock);
704
705 CameraClient* client =
706 static_cast<CameraClient*>(getClientFromCookie(user));
707 if (client == NULL) return;
708
709 if (!client->lockIfMessageWanted(msgType)) return;
710
711 switch (msgType) {
712 case CAMERA_MSG_SHUTTER:
713 // ext1 is the dimension of the yuv picture.
714 client->handleShutter();
715 break;
716 default:
717 client->handleGenericNotify(msgType, ext1, ext2);
718 break;
719 }
720}
721
722void CameraClient::dataCallback(int32_t msgType,
723 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
724 LOG2("dataCallback(%d)", msgType);
725
726 Mutex* lock = getClientLockFromCookie(user);
727 if (lock == NULL) return;
728 Mutex::Autolock alock(*lock);
729
730 CameraClient* client =
731 static_cast<CameraClient*>(getClientFromCookie(user));
732 if (client == NULL) return;
733
734 if (!client->lockIfMessageWanted(msgType)) return;
735 if (dataPtr == 0 && metadata == NULL) {
736 ALOGE("Null data returned in data callback");
737 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
738 return;
739 }
740
741 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
742 case CAMERA_MSG_PREVIEW_FRAME:
743 client->handlePreviewData(msgType, dataPtr, metadata);
744 break;
745 case CAMERA_MSG_POSTVIEW_FRAME:
746 client->handlePostview(dataPtr);
747 break;
748 case CAMERA_MSG_RAW_IMAGE:
749 client->handleRawPicture(dataPtr);
750 break;
751 case CAMERA_MSG_COMPRESSED_IMAGE:
752 client->handleCompressedPicture(dataPtr);
753 break;
754 default:
755 client->handleGenericData(msgType, dataPtr, metadata);
756 break;
757 }
758}
759
760void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
761 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
762 LOG2("dataCallbackTimestamp(%d)", msgType);
763
764 Mutex* lock = getClientLockFromCookie(user);
765 if (lock == NULL) return;
766 Mutex::Autolock alock(*lock);
767
768 CameraClient* client =
769 static_cast<CameraClient*>(getClientFromCookie(user));
770 if (client == NULL) return;
771
772 if (!client->lockIfMessageWanted(msgType)) return;
773
774 if (dataPtr == 0) {
775 ALOGE("Null data returned in data with timestamp callback");
776 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
777 return;
778 }
779
780 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
781}
782
783// snapshot taken callback
784void CameraClient::handleShutter(void) {
785 if (mPlayShutterSound) {
786 mCameraService->playSound(CameraService::SOUND_SHUTTER);
787 }
788
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800789 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700790 if (c != 0) {
791 mLock.unlock();
792 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
793 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
794 }
795 disableMsgType(CAMERA_MSG_SHUTTER);
796
797 mLock.unlock();
798}
799
800// preview callback - frame buffer update
801void CameraClient::handlePreviewData(int32_t msgType,
802 const sp<IMemory>& mem,
803 camera_frame_metadata_t *metadata) {
804 ssize_t offset;
805 size_t size;
806 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
807
808 // local copy of the callback flags
809 int flags = mPreviewCallbackFlag;
810
811 // is callback enabled?
812 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
813 // If the enable bit is off, the copy-out and one-shot bits are ignored
814 LOG2("frame callback is disabled");
815 mLock.unlock();
816 return;
817 }
818
819 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800820 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700821
822 // clear callback flags if no client or one-shot mode
823 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
824 LOG2("Disable preview callback");
825 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
826 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
827 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
828 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
829 }
830
831 if (c != 0) {
832 // Is the received frame copied out or not?
833 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
834 LOG2("frame is copied");
835 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
836 } else {
837 LOG2("frame is forwarded");
838 mLock.unlock();
839 c->dataCallback(msgType, mem, metadata);
840 }
841 } else {
842 mLock.unlock();
843 }
844}
845
846// picture callback - postview image ready
847void CameraClient::handlePostview(const sp<IMemory>& mem) {
848 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
849
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800850 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700851 mLock.unlock();
852 if (c != 0) {
853 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
854 }
855}
856
857// picture callback - raw image ready
858void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
859 disableMsgType(CAMERA_MSG_RAW_IMAGE);
860
861 ssize_t offset;
862 size_t size;
863 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
864
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800865 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700866 mLock.unlock();
867 if (c != 0) {
868 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
869 }
870}
871
872// picture callback - compressed picture ready
873void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
874 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
875
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800876 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700877 mLock.unlock();
878 if (c != 0) {
879 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
880 }
881}
882
883
884void CameraClient::handleGenericNotify(int32_t msgType,
885 int32_t ext1, int32_t ext2) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800886 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700887 mLock.unlock();
888 if (c != 0) {
889 c->notifyCallback(msgType, ext1, ext2);
890 }
891}
892
893void CameraClient::handleGenericData(int32_t msgType,
894 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800895 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700896 mLock.unlock();
897 if (c != 0) {
898 c->dataCallback(msgType, dataPtr, metadata);
899 }
900}
901
902void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
903 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800904 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700905 mLock.unlock();
906 if (c != 0) {
907 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
908 }
909}
910
911void CameraClient::copyFrameAndPostCopiedFrame(
912 int32_t msgType, const sp<ICameraClient>& client,
913 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
914 camera_frame_metadata_t *metadata) {
915 LOG2("copyFrameAndPostCopiedFrame");
916 // It is necessary to copy out of pmem before sending this to
917 // the callback. For efficiency, reuse the same MemoryHeapBase
918 // provided it's big enough. Don't allocate the memory or
919 // perform the copy if there's no callback.
920 // hold the preview lock while we grab a reference to the preview buffer
921 sp<MemoryHeapBase> previewBuffer;
922
923 if (mPreviewBuffer == 0) {
924 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
925 } else if (size > mPreviewBuffer->virtualSize()) {
926 mPreviewBuffer.clear();
927 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
928 }
929 if (mPreviewBuffer == 0) {
930 ALOGE("failed to allocate space for preview buffer");
931 mLock.unlock();
932 return;
933 }
934 previewBuffer = mPreviewBuffer;
935
936 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
937
938 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
939 if (frame == 0) {
940 ALOGE("failed to allocate space for frame callback");
941 mLock.unlock();
942 return;
943 }
944
945 mLock.unlock();
946 client->dataCallback(msgType, frame, metadata);
947}
948
949int CameraClient::getOrientation(int degrees, bool mirror) {
950 if (!mirror) {
951 if (degrees == 0) return 0;
952 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
953 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
954 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
955 } else { // Do mirror (horizontal flip)
956 if (degrees == 0) { // FLIP_H and ROT_0
957 return HAL_TRANSFORM_FLIP_H;
958 } else if (degrees == 90) { // FLIP_H and ROT_90
959 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
960 } else if (degrees == 180) { // FLIP_H and ROT_180
961 return HAL_TRANSFORM_FLIP_V;
962 } else if (degrees == 270) { // FLIP_H and ROT_270
963 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
964 }
965 }
966 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
967 return -1;
968}
969
970}; // namespace android