• 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 
TestOnResultCallback(const char * testName)180 TestOnResultCallback::TestOnResultCallback(const char* testName) : testName_(testName) {
181 }
182 
OnResult(const uint64_t timestamp,const std::shared_ptr<Camera::CameraMetadata> & result) const183 void TestOnResultCallback::OnResult(const uint64_t timestamp,
184                                     const std::shared_ptr<Camera::CameraMetadata> &result) const
185 {
186     MEDIA_DEBUG_LOG("TestOnResultCallback::OnResult(), testName_: %{public}s",
187                     testName_);
188     return;
189 }
190 
191 
TestPhotoOutputCallback(const char * testName)192 TestPhotoOutputCallback::TestPhotoOutputCallback(const char* testName) : testName_(testName) {
193 }
194 
OnCaptureStarted(const int32_t captureID) const195 void TestPhotoOutputCallback::OnCaptureStarted(const int32_t captureID) const
196 {
197     MEDIA_INFO_LOG("PhotoOutputCallback:OnCaptureStarted(), testName_: %{public}s, captureID: %{public}d",
198                    testName_, captureID);
199 }
200 
OnCaptureEnded(const int32_t captureID,const int32_t frameCount) const201 void TestPhotoOutputCallback::OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const
202 {
203     MEDIA_INFO_LOG("TestPhotoOutputCallback:OnCaptureEnded(), testName_: %{public}s, captureID: %{public}d,"
204                    " frameCount: %{public}d", testName_, captureID, frameCount);
205 }
206 
OnFrameShutter(const int32_t captureId,const uint64_t timestamp) const207 void TestPhotoOutputCallback::OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const
208 {
209     MEDIA_INFO_LOG("OnFrameShutter(), testName_: %{public}s, captureID: %{public}d", testName_, captureId);
210 }
211 
OnCaptureError(const int32_t captureId,const int32_t errorCode) const212 void TestPhotoOutputCallback::OnCaptureError(const int32_t captureId, const int32_t errorCode) const
213 {
214     MEDIA_INFO_LOG("OnCaptureError(), testName_: %{public}s, captureID: %{public}d, errorCode: %{public}d",
215                    testName_, captureId, errorCode);
216 }
217 
TestPreviewOutputCallback(const char * testName)218 TestPreviewOutputCallback::TestPreviewOutputCallback(const char* testName) : testName_(testName) {
219 }
220 
OnFrameStarted() const221 void TestPreviewOutputCallback::OnFrameStarted() const
222 {
223     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
224 }
OnFrameEnded(const int32_t frameCount) const225 void TestPreviewOutputCallback::OnFrameEnded(const int32_t frameCount) const
226 {
227     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
228                    testName_, frameCount);
229 }
OnError(const int32_t errorCode) const230 void TestPreviewOutputCallback::OnError(const int32_t errorCode) const
231 {
232     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
233                    testName_, errorCode);
234 }
235 
TestVideoOutputCallback(const char * testName)236 TestVideoOutputCallback::TestVideoOutputCallback(const char* testName) : testName_(testName) {
237 }
238 
OnFrameStarted() const239 void TestVideoOutputCallback::OnFrameStarted() const
240 {
241     MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
242 }
243 
OnFrameEnded(const int32_t frameCount) const244 void TestVideoOutputCallback::OnFrameEnded(const int32_t frameCount) const
245 {
246     MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
247                    testName_, frameCount);
248 }
249 
OnError(const int32_t errorCode) const250 void TestVideoOutputCallback::OnError(const int32_t errorCode) const
251 {
252     MEDIA_INFO_LOG("TestVideoOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
253                    testName_, errorCode);
254 }
255 
SurfaceListener(const char * testName,SurfaceType type,int32_t & fd,sptr<IConsumerSurface> surface)256 SurfaceListener::SurfaceListener(const char* testName, SurfaceType type, int32_t &fd, sptr<IConsumerSurface> surface)
257     : testName_(testName), surfaceType_(type), fd_(fd), surface_(surface) {
258 }
259 
OnBufferAvailable()260 void SurfaceListener::OnBufferAvailable()
261 {
262     int32_t flushFence = 0;
263     int64_t timestamp = 0;
264     OHOS::Rect damage;
265     MEDIA_DEBUG_LOG("SurfaceListener::OnBufferAvailable(), testName_: %{public}s, surfaceType_: %{public}d",
266                     testName_, surfaceType_);
267     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
268     if (surface_ == nullptr) {
269         MEDIA_ERR_LOG("OnBufferAvailable:surface_ is null");
270         return;
271     }
272     surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
273     if (buffer != nullptr) {
274         char* addr = static_cast<char *>(buffer->GetVirAddr());
275         int32_t size = buffer->GetSize();
276 
277         switch (surfaceType_) {
278             case SurfaceType::PREVIEW:
279                 if (previewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
280                     && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
281                     MEDIA_ERR_LOG("Failed to save buffer");
282                     previewIndex_ = 0;
283                 }
284                 previewIndex_++;
285                 break;
286 
287             case SurfaceType::SECOND_PREVIEW:
288                 if (secondPreviewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
289                     && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
290                     MEDIA_ERR_LOG("Failed to save buffer");
291                     secondPreviewIndex_ = 0;
292                 }
293                 secondPreviewIndex_++;
294                 break;
295 
296             case SurfaceType::PHOTO:
297                 if (TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
298                     MEDIA_ERR_LOG("Failed to save buffer");
299                 }
300                 break;
301 
302             case SurfaceType::VIDEO:
303                 if (fd_ == -1 && (TestUtils::SaveVideoFile(nullptr, 0, VideoSaveMode::CREATE, fd_) != CAMERA_OK)) {
304                     MEDIA_ERR_LOG("Failed to Create video file");
305                 }
306                 if (TestUtils::SaveVideoFile(addr, size, VideoSaveMode::APPEND, fd_) != CAMERA_OK) {
307                     MEDIA_ERR_LOG("Failed to save buffer");
308                 }
309                 break;
310 
311             default:
312                 MEDIA_ERR_LOG("Unexpected type");
313                 break;
314         }
315         surface_->ReleaseBuffer(buffer, -1);
316     } else {
317         MEDIA_ERR_LOG("AcquireBuffer failed!");
318     }
319 }
320 } // namespace CameraStandard
321 } // namespace OHOS
322 
323