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