blob: 41536587ba5e187b2d7ff5fa0733c13ea243b41c [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
480status_t CameraClient::storeMetaDataInBuffers(bool enabled)
481{
482 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
483 Mutex::Autolock lock(mLock);
484 if (checkPidAndHardware() != NO_ERROR) {
485 return UNKNOWN_ERROR;
486 }
487 return mHardware->storeMetaDataInBuffers(enabled);
488}
489
490bool CameraClient::previewEnabled() {
491 LOG1("previewEnabled (pid %d)", getCallingPid());
492
493 Mutex::Autolock lock(mLock);
494 if (checkPidAndHardware() != NO_ERROR) return false;
495 return mHardware->previewEnabled();
496}
497
498bool CameraClient::recordingEnabled() {
499 LOG1("recordingEnabled (pid %d)", getCallingPid());
500
501 Mutex::Autolock lock(mLock);
502 if (checkPidAndHardware() != NO_ERROR) return false;
503 return mHardware->recordingEnabled();
504}
505
506status_t CameraClient::autoFocus() {
507 LOG1("autoFocus (pid %d)", getCallingPid());
508
509 Mutex::Autolock lock(mLock);
510 status_t result = checkPidAndHardware();
511 if (result != NO_ERROR) return result;
512
513 return mHardware->autoFocus();
514}
515
516status_t CameraClient::cancelAutoFocus() {
517 LOG1("cancelAutoFocus (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->cancelAutoFocus();
524}
525
526// take a picture - image is returned in callback
527status_t CameraClient::takePicture(int msgType) {
528 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
529
530 Mutex::Autolock lock(mLock);
531 status_t result = checkPidAndHardware();
532 if (result != NO_ERROR) return result;
533
534 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
535 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
536 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
537 " cannot be both enabled");
538 return BAD_VALUE;
539 }
540
541 // We only accept picture related message types
542 // and ignore other types of messages for takePicture().
543 int picMsgType = msgType
544 & (CAMERA_MSG_SHUTTER |
545 CAMERA_MSG_POSTVIEW_FRAME |
546 CAMERA_MSG_RAW_IMAGE |
547 CAMERA_MSG_RAW_IMAGE_NOTIFY |
548 CAMERA_MSG_COMPRESSED_IMAGE);
549
550 enableMsgType(picMsgType);
551
552 return mHardware->takePicture();
553}
554
555// set preview/capture parameters - key/value pairs
556status_t CameraClient::setParameters(const String8& params) {
557 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
558
559 Mutex::Autolock lock(mLock);
560 status_t result = checkPidAndHardware();
561 if (result != NO_ERROR) return result;
562
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700563 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700564 CameraParameters p(params);
565 return mHardware->setParameters(p);
566}
567
568// get preview/capture parameters - key/value pairs
569String8 CameraClient::getParameters() const {
570 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700571 // The camera service can unconditionally get the parameters at all times
572 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700573
574 String8 params(mHardware->getParameters().flatten());
575 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
576 return params;
577}
578
579// enable shutter sound
580status_t CameraClient::enableShutterSound(bool enable) {
581 LOG1("enableShutterSound (pid %d)", getCallingPid());
582
583 status_t result = checkPidAndHardware();
584 if (result != NO_ERROR) return result;
585
586 if (enable) {
587 mPlayShutterSound = true;
588 return OK;
589 }
590
Igor Murashkina858ea02014-08-19 14:53:08 -0700591 // the camera2 api legacy mode can unconditionally disable the shutter sound
592 if (mLegacyMode) {
593 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
594 mPlayShutterSound = false;
595 return OK;
596 }
597
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700598 // Disabling shutter sound may not be allowed. In that case only
599 // allow the mediaserver process to disable the sound.
600 char value[PROPERTY_VALUE_MAX];
601 property_get("ro.camera.sound.forced", value, "0");
602 if (strcmp(value, "0") != 0) {
603 // Disabling shutter sound is not allowed. Deny if the current
604 // process is not mediaserver.
605 if (getCallingPid() != getpid()) {
606 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
607 return PERMISSION_DENIED;
608 }
609 }
610
611 mPlayShutterSound = false;
612 return OK;
613}
614
615status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
616 LOG1("sendCommand (pid %d)", getCallingPid());
617 int orientation;
618 Mutex::Autolock lock(mLock);
619 status_t result = checkPidAndHardware();
620 if (result != NO_ERROR) return result;
621
622 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
623 // Mirror the preview if the camera is front-facing.
624 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
625 if (orientation == -1) return BAD_VALUE;
626
627 if (mOrientation != orientation) {
628 mOrientation = orientation;
629 if (mPreviewWindow != 0) {
630 native_window_set_buffers_transform(mPreviewWindow.get(),
631 mOrientation);
632 }
633 }
634 return OK;
635 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
636 switch (arg1) {
637 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700638 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700639 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700640 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700641 default:
642 return BAD_VALUE;
643 }
644 return OK;
645 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
Chien-Yu Chen82104eb2015-10-14 11:29:31 -0700646 mCameraService->playSound(CameraService::SOUND_RECORDING_START);
James Dong983cf232012-08-01 16:39:55 -0700647 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
648 // Silently ignore this command
649 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700650 } else if (cmd == CAMERA_CMD_PING) {
651 // If mHardware is 0, checkPidAndHardware will return error.
652 return OK;
653 }
654
655 return mHardware->sendCommand(cmd, arg1, arg2);
656}
657
658// ----------------------------------------------------------------------------
659
660void CameraClient::enableMsgType(int32_t msgType) {
661 android_atomic_or(msgType, &mMsgEnabled);
662 mHardware->enableMsgType(msgType);
663}
664
665void CameraClient::disableMsgType(int32_t msgType) {
666 android_atomic_and(~msgType, &mMsgEnabled);
667 mHardware->disableMsgType(msgType);
668}
669
670#define CHECK_MESSAGE_INTERVAL 10 // 10ms
671bool CameraClient::lockIfMessageWanted(int32_t msgType) {
672 int sleepCount = 0;
673 while (mMsgEnabled & msgType) {
674 if (mLock.tryLock() == NO_ERROR) {
675 if (sleepCount > 0) {
676 LOG1("lockIfMessageWanted(%d): waited for %d ms",
677 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
678 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800679
680 // If messages are no longer enabled after acquiring lock, release and drop message
681 if ((mMsgEnabled & msgType) == 0) {
682 mLock.unlock();
683 break;
684 }
685
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700686 return true;
687 }
688 if (sleepCount++ == 0) {
689 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
690 }
691 usleep(CHECK_MESSAGE_INTERVAL * 1000);
692 }
693 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
694 return false;
695}
696
697// Callback messages can be dispatched to internal handlers or pass to our
698// client's callback functions, depending on the message type.
699//
700// notifyCallback:
701// CAMERA_MSG_SHUTTER handleShutter
702// (others) c->notifyCallback
703// dataCallback:
704// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
705// CAMERA_MSG_POSTVIEW_FRAME handlePostview
706// CAMERA_MSG_RAW_IMAGE handleRawPicture
707// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
708// (others) c->dataCallback
709// dataCallbackTimestamp
710// (others) c->dataCallbackTimestamp
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700711
712void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
713 int32_t ext2, void* user) {
714 LOG2("notifyCallback(%d)", msgType);
715
Ruben Brunkcc776712015-02-17 20:18:47 -0800716 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
717 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700718
719 if (!client->lockIfMessageWanted(msgType)) return;
720
721 switch (msgType) {
722 case CAMERA_MSG_SHUTTER:
723 // ext1 is the dimension of the yuv picture.
724 client->handleShutter();
725 break;
726 default:
727 client->handleGenericNotify(msgType, ext1, ext2);
728 break;
729 }
730}
731
732void CameraClient::dataCallback(int32_t msgType,
733 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
734 LOG2("dataCallback(%d)", msgType);
735
Ruben Brunkcc776712015-02-17 20:18:47 -0800736 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
737 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700738
739 if (!client->lockIfMessageWanted(msgType)) return;
740 if (dataPtr == 0 && metadata == NULL) {
741 ALOGE("Null data returned in data callback");
742 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
743 return;
744 }
745
746 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
747 case CAMERA_MSG_PREVIEW_FRAME:
748 client->handlePreviewData(msgType, dataPtr, metadata);
749 break;
750 case CAMERA_MSG_POSTVIEW_FRAME:
751 client->handlePostview(dataPtr);
752 break;
753 case CAMERA_MSG_RAW_IMAGE:
754 client->handleRawPicture(dataPtr);
755 break;
756 case CAMERA_MSG_COMPRESSED_IMAGE:
757 client->handleCompressedPicture(dataPtr);
758 break;
759 default:
760 client->handleGenericData(msgType, dataPtr, metadata);
761 break;
762 }
763}
764
765void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
766 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
767 LOG2("dataCallbackTimestamp(%d)", msgType);
768
Ruben Brunkcc776712015-02-17 20:18:47 -0800769 sp<CameraClient> client = static_cast<CameraClient*>(getClientFromCookie(user).get());
770 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700771
772 if (!client->lockIfMessageWanted(msgType)) return;
773
774 if (dataPtr == 0) {
775 ALOGE("Null data returned in data with timestamp callback");
776 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
777 return;
778 }
779
780 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
781}
782
783// snapshot taken callback
784void CameraClient::handleShutter(void) {
785 if (mPlayShutterSound) {
786 mCameraService->playSound(CameraService::SOUND_SHUTTER);
787 }
788
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800789 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700790 if (c != 0) {
791 mLock.unlock();
792 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
793 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
794 }
795 disableMsgType(CAMERA_MSG_SHUTTER);
796
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700797 // Shutters only happen in response to takePicture, so mark device as
798 // idle now, until preview is restarted
799 mCameraService->updateProxyDeviceState(
800 ICameraServiceProxy::CAMERA_STATE_IDLE,
801 String8::format("%d", mCameraId));
802
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700803 mLock.unlock();
804}
805
806// preview callback - frame buffer update
807void CameraClient::handlePreviewData(int32_t msgType,
808 const sp<IMemory>& mem,
809 camera_frame_metadata_t *metadata) {
810 ssize_t offset;
811 size_t size;
812 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
813
814 // local copy of the callback flags
815 int flags = mPreviewCallbackFlag;
816
817 // is callback enabled?
818 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
819 // If the enable bit is off, the copy-out and one-shot bits are ignored
820 LOG2("frame callback is disabled");
821 mLock.unlock();
822 return;
823 }
824
825 // hold a strong pointer to the client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800826 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700827
828 // clear callback flags if no client or one-shot mode
829 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
830 LOG2("Disable preview callback");
831 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
832 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
833 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
834 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
835 }
836
837 if (c != 0) {
838 // Is the received frame copied out or not?
839 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
840 LOG2("frame is copied");
841 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
842 } else {
843 LOG2("frame is forwarded");
844 mLock.unlock();
845 c->dataCallback(msgType, mem, metadata);
846 }
847 } else {
848 mLock.unlock();
849 }
850}
851
852// picture callback - postview image ready
853void CameraClient::handlePostview(const sp<IMemory>& mem) {
854 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
855
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800856 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700857 mLock.unlock();
858 if (c != 0) {
859 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
860 }
861}
862
863// picture callback - raw image ready
864void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
865 disableMsgType(CAMERA_MSG_RAW_IMAGE);
866
867 ssize_t offset;
868 size_t size;
869 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
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_RAW_IMAGE, mem, NULL);
875 }
876}
877
878// picture callback - compressed picture ready
879void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
880 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
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_COMPRESSED_IMAGE, mem, NULL);
886 }
887}
888
889
890void CameraClient::handleGenericNotify(int32_t msgType,
891 int32_t ext1, int32_t ext2) {
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->notifyCallback(msgType, ext1, ext2);
896 }
897}
898
899void CameraClient::handleGenericData(int32_t msgType,
900 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800901 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700902 mLock.unlock();
903 if (c != 0) {
904 c->dataCallback(msgType, dataPtr, metadata);
905 }
906}
907
908void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
909 int32_t msgType, const sp<IMemory>& dataPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800910 sp<ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700911 mLock.unlock();
912 if (c != 0) {
913 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
914 }
915}
916
917void CameraClient::copyFrameAndPostCopiedFrame(
918 int32_t msgType, const sp<ICameraClient>& client,
919 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
920 camera_frame_metadata_t *metadata) {
921 LOG2("copyFrameAndPostCopiedFrame");
922 // It is necessary to copy out of pmem before sending this to
923 // the callback. For efficiency, reuse the same MemoryHeapBase
924 // provided it's big enough. Don't allocate the memory or
925 // perform the copy if there's no callback.
926 // hold the preview lock while we grab a reference to the preview buffer
927 sp<MemoryHeapBase> previewBuffer;
928
929 if (mPreviewBuffer == 0) {
930 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
931 } else if (size > mPreviewBuffer->virtualSize()) {
932 mPreviewBuffer.clear();
933 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
934 }
935 if (mPreviewBuffer == 0) {
936 ALOGE("failed to allocate space for preview buffer");
937 mLock.unlock();
938 return;
939 }
940 previewBuffer = mPreviewBuffer;
941
Ruben Brunk65e01f72014-08-29 17:25:13 -0700942 void* previewBufferBase = previewBuffer->base();
943 void* heapBase = heap->base();
944
945 if (heapBase == MAP_FAILED) {
946 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
947 mLock.unlock();
948 return;
949 } else if (previewBufferBase == MAP_FAILED) {
950 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
951 mLock.unlock();
952 return;
953 }
954
955 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700956
957 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
958 if (frame == 0) {
959 ALOGE("failed to allocate space for frame callback");
960 mLock.unlock();
961 return;
962 }
963
964 mLock.unlock();
965 client->dataCallback(msgType, frame, metadata);
966}
967
968int CameraClient::getOrientation(int degrees, bool mirror) {
969 if (!mirror) {
970 if (degrees == 0) return 0;
971 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
972 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
973 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
974 } else { // Do mirror (horizontal flip)
975 if (degrees == 0) { // FLIP_H and ROT_0
976 return HAL_TRANSFORM_FLIP_H;
977 } else if (degrees == 90) { // FLIP_H and ROT_90
978 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
979 } else if (degrees == 180) { // FLIP_H and ROT_180
980 return HAL_TRANSFORM_FLIP_V;
981 } else if (degrees == 270) { // FLIP_H and ROT_270
982 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
983 }
984 }
985 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
986 return -1;
987}
988
989}; // namespace android