1 /*
2  * Copyright (c) 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 "ohos_adapter/bridge/ark_camera_manager_adapter_wrapper.h"
17 
18 #include "ohos_adapter/bridge/ark_camera_buffer_listener_adapter_impl.h"
19 #include "ohos_adapter/bridge/ark_camera_status_callback_adapter_impl.h"
20 #include "ohos_adapter/bridge/ark_video_capture_params_adapter_impl.h"
21 #include "ohos_adapter/bridge/ark_video_capture_range_adapter_wrapper.h"
22 #include "ohos_adapter/ctocpp/ark_video_device_descriptor_adapter_vector_ctocpp.h"
23 
24 #include "base/bridge/ark_web_bridge_macros.h"
25 
26 namespace OHOS::ArkWeb {
27 
ArkCameraManagerAdapterWrapper(ArkWebRefPtr<ArkCameraManagerAdapter> ref)28 ArkCameraManagerAdapterWrapper::ArkCameraManagerAdapterWrapper(ArkWebRefPtr<ArkCameraManagerAdapter> ref) : ctocpp_(ref)
29 {}
30 
Create(std::shared_ptr<NWeb::CameraStatusCallbackAdapter> cameraStatusCallback)31 int32_t ArkCameraManagerAdapterWrapper::Create(std::shared_ptr<NWeb::CameraStatusCallbackAdapter> cameraStatusCallback)
32 {
33     if (!ctocpp_) {
34         return -1;
35     }
36     if (CHECK_SHARED_PTR_IS_NULL(cameraStatusCallback)) {
37         return ctocpp_->Create(nullptr);
38     }
39 
40     return ctocpp_->Create(new ArkCameraStatusCallbackAdapterImpl(cameraStatusCallback));
41 }
42 
GetDevicesInfo()43 std::vector<std::shared_ptr<NWeb::VideoDeviceDescriptorAdapter>> ArkCameraManagerAdapterWrapper::GetDevicesInfo()
44 {
45     if (!ctocpp_) {
46         return std::vector<std::shared_ptr<NWeb::VideoDeviceDescriptorAdapter>>();
47     }
48 
49     ArkVideoDeviceDescriptorAdapterVector ark_vector = ctocpp_->GetDevicesInfo();
50 
51     std::vector<std::shared_ptr<NWeb::VideoDeviceDescriptorAdapter>> result =
52         ArkVideoDeviceDescriptorAdapterVectorStructToClass(ark_vector);
53 
54     ArkVideoDeviceDescriptorAdapterVectorStructRelease(ark_vector);
55     return result;
56 }
57 
ReleaseCameraManger()58 int32_t ArkCameraManagerAdapterWrapper::ReleaseCameraManger()
59 {
60     if (!ctocpp_) {
61         return -1;
62     }
63     return ctocpp_->ReleaseCameraManger();
64 }
65 
GetExposureModes(std::vector<NWeb::ExposureModeAdapter> & exposureModesAdapter)66 int32_t ArkCameraManagerAdapterWrapper::GetExposureModes(std::vector<NWeb::ExposureModeAdapter>& exposureModesAdapter)
67 {
68     if (!ctocpp_) {
69         return -1;
70     }
71     ArkWebInt32Vector vec;
72     int32_t result = ctocpp_->GetExposureModes(vec);
73     for (int count = 0; count < vec.size; count++) {
74         exposureModesAdapter.push_back((NWeb::ExposureModeAdapter)vec.value[count]);
75     }
76 
77     ArkWebBasicVectorStructRelease(vec);
78 
79     return result;
80 }
81 
GetCurrentExposureMode(NWeb::ExposureModeAdapter & exposureModeAdapter)82 int32_t ArkCameraManagerAdapterWrapper::GetCurrentExposureMode(NWeb::ExposureModeAdapter& exposureModeAdapter)
83 {
84     if (!ctocpp_) {
85         return -1;
86     }
87     int32_t temp;
88     int32_t result = ctocpp_->GetCurrentExposureMode(temp);
89     exposureModeAdapter = (NWeb::ExposureModeAdapter)temp;
90     return result;
91 }
92 
GetCaptionRangeById(NWeb::RangeIDAdapter rangeId)93 std::shared_ptr<NWeb::VideoCaptureRangeAdapter> ArkCameraManagerAdapterWrapper::GetCaptionRangeById(
94     NWeb::RangeIDAdapter rangeId)
95 {
96     if (!ctocpp_) {
97         return nullptr;
98     }
99 
100     ArkWebRefPtr<ArkVideoCaptureRangeAdapter> adapter = ctocpp_->GetCaptionRangeById((int32_t)rangeId);
101     if (CHECK_REF_PTR_IS_NULL(adapter)) {
102         return nullptr;
103     }
104 
105     return std::make_shared<ArkVideoCaptureRangeAdapterWrapper>(adapter);
106 }
107 
IsFocusModeSupported(NWeb::FocusModeAdapter focusMode)108 bool ArkCameraManagerAdapterWrapper::IsFocusModeSupported(NWeb::FocusModeAdapter focusMode)
109 {
110     if (!ctocpp_) {
111         return false;
112     }
113     return ctocpp_->IsFocusModeSupported((int32_t)focusMode);
114 }
115 
GetCurrentFocusMode()116 NWeb::FocusModeAdapter ArkCameraManagerAdapterWrapper::GetCurrentFocusMode()
117 {
118     if (!ctocpp_) {
119         return NWeb::FocusModeAdapter::FOCUS_MODE_MANUAL;
120     }
121     return (NWeb::FocusModeAdapter)ctocpp_->GetCurrentFocusMode();
122 }
123 
IsFlashModeSupported(NWeb::FlashModeAdapter flashMode)124 bool ArkCameraManagerAdapterWrapper::IsFlashModeSupported(NWeb::FlashModeAdapter flashMode)
125 {
126     if (!ctocpp_) {
127         return false;
128     }
129     return ctocpp_->IsFlashModeSupported((int32_t)flashMode);
130 }
131 
RestartSession()132 int32_t ArkCameraManagerAdapterWrapper::RestartSession()
133 {
134     if (!ctocpp_) {
135         return -1;
136     }
137     return ctocpp_->RestartSession();
138 }
139 
StopSession(NWeb::CameraStopType stopType)140 int32_t ArkCameraManagerAdapterWrapper::StopSession(NWeb::CameraStopType stopType)
141 {
142     if (!ctocpp_) {
143         return -1;
144     }
145     return ctocpp_->StopSession((int32_t)stopType);
146 }
147 
GetCameraStatus()148 NWeb::CameraStatusAdapter ArkCameraManagerAdapterWrapper::GetCameraStatus()
149 {
150     if (!ctocpp_) {
151         return NWeb::CameraStatusAdapter::UNAVAILABLE;
152     }
153     return (NWeb::CameraStatusAdapter)ctocpp_->GetCameraStatus();
154 }
155 
IsExistCaptureTask()156 bool ArkCameraManagerAdapterWrapper::IsExistCaptureTask()
157 {
158     if (!ctocpp_) {
159         return false;
160     }
161     return ctocpp_->IsExistCaptureTask();
162 }
163 
StartStream(const std::string & deviceId,const std::shared_ptr<NWeb::VideoCaptureParamsAdapter> captureParams,std::shared_ptr<NWeb::CameraBufferListenerAdapter> listener)164 int32_t ArkCameraManagerAdapterWrapper::StartStream(const std::string& deviceId,
165     const std::shared_ptr<NWeb::VideoCaptureParamsAdapter> captureParams,
166     std::shared_ptr<NWeb::CameraBufferListenerAdapter> listener)
167 {
168     if (!ctocpp_) {
169         return -1;
170     }
171     ArkWebString str = ArkWebStringClassToStruct(deviceId);
172     int32_t result;
173 
174     if (CHECK_SHARED_PTR_IS_NULL(listener) && CHECK_SHARED_PTR_IS_NULL(captureParams)) {
175         result = ctocpp_->StartStream(str, nullptr, nullptr);
176     } else if (!CHECK_SHARED_PTR_IS_NULL(listener) && !CHECK_SHARED_PTR_IS_NULL(captureParams)) {
177         result = ctocpp_->StartStream(
178             str, new ArkVideoCaptureParamsAdapterImpl(captureParams), new ArkCameraBufferListenerAdapterImpl(listener));
179     } else if (CHECK_SHARED_PTR_IS_NULL(listener)) {
180         result = ctocpp_->StartStream(str, new ArkVideoCaptureParamsAdapterImpl(captureParams), nullptr);
181     } else {
182         result = ctocpp_->StartStream(str, nullptr, new ArkCameraBufferListenerAdapterImpl(listener));
183     }
184 
185     ArkWebStringStructRelease(str);
186     return result;
187 }
188 
SetForegroundFlag(bool isForeground)189 void ArkCameraManagerAdapterWrapper::SetForegroundFlag(bool isForeground)
190 {
191     if (!ctocpp_) {
192         return;
193     }
194     ctocpp_->SetForegroundFlag(isForeground);
195 }
196 
SetCameraStatus(NWeb::CameraStatusAdapter status)197 void ArkCameraManagerAdapterWrapper::SetCameraStatus(NWeb::CameraStatusAdapter status)
198 {
199     if (!ctocpp_) {
200         return;
201     }
202     ctocpp_->SetCameraStatus((int32_t)status);
203 }
204 
GetCurrentDeviceId()205 std::string ArkCameraManagerAdapterWrapper::GetCurrentDeviceId()
206 {
207     if (!ctocpp_) {
208         return "";
209     }
210     ArkWebString str = ctocpp_->GetCurrentDeviceId();
211     std::string result = ArkWebStringStructToClass(str);
212     ArkWebStringStructRelease(str);
213     return result;
214 }
215 
216 } // namespace OHOS::ArkWeb
217