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 "input/camera_input.h"
17
18 #include <securec.h>
19 #include <cinttypes>
20 #include "camera_device_ability_items.h"
21 #include "camera_util.h"
22 #include "hcamera_device_callback_stub.h"
23 #include "camera_log.h"
24 #include "metadata_utils.h"
25 #include "session/capture_session.h"
26 #include "icamera_util.h"
27
28 namespace OHOS {
29 namespace CameraStandard {
OnError(const int32_t errorType,const int32_t errorMsg)30 int32_t CameraDeviceServiceCallback::OnError(const int32_t errorType, const int32_t errorMsg)
31 {
32 std::lock_guard<std::mutex> lock(deviceCallbackMutex_);
33 MEDIA_ERR_LOG("CameraDeviceServiceCallback::OnError() is called!, errorType: %{public}d, errorMsg: %{public}d",
34 errorType, errorMsg);
35 if (camInput_ != nullptr && camInput_->GetErrorCallback() != nullptr) {
36 int32_t serviceErrorType = ServiceToCameraError(errorType);
37 camInput_->GetErrorCallback()->OnError(serviceErrorType, errorMsg);
38 } else {
39 MEDIA_INFO_LOG("CameraDeviceServiceCallback::ErrorCallback not set!, Discarding callback");
40 }
41 return CAMERA_OK;
42 }
43
OnResult(const uint64_t timestamp,const std::shared_ptr<OHOS::Camera::CameraMetadata> & result)44 int32_t CameraDeviceServiceCallback::OnResult(const uint64_t timestamp,
45 const std::shared_ptr<OHOS::Camera::CameraMetadata> &result)
46 {
47 std::lock_guard<std::mutex> lock(deviceCallbackMutex_);
48 if (camInput_ == nullptr) {
49 MEDIA_ERR_LOG("CameraDeviceServiceCallback::OnResult() camInput_ is null!");
50 return CAMERA_OK;
51 }
52 if (camInput_->GetCameraDeviceInfo() == nullptr) {
53 MEDIA_ERR_LOG("CameraDeviceServiceCallback::OnResult() camInput_->GetCameraDeviceInfo() is null!");
54 } else {
55 MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::OnResult()"
56 "is called!, cameraId: %{public}s, timestamp: %{public}"
57 PRIu64, camInput_->GetCameraDeviceInfo()->GetID().c_str(), timestamp);
58 }
59 if (camInput_->GetResultCallback() != nullptr) {
60 camInput_->GetResultCallback()->OnResult(timestamp, result);
61 } else {
62 MEDIA_INFO_LOG("CameraDeviceServiceCallback::ResultCallback not set!, Discarding callback");
63 }
64 camInput_->ProcessDeviceCallbackUpdates(result);
65 return CAMERA_OK;
66 }
67
68
CameraInput(sptr<ICameraDeviceService> & deviceObj,sptr<CameraDevice> & cameraObj)69 CameraInput::CameraInput(sptr<ICameraDeviceService> &deviceObj,
70 sptr<CameraDevice> &cameraObj) : deviceObj_(deviceObj), cameraObj_(cameraObj)
71 {
72 MEDIA_INFO_LOG("CameraInput::CameraInput Contructor!");
73 if (cameraObj_) {
74 MEDIA_INFO_LOG("CameraInput::CameraInput Contructor Camera: %{public}s", cameraObj_->GetID().c_str());
75 }
76 CameraDeviceSvcCallback_ = new(std::nothrow) CameraDeviceServiceCallback(this);
77 if (deviceObj_) {
78 deviceObj_->SetCallback(CameraDeviceSvcCallback_);
79 } else {
80 MEDIA_ERR_LOG("CameraInput::CameraInput() deviceObj_ is nullptr");
81 }
82 }
83
~CameraInput()84 CameraInput::~CameraInput()
85 {
86 MEDIA_INFO_LOG("CameraInput::CameraInput Destructor!");
87 if (cameraObj_) {
88 MEDIA_INFO_LOG("CameraInput::CameraInput Destructor Camera: %{public}s", cameraObj_->GetID().c_str());
89 }
90 cameraObj_ = nullptr;
91 deviceObj_ = nullptr;
92 CameraDeviceSvcCallback_ = nullptr;
93 CaptureInput::Release();
94 }
95
Open()96 int CameraInput::Open()
97 {
98 std::lock_guard<std::mutex> lock(interfaceMutex_);
99 MEDIA_DEBUG_LOG("Enter Into CameraInput::Open");
100 int32_t retCode = CAMERA_UNKNOWN_ERROR;
101 if (deviceObj_) {
102 retCode = deviceObj_->Open();
103 if (retCode != CAMERA_OK) {
104 MEDIA_ERR_LOG("Failed to open Camera Input, retCode: %{public}d", retCode);
105 }
106 } else {
107 MEDIA_ERR_LOG("CameraInput::Open() deviceObj_ is nullptr");
108 }
109 return ServiceToCameraError(retCode);
110 }
111
Close()112 int CameraInput::Close()
113 {
114 std::lock_guard<std::mutex> lock(interfaceMutex_);
115 MEDIA_DEBUG_LOG("Enter Into CameraInput::Close");
116 int32_t retCode = CAMERA_UNKNOWN_ERROR;
117 if (deviceObj_) {
118 retCode = deviceObj_->Close();
119 if (retCode != CAMERA_OK) {
120 MEDIA_ERR_LOG("Failed to close Camera Input, retCode: %{public}d", retCode);
121 }
122 } else {
123 MEDIA_ERR_LOG("CameraInput::Close() deviceObj_ is nullptr");
124 }
125 cameraObj_ = nullptr;
126 deviceObj_ = nullptr;
127 CameraDeviceSvcCallback_ = nullptr;
128 CaptureInput::Release();
129 return ServiceToCameraError(retCode);
130 }
131
Release()132 int CameraInput::Release()
133 {
134 MEDIA_DEBUG_LOG("Enter Into CameraInput::Release");
135 int32_t retCode = CAMERA_UNKNOWN_ERROR;
136 if (deviceObj_) {
137 retCode = deviceObj_->Release();
138 if (retCode != CAMERA_OK) {
139 MEDIA_ERR_LOG("Failed to release Camera Input, retCode: %{public}d", retCode);
140 }
141 } else {
142 MEDIA_ERR_LOG("CameraInput::Release() deviceObj_ is nullptr");
143 }
144 cameraObj_ = nullptr;
145 deviceObj_ = nullptr;
146 CameraDeviceSvcCallback_ = nullptr;
147 CaptureInput::Release();
148 return ServiceToCameraError(retCode);
149 }
150
SetErrorCallback(std::shared_ptr<ErrorCallback> errorCallback)151 void CameraInput::SetErrorCallback(std::shared_ptr<ErrorCallback> errorCallback)
152 {
153 std::lock_guard<std::mutex> lock(errorCallbackMutex_);
154 if (errorCallback == nullptr) {
155 MEDIA_ERR_LOG("SetErrorCallback: Unregistering error callback");
156 }
157 errorCallback_ = errorCallback;
158 return;
159 }
160
SetResultCallback(std::shared_ptr<ResultCallback> resultCallback)161 void CameraInput::SetResultCallback(std::shared_ptr<ResultCallback> resultCallback)
162 {
163 if (resultCallback == nullptr) {
164 MEDIA_ERR_LOG("SetResultCallback: Unregistering error resultCallback");
165 }
166 MEDIA_DEBUG_LOG("CameraInput::setresult callback");
167 resultCallback_ = resultCallback;
168 return;
169 }
GetCameraId()170 std::string CameraInput::GetCameraId()
171 {
172 return cameraObj_->GetID();
173 }
174
GetCameraDevice()175 sptr<ICameraDeviceService> CameraInput::GetCameraDevice()
176 {
177 return deviceObj_;
178 }
179
GetErrorCallback()180 std::shared_ptr<ErrorCallback> CameraInput::GetErrorCallback()
181 {
182 std::lock_guard<std::mutex> lock(errorCallbackMutex_);
183 return errorCallback_;
184 }
GetResultCallback()185 std::shared_ptr<ResultCallback> CameraInput::GetResultCallback()
186 {
187 MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::GetResultCallback");
188 return resultCallback_;
189 }
GetCameraDeviceInfo()190 sptr<CameraDevice> CameraInput::GetCameraDeviceInfo()
191 {
192 return cameraObj_;
193 }
194
ProcessDeviceCallbackUpdates(const std::shared_ptr<Camera::CameraMetadata> & result)195 void CameraInput::ProcessDeviceCallbackUpdates(const std::shared_ptr<Camera::CameraMetadata> &result)
196 {
197 CaptureSession* captureSession = GetSession();
198 if (captureSession == nullptr) {
199 return;
200 }
201
202 captureSession->ProcessAutoFocusUpdates(result);
203 }
204
UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata)205 int32_t CameraInput::UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata)
206 {
207 CAMERA_SYNC_TRACE;
208 int32_t ret = CAMERA_OK;
209 if (!OHOS::Camera::GetCameraMetadataItemCount(changedMetadata->get())) {
210 MEDIA_INFO_LOG("CameraInput::UpdateSetting No configuration to update");
211 return ret;
212 }
213
214 if (deviceObj_) {
215 ret = deviceObj_->UpdateSetting(changedMetadata);
216 } else {
217 MEDIA_ERR_LOG("CameraInput::UpdateSetting() deviceObj_ is nullptr");
218 }
219 if (ret != CAMERA_OK) {
220 MEDIA_ERR_LOG("CameraInput::UpdateSetting Failed to update settings");
221 return ret;
222 }
223
224 size_t length;
225 uint32_t count = changedMetadata->get()->item_count;
226 uint8_t* data = OHOS::Camera::GetMetadataData(changedMetadata->get());
227 camera_metadata_item_entry_t* itemEntry = OHOS::Camera::GetMetadataItems(changedMetadata->get());
228 std::shared_ptr<OHOS::Camera::CameraMetadata> baseMetadata = cameraObj_->GetMetadata();
229 for (uint32_t i = 0; i < count; i++, itemEntry++) {
230 bool status = false;
231 camera_metadata_item_t item;
232 length = OHOS::Camera::CalculateCameraMetadataItemDataSize(itemEntry->data_type, itemEntry->count);
233 ret = OHOS::Camera::FindCameraMetadataItem(baseMetadata->get(), itemEntry->item, &item);
234 if (ret == CAM_META_SUCCESS) {
235 status = baseMetadata->updateEntry(itemEntry->item,
236 (length == 0) ? itemEntry->data.value : (data + itemEntry->data.offset),
237 itemEntry->count);
238 } else if (ret == CAM_META_ITEM_NOT_FOUND) {
239 status = baseMetadata->addEntry(itemEntry->item,
240 (length == 0) ? itemEntry->data.value : (data + itemEntry->data.offset),
241 itemEntry->count);
242 }
243 if (!status) {
244 MEDIA_ERR_LOG("CameraInput::UpdateSetting Failed to add/update metadata item: %{public}d",
245 itemEntry->item);
246 }
247 }
248 return CAMERA_OK;
249 }
250
GetCameraSettings()251 std::string CameraInput::GetCameraSettings()
252 {
253 return OHOS::Camera::MetadataUtils::EncodeToString(cameraObj_->GetMetadata());
254 }
255
SetCameraSettings(std::string setting)256 int32_t CameraInput::SetCameraSettings(std::string setting)
257 {
258 std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = OHOS::Camera::MetadataUtils::DecodeFromString(setting);
259 if (metadata == nullptr) {
260 MEDIA_ERR_LOG("CameraInput::SetCameraSettings Failed to decode metadata setting from string");
261 return CAMERA_INVALID_ARG;
262 }
263 return UpdateSetting(metadata);
264 }
265 } // namespace CameraStandard
266 } // namespace OHOS
267