Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Android Open Source Project |
| 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 | |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 17 | #include "stdio.h" |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | #include <sys/types.h> |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 20 | #include "FlowGraphNode.h" |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 21 | |
Phil Burk | 9425ffd | 2020-07-09 13:21:09 -0700 | [diff] [blame] | 22 | using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 23 | |
| 24 | /***************************************************************************/ |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 25 | int32_t FlowGraphNode::pullData(int32_t numFrames, int64_t callCount) { |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 26 | int32_t frameCount = numFrames; |
Phil Burk | 7e601f3 | 2019-03-08 16:04:43 -0800 | [diff] [blame] | 27 | // Prevent recursion and multiple execution of nodes. |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 28 | if (callCount > mLastCallCount) { |
| 29 | mLastCallCount = callCount; |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 30 | if (mDataPulledAutomatically) { |
| 31 | // Pull from all the upstream nodes. |
| 32 | for (auto &port : mInputPorts) { |
Phil Burk | e4a2e9c | 2019-05-31 11:42:10 -0700 | [diff] [blame] | 33 | // TODO fix bug of leaving unused data in some ports if using multiple AudioSource |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 34 | frameCount = port.get().pullData(callCount, frameCount); |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 35 | } |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 36 | } |
| 37 | if (frameCount > 0) { |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 38 | frameCount = onProcess(frameCount); |
Phil Burk | 22f2113 | 2019-03-08 12:43:19 -0800 | [diff] [blame] | 39 | } |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 40 | mLastFrameCount = frameCount; |
| 41 | } else { |
| 42 | frameCount = mLastFrameCount; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 43 | } |
Phil Burk | dd3971d | 2019-05-17 18:42:58 -0700 | [diff] [blame] | 44 | return frameCount; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 47 | void FlowGraphNode::pullReset() { |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 48 | if (!mBlockRecursion) { |
| 49 | mBlockRecursion = true; // for cyclic graphs |
| 50 | // Pull reset from all the upstream nodes. |
| 51 | for (auto &port : mInputPorts) { |
| 52 | port.get().pullReset(); |
| 53 | } |
| 54 | mBlockRecursion = false; |
| 55 | reset(); |
| 56 | } |
| 57 | } |
| 58 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 59 | void FlowGraphNode::reset() { |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 60 | mLastFrameCount = 0; |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 61 | mLastCallCount = kInitialCallCount; |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 64 | /***************************************************************************/ |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 65 | FlowGraphPortFloat::FlowGraphPortFloat(FlowGraphNode &parent, |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 66 | int32_t samplesPerFrame, |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 67 | int32_t framesPerBuffer) |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 68 | : FlowGraphPort(parent, samplesPerFrame) |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 69 | , mFramesPerBuffer(framesPerBuffer) |
Don Turner | 9414702 | 2019-08-19 16:13:15 +0100 | [diff] [blame] | 70 | , mBuffer(nullptr) { |
Don Turner | 085ab35 | 2019-08-19 16:14:01 +0100 | [diff] [blame] | 71 | size_t numFloats = static_cast<size_t>(framesPerBuffer * getSamplesPerFrame()); |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 72 | mBuffer = std::make_unique<float[]>(numFloats); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /***************************************************************************/ |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 76 | int32_t FlowGraphPortFloatOutput::pullData(int64_t callCount, int32_t numFrames) { |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 77 | numFrames = std::min(getFramesPerBuffer(), numFrames); |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 78 | return mContainingNode.pullData(numFrames, callCount); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 81 | void FlowGraphPortFloatOutput::pullReset() { |
Phil Burk | 2da4a84 | 2019-08-16 12:15:23 -0700 | [diff] [blame] | 82 | mContainingNode.pullReset(); |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 85 | // These need to be in the .cpp file because of forward cross references. |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 86 | void FlowGraphPortFloatOutput::connect(FlowGraphPortFloatInput *port) { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 87 | port->connect(this); |
| 88 | } |
| 89 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 90 | void FlowGraphPortFloatOutput::disconnect(FlowGraphPortFloatInput *port) { |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 91 | port->disconnect(this); |
| 92 | } |
| 93 | |
| 94 | /***************************************************************************/ |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 95 | int32_t FlowGraphPortFloatInput::pullData(int64_t callCount, int32_t numFrames) { |
Don Turner | 9414702 | 2019-08-19 16:13:15 +0100 | [diff] [blame] | 96 | return (mConnected == nullptr) |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 97 | ? std::min(getFramesPerBuffer(), numFrames) |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 98 | : mConnected->pullData(callCount, numFrames); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 99 | } |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 100 | void FlowGraphPortFloatInput::pullReset() { |
Don Turner | 9414702 | 2019-08-19 16:13:15 +0100 | [diff] [blame] | 101 | if (mConnected != nullptr) mConnected->pullReset(); |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 102 | } |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 103 | |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 104 | float *FlowGraphPortFloatInput::getBuffer() { |
Don Turner | 9414702 | 2019-08-19 16:13:15 +0100 | [diff] [blame] | 105 | if (mConnected == nullptr) { |
Phil Burk | 328860f | 2019-11-04 18:43:40 -0800 | [diff] [blame] | 106 | return FlowGraphPortFloat::getBuffer(); // loaded using setValue() |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 107 | } else { |
Phil Burk | 9136201 | 2019-03-04 12:00:30 -0800 | [diff] [blame] | 108 | return mConnected->getBuffer(); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
Phil Burk | 226f4d4 | 2020-07-01 11:35:32 -0700 | [diff] [blame] | 111 | |
| 112 | int32_t FlowGraphSink::pullData(int32_t numFrames) { |
| 113 | return FlowGraphNode::pullData(numFrames, getLastCallCount() + 1); |
Phil Burk | 7c3bff2 | 2020-08-24 17:29:21 -0700 | [diff] [blame] | 114 | } |