1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file expected 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 "hdi_common_v1_2.h"
17 #include "camera.h"
18 #include "video_key_info.h"
19 int64_t OHOS::Camera::Test::StreamConsumer::g_timestamp[2] = {0};
20 namespace OHOS::Camera {
21 Test::ResultCallback Test::resultCallback_ = 0;
22 OHOS::HDI::Camera::V1_0::FlashlightStatus Test::statusCallback =
23 static_cast<OHOS::HDI::Camera::V1_0::FlashlightStatus>(0);
GetCurrentLocalTimeStamp()24 uint64_t Test::GetCurrentLocalTimeStamp()
25 {
26 std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
27 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
28 auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
29 return static_cast<uint64_t>(tmp.count());
30 }
31
DumpImageFile(int streamId,std::string suffix,const void * buffer,int32_t size)32 int32_t Test::DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size)
33 {
34 if (imageDataSaveSwitch == SWITCH_OFF) {
35 return 0;
36 }
37 if (streamId < 0) {
38 CAMERA_LOGE("ivalid stream id: %{public}d", streamId);
39 return -1;
40 }
41 char mkdirCmd[PATH_MAX] = {0};
42 char path[PATH_MAX] = {0};
43 int ret = sprintf_s(mkdirCmd, sizeof(mkdirCmd) / sizeof(mkdirCmd[0]),
44 "mkdir -p /data/stream-%d", streamId);
45 if (ret < 0) {
46 return -1;
47 }
48 system(mkdirCmd);
49 ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/stream-%d/%lld.%s",
50 streamId, GetCurrentLocalTimeStamp(), suffix.c_str());
51 if (ret < 0) {
52 return -1;
53 }
54
55 int imgFd = open(path, O_RDWR | O_CREAT, 00766);
56 if (imgFd == -1) {
57 CAMERA_LOGE("open file failed, errno: %{public}s", strerror(errno));
58 return -1;
59 }
60
61 ret = write(imgFd, buffer, size);
62 if (ret == -1) {
63 CAMERA_LOGE("write file failed, error: %{public}s", strerror(errno));
64 close(imgFd);
65 return -1;
66 }
67 close(imgFd);
68 return 0;
69 }
70
Init()71 void Test::Init()
72 {
73 uint32_t mainVer;
74 uint32_t minVer;
75 int32_t ret;
76 if (serviceV1_2 == nullptr) {
77 serviceV1_2 = OHOS::HDI::Camera::V1_2::ICameraHost::Get("camera_service", false);
78 EXPECT_NE(serviceV1_2, nullptr);
79 CAMERA_LOGI("V1_2::ICameraHost get success");
80 ret = serviceV1_2->GetVersion(mainVer, minVer);
81 EXPECT_EQ(ret, 0);
82 CAMERA_LOGI("V1_2::ICameraHost get version success, %{public}d, %{public}d", mainVer, minVer);
83 service = static_cast<OHOS::HDI::Camera::V1_0::ICameraHost *>(serviceV1_2.GetRefPtr());
84 }
85
86 hostCallback = new TestCameraHostCallback();
87 ret = service->SetCallback(hostCallback);
88 EXPECT_EQ(ret, 0);
89 }
90
DefferredImageTestInit()91 int32_t Test::DefferredImageTestInit()
92 {
93 constexpr const char* serviceName = "camera_image_process_service";
94 constexpr const int userId = 100;
95 int ret = 0;
96
97 // get ImageProcessService
98 imageProcessService_ = OHOS::HDI::Camera::V1_2::ImageProcessServiceProxy::Get(serviceName, false);
99 if (imageProcessService_ == nullptr) {
100 CAMERA_LOGE("ImageProcessServiceProxy::Get Fail, imageProcessService is nullptr");
101 printf("ImageProcessServiceProxy::Get Fail, imageProcessService is nullptr");
102 return -1;
103 }
104 imageProcessCallback_ = new OHOS::Camera::Test::TestImageProcessCallback();
105 EXPECT_NE(imageProcessCallback_, nullptr);
106 ret = imageProcessService_->CreateImageProcessSession(userId, imageProcessCallback_, imageProcessSession_);
107 EXPECT_EQ(ret, 0);
108 if (imageProcessSession_ == nullptr) {
109 CAMERA_LOGE("CreateImageProcessSession Fail, imageProcessSession is nullptr: %{public}d", ret);
110 printf("CreateImageProcessSession Fail, imageProcessSession is nullptr: %d\r\n", ret);
111 return -1;
112 }
113 return 0;
114 }
115
Open(int cameraId)116 void Test::Open(int cameraId)
117 {
118 if (cameraDevice == nullptr) {
119 EXPECT_NE(service, nullptr);
120 service->GetCameraIds(cameraIds);
121 EXPECT_NE(cameraIds.size(), 0);
122 GetCameraMetadata(cameraId);
123 deviceCallback = new OHOS::Camera::Test::DemoCameraDeviceCallback();
124
125 EXPECT_NE(serviceV1_2, nullptr);
126 if (DEVICE_1 == cameraId) {
127 rc = serviceV1_2->OpenCamera_V1_1(cameraIds[1], deviceCallback, cameraDeviceV1_1);
128 } else {
129 rc = serviceV1_2->OpenCamera_V1_1(cameraIds[0], deviceCallback, cameraDeviceV1_1);
130 }
131 EXPECT_EQ(rc, HDI::Camera::V1_0::NO_ERROR);
132 EXPECT_NE(cameraDeviceV1_1, nullptr);
133 cameraDevice = static_cast<OHOS::HDI::Camera::V1_0::ICameraDevice *>(cameraDeviceV1_1.GetRefPtr());
134 CAMERA_LOGI("OpenCamera V1_2 success");
135 }
136 }
137
OpenCameraV1_2(int cameraId)138 void Test::OpenCameraV1_2(int cameraId)
139 {
140 if (cameraDevice == nullptr) {
141 EXPECT_NE(service, nullptr);
142 service->GetCameraIds(cameraIds);
143 EXPECT_NE(cameraIds.size(), 0);
144 GetCameraMetadata(cameraId);
145 deviceCallback = new OHOS::Camera::Test::DemoCameraDeviceCallback();
146
147 EXPECT_NE(serviceV1_2, nullptr);
148 if (cameraId == DEVICE_1) {
149 rc = serviceV1_2->OpenCamera_V1_2(cameraIds[1], deviceCallback, cameraDeviceV1_2);
150 } else {
151 rc = serviceV1_2->OpenCamera_V1_2(cameraIds[0], deviceCallback, cameraDeviceV1_2);
152 }
153 EXPECT_EQ(rc, HDI::Camera::V1_0::NO_ERROR);
154 EXPECT_NE(cameraDeviceV1_2, nullptr);
155 cameraDevice = static_cast<OHOS::HDI::Camera::V1_0::ICameraDevice *>(cameraDeviceV1_2.GetRefPtr());
156 CAMERA_LOGI("OpenCamera V1_2 success");
157 }
158 }
159
GetCameraMetadata(int cameraId)160 void Test::GetCameraMetadata(int cameraId)
161 {
162 if (DEVICE_1 == cameraId) {
163 rc = service->GetCameraAbility(cameraIds[1], abilityVec);
164 } else {
165 rc = service->GetCameraAbility(cameraIds[0], abilityVec);
166 }
167 if (rc != HDI::Camera::V1_0::NO_ERROR) {
168 CAMERA_LOGE("GetCameraAbility failed, rc = %{public}d", rc);
169 }
170 MetadataUtils::ConvertVecToMetadata(abilityVec, ability);
171 EXPECT_NE(ability, nullptr);
172 }
173
Close()174 void Test::Close()
175 {
176 if (cameraDevice != nullptr) {
177 cameraDevice->Close();
178 cameraDevice = nullptr;
179 }
180 }
181
DefaultPreview(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)182 void Test::DefaultPreview(
183 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
184 {
185 infos->v1_0.streamId_ = streamIdPreview;
186 infos->v1_0.width_ = previewWidth;
187 infos->v1_0.height_ = previewHeight;
188 infos->v1_0.format_ = previewFormat;
189 infos->v1_0.dataspace_ = UT_DATA_SIZE;
190 infos->v1_0.intent_ = StreamIntent::PREVIEW;
191 infos->v1_0.tunneledMode_ = UT_TUNNEL_MODE;
192 }
193
DefaultCapture(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)194 void Test::DefaultCapture(
195 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
196 {
197 infos->v1_0.streamId_ = streamIdCapture;
198 infos->v1_0.width_ = captureWidth;
199 infos->v1_0.height_ = captureHeight;
200 infos->v1_0.format_ = snapshotFormat;
201 infos->v1_0.dataspace_ = UT_DATA_SIZE;
202 infos->v1_0.intent_ = StreamIntent::STILL_CAPTURE;
203 infos->v1_0.tunneledMode_ = UT_TUNNEL_MODE;
204 }
205
DefaultSketch(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)206 void Test::DefaultSketch(
207 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
208 {
209 infos->v1_0.streamId_ = streamIdSketch;
210 infos->v1_0.width_ = sketchWidth;
211 infos->v1_0.height_ = sketchHeight;
212 infos->v1_0.format_ = previewFormat;
213 infos->v1_0.dataspace_ = UT_DATA_SIZE;
214 infos->v1_0.intent_ = StreamIntent::PREVIEW;
215 infos->v1_0.tunneledMode_ = UT_TUNNEL_MODE;
216 }
217
DefaultInfosSketch(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)218 void Test::DefaultInfosSketch(
219 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
220 {
221 DefaultSketch(infos);
222 std::shared_ptr<StreamConsumer> consumer_pre = std::make_shared<StreamConsumer>();
223 infos->v1_0.bufferQueue_ = consumer_pre->CreateProducerSeq([this](void* addr, uint32_t size) {
224 DumpImageFile(streamIdSketch, "yuv", addr, size);
225 });
226 infos->v1_0.bufferQueue_->producer_->SetQueueSize(UT_DATA_SIZE);
227 consumerMap_[StreamIntent::PREVIEW] = consumer_pre;
228 }
229
DefaultInfosPreviewV1_2(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)230 void Test::DefaultInfosPreviewV1_2(
231 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
232 {
233 DefaultPreview(infos);
234 std::shared_ptr<StreamConsumer> consumer_pre = std::make_shared<StreamConsumer>();
235 infos->v1_0.bufferQueue_ = consumer_pre->CreateProducerSeq([this](void* addr, uint32_t size) {
236 DumpImageFile(streamIdPreview, "yuv", addr, size);
237 });
238 infos->v1_0.bufferQueue_->producer_->SetQueueSize(UT_DATA_SIZE);
239 }
240
DefaultInfosPreview(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)241 void Test::DefaultInfosPreview(
242 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
243 {
244 DefaultPreview(infos);
245 std::shared_ptr<StreamConsumer> consumer_pre = std::make_shared<StreamConsumer>();
246 infos->v1_0.bufferQueue_ = consumer_pre->CreateProducerSeq([this](void* addr, uint32_t size) {
247 DumpImageFile(streamIdPreview, "yuv", addr, size);
248 });
249 infos->v1_0.bufferQueue_->producer_->SetQueueSize(UT_DATA_SIZE);
250 consumerMap_[StreamIntent::PREVIEW] = consumer_pre;
251 }
252
DefaultInfosCapture(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)253 void Test::DefaultInfosCapture(
254 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
255 {
256 DefaultCapture(infos);
257 std::shared_ptr<StreamConsumer> consumer_capture = std::make_shared<StreamConsumer>();
258 infos->v1_0.bufferQueue_ = consumer_capture->CreateProducerSeq([this](void* addr, uint32_t size) {
259 DumpImageFile(streamIdCapture, "jpeg", addr, size);
260 });
261 infos->v1_0.bufferQueue_->producer_->SetQueueSize(UT_DATA_SIZE);
262 consumerMap_[StreamIntent::STILL_CAPTURE] = consumer_capture;
263 }
264
DefaultInfosVideo(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)265 void Test::DefaultInfosVideo(
266 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
267 {
268 infos->v1_0.streamId_ = streamIdVideo;
269 infos->v1_0.width_ = videoWidth;
270 infos->v1_0.height_ = videoHeight;
271 infos->v1_0.format_ = videoFormat;
272 infos->v1_0.dataspace_ = UT_DATA_SIZE;
273 infos->v1_0.intent_ = StreamIntent::VIDEO;
274 infos->v1_0.encodeType_ = ENCODE_TYPE_H265;
275 infos->v1_0.tunneledMode_ = UT_TUNNEL_MODE;
276 std::shared_ptr<StreamConsumer> consumer_video = std::make_shared<StreamConsumer>();
277 infos->v1_0.bufferQueue_ = consumer_video->CreateProducerSeq([this](void* addr, uint32_t size) {
278 DumpImageFile(streamIdVideo, "yuv", addr, size);
279 });
280 infos->v1_0.bufferQueue_->producer_->SetQueueSize(UT_DATA_SIZE);
281 consumerMap_[StreamIntent::VIDEO] = consumer_video;
282 }
283
DefaultInfosAnalyze(std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> & infos)284 void Test::DefaultInfosAnalyze(
285 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos)
286 {
287 infos->v1_0.streamId_ = streamIdAnalyze;
288 infos->v1_0.width_ = analyzeWidth;
289 infos->v1_0.height_ = analyzeHeight;
290 infos->v1_0.format_ = analyzeFormat;
291 infos->v1_0.dataspace_ = UT_DATA_SIZE;
292 infos->v1_0.intent_ = StreamIntent::ANALYZE;
293 infos->v1_0.tunneledMode_ = UT_TUNNEL_MODE;
294
295 std::shared_ptr<StreamConsumer> consumer_analyze = std::make_shared<StreamConsumer>();
296 infos->v1_0.bufferQueue_ = consumer_analyze->CreateProducerSeq([this](void* addr, uint32_t size) {
297 common_metadata_header_t *data = static_cast<common_metadata_header_t *>(addr);
298 camera_metadata_item_t entry = {};
299
300 int ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_IDS, &entry);
301 if (ret == 0) {
302 for (size_t i = 0; i < entry.count; i++) {
303 int id = entry.data.i32[i];
304 CAMERA_LOGI("Face ids : %{public}d", id);
305 }
306 }
307
308 ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_RECTANGLES, &entry);
309 if (ret == 0) {
310 for (size_t i = 0; i < entry.count; i++) {
311 int id = entry.data.i32[i];
312 CAMERA_LOGI("Face rectangles : %{public}d", id);
313 }
314 }
315 });
316 infos->v1_0.bufferQueue_->producer_->SetQueueSize(UT_DATA_SIZE);
317 consumerMap_[StreamIntent::ANALYZE] = consumer_analyze;
318 }
319
StartStream(std::vector<StreamIntent> intents)320 void Test::StartStream(std::vector<StreamIntent> intents)
321 {
322 streamOperatorCallback = new TestStreamOperatorCallback();
323 uint32_t mainVersion = 1;
324 uint32_t minVersion = 0;
325 rc = cameraDeviceV1_1->GetStreamOperator_V1_1(streamOperatorCallback, streamOperator_V1_1);
326 if (rc == HDI::Camera::V1_0::NO_ERROR) {
327 rc = streamOperator_V1_1->GetVersion(mainVersion, minVersion);
328 streamOperator = static_cast<OHOS::HDI::Camera::V1_0::IStreamOperator *>(streamOperator_V1_1.GetRefPtr());
329 if (rc != HDI::Camera::V1_0::NO_ERROR) {
330 CAMERA_LOGE("StreamOperator V1_1 get version failed, rc = %{public}d", rc);
331 } else {
332 CAMERA_LOGI("StreamOperator V1_1 get version success, %{public}u, %{public}u",
333 mainVersion, minVersion);
334 }
335 CAMERA_LOGI("GetStreamOperator success");
336 } else {
337 CAMERA_LOGE("GetStreamOperator fail, rc = %{public}d", rc);
338 }
339 streamInfoPre = std::make_shared<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1>();
340 streamInfoVideo = std::make_shared<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1>();
341 streamInfoCapture = std::make_shared<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1>();
342 streamInfoAnalyze = std::make_shared<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1>();
343
344 for (auto& intent : intents) {
345 if (intent == StreamIntent::PREVIEW) {
346 DefaultInfosPreview(streamInfoPre);
347 streamInfos.push_back(*streamInfoPre);
348 } else if (intent == StreamIntent::VIDEO) {
349 DefaultInfosVideo(streamInfoVideo);
350 streamInfos.push_back(*streamInfoVideo);
351 } else if (intent == StreamIntent::ANALYZE) {
352 DefaultInfosAnalyze(streamInfoAnalyze);
353 streamInfos.push_back(*streamInfoAnalyze);
354 } else {
355 DefaultInfosCapture(streamInfoCapture);
356 streamInfos.push_back(*streamInfoCapture);
357 }
358 }
359
360 rc = streamOperator_V1_1->CreateStreams_V1_1(streamInfos);
361 EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR);
362 rc = streamOperator_V1_1->CommitStreams(OperationMode::NORMAL, abilityVec);
363 EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR);
364 sleep(1);
365 std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1>().swap(streamInfos);
366 }
367
StartCapture(int streamId,int captureId,bool shutterCallback,bool isStreaming)368 void Test::StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming)
369 {
370 captureInfo = std::make_shared<CaptureInfo>();
371 captureInfo->streamIds_ = {streamId};
372 captureInfo->captureSetting_ = abilityVec;
373 captureInfo->enableShutterCallback_ = shutterCallback;
374 if (streamOperator_V1_2 != nullptr) {
375 rc = (CamRetCode)streamOperator_V1_2->Capture(captureId, *captureInfo, isStreaming);
376 } else if (streamOperator_V1_1 != nullptr) {
377 rc = (CamRetCode)streamOperator_V1_1->Capture(captureId, *captureInfo, isStreaming);
378 } else {
379 rc = (CamRetCode)streamOperator->Capture(captureId, *captureInfo, isStreaming);
380 }
381
382 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
383 if (rc == HDI::Camera::V1_0::NO_ERROR) {
384 CAMERA_LOGI("check Capture: Capture success, %{public}d", captureId);
385 } else {
386 std::cout << "rc = " << rc << std::endl;
387 CAMERA_LOGE("check Capture: Capture fail, rc = %{public}d", rc);
388 }
389 sleep(UT_SLEEP_TIME);
390 }
391
StopStream(std::vector<int> & captureIds,std::vector<int> & streamIds)392 void Test::StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds)
393 {
394 if (sizeof(captureIds) > 0) {
395 for (auto &captureId : captureIds) {
396 if (streamOperator_V1_2 != nullptr) {
397 rc = streamOperator_V1_2->CancelCapture(captureId);
398 } else if (streamOperator_V1_1 != nullptr) {
399 rc = streamOperator_V1_1->CancelCapture(captureId);
400 } else {
401 rc = streamOperator->CancelCapture(captureId);
402 }
403 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
404 if (rc == HDI::Camera::V1_0::NO_ERROR) {
405 CAMERA_LOGI("check Capture: CancelCapture success, %{public}d", captureId);
406 } else {
407 CAMERA_LOGE("check Capture: CancelCapture fail, rc = %{public}d, captureId = %{public}d",
408 rc, captureId);
409 }
410 }
411 }
412 if (sizeof(streamIds) > 0) {
413 if (streamOperator_V1_2 != nullptr) {
414 rc = streamOperator_V1_2->ReleaseStreams(streamIds);
415 } else if (streamOperator_V1_1 != nullptr) {
416 rc = streamOperator_V1_1->ReleaseStreams(streamIds);
417 } else {
418 rc = streamOperator->ReleaseStreams(streamIds);
419 }
420 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
421 if (rc == HDI::Camera::V1_0::NO_ERROR) {
422 CAMERA_LOGI("check Capture: ReleaseStream success");
423 } else {
424 CAMERA_LOGE("check Capture: ReleaseStreams fail, rc = %{public}d", rc);
425 }
426 }
427 }
428
IsTagValueExistsU8(std::shared_ptr<CameraMetadata> ability,uint32_t tag,uint8_t value)429 bool Test::IsTagValueExistsU8(std::shared_ptr<CameraMetadata> ability, uint32_t tag, uint8_t value)
430 {
431 common_metadata_header_t* data = ability->get();
432 camera_metadata_item_t entry;
433 int ret = FindCameraMetadataItem(data, tag, &entry);
434 EXPECT_EQ(ret, 0);
435 EXPECT_NE(entry.count, 0);
436 for (int i = 0; i < entry.count; i++) {
437 if (entry.data.u8[i] == value) {
438 return true;
439 }
440 }
441 return false;
442 }
443
TakePhotoWithTags(std::shared_ptr<OHOS::Camera::CameraSetting> meta)444 void Test::TakePhotoWithTags(std::shared_ptr<OHOS::Camera::CameraSetting> meta)
445 {
446 streamOperatorCallback = new OHOS::Camera::Test::TestStreamOperatorCallback();
447 rc = cameraDeviceV1_1->GetStreamOperator_V1_1(streamOperatorCallback,
448 streamOperator_V1_1);
449 EXPECT_NE(streamOperator_V1_1, nullptr);
450 EXPECT_EQ(HDI::Camera::V1_0::NO_ERROR, rc);
451
452 // preview streamInfo
453 streamInfoV1_1 = std::make_shared<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1>();
454 DefaultInfosPreview(streamInfoV1_1);
455 streamInfosV1_1.push_back(*streamInfoV1_1);
456
457 // capture streamInfo
458 streamInfoV1_1 = std::make_shared<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1>();
459 DefaultInfosCapture(streamInfoV1_1);
460 streamInfosV1_1.push_back(*streamInfoV1_1);
461
462 // create and commitstreams
463 rc = streamOperator_V1_1->CreateStreams_V1_1(streamInfosV1_1);
464 EXPECT_EQ(HDI::Camera::V1_0::NO_ERROR, rc);
465 rc = streamOperator_V1_1->CommitStreams_V1_1(
466 static_cast<OHOS::HDI::Camera::V1_1::OperationMode_V1_1>(OHOS::HDI::Camera::V1_2::PORTRAIT),
467 abilityVec);
468 EXPECT_EQ(HDI::Camera::V1_0::NO_ERROR, rc);
469 std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1>().swap(streamInfosV1_1);
470 StartCapture(streamIdPreview, captureIdPreview, false, true);
471 StartCapture(streamIdCapture, captureIdCapture, false, true);
472 captureIds = {captureIdPreview, captureIdCapture};
473 streamIds = {streamIdPreview, streamIdCapture};
474 StopStream(captureIds, streamIds);
475 }
476
CalculateFps(int64_t timestamp,int32_t streamId)477 void Test::StreamConsumer::CalculateFps(int64_t timestamp, int32_t streamId)
478 {
479 if (isFirstCalculateFps_) {
480 if ((timestamp - intervalTimestamp_) >= interval_) {
481 int64_t timeInterval = timestamp - intervalTimestamp_;
482 if (timeInterval != 0) {
483 float fps = (int64_t)(100000000000 * timestampCount_ / timeInterval) / 100.0;
484 CAMERA_LOGI("Calculate FPS success, streamId: %{public}d, Fps:%{public}f", streamId, fps);
485 interval_ = ONESECOND_OF_MICROSECOND_UNIT;
486 } else {
487 CAMERA_LOGE("Calculate FPS error timeInerval is 0");
488 }
489 }
490 } else {
491 intervalTimestamp_ = timestamp;
492 isFirstCalculateFps_ = true;
493 }
494 if ((timestamp - intervalTimestamp_) >= ONESECOND_OF_MICROSECOND_UNIT * UT_SECOND_TIMES) {
495 intervalTimestamp_ = timestamp;
496 timestampCount_ = 0;
497 interval_ = ONESECOND_OF_MICROSECOND_UNIT;
498 }
499 timestampCount_++;
500 }
501
GetTimeStamp(int64_t * g_timestamp,uint32_t lenght,int64_t timestamp,int32_t gotSize)502 void Test::StreamConsumer::GetTimeStamp(int64_t *g_timestamp, uint32_t lenght, int64_t timestamp, int32_t gotSize)
503 {
504 if (gotSize != UT_PREVIEW_SIZE) {
505 if (g_timestamp[0] == 0) {
506 g_timestamp[0] = timestamp;
507 } else {
508 g_timestamp[1] = timestamp;
509 }
510 }
511 }
512
CreateProducer(std::function<void (void *,uint32_t)> callback)513 OHOS::sptr<OHOS::IBufferProducer> Test::StreamConsumer::CreateProducer(std::function<void(void*, uint32_t)> callback)
514 {
515 consumer_ = OHOS::IConsumerSurface::Create();
516 if (consumer_ == nullptr) {
517 return nullptr;
518 }
519 sptr<IBufferConsumerListener> listener = new TestBufferConsumerListener();
520 consumer_->RegisterConsumerListener(listener);
521 auto producer = consumer_->GetProducer();
522 if (producer == nullptr) {
523 return nullptr;
524 }
525
526 callback_ = callback;
527 consumerThread_ = new std::thread([this, listener] {
528 int32_t flushFence = 0;
529 int64_t timestamp = 0;
530 OHOS::Rect damage;
531 TestBufferConsumerListener* checker = static_cast<TestBufferConsumerListener*>(listener.GetRefPtr());
532 while (running_ == true) {
533 OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
534 if (checker->checkBufferAvailable()) {
535 consumer_->AcquireBuffer(buffer, flushFence, timestamp, damage);
536 if (buffer != nullptr) {
537 void* addr = buffer->GetVirAddr();
538 uint32_t size = buffer->GetSize();
539
540 int32_t gotSize = 0;
541 int32_t isKey = 0;
542 int32_t streamId = 0;
543 int32_t captureId = 0;
544 buffer->GetExtraData()->ExtraGet(OHOS::Camera::dataSize, gotSize);
545 buffer->GetExtraData()->ExtraGet(OHOS::Camera::isKeyFrame, isKey);
546 buffer->GetExtraData()->ExtraGet(OHOS::Camera::timeStamp, timestamp);
547 buffer->GetExtraData()->ExtraGet(OHOS::Camera::streamId, streamId);
548 buffer->GetExtraData()->ExtraGet(OHOS::Camera::captureId, captureId);
549 GetTimeStamp(g_timestamp, sizeof(g_timestamp) / sizeof(g_timestamp[0]), timestamp, gotSize);
550 if (gotSize) {
551 CalculateFps(timestamp, streamId);
552 callback_(addr, gotSize);
553 } else {
554 callback_(addr, size);
555 }
556
557 consumer_->ReleaseBuffer(buffer, -1);
558 shotCount_--;
559 if (shotCount_ == 0) {
560 std::unique_lock<std::mutex> l(l_);
561 cv_.notify_one();
562 }
563 }
564 }
565 if (running_ == false) {
566 break;
567 }
568 usleep(1);
569 }
570 });
571
572 return producer;
573 }
574
CreateProducerSeq(std::function<void (void *,uint32_t)> callback)575 OHOS::sptr<BufferProducerSequenceable> Test::StreamConsumer::CreateProducerSeq(
576 std::function<void(void*, uint32_t)> callback)
577 {
578 OHOS::sptr<OHOS::IBufferProducer> producer = CreateProducer(callback);
579 if (producer == nullptr) {
580 return nullptr;
581 }
582
583 return new BufferProducerSequenceable(producer);
584 }
585
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamId)586 int32_t Test::TestStreamOperatorCallback::OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId)
587 {
588 for (auto it : streamId) {
589 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d", captureId, it);
590 }
591 return HDI::Camera::V1_0::NO_ERROR;
592 }
593
OnCaptureEnded(int32_t captureId,const std::vector<CaptureEndedInfo> & infos)594 int32_t Test::TestStreamOperatorCallback::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos)
595 {
596 for (auto it : infos) {
597 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d, count: %{public}d", captureId, it.streamId_,
598 it.frameCount_);
599 }
600 return HDI::Camera::V1_0::NO_ERROR;
601 }
602
OnCaptureError(int32_t captureId,const std::vector<CaptureErrorInfo> & infos)603 int32_t Test::TestStreamOperatorCallback::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos)
604 {
605 for (auto it : infos) {
606 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d, error: %{public}d", captureId, it.streamId_,
607 it.error_);
608 }
609 return HDI::Camera::V1_0::NO_ERROR;
610 }
611
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)612 int32_t Test::TestStreamOperatorCallback::OnFrameShutter(int32_t captureId,
613 const std::vector<int32_t> &streamIds, uint64_t timestamp)
614 {
615 (void)timestamp;
616 for (auto it : streamIds) {
617 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d", captureId, it);
618 }
619 return HDI::Camera::V1_0::NO_ERROR;
620 }
621
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamId)622 int32_t Test::TestStreamOperatorCallbackV1_2::OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId)
623 {
624 for (auto it : streamId) {
625 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d", captureId, it);
626 }
627 return HDI::Camera::V1_0::NO_ERROR;
628 }
629
OnCaptureEnded(int32_t captureId,const std::vector<CaptureEndedInfo> & infos)630 int32_t Test::TestStreamOperatorCallbackV1_2::OnCaptureEnded(int32_t captureId,
631 const std::vector<CaptureEndedInfo> &infos)
632 {
633 for (auto it : infos) {
634 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d, count: %{public}d", captureId, it.streamId_,
635 it.frameCount_);
636 }
637 return HDI::Camera::V1_0::NO_ERROR;
638 }
639
OnCaptureError(int32_t captureId,const std::vector<CaptureErrorInfo> & infos)640 int32_t Test::TestStreamOperatorCallbackV1_2::OnCaptureError(int32_t captureId,
641 const std::vector<CaptureErrorInfo> &infos)
642 {
643 for (auto it : infos) {
644 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d, error: %{public}d", captureId, it.streamId_,
645 it.error_);
646 }
647 return HDI::Camera::V1_0::NO_ERROR;
648 }
649
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)650 int32_t Test::TestStreamOperatorCallbackV1_2::OnFrameShutter(int32_t captureId,
651 const std::vector<int32_t> &streamIds, uint64_t timestamp)
652 {
653 (void)timestamp;
654 for (auto it : streamIds) {
655 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d", captureId, it);
656 }
657 return HDI::Camera::V1_0::NO_ERROR;
658 }
659
OnCaptureStarted_V1_2(int32_t captureId,const std::vector<HDI::Camera::V1_2::CaptureStartedInfo> & infos)660 int32_t Test::TestStreamOperatorCallbackV1_2::OnCaptureStarted_V1_2(int32_t captureId,
661 const std::vector<HDI::Camera::V1_2::CaptureStartedInfo> &infos)
662 {
663 for (auto it : infos) {
664 CAMERA_LOGE("captureId: %{public}d, streamId: %{public}d", captureId, it.streamId_);
665 }
666 return HDI::Camera::V1_0::NO_ERROR;
667 }
668
OnError(ErrorType type,int32_t errorMsg)669 int32_t Test::DemoCameraDeviceCallback::OnError(ErrorType type, int32_t errorMsg)
670 {
671 CAMERA_LOGE("type: %{public}d, errorMsg: %{public}d", type, errorMsg);
672 return HDI::Camera::V1_0::NO_ERROR;
673 }
674
OnResult(uint64_t timestamp,const std::vector<uint8_t> & result)675 int32_t Test::DemoCameraDeviceCallback::OnResult(uint64_t timestamp, const std::vector<uint8_t> &result)
676 {
677 MetadataUtils::ConvertVecToMetadata(result, resultMeta);
678 if (Test::resultCallback_) {
679 Test::resultCallback_(timestamp, resultMeta);
680 }
681 return HDI::Camera::V1_0::NO_ERROR;
682 }
683
OnCameraStatus(const std::string & cameraId,CameraStatus status)684 int32_t Test::TestCameraHostCallback::OnCameraStatus(const std::string& cameraId, CameraStatus status)
685 {
686 CAMERA_LOGE("cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status);
687 return HDI::Camera::V1_0::NO_ERROR;
688 }
689
OnFlashlightStatus(const std::string & cameraId,FlashlightStatus status)690 int32_t Test::TestCameraHostCallback::OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status)
691 {
692 CAMERA_LOGE("cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status);
693 return HDI::Camera::V1_0::NO_ERROR;
694 }
695
OnCameraEvent(const std::string & cameraId,CameraEvent event)696 int32_t Test::TestCameraHostCallback::OnCameraEvent(const std::string& cameraId, CameraEvent event)
697 {
698 CAMERA_LOGE("cameraId: %{public}s, status: %{public}d", cameraId.c_str(), event);
699 return HDI::Camera::V1_0::NO_ERROR;
700 }
701
OnCameraStatus(const std::string & cameraId,CameraStatus status)702 int32_t Test::TestCameraHostCallbackV1_2::OnCameraStatus(const std::string& cameraId, CameraStatus status)
703 {
704 CAMERA_LOGE("cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status);
705 return HDI::Camera::V1_0::NO_ERROR;
706 }
707
OnFlashlightStatus(const std::string & cameraId,FlashlightStatus status)708 int32_t Test::TestCameraHostCallbackV1_2::OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status)
709 {
710 CAMERA_LOGE("cameraId: %{public}s, status: %{public}d", cameraId.c_str(), status);
711 return HDI::Camera::V1_0::NO_ERROR;
712 }
713
OnCameraEvent(const std::string & cameraId,CameraEvent event)714 int32_t Test::TestCameraHostCallbackV1_2::OnCameraEvent(const std::string& cameraId, CameraEvent event)
715 {
716 CAMERA_LOGE("cameraId: %{public}s, status: %{public}d", cameraId.c_str(), event);
717 return HDI::Camera::V1_0::NO_ERROR;
718 }
719
OnFlashlightStatus_V1_2(FlashlightStatus status)720 int32_t Test::TestCameraHostCallbackV1_2::OnFlashlightStatus_V1_2(FlashlightStatus status)
721 {
722 CAMERA_LOGE("status: %{public}d", status);
723 Test::statusCallback = status;
724 return HDI::Camera::V1_0::NO_ERROR;
725 }
726
OnProcessDone(const std::string & imageId,const OHOS::HDI::Camera::V1_2::ImageBufferInfo & buffer)727 int32_t Test::TestImageProcessCallback::OnProcessDone(const std::string& imageId,
728 const OHOS::HDI::Camera::V1_2::ImageBufferInfo& buffer)
729 {
730 CAMERA_LOGI("imageId: %{public}s", imageId.c_str());
731 coutProcessDone_++;
732 curImageId_ = imageId;
733 curImageBufferInfo_ = buffer;
734 return 0;
735 }
736
OnStatusChanged(OHOS::HDI::Camera::V1_2::SessionStatus status)737 int32_t Test::TestImageProcessCallback::OnStatusChanged(OHOS::HDI::Camera::V1_2::SessionStatus status)
738 {
739 CAMERA_LOGI("status: %{public}d", status);
740 curStatus_ = status;
741 coutStatusChanged_++;
742 return 0;
743 }
744
OnError(const std::string & imageId,OHOS::HDI::Camera::V1_2::ErrorCode errorCode)745 int32_t Test::TestImageProcessCallback::OnError(const std::string& imageId,
746 OHOS::HDI::Camera::V1_2::ErrorCode errorCode)
747 {
748 CAMERA_LOGI("imageId: %{public}s, errorCode: %{public}d", imageId.c_str(), errorCode);
749 curImageId_ = imageId;
750 curErrorCode_ = errorCode;
751 countError_++;
752 return 0;
753 }
754 } // OHOS::Camera