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