• 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 <cstdio>
18 #include <fcntl.h>
19 #include <securec.h>
20 #include <sys/time.h>
21 #include <unistd.h>
22 #include "camera_util.h"
23 #include "camera_log.h"
24 
25 namespace OHOS {
26 namespace CameraStandard {
GetCameraMetadataFormat(CameraFormat format)27 camera_format_t TestUtils::GetCameraMetadataFormat(CameraFormat format)
28 {
29     camera_format_t metaFormat = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
30     const std::unordered_map<CameraFormat, camera_format_t> mapToMetadataFormat = {
31         {CAMERA_FORMAT_YUV_420_SP, OHOS_CAMERA_FORMAT_YCRCB_420_SP},
32         {CAMERA_FORMAT_JPEG, OHOS_CAMERA_FORMAT_JPEG},
33         {CAMERA_FORMAT_RGBA_8888, OHOS_CAMERA_FORMAT_RGBA_8888},
34         {CAMERA_FORMAT_YCBCR_420_888, OHOS_CAMERA_FORMAT_YCBCR_420_888}
35     };
36     auto itr = mapToMetadataFormat.find(format);
37     if (itr != mapToMetadataFormat.end()) {
38         metaFormat = itr->second;
39     }
40     return metaFormat;
41 }
GetCurrentLocalTimeStamp()42 uint64_t TestUtils::GetCurrentLocalTimeStamp()
43 {
44     std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
45         std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
46     auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
47     return tmp.count();
48 }
49 
SaveYUV(const char * buffer,int32_t size,SurfaceType type)50 int32_t TestUtils::SaveYUV(const char* buffer, int32_t size, SurfaceType type)
51 {
52     char path[PATH_MAX] = {0};
53     int32_t retVal;
54 
55     if ((buffer == nullptr) || (size == 0)) {
56         MEDIA_ERR_LOG("buffer is null or size is 0");
57         return -1;
58     }
59 
60     MEDIA_DEBUG_LOG("TestUtils::SaveYUV(), type: %{public}d", type);
61     if (type == SurfaceType::PREVIEW) {
62         (void)system("mkdir -p /data/media/preview");
63         retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/media/preview/%s_%lld.yuv", "preview",
64                            GetCurrentLocalTimeStamp());
65         if (retVal < 0) {
66             MEDIA_ERR_LOG("Path Assignment failed");
67             return -1;
68         }
69     } else if (type == SurfaceType::PHOTO) {
70         (void)system("mkdir -p /data/media/photo");
71         retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/media/photo/%s_%lld.jpg", "photo",
72                            GetCurrentLocalTimeStamp());
73         if (retVal < 0) {
74             MEDIA_ERR_LOG("Path Assignment failed");
75             return -1;
76         }
77     } else if (type == SurfaceType::SECOND_PREVIEW) {
78         (void)system("mkdir -p /data/media/preview2");
79         retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/data/media/preview2/%s_%lld.yuv", "preview2",
80                            GetCurrentLocalTimeStamp());
81         if (retVal < 0) {
82             MEDIA_ERR_LOG("Path Assignment failed");
83             return -1;
84         }
85     } else {
86         MEDIA_ERR_LOG("Unexpected flow!");
87         return -1;
88     }
89 
90     MEDIA_DEBUG_LOG("%s, saving file to %{private}s", __FUNCTION__, path);
91     int imgFd = open(path, O_RDWR | O_CREAT, FILE_PERMISSIONS_FLAG);
92     if (imgFd == -1) {
93         MEDIA_ERR_LOG("%s, open file failed, errno = %{public}s.", __FUNCTION__, strerror(errno));
94         return -1;
95     }
96     int ret = write(imgFd, buffer, size);
97     if (ret == -1) {
98         MEDIA_ERR_LOG("%s, write file failed, error = %{public}s", __FUNCTION__, strerror(errno));
99         close(imgFd);
100         return -1;
101     }
102     close(imgFd);
103     return 0;
104 }
105 
IsNumber(const char number[])106 bool TestUtils::IsNumber(const char number[])
107 {
108     for (int i = 0; number[i] != 0; i++) {
109         if (!std::isdigit(number[i])) {
110             return false;
111         }
112     }
113     return true;
114 }
115 
SaveVideoFile(const char * buffer,int32_t size,VideoSaveMode operationMode,int32_t & fd)116 int32_t TestUtils::SaveVideoFile(const char* buffer, int32_t size, VideoSaveMode operationMode, int32_t &fd)
117 {
118     int32_t retVal = 0;
119 
120     if (operationMode == VideoSaveMode::CREATE) {
121         char path[255] = {0};
122 
123         (void)system("mkdir -p /data/media/video");
124         retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]),
125                            "/data/media/video/%s_%lld.h264", "video", GetCurrentLocalTimeStamp());
126         if (retVal < 0) {
127             MEDIA_ERR_LOG("Failed to create video file name");
128             return -1;
129         }
130         MEDIA_DEBUG_LOG("%{public}s, save video to file %{private}s", __FUNCTION__, path);
131         fd = open(path, O_RDWR | O_CREAT, FILE_PERMISSIONS_FLAG);
132         if (fd == -1) {
133             std::cout << "open file failed, errno = " << strerror(errno) << std::endl;
134             return -1;
135         }
136     } else if (operationMode == VideoSaveMode::APPEND && fd != -1) {
137         int32_t ret = write(fd, buffer, size);
138         if (ret == -1) {
139             std::cout << "write file failed, error = " << strerror(errno) << std::endl;
140             close(fd);
141             fd = -1;
142             return fd;
143         }
144     } else { // VideoSaveMode::CLOSE
145         if (fd != -1) {
146             close(fd);
147             fd = -1;
148         }
149     }
150     return 0;
151 }
152 
TestCameraMngerCallback(const char * testName)153 TestCameraMngerCallback::TestCameraMngerCallback(const char* testName) : testName_(testName) {
154 }
155 
OnCameraStatusChanged(const CameraStatusInfo & cameraStatusInfo) const156 void TestCameraMngerCallback::OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const
157 {
158     MEDIA_DEBUG_LOG("OnCameraStatusChanged()");
159     return;
160 }
161 
OnFlashlightStatusChanged(const std::string & cameraID,const FlashStatus flashStatus) const162 void TestCameraMngerCallback::OnFlashlightStatusChanged(const std::string &cameraID,
163                                                         const FlashStatus flashStatus) const
164 {
165     MEDIA_DEBUG_LOG("OnFlashlightStatusChanged(), testName_: %{public}s, cameraID: %{public}s, flashStatus: %{public}d",
166                     testName_, cameraID.c_str(), flashStatus);
167     return;
168 }
169 
TestDeviceCallback(const char * testName)170 TestDeviceCallback::TestDeviceCallback(const char* testName) : testName_(testName) {
171 }
172 
OnError(const int32_t errorType,const int32_t errorMsg) const173 void TestDeviceCallback::OnError(const int32_t errorType, const int32_t errorMsg) const
174 {
175     MEDIA_DEBUG_LOG("TestDeviceCallback::OnError(), testName_: %{public}s, errorType: %{public}d, errorMsg: %{public}d",
176                     testName_, errorType, errorMsg);
177     return;
178 }
179 
TestPhotoOutputCallback(const char * testName)180 TestPhotoOutputCallback::TestPhotoOutputCallback(const char* testName) : testName_(testName) {
181 }
182 
OnCaptureStarted(const int32_t captureID) const183 void TestPhotoOutputCallback::OnCaptureStarted(const int32_t captureID) const
184 {
185     MEDIA_INFO_LOG("PhotoOutputCallback:OnCaptureStarted(), testName_: %{public}s, captureID: %{public}d",
186                    testName_, captureID);
187 }
188 
OnCaptureEnded(const int32_t captureID,const int32_t frameCount) const189 void TestPhotoOutputCallback::OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const
190 {
191     MEDIA_INFO_LOG("TestPhotoOutputCallback:OnCaptureEnded(), testName_: %{public}s, captureID: %{public}d,"
192                    " frameCount: %{public}d", testName_, captureID, frameCount);
193 }
194 
OnFrameShutter(const int32_t captureId,const uint64_t timestamp) const195 void TestPhotoOutputCallback::OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const
196 {
197     MEDIA_INFO_LOG("OnFrameShutter(), testName_: %{public}s, captureID: %{public}d", testName_, captureId);
198 }
199 
OnCaptureError(const int32_t captureId,const int32_t errorCode) const200 void TestPhotoOutputCallback::OnCaptureError(const int32_t captureId, const int32_t errorCode) const
201 {
202     MEDIA_INFO_LOG("OnCaptureError(), testName_: %{public}s, captureID: %{public}d, errorCode: %{public}d",
203                    testName_, captureId, errorCode);
204 }
205 
TestPreviewOutputCallback(const char * testName)206 TestPreviewOutputCallback::TestPreviewOutputCallback(const char* testName) : testName_(testName) {
207 }
208 
OnFrameStarted() const209 void TestPreviewOutputCallback::OnFrameStarted() const
210 {
211     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
212 }
OnFrameEnded(const int32_t frameCount) const213 void TestPreviewOutputCallback::OnFrameEnded(const int32_t frameCount) const
214 {
215     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
216                    testName_, frameCount);
217 }
OnError(const int32_t errorCode) const218 void TestPreviewOutputCallback::OnError(const int32_t errorCode) const
219 {
220     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
221                    testName_, errorCode);
222 }
223 
TestVideoOutputCallback(const char * testName)224 TestVideoOutputCallback::TestVideoOutputCallback(const char* testName) : testName_(testName) {
225 }
226 
OnFrameStarted() const227 void TestVideoOutputCallback::OnFrameStarted() const
228 {
229     MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
230 }
231 
OnFrameEnded(const int32_t frameCount) const232 void TestVideoOutputCallback::OnFrameEnded(const int32_t frameCount) const
233 {
234     MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
235                    testName_, frameCount);
236 }
237 
OnError(const int32_t errorCode) const238 void TestVideoOutputCallback::OnError(const int32_t errorCode) const
239 {
240     MEDIA_INFO_LOG("TestVideoOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
241                    testName_, errorCode);
242 }
243 
SurfaceListener(const char * testName,SurfaceType type,int32_t & fd,sptr<Surface> surface)244 SurfaceListener::SurfaceListener(const char* testName, SurfaceType type, int32_t &fd, sptr<Surface> surface)
245     : testName_(testName), surfaceType_(type), fd_(fd), surface_(surface) {
246 }
247 
OnBufferAvailable()248 void SurfaceListener::OnBufferAvailable()
249 {
250     int32_t flushFence = 0;
251     int64_t timestamp = 0;
252     OHOS::Rect damage;
253     MEDIA_DEBUG_LOG("SurfaceListener::OnBufferAvailable(), testName_: %{public}s, surfaceType_: %{public}d",
254                     testName_, surfaceType_);
255     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
256     if (surface_ == nullptr) {
257         MEDIA_ERR_LOG("OnBufferAvailable:surface_ is null");
258         return;
259     }
260     surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
261     if (buffer != nullptr) {
262         char* addr = static_cast<char *>(buffer->GetVirAddr());
263         int32_t size = buffer->GetSize();
264 
265         switch (surfaceType_) {
266             case SurfaceType::PREVIEW:
267                 if (previewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
268                     && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
269                     MEDIA_ERR_LOG("Failed to save buffer");
270                     previewIndex_ = 0;
271                 }
272                 previewIndex_++;
273                 break;
274 
275             case SurfaceType::SECOND_PREVIEW:
276                 if (secondPreviewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
277                     && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
278                     MEDIA_ERR_LOG("Failed to save buffer");
279                     secondPreviewIndex_ = 0;
280                 }
281                 secondPreviewIndex_++;
282                 break;
283 
284             case SurfaceType::PHOTO:
285                 if (TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
286                     MEDIA_ERR_LOG("Failed to save buffer");
287                 }
288                 break;
289 
290             case SurfaceType::VIDEO:
291                 if (fd_ == -1 && (TestUtils::SaveVideoFile(nullptr, 0, VideoSaveMode::CREATE, fd_) != CAMERA_OK)) {
292                     MEDIA_ERR_LOG("Failed to Create video file");
293                 }
294                 if (TestUtils::SaveVideoFile(addr, size, VideoSaveMode::APPEND, fd_) != CAMERA_OK) {
295                     MEDIA_ERR_LOG("Failed to save buffer");
296                 }
297                 break;
298 
299             default:
300                 MEDIA_ERR_LOG("Unexpected type");
301                 break;
302         }
303         surface_->ReleaseBuffer(buffer, -1);
304     } else {
305         MEDIA_ERR_LOG("AcquireBuffer failed!");
306     }
307 }
308 } // namespace CameraStandard
309 } // namespace OHOS
310 
311