• 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_PRINT_ELOG(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_RETURN_RET_ELOG((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_RETURN_RET_ELOG(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_RETURN_RET_ELOG(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_RETURN_RET_ELOG(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_RETURN_RET_ELOG(imgFd == -1, -1,
93         "%s, open file failed, errno = %{public}s.", __FUNCTION__, strerror(errno));
94     fdsan_exchange_owner_tag(imgFd, 0, LOG_DOMAIN);
95     int ret = write(imgFd, buffer, size);
96     if (ret == -1) {
97         MEDIA_ERR_LOG("%s, write file failed, error = %{public}s", __FUNCTION__, strerror(errno));
98         fdsan_close_with_tag(imgFd, LOG_DOMAIN);
99         return -1;
100     }
101     fdsan_close_with_tag(imgFd, LOG_DOMAIN);
102     return 0;
103 }
104 
IsNumber(const char number[])105 bool TestUtils::IsNumber(const char number[])
106 {
107     for (int i = 0; number[i] != 0; i++) {
108         CHECK_RETURN_RET(!std::isdigit(number[i]), false);
109     }
110     return true;
111 }
112 
SaveVideoFile(const char * buffer,int32_t size,VideoSaveMode operationMode,int32_t & fd)113 int32_t TestUtils::SaveVideoFile(const char* buffer, int32_t size, VideoSaveMode operationMode, int32_t &fd)
114 {
115     int32_t retVal = 0;
116 
117     if (operationMode == VideoSaveMode::CREATE) {
118         char path[255] = {0};
119 
120         (void)system("mkdir -p /data/media/video");
121         retVal = sprintf_s(path, sizeof(path) / sizeof(path[0]),
122                            "/data/media/video/%s_%lld.h264", "video", GetCurrentLocalTimeStamp());
123         CHECK_RETURN_RET_ELOG(retVal < 0, -1, "Failed to create video file name");
124         MEDIA_DEBUG_LOG("%{public}s, save video to file %{private}s", __FUNCTION__, path);
125         fd = open(path, O_RDWR | O_CREAT, FILE_PERMISSIONS_FLAG);
126         if (fd == -1) {
127             std::cout << "open file failed, errno = " << strerror(errno) << std::endl;
128             return -1;
129         }
130         fdsan_exchange_owner_tag(fd, 0, LOG_DOMAIN);
131     } else if (operationMode == VideoSaveMode::APPEND && fd != -1) {
132         int32_t ret = write(fd, buffer, size);
133         if (ret == -1) {
134             std::cout << "write file failed, error = " << strerror(errno) << std::endl;
135             fdsan_close_with_tag(fd, LOG_DOMAIN);
136             fd = -1;
137             return fd;
138         }
139     } else { // VideoSaveMode::CLOSE
140         if (fd != -1) {
141             fdsan_close_with_tag(fd, LOG_DOMAIN);
142             fd = -1;
143         }
144     }
145     return 0;
146 }
147 
TestCameraMngerCallback(const char * testName)148 TestCameraMngerCallback::TestCameraMngerCallback(const char* testName) : testName_(testName) {
149 }
150 
OnCameraStatusChanged(const CameraStatusInfo & cameraStatusInfo) const151 void TestCameraMngerCallback::OnCameraStatusChanged(const CameraStatusInfo &cameraStatusInfo) const
152 {
153     MEDIA_DEBUG_LOG("OnCameraStatusChanged()");
154     return;
155 }
156 
OnFlashlightStatusChanged(const std::string & cameraID,const FlashStatus flashStatus) const157 void TestCameraMngerCallback::OnFlashlightStatusChanged(const std::string &cameraID,
158                                                         const FlashStatus flashStatus) const
159 {
160     MEDIA_DEBUG_LOG("OnFlashlightStatusChanged(), testName_: %{public}s, cameraID: %{public}s, flashStatus: %{public}d",
161                     testName_, cameraID.c_str(), flashStatus);
162     return;
163 }
164 
TestDeviceCallback(const char * testName)165 TestDeviceCallback::TestDeviceCallback(const char* testName) : testName_(testName) {
166 }
167 
OnError(const int32_t errorType,const int32_t errorMsg) const168 void TestDeviceCallback::OnError(const int32_t errorType, const int32_t errorMsg) const
169 {
170     MEDIA_DEBUG_LOG("TestDeviceCallback::OnError(), testName_: %{public}s, errorType: %{public}d, errorMsg: %{public}d",
171                     testName_, errorType, errorMsg);
172     return;
173 }
174 
TestOnResultCallback(const char * testName)175 TestOnResultCallback::TestOnResultCallback(const char* testName) : testName_(testName) {
176 }
177 
OnResult(const uint64_t timestamp,const std::shared_ptr<Camera::CameraMetadata> & result) const178 void TestOnResultCallback::OnResult(const uint64_t timestamp,
179                                     const std::shared_ptr<Camera::CameraMetadata> &result) const
180 {
181     MEDIA_DEBUG_LOG("TestOnResultCallback::OnResult(), testName_: %{public}s",
182                     testName_);
183     return;
184 }
185 
186 
TestPhotoOutputCallback(const char * testName)187 TestPhotoOutputCallback::TestPhotoOutputCallback(const char* testName) : testName_(testName) {
188 }
189 
OnCaptureStarted(const int32_t captureID) const190 void TestPhotoOutputCallback::OnCaptureStarted(const int32_t captureID) const
191 {
192     MEDIA_INFO_LOG("PhotoOutputCallback:OnCaptureStarted(), testName_: %{public}s, captureID: %{public}d",
193                    testName_, captureID);
194 }
195 
OnCaptureStarted(const int32_t captureID,uint32_t exposureTime) const196 void TestPhotoOutputCallback::OnCaptureStarted(const int32_t captureID, uint32_t exposureTime) const
197 {
198     MEDIA_INFO_LOG("PhotoOutputCallback:OnCaptureStarted(), testName_: %{public}s, captureID: %{public}d",
199                    testName_, captureID);
200 }
201 
OnCaptureEnded(const int32_t captureID,const int32_t frameCount) const202 void TestPhotoOutputCallback::OnCaptureEnded(const int32_t captureID, const int32_t frameCount) const
203 {
204     MEDIA_INFO_LOG("TestPhotoOutputCallback:OnCaptureEnded(), testName_: %{public}s, captureID: %{public}d,"
205                    " frameCount: %{public}d", testName_, captureID, frameCount);
206 }
207 
OnFrameShutter(const int32_t captureId,const uint64_t timestamp) const208 void TestPhotoOutputCallback::OnFrameShutter(const int32_t captureId, const uint64_t timestamp) const
209 {
210     MEDIA_INFO_LOG("OnFrameShutter(), testName_: %{public}s, captureID: %{public}d", testName_, captureId);
211 }
212 
OnFrameShutterEnd(const int32_t captureId,const uint64_t timestamp) const213 void TestPhotoOutputCallback::OnFrameShutterEnd(const int32_t captureId, const uint64_t timestamp) const
214 {
215     MEDIA_INFO_LOG("OnFrameShutterEnd(), testName_: %{public}s, captureID: %{public}d", testName_, captureId);
216 }
217 
OnCaptureReady(const int32_t captureId,const uint64_t timestamp) const218 void TestPhotoOutputCallback::OnCaptureReady(const int32_t captureId, const uint64_t timestamp) const
219 {
220     MEDIA_INFO_LOG("OnCaptureReady(), testName_: %{public}s, captureID: %{public}d", testName_, captureId);
221 }
222 
OnEstimatedCaptureDuration(const int32_t duration) const223 void TestPhotoOutputCallback::OnEstimatedCaptureDuration(const int32_t duration) const
224 {
225     MEDIA_INFO_LOG("OnEstimatedCaptureDuration(), duration: %{public}d", duration);
226 }
227 
OnOfflineDeliveryFinished(const int32_t captureId) const228 void TestPhotoOutputCallback::OnOfflineDeliveryFinished(const int32_t captureId) const
229 {
230     MEDIA_INFO_LOG("OnOfflineDeliveryFinished(), captureId: %{public}d", captureId);
231 }
232 
OnCaptureError(const int32_t captureId,const int32_t errorCode) const233 void TestPhotoOutputCallback::OnCaptureError(const int32_t captureId, const int32_t errorCode) const
234 {
235     MEDIA_INFO_LOG("OnCaptureError(), testName_: %{public}s, captureID: %{public}d, errorCode: %{public}d",
236                    testName_, captureId, errorCode);
237 }
238 
TestPreviewOutputCallback(const char * testName)239 TestPreviewOutputCallback::TestPreviewOutputCallback(const char* testName) : testName_(testName) {
240 }
241 
OnFrameStarted() const242 void TestPreviewOutputCallback::OnFrameStarted() const
243 {
244     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
245 }
246 
OnFrameEnded(const int32_t frameCount) const247 void TestPreviewOutputCallback::OnFrameEnded(const int32_t frameCount) const
248 {
249     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
250                    testName_, frameCount);
251 }
252 
OnError(const int32_t errorCode) const253 void TestPreviewOutputCallback::OnError(const int32_t errorCode) const
254 {
255     MEDIA_INFO_LOG("TestPreviewOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
256                    testName_, errorCode);
257 }
258 
OnSketchStatusDataChanged(const SketchStatusData & statusData) const259 void TestPreviewOutputCallback::OnSketchStatusDataChanged(const SketchStatusData& statusData) const
260 {
261     MEDIA_DEBUG_LOG("TestPreviewOutputCallback::OnSketchStatusDataChanged(), testName_: %{public}s", testName_);
262     return;
263 }
264 
TestVideoOutputCallback(const char * testName)265 TestVideoOutputCallback::TestVideoOutputCallback(const char* testName) : testName_(testName) {
266 }
267 
OnFrameStarted() const268 void TestVideoOutputCallback::OnFrameStarted() const
269 {
270     MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameStarted(), testName_: %{public}s", testName_);
271 }
272 
OnFrameEnded(const int32_t frameCount) const273 void TestVideoOutputCallback::OnFrameEnded(const int32_t frameCount) const
274 {
275     MEDIA_INFO_LOG("TestVideoOutputCallback:OnFrameEnded(), testName_: %{public}s, frameCount: %{public}d",
276                    testName_, frameCount);
277 }
278 
OnError(const int32_t errorCode) const279 void TestVideoOutputCallback::OnError(const int32_t errorCode) const
280 {
281     MEDIA_INFO_LOG("TestVideoOutputCallback:OnError(), testName_: %{public}s, errorCode: %{public}d",
282                    testName_, errorCode);
283 }
284 
OnDeferredVideoEnhancementInfo(const CaptureEndedInfoExt info) const285 void TestVideoOutputCallback::OnDeferredVideoEnhancementInfo(const CaptureEndedInfoExt info) const
286 {
287     MEDIA_INFO_LOG("TestVideoOutputCallback:OnDeferredVideoEnhancementInfo()");
288 }
289 
TestMetadataOutputObjectCallback(const char * testName)290 TestMetadataOutputObjectCallback::TestMetadataOutputObjectCallback(const char* testName) : testName_(testName) {
291 }
292 
OnMetadataObjectsAvailable(std::vector<sptr<MetadataObject>> metaObjects) const293 void TestMetadataOutputObjectCallback::OnMetadataObjectsAvailable(std::vector<sptr<MetadataObject>> metaObjects) const
294 {
295     MEDIA_INFO_LOG("TestMetadataOutputObjectCallback:OnMetadataObjectsAvailable(), testName_: %{public}s, "
296                    "metaObjects size: %{public}zu", testName_, metaObjects.size());
297     for (size_t i = 0; i < metaObjects.size(); i++) {
298         MEDIA_INFO_LOG("TestMetadataOutputObjectCallback::OnMetadataObjectsAvailable "
299                        "metaObjInfo: Type(%{public}d), Rect{x(%{pulic}f),y(%{pulic}f),w(%{pulic}f),d(%{pulic}f)} "
300                        "Timestamp: %{public}" PRId64,
301                        metaObjects[i]->GetType(),
302                        metaObjects[i]->GetBoundingBox().topLeftX, metaObjects[i]->GetBoundingBox().topLeftY,
303                        metaObjects[i]->GetBoundingBox().width, metaObjects[i]->GetBoundingBox().height,
304                        static_cast<int64_t>(metaObjects[i]->GetTimestamp()));
305     }
306 }
307 
OnProcessImageDone(const std::string & imageId,const uint8_t * addr,const long bytes,uint32_t cloudImageEnhanceFlag)308 void TestDeferredPhotoProcSessionCallback::OnProcessImageDone(const std::string& imageId,
309                                                               const uint8_t* addr,
310                                                               const long bytes, uint32_t cloudImageEnhanceFlag)
311 {
312     MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnProcessImageDone.");
313 }
314 
OnProcessImageDone(const std::string & imageId,std::shared_ptr<PictureIntf> picture,uint32_t cloudImageEnhanceFlag)315 void TestDeferredPhotoProcSessionCallback::OnProcessImageDone(const std::string &imageId,
316     std::shared_ptr<PictureIntf> picture, uint32_t cloudImageEnhanceFlag)
317 {
318     MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnProcessImageDone Picture.");
319 }
320 
OnDeliveryLowQualityImage(const std::string & imageId,std::shared_ptr<PictureIntf> picture)321 void TestDeferredPhotoProcSessionCallback::OnDeliveryLowQualityImage(const std::string &imageId,
322     std::shared_ptr<PictureIntf> picture)
323 {
324     MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnDeliveryLowQualityImage.");
325 }
326 
OnError(const std::string & imageId,const DpsErrorCode errorCode)327 void TestDeferredPhotoProcSessionCallback::OnError(const std::string& imageId, const DpsErrorCode errorCode)
328 {
329     MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnError.");
330 }
331 
OnStateChanged(const DpsStatusCode status)332 void TestDeferredPhotoProcSessionCallback::OnStateChanged(const DpsStatusCode status)
333 {
334     MEDIA_INFO_LOG("TestDeferredPhotoProcSessionCallback OnStateChanged.");
335 }
336 
OnProcessVideoDone(const std::string & videoId,const sptr<IPCFileDescriptor> ipcFd)337 void TestDeferredVideoProcSessionCallback::OnProcessVideoDone(
338     const std::string& videoId, const sptr<IPCFileDescriptor> ipcFd)
339 {
340     MEDIA_INFO_LOG("TestDeferredVideoProcSessionCallback OnProcessImageDone Picture.");
341 }
342 
OnError(const std::string & videoId,const DpsErrorCode errorCode)343 void TestDeferredVideoProcSessionCallback::OnError(const std::string& videoId, const DpsErrorCode errorCode)
344 {
345     MEDIA_INFO_LOG("TestDeferredVideoProcSessionCallback OnError.");
346 }
347 
OnStateChanged(const DpsStatusCode status)348 void TestDeferredVideoProcSessionCallback::OnStateChanged(const DpsStatusCode status)
349 {
350     MEDIA_INFO_LOG("TestDeferredVideoProcSessionCallback OnStateChanged.");
351 }
352 
SurfaceListener(const char * testName,SurfaceType type,int32_t & fd,sptr<IConsumerSurface> surface)353 SurfaceListener::SurfaceListener(const char* testName, SurfaceType type, int32_t &fd, sptr<IConsumerSurface> surface)
354     : testName_(testName), surfaceType_(type), fd_(fd), surface_(surface) {
355 }
356 
OnBufferAvailable()357 void SurfaceListener::OnBufferAvailable()
358 {
359     int32_t flushFence = 0;
360     int64_t timestamp = 0;
361     OHOS::Rect damage;
362     MEDIA_DEBUG_LOG("SurfaceListener::OnBufferAvailable(), testName_: %{public}s, surfaceType_: %{public}d",
363                     testName_, surfaceType_);
364     OHOS::sptr<OHOS::SurfaceBuffer> buffer = nullptr;
365     CHECK_RETURN_ELOG(surface_ == nullptr, "OnBufferAvailable:surface_ is null");
366     surface_->AcquireBuffer(buffer, flushFence, timestamp, damage);
367     if (buffer != nullptr) {
368         char* addr = static_cast<char *>(buffer->GetVirAddr());
369         int32_t size = buffer->GetSize();
370 
371         switch (surfaceType_) {
372             case SurfaceType::PREVIEW:
373                 if (previewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
374                     && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
375                     MEDIA_ERR_LOG("Failed to save buffer");
376                     previewIndex_ = 0;
377                 }
378                 previewIndex_++;
379                 break;
380 
381             case SurfaceType::SECOND_PREVIEW:
382                 if (secondPreviewIndex_ % TestUtils::PREVIEW_SKIP_FRAMES == 0
383                     && TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK) {
384                     MEDIA_ERR_LOG("Failed to save buffer");
385                     secondPreviewIndex_ = 0;
386                 }
387                 secondPreviewIndex_++;
388                 break;
389 
390             case SurfaceType::PHOTO:
391                 CHECK_PRINT_ELOG(TestUtils::SaveYUV(addr, size, surfaceType_) != CAMERA_OK,
392                     "Failed to save buffer");
393                 break;
394 
395             case SurfaceType::VIDEO:
396                 CHECK_PRINT_ELOG(fd_ == -1 && (TestUtils::SaveVideoFile(nullptr, 0, VideoSaveMode::CREATE, fd_) !=
397                     CAMERA_OK), "Failed to Create video file");
398                 CHECK_PRINT_ELOG(TestUtils::SaveVideoFile(addr, size, VideoSaveMode::APPEND, fd_) != CAMERA_OK,
399                     "Failed to save buffer");
400                 break;
401 
402             default:
403                 MEDIA_ERR_LOG("Unexpected type");
404                 break;
405         }
406         surface_->ReleaseBuffer(buffer, -1);
407     } else {
408         MEDIA_ERR_LOG("AcquireBuffer failed!");
409     }
410 }
411 } // namespace CameraStandard
412 } // namespace OHOS
413 
414