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