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