blob: 8ab9a65a4576b3ae449101d35e9b9bd54dd940bd [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) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800111 return BasicClient::dump(fd, args);
112}
113
114status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700115 const size_t SIZE = 256;
116 char buffer[SIZE];
117
Ruben Brunkcc776712015-02-17 20:18:47 -0800118 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n",
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700119 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800120 (getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800121 IInterface::asBinder(getRemoteCallback()).get() : NULL),
Ruben Brunkcc776712015-02-17 20:18:47 -0800122 mClientUid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700123 len = (len > SIZE - 1) ? SIZE - 1 : len;
124 write(fd, buffer, len);
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700125
126 len = snprintf(buffer, SIZE, "Latest set parameters:\n");
127 len = (len > SIZE - 1) ? SIZE - 1 : len;
128 write(fd, buffer, len);
129
130 mLatestSetParameters.dump(fd, args);
131
132 const char *enddump = "\n\n";
133 write(fd, enddump, strlen(enddump));
134
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700135 return mHardware->dump(fd, args);
136}
137
138// ----------------------------------------------------------------------------
139
140status_t CameraClient::checkPid() const {
141 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700142 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700143
144 ALOGW("attempt to use a locked camera from a different process"
145 " (old pid %d, new pid %d)", mClientPid, callingPid);
146 return EBUSY;
147}
148
149status_t CameraClient::checkPidAndHardware() const {
150 status_t result = checkPid();
151 if (result != NO_ERROR) return result;
152 if (mHardware == 0) {
153 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
154 return INVALID_OPERATION;
155 }
156 return NO_ERROR;
157}
158
159status_t CameraClient::lock() {
160 int callingPid = getCallingPid();
161 LOG1("lock (pid %d)", callingPid);
162 Mutex::Autolock lock(mLock);
163
164 // lock camera to this client if the the camera is unlocked
165 if (mClientPid == 0) {
166 mClientPid = callingPid;
167 return NO_ERROR;
168 }
169
170 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
171 return checkPid();
172}
173
174status_t CameraClient::unlock() {
175 int callingPid = getCallingPid();
176 LOG1("unlock (pid %d)", callingPid);
177 Mutex::Autolock lock(mLock);
178
179 // allow anyone to use camera (after they lock the camera)
180 status_t result = checkPid();
181 if (result == NO_ERROR) {
182 if (mHardware->recordingEnabled()) {
183 ALOGE("Not allowed to unlock camera during recording.");
184 return INVALID_OPERATION;
185 }
186 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800187 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700188 // we need to remove the reference to ICameraClient so that when the app
189 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800190 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700191 }
192 return result;
193}
194
195// connect a new client to the camera
196status_t CameraClient::connect(const sp<ICameraClient>& client) {
197 int callingPid = getCallingPid();
198 LOG1("connect E (pid %d)", callingPid);
199 Mutex::Autolock lock(mLock);
200
201 if (mClientPid != 0 && checkPid() != NO_ERROR) {
202 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
203 mClientPid, callingPid);
204 return EBUSY;
205 }
206
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800207 if (mRemoteCallback != 0 &&
Marco Nelissen06b46062014-11-14 07:58:25 -0800208 (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700209 LOG1("Connect to the same client");
210 return NO_ERROR;
211 }
212
213 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
214 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800215 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700216
217 LOG1("connect X (pid %d)", callingPid);
218 return NO_ERROR;
219}
220
221static void disconnectWindow(const sp<ANativeWindow>& window) {
222 if (window != 0) {
223 status_t result = native_window_api_disconnect(window.get(),
224 NATIVE_WINDOW_API_CAMERA);
225 if (result != NO_ERROR) {
226 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
227 result);
228 }
229 }
230}
231
232void CameraClient::disconnect() {
233 int callingPid = getCallingPid();
234 LOG1("disconnect E (pid %d)", callingPid);
235 Mutex::Autolock lock(mLock);
236
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800237 // Allow both client and the cameraserver to disconnect at all times
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700238 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700239 ALOGW("different client - don't disconnect");
240 return;
241 }
242
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700243 // Make sure disconnect() is done once and once only, whether it is called
244 // from the user directly, or called by the destructor.
245 if (mHardware == 0) return;
246
247 LOG1("hardware teardown");
248 // Before destroying mHardware, we must make sure it's in the
249 // idle state.
250 // Turn off all messages.
251 disableMsgType(CAMERA_MSG_ALL_MSGS);
252 mHardware->stopPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700253 mCameraService->updateProxyDeviceState(
254 ICameraServiceProxy::CAMERA_STATE_IDLE,
255 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700256 mHardware->cancelPicture();
257 // Release the hardware resources.
258 mHardware->release();
259
260 // Release the held ANativeWindow resources.
261 if (mPreviewWindow != 0) {
262 disconnectWindow(mPreviewWindow);
263 mPreviewWindow = 0;
264 mHardware->setPreviewWindow(mPreviewWindow);
265 }
266 mHardware.clear();
267
268 CameraService::Client::disconnect();
269
270 LOG1("disconnect X (pid %d)", callingPid);
271}
272
273// ----------------------------------------------------------------------------
274
275status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
276 const sp<ANativeWindow>& window) {
277 Mutex::Autolock lock(mLock);
278 status_t result = checkPidAndHardware();
279 if (result != NO_ERROR) return result;
280
281 // return if no change in surface.
282 if (binder == mSurface) {
283 return NO_ERROR;
284 }
285
286 if (window != 0) {
287 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
288 if (result != NO_ERROR) {
289 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
290 result);
291 return result;
292 }
293 }
294
295 // If preview has been already started, register preview buffers now.
296 if (mHardware->previewEnabled()) {
297 if (window != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800298 mHardware->setPreviewScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
299 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700300 result = mHardware->setPreviewWindow(window);
301 }
302 }
303
304 if (result == NO_ERROR) {
305 // Everything has succeeded. Disconnect the old window and remember the
306 // new window.
307 disconnectWindow(mPreviewWindow);
308 mSurface = binder;
309 mPreviewWindow = window;
310 } else {
311 // Something went wrong after we connected to the new window, so
312 // disconnect here.
313 disconnectWindow(window);
314 }
315
316 return result;
317}
318
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700319// set the buffer consumer that the preview will use
320status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800321 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700322 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700323 getCallingPid());
324
325 sp<IBinder> binder;
326 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800327 if (bufferProducer != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800328 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700329 // Using controlledByApp flag to ensure that the buffer queue remains in
330 // async mode for the old camera API, where many applications depend
331 // on that behavior.
332 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700333 }
334 return setPreviewWindow(binder, window);
335}
336
337// set the preview callback flag to affect how the received frames from
338// preview are handled.
339void CameraClient::setPreviewCallbackFlag(int callback_flag) {
340 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
341 Mutex::Autolock lock(mLock);
342 if (checkPidAndHardware() != NO_ERROR) return;
343
344 mPreviewCallbackFlag = callback_flag;
345 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
346 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
347 } else {
348 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
349 }
350}
351
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700352status_t CameraClient::setPreviewCallbackTarget(
353 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700354 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700355 ALOGE("%s: Unimplemented!", __FUNCTION__);
356 return INVALID_OPERATION;
357}
358
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700359// start preview mode
360status_t CameraClient::startPreview() {
361 LOG1("startPreview (pid %d)", getCallingPid());
362 return startCameraMode(CAMERA_PREVIEW_MODE);
363}
364
365// start recording mode
366status_t CameraClient::startRecording() {
367 LOG1("startRecording (pid %d)", getCallingPid());
368 return startCameraMode(CAMERA_RECORDING_MODE);
369}
370
371// start preview or recording
372status_t CameraClient::startCameraMode(camera_mode mode) {
373 LOG1("startCameraMode(%d)", mode);
374 Mutex::Autolock lock(mLock);
375 status_t result = checkPidAndHardware();
376 if (result != NO_ERROR) return result;
377
378 switch(mode) {
379 case CAMERA_PREVIEW_MODE:
380 if (mSurface == 0 && mPreviewWindow == 0) {
381 LOG1("mSurface is not set yet.");
382 // still able to start preview in this case.
383 }
384 return startPreviewMode();
385 case CAMERA_RECORDING_MODE:
386 if (mSurface == 0 && mPreviewWindow == 0) {
387 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
388 return INVALID_OPERATION;
389 }
390 return startRecordingMode();
391 default:
392 return UNKNOWN_ERROR;
393 }
394}
395
396status_t CameraClient::startPreviewMode() {
397 LOG1("startPreviewMode");
398 status_t result = NO_ERROR;
399
400 // if preview has been enabled, nothing needs to be done
401 if (mHardware->previewEnabled()) {
402 return NO_ERROR;
403 }
404
405 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800406 mHardware->setPreviewScalingMode(
407 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
408 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700409 }
410 mHardware->setPreviewWindow(mPreviewWindow);
411 result = mHardware->startPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700412 if (result == NO_ERROR) {
413 mCameraService->updateProxyDeviceState(
414 ICameraServiceProxy::CAMERA_STATE_ACTIVE,
415 String8::format("%d", mCameraId));
416 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700417 return result;
418}
419
420status_t CameraClient::startRecordingMode() {
421 LOG1("startRecordingMode");
422 status_t result = NO_ERROR;
423
424 // if recording has been enabled, nothing needs to be done
425 if (mHardware->recordingEnabled()) {
426 return NO_ERROR;
427 }
428
429 // if preview has not been started, start preview first
430 if (!mHardware->previewEnabled()) {
431 result = startPreviewMode();
432 if (result != NO_ERROR) {
433 return result;
434 }
435 }
436
437 // start recording mode
438 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700439 mCameraService->playSound(CameraService::SOUND_RECORDING_START);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700440 result = mHardware->startRecording();
441 if (result != NO_ERROR) {
442 ALOGE("mHardware->startRecording() failed with status %d", result);
443 }
444 return result;
445}
446
447// stop preview mode
448void CameraClient::stopPreview() {
449 LOG1("stopPreview (pid %d)", getCallingPid());
450 Mutex::Autolock lock(mLock);
451 if (checkPidAndHardware() != NO_ERROR) return;
452
453
454 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
455 mHardware->stopPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700456 mCameraService->updateProxyDeviceState(
457 ICameraServiceProxy::CAMERA_STATE_IDLE,
458 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700459 mPreviewBuffer.clear();
460}
461
462// stop recording mode
463void CameraClient::stopRecording() {
464 LOG1("stopRecording (pid %d)", getCallingPid());
465 Mutex::Autolock lock(mLock);
466 if (checkPidAndHardware() != NO_ERROR) return;
467
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700468 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
469 mHardware->stopRecording();
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700470 mCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700471
472 mPreviewBuffer.clear();
473}
474
475// release a recording frame
476void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
477 Mutex::Autolock lock(mLock);
478 if (checkPidAndHardware() != NO_ERROR) return;
479 mHardware->releaseRecordingFrame(mem);
480}
481
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800482status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
483 LOG1("setVideoBufferMode: %d", videoBufferMode);
484 bool enableMetadataInBuffers = false;
485
486 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
487 enableMetadataInBuffers = true;
488 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
489 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
490 videoBufferMode);
491 return BAD_VALUE;
492 }
493
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700494 Mutex::Autolock lock(mLock);
495 if (checkPidAndHardware() != NO_ERROR) {
496 return UNKNOWN_ERROR;
497 }
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800498
499 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700500}
501
502bool CameraClient::previewEnabled() {
503 LOG1("previewEnabled (pid %d)", getCallingPid());
504
505 Mutex::Autolock lock(mLock);
506 if (checkPidAndHardware() != NO_ERROR) return false;
507 return mHardware->previewEnabled();
508}
509
510bool CameraClient::recordingEnabled() {
511 LOG1("recordingEnabled (pid %d)", getCallingPid());
512
513 Mutex::Autolock lock(mLock);
514 if (checkPidAndHardware() != NO_ERROR) return false;
515 return mHardware->recordingEnabled();
516}
517
518status_t CameraClient::autoFocus() {
519 LOG1("autoFocus (pid %d)", getCallingPid());
520
521 Mutex::Autolock lock(mLock);
522 status_t result = checkPidAndHardware();
523 if (result != NO_ERROR) return result;
524
525 return mHardware->autoFocus();
526}
527
528status_t CameraClient::cancelAutoFocus() {
529 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
530
531 Mutex::Autolock lock(mLock);
532 status_t result = checkPidAndHardware();
533 if (result != NO_ERROR) return result;
534
535 return mHardware->cancelAutoFocus();
536}
537
538// take a picture - image is returned in callback
539status_t CameraClient::takePicture(int msgType) {
540 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
541
542 Mutex::Autolock lock(mLock);
543 status_t result = checkPidAndHardware();
544 if (result != NO_ERROR) return result;
545
546 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
547 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
548 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
549 " cannot be both enabled");
550 return BAD_VALUE;
551 }
552
553 // We only accept picture related message types
554 // and ignore other types of messages for takePicture().
555 int picMsgType = msgType
556 & (CAMERA_MSG_SHUTTER |
557 CAMERA_MSG_POSTVIEW_FRAME |
558 CAMERA_MSG_RAW_IMAGE |
559 CAMERA_MSG_RAW_IMAGE_NOTIFY |
560 CAMERA_MSG_COMPRESSED_IMAGE);
561
562 enableMsgType(picMsgType);
563
564 return mHardware->takePicture();
565}
566
567// set preview/capture parameters - key/value pairs
568status_t CameraClient::setParameters(const String8& params) {
569 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
570
571 Mutex::Autolock lock(mLock);
572 status_t result = checkPidAndHardware();
573 if (result != NO_ERROR) return result;
574
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700575 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700576 CameraParameters p(params);
577 return mHardware->setParameters(p);
578}
579
580// get preview/capture parameters - key/value pairs
581String8 CameraClient::getParameters() const {
582 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700583 // The camera service can unconditionally get the parameters at all times
584 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700585
586 String8 params(mHardware->getParameters().flatten());
587 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
588 return params;
589}
590
591// enable shutter sound
592status_t CameraClient::enableShutterSound(bool enable) {
593 LOG1("enableShutterSound (pid %d)", getCallingPid());
594
595 status_t result = checkPidAndHardware();
596 if (result != NO_ERROR) return result;
597
598 if (enable) {
599 mPlayShutterSound = true;
600 return OK;
601 }
602
Igor Murashkina858ea02014-08-19 14:53:08 -0700603 // the camera2 api legacy mode can unconditionally disable the shutter sound
604 if (mLegacyMode) {
605 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
606 mPlayShutterSound = false;
607 return OK;
608 }
609
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700610 // Disabling shutter sound may not be allowed. In that case only
611 // allow the mediaserver process to disable the sound.
612 char value[PROPERTY_VALUE_MAX];
613 property_get("ro.camera.sound.forced", value, "0");
614 if (strcmp(value, "0") != 0) {
615 // Disabling shutter sound is not allowed. Deny if the current
616 // process is not mediaserver.
617 if (getCallingPid() != getpid()) {
618 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
619 return PERMISSION_DENIED;
620 }
621 }
622
623 mPlayShutterSound = false;
624 return OK;
625}
626
627status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
628 LOG1("sendCommand (pid %d)", getCallingPid());
629 int orientation;
630 Mutex::Autolock lock(mLock);
631 status_t result = checkPidAndHardware();
632 if (result != NO_ERROR) return result;
633
634 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
635 // Mirror the preview if the camera is front-facing.
636 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
637 if (orientation == -1) return BAD_VALUE;
638
639 if (mOrientation != orientation) {
640 mOrientation = orientation;
641 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800642 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700643 }
644 }
645 return OK;
646 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
647 switch (arg1) {
648 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700649 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700650 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700651 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700652 default:
653 return BAD_VALUE;
654 }
655 return OK;
656 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700657 mCameraService->playSound(CameraService::SOUND_RECORDING_START);
James Dong983cf232012-08-01 16:39:55 -0700658 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
659 // Silently ignore this command
660 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700661 } else if (cmd == CAMERA_CMD_PING) {
662 // If mHardware is 0, checkPidAndHardware will return error.
663 return OK;
664 }
665
666 return mHardware->sendCommand(cmd, arg1, arg2);
667}
668
669// ----------------------------------------------------------------------------
670
671void CameraClient::enableMsgType(int32_t msgType) {
672 android_atomic_or(msgType, &mMsgEnabled);
673 mHardware->enableMsgType(msgType);
674}
675
676void CameraClient::disableMsgType(int32_t msgType) {
677 android_atomic_and(~msgType, &mMsgEnabled);
678 mHardware->disableMsgType(msgType);
679}
680
681#define CHECK_MESSAGE_INTERVAL 10 // 10ms
682bool CameraClient::lockIfMessageWanted(int32_t msgType) {
683 int sleepCount = 0;
684 while (mMsgEnabled & msgType) {
685 if (mLock.tryLock() == NO_ERROR) {
686 if (sleepCount > 0) {
687 LOG1("lockIfMessageWanted(%d): waited for %d ms",
688 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
689 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800690
691 // If messages are no longer enabled after acquiring lock, release and drop message
692 if ((mMsgEnabled & msgType) == 0) {
693 mLock.unlock();
694 break;
695 }
696
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700697 return true;
698 }
699 if (sleepCount++ == 0) {
700 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
701 }
702 usleep(CHECK_MESSAGE_INTERVAL * 1000);
703 }
704 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
705 return false;
706}
707
708// Callback messages can be dispatched to internal handlers or pass to our
709// client's callback functions, depending on the message type.
710//
711// notifyCallback:
712// CAMERA_MSG_SHUTTER handleShutter
713// (others) c->notifyCallback
714// dataCallback:
715// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
716// CAMERA_MSG_POSTVIEW_FRAME handlePostview
717// CAMERA_MSG_RAW_IMAGE handleRawPicture
718// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
719// (others) c->dataCallback
720// dataCallbackTimestamp
721// (others) c->dataCallbackTimestamp
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700722
723void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
724 int32_t ext2, void* user) {
725 LOG2("notifyCallback(%d)", msgType);
726
Ruben Brunkcc776712015-02-17 20:18:47 -0800727 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
728 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700729
730 if (!client->lockIfMessageWanted(msgType)) return;
731
732 switch (msgType) {
733 case CAMERA_MSG_SHUTTER:
734 // ext1 is the dimension of the yuv picture.
735 client->handleShutter();
736 break;
737 default:
738 client->handleGenericNotify(msgType, ext1, ext2);
739 break;
740 }
741}
742
743void CameraClient::dataCallback(int32_t msgType,
744 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
745 LOG2("dataCallback(%d)", msgType);
746
Ruben Brunkcc776712015-02-17 20:18:47 -0800747 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
748 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700749
750 if (!client->lockIfMessageWanted(msgType)) return;
751 if (dataPtr == 0 && metadata == NULL) {
752 ALOGE("Null data returned in data callback");
753 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
754 return;
755 }
756
757 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
758 case CAMERA_MSG_PREVIEW_FRAME:
759 client->handlePreviewData(msgType, dataPtr, metadata);
760 break;
761 case CAMERA_MSG_POSTVIEW_FRAME:
762 client->handlePostview(dataPtr);
763 break;
764 case CAMERA_MSG_RAW_IMAGE:
765 client->handleRawPicture(dataPtr);
766 break;
767 case CAMERA_MSG_COMPRESSED_IMAGE:
768 client->handleCompressedPicture(dataPtr);
769 break;
770 default:
771 client->handleGenericData(msgType, dataPtr, metadata);
772 break;
773 }
774}
775
776void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
777 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
778 LOG2("dataCallbackTimestamp(%d)", msgType);
779
Ruben Brunkcc776712015-02-17 20:18:47 -0800780 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
781 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700782
783 if (!client->lockIfMessageWanted(msgType)) return;
784
785 if (dataPtr == 0) {
786 ALOGE("Null data returned in data with timestamp callback");
787 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
788 return;
789 }
790
791 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
792}
793
794// snapshot taken callback
795void CameraClient::handleShutter(void) {
796 if (mPlayShutterSound) {
797 mCameraService->playSound(CameraService::SOUND_SHUTTER);
798 }
799
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800800 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700801 if (c != 0) {
802 mLock.unlock();
803 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
804 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
805 }
806 disableMsgType(CAMERA_MSG_SHUTTER);
807
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700808 // Shutters only happen in response to takePicture, so mark device as
809 // idle now, until preview is restarted
810 mCameraService->updateProxyDeviceState(
811 ICameraServiceProxy::CAMERA_STATE_IDLE,
812 String8::format("%d", mCameraId));
813
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700814 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
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001000status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001001 (void)bufferProducer;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001002 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1003 return INVALID_OPERATION;
1004}
1005
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001006}; // namespace android