blob: ce44efe2f8403108b41bf4b999196c91bede9364 [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,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -070043 int servicePid):
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080044 Client(cameraService, cameraClient, clientPackageName,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080045 String8::format("%d", cameraId), cameraId, cameraFacing, clientPid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080046 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);
60 mPlayShutterSound = true;
61 LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
62}
63
Emilian Peevbd8c5032018-02-14 23:05:40 +000064status_t CameraClient::initialize(sp<CameraProviderManager> manager,
65 const String8& /*monitorTags*/) {
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,
Emilian Peev573291c2018-02-10 02:10:56 +0000265 mCameraIdStr, mCameraFacing, mClientPackageName,
266 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700267 mHardware->cancelPicture();
268 // Release the hardware resources.
269 mHardware->release();
270
271 // Release the held ANativeWindow resources.
272 if (mPreviewWindow != 0) {
273 disconnectWindow(mPreviewWindow);
274 mPreviewWindow = 0;
275 mHardware->setPreviewWindow(mPreviewWindow);
276 }
277 mHardware.clear();
278
279 CameraService::Client::disconnect();
280
281 LOG1("disconnect X (pid %d)", callingPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800282
283 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700284}
285
286// ----------------------------------------------------------------------------
287
288status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
289 const sp<ANativeWindow>& window) {
290 Mutex::Autolock lock(mLock);
291 status_t result = checkPidAndHardware();
292 if (result != NO_ERROR) return result;
293
294 // return if no change in surface.
295 if (binder == mSurface) {
296 return NO_ERROR;
297 }
298
299 if (window != 0) {
300 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
301 if (result != NO_ERROR) {
302 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
303 result);
304 return result;
305 }
306 }
307
308 // If preview has been already started, register preview buffers now.
309 if (mHardware->previewEnabled()) {
310 if (window != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800311 mHardware->setPreviewScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
312 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700313 result = mHardware->setPreviewWindow(window);
314 }
315 }
316
317 if (result == NO_ERROR) {
318 // Everything has succeeded. Disconnect the old window and remember the
319 // new window.
320 disconnectWindow(mPreviewWindow);
321 mSurface = binder;
322 mPreviewWindow = window;
323 } else {
324 // Something went wrong after we connected to the new window, so
325 // disconnect here.
326 disconnectWindow(window);
327 }
328
329 return result;
330}
331
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700332// set the buffer consumer that the preview will use
333status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800334 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700335 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700336 getCallingPid());
337
338 sp<IBinder> binder;
339 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800340 if (bufferProducer != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800341 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700342 // Using controlledByApp flag to ensure that the buffer queue remains in
343 // async mode for the old camera API, where many applications depend
344 // on that behavior.
345 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700346 }
347 return setPreviewWindow(binder, window);
348}
349
350// set the preview callback flag to affect how the received frames from
351// preview are handled.
352void CameraClient::setPreviewCallbackFlag(int callback_flag) {
353 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
354 Mutex::Autolock lock(mLock);
355 if (checkPidAndHardware() != NO_ERROR) return;
356
357 mPreviewCallbackFlag = callback_flag;
358 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
359 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
360 } else {
361 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
362 }
363}
364
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700365status_t CameraClient::setPreviewCallbackTarget(
366 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700367 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700368 ALOGE("%s: Unimplemented!", __FUNCTION__);
369 return INVALID_OPERATION;
370}
371
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700372// start preview mode
373status_t CameraClient::startPreview() {
374 LOG1("startPreview (pid %d)", getCallingPid());
375 return startCameraMode(CAMERA_PREVIEW_MODE);
376}
377
378// start recording mode
379status_t CameraClient::startRecording() {
380 LOG1("startRecording (pid %d)", getCallingPid());
381 return startCameraMode(CAMERA_RECORDING_MODE);
382}
383
384// start preview or recording
385status_t CameraClient::startCameraMode(camera_mode mode) {
386 LOG1("startCameraMode(%d)", mode);
387 Mutex::Autolock lock(mLock);
388 status_t result = checkPidAndHardware();
389 if (result != NO_ERROR) return result;
390
391 switch(mode) {
392 case CAMERA_PREVIEW_MODE:
393 if (mSurface == 0 && mPreviewWindow == 0) {
394 LOG1("mSurface is not set yet.");
395 // still able to start preview in this case.
396 }
397 return startPreviewMode();
398 case CAMERA_RECORDING_MODE:
399 if (mSurface == 0 && mPreviewWindow == 0) {
400 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
401 return INVALID_OPERATION;
402 }
403 return startRecordingMode();
404 default:
405 return UNKNOWN_ERROR;
406 }
407}
408
409status_t CameraClient::startPreviewMode() {
410 LOG1("startPreviewMode");
411 status_t result = NO_ERROR;
412
413 // if preview has been enabled, nothing needs to be done
414 if (mHardware->previewEnabled()) {
415 return NO_ERROR;
416 }
417
418 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800419 mHardware->setPreviewScalingMode(
420 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
421 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700422 }
423 mHardware->setPreviewWindow(mPreviewWindow);
424 result = mHardware->startPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700425 if (result == NO_ERROR) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800426 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700427 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE,
Emilian Peev573291c2018-02-10 02:10:56 +0000428 mCameraIdStr, mCameraFacing, mClientPackageName,
429 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700430 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700431 return result;
432}
433
434status_t CameraClient::startRecordingMode() {
435 LOG1("startRecordingMode");
436 status_t result = NO_ERROR;
437
438 // if recording has been enabled, nothing needs to be done
439 if (mHardware->recordingEnabled()) {
440 return NO_ERROR;
441 }
442
443 // if preview has not been started, start preview first
444 if (!mHardware->previewEnabled()) {
445 result = startPreviewMode();
446 if (result != NO_ERROR) {
447 return result;
448 }
449 }
450
451 // start recording mode
452 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800453 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700454 result = mHardware->startRecording();
455 if (result != NO_ERROR) {
456 ALOGE("mHardware->startRecording() failed with status %d", result);
457 }
458 return result;
459}
460
461// stop preview mode
462void CameraClient::stopPreview() {
463 LOG1("stopPreview (pid %d)", getCallingPid());
464 Mutex::Autolock lock(mLock);
465 if (checkPidAndHardware() != NO_ERROR) return;
466
467
468 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
469 mHardware->stopPreview();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800470 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700471 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
Emilian Peev573291c2018-02-10 02:10:56 +0000472 mCameraIdStr, mCameraFacing, mClientPackageName,
473 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700474 mPreviewBuffer.clear();
475}
476
477// stop recording mode
478void CameraClient::stopRecording() {
479 LOG1("stopRecording (pid %d)", getCallingPid());
Emilian Peev698f0a72017-04-05 11:20:09 +0100480 {
481 Mutex::Autolock lock(mLock);
482 if (checkPidAndHardware() != NO_ERROR) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700483
Emilian Peev698f0a72017-04-05 11:20:09 +0100484 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
485 mHardware->stopRecording();
486 sCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700487
Emilian Peev698f0a72017-04-05 11:20:09 +0100488 mPreviewBuffer.clear();
489 }
490
491 {
492 Mutex::Autolock l(mAvailableCallbackBuffersLock);
493 if (!mAvailableCallbackBuffers.empty()) {
494 mAvailableCallbackBuffers.clear();
495 }
496 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700497}
498
499// release a recording frame
500void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
501 Mutex::Autolock lock(mLock);
502 if (checkPidAndHardware() != NO_ERROR) return;
Chien-Yu Chen42f27072016-01-11 17:39:17 -0800503 if (mem == nullptr) {
504 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26164272",
505 IPCThreadState::self()->getCallingUid(), nullptr, 0);
506 return;
507 }
508
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700509 mHardware->releaseRecordingFrame(mem);
510}
511
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700512void CameraClient::releaseRecordingFrameHandle(native_handle_t *handle) {
513 if (handle == nullptr) return;
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800514 Mutex::Autolock lock(mLock);
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700515 sp<IMemory> dataPtr;
516 {
517 Mutex::Autolock l(mAvailableCallbackBuffersLock);
518 if (!mAvailableCallbackBuffers.empty()) {
519 dataPtr = mAvailableCallbackBuffers.back();
520 mAvailableCallbackBuffers.pop_back();
521 }
522 }
523
524 if (dataPtr == nullptr) {
525 ALOGE("%s: %d: No callback buffer available. Dropping a native handle.", __FUNCTION__,
526 __LINE__);
527 native_handle_close(handle);
528 native_handle_delete(handle);
529 return;
530 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
531 ALOGE("%s: %d: Callback buffer size doesn't match VideoNativeHandleMetadata", __FUNCTION__,
532 __LINE__);
533 native_handle_close(handle);
534 native_handle_delete(handle);
535 return;
536 }
537
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800538 if (mHardware != nullptr) {
539 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
540 metadata->eType = kMetadataBufferTypeNativeHandleSource;
541 metadata->pHandle = handle;
542 mHardware->releaseRecordingFrame(dataPtr);
543 }
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700544}
545
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700546void CameraClient::releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles) {
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800547 Mutex::Autolock lock(mLock);
548 bool disconnected = (mHardware == nullptr);
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700549 size_t n = handles.size();
550 std::vector<sp<IMemory>> frames;
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800551 if (!disconnected) {
552 frames.reserve(n);
553 }
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700554 bool error = false;
555 for (auto& handle : handles) {
556 sp<IMemory> dataPtr;
557 {
558 Mutex::Autolock l(mAvailableCallbackBuffersLock);
559 if (!mAvailableCallbackBuffers.empty()) {
560 dataPtr = mAvailableCallbackBuffers.back();
561 mAvailableCallbackBuffers.pop_back();
562 }
563 }
564
565 if (dataPtr == nullptr) {
566 ALOGE("%s: %d: No callback buffer available. Dropping frames.", __FUNCTION__,
567 __LINE__);
568 error = true;
569 break;
570 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
571 ALOGE("%s: %d: Callback buffer must be VideoNativeHandleMetadata", __FUNCTION__,
572 __LINE__);
573 error = true;
574 break;
575 }
576
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800577 if (!disconnected) {
578 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
579 metadata->eType = kMetadataBufferTypeNativeHandleSource;
580 metadata->pHandle = handle;
581 frames.push_back(dataPtr);
582 }
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700583 }
584
585 if (error) {
586 for (auto& handle : handles) {
587 native_handle_close(handle);
588 native_handle_delete(handle);
589 }
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800590 } else if (!disconnected) {
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700591 mHardware->releaseRecordingFrameBatch(frames);
592 }
593 return;
594}
595
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800596status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
597 LOG1("setVideoBufferMode: %d", videoBufferMode);
598 bool enableMetadataInBuffers = false;
599
600 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
601 enableMetadataInBuffers = true;
602 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
603 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
604 videoBufferMode);
605 return BAD_VALUE;
606 }
607
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700608 Mutex::Autolock lock(mLock);
609 if (checkPidAndHardware() != NO_ERROR) {
610 return UNKNOWN_ERROR;
611 }
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800612
613 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700614}
615
616bool CameraClient::previewEnabled() {
617 LOG1("previewEnabled (pid %d)", getCallingPid());
618
619 Mutex::Autolock lock(mLock);
620 if (checkPidAndHardware() != NO_ERROR) return false;
621 return mHardware->previewEnabled();
622}
623
624bool CameraClient::recordingEnabled() {
625 LOG1("recordingEnabled (pid %d)", getCallingPid());
626
627 Mutex::Autolock lock(mLock);
628 if (checkPidAndHardware() != NO_ERROR) return false;
629 return mHardware->recordingEnabled();
630}
631
632status_t CameraClient::autoFocus() {
633 LOG1("autoFocus (pid %d)", getCallingPid());
634
635 Mutex::Autolock lock(mLock);
636 status_t result = checkPidAndHardware();
637 if (result != NO_ERROR) return result;
638
639 return mHardware->autoFocus();
640}
641
642status_t CameraClient::cancelAutoFocus() {
643 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
644
645 Mutex::Autolock lock(mLock);
646 status_t result = checkPidAndHardware();
647 if (result != NO_ERROR) return result;
648
649 return mHardware->cancelAutoFocus();
650}
651
652// take a picture - image is returned in callback
653status_t CameraClient::takePicture(int msgType) {
654 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
655
656 Mutex::Autolock lock(mLock);
657 status_t result = checkPidAndHardware();
658 if (result != NO_ERROR) return result;
659
660 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
661 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
662 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
663 " cannot be both enabled");
664 return BAD_VALUE;
665 }
666
667 // We only accept picture related message types
668 // and ignore other types of messages for takePicture().
669 int picMsgType = msgType
670 & (CAMERA_MSG_SHUTTER |
671 CAMERA_MSG_POSTVIEW_FRAME |
672 CAMERA_MSG_RAW_IMAGE |
673 CAMERA_MSG_RAW_IMAGE_NOTIFY |
674 CAMERA_MSG_COMPRESSED_IMAGE);
675
676 enableMsgType(picMsgType);
677
678 return mHardware->takePicture();
679}
680
681// set preview/capture parameters - key/value pairs
682status_t CameraClient::setParameters(const String8& params) {
683 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
684
685 Mutex::Autolock lock(mLock);
686 status_t result = checkPidAndHardware();
687 if (result != NO_ERROR) return result;
688
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700689 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700690 CameraParameters p(params);
691 return mHardware->setParameters(p);
692}
693
694// get preview/capture parameters - key/value pairs
695String8 CameraClient::getParameters() const {
696 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700697 // The camera service can unconditionally get the parameters at all times
698 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700699
700 String8 params(mHardware->getParameters().flatten());
701 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
702 return params;
703}
704
705// enable shutter sound
706status_t CameraClient::enableShutterSound(bool enable) {
707 LOG1("enableShutterSound (pid %d)", getCallingPid());
708
709 status_t result = checkPidAndHardware();
710 if (result != NO_ERROR) return result;
711
712 if (enable) {
713 mPlayShutterSound = true;
714 return OK;
715 }
716
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700717 mPlayShutterSound = false;
718 return OK;
719}
720
721status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
722 LOG1("sendCommand (pid %d)", getCallingPid());
723 int orientation;
724 Mutex::Autolock lock(mLock);
725 status_t result = checkPidAndHardware();
726 if (result != NO_ERROR) return result;
727
728 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
729 // Mirror the preview if the camera is front-facing.
730 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
731 if (orientation == -1) return BAD_VALUE;
732
733 if (mOrientation != orientation) {
734 mOrientation = orientation;
735 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800736 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700737 }
738 }
739 return OK;
740 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
741 switch (arg1) {
742 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700743 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700744 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700745 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700746 default:
747 return BAD_VALUE;
748 }
749 return OK;
750 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800751 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
James Dong983cf232012-08-01 16:39:55 -0700752 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
753 // Silently ignore this command
754 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700755 } else if (cmd == CAMERA_CMD_PING) {
756 // If mHardware is 0, checkPidAndHardware will return error.
757 return OK;
758 }
759
760 return mHardware->sendCommand(cmd, arg1, arg2);
761}
762
763// ----------------------------------------------------------------------------
764
765void CameraClient::enableMsgType(int32_t msgType) {
766 android_atomic_or(msgType, &mMsgEnabled);
767 mHardware->enableMsgType(msgType);
768}
769
770void CameraClient::disableMsgType(int32_t msgType) {
771 android_atomic_and(~msgType, &mMsgEnabled);
772 mHardware->disableMsgType(msgType);
773}
774
775#define CHECK_MESSAGE_INTERVAL 10 // 10ms
776bool CameraClient::lockIfMessageWanted(int32_t msgType) {
777 int sleepCount = 0;
778 while (mMsgEnabled & msgType) {
779 if (mLock.tryLock() == NO_ERROR) {
780 if (sleepCount > 0) {
781 LOG1("lockIfMessageWanted(%d): waited for %d ms",
782 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
783 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800784
785 // If messages are no longer enabled after acquiring lock, release and drop message
786 if ((mMsgEnabled & msgType) == 0) {
787 mLock.unlock();
788 break;
789 }
790
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700791 return true;
792 }
793 if (sleepCount++ == 0) {
794 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
795 }
796 usleep(CHECK_MESSAGE_INTERVAL * 1000);
797 }
798 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
799 return false;
800}
801
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800802sp<CameraClient> CameraClient::getClientFromCookie(void* user) {
803 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
804 auto clientDescriptor = sCameraService->mActiveClientManager.get(cameraId);
805 if (clientDescriptor != nullptr) {
806 return sp<CameraClient>{
807 static_cast<CameraClient*>(clientDescriptor->getValue().get())};
808 }
809 return sp<CameraClient>{nullptr};
810}
811
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700812// Callback messages can be dispatched to internal handlers or pass to our
813// client's callback functions, depending on the message type.
814//
815// notifyCallback:
816// CAMERA_MSG_SHUTTER handleShutter
817// (others) c->notifyCallback
818// dataCallback:
819// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
820// CAMERA_MSG_POSTVIEW_FRAME handlePostview
821// CAMERA_MSG_RAW_IMAGE handleRawPicture
822// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
823// (others) c->dataCallback
824// dataCallbackTimestamp
825// (others) c->dataCallbackTimestamp
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700826
827void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
828 int32_t ext2, void* user) {
829 LOG2("notifyCallback(%d)", msgType);
830
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800831 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800832 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700833
834 if (!client->lockIfMessageWanted(msgType)) return;
835
836 switch (msgType) {
837 case CAMERA_MSG_SHUTTER:
838 // ext1 is the dimension of the yuv picture.
839 client->handleShutter();
840 break;
841 default:
842 client->handleGenericNotify(msgType, ext1, ext2);
843 break;
844 }
845}
846
847void CameraClient::dataCallback(int32_t msgType,
848 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
849 LOG2("dataCallback(%d)", msgType);
850
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800851 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800852 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700853
854 if (!client->lockIfMessageWanted(msgType)) return;
855 if (dataPtr == 0 && metadata == NULL) {
856 ALOGE("Null data returned in data callback");
857 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
858 return;
859 }
860
861 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
862 case CAMERA_MSG_PREVIEW_FRAME:
863 client->handlePreviewData(msgType, dataPtr, metadata);
864 break;
865 case CAMERA_MSG_POSTVIEW_FRAME:
866 client->handlePostview(dataPtr);
867 break;
868 case CAMERA_MSG_RAW_IMAGE:
869 client->handleRawPicture(dataPtr);
870 break;
871 case CAMERA_MSG_COMPRESSED_IMAGE:
872 client->handleCompressedPicture(dataPtr);
873 break;
874 default:
875 client->handleGenericData(msgType, dataPtr, metadata);
876 break;
877 }
878}
879
880void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
881 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
882 LOG2("dataCallbackTimestamp(%d)", msgType);
883
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800884 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800885 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700886
887 if (!client->lockIfMessageWanted(msgType)) return;
888
889 if (dataPtr == 0) {
890 ALOGE("Null data returned in data with timestamp callback");
891 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
892 return;
893 }
894
895 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
896}
897
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700898void CameraClient::handleCallbackTimestampBatch(
899 int32_t msgType, const std::vector<HandleTimestampMessage>& msgs, void* user) {
900 LOG2("dataCallbackTimestampBatch");
901 sp<CameraClient> client = getClientFromCookie(user);
902 if (client.get() == nullptr) return;
903 if (!client->lockIfMessageWanted(msgType)) return;
904
905 sp<hardware::ICameraClient> c = client->mRemoteCallback;
906 client->mLock.unlock();
907 if (c != 0 && msgs.size() > 0) {
908 size_t n = msgs.size();
909 std::vector<nsecs_t> timestamps;
910 std::vector<native_handle_t*> handles;
911 timestamps.reserve(n);
912 handles.reserve(n);
913 for (auto& msg : msgs) {
914 native_handle_t* handle = nullptr;
915 if (msg.dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
916 ALOGE("%s: dataPtr does not contain VideoNativeHandleMetadata!", __FUNCTION__);
917 return;
918 }
919 VideoNativeHandleMetadata *metadata =
920 (VideoNativeHandleMetadata*)(msg.dataPtr->pointer());
921 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
922 handle = metadata->pHandle;
923 }
924
925 if (handle == nullptr) {
926 ALOGE("%s: VideoNativeHandleMetadata type mismatch or null handle passed!",
927 __FUNCTION__);
928 return;
929 }
930 {
931 Mutex::Autolock l(client->mAvailableCallbackBuffersLock);
932 client->mAvailableCallbackBuffers.push_back(msg.dataPtr);
933 }
934 timestamps.push_back(msg.timestamp);
935 handles.push_back(handle);
936 }
937 c->recordingFrameHandleCallbackTimestampBatch(timestamps, handles);
938 }
939}
940
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700941// snapshot taken callback
942void CameraClient::handleShutter(void) {
943 if (mPlayShutterSound) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800944 sCameraService->playSound(CameraService::SOUND_SHUTTER);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700945 }
946
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800947 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700948 if (c != 0) {
949 mLock.unlock();
950 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
951 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
952 }
953 disableMsgType(CAMERA_MSG_SHUTTER);
954
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700955 // Shutters only happen in response to takePicture, so mark device as
956 // idle now, until preview is restarted
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800957 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700958 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
Emilian Peev573291c2018-02-10 02:10:56 +0000959 mCameraIdStr, mCameraFacing, mClientPackageName,
960 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700961
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700962 mLock.unlock();
963}
964
965// preview callback - frame buffer update
966void CameraClient::handlePreviewData(int32_t msgType,
967 const sp<IMemory>& mem,
968 camera_frame_metadata_t *metadata) {
969 ssize_t offset;
970 size_t size;
971 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
972
973 // local copy of the callback flags
974 int flags = mPreviewCallbackFlag;
975
976 // is callback enabled?
977 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
978 // If the enable bit is off, the copy-out and one-shot bits are ignored
979 LOG2("frame callback is disabled");
980 mLock.unlock();
981 return;
982 }
983
984 // hold a strong pointer to the client
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800985 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700986
987 // clear callback flags if no client or one-shot mode
988 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
989 LOG2("Disable preview callback");
990 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
991 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
992 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
993 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
994 }
995
996 if (c != 0) {
997 // Is the received frame copied out or not?
998 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
999 LOG2("frame is copied");
1000 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
1001 } else {
1002 LOG2("frame is forwarded");
1003 mLock.unlock();
1004 c->dataCallback(msgType, mem, metadata);
1005 }
1006 } else {
1007 mLock.unlock();
1008 }
1009}
1010
1011// picture callback - postview image ready
1012void CameraClient::handlePostview(const sp<IMemory>& mem) {
1013 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1014
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001015 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001016 mLock.unlock();
1017 if (c != 0) {
1018 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
1019 }
1020}
1021
1022// picture callback - raw image ready
1023void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
1024 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1025
1026 ssize_t offset;
1027 size_t size;
1028 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1029
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001030 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001031 mLock.unlock();
1032 if (c != 0) {
1033 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
1034 }
1035}
1036
1037// picture callback - compressed picture ready
1038void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
1039 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001041 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001042 mLock.unlock();
1043 if (c != 0) {
1044 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
1045 }
1046}
1047
1048
1049void CameraClient::handleGenericNotify(int32_t msgType,
1050 int32_t ext1, int32_t ext2) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001051 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001052 mLock.unlock();
1053 if (c != 0) {
1054 c->notifyCallback(msgType, ext1, ext2);
1055 }
1056}
1057
1058void CameraClient::handleGenericData(int32_t msgType,
1059 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001060 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001061 mLock.unlock();
1062 if (c != 0) {
1063 c->dataCallback(msgType, dataPtr, metadata);
1064 }
1065}
1066
1067void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
1068 int32_t msgType, const sp<IMemory>& dataPtr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001069 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001070 mLock.unlock();
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -07001071 if (c != 0 && dataPtr != nullptr) {
1072 native_handle_t* handle = nullptr;
1073
1074 // Check if dataPtr contains a VideoNativeHandleMetadata.
1075 if (dataPtr->size() == sizeof(VideoNativeHandleMetadata)) {
1076 VideoNativeHandleMetadata *metadata =
1077 (VideoNativeHandleMetadata*)(dataPtr->pointer());
1078 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
1079 handle = metadata->pHandle;
1080 }
1081 }
1082
1083 // If dataPtr contains a native handle, send it via recordingFrameHandleCallbackTimestamp.
1084 if (handle != nullptr) {
1085 {
1086 Mutex::Autolock l(mAvailableCallbackBuffersLock);
1087 mAvailableCallbackBuffers.push_back(dataPtr);
1088 }
1089 c->recordingFrameHandleCallbackTimestamp(timestamp, handle);
1090 } else {
1091 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1092 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001093 }
1094}
1095
1096void CameraClient::copyFrameAndPostCopiedFrame(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001097 int32_t msgType, const sp<hardware::ICameraClient>& client,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001098 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1099 camera_frame_metadata_t *metadata) {
1100 LOG2("copyFrameAndPostCopiedFrame");
1101 // It is necessary to copy out of pmem before sending this to
1102 // the callback. For efficiency, reuse the same MemoryHeapBase
1103 // provided it's big enough. Don't allocate the memory or
1104 // perform the copy if there's no callback.
1105 // hold the preview lock while we grab a reference to the preview buffer
1106 sp<MemoryHeapBase> previewBuffer;
1107
1108 if (mPreviewBuffer == 0) {
1109 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1110 } else if (size > mPreviewBuffer->virtualSize()) {
1111 mPreviewBuffer.clear();
1112 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1113 }
1114 if (mPreviewBuffer == 0) {
1115 ALOGE("failed to allocate space for preview buffer");
1116 mLock.unlock();
1117 return;
1118 }
1119 previewBuffer = mPreviewBuffer;
1120
Ruben Brunk65e01f72014-08-29 17:25:13 -07001121 void* previewBufferBase = previewBuffer->base();
1122 void* heapBase = heap->base();
1123
1124 if (heapBase == MAP_FAILED) {
1125 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
1126 mLock.unlock();
1127 return;
1128 } else if (previewBufferBase == MAP_FAILED) {
1129 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
1130 mLock.unlock();
1131 return;
1132 }
1133
1134 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001135
1136 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1137 if (frame == 0) {
1138 ALOGE("failed to allocate space for frame callback");
1139 mLock.unlock();
1140 return;
1141 }
1142
1143 mLock.unlock();
1144 client->dataCallback(msgType, frame, metadata);
1145}
1146
1147int CameraClient::getOrientation(int degrees, bool mirror) {
1148 if (!mirror) {
1149 if (degrees == 0) return 0;
1150 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1151 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1152 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1153 } else { // Do mirror (horizontal flip)
1154 if (degrees == 0) { // FLIP_H and ROT_0
1155 return HAL_TRANSFORM_FLIP_H;
1156 } else if (degrees == 90) { // FLIP_H and ROT_90
1157 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1158 } else if (degrees == 180) { // FLIP_H and ROT_180
1159 return HAL_TRANSFORM_FLIP_V;
1160 } else if (degrees == 270) { // FLIP_H and ROT_270
1161 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1162 }
1163 }
1164 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1165 return -1;
1166}
1167
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001168status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001169 (void)bufferProducer;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001170 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1171 return INVALID_OPERATION;
1172}
1173
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001174}; // namespace android