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