1 /*
2 * Copyright (c) 2022-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 "dcamera_client_demo.h"
17
18 #include "access_token.h"
19 #include "accesstoken_kit.h"
20 #include "hap_token_info.h"
21 #include "ipc_skeleton.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24
25 using namespace OHOS;
26 using namespace OHOS::Camera;
27 using namespace OHOS::CameraStandard;
28 using namespace OHOS::DistributedHardware;
29
30 constexpr double LATITUDE = 22.306;
31 constexpr double LONGITUDE = 52.12;
32 constexpr double ALTITUDE = 2.365;
33 constexpr int32_t PHOTO_WIDTH = 1280;
34 constexpr int32_t PHOTO_HEIGTH = 960;
35 constexpr int32_t PREVIEW_WIDTH = 640;
36 constexpr int32_t PREVIEW_HEIGTH = 480;
37 constexpr int32_t VIDEO_WIDTH = 640;
38 constexpr int32_t VIDEO_HEIGTH = 480;
39 constexpr int32_t SLEEP_FIVE_SECOND = 5;
40 constexpr int32_t SLEEP_TWENTY_SECOND = 20;
41
42 static sptr<CameraDevice> g_cameraInfo = nullptr;
43 static sptr<CameraManager> g_cameraManager = nullptr;
44 static sptr<CaptureInput> g_cameraInput = nullptr;
45 static sptr<CaptureOutput> g_photoOutput = nullptr;
46 static sptr<CaptureOutput> g_previewOutput = nullptr;
47 static sptr<CaptureOutput> g_videoOutput = nullptr;
48 static sptr<CaptureSession> g_captureSession = nullptr;
49 static std::shared_ptr<DCameraCaptureInfo> g_photoInfo = nullptr;
50 static std::shared_ptr<DCameraCaptureInfo> g_previewInfo = nullptr;
51 static std::shared_ptr<DCameraCaptureInfo> g_videoInfo = nullptr;
52
53 #ifdef DCAMERA_YUV
54 constexpr int32_t PHOTO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_JPEG;
55 constexpr int32_t PREVIEW_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP;
56 constexpr int32_t VIDEO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP;
57 #else
58 constexpr int32_t PHOTO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_JPEG;
59 constexpr int32_t PREVIEW_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888;
60 constexpr int32_t VIDEO_FORMAT = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888;
61 #endif
62
InitCameraStandard()63 static int32_t InitCameraStandard()
64 {
65 g_cameraManager = CameraManager::GetInstance();
66 g_cameraManager->SetCallback(std::make_shared<DemoDCameraManagerCallback>());
67
68 int rv = g_cameraManager->CreateCaptureSession(&g_captureSession);
69 if (rv != DCAMERA_OK) {
70 DHLOGE("InitCameraStandard create captureSession failed, rv: %d", rv);
71 return rv;
72 }
73 std::shared_ptr<DemoDCameraSessionCallback> sessionCallback = std::make_shared<DemoDCameraSessionCallback>();
74 g_captureSession->SetCallback(sessionCallback);
75 g_captureSession->SetFocusCallback(sessionCallback);
76
77 std::vector<sptr<CameraDevice>> cameraObjList = g_cameraManager->GetSupportedCameras();
78 for (auto info : cameraObjList) {
79 DHLOGI("Camera: %s, position: %d, camera type: %d, connection type: %d", GetAnonyString(info->GetID()).c_str(),
80 info->GetPosition(), info->GetCameraType(), info->GetConnectionType());
81 // OHOS_CAMERA_POSITION_FRONT or OHOS_CAMERA_POSITION_BACK
82 if ((info->GetPosition() == CameraPosition::CAMERA_POSITION_FRONT) &&
83 (info->GetConnectionType() == ConnectionType::CAMERA_CONNECTION_REMOTE)) {
84 g_cameraInfo = info;
85 break;
86 }
87 }
88
89 if (g_cameraInfo == nullptr) {
90 DHLOGE("Distributed Camera Demo: have no remote camera");
91 return DCAMERA_BAD_VALUE;
92 }
93
94 rv = g_cameraManager->CreateCameraInput(g_cameraInfo, &((sptr<CameraInput> &)g_cameraInput));
95 if (rv != DCAMERA_OK) {
96 DHLOGE("InitCameraStandard create cameraInput failed, rv: %d", rv);
97 return rv;
98 }
99 int32_t ret = ((sptr<CameraInput> &)g_cameraInput)->Open();
100 if (ret != DCAMERA_OK) {
101 DHLOGE("InitCameraStandard g_cameraInput Open failed, ret: %d", ret);
102 return ret;
103 }
104 std::shared_ptr<DemoDCameraInputCallback> inputCallback = std::make_shared<DemoDCameraInputCallback>();
105 ((sptr<CameraInput> &)g_cameraInput)->SetErrorCallback(inputCallback);
106 return DCAMERA_OK;
107 }
108
InitCaptureInfo()109 static void InitCaptureInfo()
110 {
111 g_photoInfo = std::make_shared<DCameraCaptureInfo>();
112 g_photoInfo->width_ = PHOTO_WIDTH;
113 g_photoInfo->height_ = PHOTO_HEIGTH;
114 g_photoInfo->format_ = PHOTO_FORMAT;
115
116 g_previewInfo = std::make_shared<DCameraCaptureInfo>();
117 g_previewInfo->width_ = PREVIEW_WIDTH;
118 g_previewInfo->height_ = PREVIEW_HEIGTH;
119 g_previewInfo->format_ = PREVIEW_FORMAT;
120
121 g_videoInfo = std::make_shared<DCameraCaptureInfo>();
122 g_videoInfo->width_ = VIDEO_WIDTH;
123 g_videoInfo->height_ = VIDEO_HEIGTH;
124 g_videoInfo->format_ = VIDEO_FORMAT;
125 }
126
ConvertToCameraFormat(int32_t format)127 static CameraFormat ConvertToCameraFormat(int32_t format)
128 {
129 CameraFormat ret = CameraFormat::CAMERA_FORMAT_INVALID;
130 DCameraFormat df = static_cast<DCameraFormat>(format);
131 switch (df) {
132 case DCameraFormat::OHOS_CAMERA_FORMAT_RGBA_8888:
133 ret = CameraFormat::CAMERA_FORMAT_RGBA_8888;
134 break;
135 case DCameraFormat::OHOS_CAMERA_FORMAT_YCBCR_420_888:
136 ret = CameraFormat::CAMERA_FORMAT_YCBCR_420_888;
137 break;
138 case DCameraFormat::OHOS_CAMERA_FORMAT_YCRCB_420_SP:
139 ret = CameraFormat::CAMERA_FORMAT_YUV_420_SP;
140 break;
141 case DCameraFormat::OHOS_CAMERA_FORMAT_JPEG:
142 ret = CameraFormat::CAMERA_FORMAT_JPEG;
143 break;
144 default:
145 break;
146 }
147 return ret;
148 }
149
InitPhotoOutput()150 static void InitPhotoOutput()
151 {
152 DHLOGI("Distributed Camera Demo: Create PhotoOutput, width = %d, height = %d, format = %d",
153 g_photoInfo->width_, g_photoInfo->height_, g_photoInfo->format_);
154 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
155 sptr<IBufferConsumerListener> photoListener(new DemoDCameraPhotoSurfaceListener(photoSurface));
156 photoSurface->RegisterConsumerListener(photoListener);
157 CameraFormat photoFormat = ConvertToCameraFormat(g_photoInfo->format_);
158 Size photoSize = {g_photoInfo->width_, g_photoInfo->height_};
159 Profile photoProfile(photoFormat, photoSize);
160 sptr<IBufferProducer> photoProducer = photoSurface->GetProducer();
161 int rv = g_cameraManager->CreatePhotoOutput(photoProfile, photoProducer, &((sptr<PhotoOutput> &)g_photoOutput));
162 if (rv != DCAMERA_OK) {
163 DHLOGE("InitPhotoOutput create photoOutput failed, rv: %d", rv);
164 return;
165 }
166 ((sptr<PhotoOutput> &)g_photoOutput)->SetCallback(std::make_shared<DemoDCameraPhotoCallback>());
167 }
168
InitPreviewOutput()169 static void InitPreviewOutput()
170 {
171 DHLOGI("Distributed Camera Demo: Create PreviewOutput, width = %d, height = %d, format = %d",
172 g_previewInfo->width_, g_previewInfo->height_, g_previewInfo->format_);
173 sptr<IConsumerSurface> previewSurface = IConsumerSurface::Create();
174 sptr<IBufferConsumerListener> previewListener(new DemoDCameraPreviewSurfaceListener(previewSurface));
175 previewSurface->RegisterConsumerListener(previewListener);
176 CameraFormat previewFormat = ConvertToCameraFormat(g_previewInfo->format_);
177 Size previewSize = {g_previewInfo->width_, g_previewInfo->height_};
178 Profile previewProfile(previewFormat, previewSize);
179 sptr<IBufferProducer> previewProducer = previewSurface->GetProducer();
180 sptr<Surface> previewProducerSurface = Surface::CreateSurfaceAsProducer(previewProducer);
181 int rv = g_cameraManager->CreatePreviewOutput(
182 previewProfile, previewProducerSurface, &((sptr<PreviewOutput> &)g_previewOutput));
183 if (rv != DCAMERA_OK) {
184 DHLOGE("InitPhotoOutput create previewOutput failed, rv: %d", rv);
185 return;
186 }
187 ((sptr<PreviewOutput> &)g_previewOutput)->SetCallback(std::make_shared<DemoDCameraPreviewCallback>());
188 }
189
InitVideoOutput()190 static void InitVideoOutput()
191 {
192 DHLOGI("Distributed Camera Demo: Create VideoOutput, width = %d, height = %d, format = %d",
193 g_videoInfo->width_, g_videoInfo->height_, g_videoInfo->format_);
194 sptr<IConsumerSurface> videoSurface = IConsumerSurface::Create();
195 sptr<IBufferConsumerListener> videoListener(new DemoDCameraVideoSurfaceListener(videoSurface));
196 videoSurface->RegisterConsumerListener(videoListener);
197 CameraFormat videoFormat = ConvertToCameraFormat(g_videoInfo->format_);
198 Size videoSize = {g_videoInfo->width_, g_videoInfo->height_};
199 std::vector<int32_t> framerates = {};
200 VideoProfile videoSettings(videoFormat, videoSize, framerates);
201 sptr<IBufferProducer> videoProducer = videoSurface->GetProducer();
202 sptr<Surface> pSurface = Surface::CreateSurfaceAsProducer(videoProducer);
203 int rv = g_cameraManager->CreateVideoOutput(videoSettings, pSurface, &((sptr<VideoOutput> &)g_videoOutput));
204 if (rv != DCAMERA_OK) {
205 DHLOGE("InitPhotoOutput create videoOutput failed, rv: %d", rv);
206 return;
207 }
208 ((sptr<VideoOutput> &)g_videoOutput)->SetCallback(std::make_shared<DemoDCameraVideoCallback>());
209 }
210
ConfigCaptureSession()211 static void ConfigCaptureSession()
212 {
213 g_captureSession->BeginConfig();
214 g_captureSession->AddInput(g_cameraInput);
215 g_captureSession->AddOutput(g_previewOutput);
216 g_captureSession->AddOutput(g_videoOutput);
217 g_captureSession->AddOutput(g_photoOutput);
218 g_captureSession->CommitConfig();
219
220 std::vector<VideoStabilizationMode> stabilizationModes;
221 int32_t rv = g_captureSession->GetSupportedStabilizationMode(stabilizationModes);
222 if (rv != DCAMERA_OK) {
223 DHLOGE("ConfigCaptureSession get supported stabilization mode failed, rv: %d", rv);
224 return;
225 }
226 if (!stabilizationModes.empty()) {
227 for (auto mode : stabilizationModes) {
228 DHLOGI("Distributed Camera Demo: video stabilization mode %d", mode);
229 }
230 g_captureSession->SetVideoStabilizationMode(stabilizationModes.back());
231 }
232 }
233
ConfigFocusAndExposure()234 static void ConfigFocusAndExposure()
235 {
236 g_captureSession->LockForControl();
237 FocusMode focusMode = FOCUS_MODE_CONTINUOUS_AUTO;
238 ExposureMode exposureMode = EXPOSURE_MODE_AUTO;
239 float exposureValue = 0;
240 std::vector<float> biasRange;
241 int32_t rv = g_captureSession->GetExposureBiasRange(biasRange);
242 if (rv != DCAMERA_OK) {
243 DHLOGE("ConfigFocusAndExposure get exposure bias range failed, rv: %d", rv);
244 return;
245 }
246 if (!biasRange.empty()) {
247 DHLOGI("Distributed Camera Demo: get %d exposure compensation range", biasRange.size());
248 exposureValue = biasRange[0];
249 }
250 g_captureSession->SetFocusMode(focusMode);
251 g_captureSession->SetExposureMode(exposureMode);
252 g_captureSession->SetExposureBias(exposureValue);
253 g_captureSession->UnlockForControl();
254 }
255
ConfigPhotoCaptureSetting()256 static std::shared_ptr<PhotoCaptureSetting> ConfigPhotoCaptureSetting()
257 {
258 std::shared_ptr<PhotoCaptureSetting> photoCaptureSettings = std::make_shared<PhotoCaptureSetting>();
259 // Rotation
260 PhotoCaptureSetting::RotationConfig rotation = PhotoCaptureSetting::RotationConfig::Rotation_0;
261 photoCaptureSettings->SetRotation(rotation);
262 // QualityLevel
263 PhotoCaptureSetting::QualityLevel quality = PhotoCaptureSetting::QualityLevel::QUALITY_LEVEL_HIGH;
264 photoCaptureSettings->SetQuality(quality);
265 // Location
266 std::unique_ptr<Location> location = std::make_unique<Location>();
267 location->latitude = LATITUDE;
268 location->longitude = LONGITUDE;
269 location->altitude = ALTITUDE;
270 photoCaptureSettings->SetLocation(location);
271 return photoCaptureSettings;
272 }
273
main()274 int main()
275 {
276 uint64_t tokenId;
277 const char *perms[2];
278 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
279 perms[1] = "ohos.permission.CAMERA";
280 NativeTokenInfoParams infoInstance = {
281 .dcapsNum = 0,
282 .permsNum = 2,
283 .aclsNum = 0,
284 .dcaps = NULL,
285 .perms = perms,
286 .acls = NULL,
287 .processName = "dcamera_client_demo",
288 .aplStr = "system_basic",
289 };
290 tokenId = GetAccessTokenId(&infoInstance);
291 SetSelfTokenID(tokenId);
292 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
293
294 DHLOGI("========== Distributed Camera Demo Start ==========");
295 int32_t ret = InitCameraStandard();
296 if (ret != DCAMERA_OK) {
297 std::cout << "have no remote camera" << std::endl;
298 return 0;
299 }
300
301 InitCaptureInfo();
302 InitPhotoOutput();
303 InitPreviewOutput();
304 InitVideoOutput();
305 ConfigCaptureSession();
306
307 g_captureSession->Start();
308 sleep(SLEEP_FIVE_SECOND);
309
310 if (((sptr<VideoOutput> &)g_videoOutput)->Start() != DCAMERA_OK) {
311 DHLOGE("main g_videoOutput Start failed");
312 }
313 sleep(SLEEP_FIVE_SECOND);
314
315 ConfigFocusAndExposure();
316 sleep(SLEEP_FIVE_SECOND);
317
318 ((sptr<PhotoOutput> &)g_photoOutput)->Capture(ConfigPhotoCaptureSetting());
319 sleep(SLEEP_TWENTY_SECOND);
320
321 if (((sptr<VideoOutput> &)g_videoOutput)->Stop() != DCAMERA_OK) {
322 DHLOGE("main g_videoOutput Stop failed");
323 }
324 sleep(SLEEP_FIVE_SECOND);
325
326 g_captureSession->Stop();
327 if (g_cameraInput->Close() != DCAMERA_OK) {
328 DHLOGE("main g_cameraInput Close failed");
329 }
330 g_captureSession->Release();
331 if (g_cameraInput->Release() != DCAMERA_OK) {
332 DHLOGE("main g_cameraInput Close failed");
333 }
334 DHLOGI("========== Distributed Camera Demo End ==========");
335 return 0;
336 }