1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#include "node/processing_node.h"
18
19namespace vraudio {
20
21ProcessingNode::NodeInput::NodeInput(
22 const std::vector<const AudioBuffer*>& input_vector)
23 : input_vector_(input_vector) {}
24
25const AudioBuffer* ProcessingNode::NodeInput::GetSingleInput() const {
26 if (input_vector_.size() == 1) {
27 return input_vector_[0];
28 }
29 if (input_vector_.size() > 1) {
30 LOG(WARNING) << "GetSingleInput() called on multi buffer input";
31 }
32 return nullptr;
33}
34
35const std::vector<const AudioBuffer*>&
36ProcessingNode::NodeInput::GetInputBuffers() const {
37 return input_vector_;
38}
39
40ProcessingNode::ProcessingNode()
41 : Node(), output_stream_(this), process_on_no_input_(false) {}
42
43void ProcessingNode::Connect(
44 const std::shared_ptr<PublisherNodeType>& publisher_node) {
45 input_stream_.Connect(node: publisher_node->GetSharedNodePtr(),
46 output: publisher_node->GetOutput());
47}
48
49void ProcessingNode::Process() {
50 NodeInput input(input_stream_.Read());
51 const AudioBuffer* output = nullptr;
52 // Only call AudioProcess if input data is available.
53 if (process_on_no_input_ || !input.GetInputBuffers().empty()) {
54 output = AudioProcess(input);
55 }
56 output_stream_.Write(data: output);
57}
58
59bool ProcessingNode::CleanUp() {
60 CallCleanUpOnInputNodes();
61 return (input_stream_.GetNumConnections() == 0);
62}
63
64void ProcessingNode::EnableProcessOnEmptyInput(bool enable) {
65 process_on_no_input_ = enable;
66}
67
68void ProcessingNode::CallCleanUpOnInputNodes() {
69 // We need to make a copy of the OutputNodeMap map since it changes due to
70 // Disconnect() calls.
71 const auto connected_nodes = input_stream_.GetConnectedNodeOutputPairs();
72 for (const auto& input_node : connected_nodes) {
73 Output<const AudioBuffer*>* output = input_node.first;
74 std::shared_ptr<Node> node = input_node.second;
75 const bool is_ready_to_be_disconnected = node->CleanUp();
76 if (is_ready_to_be_disconnected) {
77 input_stream_.Disconnect(output);
78 }
79 }
80}
81
82std::shared_ptr<Node> ProcessingNode::GetSharedNodePtr() {
83 return shared_from_this();
84}
85Node::Output<const AudioBuffer*>* ProcessingNode::GetOutput() {
86 return &output_stream_;
87}
88
89} // namespace vraudio
90

source code of qtmultimedia/src/3rdparty/resonance-audio/resonance_audio/node/processing_node.cc