| 1 | /* |
|---|---|
| 2 | Copyright 2018 Google Inc. All Rights Reserved. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS-IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "node/processing_node.h" |
| 18 | |
| 19 | namespace vraudio { |
| 20 | |
| 21 | ProcessingNode::NodeInput::NodeInput( |
| 22 | const std::vector<const AudioBuffer*>& input_vector) |
| 23 | : input_vector_(input_vector) {} |
| 24 | |
| 25 | const 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 | |
| 35 | const std::vector<const AudioBuffer*>& |
| 36 | ProcessingNode::NodeInput::GetInputBuffers() const { |
| 37 | return input_vector_; |
| 38 | } |
| 39 | |
| 40 | ProcessingNode::ProcessingNode() |
| 41 | : Node(), output_stream_(this), process_on_no_input_(false) {} |
| 42 | |
| 43 | void 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 | |
| 49 | void 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 | |
| 59 | bool ProcessingNode::CleanUp() { |
| 60 | CallCleanUpOnInputNodes(); |
| 61 | return (input_stream_.GetNumConnections() == 0); |
| 62 | } |
| 63 | |
| 64 | void ProcessingNode::EnableProcessOnEmptyInput(bool enable) { |
| 65 | process_on_no_input_ = enable; |
| 66 | } |
| 67 | |
| 68 | void 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 | |
| 82 | std::shared_ptr<Node> ProcessingNode::GetSharedNodePtr() { |
| 83 | return shared_from_this(); |
| 84 | } |
| 85 | Node::Output<const AudioBuffer*>* ProcessingNode::GetOutput() { |
| 86 | return &output_stream_; |
| 87 | } |
| 88 | |
| 89 | } // namespace vraudio |
| 90 |
