Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #include <sys/cdefs.h> |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "VirtualCameraRenderThread.h" |
| 22 | #include "VirtualCameraSessionContext.h" |
| 23 | #include "aidl/android/hardware/camera/common/CameraDeviceStatus.h" |
| 24 | #include "aidl/android/hardware/camera/common/TorchModeStatus.h" |
| 25 | #include "aidl/android/hardware/camera/device/BnCameraDeviceCallback.h" |
| 26 | #include "aidl/android/hardware/camera/device/BufferRequest.h" |
| 27 | #include "aidl/android/hardware/camera/device/BufferRequestStatus.h" |
| 28 | #include "aidl/android/hardware/camera/device/BufferStatus.h" |
| 29 | #include "aidl/android/hardware/camera/device/CaptureResult.h" |
| 30 | #include "aidl/android/hardware/camera/device/NotifyMsg.h" |
| 31 | #include "aidl/android/hardware/camera/device/StreamBuffer.h" |
| 32 | #include "aidl/android/hardware/camera/device/StreamBufferRet.h" |
| 33 | #include "android/binder_auto_utils.h" |
| 34 | #include "gmock/gmock.h" |
| 35 | #include "gtest/gtest.h" |
| 36 | |
| 37 | namespace android { |
| 38 | namespace companion { |
| 39 | namespace virtualcamera { |
| 40 | namespace { |
| 41 | |
| 42 | using ::aidl::android::hardware::camera::common::CameraDeviceStatus; |
| 43 | using ::aidl::android::hardware::camera::common::TorchModeStatus; |
| 44 | using ::aidl::android::hardware::camera::device::BnCameraDeviceCallback; |
| 45 | using ::aidl::android::hardware::camera::device::BufferRequest; |
| 46 | using ::aidl::android::hardware::camera::device::BufferRequestStatus; |
| 47 | using ::aidl::android::hardware::camera::device::BufferStatus; |
| 48 | using ::aidl::android::hardware::camera::device::CaptureResult; |
Jan Sebechlebsky | b0d8cab | 2023-11-28 10:55:04 +0100 | [diff] [blame] | 49 | using ::aidl::android::hardware::camera::device::ErrorCode; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 50 | using ::aidl::android::hardware::camera::device::ErrorMsg; |
| 51 | using ::aidl::android::hardware::camera::device::NotifyMsg; |
| 52 | using ::aidl::android::hardware::camera::device::StreamBuffer; |
| 53 | using ::aidl::android::hardware::camera::device::StreamBufferRet; |
| 54 | using ::testing::AllOf; |
| 55 | using ::testing::ElementsAre; |
| 56 | using ::testing::Eq; |
| 57 | using ::testing::Field; |
| 58 | using ::testing::Matcher; |
| 59 | using ::testing::Property; |
| 60 | using ::testing::Return; |
| 61 | using ::testing::SizeIs; |
| 62 | |
| 63 | constexpr int kInputWidth = 640; |
| 64 | constexpr int kInputHeight = 480; |
| 65 | |
| 66 | Matcher<StreamBuffer> IsStreamBufferWithStatus(const int streamId, |
| 67 | const int bufferId, |
| 68 | const BufferStatus status) { |
| 69 | return AllOf(Field(&StreamBuffer::streamId, Eq(streamId)), |
| 70 | Field(&StreamBuffer::bufferId, Eq(bufferId)), |
| 71 | Field(&StreamBuffer::status, Eq(status))); |
| 72 | } |
| 73 | |
| 74 | Matcher<NotifyMsg> IsRequestErrorNotifyMsg(const int frameId) { |
| 75 | return AllOf(Property(&NotifyMsg::getTag, Eq(NotifyMsg::error)), |
| 76 | Property(&NotifyMsg::get<NotifyMsg::error>, |
Jan Sebechlebsky | b0d8cab | 2023-11-28 10:55:04 +0100 | [diff] [blame] | 77 | Field(&ErrorMsg::frameNumber, Eq(frameId))), |
| 78 | Property(&NotifyMsg::get<NotifyMsg::error>, |
| 79 | Field(&ErrorMsg::errorStreamId, Eq(-1))), |
| 80 | Property(&NotifyMsg::get<NotifyMsg::error>, |
| 81 | Field(&ErrorMsg::errorCode, Eq(ErrorCode::ERROR_REQUEST)))); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | class MockCameraDeviceCallback : public BnCameraDeviceCallback { |
| 85 | public: |
| 86 | MOCK_METHOD(ndk::ScopedAStatus, notify, (const std::vector<NotifyMsg>&), |
| 87 | (override)); |
| 88 | MOCK_METHOD(ndk::ScopedAStatus, processCaptureResult, |
| 89 | (const std::vector<CaptureResult>&), (override)); |
| 90 | MOCK_METHOD(ndk::ScopedAStatus, requestStreamBuffers, |
| 91 | (const std::vector<BufferRequest>&, std::vector<StreamBufferRet>*, |
| 92 | BufferRequestStatus*), |
| 93 | (override)); |
| 94 | MOCK_METHOD(ndk::ScopedAStatus, returnStreamBuffers, |
| 95 | (const std::vector<StreamBuffer>&), (override)); |
| 96 | }; |
| 97 | |
| 98 | class VirtualCameraRenderThreadTest : public ::testing::Test { |
| 99 | public: |
| 100 | void SetUp() override { |
| 101 | mSessionContext = std::make_unique<VirtualCameraSessionContext>(); |
| 102 | mMockCameraDeviceCallback = |
| 103 | ndk::SharedRefBase::make<MockCameraDeviceCallback>(); |
| 104 | mRenderThread = std::make_unique<VirtualCameraRenderThread>( |
| 105 | *mSessionContext, kInputWidth, kInputHeight, mMockCameraDeviceCallback); |
| 106 | } |
| 107 | |
| 108 | protected: |
| 109 | std::unique_ptr<VirtualCameraSessionContext> mSessionContext; |
| 110 | std::unique_ptr<VirtualCameraRenderThread> mRenderThread; |
| 111 | std::shared_ptr<MockCameraDeviceCallback> mMockCameraDeviceCallback; |
| 112 | }; |
| 113 | |
| 114 | TEST_F(VirtualCameraRenderThreadTest, FlushReturnsErrorForInFlightRequests) { |
| 115 | const int frameNumber = 42; |
| 116 | const int firstStreamId = 1; |
| 117 | const int firstStreamBufferId = 1234; |
| 118 | const int secondStreamId = 7; |
| 119 | const int secondStreamBufferId = 4321; |
| 120 | |
| 121 | // Notify should be called with the error set to corresponding frame. |
| 122 | EXPECT_CALL(*mMockCameraDeviceCallback, |
| 123 | notify(ElementsAre(IsRequestErrorNotifyMsg(frameNumber)))) |
| 124 | .WillOnce(Return(ndk::ScopedAStatus::ok())); |
| 125 | |
| 126 | // Process capture result should be called with all buffers in error state. |
| 127 | EXPECT_CALL( |
| 128 | *mMockCameraDeviceCallback, |
| 129 | processCaptureResult(ElementsAre(AllOf( |
| 130 | Field(&CaptureResult::frameNumber, frameNumber), |
| 131 | Field(&CaptureResult::outputBuffers, |
| 132 | testing::UnorderedElementsAre( |
| 133 | IsStreamBufferWithStatus(firstStreamId, firstStreamBufferId, |
| 134 | BufferStatus::ERROR), |
| 135 | IsStreamBufferWithStatus(secondStreamId, secondStreamBufferId, |
| 136 | BufferStatus::ERROR))))))) |
| 137 | .WillOnce([]() { return ndk::ScopedAStatus::ok(); }); |
| 138 | |
| 139 | mRenderThread->enqueueTask(std::make_unique<ProcessCaptureRequestTask>( |
| 140 | frameNumber, |
| 141 | std::vector<CaptureRequestBuffer>{ |
| 142 | CaptureRequestBuffer(firstStreamId, firstStreamBufferId), |
| 143 | CaptureRequestBuffer(secondStreamId, secondStreamBufferId)})); |
| 144 | |
| 145 | mRenderThread->flush(); |
| 146 | } |
| 147 | |
| 148 | } // namespace |
| 149 | } // namespace virtualcamera |
| 150 | } // namespace companion |
Jan Sebechlebsky | b0d8cab | 2023-11-28 10:55:04 +0100 | [diff] [blame] | 151 | } // namespace android |