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 "camera_capture_video.h"
17 #include <securec.h>
18 #include <unistd.h>
19 #include "camera_util.h"
20 #include "camera_log.h"
21 #include "test_common.h"
22 
23 #include "ipc_skeleton.h"
24 #include "access_token.h"
25 #include "hap_token_info.h"
26 #include "accesstoken_kit.h"
27 #include "nativetoken_kit.h"
28 #include "token_setproc.h"
29 
30 using namespace OHOS;
31 using namespace OHOS::CameraStandard;
32 
PhotoModeUsage(FILE * fp)33 static void PhotoModeUsage(FILE* fp)
34 {
35     int32_t result = 0;
36 
37     result = fprintf(fp,
38         "---------------------\n"
39         "Running in Photo mode\n"
40         "---------------------\n"
41         "Options:\n"
42         "h      Print this message\n"
43         "c      Capture one picture\n"
44         "v      Switch to video mode\n"
45         "d      Double preview mode\n"
46         "q      Quit this app\n");
47     if (result < 0) {
48         MEDIA_ERR_LOG("Failed to display menu, %{public}d", result);
49     }
50 }
51 
VideoModeUsage(FILE * fp)52 static void VideoModeUsage(FILE* fp)
53 {
54     int32_t result = 0;
55 
56     result = fprintf(fp,
57         "---------------------\n"
58         "Running in Video mode\n"
59         "---------------------\n"
60         "Options:\n"
61         "h      Print this message\n"
62         "r      Record video\n"
63         "p      Switch to Photo mode\n"
64         "d      Switch to Double preview mode\n"
65         "q      Quit this app\n");
66     if (result < 0) {
67         MEDIA_ERR_LOG("Failed to display menu, %{public}d", result);
68     }
69 }
70 
DoublePreviewModeUsage(FILE * fp)71 static void DoublePreviewModeUsage(FILE* fp)
72 {
73     int32_t result = 0;
74 
75     result = fprintf(fp,
76         "---------------------\n"
77         "Running in Double preview mode\n"
78         "---------------------\n"
79         "Options:\n"
80         "h      Print this message\n"
81         "p      Switch to Photo mode\n"
82         "v      Switch to Video mode\n"
83         "q      Quit this app\n");
84     if (result < 0) {
85         MEDIA_ERR_LOG("Failed to display menu, %{public}d", result);
86     }
87 }
88 
Usage(std::shared_ptr<CameraCaptureVideo> testObj)89 static void Usage(std::shared_ptr<CameraCaptureVideo> testObj)
90 {
91     if (testObj->currentState_ == State::PHOTO_CAPTURE) {
92         PhotoModeUsage(stdout);
93     } else if (testObj->currentState_ == State::VIDEO_RECORDING) {
94         VideoModeUsage(stdout);
95     } else if (testObj->currentState_ == State::DOUBLE_PREVIEW) {
96         DoublePreviewModeUsage(stdout);
97     }
98     return;
99 }
100 
PutMenuAndGetChr(std::shared_ptr<CameraCaptureVideo> & testObj)101 static char PutMenuAndGetChr(std::shared_ptr<CameraCaptureVideo> &testObj)
102 {
103     int32_t result = 0;
104     char userInput[1];
105 
106     Usage(testObj);
107     result = scanf_s(" %c", &userInput, 1);
108     if (result == 0) {
109         return 'h';
110     }
111     return userInput[0];
112 }
113 
SwitchMode(std::shared_ptr<CameraCaptureVideo> testObj,State state)114 static int32_t SwitchMode(std::shared_ptr<CameraCaptureVideo> testObj, State state)
115 {
116     int32_t result = CAMERA_OK;
117 
118     if (testObj->currentState_ != state) {
119         testObj->Release();
120         testObj->currentState_ = state;
121         result = testObj->StartPreview();
122     }
123     return result;
124 }
125 
ProcessResult(int32_t result,std::shared_ptr<CameraCaptureVideo> testObj)126 static void ProcessResult(int32_t result, std::shared_ptr<CameraCaptureVideo> testObj)
127 {
128     if (result != CAMERA_OK) {
129         std::cout << "Operation Failed!, Check logs for more details!, result: " << result << std::endl;
130         testObj->Release();
131         exit(EXIT_SUCCESS);
132     }
133 }
134 
DisplayMenu(std::shared_ptr<CameraCaptureVideo> testObj)135 static void DisplayMenu(std::shared_ptr<CameraCaptureVideo> testObj)
136 {
137     char c = 'h';
138     int32_t result = CAMERA_OK;
139     while (result == CAMERA_OK) {
140         switch (c) {
141             case 'h':
142                 c = PutMenuAndGetChr(testObj);
143                 break;
144 
145             case 'c':
146                 if (testObj->currentState_ == State::PHOTO_CAPTURE) {
147                     result = testObj->TakePhoto();
148                 }
149                 if (result == CAMERA_OK) {
150                     c = PutMenuAndGetChr(testObj);
151                 }
152                 break;
153 
154             case 'r':
155                 if (testObj->currentState_ == State::VIDEO_RECORDING) {
156                     result = testObj->RecordVideo();
157                 }
158                 if (result == CAMERA_OK) {
159                     c = PutMenuAndGetChr(testObj);
160                 }
161                 break;
162 
163             case 'v':
164                 if (testObj->currentState_ != State::VIDEO_RECORDING) {
165                     result = SwitchMode(testObj, State::VIDEO_RECORDING);
166                 }
167                 if (result == CAMERA_OK) {
168                     c = PutMenuAndGetChr(testObj);
169                 }
170                 break;
171 
172             case 'p':
173                 if (testObj->currentState_ != State::PHOTO_CAPTURE) {
174                     result = SwitchMode(testObj, State::PHOTO_CAPTURE);
175                 }
176                 if (result == CAMERA_OK) {
177                     c = PutMenuAndGetChr(testObj);
178                 }
179                 break;
180 
181             case 'd':
182                 if (testObj->currentState_ != State::DOUBLE_PREVIEW) {
183                     result = SwitchMode(testObj, State::DOUBLE_PREVIEW);
184                 }
185                 if (result == CAMERA_OK) {
186                     c = PutMenuAndGetChr(testObj);
187                 }
188                 break;
189 
190             case 'q':
191                 testObj->Release();
192                 exit(EXIT_SUCCESS);
193 
194             default:
195                 c = PutMenuAndGetChr(testObj);
196                 break;
197         }
198     }
199     ProcessResult(result, testObj);
200 }
201 
CameraCaptureVideo()202 CameraCaptureVideo::CameraCaptureVideo()
203 {
204     previewWidth_ = PREVIEW_WIDTH;
205     previewHeight_ = PREVIEW_HEIGHT;
206     previewWidth2_ = SECOND_PREVIEW_WIDTH;
207     previewHeight2_ = SECOND_PREVIEW_HEIGHT;
208     photoWidth_ = PHOTO_WIDTH;
209     photoHeight_ = PHOTO_HEIGHT;
210     videoWidth_ = VIDEO_WIDTH;
211     videoHeight_ = VIDEO_HEIGHT;
212     previewFormat_ = CAMERA_FORMAT_YUV_420_SP;
213     photoFormat_ = CAMERA_FORMAT_JPEG;
214     videoFormat_ = CAMERA_FORMAT_YUV_420_SP;
215     currentState_ = State::PHOTO_CAPTURE;
216     fd_ = -1;
217 }
218 
TakePhoto()219 int32_t CameraCaptureVideo::TakePhoto()
220 {
221     int32_t result = -1;
222 
223     if (photoOutput_ == nullptr) {
224         MEDIA_ERR_LOG("photoOutput_ is null");
225         return result;
226     }
227 
228     result = ((sptr<PhotoOutput> &)photoOutput_)->Capture();
229     if (result != CAMERA_OK) {
230         MEDIA_ERR_LOG("Failed to capture, result: %{public}d", result);
231         return result;
232     }
233     sleep(GAP_AFTER_CAPTURE);
234     return result;
235 }
236 
RecordVideo()237 int32_t CameraCaptureVideo::RecordVideo()
238 {
239     int32_t result = -1;
240 
241     if (videoOutput_ == nullptr) {
242         MEDIA_ERR_LOG("videoOutput_ is null");
243         return result;
244     }
245 
246     result = ((sptr<VideoOutput> &)videoOutput_)->Start();
247     if (result != CAMERA_OK) {
248         MEDIA_ERR_LOG("Failed to start recording, result: %{public}d", result);
249         return result;
250     }
251     sleep(VIDEO_CAPTURE_DURATION);
252     result = ((sptr<VideoOutput> &)videoOutput_)->Stop();
253     if (result != CAMERA_OK) {
254         MEDIA_ERR_LOG("Failed to stop recording, result: %{public}d", result);
255         return result;
256     }
257     sleep(GAP_AFTER_CAPTURE);
258     result = TestUtils::SaveVideoFile(nullptr, 0, VideoSaveMode::CLOSE, fd_);
259     fd_ = -1;
260     return result;
261 }
262 
Release()263 void CameraCaptureVideo::Release()
264 {
265     if (captureSession_ != nullptr) {
266         captureSession_->Stop();
267         sleep(GAP_AFTER_STOP);
268         captureSession_->Release();
269         captureSession_ = nullptr;
270     }
271     if (cameraInput_ != nullptr) {
272         cameraInput_->Release();
273         cameraInput_ = nullptr;
274     }
275     cameraInputCallback_ = nullptr;
276     previewSurface_ = nullptr;
277     previewSurfaceListener_ = nullptr;
278     previewOutput_ = nullptr;
279     previewOutputCallback_ = nullptr;
280     photoSurface_ = nullptr;
281     photoSurfaceListener_ = nullptr;
282     sptr<CaptureOutput> photoOutput_ = nullptr;
283     photoOutputCallback_ = nullptr;
284     videoSurface_ = nullptr;
285     videoSurfaceListener_ = nullptr;
286     videoOutput_ = nullptr;
287     videoOutputCallback_ = nullptr;
288     secondPreviewSurface_ = nullptr;
289     secondPreviewSurfaceListener_ = nullptr;
290     secondPreviewOutput_ = nullptr;
291     secondPreviewOutputCallback_ = nullptr;
292 }
293 
InitCameraManager()294 int32_t CameraCaptureVideo::InitCameraManager()
295 {
296     int32_t result = -1;
297 
298     if (cameraManager_ == nullptr) {
299         cameraManager_ = CameraManager::GetInstance();
300         if (cameraManager_ == nullptr) {
301             MEDIA_ERR_LOG("Failed to get camera manager!");
302             return result;
303         }
304         cameraMngrCallback_ = std::make_shared<TestCameraMngerCallback>(testName_);
305         cameraManager_->SetCallback(cameraMngrCallback_);
306     }
307     result = CAMERA_OK;
308     return result;
309 }
310 
InitCameraFormatAndResolution(sptr<CameraInput> & cameraInput)311 int32_t CameraCaptureVideo::InitCameraFormatAndResolution(sptr<CameraInput> &cameraInput)
312 {
313     std::vector<CameraFormat> previewFormats;
314     std::vector<CameraFormat> photoFormats;
315     std::vector<CameraFormat> videoFormats;
316     std::vector<Size> previewSizes;
317     std::vector<Size> photoSizes;
318     std::vector<Size> videoSizes;
319     std::vector<sptr<CameraDevice>> cameraObjList = cameraManager_->GetSupportedCameras();
320     if (cameraObjList.size() <= 0) {
321         MEDIA_ERR_LOG("No cameras are available!!!");
322     }
323     sptr<CameraOutputCapability> outputcapability =  cameraManager_->GetSupportedOutputCapability(cameraObjList[0]);
324     std::vector<Profile> previewProfiles = outputcapability->GetPreviewProfiles();
325     for (auto i : previewProfiles) {
326         previewFormats.push_back(i.GetCameraFormat());
327         previewSizes.push_back(i.GetSize());
328     }
329     MEDIA_DEBUG_LOG("Supported preview formats:");
330     for (auto &formatPreview : previewFormats) {
331         MEDIA_DEBUG_LOG("format : %{public}d", formatPreview);
332     }
333     if (std::find(previewFormats.begin(), previewFormats.end(), CAMERA_FORMAT_YUV_420_SP)
334         != previewFormats.end()) {
335         previewFormat_ = CAMERA_FORMAT_YUV_420_SP;
336         MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is present in supported preview formats");
337     } else if (!previewFormats.empty()) {
338         previewFormat_ = previewFormats[0];
339         MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is not present in supported preview formats");
340     }
341     std::vector<Profile> photoProfiles =  outputcapability->GetPhotoProfiles();
342     for (auto i : photoProfiles) {
343             photoFormats.push_back(i.GetCameraFormat());
344             photoSizes.push_back(i.GetSize());
345     }
346     MEDIA_DEBUG_LOG("Supported photo formats:");
347     for (auto &formatPhoto : photoFormats) {
348         MEDIA_DEBUG_LOG("format : %{public}d", formatPhoto);
349     }
350     if (!photoFormats.empty()) {
351         photoFormat_ = photoFormats[0];
352     }
353     std::vector<VideoProfile> videoProfiles = outputcapability->GetVideoProfiles();
354     for (auto i : videoProfiles) {
355         videoFormats.push_back(i.GetCameraFormat());
356         videoSizes.push_back(i.GetSize());
357         videoframerates_ = i.GetFrameRates();
358     }
359     MEDIA_DEBUG_LOG("Supported video formats:");
360     for (auto &formatVideo : videoFormats) {
361         MEDIA_DEBUG_LOG("format : %{public}d", formatVideo);
362     }
363     if (std::find(videoFormats.begin(), videoFormats.end(), CAMERA_FORMAT_YUV_420_SP) != videoFormats.end()) {
364         videoFormat_ = CAMERA_FORMAT_YUV_420_SP;
365         MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is present in supported video formats");
366     } else if (!videoFormats.empty()) {
367         videoFormat_ = videoFormats[0];
368         MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is not present in supported video formats");
369     }
370     MEDIA_DEBUG_LOG("Supported sizes for preview:");
371     for (auto &sizePreview : previewSizes) {
372         MEDIA_DEBUG_LOG("width: %{public}d, height: %{public}d", sizePreview.width, sizePreview.height);
373     }
374     MEDIA_DEBUG_LOG("Supported sizes for photo:");
375     for (auto &sizePhoto : photoSizes) {
376         MEDIA_DEBUG_LOG("width: %{public}d, height: %{public}d", sizePhoto.width, sizePhoto.height);
377     }
378     MEDIA_DEBUG_LOG("Supported sizes for video:");
379     for (auto &sizeVideo : videoSizes) {
380         MEDIA_DEBUG_LOG("width: %{public}d, height: %{public}d", sizeVideo.width, sizeVideo.height);
381     }
382 
383     if (!photoSizes.empty()) {
384         photoWidth_ = photoSizes[0].width;
385         photoHeight_ = photoSizes[0].height;
386     }
387     if (!videoSizes.empty()) {
388         videoWidth_ = videoSizes[0].width;
389         videoHeight_ = videoSizes[0].height;
390     }
391     if (!previewSizes.empty()) {
392         previewWidth_ = previewSizes[0].width;
393         previewHeight_ = previewSizes[0].height;
394     }
395     if (previewSizes.size() > 1) {
396         previewWidth2_ = previewSizes[1].width;
397         previewHeight2_ = previewSizes[1].height;
398     }
399 
400     MEDIA_DEBUG_LOG("previewFormat: %{public}d, previewWidth: %{public}d, previewHeight: %{public}d",
401                     previewFormat_, previewWidth_, previewHeight_);
402     MEDIA_DEBUG_LOG("previewFormat: %{public}d, previewWidth2: %{public}d, previewHeight2: %{public}d",
403                     previewFormat_, previewWidth2_, previewHeight2_);
404     MEDIA_DEBUG_LOG("photoFormat: %{public}d, photoWidth: %{public}d, photoHeight: %{public}d",
405                     photoFormat_, photoWidth_, photoHeight_);
406     MEDIA_DEBUG_LOG("videoFormat: %{public}d, videoWidth: %{public}d, videoHeight: %{public}d",
407                     videoFormat_, videoWidth_, videoHeight_);
408     return CAMERA_OK;
409 }
410 
InitCameraInput()411 int32_t CameraCaptureVideo::InitCameraInput()
412 {
413     int32_t result = -1;
414 
415     if (cameraManager_ == nullptr) {
416         MEDIA_ERR_LOG("cameraManager_ is null");
417         return result;
418     }
419 
420     if (cameraInput_ == nullptr) {
421         std::vector<sptr<CameraDevice>> cameraObjList = cameraManager_->GetSupportedCameras();
422         if (cameraObjList.size() <= 0) {
423             MEDIA_ERR_LOG("No cameras are available!!!");
424             return result;
425         }
426         cameraInput_ = cameraManager_->CreateCameraInput(cameraObjList[0]);
427         if (cameraInput_ == nullptr) {
428             MEDIA_ERR_LOG("Failed to create CameraInput");
429             return result;
430         }
431         cameraInput_->Open();
432         cameraInputCallback_ = std::make_shared<TestDeviceCallback>(testName_);
433         ((sptr<CameraInput> &)cameraInput_)->SetErrorCallback(cameraInputCallback_);
434         result = InitCameraFormatAndResolution((sptr<CameraInput> &)cameraInput_);
435         if (result != CAMERA_OK) {
436             MEDIA_ERR_LOG("Failed to initialize format and resolution for preview, photo and video");
437             return result;
438         }
439     }
440     result = CAMERA_OK;
441     return result;
442 }
443 
InitPreviewOutput()444 int32_t CameraCaptureVideo::InitPreviewOutput()
445 {
446     int32_t result = -1;
447     Size previewsize_;
448 
449     if (cameraManager_ == nullptr) {
450         MEDIA_ERR_LOG("cameraManager_ is null");
451         return result;
452     }
453 
454     if (previewOutput_ == nullptr) {
455         previewSurface_ = IConsumerSurface::Create();
456         if (previewSurface_ ==  nullptr) {
457             MEDIA_ERR_LOG("previewSurface_ is null");
458             return result;
459         }
460         previewsize_.width = previewWidth_;
461         previewsize_.height = previewHeight_;
462         previewSurface_->SetDefaultWidthAndHeight(previewWidth_, previewHeight_);
463         previewSurface_->SetUserData(CameraManager::surfaceFormat, std::to_string(previewFormat_));
464         Profile previewprofile_ = Profile(static_cast<CameraFormat>(previewFormat_), previewsize_);
465         previewSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_, SurfaceType::PREVIEW,
466                                                                     fd_, previewSurface_);
467         if (previewSurfaceListener_ == nullptr) {
468             MEDIA_ERR_LOG("fail to create new SurfaceListener");
469             return result;
470         }
471         previewSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)previewSurfaceListener_);
472         sptr<IBufferProducer> bp = previewSurface_->GetProducer();
473         sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
474         previewOutput_ = cameraManager_->CreatePreviewOutput(previewprofile_, pSurface);
475         if (previewOutput_ == nullptr) {
476             MEDIA_ERR_LOG("Failed to create previewOutput");
477             return result;
478         }
479         previewOutputCallback_ = std::make_shared<TestPreviewOutputCallback>(testName_);
480         ((sptr<PreviewOutput> &)previewOutput_)->SetCallback(previewOutputCallback_);
481     }
482     result = CAMERA_OK;
483     return result;
484 }
485 
InitSecondPreviewOutput()486 int32_t CameraCaptureVideo::InitSecondPreviewOutput()
487 {
488     int32_t result = -1;
489     Size previewsize2_;
490 
491     if (cameraManager_ == nullptr) {
492         MEDIA_ERR_LOG("cameraManager_ is null");
493         return result;
494     }
495 
496     if (secondPreviewOutput_ == nullptr) {
497         secondPreviewSurface_ = IConsumerSurface::Create();
498         if (secondPreviewSurface_ == nullptr) {
499             MEDIA_ERR_LOG("secondPreviewSurface_ is null");
500             return result;
501         }
502         previewsize2_.width = previewWidth2_;
503         previewsize2_.height = previewHeight2_;
504         Profile previewprofile2_ = Profile(static_cast<CameraFormat>(previewFormat_), previewsize2_);
505         secondPreviewSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_,
506             SurfaceType::SECOND_PREVIEW, fd_, secondPreviewSurface_);
507         if (secondPreviewSurfaceListener_ == nullptr) {
508             MEDIA_ERR_LOG("failed to create new SurfaceListener!");
509             return result;
510         }
511         secondPreviewSurface_->RegisterConsumerListener(
512             (sptr<IBufferConsumerListener> &)secondPreviewSurfaceListener_);
513         sptr<IBufferProducer> bp = secondPreviewSurface_->GetProducer();
514         sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
515         secondPreviewOutput_ = cameraManager_->CreatePreviewOutput(previewprofile2_, pSurface);
516         if (secondPreviewSurfaceListener_ ==  nullptr) {
517             MEDIA_ERR_LOG("Failed to create new SurfaceListener");
518             return result;
519         }
520         if (secondPreviewOutput_ == nullptr) {
521             MEDIA_ERR_LOG("Failed to create second previewOutput");
522             return result;
523         }
524         secondPreviewOutputCallback_ = std::make_shared<TestPreviewOutputCallback>(testName_);
525         ((sptr<PreviewOutput> &)secondPreviewOutput_)->SetCallback(secondPreviewOutputCallback_);
526     }
527     result = CAMERA_OK;
528     return result;
529 }
530 
InitPhotoOutput()531 int32_t CameraCaptureVideo::InitPhotoOutput()
532 {
533     int32_t result = -1;
534     Size photosize_;
535     if (cameraManager_ == nullptr) {
536         MEDIA_ERR_LOG("cameraManager_ is null");
537         return result;
538     }
539 
540     if (photoOutput_ == nullptr) {
541         photoSurface_ = IConsumerSurface::Create();
542         if (photoSurface_ == nullptr) {
543             MEDIA_ERR_LOG("photoSurface_ is null");
544             return result;
545         }
546         photosize_.width = photoWidth_;
547         photosize_.height = photoHeight_;
548         Profile photoprofile_ = Profile(static_cast<CameraFormat>(photoFormat_), photosize_);
549         photoSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_, SurfaceType::PHOTO, fd_, photoSurface_);
550         if (photoSurfaceListener_ == nullptr) {
551             MEDIA_ERR_LOG("Failed to create new SurfaceListener");
552             return result;
553         }
554         photoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoSurfaceListener_);
555         sptr<IBufferProducer> bp = photoSurface_->GetProducer();
556         photoOutput_ = cameraManager_->CreatePhotoOutput(photoprofile_, bp);
557         if (photoOutput_ == nullptr) {
558             MEDIA_ERR_LOG("Failed to create PhotoOutput");
559             return result;
560         }
561         photoOutputCallback_ = std::make_shared<TestPhotoOutputCallback>(testName_);
562         ((sptr<PhotoOutput> &)photoOutput_)->SetCallback(photoOutputCallback_);
563     }
564     result = CAMERA_OK;
565     return result;
566 }
567 
InitVideoOutput()568 int32_t CameraCaptureVideo::InitVideoOutput()
569 {
570     int32_t result = -1;
571     Size videosize_;
572 
573     if (cameraManager_ == nullptr) {
574         MEDIA_ERR_LOG("cameraManager_ is null");
575         return result;
576     }
577 
578     if (videoOutput_ == nullptr) {
579         videoSurface_ = IConsumerSurface::Create();
580         if (videoSurface_ == nullptr) {
581             MEDIA_ERR_LOG("videoSurface_ is null");
582             return result;
583         }
584         videosize_.width = videoWidth_;
585         videosize_.height = videoHeight_;
586         VideoProfile videoprofile_ =
587             VideoProfile(static_cast<CameraFormat>(videoFormat_), videosize_, videoframerates_);
588         videoSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_, SurfaceType::VIDEO, fd_, videoSurface_);
589         if (videoSurfaceListener_ == nullptr) {
590             MEDIA_ERR_LOG("Failed to create new SurfaceListener");
591             return result;
592         }
593         videoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)videoSurfaceListener_);
594         sptr<IBufferProducer> bp = videoSurface_->GetProducer();
595         sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
596         videoOutput_ = cameraManager_->CreateVideoOutput(videoprofile_, pSurface);
597         if (videoOutput_ == nullptr) {
598             MEDIA_ERR_LOG("Failed to create VideoOutput");
599             return result;
600         }
601         videoOutputCallback_ = std::make_shared<TestVideoOutputCallback>(testName_);
602         ((sptr<VideoOutput> &)videoOutput_)->SetCallback(videoOutputCallback_);
603     }
604     result = CAMERA_OK;
605     return result;
606 }
607 
AddOutputbyState()608 int32_t CameraCaptureVideo::AddOutputbyState()
609 {
610     int32_t result = -1;
611 
612     if (captureSession_ == nullptr) {
613         MEDIA_ERR_LOG("captureSession_ is null");
614         return result;
615     }
616     switch (currentState_) {
617         case State::PHOTO_CAPTURE:
618             result = InitPhotoOutput();
619             if (result == CAMERA_OK) {
620                 result = captureSession_->AddOutput(photoOutput_);
621             }
622             break;
623         case State::VIDEO_RECORDING:
624             result = InitVideoOutput();
625             if (result == CAMERA_OK) {
626                 result = captureSession_->AddOutput(videoOutput_);
627             }
628             break;
629         case State::DOUBLE_PREVIEW:
630             result = InitSecondPreviewOutput();
631             if (result == CAMERA_OK) {
632                 result = captureSession_->AddOutput(secondPreviewOutput_);
633             }
634             break;
635         default:
636             break;
637     }
638     return result;
639 }
640 
StartPreview()641 int32_t CameraCaptureVideo::StartPreview()
642 {
643     int32_t result = -1;
644 
645     result = InitCameraManager();
646     if (result != CAMERA_OK) {
647         return result;
648     }
649     result = InitCameraInput();
650     if (result != CAMERA_OK) {
651         return result;
652     }
653     captureSession_ = cameraManager_->CreateCaptureSession();
654     if (captureSession_ == nullptr) {
655         return result;
656     }
657     captureSession_->BeginConfig();
658     result = captureSession_->AddInput(cameraInput_);
659     if (CAMERA_OK != result) {
660         return result;
661     }
662     result = AddOutputbyState();
663     if (result != CAMERA_OK) {
664         return result;
665     }
666     result = InitPreviewOutput();
667     if (result != CAMERA_OK) {
668         return result;
669     }
670     result = captureSession_->AddOutput(previewOutput_);
671     if (CAMERA_OK != result) {
672         return result;
673     }
674     result = captureSession_->CommitConfig();
675     if (CAMERA_OK != result) {
676         MEDIA_ERR_LOG("Failed to Commit config");
677         return result;
678     }
679     result = captureSession_->Start();
680     MEDIA_DEBUG_LOG("Session started, result: %{public}d", result);
681     if (CAMERA_OK != result) {
682     }
683     return result;
684 }
685 
main(int32_t argc,char ** argv)686 int32_t main(int32_t argc, char **argv)
687 {
688     uint64_t tokenId;
689     const char *perms[0];
690     perms[0] = "ohos.permission.CAMERA";
691     NativeTokenInfoParams infoInstance = {
692         .dcapsNum = 0,
693         .permsNum = 1,
694         .aclsNum = 0,
695         .dcaps = NULL,
696         .perms = perms,
697         .acls = NULL,
698         .processName = "camera_capture_video",
699         .aplStr = "system_basic",
700     };
701     tokenId = GetAccessTokenId(&infoInstance);
702     SetSelfTokenID(tokenId);
703     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
704 
705     [[maybe_unused]] int32_t result = 0; // Default result
706     std::shared_ptr<CameraCaptureVideo> testObj =
707         std::make_shared<CameraCaptureVideo>();
708     result = testObj->StartPreview();
709     DisplayMenu(testObj);
710     return 0;
711 }
712