1 /*
2 * Copyright (c) 2021-2022 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 "test_common.h"
17 #include <cinttypes>
18 #include <cstdio>
19 #include <fcntl.h>
20 #include <securec.h>
21 #include <sys/time.h>
22 #include <unistd.h>
23 #include "camera_output_capability.h"
24 #include "camera_util.h"
25 #include "camera_log.h"
26
27 namespace OHOS {
28 namespace CameraStandard {
GetCameraMetadataFormat(CameraFormat format)29 camera_format_t TestUtils::GetCameraMetadataFormat(CameraFormat format)
30 {
31 camera_format_t metaFormat = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
32 const std::unordered_map<CameraFormat, camera_format_t> mapToMetadataFormat = {
33 {CAMERA_FORMAT_YUV_420_SP, OHOS_CAMERA_FORMAT_YCRCB_420_SP},
34 {CAMERA_FORMAT_JPEG, OHOS_CAMERA_FORMAT_JPEG},
35 {CAMERA_FORMAT_RGBA_8888, OHOS_CAMERA_FORMAT_RGBA_8888},
36 {CAMERA_FORMAT_YCBCR_420_888, OHOS_CAMERA_FORMAT_YCBCR_420_888}
37 };
38 auto itr = mapToMetadataFormat.find(format);
39 if (itr != mapToMetadataFormat.end()) {
40 metaFormat = itr->second;
41 }
42 return metaFormat;
43 }
GetCurrentLocalTimeStamp()44 uint64_t TestUtils::GetCurrentLocalTimeStamp()
45 {
46 std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
47 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
48 auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
49 return tmp.count();
50 }
51
SaveYUV(const char* buffer, int32_t size, SurfaceType type)52 int32_t TestUtils::SaveYUV(const char* buffer, int32_t size, SurfaceType type)
53 {
54 char path[PATH_MAX] = {0};
55 int32_t retVal;
56
57 if ((buffer == nullptr) || (size == 0)) {
58 MEDIA_ERR_LOG("buffer is null or size is 0");
59 return -1;
60 }
61
62 MEDIA_DEBUG_LOG("TestUtils::SaveYUV(), type: %{public}d", type);
63 if (type == SurfaceType::PREVIEW) {
64 (void)system("mkdir -p /data/media/preview");
65 retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/media/preview/%s_%lld.yuv", "preview",
66 GetCurrentLocalTimeStamp());
67 if (retVal < 0) {
68 MEDIA_ERR_LOG("Path Assignment failed");
69 return -1;
70 }
71 } else if (type == SurfaceType::PHOTO) {
72 (void)system("mkdir -p /data/media/photo");
73 retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/media/photo/%s_%lld.jpg", "photo",
74 GetCurrentLocalTimeStamp());
75 if (retVal < 0) {
76 MEDIA_ERR_LOG("Path Assignment failed");
77 return -1;
78 }
79 } else if (type == SurfaceType::SECOND_PREVIEW) {
80 (void)system("mkdir -p /data/media/preview2");
81 retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/media/preview2/%s_%lld.yuv", "preview2",
82 GetCurrentLocalTimeStamp());
83 if (retVal < 0) {
84 MEDIA_ERR_LOG("Path Assignment failed");
85 return -1;
86 }
87 } else {
88 MEDIA_ERR_LOG("Unexpected flow!");
89 return -1;
90 }
91
92 MEDIA_DEBUG_LOG("%s, saving file to %{private}s", __FUNCTION__, path);
93 int imgFd = open(path, O_RDWR | O_CREAT, FILE_PERMISSIONS_FLAG);
94 if (imgFd == -1) {
95 MEDIA_ERR_LOG("%s, open file failed, errno = %{public}s.", __FUNCTION__, strerror(errno));
96 return -1;
97 }
98 int ret = write(imgFd, buffer, size);
99 if (ret == -1) {
100 MEDIA_ERR_LOG("%s, write file failed, error = %{public}s", __FUNCTION__, strerror(errno));
101 close(imgFd);
102 return -1;
103 }
104 close(imgFd);
105 return 0;
106 }
107
IsNumber(const char number[])108 bool TestUtils::IsNumber(const char number[])
109 {
110 for (int i = 0; number[i] != 0; i++) {
111 if (!std::isdigit(number[i])) {
112 return false;
113 }
114 }
115 return true;
116 }
117
SaveVideoFile(const char* buffer, int32_t size, VideoSaveMode operationMode, int32_t &fd)118 int32_t TestUtils::SaveVideoFile(const char* buffer, int32_t size, VideoSaveMode operationMode, int32_t &fd)
119 {
120 int32_t retVal = 0;
121
122 if (operationMode == VideoSaveMode::CREATE) {
123 char path[255] = {0};
124
125 (void)system("mkdir -p /data/media/video");
126 retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]),
127 "/data/media/video/%s_%lld.h264", "video", GetCurrentLocalTimeStamp());
128 if (retVal < 0) {
129 MEDIA_ERR_LOG("Failed to create video file name");
130 return -1;
131 }
132 MEDIA_DEBUG_LOG("%{public}s, save video to file %{private}s", __FUNCTION__, path);
133 fd = open(path, O_RDWR | O_CREAT, FILE_PERMISSIONS_FLAG);
134 if (fd == -1) {
135 std::cout << "open file failed, errno = " << strerror(errno) << std::endl;
136 return -1;
137 }
138 } else if (operationMode == VideoSaveMode::APPEND && fd != -1) {
139 int32_t ret = write(fd, buffer, size);
140 if (ret == -1) {
141 std::cout << "write file failed, error = " << strerror(errno) << std::endl;
142 close(fd);
143 fd = -1;
144 return fd;
145 }
146 } else { // VideoSaveMode::CLOSE
147 if (fd != -1) {
148 close(fd);
149 fd = -1;
150 }
151 }
152 return 0;
153 }
154
TestCameraMngerCallback(const char* testName)155 TestCameraMngerCallback::TestCameraMngerCallback(const char* testName) : testName_(testName) {
156 }
157
OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const158 void TestCameraMngerCallback::OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const
159 {
160 MEDIA_DEBUG_LOG("OnCameraStatusChanged()");
161 return;
162 }
163
OnFlashlightStatusChanged(const std::string &cameraID, const FlashStatus flashStatus) const164 void TestCameraMngerCallback::OnFlashlightStatusChanged(const std::string &cameraID,
165 const FlashStatus flashStatus) const
166 {
167 MEDIA_DEBUG_LOG("OnFlashlightStatusChanged(), testName_: %{public}s, cameraID: %{public}s, flashStatus: %{public}d",
168 testName_, cameraID.c_str(), flashStatus);
169 return;
170 }
171
TestDeviceCallback(const char* testName)172 TestDeviceCallback::TestDeviceCallback(const char* testName) : testName_(testName) {
173 }
174
OnError(const int32_t errorType, const int32_t errorMsg) const175 void TestDeviceCallback::OnError(const int32_t errorType, const int32_t errorMsg) const
176 {
177 MEDIA_DEBUG_LOG("TestDeviceCallback::OnError(), testName_: %{public}s, errorType: %{public}d, errorMsg: %{public}d",
178 testName_, errorType, errorMsg);
179 return;
180 }
181
TestOnResultCallback(const char* testName)182 TestOnResultCallback::TestOnResultCallback(const char* testName) : testName_(testName) {
183 }
184
OnResult(const uint64_t timestamp, const std::shared_ptr<Camera::CameraMetadata> &result) const185 void TestOnResultCallback::OnResult(const uint64_t timestamp,
186 const std::shared_ptr<Camera::CameraMetadata> &result) const
187 {
188 MEDIA_DEBUG_LOG("TestOnResultCallback::OnResult(), testName_: %{public}s",
189 testName_);
190 return;
191 }
192
193
TestPhotoOutputCallback(const char* testName)194 TestPhotoOutputCallback::TestPhotoOutputCallback(const char* testName) : testName_(testName) {
195 }
196
OnCaptureStarted(const int32_t captureID) const197 void TestPhotoOutputCallback::OnCaptureStarted(const int32_t captureID) const
198 {
199 MEDIA_INFO_LOG("PhotoOutputCallback:OnCaptureStarted(), testName_: %{public}s, captureID: %{public}d",
200 testName_, captureID);
201 }
202
OnCaptureStarted(const int32_t captureID, uint32_t exposureTime) const203 void TestPhotoOutputCallback::OnCaptureStarted(const int32_t captureID, uint32_t exposureTime) const
204 {
205 MEDIA_INFO_LOG("PhotoOutputCallback:OnCaptureStarted(), testName_: %{public}s, captureID: %{public}d",
206 testName_, captureID);
207 }
208
OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const209 void TestPhotoOutputCallback::OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const
210 {
211 MEDIA_INFO_LOG("TestPhotoOutputCallback:OnCaptureEnded(), testName_: %{public}s, captureID: %{public}d,"
212 " frameCount: %{public}d", testName_, captureID, frameCount);
213 }
214
OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const215 void TestPhotoOutputCallback::OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const
216 {
217 MEDIA_INFO_LOG("OnFrameShutter(), testName_: %{public}s, captureID: %{public}d", testName_, captureId);
218 }
219
OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const220 void TestPhotoOutputCallback::OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const
221 {
222 MEDIA_INFO_LOG("OnFrameShutterEnd(), testName_: %{public}s, captureID: %{public}d", testName_, captureId);
223 }
224
OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const225 void TestPhotoOutputCallback::OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const
226 {
227 MEDIA_INFO_LOG("OnCaptureReady(), testName_: %{public}s, captureID: %{public}d", testName_, captureId);
228 }
229
OnEstimatedCaptureDuration(const int32_t duration) const230 void TestPhotoOutputCallback::OnEstimatedCaptureDuration(const int32_t duration) const
231 {
232 MEDIA_INFO_LOG("OnEstimatedCaptureDuration(), duration: %{public}d", duration);
233 }
234
OnCaptureError(const int32_t captureId, const int32_t errorCode) const235 void TestPhotoOutputCallback::OnCaptureError(const int32_t captureId, const int32_t errorCode) const
236 {
237 MEDIA_INFO_LOG("OnCaptureError(), testName_: %{public}s, captureID: %{public}d, errorCode: %{public}d",
238 testName_, captureId, errorCode);
239 }
240
TestPreviewOutputCallback(const char* testName)241 TestPreviewOutputCallback::TestPreviewOutputCallback(const char* testName) : testName_(testName) {
242 }
243
OnFrameStarted() const244 void TestPreviewOutputCallback::OnFrameStarted() const
245 {
246 MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
247 }
248
OnFrameEnded(const int32_t frameCount) const249 void TestPreviewOutputCallback::OnFrameEnded(const int32_t frameCount) const
250 {
251 MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
252 testName_, frameCount);
253 }
254
OnError(const int32_t errorCode) const255 void TestPreviewOutputCallback::OnError(const int32_t errorCode) const
256 {
257 MEDIA_INFO_LOG("TestPreviewOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
258 testName_, errorCode);
259 }
260
OnSketchStatusDataChanged(const SketchStatusData& statusData) const261 void TestPreviewOutputCallback::OnSketchStatusDataChanged(const SketchStatusData& statusData) const
262 {
263 MEDIA_DEBUG_LOG("TestPreviewOutputCallback::OnSketchStatusDataChanged(), testName_: %{public}s", testName_);
264 return;
265 }
266
TestVideoOutputCallback(const char* testName)267 TestVideoOutputCallback::TestVideoOutputCallback(const char* testName) : testName_(testName) {
268 }
269
OnFrameStarted() const270 void TestVideoOutputCallback::OnFrameStarted() const
271 {
272 MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
273 }
274
OnFrameEnded(const int32_t frameCount) const275 void TestVideoOutputCallback::OnFrameEnded(const int32_t frameCount) const
276 {
277 MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
278 testName_, frameCount);
279 }
280
OnError(const int32_t errorCode) const281 void TestVideoOutputCallback::OnError(const int32_t errorCode) const
282 {
283 MEDIA_INFO_LOG("TestVideoOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
284 testName_, errorCode);
285 }
286
OnDeferredVideoEnhancementInfo(const CaptureEndedInfoExt info) const287 void TestVideoOutputCallback::OnDeferredVideoEnhancementInfo(const CaptureEndedInfoExt info) const
288 {
289 MEDIA_INFO_LOG("TestVideoOutputCallback:OnDeferredVideoEnhancementInfo()");
290 }
291
TestMetadataOutputObjectCallback(const char* testName)292 TestMetadataOutputObjectCallback::TestMetadataOutputObjectCallback(const char* testName) : testName_(testName) {
293 }
294
OnMetadataObjectsAvailable(std::vector<sptr<MetadataObject>> metaObjects) const295 void TestMetadataOutputObjectCallback::OnMetadataObjectsAvailable(std::vector<sptr<MetadataObject>> metaObjects) const
296 {
297 MEDIA_INFO_LOG("TestMetadataOutputObjectCallback:OnMetadataObjectsAvailable(), testName_: %{public}s, "
298 "metaObjects size: %{public}zu", testName_, metaObjects.size());
299 for (size_t i = 0; i < metaObjects.size(); i++) {
300 MEDIA_INFO_LOG("TestMetadataOutputObjectCallback::OnMetadataObjectsAvailable "
301 "metaObjInfo: Type(%{public}d), Rect{x(%{pulic}f),y(%{pulic}f),w(%{pulic}f),d(%{pulic}f)} "
302 "Timestamp: %{public}" PRId64,
303 metaObjects[i]->GetType(),
304 metaObjects[i]->GetBoundingBox().topLeftX, metaObjects[i]->GetBoundingBox().topLeftY,
305 metaObjects[i]->GetBoundingBox().width, metaObjects[i]->GetBoundingBox().height,
306 static_cast<int64_t>(metaObjects[i]->GetTimestamp()));
307 }
308 }
309
OnProcessImageDone(const std::string& imageId, const uint8_t* addr, const long bytes, bool isCloudImageEnhanceSupported)310 void TestDeferredPhotoProcSessionCallback::OnProcessImageDone(const std::string& imageId,
311 const uint8_t* addr,
312 const long bytes, bool isCloudImageEnhanceSupported)
313 {
314 MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnProcessImageDone.");
315 }
316
OnProcessImageDone(const std::string &imageId, std::shared_ptr<Media::Picture> picture, bool isCloudImageEnhanceSupported)317 void TestDeferredPhotoProcSessionCallback::OnProcessImageDone(const std::string &imageId,
318 std::shared_ptr<Media::Picture> picture, bool isCloudImageEnhanceSupported)
319 {
320 MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnProcessImageDone Picture.");
321 }
322
OnDeliveryLowQualityImage(const std::string &imageId, std::shared_ptr<Media::Picture> picture)323 void TestDeferredPhotoProcSessionCallback::OnDeliveryLowQualityImage(const std::string &imageId,
324 std::shared_ptr<Media::Picture> picture)
325 {
326 MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnDeliveryLowQualityImage.");
327 }
328
OnError(const std::string& imageId, const DpsErrorCode errorCode)329 void TestDeferredPhotoProcSessionCallback::OnError(const std::string& imageId, const DpsErrorCode errorCode)
330 {
331 MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnError.");
332 }
333
OnStateChanged(const DpsStatusCode status)334 void TestDeferredPhotoProcSessionCallback::OnStateChanged(const DpsStatusCode status)
335 {
336 MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnStateChanged.");
337 }
338
SurfaceListener(const char* testName, SurfaceType type, int32_t &fd, sptr<IConsumerSurface> surface)339 SurfaceListener::SurfaceListener(const char* testName, SurfaceType type, int32_t &fd, sptr<IConsumerSurface> surface)
340 : testName_(testName), surfaceType_(type), fd_(fd), surface_(surface) {
341 }
342
OnBufferAvailable()343 void SurfaceListener::OnBufferAvailable()
344 {
345 int32_t flushFence = 0;
346 int64_t timestamp = 0;
347 OHOS::Rect damage;
348 MEDIA_DEBUG_LOG("SurfaceListener::OnBufferAvailable(), testName_: %{public}s, surfaceType_: %{public}d",
349 testName_, surfaceType_);
350 OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
351 if (surface_ == nullptr) {
352 MEDIA_ERR_LOG("OnBufferAvailable:surface_ is null");
353 return;
354 }
355 surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
356 if (buffer != nullptr) {
357 char* addr = static_cast<char *>(buffer->GetVirAddr());
358 int32_t size = buffer->GetSize();
359
360 switch (surfaceType_) {
361 case SurfaceType::PREVIEW:
362 if (previewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
363 && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
364 MEDIA_ERR_LOG("Failed to save buffer");
365 previewIndex_ = 0;
366 }
367 previewIndex_++;
368 break;
369
370 case SurfaceType::SECOND_PREVIEW:
371 if (secondPreviewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
372 && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
373 MEDIA_ERR_LOG("Failed to save buffer");
374 secondPreviewIndex_ = 0;
375 }
376 secondPreviewIndex_++;
377 break;
378
379 case SurfaceType::PHOTO:
380 if (TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
381 MEDIA_ERR_LOG("Failed to save buffer");
382 }
383 break;
384
385 case SurfaceType::VIDEO:
386 if (fd_ == -1 && (TestUtils::SaveVideoFile(nullptr, 0, VideoSaveMode::CREATE, fd_) != CAMERA_OK)) {
387 MEDIA_ERR_LOG("Failed to Create video file");
388 }
389 if (TestUtils::SaveVideoFile(addr, size, VideoSaveMode::APPEND, fd_) != CAMERA_OK) {
390 MEDIA_ERR_LOG("Failed to save buffer");
391 }
392 break;
393
394 default:
395 MEDIA_ERR_LOG("Unexpected type");
396 break;
397 }
398 surface_->ReleaseBuffer(buffer, -1);
399 } else {
400 MEDIA_ERR_LOG("AcquireBuffer failed!");
401 }
402 }
403 } // namespace CameraStandard
404 } // namespace OHOS
405
406