1 /*
2  * Copyright (c) 2023-2023 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 <unistd.h>
17 #include <iostream>
18 #include <string>
19 #include "input/camera_input.h"
20 #include "input/camera_manager.h"
21 #include "output/camera_output_capability.h"
22 #include "portrait_session.h"
23 #include "camera_log.h"
24 #include "surface.h"
25 #include "test_common.h"
26 
27 #include "ipc_skeleton.h"
28 #include "access_token.h"
29 #include "hap_token_info.h"
30 #include "accesstoken_kit.h"
31 #include "nativetoken_kit.h"
32 #include "token_setproc.h"
33 using namespace std;
34 using namespace OHOS;
35 using namespace OHOS::CameraStandard;
36 
ConfigPhotoCaptureSetting()37 static std::shared_ptr<PhotoCaptureSetting> ConfigPhotoCaptureSetting()
38 {
39     std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings = std::make_shared<PhotoCaptureSetting>();
40     // QualityLevel
41     PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::QUALITY_LEVEL_HIGH;
42     photoCaptureSettings->SetQuality(quality);
43     return photoCaptureSettings;
44 }
45 
46 std::map<PortraitEffect, camera_portrait_effect_type_t> g_fwToMetaPortraitEffect_ = {
47     {OFF_EFFECT, OHOS_CAMERA_PORTRAIT_EFFECT_OFF},
48     {CIRCLES, OHOS_CAMERA_PORTRAIT_CIRCLES}
49 };
50 
51 std::map<FilterType, camera_filter_type_t> g_fwToMetaFilterType_ = {
52     {NONE, OHOS_CAMERA_FILTER_TYPE_OFF},
53     {CLASSIC, OHOS_CAMERA_FILTER_TYPE_CLASSIC},
54     {DAWN, OHOS_CAMERA_FILTER_TYPE_DAWN},
55     {PURE, OHOS_CAMERA_FILTER_TYPE_PURE},
56     {GREY, OHOS_CAMERA_FILTER_TYPE_GREY},
57     {NATURAL, OHOS_CAMERA_FILTER_TYPE_NATURAL},
58     {MORI, OHOS_CAMERA_FILTER_TYPE_MORI},
59     {FAIR, OHOS_CAMERA_FILTER_TYPE_FAIR},
60     {PINK, OHOS_CAMERA_FILTER_TYPE_PINK}
61 };
62 
63 std::map<camera_beauty_type_t, BeautyType> g_metaToFwBeautyType_ = {
64     {OHOS_CAMERA_BEAUTY_TYPE_AUTO, AUTO_TYPE},
65     {OHOS_CAMERA_BEAUTY_TYPE_SKIN_SMOOTH, SKIN_SMOOTH},
66     {OHOS_CAMERA_BEAUTY_TYPE_FACE_SLENDER, FACE_SLENDER},
67     {OHOS_CAMERA_BEAUTY_TYPE_SKIN_TONE, SKIN_TONE}
68 };
69 
70 std::map<BeautyType, camera_beauty_type_t> g_fwToMetaBeautyType_ = {
71     {AUTO_TYPE, OHOS_CAMERA_BEAUTY_TYPE_AUTO},
72     {SKIN_SMOOTH, OHOS_CAMERA_BEAUTY_TYPE_SKIN_SMOOTH},
73     {FACE_SLENDER, OHOS_CAMERA_BEAUTY_TYPE_FACE_SLENDER},
74     {SKIN_TONE, OHOS_CAMERA_BEAUTY_TYPE_SKIN_TONE}
75 };
76 
77 std::map<BeautyType, camera_device_metadata_tag_t> g_beautyTypeAbility_ = {
78     {SKIN_SMOOTH, OHOS_ABILITY_BEAUTY_SKIN_SMOOTH_VALUES},
79     {FACE_SLENDER, OHOS_ABILITY_BEAUTY_FACE_SLENDER_VALUES},
80     {SKIN_TONE, OHOS_ABILITY_BEAUTY_SKIN_TONE_VALUES}
81 };
82 
83 std::map<BeautyType, std::string> g_beautyTypeStr_ = {
84     {AUTO_TYPE, "OHOS_CAMERA_BEAUTY_TYPE_AUTO"},
85     {SKIN_SMOOTH, "OHOS_CAMERA_BEAUTY_TYPE_SKIN_SMOOTH"},
86     {FACE_SLENDER, "OHOS_CAMERA_BEAUTY_TYPE_FACE_SLENDER"},
87     {SKIN_TONE, "OHOS_CAMERA_BEAUTY_TYPE_SKIN_TONE"}
88 };
89 
90 std::map<BeautyType, std::string> g_beautyAbilityStr_ = {
91     {SKIN_SMOOTH, "OHOS_ABILITY_BEAUTY_SKIN_SMOOTH_VALUES"},
92     {FACE_SLENDER, "OHOS_ABILITY_BEAUTY_FACE_SLENDER_VALUES"},
93     {SKIN_TONE, "OHOS_ABILITY_BEAUTY_SKIN_TONE_VALUES"}
94 };
95 
96 std::map<FilterType, std::string> g_filterTypeStr_ = {
97     {NONE, "OHOS_CAMERA_FILTER_TYPE_OFF"},
98     {CLASSIC, "OHOS_CAMERA_FILTER_TYPE_CLASSIC"},
99     {DAWN, "OHOS_CAMERA_FILTER_TYPE_DAWN"},
100     {PURE, "OHOS_CAMERA_FILTER_TYPE_PURE"},
101     {GREY, "OHOS_CAMERA_FILTER_TYPE_GREY"},
102     {NATURAL, "OHOS_CAMERA_FILTER_TYPE_NATURAL"},
103     {MORI, "OHOS_CAMERA_FILTER_TYPE_MORI"},
104     {FAIR, "OHOS_CAMERA_FILTER_TYPE_FAIR"},
105     {PINK, "OHOS_CAMERA_FILTER_TYPE_PINK"}
106 };
107 
108 std::map<PortraitEffect, std::string> g_portraitEffectStr_ = {
109     {OFF_EFFECT, "OHOS_CAMERA_PORTRAIT_EFFECT_OFF"},
110     {CIRCLES, "OHOS_CAMERA_PORTRAIT_CIRCLES"}
111 };
112 
113 std::map<uint32_t, std::string> g_abilityIdStr_ = {
114     {536870912, "OHOS_ABILITY_SCENE_FILTER_TYPES"},
115     {536870914, "OHOS_ABILITY_SCENE_PORTRAIT_EFFECT_TYPES"},
116     {536870916, "OHOS_ABILITY_SCENE_BEAUTY_TYPES"}
117 };
118 
PrintSupportBeautyTypes(vector<BeautyType> supportedBeautyTypes)119 void PrintSupportBeautyTypes(vector<BeautyType> supportedBeautyTypes)
120 {
121     MEDIA_INFO_LOG("Supported BeautyType size = %{public}zu", supportedBeautyTypes.size());
122     cout << g_abilityIdStr_[OHOS_ABILITY_SCENE_BEAUTY_TYPES] << " : size = " << supportedBeautyTypes.size() <<endl;
123     uint32_t index = 0;
124     for (BeautyType type : supportedBeautyTypes) {
125         for (auto itrType = g_fwToMetaBeautyType_.cbegin(); itrType != g_fwToMetaBeautyType_.cend(); itrType++) {
126             if (type == itrType->first) {
127                 break;
128             }
129         }
130         cout << "[" << index++ << "]" << "type = " << g_beautyTypeStr_[type] << ", ";
131         MEDIA_INFO_LOG("Supported BeautyType type = %{public}s", g_beautyTypeStr_[type].c_str());
132     }
133     cout << endl;
134     cout << "------------------------------------------------------------------------------------------------"<<endl;
135     return;
136 }
137 
PrintSupportFilterTypes(vector<FilterType> supportedFilterTypes)138 void PrintSupportFilterTypes(vector<FilterType> supportedFilterTypes)
139 {
140     MEDIA_INFO_LOG("Supported FilterTypes size = %{public}zu", supportedFilterTypes.size());
141     cout << g_abilityIdStr_[OHOS_ABILITY_SCENE_FILTER_TYPES] <<" : size = " << supportedFilterTypes.size() << endl;
142     uint32_t index = 0;
143     for (FilterType type : supportedFilterTypes) {
144         for (auto itrType = g_fwToMetaFilterType_.cbegin(); itrType != g_fwToMetaFilterType_.cend(); itrType++) {
145             if (type == itrType->first) {
146                 break;
147             }
148         }
149         cout << " [" << index++ << "]" << "type = " << g_filterTypeStr_[type] << ", ";
150         MEDIA_INFO_LOG("Supported FilterType type = %{public}s", g_filterTypeStr_[type].c_str());
151     }
152     cout << endl;
153     cout << "------------------------------------------------------------------------------------------------"<<endl;
154     return;
155 }
156 
PrintSupportPortraitEffects(vector<PortraitEffect> supportedPortraitEffects)157 void PrintSupportPortraitEffects(vector<PortraitEffect> supportedPortraitEffects)
158 {
159     MEDIA_INFO_LOG("Supported PortraitEffects size = %{public}zu", supportedPortraitEffects.size());
160     cout << g_abilityIdStr_[OHOS_ABILITY_SCENE_PORTRAIT_EFFECT_TYPES] <<" : size = "
161          << supportedPortraitEffects.size() <<endl;
162     uint32_t index = 0;
163     for (PortraitEffect type : supportedPortraitEffects) {
164         for (auto itrType = g_fwToMetaPortraitEffect_.cbegin(); itrType != g_fwToMetaPortraitEffect_.cend();
165              itrType++) {
166             if (type == itrType->first) {
167                 break;
168             }
169         }
170         cout << "[" << index++ << "]" << "effect = " << g_portraitEffectStr_[type] << ", ";
171         MEDIA_INFO_LOG("Supported PortraitEffect effect = %{public}s", g_portraitEffectStr_[type].c_str());
172     }
173     cout << endl;
174     cout << "------------------------------------------------------------------------------------------------"<<endl;
175     return;
176 }
177 
PrintSupportBeautyRange(BeautyType beauty,vector<int32_t> range)178 void PrintSupportBeautyRange(BeautyType beauty, vector<int32_t> range)
179 {
180     MEDIA_INFO_LOG("%{public}s size = %{public}zu", g_beautyAbilityStr_[beauty].c_str(), range.size());
181     cout << " " << g_beautyAbilityStr_[beauty] <<" size = " << range.size();
182     for (auto val : range) {
183         cout << " val = " << val << ", ";
184         MEDIA_INFO_LOG("val = %{public}d", val);
185     }
186     cout << endl;
187     return;
188 }
189 
GetSupportedPortraitEffectsStub()190 std::vector<PortraitEffect> GetSupportedPortraitEffectsStub()
191 {
192     cout<<"=================GetSupportedPortraitEffectsStub call================="<<endl;
193     std::vector<PortraitEffect> supportedPortraitEffects = {OFF_EFFECT, CIRCLES};
194     return supportedPortraitEffects;
195 }
196 
GetSupportedBeautyTypesStub()197 std::vector<BeautyType> GetSupportedBeautyTypesStub()
198 {
199     cout<<"=================GetSupportedBeautyTypesStub call================="<<endl;
200     std::vector<BeautyType> supportedBeautyTypes = {AUTO_TYPE, SKIN_SMOOTH, FACE_SLENDER, SKIN_TONE};
201     return supportedBeautyTypes;
202 }
203 
GetSupportedBeautyRangeStub(void * obj,BeautyType beautyType)204 std::vector<int32_t> GetSupportedBeautyRangeStub(void* obj, BeautyType beautyType)
205 {
206     cout<<"=================GetSupportedBeautyRangeStub call================="<<endl;
207     vector<int32_t> beautyRange;
208     switch (beautyType) {
209         case BeautyType::AUTO_TYPE : {
210             std::vector<int32_t> values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
211             beautyRange = values;
212             break;
213         }
214         case BeautyType::SKIN_TONE : {
215             std::vector<int32_t> values = {0xFFFFFFFF, 0xBF986C, 0xE9BB97, 0xEDCDA3, 0xF7D7B3, 0xFEE6CF};
216             beautyRange = values;
217             break;
218         }
219         case BeautyType::SKIN_SMOOTH : {
220             std::vector<int32_t> values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
221             beautyRange = values;
222             break;
223         }
224         case BeautyType::FACE_SLENDER : {
225             std::vector<int32_t> values = {0, 1, 2, 3, 4, 5};
226             beautyRange = values;
227             break;
228         }
229         default : {
230             std::vector<int32_t> values = {};
231             beautyRange = values;
232         }
233     }
234     cout<<"BeautyType = " << beautyType << " BeautyType size()" << beautyRange.size() <<endl;
235     return beautyRange;
236 }
237 
GetExposureRangeStub(std::vector<uint32_t> & exposureRange)238 int32_t GetExposureRangeStub(std::vector<uint32_t> &exposureRange)
239 {
240     cout<<"=================GetSupportedBeautyRangeStub call================="<<endl;
241     exposureRange.clear();
242     std::vector<uint32_t> exposureRangeStub = {250, 500, 1000, 2000, 4000, 8000,
243         12000, 16000, 20000, 24000, 28000, 32000};
244     for (size_t i = 0; i < exposureRangeStub.size(); i++) {
245         exposureRange.push_back(exposureRangeStub.at(i));
246     }
247     cout << " exposureRangeStub size()" << exposureRangeStub.size() <<endl;
248     return 0;
249 }
250 
GetSupportedFiltersStub()251 std::vector<FilterType> GetSupportedFiltersStub()
252 {
253     cout<<"=================GetSupportedFiltersStub call================="<<endl;
254     std::vector<FilterType> supportedFilterTypes = { NONE, CLASSIC, DAWN, PURE, GREY, NATURAL, MORI, FAIR, PINK };
255     return supportedFilterTypes;
256 }
257 
main(int argc,char ** argv)258 int main(int argc, char **argv)
259 {
260     cout<<"-----------------version:20230822-----------------"<<endl;
261     const int32_t previewWidthIndex = 1;
262     const int32_t previewHeightIndex = 2;
263     const int32_t photoWidthIndex = 3;
264     const int32_t photoHeightIndex = 4;
265     const int32_t devicePosionIndex = 5;
266     const int32_t validArgCount = 6;
267     const int32_t gapAfterCapture = 3; // 1 second
268     const int32_t previewCaptureGap = 5; // 5 seconds
269     const char* testName = "camera_capture_mode";
270     int32_t ret = -1;
271     int32_t previewFd = -1;
272     int32_t photoFd = -1;
273     int32_t previewFormat = CAMERA_FORMAT_YUV_420_SP;
274     int32_t previewWidth = 1280;
275     int32_t previewHeight = 960;
276     int32_t photoFormat = CAMERA_FORMAT_JPEG;
277     int32_t photoWidth = 1280;
278     int32_t photoHeight = 960;
279     int32_t photoCaptureCount = 1;
280     int32_t devicePosion = 0;
281     bool isResolutionConfigured = false;
282     Size previewsize;
283     Size photosize;
284 
285     // Update sizes if enough number of valid arguments are passed
286     if (argc == validArgCount) {
287         // Validate arguments
288         for (int counter = 1; counter < argc; counter++) {
289             if (!TestUtils::IsNumber(argv[counter])) {
290                 cout << "Invalid argument: " << argv[counter] << endl;
291                 cout << "Retry by giving proper sizes" << endl;
292                 return 0;
293             }
294         }
295         previewWidth = atoi(argv[previewWidthIndex]);
296         previewHeight = atoi(argv[previewHeightIndex]);
297         photoWidth = atoi(argv[photoWidthIndex]);
298         photoHeight = atoi(argv[photoHeightIndex]);
299         devicePosion = atoi(argv[devicePosionIndex]);
300         isResolutionConfigured = true;
301     } else if (argc != 1) {
302         cout << "Pass " << (validArgCount - 1) << "arguments" << endl;
303         cout << "PreviewHeight, PreviewWidth, PhotoWidth, PhotoHeight"
304             << endl;
305         return 0;
306     }
307 
308     uint64_t tokenId;
309     const char *perms[0];
310     perms[0] = "ohos.permission.CAMERA";
311     NativeTokenInfoParams infoInstance = {
312         .dcapsNum = 0,
313         .permsNum = 1,
314         .aclsNum = 0,
315         .dcaps = NULL,
316         .perms = perms,
317         .acls = NULL,
318         .processName = "camera_capture_mode",
319         .aplStr = "system_basic",
320     };
321     tokenId = GetAccessTokenId(&infoInstance);
322     SetSelfTokenID(tokenId);
323     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
324 
325     sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
326     MEDIA_INFO_LOG("Setting callback to listen camera status and flash status");
327     camManagerObj->SetCallback(std::make_shared<TestCameraMngerCallback>(testName));
328     std::vector<sptr<CameraDevice>> cameraObjList = camManagerObj->GetSupportedCameras();
329     if (cameraObjList.size() == 0) {
330         return 0;
331     }
332     sptr<CameraDevice> device = cameraObjList[0];
333     MEDIA_INFO_LOG("Camera ID count: %{public}zu", cameraObjList.size());
334     for (auto& it : cameraObjList) {
335         MEDIA_INFO_LOG("Camera ID: %{public}s", it->GetID().c_str());
336         MEDIA_INFO_LOG("Camera Posion: %{public}d", it->GetPosition());
337         if (devicePosion == 0 && it->GetPosition() == CameraPosition::CAMERA_POSITION_BACK) {
338             device = it;
339             break;
340         }
341         if (devicePosion == 1 && it->GetPosition() == CameraPosition::CAMERA_POSITION_FRONT) {
342             device = it;
343             break;
344         }
345     }
346     cout<<"Camera ID ="<<device->GetID()<<",camera Position = "<<device->GetPosition()<<endl;
347     sptr<CameraManager> modeManagerObj = CameraManager::GetInstance();
348     std::vector<SceneMode> supportedModes = modeManagerObj->GetSupportedModes(device);
349     std::string modes = "";
350     for (auto mode : supportedModes) {
351         modes += std::to_string(static_cast<uint32_t>(mode)) + " , ";
352     }
353     MEDIA_INFO_LOG("supportedModes : %{public}s", modes.c_str());
354     sptr<CaptureSession> captureSession = modeManagerObj->CreateCaptureSession(SceneMode::PORTRAIT);
355     sptr<PortraitSession> portraitSession = nullptr;
356     portraitSession = static_cast<PortraitSession *> (captureSession.GetRefPtr());
357     if (portraitSession == nullptr) {
358         return 0;
359     }
360     portraitSession->BeginConfig();
361     sptr<CaptureInput> captureInput = camManagerObj->CreateCameraInput(device);
362     if (captureInput == nullptr) {
363         return 0;
364     }
365 
366     sptr<CameraInput> cameraInput = (sptr<CameraInput> &)captureInput;
367     cameraInput->Open();
368     std::vector<Profile> photoProfiles = {};
369     std::vector<Profile> previewProfiles = {};
370     std::string abilityIds = "";
371     if (isResolutionConfigured) {
372         std::vector<CameraFormat> previewFormats;
373         std::vector<CameraFormat> photoFormats;
374         std::vector<Size> previewSizes;
375         std::vector<Size> photoSizes;
376         sptr<CameraOutputCapability> outputcapability =
377             modeManagerObj->GetSupportedOutputCapability(device, SceneMode::PORTRAIT);
378         previewProfiles = outputcapability->GetPreviewProfiles();
379         uint32_t profileIndex = 0;
380         for (auto i : previewProfiles) {
381             previewFormats.push_back(i.GetCameraFormat());
382             previewSizes.push_back(i.GetSize());
383 
384             abilityIds = "";
385             for (auto id : i.GetAbilityId()) {
386                 abilityIds += g_abilityIdStr_[id] + "("+std::to_string(id) + ") , ";
387             }
388             MEDIA_INFO_LOG("index(%{public}d), preview profile f(%{public}d), w(%{public}d), h(%{public}d) "
389                            "support ability: %{public}s", profileIndex++,
390                            i.GetCameraFormat(), i.GetSize().width, i.GetSize().height, abilityIds.c_str());
391         }
392 
393         photoProfiles =  outputcapability->GetPhotoProfiles();
394         profileIndex = 0;
395         for (auto i : photoProfiles) {
396             photoFormats.push_back(i.GetCameraFormat());
397             photoSizes.push_back(i.GetSize());
398             abilityIds = "";
399             for (auto id : i.GetAbilityId()) {
400                 abilityIds += g_abilityIdStr_[id] + "("+std::to_string(id) + ") , ";
401             }
402             MEDIA_INFO_LOG("index %{public}d, photo support format : %{public}d, width: %{public}d, height: %{public}d"
403                            "support ability: %{public}s", profileIndex++,
404                            i.GetCameraFormat(), i.GetSize().width, i.GetSize().height, abilityIds.c_str());
405         }
406     }
407 
408     MEDIA_INFO_LOG("photoCaptureCount: %{public}d", photoCaptureCount);
409 
410     cameraInput->SetErrorCallback(std::make_shared<TestDeviceCallback>(testName));
411     ret = portraitSession->AddInput(captureInput);
412     if (ret != 0) {
413         return 0;
414     }
415 
416     sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
417     if (photoSurface == nullptr) {
418         return 0;
419     }
420     photosize.width = photoWidth;
421     photosize.height = photoHeight;
422     Profile photoprofile;
423     for (auto it : photoProfiles) {
424         if (it.GetSize().width == photosize.width && it.GetSize().height == photosize.height
425             && it.GetCameraFormat() == static_cast<CameraFormat>(photoFormat)) {
426                 photoprofile = it;
427         }
428     }
429     abilityIds = "";
430     for (auto id : photoprofile.GetAbilityId()) {
431         abilityIds += g_abilityIdStr_[id] + "("+std::to_string(id) + ") , ";
432     }
433     MEDIA_INFO_LOG("photo support format : %{public}d, width: %{public}d, height: %{public}d"
434                    "support ability: %{public}s",
435                    photoFormat, photoWidth, photoHeight, abilityIds.c_str());
436     cout<< "photoFormat: " << photoFormat << " photoWidth: "<< photoWidth
437         << " photoHeight: " << photoHeight << " support ability: " << abilityIds.c_str() << endl;
438 
439     sptr<SurfaceListener> captureListener = new(std::nothrow) SurfaceListener("Photo", SurfaceType::PHOTO,
440                                                                               photoFd, photoSurface);
441     if (captureListener == nullptr) {
442         return 0;
443     }
444     photoSurface->RegisterConsumerListener((sptr<IBufferConsumerListener> &)captureListener);
445     sptr<IBufferProducer> bp = photoSurface->GetProducer();
446     sptr<CaptureOutput> photoOutput = camManagerObj->CreatePhotoOutput(photoprofile, bp);
447     if (photoOutput == nullptr) {
448         return 0;
449     }
450 
451     MEDIA_INFO_LOG("Setting photo callback");
452     ((sptr<PhotoOutput> &)photoOutput)->SetCallback(std::make_shared<TestPhotoOutputCallback>(testName));
453     ret = portraitSession->AddOutput(photoOutput);
454     if (ret != 0) {
455         return 0;
456     }
457 
458     sptr<CaptureOutput> metaOutput = camManagerObj->CreateMetadataOutput();
459     MEDIA_INFO_LOG("Setting Meta callback");
460     ((sptr<MetadataOutput> &)metaOutput)->SetCallback(std::make_shared<TestMetadataOutputObjectCallback>(testName));
461 
462     ret = portraitSession->AddOutput(metaOutput);
463     if (ret != 0) {
464         return 0;
465     }
466 
467     sptr<IConsumerSurface> previewSurface = IConsumerSurface::Create();
468     if (previewSurface == nullptr) {
469         return 0;
470     }
471     previewsize.width = previewWidth;
472     previewsize.height = previewHeight;
473     Profile previewprofile;
474     for (auto it : previewProfiles) {
475         if (it.GetSize().width == previewsize.width && it.GetSize().height == previewsize.height
476             && it.GetCameraFormat() == static_cast<CameraFormat>(previewFormat)) {
477                 previewprofile = it;
478             }
479     }
480     abilityIds = "";
481     for (auto id : previewprofile.GetAbilityId()) {
482         abilityIds += g_abilityIdStr_[id] + "("+std::to_string(id) + ") , ";
483     }
484     MEDIA_INFO_LOG("previewFormat: %{public}d, previewWidth: %{public}d, previewHeight: %{public}d"
485                    "support ability: %{public}s",
486                    previewFormat, previewWidth, previewHeight, abilityIds.c_str());
487 
488     cout<< "previewFormat: " << previewFormat << " previewWidth: "<< previewWidth
489         << " previewHeight: " << previewHeight << " support ability: " << abilityIds.c_str() <<endl;
490 
491     cout << "------------------------------------------------------------------------------------------------"<<endl;
492     sptr<SurfaceListener> listener = new(std::nothrow) SurfaceListener("Preview", SurfaceType::PREVIEW,
493                                                                        previewFd, previewSurface);
494     if (listener == nullptr) {
495         return 0;
496     }
497     previewSurface->RegisterConsumerListener((sptr<IBufferConsumerListener> &)listener);
498     sptr<IBufferProducer> previewProducer = previewSurface->GetProducer();
499     sptr<Surface> previewProducerSurface = Surface::CreateSurfaceAsProducer(previewProducer);
500     sptr<CaptureOutput> previewOutput = camManagerObj->CreatePreviewOutput(previewprofile, previewProducerSurface);
501     if (previewOutput == nullptr) {
502         return 0;
503     }
504 
505     MEDIA_INFO_LOG("Setting preview callback");
506     ((sptr<PreviewOutput> &)previewOutput)->SetCallback(std::make_shared<TestPreviewOutputCallback>(testName));
507     ret = portraitSession->AddOutput(previewOutput);
508     if (ret != 0) {
509         return 0;
510     }
511 
512     ret = portraitSession->CommitConfig();
513     if (ret != 0) {
514         return 0;
515     }
516 
517     MEDIA_INFO_LOG("Preview started");
518     ret = portraitSession->Start();
519     if (ret != 0) {
520         return 0;
521     }
522 
523     std::vector<float> vecZoomRatioList;
524     portraitSession->GetZoomRatioRange(vecZoomRatioList);
525     cout<< "getMode:" << portraitSession->GetFeaturesMode().Dump() << ",minZoom:" << vecZoomRatioList[0]
526         << ", maxZoom:" << vecZoomRatioList[1]<<endl;
527     std::vector<BeautyType> supportedBeautyType = portraitSession->GetSupportedBeautyTypes();
528     PrintSupportBeautyTypes(supportedBeautyType);
529 
530     std::vector<int32_t> faceSlenderRange = portraitSession->GetSupportedBeautyRange(BeautyType::FACE_SLENDER);
531     PrintSupportBeautyRange(BeautyType::FACE_SLENDER, faceSlenderRange);
532 
533     std::vector<int32_t> skinSmoothRange = portraitSession->GetSupportedBeautyRange(BeautyType::SKIN_SMOOTH);
534     PrintSupportBeautyRange(BeautyType::SKIN_SMOOTH, skinSmoothRange);
535 
536     std::vector<int32_t> skinToneRange = portraitSession->GetSupportedBeautyRange(BeautyType::SKIN_TONE);
537     PrintSupportBeautyRange(BeautyType::SKIN_TONE, skinToneRange);
538 
539     vector<FilterType> supportedFilterTypes = portraitSession->GetSupportedFilters();
540     PrintSupportFilterTypes(supportedFilterTypes);
541 
542     vector<PortraitEffect> supportedPortraitEffects = portraitSession->GetSupportedPortraitEffects();
543     PrintSupportPortraitEffects(supportedPortraitEffects);
544 
545     sleep(previewCaptureGap);
546 
547     uint32_t photoCnt = 1;
548 
549     MEDIA_INFO_LOG("Normal Capture started");
550     for (int i = 1; i <= photoCaptureCount; i++) {
551         MEDIA_INFO_LOG("Photo capture %{public}d started", i);
552         ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
553         if (ret != 0) {
554             MEDIA_INFO_LOG("Photo capture err ret=%{public}d", ret);
555             return 0;
556         }
557         sleep(gapAfterCapture);
558     }
559 
560     cout<< "Filter Capture started" <<endl;
561     for (auto filter : supportedFilterTypes) {
562         MEDIA_INFO_LOG("Set Filter: %{public}s", g_filterTypeStr_[filter].c_str());
563         portraitSession->LockForControl();
564         portraitSession->SetFilter(filter);
565         portraitSession->UnlockForControl();
566         sleep(gapAfterCapture);
567         ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
568         if (ret != 0) {
569             return 0;
570         }
571         MEDIA_INFO_LOG("Photo capture cnt [%{public}d] filter : %{public}s",
572                        photoCnt++, g_filterTypeStr_[filter].c_str());
573         sleep(gapAfterCapture);
574     }
575     cout<< "Filter Capture started" <<endl;
576 
577     cout<< "BeautyType::SKIN_TONE Capture started" <<endl;
578     for (auto beautyVal : skinToneRange) {
579         MEDIA_INFO_LOG("Set Beauty: %{public}s beautyVal= %{public}d",
580                        g_beautyTypeStr_[BeautyType::SKIN_TONE].c_str(), beautyVal);
581         portraitSession->LockForControl();
582         portraitSession->SetBeauty(BeautyType::SKIN_TONE, beautyVal);
583         portraitSession->UnlockForControl();
584         ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
585         if (ret != 0) {
586             return 0;
587         }
588         sleep(gapAfterCapture);
589     }
590     cout<< "BeautyType::SKIN_TONE Capture end" <<endl;
591 
592     cout<< "BeautyType::FACE_SLENDER Capture started" <<endl;
593     for (auto beautyVal : skinToneRange) {
594         MEDIA_INFO_LOG("Set Beauty: %{public}s beautyVal= %{public}d",
595                        g_beautyTypeStr_[BeautyType::FACE_SLENDER].c_str(), beautyVal);
596         portraitSession->LockForControl();
597         portraitSession->SetBeauty(BeautyType::FACE_SLENDER, beautyVal);
598         portraitSession->UnlockForControl();
599         ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
600         if (ret != 0) {
601             return 0;
602         }
603         sleep(gapAfterCapture);
604     }
605     cout<< "BeautyType::FACE_SLENDER Capture end" <<endl;
606 
607     cout<< "BeautyType::SKIN_SMOOTH Capture started" <<endl;
608     for (auto beautyVal : skinToneRange) {
609         MEDIA_INFO_LOG("Set Beauty: %{public}s beautyVal= %{public}d",
610                        g_beautyTypeStr_[BeautyType::SKIN_SMOOTH].c_str(), beautyVal);
611         portraitSession->LockForControl();
612         portraitSession->SetBeauty(BeautyType::SKIN_SMOOTH, beautyVal);
613         portraitSession->UnlockForControl();
614         ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
615         if (ret != 0) {
616             return 0;
617         }
618         sleep(gapAfterCapture);
619     }
620     cout<< "BeautyType::SKIN_SMOOTH Capture end" <<endl;
621 
622     cout<< "PortraitEffect Capture started" <<endl;
623     for (auto effect : supportedPortraitEffects) {
624         MEDIA_INFO_LOG("Set PortraitEffect: %{public}s", g_portraitEffectStr_[effect].c_str());
625         portraitSession->LockForControl();
626         portraitSession->SetPortraitEffect(effect);
627         portraitSession->UnlockForControl();
628         ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
629         if (ret != 0) {
630             return 0;
631         }
632         MEDIA_INFO_LOG("Photo capture [%{public}d] filter : %{public}s",
633                        photoCnt++, g_portraitEffectStr_[effect].c_str());
634         sleep(gapAfterCapture);
635     }
636     cout<< "PortraitEffect Capture end" <<endl;
637     MEDIA_INFO_LOG("Closing the session");
638     ((sptr<PreviewOutput> &)previewOutput)->Stop();
639     portraitSession->Stop();
640     portraitSession->Release();
641     cameraInput->Release();
642     camManagerObj->SetCallback(nullptr);
643 
644     MEDIA_INFO_LOG("Camera new sample end.");
645     return 0;
646 }
647