• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_pipeline_sink.h"
17 
18 #include "dcamera_hitrace_adapter.h"
19 #include "distributed_hardware_log.h"
20 
21 #include "encode_data_process.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
25 const std::string DCameraPipelineSink::PIPELINE_OWNER = "Sink";
26 
~DCameraPipelineSink()27 DCameraPipelineSink::~DCameraPipelineSink()
28 {
29     if (isProcess_.load() == true) {
30         DHLOGD("~DCameraPipelineSink : Destroy sink data process pipeline.");
31         DestroyDataProcessPipeline();
32     }
33 }
34 
CreateDataProcessPipeline(PipelineType piplineType,const VideoConfigParams & sourceConfig,const VideoConfigParams & targetConfig,const std::shared_ptr<DataProcessListener> & listener)35 int32_t DCameraPipelineSink::CreateDataProcessPipeline(PipelineType piplineType,
36     const VideoConfigParams& sourceConfig, const VideoConfigParams& targetConfig,
37     const std::shared_ptr<DataProcessListener>& listener)
38 {
39     DCAMERA_SYNC_TRACE(DCAMERA_SINK_CREATE_PIPELINE);
40     DHLOGD("Create sink data process pipeline.");
41     switch (piplineType) {
42         case PipelineType::VIDEO:
43             if (!(IsInRange(sourceConfig) && IsInRange(targetConfig))) {
44                 DHLOGE("Source config or target config of sink pipeline are invalid.");
45                 return DCAMERA_BAD_VALUE;
46             }
47             break;
48         default:
49             DHLOGE("JPEG or other pipeline type are not supported in sink pipeline.");
50             return DCAMERA_NOT_FOUND;
51     }
52     if (listener == nullptr) {
53         DHLOGE("The process listener of sink pipeline is empty.");
54         return DCAMERA_BAD_VALUE;
55     }
56     if (pipelineHead_ != nullptr) {
57         DHLOGD("The sink pipeline already exists.");
58         return DCAMERA_OK;
59     }
60 
61     int32_t err = InitDCameraPipNodes(sourceConfig, targetConfig);
62     if (err != DCAMERA_OK) {
63         DestroyDataProcessPipeline();
64         return err;
65     }
66     piplineType_ = piplineType;
67     processListener_ = listener;
68     isProcess_.store(true);
69     return DCAMERA_OK;
70 }
71 
IsInRange(const VideoConfigParams & curConfig)72 bool DCameraPipelineSink::IsInRange(const VideoConfigParams& curConfig)
73 {
74     bool isWidthValid = (curConfig.GetWidth() >= MIN_VIDEO_WIDTH && curConfig.GetWidth() <= MAX_VIDEO_WIDTH);
75     bool isHeightValid = (curConfig.GetHeight() >= MIN_VIDEO_HEIGHT && curConfig.GetHeight() <= MAX_VIDEO_HEIGHT);
76     bool isFrameRateValid = (curConfig.GetFrameRate() >= MIN_FRAME_RATE && curConfig.GetFrameRate() <= MAX_FRAME_RATE);
77     return isWidthValid && isHeightValid && isFrameRateValid;
78 }
79 
InitDCameraPipNodes(const VideoConfigParams & sourceConfig,const VideoConfigParams & targetConfig)80 int32_t DCameraPipelineSink::InitDCameraPipNodes(const VideoConfigParams& sourceConfig,
81     const VideoConfigParams& targetConfig)
82 {
83     DHLOGD("Init sink DCamera pipeline Nodes.");
84     if (piplineType_ == PipelineType::PHOTO_JPEG) {
85         DHLOGE("JPEG data process is not supported.");
86         return DCAMERA_NOT_FOUND;
87     }
88 
89     pipNodeRanks_.push_back(std::make_shared<EncodeDataProcess>(shared_from_this()));
90     if (pipNodeRanks_.size() == 0) {
91         DHLOGD("Creating an empty sink pipeline.");
92         pipelineHead_ = nullptr;
93         return DCAMERA_BAD_VALUE;
94     }
95 
96     VideoConfigParams curNodeSourceCfg = sourceConfig;
97     for (size_t i = 0; i < pipNodeRanks_.size(); i++) {
98         CHECK_AND_RETURN_RET_LOG((pipNodeRanks_[i] == nullptr), DCAMERA_BAD_VALUE, "Node is null.");
99         pipNodeRanks_[i]->SetNodeRank(i);
100 
101         VideoConfigParams curNodeProcessedCfg;
102         int32_t err = pipNodeRanks_[i]->InitNode(curNodeSourceCfg, targetConfig, curNodeProcessedCfg);
103         if (err != DCAMERA_OK) {
104             DHLOGE("Init sink DCamera pipeline Node [%{public}zu] failed.", i);
105             return DCAMERA_INIT_ERR;
106         }
107         curNodeSourceCfg = curNodeProcessedCfg;
108 
109         if (i == 0) {
110             continue;
111         }
112 
113         err = pipNodeRanks_[i - 1]->SetNextNode(pipNodeRanks_[i]);
114         if (err != DCAMERA_OK) {
115             DHLOGE("Set the next node of Node [%{public}zu] failed in sink pipeline.", i - 1);
116             return DCAMERA_INIT_ERR;
117         }
118     }
119     DHLOGD("All nodes have been linked in sink pipeline.");
120     pipelineHead_ = pipNodeRanks_[0];
121     return DCAMERA_OK;
122 }
123 
ProcessData(std::vector<std::shared_ptr<DataBuffer>> & dataBuffers)124 int32_t DCameraPipelineSink::ProcessData(std::vector<std::shared_ptr<DataBuffer>>& dataBuffers)
125 {
126     DHLOGD("Process data buffers in sink pipeline.");
127     if (piplineType_ == PipelineType::PHOTO_JPEG) {
128         DHLOGE("JPEG data process is not supported in sink pipeline.");
129         return DCAMERA_NOT_FOUND;
130     }
131     if (pipelineHead_ == nullptr) {
132         DHLOGE("The current sink pipeline node is empty. Processing failed.");
133         return DCAMERA_INIT_ERR;
134     }
135     if (dataBuffers.empty()) {
136         DHLOGE("Sink Pipeline Input Data buffers is null.");
137         return DCAMERA_BAD_VALUE;
138     }
139     if (isProcess_.load() == false) {
140         DHLOGE("Sink pipeline node occurred error or start destroy.");
141         return DCAMERA_DISABLE_PROCESS;
142     }
143 
144     int32_t err = pipelineHead_->ProcessData(dataBuffers);
145     if (err != DCAMERA_OK) {
146         DHLOGE("Sink plpeline process data buffers fail.");
147     }
148     return err;
149 }
150 
DestroyDataProcessPipeline()151 void DCameraPipelineSink::DestroyDataProcessPipeline()
152 {
153     DCAMERA_SYNC_TRACE(DCAMERA_SINK_DESTORY_PIPELINE);
154     DHLOGD("Destroy sink data process pipeline start.");
155     isProcess_.store(false);
156     if (pipelineHead_ != nullptr) {
157         pipelineHead_->ReleaseProcessNode();
158         pipelineHead_ = nullptr;
159     }
160 
161     pipNodeRanks_.clear();
162     piplineType_ = PipelineType::VIDEO;
163     processListener_ = nullptr;
164     DHLOGD("Destroy sink data process pipeline end.");
165 }
166 
OnError(DataProcessErrorType errorType)167 void DCameraPipelineSink::OnError(DataProcessErrorType errorType)
168 {
169     DHLOGE("A runtime error occurred in sink pipeline.");
170     isProcess_.store(false);
171     if (processListener_ == nullptr) {
172         DHLOGE("The process listener of sink pipeline is empty.");
173         return;
174     }
175     processListener_->OnError(errorType);
176 }
177 
OnProcessedVideoBuffer(const std::shared_ptr<DataBuffer> & videoResult)178 void DCameraPipelineSink::OnProcessedVideoBuffer(const std::shared_ptr<DataBuffer>& videoResult)
179 {
180     DHLOGD("Sink pipeline output the processed video buffer.");
181     if (processListener_ == nullptr) {
182         DHLOGE("The process listener of sink pipeline is empty.");
183         return;
184     }
185     processListener_->OnProcessedVideoBuffer(videoResult);
186 }
187 
GetProperty(const std::string & propertyName,PropertyCarrier & propertyCarrier)188 int32_t DCameraPipelineSink::GetProperty(const std::string& propertyName, PropertyCarrier& propertyCarrier)
189 {
190     if (pipelineHead_ == nullptr) {
191         DHLOGD("DCameraPipelineSink::GetProperty: pipelineHead is nullptr.");
192         return DCAMERA_BAD_VALUE;
193     }
194     std::shared_ptr<AbstractDataProcess> cur = pipelineHead_;
195     while (cur) {
196         int32_t ret = cur->GetProperty(propertyName, propertyCarrier);
197         if (ret != DCAMERA_OK) {
198             DHLOGD("DCameraPipelineSink::GetProperty: get dataProcess property fail.");
199             return DCAMERA_BAD_VALUE;
200         }
201         cur = cur->nextDataProcess_;
202     }
203     return DCAMERA_OK;
204 }
205 } // namespace DistributedHardware
206 } // namespace OHOS
207