NaiveFlowControl.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "NaiveFlowControl.hxx"
00024 #include "Processing.hxx"
00025 #include "OutPort.hxx"
00026 #include "InPort.hxx"
00027 #include "Network.hxx"
00028
00029 namespace CLAM
00030 {
00031
00032 NaiveFlowControl::NaiveFlowControl()
00033 {
00034 }
00035
00036 void NaiveFlowControl::ProcessingAddedToNetwork( Processing & added )
00037 {
00038 NetworkTopologyChanged();
00039 std::string processingType = added.GetClassName();
00040
00041 if ( processingType == "AudioSource" || processingType == "AudioBufferSource" )
00042 {
00043 mSources.push_back( &added );
00044 return;
00045 }
00046 if (added.GetNInPorts()==0 && added.GetNOutPorts()==0)
00047 {
00048 mSources.push_back( &added);
00049 return;
00050 }
00051 if (added.GetNInPorts()==0 && added.GetNOutPorts()!=0)
00052 {
00053 mGenerators.push_back( &added);
00054 return;
00055 }
00056 if ( processingType == "AudioSink" || processingType == "AudioBufferSink" )
00057 {
00058 mSinks.push_back( &added );
00059 return;
00060 }
00061 mNormalProcessings.push_back ( &added );
00062 }
00063
00064 void NaiveFlowControl::ProcessingRemovedFromNetwork( Processing & removed )
00065 {
00066 NetworkTopologyChanged();
00067 std::string processingType = removed.GetClassName();
00068 if ( processingType == "AudioSource" || processingType == "AudioBufferSource" )
00069 {
00070 mSources.remove( &removed );
00071 return;
00072 }
00073 if (removed.GetNInPorts()==0 && removed.GetNOutPorts()==0)
00074 {
00075 mSources.remove( &removed );
00076 return;
00077 }
00078 if (removed.GetNInPorts()==0 && removed.GetNOutPorts()!=0)
00079 {
00080 mGenerators.remove( &removed);
00081 return;
00082 }
00083 if ( processingType == "AudioSink" || processingType == "AudioBufferSink" )
00084 {
00085 mSinks.remove( &removed );
00086 return;
00087 }
00088 mNormalProcessings.remove ( &removed );
00089 }
00090
00091 void NaiveFlowControl::Do()
00092 {
00093
00094
00095 ProcessingList pendingSinks(mSinks);
00096 for (ProcessingList::iterator it=mSources.begin(); it!=mSources.end(); it++ )
00097 {
00098 Processing* proc = *it;
00099 if (proc->CanConsumeAndProduce())
00100 {
00101
00102 proc->Do();
00103 }
00104 else
00105 {
00106 std::cerr << "Warning: some AudioSource was not able to consume incoming audio from the call-back.";
00107 }
00108 }
00109 while (true)
00110 {
00111 bool noProcessingRun = true;
00112 for (ProcessingList::iterator it=mNormalProcessings.begin(); it!=mNormalProcessings.end(); it++)
00113 {
00114 Processing* proc = *it;
00115 if (!proc->CanConsumeAndProduce() )
00116 {
00117
00118 continue;
00119 }
00120
00121 noProcessingRun = false;
00122 proc->Do();
00123 }
00124 for (ProcessingList::iterator it=pendingSinks.begin(); it!=pendingSinks.end(); )
00125 {
00126 Processing* proc = *it;
00127 if (!proc->CanConsumeAndProduce())
00128 {
00129 it++;
00130 continue;
00131 }
00132
00133 proc->Do();
00134 it = pendingSinks.erase(it);
00135 noProcessingRun = false;
00136 }
00137 if (noProcessingRun && !pendingSinks.empty())
00138 {
00139 for (ProcessingList::iterator it=mGenerators.begin(); it!=mGenerators.end(); it++)
00140 {
00141 Processing* proc = *it;
00142 if (!proc->CanConsumeAndProduce() )
00143 {
00144
00145 continue;
00146 }
00147
00148 noProcessingRun = false;
00149 proc->Do();
00150 }
00151 }
00152 if (noProcessingRun) break;
00153 }
00154 if (!pendingSinks.empty())
00155 std::cerr << "Warning: " << pendingSinks.size() << " sinks were not fed, so could not send audio to the callback." << std::endl;
00156
00157
00158
00159 }
00160
00161
00162 }
00163
00164