• 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 "dcamera_source_data_process.h"
17 
18 #include "anonymous_string.h"
19 #include "dcamera_hitrace_adapter.h"
20 #include "distributed_camera_constants.h"
21 #include "distributed_camera_errno.h"
22 #include "distributed_hardware_log.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
DCameraSourceDataProcess(std::string devId,std::string dhId,DCStreamType streamType)26 DCameraSourceDataProcess::DCameraSourceDataProcess(std::string devId, std::string dhId, DCStreamType streamType)
27     : devId_(devId), dhId_(dhId), streamType_(streamType), isFirstContStream_(true)
28 {
29     DHLOGI("DCameraSourceDataProcess Constructor devId %{public}s dhId %{public}s streamType %{public}d",
30         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_);
31 }
32 
~DCameraSourceDataProcess()33 DCameraSourceDataProcess::~DCameraSourceDataProcess()
34 {
35     DHLOGI("DCameraSourceDataProcess Destructor devId %{public}s dhId %{public}s streamType %{public}d",
36         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_);
37     streamProcess_.clear();
38     streamIds_.clear();
39 }
40 
FeedStream(std::vector<std::shared_ptr<DataBuffer>> & buffers)41 int32_t DCameraSourceDataProcess::FeedStream(std::vector<std::shared_ptr<DataBuffer>>& buffers)
42 {
43     if (isFirstContStream_ && streamType_ == CONTINUOUS_FRAME) {
44         DcameraFinishAsyncTrace(DCAMERA_CONTINUE_FIRST_FRAME, DCAMERA_CONTINUE_FIRST_FRAME_TASKID);
45         isFirstContStream_ = false;
46     } else if (streamType_ == SNAPSHOT_FRAME) {
47         DcameraFinishAsyncTrace(DCAMERA_SNAPSHOT_FIRST_FRAME, DCAMERA_SNAPSHOT_FIRST_FRAME_TASKID);
48     }
49     uint64_t buffersSize = static_cast<uint64_t>(buffers.size());
50     if (buffers.size() > DCAMERA_MAX_NUM) {
51         DHLOGI("DCameraSourceDataProcess FeedStream devId %{public}s dhId %{public}s size: %{public}" PRIu64
52             " over flow", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), buffersSize);
53         return DCAMERA_BAD_VALUE;
54     }
55 
56     auto buffer = *(buffers.begin());
57     CHECK_AND_RETURN_RET_LOG(buffer == nullptr, DCAMERA_BAD_VALUE, "DCameraSourceDataProcess FeedStream buffer is "
58         "nullptr");
59     buffersSize = static_cast<uint64_t>(buffer->Size());
60     DHLOGD("DCameraSourceDataProcess FeedStream devId %{public}s dhId %{public}s streamType %{public}d streamSize: "
61         "%{public}" PRIu64, GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, buffersSize);
62     std::lock_guard<std::mutex> autoLock(streamMutex_);
63     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
64         (*iter)->FeedStream(buffer);
65     }
66     return DCAMERA_OK;
67 }
68 
ConfigStreams(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfos)69 int32_t DCameraSourceDataProcess::ConfigStreams(std::vector<std::shared_ptr<DCStreamInfo>>& streamInfos)
70 {
71     uint64_t infoSize = static_cast<uint64_t>(streamInfos.size());
72     DHLOGI("DCameraSourceDataProcess ConfigStreams devId %{public}s dhId %{public}s streamType %{public}d size "
73         "%{public}" PRIu64, GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, infoSize);
74     if (streamInfos.empty()) {
75         DHLOGI("DCameraSourceDataProcess ConfigStreams is empty");
76         return DCAMERA_OK;
77     }
78     std::map<DCameraStreamConfig, std::set<int>> streamConfigs;
79     for (auto iter = streamInfos.begin(); iter != streamInfos.end(); iter++) {
80         std::shared_ptr<DCStreamInfo> streamInfo = *iter;
81         if (streamInfo == nullptr) {
82             DHLOGE("DCameraSourceDataProcess ConfigStreams streamInfo is nullptr");
83             continue;
84         }
85         DCameraStreamConfig streamConfig(streamInfo->width_, streamInfo->height_, streamInfo->format_,
86             streamInfo->dataspace_, streamInfo->encodeType_, streamInfo->type_);
87         DHLOGI("DCameraSourceDataProcess ConfigStreams devId %{public}s dhId %{public}s, streamId: %{public}d info: "
88             "width: %{public}d, height: %{public}d, format: %{public}d, dataspace: %{public}d, encodeType: "
89             "%{public}d streamType: %{public}d", GetAnonyString(devId_).c_str(),
90             GetAnonyString(dhId_).c_str(), streamInfo->streamId_, streamConfig.width_, streamConfig.height_,
91             streamConfig.format_, streamConfig.dataspace_, streamConfig.encodeType_, streamConfig.type_);
92         if (streamConfigs.find(streamConfig) == streamConfigs.end()) {
93             std::set<int> streamIdSet;
94             streamConfigs.emplace(streamConfig, streamIdSet);
95         }
96         streamConfigs[streamConfig].insert(streamInfo->streamId_);
97         streamIds_.insert(streamInfo->streamId_);
98     }
99 
100     for (auto iter = streamConfigs.begin(); iter != streamConfigs.end(); iter++) {
101         DHLOGI("DCameraSourceDataProcess ConfigStreams devId %{public}s dhId %{public}s, info: width: %{public}d, "
102             "height: %{public}d, format: %{public}d, dataspace: %{public}d, encodeType: %{public}d streamType: "
103             "%{public}d", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), iter->first.width_,
104             iter->first.height_, iter->first.format_, iter->first.dataspace_, iter->first.encodeType_,
105             iter->first.type_);
106 
107         std::shared_ptr<DCameraStreamDataProcess> streamProcess =
108             std::make_shared<DCameraStreamDataProcess>(devId_, dhId_, streamType_);
109         std::shared_ptr<DCameraStreamConfig> streamConfig =
110             std::make_shared<DCameraStreamConfig>(iter->first.width_, iter->first.height_, iter->first.format_,
111             iter->first.dataspace_, iter->first.encodeType_, iter->first.type_);
112         streamProcess->ConfigStreams(streamConfig, iter->second);
113 
114         streamProcess_.push_back(streamProcess);
115     }
116 
117     return DCAMERA_OK;
118 }
119 
ReleaseStreams(std::vector<int32_t> & streamIds)120 int32_t DCameraSourceDataProcess::ReleaseStreams(std::vector<int32_t>& streamIds)
121 {
122     DHLOGI("DCameraSourceDataProcess ReleaseStreams devId %{public}s dhId %{public}s streamType: %{public}d",
123         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_);
124     std::lock_guard<std::mutex> autoLock(streamMutex_);
125     std::set<int32_t> streamIdSet(streamIds.begin(), streamIds.end());
126     auto iter = streamProcess_.begin();
127     while (iter != streamProcess_.end()) {
128         (*iter)->ReleaseStreams(streamIdSet);
129         std::set<int32_t> processStreamIds;
130         (*iter)->GetAllStreamIds(processStreamIds);
131         if (processStreamIds.empty()) {
132             iter = streamProcess_.erase(iter);
133         } else {
134             iter++;
135         }
136     }
137 
138     std::string strStreams;
139     for (auto iterSet = streamIdSet.begin(); iterSet != streamIdSet.end(); iterSet++) {
140         strStreams += (std::to_string(*iterSet) + std::string(" "));
141         streamIds_.erase(*iterSet);
142     }
143     uint64_t processSize = static_cast<uint64_t>(streamProcess_.size());
144     DHLOGI("DCameraSourceDataProcess ReleaseStreams devId %{public}s dhId %{public}s streamType: %{public}d "
145         "streamProcessSize: %{public}" PRIu64" streams: %{public}s", GetAnonyString(devId_).c_str(),
146         GetAnonyString(dhId_).c_str(), streamType_, processSize, strStreams.c_str());
147     return DCAMERA_OK;
148 }
149 
StartCapture(std::shared_ptr<DCCaptureInfo> & captureInfo)150 int32_t DCameraSourceDataProcess::StartCapture(std::shared_ptr<DCCaptureInfo>& captureInfo)
151 {
152     DHLOGI("DCameraSourceDataProcess StartCapture devId %{public}s dhId %{public}s width: %{public}d, height: "
153         "%{public}d, format: %{public}d, isCapture: %{public}d, dataspace: %{public}d, encodeType: %{public}d, "
154         "streamType: %{public}d", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(),
155         captureInfo->width_, captureInfo->height_, captureInfo->format_, captureInfo->isCapture_,
156         captureInfo->dataspace_, captureInfo->encodeType_, captureInfo->type_);
157     if (streamType_ == CONTINUOUS_FRAME && captureInfo->isCapture_ == true) {
158         isFirstContStream_ = true;
159     }
160 
161     std::shared_ptr<DCameraStreamConfig> streamConfig =
162         std::make_shared<DCameraStreamConfig>(captureInfo->width_, captureInfo->height_, captureInfo->format_,
163         captureInfo->dataspace_, captureInfo->encodeType_, captureInfo->type_);
164     std::set<int32_t> streamIds(captureInfo->streamIds_.begin(), captureInfo->streamIds_.end());
165     for (auto iterSet = streamIds.begin(); iterSet != streamIds.end(); iterSet++) {
166         DHLOGI("DCameraSourceDataProcess StartCapture devId %{public}s dhId %{public}s StartCapture id: %{public}d",
167             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), *iterSet);
168     }
169     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
170         (*iter)->StartCapture(streamConfig, streamIds);
171     }
172     return DCAMERA_OK;
173 }
174 
StopCapture(std::vector<int32_t> & streamIds)175 int32_t DCameraSourceDataProcess::StopCapture(std::vector<int32_t>& streamIds)
176 {
177     DHLOGI("DCameraSourceDataProcess StopCapture devId %{public}s dhId %{public}s streamType: %{public}d",
178         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_);
179     std::set<int32_t> streamIdSet(streamIds.begin(), streamIds.end());
180     for (auto iterSet = streamIdSet.begin(); iterSet != streamIdSet.end(); iterSet++) {
181         DHLOGI("DCameraSourceDataProcess StopCapture devId %{public}s dhId %{public}s stream id: %{public}d",
182             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), *iterSet);
183     }
184     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
185         (*iter)->StopCapture(streamIdSet);
186     }
187     if ((streamType_ == CONTINUOUS_FRAME) && (GetProducerSize() == 0)) {
188         DestroyPipeline();
189     }
190     return DCAMERA_OK;
191 }
192 
DestroyPipeline()193 void DCameraSourceDataProcess::DestroyPipeline()
194 {
195     DHLOGI("DCameraSourceDataProcess DestroyPipeline devId %{public}s dhId %{public}s streamType: %{public}d",
196         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_);
197     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
198         (*iter)->DestroyPipeline();
199     }
200 }
201 
GetProducerSize()202 int32_t DCameraSourceDataProcess::GetProducerSize()
203 {
204     int32_t ret = 0;
205     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
206         std::shared_ptr<DCameraStreamDataProcess> streamDataProcess = *iter;
207         int32_t size = streamDataProcess->GetProducerSize();
208         ret += size;
209     }
210     DHLOGI("DCameraSourceDataProcess GetProducerSize devId %{public}s dhId %{public}s size %{public}d",
211         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), ret);
212     return ret;
213 }
214 
GetAllStreamIds(std::vector<int32_t> & streamIds)215 void DCameraSourceDataProcess::GetAllStreamIds(std::vector<int32_t>& streamIds)
216 {
217     streamIds.assign(streamIds_.begin(), streamIds_.end());
218 }
219 } // namespace DistributedHardware
220 } // namespace OHOS
221