blob: 1af8b803a73c3fd35f6b431cd3bcc26a3743aeff [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
Biswarup Pal6152a302023-12-19 12:44:09 +00002 * Copyright 2023 The Android Open Source Project
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01003 *
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 Sebechlebsky3b478c42023-11-23 13:15:56 +010020#include "VirtualCameraDevice.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010021#include "VirtualCameraSession.h"
22#include "aidl/android/companion/virtualcamera/BnVirtualCameraCallback.h"
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010023#include "aidl/android/companion/virtualcamera/SupportedStreamConfiguration.h"
Biswarup Pal6152a302023-12-19 12:44:09 +000024#include "aidl/android/companion/virtualcamera/VirtualCameraConfiguration.h"
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +010025#include "aidl/android/hardware/camera/common/Status.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010026#include "aidl/android/hardware/camera/device/BnCameraDeviceCallback.h"
27#include "aidl/android/hardware/camera/device/StreamConfiguration.h"
28#include "aidl/android/hardware/graphics/common/PixelFormat.h"
29#include "android/binder_auto_utils.h"
30#include "android/binder_interface_utils.h"
31#include "gmock/gmock.h"
32#include "gtest/gtest.h"
Jan Sebechlebsky4ce32082024-02-14 16:02:11 +010033#include "util/MetadataUtil.h"
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010034
35namespace android {
36namespace companion {
37namespace virtualcamera {
38namespace {
39
Jan Sebechlebsky39129f82024-01-19 16:42:11 +010040constexpr int kVgaWidth = 640;
41constexpr int kVgaHeight = 480;
42constexpr int kSvgaWidth = 800;
43constexpr int kSvgaHeight = 600;
Biswarup Pal6152a302023-12-19 12:44:09 +000044constexpr int kMaxFps = 30;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010045constexpr int kStreamId = 0;
Jan Sebechlebsky39129f82024-01-19 16:42:11 +010046constexpr int kSecondStreamId = 1;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010047constexpr int kCameraId = 42;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010048
49using ::aidl::android::companion::virtualcamera::BnVirtualCameraCallback;
50using ::aidl::android::companion::virtualcamera::Format;
Biswarup Pal112458f2023-12-28 19:50:17 +000051using ::aidl::android::companion::virtualcamera::LensFacing;
Biswarup Pal6152a302023-12-19 12:44:09 +000052using ::aidl::android::companion::virtualcamera::SensorOrientation;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +010053using ::aidl::android::companion::virtualcamera::SupportedStreamConfiguration;
Biswarup Pal6152a302023-12-19 12:44:09 +000054using ::aidl::android::companion::virtualcamera::VirtualCameraConfiguration;
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +010055using ::aidl::android::hardware::camera::common::Status;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +010056using ::aidl::android::hardware::camera::device::BnCameraDeviceCallback;
57using ::aidl::android::hardware::camera::device::BufferRequest;
58using ::aidl::android::hardware::camera::device::BufferRequestStatus;
59using ::aidl::android::hardware::camera::device::CaptureRequest;
60using ::aidl::android::hardware::camera::device::CaptureResult;
61using ::aidl::android::hardware::camera::device::HalStream;
62using ::aidl::android::hardware::camera::device::NotifyMsg;
63using ::aidl::android::hardware::camera::device::Stream;
64using ::aidl::android::hardware::camera::device::StreamBuffer;
65using ::aidl::android::hardware::camera::device::StreamBufferRet;
66using ::aidl::android::hardware::camera::device::StreamConfiguration;
67using ::aidl::android::hardware::graphics::common::PixelFormat;
68using ::aidl::android::view::Surface;
69using ::testing::_;
70using ::testing::ElementsAre;
71using ::testing::Eq;
72using ::testing::Return;
73using ::testing::SizeIs;
74
75Stream createStream(int streamId, int width, int height, PixelFormat format) {
76 Stream s;
77 s.id = streamId;
78 s.width = width;
79 s.height = height;
80 s.format = format;
81 return s;
82}
83
84class 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
98class MockVirtualCameraCallback : public BnVirtualCameraCallback {
99 public:
100 MOCK_METHOD(ndk::ScopedAStatus, onStreamConfigured,
101 (int, const Surface&, int32_t, int32_t, Format), (override));
102 MOCK_METHOD(ndk::ScopedAStatus, onProcessCaptureRequest, (int, int),
103 (override));
104 MOCK_METHOD(ndk::ScopedAStatus, onStreamClosed, (int), (override));
105};
106
107class VirtualCameraSessionTest : public ::testing::Test {
108 public:
109 void SetUp() override {
110 mMockCameraDeviceCallback =
111 ndk::SharedRefBase::make<MockCameraDeviceCallback>();
112 mMockVirtualCameraClientCallback =
113 ndk::SharedRefBase::make<MockVirtualCameraCallback>();
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100114 mVirtualCameraDevice = ndk::SharedRefBase::make<VirtualCameraDevice>(
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100115 kCameraId,
116 VirtualCameraConfiguration{
117 .supportedStreamConfigs = {SupportedStreamConfiguration{
118 .width = kVgaWidth,
119 .height = kVgaHeight,
120 .pixelFormat = Format::YUV_420_888,
121 .maxFps = kMaxFps},
122 SupportedStreamConfiguration{
123 .width = kSvgaWidth,
124 .height = kSvgaHeight,
125 .pixelFormat = Format::YUV_420_888,
126 .maxFps = kMaxFps}},
127 .virtualCameraCallback = nullptr,
128 .sensorOrientation = SensorOrientation::ORIENTATION_0,
129 .lensFacing = LensFacing::FRONT});
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100130 mVirtualCameraSession = ndk::SharedRefBase::make<VirtualCameraSession>(
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100131 mVirtualCameraDevice, mMockCameraDeviceCallback,
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100132 mMockVirtualCameraClientCallback);
133
Jan Sebechlebsky85a2aef2023-11-29 10:35:55 +0100134 // Explicitly defining default actions below to prevent gmock from
135 // default-constructing ndk::ScopedAStatus, because default-constructed
136 // status wraps nullptr AStatus and causes crash when attempting to print
137 // it in gtest report.
138 ON_CALL(*mMockCameraDeviceCallback, notify)
139 .WillByDefault(ndk::ScopedAStatus::ok);
140 ON_CALL(*mMockCameraDeviceCallback, processCaptureResult)
141 .WillByDefault(ndk::ScopedAStatus::ok);
142 ON_CALL(*mMockCameraDeviceCallback, requestStreamBuffers)
143 .WillByDefault(ndk::ScopedAStatus::ok);
144 ON_CALL(*mMockCameraDeviceCallback, returnStreamBuffers)
145 .WillByDefault(ndk::ScopedAStatus::ok);
146
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100147 ON_CALL(*mMockVirtualCameraClientCallback, onStreamConfigured)
148 .WillByDefault(ndk::ScopedAStatus::ok);
149 ON_CALL(*mMockVirtualCameraClientCallback, onProcessCaptureRequest)
150 .WillByDefault(ndk::ScopedAStatus::ok);
Jan Sebechlebsky85a2aef2023-11-29 10:35:55 +0100151 ON_CALL(*mMockVirtualCameraClientCallback, onStreamClosed)
152 .WillByDefault(ndk::ScopedAStatus::ok);
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100153 }
154
155 protected:
156 std::shared_ptr<MockCameraDeviceCallback> mMockCameraDeviceCallback;
157 std::shared_ptr<MockVirtualCameraCallback> mMockVirtualCameraClientCallback;
Jan Sebechlebsky3b478c42023-11-23 13:15:56 +0100158 std::shared_ptr<VirtualCameraDevice> mVirtualCameraDevice;
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100159 std::shared_ptr<VirtualCameraSession> mVirtualCameraSession;
160};
161
162TEST_F(VirtualCameraSessionTest, ConfigureTriggersClientConfigureCallback) {
163 PixelFormat format = PixelFormat::YCBCR_420_888;
164 StreamConfiguration streamConfiguration;
165 streamConfiguration.streams = {
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100166 createStream(kStreamId, kVgaWidth, kVgaHeight, format),
167 createStream(kSecondStreamId, kSvgaWidth, kSvgaHeight, format)};
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100168 std::vector<HalStream> halStreams;
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100169
170 // Expect highest resolution to be picked for the client input.
171 EXPECT_CALL(*mMockVirtualCameraClientCallback,
172 onStreamConfigured(kStreamId, _, kSvgaWidth, kSvgaHeight,
173 Format::YUV_420_888));
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100174
175 ASSERT_TRUE(
176 mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams)
177 .isOk());
178
179 EXPECT_THAT(halStreams, SizeIs(streamConfiguration.streams.size()));
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100180 EXPECT_THAT(mVirtualCameraSession->getStreamIds(),
181 ElementsAre(kStreamId, kSecondStreamId));
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100182}
183
184TEST_F(VirtualCameraSessionTest, SecondConfigureDropsUnreferencedStreams) {
185 PixelFormat format = PixelFormat::YCBCR_420_888;
186 StreamConfiguration streamConfiguration;
187 std::vector<HalStream> halStreams;
188
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100189 streamConfiguration.streams = {createStream(0, kVgaWidth, kVgaHeight, format),
190 createStream(1, kVgaWidth, kVgaHeight, format),
191 createStream(2, kVgaWidth, kVgaHeight, format)};
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100192 ASSERT_TRUE(
193 mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams)
194 .isOk());
195
196 EXPECT_THAT(mVirtualCameraSession->getStreamIds(), ElementsAre(0, 1, 2));
197
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100198 streamConfiguration.streams = {createStream(0, kVgaWidth, kVgaHeight, format),
199 createStream(2, kVgaWidth, kVgaHeight, format),
200 createStream(3, kVgaWidth, kVgaHeight, format)};
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100201 ASSERT_TRUE(
202 mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams)
203 .isOk());
204
205 EXPECT_THAT(mVirtualCameraSession->getStreamIds(), ElementsAre(0, 2, 3));
206}
207
208TEST_F(VirtualCameraSessionTest, CloseTriggersClientTerminateCallback) {
209 EXPECT_CALL(*mMockVirtualCameraClientCallback, onStreamClosed(kStreamId))
210 .WillOnce(Return(ndk::ScopedAStatus::ok()));
211
212 ASSERT_TRUE(mVirtualCameraSession->close().isOk());
213}
214
Jan Sebechlebskyb0d8cab2023-11-28 10:55:04 +0100215TEST_F(VirtualCameraSessionTest, FlushBeforeConfigure) {
216 // Flush request coming before the configure request finished
217 // (so potentially the thread is not yet running) should be
218 // gracefully handled.
219
220 EXPECT_TRUE(mVirtualCameraSession->flush().isOk());
221}
222
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100223TEST_F(VirtualCameraSessionTest, onProcessCaptureRequestTriggersClientCallback) {
224 StreamConfiguration streamConfiguration;
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100225 streamConfiguration.streams = {createStream(kStreamId, kVgaWidth, kVgaHeight,
226 PixelFormat::YCBCR_420_888)};
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100227 std::vector<CaptureRequest> requests(1);
228 requests[0].frameNumber = 42;
229 requests[0].settings = *(
230 MetadataBuilder().setControlAfMode(ANDROID_CONTROL_AF_MODE_AUTO).build());
231
232 std::vector<HalStream> halStreams;
233 ASSERT_TRUE(
234 mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams)
235 .isOk());
236
237 EXPECT_CALL(*mMockVirtualCameraClientCallback,
238 onProcessCaptureRequest(kStreamId, requests[0].frameNumber))
239 .WillOnce(Return(ndk::ScopedAStatus::ok()));
240 int32_t aidlReturn = 0;
241 ASSERT_TRUE(mVirtualCameraSession
242 ->processCaptureRequest(requests, /*in_cachesToRemove=*/{},
243 &aidlReturn)
244 .isOk());
245 EXPECT_THAT(aidlReturn, Eq(requests.size()));
246}
247
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100248TEST_F(VirtualCameraSessionTest, configureAfterCameraRelease) {
249 StreamConfiguration streamConfiguration;
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100250 streamConfiguration.streams = {createStream(kStreamId, kVgaWidth, kVgaHeight,
251 PixelFormat::YCBCR_420_888)};
Jan Sebechlebsky0bb5e092023-12-08 16:17:54 +0100252 std::vector<HalStream> halStreams;
253
254 // Release virtual camera.
255 mVirtualCameraDevice.reset();
256
257 // Expect configuration attempt returns CAMERA_DISCONNECTED service specific code.
258 EXPECT_THAT(
259 mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams)
260 .getServiceSpecificError(),
261 Eq(static_cast<int32_t>(Status::CAMERA_DISCONNECTED)));
262}
263
Jan Sebechlebsky39129f82024-01-19 16:42:11 +0100264TEST_F(VirtualCameraSessionTest, ConfigureWithEmptyStreams) {
265 StreamConfiguration streamConfiguration;
266 std::vector<HalStream> halStreams;
267
268 // Expect configuration attempt returns CAMERA_DISCONNECTED service specific code.
269 EXPECT_THAT(
270 mVirtualCameraSession->configureStreams(streamConfiguration, &halStreams)
271 .getServiceSpecificError(),
272 Eq(static_cast<int32_t>(Status::ILLEGAL_ARGUMENT)));
273}
274
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +0100275} // namespace
276} // namespace virtualcamera
277} // namespace companion
278} // namespace android