blob: b860ceb7fc4903a3c7a026caeb7a4bb50e265dfe [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,
Jooyung Hanb3f7cd22020-01-23 12:27:18 +090037 const String16& clientPackageName, const std::optional<String16>& clientFeatureId,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080038 int cameraId, int cameraFacing,
39 int clientPid, int clientUid,
Shuzhen Wang06fcfb02018-07-30 18:23:31 -070040 int servicePid):
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080041 Client(cameraService, cameraClient, clientPackageName, clientFeatureId,
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) {
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700537 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->unsecurePointer());
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800538 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) {
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700576 VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->unsecurePointer());
Yin-Chia Yehf44c24a2018-01-09 10:34:29 -0800577 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 }
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700919 // TODO: Using unsecurePointer() has some associated security pitfalls
920 // (see declaration for details).
921 // Either document why it is safe in this case or address the
922 // issue (e.g. by copying).
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700923 VideoNativeHandleMetadata *metadata =
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -0700924 (VideoNativeHandleMetadata*)(msg.dataPtr->unsecurePointer());
Yin-Chia Yehb5df5472017-03-20 19:32:19 -0700925 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
926 handle = metadata->pHandle;
927 }
928
929 if (handle == nullptr) {
930 ALOGE("%s: VideoNativeHandleMetadata type mismatch or null handle passed!",
931 __FUNCTION__);
932 return;
933 }
934 {
935 Mutex::Autolock l(client->mAvailableCallbackBuffersLock);
936 client->mAvailableCallbackBuffers.push_back(msg.dataPtr);
937 }
938 timestamps.push_back(msg.timestamp);
939 handles.push_back(handle);
940 }
941 c->recordingFrameHandleCallbackTimestampBatch(timestamps, handles);
942 }
943}
944
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700945// snapshot taken callback
946void CameraClient::handleShutter(void) {
947 if (mPlayShutterSound) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800948 sCameraService->playSound(CameraService::SOUND_SHUTTER);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700949 }
950
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800951 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700952 if (c != 0) {
953 mLock.unlock();
954 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
955 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
956 }
957 disableMsgType(CAMERA_MSG_SHUTTER);
958
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700959 // Shutters only happen in response to takePicture, so mark device as
960 // idle now, until preview is restarted
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800961 sCameraService->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700962 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE,
Emilian Peev573291c2018-02-10 02:10:56 +0000963 mCameraIdStr, mCameraFacing, mClientPackageName,
964 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700965
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700966 mLock.unlock();
967}
968
969// preview callback - frame buffer update
970void CameraClient::handlePreviewData(int32_t msgType,
971 const sp<IMemory>& mem,
972 camera_frame_metadata_t *metadata) {
973 ssize_t offset;
974 size_t size;
975 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
976
977 // local copy of the callback flags
978 int flags = mPreviewCallbackFlag;
979
980 // is callback enabled?
981 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
982 // If the enable bit is off, the copy-out and one-shot bits are ignored
983 LOG2("frame callback is disabled");
984 mLock.unlock();
985 return;
986 }
987
988 // hold a strong pointer to the client
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800989 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700990
991 // clear callback flags if no client or one-shot mode
992 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
993 LOG2("Disable preview callback");
994 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
995 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
996 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
997 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
998 }
999
1000 if (c != 0) {
1001 // Is the received frame copied out or not?
1002 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
1003 LOG2("frame is copied");
1004 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
1005 } else {
1006 LOG2("frame is forwarded");
1007 mLock.unlock();
1008 c->dataCallback(msgType, mem, metadata);
1009 }
1010 } else {
1011 mLock.unlock();
1012 }
1013}
1014
1015// picture callback - postview image ready
1016void CameraClient::handlePostview(const sp<IMemory>& mem) {
1017 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1018
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001019 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001020 mLock.unlock();
1021 if (c != 0) {
1022 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
1023 }
1024}
1025
1026// picture callback - raw image ready
1027void CameraClient::handleRawPicture(const sp<IMemory>& mem) {
1028 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1029
1030 ssize_t offset;
1031 size_t size;
1032 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1033
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001034 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001035 mLock.unlock();
1036 if (c != 0) {
1037 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
1038 }
1039}
1040
1041// picture callback - compressed picture ready
1042void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) {
1043 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1044
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001045 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001046 mLock.unlock();
1047 if (c != 0) {
1048 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
1049 }
1050}
1051
1052
1053void CameraClient::handleGenericNotify(int32_t msgType,
1054 int32_t ext1, int32_t ext2) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001055 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001056 mLock.unlock();
1057 if (c != 0) {
1058 c->notifyCallback(msgType, ext1, ext2);
1059 }
1060}
1061
1062void CameraClient::handleGenericData(int32_t msgType,
1063 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001064 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001065 mLock.unlock();
1066 if (c != 0) {
1067 c->dataCallback(msgType, dataPtr, metadata);
1068 }
1069}
1070
1071void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp,
1072 int32_t msgType, const sp<IMemory>& dataPtr) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001073 sp<hardware::ICameraClient> c = mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001074 mLock.unlock();
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -07001075 if (c != 0 && dataPtr != nullptr) {
1076 native_handle_t* handle = nullptr;
1077
1078 // Check if dataPtr contains a VideoNativeHandleMetadata.
1079 if (dataPtr->size() == sizeof(VideoNativeHandleMetadata)) {
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001080 // TODO: Using unsecurePointer() has some associated security pitfalls
1081 // (see declaration for details).
1082 // Either document why it is safe in this case or address the
1083 // issue (e.g. by copying).
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -07001084 VideoNativeHandleMetadata *metadata =
Ytai Ben-Tsvi7dd39722019-09-05 15:14:30 -07001085 (VideoNativeHandleMetadata*)(dataPtr->unsecurePointer());
Chien-Yu Chen2d13b1d2016-04-28 12:11:20 -07001086 if (metadata->eType == kMetadataBufferTypeNativeHandleSource) {
1087 handle = metadata->pHandle;
1088 }
1089 }
1090
1091 // If dataPtr contains a native handle, send it via recordingFrameHandleCallbackTimestamp.
1092 if (handle != nullptr) {
1093 {
1094 Mutex::Autolock l(mAvailableCallbackBuffersLock);
1095 mAvailableCallbackBuffers.push_back(dataPtr);
1096 }
1097 c->recordingFrameHandleCallbackTimestamp(timestamp, handle);
1098 } else {
1099 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1100 }
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001101 }
1102}
1103
1104void CameraClient::copyFrameAndPostCopiedFrame(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001105 int32_t msgType, const sp<hardware::ICameraClient>& client,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001106 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1107 camera_frame_metadata_t *metadata) {
1108 LOG2("copyFrameAndPostCopiedFrame");
1109 // It is necessary to copy out of pmem before sending this to
1110 // the callback. For efficiency, reuse the same MemoryHeapBase
1111 // provided it's big enough. Don't allocate the memory or
1112 // perform the copy if there's no callback.
1113 // hold the preview lock while we grab a reference to the preview buffer
1114 sp<MemoryHeapBase> previewBuffer;
1115
1116 if (mPreviewBuffer == 0) {
1117 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1118 } else if (size > mPreviewBuffer->virtualSize()) {
1119 mPreviewBuffer.clear();
1120 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1121 }
1122 if (mPreviewBuffer == 0) {
1123 ALOGE("failed to allocate space for preview buffer");
1124 mLock.unlock();
1125 return;
1126 }
1127 previewBuffer = mPreviewBuffer;
1128
Ruben Brunk65e01f72014-08-29 17:25:13 -07001129 void* previewBufferBase = previewBuffer->base();
1130 void* heapBase = heap->base();
1131
1132 if (heapBase == MAP_FAILED) {
1133 ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__);
1134 mLock.unlock();
1135 return;
1136 } else if (previewBufferBase == MAP_FAILED) {
1137 ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__);
1138 mLock.unlock();
1139 return;
1140 }
1141
1142 memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001143
1144 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
1145 if (frame == 0) {
1146 ALOGE("failed to allocate space for frame callback");
1147 mLock.unlock();
1148 return;
1149 }
1150
1151 mLock.unlock();
1152 client->dataCallback(msgType, frame, metadata);
1153}
1154
1155int CameraClient::getOrientation(int degrees, bool mirror) {
1156 if (!mirror) {
1157 if (degrees == 0) return 0;
1158 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1159 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1160 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1161 } else { // Do mirror (horizontal flip)
1162 if (degrees == 0) { // FLIP_H and ROT_0
1163 return HAL_TRANSFORM_FLIP_H;
1164 } else if (degrees == 90) { // FLIP_H and ROT_90
1165 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1166 } else if (degrees == 180) { // FLIP_H and ROT_180
1167 return HAL_TRANSFORM_FLIP_V;
1168 } else if (degrees == 270) { // FLIP_H and ROT_270
1169 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1170 }
1171 }
1172 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1173 return -1;
1174}
1175
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001176status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) {
Chien-Yu Chen98a668f2015-12-18 14:10:33 -08001177 (void)bufferProducer;
Chien-Yu Chen8cca0752015-11-13 15:28:48 -08001178 ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__);
1179 return INVALID_OPERATION;
1180}
1181
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001182status_t CameraClient::setAudioRestriction(int mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001183 if (!isValidAudioRestriction(mode)) {
1184 ALOGE("%s: invalid audio restriction mode %d", __FUNCTION__, mode);
1185 return BAD_VALUE;
1186 }
1187
1188 Mutex::Autolock lock(mLock);
1189 if (checkPidAndHardware() != NO_ERROR) {
1190 return INVALID_OPERATION;
1191 }
1192 return BasicClient::setAudioRestriction(mode);
1193}
1194
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001195int32_t CameraClient::getGlobalAudioRestriction() {
1196 Mutex::Autolock lock(mLock);
1197 if (checkPidAndHardware() != NO_ERROR) {
1198 return INVALID_OPERATION;
1199 }
1200 return BasicClient::getServiceAudioRestriction();
1201}
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001202
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001203// API1->Device1 does not support this feature
1204status_t CameraClient::setRotateAndCropOverride(uint8_t /*rotateAndCrop*/) {
1205 return OK;
1206}
1207
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -07001208}; // namespace android