| 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 | #ifndef RESONANCE_AUDIO_NODE_SOURCE_NODE_H_ |
| 18 | #define RESONANCE_AUDIO_NODE_SOURCE_NODE_H_ |
| 19 | |
| 20 | #include <atomic> |
| 21 | #include <memory> |
| 22 | |
| 23 | #include "base/audio_buffer.h" |
| 24 | #include "node/publisher_node.h" |
| 25 | |
| 26 | namespace vraudio { |
| 27 | |
| 28 | // Audio source node that outputs data via the AudioProcess() method. |
| 29 | class SourceNode : public Node, public PublisherNode<const AudioBuffer*> { |
| 30 | public: |
| 31 | typedef PublisherNode<const AudioBuffer*> PublisherNodeType; |
| 32 | |
| 33 | SourceNode(); |
| 34 | |
| 35 | // Node implementation. |
| 36 | void Process() final; |
| 37 | bool CleanUp() final; |
| 38 | |
| 39 | // PublisherNode<OutputType> implementation. |
| 40 | std::shared_ptr<Node> GetSharedNodePtr() final; |
| 41 | Node::Output<const AudioBuffer*>* GetOutput() final; |
| 42 | |
| 43 | // Marks this node as being out of data and to be removed during the next |
| 44 | // clean-up cycle. |
| 45 | void MarkEndOfStream(); |
| 46 | |
| 47 | // Disable copy constructor. |
| 48 | SourceNode(const SourceNode& that) = delete; |
| 49 | |
| 50 | protected: |
| 51 | // Pure virtual method to implement the audio processing method. This method |
| 52 | // requires to return a single output buffer that can be processed by the node |
| 53 | // subscribers. |
| 54 | // |
| 55 | // @return Returns output data. |
| 56 | virtual const AudioBuffer* AudioProcess() = 0; |
| 57 | |
| 58 | private: |
| 59 | // Output stream to write processed data to. |
| 60 | Node::Output<const AudioBuffer*> output_stream_; |
| 61 | |
| 62 | // Flag indicating if this source node can be removed. |
| 63 | std::atomic<bool> end_of_stream_; |
| 64 | }; |
| 65 | |
| 66 | } // namespace vraudio |
| 67 | |
| 68 | #endif // RESONANCE_AUDIO_NODE_SOURCE_NODE_H_ |
| 69 | |