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