blob: ced9d8c287854c6e1303bab871f68b58934d8ddb [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
Yin-Chia Yehe074a932015-01-30 10:29:02 -080062status_t CameraClient::initialize(CameraModule *module) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070063 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);
Yin-Chia Yehe074a932015-01-30 10:29:02 -080078 res = mHardware->initialize(module);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070079 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() {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700102 mDestructionStarted = true;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700103 int callingPid = getCallingPid();
104 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
105
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700106 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700107 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
108}
109
110status_t CameraClient::dump(int fd, const Vector<String16>& args) {
111 const size_t SIZE = 256;
112 char buffer[SIZE];
113
Ruben Brunkcc776712015-02-17 20:18:47 -0800114 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n",
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700115 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800116 (getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800117 IInterface::asBinder(getRemoteCallback()).get() : NULL),
Ruben Brunkcc776712015-02-17 20:18:47 -0800118 mClientUid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700119 len = (len > SIZE - 1) ? SIZE - 1 : len;
120 write(fd, buffer, len);
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700121
122 len = snprintf(buffer, SIZE, "Latest set parameters:\n");
123 len = (len > SIZE - 1) ? SIZE - 1 : len;
124 write(fd, buffer, len);
125
126 mLatestSetParameters.dump(fd, args);
127
128 const char *enddump = "\n\n";
129 write(fd, enddump, strlen(enddump));
130
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700131 return mHardware->dump(fd, args);
132}
133
134// ----------------------------------------------------------------------------
135
136status_t CameraClient::checkPid() const {
137 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700138 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700139
140 ALOGW("attempt to use a locked camera from a different process"
141 " (old pid %d, new pid %d)", mClientPid, callingPid);
142 return EBUSY;
143}
144
145status_t CameraClient::checkPidAndHardware() const {
146 status_t result = checkPid();
147 if (result != NO_ERROR) return result;
148 if (mHardware == 0) {
149 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
150 return INVALID_OPERATION;
151 }
152 return NO_ERROR;
153}
154
155status_t CameraClient::lock() {
156 int callingPid = getCallingPid();
157 LOG1("lock (pid %d)", callingPid);
158 Mutex::Autolock lock(mLock);
159
160 // lock camera to this client if the the camera is unlocked
161 if (mClientPid == 0) {
162 mClientPid = callingPid;
163 return NO_ERROR;
164 }
165
166 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
167 return checkPid();
168}
169
170status_t CameraClient::unlock() {
171 int callingPid = getCallingPid();
172 LOG1("unlock (pid %d)", callingPid);
173 Mutex::Autolock lock(mLock);
174
175 // allow anyone to use camera (after they lock the camera)
176 status_t result = checkPid();
177 if (result == NO_ERROR) {
178 if (mHardware->recordingEnabled()) {
179 ALOGE("Not allowed to unlock camera during recording.");
180 return INVALID_OPERATION;
181 }
182 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800183 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700184 // we need to remove the reference to ICameraClient so that when the app
185 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800186 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700187 }
188 return result;
189}
190
191// connect a new client to the camera
192status_t CameraClient::connect(const sp<ICameraClient>& client) {
193 int callingPid = getCallingPid();
194 LOG1("connect E (pid %d)", callingPid);
195 Mutex::Autolock lock(mLock);
196
197 if (mClientPid != 0 && checkPid() != NO_ERROR) {
198 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
199 mClientPid, callingPid);
200 return EBUSY;
201 }
202
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800203 if (mRemoteCallback != 0 &&
Marco Nelissen06b46062014-11-14 07:58:25 -0800204 (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700205 LOG1("Connect to the same client");
206 return NO_ERROR;
207 }
208
209 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
210 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800211 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700212
213 LOG1("connect X (pid %d)", callingPid);
214 return NO_ERROR;
215}
216
217static void disconnectWindow(const sp<ANativeWindow>& window) {
218 if (window != 0) {
219 status_t result = native_window_api_disconnect(window.get(),
220 NATIVE_WINDOW_API_CAMERA);
221 if (result != NO_ERROR) {
222 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
223 result);
224 }
225 }
226}
227
228void CameraClient::disconnect() {
229 int callingPid = getCallingPid();
230 LOG1("disconnect E (pid %d)", callingPid);
231 Mutex::Autolock lock(mLock);
232
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700233 // Allow both client and the media server to disconnect at all times
234 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700235 ALOGW("different client - don't disconnect");
236 return;
237 }
238
239 if (mClientPid <= 0) {
240 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
241 return;
242 }
243
244 // Make sure disconnect() is done once and once only, whether it is called
245 // from the user directly, or called by the destructor.
246 if (mHardware == 0) return;
247
248 LOG1("hardware teardown");
249 // Before destroying mHardware, we must make sure it's in the
250 // idle state.
251 // Turn off all messages.
252 disableMsgType(CAMERA_MSG_ALL_MSGS);
253 mHardware->stopPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700254 mCameraService->updateProxyDeviceState(
255 ICameraServiceProxy::CAMERA_STATE_IDLE,
256 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700257 mHardware->cancelPicture();
258 // Release the hardware resources.
259 mHardware->release();
260
261 // Release the held ANativeWindow resources.
262 if (mPreviewWindow != 0) {
263 disconnectWindow(mPreviewWindow);
264 mPreviewWindow = 0;
265 mHardware->setPreviewWindow(mPreviewWindow);
266 }
267 mHardware.clear();
268
269 CameraService::Client::disconnect();
270
271 LOG1("disconnect X (pid %d)", callingPid);
272}
273
274// ----------------------------------------------------------------------------
275
276status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
277 const sp<ANativeWindow>& window) {
278 Mutex::Autolock lock(mLock);
279 status_t result = checkPidAndHardware();
280 if (result != NO_ERROR) return result;
281
282 // return if no change in surface.
283 if (binder == mSurface) {
284 return NO_ERROR;
285 }
286
287 if (window != 0) {
288 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
289 if (result != NO_ERROR) {
290 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
291 result);
292 return result;
293 }
294 }
295
296 // If preview has been already started, register preview buffers now.
297 if (mHardware->previewEnabled()) {
298 if (window != 0) {
299 native_window_set_scaling_mode(window.get(),
300 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
301 native_window_set_buffers_transform(window.get(), mOrientation);
302 result = mHardware->setPreviewWindow(window);
303 }
304 }
305
306 if (result == NO_ERROR) {
307 // Everything has succeeded. Disconnect the old window and remember the
308 // new window.
309 disconnectWindow(mPreviewWindow);
310 mSurface = binder;
311 mPreviewWindow = window;
312 } else {
313 // Something went wrong after we connected to the new window, so
314 // disconnect here.
315 disconnectWindow(window);
316 }
317
318 return result;
319}
320
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700321// set the buffer consumer that the preview will use
322status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800323 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700324 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700325 getCallingPid());
326
327 sp<IBinder> binder;
328 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800329 if (bufferProducer != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800330 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700331 // Using controlledByApp flag to ensure that the buffer queue remains in
332 // async mode for the old camera API, where many applications depend
333 // on that behavior.
334 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700335 }
336 return setPreviewWindow(binder, window);
337}
338
339// set the preview callback flag to affect how the received frames from
340// preview are handled.
341void CameraClient::setPreviewCallbackFlag(int callback_flag) {
342 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
343 Mutex::Autolock lock(mLock);
344 if (checkPidAndHardware() != NO_ERROR) return;
345
346 mPreviewCallbackFlag = callback_flag;
347 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
348 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
349 } else {
350 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
351 }
352}
353
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700354status_t CameraClient::setPreviewCallbackTarget(
355 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700356 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700357 ALOGE("%s: Unimplemented!", __FUNCTION__);
358 return INVALID_OPERATION;
359}
360
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700361// start preview mode
362status_t CameraClient::startPreview() {
363 LOG1("startPreview (pid %d)", getCallingPid());
364 return startCameraMode(CAMERA_PREVIEW_MODE);
365}
366
367// start recording mode
368status_t CameraClient::startRecording() {
369 LOG1("startRecording (pid %d)", getCallingPid());
370 return startCameraMode(CAMERA_RECORDING_MODE);
371}
372
373// start preview or recording
374status_t CameraClient::startCameraMode(camera_mode mode) {
375 LOG1("startCameraMode(%d)", mode);
376 Mutex::Autolock lock(mLock);
377 status_t result = checkPidAndHardware();
378 if (result != NO_ERROR) return result;
379
380 switch(mode) {
381 case CAMERA_PREVIEW_MODE:
382 if (mSurface == 0 && mPreviewWindow == 0) {
383 LOG1("mSurface is not set yet.");
384 // still able to start preview in this case.
385 }
386 return startPreviewMode();
387 case CAMERA_RECORDING_MODE:
388 if (mSurface == 0 && mPreviewWindow == 0) {
389 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
390 return INVALID_OPERATION;
391 }
392 return startRecordingMode();
393 default:
394 return UNKNOWN_ERROR;
395 }
396}
397
398status_t CameraClient::startPreviewMode() {
399 LOG1("startPreviewMode");
400 status_t result = NO_ERROR;
401
402 // if preview has been enabled, nothing needs to be done
403 if (mHardware->previewEnabled()) {
404 return NO_ERROR;
405 }
406
407 if (mPreviewWindow != 0) {
408 native_window_set_scaling_mode(mPreviewWindow.get(),
409 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
410 native_window_set_buffers_transform(mPreviewWindow.get(),
411 mOrientation);
412 }
413 mHardware->setPreviewWindow(mPreviewWindow);
414 result = mHardware->startPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700415 if (result == NO_ERROR) {
416 mCameraService->updateProxyDeviceState(
417 ICameraServiceProxy::CAMERA_STATE_ACTIVE,
418 String8::format("%d", mCameraId));
419 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700420 return result;
421}
422
423status_t CameraClient::startRecordingMode() {
424 LOG1("startRecordingMode");
425 status_t result = NO_ERROR;
426
427 // if recording has been enabled, nothing needs to be done
428 if (mHardware->recordingEnabled()) {
429 return NO_ERROR;
430 }
431
432 // if preview has not been started, start preview first
433 if (!mHardware->previewEnabled()) {
434 result = startPreviewMode();
435 if (result != NO_ERROR) {
436 return result;
437 }
438 }
439
440 // start recording mode
441 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700442 mCameraService->playSound(CameraService::SOUND_RECORDING_START);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700443 result = mHardware->startRecording();
444 if (result != NO_ERROR) {
445 ALOGE("mHardware->startRecording() failed with status %d", result);
446 }
447 return result;
448}
449
450// stop preview mode
451void CameraClient::stopPreview() {
452 LOG1("stopPreview (pid %d)", getCallingPid());
453 Mutex::Autolock lock(mLock);
454 if (checkPidAndHardware() != NO_ERROR) return;
455
456
457 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
458 mHardware->stopPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700459 mCameraService->updateProxyDeviceState(
460 ICameraServiceProxy::CAMERA_STATE_IDLE,
461 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700462 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();
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700473 mCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
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
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800485status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
486 LOG1("setVideoBufferMode: %d", videoBufferMode);
487 bool enableMetadataInBuffers = false;
488
489 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
490 enableMetadataInBuffers = true;
491 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
492 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
493 videoBufferMode);
494 return BAD_VALUE;
495 }
496
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700497 Mutex::Autolock lock(mLock);
498 if (checkPidAndHardware() != NO_ERROR) {
499 return UNKNOWN_ERROR;
500 }
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800501
502 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700503}
504
505bool CameraClient::previewEnabled() {
506 LOG1("previewEnabled (pid %d)", getCallingPid());
507
508 Mutex::Autolock lock(mLock);
509 if (checkPidAndHardware() != NO_ERROR) return false;
510 return mHardware->previewEnabled();
511}
512
513bool CameraClient::recordingEnabled() {
514 LOG1("recordingEnabled (pid %d)", getCallingPid());
515
516 Mutex::Autolock lock(mLock);
517 if (checkPidAndHardware() != NO_ERROR) return false;
518 return mHardware->recordingEnabled();
519}
520
521status_t CameraClient::autoFocus() {
522 LOG1("autoFocus (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->autoFocus();
529}
530
531status_t CameraClient::cancelAutoFocus() {
532 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
533
534 Mutex::Autolock lock(mLock);
535 status_t result = checkPidAndHardware();
536 if (result != NO_ERROR) return result;
537
538 return mHardware->cancelAutoFocus();
539}
540
541// take a picture - image is returned in callback
542status_t CameraClient::takePicture(int msgType) {
543 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
544
545 Mutex::Autolock lock(mLock);
546 status_t result = checkPidAndHardware();
547 if (result != NO_ERROR) return result;
548
549 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
550 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
551 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
552 " cannot be both enabled");
553 return BAD_VALUE;
554 }
555
556 // We only accept picture related message types
557 // and ignore other types of messages for takePicture().
558 int picMsgType = msgType
559 & (CAMERA_MSG_SHUTTER |
560 CAMERA_MSG_POSTVIEW_FRAME |
561 CAMERA_MSG_RAW_IMAGE |
562 CAMERA_MSG_RAW_IMAGE_NOTIFY |
563 CAMERA_MSG_COMPRESSED_IMAGE);
564
565 enableMsgType(picMsgType);
566
567 return mHardware->takePicture();
568}
569
570// set preview/capture parameters - key/value pairs
571status_t CameraClient::setParameters(const String8& params) {
572 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
573
574 Mutex::Autolock lock(mLock);
575 status_t result = checkPidAndHardware();
576 if (result != NO_ERROR) return result;
577
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700578 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700579 CameraParameters p(params);
580 return mHardware->setParameters(p);
581}
582
583// get preview/capture parameters - key/value pairs
584String8 CameraClient::getParameters() const {
585 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700586 // The camera service can unconditionally get the parameters at all times
587 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700588
589 String8 params(mHardware->getParameters().flatten());
590 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
591 return params;
592}
593
594// enable shutter sound
595status_t CameraClient::enableShutterSound(bool enable) {
596 LOG1("enableShutterSound (pid %d)", getCallingPid());
597
598 status_t result = checkPidAndHardware();
599 if (result != NO_ERROR) return result;
600
601 if (enable) {
602 mPlayShutterSound = true;
603 return OK;
604 }
605
Igor Murashkina858ea02014-08-19 14:53:08 -0700606 // the camera2 api legacy mode can unconditionally disable the shutter sound
607 if (mLegacyMode) {
608 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
609 mPlayShutterSound = false;
610 return OK;
611 }
612
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700613 // Disabling shutter sound may not be allowed. In that case only
614 // allow the mediaserver process to disable the sound.
615 char value[PROPERTY_VALUE_MAX];
616 property_get("ro.camera.sound.forced", value, "0");
617 if (strcmp(value, "0") != 0) {
618 // Disabling shutter sound is not allowed. Deny if the current
619 // process is not mediaserver.
620 if (getCallingPid() != getpid()) {
621 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
622 return PERMISSION_DENIED;
623 }
624 }
625
626 mPlayShutterSound = false;
627 return OK;
628}
629
630status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
631 LOG1("sendCommand (pid %d)", getCallingPid());
632 int orientation;
633 Mutex::Autolock lock(mLock);
634 status_t result = checkPidAndHardware();
635 if (result != NO_ERROR) return result;
636
637 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
638 // Mirror the preview if the camera is front-facing.
639 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
640 if (orientation == -1) return BAD_VALUE;
641
642 if (mOrientation != orientation) {
643 mOrientation = orientation;
644 if (mPreviewWindow != 0) {
645 native_window_set_buffers_transform(mPreviewWindow.get(),
646 mOrientation);
647 }
648 }
649 return OK;
650 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
651 switch (arg1) {
652 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700653 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700654 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700655 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700656 default:
657 return BAD_VALUE;
658 }
659 return OK;
660 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700661 mCameraService->playSound(CameraService::SOUND_RECORDING_START);
James Dong983cf232012-08-01 16:39:55 -0700662 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
663 // Silently ignore this command
664 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700665 } else if (cmd == CAMERA_CMD_PING) {
666 // If mHardware is 0, checkPidAndHardware will return error.
667 return OK;
668 }
669
670 return mHardware->sendCommand(cmd, arg1, arg2);
671}
672
673// ----------------------------------------------------------------------------
674
675void CameraClient::enableMsgType(int32_t msgType) {
676 android_atomic_or(msgType, &mMsgEnabled);
677 mHardware->enableMsgType(msgType);
678}
679
680void CameraClient::disableMsgType(int32_t msgType) {
681 android_atomic_and(~msgType, &mMsgEnabled);
682 mHardware->disableMsgType(msgType);
683}
684
685#define CHECK_MESSAGE_INTERVAL 10 // 10ms
686bool CameraClient::lockIfMessageWanted(int32_t msgType) {
687 int sleepCount = 0;
688 while (mMsgEnabled & msgType) {
689 if (mLock.tryLock() == NO_ERROR) {
690 if (sleepCount > 0) {
691 LOG1("lockIfMessageWanted(%d): waited for %d ms",
692 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
693 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800694
695 // If messages are no longer enabled after acquiring lock, release and drop message
696 if ((mMsgEnabled & msgType) == 0) {
697 mLock.unlock();
698 break;
699 }
700
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700701 return true;
702 }
703 if (sleepCount++ == 0) {
704 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
705 }
706 usleep(CHECK_MESSAGE_INTERVAL * 1000);
707 }
708 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
709 return false;
710}
711
712// Callback messages can be dispatched to internal handlers or pass to our
713// client's callback functions, depending on the message type.
714//
715// notifyCallback:
716// CAMERA_MSG_SHUTTER handleShutter
717// (others) c->notifyCallback
718// dataCallback:
719// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
720// CAMERA_MSG_POSTVIEW_FRAME handlePostview
721// CAMERA_MSG_RAW_IMAGE handleRawPicture
722// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
723// (others) c->dataCallback
724// dataCallbackTimestamp
725// (others) c->dataCallbackTimestamp
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700726
727void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
728 int32_t ext2, void* user) {
729 LOG2("notifyCallback(%d)", msgType);
730
Ruben Brunkcc776712015-02-17 20:18:47 -0800731 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
732 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700733
734 if (!client->lockIfMessageWanted(msgType)) return;
735
736 switch (msgType) {
737 case CAMERA_MSG_SHUTTER:
738 // ext1 is the dimension of the yuv picture.
739 client->handleShutter();
740 break;
741 default:
742 client->handleGenericNotify(msgType, ext1, ext2);
743 break;
744 }
745}
746
747void CameraClient::dataCallback(int32_t msgType,
748 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
749 LOG2("dataCallback(%d)", msgType);
750
Ruben Brunkcc776712015-02-17 20:18:47 -0800751 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
752 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700753
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
Ruben Brunkcc776712015-02-17 20:18:47 -0800784 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
785 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700786
787 if (!client->lockIfMessageWanted(msgType)) return;
788
789 if (dataPtr == 0) {
790 ALOGE("Null data returned in data with timestamp callback");
791 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
792 return;
793 }
794
795 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
796}
797
798// snapshot taken callback
799void CameraClient::handleShutter(void) {
800 if (mPlayShutterSound) {
801 mCameraService->playSound(CameraService::SOUND_SHUTTER);
802 }
803
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800804 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700805 if (c != 0) {
806 mLock.unlock();
807 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
808 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
809 }
810 disableMsgType(CAMERA_MSG_SHUTTER);
811
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700812 // Shutters only happen in response to takePicture, so mark device as
813 // idle now, until preview is restarted
814 mCameraService->updateProxyDeviceState(
815 ICameraServiceProxy::CAMERA_STATE_IDLE,
816 String8::format("%d", mCameraId));
817
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700818 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
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001004status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
1005 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1006 return INVALID_OPERATION;
1007}
1008
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001009}; // namespace android