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