blob: b83d4255badea58027728e188e656fe421b79b9b [file] [log] [blame]
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraClient"
18//#define LOG_NDEBUG 0
19
20#include <cutils/properties.h>
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070021#include <gui/Surface.h>
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -070022#include <media/hardware/HardwareAPI.h>
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070023
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070024#include "api1/CameraClient.h"
25#include "device1/CameraHardwareInterface.h"
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070026#include "CameraService.h"
27
28namespace android {
29
30#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
31#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
32
33static int getCallingPid() {
34 return IPCThreadState::self()->getCallingPid();
35}
36
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070037CameraClient::CameraClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080038 const sp<hardware::ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080039 const String16& clientPackageName,
40 int cameraId, int cameraFacing,
41 int clientPid, int clientUid,
Igor Murashkina858ea02014-08-19 14:53:08 -070042 int servicePid, bool legacyMode):
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080043 Client(cameraService, cameraClient, clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080044 String8::format("%d", cameraId), cameraFacing, clientPid,
45 clientUid, servicePid)
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070046{
47 int callingPid = getCallingPid();
48 LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
49
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070050 mHardware = NULL;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070051 mMsgEnabled = 0;
52 mSurface = 0;
53 mPreviewWindow = 0;
54 mDestructionStarted = false;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070055
56 // Callback is disabled by default
57 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
58 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Igor Murashkina858ea02014-08-19 14:53:08 -070059 mLegacyMode = legacyMode;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070060 mPlayShutterSound = true;
61 LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
62}
63
Yin-Chia Yehe074a932015-01-30 10:29:02 -080064status_t CameraClient::initialize(CameraModule *module) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080065 return initializeImpl<CameraModule*>(module);
66}
67
68status_t CameraClient::initialize(sp<CameraProviderManager> manager) {
69 return initializeImpl<sp<CameraProviderManager>>(manager);
70}
71
72template<typename TProviderPtr>
73status_t CameraClient::initializeImpl(TProviderPtr providerPtr) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070074 int callingPid = getCallingPid();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080075 status_t res;
76
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070077 LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
78
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080079 // Verify ops permissions
80 res = startCameraOps();
81 if (res != OK) {
82 return res;
83 }
84
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070085 char camera_device_name[10];
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070086 snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
87
88 mHardware = new CameraHardwareInterface(camera_device_name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080089 res = mHardware->initialize(providerPtr);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070090 if (res != OK) {
91 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
92 __FUNCTION__, mCameraId, strerror(-res), res);
Igor Murashkin44f120f2012-10-09 14:45:37 -070093 mHardware.clear();
Zhijun Heb10cdad2014-06-16 16:38:35 -070094 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070095 }
96
97 mHardware->setCallbacks(notifyCallback,
98 dataCallback,
99 dataCallbackTimestamp,
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000100 (void *)(uintptr_t)mCameraId);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700101
102 // Enable zoom, error, focus, and metadata messages by default
103 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
104 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
105
106 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
107 return OK;
108}
109
110
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700111// tear down the client
112CameraClient::~CameraClient() {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700113 mDestructionStarted = true;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700114 int callingPid = getCallingPid();
115 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
116
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700117 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700118 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
119}
120
121status_t CameraClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800122 return BasicClient::dump(fd, args);
123}
124
125status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700126 const size_t SIZE = 256;
127 char buffer[SIZE];
128
Ruben Brunkcc776712015-02-17 20:18:47 -0800129 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n",
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700130 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800131 (getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800132 IInterface::asBinder(getRemoteCallback()).get() : NULL),
Ruben Brunkcc776712015-02-17 20:18:47 -0800133 mClientUid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700134 len = (len > SIZE - 1) ? SIZE - 1 : len;
135 write(fd, buffer, len);
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700136
137 len = snprintf(buffer, SIZE, "Latest set parameters:\n");
138 len = (len > SIZE - 1) ? SIZE - 1 : len;
139 write(fd, buffer, len);
140
141 mLatestSetParameters.dump(fd, args);
142
143 const char *enddump = "\n\n";
144 write(fd, enddump, strlen(enddump));
145
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700146 return mHardware->dump(fd, args);
147}
148
149// ----------------------------------------------------------------------------
150
151status_t CameraClient::checkPid() const {
152 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700153 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700154
155 ALOGW("attempt to use a locked camera from a different process"
156 " (old pid %d, new pid %d)", mClientPid, callingPid);
157 return EBUSY;
158}
159
160status_t CameraClient::checkPidAndHardware() const {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700161 if (mHardware == 0) {
162 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
163 return INVALID_OPERATION;
164 }
Eino-Ville Talvala6192b892016-04-04 12:31:18 -0700165 status_t result = checkPid();
166 if (result != NO_ERROR) return result;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700167 return NO_ERROR;
168}
169
170status_t CameraClient::lock() {
171 int callingPid = getCallingPid();
172 LOG1("lock (pid %d)", callingPid);
173 Mutex::Autolock lock(mLock);
174
175 // lock camera to this client if the the camera is unlocked
176 if (mClientPid == 0) {
177 mClientPid = callingPid;
178 return NO_ERROR;
179 }
180
181 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
182 return checkPid();
183}
184
185status_t CameraClient::unlock() {
186 int callingPid = getCallingPid();
187 LOG1("unlock (pid %d)", callingPid);
188 Mutex::Autolock lock(mLock);
189
190 // allow anyone to use camera (after they lock the camera)
191 status_t result = checkPid();
192 if (result == NO_ERROR) {
193 if (mHardware->recordingEnabled()) {
194 ALOGE("Not allowed to unlock camera during recording.");
195 return INVALID_OPERATION;
196 }
197 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800198 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700199 // we need to remove the reference to ICameraClient so that when the app
200 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800201 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700202 }
203 return result;
204}
205
206// connect a new client to the camera
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800207status_t CameraClient::connect(const sp<hardware::ICameraClient>& client) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700208 int callingPid = getCallingPid();
209 LOG1("connect E (pid %d)", callingPid);
210 Mutex::Autolock lock(mLock);
211
212 if (mClientPid != 0 && checkPid() != NO_ERROR) {
213 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
214 mClientPid, callingPid);
215 return EBUSY;
216 }
217
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800218 if (mRemoteCallback != 0 &&
Marco Nelissen06b46062014-11-14 07:58:25 -0800219 (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700220 LOG1("Connect to the same client");
221 return NO_ERROR;
222 }
223
224 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
225 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800226 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700227
228 LOG1("connect X (pid %d)", callingPid);
229 return NO_ERROR;
230}
231
232static void disconnectWindow(const sp<ANativeWindow>& window) {
233 if (window != 0) {
234 status_t result = native_window_api_disconnect(window.get(),
235 NATIVE_WINDOW_API_CAMERA);
236 if (result != NO_ERROR) {
237 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
238 result);
239 }
240 }
241}
242
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800243binder::Status CameraClient::disconnect() {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700244 int callingPid = getCallingPid();
245 LOG1("disconnect E (pid %d)", callingPid);
246 Mutex::Autolock lock(mLock);
247
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800248 binder::Status res = binder::Status::ok();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800249 // Allow both client and the cameraserver to disconnect at all times
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700250 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700251 ALOGW("different client - don't disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800252 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700253 }
254
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700255 // Make sure disconnect() is done once and once only, whether it is called
256 // from the user directly, or called by the destructor.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800257 if (mHardware == 0) return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700258
259 LOG1("hardware teardown");
260 // Before destroying mHardware, we must make sure it's in the
261 // idle state.
262 // Turn off all messages.
263 disableMsgType(CAMERA_MSG_ALL_MSGS);
264 mHardware->stopPreview();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800265 sCameraService->updateProxyDeviceState(
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700266 ICameraServiceProxy::CAMERA_STATE_IDLE,
267 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700268 mHardware->cancelPicture();
269 // Release the hardware resources.
270 mHardware->release();
271
272 // Release the held ANativeWindow resources.
273 if (mPreviewWindow != 0) {
274 disconnectWindow(mPreviewWindow);
275 mPreviewWindow = 0;
276 mHardware->setPreviewWindow(mPreviewWindow);
277 }
278 mHardware.clear();
279
280 CameraService::Client::disconnect();
281
282 LOG1("disconnect X (pid %d)", callingPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283
284 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700285}
286
287// ----------------------------------------------------------------------------
288
289status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
290 const sp<ANativeWindow>& window) {
291 Mutex::Autolock lock(mLock);
292 status_t result = checkPidAndHardware();
293 if (result != NO_ERROR) return result;
294
295 // return if no change in surface.
296 if (binder == mSurface) {
297 return NO_ERROR;
298 }
299
300 if (window != 0) {
301 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
302 if (result != NO_ERROR) {
303 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
304 result);
305 return result;
306 }
307 }
308
309 // If preview has been already started, register preview buffers now.
310 if (mHardware->previewEnabled()) {
311 if (window != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800312 mHardware->setPreviewScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
313 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700314 result = mHardware->setPreviewWindow(window);
315 }
316 }
317
318 if (result == NO_ERROR) {
319 // Everything has succeeded. Disconnect the old window and remember the
320 // new window.
321 disconnectWindow(mPreviewWindow);
322 mSurface = binder;
323 mPreviewWindow = window;
324 } else {
325 // Something went wrong after we connected to the new window, so
326 // disconnect here.
327 disconnectWindow(window);
328 }
329
330 return result;
331}
332
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700333// set the buffer consumer that the preview will use
334status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800335 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700336 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700337 getCallingPid());
338
339 sp<IBinder> binder;
340 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800341 if (bufferProducer != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800342 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700343 // Using controlledByApp flag to ensure that the buffer queue remains in
344 // async mode for the old camera API, where many applications depend
345 // on that behavior.
346 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700347 }
348 return setPreviewWindow(binder, window);
349}
350
351// set the preview callback flag to affect how the received frames from
352// preview are handled.
353void CameraClient::setPreviewCallbackFlag(int callback_flag) {
354 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
355 Mutex::Autolock lock(mLock);
356 if (checkPidAndHardware() != NO_ERROR) return;
357
358 mPreviewCallbackFlag = callback_flag;
359 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
360 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
361 } else {
362 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
363 }
364}
365
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700366status_t CameraClient::setPreviewCallbackTarget(
367 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700368 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700369 ALOGE("%s: Unimplemented!", __FUNCTION__);
370 return INVALID_OPERATION;
371}
372
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700373// start preview mode
374status_t CameraClient::startPreview() {
375 LOG1("startPreview (pid %d)", getCallingPid());
376 return startCameraMode(CAMERA_PREVIEW_MODE);
377}
378
379// start recording mode
380status_t CameraClient::startRecording() {
381 LOG1("startRecording (pid %d)", getCallingPid());
382 return startCameraMode(CAMERA_RECORDING_MODE);
383}
384
385// start preview or recording
386status_t CameraClient::startCameraMode(camera_mode mode) {
387 LOG1("startCameraMode(%d)", mode);
388 Mutex::Autolock lock(mLock);
389 status_t result = checkPidAndHardware();
390 if (result != NO_ERROR) return result;
391
392 switch(mode) {
393 case CAMERA_PREVIEW_MODE:
394 if (mSurface == 0 && mPreviewWindow == 0) {
395 LOG1("mSurface is not set yet.");
396 // still able to start preview in this case.
397 }
398 return startPreviewMode();
399 case CAMERA_RECORDING_MODE:
400 if (mSurface == 0 && mPreviewWindow == 0) {
401 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
402 return INVALID_OPERATION;
403 }
404 return startRecordingMode();
405 default:
406 return UNKNOWN_ERROR;
407 }
408}
409
410status_t CameraClient::startPreviewMode() {
411 LOG1("startPreviewMode");
412 status_t result = NO_ERROR;
413
414 // if preview has been enabled, nothing needs to be done
415 if (mHardware->previewEnabled()) {
416 return NO_ERROR;
417 }
418
419 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800420 mHardware->setPreviewScalingMode(
421 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
422 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700423 }
424 mHardware->setPreviewWindow(mPreviewWindow);
425 result = mHardware->startPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700426 if (result == NO_ERROR) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800427 sCameraService->updateProxyDeviceState(
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700428 ICameraServiceProxy::CAMERA_STATE_ACTIVE,
429 String8::format("%d", mCameraId));
430 }
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 Talvala412fe562015-08-20 17:08:32 -0700471 ICameraServiceProxy::CAMERA_STATE_IDLE,
472 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700473 mPreviewBuffer.clear();
474}
475
476// stop recording mode
477void CameraClient::stopRecording() {
478 LOG1("stopRecording (pid %d)", getCallingPid());
479 Mutex::Autolock lock(mLock);
480 if (checkPidAndHardware() != NO_ERROR) return;
481
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700482 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
483 mHardware->stopRecording();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800484 sCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700485
486 mPreviewBuffer.clear();
487}
488
489// release a recording frame
490void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
491 Mutex::Autolock lock(mLock);
492 if (checkPidAndHardware() != NO_ERROR) return;
Chien-Yu Chen42f27072016-01-11 17:39:17 -0800493 if (mem == nullptr) {
494 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26164272",
495 IPCThreadState::self()->getCallingUid(), nullptr, 0);
496 return;
497 }
498
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700499 mHardware->releaseRecordingFrame(mem);
500}
501
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700502void CameraClient::releaseRecordingFrameHandle(native_handle_t *handle) {
503 if (handle == nullptr) return;
504
505 sp<IMemory> dataPtr;
506 {
507 Mutex::Autolock l(mAvailableCallbackBuffersLock);
508 if (!mAvailableCallbackBuffers.empty()) {
509 dataPtr = mAvailableCallbackBuffers.back();
510 mAvailableCallbackBuffers.pop_back();
511 }
512 }
513
514 if (dataPtr == nullptr) {
515 ALOGE("%s: %d: No callback buffer available. Dropping a native handle.", __FUNCTION__,
516 __LINE__);
517 native_handle_close(handle);
518 native_handle_delete(handle);
519 return;
520 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
521 ALOGE("%s: %d: Callback buffer size doesn't match VideoNativeHandleMetadata", __FUNCTION__,
522 __LINE__);
523 native_handle_close(handle);
524 native_handle_delete(handle);
525 return;
526 }
527
528 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
529 metadata->eType = kMetadataBufferTypeNativeHandleSource;
530 metadata->pHandle = handle;
531
532 mHardware->releaseRecordingFrame(dataPtr);
533}
534
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800535status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
536 LOG1("setVideoBufferMode: %d", videoBufferMode);
537 bool enableMetadataInBuffers = false;
538
539 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
540 enableMetadataInBuffers = true;
541 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
542 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
543 videoBufferMode);
544 return BAD_VALUE;
545 }
546
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700547 Mutex::Autolock lock(mLock);
548 if (checkPidAndHardware() != NO_ERROR) {
549 return UNKNOWN_ERROR;
550 }
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800551
552 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700553}
554
555bool CameraClient::previewEnabled() {
556 LOG1("previewEnabled (pid %d)", getCallingPid());
557
558 Mutex::Autolock lock(mLock);
559 if (checkPidAndHardware() != NO_ERROR) return false;
560 return mHardware->previewEnabled();
561}
562
563bool CameraClient::recordingEnabled() {
564 LOG1("recordingEnabled (pid %d)", getCallingPid());
565
566 Mutex::Autolock lock(mLock);
567 if (checkPidAndHardware() != NO_ERROR) return false;
568 return mHardware->recordingEnabled();
569}
570
571status_t CameraClient::autoFocus() {
572 LOG1("autoFocus (pid %d)", getCallingPid());
573
574 Mutex::Autolock lock(mLock);
575 status_t result = checkPidAndHardware();
576 if (result != NO_ERROR) return result;
577
578 return mHardware->autoFocus();
579}
580
581status_t CameraClient::cancelAutoFocus() {
582 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
583
584 Mutex::Autolock lock(mLock);
585 status_t result = checkPidAndHardware();
586 if (result != NO_ERROR) return result;
587
588 return mHardware->cancelAutoFocus();
589}
590
591// take a picture - image is returned in callback
592status_t CameraClient::takePicture(int msgType) {
593 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
594
595 Mutex::Autolock lock(mLock);
596 status_t result = checkPidAndHardware();
597 if (result != NO_ERROR) return result;
598
599 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
600 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
601 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
602 " cannot be both enabled");
603 return BAD_VALUE;
604 }
605
606 // We only accept picture related message types
607 // and ignore other types of messages for takePicture().
608 int picMsgType = msgType
609 & (CAMERA_MSG_SHUTTER |
610 CAMERA_MSG_POSTVIEW_FRAME |
611 CAMERA_MSG_RAW_IMAGE |
612 CAMERA_MSG_RAW_IMAGE_NOTIFY |
613 CAMERA_MSG_COMPRESSED_IMAGE);
614
615 enableMsgType(picMsgType);
616
617 return mHardware->takePicture();
618}
619
620// set preview/capture parameters - key/value pairs
621status_t CameraClient::setParameters(const String8& params) {
622 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
623
624 Mutex::Autolock lock(mLock);
625 status_t result = checkPidAndHardware();
626 if (result != NO_ERROR) return result;
627
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700628 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700629 CameraParameters p(params);
630 return mHardware->setParameters(p);
631}
632
633// get preview/capture parameters - key/value pairs
634String8 CameraClient::getParameters() const {
635 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700636 // The camera service can unconditionally get the parameters at all times
637 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700638
639 String8 params(mHardware->getParameters().flatten());
640 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
641 return params;
642}
643
644// enable shutter sound
645status_t CameraClient::enableShutterSound(bool enable) {
646 LOG1("enableShutterSound (pid %d)", getCallingPid());
647
648 status_t result = checkPidAndHardware();
649 if (result != NO_ERROR) return result;
650
651 if (enable) {
652 mPlayShutterSound = true;
653 return OK;
654 }
655
Igor Murashkina858ea02014-08-19 14:53:08 -0700656 // the camera2 api legacy mode can unconditionally disable the shutter sound
657 if (mLegacyMode) {
658 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
659 mPlayShutterSound = false;
660 return OK;
661 }
662
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700663 // Disabling shutter sound may not be allowed. In that case only
664 // allow the mediaserver process to disable the sound.
665 char value[PROPERTY_VALUE_MAX];
666 property_get("ro.camera.sound.forced", value, "0");
667 if (strcmp(value, "0") != 0) {
668 // Disabling shutter sound is not allowed. Deny if the current
669 // process is not mediaserver.
670 if (getCallingPid() != getpid()) {
671 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
672 return PERMISSION_DENIED;
673 }
674 }
675
676 mPlayShutterSound = false;
677 return OK;
678}
679
680status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
681 LOG1("sendCommand (pid %d)", getCallingPid());
682 int orientation;
683 Mutex::Autolock lock(mLock);
684 status_t result = checkPidAndHardware();
685 if (result != NO_ERROR) return result;
686
687 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
688 // Mirror the preview if the camera is front-facing.
689 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
690 if (orientation == -1) return BAD_VALUE;
691
692 if (mOrientation != orientation) {
693 mOrientation = orientation;
694 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800695 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700696 }
697 }
698 return OK;
699 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
700 switch (arg1) {
701 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700702 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700703 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700704 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700705 default:
706 return BAD_VALUE;
707 }
708 return OK;
709 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800710 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
James Dong983cf232012-08-01 16:39:55 -0700711 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
712 // Silently ignore this command
713 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700714 } else if (cmd == CAMERA_CMD_PING) {
715 // If mHardware is 0, checkPidAndHardware will return error.
716 return OK;
717 }
718
719 return mHardware->sendCommand(cmd, arg1, arg2);
720}
721
722// ----------------------------------------------------------------------------
723
724void CameraClient::enableMsgType(int32_t msgType) {
725 android_atomic_or(msgType, &mMsgEnabled);
726 mHardware->enableMsgType(msgType);
727}
728
729void CameraClient::disableMsgType(int32_t msgType) {
730 android_atomic_and(~msgType, &mMsgEnabled);
731 mHardware->disableMsgType(msgType);
732}
733
734#define CHECK_MESSAGE_INTERVAL 10 // 10ms
735bool CameraClient::lockIfMessageWanted(int32_t msgType) {
736 int sleepCount = 0;
737 while (mMsgEnabled & msgType) {
738 if (mLock.tryLock() == NO_ERROR) {
739 if (sleepCount > 0) {
740 LOG1("lockIfMessageWanted(%d): waited for %d ms",
741 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
742 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800743
744 // If messages are no longer enabled after acquiring lock, release and drop message
745 if ((mMsgEnabled & msgType) == 0) {
746 mLock.unlock();
747 break;
748 }
749
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700750 return true;
751 }
752 if (sleepCount++ == 0) {
753 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
754 }
755 usleep(CHECK_MESSAGE_INTERVAL * 1000);
756 }
757 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
758 return false;
759}
760
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800761sp<CameraClient> CameraClient::getClientFromCookie(void* user) {
762 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
763 auto clientDescriptor = sCameraService->mActiveClientManager.get(cameraId);
764 if (clientDescriptor != nullptr) {
765 return sp<CameraClient>{
766 static_cast<CameraClient*>(clientDescriptor->getValue().get())};
767 }
768 return sp<CameraClient>{nullptr};
769}
770
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700771// Callback messages can be dispatched to internal handlers or pass to our
772// client's callback functions, depending on the message type.
773//
774// notifyCallback:
775// CAMERA_MSG_SHUTTER handleShutter
776// (others) c->notifyCallback
777// dataCallback:
778// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
779// CAMERA_MSG_POSTVIEW_FRAME handlePostview
780// CAMERA_MSG_RAW_IMAGE handleRawPicture
781// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
782// (others) c->dataCallback
783// dataCallbackTimestamp
784// (others) c->dataCallbackTimestamp
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700785
786void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
787 int32_t ext2, void* user) {
788 LOG2("notifyCallback(%d)", msgType);
789
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800790 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800791 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700792
793 if (!client->lockIfMessageWanted(msgType)) return;
794
795 switch (msgType) {
796 case CAMERA_MSG_SHUTTER:
797 // ext1 is the dimension of the yuv picture.
798 client->handleShutter();
799 break;
800 default:
801 client->handleGenericNotify(msgType, ext1, ext2);
802 break;
803 }
804}
805
806void CameraClient::dataCallback(int32_t msgType,
807 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
808 LOG2("dataCallback(%d)", msgType);
809
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800810 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800811 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700812
813 if (!client->lockIfMessageWanted(msgType)) return;
814 if (dataPtr == 0 && metadata == NULL) {
815 ALOGE("Null data returned in data callback");
816 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
817 return;
818 }
819
820 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
821 case CAMERA_MSG_PREVIEW_FRAME:
822 client->handlePreviewData(msgType, dataPtr, metadata);
823 break;
824 case CAMERA_MSG_POSTVIEW_FRAME:
825 client->handlePostview(dataPtr);
826 break;
827 case CAMERA_MSG_RAW_IMAGE:
828 client->handleRawPicture(dataPtr);
829 break;
830 case CAMERA_MSG_COMPRESSED_IMAGE:
831 client->handleCompressedPicture(dataPtr);
832 break;
833 default:
834 client->handleGenericData(msgType, dataPtr, metadata);
835 break;
836 }
837}
838
839void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
840 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
841 LOG2("dataCallbackTimestamp(%d)", msgType);
842
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800843 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800844 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700845
846 if (!client->lockIfMessageWanted(msgType)) return;
847
848 if (dataPtr == 0) {
849 ALOGE("Null data returned in data with timestamp callback");
850 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
851 return;
852 }
853
854 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
855}
856
857// snapshot taken callback
858void CameraClient::handleShutter(void) {
859 if (mPlayShutterSound) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800860 sCameraService->playSound(CameraService::SOUND_SHUTTER);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700861 }
862
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800863 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700864 if (c != 0) {
865 mLock.unlock();
866 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
867 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
868 }
869 disableMsgType(CAMERA_MSG_SHUTTER);
870
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700871 // Shutters only happen in response to takePicture, so mark device as
872 // idle now, until preview is restarted
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800873 sCameraService->updateProxyDeviceState(
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700874 ICameraServiceProxy::CAMERA_STATE_IDLE,
875 String8::format("%d", mCameraId));
876
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700877 mLock.unlock();
878}
879
880// preview callback - frame buffer update
881void CameraClient::handlePreviewData(int32_t msgType,
882 const sp<IMemory>& mem,
883 camera_frame_metadata_t *metadata) {
884 ssize_t offset;
885 size_t size;
886 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
887
888 // local copy of the callback flags
889 int flags = mPreviewCallbackFlag;
890
891 // is callback enabled?
892 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
893 // If the enable bit is off, the copy-out and one-shot bits are ignored
894 LOG2("frame callback is disabled");
895 mLock.unlock();
896 return;
897 }
898
899 // hold a strong pointer to the client
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800900 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700901
902 // clear callback flags if no client or one-shot mode
903 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
904 LOG2("Disable preview callback");
905 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
906 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
907 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
908 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
909 }
910
911 if (c != 0) {
912 // Is the received frame copied out or not?
913 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
914 LOG2("frame is copied");
915 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
916 } else {
917 LOG2("frame is forwarded");
918 mLock.unlock();
919 c->dataCallback(msgType, mem, metadata);
920 }
921 } else {
922 mLock.unlock();
923 }
924}
925
926// picture callback - postview image ready
927void CameraClient::handlePostview(const sp<IMemory>& mem) {
928 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
929
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800930 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700931 mLock.unlock();
932 if (c != 0) {
933 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
934 }
935}
936
937// picture callback - raw image ready
938void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
939 disableMsgType(CAMERA_MSG_RAW_IMAGE);
940
941 ssize_t offset;
942 size_t size;
943 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
944
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800945 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700946 mLock.unlock();
947 if (c != 0) {
948 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
949 }
950}
951
952// picture callback - compressed picture ready
953void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
954 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
955
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800956 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700957 mLock.unlock();
958 if (c != 0) {
959 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
960 }
961}
962
963
964void CameraClient::handleGenericNotify(int32_t msgType,
965 int32_t ext1, int32_t ext2) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800966 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700967 mLock.unlock();
968 if (c != 0) {
969 c->notifyCallback(msgType, ext1, ext2);
970 }
971}
972
973void CameraClient::handleGenericData(int32_t msgType,
974 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800975 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700976 mLock.unlock();
977 if (c != 0) {
978 c->dataCallback(msgType, dataPtr, metadata);
979 }
980}
981
982void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
983 int32_t msgType, const sp<IMemory>& dataPtr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800984 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700985 mLock.unlock();
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700986 if (c != 0 && dataPtr != nullptr) {
987 native_handle_t* handle = nullptr;
988
989 // Check if dataPtr contains a VideoNativeHandleMetadata.
990 if (dataPtr->size() == sizeof(VideoNativeHandleMetadata)) {
991 VideoNativeHandleMetadata *metadata =
992 (VideoNativeHandleMetadata*)(dataPtr->pointer());
993 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
994 handle = metadata->pHandle;
995 }
996 }
997
998 // If dataPtr contains a native handle, send it via recordingFrameHandleCallbackTimestamp.
999 if (handle != nullptr) {
1000 {
1001 Mutex::Autolock l(mAvailableCallbackBuffersLock);
1002 mAvailableCallbackBuffers.push_back(dataPtr);
1003 }
1004 c->recordingFrameHandleCallbackTimestamp(timestamp, handle);
1005 } else {
1006 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1007 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001008 }
1009}
1010
1011void CameraClient::copyFrameAndPostCopiedFrame(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001012 int32_t msgType, const sp<hardware::ICameraClient>& client,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001013 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1014 camera_frame_metadata_t *metadata) {
1015 LOG2("copyFrameAndPostCopiedFrame");
1016 // It is necessary to copy out of pmem before sending this to
1017 // the callback. For efficiency, reuse the same MemoryHeapBase
1018 // provided it's big enough. Don't allocate the memory or
1019 // perform the copy if there's no callback.
1020 // hold the preview lock while we grab a reference to the preview buffer
1021 sp<MemoryHeapBase> previewBuffer;
1022
1023 if (mPreviewBuffer == 0) {
1024 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1025 } else if (size > mPreviewBuffer->virtualSize()) {
1026 mPreviewBuffer.clear();
1027 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1028 }
1029 if (mPreviewBuffer == 0) {
1030 ALOGE("failed to allocate space for preview buffer");
1031 mLock.unlock();
1032 return;
1033 }
1034 previewBuffer = mPreviewBuffer;
1035
Ruben Brunk65e01f72014-08-29 17:25:13 -07001036 void* previewBufferBase = previewBuffer->base();
1037 void* heapBase = heap->base();
1038
1039 if (heapBase == MAP_FAILED) {
1040 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
1041 mLock.unlock();
1042 return;
1043 } else if (previewBufferBase == MAP_FAILED) {
1044 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
1045 mLock.unlock();
1046 return;
1047 }
1048
1049 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001050
1051 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1052 if (frame == 0) {
1053 ALOGE("failed to allocate space for frame callback");
1054 mLock.unlock();
1055 return;
1056 }
1057
1058 mLock.unlock();
1059 client->dataCallback(msgType, frame, metadata);
1060}
1061
1062int CameraClient::getOrientation(int degrees, bool mirror) {
1063 if (!mirror) {
1064 if (degrees == 0) return 0;
1065 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1066 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1067 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1068 } else { // Do mirror (horizontal flip)
1069 if (degrees == 0) { // FLIP_H and ROT_0
1070 return HAL_TRANSFORM_FLIP_H;
1071 } else if (degrees == 90) { // FLIP_H and ROT_90
1072 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1073 } else if (degrees == 180) { // FLIP_H and ROT_180
1074 return HAL_TRANSFORM_FLIP_V;
1075 } else if (degrees == 270) { // FLIP_H and ROT_270
1076 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1077 }
1078 }
1079 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1080 return -1;
1081}
1082
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001083status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001084 (void)bufferProducer;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001085 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1086 return INVALID_OPERATION;
1087}
1088
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001089}; // namespace android