1 /*
2 * Copyright (c) 2021 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 "dcamera_handler.h"
17
18 #include <functional>
19
20 #include "anonymous_string.h"
21 #include "avcodec_info.h"
22 #include "avcodec_list.h"
23 #include "dcamera_manager_callback.h"
24 #include "dcamera_utils_tools.h"
25 #include "distributed_camera_constants.h"
26 #include "distributed_camera_errno.h"
27 #include "distributed_hardware_log.h"
28 #include "metadata_utils.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 IMPLEMENT_SINGLE_INSTANCE(DCameraHandler);
33
~DCameraHandler()34 DCameraHandler::~DCameraHandler()
35 {
36 DHLOGI("~DCameraHandlerCommon");
37 }
38
Initialize()39 int32_t DCameraHandler::Initialize()
40 {
41 DHLOGI("DCameraHandlerCommon::Initialize");
42 cameraManager_ = CameraStandard::CameraManager::GetInstance();
43 if (cameraManager_ == nullptr) {
44 DHLOGE("DCameraHandlerCommon::Initialize cameraManager getInstance failed");
45 return DCAMERA_INIT_ERR;
46 }
47 std::shared_ptr<DCameraManagerCallback> cameraMgrCallback = std::make_shared<DCameraManagerCallback>();
48 cameraManager_->SetCallback(cameraMgrCallback);
49 DHLOGI("DCameraHandlerCommon::Initialize success");
50 return DCAMERA_OK;
51 }
52
Query()53 std::vector<DHItem> DCameraHandler::Query()
54 {
55 std::vector<DHItem> itemList;
56 std::vector<sptr<CameraStandard::CameraDevice>> cameraList = cameraManager_->GetSupportedCameras();
57 DHLOGI("DCameraHandlerCommon::Query get %d cameras", cameraList.size());
58 if (cameraList.empty()) {
59 DHLOGE("DCameraHandlerCommon::Query no camera device");
60 return itemList;
61 }
62 for (auto& info : cameraList) {
63 if (info->GetConnectionType() != CameraStandard::ConnectionType::CAMERA_CONNECTION_BUILT_IN) {
64 DHLOGI("DCameraHandlerCommon::Query connection type: %d", info->GetConnectionType());
65 continue;
66 }
67 DHLOGI("DCameraHandlerCommon::Query get %s, position: %d, cameraType: %d",
68 GetAnonyString(info->GetID()).c_str(), info->GetPosition(), info->GetCameraType());
69 DHItem item;
70 if (CreateDHItem(info, item) == DCAMERA_OK) {
71 itemList.emplace_back(item);
72 }
73 }
74 DHLOGI("DCameraHandlerCommon::Query success, get %d items", itemList.size());
75 return itemList;
76 }
77
QueryExtraInfo()78 std::map<std::string, std::string> DCameraHandler::QueryExtraInfo()
79 {
80 DHLOGI("DCameraHandler::QueryExtraInfo");
81 std::map<std::string, std::string> extraInfo;
82 return extraInfo;
83 }
84
IsSupportPlugin()85 bool DCameraHandler::IsSupportPlugin()
86 {
87 DHLOGI("DCameraHandler::IsSupportPlugin");
88 return false;
89 }
90
RegisterPluginListener(std::shared_ptr<PluginListener> listener)91 void DCameraHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
92 {
93 DHLOGI("DCameraHandler::RegisterPluginListener");
94 if (listener == nullptr) {
95 DHLOGE("DCameraHandler unregistering plugin listener");
96 }
97 pluginListener_ = listener;
98 }
99
UnRegisterPluginListener()100 void DCameraHandler::UnRegisterPluginListener()
101 {
102 DHLOGI("DCameraHandler::UnRegisterPluginListener");
103 pluginListener_ = nullptr;
104 }
105
GetCameras()106 std::vector<std::string> DCameraHandler::GetCameras()
107 {
108 std::vector<std::string> cameras;
109 std::vector<sptr<CameraStandard::CameraDevice>> cameraList = cameraManager_->GetSupportedCameras();
110 DHLOGI("DCameraHandlerCommon::GetCameras get %d cameras", cameraList.size());
111 if (cameraList.empty()) {
112 DHLOGE("DCameraHandlerCommon::GetCameras no camera device");
113 return cameras;
114 }
115 for (auto& info : cameraList) {
116 sptr<CameraStandard::CameraOutputCapability> capability = cameraManager_->GetSupportedOutputCapability(info);
117 if (capability == nullptr) {
118 DHLOGI("DCameraHandlerCommon::GetCameras get supported capability is null");
119 continue;
120 }
121 if (info->GetConnectionType() != CameraStandard::ConnectionType::CAMERA_CONNECTION_BUILT_IN) {
122 DHLOGI("DCameraHandlerCommon::GetCameras connection type: %d", info->GetConnectionType());
123 continue;
124 }
125 DHLOGI("DCameraHandlerCommon::GetCameras get %s, position: %d, cameraType: %d",
126 GetAnonyString(info->GetID()).c_str(), info->GetPosition(), info->GetCameraType());
127 std::string dhId = CAMERA_ID_PREFIX + info->GetID();
128 cameras.emplace_back(dhId);
129 }
130 DHLOGI("DCameraHandlerCommon::GetCameras success, get %d items", cameras.size());
131 return cameras;
132 }
133
CreateDHItem(sptr<CameraStandard::CameraDevice> & info,DHItem & item)134 int32_t DCameraHandler::CreateDHItem(sptr<CameraStandard::CameraDevice>& info, DHItem& item)
135 {
136 std::string id = info->GetID();
137 item.dhId = CAMERA_ID_PREFIX + id;
138 DHLOGI("DCameraHandlerCommon::CreateDHItem camera id: %s", GetAnonyString(id).c_str());
139
140 Json::Value root;
141 root[CAMERA_PROTOCOL_VERSION_KEY] = Json::Value(CAMERA_PROTOCOL_VERSION_VALUE);
142 root[CAMERA_POSITION_KEY] = Json::Value(GetCameraPosition(info->GetPosition()));
143 root[CAMERA_CODEC_TYPE_KEY].append("OMX_hisi_video_encoder_avc");
144
145 sptr<CameraStandard::CameraInput> cameraInput = nullptr;
146 int rv = cameraManager_->CreateCameraInput(info, &cameraInput);
147 if (rv != DCAMERA_OK) {
148 DHLOGE("DCameraHandlerCommon::CreateDHItem create cameraInput failed");
149 return DCAMERA_BAD_VALUE;
150 }
151
152 sptr<CameraStandard::CameraOutputCapability> capability = cameraManager_->GetSupportedOutputCapability(info);
153 if (capability == nullptr) {
154 DHLOGI("DCameraHandlerCommon::CreateDHItem get supported capability is null");
155 return DCAMERA_BAD_VALUE;
156 }
157 std::vector<CameraStandard::Profile> photoProfiles = capability->GetPhotoProfiles();
158 ConfigFormatAndResolution(SNAPSHOT_FRAME, root, photoProfiles);
159
160 std::vector<CameraStandard::Profile> previewProfiles = capability->GetPreviewProfiles();
161 ConfigFormatAndResolution(CONTINUOUS_FRAME, root, previewProfiles);
162
163 std::hash<std::string> h;
164 std::string abilityString = cameraInput->GetCameraSettings();
165 DHLOGI("DCameraHandlerCommon::CreateDHItem abilityString hash: %zu, length: %zu",
166 h(abilityString), abilityString.length());
167
168 std::string encodeString = Base64Encode(reinterpret_cast<const unsigned char *>(abilityString.c_str()),
169 abilityString.length());
170 DHLOGI("DCameraHandlerCommon::CreateDHItem encodeString hash: %zu, length: %zu",
171 h(encodeString), encodeString.length());
172 root[CAMERA_METADATA_KEY] = Json::Value(encodeString);
173
174 item.attrs = root.toStyledString();
175 if (cameraInput->Release() != DCAMERA_OK) {
176 DHLOGE("DCameraHandlerCommon::CreateDHItem cameraInput Release failed");
177 }
178 return DCAMERA_OK;
179 }
180
GetCameraPosition(CameraStandard::CameraPosition position)181 std::string DCameraHandler::GetCameraPosition(CameraStandard::CameraPosition position)
182 {
183 DHLOGI("DCameraHandler::GetCameraPosition position: %d", position);
184 std::string ret = "";
185 switch (position) {
186 case CameraStandard::CameraPosition::CAMERA_POSITION_BACK: {
187 ret = CAMERA_POSITION_BACK;
188 break;
189 }
190 case CameraStandard::CameraPosition::CAMERA_POSITION_FRONT: {
191 ret = CAMERA_POSITION_FRONT;
192 break;
193 }
194 case CameraStandard::CameraPosition::CAMERA_POSITION_UNSPECIFIED: {
195 ret = CAMERA_POSITION_UNSPECIFIED;
196 break;
197 }
198 default: {
199 DHLOGE("DCameraHandler::GetCameraPosition unknown camera position");
200 break;
201 }
202 }
203 DHLOGI("DCameraHandler::GetCameraPosition success ret: %s", ret.c_str());
204 return ret;
205 }
206
ConfigFormatAndResolution(const DCStreamType type,Json::Value & root,std::vector<CameraStandard::Profile> & profileList)207 void DCameraHandler::ConfigFormatAndResolution(const DCStreamType type, Json::Value& root,
208 std::vector<CameraStandard::Profile>& profileList)
209 {
210 DHLOGI("DCameraHandler::ConfigFormatAndResolution camera Profile size: %d", profileList.size());
211 std::set<int32_t> formatSet;
212 for (auto& profile : profileList) {
213 CameraStandard::CameraFormat format = profile.GetCameraFormat();
214 CameraStandard::Size picSize = profile.GetSize();
215 int32_t dformat = CovertToDcameraFormat(format);
216 formatSet.insert(dformat);
217 DHLOGI("DCameraHandler::ConfigFormatAndResolution, width: %d, height: %d, format: %d",
218 picSize.width, picSize.height, dformat);
219 std::string formatName = std::to_string(dformat);
220 if (IsValid(type, picSize)) {
221 std::string resolutionValue = std::to_string(picSize.width) + "*" + std::to_string(picSize.height);
222 if (type == SNAPSHOT_FRAME) {
223 root[CAMERA_FORMAT_PHOTO][CAMERA_RESOLUTION_KEY][formatName].append(resolutionValue);
224 } else if (type == CONTINUOUS_FRAME) {
225 root[CAMERA_FORMAT_PREVIEW][CAMERA_RESOLUTION_KEY][formatName].append(resolutionValue);
226 root[CAMERA_FORMAT_VIDEO][CAMERA_RESOLUTION_KEY][formatName].append(resolutionValue);
227 }
228 }
229 }
230
231 for (auto format : formatSet) {
232 if (type == SNAPSHOT_FRAME) {
233 root[CAMERA_FORMAT_PHOTO][CAMERA_FORMAT_KEY].append(format);
234 } else if (type == CONTINUOUS_FRAME) {
235 root[CAMERA_FORMAT_PREVIEW][CAMERA_FORMAT_KEY].append(format);
236 root[CAMERA_FORMAT_VIDEO][CAMERA_FORMAT_KEY].append(format);
237 }
238 }
239 }
240
CovertToDcameraFormat(CameraStandard::CameraFormat format)241 int32_t DCameraHandler::CovertToDcameraFormat(CameraStandard::CameraFormat format)
242 {
243 DHLOGI("DCameraHandler::CovertToDcameraFormat format: %d", format);
244 int32_t ret = -1;
245 switch (format) {
246 case CameraStandard::CameraFormat::CAMERA_FORMAT_RGBA_8888:
247 ret = camera_format_t::OHOS_CAMERA_FORMAT_RGBA_8888;
248 break;
249 case CameraStandard::CameraFormat::CAMERA_FORMAT_YCBCR_420_888:
250 ret = camera_format_t::OHOS_CAMERA_FORMAT_YCBCR_420_888;
251 break;
252 case CameraStandard::CameraFormat::CAMERA_FORMAT_YUV_420_SP:
253 ret = camera_format_t::OHOS_CAMERA_FORMAT_YCRCB_420_SP;
254 break;
255 case CameraStandard::CameraFormat::CAMERA_FORMAT_JPEG:
256 ret = camera_format_t::OHOS_CAMERA_FORMAT_JPEG;
257 break;
258 default:
259 DHLOGE("DCameraHandler::CovertToDcameraFormat invalid camera format");
260 break;
261 }
262 return ret;
263 }
264
IsValid(const DCStreamType type,const CameraStandard::Size & size)265 bool DCameraHandler::IsValid(const DCStreamType type, const CameraStandard::Size& size)
266 {
267 bool ret = false;
268 switch (type) {
269 case CONTINUOUS_FRAME: {
270 ret = (size.width >= RESOLUTION_MIN_WIDTH) &&
271 (size.height >= RESOLUTION_MIN_HEIGHT) &&
272 (size.width <= RESOLUTION_MAX_WIDTH_CONTINUOUS) &&
273 (size.height <= RESOLUTION_MAX_HEIGHT_CONTINUOUS);
274 break;
275 }
276 case SNAPSHOT_FRAME: {
277 ret = (size.width >= RESOLUTION_MIN_WIDTH) &&
278 (size.height >= RESOLUTION_MIN_HEIGHT) &&
279 (size.width <= RESOLUTION_MAX_WIDTH_SNAPSHOT) &&
280 (size.height <= RESOLUTION_MAX_HEIGHT_SNAPSHOT);
281 break;
282 }
283 default: {
284 DHLOGE("DCameraHandler::isValid unknown stream type");
285 break;
286 }
287 }
288 return ret;
289 }
290
GetHardwareHandler()291 IHardwareHandler* GetHardwareHandler()
292 {
293 DHLOGI("DCameraHandler::GetHardwareHandler");
294 return &DCameraHandler::GetInstance();
295 }
296 } // namespace DistributedHardware
297 } // namespace OHOS