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