• 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 "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         InsertNotifyCaptureMap(id);
358         auto stream = FindHalStreamById(id);
359         if (stream == nullptr) {
360             DHLOGE("Invalid stream id %d", id);
361             return CamRetCode::INVALID_ARGUMENT;
362         }
363         if (!stream->HasBufferQueue()) {
364             DHLOGE("Stream %d has not bufferQueue.", id);
365             return CamRetCode::INVALID_ARGUMENT;
366         }
367         stream->DoCapture();
368         InsertEnableShutter(id, info.enableShutterCallback_);
369         DHLOGI("DStreamOperator::DoCapture info: "+
370             "captureId=%d, streamId=%d, isStreaming=%d", captureId, id, isStreaming);
371     }
372 
373     DCamRetCode ret = NegotiateSuitableCaptureInfo(info, isStreaming);
374     if (ret != SUCCESS) {
375         DHLOGE("Negotiate suitable capture info failed.");
376         return MapToExternalRetCode(ret);
377     }
378 
379     OHOS::sptr<DCameraProvider> provider = DCameraProvider::GetInstance();
380     if (provider == nullptr) {
381         DHLOGE("Distributed camera provider not init.");
382         return CamRetCode::DEVICE_ERROR;
383     }
384     std::vector<DCCaptureInfo> captureInfos;
385     ExtractCaptureInfo(captureInfos);
386     int32_t retProv = provider->StartCapture(dhBase_, captureInfos);
387     if (retProv != SUCCESS) {
388         DHLOGE("Start distributed camera capture failed.");
389         return MapToExternalRetCode(static_cast<DCamRetCode>(retProv));
390     }
391     std::shared_ptr<CaptureInfo> captureInfo = std::make_shared<CaptureInfo>();
392     captureInfo->streamIds_.assign(info.streamIds_.begin(), info.streamIds_.end());
393     captureInfo->captureSetting_.assign(info.captureSetting_.begin(), info.captureSetting_.end());
394     captureInfo->enableShutterCallback_ = info.enableShutterCallback_;
395     InsertCaptureInfo(captureId, captureInfo);
396 
397     SetCapturing(true);
398     DHLOGI("DStreamOperator::DoCapture, start distributed camera capture success.");
399 
400     return CamRetCode::NO_ERROR;
401 }
402 
CancelCapture(int32_t captureId)403 int32_t DStreamOperator::CancelCapture(int32_t captureId)
404 {
405     if (captureId < 0) {
406         DHLOGE("DStreamOperator::CancelCapture, input captureId is valid.");
407         return CamRetCode::INVALID_ARGUMENT;
408     }
409 
410     DHLOGI("DStreamOperator::CancelCapture, cancel distributed camera capture, captureId=%d.", captureId);
411     auto halCaptureInfo = FindCaptureInfoById(captureId);
412     if (captureId < 0 || halCaptureInfo == nullptr) {
413         DHLOGE("Input captureId %d is not exist.", captureId);
414         return CamRetCode::INVALID_ARGUMENT;
415     }
416 
417     OHOS::sptr<DCameraProvider> provider = DCameraProvider::GetInstance();
418     if (provider == nullptr) {
419         DHLOGE("Distributed camera provider not init.");
420         return CamRetCode::DEVICE_ERROR;
421     }
422 
423     std::vector<int> streamIds = halCaptureInfo->streamIds_;
424     int32_t ret = provider->StopCapture(dhBase_, streamIds);
425     if (ret != SUCCESS) {
426         DHLOGE("Cancel distributed camera capture failed.");
427         return MapToExternalRetCode(static_cast<DCamRetCode>(ret));
428     }
429 
430     std::vector<CaptureEndedInfo> info;
431     for (auto id : streamIds) {
432         auto stream = FindHalStreamById(id);
433         if (stream != nullptr) {
434             stream->CancelCaptureWait();
435         }
436         CaptureEndedInfo tmp;
437         tmp.frameCount_ = FindStreamCaptureBufferNum(std::make_pair(captureId, id));
438         tmp.streamId_ = id;
439         info.push_back(tmp);
440         EraseStreamCaptureBufferNum(std::make_pair(captureId, id));
441         EraseNotifyCaptureMap(id);
442     }
443     if (dcStreamOperatorCallback_) {
444         dcStreamOperatorCallback_->OnCaptureEnded(captureId, info);
445     }
446 
447     EraseCaptureInfo(captureId);
448     if (!HasContinuousCaptureInfo(captureId)) {
449         SetCapturing(false);
450         cachedDCaptureInfoList_.clear();
451     }
452     DHLOGI("DStreamOperator::CancelCapture success, captureId=%d.", captureId);
453     return CamRetCode::NO_ERROR;
454 }
455 
HasContinuousCaptureInfo(int captureId)456 bool DStreamOperator::HasContinuousCaptureInfo(int captureId)
457 {
458     bool flag = false;
459     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
460     for (auto iter : halCaptureInfoMap_) {
461         for (auto id : iter.second->streamIds_) {
462             auto dcStreamInfo = dcStreamInfoMap_.find(id);
463             if (dcStreamInfo == dcStreamInfoMap_.end()) {
464                 continue;
465             }
466 
467             DHLOGI("DStreamOperator::HasContinuousCaptureInfo, captureId=%d, streamId=%d, streamType=%d",
468                 captureId, id, dcStreamInfo->second->type_);
469             if (dcStreamInfo->second->type_ == DCStreamType::CONTINUOUS_FRAME) {
470                 DHLOGI("DStreamOperator::HasContinuousCaptureInfo, captureId=%d, stream %d is continuous stream.",
471                     captureId, id);
472                 flag = true;
473                 break;
474             }
475         }
476     }
477     return flag;
478 }
479 
ChangeToOfflineStream(const std::vector<int32_t> & streamIds,const sptr<IStreamOperatorCallback> & callbackObj,sptr<IOfflineStreamOperator> & offlineOperator)480 int32_t DStreamOperator::ChangeToOfflineStream(const std::vector<int32_t> &streamIds,
481     const sptr<IStreamOperatorCallback> &callbackObj, sptr<IOfflineStreamOperator> &offlineOperator)
482 {
483     (void)streamIds;
484     (void)callbackObj;
485     offlineOperator = nullptr;
486     return CamRetCode::METHOD_NOT_SUPPORTED;
487 }
488 
ExtractCameraAttr(Json::Value & rootValue,std::vector<int> & formats,const std::string rootNode)489 void DStreamOperator::ExtractCameraAttr(Json::Value &rootValue, std::vector<int>& formats, const std::string rootNode)
490 {
491     for (const auto &format : formats) {
492         std::string formatStr = std::to_string(format);
493         if (!rootValue[rootNode].isMember("Resolution") || !rootValue[rootNode]["Resolution"].isMember(formatStr) ||
494             !rootValue[rootNode]["Resolution"][formatStr].isArray() ||
495             rootValue[rootNode]["Resolution"][formatStr].size() == 0 ||
496             rootValue[rootNode]["Resolution"][formatStr].size() > JSON_ARRAY_MAX_SIZE) {
497             DHLOGE("Resolution or %s error.", formatStr.c_str());
498             continue;
499         }
500         GetCameraAttr(rootValue, formatStr, rootNode, format);
501     }
502 }
503 
GetCameraAttr(Json::Value & rootValue,std::string formatStr,const std::string rootNode,int format)504 void DStreamOperator::GetCameraAttr(Json::Value &rootValue, std::string formatStr, const std::string rootNode,
505     int format)
506 {
507     std::vector<DCResolution> resolutionVec;
508     uint32_t size = rootValue[rootNode]["Resolution"][formatStr].size();
509     for (uint32_t i = 0; i < size; i++) {
510         if (!rootValue[rootNode]["Resolution"][formatStr][i].isString()) {
511             DHLOGE("Resolution %s %d ,is not string.", formatStr.c_str(), i);
512             continue;
513         }
514         std::string resoStr = rootValue[rootNode]["Resolution"][formatStr][i].asString();
515         std::vector<std::string> reso;
516         SplitString(resoStr, reso, STAR_SEPARATOR);
517         if (reso.size() != SIZE_FMT_LEN) {
518             continue;
519         }
520         uint32_t width = static_cast<uint32_t>(std::stoi(reso[0]));
521         uint32_t height = static_cast<uint32_t>(std::stoi(reso[1]));
522         if (height == 0 || width == 0 ||
523             ((rootNode == "Photo") &&
524                 ((width * height) > (MAX_SUPPORT_PHOTO_WIDTH * MAX_SUPPORT_PHOTO_HEIGHT))) ||
525             ((rootNode != "Photo") &&
526                 (width > MAX_SUPPORT_PREVIEW_WIDTH || height > MAX_SUPPORT_PREVIEW_HEIGHT))) {
527             continue;
528         }
529         DCResolution resolution(width, height);
530         resolutionVec.push_back(resolution);
531     }
532     if (!resolutionVec.empty()) {
533         std::sort(resolutionVec.begin(), resolutionVec.end());
534         if (rootNode == "Preview") {
535             dcSupportedPreviewResolutionMap_[format] = resolutionVec;
536         } else if (rootNode == "Video") {
537             dcSupportedVideoResolutionMap_[format] = resolutionVec;
538         } else if (rootNode == "Photo") {
539             dcSupportedPhotoResolutionMap_[format] = resolutionVec;
540         }
541     }
542 }
543 
InitOutputConfigurations(const DHBase & dhBase,const std::string & abilityInfo)544 DCamRetCode DStreamOperator::InitOutputConfigurations(const DHBase &dhBase, const std::string &abilityInfo)
545 {
546     dhBase_ = dhBase;
547     JSONCPP_STRING errs;
548     Json::CharReaderBuilder readerBuilder;
549     Json::Value rootValue;
550 
551     std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
552     if (!jsonReader->parse(abilityInfo.c_str(), abilityInfo.c_str() + abilityInfo.length(), &rootValue, &errs) ||
553         !rootValue.isObject()) {
554         DHLOGE("Input ablity info is not json object.");
555         return DCamRetCode::INVALID_ARGUMENT;
556     }
557 
558     if (!rootValue.isMember("CodecType") || !rootValue["CodecType"].isArray() ||
559         rootValue["CodecType"].size() == 0 || rootValue["CodecType"].size() > JSON_ARRAY_MAX_SIZE) {
560         DHLOGE("CodecType error.");
561         return DCamRetCode::INVALID_ARGUMENT;
562     }
563     uint32_t size = rootValue["CodecType"].size();
564     for (uint32_t i = 0; i < size; i++) {
565         if ((rootValue["CodecType"][i]).isString()) {
566             std::string codeType = (rootValue["CodecType"][i]).asString();
567             dcSupportedCodecType_.push_back(ConvertDCEncodeType(codeType));
568         }
569     }
570 
571     if (ParsePhotoFormats(rootValue) != SUCCESS || ParsePreviewFormats(rootValue) != SUCCESS ||
572         ParseVideoFormats(rootValue) != SUCCESS) {
573         return DCamRetCode::INVALID_ARGUMENT;
574     }
575 
576     bool resolutionMap = false;
577     if (!dcSupportedPhotoResolutionMap_.empty() || !dcSupportedPreviewResolutionMap_.empty() ||
578         !dcSupportedVideoResolutionMap_.empty()) {
579         resolutionMap = true;
580     }
581 
582     if (dcSupportedCodecType_.empty() || dcSupportedFormatMap_.empty() || !resolutionMap) {
583         DHLOGE("Input ablity info is invalid.");
584         return DEVICE_NOT_INIT;
585     }
586     return SUCCESS;
587 }
588 
ParsePhotoFormats(Json::Value & rootValue)589 DCamRetCode DStreamOperator::ParsePhotoFormats(Json::Value& rootValue)
590 {
591     if (!rootValue.isMember("Photo") || !rootValue["Photo"].isMember("OutputFormat") ||
592         !rootValue["Photo"]["OutputFormat"].isArray() || rootValue["Photo"]["OutputFormat"].size() == 0 ||
593         rootValue["Photo"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) {
594         DHLOGE("Photo or photo output format error.");
595         return DCamRetCode::INVALID_ARGUMENT;
596     }
597     std::vector<int> photoFormats;
598     uint32_t size = rootValue["Photo"]["OutputFormat"].size();
599     for (uint32_t i = 0; i < size; i++) {
600         if ((rootValue["Photo"]["OutputFormat"][i]).isInt()) {
601             photoFormats.push_back((rootValue["Photo"]["OutputFormat"][i]).asInt());
602         }
603     }
604     dcSupportedFormatMap_[DCSceneType::PHOTO] = photoFormats;
605     ExtractCameraAttr(rootValue, photoFormats, "Photo");
606     return SUCCESS;
607 }
608 
ParsePreviewFormats(Json::Value & rootValue)609 DCamRetCode DStreamOperator::ParsePreviewFormats(Json::Value& rootValue)
610 {
611     if (!rootValue.isMember("Preview") || !rootValue["Preview"].isMember("OutputFormat") ||
612         !rootValue["Preview"]["OutputFormat"].isArray() || rootValue["Preview"]["OutputFormat"].size() == 0 ||
613         rootValue["Preview"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) {
614         DHLOGE("Preview or preview output format error.");
615         return DCamRetCode::INVALID_ARGUMENT;
616     }
617     std::vector<int> previewFormats;
618     uint32_t size = rootValue["Preview"]["OutputFormat"].size();
619     for (uint32_t i = 0; i < size; i++) {
620         if ((rootValue["Preview"]["OutputFormat"][i]).isInt()) {
621             previewFormats.push_back((rootValue["Preview"]["OutputFormat"][i]).asInt());
622         }
623     }
624     dcSupportedFormatMap_[DCSceneType::PREVIEW] = previewFormats;
625     ExtractCameraAttr(rootValue, previewFormats, "Preview");
626     return SUCCESS;
627 }
628 
ParseVideoFormats(Json::Value & rootValue)629 DCamRetCode DStreamOperator::ParseVideoFormats(Json::Value& rootValue)
630 {
631     if (!rootValue.isMember("Video") || !rootValue["Video"].isMember("OutputFormat") ||
632         !rootValue["Video"]["OutputFormat"].isArray() || rootValue["Video"]["OutputFormat"].size() == 0 ||
633         rootValue["Video"]["OutputFormat"].size() > JSON_ARRAY_MAX_SIZE) {
634         DHLOGE("Video or video output format error.");
635         return DCamRetCode::INVALID_ARGUMENT;
636     }
637     std::vector<int> videoFormats;
638     uint32_t size = rootValue["Video"]["OutputFormat"].size();
639     for (uint32_t i = 0; i < size; i++) {
640         if ((rootValue["Video"]["OutputFormat"][i]).isInt()) {
641             videoFormats.push_back((rootValue["Video"]["OutputFormat"][i]).asInt());
642         }
643     }
644     dcSupportedFormatMap_[DCSceneType::VIDEO] = videoFormats;
645     ExtractCameraAttr(rootValue, videoFormats, "Video");
646     return SUCCESS;
647 }
648 
AcquireBuffer(int streamId,DCameraBuffer & buffer)649 DCamRetCode DStreamOperator::AcquireBuffer(int streamId, DCameraBuffer &buffer)
650 {
651     if (!IsCapturing()) {
652         DHLOGE("Not in capturing state, can not acquire buffer.");
653         return DCamRetCode::CAMERA_OFFLINE;
654     }
655 
656     auto stream = FindHalStreamById(streamId);
657     if (stream == nullptr) {
658         DHLOGE("streamId %d is invalid, can not acquire buffer.", streamId);
659         return DCamRetCode::INVALID_ARGUMENT;
660     }
661 
662     DCamRetCode ret = stream->GetDCameraBuffer(buffer);
663     if (ret == DCamRetCode::EXCEED_MAX_NUMBER) {
664         DHLOGE("Buffer list is full, cannot get new idle buffer.");
665     } else if (ret == DCamRetCode::INVALID_ARGUMENT) {
666         DHLOGE("Get distributed camera buffer failed, invalid buffer parameter.");
667     }
668     return ret;
669 }
670 
ShutterBuffer(int streamId,const DCameraBuffer & buffer)671 DCamRetCode DStreamOperator::ShutterBuffer(int streamId, const DCameraBuffer &buffer)
672 {
673     DHLOGD("DStreamOperator::ShutterBuffer begin shutter buffer for streamId = %d", streamId);
674 
675     int32_t captureId = FindCaptureIdByStreamId(streamId);
676     if (captureId == -1) {
677         DHLOGE("ShutterBuffer failed, invalid streamId = %d", streamId);
678         return DCamRetCode::INVALID_ARGUMENT;
679     }
680 
681     auto iter = notifyCaptureStartedMap_.find(streamId);
682     if (iter != notifyCaptureStartedMap_.end()) {
683         if (!iter->second && dcStreamOperatorCallback_ != nullptr) {
684             vector<int> tmpStreamIds;
685             tmpStreamIds.push_back(streamId);
686             dcStreamOperatorCallback_->OnCaptureStarted(captureId, tmpStreamIds);
687             iter->second = true;
688         }
689     }
690 
691     auto stream = FindHalStreamById(streamId);
692     if (stream != nullptr) {
693         DCamRetCode ret = stream->ReturnDCameraBuffer(buffer);
694         if (ret != DCamRetCode::SUCCESS) {
695             DHLOGE("Flush distributed camera buffer failed.");
696             return ret;
697         }
698         AddStreamCaptureBufferNum(std::make_pair(captureId, streamId));
699 
700         SnapShotStreamOnCaptureEnded(captureId, streamId);
701     }
702 
703     uint64_t resultTimestamp = GetCurrentLocalTimeStamp();
704     dMetadataProcessor_->UpdateResultMetadata(resultTimestamp);
705 
706     bool enableShutter = FindEnableShutter(streamId);
707     if (!enableShutter) {
708         if (dcStreamOperatorCallback_ == nullptr) {
709             DHLOGE("DStreamOperator::ShutterBuffer failed, need shutter frame, but stream operator callback is null.");
710             return DCamRetCode::FAILED;
711         }
712         std::vector<int32_t> streamIds;
713         streamIds.push_back(streamId);
714         dcStreamOperatorCallback_->OnFrameShutter(captureId, streamIds, resultTimestamp);
715     }
716     return DCamRetCode::SUCCESS;
717 }
718 
SetCallBack(OHOS::sptr<IStreamOperatorCallback> const & callback)719 DCamRetCode DStreamOperator::SetCallBack(OHOS::sptr<IStreamOperatorCallback> const &callback)
720 {
721     dcStreamOperatorCallback_ = callback;
722     return SUCCESS;
723 }
724 
SetDeviceCallback(std::function<void (ErrorType,int)> & errorCbk,std::function<void (uint64_t,std::shared_ptr<OHOS::Camera::CameraMetadata>)> & resultCbk)725 DCamRetCode DStreamOperator::SetDeviceCallback(
726     std::function<void(ErrorType, int)> &errorCbk,
727     std::function<void(uint64_t, std::shared_ptr<OHOS::Camera::CameraMetadata>)> &resultCbk)
728 {
729     errorCallback_ = errorCbk;
730     dMetadataProcessor_->SetResultCallback(resultCbk);
731     return SUCCESS;
732 }
733 
SnapShotStreamOnCaptureEnded(int32_t captureId,int streamId)734 void DStreamOperator::SnapShotStreamOnCaptureEnded(int32_t captureId, int streamId)
735 {
736     auto dcStreamInfo = FindDCStreamById(streamId);
737     if (dcStreamInfo == nullptr) {
738         return;
739     }
740     if (dcStreamInfo->type_ != DCStreamType::SNAPSHOT_FRAME) {
741         return;
742     }
743     if (dcStreamOperatorCallback_ == nullptr) {
744         return;
745     }
746     std::vector<CaptureEndedInfo> info;
747     CaptureEndedInfo tmp;
748     tmp.frameCount_ = FindStreamCaptureBufferNum(std::make_pair(captureId, streamId));
749     tmp.streamId_ = streamId;
750     info.push_back(tmp);
751     dcStreamOperatorCallback_->OnCaptureEnded(captureId, info);
752     DHLOGD("snapshot stream successfully reported captureId = %d streamId = %d.", captureId, streamId);
753 }
754 
Release()755 void DStreamOperator::Release()
756 {
757     DHLOGI("DStreamOperator::Release, begin release stream operator.");
758 
759     std::vector<int> streamIds = GetStreamIds();
760     SetCapturing(false);
761     ReleaseStreams(streamIds);
762     if (latestStreamSetting_) {
763         latestStreamSetting_ = nullptr;
764     }
765     {
766         std::lock_guard<std::mutex> lockStream(halStreamLock_);
767         halStreamMap_.clear();
768     }
769     std::lock_guard<std::mutex> lock(streamAttrLock_);
770     dcStreamInfoMap_.clear();
771     halCaptureInfoMap_.clear();
772     enableShutterCbkMap_.clear();
773     acceptedBufferNum_.clear();
774     cachedDCaptureInfoList_.clear();
775     notifyCaptureStartedMap_.clear();
776     dcStreamOperatorCallback_ = nullptr;
777 }
778 
GetStreamIds()779 std::vector<int> DStreamOperator::GetStreamIds()
780 {
781     DHLOGI("DStreamOperator::GetStreamIds, begin get stream id.");
782     std::lock_guard<std::mutex> autoLock(halStreamLock_);
783     std::vector<int> streamIds;
784     std::string idString = "";
785     for (auto iter : halStreamMap_) {
786         streamIds.push_back(iter.first);
787         idString += (std::to_string(iter.first) + ", ");
788     }
789     DHLOGI("DStreamOperator::GetStreamIds, ids=[%s]", idString.c_str());
790     return streamIds;
791 }
792 
IsCapturing()793 bool DStreamOperator::IsCapturing()
794 {
795     std::unique_lock<mutex> lock(isCapturingLock_);
796     return isCapturing_;
797 }
798 
SetCapturing(bool isCapturing)799 void DStreamOperator::SetCapturing(bool isCapturing)
800 {
801     std::unique_lock<mutex> lock(isCapturingLock_);
802     isCapturing_ = isCapturing;
803 }
804 
ConvertStreamInfo(const StreamInfo & srcInfo,std::shared_ptr<DCStreamInfo> & dstInfo)805 void DStreamOperator::ConvertStreamInfo(const StreamInfo &srcInfo, std::shared_ptr<DCStreamInfo> &dstInfo)
806 {
807     dstInfo->streamId_ = srcInfo.streamId_;
808     dstInfo->width_ = srcInfo.width_;
809     dstInfo->stride_ = srcInfo.width_;
810     dstInfo->height_ = srcInfo.height_;
811     dstInfo->dataspace_ = srcInfo.dataspace_;
812     dstInfo->encodeType_ = (DCEncodeType)srcInfo.encodeType_;
813 
814     if ((srcInfo.intent_ == STILL_CAPTURE) || (srcInfo.intent_ == POST_VIEW)) {
815         dstInfo->type_ = DCStreamType::SNAPSHOT_FRAME;
816         if (srcInfo.format_ == PIXEL_FMT_RGBA_8888) {
817             dstInfo->format_ = OHOS_CAMERA_FORMAT_RGBA_8888;
818         } else if (srcInfo.format_ == PIXEL_FMT_YCRCB_420_SP) {
819             dstInfo->format_ = OHOS_CAMERA_FORMAT_JPEG;
820         } else if (dstInfo->encodeType_ == DCEncodeType::ENCODE_TYPE_NULL) {
821             dstInfo->format_ = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
822         }
823     } else {
824         // Distributed Camera Do Not Support Encoding at HAL
825         dstInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_NULL;
826         dstInfo->type_ = DCStreamType::CONTINUOUS_FRAME;
827         dstInfo->format_ =
828             static_cast<int>(DBufferManager::PixelFormatToDCameraFormat(static_cast<PixelFormat>(srcInfo.format_)));
829     }
830 }
831 
NegotiateSuitableCaptureInfo(const CaptureInfo & srcCaptureInfo,bool isStreaming)832 DCamRetCode DStreamOperator::NegotiateSuitableCaptureInfo(const CaptureInfo& srcCaptureInfo, bool isStreaming)
833 {
834     for (auto streamId : srcCaptureInfo.streamIds_) {
835         DHLOGI("DStreamOperator::NegotiateSuitableCaptureInfo, streamId=%d, isStreaming=%d", streamId, isStreaming);
836     }
837 
838     std::shared_ptr<DCCaptureInfo> inputCaptureInfo = nullptr;
839     DCamRetCode ret = GetInputCaptureInfo(srcCaptureInfo, isStreaming, inputCaptureInfo);
840     if (ret != DCamRetCode::SUCCESS) {
841         DHLOGE("Negotiate input capture info failed.");
842         return ret;
843     }
844 
845     std::shared_ptr<DCCaptureInfo> appendCaptureInfo = nullptr;
846     AppendCaptureInfo(appendCaptureInfo, isStreaming, inputCaptureInfo, srcCaptureInfo);
847     cachedDCaptureInfoList_.clear();
848     cachedDCaptureInfoList_.push_back(inputCaptureInfo);
849     if (appendCaptureInfo != nullptr) {
850         cachedDCaptureInfoList_.push_back(appendCaptureInfo);
851     }
852 
853     for (auto info : cachedDCaptureInfoList_) {
854         std::string idString = "";
855         for (auto id : info->streamIds_) {
856             idString += (std::to_string(id) + ", ");
857         }
858         DHLOGI("cachedDCaptureInfo: ids=%s width=%d, height=%d, format=%d, dataspace=%d, isCapture=%d," +
859             "encodeType=%d, streamType=%d", idString.c_str(), info->width_, info->height_, info->format_,
860             info->dataspace_, info->isCapture_, info->encodeType_, info->type_);
861     }
862     return SUCCESS;
863 }
864 
AppendCaptureInfo(std::shared_ptr<DCCaptureInfo> & appendCaptureInfo,bool isStreaming,std::shared_ptr<DCCaptureInfo> & inputCaptureInfo,const CaptureInfo & srcCaptureInfo)865 void DStreamOperator::AppendCaptureInfo(std::shared_ptr<DCCaptureInfo> &appendCaptureInfo, bool isStreaming,
866     std::shared_ptr<DCCaptureInfo> &inputCaptureInfo, const CaptureInfo& srcCaptureInfo)
867 {
868     if (cachedDCaptureInfoList_.empty()) {
869         std::vector<std::shared_ptr<DCStreamInfo>> appendStreamInfo;
870         ExtractNotCaptureStream(isStreaming, appendStreamInfo);
871         if (!appendStreamInfo.empty()) {
872             appendCaptureInfo = BuildSuitableCaptureInfo(srcCaptureInfo, appendStreamInfo);
873             appendCaptureInfo->type_ = isStreaming ? DCStreamType::SNAPSHOT_FRAME : DCStreamType::CONTINUOUS_FRAME;
874             appendCaptureInfo->isCapture_ = false;
875         }
876     } else {
877         for (auto cacheCapture : cachedDCaptureInfoList_) {
878             if ((isStreaming && (cacheCapture->type_ == DCStreamType::SNAPSHOT_FRAME)) ||
879                 (!isStreaming && (cacheCapture->type_ == DCStreamType::CONTINUOUS_FRAME))) {
880                 cacheCapture->isCapture_ = false;
881                 appendCaptureInfo = cacheCapture;
882                 break;
883             }
884         }
885         if (inputCaptureInfo->type_ == DCStreamType::SNAPSHOT_FRAME) {
886             ChooseSuitableStreamId(appendCaptureInfo);
887         }
888         inputCaptureInfo->isCapture_ = isStreaming ? false : true;
889     }
890 }
891 
GetInputCaptureInfo(const CaptureInfo & srcCaptureInfo,bool isStreaming,std::shared_ptr<DCCaptureInfo> & inputCaptureInfo)892 DCamRetCode DStreamOperator::GetInputCaptureInfo(const CaptureInfo& srcCaptureInfo, bool isStreaming,
893     std::shared_ptr<DCCaptureInfo>& inputCaptureInfo)
894 {
895     std::vector<std::shared_ptr<DCStreamInfo>> srcStreamInfo;
896     for (auto &id : srcCaptureInfo.streamIds_) {
897         auto dcStreamInfo = FindDCStreamById(id);
898         if (dcStreamInfo != nullptr) {
899             srcStreamInfo.push_back(dcStreamInfo);
900         }
901     }
902 
903     if (srcStreamInfo.empty()) {
904         DHLOGE("Input source stream info vector is empty.");
905         return DCamRetCode::INVALID_ARGUMENT;
906     }
907 
908     inputCaptureInfo = BuildSuitableCaptureInfo(srcCaptureInfo, srcStreamInfo);
909     inputCaptureInfo->type_ = isStreaming ? DCStreamType::CONTINUOUS_FRAME : DCStreamType::SNAPSHOT_FRAME;
910     inputCaptureInfo->isCapture_ = true;
911     return DCamRetCode::SUCCESS;
912 }
913 
BuildSuitableCaptureInfo(const CaptureInfo & srcCaptureInfo,std::vector<std::shared_ptr<DCStreamInfo>> & srcStreamInfo)914 std::shared_ptr<DCCaptureInfo> DStreamOperator::BuildSuitableCaptureInfo(const CaptureInfo& srcCaptureInfo,
915     std::vector<std::shared_ptr<DCStreamInfo>> &srcStreamInfo)
916 {
917     std::shared_ptr<DCCaptureInfo> captureInfo = std::make_shared<DCCaptureInfo>();
918 
919     ChooseSuitableFormat(srcStreamInfo, captureInfo);
920     ChooseSuitableResolution(srcStreamInfo, captureInfo);
921     ChooseSuitableDataSpace(srcStreamInfo, captureInfo);
922     ChooseSuitableEncodeType(srcStreamInfo, captureInfo);
923 
924     DCameraSettings dcSetting;
925     dcSetting.type_ = DCSettingsType::UPDATE_METADATA;
926     std::shared_ptr<OHOS::Camera::CameraMetadata> captureSetting = nullptr;
927     OHOS::Camera::MetadataUtils::ConvertVecToMetadata(srcCaptureInfo.captureSetting_, captureSetting);
928     std::string settingStr = OHOS::Camera::MetadataUtils::EncodeToString(captureSetting);
929     dcSetting.value_ = Base64Encode(reinterpret_cast<const unsigned char *>(settingStr.c_str()), settingStr.length());
930 
931     captureInfo->captureSettings_.push_back(dcSetting);
932 
933     return captureInfo;
934 }
935 
ChooseSuitableFormat(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfo,std::shared_ptr<DCCaptureInfo> & captureInfo)936 void DStreamOperator::ChooseSuitableFormat(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
937     std::shared_ptr<DCCaptureInfo> &captureInfo)
938 {
939     for (auto stream : streamInfo) {
940         if ((streamInfo.at(0)->type_ == DCStreamType::CONTINUOUS_FRAME &&
941             dcSupportedVideoResolutionMap_.count(stream->format_) > 0) ||
942             (streamInfo.at(0)->type_ == DCStreamType::SNAPSHOT_FRAME &&
943             dcSupportedPhotoResolutionMap_.count(stream->format_) > 0)) {
944             captureInfo->format_ = stream->format_;
945             return;
946         }
947     }
948     if ((streamInfo.at(0))->type_ == DCStreamType::CONTINUOUS_FRAME) {
949         if (dcSupportedFormatMap_.count(DCSceneType::PREVIEW) > 0) {
950             captureInfo->format_ = dcSupportedFormatMap_[DCSceneType::PREVIEW].at(0);
951         } else if (dcSupportedFormatMap_.count(DCSceneType::VIDEO) > 0) {
952             captureInfo->format_ = dcSupportedFormatMap_[DCSceneType::VIDEO].at(0);
953         } else {
954             captureInfo->format_ = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
955         }
956     } else {
957         if (dcSupportedFormatMap_.count(DCSceneType::PHOTO) > 0) {
958             captureInfo->format_ = dcSupportedFormatMap_[DCSceneType::PHOTO].at(0);
959         } else {
960             if ((streamInfo.at(0))->encodeType_ == DCEncodeType::ENCODE_TYPE_JPEG) {
961                 captureInfo->format_ = OHOS_CAMERA_FORMAT_JPEG;
962             } else {
963                 captureInfo->format_ = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
964             }
965         }
966     }
967 }
968 
ChooseSuitableResolution(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfo,std::shared_ptr<DCCaptureInfo> & captureInfo)969 void DStreamOperator::ChooseSuitableResolution(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
970     std::shared_ptr<DCCaptureInfo> &captureInfo)
971 {
972     if (captureInfo == nullptr) {
973         DHLOGE("DStreamOperator::ChooseSuitableResolution, captureInfo is null.");
974         return;
975     }
976 
977     std::vector<DCResolution> supportedResolutionList;
978     if ((streamInfo.at(0))->type_ == DCStreamType::CONTINUOUS_FRAME) {
979         supportedResolutionList = dcSupportedVideoResolutionMap_[captureInfo->format_];
980     } else {
981         supportedResolutionList = dcSupportedPhotoResolutionMap_[captureInfo->format_];
982     }
983 
984     for (auto stream : streamInfo) {
985         captureInfo->streamIds_.push_back(stream->streamId_);
986     }
987 
988     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
989     DCResolution tempResolution = { 0, 0 };
990     for (auto iter : dcStreamInfoMap_) {
991         if (iter.second->type_ != (streamInfo.at(0))->type_) {
992             continue;
993         }
994         for (auto resolution : supportedResolutionList) {
995             if ((resolution.width_ == iter.second->width_) &&
996                 (resolution.height_ == iter.second->height_) &&
997                 (tempResolution < resolution)) {
998                 tempResolution = resolution;
999                 break;
1000             }
1001         }
1002     }
1003 
1004     if ((tempResolution.width_ == 0) || (tempResolution.height_ == 0)) {
1005         captureInfo->width_ = MAX_SUPPORT_PREVIEW_WIDTH;
1006         captureInfo->height_ = MAX_SUPPORT_PREVIEW_HEIGHT;
1007     } else {
1008         captureInfo->width_ = tempResolution.width_;
1009         captureInfo->height_ = tempResolution.height_;
1010     }
1011 }
1012 
ChooseSuitableDataSpace(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfo,std::shared_ptr<DCCaptureInfo> & captureInfo)1013 void DStreamOperator::ChooseSuitableDataSpace(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
1014     std::shared_ptr<DCCaptureInfo> &captureInfo)
1015 {
1016     captureInfo->dataspace_ = (streamInfo.at(0))->dataspace_;
1017 }
1018 
ChooseSuitableEncodeType(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfo,std::shared_ptr<DCCaptureInfo> & captureInfo)1019 void DStreamOperator::ChooseSuitableEncodeType(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
1020     std::shared_ptr<DCCaptureInfo> &captureInfo)
1021 {
1022     if ((streamInfo.at(0))->type_ == DCStreamType::CONTINUOUS_FRAME) {
1023         if (count(dcSupportedCodecType_.begin(), dcSupportedCodecType_.end(),
1024             DCEncodeType::ENCODE_TYPE_H265)) {
1025             captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_H265;
1026         } else if (count(dcSupportedCodecType_.begin(), dcSupportedCodecType_.end(),
1027             DCEncodeType::ENCODE_TYPE_H264)) {
1028             captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_H264;
1029         } else if (count(dcSupportedCodecType_.begin(), dcSupportedCodecType_.end(),
1030             DCEncodeType::ENCODE_TYPE_MPEG4_ES)) {
1031             captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_MPEG4_ES;
1032         } else {
1033             captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_NULL;
1034         }
1035     } else {
1036         if ((streamInfo.at(0))->encodeType_ == DCEncodeType::ENCODE_TYPE_JPEG) {
1037             captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_JPEG;
1038         } else {
1039             captureInfo->encodeType_ = DCEncodeType::ENCODE_TYPE_NULL;
1040         }
1041     }
1042 }
1043 
ChooseSuitableStreamId(std::shared_ptr<DCCaptureInfo> & captureInfo)1044 void DStreamOperator::ChooseSuitableStreamId(std::shared_ptr<DCCaptureInfo> &captureInfo)
1045 {
1046     if (captureInfo == nullptr) {
1047         DHLOGE("DStreamOperator::ChooseSuitableStreamId captureInfo is null");
1048         return;
1049     }
1050 
1051     captureInfo->streamIds_.clear();
1052     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1053     for (auto iter : halCaptureInfoMap_) {
1054         for (auto id : iter.second->streamIds_) {
1055             auto dcStreamInfo = dcStreamInfoMap_.find(id);
1056             if (dcStreamInfo == dcStreamInfoMap_.end()) {
1057                 continue;
1058             }
1059 
1060             if (dcStreamInfo->second->type_ == DCStreamType::CONTINUOUS_FRAME) {
1061                 DHLOGI("DStreamOperator::ChooseSuitableStreamId, streamId: %d", id);
1062                 captureInfo->streamIds_.push_back(id);
1063             }
1064         }
1065     }
1066 }
1067 
ConvertDCEncodeType(std::string & srcEncodeType)1068 DCEncodeType DStreamOperator::ConvertDCEncodeType(std::string &srcEncodeType)
1069 {
1070     DHLOGI("DStreamOperator::ConvertDCEncodeType %s", srcEncodeType.c_str());
1071     if (srcEncodeType == ENCODE_TYPE_STR_H264) {
1072         return DCEncodeType::ENCODE_TYPE_H264;
1073     } else if (srcEncodeType == ENCODE_TYPE_STR_H265) {
1074         return DCEncodeType::ENCODE_TYPE_H265;
1075     } else if (srcEncodeType == ENCODE_TYPE_STR_JPEG) {
1076         return DCEncodeType::ENCODE_TYPE_JPEG;
1077     }  else if (srcEncodeType == ENCODE_TYPE_STR_MPEG4_ES) {
1078         return DCEncodeType::ENCODE_TYPE_MPEG4_ES;
1079     } else {
1080         return DCEncodeType::ENCODE_TYPE_NULL;
1081     }
1082 }
1083 
FindHalStreamById(int32_t streamId)1084 std::shared_ptr<DCameraStream> DStreamOperator::FindHalStreamById(int32_t streamId)
1085 {
1086     std::lock_guard<std::mutex> autoLock(halStreamLock_);
1087     auto iter = halStreamMap_.find(streamId);
1088     if (iter == halStreamMap_.end()) {
1089         return nullptr;
1090     }
1091     return iter->second;
1092 }
1093 
InsertHalStream(int32_t streamId,std::shared_ptr<DCameraStream> & dcStream)1094 void DStreamOperator::InsertHalStream(int32_t streamId, std::shared_ptr<DCameraStream>& dcStream)
1095 {
1096     std::lock_guard<std::mutex> autoLock(halStreamLock_);
1097     halStreamMap_.emplace(streamId, dcStream);
1098 }
1099 
EraseHalStream(int32_t streamId)1100 void DStreamOperator::EraseHalStream(int32_t streamId)
1101 {
1102     std::lock_guard<std::mutex> autoLock(halStreamLock_);
1103     halStreamMap_.erase(streamId);
1104 }
1105 
FindCaptureInfoById(int32_t captureId)1106 std::shared_ptr<CaptureInfo> DStreamOperator::FindCaptureInfoById(int32_t captureId)
1107 {
1108     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1109     auto iter = halCaptureInfoMap_.find(captureId);
1110     if (iter == halCaptureInfoMap_.end()) {
1111         return nullptr;
1112     }
1113     return iter->second;
1114 }
1115 
InsertCaptureInfo(int32_t captureId,std::shared_ptr<CaptureInfo> & captureInfo)1116 void DStreamOperator::InsertCaptureInfo(int32_t captureId, std::shared_ptr<CaptureInfo>& captureInfo)
1117 {
1118     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1119     halCaptureInfoMap_.emplace(captureId, captureInfo);
1120 }
1121 
FindCaptureIdByStreamId(int32_t streamId)1122 int32_t DStreamOperator::FindCaptureIdByStreamId(int32_t streamId)
1123 {
1124     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1125     int32_t captureId = -1;
1126     for (auto iter = halCaptureInfoMap_.begin(); iter != halCaptureInfoMap_.end(); iter++) {
1127         std::shared_ptr<CaptureInfo> captureInfo = iter->second;
1128         std::vector<int> streamIds = captureInfo->streamIds_;
1129         if (std::find(streamIds.begin(), streamIds.end(), streamId) != streamIds.end()) {
1130             captureId = iter->first;
1131             break;
1132         }
1133     }
1134     return captureId;
1135 }
1136 
EraseCaptureInfo(int32_t captureId)1137 void DStreamOperator::EraseCaptureInfo(int32_t captureId)
1138 {
1139     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1140     halCaptureInfoMap_.erase(captureId);
1141 }
1142 
FindDCStreamById(int32_t streamId)1143 std::shared_ptr<DCStreamInfo> DStreamOperator::FindDCStreamById(int32_t streamId)
1144 {
1145     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1146     auto iter = dcStreamInfoMap_.find(streamId);
1147     if (iter == dcStreamInfoMap_.end()) {
1148         return nullptr;
1149     }
1150     return iter->second;
1151 }
1152 
InsertDCStream(int32_t streamId,std::shared_ptr<DCStreamInfo> & dcStreamInfo)1153 void DStreamOperator::InsertDCStream(int32_t streamId, std::shared_ptr<DCStreamInfo>& dcStreamInfo)
1154 {
1155     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1156     dcStreamInfoMap_.emplace(streamId, dcStreamInfo);
1157 }
1158 
EraseDCStream(int32_t streamId)1159 void DStreamOperator::EraseDCStream(int32_t streamId)
1160 {
1161     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1162     dcStreamInfoMap_.erase(streamId);
1163 }
1164 
ExtractNotCaptureStream(bool isStreaming,std::vector<std::shared_ptr<DCStreamInfo>> & appendStreamInfo)1165 void DStreamOperator::ExtractNotCaptureStream(bool isStreaming,
1166     std::vector<std::shared_ptr<DCStreamInfo>>& appendStreamInfo)
1167 {
1168     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1169     for (auto iter = dcStreamInfoMap_.begin(); iter != dcStreamInfoMap_.end(); iter++) {
1170         if ((isStreaming && (iter->second->type_ == DCStreamType::SNAPSHOT_FRAME)) ||
1171             (!isStreaming && (iter->second->type_ == DCStreamType::CONTINUOUS_FRAME))) {
1172             appendStreamInfo.push_back(iter->second);
1173         }
1174     }
1175 }
1176 
FindEnableShutter(int32_t streamId)1177 bool DStreamOperator::FindEnableShutter(int32_t streamId)
1178 {
1179     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1180     auto iter = enableShutterCbkMap_.find(streamId);
1181     if (iter == enableShutterCbkMap_.end()) {
1182         return false;
1183     }
1184     return iter->second;
1185 }
1186 
InsertEnableShutter(int32_t streamId,bool enableShutterCallback)1187 void DStreamOperator::InsertEnableShutter(int32_t streamId, bool enableShutterCallback)
1188 {
1189     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1190     enableShutterCbkMap_.emplace(streamId, enableShutterCallback);
1191 }
1192 
EraseEnableShutter(int32_t streamId)1193 void DStreamOperator::EraseEnableShutter(int32_t streamId)
1194 {
1195     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1196     enableShutterCbkMap_.erase(streamId);
1197 }
1198 
FindStreamCaptureBufferNum(const pair<int,int> & streamPair)1199 int32_t DStreamOperator::FindStreamCaptureBufferNum(const pair<int, int>& streamPair)
1200 {
1201     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1202     auto iter = acceptedBufferNum_.find(streamPair);
1203     if (iter == acceptedBufferNum_.end()) {
1204         return 0;
1205     }
1206     return iter->second;
1207 }
1208 
AddStreamCaptureBufferNum(const pair<int,int> & streamPair)1209 void DStreamOperator::AddStreamCaptureBufferNum(const pair<int, int>& streamPair)
1210 {
1211     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1212     acceptedBufferNum_[streamPair]++;
1213 }
1214 
EraseStreamCaptureBufferNum(const pair<int,int> & streamPair)1215 void DStreamOperator::EraseStreamCaptureBufferNum(const pair<int, int>& streamPair)
1216 {
1217     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1218     acceptedBufferNum_.erase(streamPair);
1219 }
1220 
InsertNotifyCaptureMap(int32_t streamId)1221 void DStreamOperator::InsertNotifyCaptureMap(int32_t streamId)
1222 {
1223     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1224     notifyCaptureStartedMap_.emplace(streamId, false);
1225 }
1226 
EraseNotifyCaptureMap(int32_t streamId)1227 void DStreamOperator::EraseNotifyCaptureMap(int32_t streamId)
1228 {
1229     std::lock_guard<std::mutex> autoLock(streamAttrLock_);
1230     notifyCaptureStartedMap_.erase(streamId);
1231 }
1232 } // namespace DistributedHardware
1233 } // namespace OHOS
1234