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