1 /*
2 * Copyright (c) 2024 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 <cstdlib>
17 #include <iomanip>
18 #include <map>
19 #include <refbase.h>
20 #include <string>
21 #include <variant>
22 #include <vector>
23 #include "camera_log.h"
24 #include "cj_lambda.h"
25 #include "ffi_remote_data.h"
26 #include "securec.h"
27 #include "camera_error.h"
28 #include "camera_session_impl.h"
29
30 using namespace OHOS::FFI;
31
32 namespace OHOS::CameraStandard {
CJSession(sptr<CameraStandard::CaptureSession> session)33 CJSession::CJSession(sptr<CameraStandard::CaptureSession> session)
34 {
35 session_ = session;
36 }
37
GetCaptureSession()38 sptr<CameraStandard::CaptureSession> CJSession::GetCaptureSession()
39 {
40 return session_;
41 }
42
BeginConfig()43 int32_t CJSession::BeginConfig()
44 {
45 if (session_ == nullptr) {
46 return CameraError::CAMERA_SERVICE_ERROR;
47 }
48 return session_->BeginConfig();
49 }
50
CommitConfig()51 int32_t CJSession::CommitConfig()
52 {
53 if (session_ == nullptr) {
54 return CameraError::CAMERA_SERVICE_ERROR;
55 }
56 return session_->CommitConfig();
57 }
58
CanAddInput(sptr<CJCameraInput> cameraInput)59 bool CJSession::CanAddInput(sptr<CJCameraInput> cameraInput)
60 {
61 if (session_ == nullptr || cameraInput == nullptr) {
62 return false;
63 }
64 sptr<CameraStandard::CaptureInput> captureInput = cameraInput->GetCameraInput();
65 return session_->CanAddInput(captureInput);
66 }
67
AddInput(sptr<CJCameraInput> cameraInput)68 int32_t CJSession::AddInput(sptr<CJCameraInput> cameraInput)
69 {
70 if (session_ == nullptr || cameraInput == nullptr) {
71 return CameraError::CAMERA_SERVICE_ERROR;
72 }
73 sptr<CameraStandard::CaptureInput> captureInput = cameraInput->GetCameraInput();
74 return session_->AddInput(captureInput);
75 }
76
RemoveInput(sptr<CJCameraInput> cameraInput)77 int32_t CJSession::RemoveInput(sptr<CJCameraInput> cameraInput)
78 {
79 if (session_ == nullptr || cameraInput == nullptr) {
80 return CameraError::CAMERA_SERVICE_ERROR;
81 }
82 sptr<CameraStandard::CaptureInput> captureInput = cameraInput->GetCameraInput();
83 return session_->RemoveInput(captureInput);
84 }
85
CanAddOutput(sptr<CameraOutput> cameraOutput)86 bool CJSession::CanAddOutput(sptr<CameraOutput> cameraOutput)
87 {
88 if (session_ == nullptr || cameraOutput == nullptr) {
89 return false;
90 }
91 sptr<CameraStandard::CaptureOutput> captureOutput = cameraOutput->GetCameraOutput();
92 return session_->CanAddOutput(captureOutput);
93 }
94
AddOutput(sptr<CameraOutput> cameraOutput)95 int32_t CJSession::AddOutput(sptr<CameraOutput> cameraOutput)
96 {
97 if (session_ == nullptr || cameraOutput == nullptr) {
98 return CameraError::CAMERA_SERVICE_ERROR;
99 }
100 sptr<CameraStandard::CaptureOutput> captureOutput = cameraOutput->GetCameraOutput();
101 return session_->AddOutput(captureOutput);
102 }
103
RemoveOutput(sptr<CameraOutput> cameraOutput)104 int32_t CJSession::RemoveOutput(sptr<CameraOutput> cameraOutput)
105 {
106 if (session_ == nullptr || cameraOutput == nullptr) {
107 return CameraError::CAMERA_SERVICE_ERROR;
108 }
109 sptr<CameraStandard::CaptureOutput> captureOutput = cameraOutput->GetCameraOutput();
110 return session_->RemoveOutput(captureOutput);
111 }
112
Start()113 int32_t CJSession::Start()
114 {
115 if (session_ == nullptr) {
116 return CameraError::CAMERA_SERVICE_ERROR;
117 }
118 return session_->Start();
119 }
120
Stop()121 int32_t CJSession::Stop()
122 {
123 if (session_ == nullptr) {
124 return CameraError::CAMERA_SERVICE_ERROR;
125 }
126 return session_->Stop();
127 }
128
Release()129 int32_t CJSession::Release()
130 {
131 if (session_ == nullptr) {
132 return CameraError::CAMERA_SERVICE_ERROR;
133 }
134 return session_->Release();
135 }
136
CanPreconfig(PreconfigType preconfigType,ProfileSizeRatio preconfigRatio,bool & isSupported)137 int32_t CJSession::CanPreconfig(PreconfigType preconfigType, ProfileSizeRatio preconfigRatio, bool &isSupported)
138 {
139 if (session_ == nullptr) {
140 return CameraError::CAMERA_SERVICE_ERROR;
141 }
142 isSupported = session_->CanPreconfig(preconfigType, preconfigRatio);
143 return CameraErrorCode::SUCCESS;
144 }
145
Preconfig(PreconfigType preconfigType,ProfileSizeRatio preconfigRatio)146 int32_t CJSession::Preconfig(PreconfigType preconfigType, ProfileSizeRatio preconfigRatio)
147 {
148 if (session_ == nullptr) {
149 return CameraError::CAMERA_SERVICE_ERROR;
150 }
151 return session_->Preconfig(preconfigType, preconfigRatio);
152 }
153
AddSecureOutput(sptr<CameraOutput> cameraOutput)154 int32_t CJSession::AddSecureOutput(sptr<CameraOutput> cameraOutput)
155 {
156 if (session_ == nullptr || cameraOutput == nullptr) {
157 return CameraError::CAMERA_SERVICE_ERROR;
158 }
159 sptr<CameraStandard::CaptureOutput> captureOutput = cameraOutput->GetCameraOutput();
160 return session_->AddSecureOutput(captureOutput);
161 }
162
IsExposureModeSupported(ExposureMode mode,bool & isSupported)163 int32_t CJSession::IsExposureModeSupported(ExposureMode mode, bool &isSupported)
164 {
165 if (session_ == nullptr) {
166 return CameraError::CAMERA_SERVICE_ERROR;
167 }
168 return session_->IsExposureModeSupported(mode, isSupported);
169 }
170
GetExposureBiasRange(CArrFloat32 & cArr)171 int32_t CJSession::GetExposureBiasRange(CArrFloat32 &cArr)
172 {
173 if (session_ == nullptr) {
174 return CameraError::CAMERA_SERVICE_ERROR;
175 }
176 std::vector<float> exposureBiasRange;
177 int32_t ret = session_->GetExposureBiasRange(exposureBiasRange);
178 cArr.size = static_cast<int64_t>(exposureBiasRange.size());
179 if (cArr.size <= 0) {
180 return ret;
181 }
182 cArr.head = static_cast<float *>(malloc(sizeof(float) * cArr.size));
183 if (cArr.head == nullptr) {
184 return CameraError::CAMERA_SERVICE_ERROR;
185 }
186 for (int64_t i = 0; i < cArr.size; i++) {
187 cArr.head[i] = exposureBiasRange[i];
188 }
189
190 return ret;
191 }
192
GetExposureMode(ExposureMode & mode)193 int32_t CJSession::GetExposureMode(ExposureMode &mode)
194 {
195 if (session_ == nullptr) {
196 return CameraError::CAMERA_SERVICE_ERROR;
197 }
198 return session_->GetExposureMode(mode);
199 }
200
SetExposureMode(ExposureMode mode)201 int32_t CJSession::SetExposureMode(ExposureMode mode)
202 {
203 if (session_ == nullptr) {
204 return CameraError::CAMERA_SERVICE_ERROR;
205 }
206 session_->LockForControl();
207 int32_t ret = session_->SetExposureMode(mode);
208 session_->UnlockForControl();
209 return ret;
210 }
211
GetMeteringPoint(Point & point)212 int32_t CJSession::GetMeteringPoint(Point &point)
213 {
214 if (session_ == nullptr) {
215 return CameraError::CAMERA_SERVICE_ERROR;
216 }
217 session_->LockForControl();
218 int32_t ret = session_->GetMeteringPoint(point);
219 session_->UnlockForControl();
220 return ret;
221 }
222
SetMeteringPoint(Point point)223 int32_t CJSession::SetMeteringPoint(Point point)
224 {
225 if (session_ == nullptr) {
226 return CameraError::CAMERA_SERVICE_ERROR;
227 }
228 session_->LockForControl();
229 int32_t ret = session_->SetMeteringPoint(point);
230 session_->UnlockForControl();
231 return ret;
232 }
233
SetExposureBias(float value)234 int32_t CJSession::SetExposureBias(float value)
235 {
236 if (session_ == nullptr) {
237 return CameraError::CAMERA_SERVICE_ERROR;
238 }
239 session_->LockForControl();
240 int32_t ret = session_->SetExposureBias(value);
241 session_->UnlockForControl();
242 return ret;
243 }
244
GetExposureValue(float & value)245 int32_t CJSession::GetExposureValue(float &value)
246 {
247 if (session_ == nullptr) {
248 return CameraError::CAMERA_SERVICE_ERROR;
249 }
250 return session_->GetExposureValue(value);
251 }
252
GetSupportedColorSpaces(CArrI32 & cArr)253 void CJSession::GetSupportedColorSpaces(CArrI32 &cArr)
254 {
255 if (session_ == nullptr) {
256 return;
257 }
258 auto colorSpaces = session_->GetSupportedColorSpaces();
259 cArr.size = static_cast<int64_t>(colorSpaces.size());
260 if (cArr.size <= 0) {
261 return;
262 }
263 cArr.head = static_cast<int32_t *>(malloc(sizeof(int32_t) * cArr.size));
264 if (cArr.head == nullptr) {
265 cArr.size = 0;
266 return;
267 }
268 for (int i = 0; i < cArr.size; i++) {
269 cArr.head[i] = static_cast<int32_t>(colorSpaces[i]);
270 }
271 return;
272 }
273
SetColorSpace(ColorSpace colorSpace)274 int32_t CJSession::SetColorSpace(ColorSpace colorSpace)
275 {
276 if (session_ == nullptr) {
277 return CameraError::CAMERA_SERVICE_ERROR;
278 }
279 return session_->SetColorSpace(colorSpace);
280 }
281
GetActiveColorSpace(ColorSpace & colorSpace)282 int32_t CJSession::GetActiveColorSpace(ColorSpace &colorSpace)
283 {
284 if (session_ == nullptr) {
285 return CameraError::CAMERA_SERVICE_ERROR;
286 }
287 return session_->GetActiveColorSpace(colorSpace);
288 }
289
IsFlashModeSupported(FlashMode mode,bool & isSupported)290 int32_t CJSession::IsFlashModeSupported(FlashMode mode, bool &isSupported)
291 {
292 if (session_ == nullptr) {
293 return CameraError::CAMERA_SERVICE_ERROR;
294 }
295 return session_->IsFlashModeSupported(mode, isSupported);
296 }
297
HasFlash(bool & hasFlash)298 int32_t CJSession::HasFlash(bool &hasFlash)
299 {
300 if (session_ == nullptr) {
301 return CameraError::CAMERA_SERVICE_ERROR;
302 }
303 return session_->HasFlash(hasFlash);
304 }
305
GetFlashMode(FlashMode & mode)306 int32_t CJSession::GetFlashMode(FlashMode &mode)
307 {
308 if (session_ == nullptr) {
309 return CameraError::CAMERA_SERVICE_ERROR;
310 }
311 return session_->GetFlashMode(mode);
312 }
313
SetFlashMode(FlashMode mode)314 int32_t CJSession::SetFlashMode(FlashMode mode)
315 {
316 if (session_ == nullptr) {
317 return CameraError::CAMERA_SERVICE_ERROR;
318 }
319 session_->LockForControl();
320 int32_t ret = session_->SetFlashMode(mode);
321 session_->UnlockForControl();
322 return ret;
323 }
324
IsFocusModeSupported(FocusMode mode,bool & isSupported)325 int32_t CJSession::IsFocusModeSupported(FocusMode mode, bool &isSupported)
326 {
327 if (session_ == nullptr) {
328 return CameraError::CAMERA_SERVICE_ERROR;
329 }
330 return session_->IsFocusModeSupported(mode, isSupported);
331 }
332
SetFocusMode(FocusMode mode)333 int32_t CJSession::SetFocusMode(FocusMode mode)
334 {
335 if (session_ == nullptr) {
336 return CameraError::CAMERA_SERVICE_ERROR;
337 }
338 session_->LockForControl();
339 int32_t ret = session_->SetFocusMode(mode);
340 session_->UnlockForControl();
341 return ret;
342 }
343
GetFocusMode(FocusMode & mode)344 int32_t CJSession::GetFocusMode(FocusMode &mode)
345 {
346 if (session_ == nullptr) {
347 return CameraError::CAMERA_SERVICE_ERROR;
348 }
349 session_->LockForControl();
350 int32_t ret = session_->GetFocusMode(mode);
351 session_->UnlockForControl();
352 return ret;
353 }
354
SetFocusPoint(Point point)355 int32_t CJSession::SetFocusPoint(Point point)
356 {
357 if (session_ == nullptr) {
358 return CameraError::CAMERA_SERVICE_ERROR;
359 }
360 session_->LockForControl();
361 int32_t ret = session_->SetFocusPoint(point);
362 session_->UnlockForControl();
363 return ret;
364 }
365
GetFocusPoint(Point & point)366 int32_t CJSession::GetFocusPoint(Point &point)
367 {
368 if (session_ == nullptr) {
369 return CameraError::CAMERA_SERVICE_ERROR;
370 }
371 return session_->GetFocusPoint(point);
372 }
373
GetFocalLength(float & focalLength)374 int32_t CJSession::GetFocalLength(float &focalLength)
375 {
376 if (session_ == nullptr) {
377 return CameraError::CAMERA_SERVICE_ERROR;
378 }
379 return session_->GetFocalLength(focalLength);
380 }
381
IsVideoStabilizationModeSupported(VideoStabilizationMode mode,bool & isSupported)382 int32_t CJSession::IsVideoStabilizationModeSupported(VideoStabilizationMode mode, bool &isSupported)
383 {
384 if (session_ == nullptr) {
385 return CameraError::CAMERA_SERVICE_ERROR;
386 }
387 return session_->IsVideoStabilizationModeSupported(mode, isSupported);
388 }
389
GetActiveVideoStabilizationMode(VideoStabilizationMode & mode)390 int32_t CJSession::GetActiveVideoStabilizationMode(VideoStabilizationMode &mode)
391 {
392 if (session_ == nullptr) {
393 return CameraError::CAMERA_SERVICE_ERROR;
394 }
395 return session_->GetActiveVideoStabilizationMode(mode);
396 }
397
SetVideoStabilizationMode(VideoStabilizationMode mode)398 int32_t CJSession::SetVideoStabilizationMode(VideoStabilizationMode mode)
399 {
400 if (session_ == nullptr) {
401 return CameraError::CAMERA_SERVICE_ERROR;
402 }
403 return session_->SetVideoStabilizationMode(mode);
404 }
405
GetZoomRatioRange(CArrFloat32 & ranges)406 int32_t CJSession::GetZoomRatioRange(CArrFloat32 &ranges)
407 {
408 if (session_ == nullptr) {
409 return CameraError::CAMERA_SERVICE_ERROR;
410 }
411 std::vector<float> zoomRatioRange;
412 int32_t ret = session_->GetZoomRatioRange(zoomRatioRange);
413
414 ranges.size = static_cast<int64_t>(zoomRatioRange.size());
415 if (ranges.size <= 0) {
416 return ret;
417 }
418 ranges.head = static_cast<float *>(malloc(sizeof(float) * ranges.size));
419 if (ranges.head == nullptr) {
420 return CameraError::CAMERA_SERVICE_ERROR;
421 }
422 for (int64_t i = 0; i < ranges.size; i++) {
423 ranges.head[i] = zoomRatioRange[i];
424 }
425
426 return ret;
427 }
428
SetZoomRatio(float ratio)429 int32_t CJSession::SetZoomRatio(float ratio)
430 {
431 if (session_ == nullptr) {
432 return CameraError::CAMERA_SERVICE_ERROR;
433 }
434 session_->LockForControl();
435 int32_t ret = session_->SetZoomRatio(ratio);
436 session_->UnlockForControl();
437 return ret;
438 }
439
GetZoomRatio(float & ratio)440 int32_t CJSession::GetZoomRatio(float &ratio)
441 {
442 if (session_ == nullptr) {
443 return CameraError::CAMERA_SERVICE_ERROR;
444 }
445 return session_->GetZoomRatio(ratio);
446 }
447
SetSmoothZoom(float targetZoomRatio,uint32_t smoothZoomType)448 int32_t CJSession::SetSmoothZoom(float targetZoomRatio, uint32_t smoothZoomType)
449 {
450 if (session_ == nullptr) {
451 return CameraError::CAMERA_SERVICE_ERROR;
452 }
453 return session_->SetSmoothZoom(targetZoomRatio, smoothZoomType);
454 }
455
OnSmoothZoom(int64_t callbackId)456 void CJSession::OnSmoothZoom(int64_t callbackId)
457 {
458 if (smoothZoomCallback_ == nullptr) {
459 smoothZoomCallback_ = std::make_shared<CJSmoothZoomCallback>();
460 if (session_ == nullptr || smoothZoomCallback_ == nullptr) {
461 return;
462 }
463 session_->SetSmoothZoomCallback(smoothZoomCallback_);
464 }
465
466 // wrapper that helps us execute CJ callback according to the id
467 auto callback = CJLambda::Create(reinterpret_cast<void (*)(int32_t)>(callbackId));
468 auto callbackRef = std::make_shared<CallbackRef<int32_t>>(callback, callbackId);
469 smoothZoomCallback_->SaveCallbackRef(callbackRef);
470 }
471
OffSmoothZoom(int64_t callbackId)472 void CJSession::OffSmoothZoom(int64_t callbackId)
473 {
474 if (smoothZoomCallback_ == nullptr) {
475 MEDIA_ERR_LOG("smoothZoomCallback is null");
476 return;
477 }
478 smoothZoomCallback_->RemoveCallbackRef(callbackId);
479 }
480
OffAllSmoothZoom()481 void CJSession::OffAllSmoothZoom()
482 {
483 if (smoothZoomCallback_ == nullptr) {
484 MEDIA_ERR_LOG("smoothZoomCallback is null");
485 return;
486 }
487 smoothZoomCallback_->RemoveAllCallbackRef();
488 }
489
OnError(int64_t callbackId)490 void CJSession::OnError(int64_t callbackId)
491 {
492 if (errorCallback_ == nullptr) {
493 errorCallback_ = std::make_shared<CJSessionCallback>();
494 if (session_ == nullptr || errorCallback_ == nullptr) {
495 return;
496 }
497 session_->SetCallback(errorCallback_);
498 }
499
500 auto callback = CJLambda::Create(reinterpret_cast<void (*)(int32_t)>(callbackId));
501 auto callbackRef = std::make_shared<CallbackRef<int32_t>>(callback, callbackId);
502 errorCallback_->SaveCallbackRef(callbackRef);
503 }
504
OffError(int64_t callbackId)505 void CJSession::OffError(int64_t callbackId)
506 {
507 if (errorCallback_ == nullptr) {
508 MEDIA_ERR_LOG("errorCallback is null");
509 return;
510 }
511 errorCallback_->RemoveCallbackRef(callbackId);
512 }
513
OffAllError()514 void CJSession::OffAllError()
515 {
516 if (errorCallback_ == nullptr) {
517 MEDIA_ERR_LOG("errorCallback is null");
518 return;
519 }
520 errorCallback_->RemoveAllCallbackRef();
521 }
522
OnFocusStateChange(int64_t callbackId)523 void CJSession::OnFocusStateChange(int64_t callbackId)
524 {
525 if (focusStateCallback_ == nullptr) {
526 focusStateCallback_ = std::make_shared<CJFocusStateCallback>();
527 if (session_ == nullptr || focusStateCallback_ == nullptr) {
528 return;
529 }
530 session_->SetFocusCallback(focusStateCallback_);
531 }
532
533 auto callback = CJLambda::Create(reinterpret_cast<void (*)(int32_t)>(callbackId));
534 auto callbackRef = std::make_shared<CallbackRef<int32_t>>(callback, callbackId);
535 focusStateCallback_->SaveCallbackRef(callbackRef);
536 }
537
OffFocusStateChange(int64_t callbackId)538 void CJSession::OffFocusStateChange(int64_t callbackId)
539 {
540 if (focusStateCallback_ == nullptr) {
541 MEDIA_ERR_LOG("focusStateCallback is null");
542 return;
543 }
544 focusStateCallback_->RemoveCallbackRef(callbackId);
545 }
546
OffAllFocusStateChange()547 void CJSession::OffAllFocusStateChange()
548 {
549 if (focusStateCallback_ == nullptr) {
550 MEDIA_ERR_LOG("focusStateCallback is null");
551 return;
552 }
553 focusStateCallback_->RemoveAllCallbackRef();
554 }
555
OnSmoothZoom(int32_t duration)556 void CJSmoothZoomCallback::OnSmoothZoom(int32_t duration)
557 {
558 MEDIA_DEBUG_LOG("OnSmoothZoomInfoAvailable is called");
559 ExecuteCallback(duration);
560 }
561
OnError(int32_t errorCode)562 void CJSessionCallback::OnError(int32_t errorCode)
563 {
564 MEDIA_DEBUG_LOG("OnError is called");
565 ExecuteCallback(errorCode);
566 }
567
OnFocusState(FocusState state)568 void CJFocusStateCallback::OnFocusState(FocusState state)
569 {
570 MEDIA_DEBUG_LOG("OnFocusStateChange is called");
571 // CJ callback expects int32_t
572 ExecuteCallback(static_cast<int32_t>(state));
573 }
574
575 } // namespace OHOS::CameraStandard