blob: e848a3fed5f83476b716d20203bc48364ad6389d [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
Mathias Agopian05d19b02017-02-28 16:28:19 -080020#include <cutils/atomic.h>
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070021#include <cutils/properties.h>
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070022#include <gui/Surface.h>
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -070023#include <media/hardware/HardwareAPI.h>
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070024
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025#include "api1/CameraClient.h"
26#include "device1/CameraHardwareInterface.h"
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070027#include "CameraService.h"
28
29namespace android {
30
31#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
32#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
33
34static int getCallingPid() {
35 return IPCThreadState::self()->getCallingPid();
36}
37
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070038CameraClient::CameraClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080039 const sp<hardware::ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080040 const String16& clientPackageName,
41 int cameraId, int cameraFacing,
42 int clientPid, int clientUid,
Igor Murashkina858ea02014-08-19 14:53:08 -070043 int servicePid, bool legacyMode):
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080044 Client(cameraService, cameraClient, clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080045 String8::format("%d", cameraId), cameraFacing, clientPid,
46 clientUid, servicePid)
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070047{
48 int callingPid = getCallingPid();
49 LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
50
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070051 mHardware = NULL;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070052 mMsgEnabled = 0;
53 mSurface = 0;
54 mPreviewWindow = 0;
55 mDestructionStarted = false;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070056
57 // Callback is disabled by default
58 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
59 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Igor Murashkina858ea02014-08-19 14:53:08 -070060 mLegacyMode = legacyMode;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070061 mPlayShutterSound = true;
62 LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
63}
64
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080065status_t CameraClient::initialize(sp<CameraProviderManager> manager) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070066 int callingPid = getCallingPid();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080067 status_t res;
68
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070069 LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
70
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080071 // Verify ops permissions
72 res = startCameraOps();
73 if (res != OK) {
74 return res;
75 }
76
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070077 char camera_device_name[10];
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070078 snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
79
80 mHardware = new CameraHardwareInterface(camera_device_name);
Emilian Peevf53f66e2017-04-11 14:29:43 +010081 res = mHardware->initialize(manager);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070082 if (res != OK) {
83 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
84 __FUNCTION__, mCameraId, strerror(-res), res);
Igor Murashkin44f120f2012-10-09 14:45:37 -070085 mHardware.clear();
Zhijun Heb10cdad2014-06-16 16:38:35 -070086 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070087 }
88
89 mHardware->setCallbacks(notifyCallback,
90 dataCallback,
91 dataCallbackTimestamp,
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070092 handleCallbackTimestampBatch,
Kévin PETIT377b2ec2014-02-03 12:35:36 +000093 (void *)(uintptr_t)mCameraId);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070094
95 // Enable zoom, error, focus, and metadata messages by default
96 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
97 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
98
99 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
100 return OK;
101}
102
103
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700104// tear down the client
105CameraClient::~CameraClient() {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700106 mDestructionStarted = true;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700107 int callingPid = getCallingPid();
108 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
109
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700110 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700111 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
112}
113
114status_t CameraClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800115 return BasicClient::dump(fd, args);
116}
117
118status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700119 const size_t SIZE = 256;
120 char buffer[SIZE];
121
Ruben Brunkcc776712015-02-17 20:18:47 -0800122 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n",
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700123 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800124 (getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800125 IInterface::asBinder(getRemoteCallback()).get() : NULL),
Ruben Brunkcc776712015-02-17 20:18:47 -0800126 mClientUid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700127 len = (len > SIZE - 1) ? SIZE - 1 : len;
128 write(fd, buffer, len);
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700129
130 len = snprintf(buffer, SIZE, "Latest set parameters:\n");
131 len = (len > SIZE - 1) ? SIZE - 1 : len;
132 write(fd, buffer, len);
133
134 mLatestSetParameters.dump(fd, args);
135
136 const char *enddump = "\n\n";
137 write(fd, enddump, strlen(enddump));
138
Yin-Chia Yeh9cf785b2017-12-01 13:37:30 -0800139 sp<CameraHardwareInterface> hardware = mHardware;
140 if (hardware != nullptr) {
141 return hardware->dump(fd, args);
142 }
143 ALOGI("%s: camera device closed already, skip dumping", __FUNCTION__);
144 return OK;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700145}
146
147// ----------------------------------------------------------------------------
148
149status_t CameraClient::checkPid() const {
150 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700151 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700152
153 ALOGW("attempt to use a locked camera from a different process"
154 " (old pid %d, new pid %d)", mClientPid, callingPid);
155 return EBUSY;
156}
157
158status_t CameraClient::checkPidAndHardware() const {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700159 if (mHardware == 0) {
160 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
161 return INVALID_OPERATION;
162 }
Eino-Ville Talvala6192b892016-04-04 12:31:18 -0700163 status_t result = checkPid();
164 if (result != NO_ERROR) return result;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700165 return NO_ERROR;
166}
167
168status_t CameraClient::lock() {
169 int callingPid = getCallingPid();
170 LOG1("lock (pid %d)", callingPid);
171 Mutex::Autolock lock(mLock);
172
173 // lock camera to this client if the the camera is unlocked
174 if (mClientPid == 0) {
175 mClientPid = callingPid;
176 return NO_ERROR;
177 }
178
179 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
180 return checkPid();
181}
182
183status_t CameraClient::unlock() {
184 int callingPid = getCallingPid();
185 LOG1("unlock (pid %d)", callingPid);
186 Mutex::Autolock lock(mLock);
187
188 // allow anyone to use camera (after they lock the camera)
189 status_t result = checkPid();
190 if (result == NO_ERROR) {
191 if (mHardware->recordingEnabled()) {
192 ALOGE("Not allowed to unlock camera during recording.");
193 return INVALID_OPERATION;
194 }
195 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800196 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700197 // we need to remove the reference to ICameraClient so that when the app
198 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800199 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700200 }
201 return result;
202}
203
204// connect a new client to the camera
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800205status_t CameraClient::connect(const sp<hardware::ICameraClient>& client) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700206 int callingPid = getCallingPid();
207 LOG1("connect E (pid %d)", callingPid);
208 Mutex::Autolock lock(mLock);
209
210 if (mClientPid != 0 && checkPid() != NO_ERROR) {
211 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
212 mClientPid, callingPid);
213 return EBUSY;
214 }
215
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800216 if (mRemoteCallback != 0 &&
Marco Nelissen06b46062014-11-14 07:58:25 -0800217 (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700218 LOG1("Connect to the same client");
219 return NO_ERROR;
220 }
221
222 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
223 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800224 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700225
226 LOG1("connect X (pid %d)", callingPid);
227 return NO_ERROR;
228}
229
230static void disconnectWindow(const sp<ANativeWindow>& window) {
231 if (window != 0) {
232 status_t result = native_window_api_disconnect(window.get(),
233 NATIVE_WINDOW_API_CAMERA);
234 if (result != NO_ERROR) {
235 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
236 result);
237 }
238 }
239}
240
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800241binder::Status CameraClient::disconnect() {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700242 int callingPid = getCallingPid();
243 LOG1("disconnect E (pid %d)", callingPid);
244 Mutex::Autolock lock(mLock);
245
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800246 binder::Status res = binder::Status::ok();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800247 // Allow both client and the cameraserver to disconnect at all times
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700248 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700249 ALOGW("different client - don't disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800250 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700251 }
252
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700253 // Make sure disconnect() is done once and once only, whether it is called
254 // from the user directly, or called by the destructor.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800255 if (mHardware == 0) return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700256
257 LOG1("hardware teardown");
258 // Before destroying mHardware, we must make sure it's in the
259 // idle state.
260 // Turn off all messages.
261 disableMsgType(CAMERA_MSG_ALL_MSGS);
262 mHardware->stopPreview();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800263 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700264 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
265 mCameraIdStr, mCameraFacing, mClientPackageName);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700266 mHardware->cancelPicture();
267 // Release the hardware resources.
268 mHardware->release();
269
270 // Release the held ANativeWindow resources.
271 if (mPreviewWindow != 0) {
272 disconnectWindow(mPreviewWindow);
273 mPreviewWindow = 0;
274 mHardware->setPreviewWindow(mPreviewWindow);
275 }
276 mHardware.clear();
277
278 CameraService::Client::disconnect();
279
280 LOG1("disconnect X (pid %d)", callingPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800281
282 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700283}
284
285// ----------------------------------------------------------------------------
286
287status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
288 const sp<ANativeWindow>& window) {
289 Mutex::Autolock lock(mLock);
290 status_t result = checkPidAndHardware();
291 if (result != NO_ERROR) return result;
292
293 // return if no change in surface.
294 if (binder == mSurface) {
295 return NO_ERROR;
296 }
297
298 if (window != 0) {
299 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
300 if (result != NO_ERROR) {
301 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
302 result);
303 return result;
304 }
305 }
306
307 // If preview has been already started, register preview buffers now.
308 if (mHardware->previewEnabled()) {
309 if (window != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800310 mHardware->setPreviewScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
311 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700312 result = mHardware->setPreviewWindow(window);
313 }
314 }
315
316 if (result == NO_ERROR) {
317 // Everything has succeeded. Disconnect the old window and remember the
318 // new window.
319 disconnectWindow(mPreviewWindow);
320 mSurface = binder;
321 mPreviewWindow = window;
322 } else {
323 // Something went wrong after we connected to the new window, so
324 // disconnect here.
325 disconnectWindow(window);
326 }
327
328 return result;
329}
330
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700331// set the buffer consumer that the preview will use
332status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800333 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700334 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700335 getCallingPid());
336
337 sp<IBinder> binder;
338 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800339 if (bufferProducer != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800340 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700341 // Using controlledByApp flag to ensure that the buffer queue remains in
342 // async mode for the old camera API, where many applications depend
343 // on that behavior.
344 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700345 }
346 return setPreviewWindow(binder, window);
347}
348
349// set the preview callback flag to affect how the received frames from
350// preview are handled.
351void CameraClient::setPreviewCallbackFlag(int callback_flag) {
352 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
353 Mutex::Autolock lock(mLock);
354 if (checkPidAndHardware() != NO_ERROR) return;
355
356 mPreviewCallbackFlag = callback_flag;
357 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
358 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
359 } else {
360 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
361 }
362}
363
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700364status_t CameraClient::setPreviewCallbackTarget(
365 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700366 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700367 ALOGE("%s: Unimplemented!", __FUNCTION__);
368 return INVALID_OPERATION;
369}
370
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700371// start preview mode
372status_t CameraClient::startPreview() {
373 LOG1("startPreview (pid %d)", getCallingPid());
374 return startCameraMode(CAMERA_PREVIEW_MODE);
375}
376
377// start recording mode
378status_t CameraClient::startRecording() {
379 LOG1("startRecording (pid %d)", getCallingPid());
380 return startCameraMode(CAMERA_RECORDING_MODE);
381}
382
383// start preview or recording
384status_t CameraClient::startCameraMode(camera_mode mode) {
385 LOG1("startCameraMode(%d)", mode);
386 Mutex::Autolock lock(mLock);
387 status_t result = checkPidAndHardware();
388 if (result != NO_ERROR) return result;
389
390 switch(mode) {
391 case CAMERA_PREVIEW_MODE:
392 if (mSurface == 0 && mPreviewWindow == 0) {
393 LOG1("mSurface is not set yet.");
394 // still able to start preview in this case.
395 }
396 return startPreviewMode();
397 case CAMERA_RECORDING_MODE:
398 if (mSurface == 0 && mPreviewWindow == 0) {
399 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
400 return INVALID_OPERATION;
401 }
402 return startRecordingMode();
403 default:
404 return UNKNOWN_ERROR;
405 }
406}
407
408status_t CameraClient::startPreviewMode() {
409 LOG1("startPreviewMode");
410 status_t result = NO_ERROR;
411
412 // if preview has been enabled, nothing needs to be done
413 if (mHardware->previewEnabled()) {
414 return NO_ERROR;
415 }
416
417 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800418 mHardware->setPreviewScalingMode(
419 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
420 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700421 }
422 mHardware->setPreviewWindow(mPreviewWindow);
423 result = mHardware->startPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700424 if (result == NO_ERROR) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800425 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700426 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE,
427 mCameraIdStr, mCameraFacing, mClientPackageName);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700428 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700429 return result;
430}
431
432status_t CameraClient::startRecordingMode() {
433 LOG1("startRecordingMode");
434 status_t result = NO_ERROR;
435
436 // if recording has been enabled, nothing needs to be done
437 if (mHardware->recordingEnabled()) {
438 return NO_ERROR;
439 }
440
441 // if preview has not been started, start preview first
442 if (!mHardware->previewEnabled()) {
443 result = startPreviewMode();
444 if (result != NO_ERROR) {
445 return result;
446 }
447 }
448
449 // start recording mode
450 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800451 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700452 result = mHardware->startRecording();
453 if (result != NO_ERROR) {
454 ALOGE("mHardware->startRecording() failed with status %d", result);
455 }
456 return result;
457}
458
459// stop preview mode
460void CameraClient::stopPreview() {
461 LOG1("stopPreview (pid %d)", getCallingPid());
462 Mutex::Autolock lock(mLock);
463 if (checkPidAndHardware() != NO_ERROR) return;
464
465
466 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
467 mHardware->stopPreview();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800468 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700469 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
470 mCameraIdStr, mCameraFacing, mClientPackageName);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700471 mPreviewBuffer.clear();
472}
473
474// stop recording mode
475void CameraClient::stopRecording() {
476 LOG1("stopRecording (pid %d)", getCallingPid());
Emilian Peev698f0a72017-04-05 11:20:09 +0100477 {
478 Mutex::Autolock lock(mLock);
479 if (checkPidAndHardware() != NO_ERROR) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700480
Emilian Peev698f0a72017-04-05 11:20:09 +0100481 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
482 mHardware->stopRecording();
483 sCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700484
Emilian Peev698f0a72017-04-05 11:20:09 +0100485 mPreviewBuffer.clear();
486 }
487
488 {
489 Mutex::Autolock l(mAvailableCallbackBuffersLock);
490 if (!mAvailableCallbackBuffers.empty()) {
491 mAvailableCallbackBuffers.clear();
492 }
493 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700494}
495
496// release a recording frame
497void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
498 Mutex::Autolock lock(mLock);
499 if (checkPidAndHardware() != NO_ERROR) return;
Chien-Yu Chen42f27072016-01-11 17:39:17 -0800500 if (mem == nullptr) {
501 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26164272",
502 IPCThreadState::self()->getCallingUid(), nullptr, 0);
503 return;
504 }
505
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700506 mHardware->releaseRecordingFrame(mem);
507}
508
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700509void CameraClient::releaseRecordingFrameHandle(native_handle_t *handle) {
510 if (handle == nullptr) return;
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800511 Mutex::Autolock lock(mLock);
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700512 sp<IMemory> dataPtr;
513 {
514 Mutex::Autolock l(mAvailableCallbackBuffersLock);
515 if (!mAvailableCallbackBuffers.empty()) {
516 dataPtr = mAvailableCallbackBuffers.back();
517 mAvailableCallbackBuffers.pop_back();
518 }
519 }
520
521 if (dataPtr == nullptr) {
522 ALOGE("%s: %d: No callback buffer available. Dropping a native handle.", __FUNCTION__,
523 __LINE__);
524 native_handle_close(handle);
525 native_handle_delete(handle);
526 return;
527 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
528 ALOGE("%s: %d: Callback buffer size doesn't match VideoNativeHandleMetadata", __FUNCTION__,
529 __LINE__);
530 native_handle_close(handle);
531 native_handle_delete(handle);
532 return;
533 }
534
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800535 if (mHardware != nullptr) {
536 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
537 metadata->eType = kMetadataBufferTypeNativeHandleSource;
538 metadata->pHandle = handle;
539 mHardware->releaseRecordingFrame(dataPtr);
540 }
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700541}
542
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700543void CameraClient::releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles) {
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800544 Mutex::Autolock lock(mLock);
545 bool disconnected = (mHardware == nullptr);
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700546 size_t n = handles.size();
547 std::vector<sp<IMemory>> frames;
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800548 if (!disconnected) {
549 frames.reserve(n);
550 }
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700551 bool error = false;
552 for (auto& handle : handles) {
553 sp<IMemory> dataPtr;
554 {
555 Mutex::Autolock l(mAvailableCallbackBuffersLock);
556 if (!mAvailableCallbackBuffers.empty()) {
557 dataPtr = mAvailableCallbackBuffers.back();
558 mAvailableCallbackBuffers.pop_back();
559 }
560 }
561
562 if (dataPtr == nullptr) {
563 ALOGE("%s: %d: No callback buffer available. Dropping frames.", __FUNCTION__,
564 __LINE__);
565 error = true;
566 break;
567 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
568 ALOGE("%s: %d: Callback buffer must be VideoNativeHandleMetadata", __FUNCTION__,
569 __LINE__);
570 error = true;
571 break;
572 }
573
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800574 if (!disconnected) {
575 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
576 metadata->eType = kMetadataBufferTypeNativeHandleSource;
577 metadata->pHandle = handle;
578 frames.push_back(dataPtr);
579 }
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700580 }
581
582 if (error) {
583 for (auto& handle : handles) {
584 native_handle_close(handle);
585 native_handle_delete(handle);
586 }
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800587 } else if (!disconnected) {
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700588 mHardware->releaseRecordingFrameBatch(frames);
589 }
590 return;
591}
592
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800593status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
594 LOG1("setVideoBufferMode: %d", videoBufferMode);
595 bool enableMetadataInBuffers = false;
596
597 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
598 enableMetadataInBuffers = true;
599 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
600 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
601 videoBufferMode);
602 return BAD_VALUE;
603 }
604
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700605 Mutex::Autolock lock(mLock);
606 if (checkPidAndHardware() != NO_ERROR) {
607 return UNKNOWN_ERROR;
608 }
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800609
610 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700611}
612
613bool CameraClient::previewEnabled() {
614 LOG1("previewEnabled (pid %d)", getCallingPid());
615
616 Mutex::Autolock lock(mLock);
617 if (checkPidAndHardware() != NO_ERROR) return false;
618 return mHardware->previewEnabled();
619}
620
621bool CameraClient::recordingEnabled() {
622 LOG1("recordingEnabled (pid %d)", getCallingPid());
623
624 Mutex::Autolock lock(mLock);
625 if (checkPidAndHardware() != NO_ERROR) return false;
626 return mHardware->recordingEnabled();
627}
628
629status_t CameraClient::autoFocus() {
630 LOG1("autoFocus (pid %d)", getCallingPid());
631
632 Mutex::Autolock lock(mLock);
633 status_t result = checkPidAndHardware();
634 if (result != NO_ERROR) return result;
635
636 return mHardware->autoFocus();
637}
638
639status_t CameraClient::cancelAutoFocus() {
640 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
641
642 Mutex::Autolock lock(mLock);
643 status_t result = checkPidAndHardware();
644 if (result != NO_ERROR) return result;
645
646 return mHardware->cancelAutoFocus();
647}
648
649// take a picture - image is returned in callback
650status_t CameraClient::takePicture(int msgType) {
651 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
652
653 Mutex::Autolock lock(mLock);
654 status_t result = checkPidAndHardware();
655 if (result != NO_ERROR) return result;
656
657 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
658 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
659 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
660 " cannot be both enabled");
661 return BAD_VALUE;
662 }
663
664 // We only accept picture related message types
665 // and ignore other types of messages for takePicture().
666 int picMsgType = msgType
667 & (CAMERA_MSG_SHUTTER |
668 CAMERA_MSG_POSTVIEW_FRAME |
669 CAMERA_MSG_RAW_IMAGE |
670 CAMERA_MSG_RAW_IMAGE_NOTIFY |
671 CAMERA_MSG_COMPRESSED_IMAGE);
672
673 enableMsgType(picMsgType);
674
675 return mHardware->takePicture();
676}
677
678// set preview/capture parameters - key/value pairs
679status_t CameraClient::setParameters(const String8& params) {
680 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
681
682 Mutex::Autolock lock(mLock);
683 status_t result = checkPidAndHardware();
684 if (result != NO_ERROR) return result;
685
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700686 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700687 CameraParameters p(params);
688 return mHardware->setParameters(p);
689}
690
691// get preview/capture parameters - key/value pairs
692String8 CameraClient::getParameters() const {
693 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700694 // The camera service can unconditionally get the parameters at all times
695 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700696
697 String8 params(mHardware->getParameters().flatten());
698 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
699 return params;
700}
701
702// enable shutter sound
703status_t CameraClient::enableShutterSound(bool enable) {
704 LOG1("enableShutterSound (pid %d)", getCallingPid());
705
706 status_t result = checkPidAndHardware();
707 if (result != NO_ERROR) return result;
708
709 if (enable) {
710 mPlayShutterSound = true;
711 return OK;
712 }
713
Igor Murashkina858ea02014-08-19 14:53:08 -0700714 // the camera2 api legacy mode can unconditionally disable the shutter sound
715 if (mLegacyMode) {
716 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
717 mPlayShutterSound = false;
718 return OK;
719 }
720
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700721 // Disabling shutter sound may not be allowed. In that case only
722 // allow the mediaserver process to disable the sound.
723 char value[PROPERTY_VALUE_MAX];
724 property_get("ro.camera.sound.forced", value, "0");
725 if (strcmp(value, "0") != 0) {
726 // Disabling shutter sound is not allowed. Deny if the current
727 // process is not mediaserver.
728 if (getCallingPid() != getpid()) {
729 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
730 return PERMISSION_DENIED;
731 }
732 }
733
734 mPlayShutterSound = false;
735 return OK;
736}
737
738status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
739 LOG1("sendCommand (pid %d)", getCallingPid());
740 int orientation;
741 Mutex::Autolock lock(mLock);
742 status_t result = checkPidAndHardware();
743 if (result != NO_ERROR) return result;
744
745 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
746 // Mirror the preview if the camera is front-facing.
747 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
748 if (orientation == -1) return BAD_VALUE;
749
750 if (mOrientation != orientation) {
751 mOrientation = orientation;
752 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800753 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700754 }
755 }
756 return OK;
757 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
758 switch (arg1) {
759 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700760 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700761 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700762 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700763 default:
764 return BAD_VALUE;
765 }
766 return OK;
767 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800768 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
James Dong983cf232012-08-01 16:39:55 -0700769 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
770 // Silently ignore this command
771 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700772 } else if (cmd == CAMERA_CMD_PING) {
773 // If mHardware is 0, checkPidAndHardware will return error.
774 return OK;
775 }
776
777 return mHardware->sendCommand(cmd, arg1, arg2);
778}
779
780// ----------------------------------------------------------------------------
781
782void CameraClient::enableMsgType(int32_t msgType) {
783 android_atomic_or(msgType, &mMsgEnabled);
784 mHardware->enableMsgType(msgType);
785}
786
787void CameraClient::disableMsgType(int32_t msgType) {
788 android_atomic_and(~msgType, &mMsgEnabled);
789 mHardware->disableMsgType(msgType);
790}
791
792#define CHECK_MESSAGE_INTERVAL 10 // 10ms
793bool CameraClient::lockIfMessageWanted(int32_t msgType) {
794 int sleepCount = 0;
795 while (mMsgEnabled & msgType) {
796 if (mLock.tryLock() == NO_ERROR) {
797 if (sleepCount > 0) {
798 LOG1("lockIfMessageWanted(%d): waited for %d ms",
799 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
800 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800801
802 // If messages are no longer enabled after acquiring lock, release and drop message
803 if ((mMsgEnabled & msgType) == 0) {
804 mLock.unlock();
805 break;
806 }
807
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700808 return true;
809 }
810 if (sleepCount++ == 0) {
811 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
812 }
813 usleep(CHECK_MESSAGE_INTERVAL * 1000);
814 }
815 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
816 return false;
817}
818
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800819sp<CameraClient> CameraClient::getClientFromCookie(void* user) {
820 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
821 auto clientDescriptor = sCameraService->mActiveClientManager.get(cameraId);
822 if (clientDescriptor != nullptr) {
823 return sp<CameraClient>{
824 static_cast<CameraClient*>(clientDescriptor->getValue().get())};
825 }
826 return sp<CameraClient>{nullptr};
827}
828
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700829// Callback messages can be dispatched to internal handlers or pass to our
830// client's callback functions, depending on the message type.
831//
832// notifyCallback:
833// CAMERA_MSG_SHUTTER handleShutter
834// (others) c->notifyCallback
835// dataCallback:
836// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
837// CAMERA_MSG_POSTVIEW_FRAME handlePostview
838// CAMERA_MSG_RAW_IMAGE handleRawPicture
839// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
840// (others) c->dataCallback
841// dataCallbackTimestamp
842// (others) c->dataCallbackTimestamp
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700843
844void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
845 int32_t ext2, void* user) {
846 LOG2("notifyCallback(%d)", msgType);
847
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800848 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800849 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700850
851 if (!client->lockIfMessageWanted(msgType)) return;
852
853 switch (msgType) {
854 case CAMERA_MSG_SHUTTER:
855 // ext1 is the dimension of the yuv picture.
856 client->handleShutter();
857 break;
858 default:
859 client->handleGenericNotify(msgType, ext1, ext2);
860 break;
861 }
862}
863
864void CameraClient::dataCallback(int32_t msgType,
865 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
866 LOG2("dataCallback(%d)", msgType);
867
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800868 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800869 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700870
871 if (!client->lockIfMessageWanted(msgType)) return;
872 if (dataPtr == 0 && metadata == NULL) {
873 ALOGE("Null data returned in data callback");
874 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
875 return;
876 }
877
878 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
879 case CAMERA_MSG_PREVIEW_FRAME:
880 client->handlePreviewData(msgType, dataPtr, metadata);
881 break;
882 case CAMERA_MSG_POSTVIEW_FRAME:
883 client->handlePostview(dataPtr);
884 break;
885 case CAMERA_MSG_RAW_IMAGE:
886 client->handleRawPicture(dataPtr);
887 break;
888 case CAMERA_MSG_COMPRESSED_IMAGE:
889 client->handleCompressedPicture(dataPtr);
890 break;
891 default:
892 client->handleGenericData(msgType, dataPtr, metadata);
893 break;
894 }
895}
896
897void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
898 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
899 LOG2("dataCallbackTimestamp(%d)", msgType);
900
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800901 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800902 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700903
904 if (!client->lockIfMessageWanted(msgType)) return;
905
906 if (dataPtr == 0) {
907 ALOGE("Null data returned in data with timestamp callback");
908 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
909 return;
910 }
911
912 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
913}
914
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700915void CameraClient::handleCallbackTimestampBatch(
916 int32_t msgType, const std::vector<HandleTimestampMessage>& msgs, void* user) {
917 LOG2("dataCallbackTimestampBatch");
918 sp<CameraClient> client = getClientFromCookie(user);
919 if (client.get() == nullptr) return;
920 if (!client->lockIfMessageWanted(msgType)) return;
921
922 sp<hardware::ICameraClient> c = client->mRemoteCallback;
923 client->mLock.unlock();
924 if (c != 0 && msgs.size() > 0) {
925 size_t n = msgs.size();
926 std::vector<nsecs_t> timestamps;
927 std::vector<native_handle_t*> handles;
928 timestamps.reserve(n);
929 handles.reserve(n);
930 for (auto& msg : msgs) {
931 native_handle_t* handle = nullptr;
932 if (msg.dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
933 ALOGE("%s: dataPtr does not contain VideoNativeHandleMetadata!", __FUNCTION__);
934 return;
935 }
936 VideoNativeHandleMetadata *metadata =
937 (VideoNativeHandleMetadata*)(msg.dataPtr->pointer());
938 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
939 handle = metadata->pHandle;
940 }
941
942 if (handle == nullptr) {
943 ALOGE("%s: VideoNativeHandleMetadata type mismatch or null handle passed!",
944 __FUNCTION__);
945 return;
946 }
947 {
948 Mutex::Autolock l(client->mAvailableCallbackBuffersLock);
949 client->mAvailableCallbackBuffers.push_back(msg.dataPtr);
950 }
951 timestamps.push_back(msg.timestamp);
952 handles.push_back(handle);
953 }
954 c->recordingFrameHandleCallbackTimestampBatch(timestamps, handles);
955 }
956}
957
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700958// snapshot taken callback
959void CameraClient::handleShutter(void) {
960 if (mPlayShutterSound) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800961 sCameraService->playSound(CameraService::SOUND_SHUTTER);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700962 }
963
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800964 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700965 if (c != 0) {
966 mLock.unlock();
967 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
968 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
969 }
970 disableMsgType(CAMERA_MSG_SHUTTER);
971
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700972 // Shutters only happen in response to takePicture, so mark device as
973 // idle now, until preview is restarted
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800974 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700975 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
976 mCameraIdStr, mCameraFacing, mClientPackageName);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700977
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700978 mLock.unlock();
979}
980
981// preview callback - frame buffer update
982void CameraClient::handlePreviewData(int32_t msgType,
983 const sp<IMemory>& mem,
984 camera_frame_metadata_t *metadata) {
985 ssize_t offset;
986 size_t size;
987 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
988
989 // local copy of the callback flags
990 int flags = mPreviewCallbackFlag;
991
992 // is callback enabled?
993 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
994 // If the enable bit is off, the copy-out and one-shot bits are ignored
995 LOG2("frame callback is disabled");
996 mLock.unlock();
997 return;
998 }
999
1000 // hold a strong pointer to the client
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001001 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001002
1003 // clear callback flags if no client or one-shot mode
1004 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
1005 LOG2("Disable preview callback");
1006 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1007 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1008 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
1009 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
1010 }
1011
1012 if (c != 0) {
1013 // Is the received frame copied out or not?
1014 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1015 LOG2("frame is copied");
1016 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
1017 } else {
1018 LOG2("frame is forwarded");
1019 mLock.unlock();
1020 c->dataCallback(msgType, mem, metadata);
1021 }
1022 } else {
1023 mLock.unlock();
1024 }
1025}
1026
1027// picture callback - postview image ready
1028void CameraClient::handlePostview(const sp<IMemory>& mem) {
1029 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1030
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001031 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001032 mLock.unlock();
1033 if (c != 0) {
1034 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
1035 }
1036}
1037
1038// picture callback - raw image ready
1039void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
1040 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1041
1042 ssize_t offset;
1043 size_t size;
1044 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1045
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001046 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001047 mLock.unlock();
1048 if (c != 0) {
1049 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
1050 }
1051}
1052
1053// picture callback - compressed picture ready
1054void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
1055 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1056
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001057 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001058 mLock.unlock();
1059 if (c != 0) {
1060 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
1061 }
1062}
1063
1064
1065void CameraClient::handleGenericNotify(int32_t msgType,
1066 int32_t ext1, int32_t ext2) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001067 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001068 mLock.unlock();
1069 if (c != 0) {
1070 c->notifyCallback(msgType, ext1, ext2);
1071 }
1072}
1073
1074void CameraClient::handleGenericData(int32_t msgType,
1075 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001076 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001077 mLock.unlock();
1078 if (c != 0) {
1079 c->dataCallback(msgType, dataPtr, metadata);
1080 }
1081}
1082
1083void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
1084 int32_t msgType, const sp<IMemory>& dataPtr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001085 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001086 mLock.unlock();
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -07001087 if (c != 0 && dataPtr != nullptr) {
1088 native_handle_t* handle = nullptr;
1089
1090 // Check if dataPtr contains a VideoNativeHandleMetadata.
1091 if (dataPtr->size() == sizeof(VideoNativeHandleMetadata)) {
1092 VideoNativeHandleMetadata *metadata =
1093 (VideoNativeHandleMetadata*)(dataPtr->pointer());
1094 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
1095 handle = metadata->pHandle;
1096 }
1097 }
1098
1099 // If dataPtr contains a native handle, send it via recordingFrameHandleCallbackTimestamp.
1100 if (handle != nullptr) {
1101 {
1102 Mutex::Autolock l(mAvailableCallbackBuffersLock);
1103 mAvailableCallbackBuffers.push_back(dataPtr);
1104 }
1105 c->recordingFrameHandleCallbackTimestamp(timestamp, handle);
1106 } else {
1107 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1108 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001109 }
1110}
1111
1112void CameraClient::copyFrameAndPostCopiedFrame(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001113 int32_t msgType, const sp<hardware::ICameraClient>& client,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001114 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1115 camera_frame_metadata_t *metadata) {
1116 LOG2("copyFrameAndPostCopiedFrame");
1117 // It is necessary to copy out of pmem before sending this to
1118 // the callback. For efficiency, reuse the same MemoryHeapBase
1119 // provided it's big enough. Don't allocate the memory or
1120 // perform the copy if there's no callback.
1121 // hold the preview lock while we grab a reference to the preview buffer
1122 sp<MemoryHeapBase> previewBuffer;
1123
1124 if (mPreviewBuffer == 0) {
1125 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1126 } else if (size > mPreviewBuffer->virtualSize()) {
1127 mPreviewBuffer.clear();
1128 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1129 }
1130 if (mPreviewBuffer == 0) {
1131 ALOGE("failed to allocate space for preview buffer");
1132 mLock.unlock();
1133 return;
1134 }
1135 previewBuffer = mPreviewBuffer;
1136
Ruben Brunk65e01f72014-08-29 17:25:13 -07001137 void* previewBufferBase = previewBuffer->base();
1138 void* heapBase = heap->base();
1139
1140 if (heapBase == MAP_FAILED) {
1141 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
1142 mLock.unlock();
1143 return;
1144 } else if (previewBufferBase == MAP_FAILED) {
1145 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
1146 mLock.unlock();
1147 return;
1148 }
1149
1150 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001151
1152 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1153 if (frame == 0) {
1154 ALOGE("failed to allocate space for frame callback");
1155 mLock.unlock();
1156 return;
1157 }
1158
1159 mLock.unlock();
1160 client->dataCallback(msgType, frame, metadata);
1161}
1162
1163int CameraClient::getOrientation(int degrees, bool mirror) {
1164 if (!mirror) {
1165 if (degrees == 0) return 0;
1166 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1167 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1168 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1169 } else { // Do mirror (horizontal flip)
1170 if (degrees == 0) { // FLIP_H and ROT_0
1171 return HAL_TRANSFORM_FLIP_H;
1172 } else if (degrees == 90) { // FLIP_H and ROT_90
1173 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1174 } else if (degrees == 180) { // FLIP_H and ROT_180
1175 return HAL_TRANSFORM_FLIP_V;
1176 } else if (degrees == 270) { // FLIP_H and ROT_270
1177 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1178 }
1179 }
1180 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1181 return -1;
1182}
1183
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001184status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001185 (void)bufferProducer;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001186 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1187 return INVALID_OPERATION;
1188}
1189
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001190}; // namespace android