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