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_ = Surface::CreateSurfaceAsConsumer();
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 previewOutput_ = cameraManager_->CreatePreviewOutput(previewprofile_, previewSurface_);
465 if (previewOutput_ == nullptr) {
466 MEDIA_ERR_LOG("Failed to create previewOutput");
467 return result;
468 }
469 previewOutputCallback_ = std::make_shared<TestPreviewOutputCallback>(testName_);
470 ((sptr<PreviewOutput> &)previewOutput_)->SetCallback(previewOutputCallback_);
471 }
472 result = CAMERA_OK;
473 return result;
474 }
475
InitSecondPreviewOutput()476 int32_t CameraCaptureVideo::InitSecondPreviewOutput()
477 {
478 int32_t result = -1;
479 Size previewsize2_;
480
481 if (cameraManager_ == nullptr) {
482 MEDIA_ERR_LOG("cameraManager_ is null");
483 return result;
484 }
485
486 if (secondPreviewOutput_ == nullptr) {
487 secondPreviewSurface_ = Surface::CreateSurfaceAsConsumer();
488 if (secondPreviewSurface_ == nullptr) {
489 MEDIA_ERR_LOG("secondPreviewSurface_ is null");
490 return result;
491 }
492 previewsize2_.width = previewWidth2_;
493 previewsize2_.height = previewHeight2_;
494 Profile previewprofile2_ = Profile(static_cast<CameraFormat>(previewFormat_), previewsize2_);
495 secondPreviewSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_,
496 SurfaceType::SECOND_PREVIEW, fd_, secondPreviewSurface_);
497 secondPreviewSurface_->RegisterConsumerListener(
498 (sptr<IBufferConsumerListener> &)secondPreviewSurfaceListener_);
499 secondPreviewOutput_ = cameraManager_->CreatePreviewOutput(previewprofile2_, secondPreviewSurface_);
500 if (secondPreviewSurfaceListener_ == nullptr) {
501 MEDIA_ERR_LOG("Failed to create new SurfaceListener");
502 return result;
503 }
504 if (secondPreviewOutput_ == nullptr) {
505 MEDIA_ERR_LOG("Failed to create second previewOutput");
506 return result;
507 }
508 secondPreviewOutputCallback_ = std::make_shared<TestPreviewOutputCallback>(testName_);
509 ((sptr<PreviewOutput> &)secondPreviewOutput_)->SetCallback(secondPreviewOutputCallback_);
510 }
511 result = CAMERA_OK;
512 return result;
513 }
514
InitPhotoOutput()515 int32_t CameraCaptureVideo::InitPhotoOutput()
516 {
517 int32_t result = -1;
518 Size photosize_;
519 if (cameraManager_ == nullptr) {
520 MEDIA_ERR_LOG("cameraManager_ is null");
521 return result;
522 }
523
524 if (photoOutput_ == nullptr) {
525 photoSurface_ = Surface::CreateSurfaceAsConsumer();
526 if (photoSurface_ == nullptr) {
527 MEDIA_ERR_LOG("photoSurface_ is null");
528 return result;
529 }
530 photosize_.width = photoWidth_;
531 photosize_.height = photoHeight_;
532 Profile photoprofile_ = Profile(static_cast<CameraFormat>(photoFormat_), photosize_);
533 photoSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_, SurfaceType::PHOTO, fd_, photoSurface_);
534 if (photoSurfaceListener_ == nullptr) {
535 MEDIA_ERR_LOG("Failed to create new SurfaceListener");
536 return result;
537 }
538 photoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)photoSurfaceListener_);
539 photoOutput_ = cameraManager_->CreatePhotoOutput(photoprofile_, photoSurface_);
540 if (photoOutput_ == nullptr) {
541 MEDIA_ERR_LOG("Failed to create PhotoOutput");
542 return result;
543 }
544 photoOutputCallback_ = std::make_shared<TestPhotoOutputCallback>(testName_);
545 ((sptr<PhotoOutput> &)photoOutput_)->SetCallback(photoOutputCallback_);
546 }
547 result = CAMERA_OK;
548 return result;
549 }
550
InitVideoOutput()551 int32_t CameraCaptureVideo::InitVideoOutput()
552 {
553 int32_t result = -1;
554 Size videosize_;
555
556 if (cameraManager_ == nullptr) {
557 MEDIA_ERR_LOG("cameraManager_ is null");
558 return result;
559 }
560
561 if (videoOutput_ == nullptr) {
562 videoSurface_ = Surface::CreateSurfaceAsConsumer();
563 if (videoSurface_ == nullptr) {
564 MEDIA_ERR_LOG("videoSurface_ is null");
565 return result;
566 }
567 videosize_.width = videoWidth_;
568 videosize_.height = videoHeight_;
569 VideoProfile videoprofile_ =
570 VideoProfile(static_cast<CameraFormat>(videoFormat_), videosize_, videoframerates_);
571 videoSurfaceListener_ = new(std::nothrow) SurfaceListener(testName_, SurfaceType::VIDEO, fd_, videoSurface_);
572 if (videoSurfaceListener_ == nullptr) {
573 MEDIA_ERR_LOG("Failed to create new SurfaceListener");
574 return result;
575 }
576 videoSurface_->RegisterConsumerListener((sptr<IBufferConsumerListener> &)videoSurfaceListener_);
577 videoOutput_ = cameraManager_->CreateVideoOutput(videoprofile_, videoSurface_);
578 if (videoOutput_ == nullptr) {
579 MEDIA_ERR_LOG("Failed to create VideoOutput");
580 return result;
581 }
582 videoOutputCallback_ = std::make_shared<TestVideoOutputCallback>(testName_);
583 ((sptr<VideoOutput> &)videoOutput_)->SetCallback(videoOutputCallback_);
584 }
585 result = CAMERA_OK;
586 return result;
587 }
588
AddOutputbyState()589 int32_t CameraCaptureVideo::AddOutputbyState()
590 {
591 int32_t result = -1;
592
593 if (captureSession_ == nullptr) {
594 MEDIA_ERR_LOG("captureSession_ is null");
595 return result;
596 }
597 switch (currentState_) {
598 case State::PHOTO_CAPTURE:
599 result = InitPhotoOutput();
600 if (result == CAMERA_OK) {
601 result = captureSession_->AddOutput(photoOutput_);
602 }
603 break;
604 case State::VIDEO_RECORDING:
605 result = InitVideoOutput();
606 if (result == CAMERA_OK) {
607 result = captureSession_->AddOutput(videoOutput_);
608 }
609 break;
610 case State::DOUBLE_PREVIEW:
611 result = InitSecondPreviewOutput();
612 if (result == CAMERA_OK) {
613 result = captureSession_->AddOutput(secondPreviewOutput_);
614 }
615 break;
616 default:
617 break;
618 }
619 return result;
620 }
621
StartPreview()622 int32_t CameraCaptureVideo::StartPreview()
623 {
624 int32_t result = -1;
625
626 result = InitCameraManager();
627 if (result != CAMERA_OK) {
628 return result;
629 }
630 result = InitCameraInput();
631 if (result != CAMERA_OK) {
632 return result;
633 }
634 captureSession_ = cameraManager_->CreateCaptureSession();
635 if (captureSession_ == nullptr) {
636 return result;
637 }
638 captureSession_->BeginConfig();
639 result = captureSession_->AddInput(cameraInput_);
640 if (CAMERA_OK != result) {
641 return result;
642 }
643 result = AddOutputbyState();
644 if (result != CAMERA_OK) {
645 return result;
646 }
647 result = InitPreviewOutput();
648 if (result != CAMERA_OK) {
649 return result;
650 }
651 result = captureSession_->AddOutput(previewOutput_);
652 if (CAMERA_OK != result) {
653 return result;
654 }
655 result = captureSession_->CommitConfig();
656 if (CAMERA_OK != result) {
657 MEDIA_ERR_LOG("Failed to Commit config");
658 return result;
659 }
660 result = captureSession_->Start();
661 MEDIA_DEBUG_LOG("Session started, result: %{public}d", result);
662 if (CAMERA_OK != result) {
663 }
664 return result;
665 }
666
main(int32_t argc,char ** argv)667 int32_t main(int32_t argc, char **argv)
668 {
669 uint64_t tokenId;
670 const char *perms[0];
671 perms[0] = "ohos.permission.CAMERA";
672 NativeTokenInfoParams infoInstance = {
673 .dcapsNum = 0,
674 .permsNum = 1,
675 .aclsNum = 0,
676 .dcaps = NULL,
677 .perms = perms,
678 .acls = NULL,
679 .processName = "camera_capture_video",
680 .aplStr = "system_basic",
681 };
682 tokenId = GetAccessTokenId(&infoInstance);
683 SetSelfTokenID(tokenId);
684 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
685
686 int32_t result = 0; // Default result
687 std::shared_ptr<CameraCaptureVideo> testObj =
688 std::make_shared<CameraCaptureVideo>();
689 result = testObj->StartPreview();
690 DisplayMenu(testObj);
691 return 0;
692 }