• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
OnCaptureError(const int32_t captureId,const int32_t errorCode) const220 void TestPhotoOutputCallback::OnCaptureError(const int32_t captureId, const int32_t errorCode) const
221 {
222     MEDIA_INFO_LOG("OnCaptureError(), testName_: %{public}s, captureID: %{public}d, errorCode: %{public}d",
223                    testName_, captureId, errorCode);
224 }
225 
TestPreviewOutputCallback(const char * testName)226 TestPreviewOutputCallback::TestPreviewOutputCallback(const char* testName) : testName_(testName) {
227 }
228 
OnFrameStarted() const229 void TestPreviewOutputCallback::OnFrameStarted() const
230 {
231     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
232 }
233 
OnFrameEnded(const int32_t frameCount) const234 void TestPreviewOutputCallback::OnFrameEnded(const int32_t frameCount) const
235 {
236     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
237                    testName_, frameCount);
238 }
239 
OnError(const int32_t errorCode) const240 void TestPreviewOutputCallback::OnError(const int32_t errorCode) const
241 {
242     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
243                    testName_, errorCode);
244 }
245 
OnSketchStatusDataChanged(const SketchStatusData & sketchData) const246 void TestPreviewOutputCallback::OnSketchStatusDataChanged(const SketchStatusData& sketchData) const
247 {
248     MEDIA_DEBUG_LOG("TestPreviewOutputCallback::OnSketchStatusDataChanged(), testName_: %{public}s", testName_);
249     return;
250 }
251 
TestVideoOutputCallback(const char * testName)252 TestVideoOutputCallback::TestVideoOutputCallback(const char* testName) : testName_(testName) {
253 }
254 
OnFrameStarted() const255 void TestVideoOutputCallback::OnFrameStarted() const
256 {
257     MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
258 }
259 
OnFrameEnded(const int32_t frameCount) const260 void TestVideoOutputCallback::OnFrameEnded(const int32_t frameCount) const
261 {
262     MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
263                    testName_, frameCount);
264 }
265 
OnError(const int32_t errorCode) const266 void TestVideoOutputCallback::OnError(const int32_t errorCode) const
267 {
268     MEDIA_INFO_LOG("TestVideoOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
269                    testName_, errorCode);
270 }
271 
TestMetadataOutputObjectCallback(const char * testName)272 TestMetadataOutputObjectCallback::TestMetadataOutputObjectCallback(const char* testName) : testName_(testName) {
273 }
274 
OnMetadataObjectsAvailable(std::vector<sptr<MetadataObject>> metaObjects) const275 void TestMetadataOutputObjectCallback::OnMetadataObjectsAvailable(std::vector<sptr<MetadataObject>> metaObjects) const
276 {
277     MEDIA_INFO_LOG("TestMetadataOutputObjectCallback:OnMetadataObjectsAvailable(), testName_: %{public}s, "
278                    "metaObjects size: %{public}zu", testName_, metaObjects.size());
279     for (size_t i = 0; i < metaObjects.size(); i++) {
280         MEDIA_INFO_LOG("TestMetadataOutputObjectCallback::OnMetadataObjectsAvailable "
281                        "metaObjInfo: Type(%{public}d), Rect{x(%{pulic}f),y(%{pulic}f),w(%{pulic}f),d(%{pulic}f)} "
282                        "Timestamp: %{public}" PRId64,
283                        metaObjects[i]->GetType(),
284                        metaObjects[i]->GetBoundingBox().topLeftX, metaObjects[i]->GetBoundingBox().topLeftY,
285                        metaObjects[i]->GetBoundingBox().width, metaObjects[i]->GetBoundingBox().height,
286                        static_cast<int64_t>(metaObjects[i]->GetTimestamp()));
287     }
288 }
289 
SurfaceListener(const char * testName,SurfaceType type,int32_t & fd,sptr<IConsumerSurface> surface)290 SurfaceListener::SurfaceListener(const char* testName, SurfaceType type, int32_t &fd, sptr<IConsumerSurface> surface)
291     : testName_(testName), surfaceType_(type), fd_(fd), surface_(surface) {
292 }
293 
OnBufferAvailable()294 void SurfaceListener::OnBufferAvailable()
295 {
296     int32_t flushFence = 0;
297     int64_t timestamp = 0;
298     OHOS::Rect damage;
299     MEDIA_DEBUG_LOG("SurfaceListener::OnBufferAvailable(), testName_: %{public}s, surfaceType_: %{public}d",
300                     testName_, surfaceType_);
301     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
302     if (surface_ == nullptr) {
303         MEDIA_ERR_LOG("OnBufferAvailable:surface_ is null");
304         return;
305     }
306     surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
307     if (buffer != nullptr) {
308         char* addr = static_cast<char *>(buffer->GetVirAddr());
309         int32_t size = buffer->GetSize();
310 
311         switch (surfaceType_) {
312             case SurfaceType::PREVIEW:
313                 if (previewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
314                     && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
315                     MEDIA_ERR_LOG("Failed to save buffer");
316                     previewIndex_ = 0;
317                 }
318                 previewIndex_++;
319                 break;
320 
321             case SurfaceType::SECOND_PREVIEW:
322                 if (secondPreviewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
323                     && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
324                     MEDIA_ERR_LOG("Failed to save buffer");
325                     secondPreviewIndex_ = 0;
326                 }
327                 secondPreviewIndex_++;
328                 break;
329 
330             case SurfaceType::PHOTO:
331                 if (TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
332                     MEDIA_ERR_LOG("Failed to save buffer");
333                 }
334                 break;
335 
336             case SurfaceType::VIDEO:
337                 if (fd_ == -1 && (TestUtils::SaveVideoFile(nullptr, 0, VideoSaveMode::CREATE, fd_) != CAMERA_OK)) {
338                     MEDIA_ERR_LOG("Failed to Create video file");
339                 }
340                 if (TestUtils::SaveVideoFile(addr, size, VideoSaveMode::APPEND, fd_) != CAMERA_OK) {
341                     MEDIA_ERR_LOG("Failed to save buffer");
342                 }
343                 break;
344 
345             default:
346                 MEDIA_ERR_LOG("Unexpected type");
347                 break;
348         }
349         surface_->ReleaseBuffer(buffer, -1);
350     } else {
351         MEDIA_ERR_LOG("AcquireBuffer failed!");
352     }
353 }
354 } // namespace CameraStandard
355 } // namespace OHOS
356 
357