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