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