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