• 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_stream_data_process.h"
17 
18 #include "anonymous_string.h"
19 #include "distributed_camera_constants.h"
20 #include "distributed_camera_errno.h"
21 #include "distributed_hardware_log.h"
22 
23 #include "dcamera_pipeline_source.h"
24 #include "dcamera_stream_data_process_pipeline_listener.h"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
DCameraStreamDataProcess(std::string devId,std::string dhId,DCStreamType streamType)28 DCameraStreamDataProcess::DCameraStreamDataProcess(std::string devId, std::string dhId, DCStreamType streamType)
29     : devId_(devId), dhId_(dhId), streamType_(streamType)
30 {
31     DHLOGI("DCameraStreamDataProcess Constructor devId %s dhId %s", GetAnonyString(devId_).c_str(),
32         GetAnonyString(dhId_).c_str());
33     pipeline_ = nullptr;
34     listener_ = nullptr;
35 }
36 
~DCameraStreamDataProcess()37 DCameraStreamDataProcess::~DCameraStreamDataProcess()
38 {
39     DHLOGI("DCameraStreamDataProcess Destructor devId %s dhId %s streamType: %d", GetAnonyString(devId_).c_str(),
40         GetAnonyString(dhId_).c_str(), streamType_);
41     streamIds_.clear();
42     producers_.clear();
43     if (pipeline_ != nullptr) {
44         pipeline_->DestroyDataProcessPipeline();
45     }
46 }
47 
FeedStream(std::shared_ptr<DataBuffer> & buffer)48 void DCameraStreamDataProcess::FeedStream(std::shared_ptr<DataBuffer>& buffer)
49 {
50     for (auto streamId : streamIds_) {
51         DHLOGD("DCameraStreamDataProcess FeedStream devId %s dhId %s streamId %d streamType %d streamSize: %d",
52             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamId, streamType_, buffer->Size());
53     }
54     switch (streamType_) {
55         case SNAPSHOT_FRAME: {
56             FeedStreamToSnapShot(buffer);
57             break;
58         }
59         case CONTINUOUS_FRAME: {
60             FeedStreamToContinue(buffer);
61             break;
62         }
63         default:
64             break;
65     }
66 }
67 
ConfigStreams(std::shared_ptr<DCameraStreamConfig> & dstConfig,std::set<int32_t> & streamIds)68 void DCameraStreamDataProcess::ConfigStreams(std::shared_ptr<DCameraStreamConfig>& dstConfig,
69     std::set<int32_t>& streamIds)
70 {
71     for (auto streamId : streamIds) {
72         DHLOGI("DCameraStreamDataProcess ConfigStreams devId %s dhId %s streamId %d, width: %d, height: %d, " +
73             "format: %d, dataspace: %d, encodeType: %d, streamType: %d", GetAnonyString(devId_).c_str(),
74             GetAnonyString(dhId_).c_str(), streamId, dstConfig->width_, dstConfig->height_, dstConfig->format_,
75             dstConfig->dataspace_, dstConfig->encodeType_, dstConfig->type_);
76     }
77     dstConfig_ = dstConfig;
78     streamIds_ = streamIds;
79 }
80 
ReleaseStreams(std::set<int32_t> & streamIds)81 void DCameraStreamDataProcess::ReleaseStreams(std::set<int32_t>& streamIds)
82 {
83     for (auto streamId : streamIds) {
84         DHLOGI("DCameraStreamDataProcess ReleaseStreams devId %s dhId %s streamId %d streamType %d",
85             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamId, streamType_);
86     }
87     std::lock_guard<std::mutex> autoLock(producerMutex_);
88     for (auto iter = streamIds.begin(); iter != streamIds.end(); iter++) {
89         int32_t streamId = *iter;
90         DHLOGI("DCameraStreamDataProcess ReleaseStreams devId %s dhId %s streamId: %d", GetAnonyString(devId_).c_str(),
91             GetAnonyString(dhId_).c_str(), streamId);
92         streamIds_.erase(streamId);
93         auto producerIter = producers_.find(streamId);
94         if (producerIter == producers_.end()) {
95             continue;
96         }
97         producerIter->second->Stop();
98         producers_.erase(streamId);
99     }
100 }
101 
StartCapture(std::shared_ptr<DCameraStreamConfig> & srcConfig,std::set<int32_t> & streamIds)102 void DCameraStreamDataProcess::StartCapture(std::shared_ptr<DCameraStreamConfig>& srcConfig,
103     std::set<int32_t>& streamIds)
104 {
105     for (auto iter = streamIds.begin(); iter != streamIds.end(); iter++) {
106         DHLOGI("DCameraStreamDataProcess StartCapture devId %s dhId %s streamType: %d streamId: %d, " +
107             "srcConfig: width: %d, height: %d, format: %d, dataspace: %d, streamType: %d, encodeType: %d",
108             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, *iter,
109             srcConfig->width_,  srcConfig->height_, srcConfig->format_, srcConfig->dataspace_,
110             srcConfig->type_, srcConfig->encodeType_);
111     }
112     srcConfig_ = srcConfig;
113     if (streamType_ == CONTINUOUS_FRAME) {
114         CreatePipeline();
115     }
116     {
117         std::lock_guard<std::mutex> autoLock(producerMutex_);
118         for (auto iter = streamIds_.begin(); iter != streamIds_.end(); iter++) {
119             uint32_t streamId = *iter;
120             DHLOGI("DCameraStreamDataProcess StartCapture streamId: %d", streamId);
121             if (streamIds.find(streamId) == streamIds.end()) {
122                 continue;
123             }
124 
125             DHLOGI("DCameraStreamDataProcess StartCapture findProducer devId %s dhId %s streamType: %d streamId: %d",
126                 GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, streamId);
127             auto producerIter = producers_.find(streamId);
128             if (producerIter != producers_.end()) {
129                 continue;
130             }
131             DHLOGI("DCameraStreamDataProcess StartCapture CreateProducer devId %s dhId %s streamType: %d streamId: %d",
132                 GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, streamId);
133             producers_[streamId] =
134                 std::make_shared<DCameraStreamDataProcessProducer>(devId_, dhId_, streamId, streamType_);
135             producers_[streamId]->Start();
136         }
137     }
138 }
139 
StopCapture(std::set<int32_t> & streamIds)140 void DCameraStreamDataProcess::StopCapture(std::set<int32_t>& streamIds)
141 {
142     for (auto iter = streamIds.begin(); iter != streamIds.end(); iter++) {
143         DHLOGI("DCameraStreamDataProcess StopCapture devId %s dhId %s streamType: %d streamId: %d",
144             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, *iter);
145     }
146     {
147         std::lock_guard<std::mutex> autoLock(producerMutex_);
148         for (auto iter = streamIds_.begin(); iter != streamIds_.end(); iter++) {
149             uint32_t streamId = *iter;
150             DHLOGI("DCameraStreamDataProcess StopCapture streamId: %d", streamId);
151             if (streamIds.find(streamId) == streamIds.end()) {
152                 continue;
153             }
154 
155             DHLOGI("DCameraStreamDataProcess StopCapture findProducer devId %s dhId %s streamType: %d streamId: %d",
156                 GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, streamId);
157             auto producerIter = producers_.find(streamId);
158             if (producerIter == producers_.end()) {
159                 DHLOGE("DCameraStreamDataProcess StopCapture no producer, devId %s dhId %s streamType: %d streamId: %d",
160                     GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, streamId);
161                 continue;
162             }
163             DHLOGI("DCameraStreamDataProcess StopCapture stop producer, devId %s dhId %s streamType: %d streamId: %d",
164                 GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, streamId);
165             producerIter->second->Stop();
166             producerIter = producers_.erase(producerIter);
167         }
168     }
169 }
170 
GetAllStreamIds(std::set<int32_t> & streamIds)171 void DCameraStreamDataProcess::GetAllStreamIds(std::set<int32_t>& streamIds)
172 {
173     streamIds = streamIds_;
174 }
175 
GetProducerSize()176 int32_t DCameraStreamDataProcess::GetProducerSize()
177 {
178     DHLOGI("DCameraStreamDataProcess GetProducerSize devId %s dhId %s",
179         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str());
180     std::lock_guard<std::mutex> autoLock(producerMutex_);
181     return producers_.size();
182 }
183 
FeedStreamToSnapShot(const std::shared_ptr<DataBuffer> & buffer)184 void DCameraStreamDataProcess::FeedStreamToSnapShot(const std::shared_ptr<DataBuffer>& buffer)
185 {
186     DHLOGD("DCameraStreamDataProcess FeedStreamToSnapShot devId %s dhId %s streamType %d streamSize: %d",
187         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, buffer->Size());
188     std::lock_guard<std::mutex> autoLock(producerMutex_);
189     for (auto iter = producers_.begin(); iter != producers_.end(); iter++) {
190         iter->second->FeedStream(buffer);
191     }
192 }
193 
FeedStreamToContinue(const std::shared_ptr<DataBuffer> & buffer)194 void DCameraStreamDataProcess::FeedStreamToContinue(const std::shared_ptr<DataBuffer>& buffer)
195 {
196     DHLOGD("DCameraStreamDataProcess FeedStreamToContinue devId %s dhId %s streamType %d streamSize: %d",
197         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, buffer->Size());
198     std::lock_guard<std::mutex> autoLock(pipelineMutex_);
199     std::vector<std::shared_ptr<DataBuffer>> buffers;
200     buffers.push_back(buffer);
201     if (pipeline_ == nullptr) {
202         DHLOGE("DCameraStreamDataProcess FeedStreamToContinue pipeline null devId %s dhId %s type: %d streamSize: %d",
203             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, buffer->Size());
204         return;
205     }
206     int32_t ret = pipeline_->ProcessData(buffers);
207     if (ret != DCAMERA_OK) {
208         DHLOGE("DCameraStreamDataProcess FeedStreamToContinue pipeline ProcessData failed, ret: %d", ret);
209     }
210 }
211 
OnProcessedVideoBuffer(const std::shared_ptr<DataBuffer> & videoResult)212 void DCameraStreamDataProcess::OnProcessedVideoBuffer(const std::shared_ptr<DataBuffer>& videoResult)
213 {
214     DHLOGI("DCameraStreamDataProcess OnProcessedVideoBuffer devId %s dhId %s streamType: %d streamSize: %d",
215         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str(), streamType_, videoResult->Size());
216     std::lock_guard<std::mutex> autoLock(producerMutex_);
217     for (auto iter = producers_.begin(); iter != producers_.end(); iter++) {
218         iter->second->FeedStream(videoResult);
219     }
220 }
221 
OnError(DataProcessErrorType errorType)222 void DCameraStreamDataProcess::OnError(DataProcessErrorType errorType)
223 {
224     DHLOGE("DCameraStreamDataProcess OnError pipeline errorType: %d", errorType);
225 }
226 
CreatePipeline()227 void DCameraStreamDataProcess::CreatePipeline()
228 {
229     DHLOGI("DCameraStreamDataProcess CreatePipeline devId %s dhId %s",
230         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str());
231     std::lock_guard<std::mutex> autoLock(pipelineMutex_);
232     if (pipeline_ != nullptr) {
233         DHLOGI("DCameraStreamDataProcess CreatePipeline already exist, devId %s dhId %s",
234             GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str());
235         return;
236     }
237     pipeline_ = std::make_shared<DCameraPipelineSource>();
238     auto process = std::shared_ptr<DCameraStreamDataProcess>(shared_from_this());
239     listener_ = std::make_shared<DCameraStreamDataProcessPipelineListener>(process);
240     VideoConfigParams srcParams(GetPipelineCodecType(srcConfig_->encodeType_), GetPipelineFormat(srcConfig_->format_),
241         DCAMERA_PRODUCER_FPS_DEFAULT, srcConfig_->width_, srcConfig_->height_);
242     VideoConfigParams dstParams(GetPipelineCodecType(dstConfig_->encodeType_), GetPipelineFormat(dstConfig_->format_),
243         DCAMERA_PRODUCER_FPS_DEFAULT, dstConfig_->width_, dstConfig_->height_);
244     int32_t ret = pipeline_->CreateDataProcessPipeline(PipelineType::VIDEO, srcParams, dstParams, listener_);
245     if (ret != DCAMERA_OK) {
246         DHLOGE("DCameraStreamDataProcess CreateDataProcessPipeline type: %d failed, ret: %d", PipelineType::VIDEO, ret);
247     }
248 }
249 
DestroyPipeline()250 void DCameraStreamDataProcess::DestroyPipeline()
251 {
252     DHLOGI("DCameraStreamDataProcess DestroyPipeline devId %s dhId %s",
253         GetAnonyString(devId_).c_str(), GetAnonyString(dhId_).c_str());
254     std::lock_guard<std::mutex> autoLock(pipelineMutex_);
255     if (pipeline_ == nullptr) {
256         return;
257     }
258     pipeline_->DestroyDataProcessPipeline();
259     pipeline_ = nullptr;
260 }
261 
GetPipelineCodecType(DCEncodeType encodeType)262 VideoCodecType DCameraStreamDataProcess::GetPipelineCodecType(DCEncodeType encodeType)
263 {
264     VideoCodecType codecType;
265     switch (encodeType) {
266         case ENCODE_TYPE_H264:
267             codecType = VideoCodecType::CODEC_H264;
268             break;
269         case ENCODE_TYPE_H265:
270             codecType = VideoCodecType::CODEC_H265;
271             break;
272         default:
273             codecType = VideoCodecType::NO_CODEC;
274             break;
275     }
276     return codecType;
277 }
278 
GetPipelineFormat(int32_t format)279 Videoformat DCameraStreamDataProcess::GetPipelineFormat(int32_t format)
280 {
281     return Videoformat::NV21;
282 }
283 } // namespace DistributedHardware
284 } // namespace OHOS
285