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