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 <cstdint> |
| 18 | #include <memory> |
| 19 | |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame^] | 20 | #include "VirtualCameraDevice.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 21 | #include "VirtualCameraSession.h" |
| 22 | #include "aidl/android/companion/virtualcamera/BnVirtualCameraCallback.h" |
| 23 | #include "aidl/android/companion/virtualcamera/IVirtualCameraCallback.h" |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame^] | 24 | #include "aidl/android/companion/virtualcamera/SupportedStreamConfiguration.h" |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 25 | #include "aidl/android/hardware/camera/device/BnCameraDeviceCallback.h" |
| 26 | #include "aidl/android/hardware/camera/device/StreamConfiguration.h" |
| 27 | #include "aidl/android/hardware/graphics/common/PixelFormat.h" |
| 28 | #include "android/binder_auto_utils.h" |
| 29 | #include "android/binder_interface_utils.h" |
| 30 | #include "gmock/gmock.h" |
| 31 | #include "gtest/gtest.h" |
| 32 | #include "util/MetadataBuilder.h" |
| 33 | |
| 34 | namespace android { |
| 35 | namespace companion { |
| 36 | namespace virtualcamera { |
| 37 | namespace { |
| 38 | |
| 39 | constexpr int kWidth = 640; |
| 40 | constexpr int kHeight = 480; |
| 41 | constexpr int kStreamId = 0; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame^] | 42 | constexpr int kCameraId = 42; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 43 | |
| 44 | using ::aidl::android::companion::virtualcamera::BnVirtualCameraCallback; |
| 45 | using ::aidl::android::companion::virtualcamera::Format; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame^] | 46 | using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 47 | using ::aidl::android::hardware::camera::device::BnCameraDeviceCallback; |
| 48 | using ::aidl::android::hardware::camera::device::BufferRequest; |
| 49 | using ::aidl::android::hardware::camera::device::BufferRequestStatus; |
| 50 | using ::aidl::android::hardware::camera::device::CaptureRequest; |
| 51 | using ::aidl::android::hardware::camera::device::CaptureResult; |
| 52 | using ::aidl::android::hardware::camera::device::HalStream; |
| 53 | using ::aidl::android::hardware::camera::device::NotifyMsg; |
| 54 | using ::aidl::android::hardware::camera::device::Stream; |
| 55 | using ::aidl::android::hardware::camera::device::StreamBuffer; |
| 56 | using ::aidl::android::hardware::camera::device::StreamBufferRet; |
| 57 | using ::aidl::android::hardware::camera::device::StreamConfiguration; |
| 58 | using ::aidl::android::hardware::graphics::common::PixelFormat; |
| 59 | using ::aidl::android::view::Surface; |
| 60 | using ::testing::_; |
| 61 | using ::testing::ElementsAre; |
| 62 | using ::testing::Eq; |
| 63 | using ::testing::Return; |
| 64 | using ::testing::SizeIs; |
| 65 | |
| 66 | Stream createStream(int streamId, int width, int height, PixelFormat format) { |
| 67 | Stream s; |
| 68 | s.id = streamId; |
| 69 | s.width = width; |
| 70 | s.height = height; |
| 71 | s.format = format; |
| 72 | return s; |
| 73 | } |
| 74 | |
| 75 | class MockCameraDeviceCallback : public BnCameraDeviceCallback { |
| 76 | public: |
| 77 | MOCK_METHOD(ndk::ScopedAStatus, notify, (const std::vector<NotifyMsg>&), |
| 78 | (override)); |
| 79 | MOCK_METHOD(ndk::ScopedAStatus, processCaptureResult, |
| 80 | (const std::vector<CaptureResult>&), (override)); |
| 81 | MOCK_METHOD(ndk::ScopedAStatus, requestStreamBuffers, |
| 82 | (const std::vector<BufferRequest>&, std::vector<StreamBufferRet>*, |
| 83 | BufferRequestStatus*), |
| 84 | (override)); |
| 85 | MOCK_METHOD(ndk::ScopedAStatus, returnStreamBuffers, |
| 86 | (const std::vector<StreamBuffer>&), (override)); |
| 87 | }; |
| 88 | |
| 89 | class MockVirtualCameraCallback : public BnVirtualCameraCallback { |
| 90 | public: |
| 91 | MOCK_METHOD(ndk::ScopedAStatus, onStreamConfigured, |
| 92 | (int, const Surface&, int32_t, int32_t, Format), (override)); |
| 93 | MOCK_METHOD(ndk::ScopedAStatus, onProcessCaptureRequest, (int, int), |
| 94 | (override)); |
| 95 | MOCK_METHOD(ndk::ScopedAStatus, onStreamClosed, (int), (override)); |
| 96 | }; |
| 97 | |
| 98 | class VirtualCameraSessionTest : public ::testing::Test { |
| 99 | public: |
| 100 | void SetUp() override { |
| 101 | mMockCameraDeviceCallback = |
| 102 | ndk::SharedRefBase::make<MockCameraDeviceCallback>(); |
| 103 | mMockVirtualCameraClientCallback = |
| 104 | ndk::SharedRefBase::make<MockVirtualCameraCallback>(); |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame^] | 105 | mVirtualCameraDevice = ndk::SharedRefBase::make<VirtualCameraDevice>( |
| 106 | kCameraId, |
| 107 | std::vector<SupportedStreamConfiguration>{ |
| 108 | SupportedStreamConfiguration{.width = kWidth, |
| 109 | .height = kHeight, |
| 110 | .pixelFormat = Format::YUV_420_888}}, |
| 111 | mMockVirtualCameraClientCallback); |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 112 | mVirtualCameraSession = ndk::SharedRefBase::make<VirtualCameraSession>( |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame^] | 113 | *mVirtualCameraDevice, mMockCameraDeviceCallback, |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 114 | mMockVirtualCameraClientCallback); |
| 115 | |
| 116 | ON_CALL(*mMockVirtualCameraClientCallback, onStreamConfigured) |
| 117 | .WillByDefault(ndk::ScopedAStatus::ok); |
| 118 | ON_CALL(*mMockVirtualCameraClientCallback, onProcessCaptureRequest) |
| 119 | .WillByDefault(ndk::ScopedAStatus::ok); |
| 120 | } |
| 121 | |
| 122 | protected: |
| 123 | std::shared_ptr<MockCameraDeviceCallback> mMockCameraDeviceCallback; |
| 124 | std::shared_ptr<MockVirtualCameraCallback> mMockVirtualCameraClientCallback; |
Jan Sebechlebsky | 3b478c4 | 2023-11-23 13:15:56 +0100 | [diff] [blame^] | 125 | std::shared_ptr<VirtualCameraDevice> mVirtualCameraDevice; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 126 | std::shared_ptr<VirtualCameraSession> mVirtualCameraSession; |
| 127 | }; |
| 128 | |
| 129 | TEST_F(VirtualCameraSessionTest, ConfigureTriggersClientConfigureCallback) { |
| 130 | PixelFormat format = PixelFormat::YCBCR_420_888; |
| 131 | StreamConfiguration streamConfiguration; |
| 132 | streamConfiguration.streams = { |
| 133 | createStream(kStreamId, kWidth, kHeight, format)}; |
| 134 | std::vector<HalStream> halStreams; |
| 135 | EXPECT_CALL( |
| 136 | *mMockVirtualCameraClientCallback, |
| 137 | onStreamConfigured(kStreamId, _, kWidth, kHeight, Format::YUV_420_888)); |
| 138 | |
| 139 | ASSERT_TRUE( |
| 140 | mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams) |
| 141 | .isOk()); |
| 142 | |
| 143 | EXPECT_THAT(halStreams, SizeIs(streamConfiguration.streams.size())); |
| 144 | EXPECT_THAT(mVirtualCameraSession->getStreamIds(), ElementsAre(0)); |
| 145 | } |
| 146 | |
| 147 | TEST_F(VirtualCameraSessionTest, SecondConfigureDropsUnreferencedStreams) { |
| 148 | PixelFormat format = PixelFormat::YCBCR_420_888; |
| 149 | StreamConfiguration streamConfiguration; |
| 150 | std::vector<HalStream> halStreams; |
| 151 | |
| 152 | streamConfiguration.streams = {createStream(0, kWidth, kHeight, format), |
| 153 | createStream(1, kWidth, kHeight, format), |
| 154 | createStream(2, kWidth, kHeight, format)}; |
| 155 | ASSERT_TRUE( |
| 156 | mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams) |
| 157 | .isOk()); |
| 158 | |
| 159 | EXPECT_THAT(mVirtualCameraSession->getStreamIds(), ElementsAre(0, 1, 2)); |
| 160 | |
| 161 | streamConfiguration.streams = {createStream(0, kWidth, kHeight, format), |
| 162 | createStream(2, kWidth, kHeight, format), |
| 163 | createStream(3, kWidth, kHeight, format)}; |
| 164 | ASSERT_TRUE( |
| 165 | mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams) |
| 166 | .isOk()); |
| 167 | |
| 168 | EXPECT_THAT(mVirtualCameraSession->getStreamIds(), ElementsAre(0, 2, 3)); |
| 169 | } |
| 170 | |
| 171 | TEST_F(VirtualCameraSessionTest, CloseTriggersClientTerminateCallback) { |
| 172 | EXPECT_CALL(*mMockVirtualCameraClientCallback, onStreamClosed(kStreamId)) |
| 173 | .WillOnce(Return(ndk::ScopedAStatus::ok())); |
| 174 | |
| 175 | ASSERT_TRUE(mVirtualCameraSession->close().isOk()); |
| 176 | } |
| 177 | |
| 178 | TEST_F(VirtualCameraSessionTest, onProcessCaptureRequestTriggersClientCallback) { |
| 179 | StreamConfiguration streamConfiguration; |
| 180 | streamConfiguration.streams = { |
| 181 | createStream(kStreamId, kWidth, kHeight, PixelFormat::YCBCR_420_888)}; |
| 182 | std::vector<CaptureRequest> requests(1); |
| 183 | requests[0].frameNumber = 42; |
| 184 | requests[0].settings = *( |
| 185 | MetadataBuilder().setControlAfMode(ANDROID_CONTROL_AF_MODE_AUTO).build()); |
| 186 | |
| 187 | std::vector<HalStream> halStreams; |
| 188 | ASSERT_TRUE( |
| 189 | mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams) |
| 190 | .isOk()); |
| 191 | |
| 192 | EXPECT_CALL(*mMockVirtualCameraClientCallback, |
| 193 | onProcessCaptureRequest(kStreamId, requests[0].frameNumber)) |
| 194 | .WillOnce(Return(ndk::ScopedAStatus::ok())); |
| 195 | int32_t aidlReturn = 0; |
| 196 | ASSERT_TRUE(mVirtualCameraSession |
| 197 | ->processCaptureRequest(requests, /*in_cachesToRemove=*/{}, |
| 198 | &aidlReturn) |
| 199 | .isOk()); |
| 200 | EXPECT_THAT(aidlReturn, Eq(requests.size())); |
| 201 | } |
| 202 | |
| 203 | } // namespace |
| 204 | } // namespace virtualcamera |
| 205 | } // namespace companion |
| 206 | } // namespace android |