blob: 9a62d7d8924401b6d145554238c464cf7428956f [file] [log] [blame]
Phil Burk0817a2b2019-01-03 14:50:18 -08001/*
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 Burkf67a97f2019-05-30 12:48:17 -070017#include "stdio.h"
Phil Burk0817a2b2019-01-03 14:50:18 -080018#include <algorithm>
19#include <sys/types.h>
Phil Burk328860f2019-11-04 18:43:40 -080020#include "FlowGraphNode.h"
Phil Burk0817a2b2019-01-03 14:50:18 -080021
Phil Burk9425ffd2020-07-09 13:21:09 -070022using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
Phil Burk0817a2b2019-01-03 14:50:18 -080023
24/***************************************************************************/
Phil Burk226f4d42020-07-01 11:35:32 -070025int32_t FlowGraphNode::pullData(int32_t numFrames, int64_t callCount) {
Phil Burk22f21132019-03-08 12:43:19 -080026 int32_t frameCount = numFrames;
Phil Burk7e601f32019-03-08 16:04:43 -080027 // Prevent recursion and multiple execution of nodes.
Phil Burk226f4d42020-07-01 11:35:32 -070028 if (callCount > mLastCallCount) {
29 mLastCallCount = callCount;
Phil Burkdd3971d2019-05-17 18:42:58 -070030 if (mDataPulledAutomatically) {
31 // Pull from all the upstream nodes.
32 for (auto &port : mInputPorts) {
Phil Burke4a2e9c2019-05-31 11:42:10 -070033 // TODO fix bug of leaving unused data in some ports if using multiple AudioSource
Phil Burk226f4d42020-07-01 11:35:32 -070034 frameCount = port.get().pullData(callCount, frameCount);
Phil Burkdd3971d2019-05-17 18:42:58 -070035 }
Phil Burk22f21132019-03-08 12:43:19 -080036 }
37 if (frameCount > 0) {
Phil Burkdd3971d2019-05-17 18:42:58 -070038 frameCount = onProcess(frameCount);
Phil Burk22f21132019-03-08 12:43:19 -080039 }
Phil Burkf67a97f2019-05-30 12:48:17 -070040 mLastFrameCount = frameCount;
41 } else {
42 frameCount = mLastFrameCount;
Phil Burk0817a2b2019-01-03 14:50:18 -080043 }
Phil Burkdd3971d2019-05-17 18:42:58 -070044 return frameCount;
Phil Burk0817a2b2019-01-03 14:50:18 -080045}
46
Phil Burk328860f2019-11-04 18:43:40 -080047void FlowGraphNode::pullReset() {
Phil Burkf67a97f2019-05-30 12:48:17 -070048 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 Burk328860f2019-11-04 18:43:40 -080059void FlowGraphNode::reset() {
Phil Burkf67a97f2019-05-30 12:48:17 -070060 mLastFrameCount = 0;
Phil Burk226f4d42020-07-01 11:35:32 -070061 mLastCallCount = kInitialCallCount;
Phil Burkf67a97f2019-05-30 12:48:17 -070062}
63
Phil Burk0817a2b2019-01-03 14:50:18 -080064/***************************************************************************/
Phil Burk328860f2019-11-04 18:43:40 -080065FlowGraphPortFloat::FlowGraphPortFloat(FlowGraphNode &parent,
Phil Burk0817a2b2019-01-03 14:50:18 -080066 int32_t samplesPerFrame,
Phil Burk91362012019-03-04 12:00:30 -080067 int32_t framesPerBuffer)
Phil Burk328860f2019-11-04 18:43:40 -080068 : FlowGraphPort(parent, samplesPerFrame)
Phil Burk91362012019-03-04 12:00:30 -080069 , mFramesPerBuffer(framesPerBuffer)
Don Turner94147022019-08-19 16:13:15 +010070 , mBuffer(nullptr) {
Don Turner085ab352019-08-19 16:14:01 +010071 size_t numFloats = static_cast<size_t>(framesPerBuffer * getSamplesPerFrame());
Phil Burk91362012019-03-04 12:00:30 -080072 mBuffer = std::make_unique<float[]>(numFloats);
Phil Burk0817a2b2019-01-03 14:50:18 -080073}
74
75/***************************************************************************/
Phil Burk226f4d42020-07-01 11:35:32 -070076int32_t FlowGraphPortFloatOutput::pullData(int64_t callCount, int32_t numFrames) {
Phil Burk91362012019-03-04 12:00:30 -080077 numFrames = std::min(getFramesPerBuffer(), numFrames);
Phil Burk226f4d42020-07-01 11:35:32 -070078 return mContainingNode.pullData(numFrames, callCount);
Phil Burk0817a2b2019-01-03 14:50:18 -080079}
80
Phil Burk328860f2019-11-04 18:43:40 -080081void FlowGraphPortFloatOutput::pullReset() {
Phil Burk2da4a842019-08-16 12:15:23 -070082 mContainingNode.pullReset();
Phil Burkf67a97f2019-05-30 12:48:17 -070083}
84
Phil Burk0817a2b2019-01-03 14:50:18 -080085// These need to be in the .cpp file because of forward cross references.
Phil Burk328860f2019-11-04 18:43:40 -080086void FlowGraphPortFloatOutput::connect(FlowGraphPortFloatInput *port) {
Phil Burk0817a2b2019-01-03 14:50:18 -080087 port->connect(this);
88}
89
Phil Burk328860f2019-11-04 18:43:40 -080090void FlowGraphPortFloatOutput::disconnect(FlowGraphPortFloatInput *port) {
Phil Burk0817a2b2019-01-03 14:50:18 -080091 port->disconnect(this);
92}
93
94/***************************************************************************/
Phil Burk226f4d42020-07-01 11:35:32 -070095int32_t FlowGraphPortFloatInput::pullData(int64_t callCount, int32_t numFrames) {
Don Turner94147022019-08-19 16:13:15 +010096 return (mConnected == nullptr)
Phil Burk91362012019-03-04 12:00:30 -080097 ? std::min(getFramesPerBuffer(), numFrames)
Phil Burk226f4d42020-07-01 11:35:32 -070098 : mConnected->pullData(callCount, numFrames);
Phil Burk0817a2b2019-01-03 14:50:18 -080099}
Phil Burk328860f2019-11-04 18:43:40 -0800100void FlowGraphPortFloatInput::pullReset() {
Don Turner94147022019-08-19 16:13:15 +0100101 if (mConnected != nullptr) mConnected->pullReset();
Phil Burkf67a97f2019-05-30 12:48:17 -0700102}
Phil Burk0817a2b2019-01-03 14:50:18 -0800103
Phil Burk328860f2019-11-04 18:43:40 -0800104float *FlowGraphPortFloatInput::getBuffer() {
Don Turner94147022019-08-19 16:13:15 +0100105 if (mConnected == nullptr) {
Phil Burk328860f2019-11-04 18:43:40 -0800106 return FlowGraphPortFloat::getBuffer(); // loaded using setValue()
Phil Burk0817a2b2019-01-03 14:50:18 -0800107 } else {
Phil Burk91362012019-03-04 12:00:30 -0800108 return mConnected->getBuffer();
Phil Burk0817a2b2019-01-03 14:50:18 -0800109 }
110}
Phil Burk226f4d42020-07-01 11:35:32 -0700111
112int32_t FlowGraphSink::pullData(int32_t numFrames) {
113 return FlowGraphNode::pullData(numFrames, getLastCallCount() + 1);
Phil Burk7c3bff22020-08-24 17:29:21 -0700114}