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     buffersSize = static_cast<uint64_t>(buffer->Size());
58     DHLOGD("DCameraSourceDataProcess FeedStream devId %{public}s dhId %{public}s streamType %{public}d streamSize: "
59         "%{public}" PRIu64, GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, buffersSize);
60     std::lock_guard<std::mutex> autoLock(streamMutex_);
61     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
62         (*iter)->FeedStream(buffer);
63     }
64     return DCAMERA_OK;
65 }
66 
ConfigStreams(std::vector<std::shared_ptr<DCStreamInfo>> & streamInfos)67 int32_t DCameraSourceDataProcess::ConfigStreams(std::vector<std::shared_ptr<DCStreamInfo>>& streamInfos)
68 {
69     uint64_t infoSize = static_cast<uint64_t>(streamInfos.size());
70     DHLOGI("DCameraSourceDataProcess ConfigStreams devId %{public}s dhId %{public}s streamType %{public}d size "
71         "%{public}" PRIu64, GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, infoSize);
72     if (streamInfos.empty()) {
73         DHLOGI("DCameraSourceDataProcess ConfigStreams is empty");
74         return DCAMERA_OK;
75     }
76     std::map<DCameraStreamConfig, std::set<int>> streamConfigs;
77     for (auto iter = streamInfos.begin(); iter != streamInfos.end(); iter++) {
78         std::shared_ptr<DCStreamInfo> streamInfo = *iter;
79         DCameraStreamConfig streamConfig(streamInfo->width_, streamInfo->height_, streamInfo->format_,
80             streamInfo->dataspace_, streamInfo->encodeType_, streamInfo->type_);
81         DHLOGI("DCameraSourceDataProcess ConfigStreams devId %{public}s dhId %{public}s, streamId: %{public}d info: "
82             "width: %{public}d, height: %{public}d, format: %{public}d, dataspace: %{public}d, encodeType: "
83             "%{public}d streamType: %{public}d", GetAnonyString(devId_).c_str(),
84             GetAnonyString(dhId_).c_str(), streamInfo->streamId_, streamConfig.width_, streamConfig.height_,
85             streamConfig.format_, streamConfig.dataspace_, streamConfig.encodeType_, streamConfig.type_);
86         if (streamConfigs.find(streamConfig) == streamConfigs.end()) {
87             std::set<int> streamIdSet;
88             streamConfigs.emplace(streamConfig, streamIdSet);
89         }
90         streamConfigs[streamConfig].insert(streamInfo->streamId_);
91         streamIds_.insert(streamInfo->streamId_);
92     }
93 
94     for (auto iter = streamConfigs.begin(); iter != streamConfigs.end(); iter++) {
95         DHLOGI("DCameraSourceDataProcess ConfigStreams devId %{public}s dhId %{public}s, info: width: %{public}d, "
96             "height: %{public}d, format: %{public}d, dataspace: %{public}d, encodeType: %{public}d streamType: "
97             "%{public}d", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), iter->first.width_,
98             iter->first.height_, iter->first.format_, iter->first.dataspace_, iter->first.encodeType_,
99             iter->first.type_);
100 
101         std::shared_ptr<DCameraStreamDataProcess> streamProcess =
102             std::make_shared<DCameraStreamDataProcess>(devId_, dhId_, streamType_);
103         std::shared_ptr<DCameraStreamConfig> streamConfig =
104             std::make_shared<DCameraStreamConfig>(iter->first.width_, iter->first.height_, iter->first.format_,
105             iter->first.dataspace_, iter->first.encodeType_, iter->first.type_);
106         streamProcess->ConfigStreams(streamConfig, iter->second);
107 
108         streamProcess_.push_back(streamProcess);
109     }
110 
111     return DCAMERA_OK;
112 }
113 
ReleaseStreams(std::vector<int32_t> & streamIds)114 int32_t DCameraSourceDataProcess::ReleaseStreams(std::vector<int32_t>& streamIds)
115 {
116     DHLOGI("DCameraSourceDataProcess ReleaseStreams devId %{public}s dhId %{public}s streamType: %{public}d",
117         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_);
118     std::lock_guard<std::mutex> autoLock(streamMutex_);
119     std::set<int32_t> streamIdSet(streamIds.begin(), streamIds.end());
120     auto iter = streamProcess_.begin();
121     while (iter != streamProcess_.end()) {
122         (*iter)->ReleaseStreams(streamIdSet);
123         std::set<int32_t> processStreamIds;
124         (*iter)->GetAllStreamIds(processStreamIds);
125         if (processStreamIds.empty()) {
126             iter = streamProcess_.erase(iter);
127         } else {
128             iter++;
129         }
130     }
131 
132     std::string strStreams;
133     for (auto iterSet = streamIdSet.begin(); iterSet != streamIdSet.end(); iterSet++) {
134         strStreams += (std::to_string(*iterSet) + std::string(" "));
135         streamIds_.erase(*iterSet);
136     }
137     uint64_t processSize = static_cast<uint64_t>(streamProcess_.size());
138     DHLOGI("DCameraSourceDataProcess ReleaseStreams devId %{public}s dhId %{public}s streamType: %{public}d "
139         "streamProcessSize: %{public}" PRIu64" streams: %{public}s", GetAnonyString(devId_).c_str(),
140         GetAnonyString(dhId_).c_str(), streamType_, processSize, strStreams.c_str());
141     return DCAMERA_OK;
142 }
143 
StartCapture(std::shared_ptr<DCCaptureInfo> & captureInfo)144 int32_t DCameraSourceDataProcess::StartCapture(std::shared_ptr<DCCaptureInfo>& captureInfo)
145 {
146     DHLOGI("DCameraSourceDataProcess StartCapture devId %{public}s dhId %{public}s width: %{public}d, height: "
147         "%{public}d, format: %{public}d, isCapture: %{public}d, dataspace: %{public}d, encodeType: %{public}d, "
148         "streamType: %{public}d", GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(),
149         captureInfo->width_, captureInfo->height_, captureInfo->format_, captureInfo->isCapture_,
150         captureInfo->dataspace_, captureInfo->encodeType_, captureInfo->type_);
151     if (streamType_ == CONTINUOUS_FRAME && captureInfo->isCapture_ == true) {
152         isFirstContStream_ = true;
153     }
154 
155     std::shared_ptr<DCameraStreamConfig> streamConfig =
156         std::make_shared<DCameraStreamConfig>(captureInfo->width_, captureInfo->height_, captureInfo->format_,
157         captureInfo->dataspace_, captureInfo->encodeType_, captureInfo->type_);
158     std::set<int32_t> streamIds(captureInfo->streamIds_.begin(), captureInfo->streamIds_.end());
159     for (auto iterSet = streamIds.begin(); iterSet != streamIds.end(); iterSet++) {
160         DHLOGI("DCameraSourceDataProcess StartCapture devId %{public}s dhId %{public}s StartCapture id: %{public}d",
161             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), *iterSet);
162     }
163     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
164         (*iter)->StartCapture(streamConfig, streamIds);
165     }
166     return DCAMERA_OK;
167 }
168 
StopCapture(std::vector<int32_t> & streamIds)169 int32_t DCameraSourceDataProcess::StopCapture(std::vector<int32_t>& streamIds)
170 {
171     DHLOGI("DCameraSourceDataProcess StopCapture devId %{public}s dhId %{public}s streamType: %{public}d",
172         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_);
173     std::set<int32_t> streamIdSet(streamIds.begin(), streamIds.end());
174     for (auto iterSet = streamIdSet.begin(); iterSet != streamIdSet.end(); iterSet++) {
175         DHLOGI("DCameraSourceDataProcess StopCapture devId %{public}s dhId %{public}s stream id: %{public}d",
176             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), *iterSet);
177     }
178     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
179         (*iter)->StopCapture(streamIdSet);
180     }
181     if ((streamType_ == CONTINUOUS_FRAME) && (GetProducerSize() == 0)) {
182         DestroyPipeline();
183     }
184     return DCAMERA_OK;
185 }
186 
DestroyPipeline()187 void DCameraSourceDataProcess::DestroyPipeline()
188 {
189     DHLOGI("DCameraSourceDataProcess DestroyPipeline devId %{public}s dhId %{public}s streamType: %{public}d",
190         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_);
191     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
192         (*iter)->DestroyPipeline();
193     }
194 }
195 
GetProducerSize()196 int32_t DCameraSourceDataProcess::GetProducerSize()
197 {
198     int32_t ret = 0;
199     for (auto iter = streamProcess_.begin(); iter != streamProcess_.end(); iter++) {
200         std::shared_ptr<DCameraStreamDataProcess> streamDataProcess = *iter;
201         int32_t size = streamDataProcess->GetProducerSize();
202         ret += size;
203     }
204     DHLOGI("DCameraSourceDataProcess GetProducerSize devId %{public}s dhId %{public}s size %{public}d",
205         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), ret);
206     return ret;
207 }
208 
GetAllStreamIds(std::vector<int32_t> & streamIds)209 void DCameraSourceDataProcess::GetAllStreamIds(std::vector<int32_t>& streamIds)
210 {
211     streamIds.assign(streamIds_.begin(), streamIds_.end());
212 }
213 } // namespace DistributedHardware
214 } // namespace OHOS
215