1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <thread> 17 #include <unistd.h> 18 #include <vector> 19 #include <gtest/gtest.h> 20 #include <map> 21 #include <hdf_log.h> 22 #include <osal_mem.h> 23 #include "securec.h" 24 25 #include "camera_metadata_info.h" 26 #include "camera.h" 27 #include "ibuffer.h" 28 #include "if_system_ability_manager.h" 29 #include "iservice_registry.h" 30 #include "ioffline_stream_operator.h" 31 #include <surface.h> 32 #include <display_type.h> 33 #include <stdio.h> 34 #include <fcntl.h> 35 #include <sys/ioctl.h> 36 #include <sys/wait.h> 37 38 #include "icamera_host.h" 39 #include "camera_host_proxy.h" 40 #include "camera_device_callback.h" 41 #include "camera_host_callback.h" 42 #include "icamera_device.h" 43 #include "stream_operator_callback.h" 44 #include "istream_operator_callback.h" 45 46 using namespace OHOS; 47 using namespace testing::ext; 48 using namespace OHOS::Camera; 49 50 #ifdef HDF_LOG_TAG 51 #undef HDF_LOG_TAG 52 #endif 53 54 #define HDF_LOG_TAG camera_service_test 55 56 constexpr const char *TEST_SERVICE_NAME = "camera_service"; 57 58 class TestStreamConsumerListener : public IBufferConsumerListener { 59 public: TestStreamConsumerListener()60 TestStreamConsumerListener() 61 { 62 } 63 ~TestStreamConsumerListener()64 ~TestStreamConsumerListener() 65 { 66 } 67 OnBufferAvailable()68 void OnBufferAvailable() 69 { 70 } 71 }; 72 73 class StreamConsumer { 74 public: CreateProducer(const std::function<void (void *,uint32_t)> & callback)75 OHOS::sptr<OHOS::IBufferProducer> CreateProducer(const std::function<void(void*, uint32_t)>& callback) 76 { 77 consumer_ = OHOS::Surface::CreateSurfaceAsConsumer(); 78 if (consumer_ == nullptr) { 79 return nullptr; 80 } 81 sptr<IBufferConsumerListener> listener = new TestStreamConsumerListener(); 82 consumer_->RegisterConsumerListener(listener); 83 84 auto producer = consumer_->GetProducer(); 85 std::cout << "create a buffer queue producer: " << producer.GetRefPtr() << std::endl; 86 87 if (producer == nullptr) { 88 return nullptr; 89 } 90 callback_ = callback; 91 92 consumerThread_ = new std::thread([this] { 93 int64_t timestamp = 0; 94 int32_t flushFence = 0; 95 OHOS::Rect damage; 96 OHOS::BufferRequestConfig config; 97 while (running_ == true) { 98 OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr; 99 consumer_->AcquireBuffer(buffer, flushFence, timestamp, damage); 100 if (buffer != nullptr) { 101 uint32_t size = buffer->GetSize(); 102 void* addr = buffer->GetVirAddr(); 103 if (callback_ != nullptr) { 104 callback_(addr, size); 105 } 106 consumer_->ReleaseBuffer(buffer, -1); 107 shotCount_--; 108 if (shotCount_ == 0) { 109 std::unique_lock<std::mutex> l(l_); 110 cv_.notify_one(); 111 } 112 } 113 } 114 }); 115 116 return producer; 117 } 118 WaitSnapshotEnd() const119 void WaitSnapshotEnd() const 120 { 121 std::unique_lock<std::mutex> l(l_); 122 cv_.wait(l, [this]() { return shotCount_ == 0; }); 123 } 124 TakeSnapshot()125 void TakeSnapshot() 126 { 127 shotCount_++; 128 } 129 ~StreamConsumer()130 ~StreamConsumer() 131 { 132 running_ = false; 133 if (consumerThread_ != nullptr) { 134 consumerThread_->join(); 135 delete consumerThread_; 136 } 137 } 138 139 public: 140 std::atomic<uint64_t> shotCount_ = 0; 141 std::mutex l_; 142 std::condition_variable cv_; 143 bool running_ = true; 144 OHOS::sptr<OHOS::Surface> consumer_ = nullptr; 145 std::thread* consumerThread_ = nullptr; 146 std::function<void(void*, uint32_t)> callback_ = nullptr; 147 }; 148 149 class CameraRemoteTest : public testing::Test { 150 public: SetUpTestCase()151 static void SetUpTestCase() {} TearDownTestCase()152 static void TearDownTestCase() {} SetUp() const153 void SetUp() const {} TearDown() const154 void TearDown() const {} 155 }; 156 157 HWTEST_F(CameraRemoteTest, HostSetCallback, TestSize.Level0) 158 { 159 sptr<ICameraHost> sampleObj = ICameraHost::Get(TEST_SERVICE_NAME); 160 if (sampleObj == nullptr) { 161 std::cout << "ICameraHost get failed." << std::endl; 162 return; 163 } 164 ASSERT_NE(nullptr, sampleObj); 165 166 OHOS::sptr<CameraHostCallback> callback = new CameraHostCallback(); 167 sampleObj->SetCallback(callback); 168 169 std::vector<std::string> cameraIds; 170 Camera::CamRetCode ret = sampleObj->GetCameraIds(cameraIds); 171 std::cout << "GetCameraIds ret = " << ret << std::endl; 172 173 std::shared_ptr<CameraAbility> ability; 174 std::string cameraId = cameraIds.front(); 175 176 ret |= sampleObj->GetCameraAbility(cameraId, ability); 177 common_metadata_header_t *meta = ability->get(); 178 size_t tagCount = get_camera_metadata_data_count(meta); 179 std::cout << "CameraRemoteTest tagcount = " << tagCount << std::endl; 180 181 OHOS::sptr<CameraDeviceCallback> deviceCallback = new CameraDeviceCallback(); 182 OHOS::sptr<ICameraDevice> cameraDevice = nullptr; 183 ret |= sampleObj->OpenCamera(cameraId, deviceCallback, cameraDevice); 184 if (cameraDevice == nullptr) { 185 return; 186 } 187 std::cout << "sampleObj->OpenCamera ret = " << ret << std::endl; 188 189 ret |= cameraDevice->UpdateSettings(ability); 190 std::cout << "UpdateSettings camRetCode = " << ret << std::endl; 191 192 OHOS::Camera::ResultCallbackMode resultCallbackMode = OHOS::Camera::ON_CHANGED; 193 ret |= cameraDevice->SetResultMode(resultCallbackMode); 194 std::cout << "cameraDevice->SetResultMode = " << ret << std::endl; 195 196 std::vector<OHOS::Camera::MetaType> results; 197 results.push_back(OHOS_SENSOR_EXPOSURE_TIME); 198 results.push_back(OHOS_SENSOR_COLOR_CORRECTION_GAINS); 199 ret |= cameraDevice->EnableResult(results); 200 std::cout << "cameraDevice->EnableResult = " << ret << std::endl; 201 202 std::vector<Camera::MetaType> disable_tag; 203 ret |= cameraDevice->GetEnabledResults(disable_tag); 204 std::cout << "cameraDevice->GetEnabledResults = " << ret << std::endl; 205 206 ret |= cameraDevice->DisableResult(disable_tag); 207 std::cout << "cameraDevice->DisableResult = " << ret << std::endl; 208 209 OHOS::sptr<StreamOperatorCallback> streamOperatorCallback = new StreamOperatorCallback(); 210 OHOS::sptr<IStreamOperator> streamOperator = nullptr; 211 ret |= cameraDevice->GetStreamOperator(streamOperatorCallback, streamOperator); 212 std::cout << "GetStreamOperator camRetCode = " << ret << std::endl; 213 214 OHOS::Camera::OperationMode operationMode = NORMAL; 215 StreamSupportType supportType; 216 std::vector<std::shared_ptr<Camera::StreamInfo>> streamInfos; 217 std::shared_ptr<Camera::StreamInfo> streamInfo = std::make_shared<Camera::StreamInfo>(); 218 streamInfo->streamId_ = 1001; 219 streamInfo->width_ = 720; 220 streamInfo->height_ = 480; 221 streamInfo->format_ = PIXEL_FMT_YCRCB_420_SP; 222 streamInfo->dataspace_ = 8; 223 streamInfo->intent_ = Camera::PREVIEW; 224 StreamConsumer previewConsumer; 225 streamInfo->bufferQueue_ = previewConsumer.CreateProducer( __anon2aa40d1a0302(void* addr, uint32_t size) 226 [](void* addr, uint32_t size) { 227 CAMERA_LOGI("received a preview buffer ..."); }); 228 streamInfo->tunneledMode_ = 5; 229 streamInfos.push_back(streamInfo); 230 ret |= streamOperator->IsStreamsSupported(NORMAL, ability, streamInfo, supportType); 231 std::cout << "streamOperator->IsStreamsSupported = " << ret << std::endl; 232 233 std::shared_ptr<Camera::StreamInfo> streamInfoSnapshot = std::make_shared<Camera::StreamInfo>(); 234 streamInfoSnapshot->streamId_ = 1002; 235 streamInfoSnapshot->width_ = 1920; 236 streamInfoSnapshot->height_ = 1080; 237 streamInfoSnapshot->format_ = PIXEL_FMT_YCRCB_420_SP; 238 streamInfoSnapshot->dataspace_ = 8; 239 streamInfoSnapshot->intent_ = Camera::STILL_CAPTURE; 240 StreamConsumer snapshotConsumer; 241 streamInfoSnapshot->bufferQueue_ = snapshotConsumer.CreateProducer( __anon2aa40d1a0402(void* addr, uint32_t size) 242 [](void* addr, uint32_t size) { 243 std::cout << "received a snapshot buffer ..." << std::endl; }); 244 streamInfoSnapshot->tunneledMode_ = 5; 245 streamInfos.push_back(streamInfoSnapshot); 246 247 ret |= streamOperator->CreateStreams(streamInfos); 248 std::cout << "streamOperator->CreateStreams = " << ret << std::endl; 249 250 ret |= streamOperator->CommitStreams(OHOS::Camera::NORMAL, ability); 251 std::cout << "streamOperator->CommitStreams = " << ret << std::endl; 252 253 int captureId = 2001; 254 std::shared_ptr<OHOS::Camera::CaptureInfo> captureInfo = std::make_shared<OHOS::Camera::CaptureInfo>(); 255 captureInfo->streamIds_ = {streamInfo->streamId_, streamInfoSnapshot->streamId_}; 256 captureInfo->captureSetting_ = ability; 257 captureInfo->enableShutterCallback_ = false; 258 ret |= streamOperator->Capture(captureId, captureInfo, true); 259 std::cout << "streamOperator->Capture = " << ret << std::endl; 260 sleep(10); 261 262 std::vector<std::shared_ptr<StreamAttribute>> attributes; 263 ret |= streamOperator->GetStreamAttributes(attributes); 264 std::cout << "streamOperator->GetStreamAttributes = " << ret << std::endl; 265 266 OHOS::sptr<IOfflineStreamOperator> offlineOperator = nullptr; 267 OHOS::sptr<IStreamOperatorCallback> offlineOperatorCallback = streamOperatorCallback; 268 ret |= streamOperator->ChangeToOfflineStream({streamInfoSnapshot->streamId_}, 269 offlineOperatorCallback, offlineOperator); 270 std::cout << "4streamOperator->ChangeToOfflineStream ret = " << ret << std::endl; 271 ASSERT_EQ(Camera::NO_ERROR, ret); 272 ASSERT_NE(nullptr, offlineOperator); 273 cameraDevice->Close(); 274 sleep(10); 275 ret = offlineOperator->Release(); 276 ASSERT_EQ(Camera::NO_ERROR, ret); 277 } 278