Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_NDEBUG 0 |
| 18 | #define LOG_TAG "CameraBinderTests" |
| 19 | |
| 20 | #include <binder/IInterface.h> |
| 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/Parcel.h> |
| 23 | #include <binder/ProcessState.h> |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include <utils/List.h> |
| 27 | #include <utils/String8.h> |
| 28 | #include <utils/String16.h> |
| 29 | #include <utils/Condition.h> |
| 30 | #include <utils/Mutex.h> |
| 31 | #include <system/graphics.h> |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 32 | #include <hardware/camera3.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 33 | #include <hardware/gralloc.h> |
| 34 | |
| 35 | #include <camera/CameraMetadata.h> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 36 | #include <android/hardware/ICameraService.h> |
| 37 | #include <android/hardware/ICameraServiceListener.h> |
| 38 | #include <android/hardware/BnCameraServiceListener.h> |
| 39 | #include <android/hardware/camera2/ICameraDeviceUser.h> |
| 40 | #include <android/hardware/camera2/ICameraDeviceCallbacks.h> |
| 41 | #include <android/hardware/camera2/BnCameraDeviceCallbacks.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 42 | #include <camera/camera2/CaptureRequest.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 43 | #include <camera/camera2/OutputConfiguration.h> |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 44 | #include <camera/camera2/SessionConfiguration.h> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 45 | #include <camera/camera2/SubmitInfo.h> |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 46 | #include <camera/CameraUtils.h> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 47 | #include <camera/StringUtils.h> |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 48 | |
| 49 | #include <gui/BufferItemConsumer.h> |
| 50 | #include <gui/IGraphicBufferProducer.h> |
| 51 | #include <gui/Surface.h> |
| 52 | |
| 53 | #include <gtest/gtest.h> |
| 54 | #include <unistd.h> |
| 55 | #include <stdint.h> |
| 56 | #include <utility> |
| 57 | #include <vector> |
| 58 | #include <map> |
| 59 | #include <algorithm> |
| 60 | |
| 61 | using namespace android; |
Jiyong Park | 5a095ea | 2019-07-09 15:43:57 +0900 | [diff] [blame] | 62 | using ::android::hardware::ICameraService; |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 63 | using ::android::hardware::camera2::ICameraDeviceUser; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 64 | |
| 65 | #define ASSERT_NOT_NULL(x) \ |
| 66 | ASSERT_TRUE((x) != nullptr) |
| 67 | |
| 68 | #define SETUP_TIMEOUT 2000000000 // ns |
| 69 | #define IDLE_TIMEOUT 2000000000 // ns |
| 70 | |
| 71 | // Stub listener implementation |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 72 | class TestCameraServiceListener : public hardware::BnCameraServiceListener { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 73 | std::map<std::string, int32_t> mCameraTorchStatuses; |
| 74 | std::map<std::string, int32_t> mCameraStatuses; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 75 | mutable Mutex mLock; |
| 76 | mutable Condition mCondition; |
| 77 | mutable Condition mTorchCondition; |
| 78 | public: |
| 79 | virtual ~TestCameraServiceListener() {}; |
| 80 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 81 | virtual binder::Status onStatusChanged(int32_t status, const std::string& cameraId, |
| 82 | [[maybe_unused]] int32_t /*deviceId*/) override { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 83 | Mutex::Autolock l(mLock); |
| 84 | mCameraStatuses[cameraId] = status; |
| 85 | mCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 86 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 87 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 88 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 89 | virtual binder::Status onPhysicalCameraStatusChanged([[maybe_unused]] int32_t /*status*/, |
| 90 | [[maybe_unused]] const std::string& /*cameraId*/, |
| 91 | [[maybe_unused]] const std::string& /*physicalCameraId*/, |
| 92 | [[maybe_unused]] int32_t /*deviceId*/) override { |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 93 | // No op |
| 94 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 95 | } |
Shuzhen Wang | 4385816 | 2020-01-10 13:42:15 -0800 | [diff] [blame] | 96 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 97 | virtual binder::Status onTorchStatusChanged(int32_t status, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 98 | const std::string& cameraId, [[maybe_unused]] int32_t /*deviceId*/) override { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 99 | Mutex::Autolock l(mLock); |
| 100 | mCameraTorchStatuses[cameraId] = status; |
| 101 | mTorchCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 102 | return binder::Status::ok(); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 103 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 104 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 105 | virtual binder::Status onTorchStrengthLevelChanged( |
| 106 | [[maybe_unused]] const std::string& /*cameraId*/, |
| 107 | [[maybe_unused]] int32_t /*torchStrength*/, |
| 108 | [[maybe_unused]] int32_t /*deviceId*/) override { |
Rucha Katakwar | 3828452 | 2021-11-10 11:25:21 -0800 | [diff] [blame] | 109 | // No op |
| 110 | return binder::Status::ok(); |
| 111 | } |
| 112 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 113 | virtual binder::Status onCameraAccessPrioritiesChanged() override { |
Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 114 | // No op |
| 115 | return binder::Status::ok(); |
| 116 | } |
| 117 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 118 | virtual binder::Status onCameraOpened([[maybe_unused]] const std::string& /*cameraId*/, |
| 119 | [[maybe_unused]] const std::string& /*clientPackageName*/, |
| 120 | [[maybe_unused]] int32_t /*deviceId*/) { |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 121 | // No op |
| 122 | return binder::Status::ok(); |
| 123 | } |
| 124 | |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 125 | virtual binder::Status onCameraClosed([[maybe_unused]] const std::string& /*cameraId*/, |
| 126 | [[maybe_unused]] int32_t /*deviceId*/) override { |
Shuzhen Wang | 695044d | 2020-03-06 09:02:23 -0800 | [diff] [blame] | 127 | // No op |
| 128 | return binder::Status::ok(); |
| 129 | } |
| 130 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 131 | bool waitForNumCameras(size_t num) const { |
| 132 | Mutex::Autolock l(mLock); |
| 133 | |
| 134 | if (mCameraStatuses.size() == num) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | while (mCameraStatuses.size() < num) { |
| 139 | if (mCondition.waitRelative(mLock, SETUP_TIMEOUT) != OK) { |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | return true; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 144 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 145 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 146 | bool waitForTorchState(int32_t status, int32_t cameraId) const { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 147 | Mutex::Autolock l(mLock); |
| 148 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 149 | const auto& iter = mCameraTorchStatuses.find(std::to_string(cameraId)); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 150 | if (iter != mCameraTorchStatuses.end() && iter->second == status) { |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool foundStatus = false; |
| 155 | while (!foundStatus) { |
| 156 | if (mTorchCondition.waitRelative(mLock, SETUP_TIMEOUT) != OK) { |
| 157 | return false; |
| 158 | } |
| 159 | const auto& iter = |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 160 | mCameraTorchStatuses.find(std::to_string(cameraId)); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 161 | foundStatus = (iter != mCameraTorchStatuses.end() && iter->second == status); |
| 162 | } |
| 163 | return true; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 164 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 165 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 166 | int32_t getTorchStatus(int32_t cameraId) const { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 167 | Mutex::Autolock l(mLock); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 168 | const auto& iter = mCameraTorchStatuses.find(std::to_string(cameraId)); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 169 | if (iter == mCameraTorchStatuses.end()) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 170 | return hardware::ICameraServiceListener::TORCH_STATUS_UNKNOWN; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 171 | } |
| 172 | return iter->second; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 173 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 174 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 175 | int32_t getStatus(const std::string& cameraId) const { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 176 | Mutex::Autolock l(mLock); |
| 177 | const auto& iter = mCameraStatuses.find(cameraId); |
| 178 | if (iter == mCameraStatuses.end()) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 179 | return hardware::ICameraServiceListener::STATUS_UNKNOWN; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 180 | } |
| 181 | return iter->second; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 182 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | // Callback implementation |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 186 | class TestCameraDeviceCallbacks : public hardware::camera2::BnCameraDeviceCallbacks { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 187 | public: |
| 188 | enum Status { |
| 189 | IDLE, |
| 190 | ERROR, |
| 191 | PREPARED, |
| 192 | RUNNING, |
| 193 | SENT_RESULT, |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 194 | UNINITIALIZED, |
| 195 | REPEATING_REQUEST_ERROR, |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 196 | REQUEST_QUEUE_EMPTY, |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | protected: |
| 200 | bool mError; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 201 | int32_t mLastStatus; |
| 202 | mutable std::vector<int32_t> mStatusesHit; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 203 | mutable Mutex mLock; |
| 204 | mutable Condition mStatusCondition; |
| 205 | public: |
| 206 | TestCameraDeviceCallbacks() : mError(false), mLastStatus(UNINITIALIZED) {} |
| 207 | |
| 208 | virtual ~TestCameraDeviceCallbacks() {} |
| 209 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 210 | virtual binder::Status onDeviceError(int errorCode, |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 211 | const CaptureResultExtras& resultExtras) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 212 | (void) resultExtras; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 213 | ALOGE("%s: onDeviceError occurred with: %d", __FUNCTION__, static_cast<int>(errorCode)); |
| 214 | Mutex::Autolock l(mLock); |
| 215 | mError = true; |
| 216 | mLastStatus = ERROR; |
| 217 | mStatusesHit.push_back(mLastStatus); |
| 218 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 219 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 222 | virtual binder::Status onDeviceIdle() { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 223 | Mutex::Autolock l(mLock); |
| 224 | mLastStatus = IDLE; |
| 225 | mStatusesHit.push_back(mLastStatus); |
| 226 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 227 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 230 | virtual binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras, |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 231 | int64_t timestamp) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 232 | (void) resultExtras; |
| 233 | (void) timestamp; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 234 | Mutex::Autolock l(mLock); |
| 235 | mLastStatus = RUNNING; |
| 236 | mStatusesHit.push_back(mLastStatus); |
| 237 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 238 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 241 | virtual binder::Status onResultReceived(const CameraMetadata& metadata, |
Shuzhen Wang | 5c22c15 | 2017-12-31 17:12:25 -0800 | [diff] [blame] | 242 | const CaptureResultExtras& resultExtras, |
| 243 | const std::vector<PhysicalCaptureResultInfo>& physicalResultInfos) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 244 | (void) metadata; |
| 245 | (void) resultExtras; |
Shuzhen Wang | 5c22c15 | 2017-12-31 17:12:25 -0800 | [diff] [blame] | 246 | (void) physicalResultInfos; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 247 | Mutex::Autolock l(mLock); |
| 248 | mLastStatus = SENT_RESULT; |
| 249 | mStatusesHit.push_back(mLastStatus); |
| 250 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 251 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 254 | virtual binder::Status onPrepared(int streamId) { |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 255 | (void) streamId; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 256 | Mutex::Autolock l(mLock); |
| 257 | mLastStatus = PREPARED; |
| 258 | mStatusesHit.push_back(mLastStatus); |
| 259 | mStatusCondition.broadcast(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 260 | return binder::Status::ok(); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Yin-Chia Yeh | 8ca23dc | 2017-09-05 18:15:56 -0700 | [diff] [blame] | 263 | virtual binder::Status onRepeatingRequestError( |
| 264 | int64_t lastFrameNumber, int32_t stoppedSequenceId) { |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 265 | (void) lastFrameNumber; |
Yin-Chia Yeh | 8ca23dc | 2017-09-05 18:15:56 -0700 | [diff] [blame] | 266 | (void) stoppedSequenceId; |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 267 | Mutex::Autolock l(mLock); |
| 268 | mLastStatus = REPEATING_REQUEST_ERROR; |
| 269 | mStatusesHit.push_back(mLastStatus); |
| 270 | mStatusCondition.broadcast(); |
| 271 | return binder::Status::ok(); |
| 272 | } |
| 273 | |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 274 | virtual binder::Status onRequestQueueEmpty() { |
| 275 | Mutex::Autolock l(mLock); |
| 276 | mLastStatus = REQUEST_QUEUE_EMPTY; |
| 277 | mStatusesHit.push_back(mLastStatus); |
| 278 | mStatusCondition.broadcast(); |
| 279 | return binder::Status::ok(); |
| 280 | } |
| 281 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 282 | // Test helper functions: |
| 283 | |
| 284 | bool hadError() const { |
| 285 | Mutex::Autolock l(mLock); |
| 286 | return mError; |
| 287 | } |
| 288 | |
| 289 | bool waitForStatus(Status status) const { |
| 290 | Mutex::Autolock l(mLock); |
| 291 | if (mLastStatus == status) { |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | while (std::find(mStatusesHit.begin(), mStatusesHit.end(), status) |
| 296 | == mStatusesHit.end()) { |
| 297 | |
| 298 | if (mStatusCondition.waitRelative(mLock, IDLE_TIMEOUT) != OK) { |
| 299 | mStatusesHit.clear(); |
| 300 | return false; |
| 301 | } |
| 302 | } |
| 303 | mStatusesHit.clear(); |
| 304 | |
| 305 | return true; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void clearStatus() const { |
| 309 | Mutex::Autolock l(mLock); |
| 310 | mStatusesHit.clear(); |
| 311 | } |
| 312 | |
| 313 | bool waitForIdle() const { |
| 314 | return waitForStatus(IDLE); |
| 315 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 318 | namespace { |
| 319 | Mutex gLock; |
| 320 | class DeathNotifier : public IBinder::DeathRecipient |
| 321 | { |
| 322 | public: |
| 323 | DeathNotifier() {} |
| 324 | |
| 325 | virtual void binderDied(const wp<IBinder>& /*who*/) { |
| 326 | ALOGV("binderDied"); |
| 327 | Mutex::Autolock _l(gLock); |
| 328 | ALOGW("Camera service died!"); |
| 329 | } |
| 330 | }; |
| 331 | sp<DeathNotifier> gDeathNotifier; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 332 | } // anonymous namespace |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 333 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 334 | // Exercise basic binder calls for the camera service |
| 335 | TEST(CameraServiceBinderTest, CheckBinderCameraService) { |
| 336 | ProcessState::self()->startThreadPool(); |
| 337 | sp<IServiceManager> sm = defaultServiceManager(); |
| 338 | sp<IBinder> binder = sm->getService(String16("media.camera")); |
| 339 | ASSERT_NOT_NULL(binder); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 340 | if (gDeathNotifier == NULL) { |
| 341 | gDeathNotifier = new DeathNotifier(); |
| 342 | } |
| 343 | binder->linkToDeath(gDeathNotifier); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 344 | sp<hardware::ICameraService> service = |
| 345 | interface_cast<hardware::ICameraService>(binder); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 346 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 347 | binder::Status res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 348 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 349 | int32_t numCameras = 0; |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 350 | res = service->getNumberOfCameras(hardware::ICameraService::CAMERA_TYPE_ALL, kDefaultDeviceId, |
| 351 | /*devicePolicy*/0, &numCameras); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 352 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 353 | EXPECT_LE(0, numCameras); |
| 354 | |
| 355 | // Check listener binder calls |
| 356 | sp<TestCameraServiceListener> listener(new TestCameraServiceListener()); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 357 | std::vector<hardware::CameraStatus> statuses; |
| 358 | res = service->addListener(listener, &statuses); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 359 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 360 | |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 361 | EXPECT_EQ(numCameras, static_cast<const int>(statuses.size())); |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 362 | for (const auto &it : statuses) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 363 | listener->onStatusChanged(it.status, it.cameraId, kDefaultDeviceId); |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 364 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 365 | |
| 366 | for (int32_t i = 0; i < numCameras; i++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 367 | std::string cameraId = std::to_string(i); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 368 | bool isSupported = false; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 369 | res = service->supportsCameraApi(cameraId, |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 370 | hardware::ICameraService::API_VERSION_2, &isSupported); |
| 371 | EXPECT_TRUE(res.isOk()) << res; |
| 372 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 373 | // We only care about binder calls for the Camera2 API. Camera1 is deprecated. |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 374 | if (!isSupported) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 375 | continue; |
| 376 | } |
| 377 | |
| 378 | // Check metadata binder call |
| 379 | CameraMetadata metadata; |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 380 | res = service->getCameraCharacteristics(cameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 381 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false, |
| 382 | kDefaultDeviceId, /*devicePolicy*/0, &metadata); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 383 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 384 | EXPECT_FALSE(metadata.isEmpty()); |
| 385 | |
| 386 | // Make sure we're available, or skip device tests otherwise |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 387 | int32_t s = listener->getStatus(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 388 | EXPECT_EQ(::android::hardware::ICameraServiceListener::STATUS_PRESENT, s); |
| 389 | if (s != ::android::hardware::ICameraServiceListener::STATUS_PRESENT) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 390 | continue; |
| 391 | } |
| 392 | |
| 393 | // Check connect binder calls |
| 394 | sp<TestCameraDeviceCallbacks> callbacks(new TestCameraDeviceCallbacks()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 395 | sp<hardware::camera2::ICameraDeviceUser> device; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 396 | res = service->connectDevice(callbacks, cameraId, "meeeeeeeee!", |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 397 | {}, hardware::ICameraService::USE_CALLING_UID, /*oomScoreOffset*/ 0, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 398 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 399 | /*overrideToPortrait*/false, kDefaultDeviceId, /*devicePolicy*/0, /*out*/&device); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 400 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 401 | ASSERT_NE(nullptr, device.get()); |
| 402 | device->disconnect(); |
| 403 | EXPECT_FALSE(callbacks->hadError()); |
| 404 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 405 | int32_t torchStatus = listener->getTorchStatus(i); |
| 406 | if (torchStatus == hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 407 | // Check torch calls |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 408 | res = service->setTorchMode(cameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 409 | /*enabled*/true, callbacks, kDefaultDeviceId, /*devicePolicy*/0); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 410 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 411 | EXPECT_TRUE(listener->waitForTorchState( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 412 | hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON, i)); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 413 | res = service->setTorchMode(cameraId, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 414 | /*enabled*/false, callbacks, kDefaultDeviceId, /*devicePolicy*/0); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 415 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 416 | EXPECT_TRUE(listener->waitForTorchState( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 417 | hardware::ICameraServiceListener::TORCH_STATUS_AVAILABLE_OFF, i)); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 421 | res = service->removeListener(listener); |
| 422 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | // Test fixture for client focused binder tests |
| 426 | class CameraClientBinderTest : public testing::Test { |
| 427 | protected: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 428 | sp<hardware::ICameraService> service; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 429 | int32_t numCameras; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 430 | std::vector<std::pair<sp<TestCameraDeviceCallbacks>, sp<hardware::camera2::ICameraDeviceUser>>> |
| 431 | openDeviceList; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 432 | sp<TestCameraServiceListener> serviceListener; |
| 433 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 434 | std::pair<sp<TestCameraDeviceCallbacks>, sp<hardware::camera2::ICameraDeviceUser>> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 435 | openNewDevice(const std::string& deviceId) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 436 | sp<TestCameraDeviceCallbacks> callbacks(new TestCameraDeviceCallbacks()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 437 | sp<hardware::camera2::ICameraDeviceUser> device; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 438 | { |
| 439 | SCOPED_TRACE("openNewDevice"); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 440 | binder::Status res = service->connectDevice(callbacks, deviceId, "meeeeeeeee!", |
Jayant Chowdhary | 8eb8d91 | 2021-05-18 17:41:56 +0000 | [diff] [blame] | 441 | {}, hardware::ICameraService::USE_CALLING_UID, /*oomScoreOffset*/ 0, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 442 | /*targetSdkVersion*/__ANDROID_API_FUTURE__, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 443 | /*overrideToPortrait*/false, kDefaultDeviceId, /*devicePolicy*/0, |
| 444 | /*out*/&device); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 445 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 446 | } |
| 447 | auto p = std::make_pair(callbacks, device); |
| 448 | openDeviceList.push_back(p); |
| 449 | return p; |
| 450 | } |
| 451 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 452 | void closeDevice(std::pair<sp<TestCameraDeviceCallbacks>, |
| 453 | sp<hardware::camera2::ICameraDeviceUser>>& p) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 454 | if (p.second.get() != nullptr) { |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 455 | binder::Status res = p.second->disconnect(); |
| 456 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 457 | { |
| 458 | SCOPED_TRACE("closeDevice"); |
| 459 | EXPECT_FALSE(p.first->hadError()); |
| 460 | } |
| 461 | } |
| 462 | auto iter = std::find(openDeviceList.begin(), openDeviceList.end(), p); |
| 463 | if (iter != openDeviceList.end()) { |
| 464 | openDeviceList.erase(iter); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | virtual void SetUp() { |
| 469 | ProcessState::self()->startThreadPool(); |
| 470 | sp<IServiceManager> sm = defaultServiceManager(); |
| 471 | sp<IBinder> binder = sm->getService(String16("media.camera")); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 472 | service = interface_cast<hardware::ICameraService>(binder); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 473 | serviceListener = new TestCameraServiceListener(); |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 474 | std::vector<hardware::CameraStatus> statuses; |
| 475 | service->addListener(serviceListener, &statuses); |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 476 | for (const auto &it : statuses) { |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 477 | serviceListener->onStatusChanged(it.status, it.cameraId, kDefaultDeviceId); |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 478 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 479 | service->getNumberOfCameras(hardware::ICameraService::CAMERA_TYPE_BACKWARD_COMPATIBLE, |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 480 | kDefaultDeviceId, /*devicePolicy*/0, &numCameras); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | virtual void TearDown() { |
| 484 | service = nullptr; |
| 485 | numCameras = 0; |
| 486 | for (auto& p : openDeviceList) { |
| 487 | closeDevice(p); |
| 488 | } |
| 489 | } |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | TEST_F(CameraClientBinderTest, CheckBinderCameraDeviceUser) { |
| 493 | ASSERT_NOT_NULL(service); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 494 | EXPECT_TRUE(serviceListener->waitForNumCameras(numCameras)); |
| 495 | for (int32_t i = 0; i < numCameras; i++) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 496 | std::string cameraId = std::to_string(i); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 497 | // Make sure we're available, or skip device tests otherwise |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 498 | int32_t s = serviceListener->getStatus(cameraId); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 499 | EXPECT_EQ(hardware::ICameraServiceListener::STATUS_PRESENT, s); |
| 500 | if (s != hardware::ICameraServiceListener::STATUS_PRESENT) { |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 501 | continue; |
| 502 | } |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 503 | binder::Status res; |
Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 504 | auto p = openNewDevice(cameraId); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 505 | sp<TestCameraDeviceCallbacks> callbacks = p.first; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 506 | sp<hardware::camera2::ICameraDeviceUser> device = p.second; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 507 | |
| 508 | // Setup a buffer queue; I'm just using the vendor opaque format here as that is |
| 509 | // guaranteed to be present |
| 510 | sp<IGraphicBufferProducer> gbProducer; |
| 511 | sp<IGraphicBufferConsumer> gbConsumer; |
| 512 | BufferQueue::createBufferQueue(&gbProducer, &gbConsumer); |
| 513 | sp<BufferItemConsumer> opaqueConsumer = new BufferItemConsumer(gbConsumer, |
| 514 | GRALLOC_USAGE_SW_READ_NEVER, /*maxImages*/2, /*controlledByApp*/true); |
| 515 | EXPECT_TRUE(opaqueConsumer.get() != nullptr); |
| 516 | opaqueConsumer->setName(String8("nom nom nom")); |
| 517 | |
| 518 | // Set to VGA dimens for default, as that is guaranteed to be present |
| 519 | EXPECT_EQ(OK, gbConsumer->setDefaultBufferSize(640, 480)); |
| 520 | EXPECT_EQ(OK, gbConsumer->setDefaultBufferFormat(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)); |
| 521 | |
| 522 | sp<Surface> surface(new Surface(gbProducer, /*controlledByApp*/false)); |
| 523 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 524 | std::string noPhysicalId; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 525 | OutputConfiguration output(gbProducer, /*rotation*/0, noPhysicalId); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 526 | |
| 527 | // Can we configure? |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 528 | res = device->beginConfigure(); |
| 529 | EXPECT_TRUE(res.isOk()) << res; |
| 530 | status_t streamId; |
| 531 | res = device->createStream(output, &streamId); |
| 532 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 533 | EXPECT_LE(0, streamId); |
Emilian Peev | 5fbe0ba | 2017-10-20 15:45:45 +0100 | [diff] [blame] | 534 | CameraMetadata sessionParams; |
Emilian Peev | cc0b795 | 2020-01-07 13:54:47 -0800 | [diff] [blame] | 535 | std::vector<int> offlineStreamIds; |
| 536 | res = device->endConfigure(/*isConstrainedHighSpeed*/ false, sessionParams, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 537 | ns2ms(systemTime()), &offlineStreamIds); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 538 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 539 | EXPECT_FALSE(callbacks->hadError()); |
| 540 | |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 541 | // Session configuration must also be supported in this case |
| 542 | SessionConfiguration sessionConfiguration = { /*inputWidth*/ 0, /*inputHeight*/0, |
| 543 | /*inputFormat*/ -1, CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE}; |
| 544 | sessionConfiguration.addOutputConfiguration(output); |
| 545 | bool queryStatus; |
| 546 | res = device->isSessionConfigurationSupported(sessionConfiguration, &queryStatus); |
| 547 | EXPECT_TRUE(res.isOk() || |
Jiyong Park | 5a095ea | 2019-07-09 15:43:57 +0900 | [diff] [blame] | 548 | (res.serviceSpecificErrorCode() == ICameraService::ERROR_INVALID_OPERATION)) |
Emilian Peev | 35ae826 | 2018-11-08 13:11:32 +0000 | [diff] [blame] | 549 | << res; |
| 550 | if (res.isOk()) { |
| 551 | EXPECT_TRUE(queryStatus); |
| 552 | } |
| 553 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 554 | // Can we make requests? |
| 555 | CameraMetadata requestTemplate; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 556 | res = device->createDefaultRequest(/*preview template*/1, |
| 557 | /*out*/&requestTemplate); |
| 558 | EXPECT_TRUE(res.isOk()) << res; |
| 559 | |
| 560 | hardware::camera2::CaptureRequest request; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 561 | request.mPhysicalCameraSettings.push_back({cameraId, requestTemplate}); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 562 | request.mSurfaceList.add(surface); |
| 563 | request.mIsReprocess = false; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 564 | int64_t lastFrameNumber = 0; |
| 565 | int64_t lastFrameNumberPrev = 0; |
| 566 | callbacks->clearStatus(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 567 | |
| 568 | hardware::camera2::utils::SubmitInfo info; |
| 569 | res = device->submitRequest(request, /*streaming*/true, /*out*/&info); |
| 570 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 571 | EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT)); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 572 | EXPECT_LE(0, info.mRequestId); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 573 | |
| 574 | // Can we stop requests? |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 575 | res = device->cancelRequest(info.mRequestId, /*out*/&lastFrameNumber); |
| 576 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 577 | EXPECT_TRUE(callbacks->waitForIdle()); |
| 578 | EXPECT_FALSE(callbacks->hadError()); |
| 579 | |
| 580 | // Can we do it again? |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 581 | lastFrameNumberPrev = info.mLastFrameNumber; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 582 | lastFrameNumber = 0; |
| 583 | requestTemplate.clear(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 584 | res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW, |
| 585 | /*out*/&requestTemplate); |
| 586 | EXPECT_TRUE(res.isOk()) << res; |
| 587 | hardware::camera2::CaptureRequest request2; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 588 | request2.mPhysicalCameraSettings.push_back({cameraId, requestTemplate}); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 589 | request2.mSurfaceList.add(surface); |
| 590 | request2.mIsReprocess = false; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 591 | callbacks->clearStatus(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 592 | hardware::camera2::utils::SubmitInfo info2; |
| 593 | res = device->submitRequest(request2, /*streaming*/true, |
| 594 | /*out*/&info2); |
| 595 | EXPECT_TRUE(res.isOk()) << res; |
| 596 | EXPECT_EQ(hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES, |
| 597 | info2.mLastFrameNumber); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 598 | lastFrameNumber = 0; |
| 599 | EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT)); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 600 | EXPECT_LE(0, info2.mRequestId); |
| 601 | res = device->cancelRequest(info2.mRequestId, /*out*/&lastFrameNumber); |
| 602 | EXPECT_TRUE(res.isOk()) << res; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 603 | EXPECT_TRUE(callbacks->waitForIdle()); |
| 604 | EXPECT_LE(lastFrameNumberPrev, lastFrameNumber); |
| 605 | sleep(/*second*/1); // allow some time for errors to show up, if any |
| 606 | EXPECT_FALSE(callbacks->hadError()); |
| 607 | |
| 608 | // Can we do it with a request list? |
| 609 | lastFrameNumberPrev = lastFrameNumber; |
| 610 | lastFrameNumber = 0; |
| 611 | requestTemplate.clear(); |
| 612 | CameraMetadata requestTemplate2; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 613 | res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW, |
| 614 | /*out*/&requestTemplate); |
| 615 | EXPECT_TRUE(res.isOk()) << res; |
| 616 | res = device->createDefaultRequest(hardware::camera2::ICameraDeviceUser::TEMPLATE_PREVIEW, |
| 617 | /*out*/&requestTemplate2); |
| 618 | EXPECT_TRUE(res.isOk()) << res; |
| 619 | android::hardware::camera2::CaptureRequest request3; |
| 620 | android::hardware::camera2::CaptureRequest request4; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 621 | request3.mPhysicalCameraSettings.push_back({cameraId, requestTemplate}); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 622 | request3.mSurfaceList.add(surface); |
| 623 | request3.mIsReprocess = false; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 624 | request4.mPhysicalCameraSettings.push_back({cameraId, requestTemplate2}); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 625 | request4.mSurfaceList.add(surface); |
| 626 | request4.mIsReprocess = false; |
| 627 | std::vector<hardware::camera2::CaptureRequest> requestList; |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 628 | requestList.push_back(request3); |
| 629 | requestList.push_back(request4); |
| 630 | |
| 631 | callbacks->clearStatus(); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 632 | hardware::camera2::utils::SubmitInfo info3; |
| 633 | res = device->submitRequestList(requestList, /*streaming*/false, |
| 634 | /*out*/&info3); |
| 635 | EXPECT_TRUE(res.isOk()) << res; |
| 636 | EXPECT_LE(0, info3.mRequestId); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 637 | EXPECT_TRUE(callbacks->waitForStatus(TestCameraDeviceCallbacks::SENT_RESULT)); |
| 638 | EXPECT_TRUE(callbacks->waitForIdle()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 639 | EXPECT_LE(lastFrameNumberPrev, info3.mLastFrameNumber); |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 640 | sleep(/*second*/1); // allow some time for errors to show up, if any |
| 641 | EXPECT_FALSE(callbacks->hadError()); |
| 642 | |
| 643 | // Can we unconfigure? |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 644 | res = device->beginConfigure(); |
| 645 | EXPECT_TRUE(res.isOk()) << res; |
| 646 | res = device->deleteStream(streamId); |
| 647 | EXPECT_TRUE(res.isOk()) << res; |
Emilian Peev | cc0b795 | 2020-01-07 13:54:47 -0800 | [diff] [blame] | 648 | res = device->endConfigure(/*isConstrainedHighSpeed*/ false, sessionParams, |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 649 | ns2ms(systemTime()), &offlineStreamIds); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 650 | EXPECT_TRUE(res.isOk()) << res; |
| 651 | |
Ruben Brunk | 3450ba7 | 2015-06-16 11:00:37 -0700 | [diff] [blame] | 652 | sleep(/*second*/1); // allow some time for errors to show up, if any |
| 653 | EXPECT_FALSE(callbacks->hadError()); |
| 654 | |
| 655 | closeDevice(p); |
| 656 | } |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 657 | } |
Emilian Peev | aebbe41 | 2018-01-15 13:53:24 +0000 | [diff] [blame] | 658 | |
| 659 | TEST_F(CameraClientBinderTest, CheckBinderCaptureRequest) { |
| 660 | sp<CaptureRequest> requestOriginal, requestParceled; |
| 661 | sp<IGraphicBufferProducer> gbProducer; |
| 662 | sp<IGraphicBufferConsumer> gbConsumer; |
| 663 | BufferQueue::createBufferQueue(&gbProducer, &gbConsumer); |
| 664 | sp<Surface> surface(new Surface(gbProducer, /*controlledByApp*/false)); |
| 665 | Vector<sp<Surface>> surfaceList; |
| 666 | surfaceList.push_back(surface); |
| 667 | std::string physicalDeviceId1 = "0"; |
| 668 | std::string physicalDeviceId2 = "1"; |
| 669 | CameraMetadata physicalDeviceSettings1, physicalDeviceSettings2; |
| 670 | uint8_t intent1 = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW; |
| 671 | uint8_t intent2 = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD; |
| 672 | EXPECT_EQ(OK, physicalDeviceSettings1.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent1, 1)); |
| 673 | EXPECT_EQ(OK, physicalDeviceSettings2.update(ANDROID_CONTROL_CAPTURE_INTENT, &intent2, 1)); |
| 674 | |
| 675 | requestParceled = new CaptureRequest(); |
| 676 | Parcel p; |
| 677 | EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK); |
| 678 | p.writeInt32(0); |
| 679 | p.setDataPosition(0); |
| 680 | EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK); |
| 681 | p.freeData(); |
| 682 | p.writeInt32(-1); |
| 683 | p.setDataPosition(0); |
| 684 | EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK); |
| 685 | p.freeData(); |
| 686 | p.writeInt32(1); |
| 687 | p.setDataPosition(0); |
| 688 | EXPECT_TRUE(requestParceled->readFromParcel(&p) != OK); |
| 689 | |
| 690 | requestOriginal = new CaptureRequest(); |
| 691 | requestOriginal->mPhysicalCameraSettings.push_back({physicalDeviceId1, |
| 692 | physicalDeviceSettings1}); |
| 693 | requestOriginal->mPhysicalCameraSettings.push_back({physicalDeviceId2, |
| 694 | physicalDeviceSettings2}); |
| 695 | requestOriginal->mSurfaceList.push_back(surface); |
| 696 | requestOriginal->mIsReprocess = false; |
| 697 | requestOriginal->mSurfaceConverted = false; |
| 698 | |
| 699 | p.freeData(); |
| 700 | EXPECT_TRUE(requestOriginal->writeToParcel(&p) == OK); |
| 701 | p.setDataPosition(0); |
| 702 | EXPECT_TRUE(requestParceled->readFromParcel(&p) == OK); |
| 703 | EXPECT_EQ(requestParceled->mIsReprocess, false); |
| 704 | EXPECT_FALSE(requestParceled->mSurfaceList.empty()); |
| 705 | EXPECT_EQ(2u, requestParceled->mPhysicalCameraSettings.size()); |
| 706 | auto it = requestParceled->mPhysicalCameraSettings.begin(); |
| 707 | EXPECT_EQ(physicalDeviceId1, it->id); |
| 708 | EXPECT_TRUE(it->settings.exists(ANDROID_CONTROL_CAPTURE_INTENT)); |
| 709 | auto entry = it->settings.find(ANDROID_CONTROL_CAPTURE_INTENT); |
| 710 | EXPECT_EQ(entry.data.u8[0], intent1); |
| 711 | it++; |
| 712 | EXPECT_EQ(physicalDeviceId2, it->id); |
| 713 | EXPECT_TRUE(it->settings.exists(ANDROID_CONTROL_CAPTURE_INTENT)); |
| 714 | entry = it->settings.find(ANDROID_CONTROL_CAPTURE_INTENT); |
| 715 | EXPECT_EQ(entry.data.u8[0], intent2); |
Biswarup Pal | 37a7518 | 2024-01-16 15:53:35 +0000 | [diff] [blame] | 716 | } |