• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (errorCallback == nullptr) {
154         MEDIA_ERR_LOG("SetErrorCallback: Unregistering error callback");
155     }
156     errorCallback_ = errorCallback;
157     return;
158 }
159 
SetResultCallback(std::shared_ptr<ResultCallback> resultCallback)160 void CameraInput::SetResultCallback(std::shared_ptr<ResultCallback> resultCallback)
161 {
162     if (resultCallback == nullptr) {
163         MEDIA_ERR_LOG("SetResultCallback: Unregistering error resultCallback");
164     }
165     MEDIA_DEBUG_LOG("CameraInput::setresult callback");
166     resultCallback_ = resultCallback;
167     return;
168 }
GetCameraId()169 std::string CameraInput::GetCameraId()
170 {
171     return cameraObj_->GetID();
172 }
173 
GetCameraDevice()174 sptr<ICameraDeviceService> CameraInput::GetCameraDevice()
175 {
176     return deviceObj_;
177 }
178 
GetErrorCallback()179 std::shared_ptr<ErrorCallback> CameraInput::GetErrorCallback()
180 {
181     return errorCallback_;
182 }
GetResultCallback()183 std::shared_ptr<ResultCallback> CameraInput::GetResultCallback()
184 {
185     MEDIA_DEBUG_LOG("CameraDeviceServiceCallback::GetResultCallback");
186     return resultCallback_;
187 }
GetCameraDeviceInfo()188 sptr<CameraDevice> CameraInput::GetCameraDeviceInfo()
189 {
190     return cameraObj_;
191 }
192 
ProcessDeviceCallbackUpdates(const std::shared_ptr<Camera::CameraMetadata> & result)193 void CameraInput::ProcessDeviceCallbackUpdates(const std::shared_ptr<Camera::CameraMetadata> &result)
194 {
195     CaptureSession* captureSession = GetSession();
196     if (captureSession == nullptr) {
197         return;
198     }
199 
200     captureSession->ProcessAutoFocusUpdates(result);
201 }
202 
UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata)203 int32_t CameraInput::UpdateSetting(std::shared_ptr<OHOS::Camera::CameraMetadata> changedMetadata)
204 {
205     CAMERA_SYNC_TRACE;
206     int32_t ret = CAMERA_OK;
207     if (!OHOS::Camera::GetCameraMetadataItemCount(changedMetadata->get())) {
208         MEDIA_INFO_LOG("CameraInput::UpdateSetting No configuration to update");
209         return ret;
210     }
211 
212     if (deviceObj_) {
213         ret = deviceObj_->UpdateSetting(changedMetadata);
214     } else {
215         MEDIA_ERR_LOG("CameraInput::UpdateSetting() deviceObj_ is nullptr");
216     }
217     if (ret != CAMERA_OK) {
218         MEDIA_ERR_LOG("CameraInput::UpdateSetting Failed to update settings");
219         return ret;
220     }
221 
222     size_t length;
223     uint32_t count = changedMetadata->get()->item_count;
224     uint8_t* data = OHOS::Camera::GetMetadataData(changedMetadata->get());
225     camera_metadata_item_entry_t* itemEntry = OHOS::Camera::GetMetadataItems(changedMetadata->get());
226     std::shared_ptr<OHOS::Camera::CameraMetadata> baseMetadata = cameraObj_->GetMetadata();
227     for (uint32_t i = 0; i < count; i++, itemEntry++) {
228         bool status = false;
229         camera_metadata_item_t item;
230         length = OHOS::Camera::CalculateCameraMetadataItemDataSize(itemEntry->data_type, itemEntry->count);
231         ret = OHOS::Camera::FindCameraMetadataItem(baseMetadata->get(), itemEntry->item, &item);
232         if (ret == CAM_META_SUCCESS) {
233             status = baseMetadata->updateEntry(itemEntry->item,
234                                                (length == 0) ? itemEntry->data.value : (data + itemEntry->data.offset),
235                                                itemEntry->count);
236         } else if (ret == CAM_META_ITEM_NOT_FOUND) {
237             status = baseMetadata->addEntry(itemEntry->item,
238                                             (length == 0) ? itemEntry->data.value : (data + itemEntry->data.offset),
239                                             itemEntry->count);
240         }
241         if (!status) {
242             MEDIA_ERR_LOG("CameraInput::UpdateSetting Failed to add/update metadata item: %{public}d",
243                           itemEntry->item);
244         }
245     }
246     return CAMERA_OK;
247 }
248 
GetCameraSettings()249 std::string CameraInput::GetCameraSettings()
250 {
251     return OHOS::Camera::MetadataUtils::EncodeToString(cameraObj_->GetMetadata());
252 }
253 
SetCameraSettings(std::string setting)254 int32_t CameraInput::SetCameraSettings(std::string setting)
255 {
256     std::shared_ptr<OHOS::Camera::CameraMetadata> metadata = OHOS::Camera::MetadataUtils::DecodeFromString(setting);
257     if (metadata == nullptr) {
258         MEDIA_ERR_LOG("CameraInput::SetCameraSettings Failed to decode metadata setting from string");
259         return CAMERA_INVALID_ARG;
260     }
261     return UpdateSetting(metadata);
262 }
263 } // namespace CameraStandard
264 } // namespace OHOS
265