blob: 0eeb1c67cc2d01f2bbdd11dc27d713f93d32a0e8 [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
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700239 // Make sure disconnect() is done once and once only, whether it is called
240 // from the user directly, or called by the destructor.
241 if (mHardware == 0) return;
242
243 LOG1("hardware teardown");
244 // Before destroying mHardware, we must make sure it's in the
245 // idle state.
246 // Turn off all messages.
247 disableMsgType(CAMERA_MSG_ALL_MSGS);
248 mHardware->stopPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700249 mCameraService->updateProxyDeviceState(
250 ICameraServiceProxy::CAMERA_STATE_IDLE,
251 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700252 mHardware->cancelPicture();
253 // Release the hardware resources.
254 mHardware->release();
255
256 // Release the held ANativeWindow resources.
257 if (mPreviewWindow != 0) {
258 disconnectWindow(mPreviewWindow);
259 mPreviewWindow = 0;
260 mHardware->setPreviewWindow(mPreviewWindow);
261 }
262 mHardware.clear();
263
264 CameraService::Client::disconnect();
265
266 LOG1("disconnect X (pid %d)", callingPid);
267}
268
269// ----------------------------------------------------------------------------
270
271status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
272 const sp<ANativeWindow>& window) {
273 Mutex::Autolock lock(mLock);
274 status_t result = checkPidAndHardware();
275 if (result != NO_ERROR) return result;
276
277 // return if no change in surface.
278 if (binder == mSurface) {
279 return NO_ERROR;
280 }
281
282 if (window != 0) {
283 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
284 if (result != NO_ERROR) {
285 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
286 result);
287 return result;
288 }
289 }
290
291 // If preview has been already started, register preview buffers now.
292 if (mHardware->previewEnabled()) {
293 if (window != 0) {
294 native_window_set_scaling_mode(window.get(),
295 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
296 native_window_set_buffers_transform(window.get(), mOrientation);
297 result = mHardware->setPreviewWindow(window);
298 }
299 }
300
301 if (result == NO_ERROR) {
302 // Everything has succeeded. Disconnect the old window and remember the
303 // new window.
304 disconnectWindow(mPreviewWindow);
305 mSurface = binder;
306 mPreviewWindow = window;
307 } else {
308 // Something went wrong after we connected to the new window, so
309 // disconnect here.
310 disconnectWindow(window);
311 }
312
313 return result;
314}
315
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700316// set the buffer consumer that the preview will use
317status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800318 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700319 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700320 getCallingPid());
321
322 sp<IBinder> binder;
323 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800324 if (bufferProducer != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800325 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700326 // Using controlledByApp flag to ensure that the buffer queue remains in
327 // async mode for the old camera API, where many applications depend
328 // on that behavior.
329 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700330 }
331 return setPreviewWindow(binder, window);
332}
333
334// set the preview callback flag to affect how the received frames from
335// preview are handled.
336void CameraClient::setPreviewCallbackFlag(int callback_flag) {
337 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
338 Mutex::Autolock lock(mLock);
339 if (checkPidAndHardware() != NO_ERROR) return;
340
341 mPreviewCallbackFlag = callback_flag;
342 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
343 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
344 } else {
345 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
346 }
347}
348
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700349status_t CameraClient::setPreviewCallbackTarget(
350 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700351 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700352 ALOGE("%s: Unimplemented!", __FUNCTION__);
353 return INVALID_OPERATION;
354}
355
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700356// start preview mode
357status_t CameraClient::startPreview() {
358 LOG1("startPreview (pid %d)", getCallingPid());
359 return startCameraMode(CAMERA_PREVIEW_MODE);
360}
361
362// start recording mode
363status_t CameraClient::startRecording() {
364 LOG1("startRecording (pid %d)", getCallingPid());
365 return startCameraMode(CAMERA_RECORDING_MODE);
366}
367
368// start preview or recording
369status_t CameraClient::startCameraMode(camera_mode mode) {
370 LOG1("startCameraMode(%d)", mode);
371 Mutex::Autolock lock(mLock);
372 status_t result = checkPidAndHardware();
373 if (result != NO_ERROR) return result;
374
375 switch(mode) {
376 case CAMERA_PREVIEW_MODE:
377 if (mSurface == 0 && mPreviewWindow == 0) {
378 LOG1("mSurface is not set yet.");
379 // still able to start preview in this case.
380 }
381 return startPreviewMode();
382 case CAMERA_RECORDING_MODE:
383 if (mSurface == 0 && mPreviewWindow == 0) {
384 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
385 return INVALID_OPERATION;
386 }
387 return startRecordingMode();
388 default:
389 return UNKNOWN_ERROR;
390 }
391}
392
393status_t CameraClient::startPreviewMode() {
394 LOG1("startPreviewMode");
395 status_t result = NO_ERROR;
396
397 // if preview has been enabled, nothing needs to be done
398 if (mHardware->previewEnabled()) {
399 return NO_ERROR;
400 }
401
402 if (mPreviewWindow != 0) {
403 native_window_set_scaling_mode(mPreviewWindow.get(),
404 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
405 native_window_set_buffers_transform(mPreviewWindow.get(),
406 mOrientation);
407 }
408 mHardware->setPreviewWindow(mPreviewWindow);
409 result = mHardware->startPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700410 if (result == NO_ERROR) {
411 mCameraService->updateProxyDeviceState(
412 ICameraServiceProxy::CAMERA_STATE_ACTIVE,
413 String8::format("%d", mCameraId));
414 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700415 return result;
416}
417
418status_t CameraClient::startRecordingMode() {
419 LOG1("startRecordingMode");
420 status_t result = NO_ERROR;
421
422 // if recording has been enabled, nothing needs to be done
423 if (mHardware->recordingEnabled()) {
424 return NO_ERROR;
425 }
426
427 // if preview has not been started, start preview first
428 if (!mHardware->previewEnabled()) {
429 result = startPreviewMode();
430 if (result != NO_ERROR) {
431 return result;
432 }
433 }
434
435 // start recording mode
436 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700437 mCameraService->playSound(CameraService::SOUND_RECORDING_START);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700438 result = mHardware->startRecording();
439 if (result != NO_ERROR) {
440 ALOGE("mHardware->startRecording() failed with status %d", result);
441 }
442 return result;
443}
444
445// stop preview mode
446void CameraClient::stopPreview() {
447 LOG1("stopPreview (pid %d)", getCallingPid());
448 Mutex::Autolock lock(mLock);
449 if (checkPidAndHardware() != NO_ERROR) return;
450
451
452 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
453 mHardware->stopPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700454 mCameraService->updateProxyDeviceState(
455 ICameraServiceProxy::CAMERA_STATE_IDLE,
456 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700457 mPreviewBuffer.clear();
458}
459
460// stop recording mode
461void CameraClient::stopRecording() {
462 LOG1("stopRecording (pid %d)", getCallingPid());
463 Mutex::Autolock lock(mLock);
464 if (checkPidAndHardware() != NO_ERROR) return;
465
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700466 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
467 mHardware->stopRecording();
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700468 mCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700469
470 mPreviewBuffer.clear();
471}
472
473// release a recording frame
474void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
475 Mutex::Autolock lock(mLock);
476 if (checkPidAndHardware() != NO_ERROR) return;
477 mHardware->releaseRecordingFrame(mem);
478}
479
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800480status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
481 LOG1("setVideoBufferMode: %d", videoBufferMode);
482 bool enableMetadataInBuffers = false;
483
484 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
485 enableMetadataInBuffers = true;
486 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
487 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
488 videoBufferMode);
489 return BAD_VALUE;
490 }
491
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700492 Mutex::Autolock lock(mLock);
493 if (checkPidAndHardware() != NO_ERROR) {
494 return UNKNOWN_ERROR;
495 }
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800496
497 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700498}
499
500bool CameraClient::previewEnabled() {
501 LOG1("previewEnabled (pid %d)", getCallingPid());
502
503 Mutex::Autolock lock(mLock);
504 if (checkPidAndHardware() != NO_ERROR) return false;
505 return mHardware->previewEnabled();
506}
507
508bool CameraClient::recordingEnabled() {
509 LOG1("recordingEnabled (pid %d)", getCallingPid());
510
511 Mutex::Autolock lock(mLock);
512 if (checkPidAndHardware() != NO_ERROR) return false;
513 return mHardware->recordingEnabled();
514}
515
516status_t CameraClient::autoFocus() {
517 LOG1("autoFocus (pid %d)", getCallingPid());
518
519 Mutex::Autolock lock(mLock);
520 status_t result = checkPidAndHardware();
521 if (result != NO_ERROR) return result;
522
523 return mHardware->autoFocus();
524}
525
526status_t CameraClient::cancelAutoFocus() {
527 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
528
529 Mutex::Autolock lock(mLock);
530 status_t result = checkPidAndHardware();
531 if (result != NO_ERROR) return result;
532
533 return mHardware->cancelAutoFocus();
534}
535
536// take a picture - image is returned in callback
537status_t CameraClient::takePicture(int msgType) {
538 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
539
540 Mutex::Autolock lock(mLock);
541 status_t result = checkPidAndHardware();
542 if (result != NO_ERROR) return result;
543
544 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
545 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
546 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
547 " cannot be both enabled");
548 return BAD_VALUE;
549 }
550
551 // We only accept picture related message types
552 // and ignore other types of messages for takePicture().
553 int picMsgType = msgType
554 & (CAMERA_MSG_SHUTTER |
555 CAMERA_MSG_POSTVIEW_FRAME |
556 CAMERA_MSG_RAW_IMAGE |
557 CAMERA_MSG_RAW_IMAGE_NOTIFY |
558 CAMERA_MSG_COMPRESSED_IMAGE);
559
560 enableMsgType(picMsgType);
561
562 return mHardware->takePicture();
563}
564
565// set preview/capture parameters - key/value pairs
566status_t CameraClient::setParameters(const String8& params) {
567 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
568
569 Mutex::Autolock lock(mLock);
570 status_t result = checkPidAndHardware();
571 if (result != NO_ERROR) return result;
572
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700573 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700574 CameraParameters p(params);
575 return mHardware->setParameters(p);
576}
577
578// get preview/capture parameters - key/value pairs
579String8 CameraClient::getParameters() const {
580 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700581 // The camera service can unconditionally get the parameters at all times
582 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700583
584 String8 params(mHardware->getParameters().flatten());
585 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
586 return params;
587}
588
589// enable shutter sound
590status_t CameraClient::enableShutterSound(bool enable) {
591 LOG1("enableShutterSound (pid %d)", getCallingPid());
592
593 status_t result = checkPidAndHardware();
594 if (result != NO_ERROR) return result;
595
596 if (enable) {
597 mPlayShutterSound = true;
598 return OK;
599 }
600
Igor Murashkina858ea02014-08-19 14:53:08 -0700601 // the camera2 api legacy mode can unconditionally disable the shutter sound
602 if (mLegacyMode) {
603 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
604 mPlayShutterSound = false;
605 return OK;
606 }
607
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700608 // Disabling shutter sound may not be allowed. In that case only
609 // allow the mediaserver process to disable the sound.
610 char value[PROPERTY_VALUE_MAX];
611 property_get("ro.camera.sound.forced", value, "0");
612 if (strcmp(value, "0") != 0) {
613 // Disabling shutter sound is not allowed. Deny if the current
614 // process is not mediaserver.
615 if (getCallingPid() != getpid()) {
616 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
617 return PERMISSION_DENIED;
618 }
619 }
620
621 mPlayShutterSound = false;
622 return OK;
623}
624
625status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
626 LOG1("sendCommand (pid %d)", getCallingPid());
627 int orientation;
628 Mutex::Autolock lock(mLock);
629 status_t result = checkPidAndHardware();
630 if (result != NO_ERROR) return result;
631
632 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
633 // Mirror the preview if the camera is front-facing.
634 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
635 if (orientation == -1) return BAD_VALUE;
636
637 if (mOrientation != orientation) {
638 mOrientation = orientation;
639 if (mPreviewWindow != 0) {
640 native_window_set_buffers_transform(mPreviewWindow.get(),
641 mOrientation);
642 }
643 }
644 return OK;
645 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
646 switch (arg1) {
647 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700648 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700649 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700650 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700651 default:
652 return BAD_VALUE;
653 }
654 return OK;
655 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700656 mCameraService->playSound(CameraService::SOUND_RECORDING_START);
James Dong983cf232012-08-01 16:39:55 -0700657 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
658 // Silently ignore this command
659 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700660 } else if (cmd == CAMERA_CMD_PING) {
661 // If mHardware is 0, checkPidAndHardware will return error.
662 return OK;
663 }
664
665 return mHardware->sendCommand(cmd, arg1, arg2);
666}
667
668// ----------------------------------------------------------------------------
669
670void CameraClient::enableMsgType(int32_t msgType) {
671 android_atomic_or(msgType, &mMsgEnabled);
672 mHardware->enableMsgType(msgType);
673}
674
675void CameraClient::disableMsgType(int32_t msgType) {
676 android_atomic_and(~msgType, &mMsgEnabled);
677 mHardware->disableMsgType(msgType);
678}
679
680#define CHECK_MESSAGE_INTERVAL 10 // 10ms
681bool CameraClient::lockIfMessageWanted(int32_t msgType) {
682 int sleepCount = 0;
683 while (mMsgEnabled & msgType) {
684 if (mLock.tryLock() == NO_ERROR) {
685 if (sleepCount > 0) {
686 LOG1("lockIfMessageWanted(%d): waited for %d ms",
687 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
688 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800689
690 // If messages are no longer enabled after acquiring lock, release and drop message
691 if ((mMsgEnabled & msgType) == 0) {
692 mLock.unlock();
693 break;
694 }
695
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700696 return true;
697 }
698 if (sleepCount++ == 0) {
699 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
700 }
701 usleep(CHECK_MESSAGE_INTERVAL * 1000);
702 }
703 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
704 return false;
705}
706
707// Callback messages can be dispatched to internal handlers or pass to our
708// client's callback functions, depending on the message type.
709//
710// notifyCallback:
711// CAMERA_MSG_SHUTTER handleShutter
712// (others) c->notifyCallback
713// dataCallback:
714// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
715// CAMERA_MSG_POSTVIEW_FRAME handlePostview
716// CAMERA_MSG_RAW_IMAGE handleRawPicture
717// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
718// (others) c->dataCallback
719// dataCallbackTimestamp
720// (others) c->dataCallbackTimestamp
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700721
722void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
723 int32_t ext2, void* user) {
724 LOG2("notifyCallback(%d)", msgType);
725
Ruben Brunkcc776712015-02-17 20:18:47 -0800726 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
727 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700728
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
Ruben Brunkcc776712015-02-17 20:18:47 -0800746 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
747 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700748
749 if (!client->lockIfMessageWanted(msgType)) return;
750 if (dataPtr == 0 && metadata == NULL) {
751 ALOGE("Null data returned in data callback");
752 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
753 return;
754 }
755
756 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
757 case CAMERA_MSG_PREVIEW_FRAME:
758 client->handlePreviewData(msgType, dataPtr, metadata);
759 break;
760 case CAMERA_MSG_POSTVIEW_FRAME:
761 client->handlePostview(dataPtr);
762 break;
763 case CAMERA_MSG_RAW_IMAGE:
764 client->handleRawPicture(dataPtr);
765 break;
766 case CAMERA_MSG_COMPRESSED_IMAGE:
767 client->handleCompressedPicture(dataPtr);
768 break;
769 default:
770 client->handleGenericData(msgType, dataPtr, metadata);
771 break;
772 }
773}
774
775void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
776 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
777 LOG2("dataCallbackTimestamp(%d)", msgType);
778
Ruben Brunkcc776712015-02-17 20:18:47 -0800779 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
780 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700781
782 if (!client->lockIfMessageWanted(msgType)) return;
783
784 if (dataPtr == 0) {
785 ALOGE("Null data returned in data with timestamp callback");
786 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
787 return;
788 }
789
790 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
791}
792
793// snapshot taken callback
794void CameraClient::handleShutter(void) {
795 if (mPlayShutterSound) {
796 mCameraService->playSound(CameraService::SOUND_SHUTTER);
797 }
798
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800799 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700800 if (c != 0) {
801 mLock.unlock();
802 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
803 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
804 }
805 disableMsgType(CAMERA_MSG_SHUTTER);
806
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700807 // Shutters only happen in response to takePicture, so mark device as
808 // idle now, until preview is restarted
809 mCameraService->updateProxyDeviceState(
810 ICameraServiceProxy::CAMERA_STATE_IDLE,
811 String8::format("%d", mCameraId));
812
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700813 mLock.unlock();
814}
815
816// preview callback - frame buffer update
817void CameraClient::handlePreviewData(int32_t msgType,
818 const sp<IMemory>& mem,
819 camera_frame_metadata_t *metadata) {
820 ssize_t offset;
821 size_t size;
822 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
823
824 // local copy of the callback flags
825 int flags = mPreviewCallbackFlag;
826
827 // is callback enabled?
828 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
829 // If the enable bit is off, the copy-out and one-shot bits are ignored
830 LOG2("frame callback is disabled");
831 mLock.unlock();
832 return;
833 }
834
835 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800836 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700837
838 // clear callback flags if no client or one-shot mode
839 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
840 LOG2("Disable preview callback");
841 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
842 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
843 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
844 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
845 }
846
847 if (c != 0) {
848 // Is the received frame copied out or not?
849 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
850 LOG2("frame is copied");
851 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
852 } else {
853 LOG2("frame is forwarded");
854 mLock.unlock();
855 c->dataCallback(msgType, mem, metadata);
856 }
857 } else {
858 mLock.unlock();
859 }
860}
861
862// picture callback - postview image ready
863void CameraClient::handlePostview(const sp<IMemory>& mem) {
864 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
865
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800866 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700867 mLock.unlock();
868 if (c != 0) {
869 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
870 }
871}
872
873// picture callback - raw image ready
874void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
875 disableMsgType(CAMERA_MSG_RAW_IMAGE);
876
877 ssize_t offset;
878 size_t size;
879 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
880
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800881 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700882 mLock.unlock();
883 if (c != 0) {
884 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
885 }
886}
887
888// picture callback - compressed picture ready
889void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
890 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
891
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800892 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700893 mLock.unlock();
894 if (c != 0) {
895 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
896 }
897}
898
899
900void CameraClient::handleGenericNotify(int32_t msgType,
901 int32_t ext1, int32_t ext2) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800902 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700903 mLock.unlock();
904 if (c != 0) {
905 c->notifyCallback(msgType, ext1, ext2);
906 }
907}
908
909void CameraClient::handleGenericData(int32_t msgType,
910 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800911 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700912 mLock.unlock();
913 if (c != 0) {
914 c->dataCallback(msgType, dataPtr, metadata);
915 }
916}
917
918void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
919 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800920 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700921 mLock.unlock();
922 if (c != 0) {
923 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
924 }
925}
926
927void CameraClient::copyFrameAndPostCopiedFrame(
928 int32_t msgType, const sp<ICameraClient>& client,
929 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
930 camera_frame_metadata_t *metadata) {
931 LOG2("copyFrameAndPostCopiedFrame");
932 // It is necessary to copy out of pmem before sending this to
933 // the callback. For efficiency, reuse the same MemoryHeapBase
934 // provided it's big enough. Don't allocate the memory or
935 // perform the copy if there's no callback.
936 // hold the preview lock while we grab a reference to the preview buffer
937 sp<MemoryHeapBase> previewBuffer;
938
939 if (mPreviewBuffer == 0) {
940 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
941 } else if (size > mPreviewBuffer->virtualSize()) {
942 mPreviewBuffer.clear();
943 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
944 }
945 if (mPreviewBuffer == 0) {
946 ALOGE("failed to allocate space for preview buffer");
947 mLock.unlock();
948 return;
949 }
950 previewBuffer = mPreviewBuffer;
951
Ruben Brunk65e01f72014-08-29 17:25:13 -0700952 void* previewBufferBase = previewBuffer->base();
953 void* heapBase = heap->base();
954
955 if (heapBase == MAP_FAILED) {
956 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
957 mLock.unlock();
958 return;
959 } else if (previewBufferBase == MAP_FAILED) {
960 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
961 mLock.unlock();
962 return;
963 }
964
965 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700966
967 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
968 if (frame == 0) {
969 ALOGE("failed to allocate space for frame callback");
970 mLock.unlock();
971 return;
972 }
973
974 mLock.unlock();
975 client->dataCallback(msgType, frame, metadata);
976}
977
978int CameraClient::getOrientation(int degrees, bool mirror) {
979 if (!mirror) {
980 if (degrees == 0) return 0;
981 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
982 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
983 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
984 } else { // Do mirror (horizontal flip)
985 if (degrees == 0) { // FLIP_H and ROT_0
986 return HAL_TRANSFORM_FLIP_H;
987 } else if (degrees == 90) { // FLIP_H and ROT_90
988 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
989 } else if (degrees == 180) { // FLIP_H and ROT_180
990 return HAL_TRANSFORM_FLIP_V;
991 } else if (degrees == 270) { // FLIP_H and ROT_270
992 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
993 }
994 }
995 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
996 return -1;
997}
998
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800999status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
1000 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1001 return INVALID_OPERATION;
1002}
1003
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001004}; // namespace android