blob: d1b929e1d94ae4a8fa555a62916f8f4e95a722de [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,
Igor Murashkina858ea02014-08-19 14:53:08 -070041 int servicePid, bool legacyMode):
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080042 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);
Igor Murashkina858ea02014-08-19 14:53:08 -070057 mLegacyMode = legacyMode;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070058 mPlayShutterSound = true;
59 LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
60}
61
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070062status_t CameraClient::initialize(camera_module_t *module) {
63 int callingPid = getCallingPid();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080064 status_t res;
65
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070066 LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
67
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080068 // Verify ops permissions
69 res = startCameraOps();
70 if (res != OK) {
71 return res;
72 }
73
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070074 char camera_device_name[10];
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070075 snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
76
77 mHardware = new CameraHardwareInterface(camera_device_name);
78 res = mHardware->initialize(&module->common);
79 if (res != OK) {
80 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
81 __FUNCTION__, mCameraId, strerror(-res), res);
Igor Murashkin44f120f2012-10-09 14:45:37 -070082 mHardware.clear();
Zhijun Heb10cdad2014-06-16 16:38:35 -070083 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070084 }
85
86 mHardware->setCallbacks(notifyCallback,
87 dataCallback,
88 dataCallbackTimestamp,
Kévin PETIT377b2ec2014-02-03 12:35:36 +000089 (void *)(uintptr_t)mCameraId);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070090
91 // Enable zoom, error, focus, and metadata messages by default
92 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
93 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
94
95 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
96 return OK;
97}
98
99
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700100// tear down the client
101CameraClient::~CameraClient() {
102 // this lock should never be NULL
103 Mutex* lock = mCameraService->getClientLockById(mCameraId);
104 lock->lock();
105 mDestructionStarted = true;
106 // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect
107 lock->unlock();
108 int callingPid = getCallingPid();
109 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
110
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700111 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700112 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
113}
114
115status_t CameraClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac9ab2b02016-01-13 10:07:04 -0800116 return BasicClient::dump(fd, args);
117}
118
119status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700120 const size_t SIZE = 256;
121 char buffer[SIZE];
122
123 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n",
124 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800125 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -0800126 IInterface::asBinder(getRemoteCallback()).get() : NULL),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700127 mClientPid);
128 len = (len > SIZE - 1) ? SIZE - 1 : len;
129 write(fd, buffer, len);
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700130
131 len = snprintf(buffer, SIZE, "Latest set parameters:\n");
132 len = (len > SIZE - 1) ? SIZE - 1 : len;
133 write(fd, buffer, len);
134
135 mLatestSetParameters.dump(fd, args);
136
137 const char *enddump = "\n\n";
138 write(fd, enddump, strlen(enddump));
139
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700140 return mHardware->dump(fd, args);
141}
142
143// ----------------------------------------------------------------------------
144
145status_t CameraClient::checkPid() const {
146 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700147 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700148
149 ALOGW("attempt to use a locked camera from a different process"
150 " (old pid %d, new pid %d)", mClientPid, callingPid);
151 return EBUSY;
152}
153
154status_t CameraClient::checkPidAndHardware() const {
155 status_t result = checkPid();
156 if (result != NO_ERROR) return result;
157 if (mHardware == 0) {
158 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
159 return INVALID_OPERATION;
160 }
161 return NO_ERROR;
162}
163
164status_t CameraClient::lock() {
165 int callingPid = getCallingPid();
166 LOG1("lock (pid %d)", callingPid);
167 Mutex::Autolock lock(mLock);
168
169 // lock camera to this client if the the camera is unlocked
170 if (mClientPid == 0) {
171 mClientPid = callingPid;
172 return NO_ERROR;
173 }
174
175 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
176 return checkPid();
177}
178
179status_t CameraClient::unlock() {
180 int callingPid = getCallingPid();
181 LOG1("unlock (pid %d)", callingPid);
182 Mutex::Autolock lock(mLock);
183
184 // allow anyone to use camera (after they lock the camera)
185 status_t result = checkPid();
186 if (result == NO_ERROR) {
187 if (mHardware->recordingEnabled()) {
188 ALOGE("Not allowed to unlock camera during recording.");
189 return INVALID_OPERATION;
190 }
191 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800192 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700193 // we need to remove the reference to ICameraClient so that when the app
194 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800195 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700196 }
197 return result;
198}
199
200// connect a new client to the camera
201status_t CameraClient::connect(const sp<ICameraClient>& client) {
202 int callingPid = getCallingPid();
203 LOG1("connect E (pid %d)", callingPid);
204 Mutex::Autolock lock(mLock);
205
206 if (mClientPid != 0 && checkPid() != NO_ERROR) {
207 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
208 mClientPid, callingPid);
209 return EBUSY;
210 }
211
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800212 if (mRemoteCallback != 0 &&
Marco Nelissenf8880202014-11-14 07:58:25 -0800213 (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700214 LOG1("Connect to the same client");
215 return NO_ERROR;
216 }
217
218 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
219 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800220 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700221
222 LOG1("connect X (pid %d)", callingPid);
223 return NO_ERROR;
224}
225
226static void disconnectWindow(const sp<ANativeWindow>& window) {
227 if (window != 0) {
228 status_t result = native_window_api_disconnect(window.get(),
229 NATIVE_WINDOW_API_CAMERA);
230 if (result != NO_ERROR) {
231 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
232 result);
233 }
234 }
235}
236
237void CameraClient::disconnect() {
238 int callingPid = getCallingPid();
239 LOG1("disconnect E (pid %d)", callingPid);
240 Mutex::Autolock lock(mLock);
241
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700242 // Allow both client and the media server to disconnect at all times
243 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700244 ALOGW("different client - don't disconnect");
245 return;
246 }
247
248 if (mClientPid <= 0) {
249 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
250 return;
251 }
252
253 // Make sure disconnect() is done once and once only, whether it is called
254 // from the user directly, or called by the destructor.
255 if (mHardware == 0) return;
256
257 LOG1("hardware teardown");
258 // Before destroying mHardware, we must make sure it's in the
259 // idle state.
260 // Turn off all messages.
261 disableMsgType(CAMERA_MSG_ALL_MSGS);
262 mHardware->stopPreview();
263 mHardware->cancelPicture();
264 // Release the hardware resources.
265 mHardware->release();
266
267 // Release the held ANativeWindow resources.
268 if (mPreviewWindow != 0) {
269 disconnectWindow(mPreviewWindow);
270 mPreviewWindow = 0;
271 mHardware->setPreviewWindow(mPreviewWindow);
272 }
273 mHardware.clear();
274
275 CameraService::Client::disconnect();
276
277 LOG1("disconnect X (pid %d)", callingPid);
278}
279
280// ----------------------------------------------------------------------------
281
282status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
283 const sp<ANativeWindow>& window) {
284 Mutex::Autolock lock(mLock);
285 status_t result = checkPidAndHardware();
286 if (result != NO_ERROR) return result;
287
288 // return if no change in surface.
289 if (binder == mSurface) {
290 return NO_ERROR;
291 }
292
293 if (window != 0) {
294 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
295 if (result != NO_ERROR) {
296 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
297 result);
298 return result;
299 }
300 }
301
302 // If preview has been already started, register preview buffers now.
303 if (mHardware->previewEnabled()) {
304 if (window != 0) {
305 native_window_set_scaling_mode(window.get(),
306 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
307 native_window_set_buffers_transform(window.get(), mOrientation);
308 result = mHardware->setPreviewWindow(window);
309 }
310 }
311
312 if (result == NO_ERROR) {
313 // Everything has succeeded. Disconnect the old window and remember the
314 // new window.
315 disconnectWindow(mPreviewWindow);
316 mSurface = binder;
317 mPreviewWindow = window;
318 } else {
319 // Something went wrong after we connected to the new window, so
320 // disconnect here.
321 disconnectWindow(window);
322 }
323
324 return result;
325}
326
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700327// set the buffer consumer that the preview will use
328status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800329 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700330 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700331 getCallingPid());
332
333 sp<IBinder> binder;
334 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800335 if (bufferProducer != 0) {
Marco Nelissenf8880202014-11-14 07:58:25 -0800336 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700337 // Using controlledByApp flag to ensure that the buffer queue remains in
338 // async mode for the old camera API, where many applications depend
339 // on that behavior.
340 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700341 }
342 return setPreviewWindow(binder, window);
343}
344
345// set the preview callback flag to affect how the received frames from
346// preview are handled.
347void CameraClient::setPreviewCallbackFlag(int callback_flag) {
348 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
349 Mutex::Autolock lock(mLock);
350 if (checkPidAndHardware() != NO_ERROR) return;
351
352 mPreviewCallbackFlag = callback_flag;
353 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
354 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
355 } else {
356 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
357 }
358}
359
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700360status_t CameraClient::setPreviewCallbackTarget(
361 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700362 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700363 ALOGE("%s: Unimplemented!", __FUNCTION__);
364 return INVALID_OPERATION;
365}
366
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700367// start preview mode
368status_t CameraClient::startPreview() {
369 LOG1("startPreview (pid %d)", getCallingPid());
370 return startCameraMode(CAMERA_PREVIEW_MODE);
371}
372
373// start recording mode
374status_t CameraClient::startRecording() {
375 LOG1("startRecording (pid %d)", getCallingPid());
376 return startCameraMode(CAMERA_RECORDING_MODE);
377}
378
379// start preview or recording
380status_t CameraClient::startCameraMode(camera_mode mode) {
381 LOG1("startCameraMode(%d)", mode);
382 Mutex::Autolock lock(mLock);
383 status_t result = checkPidAndHardware();
384 if (result != NO_ERROR) return result;
385
386 switch(mode) {
387 case CAMERA_PREVIEW_MODE:
388 if (mSurface == 0 && mPreviewWindow == 0) {
389 LOG1("mSurface is not set yet.");
390 // still able to start preview in this case.
391 }
392 return startPreviewMode();
393 case CAMERA_RECORDING_MODE:
394 if (mSurface == 0 && mPreviewWindow == 0) {
395 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
396 return INVALID_OPERATION;
397 }
398 return startRecordingMode();
399 default:
400 return UNKNOWN_ERROR;
401 }
402}
403
404status_t CameraClient::startPreviewMode() {
405 LOG1("startPreviewMode");
406 status_t result = NO_ERROR;
407
408 // if preview has been enabled, nothing needs to be done
409 if (mHardware->previewEnabled()) {
410 return NO_ERROR;
411 }
412
413 if (mPreviewWindow != 0) {
414 native_window_set_scaling_mode(mPreviewWindow.get(),
415 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
416 native_window_set_buffers_transform(mPreviewWindow.get(),
417 mOrientation);
418 }
419 mHardware->setPreviewWindow(mPreviewWindow);
420 result = mHardware->startPreview();
421
422 return result;
423}
424
425status_t CameraClient::startRecordingMode() {
426 LOG1("startRecordingMode");
427 status_t result = NO_ERROR;
428
429 // if recording has been enabled, nothing needs to be done
430 if (mHardware->recordingEnabled()) {
431 return NO_ERROR;
432 }
433
434 // if preview has not been started, start preview first
435 if (!mHardware->previewEnabled()) {
436 result = startPreviewMode();
437 if (result != NO_ERROR) {
438 return result;
439 }
440 }
441
442 // start recording mode
443 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
444 mCameraService->playSound(CameraService::SOUND_RECORDING);
445 result = mHardware->startRecording();
446 if (result != NO_ERROR) {
447 ALOGE("mHardware->startRecording() failed with status %d", result);
448 }
449 return result;
450}
451
452// stop preview mode
453void CameraClient::stopPreview() {
454 LOG1("stopPreview (pid %d)", getCallingPid());
455 Mutex::Autolock lock(mLock);
456 if (checkPidAndHardware() != NO_ERROR) return;
457
458
459 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
460 mHardware->stopPreview();
461
462 mPreviewBuffer.clear();
463}
464
465// stop recording mode
466void CameraClient::stopRecording() {
467 LOG1("stopRecording (pid %d)", getCallingPid());
468 Mutex::Autolock lock(mLock);
469 if (checkPidAndHardware() != NO_ERROR) return;
470
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700471 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
472 mHardware->stopRecording();
Patric Frederiksen35f859b2012-07-10 13:38:35 +0200473 mCameraService->playSound(CameraService::SOUND_RECORDING);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700474
475 mPreviewBuffer.clear();
476}
477
478// release a recording frame
479void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
480 Mutex::Autolock lock(mLock);
481 if (checkPidAndHardware() != NO_ERROR) return;
482 mHardware->releaseRecordingFrame(mem);
483}
484
485status_t CameraClient::storeMetaDataInBuffers(bool enabled)
486{
487 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
488 Mutex::Autolock lock(mLock);
489 if (checkPidAndHardware() != NO_ERROR) {
490 return UNKNOWN_ERROR;
491 }
492 return mHardware->storeMetaDataInBuffers(enabled);
493}
494
495bool CameraClient::previewEnabled() {
496 LOG1("previewEnabled (pid %d)", getCallingPid());
497
498 Mutex::Autolock lock(mLock);
499 if (checkPidAndHardware() != NO_ERROR) return false;
500 return mHardware->previewEnabled();
501}
502
503bool CameraClient::recordingEnabled() {
504 LOG1("recordingEnabled (pid %d)", getCallingPid());
505
506 Mutex::Autolock lock(mLock);
507 if (checkPidAndHardware() != NO_ERROR) return false;
508 return mHardware->recordingEnabled();
509}
510
511status_t CameraClient::autoFocus() {
512 LOG1("autoFocus (pid %d)", getCallingPid());
513
514 Mutex::Autolock lock(mLock);
515 status_t result = checkPidAndHardware();
516 if (result != NO_ERROR) return result;
517
518 return mHardware->autoFocus();
519}
520
521status_t CameraClient::cancelAutoFocus() {
522 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
523
524 Mutex::Autolock lock(mLock);
525 status_t result = checkPidAndHardware();
526 if (result != NO_ERROR) return result;
527
528 return mHardware->cancelAutoFocus();
529}
530
531// take a picture - image is returned in callback
532status_t CameraClient::takePicture(int msgType) {
533 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
534
535 Mutex::Autolock lock(mLock);
536 status_t result = checkPidAndHardware();
537 if (result != NO_ERROR) return result;
538
539 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
540 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
541 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
542 " cannot be both enabled");
543 return BAD_VALUE;
544 }
545
546 // We only accept picture related message types
547 // and ignore other types of messages for takePicture().
548 int picMsgType = msgType
549 & (CAMERA_MSG_SHUTTER |
550 CAMERA_MSG_POSTVIEW_FRAME |
551 CAMERA_MSG_RAW_IMAGE |
552 CAMERA_MSG_RAW_IMAGE_NOTIFY |
553 CAMERA_MSG_COMPRESSED_IMAGE);
554
555 enableMsgType(picMsgType);
556
557 return mHardware->takePicture();
558}
559
560// set preview/capture parameters - key/value pairs
561status_t CameraClient::setParameters(const String8& params) {
562 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
563
564 Mutex::Autolock lock(mLock);
565 status_t result = checkPidAndHardware();
566 if (result != NO_ERROR) return result;
567
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700568 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700569 CameraParameters p(params);
570 return mHardware->setParameters(p);
571}
572
573// get preview/capture parameters - key/value pairs
574String8 CameraClient::getParameters() const {
575 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700576 // The camera service can unconditionally get the parameters at all times
577 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700578
579 String8 params(mHardware->getParameters().flatten());
580 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
581 return params;
582}
583
584// enable shutter sound
585status_t CameraClient::enableShutterSound(bool enable) {
586 LOG1("enableShutterSound (pid %d)", getCallingPid());
587
588 status_t result = checkPidAndHardware();
589 if (result != NO_ERROR) return result;
590
591 if (enable) {
592 mPlayShutterSound = true;
593 return OK;
594 }
595
Igor Murashkina858ea02014-08-19 14:53:08 -0700596 // the camera2 api legacy mode can unconditionally disable the shutter sound
597 if (mLegacyMode) {
598 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
599 mPlayShutterSound = false;
600 return OK;
601 }
602
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700603 // Disabling shutter sound may not be allowed. In that case only
604 // allow the mediaserver process to disable the sound.
605 char value[PROPERTY_VALUE_MAX];
606 property_get("ro.camera.sound.forced", value, "0");
607 if (strcmp(value, "0") != 0) {
608 // Disabling shutter sound is not allowed. Deny if the current
609 // process is not mediaserver.
610 if (getCallingPid() != getpid()) {
611 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
612 return PERMISSION_DENIED;
613 }
614 }
615
616 mPlayShutterSound = false;
617 return OK;
618}
619
620status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
621 LOG1("sendCommand (pid %d)", getCallingPid());
622 int orientation;
623 Mutex::Autolock lock(mLock);
624 status_t result = checkPidAndHardware();
625 if (result != NO_ERROR) return result;
626
627 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
628 // Mirror the preview if the camera is front-facing.
629 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
630 if (orientation == -1) return BAD_VALUE;
631
632 if (mOrientation != orientation) {
633 mOrientation = orientation;
634 if (mPreviewWindow != 0) {
635 native_window_set_buffers_transform(mPreviewWindow.get(),
636 mOrientation);
637 }
638 }
639 return OK;
640 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
641 switch (arg1) {
642 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700643 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700644 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700645 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700646 default:
647 return BAD_VALUE;
648 }
649 return OK;
650 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
651 mCameraService->playSound(CameraService::SOUND_RECORDING);
James Dong983cf232012-08-01 16:39:55 -0700652 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
653 // Silently ignore this command
654 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700655 } else if (cmd == CAMERA_CMD_PING) {
656 // If mHardware is 0, checkPidAndHardware will return error.
657 return OK;
658 }
659
660 return mHardware->sendCommand(cmd, arg1, arg2);
661}
662
663// ----------------------------------------------------------------------------
664
665void CameraClient::enableMsgType(int32_t msgType) {
666 android_atomic_or(msgType, &mMsgEnabled);
667 mHardware->enableMsgType(msgType);
668}
669
670void CameraClient::disableMsgType(int32_t msgType) {
671 android_atomic_and(~msgType, &mMsgEnabled);
672 mHardware->disableMsgType(msgType);
673}
674
675#define CHECK_MESSAGE_INTERVAL 10 // 10ms
676bool CameraClient::lockIfMessageWanted(int32_t msgType) {
677 int sleepCount = 0;
678 while (mMsgEnabled & msgType) {
679 if (mLock.tryLock() == NO_ERROR) {
680 if (sleepCount > 0) {
681 LOG1("lockIfMessageWanted(%d): waited for %d ms",
682 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
683 }
684 return true;
685 }
686 if (sleepCount++ == 0) {
687 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
688 }
689 usleep(CHECK_MESSAGE_INTERVAL * 1000);
690 }
691 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
692 return false;
693}
694
695// Callback messages can be dispatched to internal handlers or pass to our
696// client's callback functions, depending on the message type.
697//
698// notifyCallback:
699// CAMERA_MSG_SHUTTER handleShutter
700// (others) c->notifyCallback
701// dataCallback:
702// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
703// CAMERA_MSG_POSTVIEW_FRAME handlePostview
704// CAMERA_MSG_RAW_IMAGE handleRawPicture
705// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
706// (others) c->dataCallback
707// dataCallbackTimestamp
708// (others) c->dataCallbackTimestamp
709//
710// NOTE: the *Callback functions grab mLock of the client before passing
711// control to handle* functions. So the handle* functions must release the
712// lock before calling the ICameraClient's callbacks, so those callbacks can
713// invoke methods in the Client class again (For example, the preview frame
714// callback may want to releaseRecordingFrame). The handle* functions must
715// release the lock after all accesses to member variables, so it must be
716// handled very carefully.
717
718void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
719 int32_t ext2, void* user) {
720 LOG2("notifyCallback(%d)", msgType);
721
722 Mutex* lock = getClientLockFromCookie(user);
723 if (lock == NULL) return;
724 Mutex::Autolock alock(*lock);
725
726 CameraClient* client =
727 static_cast<CameraClient*>(getClientFromCookie(user));
728 if (client == NULL) return;
729
730 if (!client->lockIfMessageWanted(msgType)) return;
731
732 switch (msgType) {
733 case CAMERA_MSG_SHUTTER:
734 // ext1 is the dimension of the yuv picture.
735 client->handleShutter();
736 break;
737 default:
738 client->handleGenericNotify(msgType, ext1, ext2);
739 break;
740 }
741}
742
743void CameraClient::dataCallback(int32_t msgType,
744 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
745 LOG2("dataCallback(%d)", msgType);
746
747 Mutex* lock = getClientLockFromCookie(user);
748 if (lock == NULL) return;
749 Mutex::Autolock alock(*lock);
750
751 CameraClient* client =
752 static_cast<CameraClient*>(getClientFromCookie(user));
753 if (client == NULL) return;
754
755 if (!client->lockIfMessageWanted(msgType)) return;
756 if (dataPtr == 0 && metadata == NULL) {
757 ALOGE("Null data returned in data callback");
758 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
759 return;
760 }
761
762 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
763 case CAMERA_MSG_PREVIEW_FRAME:
764 client->handlePreviewData(msgType, dataPtr, metadata);
765 break;
766 case CAMERA_MSG_POSTVIEW_FRAME:
767 client->handlePostview(dataPtr);
768 break;
769 case CAMERA_MSG_RAW_IMAGE:
770 client->handleRawPicture(dataPtr);
771 break;
772 case CAMERA_MSG_COMPRESSED_IMAGE:
773 client->handleCompressedPicture(dataPtr);
774 break;
775 default:
776 client->handleGenericData(msgType, dataPtr, metadata);
777 break;
778 }
779}
780
781void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
782 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
783 LOG2("dataCallbackTimestamp(%d)", msgType);
784
785 Mutex* lock = getClientLockFromCookie(user);
786 if (lock == NULL) return;
787 Mutex::Autolock alock(*lock);
788
789 CameraClient* client =
790 static_cast<CameraClient*>(getClientFromCookie(user));
791 if (client == NULL) return;
792
793 if (!client->lockIfMessageWanted(msgType)) return;
794
795 if (dataPtr == 0) {
796 ALOGE("Null data returned in data with timestamp callback");
797 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
798 return;
799 }
800
801 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
802}
803
804// snapshot taken callback
805void CameraClient::handleShutter(void) {
806 if (mPlayShutterSound) {
807 mCameraService->playSound(CameraService::SOUND_SHUTTER);
808 }
809
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800810 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700811 if (c != 0) {
812 mLock.unlock();
813 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
814 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
815 }
816 disableMsgType(CAMERA_MSG_SHUTTER);
817
818 mLock.unlock();
819}
820
821// preview callback - frame buffer update
822void CameraClient::handlePreviewData(int32_t msgType,
823 const sp<IMemory>& mem,
824 camera_frame_metadata_t *metadata) {
825 ssize_t offset;
826 size_t size;
827 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
828
829 // local copy of the callback flags
830 int flags = mPreviewCallbackFlag;
831
832 // is callback enabled?
833 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
834 // If the enable bit is off, the copy-out and one-shot bits are ignored
835 LOG2("frame callback is disabled");
836 mLock.unlock();
837 return;
838 }
839
840 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800841 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700842
843 // clear callback flags if no client or one-shot mode
844 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
845 LOG2("Disable preview callback");
846 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
847 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
848 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
849 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
850 }
851
852 if (c != 0) {
853 // Is the received frame copied out or not?
854 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
855 LOG2("frame is copied");
856 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
857 } else {
858 LOG2("frame is forwarded");
859 mLock.unlock();
860 c->dataCallback(msgType, mem, metadata);
861 }
862 } else {
863 mLock.unlock();
864 }
865}
866
867// picture callback - postview image ready
868void CameraClient::handlePostview(const sp<IMemory>& mem) {
869 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
870
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800871 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700872 mLock.unlock();
873 if (c != 0) {
874 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
875 }
876}
877
878// picture callback - raw image ready
879void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
880 disableMsgType(CAMERA_MSG_RAW_IMAGE);
881
882 ssize_t offset;
883 size_t size;
884 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
885
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->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
890 }
891}
892
893// picture callback - compressed picture ready
894void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
895 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
896
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800897 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700898 mLock.unlock();
899 if (c != 0) {
900 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
901 }
902}
903
904
905void CameraClient::handleGenericNotify(int32_t msgType,
906 int32_t ext1, int32_t ext2) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800907 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700908 mLock.unlock();
909 if (c != 0) {
910 c->notifyCallback(msgType, ext1, ext2);
911 }
912}
913
914void CameraClient::handleGenericData(int32_t msgType,
915 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800916 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700917 mLock.unlock();
918 if (c != 0) {
919 c->dataCallback(msgType, dataPtr, metadata);
920 }
921}
922
923void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
924 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800925 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700926 mLock.unlock();
927 if (c != 0) {
928 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
929 }
930}
931
932void CameraClient::copyFrameAndPostCopiedFrame(
933 int32_t msgType, const sp<ICameraClient>& client,
934 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
935 camera_frame_metadata_t *metadata) {
936 LOG2("copyFrameAndPostCopiedFrame");
937 // It is necessary to copy out of pmem before sending this to
938 // the callback. For efficiency, reuse the same MemoryHeapBase
939 // provided it's big enough. Don't allocate the memory or
940 // perform the copy if there's no callback.
941 // hold the preview lock while we grab a reference to the preview buffer
942 sp<MemoryHeapBase> previewBuffer;
943
944 if (mPreviewBuffer == 0) {
945 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
946 } else if (size > mPreviewBuffer->virtualSize()) {
947 mPreviewBuffer.clear();
948 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
949 }
950 if (mPreviewBuffer == 0) {
951 ALOGE("failed to allocate space for preview buffer");
952 mLock.unlock();
953 return;
954 }
955 previewBuffer = mPreviewBuffer;
956
Ruben Brunk65e01f72014-08-29 17:25:13 -0700957 void* previewBufferBase = previewBuffer->base();
958 void* heapBase = heap->base();
959
960 if (heapBase == MAP_FAILED) {
961 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
962 mLock.unlock();
963 return;
964 } else if (previewBufferBase == MAP_FAILED) {
965 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
966 mLock.unlock();
967 return;
968 }
969
970 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700971
972 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
973 if (frame == 0) {
974 ALOGE("failed to allocate space for frame callback");
975 mLock.unlock();
976 return;
977 }
978
979 mLock.unlock();
980 client->dataCallback(msgType, frame, metadata);
981}
982
983int CameraClient::getOrientation(int degrees, bool mirror) {
984 if (!mirror) {
985 if (degrees == 0) return 0;
986 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
987 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
988 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
989 } else { // Do mirror (horizontal flip)
990 if (degrees == 0) { // FLIP_H and ROT_0
991 return HAL_TRANSFORM_FLIP_H;
992 } else if (degrees == 90) { // FLIP_H and ROT_90
993 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
994 } else if (degrees == 180) { // FLIP_H and ROT_180
995 return HAL_TRANSFORM_FLIP_V;
996 } else if (degrees == 270) { // FLIP_H and ROT_270
997 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
998 }
999 }
1000 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1001 return -1;
1002}
1003
1004}; // namespace android