1 /*
2 * Copyright (C) 2022 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_TAG "VtsAidlCameraServiceTargetTest"
18 // #define LOG_NDEBUG 0
19
20 #include <CameraMetadata.h>
21 #include <aidl/android/frameworks/cameraservice/device/BnCameraDeviceCallback.h>
22 #include <aidl/android/frameworks/cameraservice/device/CaptureRequest.h>
23 #include <aidl/android/frameworks/cameraservice/device/ICameraDeviceUser.h>
24 #include <aidl/android/frameworks/cameraservice/device/OutputConfiguration.h>
25 #include <aidl/android/frameworks/cameraservice/device/StreamConfigurationMode.h>
26 #include <aidl/android/frameworks/cameraservice/device/SubmitInfo.h>
27 #include <aidl/android/frameworks/cameraservice/device/TemplateId.h>
28 #include <aidl/android/frameworks/cameraservice/service/BnCameraServiceListener.h>
29 #include <aidl/android/frameworks/cameraservice/service/CameraStatusAndId.h>
30 #include <aidl/android/frameworks/cameraservice/service/ICameraService.h>
31 #include <aidlcommonsupport/NativeHandle.h>
32 #include <android-base/properties.h>
33 #include <android/binder_manager.h>
34 #include <android/binder_process.h>
35 #include <android/log.h>
36 #include <android/native_window_aidl.h>
37 #include <fmq/AidlMessageQueue.h>
38 #include <gtest/gtest.h>
39 #include <hidl/GtestPrinter.h>
40 #include <hidl/ServiceManagement.h>
41 #include <media/NdkImageReader.h>
42 #include <stdint.h>
43 #include <system/camera_metadata.h>
44 #include <system/graphics.h>
45 #include <utils/Condition.h>
46 #include <utils/Mutex.h>
47 #include <utils/StrongPointer.h>
48
49 #include <algorithm>
50 #include <string>
51 #include <unordered_map>
52 #include <unordered_set>
53 #include <vector>
54
55 namespace android {
56
57 using ::aidl::android::frameworks::cameraservice::device::BnCameraDeviceCallback;
58 using ::aidl::android::frameworks::cameraservice::device::CaptureMetadataInfo;
59 using ::aidl::android::frameworks::cameraservice::device::CaptureRequest;
60 using ::aidl::android::frameworks::cameraservice::device::CaptureResultExtras;
61 using ::aidl::android::frameworks::cameraservice::device::ErrorCode;
62 using ::aidl::android::frameworks::cameraservice::device::ICameraDeviceUser;
63 using ::aidl::android::frameworks::cameraservice::device::OutputConfiguration;
64 using ::aidl::android::frameworks::cameraservice::device::PhysicalCaptureResultInfo;
65 using ::aidl::android::frameworks::cameraservice::device::StreamConfigurationMode;
66 using ::aidl::android::frameworks::cameraservice::device::SubmitInfo;
67 using ::aidl::android::frameworks::cameraservice::device::TemplateId;
68 using ::aidl::android::frameworks::cameraservice::service::BnCameraServiceListener;
69 using ::aidl::android::frameworks::cameraservice::service::CameraDeviceStatus;
70 using ::aidl::android::frameworks::cameraservice::service::CameraStatusAndId;
71 using ::aidl::android::frameworks::cameraservice::service::ICameraService;
72 using ::aidl::android::hardware::common::fmq::MQDescriptor;
73 using ::aidl::android::view::Surface;
74 using ::android::hardware::isHidlSupported;
75 using ::android::hardware::camera::common::helper::CameraMetadata;
76 using ::ndk::SpAIBinder;
77
78 using AidlCameraMetadata = ::aidl::android::frameworks::cameraservice::device::CameraMetadata;
79 using RequestMetadataQueue = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
80
81 static constexpr int kCaptureRequestCount = 10;
82 static constexpr int kVGAImageWidth = 640;
83 static constexpr int kVGAImageHeight = 480;
84 static constexpr int kNumRequests = 4;
85
86 #define IDLE_TIMEOUT 2000000000 // ns
87
88 using scoped_unique_image_reader = std::unique_ptr<AImageReader, decltype(&AImageReader_delete)>;
89
90 class CameraServiceListener : public BnCameraServiceListener {
91 std::map<std::string, CameraDeviceStatus> mCameraStatuses;
92 // map: logical camera id -> set of unavailable physical camera ids
93 std::map<std::string, std::set<std::string>> mUnavailablePhysicalCameras;
94 mutable Mutex mLock;
95
96 public:
97 ~CameraServiceListener() override = default;
98
onStatusChanged(CameraDeviceStatus in_status,const std::string & in_cameraId)99 ndk::ScopedAStatus onStatusChanged(CameraDeviceStatus in_status,
100 const std::string& in_cameraId) override {
101 Mutex::Autolock l(mLock);
102 mCameraStatuses[in_cameraId] = in_status;
103 return ndk::ScopedAStatus::ok();
104 }
105
onPhysicalCameraStatusChanged(CameraDeviceStatus in_status,const std::string & in_cameraId,const std::string & in_physicalCameraId)106 ndk::ScopedAStatus onPhysicalCameraStatusChanged(
107 CameraDeviceStatus in_status, const std::string& in_cameraId,
108 const std::string& in_physicalCameraId) override {
109 Mutex::Autolock l(mLock);
110 ALOGI("%s: Physical camera %s : %s status changed to %d", __FUNCTION__, in_cameraId.c_str(),
111 in_physicalCameraId.c_str(), in_status);
112
113 EXPECT_NE(mCameraStatuses.find(in_cameraId), mCameraStatuses.end());
114 EXPECT_EQ(mCameraStatuses[in_cameraId], CameraDeviceStatus::STATUS_PRESENT);
115
116 if (in_status == CameraDeviceStatus::STATUS_PRESENT) {
117 auto res = mUnavailablePhysicalCameras[in_cameraId].erase(in_physicalCameraId);
118 EXPECT_EQ(res, 1);
119 } else {
120 auto res = mUnavailablePhysicalCameras[in_cameraId].emplace(in_physicalCameraId);
121 EXPECT_TRUE(res.second);
122 }
123 return ndk::ScopedAStatus::ok();
124 }
125
initializeStatuses(const std::vector<CameraStatusAndId> & statuses)126 void initializeStatuses(const std::vector<CameraStatusAndId>& statuses) {
127 Mutex::Autolock l(mLock);
128
129 for (auto& status : statuses) {
130 mCameraStatuses[status.cameraId] = status.deviceStatus;
131 for (auto& physicalId : status.unavailPhysicalCameraIds) {
132 mUnavailablePhysicalCameras[status.cameraId].emplace(physicalId);
133 }
134 }
135 }
136 };
137
138 // ICameraDeviceCallback implementation
139 class CameraDeviceCallback : public BnCameraDeviceCallback {
140 public:
141 enum LocalCameraDeviceStatus {
142 IDLE,
143 ERROR,
144 RUNNING,
145 RESULT_RECEIVED,
146 UNINITIALIZED,
147 REPEATING_REQUEST_ERROR,
148 };
149
150 protected:
151 bool mError = false;
152 LocalCameraDeviceStatus mLastStatus = UNINITIALIZED;
153 mutable std::vector<LocalCameraDeviceStatus> mStatusesHit;
154 // stream id -> prepared count;
155 mutable std::unordered_map<int, int> mStreamsPreparedCount;
156 mutable Mutex mLock;
157 mutable Condition mStatusCondition;
158 mutable Condition mPreparedCondition;
159
160 public:
CameraDeviceCallback()161 CameraDeviceCallback() {}
162
onDeviceError(ErrorCode in_errorCode,const CaptureResultExtras &)163 ndk::ScopedAStatus onDeviceError(ErrorCode in_errorCode,
164 const CaptureResultExtras& /*in_resultExtras*/) override {
165 ALOGE("%s: onDeviceError occurred with: %d", __FUNCTION__, static_cast<int>(in_errorCode));
166 Mutex::Autolock l(mLock);
167 mError = true;
168 mLastStatus = ERROR;
169 mStatusesHit.push_back(mLastStatus);
170 mStatusCondition.broadcast();
171 return ndk::ScopedAStatus::ok();
172 }
173
onDeviceIdle()174 ndk::ScopedAStatus onDeviceIdle() override {
175 Mutex::Autolock l(mLock);
176 mLastStatus = IDLE;
177 mStatusesHit.push_back(mLastStatus);
178 mStatusCondition.broadcast();
179 return ndk::ScopedAStatus::ok();
180 }
181
onCaptureStarted(const CaptureResultExtras &,int64_t)182 ndk::ScopedAStatus onCaptureStarted(const CaptureResultExtras& /*in_resultExtras*/,
183 int64_t /*in_timestamp*/) override {
184 Mutex::Autolock l(mLock);
185 mLastStatus = RUNNING;
186 mStatusesHit.push_back(mLastStatus);
187 mStatusCondition.broadcast();
188 return ndk::ScopedAStatus::ok();
189 }
190
onResultReceived(const CaptureMetadataInfo &,const CaptureResultExtras &,const std::vector<PhysicalCaptureResultInfo> &)191 ndk::ScopedAStatus onResultReceived(
192 const CaptureMetadataInfo& /*in_result*/, const CaptureResultExtras& /*in_resultExtras*/,
193 const std::vector<PhysicalCaptureResultInfo>& /*in_physicalCaptureResultInfos*/) override {
194 Mutex::Autolock l(mLock);
195 mLastStatus = RESULT_RECEIVED;
196 mStatusesHit.push_back(mLastStatus);
197 mStatusCondition.broadcast();
198 return ndk::ScopedAStatus::ok();
199 }
200
onRepeatingRequestError(int64_t,int32_t)201 ndk::ScopedAStatus onRepeatingRequestError(int64_t /*in_lastFrameNumber*/,
202 int32_t /*in_repeatingRequestId*/) override {
203 Mutex::Autolock l(mLock);
204 mLastStatus = REPEATING_REQUEST_ERROR;
205 mStatusesHit.push_back(mLastStatus);
206 mStatusCondition.broadcast();
207 return ndk::ScopedAStatus::ok();
208 }
209
onPrepared(int32_t streamId)210 ndk::ScopedAStatus onPrepared(int32_t streamId) override {
211 Mutex::Autolock l(mLock);
212 if (mStreamsPreparedCount.find(streamId) == mStreamsPreparedCount.end()) {
213 mStreamsPreparedCount[streamId] = 0;
214 }
215 mStreamsPreparedCount[streamId]++;
216 mPreparedCondition.broadcast();
217 return ndk::ScopedAStatus::ok();
218 }
219
waitForPreparedCount(int streamId,int count) const220 bool waitForPreparedCount(int streamId, int count) const {
221 Mutex::Autolock l(mLock);
222 if ((mStreamsPreparedCount.find(streamId) != mStreamsPreparedCount.end()) &&
223 (mStreamsPreparedCount[streamId] == count)) {
224 return true;
225 }
226
227 while ((mStreamsPreparedCount.find(streamId) == mStreamsPreparedCount.end()) ||
228 (mStreamsPreparedCount[streamId] < count)) {
229 if (mPreparedCondition.waitRelative(mLock, IDLE_TIMEOUT) != android::OK) {
230 return false;
231 }
232 }
233 return (mStreamsPreparedCount[streamId] == count);
234 }
235
236 // Test helper functions:
waitForStatus(LocalCameraDeviceStatus status) const237 bool waitForStatus(LocalCameraDeviceStatus status) const {
238 Mutex::Autolock l(mLock);
239 if (mLastStatus == status) {
240 return true;
241 }
242
243 while (std::find(mStatusesHit.begin(), mStatusesHit.end(), status) == mStatusesHit.end()) {
244 if (mStatusCondition.waitRelative(mLock, IDLE_TIMEOUT) != android::OK) {
245 mStatusesHit.clear();
246 return false;
247 }
248 }
249 mStatusesHit.clear();
250
251 return true;
252 }
253
waitForIdle() const254 bool waitForIdle() const { return waitForStatus(IDLE); }
255 };
256
convertFromAidlCloned(const AidlCameraMetadata & metadata,CameraMetadata * rawMetadata)257 static bool convertFromAidlCloned(const AidlCameraMetadata& metadata, CameraMetadata* rawMetadata) {
258 const camera_metadata* buffer = (camera_metadata_t*)(metadata.metadata.data());
259 size_t expectedSize = metadata.metadata.size();
260 int ret = validate_camera_metadata_structure(buffer, &expectedSize);
261 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
262 *rawMetadata = buffer; // assignment operator clones
263 } else {
264 ALOGE("%s: Malformed camera metadata received from caller", __FUNCTION__);
265 return false;
266 }
267 return true;
268 }
269
270 struct StreamConfiguration {
271 int32_t width = -1;
272 int32_t height = -1;
273 };
274
275 class VtsAidlCameraServiceTargetTest : public ::testing::TestWithParam<std::string> {
276 public:
SetUp()277 void SetUp() override {
278 bool success = ABinderProcess_setThreadPoolMaxThreadCount(5);
279 ASSERT_TRUE(success);
280 ABinderProcess_startThreadPool();
281
282 SpAIBinder cameraServiceBinder =
283 SpAIBinder(AServiceManager_checkService(GetParam().c_str()));
284 ASSERT_NE(cameraServiceBinder.get(), nullptr);
285
286 std::shared_ptr<ICameraService> cameraService =
287 ICameraService::fromBinder(cameraServiceBinder);
288 ASSERT_NE(cameraService.get(), nullptr);
289 mCameraService = cameraService;
290 }
291
TearDown()292 void TearDown() override {}
293
createOutputConfigurationMinimal()294 static OutputConfiguration createOutputConfigurationMinimal() {
295 OutputConfiguration output;
296 output.rotation = OutputConfiguration::Rotation::R0;
297 output.windowGroupId = -1;
298 output.width = 0;
299 output.height = 0;
300 output.isDeferred = false;
301 return output;
302 }
303
304 // creates an outputConfiguration with no deferred streams
createOutputConfiguration(const std::vector<native_handle_t * > & nhs)305 static OutputConfiguration createOutputConfiguration(const std::vector<native_handle_t*>& nhs) {
306 OutputConfiguration output = createOutputConfigurationMinimal();
307 output.windowHandles.reserve(nhs.size());
308 for (auto nh : nhs) {
309 output.windowHandles.push_back(::android::makeToAidl(nh));
310 }
311 return output;
312 }
313
createOutputConfiguration(const std::vector<ANativeWindow * > & windows)314 static OutputConfiguration createOutputConfiguration(
315 const std::vector<ANativeWindow*>& windows) {
316 OutputConfiguration output = createOutputConfigurationMinimal();
317 auto& surfaces = output.surfaces;
318 surfaces.reserve(windows.size());
319 for (auto anw : windows) {
320 surfaces.emplace_back(anw);
321 }
322 return output;
323 }
324
initializeCaptureRequestPartial(CaptureRequest * captureRequest,int32_t streamId,const std::string & cameraId,size_t settingsSize)325 static void initializeCaptureRequestPartial(CaptureRequest* captureRequest, int32_t streamId,
326 const std::string& cameraId, size_t settingsSize) {
327 captureRequest->physicalCameraSettings.resize(1);
328 captureRequest->physicalCameraSettings[0].id = cameraId;
329 captureRequest->streamAndWindowIds.resize(1);
330 captureRequest->streamAndWindowIds[0].streamId = streamId;
331 captureRequest->streamAndWindowIds[0].windowId = 0;
332 // Write the settings metadata into the fmq.
333 captureRequest->physicalCameraSettings[0]
334 .settings.set<CaptureMetadataInfo::fmqMetadataSize>(settingsSize);
335 }
336
doesCapabilityExist(const CameraMetadata & characteristics,int capability)337 static bool doesCapabilityExist(const CameraMetadata& characteristics, int capability) {
338 camera_metadata_ro_entry rawEntry =
339 characteristics.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
340 EXPECT_TRUE(rawEntry.count > 0);
341 for (size_t i = 0; i < rawEntry.count; i++) {
342 if (rawEntry.data.u8[i] == capability) {
343 return true;
344 }
345 }
346 return false;
347 }
348
isSecureOnlyDevice(const CameraMetadata & characteristics)349 static bool isSecureOnlyDevice(const CameraMetadata& characteristics) {
350 camera_metadata_ro_entry rawEntry =
351 characteristics.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
352 EXPECT_TRUE(rawEntry.count > 0);
353 if (rawEntry.count == 1 &&
354 rawEntry.data.u8[0] == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SECURE_IMAGE_DATA) {
355 return true;
356 }
357 return false;
358 }
359
360 // Return the first advertised available stream sizes for the given format
361 // and use-case.
getStreamConfiguration(const CameraMetadata & characteristics,uint32_t tag,int32_t chosenUse,int32_t chosenFormat)362 static StreamConfiguration getStreamConfiguration(const CameraMetadata& characteristics,
363 uint32_t tag, int32_t chosenUse,
364 int32_t chosenFormat) {
365 camera_metadata_ro_entry rawEntry = characteristics.find(tag);
366 StreamConfiguration streamConfig;
367 const size_t STREAM_FORMAT_OFFSET = 0;
368 const size_t STREAM_WIDTH_OFFSET = 1;
369 const size_t STREAM_HEIGHT_OFFSET = 2;
370 const size_t STREAM_INOUT_OFFSET = 3;
371 const size_t STREAM_CONFIG_SIZE = 4;
372 if (rawEntry.count < STREAM_CONFIG_SIZE) {
373 return streamConfig;
374 }
375 EXPECT_TRUE((rawEntry.count % STREAM_CONFIG_SIZE) == 0);
376 for (size_t i = 0; i < rawEntry.count; i += STREAM_CONFIG_SIZE) {
377 int32_t format = rawEntry.data.i32[i + STREAM_FORMAT_OFFSET];
378 int32_t use = rawEntry.data.i32[i + STREAM_INOUT_OFFSET];
379 if (format == chosenFormat && use == chosenUse) {
380 streamConfig.width = rawEntry.data.i32[i + STREAM_WIDTH_OFFSET];
381 streamConfig.height = rawEntry.data.i32[i + STREAM_HEIGHT_OFFSET];
382 return streamConfig;
383 }
384 }
385 return streamConfig;
386 }
387 template <class Func>
BasicCameraTests(bool prepareWindows,Func prepareOutputConfiguration)388 void BasicCameraTests(bool prepareWindows, Func prepareOutputConfiguration) {
389 std::shared_ptr<CameraServiceListener> listener =
390 ::ndk::SharedRefBase::make<CameraServiceListener>();
391 std::vector<CameraStatusAndId> cameraStatuses;
392
393 ndk::ScopedAStatus ret = mCameraService->addListener(listener, &cameraStatuses);
394 EXPECT_TRUE(ret.isOk());
395 listener->initializeStatuses(cameraStatuses);
396 for (const auto& it : cameraStatuses) {
397 CameraMetadata rawMetadata;
398 if (it.deviceStatus != CameraDeviceStatus::STATUS_PRESENT) {
399 continue;
400 }
401 AidlCameraMetadata aidlMetadata;
402 ret = mCameraService->getCameraCharacteristics(it.cameraId, &aidlMetadata);
403 EXPECT_TRUE(ret.isOk());
404 bool cStatus = convertFromAidlCloned(aidlMetadata, &rawMetadata);
405 EXPECT_TRUE(cStatus);
406 EXPECT_FALSE(rawMetadata.isEmpty());
407
408 std::shared_ptr<CameraDeviceCallback> callbacks =
409 ndk::SharedRefBase::make<CameraDeviceCallback>();
410 std::shared_ptr<ICameraDeviceUser> deviceRemote = nullptr;
411 ret = mCameraService->connectDevice(callbacks, it.cameraId, &deviceRemote);
412 EXPECT_TRUE(ret.isOk());
413 EXPECT_TRUE(deviceRemote != nullptr);
414
415 MQDescriptor<int8_t, SynchronizedReadWrite> mqDesc;
416 ret = deviceRemote->getCaptureRequestMetadataQueue(&mqDesc);
417 EXPECT_TRUE(ret.isOk());
418 std::shared_ptr<RequestMetadataQueue> requestMQ =
419 std::make_shared<RequestMetadataQueue>(mqDesc);
420 EXPECT_TRUE(requestMQ->isValid());
421 EXPECT_TRUE((requestMQ->availableToWrite() >= 0));
422
423 bool isDepthOnlyDevice =
424 !doesCapabilityExist(rawMetadata,
425 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE) &&
426 doesCapabilityExist(rawMetadata,
427 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT);
428 int chosenImageFormat = AIMAGE_FORMAT_YUV_420_888;
429 int chosenImageWidth = kVGAImageWidth;
430 int chosenImageHeight = kVGAImageHeight;
431 bool isSecureOnlyCamera = isSecureOnlyDevice(rawMetadata);
432 status_t mStatus = OK;
433 scoped_unique_image_reader readerPtr(nullptr, AImageReader_delete);
434
435 if (isSecureOnlyCamera) {
436 AImageReader* reader = nullptr;
437 StreamConfiguration secureStreamConfig = getStreamConfiguration(
438 rawMetadata, ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
439 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT,
440 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED);
441 EXPECT_TRUE(secureStreamConfig.width != -1);
442 EXPECT_TRUE(secureStreamConfig.height != -1);
443 chosenImageFormat = AIMAGE_FORMAT_PRIVATE;
444 chosenImageWidth = secureStreamConfig.width;
445 chosenImageHeight = secureStreamConfig.height;
446 mStatus = AImageReader_newWithUsage(
447 chosenImageWidth, chosenImageHeight, chosenImageFormat,
448 AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT, kCaptureRequestCount, &reader);
449 EXPECT_EQ(mStatus, AMEDIA_OK);
450 readerPtr = scoped_unique_image_reader(reader, AImageReader_delete);
451 } else {
452 if (isDepthOnlyDevice) {
453 StreamConfiguration depthStreamConfig = getStreamConfiguration(
454 rawMetadata, ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS,
455 ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS_OUTPUT,
456 HAL_PIXEL_FORMAT_Y16);
457 EXPECT_TRUE(depthStreamConfig.width != -1);
458 EXPECT_TRUE(depthStreamConfig.height != -1);
459 chosenImageFormat = AIMAGE_FORMAT_DEPTH16;
460 chosenImageWidth = depthStreamConfig.width;
461 chosenImageHeight = depthStreamConfig.height;
462 }
463 AImageReader* reader = nullptr;
464 mStatus = AImageReader_new(chosenImageWidth, chosenImageHeight, chosenImageFormat,
465 kCaptureRequestCount, &reader);
466 EXPECT_EQ(mStatus, AMEDIA_OK);
467 readerPtr = scoped_unique_image_reader(reader, AImageReader_delete);
468 }
469
470 OutputConfiguration output = prepareOutputConfiguration(readerPtr);
471
472 ret = deviceRemote->beginConfigure();
473 EXPECT_TRUE(ret.isOk());
474
475 int32_t streamId = -1;
476 ret = deviceRemote->createStream(output, &streamId);
477 EXPECT_TRUE(ret.isOk());
478 EXPECT_TRUE(streamId >= 0);
479
480 AidlCameraMetadata sessionParams;
481 ret = deviceRemote->endConfigure(StreamConfigurationMode::NORMAL_MODE, sessionParams,
482 systemTime());
483 EXPECT_TRUE(ret.isOk());
484
485 if (prepareWindows) {
486 ret = deviceRemote->prepare(streamId);
487 EXPECT_TRUE(ret.isOk());
488 EXPECT_TRUE(callbacks->waitForPreparedCount(streamId, 1));
489
490 ret = deviceRemote->prepare(streamId);
491 // We should get another callback;
492 EXPECT_TRUE(ret.isOk());
493 EXPECT_TRUE(callbacks->waitForPreparedCount(streamId, 2));
494 }
495 AidlCameraMetadata aidlSettingsMetadata;
496 ret = deviceRemote->createDefaultRequest(TemplateId::PREVIEW, &aidlSettingsMetadata);
497 EXPECT_TRUE(ret.isOk());
498 EXPECT_GE(aidlSettingsMetadata.metadata.size(), 0);
499 std::vector<CaptureRequest> captureRequests;
500 captureRequests.resize(kNumRequests);
501 for (int i = 0; i < kNumRequests; i++) {
502 CaptureRequest& captureRequest = captureRequests[i];
503 initializeCaptureRequestPartial(&captureRequest, streamId, it.cameraId,
504 aidlSettingsMetadata.metadata.size());
505 // Write the settings metadata into the fmq.
506 bool written = requestMQ->write(
507 reinterpret_cast<int8_t*>(aidlSettingsMetadata.metadata.data()),
508 aidlSettingsMetadata.metadata.size());
509 EXPECT_TRUE(written);
510 }
511
512 SubmitInfo info;
513 // Test a single capture
514 ret = deviceRemote->submitRequestList(captureRequests, false, &info);
515 EXPECT_TRUE(ret.isOk());
516 EXPECT_GE(info.requestId, 0);
517 EXPECT_TRUE(callbacks->waitForStatus(
518 CameraDeviceCallback::LocalCameraDeviceStatus::RESULT_RECEIVED));
519 EXPECT_TRUE(callbacks->waitForIdle());
520
521 // Test repeating requests
522 CaptureRequest captureRequest;
523 initializeCaptureRequestPartial(&captureRequest, streamId, it.cameraId,
524 aidlSettingsMetadata.metadata.size());
525
526 bool written =
527 requestMQ->write(reinterpret_cast<int8_t*>(aidlSettingsMetadata.metadata.data()),
528 aidlSettingsMetadata.metadata.size());
529 EXPECT_TRUE(written);
530
531 ret = deviceRemote->submitRequestList({captureRequest}, true, &info);
532 EXPECT_TRUE(ret.isOk());
533 EXPECT_TRUE(callbacks->waitForStatus(
534 CameraDeviceCallback::LocalCameraDeviceStatus::RESULT_RECEIVED));
535
536 int64_t lastFrameNumber = -1;
537 ret = deviceRemote->cancelRepeatingRequest(&lastFrameNumber);
538 EXPECT_TRUE(ret.isOk());
539 EXPECT_GE(lastFrameNumber, 0);
540
541 // Test waitUntilIdle()
542 auto statusRet = deviceRemote->waitUntilIdle();
543 EXPECT_TRUE(statusRet.isOk());
544
545 // Test deleteStream()
546 statusRet = deviceRemote->deleteStream(streamId);
547 EXPECT_TRUE(statusRet.isOk());
548
549 ret = deviceRemote->disconnect();
550 EXPECT_TRUE(ret.isOk());
551 }
552 ret = mCameraService->removeListener(listener);
553 EXPECT_TRUE(ret.isOk());
554 }
555
556 std::shared_ptr<ICameraService> mCameraService = nullptr;
557 };
558
559 // Basic AIDL calls for ICameraService
TEST_P(VtsAidlCameraServiceTargetTest,BasicCameraLifeCycleTest)560 TEST_P(VtsAidlCameraServiceTargetTest, BasicCameraLifeCycleTest) {
561 auto prepareOutputConfigurationWh = [](scoped_unique_image_reader& readerPtr) {
562 native_handle_t* wh = nullptr;
563 status_t status = AImageReader_getWindowNativeHandle(readerPtr.get(), &wh);
564 EXPECT_TRUE(status == AMEDIA_OK && wh != nullptr);
565
566 return createOutputConfiguration({wh});
567 };
568 auto prepareOutputConfigurationSurface = [](scoped_unique_image_reader& readerPtr) {
569 ANativeWindow* anw = nullptr;
570 status_t status = AImageReader_getWindow(readerPtr.get(), &anw);
571 EXPECT_TRUE(status == AMEDIA_OK && anw != nullptr);
572
573 return createOutputConfiguration({anw});
574 };
575
576 if (isHidlSupported()) {
577 BasicCameraTests(/*prepareWindows*/ false, prepareOutputConfigurationWh);
578 BasicCameraTests(/*prepareWindows*/ true, prepareOutputConfigurationWh);
579 }
580 // The framework supports AIDL interface version 2
581 BasicCameraTests(/*prepareWindows*/ false, prepareOutputConfigurationSurface);
582 BasicCameraTests(/*prepareWindows*/ true, prepareOutputConfigurationSurface);
583 }
584
TEST_P(VtsAidlCameraServiceTargetTest,CameraServiceListenerTest)585 TEST_P(VtsAidlCameraServiceTargetTest, CameraServiceListenerTest) {
586 std::shared_ptr<CameraServiceListener> listener =
587 ndk::SharedRefBase::make<CameraServiceListener>();
588 if (mCameraService == nullptr) return;
589
590 std::vector<CameraStatusAndId> cameraStatuses;
591 ndk::ScopedAStatus ret = mCameraService->addListener(listener, &cameraStatuses);
592 EXPECT_TRUE(ret.isOk());
593 listener->initializeStatuses(cameraStatuses);
594
595 for (const auto& it : cameraStatuses) {
596 CameraMetadata rawMetadata;
597 AidlCameraMetadata aidlCameraMetadata;
598 ret = mCameraService->getCameraCharacteristics(it.cameraId, &aidlCameraMetadata);
599 EXPECT_TRUE(ret.isOk());
600 bool cStatus = convertFromAidlCloned(aidlCameraMetadata, &rawMetadata);
601 EXPECT_TRUE(cStatus);
602 EXPECT_FALSE(rawMetadata.isEmpty());
603
604 bool isLogicalCamera = doesCapabilityExist(
605 rawMetadata, ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA);
606 if (!isLogicalCamera) {
607 EXPECT_TRUE(it.unavailPhysicalCameraIds.empty());
608 continue;
609 }
610 camera_metadata_entry entry = rawMetadata.find(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS);
611 EXPECT_GT(entry.count, 0);
612
613 std::unordered_set<std::string> validPhysicalIds;
614 const uint8_t* ids = entry.data.u8;
615 size_t start = 0;
616 for (size_t i = 0; i < entry.count; i++) {
617 if (ids[i] == '\0') {
618 if (start != i) {
619 std::string currentId(reinterpret_cast<const char*>(ids + start));
620 validPhysicalIds.emplace(currentId);
621 }
622 start = i + 1;
623 }
624 }
625
626 std::unordered_set<std::string> unavailablePhysicalIds(it.unavailPhysicalCameraIds.begin(),
627 it.unavailPhysicalCameraIds.end());
628 EXPECT_EQ(unavailablePhysicalIds.size(), it.unavailPhysicalCameraIds.size());
629 for (auto& unavailablePhysicalId : unavailablePhysicalIds) {
630 EXPECT_NE(validPhysicalIds.find(unavailablePhysicalId), validPhysicalIds.end());
631 }
632 }
633
634 ret = mCameraService->removeListener(listener);
635 EXPECT_TRUE(ret.isOk());
636 }
637
638 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VtsAidlCameraServiceTargetTest);
639 INSTANTIATE_TEST_SUITE_P(PerInstance, VtsAidlCameraServiceTargetTest,
640 testing::ValuesIn({std::string(ICameraService::descriptor) + "/default"}),
641 android::hardware::PrintInstanceNameToString);
642
643 } // namespace android
644