blob: ffb657e88ff4a665d66f899bb0c692bd646db90e [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
Yin-Chia Yehe074a932015-01-30 10:29:02 -080065status_t CameraClient::initialize(CameraModule *module) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080066 return initializeImpl<CameraModule*>(module);
67}
68
69status_t CameraClient::initialize(sp<CameraProviderManager> manager) {
70 return initializeImpl<sp<CameraProviderManager>>(manager);
71}
72
73template<typename TProviderPtr>
74status_t CameraClient::initializeImpl(TProviderPtr providerPtr) {
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070075 int callingPid = getCallingPid();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080076 status_t res;
77
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070078 LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
79
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080080 // Verify ops permissions
81 res = startCameraOps();
82 if (res != OK) {
83 return res;
84 }
85
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070086 char camera_device_name[10];
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070087 snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
88
89 mHardware = new CameraHardwareInterface(camera_device_name);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080090 res = mHardware->initialize(providerPtr);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070091 if (res != OK) {
92 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
93 __FUNCTION__, mCameraId, strerror(-res), res);
Igor Murashkin44f120f2012-10-09 14:45:37 -070094 mHardware.clear();
Zhijun Heb10cdad2014-06-16 16:38:35 -070095 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070096 }
97
98 mHardware->setCallbacks(notifyCallback,
99 dataCallback,
100 dataCallbackTimestamp,
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000101 (void *)(uintptr_t)mCameraId);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700102
103 // Enable zoom, error, focus, and metadata messages by default
104 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
105 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
106
107 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
108 return OK;
109}
110
111
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700112// tear down the client
113CameraClient::~CameraClient() {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700114 mDestructionStarted = true;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700115 int callingPid = getCallingPid();
116 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
117
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700118 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700119 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
120}
121
122status_t CameraClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800123 return BasicClient::dump(fd, args);
124}
125
126status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700127 const size_t SIZE = 256;
128 char buffer[SIZE];
129
Ruben Brunkcc776712015-02-17 20:18:47 -0800130 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n",
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700131 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800132 (getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800133 IInterface::asBinder(getRemoteCallback()).get() : NULL),
Ruben Brunkcc776712015-02-17 20:18:47 -0800134 mClientUid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700135 len = (len > SIZE - 1) ? SIZE - 1 : len;
136 write(fd, buffer, len);
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700137
138 len = snprintf(buffer, SIZE, "Latest set parameters:\n");
139 len = (len > SIZE - 1) ? SIZE - 1 : len;
140 write(fd, buffer, len);
141
142 mLatestSetParameters.dump(fd, args);
143
144 const char *enddump = "\n\n";
145 write(fd, enddump, strlen(enddump));
146
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700147 return mHardware->dump(fd, args);
148}
149
150// ----------------------------------------------------------------------------
151
152status_t CameraClient::checkPid() const {
153 int callingPid = getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700154 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700155
156 ALOGW("attempt to use a locked camera from a different process"
157 " (old pid %d, new pid %d)", mClientPid, callingPid);
158 return EBUSY;
159}
160
161status_t CameraClient::checkPidAndHardware() const {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700162 if (mHardware == 0) {
163 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
164 return INVALID_OPERATION;
165 }
Eino-Ville Talvala6192b892016-04-04 12:31:18 -0700166 status_t result = checkPid();
167 if (result != NO_ERROR) return result;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700168 return NO_ERROR;
169}
170
171status_t CameraClient::lock() {
172 int callingPid = getCallingPid();
173 LOG1("lock (pid %d)", callingPid);
174 Mutex::Autolock lock(mLock);
175
176 // lock camera to this client if the the camera is unlocked
177 if (mClientPid == 0) {
178 mClientPid = callingPid;
179 return NO_ERROR;
180 }
181
182 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
183 return checkPid();
184}
185
186status_t CameraClient::unlock() {
187 int callingPid = getCallingPid();
188 LOG1("unlock (pid %d)", callingPid);
189 Mutex::Autolock lock(mLock);
190
191 // allow anyone to use camera (after they lock the camera)
192 status_t result = checkPid();
193 if (result == NO_ERROR) {
194 if (mHardware->recordingEnabled()) {
195 ALOGE("Not allowed to unlock camera during recording.");
196 return INVALID_OPERATION;
197 }
198 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800199 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700200 // we need to remove the reference to ICameraClient so that when the app
201 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800202 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700203 }
204 return result;
205}
206
207// connect a new client to the camera
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800208status_t CameraClient::connect(const sp<hardware::ICameraClient>& client) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700209 int callingPid = getCallingPid();
210 LOG1("connect E (pid %d)", callingPid);
211 Mutex::Autolock lock(mLock);
212
213 if (mClientPid != 0 && checkPid() != NO_ERROR) {
214 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
215 mClientPid, callingPid);
216 return EBUSY;
217 }
218
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800219 if (mRemoteCallback != 0 &&
Marco Nelissen06b46062014-11-14 07:58:25 -0800220 (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700221 LOG1("Connect to the same client");
222 return NO_ERROR;
223 }
224
225 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
226 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800227 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700228
229 LOG1("connect X (pid %d)", callingPid);
230 return NO_ERROR;
231}
232
233static void disconnectWindow(const sp<ANativeWindow>& window) {
234 if (window != 0) {
235 status_t result = native_window_api_disconnect(window.get(),
236 NATIVE_WINDOW_API_CAMERA);
237 if (result != NO_ERROR) {
238 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
239 result);
240 }
241 }
242}
243
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800244binder::Status CameraClient::disconnect() {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700245 int callingPid = getCallingPid();
246 LOG1("disconnect E (pid %d)", callingPid);
247 Mutex::Autolock lock(mLock);
248
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800249 binder::Status res = binder::Status::ok();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800250 // Allow both client and the cameraserver to disconnect at all times
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700251 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700252 ALOGW("different client - don't disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800253 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700254 }
255
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700256 // Make sure disconnect() is done once and once only, whether it is called
257 // from the user directly, or called by the destructor.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800258 if (mHardware == 0) return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700259
260 LOG1("hardware teardown");
261 // Before destroying mHardware, we must make sure it's in the
262 // idle state.
263 // Turn off all messages.
264 disableMsgType(CAMERA_MSG_ALL_MSGS);
265 mHardware->stopPreview();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800266 sCameraService->updateProxyDeviceState(
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700267 ICameraServiceProxy::CAMERA_STATE_IDLE,
268 String8::format("%d", mCameraId));
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700269 mHardware->cancelPicture();
270 // Release the hardware resources.
271 mHardware->release();
272
273 // Release the held ANativeWindow resources.
274 if (mPreviewWindow != 0) {
275 disconnectWindow(mPreviewWindow);
276 mPreviewWindow = 0;
277 mHardware->setPreviewWindow(mPreviewWindow);
278 }
279 mHardware.clear();
280
281 CameraService::Client::disconnect();
282
283 LOG1("disconnect X (pid %d)", callingPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800284
285 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700286}
287
288// ----------------------------------------------------------------------------
289
290status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
291 const sp<ANativeWindow>& window) {
292 Mutex::Autolock lock(mLock);
293 status_t result = checkPidAndHardware();
294 if (result != NO_ERROR) return result;
295
296 // return if no change in surface.
297 if (binder == mSurface) {
298 return NO_ERROR;
299 }
300
301 if (window != 0) {
302 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
303 if (result != NO_ERROR) {
304 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
305 result);
306 return result;
307 }
308 }
309
310 // If preview has been already started, register preview buffers now.
311 if (mHardware->previewEnabled()) {
312 if (window != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800313 mHardware->setPreviewScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
314 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700315 result = mHardware->setPreviewWindow(window);
316 }
317 }
318
319 if (result == NO_ERROR) {
320 // Everything has succeeded. Disconnect the old window and remember the
321 // new window.
322 disconnectWindow(mPreviewWindow);
323 mSurface = binder;
324 mPreviewWindow = window;
325 } else {
326 // Something went wrong after we connected to the new window, so
327 // disconnect here.
328 disconnectWindow(window);
329 }
330
331 return result;
332}
333
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700334// set the buffer consumer that the preview will use
335status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800336 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700337 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700338 getCallingPid());
339
340 sp<IBinder> binder;
341 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800342 if (bufferProducer != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800343 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700344 // Using controlledByApp flag to ensure that the buffer queue remains in
345 // async mode for the old camera API, where many applications depend
346 // on that behavior.
347 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700348 }
349 return setPreviewWindow(binder, window);
350}
351
352// set the preview callback flag to affect how the received frames from
353// preview are handled.
354void CameraClient::setPreviewCallbackFlag(int callback_flag) {
355 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
356 Mutex::Autolock lock(mLock);
357 if (checkPidAndHardware() != NO_ERROR) return;
358
359 mPreviewCallbackFlag = callback_flag;
360 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
361 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
362 } else {
363 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
364 }
365}
366
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700367status_t CameraClient::setPreviewCallbackTarget(
368 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700369 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700370 ALOGE("%s: Unimplemented!", __FUNCTION__);
371 return INVALID_OPERATION;
372}
373
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700374// start preview mode
375status_t CameraClient::startPreview() {
376 LOG1("startPreview (pid %d)", getCallingPid());
377 return startCameraMode(CAMERA_PREVIEW_MODE);
378}
379
380// start recording mode
381status_t CameraClient::startRecording() {
382 LOG1("startRecording (pid %d)", getCallingPid());
383 return startCameraMode(CAMERA_RECORDING_MODE);
384}
385
386// start preview or recording
387status_t CameraClient::startCameraMode(camera_mode mode) {
388 LOG1("startCameraMode(%d)", mode);
389 Mutex::Autolock lock(mLock);
390 status_t result = checkPidAndHardware();
391 if (result != NO_ERROR) return result;
392
393 switch(mode) {
394 case CAMERA_PREVIEW_MODE:
395 if (mSurface == 0 && mPreviewWindow == 0) {
396 LOG1("mSurface is not set yet.");
397 // still able to start preview in this case.
398 }
399 return startPreviewMode();
400 case CAMERA_RECORDING_MODE:
401 if (mSurface == 0 && mPreviewWindow == 0) {
402 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
403 return INVALID_OPERATION;
404 }
405 return startRecordingMode();
406 default:
407 return UNKNOWN_ERROR;
408 }
409}
410
411status_t CameraClient::startPreviewMode() {
412 LOG1("startPreviewMode");
413 status_t result = NO_ERROR;
414
415 // if preview has been enabled, nothing needs to be done
416 if (mHardware->previewEnabled()) {
417 return NO_ERROR;
418 }
419
420 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800421 mHardware->setPreviewScalingMode(
422 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
423 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700424 }
425 mHardware->setPreviewWindow(mPreviewWindow);
426 result = mHardware->startPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700427 if (result == NO_ERROR) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800428 sCameraService->updateProxyDeviceState(
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700429 ICameraServiceProxy::CAMERA_STATE_ACTIVE,
430 String8::format("%d", mCameraId));
431 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700432 return result;
433}
434
435status_t CameraClient::startRecordingMode() {
436 LOG1("startRecordingMode");
437 status_t result = NO_ERROR;
438
439 // if recording has been enabled, nothing needs to be done
440 if (mHardware->recordingEnabled()) {
441 return NO_ERROR;
442 }
443
444 // if preview has not been started, start preview first
445 if (!mHardware->previewEnabled()) {
446 result = startPreviewMode();
447 if (result != NO_ERROR) {
448 return result;
449 }
450 }
451
452 // start recording mode
453 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800454 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700455 result = mHardware->startRecording();
456 if (result != NO_ERROR) {
457 ALOGE("mHardware->startRecording() failed with status %d", result);
458 }
459 return result;
460}
461
462// stop preview mode
463void CameraClient::stopPreview() {
464 LOG1("stopPreview (pid %d)", getCallingPid());
465 Mutex::Autolock lock(mLock);
466 if (checkPidAndHardware() != NO_ERROR) return;
467
468
469 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
470 mHardware->stopPreview();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800471 sCameraService->updateProxyDeviceState(
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700472 ICameraServiceProxy::CAMERA_STATE_IDLE,
473 String8::format("%d", mCameraId));
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());
480 Mutex::Autolock lock(mLock);
481 if (checkPidAndHardware() != NO_ERROR) return;
482
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700483 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
484 mHardware->stopRecording();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800485 sCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700486
487 mPreviewBuffer.clear();
488}
489
490// release a recording frame
491void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
492 Mutex::Autolock lock(mLock);
493 if (checkPidAndHardware() != NO_ERROR) return;
Chien-Yu Chen42f27072016-01-11 17:39:17 -0800494 if (mem == nullptr) {
495 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26164272",
496 IPCThreadState::self()->getCallingUid(), nullptr, 0);
497 return;
498 }
499
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700500 mHardware->releaseRecordingFrame(mem);
501}
502
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700503void CameraClient::releaseRecordingFrameHandle(native_handle_t *handle) {
504 if (handle == nullptr) return;
505
506 sp<IMemory> dataPtr;
507 {
508 Mutex::Autolock l(mAvailableCallbackBuffersLock);
509 if (!mAvailableCallbackBuffers.empty()) {
510 dataPtr = mAvailableCallbackBuffers.back();
511 mAvailableCallbackBuffers.pop_back();
512 }
513 }
514
515 if (dataPtr == nullptr) {
516 ALOGE("%s: %d: No callback buffer available. Dropping a native handle.", __FUNCTION__,
517 __LINE__);
518 native_handle_close(handle);
519 native_handle_delete(handle);
520 return;
521 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
522 ALOGE("%s: %d: Callback buffer size doesn't match VideoNativeHandleMetadata", __FUNCTION__,
523 __LINE__);
524 native_handle_close(handle);
525 native_handle_delete(handle);
526 return;
527 }
528
529 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
530 metadata->eType = kMetadataBufferTypeNativeHandleSource;
531 metadata->pHandle = handle;
532
533 mHardware->releaseRecordingFrame(dataPtr);
534}
535
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800536status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
537 LOG1("setVideoBufferMode: %d", videoBufferMode);
538 bool enableMetadataInBuffers = false;
539
540 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
541 enableMetadataInBuffers = true;
542 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
543 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
544 videoBufferMode);
545 return BAD_VALUE;
546 }
547
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700548 Mutex::Autolock lock(mLock);
549 if (checkPidAndHardware() != NO_ERROR) {
550 return UNKNOWN_ERROR;
551 }
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800552
553 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700554}
555
556bool CameraClient::previewEnabled() {
557 LOG1("previewEnabled (pid %d)", getCallingPid());
558
559 Mutex::Autolock lock(mLock);
560 if (checkPidAndHardware() != NO_ERROR) return false;
561 return mHardware->previewEnabled();
562}
563
564bool CameraClient::recordingEnabled() {
565 LOG1("recordingEnabled (pid %d)", getCallingPid());
566
567 Mutex::Autolock lock(mLock);
568 if (checkPidAndHardware() != NO_ERROR) return false;
569 return mHardware->recordingEnabled();
570}
571
572status_t CameraClient::autoFocus() {
573 LOG1("autoFocus (pid %d)", getCallingPid());
574
575 Mutex::Autolock lock(mLock);
576 status_t result = checkPidAndHardware();
577 if (result != NO_ERROR) return result;
578
579 return mHardware->autoFocus();
580}
581
582status_t CameraClient::cancelAutoFocus() {
583 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
584
585 Mutex::Autolock lock(mLock);
586 status_t result = checkPidAndHardware();
587 if (result != NO_ERROR) return result;
588
589 return mHardware->cancelAutoFocus();
590}
591
592// take a picture - image is returned in callback
593status_t CameraClient::takePicture(int msgType) {
594 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
595
596 Mutex::Autolock lock(mLock);
597 status_t result = checkPidAndHardware();
598 if (result != NO_ERROR) return result;
599
600 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
601 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
602 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
603 " cannot be both enabled");
604 return BAD_VALUE;
605 }
606
607 // We only accept picture related message types
608 // and ignore other types of messages for takePicture().
609 int picMsgType = msgType
610 & (CAMERA_MSG_SHUTTER |
611 CAMERA_MSG_POSTVIEW_FRAME |
612 CAMERA_MSG_RAW_IMAGE |
613 CAMERA_MSG_RAW_IMAGE_NOTIFY |
614 CAMERA_MSG_COMPRESSED_IMAGE);
615
616 enableMsgType(picMsgType);
617
618 return mHardware->takePicture();
619}
620
621// set preview/capture parameters - key/value pairs
622status_t CameraClient::setParameters(const String8& params) {
623 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
624
625 Mutex::Autolock lock(mLock);
626 status_t result = checkPidAndHardware();
627 if (result != NO_ERROR) return result;
628
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700629 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700630 CameraParameters p(params);
631 return mHardware->setParameters(p);
632}
633
634// get preview/capture parameters - key/value pairs
635String8 CameraClient::getParameters() const {
636 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700637 // The camera service can unconditionally get the parameters at all times
638 if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700639
640 String8 params(mHardware->getParameters().flatten());
641 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
642 return params;
643}
644
645// enable shutter sound
646status_t CameraClient::enableShutterSound(bool enable) {
647 LOG1("enableShutterSound (pid %d)", getCallingPid());
648
649 status_t result = checkPidAndHardware();
650 if (result != NO_ERROR) return result;
651
652 if (enable) {
653 mPlayShutterSound = true;
654 return OK;
655 }
656
Igor Murashkina858ea02014-08-19 14:53:08 -0700657 // the camera2 api legacy mode can unconditionally disable the shutter sound
658 if (mLegacyMode) {
659 ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__);
660 mPlayShutterSound = false;
661 return OK;
662 }
663
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700664 // Disabling shutter sound may not be allowed. In that case only
665 // allow the mediaserver process to disable the sound.
666 char value[PROPERTY_VALUE_MAX];
667 property_get("ro.camera.sound.forced", value, "0");
668 if (strcmp(value, "0") != 0) {
669 // Disabling shutter sound is not allowed. Deny if the current
670 // process is not mediaserver.
671 if (getCallingPid() != getpid()) {
672 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
673 return PERMISSION_DENIED;
674 }
675 }
676
677 mPlayShutterSound = false;
678 return OK;
679}
680
681status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
682 LOG1("sendCommand (pid %d)", getCallingPid());
683 int orientation;
684 Mutex::Autolock lock(mLock);
685 status_t result = checkPidAndHardware();
686 if (result != NO_ERROR) return result;
687
688 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
689 // Mirror the preview if the camera is front-facing.
690 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
691 if (orientation == -1) return BAD_VALUE;
692
693 if (mOrientation != orientation) {
694 mOrientation = orientation;
695 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800696 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700697 }
698 }
699 return OK;
700 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
701 switch (arg1) {
702 case 0:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700703 return enableShutterSound(false);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700704 case 1:
Eino-Ville Talvalac5268e82012-09-11 11:01:18 -0700705 return enableShutterSound(true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700706 default:
707 return BAD_VALUE;
708 }
709 return OK;
710 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800711 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
James Dong983cf232012-08-01 16:39:55 -0700712 } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) {
713 // Silently ignore this command
714 return INVALID_OPERATION;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700715 } else if (cmd == CAMERA_CMD_PING) {
716 // If mHardware is 0, checkPidAndHardware will return error.
717 return OK;
718 }
719
720 return mHardware->sendCommand(cmd, arg1, arg2);
721}
722
723// ----------------------------------------------------------------------------
724
725void CameraClient::enableMsgType(int32_t msgType) {
726 android_atomic_or(msgType, &mMsgEnabled);
727 mHardware->enableMsgType(msgType);
728}
729
730void CameraClient::disableMsgType(int32_t msgType) {
731 android_atomic_and(~msgType, &mMsgEnabled);
732 mHardware->disableMsgType(msgType);
733}
734
735#define CHECK_MESSAGE_INTERVAL 10 // 10ms
736bool CameraClient::lockIfMessageWanted(int32_t msgType) {
737 int sleepCount = 0;
738 while (mMsgEnabled & msgType) {
739 if (mLock.tryLock() == NO_ERROR) {
740 if (sleepCount > 0) {
741 LOG1("lockIfMessageWanted(%d): waited for %d ms",
742 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
743 }
Ruben Brunkcc776712015-02-17 20:18:47 -0800744
745 // If messages are no longer enabled after acquiring lock, release and drop message
746 if ((mMsgEnabled & msgType) == 0) {
747 mLock.unlock();
748 break;
749 }
750
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700751 return true;
752 }
753 if (sleepCount++ == 0) {
754 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
755 }
756 usleep(CHECK_MESSAGE_INTERVAL * 1000);
757 }
758 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
759 return false;
760}
761
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800762sp<CameraClient> CameraClient::getClientFromCookie(void* user) {
763 String8 cameraId = String8::format("%d", (int)(intptr_t) user);
764 auto clientDescriptor = sCameraService->mActiveClientManager.get(cameraId);
765 if (clientDescriptor != nullptr) {
766 return sp<CameraClient>{
767 static_cast<CameraClient*>(clientDescriptor->getValue().get())};
768 }
769 return sp<CameraClient>{nullptr};
770}
771
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700772// Callback messages can be dispatched to internal handlers or pass to our
773// client's callback functions, depending on the message type.
774//
775// notifyCallback:
776// CAMERA_MSG_SHUTTER handleShutter
777// (others) c->notifyCallback
778// dataCallback:
779// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
780// CAMERA_MSG_POSTVIEW_FRAME handlePostview
781// CAMERA_MSG_RAW_IMAGE handleRawPicture
782// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
783// (others) c->dataCallback
784// dataCallbackTimestamp
785// (others) c->dataCallbackTimestamp
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700786
787void CameraClient::notifyCallback(int32_t msgType, int32_t ext1,
788 int32_t ext2, void* user) {
789 LOG2("notifyCallback(%d)", msgType);
790
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800791 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800792 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700793
794 if (!client->lockIfMessageWanted(msgType)) return;
795
796 switch (msgType) {
797 case CAMERA_MSG_SHUTTER:
798 // ext1 is the dimension of the yuv picture.
799 client->handleShutter();
800 break;
801 default:
802 client->handleGenericNotify(msgType, ext1, ext2);
803 break;
804 }
805}
806
807void CameraClient::dataCallback(int32_t msgType,
808 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
809 LOG2("dataCallback(%d)", msgType);
810
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800811 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800812 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700813
814 if (!client->lockIfMessageWanted(msgType)) return;
815 if (dataPtr == 0 && metadata == NULL) {
816 ALOGE("Null data returned in data callback");
817 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
818 return;
819 }
820
821 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
822 case CAMERA_MSG_PREVIEW_FRAME:
823 client->handlePreviewData(msgType, dataPtr, metadata);
824 break;
825 case CAMERA_MSG_POSTVIEW_FRAME:
826 client->handlePostview(dataPtr);
827 break;
828 case CAMERA_MSG_RAW_IMAGE:
829 client->handleRawPicture(dataPtr);
830 break;
831 case CAMERA_MSG_COMPRESSED_IMAGE:
832 client->handleCompressedPicture(dataPtr);
833 break;
834 default:
835 client->handleGenericData(msgType, dataPtr, metadata);
836 break;
837 }
838}
839
840void CameraClient::dataCallbackTimestamp(nsecs_t timestamp,
841 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
842 LOG2("dataCallbackTimestamp(%d)", msgType);
843
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800844 sp<CameraClient> client = getClientFromCookie(user);
Ruben Brunkcc776712015-02-17 20:18:47 -0800845 if (client.get() == nullptr) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700846
847 if (!client->lockIfMessageWanted(msgType)) return;
848
849 if (dataPtr == 0) {
850 ALOGE("Null data returned in data with timestamp callback");
851 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
852 return;
853 }
854
855 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
856}
857
858// snapshot taken callback
859void CameraClient::handleShutter(void) {
860 if (mPlayShutterSound) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800861 sCameraService->playSound(CameraService::SOUND_SHUTTER);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700862 }
863
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800864 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700865 if (c != 0) {
866 mLock.unlock();
867 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
868 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
869 }
870 disableMsgType(CAMERA_MSG_SHUTTER);
871
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700872 // Shutters only happen in response to takePicture, so mark device as
873 // idle now, until preview is restarted
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800874 sCameraService->updateProxyDeviceState(
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700875 ICameraServiceProxy::CAMERA_STATE_IDLE,
876 String8::format("%d", mCameraId));
877
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700878 mLock.unlock();
879}
880
881// preview callback - frame buffer update
882void CameraClient::handlePreviewData(int32_t msgType,
883 const sp<IMemory>& mem,
884 camera_frame_metadata_t *metadata) {
885 ssize_t offset;
886 size_t size;
887 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
888
889 // local copy of the callback flags
890 int flags = mPreviewCallbackFlag;
891
892 // is callback enabled?
893 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
894 // If the enable bit is off, the copy-out and one-shot bits are ignored
895 LOG2("frame callback is disabled");
896 mLock.unlock();
897 return;
898 }
899
900 // hold a strong pointer to the client
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800901 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700902
903 // clear callback flags if no client or one-shot mode
904 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
905 LOG2("Disable preview callback");
906 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
907 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
908 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
909 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
910 }
911
912 if (c != 0) {
913 // Is the received frame copied out or not?
914 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
915 LOG2("frame is copied");
916 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
917 } else {
918 LOG2("frame is forwarded");
919 mLock.unlock();
920 c->dataCallback(msgType, mem, metadata);
921 }
922 } else {
923 mLock.unlock();
924 }
925}
926
927// picture callback - postview image ready
928void CameraClient::handlePostview(const sp<IMemory>& mem) {
929 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
930
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800931 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700932 mLock.unlock();
933 if (c != 0) {
934 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
935 }
936}
937
938// picture callback - raw image ready
939void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
940 disableMsgType(CAMERA_MSG_RAW_IMAGE);
941
942 ssize_t offset;
943 size_t size;
944 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
945
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800946 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700947 mLock.unlock();
948 if (c != 0) {
949 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
950 }
951}
952
953// picture callback - compressed picture ready
954void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
955 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
956
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800957 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700958 mLock.unlock();
959 if (c != 0) {
960 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
961 }
962}
963
964
965void CameraClient::handleGenericNotify(int32_t msgType,
966 int32_t ext1, int32_t ext2) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800967 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700968 mLock.unlock();
969 if (c != 0) {
970 c->notifyCallback(msgType, ext1, ext2);
971 }
972}
973
974void CameraClient::handleGenericData(int32_t msgType,
975 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800976 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700977 mLock.unlock();
978 if (c != 0) {
979 c->dataCallback(msgType, dataPtr, metadata);
980 }
981}
982
983void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
984 int32_t msgType, const sp<IMemory>& dataPtr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800985 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700986 mLock.unlock();
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700987 if (c != 0 && dataPtr != nullptr) {
988 native_handle_t* handle = nullptr;
989
990 // Check if dataPtr contains a VideoNativeHandleMetadata.
991 if (dataPtr->size() == sizeof(VideoNativeHandleMetadata)) {
992 VideoNativeHandleMetadata *metadata =
993 (VideoNativeHandleMetadata*)(dataPtr->pointer());
994 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
995 handle = metadata->pHandle;
996 }
997 }
998
999 // If dataPtr contains a native handle, send it via recordingFrameHandleCallbackTimestamp.
1000 if (handle != nullptr) {
1001 {
1002 Mutex::Autolock l(mAvailableCallbackBuffersLock);
1003 mAvailableCallbackBuffers.push_back(dataPtr);
1004 }
1005 c->recordingFrameHandleCallbackTimestamp(timestamp, handle);
1006 } else {
1007 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1008 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001009 }
1010}
1011
1012void CameraClient::copyFrameAndPostCopiedFrame(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001013 int32_t msgType, const sp<hardware::ICameraClient>& client,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001014 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1015 camera_frame_metadata_t *metadata) {
1016 LOG2("copyFrameAndPostCopiedFrame");
1017 // It is necessary to copy out of pmem before sending this to
1018 // the callback. For efficiency, reuse the same MemoryHeapBase
1019 // provided it's big enough. Don't allocate the memory or
1020 // perform the copy if there's no callback.
1021 // hold the preview lock while we grab a reference to the preview buffer
1022 sp<MemoryHeapBase> previewBuffer;
1023
1024 if (mPreviewBuffer == 0) {
1025 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1026 } else if (size > mPreviewBuffer->virtualSize()) {
1027 mPreviewBuffer.clear();
1028 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1029 }
1030 if (mPreviewBuffer == 0) {
1031 ALOGE("failed to allocate space for preview buffer");
1032 mLock.unlock();
1033 return;
1034 }
1035 previewBuffer = mPreviewBuffer;
1036
Ruben Brunk65e01f72014-08-29 17:25:13 -07001037 void* previewBufferBase = previewBuffer->base();
1038 void* heapBase = heap->base();
1039
1040 if (heapBase == MAP_FAILED) {
1041 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
1042 mLock.unlock();
1043 return;
1044 } else if (previewBufferBase == MAP_FAILED) {
1045 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
1046 mLock.unlock();
1047 return;
1048 }
1049
1050 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001051
1052 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1053 if (frame == 0) {
1054 ALOGE("failed to allocate space for frame callback");
1055 mLock.unlock();
1056 return;
1057 }
1058
1059 mLock.unlock();
1060 client->dataCallback(msgType, frame, metadata);
1061}
1062
1063int CameraClient::getOrientation(int degrees, bool mirror) {
1064 if (!mirror) {
1065 if (degrees == 0) return 0;
1066 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1067 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1068 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1069 } else { // Do mirror (horizontal flip)
1070 if (degrees == 0) { // FLIP_H and ROT_0
1071 return HAL_TRANSFORM_FLIP_H;
1072 } else if (degrees == 90) { // FLIP_H and ROT_90
1073 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1074 } else if (degrees == 180) { // FLIP_H and ROT_180
1075 return HAL_TRANSFORM_FLIP_V;
1076 } else if (degrees == 270) { // FLIP_H and ROT_270
1077 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1078 }
1079 }
1080 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1081 return -1;
1082}
1083
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001084status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001085 (void)bufferProducer;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001086 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1087 return INVALID_OPERATION;
1088}
1089
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001090}; // namespace android