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 "dstream_operator.h"
17 #include "dbuffer_manager.h"
18 #include "dcamera_provider.h"
19 #include "dcamera.h"
20 #include "distributed_hardware_log.h"
21 #include "metadata_utils.h"
22 #include "constants.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
DStreamOperator(std::shared_ptr<DMetadataProcessor> & dMetadataProcessor)26 DStreamOperator::DStreamOperator(std::shared_ptr<DMetadataProcessor> &dMetadataProcessor)
27 : dMetadataProcessor_(dMetadataProcessor)
28 {
29 DHLOGI("DStreamOperator construct");
30 }
31
IsStreamsSupported(OperationMode mode,const std::vector<uint8_t> & modeSetting,const std::vector<StreamInfo> & infos,StreamSupportType & type)32 int32_t DStreamOperator::IsStreamsSupported(OperationMode mode, const std::vector<uint8_t> &modeSetting,
33 const std::vector<StreamInfo> &infos, StreamSupportType &type)
34 {
35 if (IsStreamInfosInvalid(infos)) {
36 DHLOGE("DStreamOperator::IsStreamsSupported, input stream infos is invalid.");
37 return CamRetCode::INVALID_ARGUMENT;
38 }
39
40 (void)mode;
41 (void)modeSetting;
42 type = DYNAMIC_SUPPORTED;
43
44 for (const auto &it : infos) {
45 int id = it.streamId_;
46 if (FindHalStreamById(id) != nullptr) {
47 DHLOGE("Repeat streamId.");
48 return CamRetCode::INVALID_ARGUMENT;
49 }
50 }
51 return CamRetCode::NO_ERROR;
52 }
53
CreateStreams(const std::vector<StreamInfo> & streamInfos)54 int32_t DStreamOperator::CreateStreams(const std::vector<StreamInfo> &streamInfos)
55 {
56 if (IsStreamInfosInvalid(streamInfos)) {
57 DHLOGE("DStreamOperator::CreateStreams, input stream Infos is invalid.");
58 return CamRetCode::INVALID_ARGUMENT;
59 }
60
61 DHLOGI("DStreamOperator::CreateStreams, input stream info size=%d.", streamInfos.size());
62
63 for (const auto &info : streamInfos) {
64 DHLOGI("DStreamOperator::CreateStreams, streamInfo: id=%d, intent=%d, width=%d, height=%d, format=%d, " +
65 "dataspace=%d, encodeType=%d", info.streamId_, info.intent_, info.width_, info.height_, info.format_,
66 info.dataspace_, info.encodeType_);
67 if (FindHalStreamById(info.streamId_) != nullptr) {
68 return CamRetCode::INVALID_ARGUMENT;
69 }
70 if (!info.tunneledMode_) {
71 return CamRetCode::METHOD_NOT_SUPPORTED;
72 }
73
74 std::shared_ptr<DCameraStream> dcStream = std::make_shared<DCameraStream>();
75 DCamRetCode ret = dcStream->InitDCameraStream(info);
76 if (ret != SUCCESS) {
77 DHLOGE("Init distributed camera stream failed.");
78 return CamRetCode::INVALID_ARGUMENT;
79 }
80 InsertHalStream(info.streamId_, dcStream);
81
82 std::shared_ptr<DCStreamInfo> dcStreamInfo = std::make_shared<DCStreamInfo>();
83 ConvertStreamInfo(info, dcStreamInfo);
84 InsertDCStream(info.streamId_, dcStreamInfo);
85 DHLOGI("DStreamOperator::CreateStreams, dcStreamInfo: id=%d, type=%d, width=%d, height=%d, format=%d, " +
86 "dataspace=%d, encodeType=%d", dcStreamInfo->streamId_, dcStreamInfo->type_, dcStreamInfo->width_,
87 dcStreamInfo->height_, dcStreamInfo->format_, dcStreamInfo->dataspace_, dcStreamInfo->encodeType_);
88 }
89 DHLOGI("DStreamOperator::Create distributed camera streams success.");
90 return CamRetCode::NO_ERROR;
91 }
92
ReleaseStreams(const std::vector<int32_t> & streamIds)93 int32_t DStreamOperator::ReleaseStreams(const std::vector<int32_t> &streamIds)
94 {
95 if (streamIds.empty() || streamIds.size() > CONTAINER_CAPACITY_MAX_SIZE) {
96 DHLOGE("DStreamOperator::ReleaseStreams, input streamIds is invalid.");
97 return CamRetCode::INVALID_ARGUMENT;
98 }
99
100 DHLOGI("DStreamOperator::ReleaseStreams, input stream id list size=%d.", streamIds.size());
101
102 if (IsCapturing()) {
103 DHLOGE("Can not release streams when capture.");
104 return CamRetCode::CAMERA_BUSY;
105 }
106
107 for (int id : streamIds) {
108 auto stream = FindHalStreamById(id);
109 if (stream != nullptr) {
110 DCamRetCode ret = stream->ReleaseDCameraBufferQueue();
111 if (ret != SUCCESS) {
112 DHLOGE("Release distributed camera buffer queue for stream %d failed.", id);
113 return MapToExternalRetCode(ret);
114 } else {
115 DHLOGI("Release distributed camera buffer queue for stream %d success.", id);
116 }
117 stream = nullptr;
118 EraseHalStream(id);
119 EraseDCStream(id);
120 } else {
121 DHLOGE("Error streamId %d.", id);
122 return CamRetCode::INVALID_ARGUMENT;
123 }
124 }
125
126 OHOS::sptr<DCameraProvider> provider = DCameraProvider::GetInstance();
127 if (provider == nullptr) {
128 DHLOGE("Distributed camera provider not init.");
129 return CamRetCode::DEVICE_ERROR;
130 }
131 int32_t ret = provider->ReleaseStreams(dhBase_, streamIds);
132 if (ret != SUCCESS) {
133 DHLOGE("Release distributed camera streams failed.");
134 return MapToExternalRetCode(static_cast<DCamRetCode>(ret));
135 }
136
137 DHLOGI("DStreamOperator::Release distributed camera streams success.");
138 return CamRetCode::NO_ERROR;
139 }
140
ExtractStreamInfo(std::vector<DCStreamInfo> & dCameraStreams)141 int32_t DStreamOperator::ExtractStreamInfo(std::vector<DCStreamInfo>& dCameraStreams)
142 {
143 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
144 if (dcStreamInfoMap_.size() == 0) {
145 DHLOGE("No stream to extract.");
146 return CamRetCode::INVALID_ARGUMENT;
147 }
148 for (auto streamInfo : dcStreamInfoMap_) {
149 DCStreamInfo dstStreamInfo;
150 dstStreamInfo.streamId_ = streamInfo.second->streamId_;
151 dstStreamInfo.width_ = streamInfo.second->width_;
152 dstStreamInfo.height_ = streamInfo.second->height_;
153 dstStreamInfo.stride_ = streamInfo.second->stride_;
154 dstStreamInfo.format_ = streamInfo.second->format_;
155 dstStreamInfo.dataspace_ = streamInfo.second->dataspace_;
156 dstStreamInfo.encodeType_ = streamInfo.second->encodeType_;
157 dstStreamInfo.type_ = streamInfo.second->type_;
158 dCameraStreams.push_back(dstStreamInfo);
159 }
160 return CamRetCode::NO_ERROR;
161 }
162
CommitStreams(OperationMode mode,const std::vector<uint8_t> & modeSetting)163 int32_t DStreamOperator::CommitStreams(OperationMode mode, const std::vector<uint8_t> &modeSetting)
164 {
165 DHLOGI("DStreamOperator::CommitStreams, input operation mode=%d.", mode);
166
167 if (modeSetting.empty() || modeSetting.size() > METADATA_CAPACITY_MAX_SIZE) {
168 DHLOGE("DStreamOperator::CommitStreams, input modeSetting is invalid.");
169 return CamRetCode::INVALID_ARGUMENT;
170 }
171
172 if (IsCapturing()) {
173 DHLOGE("Can not commit streams when capture.");
174 return CamRetCode::CAMERA_BUSY;
175 }
176
177 currentOperMode_ = mode;
178
179 std::shared_ptr<OHOS::Camera::CameraMetadata> setting = nullptr;
180 OHOS::Camera::MetadataUtils::ConvertVecToMetadata(modeSetting, setting);
181 if (setting == nullptr || setting.get() == nullptr) {
182 DHLOGE("Input stream mode setting is invalid.");
183 } else {
184 latestStreamSetting_ = setting;
185 }
186
187 std::vector<DCStreamInfo> dCameraStreams;
188 int32_t ret = ExtractStreamInfo(dCameraStreams);
189 if (ret != CamRetCode::NO_ERROR) {
190 DHLOGE("No stream to commit.");
191 return ret;
192 }
193
194 OHOS::sptr<DCameraProvider> provider = DCameraProvider::GetInstance();
195 if (provider == nullptr) {
196 DHLOGE("Distributed camera provider not init.");
197 return CamRetCode::DEVICE_ERROR;
198 }
199 ret = provider->ConfigureStreams(dhBase_, dCameraStreams);
200 if (ret != DCamRetCode::SUCCESS) {
201 DHLOGE("Commit distributed camera streams failed.");
202 return MapToExternalRetCode(static_cast<DCamRetCode>(ret));
203 }
204
205 for (size_t i = 0; i < dCameraStreams.size(); i++) {
206 auto streamInfo = dCameraStreams[i];
207 HalStreamCommit(streamInfo);
208 }
209 DHLOGI("DStreamOperator::Commit distributed camera streams success.");
210 return CamRetCode::NO_ERROR;
211 }
212
HalStreamCommit(const DCStreamInfo & streamInfo)213 int32_t DStreamOperator::HalStreamCommit(const DCStreamInfo &streamInfo)
214 {
215 auto stream = FindHalStreamById(streamInfo.streamId_);
216 if (stream != nullptr) {
217 int32_t ret = stream->FinishCommitStream();
218 if (ret != DCamRetCode::SUCCESS) {
219 DHLOGE("Stream %d cannot init.", streamInfo.streamId_);
220 return MapToExternalRetCode(static_cast<DCamRetCode>(ret));
221 }
222 }
223 return CamRetCode::NO_ERROR;
224 }
225
GetStreamAttributes(std::vector<StreamAttribute> & attributes)226 int32_t DStreamOperator::GetStreamAttributes(std::vector<StreamAttribute> &attributes)
227 {
228 attributes.clear();
229 std::lock_guard<std::mutex> autoLock(halStreamLock_);
230 for (const auto &stream : halStreamMap_) {
231 StreamAttribute attribute;
232 DCamRetCode ret = stream.second->GetDCameraStreamAttribute(attribute);
233 if (ret != SUCCESS) {
234 DHLOGE("Get distributed camera stream attribute failed.");
235 attributes.clear();
236 return MapToExternalRetCode(ret);
237 }
238 attributes.push_back(attribute);
239 }
240 return CamRetCode::NO_ERROR;
241 }
242
AttachBufferQueue(int32_t streamId,const sptr<BufferProducerSequenceable> & bufferProducer)243 int32_t DStreamOperator::AttachBufferQueue(int32_t streamId, const sptr<BufferProducerSequenceable> &bufferProducer)
244 {
245 if (streamId < 0) {
246 DHLOGE("DStreamOperator::AttachBufferQueue, input streamId is invalid.");
247 return CamRetCode::INVALID_ARGUMENT;
248 }
249
250 if (bufferProducer == nullptr) {
251 DHLOGE("DStreamOperator::AttachBufferQueue, input bufferProducer is null.");
252 return CamRetCode::INVALID_ARGUMENT;
253 }
254
255 if (IsCapturing()) {
256 DHLOGE("Can not attach buffer queue when capture.");
257 return CamRetCode::CAMERA_BUSY;
258 }
259
260 auto stream = FindHalStreamById(streamId);
261 if (stream != nullptr) {
262 DCamRetCode ret = stream->SetDCameraBufferQueue(bufferProducer);
263 if (ret != SUCCESS) {
264 DHLOGE("Attach distributed camera buffer queue failed.");
265 }
266 return MapToExternalRetCode(ret);
267 } else {
268 DHLOGE("Not found stream id %d when attach bubfer queue.", streamId);
269 return CamRetCode::INVALID_ARGUMENT;
270 }
271 }
272
IsStreamInfosInvalid(const std::vector<StreamInfo> & infos)273 bool DStreamOperator::IsStreamInfosInvalid(const std::vector<StreamInfo> &infos)
274 {
275 if (infos.empty() || infos.size() > CONTAINER_CAPACITY_MAX_SIZE) {
276 return true;
277 }
278 for (auto streamInfo : infos) {
279 if (streamInfo.streamId_ < 0 ||
280 streamInfo.width_ < 0 ||
281 streamInfo.width_ > (STREAM_WIDTH_MAX_SIZE * STREAM_HEIGHT_MAX_SIZE) ||
282 streamInfo.height_ < 0 ||
283 streamInfo.height_ > (STREAM_WIDTH_MAX_SIZE * STREAM_HEIGHT_MAX_SIZE) ||
284 streamInfo.format_ < 0 ||
285 streamInfo.dataspace_ < 0) {
286 return true;
287 }
288 }
289 return false;
290 }
291
IsCaptureInfoInvalid(const CaptureInfo & info)292 bool DStreamOperator::IsCaptureInfoInvalid(const CaptureInfo &info)
293 {
294 return info.streamIds_.size() == 0 || info.streamIds_.size() > CONTAINER_CAPACITY_MAX_SIZE ||
295 info.captureSetting_.size() == 0 || info.captureSetting_.size() > CONTAINER_CAPACITY_MAX_SIZE;
296 }
297
DetachBufferQueue(int32_t streamId)298 int32_t DStreamOperator::DetachBufferQueue(int32_t streamId)
299 {
300 if (streamId < 0) {
301 DHLOGE("DStreamOperator::DetachBufferQueue, input streamId is invalid.");
302 return CamRetCode::INVALID_ARGUMENT;
303 }
304
305 if (IsCapturing()) {
306 DHLOGE("Can not detach buffer queue when capture.");
307 return CamRetCode::CAMERA_BUSY;
308 }
309
310 auto stream = FindHalStreamById(streamId);
311 if (stream != nullptr) {
312 DCamRetCode ret = stream->ReleaseDCameraBufferQueue();
313 if (ret != SUCCESS) {
314 DHLOGE("Detach distributed camera buffer queue failed.");
315 }
316 return MapToExternalRetCode(ret);
317 } else {
318 DHLOGE("Not found stream id %d when detach bubfer queue.", streamId);
319 return CamRetCode::INVALID_ARGUMENT;
320 }
321 }
322
ExtractCaptureInfo(std::vector<DCCaptureInfo> & captureInfos)323 void DStreamOperator::ExtractCaptureInfo(std::vector<DCCaptureInfo> &captureInfos)
324 {
325 for (const auto &captureInfo : cachedDCaptureInfoList_) {
326 DCCaptureInfo capture;
327 capture.streamIds_.assign(captureInfo->streamIds_.begin(), captureInfo->streamIds_.end());
328 capture.width_ = captureInfo->width_;
329 capture.height_ = captureInfo->height_;
330 capture.stride_ = captureInfo->stride_;
331 capture.format_ = captureInfo->format_;
332 capture.dataspace_ = captureInfo->dataspace_;
333 capture.isCapture_ = captureInfo->isCapture_;
334 capture.encodeType_ = captureInfo->encodeType_;
335 capture.type_ = captureInfo->type_;
336 capture.captureSettings_.assign(captureInfo->captureSettings_.begin(), captureInfo->captureSettings_.end());
337 captureInfos.emplace_back(capture);
338 }
339 }
340
Capture(int32_t captureId,const CaptureInfo & info,bool isStreaming)341 int32_t DStreamOperator::Capture(int32_t captureId, const CaptureInfo &info, bool isStreaming)
342 {
343 if (IsCaptureInfoInvalid(info)) {
344 DHLOGE("DStreamOperator::Capture, input capture info is valid.");
345 return CamRetCode::INVALID_ARGUMENT;
346 }
347 if (captureId < 0 || FindCaptureInfoById(captureId) != nullptr) {
348 DHLOGE("Input captureId %d is exist.", captureId);
349 return CamRetCode::INVALID_ARGUMENT;
350 }
351 return DoCapture(captureId, info, isStreaming);
352 }
353
DoCapture(int32_t captureId,const CaptureInfo & info,bool isStreaming)354 int32_t DStreamOperator::DoCapture(int32_t captureId, const CaptureInfo &info, bool isStreaming)
355 {
356 for (const auto &id : info.streamIds_) {
357 auto stream = FindHalStreamById(id);
358 if (stream == nullptr) {
359 DHLOGE("Invalid stream id %d", id);
360 return CamRetCode::INVALID_ARGUMENT;
361 }
362 if (!stream->HasBufferQueue()) {
363 DHLOGE("Stream %d has not bufferQueue.", id);
364 return CamRetCode::INVALID_ARGUMENT;
365 }
366 stream->DoCapture();
367 InsertEnableShutter(id, info.enableShutterCallback_);
368 DHLOGI("DStreamOperator::DoCapture info: "+
369 "captureId=%d, streamId=%d, isStreaming=%d", captureId, id, isStreaming);
370 }
371
372 DCamRetCode ret = NegotiateSuitableCaptureInfo(info, isStreaming);
373 if (ret != SUCCESS) {
374 DHLOGE("Negotiate suitable capture info failed.");
375 return MapToExternalRetCode(ret);
376 }
377
378 OHOS::sptr<DCameraProvider> provider = DCameraProvider::GetInstance();
379 if (provider == nullptr) {
380 DHLOGE("Distributed camera provider not init.");
381 return CamRetCode::DEVICE_ERROR;
382 }
383 std::vector<DCCaptureInfo> captureInfos;
384 ExtractCaptureInfo(captureInfos);
385 int32_t retProv = provider->StartCapture(dhBase_, captureInfos);
386 if (retProv != SUCCESS) {
387 DHLOGE("Start distributed camera capture failed.");
388 return MapToExternalRetCode(static_cast<DCamRetCode>(retProv));
389 }
390 std::shared_ptr<CaptureInfo> captureInfo = std::make_shared<CaptureInfo>();
391 captureInfo->streamIds_.assign(info.streamIds_.begin(), info.streamIds_.end());
392 captureInfo->captureSetting_.assign(info.captureSetting_.begin(), info.captureSetting_.end());
393 captureInfo->enableShutterCallback_ = info.enableShutterCallback_;
394 InsertCaptureInfo(captureId, captureInfo);
395
396 SetCapturing(true);
397 DHLOGI("DStreamOperator::DoCapture, start distributed camera capture success.");
398
399 return CamRetCode::NO_ERROR;
400 }
401
CancelCapture(int32_t captureId)402 int32_t DStreamOperator::CancelCapture(int32_t captureId)
403 {
404 if (captureId < 0) {
405 DHLOGE("DStreamOperator::CancelCapture, input captureId is valid.");
406 return CamRetCode::INVALID_ARGUMENT;
407 }
408
409 DHLOGI("DStreamOperator::CancelCapture, cancel distributed camera capture, captureId=%d.", captureId);
410 auto halCaptureInfo = FindCaptureInfoById(captureId);
411 if (captureId < 0 || halCaptureInfo == nullptr) {
412 DHLOGE("Input captureId %d is not exist.", captureId);
413 return CamRetCode::INVALID_ARGUMENT;
414 }
415
416 OHOS::sptr<DCameraProvider> provider = DCameraProvider::GetInstance();
417 if (provider == nullptr) {
418 DHLOGE("Distributed camera provider not init.");
419 return CamRetCode::DEVICE_ERROR;
420 }
421
422 std::vector<int> streamIds = halCaptureInfo->streamIds_;
423 int32_t ret = provider->StopCapture(dhBase_, streamIds);
424 if (ret != SUCCESS) {
425 DHLOGE("Cancel distributed camera capture failed.");
426 return MapToExternalRetCode(static_cast<DCamRetCode>(ret));
427 }
428
429 std::vector<CaptureEndedInfo> info;
430 for (auto id : streamIds) {
431 auto stream = FindHalStreamById(id);
432 if (stream != nullptr) {
433 stream->CancelCaptureWait();
434 }
435 CaptureEndedInfo tmp;
436 tmp.frameCount_ = FindStreamCaptureBufferNum(std::make_pair(captureId, id));
437 tmp.streamId_ = id;
438 info.push_back(tmp);
439 EraseStreamCaptureBufferNum(std::make_pair(captureId, id));
440 }
441 if (dcStreamOperatorCallback_) {
442 dcStreamOperatorCallback_->OnCaptureEnded(captureId, info);
443 }
444
445 EraseCaptureInfo(captureId);
446 if (!HasContinuousCaptureInfo(captureId)) {
447 SetCapturing(false);
448 cachedDCaptureInfoList_.clear();
449 }
450 DHLOGI("DStreamOperator::CancelCapture success, captureId=%d.", captureId);
451 return CamRetCode::NO_ERROR;
452 }
453
HasContinuousCaptureInfo(int captureId)454 bool DStreamOperator::HasContinuousCaptureInfo(int captureId)
455 {
456 bool flag = false;
457 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
458 for (auto iter : halCaptureInfoMap_) {
459 for (auto id : iter.second->streamIds_) {
460 auto dcStreamInfo = dcStreamInfoMap_.find(id);
461 if (dcStreamInfo == dcStreamInfoMap_.end()) {
462 continue;
463 }
464
465 DHLOGI("DStreamOperator::HasContinuousCaptureInfo, captureId=%d, streamId=%d, streamType=%d",
466 captureId, id, dcStreamInfo->second->type_);
467 if (dcStreamInfo->second->type_ == DCStreamType::CONTINUOUS_FRAME) {
468 DHLOGI("DStreamOperator::HasContinuousCaptureInfo, captureId=%d, stream %d is continuous stream.",
469 captureId, id);
470 flag = true;
471 break;
472 }
473 }
474 }
475 return flag;
476 }
477
ChangeToOfflineStream(const std::vector<int32_t> & streamIds,const sptr<IStreamOperatorCallback> & callbackObj,sptr<IOfflineStreamOperator> & offlineOperator)478 int32_t DStreamOperator::ChangeToOfflineStream(const std::vector<int32_t> &streamIds,
479 const sptr<IStreamOperatorCallback> &callbackObj, sptr<IOfflineStreamOperator> &offlineOperator)
480 {
481 (void)streamIds;
482 (void)callbackObj;
483 offlineOperator = nullptr;
484 return CamRetCode::METHOD_NOT_SUPPORTED;
485 }
486
ExtractCameraAttr(Json::Value & rootValue,std::vector<int> & formats,const std::string rootNode)487 void DStreamOperator::ExtractCameraAttr(Json::Value &rootValue, std::vector<int>& formats, const std::string rootNode)
488 {
489 for (const auto &format : formats) {
490 std::string formatStr = std::to_string(format);
491 if (!rootValue[rootNode].isMember("Resolution") || !rootValue[rootNode]["Resolution"].isMember(formatStr) ||
492 !rootValue[rootNode]["Resolution"][formatStr].isArray() ||
493 rootValue[rootNode]["Resolution"][formatStr].size() == 0 ||
494 rootValue[rootNode]["Resolution"][formatStr].size() > JSON_ARRAY_MAX_SIZE) {
495 DHLOGE("Resolution or %s error.", formatStr.c_str());
496 continue;
497 }
498 GetCameraAttr(rootValue, formatStr, rootNode, format);
499 }
500 }
501
GetCameraAttr(Json::Value & rootValue,std::string formatStr,const std::string rootNode,int format)502 void DStreamOperator::GetCameraAttr(Json::Value &rootValue, std::string formatStr, const std::string rootNode,
503 int format)
504 {
505 std::vector<DCResolution> resolutionVec;
506 uint32_t size = rootValue[rootNode]["Resolution"][formatStr].size();
507 for (uint32_t i = 0; i < size; i++) {
508 if (!rootValue[rootNode]["Resolution"][formatStr][i].isString()) {
509 DHLOGE("Resolution %s %d ,is not string.", formatStr.c_str(), i);
510 continue;
511 }
512 std::string resoStr = rootValue[rootNode]["Resolution"][formatStr][i].asString();
513 std::vector<std::string> reso;
514 SplitString(resoStr, reso, STAR_SEPARATOR);
515 if (reso.size() != SIZE_FMT_LEN) {
516 continue;
517 }
518 uint32_t width = static_cast<uint32_t>(std::stoi(reso[0]));
519 uint32_t height = static_cast<uint32_t>(std::stoi(reso[1]));
520 if (height == 0 || width == 0 ||
521 ((rootNode == "Photo") &&
522 ((width * height) > (MAX_SUPPORT_PHOTO_WIDTH * MAX_SUPPORT_PHOTO_HEIGHT))) ||
523 ((rootNode != "Photo") &&
524 (width > MAX_SUPPORT_PREVIEW_WIDTH || height > MAX_SUPPORT_PREVIEW_HEIGHT))) {
525 continue;
526 }
527 DCResolution resolution(width, height);
528 resolutionVec.push_back(resolution);
529 }
530 if (!resolutionVec.empty()) {
531 std::sort(resolutionVec.begin(), resolutionVec.end());
532 if (rootNode == "Preview") {
533 dcSupportedPreviewResolutionMap_[format] = resolutionVec;
534 } else if (rootNode == "Video") {
535 dcSupportedVideoResolutionMap_[format] = resolutionVec;
536 } else if (rootNode == "Photo") {
537 dcSupportedPhotoResolutionMap_[format] = resolutionVec;
538 }
539 }
540 }
541
InitOutputConfigurations(const DHBase & dhBase,const std::string & abilityInfo)542 DCamRetCode DStreamOperator::InitOutputConfigurations(const DHBase &dhBase, const std::string &abilityInfo)
543 {
544 dhBase_ = dhBase;
545 JSONCPP_STRING errs;
546 Json::CharReaderBuilder readerBuilder;
547 Json::Value rootValue;
548
549 std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
550 if (!jsonReader->parse(abilityInfo.c_str(), abilityInfo.c_str() + abilityInfo.length(), &rootValue, &errs) ||
551 !rootValue.isObject()) {
552 DHLOGE("Input ablity info is not json object.");
553 return DCamRetCode::INVALID_ARGUMENT;
554 }
555
556 if (!rootValue.isMember("CodecType") || !rootValue["CodecType"].isArray() ||
557 rootValue["CodecType"].size() == 0 || rootValue["CodecType"].size() > JSON_ARRAY_MAX_SIZE) {
558 DHLOGE("CodecType error.");
559 return DCamRetCode::INVALID_ARGUMENT;
560 }
561 uint32_t size = rootValue["CodecType"].size();
562 for (uint32_t i = 0; i < size; i++) {
563 if ((rootValue["CodecType"][i]).isString()) {
564 std::string codeType = (rootValue["CodecType"][i]).asString();
565 dcSupportedCodecType_.push_back(ConvertDCEncodeType(codeType));
566 }
567 }
568
569 if (ParsePhotoFormats(rootValue) != SUCCESS || ParsePreviewFormats(rootValue) != SUCCESS ||
570 ParseVideoFormats(rootValue) != SUCCESS) {
571 return DCamRetCode::INVALID_ARGUMENT;
572 }
573
574 bool resolutionMap = false;
575 if (!dcSupportedPhotoResolutionMap_.empty() || !dcSupportedPreviewResolutionMap_.empty() ||
576 !dcSupportedVideoResolutionMap_.empty()) {
577 resolutionMap = true;
578 }
579
580 if (dcSupportedCodecType_.empty() || dcSupportedFormatMap_.empty() || !resolutionMap) {
581 DHLOGE("Input ablity info is invalid.");
582 return DEVICE_NOT_INIT;
583 }
584 return SUCCESS;
585 }
586
ParsePhotoFormats(Json::Value & rootValue)587 DCamRetCode DStreamOperator::ParsePhotoFormats(Json::Value& rootValue)
588 {
589 if (!rootValue.isMember("Photo") || !rootValue["Photo"].isMember("OutputFormat") ||
590 !rootValue["Photo"]["OutputFormat"].isArray() || rootValue["Photo"]["OutputFormat"].size() == 0 ||
591 rootValue["Photo"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) {
592 DHLOGE("Photo or photo output format error.");
593 return DCamRetCode::INVALID_ARGUMENT;
594 }
595 std::vector<int> photoFormats;
596 uint32_t size = rootValue["Photo"]["OutputFormat"].size();
597 for (uint32_t i = 0; i < size; i++) {
598 if ((rootValue["Photo"]["OutputFormat"][i]).isInt()) {
599 photoFormats.push_back((rootValue["Photo"]["OutputFormat"][i]).asInt());
600 }
601 }
602 dcSupportedFormatMap_[DCSceneType::PHOTO] = photoFormats;
603 ExtractCameraAttr(rootValue, photoFormats, "Photo");
604 return SUCCESS;
605 }
606
ParsePreviewFormats(Json::Value & rootValue)607 DCamRetCode DStreamOperator::ParsePreviewFormats(Json::Value& rootValue)
608 {
609 if (!rootValue.isMember("Preview") || !rootValue["Preview"].isMember("OutputFormat") ||
610 !rootValue["Preview"]["OutputFormat"].isArray() || rootValue["Preview"]["OutputFormat"].size() == 0 ||
611 rootValue["Preview"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) {
612 DHLOGE("Preview or preview output format error.");
613 return DCamRetCode::INVALID_ARGUMENT;
614 }
615 std::vector<int> previewFormats;
616 uint32_t size = rootValue["Preview"]["OutputFormat"].size();
617 for (uint32_t i = 0; i < size; i++) {
618 if ((rootValue["Preview"]["OutputFormat"][i]).isInt()) {
619 previewFormats.push_back((rootValue["Preview"]["OutputFormat"][i]).asInt());
620 }
621 }
622 dcSupportedFormatMap_[DCSceneType::PREVIEW] = previewFormats;
623 ExtractCameraAttr(rootValue, previewFormats, "Preview");
624 return SUCCESS;
625 }
626
ParseVideoFormats(Json::Value & rootValue)627 DCamRetCode DStreamOperator::ParseVideoFormats(Json::Value& rootValue)
628 {
629 if (!rootValue.isMember("Video") || !rootValue["Video"].isMember("OutputFormat") ||
630 !rootValue["Video"]["OutputFormat"].isArray() || rootValue["Video"]["OutputFormat"].size() == 0 ||
631 rootValue["Video"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) {
632 DHLOGE("Video or video output format error.");
633 return DCamRetCode::INVALID_ARGUMENT;
634 }
635 std::vector<int> videoFormats;
636 uint32_t size = rootValue["Video"]["OutputFormat"].size();
637 for (uint32_t i = 0; i < size; i++) {
638 if ((rootValue["Video"]["OutputFormat"][i]).isInt()) {
639 videoFormats.push_back((rootValue["Video"]["OutputFormat"][i]).asInt());
640 }
641 }
642 dcSupportedFormatMap_[DCSceneType::VIDEO] = videoFormats;
643 ExtractCameraAttr(rootValue, videoFormats, "Video");
644 return SUCCESS;
645 }
646
AcquireBuffer(int streamId,DCameraBuffer & buffer)647 DCamRetCode DStreamOperator::AcquireBuffer(int streamId, DCameraBuffer &buffer)
648 {
649 if (!IsCapturing()) {
650 DHLOGE("Not in capturing state, can not acquire buffer.");
651 return DCamRetCode::CAMERA_OFFLINE;
652 }
653
654 auto stream = FindHalStreamById(streamId);
655 if (stream == nullptr) {
656 DHLOGE("streamId %d is invalid, can not acquire buffer.", streamId);
657 return DCamRetCode::INVALID_ARGUMENT;
658 }
659
660 DCamRetCode ret = stream->GetDCameraBuffer(buffer);
661 if (ret == DCamRetCode::EXCEED_MAX_NUMBER) {
662 DHLOGE("Buffer list is full, cannot get new idle buffer.");
663 } else if (ret == DCamRetCode::INVALID_ARGUMENT) {
664 DHLOGE("Get distributed camera buffer failed, invalid buffer parameter.");
665 }
666 return ret;
667 }
668
ShutterBuffer(int streamId,const DCameraBuffer & buffer)669 DCamRetCode DStreamOperator::ShutterBuffer(int streamId, const DCameraBuffer &buffer)
670 {
671 DHLOGD("DStreamOperator::ShutterBuffer begin shutter buffer for streamId = %d", streamId);
672
673 int32_t captureId = FindCaptureIdByStreamId(streamId);
674 if (captureId == -1) {
675 DHLOGE("ShutterBuffer failed, invalid streamId = %d", streamId);
676 return DCamRetCode::INVALID_ARGUMENT;
677 }
678
679 if (buffer.index_ == 0 && dcStreamOperatorCallback_ != nullptr) {
680 vector<int> tmpStreamIds;
681 tmpStreamIds.push_back(streamId);
682 dcStreamOperatorCallback_->OnCaptureStarted(captureId, tmpStreamIds);
683 }
684
685 auto stream = FindHalStreamById(streamId);
686 if (stream != nullptr) {
687 DCamRetCode ret = stream->ReturnDCameraBuffer(buffer);
688 if (ret != DCamRetCode::SUCCESS) {
689 DHLOGE("Flush distributed camera buffer failed.");
690 return ret;
691 }
692 AddStreamCaptureBufferNum(std::make_pair(captureId, streamId));
693
694 SnapShotStreamOnCaptureEnded(captureId, streamId);
695 }
696
697 uint64_t resultTimestamp = GetCurrentLocalTimeStamp();
698 dMetadataProcessor_->UpdateResultMetadata(resultTimestamp);
699
700 bool enableShutter = FindEnableShutter(streamId);
701 if (!enableShutter) {
702 if (dcStreamOperatorCallback_ == nullptr) {
703 DHLOGE("DStreamOperator::ShutterBuffer failed, need shutter frame, but stream operator callback is null.");
704 return DCamRetCode::FAILED;
705 }
706 std::vector<int32_t> streamIds;
707 streamIds.push_back(streamId);
708 dcStreamOperatorCallback_->OnFrameShutter(captureId, streamIds, resultTimestamp);
709 }
710 return DCamRetCode::SUCCESS;
711 }
712
SetCallBack(OHOS::sptr<IStreamOperatorCallback> const & callback)713 DCamRetCode DStreamOperator::SetCallBack(OHOS::sptr<IStreamOperatorCallback> const &callback)
714 {
715 dcStreamOperatorCallback_ = callback;
716 return SUCCESS;
717 }
718
SetDeviceCallback(std::function<void (ErrorType,int)> & errorCbk,std::function<void (uint64_t,std::shared_ptr<OHOS::Camera::CameraMetadata>)> & resultCbk)719 DCamRetCode DStreamOperator::SetDeviceCallback(
720 std::function<void(ErrorType, int)> &errorCbk,
721 std::function<void(uint64_t, std::shared_ptr<OHOS::Camera::CameraMetadata>)> &resultCbk)
722 {
723 errorCallback_ = errorCbk;
724 dMetadataProcessor_->SetResultCallback(resultCbk);
725 return SUCCESS;
726 }
727
SnapShotStreamOnCaptureEnded(int32_t captureId,int streamId)728 void DStreamOperator::SnapShotStreamOnCaptureEnded(int32_t captureId, int streamId)
729 {
730 auto dcStreamInfo = FindDCStreamById(streamId);
731 if (dcStreamInfo == nullptr) {
732 return;
733 }
734 if (dcStreamInfo->type_ != DCStreamType::SNAPSHOT_FRAME) {
735 return;
736 }
737 if (dcStreamOperatorCallback_ == nullptr) {
738 return;
739 }
740 std::vector<CaptureEndedInfo> info;
741 CaptureEndedInfo tmp;
742 tmp.frameCount_ = FindStreamCaptureBufferNum(std::make_pair(captureId, streamId));
743 tmp.streamId_ = streamId;
744 info.push_back(tmp);
745 dcStreamOperatorCallback_->OnCaptureEnded(captureId, info);
746 DHLOGD("snapshot stream successfully reported captureId = %d streamId = %d.", captureId, streamId);
747 }
748
Release()749 void DStreamOperator::Release()
750 {
751 DHLOGI("DStreamOperator::Release, begin release stream operator.");
752
753 std::vector<int> streamIds = GetStreamIds();
754 SetCapturing(false);
755 ReleaseStreams(streamIds);
756 if (latestStreamSetting_) {
757 latestStreamSetting_ = nullptr;
758 }
759 {
760 std::lock_guard<std::mutex> lockStream(halStreamLock_);
761 halStreamMap_.clear();
762 }
763 std::lock_guard<std::mutex> lock(streamAttrLock_);
764 dcStreamInfoMap_.clear();
765 halCaptureInfoMap_.clear();
766 enableShutterCbkMap_.clear();
767 acceptedBufferNum_.clear();
768 cachedDCaptureInfoList_.clear();
769 dcStreamOperatorCallback_ = nullptr;
770 }
771
GetStreamIds()772 std::vector<int> DStreamOperator::GetStreamIds()
773 {
774 DHLOGI("DStreamOperator::GetStreamIds, begin get stream id.");
775 std::lock_guard<std::mutex> autoLock(halStreamLock_);
776 std::vector<int> streamIds;
777 std::string idString = "";
778 for (auto iter : halStreamMap_) {
779 streamIds.push_back(iter.first);
780 idString += (std::to_string(iter.first) + ", ");
781 }
782 DHLOGI("DStreamOperator::GetStreamIds, ids=[%s]", idString.c_str());
783 return streamIds;
784 }
785
IsCapturing()786 bool DStreamOperator::IsCapturing()
787 {
788 std::unique_lock<mutex> lock(isCapturingLock_);
789 return isCapturing_;
790 }
791
SetCapturing(bool isCapturing)792 void DStreamOperator::SetCapturing(bool isCapturing)
793 {
794 std::unique_lock<mutex> lock(isCapturingLock_);
795 isCapturing_ = isCapturing;
796 }
797
ConvertStreamInfo(const StreamInfo & srcInfo,std::shared_ptr<DCStreamInfo> & dstInfo)798 void DStreamOperator::ConvertStreamInfo(const StreamInfo &srcInfo, std::shared_ptr<DCStreamInfo> &dstInfo)
799 {
800 dstInfo->streamId_ = srcInfo.streamId_;
801 dstInfo->width_ = srcInfo.width_;
802 dstInfo->stride_ = srcInfo.width_;
803 dstInfo->height_ = srcInfo.height_;
804 dstInfo->dataspace_ = srcInfo.dataspace_;
805 dstInfo->encodeType_ = (DCEncodeType)srcInfo.encodeType_;
806
807 if ((srcInfo.intent_ == STILL_CAPTURE) || (srcInfo.intent_ == POST_VIEW)) {
808 dstInfo->type_ = DCStreamType::SNAPSHOT_FRAME;
809 if (dstInfo->encodeType_ == DCEncodeType::ENCODE_TYPE_JPEG) {
810 dstInfo->format_ = OHOS_CAMERA_FORMAT_JPEG;
811 } else if (dstInfo->encodeType_ == DCEncodeType::ENCODE_TYPE_NULL) {
812 dstInfo->format_ = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
813 }
814 } else {
815 // Distributed Camera Do Not Support Encoding at HAL
816 dstInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_NULL;
817 dstInfo->type_ = DCStreamType::CONTINUOUS_FRAME;
818 dstInfo->format_ =
819 static_cast<int>(DBufferManager::PixelFormatToDCameraFormat(static_cast<PixelFormat>(srcInfo.format_)));
820 }
821 }
822
NegotiateSuitableCaptureInfo(const CaptureInfo & srcCaptureInfo,bool isStreaming)823 DCamRetCode DStreamOperator::NegotiateSuitableCaptureInfo(const CaptureInfo& srcCaptureInfo, bool isStreaming)
824 {
825 for (auto streamId : srcCaptureInfo.streamIds_) {
826 DHLOGI("DStreamOperator::NegotiateSuitableCaptureInfo, streamId=%d, isStreaming=%d", streamId, isStreaming);
827 }
828
829 std::shared_ptr<DCCaptureInfo> inputCaptureInfo = nullptr;
830 DCamRetCode ret = GetInputCaptureInfo(srcCaptureInfo, isStreaming, inputCaptureInfo);
831 if (ret != DCamRetCode::SUCCESS) {
832 DHLOGE("Negotiate input capture info failed.");
833 return ret;
834 }
835
836 std::shared_ptr<DCCaptureInfo> appendCaptureInfo = nullptr;
837 AppendCaptureInfo(appendCaptureInfo, isStreaming, inputCaptureInfo, srcCaptureInfo);
838 cachedDCaptureInfoList_.clear();
839 cachedDCaptureInfoList_.push_back(inputCaptureInfo);
840 if (appendCaptureInfo != nullptr) {
841 cachedDCaptureInfoList_.push_back(appendCaptureInfo);
842 }
843
844 for (auto info : cachedDCaptureInfoList_) {
845 std::string idString = "";
846 for (auto id : info->streamIds_) {
847 idString += (std::to_string(id) + ", ");
848 }
849 DHLOGI("cachedDCaptureInfo: ids=[%s], width=%d, height=%d, format=%d, type=%d, isCapture=%d",
850 idString.empty() ? idString.c_str() : (idString.substr(0, idString.length() - INGNORE_STR_LEN)).c_str(),
851 info->width_, info->height_, info->format_, info->type_, info->isCapture_);
852 }
853 return SUCCESS;
854 }
855
AppendCaptureInfo(std::shared_ptr<DCCaptureInfo> & appendCaptureInfo,bool isStreaming,std::shared_ptr<DCCaptureInfo> & inputCaptureInfo,const CaptureInfo & srcCaptureInfo)856 void DStreamOperator::AppendCaptureInfo(std::shared_ptr<DCCaptureInfo> &appendCaptureInfo, bool isStreaming,
857 std::shared_ptr<DCCaptureInfo> &inputCaptureInfo, const CaptureInfo& srcCaptureInfo)
858 {
859 if (cachedDCaptureInfoList_.empty()) {
860 std::vector<std::shared_ptr<DCStreamInfo>> appendStreamInfo;
861 ExtractNotCaptureStream(isStreaming, appendStreamInfo);
862 if (!appendStreamInfo.empty()) {
863 appendCaptureInfo = BuildSuitableCaptureInfo(srcCaptureInfo, appendStreamInfo);
864 appendCaptureInfo->type_ = isStreaming ? DCStreamType::SNAPSHOT_FRAME : DCStreamType::CONTINUOUS_FRAME;
865 appendCaptureInfo->isCapture_ = false;
866 }
867 } else {
868 for (auto cacheCapture : cachedDCaptureInfoList_) {
869 if ((isStreaming && (cacheCapture->type_ == DCStreamType::SNAPSHOT_FRAME)) ||
870 (!isStreaming && (cacheCapture->type_ == DCStreamType::CONTINUOUS_FRAME))) {
871 cacheCapture->isCapture_ = false;
872 appendCaptureInfo = cacheCapture;
873 break;
874 }
875 }
876 if (inputCaptureInfo->type_ == DCStreamType::SNAPSHOT_FRAME) {
877 ChooseSuitableStreamId(appendCaptureInfo);
878 }
879 inputCaptureInfo->isCapture_ = isStreaming ? false : true;
880 }
881 }
882
GetInputCaptureInfo(const CaptureInfo & srcCaptureInfo,bool isStreaming,std::shared_ptr<DCCaptureInfo> & inputCaptureInfo)883 DCamRetCode DStreamOperator::GetInputCaptureInfo(const CaptureInfo& srcCaptureInfo, bool isStreaming,
884 std::shared_ptr<DCCaptureInfo>& inputCaptureInfo)
885 {
886 std::vector<std::shared_ptr<DCStreamInfo>> srcStreamInfo;
887 for (auto &id : srcCaptureInfo.streamIds_) {
888 auto dcStreamInfo = FindDCStreamById(id);
889 if (dcStreamInfo != nullptr) {
890 srcStreamInfo.push_back(dcStreamInfo);
891 }
892 }
893
894 if (srcStreamInfo.empty()) {
895 DHLOGE("Input source stream info vector is empty.");
896 return DCamRetCode::INVALID_ARGUMENT;
897 }
898
899 inputCaptureInfo = BuildSuitableCaptureInfo(srcCaptureInfo, srcStreamInfo);
900 inputCaptureInfo->type_ = isStreaming ? DCStreamType::CONTINUOUS_FRAME : DCStreamType::SNAPSHOT_FRAME;
901 inputCaptureInfo->isCapture_ = true;
902 return DCamRetCode::SUCCESS;
903 }
904
BuildSuitableCaptureInfo(const CaptureInfo & srcCaptureInfo,std::vector<std::shared_ptr<DCStreamInfo>> & srcStreamInfo)905 std::shared_ptr<DCCaptureInfo> DStreamOperator::BuildSuitableCaptureInfo(const CaptureInfo& srcCaptureInfo,
906 std::vector<std::shared_ptr<DCStreamInfo>> &srcStreamInfo)
907 {
908 std::shared_ptr<DCCaptureInfo> captureInfo = std::make_shared<DCCaptureInfo>();
909
910 ChooseSuitableFormat(srcStreamInfo, captureInfo);
911 ChooseSuitableResolution(srcStreamInfo, captureInfo);
912 ChooseSuitableDataSpace(srcStreamInfo, captureInfo);
913 ChooseSuitableEncodeType(srcStreamInfo, captureInfo);
914
915 DCameraSettings dcSetting;
916 dcSetting.type_ = DCSettingsType::UPDATE_METADATA;
917 std::shared_ptr<OHOS::Camera::CameraMetadata> captureSetting = nullptr;
918 OHOS::Camera::MetadataUtils::ConvertVecToMetadata(srcCaptureInfo.captureSetting_, captureSetting);
919 std::string settingStr = OHOS::Camera::MetadataUtils::EncodeToString(captureSetting);
920 dcSetting.value_ = Base64Encode(reinterpret_cast<const unsigned char *>(settingStr.c_str()), settingStr.length());
921
922 captureInfo->captureSettings_.push_back(dcSetting);
923
924 return captureInfo;
925 }
926
ChooseSuitableFormat(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfo,std::shared_ptr<DCCaptureInfo> & captureInfo)927 void DStreamOperator::ChooseSuitableFormat(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
928 std::shared_ptr<DCCaptureInfo> &captureInfo)
929 {
930 for (auto stream : streamInfo) {
931 if ((streamInfo.at(0)->type_ == DCStreamType::CONTINUOUS_FRAME &&
932 dcSupportedVideoResolutionMap_.count(stream->format_) > 0) ||
933 (streamInfo.at(0)->type_ == DCStreamType::SNAPSHOT_FRAME &&
934 dcSupportedPhotoResolutionMap_.count(stream->format_) > 0)) {
935 captureInfo->format_ = stream->format_;
936 return;
937 }
938 }
939 if ((streamInfo.at(0))->type_ == DCStreamType::CONTINUOUS_FRAME) {
940 if (dcSupportedFormatMap_.count(DCSceneType::PREVIEW) > 0) {
941 captureInfo->format_ = dcSupportedFormatMap_[DCSceneType::PREVIEW].at(0);
942 } else if (dcSupportedFormatMap_.count(DCSceneType::VIDEO) > 0) {
943 captureInfo->format_ = dcSupportedFormatMap_[DCSceneType::VIDEO].at(0);
944 } else {
945 captureInfo->format_ = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
946 }
947 } else {
948 if (dcSupportedFormatMap_.count(DCSceneType::PHOTO) > 0) {
949 captureInfo->format_ = dcSupportedFormatMap_[DCSceneType::PHOTO].at(0);
950 } else {
951 if ((streamInfo.at(0))->encodeType_ == DCEncodeType::ENCODE_TYPE_JPEG) {
952 captureInfo->format_ = OHOS_CAMERA_FORMAT_JPEG;
953 } else {
954 captureInfo->format_ = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
955 }
956 }
957 }
958 }
959
ChooseSuitableResolution(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfo,std::shared_ptr<DCCaptureInfo> & captureInfo)960 void DStreamOperator::ChooseSuitableResolution(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
961 std::shared_ptr<DCCaptureInfo> &captureInfo)
962 {
963 if (captureInfo == nullptr) {
964 DHLOGE("DStreamOperator::ChooseSuitableResolution, captureInfo is null.");
965 return;
966 }
967
968 std::vector<DCResolution> supportedResolutionList;
969 if ((streamInfo.at(0))->type_ == DCStreamType::CONTINUOUS_FRAME) {
970 supportedResolutionList = dcSupportedVideoResolutionMap_[captureInfo->format_];
971 } else {
972 supportedResolutionList = dcSupportedPhotoResolutionMap_[captureInfo->format_];
973 }
974
975 for (auto stream : streamInfo) {
976 captureInfo->streamIds_.push_back(stream->streamId_);
977 }
978
979 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
980 DCResolution tempResolution = { 0, 0 };
981 for (auto iter : dcStreamInfoMap_) {
982 if (iter.second->type_ != (streamInfo.at(0))->type_) {
983 continue;
984 }
985 for (auto resolution : supportedResolutionList) {
986 if ((resolution.width_ == iter.second->width_) &&
987 (resolution.height_ == iter.second->height_) &&
988 (tempResolution < resolution)) {
989 tempResolution = resolution;
990 break;
991 }
992 }
993 }
994
995 if ((tempResolution.width_ == 0) || (tempResolution.height_ == 0)) {
996 captureInfo->width_ = MAX_SUPPORT_PREVIEW_WIDTH;
997 captureInfo->height_ = MAX_SUPPORT_PREVIEW_HEIGHT;
998 } else {
999 captureInfo->width_ = tempResolution.width_;
1000 captureInfo->height_ = tempResolution.height_;
1001 }
1002 }
1003
ChooseSuitableDataSpace(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfo,std::shared_ptr<DCCaptureInfo> & captureInfo)1004 void DStreamOperator::ChooseSuitableDataSpace(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
1005 std::shared_ptr<DCCaptureInfo> &captureInfo)
1006 {
1007 captureInfo->dataspace_ = (streamInfo.at(0))->dataspace_;
1008 }
1009
ChooseSuitableEncodeType(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfo,std::shared_ptr<DCCaptureInfo> & captureInfo)1010 void DStreamOperator::ChooseSuitableEncodeType(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
1011 std::shared_ptr<DCCaptureInfo> &captureInfo)
1012 {
1013 if ((streamInfo.at(0))->type_ == DCStreamType::CONTINUOUS_FRAME) {
1014 if (count(dcSupportedCodecType_.begin(), dcSupportedCodecType_.end(), DCEncodeType::ENCODE_TYPE_H265)) {
1015 captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_H265;
1016 } else if (count(dcSupportedCodecType_.begin(), dcSupportedCodecType_.end(), DCEncodeType::ENCODE_TYPE_H264)) {
1017 captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_H264;
1018 } else {
1019 captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_NULL;
1020 }
1021 } else {
1022 if ((streamInfo.at(0))->encodeType_ == DCEncodeType::ENCODE_TYPE_JPEG) {
1023 captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_JPEG;
1024 } else {
1025 captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_NULL;
1026 }
1027 }
1028 }
1029
ChooseSuitableStreamId(std::shared_ptr<DCCaptureInfo> & captureInfo)1030 void DStreamOperator::ChooseSuitableStreamId(std::shared_ptr<DCCaptureInfo> &captureInfo)
1031 {
1032 if (captureInfo == nullptr) {
1033 DHLOGE("DStreamOperator::ChooseSuitableStreamId captureInfo is null");
1034 return;
1035 }
1036
1037 captureInfo->streamIds_.clear();
1038 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1039 for (auto iter : halCaptureInfoMap_) {
1040 for (auto id : iter.second->streamIds_) {
1041 auto dcStreamInfo = dcStreamInfoMap_.find(id);
1042 if (dcStreamInfo == dcStreamInfoMap_.end()) {
1043 continue;
1044 }
1045
1046 if (dcStreamInfo->second->type_ == DCStreamType::CONTINUOUS_FRAME) {
1047 DHLOGI("DStreamOperator::ChooseSuitableStreamId, streamId: %d", id);
1048 captureInfo->streamIds_.push_back(id);
1049 }
1050 }
1051 }
1052 }
1053
ConvertDCEncodeType(std::string & srcEncodeType)1054 DCEncodeType DStreamOperator::ConvertDCEncodeType(std::string &srcEncodeType)
1055 {
1056 if (srcEncodeType == ENCODE_TYPE_STR_H264) {
1057 return DCEncodeType::ENCODE_TYPE_H264;
1058 } else if (srcEncodeType == ENCODE_TYPE_STR_H265) {
1059 return DCEncodeType::ENCODE_TYPE_H265;
1060 } else if (srcEncodeType == ENCODE_TYPE_STR_JPEG) {
1061 return DCEncodeType::ENCODE_TYPE_JPEG;
1062 } else {
1063 return DCEncodeType::ENCODE_TYPE_NULL;
1064 }
1065 }
1066
FindHalStreamById(int32_t streamId)1067 std::shared_ptr<DCameraStream> DStreamOperator::FindHalStreamById(int32_t streamId)
1068 {
1069 std::lock_guard<std::mutex> autoLock(halStreamLock_);
1070 auto iter = halStreamMap_.find(streamId);
1071 if (iter == halStreamMap_.end()) {
1072 return nullptr;
1073 }
1074 return iter->second;
1075 }
1076
InsertHalStream(int32_t streamId,std::shared_ptr<DCameraStream> & dcStream)1077 void DStreamOperator::InsertHalStream(int32_t streamId, std::shared_ptr<DCameraStream>& dcStream)
1078 {
1079 std::lock_guard<std::mutex> autoLock(halStreamLock_);
1080 halStreamMap_.emplace(streamId, dcStream);
1081 }
1082
EraseHalStream(int32_t streamId)1083 void DStreamOperator::EraseHalStream(int32_t streamId)
1084 {
1085 std::lock_guard<std::mutex> autoLock(halStreamLock_);
1086 halStreamMap_.erase(streamId);
1087 }
1088
FindCaptureInfoById(int32_t captureId)1089 std::shared_ptr<CaptureInfo> DStreamOperator::FindCaptureInfoById(int32_t captureId)
1090 {
1091 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1092 auto iter = halCaptureInfoMap_.find(captureId);
1093 if (iter == halCaptureInfoMap_.end()) {
1094 return nullptr;
1095 }
1096 return iter->second;
1097 }
1098
InsertCaptureInfo(int32_t captureId,std::shared_ptr<CaptureInfo> & captureInfo)1099 void DStreamOperator::InsertCaptureInfo(int32_t captureId, std::shared_ptr<CaptureInfo>& captureInfo)
1100 {
1101 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1102 halCaptureInfoMap_.emplace(captureId, captureInfo);
1103 }
1104
FindCaptureIdByStreamId(int32_t streamId)1105 int32_t DStreamOperator::FindCaptureIdByStreamId(int32_t streamId)
1106 {
1107 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1108 int32_t captureId = -1;
1109 for (auto iter = halCaptureInfoMap_.begin(); iter != halCaptureInfoMap_.end(); iter++) {
1110 std::shared_ptr<CaptureInfo> captureInfo = iter->second;
1111 std::vector<int> streamIds = captureInfo->streamIds_;
1112 if (std::find(streamIds.begin(), streamIds.end(), streamId) != streamIds.end()) {
1113 captureId = iter->first;
1114 break;
1115 }
1116 }
1117 return captureId;
1118 }
1119
EraseCaptureInfo(int32_t captureId)1120 void DStreamOperator::EraseCaptureInfo(int32_t captureId)
1121 {
1122 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1123 halCaptureInfoMap_.erase(captureId);
1124 }
1125
FindDCStreamById(int32_t streamId)1126 std::shared_ptr<DCStreamInfo> DStreamOperator::FindDCStreamById(int32_t streamId)
1127 {
1128 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1129 auto iter = dcStreamInfoMap_.find(streamId);
1130 if (iter == dcStreamInfoMap_.end()) {
1131 return nullptr;
1132 }
1133 return iter->second;
1134 }
1135
InsertDCStream(int32_t streamId,std::shared_ptr<DCStreamInfo> & dcStreamInfo)1136 void DStreamOperator::InsertDCStream(int32_t streamId, std::shared_ptr<DCStreamInfo>& dcStreamInfo)
1137 {
1138 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1139 dcStreamInfoMap_.emplace(streamId, dcStreamInfo);
1140 }
1141
EraseDCStream(int32_t streamId)1142 void DStreamOperator::EraseDCStream(int32_t streamId)
1143 {
1144 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1145 dcStreamInfoMap_.erase(streamId);
1146 }
1147
ExtractNotCaptureStream(bool isStreaming,std::vector<std::shared_ptr<DCStreamInfo>> & appendStreamInfo)1148 void DStreamOperator::ExtractNotCaptureStream(bool isStreaming,
1149 std::vector<std::shared_ptr<DCStreamInfo>>& appendStreamInfo)
1150 {
1151 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1152 for (auto iter = dcStreamInfoMap_.begin(); iter != dcStreamInfoMap_.end(); iter++) {
1153 if ((isStreaming && (iter->second->type_ == DCStreamType::SNAPSHOT_FRAME)) ||
1154 (!isStreaming && (iter->second->type_ == DCStreamType::CONTINUOUS_FRAME))) {
1155 appendStreamInfo.push_back(iter->second);
1156 }
1157 }
1158 }
1159
FindEnableShutter(int32_t streamId)1160 bool DStreamOperator::FindEnableShutter(int32_t streamId)
1161 {
1162 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1163 auto iter = enableShutterCbkMap_.find(streamId);
1164 if (iter == enableShutterCbkMap_.end()) {
1165 return false;
1166 }
1167 return iter->second;
1168 }
1169
InsertEnableShutter(int32_t streamId,bool enableShutterCallback)1170 void DStreamOperator::InsertEnableShutter(int32_t streamId, bool enableShutterCallback)
1171 {
1172 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1173 enableShutterCbkMap_.emplace(streamId, enableShutterCallback);
1174 }
1175
EraseEnableShutter(int32_t streamId)1176 void DStreamOperator::EraseEnableShutter(int32_t streamId)
1177 {
1178 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1179 enableShutterCbkMap_.erase(streamId);
1180 }
1181
FindStreamCaptureBufferNum(const pair<int,int> & streamPair)1182 int32_t DStreamOperator::FindStreamCaptureBufferNum(const pair<int, int>& streamPair)
1183 {
1184 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1185 auto iter = acceptedBufferNum_.find(streamPair);
1186 if (iter == acceptedBufferNum_.end()) {
1187 return 0;
1188 }
1189 return iter->second;
1190 }
1191
AddStreamCaptureBufferNum(const pair<int,int> & streamPair)1192 void DStreamOperator::AddStreamCaptureBufferNum(const pair<int, int>& streamPair)
1193 {
1194 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1195 acceptedBufferNum_[streamPair]++;
1196 }
1197
EraseStreamCaptureBufferNum(const pair<int,int> & streamPair)1198 void DStreamOperator::EraseStreamCaptureBufferNum(const pair<int, int>& streamPair)
1199 {
1200 std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1201 acceptedBufferNum_.erase(streamPair);
1202 }
1203 } // namespace DistributedHardware
1204 } // namespace OHOS
1205