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
DisplayMenu(std::shared_ptr<CameraCaptureVideo> testObj)126 static void DisplayMenu(std::shared_ptr<CameraCaptureVideo> testObj)
127 {
128 char c = 'h';
129 int32_t result = CAMERA_OK;
130
131 while (1 && (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 if (result != CAMERA_OK) {
192 std::cout << "Operation Failed!, Check logs for more details!, result: " << result << std::endl;
193 testObj->Release();
194 exit(EXIT_SUCCESS);
195 }
196 }
197
CameraCaptureVideo()198 CameraCaptureVideo::CameraCaptureVideo()
199 {
200 previewWidth_ = PREVIEW_WIDTH;
201 previewHeight_ = PREVIEW_HEIGHT;
202 previewWidth2_ = SECOND_PREVIEW_WIDTH;
203 previewHeight2_ = SECOND_PREVIEW_HEIGHT;
204 photoWidth_ = PHOTO_WIDTH;
205 photoHeight_ = PHOTO_HEIGHT;
206 videoWidth_ = VIDEO_WIDTH;
207 videoHeight_ = VIDEO_HEIGHT;
208 previewFormat_ = CAMERA_FORMAT_YUV_420_SP;
209 photoFormat_ = CAMERA_FORMAT_JPEG;
210 videoFormat_ = CAMERA_FORMAT_YUV_420_SP;
211 currentState_ = State::PHOTO_CAPTURE;
212 fd_ = -1;
213 }
214
TakePhoto()215 int32_t CameraCaptureVideo::TakePhoto()
216 {
217 int32_t result = -1;
218
219 if (photoOutput_ == nullptr) {
220 MEDIA_ERR_LOG("photoOutput_ is null");
221 return result;
222 }
223
224 result = ((sptr<PhotoOutput> &)photoOutput_)->Capture();
225 if (result != CAMERA_OK) {
226 MEDIA_ERR_LOG("Failed to capture, result: %{public}d", result);
227 return result;
228 }
229 sleep(GAP_AFTER_CAPTURE);
230 return result;
231 }
232
RecordVideo()233 int32_t CameraCaptureVideo::RecordVideo()
234 {
235 int32_t result = -1;
236
237 if (videoOutput_ == nullptr) {
238 MEDIA_ERR_LOG("videoOutput_ is null");
239 return result;
240 }
241
242 result = ((sptr<VideoOutput> &)videoOutput_)->Start();
243 if (result != CAMERA_OK) {
244 MEDIA_ERR_LOG("Failed to start recording, result: %{public}d", result);
245 return result;
246 }
247 sleep(VIDEO_CAPTURE_DURATION);
248 result = ((sptr<VideoOutput> &)videoOutput_)->Stop();
249 if (result != CAMERA_OK) {
250 MEDIA_ERR_LOG("Failed to stop recording, result: %{public}d", result);
251 return result;
252 }
253 sleep(GAP_AFTER_CAPTURE);
254 result = TestUtils::SaveVideoFile(nullptr, 0, VideoSaveMode::CLOSE, fd_);
255 fd_ = -1;
256 return result;
257 }
258
Release()259 void CameraCaptureVideo::Release()
260 {
261 if (captureSession_ != nullptr) {
262 captureSession_->Stop();
263 sleep(GAP_AFTER_STOP);
264 captureSession_->Release();
265 captureSession_ = nullptr;
266 }
267 if (cameraInput_ != nullptr) {
268 cameraInput_->Release();
269 cameraInput_ = nullptr;
270 }
271 cameraInputCallback_ = nullptr;
272 previewSurface_ = nullptr;
273 previewSurfaceListener_ = nullptr;
274 previewOutput_ = nullptr;
275 previewOutputCallback_ = nullptr;
276 photoSurface_ = nullptr;
277 photoSurfaceListener_ = nullptr;
278 sptr<CaptureOutput> photoOutput_ = nullptr;
279 photoOutputCallback_ = nullptr;
280 videoSurface_ = nullptr;
281 videoSurfaceListener_ = nullptr;
282 videoOutput_ = nullptr;
283 videoOutputCallback_ = nullptr;
284 secondPreviewSurface_ = nullptr;
285 secondPreviewSurfaceListener_ = nullptr;
286 secondPreviewOutput_ = nullptr;
287 secondPreviewOutputCallback_ = nullptr;
288 }
289
InitCameraManager()290 int32_t CameraCaptureVideo::InitCameraManager()
291 {
292 int32_t result = -1;
293
294 if (cameraManager_ == nullptr) {
295 cameraManager_ = CameraManager::GetInstance();
296 if (cameraManager_ == nullptr) {
297 MEDIA_ERR_LOG("Failed to get camera manager!");
298 return result;
299 }
300 cameraMngrCallback_ = std::make_shared<TestCameraMngerCallback>(testName_);
301 cameraManager_->SetCallback(cameraMngrCallback_);
302 }
303 result = CAMERA_OK;
304 return result;
305 }
306
InitCameraFormatAndResolution(sptr<CameraInput> & cameraInput)307 int32_t CameraCaptureVideo::InitCameraFormatAndResolution(sptr<CameraInput> &cameraInput)
308 {
309 std::vector<CameraFormat> previewFormats;
310 std::vector<CameraFormat> photoFormats;
311 std::vector<CameraFormat> videoFormats;
312 std::vector<Size> previewSizes;
313 std::vector<Size> photoSizes;
314 std::vector<Size> videoSizes;
315 std::vector<sptr<CameraDevice>> cameraObjList = cameraManager_->GetSupportedCameras();
316 if (cameraObjList.size() <= 0) {
317 MEDIA_ERR_LOG("No cameras are available!!!");
318 }
319 sptr<CameraOutputCapability> outputcapability = cameraManager_->GetSupportedOutputCapability(cameraObjList[0]);
320 std::vector<Profile> previewProfiles = outputcapability->GetPreviewProfiles();
321 for (auto i : previewProfiles) {
322 previewFormats.push_back(i.GetCameraFormat());
323 previewSizes.push_back(i.GetSize());
324 }
325 MEDIA_DEBUG_LOG("Supported preview formats:");
326 for (auto &formatPreview : previewFormats) {
327 MEDIA_DEBUG_LOG("format : %{public}d", formatPreview);
328 }
329 if (std::find(previewFormats.begin(), previewFormats.end(), CAMERA_FORMAT_YUV_420_SP)
330 != previewFormats.end()) {
331 previewFormat_ = CAMERA_FORMAT_YUV_420_SP;
332 MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is present in supported preview formats");
333 } else if (!previewFormats.empty()) {
334 previewFormat_ = previewFormats[0];
335 MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is not present in supported preview formats");
336 }
337 std::vector<Profile> photoProfiles = outputcapability->GetPhotoProfiles();
338 for (auto i : photoProfiles) {
339 photoFormats.push_back(i.GetCameraFormat());
340 photoSizes.push_back(i.GetSize());
341 }
342 MEDIA_DEBUG_LOG("Supported photo formats:");
343 for (auto &formatPhoto : photoFormats) {
344 MEDIA_DEBUG_LOG("format : %{public}d", formatPhoto);
345 }
346 if (!photoFormats.empty()) {
347 photoFormat_ = photoFormats[0];
348 }
349 std::vector<VideoProfile> videoProfiles = outputcapability->GetVideoProfiles();
350 for (auto i : videoProfiles) {
351 videoFormats.push_back(i.GetCameraFormat());
352 videoSizes.push_back(i.GetSize());
353 videoframerates_ = i.GetFrameRates();
354 }
355 MEDIA_DEBUG_LOG("Supported video formats:");
356 for (auto &formatVideo : videoFormats) {
357 MEDIA_DEBUG_LOG("format : %{public}d", formatVideo);
358 }
359 if (std::find(videoFormats.begin(), videoFormats.end(), CAMERA_FORMAT_YUV_420_SP) != videoFormats.end()) {
360 videoFormat_ = CAMERA_FORMAT_YUV_420_SP;
361 MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is present in supported video formats");
362 } else if (!videoFormats.empty()) {
363 videoFormat_ = videoFormats[0];
364 MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is not present in supported video formats");
365 }
366 MEDIA_DEBUG_LOG("Supported sizes for preview:");
367 for (auto &sizePreview : previewSizes) {
368 MEDIA_DEBUG_LOG("width: %{public}d, height: %{public}d", sizePreview.width, sizePreview.height);
369 }
370 MEDIA_DEBUG_LOG("Supported sizes for photo:");
371 for (auto &sizePhoto : photoSizes) {
372 MEDIA_DEBUG_LOG("width: %{public}d, height: %{public}d", sizePhoto.width, sizePhoto.height);
373 }
374 MEDIA_DEBUG_LOG("Supported sizes for video:");
375 for (auto &sizeVideo : videoSizes) {
376 MEDIA_DEBUG_LOG("width: %{public}d, height: %{public}d", sizeVideo.width, sizeVideo.height);
377 }
378
379 if (!photoSizes.empty()) {
380 photoWidth_ = photoSizes[0].width;
381 photoHeight_ = photoSizes[0].height;
382 }
383 if (!videoSizes.empty()) {
384 videoWidth_ = videoSizes[0].width;
385 videoHeight_ = videoSizes[0].height;
386 }
387 if (!previewSizes.empty()) {
388 previewWidth_ = previewSizes[0].width;
389 previewHeight_ = previewSizes[0].height;
390 }
391 if (previewSizes.size() > 1) {
392 previewWidth2_ = previewSizes[1].width;
393 previewHeight2_ = previewSizes[1].height;
394 }
395
396 MEDIA_DEBUG_LOG("previewFormat: %{public}d, previewWidth: %{public}d, previewHeight: %{public}d",
397 previewFormat_, previewWidth_, previewHeight_);
398 MEDIA_DEBUG_LOG("previewFormat: %{public}d, previewWidth2: %{public}d, previewHeight2: %{public}d",
399 previewFormat_, previewWidth2_, previewHeight2_);
400 MEDIA_DEBUG_LOG("photoFormat: %{public}d, photoWidth: %{public}d, photoHeight: %{public}d",
401 photoFormat_, photoWidth_, photoHeight_);
402 MEDIA_DEBUG_LOG("videoFormat: %{public}d, videoWidth: %{public}d, videoHeight: %{public}d",
403 videoFormat_, videoWidth_, videoHeight_);
404 return CAMERA_OK;
405 }
406
InitCameraInput()407 int32_t CameraCaptureVideo::InitCameraInput()
408 {
409 int32_t result = -1;
410
411 if (cameraManager_ == nullptr) {
412 MEDIA_ERR_LOG("cameraManager_ is null");
413 return result;
414 }
415
416 if (cameraInput_ == nullptr) {
417 std::vector<sptr<CameraDevice>> cameraObjList = cameraManager_->GetSupportedCameras();
418 if (cameraObjList.size() <= 0) {
419 MEDIA_ERR_LOG("No cameras are available!!!");
420 return result;
421 }
422 cameraInput_ = cameraManager_->CreateCameraInput(cameraObjList[0]);
423 if (cameraInput_ == nullptr) {
424 MEDIA_ERR_LOG("Failed to create CameraInput");
425 return result;
426 }
427 cameraInput_->Open();
428 cameraInputCallback_ = std::make_shared<TestDeviceCallback>(testName_);
429 ((sptr<CameraInput> &)cameraInput_)->SetErrorCallback(cameraInputCallback_);
430 result = InitCameraFormatAndResolution((sptr<CameraInput> &)cameraInput_);
431 if (result != CAMERA_OK) {
432 MEDIA_ERR_LOG("Failed to initialize format and resolution for preview, photo and video");
433 return result;
434 }
435 }
436 result = CAMERA_OK;
437 return result;
438 }
439
InitPreviewOutput()440 int32_t CameraCaptureVideo::InitPreviewOutput()
441 {
442 int32_t result = -1;
443 Size previewsize_;
444
445 if (cameraManager_ == nullptr) {
446 MEDIA_ERR_LOG("cameraManager_ is null");
447 return result;
448 }
449
450 if (previewOutput_ == nullptr) {
451 previewSurface_ = IConsumerSurface::Create();
452 if (previewSurface_ == nullptr) {
453 MEDIA_ERR_LOG("previewSurface_ is null");
454 return result;
455 }
456 previewsize_.width = previewWidth_;
457 previewsize_.height = previewHeight_;
458 previewSurface_->SetDefaultWidthAndHeight(previewWidth_, previewHeight_);
459 previewSurface_->SetUserData(CameraManager::surfaceFormat, std::to_string(previewFormat_));
460 Profile previewprofile_ = Profile(static_cast<CameraFormat>(previewFormat_), previewsize_);
461 previewSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_, SurfaceType::PREVIEW,
462 fd_, previewSurface_);
463 previewSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)previewSurfaceListener_);
464 sptr<IBufferProducer> bp = previewSurface_->GetProducer();
465 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
466 previewOutput_ = cameraManager_->CreatePreviewOutput(previewprofile_, pSurface);
467 if (previewOutput_ == nullptr) {
468 MEDIA_ERR_LOG("Failed to create previewOutput");
469 return result;
470 }
471 previewOutputCallback_ = std::make_shared<TestPreviewOutputCallback>(testName_);
472 ((sptr<PreviewOutput> &)previewOutput_)->SetCallback(previewOutputCallback_);
473 }
474 result = CAMERA_OK;
475 return result;
476 }
477
InitSecondPreviewOutput()478 int32_t CameraCaptureVideo::InitSecondPreviewOutput()
479 {
480 int32_t result = -1;
481 Size previewsize2_;
482
483 if (cameraManager_ == nullptr) {
484 MEDIA_ERR_LOG("cameraManager_ is null");
485 return result;
486 }
487
488 if (secondPreviewOutput_ == nullptr) {
489 secondPreviewSurface_ = IConsumerSurface::Create();
490 if (secondPreviewSurface_ == nullptr) {
491 MEDIA_ERR_LOG("secondPreviewSurface_ is null");
492 return result;
493 }
494 previewsize2_.width = previewWidth2_;
495 previewsize2_.height = previewHeight2_;
496 Profile previewprofile2_ = Profile(static_cast<CameraFormat>(previewFormat_), previewsize2_);
497 secondPreviewSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_,
498 SurfaceType::SECOND_PREVIEW, fd_, secondPreviewSurface_);
499 secondPreviewSurface_->RegisterConsumerListener(
500 (sptr<IBufferConsumerListener> &)secondPreviewSurfaceListener_);
501 sptr<IBufferProducer> bp = secondPreviewSurface_->GetProducer();
502 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
503 secondPreviewOutput_ = cameraManager_->CreatePreviewOutput(previewprofile2_, pSurface);
504 if (secondPreviewSurfaceListener_ == nullptr) {
505 MEDIA_ERR_LOG("Failed to create new SurfaceListener");
506 return result;
507 }
508 if (secondPreviewOutput_ == nullptr) {
509 MEDIA_ERR_LOG("Failed to create second previewOutput");
510 return result;
511 }
512 secondPreviewOutputCallback_ = std::make_shared<TestPreviewOutputCallback>(testName_);
513 ((sptr<PreviewOutput> &)secondPreviewOutput_)->SetCallback(secondPreviewOutputCallback_);
514 }
515 result = CAMERA_OK;
516 return result;
517 }
518
InitPhotoOutput()519 int32_t CameraCaptureVideo::InitPhotoOutput()
520 {
521 int32_t result = -1;
522 Size photosize_;
523 if (cameraManager_ == nullptr) {
524 MEDIA_ERR_LOG("cameraManager_ is null");
525 return result;
526 }
527
528 if (photoOutput_ == nullptr) {
529 photoSurface_ = IConsumerSurface::Create();
530 if (photoSurface_ == nullptr) {
531 MEDIA_ERR_LOG("photoSurface_ is null");
532 return result;
533 }
534 photosize_.width = photoWidth_;
535 photosize_.height = photoHeight_;
536 Profile photoprofile_ = Profile(static_cast<CameraFormat>(photoFormat_), photosize_);
537 photoSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_, SurfaceType::PHOTO, fd_, photoSurface_);
538 if (photoSurfaceListener_ == nullptr) {
539 MEDIA_ERR_LOG("Failed to create new SurfaceListener");
540 return result;
541 }
542 photoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoSurfaceListener_);
543 sptr<IBufferProducer> bp = photoSurface_->GetProducer();
544 photoOutput_ = cameraManager_->CreatePhotoOutput(photoprofile_, bp);
545 if (photoOutput_ == nullptr) {
546 MEDIA_ERR_LOG("Failed to create PhotoOutput");
547 return result;
548 }
549 photoOutputCallback_ = std::make_shared<TestPhotoOutputCallback>(testName_);
550 ((sptr<PhotoOutput> &)photoOutput_)->SetCallback(photoOutputCallback_);
551 }
552 result = CAMERA_OK;
553 return result;
554 }
555
InitVideoOutput()556 int32_t CameraCaptureVideo::InitVideoOutput()
557 {
558 int32_t result = -1;
559 Size videosize_;
560
561 if (cameraManager_ == nullptr) {
562 MEDIA_ERR_LOG("cameraManager_ is null");
563 return result;
564 }
565
566 if (videoOutput_ == nullptr) {
567 videoSurface_ = IConsumerSurface::Create();
568 if (videoSurface_ == nullptr) {
569 MEDIA_ERR_LOG("videoSurface_ is null");
570 return result;
571 }
572 videosize_.width = videoWidth_;
573 videosize_.height = videoHeight_;
574 VideoProfile videoprofile_ =
575 VideoProfile(static_cast<CameraFormat>(videoFormat_), videosize_, videoframerates_);
576 videoSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_, SurfaceType::VIDEO, fd_, videoSurface_);
577 if (videoSurfaceListener_ == nullptr) {
578 MEDIA_ERR_LOG("Failed to create new SurfaceListener");
579 return result;
580 }
581 videoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)videoSurfaceListener_);
582 sptr<IBufferProducer> bp = videoSurface_->GetProducer();
583 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(bp);
584 videoOutput_ = cameraManager_->CreateVideoOutput(videoprofile_, pSurface);
585 if (videoOutput_ == nullptr) {
586 MEDIA_ERR_LOG("Failed to create VideoOutput");
587 return result;
588 }
589 videoOutputCallback_ = std::make_shared<TestVideoOutputCallback>(testName_);
590 ((sptr<VideoOutput> &)videoOutput_)->SetCallback(videoOutputCallback_);
591 }
592 result = CAMERA_OK;
593 return result;
594 }
595
AddOutputbyState()596 int32_t CameraCaptureVideo::AddOutputbyState()
597 {
598 int32_t result = -1;
599
600 if (captureSession_ == nullptr) {
601 MEDIA_ERR_LOG("captureSession_ is null");
602 return result;
603 }
604 switch (currentState_) {
605 case State::PHOTO_CAPTURE:
606 result = InitPhotoOutput();
607 if (result == CAMERA_OK) {
608 result = captureSession_->AddOutput(photoOutput_);
609 }
610 break;
611 case State::VIDEO_RECORDING:
612 result = InitVideoOutput();
613 if (result == CAMERA_OK) {
614 result = captureSession_->AddOutput(videoOutput_);
615 }
616 break;
617 case State::DOUBLE_PREVIEW:
618 result = InitSecondPreviewOutput();
619 if (result == CAMERA_OK) {
620 result = captureSession_->AddOutput(secondPreviewOutput_);
621 }
622 break;
623 default:
624 break;
625 }
626 return result;
627 }
628
StartPreview()629 int32_t CameraCaptureVideo::StartPreview()
630 {
631 int32_t result = -1;
632
633 result = InitCameraManager();
634 if (result != CAMERA_OK) {
635 return result;
636 }
637 result = InitCameraInput();
638 if (result != CAMERA_OK) {
639 return result;
640 }
641 captureSession_ = cameraManager_->CreateCaptureSession();
642 if (captureSession_ == nullptr) {
643 return result;
644 }
645 captureSession_->BeginConfig();
646 result = captureSession_->AddInput(cameraInput_);
647 if (CAMERA_OK != result) {
648 return result;
649 }
650 result = AddOutputbyState();
651 if (result != CAMERA_OK) {
652 return result;
653 }
654 result = InitPreviewOutput();
655 if (result != CAMERA_OK) {
656 return result;
657 }
658 result = captureSession_->AddOutput(previewOutput_);
659 if (CAMERA_OK != result) {
660 return result;
661 }
662 result = captureSession_->CommitConfig();
663 if (CAMERA_OK != result) {
664 MEDIA_ERR_LOG("Failed to Commit config");
665 return result;
666 }
667 result = captureSession_->Start();
668 MEDIA_DEBUG_LOG("Session started, result: %{public}d", result);
669 if (CAMERA_OK != result) {
670 }
671 return result;
672 }
673
main(int32_t argc,char ** argv)674 int32_t main(int32_t argc, char **argv)
675 {
676 uint64_t tokenId;
677 const char *perms[0];
678 perms[0] = "ohos.permission.CAMERA";
679 NativeTokenInfoParams infoInstance = {
680 .dcapsNum = 0,
681 .permsNum = 1,
682 .aclsNum = 0,
683 .dcaps = NULL,
684 .perms = perms,
685 .acls = NULL,
686 .processName = "camera_capture_video",
687 .aplStr = "system_basic",
688 };
689 tokenId = GetAccessTokenId(&infoInstance);
690 SetSelfTokenID(tokenId);
691 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
692
693 [[maybe_unused]] int32_t result = 0; // Default result
694 std::shared_ptr<CameraCaptureVideo> testObj =
695 std::make_shared<CameraCaptureVideo>();
696 result = testObj->StartPreview();
697 DisplayMenu(testObj);
698 return 0;
699 }
700