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