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 "camera_util.h"
17 #include "media_log.h"
18 #include "hcamera_device.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
HCameraDevice(sptr<HCameraHostManager> & cameraHostManager,sptr<CameraDeviceCallback> deviceCallback,std::string cameraID)22 HCameraDevice::HCameraDevice(sptr<HCameraHostManager> &cameraHostManager,
23 sptr<CameraDeviceCallback> deviceCallback, std::string cameraID)
24 {
25 cameraHostManager_ = cameraHostManager;
26 deviceHDICallback_ = deviceCallback;
27 cameraID_ = cameraID;
28 streamOperator_ = nullptr;
29 isReleaseCameraDevice_ = false;
30 }
31
~HCameraDevice()32 HCameraDevice::~HCameraDevice()
33 {}
34
GetCameraId()35 std::string HCameraDevice::GetCameraId()
36 {
37 return cameraID_;
38 }
39
SetReleaseCameraDevice(bool isRelease)40 int32_t HCameraDevice::SetReleaseCameraDevice(bool isRelease)
41 {
42 isReleaseCameraDevice_ = isRelease;
43 return CAMERA_OK;
44 }
45
IsReleaseCameraDevice()46 bool HCameraDevice::IsReleaseCameraDevice()
47 {
48 return isReleaseCameraDevice_;
49 }
50
GetSettings()51 std::shared_ptr<Camera::CameraMetadata> HCameraDevice::GetSettings()
52 {
53 int32_t errCode;
54 std::shared_ptr<Camera::CameraMetadata> ability = nullptr;
55 errCode = cameraHostManager_->GetCameraAbility(cameraID_, ability);
56 if (errCode != CAMERA_OK) {
57 MEDIA_ERR_LOG("HCameraDevice::GetSettings Failed to get Camera Ability: %{public}d", errCode);
58 return nullptr;
59 }
60 return ability;
61 }
62
Open()63 int32_t HCameraDevice::Open()
64 {
65 int32_t errorCode;
66 MEDIA_INFO_LOG("HCameraDevice::Open Opening camera device: %{public}s", cameraID_.c_str());
67 errorCode = cameraHostManager_->OpenCameraDevice(cameraID_, deviceHDICallback_, hdiCameraDevice_);
68 if (errorCode == CAMERA_OK) {
69 if (updateSettings_ != nullptr) {
70 Camera::CamRetCode rc = hdiCameraDevice_->UpdateSettings(updateSettings_);
71 if (rc != Camera::NO_ERROR) {
72 MEDIA_ERR_LOG("HCameraDevice::Open Update setting failed with error Code: %{public}d", rc);
73 return HdiToServiceError(rc);
74 }
75 }
76 errorCode = HdiToServiceError(hdiCameraDevice_->SetResultMode(Camera::ON_CHANGED));
77 } else {
78 MEDIA_ERR_LOG("HCameraDevice::Open Failed to open camera");
79 }
80 return errorCode;
81 }
82
Close()83 int32_t HCameraDevice::Close()
84 {
85 if (hdiCameraDevice_ != nullptr) {
86 MEDIA_INFO_LOG("HCameraDevice::Close Closing camera device: %{public}s", cameraID_.c_str());
87 hdiCameraDevice_->Close();
88 }
89 hdiCameraDevice_ = nullptr;
90 return CAMERA_OK;
91 }
92
Release()93 int32_t HCameraDevice::Release()
94 {
95 if (hdiCameraDevice_ != nullptr) {
96 Close();
97 }
98 if (deviceHDICallback_ != nullptr) {
99 deviceHDICallback_->SetHCameraDevice(nullptr);
100 deviceHDICallback_ = nullptr;
101 }
102 deviceSvcCallback_ = nullptr;
103 return CAMERA_OK;
104 }
105
GetEnabledResults(std::vector<int32_t> & results)106 int32_t HCameraDevice::GetEnabledResults(std::vector<int32_t> &results)
107 {
108 std::vector<int32_t> settings;
109
110 Camera::CamRetCode rc = hdiCameraDevice_->GetEnabledResults(settings);
111 if (rc != Camera::NO_ERROR) {
112 MEDIA_ERR_LOG("HCameraDevice::GetEnabledResults failed with error Code:%{public}d", rc);
113 return HdiToServiceError(rc);
114 }
115 results = settings;
116 return CAMERA_OK;
117 }
118
UpdateSetting(const std::shared_ptr<Camera::CameraMetadata> & settings)119 int32_t HCameraDevice::UpdateSetting(const std::shared_ptr<Camera::CameraMetadata> &settings)
120 {
121 if (settings == nullptr) {
122 MEDIA_ERR_LOG("HCameraDevice::UpdateSetting settings is null");
123 return CAMERA_INVALID_ARG;
124 }
125
126 if (!Camera::GetCameraMetadataItemCount(settings->get())) {
127 return CAMERA_OK;
128 }
129 updateSettings_ = settings;
130 if (hdiCameraDevice_ != nullptr) {
131 Camera::CamRetCode rc = hdiCameraDevice_->UpdateSettings(settings);
132 if (rc != Camera::NO_ERROR) {
133 MEDIA_ERR_LOG("HCameraDevice::UpdateSetting failed with error Code: %{public}d", rc);
134 return HdiToServiceError(rc);
135 }
136 }
137 return CAMERA_OK;
138 }
139
EnableResult(std::vector<int32_t> & results)140 int32_t HCameraDevice::EnableResult(std::vector<int32_t> &results)
141 {
142 if (results.empty()) {
143 MEDIA_ERR_LOG("HCameraDevice::EnableResult results vector empty");
144 return CAMERA_INVALID_ARG;
145 }
146
147 if (hdiCameraDevice_ == nullptr) {
148 MEDIA_ERR_LOG("HCameraDevice::hdiCameraDevice_ is null");
149 return CAMERA_UNKNOWN_ERROR;
150 }
151
152 Camera::CamRetCode rc = hdiCameraDevice_->EnableResult(results);
153 if (rc != Camera::NO_ERROR) {
154 MEDIA_ERR_LOG("HCameraDevice::EnableResult failed with error Code:%{public}d", rc);
155 return HdiToServiceError(rc);
156 }
157
158 return CAMERA_OK;
159 }
160
DisableResult(std::vector<int32_t> & results)161 int32_t HCameraDevice::DisableResult(std::vector<int32_t> &results)
162 {
163 if (results.empty()) {
164 MEDIA_ERR_LOG("HCameraDevice::DisableResult results vector empty");
165 return CAMERA_INVALID_ARG;
166 }
167
168 if (hdiCameraDevice_ == nullptr) {
169 MEDIA_ERR_LOG("HCameraDevice::hdiCameraDevice_ is null");
170 return CAMERA_UNKNOWN_ERROR;
171 }
172
173 Camera::CamRetCode rc = hdiCameraDevice_->DisableResult(results);
174 if (rc != Camera::NO_ERROR) {
175 MEDIA_ERR_LOG("HCameraDevice::DisableResult failed with error Code:%{public}d", rc);
176 return HdiToServiceError(rc);
177 }
178 return CAMERA_OK;
179 }
180
SetCallback(sptr<ICameraDeviceServiceCallback> & callback)181 int32_t HCameraDevice::SetCallback(sptr<ICameraDeviceServiceCallback> &callback)
182 {
183 if (callback == nullptr) {
184 MEDIA_ERR_LOG("HCameraDevice::SetCallback callback is null");
185 return CAMERA_INVALID_ARG;
186 }
187 deviceHDICallback_->SetHCameraDevice(this);
188 deviceSvcCallback_ = callback;
189 return CAMERA_OK;
190 }
191
GetStreamOperator(sptr<Camera::IStreamOperatorCallback> callback,sptr<Camera::IStreamOperator> & streamOperator)192 int32_t HCameraDevice::GetStreamOperator(sptr<Camera::IStreamOperatorCallback> callback,
193 sptr<Camera::IStreamOperator> &streamOperator)
194 {
195 if (callback == nullptr) {
196 MEDIA_ERR_LOG("HCameraDevice::GetStreamOperator callback is null");
197 return CAMERA_INVALID_ARG;
198 }
199
200 if (hdiCameraDevice_ == nullptr) {
201 MEDIA_ERR_LOG("HCameraDevice::hdiCameraDevice_ is null");
202 return CAMERA_UNKNOWN_ERROR;
203 }
204
205 Camera::CamRetCode rc = hdiCameraDevice_->GetStreamOperator(callback, streamOperator);
206 if (rc != Camera::NO_ERROR) {
207 MEDIA_ERR_LOG("HCameraDevice::GetStreamOperator failed with error Code:%{public}d", rc);
208 return HdiToServiceError(rc);
209 }
210 streamOperator_ = streamOperator;
211 return CAMERA_OK;
212 }
213
GetStreamOperator()214 sptr<Camera::IStreamOperator> HCameraDevice::GetStreamOperator()
215 {
216 return streamOperator_;
217 }
218
OnError(const Camera::ErrorType type,const int32_t errorMsg)219 int32_t HCameraDevice::OnError(const Camera::ErrorType type, const int32_t errorMsg)
220 {
221 if (deviceSvcCallback_ != nullptr) {
222 if (type == Camera::REQUEST_TIMEOUT) {
223 deviceSvcCallback_->OnError(CAMERA_DEVICE_REQUEST_TIMEOUT, errorMsg);
224 } else {
225 deviceSvcCallback_->OnError(CAMERA_UNKNOWN_ERROR, errorMsg);
226 }
227 }
228 return CAMERA_OK;
229 }
230
OnResult(const uint64_t timestamp,const std::shared_ptr<Camera::CameraMetadata> & result)231 int32_t HCameraDevice::OnResult(const uint64_t timestamp,
232 const std::shared_ptr<Camera::CameraMetadata> &result)
233 {
234 if (deviceSvcCallback_ != nullptr) {
235 deviceSvcCallback_->OnResult(timestamp, result);
236 }
237 return CAMERA_OK;
238 }
239
CameraDeviceCallback(sptr<HCameraDevice> hCameraDevice)240 CameraDeviceCallback::CameraDeviceCallback(sptr<HCameraDevice> hCameraDevice)
241 {
242 hCameraDevice_ = hCameraDevice;
243 }
244
OnError(const Camera::ErrorType type,const int32_t errorMsg)245 void CameraDeviceCallback::OnError(const Camera::ErrorType type, const int32_t errorMsg)
246 {
247 if (hCameraDevice_ != nullptr) {
248 hCameraDevice_->OnError(type, errorMsg);
249 }
250 }
251
OnResult(const uint64_t timestamp,const std::shared_ptr<Camera::CameraMetadata> & result)252 void CameraDeviceCallback::OnResult(const uint64_t timestamp,
253 const std::shared_ptr<Camera::CameraMetadata> &result)
254 {
255 if (hCameraDevice_ != nullptr) {
256 hCameraDevice_->OnResult(timestamp, result);
257 }
258 }
259
SetHCameraDevice(sptr<HCameraDevice> hCameraDevice)260 void CameraDeviceCallback::SetHCameraDevice(sptr<HCameraDevice> hCameraDevice)
261 {
262 hCameraDevice_ = hCameraDevice;
263 }
264 } // namespace CameraStandard
265 } // namespace OHOS
266