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