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 <unistd.h>
17 #include "input/camera_input.h"
18 #include "input/camera_manager.h"
19 #include "output/camera_output_capability.h"
20
21 #include "camera_log.h"
22 #include "surface.h"
23 #include "test_common.h"
24
25 #include "ipc_skeleton.h"
26 #include "access_token.h"
27 #include "hap_token_info.h"
28 #include "accesstoken_kit.h"
29 #include "nativetoken_kit.h"
30 #include "token_setproc.h"
31
32 using namespace std;
33 using namespace OHOS;
34 using namespace OHOS::CameraStandard;
35
ConfigPhotoCaptureSetting()36 static std::shared_ptr<PhotoCaptureSetting> ConfigPhotoCaptureSetting()
37 {
38 std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings = std::make_shared<PhotoCaptureSetting>();
39 // QualityLevel
40 PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::QUALITY_LEVEL_HIGH;
41 photoCaptureSettings->SetQuality(quality);
42 return photoCaptureSettings;
43 }
44
main(int argc,char ** argv)45 int main(int argc, char **argv)
46 {
47 const int32_t previewFormatIndex = 1;
48 const int32_t previewWidthIndex = 2;
49 const int32_t previewHeightIndex = 3;
50 const int32_t photoFormatIndex = 4;
51 const int32_t photoWidthIndex = 5;
52 const int32_t photoHeightIndex = 6;
53 const int32_t photoCaptureCountIndex = 7;
54 const int32_t validArgCount = 8;
55 const int32_t gapAfterCapture = 1; // 1 second
56 const int32_t previewCaptureGap = 5; // 5 seconds
57 const char* testName = "camera_capture";
58 int32_t ret = -1;
59 int32_t previewFd = -1;
60 int32_t photoFd = -1;
61 int32_t previewFormat = CAMERA_FORMAT_YUV_420_SP;
62 int32_t previewWidth = 640;
63 int32_t previewHeight = 480;
64 int32_t photoFormat = CAMERA_FORMAT_JPEG;
65 int32_t photoWidth = 1280;
66 int32_t photoHeight = 960;
67 int32_t photoCaptureCount = 1;
68 bool isResolutionConfigured = false;
69 Size previewsize;
70 Size photosize;
71
72 MEDIA_DEBUG_LOG("Camera new(std::nothrow) sample begin.");
73 // Update sizes if enough number of valid arguments are passed
74 if (argc == validArgCount) {
75 // Validate arguments
76 for (int counter = 1; counter < argc; counter++) {
77 if (!TestUtils::IsNumber(argv[counter])) {
78 cout << "Invalid argument: " << argv[counter] << endl;
79 cout << "Retry by giving proper sizes" << endl;
80 return 0;
81 }
82 }
83 previewFormat = atoi(argv[previewFormatIndex]);
84 previewWidth = atoi(argv[previewWidthIndex]);
85 previewHeight = atoi(argv[previewHeightIndex]);
86 photoFormat = atoi(argv[photoFormatIndex]);
87 photoWidth = atoi(argv[photoWidthIndex]);
88 photoHeight = atoi(argv[photoHeightIndex]);
89 photoCaptureCount = atoi(argv[photoCaptureCountIndex]);
90 isResolutionConfigured = true;
91 } else if (argc != 1) {
92 cout << "Pass " << (validArgCount - 1) << "arguments" << endl;
93 cout << "PreviewFormat, PreviewHeight, PreviewWidth, PhotoFormat, PhotoWidth, PhotoHeight, CaptureCount"
94 << endl;
95 return 0;
96 }
97
98 uint64_t tokenId;
99 const char *perms[0];
100 perms[0] = "ohos.permission.CAMERA";
101 NativeTokenInfoParams infoInstance = {
102 .dcapsNum = 0,
103 .permsNum = 1,
104 .aclsNum = 0,
105 .dcaps = NULL,
106 .perms = perms,
107 .acls = NULL,
108 .processName = "camera_capture",
109 .aplStr = "system_basic",
110 };
111 tokenId = GetAccessTokenId(&infoInstance);
112 SetSelfTokenID(tokenId);
113 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
114
115 sptr<CameraManager> camManagerObj = CameraManager::GetInstance();
116 MEDIA_DEBUG_LOG("Setting callback to listen camera status and flash status");
117 camManagerObj->RegisterCameraStatusCallback(std::make_shared<TestCameraMngerCallback>(testName));
118 std::vector<sptr<CameraDevice>> cameraObjList = camManagerObj->GetSupportedCameras();
119 CHECK_ERROR_RETURN_RET(cameraObjList.size() == 0, 0);
120
121 MEDIA_DEBUG_LOG("Camera ID count: %{public}zu", cameraObjList.size());
122 for (auto& it : cameraObjList) {
123 MEDIA_DEBUG_LOG("Camera ID: %{public}s", it->GetID().c_str());
124 }
125
126 sptr<CaptureSession> captureSession = camManagerObj->CreateCaptureSession();
127 CHECK_ERROR_RETURN_RET(captureSession == nullptr, 0);
128
129 captureSession->BeginConfig();
130 sptr<CaptureInput> captureInput = camManagerObj->CreateCameraInput(cameraObjList[0]);
131 CHECK_ERROR_RETURN_RET(captureInput == nullptr, 0);
132
133 sptr<CameraInput> cameraInput = (sptr<CameraInput> &)captureInput;
134 cameraInput->Open();
135 if (!isResolutionConfigured) {
136 std::vector<CameraFormat> previewFormats;
137 std::vector<CameraFormat> photoFormats;
138 std::vector<Size> previewSizes;
139 std::vector<Size> photoSizes;
140 sptr<CameraOutputCapability> outputcapability = camManagerObj->GetSupportedOutputCapability(cameraObjList[0]);
141 std::vector<Profile> previewProfiles = outputcapability->GetPreviewProfiles();
142 for (auto i : previewProfiles) {
143 previewFormats.push_back(i.GetCameraFormat());
144 previewSizes.push_back(i.GetSize());
145 }
146 MEDIA_DEBUG_LOG("Supported preview formats:");
147 for (auto &formatPreview : previewFormats) {
148 MEDIA_DEBUG_LOG("format : %{public}d", formatPreview);
149 }
150 if (std::find(previewFormats.begin(), previewFormats.end(), CAMERA_FORMAT_YUV_420_SP)
151 != previewFormats.end()) {
152 previewFormat = CAMERA_FORMAT_YUV_420_SP;
153 MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is present in supported preview formats");
154 } else if (!previewFormats.empty()) {
155 previewFormat = previewFormats[0];
156 MEDIA_DEBUG_LOG("CAMERA_FORMAT_YUV_420_SP format is not present in supported preview formats");
157 }
158 std::vector<Profile> photoProfiles = outputcapability->GetPhotoProfiles();
159 for (auto i : photoProfiles) {
160 photoFormats.push_back(i.GetCameraFormat());
161 photoSizes.push_back(i.GetSize());
162 }
163 MEDIA_DEBUG_LOG("Supported photo formats:");
164 for (auto &formatPhoto : photoFormats) {
165 MEDIA_DEBUG_LOG("format : %{public}d", formatPhoto);
166 }
167 if (!photoFormats.empty()) {
168 photoFormat = photoFormats[0];
169 }
170 MEDIA_DEBUG_LOG("Supported sizes for preview:");
171 for (auto &size : previewSizes) {
172 MEDIA_DEBUG_LOG("width: %{public}d, height: %{public}d", size.width, size.height);
173 }
174 MEDIA_DEBUG_LOG("Supported sizes for photo:");
175 for (auto &size : photoSizes) {
176 MEDIA_DEBUG_LOG("width: %{public}d, height: %{public}d", size.width, size.height);
177 }
178 if (!previewSizes.empty()) {
179 previewWidth = previewSizes[0].width;
180 previewHeight = previewSizes[0].height;
181 }
182 if (!photoSizes.empty()) {
183 photoWidth = photoSizes[0].width;
184 photoHeight = photoSizes[0].height;
185 }
186 }
187
188 MEDIA_DEBUG_LOG("previewFormat: %{public}d, previewWidth: %{public}d, previewHeight: %{public}d",
189 previewFormat, previewWidth, previewHeight);
190 MEDIA_DEBUG_LOG("photoFormat: %{public}d, photoWidth: %{public}d, photoHeight: %{public}d",
191 photoFormat, photoWidth, photoHeight);
192 MEDIA_DEBUG_LOG("photoCaptureCount: %{public}d", photoCaptureCount);
193
194 cameraInput->SetErrorCallback(std::make_shared<TestDeviceCallback>(testName));
195 ret = captureSession->AddInput(captureInput);
196 CHECK_ERROR_RETURN_RET(ret != 0, 0);
197
198 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
199 CHECK_ERROR_RETURN_RET(photoSurface == nullptr, 0);
200 photosize.width = photoWidth;
201 photosize.height = photoHeight;
202 Profile photoprofile = Profile(static_cast<CameraFormat>(photoFormat), photosize);
203 sptr<SurfaceListener> captureListener = new(std::nothrow) SurfaceListener("Photo", SurfaceType::PHOTO,
204 photoFd, photoSurface);
205 CHECK_ERROR_RETURN_RET(captureListener == nullptr, 0);
206 photoSurface->RegisterConsumerListener((sptr<IBufferConsumerListener> &)captureListener);
207 sptr<IBufferProducer> bp = photoSurface->GetProducer();
208 sptr<CaptureOutput> photoOutput = camManagerObj->CreatePhotoOutput(photoprofile, bp);
209 CHECK_ERROR_RETURN_RET(photoOutput == nullptr, 0);
210
211 MEDIA_DEBUG_LOG("Setting photo callback");
212 ((sptr<PhotoOutput> &)photoOutput)->SetCallback(std::make_shared<TestPhotoOutputCallback>(testName));
213 ret = captureSession->AddOutput(photoOutput);
214 CHECK_ERROR_RETURN_RET(ret != 0, 0);
215
216 sptr<IConsumerSurface> previewSurface = IConsumerSurface::Create();
217 CHECK_ERROR_RETURN_RET(previewSurface == nullptr, 0);
218 previewsize.width = previewWidth;
219 previewsize.height = previewHeight;
220 Profile previewprofile = Profile(static_cast<CameraFormat>(previewFormat), previewsize);
221 sptr<SurfaceListener> listener = new(std::nothrow) SurfaceListener("Preview", SurfaceType::PREVIEW,
222 previewFd, previewSurface);
223 CHECK_ERROR_RETURN_RET(listener == nullptr, 0);
224 previewSurface->RegisterConsumerListener((sptr<IBufferConsumerListener> &)listener);
225 sptr<IBufferProducer> previewProducer = previewSurface->GetProducer();
226 sptr<Surface> previewProducerSurface = Surface::CreateSurfaceAsProducer(previewProducer);
227 sptr<CaptureOutput> previewOutput = camManagerObj->CreatePreviewOutput(previewprofile, previewProducerSurface);
228 CHECK_ERROR_RETURN_RET(previewOutput == nullptr, 0);
229
230 MEDIA_DEBUG_LOG("Setting preview callback");
231 ((sptr<PreviewOutput> &)previewOutput)->SetCallback(std::make_shared<TestPreviewOutputCallback>(testName));
232 ret = captureSession->AddOutput(previewOutput);
233 CHECK_ERROR_RETURN_RET(ret != 0, 0);
234
235 ret = captureSession->CommitConfig();
236 CHECK_ERROR_RETURN_RET(ret != 0, 0);
237
238 ret = captureSession->Start();
239 CHECK_ERROR_RETURN_RET(ret != 0, 0);
240
241 MEDIA_DEBUG_LOG("Preview started");
242 sleep(previewCaptureGap);
243 for (int i = 1; i <= photoCaptureCount; i++) {
244 MEDIA_DEBUG_LOG("Photo capture %{public}d started", i);
245 ret = ((sptr<PhotoOutput> &)photoOutput)->Capture(ConfigPhotoCaptureSetting());
246 CHECK_ERROR_RETURN_RET(ret != 0, 0);
247 sleep(gapAfterCapture);
248 }
249
250 MEDIA_DEBUG_LOG("Closing the session");
251 ((sptr<PreviewOutput> &)previewOutput)->Stop();
252 captureSession->Stop();
253 captureSession->Release();
254 cameraInput->Release();
255 camManagerObj->RegisterCameraStatusCallback(nullptr);
256
257 MEDIA_DEBUG_LOG("Camera new sample end.");
258 return 0;
259 }
260