NaiveFlowControl.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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) //isolated processings
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) //isolated processings
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         // by now it have nothing of pulling
00094         // a naive approach: do sources, do normal processings, do sinks
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                         //std::cerr << "Do: "<<proc->GetClassName() << std::endl;
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                                 //std::cerr << "could NOT Do: "<<proc->GetClassName() << std::endl;
00118                                 continue;
00119                         }
00120                         //std::cerr << "Do: "<<proc->GetClassName() << std::endl;
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                         //std::cerr << "Do: "<<proc->GetClassName() << std::endl;
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                                         // std::cerr << "could NOT Do: "<<proc->GetClassName() << std::endl;
00145                                         continue;
00146                                 }
00147                                 //std::cerr << "Do: "<<proc->GetClassName() << std::endl;
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         //std::cerr << "<<< Network.Do() is Done" << std::endl;
00158 
00159 }
00160 
00161 
00162 } // namespace CLAM
00163 
00164 
Generated by  doxygen 1.6.3