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_) {
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_ = true;
69     return DCAMERA_OK;
70 }
71 
IsInRange(const VideoConfigParams & curConfig)72 bool DCameraPipelineSink::IsInRange(const VideoConfigParams& curConfig)
73 {
74     return (curConfig.GetFrameRate() >= MIN_FRAME_RATE || curConfig.GetFrameRate() <= MAX_FRAME_RATE ||
75         curConfig.GetWidth() >= MIN_VIDEO_WIDTH || curConfig.GetWidth() <= MAX_VIDEO_WIDTH ||
76         curConfig.GetHeight() >= MIN_VIDEO_HEIGHT || curConfig.GetHeight() <= MAX_VIDEO_HEIGHT);
77 }
78 
InitDCameraPipNodes(const VideoConfigParams & sourceConfig,const VideoConfigParams & targetConfig)79 int32_t DCameraPipelineSink::InitDCameraPipNodes(const VideoConfigParams& sourceConfig,
80     const VideoConfigParams& targetConfig)
81 {
82     DHLOGD("Init sink DCamera pipeline Nodes.");
83     if (piplineType_ == PipelineType::PHOTO_JPEG) {
84         DHLOGE("JPEG data process is not supported.");
85         return DCAMERA_NOT_FOUND;
86     }
87 
88     pipNodeRanks_.push_back(std::make_shared<EncodeDataProcess>(shared_from_this()));
89     if (pipNodeRanks_.size() == 0) {
90         DHLOGD("Creating an empty sink pipeline.");
91         pipelineHead_ = nullptr;
92         return DCAMERA_BAD_VALUE;
93     }
94 
95     VideoConfigParams curNodeSourceCfg = sourceConfig;
96     for (size_t i = 0; i < pipNodeRanks_.size(); i++) {
97         CHECK_AND_RETURN_RET_LOG((pipNodeRanks_[i] == nullptr), DCAMERA_BAD_VALUE, "Node is null.");
98         pipNodeRanks_[i]->SetNodeRank(i);
99 
100         VideoConfigParams curNodeProcessedCfg;
101         int32_t err = pipNodeRanks_[i]->InitNode(curNodeSourceCfg, targetConfig, curNodeProcessedCfg);
102         if (err != DCAMERA_OK) {
103             DHLOGE("Init sink DCamera pipeline Node [%{public}zu] failed.", i);
104             return DCAMERA_INIT_ERR;
105         }
106         curNodeSourceCfg = curNodeProcessedCfg;
107 
108         if (i == 0) {
109             continue;
110         }
111 
112         err = pipNodeRanks_[i - 1]->SetNextNode(pipNodeRanks_[i]);
113         if (err != DCAMERA_OK) {
114             DHLOGE("Set the next node of Node [%{public}zu] failed in sink pipeline.", i - 1);
115             return DCAMERA_INIT_ERR;
116         }
117     }
118     DHLOGD("All nodes have been linked in sink pipeline.");
119     pipelineHead_ = pipNodeRanks_[0];
120     return DCAMERA_OK;
121 }
122 
ProcessData(std::vector<std::shared_ptr<DataBuffer>> & dataBuffers)123 int32_t DCameraPipelineSink::ProcessData(std::vector<std::shared_ptr<DataBuffer>>& dataBuffers)
124 {
125     DHLOGD("Process data buffers in sink pipeline.");
126     if (piplineType_ == PipelineType::PHOTO_JPEG) {
127         DHLOGE("JPEG data process is not supported in sink pipeline.");
128         return DCAMERA_NOT_FOUND;
129     }
130     if (pipelineHead_ == nullptr) {
131         DHLOGE("The current sink pipeline node is empty. Processing failed.");
132         return DCAMERA_INIT_ERR;
133     }
134     if (dataBuffers.empty()) {
135         DHLOGE("Sink Pipeline Input Data buffers is null.");
136         return DCAMERA_BAD_VALUE;
137     }
138     if (!isProcess_) {
139         DHLOGE("Sink pipeline node occurred error or start destroy.");
140         return DCAMERA_DISABLE_PROCESS;
141     }
142 
143     int32_t err = pipelineHead_->ProcessData(dataBuffers);
144     if (err != DCAMERA_OK) {
145         DHLOGE("Sink plpeline process data buffers fail.");
146     }
147     return err;
148 }
149 
DestroyDataProcessPipeline()150 void DCameraPipelineSink::DestroyDataProcessPipeline()
151 {
152     DCAMERA_SYNC_TRACE(DCAMERA_SINK_DESTORY_PIPELINE);
153     DHLOGD("Destroy sink data process pipeline start.");
154     isProcess_ = false;
155     if (pipelineHead_ != nullptr) {
156         pipelineHead_->ReleaseProcessNode();
157         pipelineHead_ = nullptr;
158     }
159 
160     pipNodeRanks_.clear();
161     piplineType_ = PipelineType::VIDEO;
162     processListener_ = nullptr;
163     DHLOGD("Destroy sink data process pipeline end.");
164 }
165 
OnError(DataProcessErrorType errorType)166 void DCameraPipelineSink::OnError(DataProcessErrorType errorType)
167 {
168     DHLOGE("A runtime error occurred in sink pipeline.");
169     isProcess_ = false;
170     if (processListener_ == nullptr) {
171         DHLOGE("The process listener of sink pipeline is empty.");
172         return;
173     }
174     processListener_->OnError(errorType);
175 }
176 
OnProcessedVideoBuffer(const std::shared_ptr<DataBuffer> & videoResult)177 void DCameraPipelineSink::OnProcessedVideoBuffer(const std::shared_ptr<DataBuffer>& videoResult)
178 {
179     DHLOGD("Sink pipeline output the processed video buffer.");
180     if (processListener_ == nullptr) {
181         DHLOGE("The process listener of sink pipeline is empty.");
182         return;
183     }
184     processListener_->OnProcessedVideoBuffer(videoResult);
185 }
186 
GetProperty(const std::string & propertyName,PropertyCarrier & propertyCarrier)187 int32_t DCameraPipelineSink::GetProperty(const std::string& propertyName, PropertyCarrier& propertyCarrier)
188 {
189     if (pipelineHead_ == nullptr) {
190         DHLOGD("DCameraPipelineSink::GetProperty: pipelineHead is nullptr.");
191         return DCAMERA_BAD_VALUE;
192     }
193     std::shared_ptr<AbstractDataProcess> cur = pipelineHead_;
194     while (cur) {
195         int32_t ret = cur->GetProperty(propertyName, propertyCarrier);
196         if (ret != DCAMERA_OK) {
197             DHLOGD("DCameraPipelineSink::GetProperty: get dataProcess property fail.");
198             return DCAMERA_BAD_VALUE;
199         }
200         cur = cur->nextDataProcess_;
201     }
202     return DCAMERA_OK;
203 }
204 } // namespace DistributedHardware
205 } // namespace OHOS
206