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