Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | 05d19b0 | 2017-02-28 16:28:19 -0800 | [diff] [blame] | 20 | #include <cutils/atomic.h> |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 22 | #include <gui/Surface.h> |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 23 | #include <media/hardware/HardwareAPI.h> |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 24 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 25 | #include "api1/CameraClient.h" |
| 26 | #include "device1/CameraHardwareInterface.h" |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 27 | #include "CameraService.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 32 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
| 33 | |
| 34 | static int getCallingPid() { |
| 35 | return IPCThreadState::self()->getCallingPid(); |
| 36 | } |
| 37 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 38 | CameraClient::CameraClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 39 | const sp<hardware::ICameraClient>& cameraClient, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 40 | const String16& clientPackageName, |
| 41 | int cameraId, int cameraFacing, |
| 42 | int clientPid, int clientUid, |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 43 | int servicePid, bool legacyMode): |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 44 | Client(cameraService, cameraClient, clientPackageName, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 45 | String8::format("%d", cameraId), cameraFacing, clientPid, |
| 46 | clientUid, servicePid) |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 47 | { |
| 48 | int callingPid = getCallingPid(); |
| 49 | LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId); |
| 50 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 51 | mHardware = NULL; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 52 | mMsgEnabled = 0; |
| 53 | mSurface = 0; |
| 54 | mPreviewWindow = 0; |
| 55 | mDestructionStarted = false; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 56 | |
| 57 | // Callback is disabled by default |
| 58 | mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP; |
| 59 | mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT); |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 60 | mLegacyMode = legacyMode; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 61 | mPlayShutterSound = true; |
| 62 | LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId); |
| 63 | } |
| 64 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 65 | status_t CameraClient::initialize(sp<CameraProviderManager> manager) { |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 66 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 67 | status_t res; |
| 68 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 69 | LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId); |
| 70 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 71 | // Verify ops permissions |
| 72 | res = startCameraOps(); |
| 73 | if (res != OK) { |
| 74 | return res; |
| 75 | } |
| 76 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 77 | char camera_device_name[10]; |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 78 | snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId); |
| 79 | |
| 80 | mHardware = new CameraHardwareInterface(camera_device_name); |
Emilian Peev | f53f66e | 2017-04-11 14:29:43 +0100 | [diff] [blame] | 81 | res = mHardware->initialize(manager); |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 82 | if (res != OK) { |
| 83 | ALOGE("%s: Camera %d: unable to initialize device: %s (%d)", |
| 84 | __FUNCTION__, mCameraId, strerror(-res), res); |
Igor Murashkin | 44f120f | 2012-10-09 14:45:37 -0700 | [diff] [blame] | 85 | mHardware.clear(); |
Zhijun He | b10cdad | 2014-06-16 16:38:35 -0700 | [diff] [blame] | 86 | return res; |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | mHardware->setCallbacks(notifyCallback, |
| 90 | dataCallback, |
| 91 | dataCallbackTimestamp, |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 92 | handleCallbackTimestampBatch, |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 93 | (void *)(uintptr_t)mCameraId); |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 94 | |
| 95 | // Enable zoom, error, focus, and metadata messages by default |
| 96 | enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS | |
| 97 | CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE); |
| 98 | |
| 99 | LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId); |
| 100 | return OK; |
| 101 | } |
| 102 | |
| 103 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 104 | // tear down the client |
| 105 | CameraClient::~CameraClient() { |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 106 | mDestructionStarted = true; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 107 | int callingPid = getCallingPid(); |
| 108 | LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this); |
| 109 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 110 | disconnect(); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 111 | LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this); |
| 112 | } |
| 113 | |
| 114 | status_t CameraClient::dump(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 115 | return BasicClient::dump(fd, args); |
| 116 | } |
| 117 | |
| 118 | status_t CameraClient::dumpClient(int fd, const Vector<String16>& args) { |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 119 | const size_t SIZE = 256; |
| 120 | char buffer[SIZE]; |
| 121 | |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 122 | size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) with UID %d\n", |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 123 | mCameraId, |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 124 | (getRemoteCallback() != NULL ? |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 125 | IInterface::asBinder(getRemoteCallback()).get() : NULL), |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 126 | mClientUid); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 127 | len = (len > SIZE - 1) ? SIZE - 1 : len; |
| 128 | write(fd, buffer, len); |
Igor Murashkin | fcf5fea | 2014-09-11 14:43:24 -0700 | [diff] [blame] | 129 | |
| 130 | len = snprintf(buffer, SIZE, "Latest set parameters:\n"); |
| 131 | len = (len > SIZE - 1) ? SIZE - 1 : len; |
| 132 | write(fd, buffer, len); |
| 133 | |
| 134 | mLatestSetParameters.dump(fd, args); |
| 135 | |
| 136 | const char *enddump = "\n\n"; |
| 137 | write(fd, enddump, strlen(enddump)); |
| 138 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 139 | return mHardware->dump(fd, args); |
| 140 | } |
| 141 | |
| 142 | // ---------------------------------------------------------------------------- |
| 143 | |
| 144 | status_t CameraClient::checkPid() const { |
| 145 | int callingPid = getCallingPid(); |
Eino-Ville Talvala | c037920 | 2012-10-09 22:16:58 -0700 | [diff] [blame] | 146 | if (callingPid == mClientPid) return NO_ERROR; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 147 | |
| 148 | ALOGW("attempt to use a locked camera from a different process" |
| 149 | " (old pid %d, new pid %d)", mClientPid, callingPid); |
| 150 | return EBUSY; |
| 151 | } |
| 152 | |
| 153 | status_t CameraClient::checkPidAndHardware() const { |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 154 | if (mHardware == 0) { |
| 155 | ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid()); |
| 156 | return INVALID_OPERATION; |
| 157 | } |
Eino-Ville Talvala | 6192b89 | 2016-04-04 12:31:18 -0700 | [diff] [blame] | 158 | status_t result = checkPid(); |
| 159 | if (result != NO_ERROR) return result; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 160 | return NO_ERROR; |
| 161 | } |
| 162 | |
| 163 | status_t CameraClient::lock() { |
| 164 | int callingPid = getCallingPid(); |
| 165 | LOG1("lock (pid %d)", callingPid); |
| 166 | Mutex::Autolock lock(mLock); |
| 167 | |
| 168 | // lock camera to this client if the the camera is unlocked |
| 169 | if (mClientPid == 0) { |
| 170 | mClientPid = callingPid; |
| 171 | return NO_ERROR; |
| 172 | } |
| 173 | |
| 174 | // returns NO_ERROR if the client already owns the camera, EBUSY otherwise |
| 175 | return checkPid(); |
| 176 | } |
| 177 | |
| 178 | status_t CameraClient::unlock() { |
| 179 | int callingPid = getCallingPid(); |
| 180 | LOG1("unlock (pid %d)", callingPid); |
| 181 | Mutex::Autolock lock(mLock); |
| 182 | |
| 183 | // allow anyone to use camera (after they lock the camera) |
| 184 | status_t result = checkPid(); |
| 185 | if (result == NO_ERROR) { |
| 186 | if (mHardware->recordingEnabled()) { |
| 187 | ALOGE("Not allowed to unlock camera during recording."); |
| 188 | return INVALID_OPERATION; |
| 189 | } |
| 190 | mClientPid = 0; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 191 | LOG1("clear mRemoteCallback (pid %d)", callingPid); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 192 | // we need to remove the reference to ICameraClient so that when the app |
| 193 | // goes away, the reference count goes to 0. |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 194 | mRemoteCallback.clear(); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 195 | } |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | // connect a new client to the camera |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 200 | status_t CameraClient::connect(const sp<hardware::ICameraClient>& client) { |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 201 | int callingPid = getCallingPid(); |
| 202 | LOG1("connect E (pid %d)", callingPid); |
| 203 | Mutex::Autolock lock(mLock); |
| 204 | |
| 205 | if (mClientPid != 0 && checkPid() != NO_ERROR) { |
| 206 | ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)", |
| 207 | mClientPid, callingPid); |
| 208 | return EBUSY; |
| 209 | } |
| 210 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 211 | if (mRemoteCallback != 0 && |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 212 | (IInterface::asBinder(client) == IInterface::asBinder(mRemoteCallback))) { |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 213 | LOG1("Connect to the same client"); |
| 214 | return NO_ERROR; |
| 215 | } |
| 216 | |
| 217 | mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP; |
| 218 | mClientPid = callingPid; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 219 | mRemoteCallback = client; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 220 | |
| 221 | LOG1("connect X (pid %d)", callingPid); |
| 222 | return NO_ERROR; |
| 223 | } |
| 224 | |
| 225 | static void disconnectWindow(const sp<ANativeWindow>& window) { |
| 226 | if (window != 0) { |
| 227 | status_t result = native_window_api_disconnect(window.get(), |
| 228 | NATIVE_WINDOW_API_CAMERA); |
| 229 | if (result != NO_ERROR) { |
| 230 | ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result), |
| 231 | result); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 236 | binder::Status CameraClient::disconnect() { |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 237 | int callingPid = getCallingPid(); |
| 238 | LOG1("disconnect E (pid %d)", callingPid); |
| 239 | Mutex::Autolock lock(mLock); |
| 240 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 241 | binder::Status res = binder::Status::ok(); |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 242 | // Allow both client and the cameraserver to disconnect at all times |
Eino-Ville Talvala | c037920 | 2012-10-09 22:16:58 -0700 | [diff] [blame] | 243 | if (callingPid != mClientPid && callingPid != mServicePid) { |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 244 | ALOGW("different client - don't disconnect"); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 245 | return res; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 248 | // Make sure disconnect() is done once and once only, whether it is called |
| 249 | // from the user directly, or called by the destructor. |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 250 | if (mHardware == 0) return res; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 251 | |
| 252 | LOG1("hardware teardown"); |
| 253 | // Before destroying mHardware, we must make sure it's in the |
| 254 | // idle state. |
| 255 | // Turn off all messages. |
| 256 | disableMsgType(CAMERA_MSG_ALL_MSGS); |
| 257 | mHardware->stopPreview(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 258 | sCameraService->updateProxyDeviceState( |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 259 | ICameraServiceProxy::CAMERA_STATE_IDLE, |
| 260 | String8::format("%d", mCameraId)); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 261 | mHardware->cancelPicture(); |
| 262 | // Release the hardware resources. |
| 263 | mHardware->release(); |
| 264 | |
| 265 | // Release the held ANativeWindow resources. |
| 266 | if (mPreviewWindow != 0) { |
| 267 | disconnectWindow(mPreviewWindow); |
| 268 | mPreviewWindow = 0; |
| 269 | mHardware->setPreviewWindow(mPreviewWindow); |
| 270 | } |
| 271 | mHardware.clear(); |
| 272 | |
| 273 | CameraService::Client::disconnect(); |
| 274 | |
| 275 | LOG1("disconnect X (pid %d)", callingPid); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 276 | |
| 277 | return res; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // ---------------------------------------------------------------------------- |
| 281 | |
| 282 | status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder, |
| 283 | const sp<ANativeWindow>& window) { |
| 284 | Mutex::Autolock lock(mLock); |
| 285 | status_t result = checkPidAndHardware(); |
| 286 | if (result != NO_ERROR) return result; |
| 287 | |
| 288 | // return if no change in surface. |
| 289 | if (binder == mSurface) { |
| 290 | return NO_ERROR; |
| 291 | } |
| 292 | |
| 293 | if (window != 0) { |
| 294 | result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA); |
| 295 | if (result != NO_ERROR) { |
| 296 | ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result), |
| 297 | result); |
| 298 | return result; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // If preview has been already started, register preview buffers now. |
| 303 | if (mHardware->previewEnabled()) { |
| 304 | if (window != 0) { |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 305 | mHardware->setPreviewScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 306 | mHardware->setPreviewTransform(mOrientation); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 307 | result = mHardware->setPreviewWindow(window); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (result == NO_ERROR) { |
| 312 | // Everything has succeeded. Disconnect the old window and remember the |
| 313 | // new window. |
| 314 | disconnectWindow(mPreviewWindow); |
| 315 | mSurface = binder; |
| 316 | mPreviewWindow = window; |
| 317 | } else { |
| 318 | // Something went wrong after we connected to the new window, so |
| 319 | // disconnect here. |
| 320 | disconnectWindow(window); |
| 321 | } |
| 322 | |
| 323 | return result; |
| 324 | } |
| 325 | |
Eino-Ville Talvala | 1ce7c34 | 2013-08-21 13:57:21 -0700 | [diff] [blame] | 326 | // set the buffer consumer that the preview will use |
| 327 | status_t CameraClient::setPreviewTarget( |
Andy McFadden | 8ba0102 | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 328 | const sp<IGraphicBufferProducer>& bufferProducer) { |
Eino-Ville Talvala | 1ce7c34 | 2013-08-21 13:57:21 -0700 | [diff] [blame] | 329 | LOG1("setPreviewTarget(%p) (pid %d)", bufferProducer.get(), |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 330 | getCallingPid()); |
| 331 | |
| 332 | sp<IBinder> binder; |
| 333 | sp<ANativeWindow> window; |
Andy McFadden | 8ba0102 | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 334 | if (bufferProducer != 0) { |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 335 | binder = IInterface::asBinder(bufferProducer); |
Eino-Ville Talvala | 1ce7c34 | 2013-08-21 13:57:21 -0700 | [diff] [blame] | 336 | // Using controlledByApp flag to ensure that the buffer queue remains in |
| 337 | // async mode for the old camera API, where many applications depend |
| 338 | // on that behavior. |
| 339 | window = new Surface(bufferProducer, /*controlledByApp*/ true); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 340 | } |
| 341 | return setPreviewWindow(binder, window); |
| 342 | } |
| 343 | |
| 344 | // set the preview callback flag to affect how the received frames from |
| 345 | // preview are handled. |
| 346 | void CameraClient::setPreviewCallbackFlag(int callback_flag) { |
| 347 | LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid()); |
| 348 | Mutex::Autolock lock(mLock); |
| 349 | if (checkPidAndHardware() != NO_ERROR) return; |
| 350 | |
| 351 | mPreviewCallbackFlag = callback_flag; |
| 352 | if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) { |
| 353 | enableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 354 | } else { |
| 355 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 356 | } |
| 357 | } |
| 358 | |
Eino-Ville Talvala | 3ee3550 | 2013-04-02 15:45:11 -0700 | [diff] [blame] | 359 | status_t CameraClient::setPreviewCallbackTarget( |
| 360 | const sp<IGraphicBufferProducer>& callbackProducer) { |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 361 | (void)callbackProducer; |
Eino-Ville Talvala | 3ee3550 | 2013-04-02 15:45:11 -0700 | [diff] [blame] | 362 | ALOGE("%s: Unimplemented!", __FUNCTION__); |
| 363 | return INVALID_OPERATION; |
| 364 | } |
| 365 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 366 | // start preview mode |
| 367 | status_t CameraClient::startPreview() { |
| 368 | LOG1("startPreview (pid %d)", getCallingPid()); |
| 369 | return startCameraMode(CAMERA_PREVIEW_MODE); |
| 370 | } |
| 371 | |
| 372 | // start recording mode |
| 373 | status_t CameraClient::startRecording() { |
| 374 | LOG1("startRecording (pid %d)", getCallingPid()); |
| 375 | return startCameraMode(CAMERA_RECORDING_MODE); |
| 376 | } |
| 377 | |
| 378 | // start preview or recording |
| 379 | status_t CameraClient::startCameraMode(camera_mode mode) { |
| 380 | LOG1("startCameraMode(%d)", mode); |
| 381 | Mutex::Autolock lock(mLock); |
| 382 | status_t result = checkPidAndHardware(); |
| 383 | if (result != NO_ERROR) return result; |
| 384 | |
| 385 | switch(mode) { |
| 386 | case CAMERA_PREVIEW_MODE: |
| 387 | if (mSurface == 0 && mPreviewWindow == 0) { |
| 388 | LOG1("mSurface is not set yet."); |
| 389 | // still able to start preview in this case. |
| 390 | } |
| 391 | return startPreviewMode(); |
| 392 | case CAMERA_RECORDING_MODE: |
| 393 | if (mSurface == 0 && mPreviewWindow == 0) { |
| 394 | ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode."); |
| 395 | return INVALID_OPERATION; |
| 396 | } |
| 397 | return startRecordingMode(); |
| 398 | default: |
| 399 | return UNKNOWN_ERROR; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | status_t CameraClient::startPreviewMode() { |
| 404 | LOG1("startPreviewMode"); |
| 405 | status_t result = NO_ERROR; |
| 406 | |
| 407 | // if preview has been enabled, nothing needs to be done |
| 408 | if (mHardware->previewEnabled()) { |
| 409 | return NO_ERROR; |
| 410 | } |
| 411 | |
| 412 | if (mPreviewWindow != 0) { |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 413 | mHardware->setPreviewScalingMode( |
| 414 | NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
| 415 | mHardware->setPreviewTransform(mOrientation); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 416 | } |
| 417 | mHardware->setPreviewWindow(mPreviewWindow); |
| 418 | result = mHardware->startPreview(); |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 419 | if (result == NO_ERROR) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 420 | sCameraService->updateProxyDeviceState( |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 421 | ICameraServiceProxy::CAMERA_STATE_ACTIVE, |
| 422 | String8::format("%d", mCameraId)); |
| 423 | } |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 424 | return result; |
| 425 | } |
| 426 | |
| 427 | status_t CameraClient::startRecordingMode() { |
| 428 | LOG1("startRecordingMode"); |
| 429 | status_t result = NO_ERROR; |
| 430 | |
| 431 | // if recording has been enabled, nothing needs to be done |
| 432 | if (mHardware->recordingEnabled()) { |
| 433 | return NO_ERROR; |
| 434 | } |
| 435 | |
| 436 | // if preview has not been started, start preview first |
| 437 | if (!mHardware->previewEnabled()) { |
| 438 | result = startPreviewMode(); |
| 439 | if (result != NO_ERROR) { |
| 440 | return result; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // start recording mode |
| 445 | enableMsgType(CAMERA_MSG_VIDEO_FRAME); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 446 | sCameraService->playSound(CameraService::SOUND_RECORDING_START); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 447 | result = mHardware->startRecording(); |
| 448 | if (result != NO_ERROR) { |
| 449 | ALOGE("mHardware->startRecording() failed with status %d", result); |
| 450 | } |
| 451 | return result; |
| 452 | } |
| 453 | |
| 454 | // stop preview mode |
| 455 | void CameraClient::stopPreview() { |
| 456 | LOG1("stopPreview (pid %d)", getCallingPid()); |
| 457 | Mutex::Autolock lock(mLock); |
| 458 | if (checkPidAndHardware() != NO_ERROR) return; |
| 459 | |
| 460 | |
| 461 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 462 | mHardware->stopPreview(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 463 | sCameraService->updateProxyDeviceState( |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 464 | ICameraServiceProxy::CAMERA_STATE_IDLE, |
| 465 | String8::format("%d", mCameraId)); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 466 | mPreviewBuffer.clear(); |
| 467 | } |
| 468 | |
| 469 | // stop recording mode |
| 470 | void CameraClient::stopRecording() { |
| 471 | LOG1("stopRecording (pid %d)", getCallingPid()); |
Emilian Peev | 698f0a7 | 2017-04-05 11:20:09 +0100 | [diff] [blame] | 472 | { |
| 473 | Mutex::Autolock lock(mLock); |
| 474 | if (checkPidAndHardware() != NO_ERROR) return; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 475 | |
Emilian Peev | 698f0a7 | 2017-04-05 11:20:09 +0100 | [diff] [blame] | 476 | disableMsgType(CAMERA_MSG_VIDEO_FRAME); |
| 477 | mHardware->stopRecording(); |
| 478 | sCameraService->playSound(CameraService::SOUND_RECORDING_STOP); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 479 | |
Emilian Peev | 698f0a7 | 2017-04-05 11:20:09 +0100 | [diff] [blame] | 480 | mPreviewBuffer.clear(); |
| 481 | } |
| 482 | |
| 483 | { |
| 484 | Mutex::Autolock l(mAvailableCallbackBuffersLock); |
| 485 | if (!mAvailableCallbackBuffers.empty()) { |
| 486 | mAvailableCallbackBuffers.clear(); |
| 487 | } |
| 488 | } |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | // release a recording frame |
| 492 | void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) { |
| 493 | Mutex::Autolock lock(mLock); |
| 494 | if (checkPidAndHardware() != NO_ERROR) return; |
Chien-Yu Chen | 42f2707 | 2016-01-11 17:39:17 -0800 | [diff] [blame] | 495 | if (mem == nullptr) { |
| 496 | android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26164272", |
| 497 | IPCThreadState::self()->getCallingUid(), nullptr, 0); |
| 498 | return; |
| 499 | } |
| 500 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 501 | mHardware->releaseRecordingFrame(mem); |
| 502 | } |
| 503 | |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 504 | void CameraClient::releaseRecordingFrameHandle(native_handle_t *handle) { |
| 505 | if (handle == nullptr) return; |
| 506 | |
| 507 | sp<IMemory> dataPtr; |
| 508 | { |
| 509 | Mutex::Autolock l(mAvailableCallbackBuffersLock); |
| 510 | if (!mAvailableCallbackBuffers.empty()) { |
| 511 | dataPtr = mAvailableCallbackBuffers.back(); |
| 512 | mAvailableCallbackBuffers.pop_back(); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (dataPtr == nullptr) { |
| 517 | ALOGE("%s: %d: No callback buffer available. Dropping a native handle.", __FUNCTION__, |
| 518 | __LINE__); |
| 519 | native_handle_close(handle); |
| 520 | native_handle_delete(handle); |
| 521 | return; |
| 522 | } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) { |
| 523 | ALOGE("%s: %d: Callback buffer size doesn't match VideoNativeHandleMetadata", __FUNCTION__, |
| 524 | __LINE__); |
| 525 | native_handle_close(handle); |
| 526 | native_handle_delete(handle); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer()); |
| 531 | metadata->eType = kMetadataBufferTypeNativeHandleSource; |
| 532 | metadata->pHandle = handle; |
| 533 | |
| 534 | mHardware->releaseRecordingFrame(dataPtr); |
| 535 | } |
| 536 | |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 537 | void CameraClient::releaseRecordingFrameHandleBatch(const std::vector<native_handle_t*>& handles) { |
| 538 | size_t n = handles.size(); |
| 539 | std::vector<sp<IMemory>> frames; |
| 540 | frames.reserve(n); |
| 541 | bool error = false; |
| 542 | for (auto& handle : handles) { |
| 543 | sp<IMemory> dataPtr; |
| 544 | { |
| 545 | Mutex::Autolock l(mAvailableCallbackBuffersLock); |
| 546 | if (!mAvailableCallbackBuffers.empty()) { |
| 547 | dataPtr = mAvailableCallbackBuffers.back(); |
| 548 | mAvailableCallbackBuffers.pop_back(); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (dataPtr == nullptr) { |
| 553 | ALOGE("%s: %d: No callback buffer available. Dropping frames.", __FUNCTION__, |
| 554 | __LINE__); |
| 555 | error = true; |
| 556 | break; |
| 557 | } else if (dataPtr->size() != sizeof(VideoNativeHandleMetadata)) { |
| 558 | ALOGE("%s: %d: Callback buffer must be VideoNativeHandleMetadata", __FUNCTION__, |
| 559 | __LINE__); |
| 560 | error = true; |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | VideoNativeHandleMetadata *metadata = (VideoNativeHandleMetadata*)(dataPtr->pointer()); |
| 565 | metadata->eType = kMetadataBufferTypeNativeHandleSource; |
| 566 | metadata->pHandle = handle; |
| 567 | frames.push_back(dataPtr); |
| 568 | } |
| 569 | |
| 570 | if (error) { |
| 571 | for (auto& handle : handles) { |
| 572 | native_handle_close(handle); |
| 573 | native_handle_delete(handle); |
| 574 | } |
| 575 | } else { |
| 576 | mHardware->releaseRecordingFrameBatch(frames); |
| 577 | } |
| 578 | return; |
| 579 | } |
| 580 | |
Chien-Yu Chen | 8cca075 | 2015-11-13 15:28:48 -0800 | [diff] [blame] | 581 | status_t CameraClient::setVideoBufferMode(int32_t videoBufferMode) { |
| 582 | LOG1("setVideoBufferMode: %d", videoBufferMode); |
| 583 | bool enableMetadataInBuffers = false; |
| 584 | |
| 585 | if (videoBufferMode == VIDEO_BUFFER_MODE_DATA_CALLBACK_METADATA) { |
| 586 | enableMetadataInBuffers = true; |
| 587 | } else if (videoBufferMode != VIDEO_BUFFER_MODE_DATA_CALLBACK_YUV) { |
| 588 | ALOGE("%s: %d: videoBufferMode %d is not supported.", __FUNCTION__, __LINE__, |
| 589 | videoBufferMode); |
| 590 | return BAD_VALUE; |
| 591 | } |
| 592 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 593 | Mutex::Autolock lock(mLock); |
| 594 | if (checkPidAndHardware() != NO_ERROR) { |
| 595 | return UNKNOWN_ERROR; |
| 596 | } |
Chien-Yu Chen | 8cca075 | 2015-11-13 15:28:48 -0800 | [diff] [blame] | 597 | |
| 598 | return mHardware->storeMetaDataInBuffers(enableMetadataInBuffers); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | bool CameraClient::previewEnabled() { |
| 602 | LOG1("previewEnabled (pid %d)", getCallingPid()); |
| 603 | |
| 604 | Mutex::Autolock lock(mLock); |
| 605 | if (checkPidAndHardware() != NO_ERROR) return false; |
| 606 | return mHardware->previewEnabled(); |
| 607 | } |
| 608 | |
| 609 | bool CameraClient::recordingEnabled() { |
| 610 | LOG1("recordingEnabled (pid %d)", getCallingPid()); |
| 611 | |
| 612 | Mutex::Autolock lock(mLock); |
| 613 | if (checkPidAndHardware() != NO_ERROR) return false; |
| 614 | return mHardware->recordingEnabled(); |
| 615 | } |
| 616 | |
| 617 | status_t CameraClient::autoFocus() { |
| 618 | LOG1("autoFocus (pid %d)", getCallingPid()); |
| 619 | |
| 620 | Mutex::Autolock lock(mLock); |
| 621 | status_t result = checkPidAndHardware(); |
| 622 | if (result != NO_ERROR) return result; |
| 623 | |
| 624 | return mHardware->autoFocus(); |
| 625 | } |
| 626 | |
| 627 | status_t CameraClient::cancelAutoFocus() { |
| 628 | LOG1("cancelAutoFocus (pid %d)", getCallingPid()); |
| 629 | |
| 630 | Mutex::Autolock lock(mLock); |
| 631 | status_t result = checkPidAndHardware(); |
| 632 | if (result != NO_ERROR) return result; |
| 633 | |
| 634 | return mHardware->cancelAutoFocus(); |
| 635 | } |
| 636 | |
| 637 | // take a picture - image is returned in callback |
| 638 | status_t CameraClient::takePicture(int msgType) { |
| 639 | LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType); |
| 640 | |
| 641 | Mutex::Autolock lock(mLock); |
| 642 | status_t result = checkPidAndHardware(); |
| 643 | if (result != NO_ERROR) return result; |
| 644 | |
| 645 | if ((msgType & CAMERA_MSG_RAW_IMAGE) && |
| 646 | (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) { |
| 647 | ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY" |
| 648 | " cannot be both enabled"); |
| 649 | return BAD_VALUE; |
| 650 | } |
| 651 | |
| 652 | // We only accept picture related message types |
| 653 | // and ignore other types of messages for takePicture(). |
| 654 | int picMsgType = msgType |
| 655 | & (CAMERA_MSG_SHUTTER | |
| 656 | CAMERA_MSG_POSTVIEW_FRAME | |
| 657 | CAMERA_MSG_RAW_IMAGE | |
| 658 | CAMERA_MSG_RAW_IMAGE_NOTIFY | |
| 659 | CAMERA_MSG_COMPRESSED_IMAGE); |
| 660 | |
| 661 | enableMsgType(picMsgType); |
| 662 | |
| 663 | return mHardware->takePicture(); |
| 664 | } |
| 665 | |
| 666 | // set preview/capture parameters - key/value pairs |
| 667 | status_t CameraClient::setParameters(const String8& params) { |
| 668 | LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string()); |
| 669 | |
| 670 | Mutex::Autolock lock(mLock); |
| 671 | status_t result = checkPidAndHardware(); |
| 672 | if (result != NO_ERROR) return result; |
| 673 | |
Igor Murashkin | fcf5fea | 2014-09-11 14:43:24 -0700 | [diff] [blame] | 674 | mLatestSetParameters = CameraParameters(params); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 675 | CameraParameters p(params); |
| 676 | return mHardware->setParameters(p); |
| 677 | } |
| 678 | |
| 679 | // get preview/capture parameters - key/value pairs |
| 680 | String8 CameraClient::getParameters() const { |
| 681 | Mutex::Autolock lock(mLock); |
Igor Murashkin | ebe865b | 2014-08-07 17:07:28 -0700 | [diff] [blame] | 682 | // The camera service can unconditionally get the parameters at all times |
| 683 | if (getCallingPid() != mServicePid && checkPidAndHardware() != NO_ERROR) return String8(); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 684 | |
| 685 | String8 params(mHardware->getParameters().flatten()); |
| 686 | LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string()); |
| 687 | return params; |
| 688 | } |
| 689 | |
| 690 | // enable shutter sound |
| 691 | status_t CameraClient::enableShutterSound(bool enable) { |
| 692 | LOG1("enableShutterSound (pid %d)", getCallingPid()); |
| 693 | |
| 694 | status_t result = checkPidAndHardware(); |
| 695 | if (result != NO_ERROR) return result; |
| 696 | |
| 697 | if (enable) { |
| 698 | mPlayShutterSound = true; |
| 699 | return OK; |
| 700 | } |
| 701 | |
Igor Murashkin | a858ea0 | 2014-08-19 14:53:08 -0700 | [diff] [blame] | 702 | // the camera2 api legacy mode can unconditionally disable the shutter sound |
| 703 | if (mLegacyMode) { |
| 704 | ALOGV("%s: Disable shutter sound in legacy mode", __FUNCTION__); |
| 705 | mPlayShutterSound = false; |
| 706 | return OK; |
| 707 | } |
| 708 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 709 | // Disabling shutter sound may not be allowed. In that case only |
| 710 | // allow the mediaserver process to disable the sound. |
| 711 | char value[PROPERTY_VALUE_MAX]; |
| 712 | property_get("ro.camera.sound.forced", value, "0"); |
| 713 | if (strcmp(value, "0") != 0) { |
| 714 | // Disabling shutter sound is not allowed. Deny if the current |
| 715 | // process is not mediaserver. |
| 716 | if (getCallingPid() != getpid()) { |
| 717 | ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid()); |
| 718 | return PERMISSION_DENIED; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | mPlayShutterSound = false; |
| 723 | return OK; |
| 724 | } |
| 725 | |
| 726 | status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) { |
| 727 | LOG1("sendCommand (pid %d)", getCallingPid()); |
| 728 | int orientation; |
| 729 | Mutex::Autolock lock(mLock); |
| 730 | status_t result = checkPidAndHardware(); |
| 731 | if (result != NO_ERROR) return result; |
| 732 | |
| 733 | if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) { |
| 734 | // Mirror the preview if the camera is front-facing. |
| 735 | orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT); |
| 736 | if (orientation == -1) return BAD_VALUE; |
| 737 | |
| 738 | if (mOrientation != orientation) { |
| 739 | mOrientation = orientation; |
| 740 | if (mPreviewWindow != 0) { |
Eino-Ville Talvala | f735117 | 2016-02-09 12:13:57 -0800 | [diff] [blame] | 741 | mHardware->setPreviewTransform(mOrientation); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | return OK; |
| 745 | } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) { |
| 746 | switch (arg1) { |
| 747 | case 0: |
Eino-Ville Talvala | c5268e8 | 2012-09-11 11:01:18 -0700 | [diff] [blame] | 748 | return enableShutterSound(false); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 749 | case 1: |
Eino-Ville Talvala | c5268e8 | 2012-09-11 11:01:18 -0700 | [diff] [blame] | 750 | return enableShutterSound(true); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 751 | default: |
| 752 | return BAD_VALUE; |
| 753 | } |
| 754 | return OK; |
| 755 | } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 756 | sCameraService->playSound(CameraService::SOUND_RECORDING_START); |
James Dong | 983cf23 | 2012-08-01 16:39:55 -0700 | [diff] [blame] | 757 | } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) { |
| 758 | // Silently ignore this command |
| 759 | return INVALID_OPERATION; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 760 | } else if (cmd == CAMERA_CMD_PING) { |
| 761 | // If mHardware is 0, checkPidAndHardware will return error. |
| 762 | return OK; |
| 763 | } |
| 764 | |
| 765 | return mHardware->sendCommand(cmd, arg1, arg2); |
| 766 | } |
| 767 | |
| 768 | // ---------------------------------------------------------------------------- |
| 769 | |
| 770 | void CameraClient::enableMsgType(int32_t msgType) { |
| 771 | android_atomic_or(msgType, &mMsgEnabled); |
| 772 | mHardware->enableMsgType(msgType); |
| 773 | } |
| 774 | |
| 775 | void CameraClient::disableMsgType(int32_t msgType) { |
| 776 | android_atomic_and(~msgType, &mMsgEnabled); |
| 777 | mHardware->disableMsgType(msgType); |
| 778 | } |
| 779 | |
| 780 | #define CHECK_MESSAGE_INTERVAL 10 // 10ms |
| 781 | bool CameraClient::lockIfMessageWanted(int32_t msgType) { |
| 782 | int sleepCount = 0; |
| 783 | while (mMsgEnabled & msgType) { |
| 784 | if (mLock.tryLock() == NO_ERROR) { |
| 785 | if (sleepCount > 0) { |
| 786 | LOG1("lockIfMessageWanted(%d): waited for %d ms", |
| 787 | msgType, sleepCount * CHECK_MESSAGE_INTERVAL); |
| 788 | } |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 789 | |
| 790 | // If messages are no longer enabled after acquiring lock, release and drop message |
| 791 | if ((mMsgEnabled & msgType) == 0) { |
| 792 | mLock.unlock(); |
| 793 | break; |
| 794 | } |
| 795 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 796 | return true; |
| 797 | } |
| 798 | if (sleepCount++ == 0) { |
| 799 | LOG1("lockIfMessageWanted(%d): enter sleep", msgType); |
| 800 | } |
| 801 | usleep(CHECK_MESSAGE_INTERVAL * 1000); |
| 802 | } |
| 803 | ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType); |
| 804 | return false; |
| 805 | } |
| 806 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 807 | sp<CameraClient> CameraClient::getClientFromCookie(void* user) { |
| 808 | String8 cameraId = String8::format("%d", (int)(intptr_t) user); |
| 809 | auto clientDescriptor = sCameraService->mActiveClientManager.get(cameraId); |
| 810 | if (clientDescriptor != nullptr) { |
| 811 | return sp<CameraClient>{ |
| 812 | static_cast<CameraClient*>(clientDescriptor->getValue().get())}; |
| 813 | } |
| 814 | return sp<CameraClient>{nullptr}; |
| 815 | } |
| 816 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 817 | // Callback messages can be dispatched to internal handlers or pass to our |
| 818 | // client's callback functions, depending on the message type. |
| 819 | // |
| 820 | // notifyCallback: |
| 821 | // CAMERA_MSG_SHUTTER handleShutter |
| 822 | // (others) c->notifyCallback |
| 823 | // dataCallback: |
| 824 | // CAMERA_MSG_PREVIEW_FRAME handlePreviewData |
| 825 | // CAMERA_MSG_POSTVIEW_FRAME handlePostview |
| 826 | // CAMERA_MSG_RAW_IMAGE handleRawPicture |
| 827 | // CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture |
| 828 | // (others) c->dataCallback |
| 829 | // dataCallbackTimestamp |
| 830 | // (others) c->dataCallbackTimestamp |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 831 | |
| 832 | void CameraClient::notifyCallback(int32_t msgType, int32_t ext1, |
| 833 | int32_t ext2, void* user) { |
| 834 | LOG2("notifyCallback(%d)", msgType); |
| 835 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 836 | sp<CameraClient> client = getClientFromCookie(user); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 837 | if (client.get() == nullptr) return; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 838 | |
| 839 | if (!client->lockIfMessageWanted(msgType)) return; |
| 840 | |
| 841 | switch (msgType) { |
| 842 | case CAMERA_MSG_SHUTTER: |
| 843 | // ext1 is the dimension of the yuv picture. |
| 844 | client->handleShutter(); |
| 845 | break; |
| 846 | default: |
| 847 | client->handleGenericNotify(msgType, ext1, ext2); |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | void CameraClient::dataCallback(int32_t msgType, |
| 853 | const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) { |
| 854 | LOG2("dataCallback(%d)", msgType); |
| 855 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 856 | sp<CameraClient> client = getClientFromCookie(user); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 857 | if (client.get() == nullptr) return; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 858 | |
| 859 | if (!client->lockIfMessageWanted(msgType)) return; |
| 860 | if (dataPtr == 0 && metadata == NULL) { |
| 861 | ALOGE("Null data returned in data callback"); |
| 862 | client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 863 | return; |
| 864 | } |
| 865 | |
| 866 | switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) { |
| 867 | case CAMERA_MSG_PREVIEW_FRAME: |
| 868 | client->handlePreviewData(msgType, dataPtr, metadata); |
| 869 | break; |
| 870 | case CAMERA_MSG_POSTVIEW_FRAME: |
| 871 | client->handlePostview(dataPtr); |
| 872 | break; |
| 873 | case CAMERA_MSG_RAW_IMAGE: |
| 874 | client->handleRawPicture(dataPtr); |
| 875 | break; |
| 876 | case CAMERA_MSG_COMPRESSED_IMAGE: |
| 877 | client->handleCompressedPicture(dataPtr); |
| 878 | break; |
| 879 | default: |
| 880 | client->handleGenericData(msgType, dataPtr, metadata); |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | void CameraClient::dataCallbackTimestamp(nsecs_t timestamp, |
| 886 | int32_t msgType, const sp<IMemory>& dataPtr, void* user) { |
| 887 | LOG2("dataCallbackTimestamp(%d)", msgType); |
| 888 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 889 | sp<CameraClient> client = getClientFromCookie(user); |
Ruben Brunk | cc77671 | 2015-02-17 20:18:47 -0800 | [diff] [blame] | 890 | if (client.get() == nullptr) return; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 891 | |
| 892 | if (!client->lockIfMessageWanted(msgType)) return; |
| 893 | |
| 894 | if (dataPtr == 0) { |
| 895 | ALOGE("Null data returned in data with timestamp callback"); |
| 896 | client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | client->handleGenericDataTimestamp(timestamp, msgType, dataPtr); |
| 901 | } |
| 902 | |
Yin-Chia Yeh | b5df547 | 2017-03-20 19:32:19 -0700 | [diff] [blame] | 903 | void CameraClient::handleCallbackTimestampBatch( |
| 904 | int32_t msgType, const std::vector<HandleTimestampMessage>& msgs, void* user) { |
| 905 | LOG2("dataCallbackTimestampBatch"); |
| 906 | sp<CameraClient> client = getClientFromCookie(user); |
| 907 | if (client.get() == nullptr) return; |
| 908 | if (!client->lockIfMessageWanted(msgType)) return; |
| 909 | |
| 910 | sp<hardware::ICameraClient> c = client->mRemoteCallback; |
| 911 | client->mLock.unlock(); |
| 912 | if (c != 0 && msgs.size() > 0) { |
| 913 | size_t n = msgs.size(); |
| 914 | std::vector<nsecs_t> timestamps; |
| 915 | std::vector<native_handle_t*> handles; |
| 916 | timestamps.reserve(n); |
| 917 | handles.reserve(n); |
| 918 | for (auto& msg : msgs) { |
| 919 | native_handle_t* handle = nullptr; |
| 920 | if (msg.dataPtr->size() != sizeof(VideoNativeHandleMetadata)) { |
| 921 | ALOGE("%s: dataPtr does not contain VideoNativeHandleMetadata!", __FUNCTION__); |
| 922 | return; |
| 923 | } |
| 924 | VideoNativeHandleMetadata *metadata = |
| 925 | (VideoNativeHandleMetadata*)(msg.dataPtr->pointer()); |
| 926 | if (metadata->eType == kMetadataBufferTypeNativeHandleSource) { |
| 927 | handle = metadata->pHandle; |
| 928 | } |
| 929 | |
| 930 | if (handle == nullptr) { |
| 931 | ALOGE("%s: VideoNativeHandleMetadata type mismatch or null handle passed!", |
| 932 | __FUNCTION__); |
| 933 | return; |
| 934 | } |
| 935 | { |
| 936 | Mutex::Autolock l(client->mAvailableCallbackBuffersLock); |
| 937 | client->mAvailableCallbackBuffers.push_back(msg.dataPtr); |
| 938 | } |
| 939 | timestamps.push_back(msg.timestamp); |
| 940 | handles.push_back(handle); |
| 941 | } |
| 942 | c->recordingFrameHandleCallbackTimestampBatch(timestamps, handles); |
| 943 | } |
| 944 | } |
| 945 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 946 | // snapshot taken callback |
| 947 | void CameraClient::handleShutter(void) { |
| 948 | if (mPlayShutterSound) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 949 | sCameraService->playSound(CameraService::SOUND_SHUTTER); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 952 | sp<hardware::ICameraClient> c = mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 953 | if (c != 0) { |
| 954 | mLock.unlock(); |
| 955 | c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0); |
| 956 | if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return; |
| 957 | } |
| 958 | disableMsgType(CAMERA_MSG_SHUTTER); |
| 959 | |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 960 | // Shutters only happen in response to takePicture, so mark device as |
| 961 | // idle now, until preview is restarted |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 962 | sCameraService->updateProxyDeviceState( |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 963 | ICameraServiceProxy::CAMERA_STATE_IDLE, |
| 964 | String8::format("%d", mCameraId)); |
| 965 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 966 | mLock.unlock(); |
| 967 | } |
| 968 | |
| 969 | // preview callback - frame buffer update |
| 970 | void 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 Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 989 | sp<hardware::ICameraClient> c = mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 990 | |
| 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 |
| 1016 | void CameraClient::handlePostview(const sp<IMemory>& mem) { |
| 1017 | disableMsgType(CAMERA_MSG_POSTVIEW_FRAME); |
| 1018 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1019 | sp<hardware::ICameraClient> c = mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1020 | mLock.unlock(); |
| 1021 | if (c != 0) { |
| 1022 | c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | // picture callback - raw image ready |
| 1027 | void 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 Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1034 | sp<hardware::ICameraClient> c = mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1035 | mLock.unlock(); |
| 1036 | if (c != 0) { |
| 1037 | c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // picture callback - compressed picture ready |
| 1042 | void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) { |
| 1043 | disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE); |
| 1044 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1045 | sp<hardware::ICameraClient> c = mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1046 | mLock.unlock(); |
| 1047 | if (c != 0) { |
| 1048 | c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | void CameraClient::handleGenericNotify(int32_t msgType, |
| 1054 | int32_t ext1, int32_t ext2) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1055 | sp<hardware::ICameraClient> c = mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1056 | mLock.unlock(); |
| 1057 | if (c != 0) { |
| 1058 | c->notifyCallback(msgType, ext1, ext2); |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | void CameraClient::handleGenericData(int32_t msgType, |
| 1063 | const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1064 | sp<hardware::ICameraClient> c = mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1065 | mLock.unlock(); |
| 1066 | if (c != 0) { |
| 1067 | c->dataCallback(msgType, dataPtr, metadata); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp, |
| 1072 | int32_t msgType, const sp<IMemory>& dataPtr) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1073 | sp<hardware::ICameraClient> c = mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1074 | mLock.unlock(); |
Chien-Yu Chen | 2d13b1d | 2016-04-28 12:11:20 -0700 | [diff] [blame] | 1075 | 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)) { |
| 1080 | VideoNativeHandleMetadata *metadata = |
| 1081 | (VideoNativeHandleMetadata*)(dataPtr->pointer()); |
| 1082 | if (metadata->eType == kMetadataBufferTypeNativeHandleSource) { |
| 1083 | handle = metadata->pHandle; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | // If dataPtr contains a native handle, send it via recordingFrameHandleCallbackTimestamp. |
| 1088 | if (handle != nullptr) { |
| 1089 | { |
| 1090 | Mutex::Autolock l(mAvailableCallbackBuffersLock); |
| 1091 | mAvailableCallbackBuffers.push_back(dataPtr); |
| 1092 | } |
| 1093 | c->recordingFrameHandleCallbackTimestamp(timestamp, handle); |
| 1094 | } else { |
| 1095 | c->dataCallbackTimestamp(timestamp, msgType, dataPtr); |
| 1096 | } |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | void CameraClient::copyFrameAndPostCopiedFrame( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 1101 | int32_t msgType, const sp<hardware::ICameraClient>& client, |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1102 | const sp<IMemoryHeap>& heap, size_t offset, size_t size, |
| 1103 | camera_frame_metadata_t *metadata) { |
| 1104 | LOG2("copyFrameAndPostCopiedFrame"); |
| 1105 | // It is necessary to copy out of pmem before sending this to |
| 1106 | // the callback. For efficiency, reuse the same MemoryHeapBase |
| 1107 | // provided it's big enough. Don't allocate the memory or |
| 1108 | // perform the copy if there's no callback. |
| 1109 | // hold the preview lock while we grab a reference to the preview buffer |
| 1110 | sp<MemoryHeapBase> previewBuffer; |
| 1111 | |
| 1112 | if (mPreviewBuffer == 0) { |
| 1113 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1114 | } else if (size > mPreviewBuffer->virtualSize()) { |
| 1115 | mPreviewBuffer.clear(); |
| 1116 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1117 | } |
| 1118 | if (mPreviewBuffer == 0) { |
| 1119 | ALOGE("failed to allocate space for preview buffer"); |
| 1120 | mLock.unlock(); |
| 1121 | return; |
| 1122 | } |
| 1123 | previewBuffer = mPreviewBuffer; |
| 1124 | |
Ruben Brunk | 65e01f7 | 2014-08-29 17:25:13 -0700 | [diff] [blame] | 1125 | void* previewBufferBase = previewBuffer->base(); |
| 1126 | void* heapBase = heap->base(); |
| 1127 | |
| 1128 | if (heapBase == MAP_FAILED) { |
| 1129 | ALOGE("%s: Failed to mmap heap for preview frame.", __FUNCTION__); |
| 1130 | mLock.unlock(); |
| 1131 | return; |
| 1132 | } else if (previewBufferBase == MAP_FAILED) { |
| 1133 | ALOGE("%s: Failed to mmap preview buffer for preview frame.", __FUNCTION__); |
| 1134 | mLock.unlock(); |
| 1135 | return; |
| 1136 | } |
| 1137 | |
| 1138 | memcpy(previewBufferBase, (uint8_t *) heapBase + offset, size); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1139 | |
| 1140 | sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size); |
| 1141 | if (frame == 0) { |
| 1142 | ALOGE("failed to allocate space for frame callback"); |
| 1143 | mLock.unlock(); |
| 1144 | return; |
| 1145 | } |
| 1146 | |
| 1147 | mLock.unlock(); |
| 1148 | client->dataCallback(msgType, frame, metadata); |
| 1149 | } |
| 1150 | |
| 1151 | int CameraClient::getOrientation(int degrees, bool mirror) { |
| 1152 | if (!mirror) { |
| 1153 | if (degrees == 0) return 0; |
| 1154 | else if (degrees == 90) return HAL_TRANSFORM_ROT_90; |
| 1155 | else if (degrees == 180) return HAL_TRANSFORM_ROT_180; |
| 1156 | else if (degrees == 270) return HAL_TRANSFORM_ROT_270; |
| 1157 | } else { // Do mirror (horizontal flip) |
| 1158 | if (degrees == 0) { // FLIP_H and ROT_0 |
| 1159 | return HAL_TRANSFORM_FLIP_H; |
| 1160 | } else if (degrees == 90) { // FLIP_H and ROT_90 |
| 1161 | return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90; |
| 1162 | } else if (degrees == 180) { // FLIP_H and ROT_180 |
| 1163 | return HAL_TRANSFORM_FLIP_V; |
| 1164 | } else if (degrees == 270) { // FLIP_H and ROT_270 |
| 1165 | return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90; |
| 1166 | } |
| 1167 | } |
| 1168 | ALOGE("Invalid setDisplayOrientation degrees=%d", degrees); |
| 1169 | return -1; |
| 1170 | } |
| 1171 | |
Chien-Yu Chen | 8cca075 | 2015-11-13 15:28:48 -0800 | [diff] [blame] | 1172 | status_t CameraClient::setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer) { |
Chien-Yu Chen | 98a668f | 2015-12-18 14:10:33 -0800 | [diff] [blame] | 1173 | (void)bufferProducer; |
Chien-Yu Chen | 8cca075 | 2015-11-13 15:28:48 -0800 | [diff] [blame] | 1174 | ALOGE("%s: %d: CameraClient doesn't support setting a video target.", __FUNCTION__, __LINE__); |
| 1175 | return INVALID_OPERATION; |
| 1176 | } |
| 1177 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1178 | }; // namespace android |