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