/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include "securec.h" #include "camera_metadata_info.h" #include "camera.h" #include "ibuffer.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "ioffline_stream_operator.h" #include #include #include #include #include #include #include "icamera_host.h" #include "camera_host_proxy.h" #include "camera_device_callback.h" #include "camera_host_callback.h" #include "icamera_device.h" #include "stream_operator_callback.h" #include "istream_operator_callback.h" using namespace OHOS; using namespace testing::ext; using namespace OHOS::Camera; #ifdef HDF_LOG_TAG #undef HDF_LOG_TAG #endif #define HDF_LOG_TAG camera_service_test constexpr const char *TEST_SERVICE_NAME = "camera_service"; class TestStreamConsumerListener : public IBufferConsumerListener { public: TestStreamConsumerListener() { } ~TestStreamConsumerListener() { } void OnBufferAvailable() { } }; class StreamConsumer { public: OHOS::sptr CreateProducer(const std::function& callback) { consumer_ = OHOS::Surface::CreateSurfaceAsConsumer(); if (consumer_ == nullptr) { return nullptr; } sptr listener = new TestStreamConsumerListener(); consumer_->RegisterConsumerListener(listener); auto producer = consumer_->GetProducer(); std::cout << "create a buffer queue producer: " << producer.GetRefPtr() << std::endl; if (producer == nullptr) { return nullptr; } callback_ = callback; consumerThread_ = new std::thread([this] { int64_t timestamp = 0; int32_t flushFence = 0; OHOS::Rect damage; OHOS::BufferRequestConfig config; while (running_ == true) { OHOS::sptr buffer = nullptr; consumer_->AcquireBuffer(buffer, flushFence, timestamp, damage); if (buffer != nullptr) { uint32_t size = buffer->GetSize(); void* addr = buffer->GetVirAddr(); if (callback_ != nullptr) { callback_(addr, size); } consumer_->ReleaseBuffer(buffer, -1); shotCount_--; if (shotCount_ == 0) { std::unique_lock l(l_); cv_.notify_one(); } } } }); return producer; } void WaitSnapshotEnd() const { std::unique_lock l(l_); cv_.wait(l, [this]() { return shotCount_ == 0; }); } void TakeSnapshot() { shotCount_++; } ~StreamConsumer() { running_ = false; if (consumerThread_ != nullptr) { consumerThread_->join(); delete consumerThread_; } } public: std::atomic shotCount_ = 0; std::mutex l_; std::condition_variable cv_; bool running_ = true; OHOS::sptr consumer_ = nullptr; std::thread* consumerThread_ = nullptr; std::function callback_ = nullptr; }; class CameraRemoteTest : public testing::Test { public: static void SetUpTestCase() {} static void TearDownTestCase() {} void SetUp() const {} void TearDown() const {} }; HWTEST_F(CameraRemoteTest, HostSetCallback, TestSize.Level0) { sptr sampleObj = ICameraHost::Get(TEST_SERVICE_NAME); if (sampleObj == nullptr) { std::cout << "ICameraHost get failed." << std::endl; return; } ASSERT_NE(nullptr, sampleObj); OHOS::sptr callback = new CameraHostCallback(); sampleObj->SetCallback(callback); std::vector cameraIds; Camera::CamRetCode ret = sampleObj->GetCameraIds(cameraIds); std::cout << "GetCameraIds ret = " << ret << std::endl; std::shared_ptr ability; std::string cameraId = cameraIds.front(); ret |= sampleObj->GetCameraAbility(cameraId, ability); common_metadata_header_t *meta = ability->get(); size_t tagCount = get_camera_metadata_data_count(meta); std::cout << "CameraRemoteTest tagcount = " << tagCount << std::endl; OHOS::sptr deviceCallback = new CameraDeviceCallback(); OHOS::sptr cameraDevice = nullptr; ret |= sampleObj->OpenCamera(cameraId, deviceCallback, cameraDevice); if (cameraDevice == nullptr) { return; } std::cout << "sampleObj->OpenCamera ret = " << ret << std::endl; ret |= cameraDevice->UpdateSettings(ability); std::cout << "UpdateSettings camRetCode = " << ret << std::endl; OHOS::Camera::ResultCallbackMode resultCallbackMode = OHOS::Camera::ON_CHANGED; ret |= cameraDevice->SetResultMode(resultCallbackMode); std::cout << "cameraDevice->SetResultMode = " << ret << std::endl; std::vector results; results.push_back(OHOS_SENSOR_EXPOSURE_TIME); results.push_back(OHOS_SENSOR_COLOR_CORRECTION_GAINS); ret |= cameraDevice->EnableResult(results); std::cout << "cameraDevice->EnableResult = " << ret << std::endl; std::vector disable_tag; ret |= cameraDevice->GetEnabledResults(disable_tag); std::cout << "cameraDevice->GetEnabledResults = " << ret << std::endl; ret |= cameraDevice->DisableResult(disable_tag); std::cout << "cameraDevice->DisableResult = " << ret << std::endl; OHOS::sptr streamOperatorCallback = new StreamOperatorCallback(); OHOS::sptr streamOperator = nullptr; ret |= cameraDevice->GetStreamOperator(streamOperatorCallback, streamOperator); std::cout << "GetStreamOperator camRetCode = " << ret << std::endl; OHOS::Camera::OperationMode operationMode = NORMAL; StreamSupportType supportType; std::vector> streamInfos; std::shared_ptr streamInfo = std::make_shared(); streamInfo->streamId_ = 1001; streamInfo->width_ = 720; streamInfo->height_ = 480; streamInfo->format_ = PIXEL_FMT_YCRCB_420_SP; streamInfo->dataspace_ = 8; streamInfo->intent_ = Camera::PREVIEW; StreamConsumer previewConsumer; streamInfo->bufferQueue_ = previewConsumer.CreateProducer( [](void* addr, uint32_t size) { CAMERA_LOGI("received a preview buffer ..."); }); streamInfo->tunneledMode_ = 5; streamInfos.push_back(streamInfo); ret |= streamOperator->IsStreamsSupported(NORMAL, ability, streamInfo, supportType); std::cout << "streamOperator->IsStreamsSupported = " << ret << std::endl; std::shared_ptr streamInfoSnapshot = std::make_shared(); streamInfoSnapshot->streamId_ = 1002; streamInfoSnapshot->width_ = 1920; streamInfoSnapshot->height_ = 1080; streamInfoSnapshot->format_ = PIXEL_FMT_YCRCB_420_SP; streamInfoSnapshot->dataspace_ = 8; streamInfoSnapshot->intent_ = Camera::STILL_CAPTURE; StreamConsumer snapshotConsumer; streamInfoSnapshot->bufferQueue_ = snapshotConsumer.CreateProducer( [](void* addr, uint32_t size) { std::cout << "received a snapshot buffer ..." << std::endl; }); streamInfoSnapshot->tunneledMode_ = 5; streamInfos.push_back(streamInfoSnapshot); ret |= streamOperator->CreateStreams(streamInfos); std::cout << "streamOperator->CreateStreams = " << ret << std::endl; ret |= streamOperator->CommitStreams(OHOS::Camera::NORMAL, ability); std::cout << "streamOperator->CommitStreams = " << ret << std::endl; int captureId = 2001; std::shared_ptr captureInfo = std::make_shared(); captureInfo->streamIds_ = {streamInfo->streamId_, streamInfoSnapshot->streamId_}; captureInfo->captureSetting_ = ability; captureInfo->enableShutterCallback_ = false; ret |= streamOperator->Capture(captureId, captureInfo, true); std::cout << "streamOperator->Capture = " << ret << std::endl; sleep(10); std::vector> attributes; ret |= streamOperator->GetStreamAttributes(attributes); std::cout << "streamOperator->GetStreamAttributes = " << ret << std::endl; OHOS::sptr offlineOperator = nullptr; OHOS::sptr offlineOperatorCallback = streamOperatorCallback; ret |= streamOperator->ChangeToOfflineStream({streamInfoSnapshot->streamId_}, offlineOperatorCallback, offlineOperator); std::cout << "4streamOperator->ChangeToOfflineStream ret = " << ret << std::endl; ASSERT_EQ(Camera::NO_ERROR, ret); ASSERT_NE(nullptr, offlineOperator); cameraDevice->Close(); sleep(10); ret = offlineOperator->Release(); ASSERT_EQ(Camera::NO_ERROR, ret); }