blob: d65ac7b339d59ce2e6a8d15f50b5c69c1c7b3283 [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"
Jayant Chowdhary12361932018-08-27 14:46:13 -070028#include "utils/CameraThreadState.h"
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070029
30namespace android {
31
32#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
33#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
34
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070035CameraClient::CameraClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080036 const sp<hardware::ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080037 const String16& clientPackageName,
38 int cameraId, int cameraFacing,
39 int clientPid, int clientUid,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -070040 int servicePid):
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080041 Client(cameraService, cameraClient, clientPackageName,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080042 String8::format("%d", cameraId), cameraId, cameraFacing, clientPid,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080043 clientUid, servicePid)
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070044{
Jayant Chowdhary12361932018-08-27 14:46:13 -070045 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070046 LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId);
47
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070048 mHardware = NULL;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070049 mMsgEnabled = 0;
50 mSurface = 0;
51 mPreviewWindow = 0;
52 mDestructionStarted = false;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070053
54 // Callback is disabled by default
55 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
56 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
57 mPlayShutterSound = true;
58 LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId);
59}
60
Emilian Peevbd8c5032018-02-14 23:05:40 +000061status_t CameraClient::initialize(sp<CameraProviderManager> manager,
62 const String8& /*monitorTags*/) {
Jayant Chowdhary12361932018-08-27 14:46:13 -070063 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080064 status_t res;
65
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070066 LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId);
67
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080068 // Verify ops permissions
69 res = startCameraOps();
70 if (res != OK) {
71 return res;
72 }
73
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070074 char camera_device_name[10];
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070075 snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId);
76
77 mHardware = new CameraHardwareInterface(camera_device_name);
Emilian Peevf53f66e2017-04-11 14:29:43 +010078 res = mHardware->initialize(manager);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070079 if (res != OK) {
80 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
81 __FUNCTION__, mCameraId, strerror(-res), res);
Igor Murashkin44f120f2012-10-09 14:45:37 -070082 mHardware.clear();
Zhijun Heb10cdad2014-06-16 16:38:35 -070083 return res;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070084 }
85
86 mHardware->setCallbacks(notifyCallback,
87 dataCallback,
88 dataCallbackTimestamp,
Yin-Chia Yehb5df5472017-03-20 19:32:19 -070089 handleCallbackTimestampBatch,
Kévin PETIT377b2ec2014-02-03 12:35:36 +000090 (void *)(uintptr_t)mCameraId);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070091
92 // Enable zoom, error, focus, and metadata messages by default
93 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
94 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
95
96 LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId);
97 return OK;
98}
99
100
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700101// tear down the client
102CameraClient::~CameraClient() {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700103 mDestructionStarted = true;
Jayant Chowdhary12361932018-08-27 14:46:13 -0700104 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700105 LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this);
106
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700107 disconnect();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700108 LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this);
109}
110
111status_t CameraClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800112 return BasicClient::dump(fd, args);
113}
114
115status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700116 const size_t SIZE = 256;
117 char buffer[SIZE];
118
Ruben Brunkcc776712015-02-17 20:18:47 -0800119 size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n",
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700120 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800121 (getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800122 IInterface::asBinder(getRemoteCallback()).get() : NULL),
Ruben Brunkcc776712015-02-17 20:18:47 -0800123 mClientUid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700124 len = (len > SIZE - 1) ? SIZE - 1 : len;
125 write(fd, buffer, len);
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700126
127 len = snprintf(buffer, SIZE, "Latest set parameters:\n");
128 len = (len > SIZE - 1) ? SIZE - 1 : len;
129 write(fd, buffer, len);
130
131 mLatestSetParameters.dump(fd, args);
132
133 const char *enddump = "\n\n";
134 write(fd, enddump, strlen(enddump));
135
Yin-Chia Yeh9cf785b2017-12-01 13:37:30 -0800136 sp<CameraHardwareInterface> hardware = mHardware;
137 if (hardware != nullptr) {
138 return hardware->dump(fd, args);
139 }
140 ALOGI("%s: camera device closed already, skip dumping", __FUNCTION__);
141 return OK;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700142}
143
144// ----------------------------------------------------------------------------
145
146status_t CameraClient::checkPid() const {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700147 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700148 if (callingPid == mClientPid) return NO_ERROR;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700149
150 ALOGW("attempt to use a locked camera from a different process"
151 " (old pid %d, new pid %d)", mClientPid, callingPid);
152 return EBUSY;
153}
154
155status_t CameraClient::checkPidAndHardware() const {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700156 if (mHardware == 0) {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700157 ALOGE("attempt to use a camera after disconnect() (pid %d)",
158 CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700159 return INVALID_OPERATION;
160 }
Eino-Ville Talvala6192b892016-04-04 12:31:18 -0700161 status_t result = checkPid();
162 if (result != NO_ERROR) return result;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700163 return NO_ERROR;
164}
165
166status_t CameraClient::lock() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700167 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700168 LOG1("lock (pid %d)", callingPid);
169 Mutex::Autolock lock(mLock);
170
171 // lock camera to this client if the the camera is unlocked
172 if (mClientPid == 0) {
173 mClientPid = callingPid;
174 return NO_ERROR;
175 }
176
177 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
178 return checkPid();
179}
180
181status_t CameraClient::unlock() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700182 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700183 LOG1("unlock (pid %d)", callingPid);
184 Mutex::Autolock lock(mLock);
185
186 // allow anyone to use camera (after they lock the camera)
187 status_t result = checkPid();
188 if (result == NO_ERROR) {
189 if (mHardware->recordingEnabled()) {
190 ALOGE("Not allowed to unlock camera during recording.");
191 return INVALID_OPERATION;
192 }
193 mClientPid = 0;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800194 LOG1("clear mRemoteCallback (pid %d)", callingPid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700195 // we need to remove the reference to ICameraClient so that when the app
196 // goes away, the reference count goes to 0.
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800197 mRemoteCallback.clear();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700198 }
199 return result;
200}
201
202// connect a new client to the camera
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800203status_t CameraClient::connect(const sp<hardware::ICameraClient>& client) {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700204 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700205 LOG1("connect E (pid %d)", callingPid);
206 Mutex::Autolock lock(mLock);
207
208 if (mClientPid != 0 && checkPid() != NO_ERROR) {
209 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
210 mClientPid, callingPid);
211 return EBUSY;
212 }
213
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800214 if (mRemoteCallback != 0 &&
Marco Nelissen06b46062014-11-14 07:58:25 -0800215 (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700216 LOG1("Connect to the same client");
217 return NO_ERROR;
218 }
219
220 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
221 mClientPid = callingPid;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800222 mRemoteCallback = client;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700223
224 LOG1("connect X (pid %d)", callingPid);
225 return NO_ERROR;
226}
227
228static void disconnectWindow(const sp<ANativeWindow>& window) {
229 if (window != 0) {
230 status_t result = native_window_api_disconnect(window.get(),
231 NATIVE_WINDOW_API_CAMERA);
232 if (result != NO_ERROR) {
233 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
234 result);
235 }
236 }
237}
238
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800239binder::Status CameraClient::disconnect() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700240 int callingPid = CameraThreadState::getCallingPid();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700241 LOG1("disconnect E (pid %d)", callingPid);
242 Mutex::Autolock lock(mLock);
243
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800244 binder::Status res = binder::Status::ok();
Chien-Yu Chen98a668f2015-12-18 14:10:33 -0800245 // Allow both client and the cameraserver to disconnect at all times
Eino-Ville Talvalac0379202012-10-09 22:16:58 -0700246 if (callingPid != mClientPid && callingPid != mServicePid) {
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700247 ALOGW("different client - don't disconnect");
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800248 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700249 }
250
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700251 // Make sure disconnect() is done once and once only, whether it is called
252 // from the user directly, or called by the destructor.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800253 if (mHardware == 0) return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700254
255 LOG1("hardware teardown");
256 // Before destroying mHardware, we must make sure it's in the
257 // idle state.
258 // Turn off all messages.
259 disableMsgType(CAMERA_MSG_ALL_MSGS);
260 mHardware->stopPreview();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800261 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700262 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
Emilian Peev573291c2018-02-10 02:10:56 +0000263 mCameraIdStr, mCameraFacing, mClientPackageName,
264 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700265 mHardware->cancelPicture();
266 // Release the hardware resources.
267 mHardware->release();
268
269 // Release the held ANativeWindow resources.
270 if (mPreviewWindow != 0) {
271 disconnectWindow(mPreviewWindow);
272 mPreviewWindow = 0;
273 mHardware->setPreviewWindow(mPreviewWindow);
274 }
275 mHardware.clear();
276
277 CameraService::Client::disconnect();
278
279 LOG1("disconnect X (pid %d)", callingPid);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800280
281 return res;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700282}
283
284// ----------------------------------------------------------------------------
285
286status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder,
287 const sp<ANativeWindow>& window) {
288 Mutex::Autolock lock(mLock);
289 status_t result = checkPidAndHardware();
290 if (result != NO_ERROR) return result;
291
292 // return if no change in surface.
293 if (binder == mSurface) {
294 return NO_ERROR;
295 }
296
297 if (window != 0) {
298 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
299 if (result != NO_ERROR) {
300 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
301 result);
302 return result;
303 }
304 }
305
306 // If preview has been already started, register preview buffers now.
307 if (mHardware->previewEnabled()) {
308 if (window != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800309 mHardware->setPreviewScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
310 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700311 result = mHardware->setPreviewWindow(window);
312 }
313 }
314
315 if (result == NO_ERROR) {
316 // Everything has succeeded. Disconnect the old window and remember the
317 // new window.
318 disconnectWindow(mPreviewWindow);
319 mSurface = binder;
320 mPreviewWindow = window;
321 } else {
322 // Something went wrong after we connected to the new window, so
323 // disconnect here.
324 disconnectWindow(window);
325 }
326
327 return result;
328}
329
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700330// set the buffer consumer that the preview will use
331status_t CameraClient::setPreviewTarget(
Andy McFadden8ba01022012-12-18 09:46:54 -0800332 const sp<IGraphicBufferProducer>& bufferProducer) {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700333 LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(),
Jayant Chowdhary12361932018-08-27 14:46:13 -0700334 CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700335
336 sp<IBinder> binder;
337 sp<ANativeWindow> window;
Andy McFadden8ba01022012-12-18 09:46:54 -0800338 if (bufferProducer != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800339 binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700340 // Using controlledByApp flag to ensure that the buffer queue remains in
341 // async mode for the old camera API, where many applications depend
342 // on that behavior.
343 window = new Surface(bufferProducer, /*controlledByApp*/ true);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700344 }
345 return setPreviewWindow(binder, window);
346}
347
348// set the preview callback flag to affect how the received frames from
349// preview are handled.
350void CameraClient::setPreviewCallbackFlag(int callback_flag) {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700351 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700352 Mutex::Autolock lock(mLock);
353 if (checkPidAndHardware() != NO_ERROR) return;
354
355 mPreviewCallbackFlag = callback_flag;
356 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
357 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
358 } else {
359 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
360 }
361}
362
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700363status_t CameraClient::setPreviewCallbackTarget(
364 const sp<IGraphicBufferProducer>& callbackProducer) {
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700365 (void)callbackProducer;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700366 ALOGE("%s: Unimplemented!", __FUNCTION__);
367 return INVALID_OPERATION;
368}
369
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700370// start preview mode
371status_t CameraClient::startPreview() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700372 LOG1("startPreview (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700373 return startCameraMode(CAMERA_PREVIEW_MODE);
374}
375
376// start recording mode
377status_t CameraClient::startRecording() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700378 LOG1("startRecording (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700379 return startCameraMode(CAMERA_RECORDING_MODE);
380}
381
382// start preview or recording
383status_t CameraClient::startCameraMode(camera_mode mode) {
384 LOG1("startCameraMode(%d)", mode);
385 Mutex::Autolock lock(mLock);
386 status_t result = checkPidAndHardware();
387 if (result != NO_ERROR) return result;
388
389 switch(mode) {
390 case CAMERA_PREVIEW_MODE:
391 if (mSurface == 0 && mPreviewWindow == 0) {
392 LOG1("mSurface is not set yet.");
393 // still able to start preview in this case.
394 }
395 return startPreviewMode();
396 case CAMERA_RECORDING_MODE:
397 if (mSurface == 0 && mPreviewWindow == 0) {
398 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
399 return INVALID_OPERATION;
400 }
401 return startRecordingMode();
402 default:
403 return UNKNOWN_ERROR;
404 }
405}
406
407status_t CameraClient::startPreviewMode() {
408 LOG1("startPreviewMode");
409 status_t result = NO_ERROR;
410
411 // if preview has been enabled, nothing needs to be done
412 if (mHardware->previewEnabled()) {
413 return NO_ERROR;
414 }
415
416 if (mPreviewWindow != 0) {
Eino-Ville Talvalaf7351172016-02-09 12:13:57 -0800417 mHardware->setPreviewScalingMode(
418 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
419 mHardware->setPreviewTransform(mOrientation);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700420 }
421 mHardware->setPreviewWindow(mPreviewWindow);
422 result = mHardware->startPreview();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700423 if (result == NO_ERROR) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800424 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700425 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE,
Emilian Peev573291c2018-02-10 02:10:56 +0000426 mCameraIdStr, mCameraFacing, mClientPackageName,
427 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700428 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700429 return result;
430}
431
432status_t CameraClient::startRecordingMode() {
433 LOG1("startRecordingMode");
434 status_t result = NO_ERROR;
435
436 // if recording has been enabled, nothing needs to be done
437 if (mHardware->recordingEnabled()) {
438 return NO_ERROR;
439 }
440
441 // if preview has not been started, start preview first
442 if (!mHardware->previewEnabled()) {
443 result = startPreviewMode();
444 if (result != NO_ERROR) {
445 return result;
446 }
447 }
448
449 // start recording mode
450 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800451 sCameraService->playSound(CameraService::SOUND_RECORDING_START);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700452 result = mHardware->startRecording();
453 if (result != NO_ERROR) {
454 ALOGE("mHardware->startRecording() failed with status %d", result);
455 }
456 return result;
457}
458
459// stop preview mode
460void CameraClient::stopPreview() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700461 LOG1("stopPreview (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700462 Mutex::Autolock lock(mLock);
463 if (checkPidAndHardware() != NO_ERROR) return;
464
465
466 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
467 mHardware->stopPreview();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800468 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700469 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
Emilian Peev573291c2018-02-10 02:10:56 +0000470 mCameraIdStr, mCameraFacing, mClientPackageName,
471 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700472 mPreviewBuffer.clear();
473}
474
475// stop recording mode
476void CameraClient::stopRecording() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700477 LOG1("stopRecording (pid %d)", CameraThreadState::getCallingPid());
Emilian Peev698f0a72017-04-05 11:20:09 +0100478 {
479 Mutex::Autolock lock(mLock);
480 if (checkPidAndHardware() != NO_ERROR) return;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700481
Emilian Peev698f0a72017-04-05 11:20:09 +0100482 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
483 mHardware->stopRecording();
484 sCameraService->playSound(CameraService::SOUND_RECORDING_STOP);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700485
Emilian Peev698f0a72017-04-05 11:20:09 +0100486 mPreviewBuffer.clear();
487 }
488
489 {
490 Mutex::Autolock l(mAvailableCallbackBuffersLock);
491 if (!mAvailableCallbackBuffers.empty()) {
492 mAvailableCallbackBuffers.clear();
493 }
494 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700495}
496
497// release a recording frame
498void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) {
499 Mutex::Autolock lock(mLock);
500 if (checkPidAndHardware() != NO_ERROR) return;
Chien-Yu Chen42f27072016-01-11 17:39:17 -0800501 if (mem == nullptr) {
502 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26164272",
Jayant Chowdhary12361932018-08-27 14:46:13 -0700503 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen42f27072016-01-11 17:39:17 -0800504 return;
505 }
506
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700507 mHardware->releaseRecordingFrame(mem);
508}
509
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700510void CameraClient::releaseRecordingFrameHandle(native_handle_t *handle) {
511 if (handle == nullptr) return;
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800512 Mutex::Autolock lock(mLock);
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700513 sp<IMemory> dataPtr;
514 {
515 Mutex::Autolock l(mAvailableCallbackBuffersLock);
516 if (!mAvailableCallbackBuffers.empty()) {
517 dataPtr = mAvailableCallbackBuffers.back();
518 mAvailableCallbackBuffers.pop_back();
519 }
520 }
521
522 if (dataPtr == nullptr) {
523 ALOGE("%s: %d: No callback buffer available. Dropping a native handle.", __FUNCTION__,
524 __LINE__);
525 native_handle_close(handle);
526 native_handle_delete(handle);
527 return;
528 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
529 ALOGE("%s: %d: Callback buffer size doesn't match VideoNativeHandleMetadata", __FUNCTION__,
530 __LINE__);
531 native_handle_close(handle);
532 native_handle_delete(handle);
533 return;
534 }
535
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800536 if (mHardware != nullptr) {
537 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
538 metadata->eType = kMetadataBufferTypeNativeHandleSource;
539 metadata->pHandle = handle;
540 mHardware->releaseRecordingFrame(dataPtr);
541 }
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -0700542}
543
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700544void CameraClient::releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles) {
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800545 Mutex::Autolock lock(mLock);
546 bool disconnected = (mHardware == nullptr);
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700547 size_t n = handles.size();
548 std::vector<sp<IMemory>> frames;
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800549 if (!disconnected) {
550 frames.reserve(n);
551 }
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700552 bool error = false;
553 for (auto& handle : handles) {
554 sp<IMemory> dataPtr;
555 {
556 Mutex::Autolock l(mAvailableCallbackBuffersLock);
557 if (!mAvailableCallbackBuffers.empty()) {
558 dataPtr = mAvailableCallbackBuffers.back();
559 mAvailableCallbackBuffers.pop_back();
560 }
561 }
562
563 if (dataPtr == nullptr) {
564 ALOGE("%s: %d: No callback buffer available. Dropping frames.", __FUNCTION__,
565 __LINE__);
566 error = true;
567 break;
568 } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) {
569 ALOGE("%s: %d: Callback buffer must be VideoNativeHandleMetadata", __FUNCTION__,
570 __LINE__);
571 error = true;
572 break;
573 }
574
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800575 if (!disconnected) {
576 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer());
577 metadata->eType = kMetadataBufferTypeNativeHandleSource;
578 metadata->pHandle = handle;
579 frames.push_back(dataPtr);
580 }
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700581 }
582
583 if (error) {
584 for (auto& handle : handles) {
585 native_handle_close(handle);
586 native_handle_delete(handle);
587 }
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800588 } else if (!disconnected) {
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700589 mHardware->releaseRecordingFrameBatch(frames);
590 }
591 return;
592}
593
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800594status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) {
595 LOG1("setVideoBufferMode: %d", videoBufferMode);
596 bool enableMetadataInBuffers = false;
597
598 if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) {
599 enableMetadataInBuffers = true;
600 } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) {
601 ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__,
602 videoBufferMode);
603 return BAD_VALUE;
604 }
605
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700606 Mutex::Autolock lock(mLock);
607 if (checkPidAndHardware() != NO_ERROR) {
608 return UNKNOWN_ERROR;
609 }
Chien-Yu Chen8cca0752015-11-13 15:28:48 -0800610
611 return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700612}
613
614bool CameraClient::previewEnabled() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700615 LOG1("previewEnabled (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700616
617 Mutex::Autolock lock(mLock);
618 if (checkPidAndHardware() != NO_ERROR) return false;
619 return mHardware->previewEnabled();
620}
621
622bool CameraClient::recordingEnabled() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700623 LOG1("recordingEnabled (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700624
625 Mutex::Autolock lock(mLock);
626 if (checkPidAndHardware() != NO_ERROR) return false;
627 return mHardware->recordingEnabled();
628}
629
630status_t CameraClient::autoFocus() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700631 LOG1("autoFocus (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700632
633 Mutex::Autolock lock(mLock);
634 status_t result = checkPidAndHardware();
635 if (result != NO_ERROR) return result;
636
637 return mHardware->autoFocus();
638}
639
640status_t CameraClient::cancelAutoFocus() {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700641 LOG1("cancelAutoFocus (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700642
643 Mutex::Autolock lock(mLock);
644 status_t result = checkPidAndHardware();
645 if (result != NO_ERROR) return result;
646
647 return mHardware->cancelAutoFocus();
648}
649
650// take a picture - image is returned in callback
651status_t CameraClient::takePicture(int msgType) {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700652 LOG1("takePicture (pid %d): 0x%x", CameraThreadState::getCallingPid(), msgType);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700653
654 Mutex::Autolock lock(mLock);
655 status_t result = checkPidAndHardware();
656 if (result != NO_ERROR) return result;
657
658 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
659 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
660 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
661 " cannot be both enabled");
662 return BAD_VALUE;
663 }
664
665 // We only accept picture related message types
666 // and ignore other types of messages for takePicture().
667 int picMsgType = msgType
668 & (CAMERA_MSG_SHUTTER |
669 CAMERA_MSG_POSTVIEW_FRAME |
670 CAMERA_MSG_RAW_IMAGE |
671 CAMERA_MSG_RAW_IMAGE_NOTIFY |
672 CAMERA_MSG_COMPRESSED_IMAGE);
673
674 enableMsgType(picMsgType);
675
676 return mHardware->takePicture();
677}
678
679// set preview/capture parameters - key/value pairs
680status_t CameraClient::setParameters(const String8& params) {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700681 LOG1("setParameters (pid %d) (%s)", CameraThreadState::getCallingPid(), params.string());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700682
683 Mutex::Autolock lock(mLock);
684 status_t result = checkPidAndHardware();
685 if (result != NO_ERROR) return result;
686
Igor Murashkinfcf5fea2014-09-11 14:43:24 -0700687 mLatestSetParameters = CameraParameters(params);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700688 CameraParameters p(params);
689 return mHardware->setParameters(p);
690}
691
692// get preview/capture parameters - key/value pairs
693String8 CameraClient::getParameters() const {
694 Mutex::Autolock lock(mLock);
Igor Murashkinebe865b2014-08-07 17:07:28 -0700695 // The camera service can unconditionally get the parameters at all times
Jayant Chowdhary12361932018-08-27 14:46:13 -0700696 if (CameraThreadState::getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) {
697 return String8();
698 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700699
700 String8 params(mHardware->getParameters().flatten());
Jayant Chowdhary12361932018-08-27 14:46:13 -0700701 LOG1("getParameters (pid %d) (%s)", CameraThreadState::getCallingPid(), params.string());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700702 return params;
703}
704
705// enable shutter sound
706status_t CameraClient::enableShutterSound(bool enable) {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700707 LOG1("enableShutterSound (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700708
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) {
Jayant Chowdhary12361932018-08-27 14:46:13 -0700722 LOG1("sendCommand (pid %d)", CameraThreadState::getCallingPid());
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700723 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