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 "anonymous_string.h"
19 #include "avcodec_info.h"
20 #include "avcodec_list.h"
21 #include "dcamera_manager_callback.h"
22 #include "dcamera_utils_tools.h"
23 #include "distributed_camera_constants.h"
24 #include "distributed_camera_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "metadata_utils.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 IMPLEMENT_SINGLE_INSTANCE(DCameraHandler);
31
~DCameraHandler()32 DCameraHandler::~DCameraHandler()
33 {
34 DHLOGI("~DCameraHandler");
35 }
36
Initialize()37 int32_t DCameraHandler::Initialize()
38 {
39 DHLOGI("DCameraHandler::Initialize");
40 cameraManager_ = CameraStandard::CameraManager::GetInstance();
41 if (cameraManager_ == nullptr) {
42 DHLOGE("DCameraHandler::Initialize cameraManager getInstance failed");
43 return DCAMERA_INIT_ERR;
44 }
45 std::shared_ptr<DCameraManagerCallback> cameraMgrCallback = std::make_shared<DCameraManagerCallback>();
46 cameraManager_->SetCallback(cameraMgrCallback);
47 DHLOGI("DCameraHandler::Initialize success");
48 return DCAMERA_OK;
49 }
50
Query()51 std::vector<DHItem> DCameraHandler::Query()
52 {
53 std::vector<DHItem> itemList;
54 std::vector<sptr<CameraStandard::CameraInfo>> cameraList = cameraManager_->GetCameras();
55 DHLOGI("DCameraHandler::Query get %d cameras", cameraList.size());
56 if (cameraList.empty()) {
57 DHLOGE("DCameraHandler::Query no camera device");
58 return itemList;
59 }
60 for (auto& info : cameraList) {
61 if (info->GetConnectionType() != OHOS_CAMERA_CONNECTION_TYPE_BUILTIN) {
62 DHLOGI("DCameraHandler::Query connection type: %d", info->GetConnectionType());
63 continue;
64 }
65 if ((info->GetPosition() == OHOS_CAMERA_POSITION_FRONT) ||
66 (info->GetPosition() == OHOS_CAMERA_POSITION_BACK && info->GetCameraType() == OHOS_CAMERA_TYPE_LOGICAL)) {
67 DHItem item = CreateDHItem(info);
68 itemList.push_back(item);
69 }
70 }
71 DHLOGI("DCameraHandler::Query success, get %d items", itemList.size());
72 return itemList;
73 }
74
QueryExtraInfo()75 std::map<std::string, std::string> DCameraHandler::QueryExtraInfo()
76 {
77 DHLOGI("DCameraHandler::QueryExtraInfo");
78 std::map<std::string, std::string> extraInfo;
79 return extraInfo;
80 }
81
IsSupportPlugin()82 bool DCameraHandler::IsSupportPlugin()
83 {
84 DHLOGI("DCameraHandler::IsSupportPlugin");
85 return false;
86 }
87
RegisterPluginListener(std::shared_ptr<PluginListener> listener)88 void DCameraHandler::RegisterPluginListener(std::shared_ptr<PluginListener> listener)
89 {
90 DHLOGI("DCameraHandler::RegisterPluginListener");
91 if (listener == nullptr) {
92 DHLOGE("DCameraHandler unregistering plugin listener");
93 }
94 pluginListener_ = listener;
95 }
96
GetCameras()97 std::vector<std::string> DCameraHandler::GetCameras()
98 {
99 std::vector<std::string> cameras;
100 std::vector<sptr<CameraStandard::CameraInfo>> cameraList = cameraManager_->GetCameras();
101 DHLOGI("DCameraHandler::GetCameras get %d cameras", cameraList.size());
102 if (cameraList.empty()) {
103 DHLOGE("DCameraHandler::GetCameras no camera device");
104 return cameras;
105 }
106 for (auto& info : cameraList) {
107 if (info->GetConnectionType() != OHOS_CAMERA_CONNECTION_TYPE_BUILTIN) {
108 DHLOGI("DCameraHandler::GetCameras connection type: %d", info->GetConnectionType());
109 continue;
110 }
111 if ((info->GetPosition() == OHOS_CAMERA_POSITION_FRONT) ||
112 (info->GetPosition() == OHOS_CAMERA_POSITION_BACK && info->GetCameraType() == OHOS_CAMERA_TYPE_LOGICAL)) {
113 std::string dhId = CAMERA_ID_PREFIX + info->GetID();
114 cameras.push_back(dhId);
115 }
116 }
117 DHLOGI("DCameraHandler::GetCameras success, get %d items", cameras.size());
118 return cameras;
119 }
120
CreateDHItem(sptr<CameraStandard::CameraInfo> & info)121 DHItem DCameraHandler::CreateDHItem(sptr<CameraStandard::CameraInfo>& info)
122 {
123 DHItem item;
124 std::string id = info->GetID();
125 item.dhId = CAMERA_ID_PREFIX + id;
126 DHLOGI("DCameraHandler::CreateDHItem camera id: %s", GetAnonyString(id).c_str());
127
128 Json::Value root;
129 root[CAMERA_PROTOCOL_VERSION_KEY] = Json::Value(CAMERA_PROTOCOL_VERSION_VALUE);
130 root[CAMERA_POSITION_KEY] = Json::Value(GetCameraPosition(info->GetPosition()));
131
132 std::shared_ptr<Media::AVCodecList> avCodecList = Media::AVCodecListFactory::CreateAVCodecList();
133 std::vector<std::shared_ptr<Media::VideoCaps>> videoCapsList = avCodecList->GetVideoEncoderCaps();
134 for (auto& videoCaps : videoCapsList) {
135 std::shared_ptr<Media::AVCodecInfo> codecInfo = videoCaps->GetCodecInfo();
136 std::string name = codecInfo->GetName();
137 root[CAMERA_CODEC_TYPE_KEY].append(name);
138 DHLOGI("DCameraHandler::CreateDHItem codec type: %s", name.c_str());
139 }
140
141 sptr<CameraStandard::CameraInput> cameraInput = cameraManager_->CreateCameraInput(info);
142 if (cameraInput == nullptr) {
143 DHLOGE("DCameraHandler::CreateDHItem create cameraInput failed");
144 return item;
145 }
146
147 Json::Value outputFormat;
148 Json::Value resolution;
149 std::set<camera_format_t> formatSet;
150
151 std::vector<camera_format_t> videoFormats = cameraInput->GetSupportedVideoFormats();
152 ConfigInfo videoConfig = {CONTINUOUS_FRAME, CAMERA_FORMAT_VIDEO, cameraInput};
153 ConfigFormatAndResolution(videoConfig, outputFormat, resolution, videoFormats, formatSet);
154
155 std::vector<camera_format_t> previewFormats = cameraInput->GetSupportedPreviewFormats();
156 ConfigInfo previewInfo = {CONTINUOUS_FRAME, CAMERA_FORMAT_PREVIEW, cameraInput};
157 ConfigFormatAndResolution(previewInfo, outputFormat, resolution, previewFormats, formatSet);
158
159 std::vector<camera_format_t> photoFormats = cameraInput->GetSupportedPhotoFormats();
160 ConfigInfo photoConfig = {SNAPSHOT_FRAME, CAMERA_FORMAT_PHOTO, cameraInput};
161 ConfigFormatAndResolution(photoConfig, outputFormat, resolution, photoFormats, formatSet);
162
163 root[CAMERA_FORMAT_KEY] = outputFormat;
164 root[CAMERA_RESOLUTION_KEY] = resolution;
165
166 std::string abilityString = cameraInput->GetCameraSettings();
167 std::string encodeString = Base64Encode(reinterpret_cast<const unsigned char *>(abilityString.c_str()),
168 abilityString.length());
169 root[CAMERA_METADATA_KEY] = Json::Value(encodeString);
170
171 item.attrs = root.toStyledString();
172 cameraInput->Release();
173 return item;
174 }
175
GetCameraPosition(camera_position_enum_t position)176 std::string DCameraHandler::GetCameraPosition(camera_position_enum_t position)
177 {
178 DHLOGI("DCameraHandler::GetCameraPosition position: %d", position);
179 std::string ret = "";
180 switch (position) {
181 case OHOS_CAMERA_POSITION_BACK: {
182 ret = CAMERA_POSITION_BACK;
183 break;
184 }
185 case OHOS_CAMERA_POSITION_FRONT: {
186 ret = CAMERA_POSITION_FRONT;
187 break;
188 }
189 case OHOS_CAMERA_POSITION_OTHER: {
190 ret = CAMERA_POSITION_UNSPECIFIED;
191 break;
192 }
193 default: {
194 DHLOGE("DCameraHandler::GetCameraPosition unknown camera position");
195 break;
196 }
197 }
198 DHLOGI("DCameraHandler::GetCameraPosition success ret: %s", ret.c_str());
199 return ret;
200 }
201
ConfigFormatAndResolution(ConfigInfo & info,Json::Value & outputFormat,Json::Value & resolution,std::vector<camera_format_t> & formatList,std::set<camera_format_t> & formatSet)202 void DCameraHandler::ConfigFormatAndResolution(ConfigInfo& info, Json::Value& outputFormat, Json::Value& resolution,
203 std::vector<camera_format_t>& formatList, std::set<camera_format_t>& formatSet)
204 {
205 DHLOGI("DCameraHandler::ConfigFormatAndResolution camera format size: %d", formatList.size());
206 for (auto& format : formatList) {
207 DHLOGI("DCameraHandler::ConfigFormatAndResolution %s format: %d", info.formatKey.c_str(), format);
208 outputFormat[info.formatKey].append(format);
209 if (formatSet.insert(format).second) {
210 std::vector<CameraStandard::CameraPicSize> picSizes = info.cameraInput->getSupportedSizes(format);
211 std::string keyName = std::to_string(format);
212 DHLOGI("DCameraHandler::ConfigFormatAndResolution get %d supported camera pic size", picSizes.size());
213 for (auto& size : picSizes) {
214 if (IsValid(info.type, size)) {
215 std::string value = std::to_string(size.width) + "*" + std::to_string(size.height);
216 resolution[keyName].append(value);
217 DHLOGI("DCameraHandler::ConfigFormatAndResolution format %d resolution %s", format, value.c_str());
218 }
219 }
220 }
221 }
222 }
223
IsValid(DCStreamType type,CameraStandard::CameraPicSize & size)224 bool DCameraHandler::IsValid(DCStreamType type, CameraStandard::CameraPicSize& size)
225 {
226 bool ret = false;
227 switch (type) {
228 case CONTINUOUS_FRAME: {
229 ret = (size.width >= RESOLUTION_MIN_WIDTH) &&
230 (size.height >= RESOLUTION_MIN_HEIGHT) &&
231 (size.width <= RESOLUTION_MAX_WIDTH_CONTINUOUS) &&
232 (size.height <= RESOLUTION_MAX_HEIGHT_CONTINUOUS);
233 break;
234 }
235 case SNAPSHOT_FRAME: {
236 ret = (size.width >= RESOLUTION_MIN_WIDTH) &&
237 (size.height >= RESOLUTION_MIN_HEIGHT) &&
238 (size.width <= RESOLUTION_MAX_WIDTH_SNAPSHOT) &&
239 (size.height <= RESOLUTION_MAX_HEIGHT_SNAPSHOT);
240 break;
241 }
242 default: {
243 DHLOGE("DCameraHandler::isValid unknown stream type");
244 break;
245 }
246 }
247 return ret;
248 }
249
GetHardwareHandler()250 IHardwareHandler* GetHardwareHandler()
251 {
252 DHLOGI("DCameraHandler::GetHardwareHandler");
253 return &DCameraHandler::GetInstance();
254 }
255 } // namespace DistributedHardware
256 } // namespace OHOS