Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/realtime/runner.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2013 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef MARSYAS_THREAD_SYSTEM_INCLUDED
00020 #define MARSYAS_THREAD_SYSTEM_INCLUDED
00021 
00022 #include <marsyas/realtime/atomic_control.h>
00023 #include <marsyas/realtime/any.h>
00024 #include <marsyas/realtime/packet_queue.h>
00025 #include <marsyas/realtime/osc_receiver.h>
00026 #include <marsyas/realtime/osc_transmitter.h>
00027 #include <marsyas/system/MarSystem.h>
00028 
00029 #include <thread>
00030 #include <atomic>
00031 #include <mutex>
00032 #include <list>
00033 #include <map>
00034 
00035 namespace Marsyas {
00036 namespace RealTime {
00037 
00038 class Control;
00039 class RunnerThread;
00040 class OscTransmitter;
00041 class OscReceiver;
00042 
00048 class marsyas_EXPORT Runner
00049 {
00050 public:
00051 
00052   Runner( MarSystem * system );
00053   ~Runner();
00054 
00055   MarSystem * system() { return m_system; }
00056 
00057   void setRtPriorityEnabled( bool on ) { m_realtime_priority = on; }
00058 
00059   void start(unsigned int ticks = 0);
00060   void stop();
00061   void wait();
00062   bool isRunning() const
00063   {
00064     return m_thread != 0;
00065   }
00066 
00067   void addController( OscProvider * );
00068   void removeController( OscProvider * );
00069   bool subscribe( const std::string & path, OscSubscriber * );
00070   void unsubscribe( const std::string & path,  OscSubscriber * );
00071 
00072   Control * control( const std::string & path );
00073 
00074 private:
00075   friend class Control;
00076   friend class RunnerThread;
00077 
00078   Control * create_control( const std::string & path );
00079 
00080   void refit_realvec_controls();
00081 
00082 private:
00083   MarSystem * m_system;
00084 
00085   bool m_realtime_priority;
00086 
00087   OscReceiver m_osc_receiver;
00088   OscTransmitter m_osc_transmitter;
00089 
00090   RunnerThread *m_thread;
00091 
00092   struct Shared
00093   {
00094     Shared(OscReceiver *osc_rcv): controller(osc_rcv) {}
00095     OscReceiver * controller;
00096     std::map<std::string, Control*> controls;
00097   };
00098   Shared * m_shared;
00099 };
00100 
00101 class marsyas_EXPORT Control
00102 {
00103 public:
00104   std::string path() const { return m_path; }
00105 
00106   any value() const;
00107 
00108   template <typename T>
00109   bool isValueType()
00110   {
00111     return typeid(*m_atomic) == typeid(AtomicControlT<T>);
00112   }
00113 
00114   bool isValueType( const std::string & type )
00115   {
00116     return (m_atomic->systemControl()->getType() == type);
00117   }
00118 
00119 private:
00120   friend class Runner;
00121   friend class RunnerThread;
00122 
00123   Control( Runner * runner, const std::string & path, AtomicControl * atomic ):
00124     m_runner(runner),
00125     m_path(path),
00126     m_atomic(atomic)
00127   {}
00128 
00129   ~Control()
00130   {
00131     delete m_atomic;
00132   }
00133 
00134   void resizeToFit()
00135   {
00136     if (isValueType<mrs_realvec>())
00137     {
00138       static_cast<AtomicControlT<mrs_realvec>*>(m_atomic)->resizeToFit();
00139     }
00140   }
00141 
00142   void push() { m_atomic->push(); }
00143 
00144   Runner * m_runner;
00145   std::string m_path;
00146   AtomicControl * m_atomic;
00147 };
00148 
00149 } // namespace RealTime
00150 } // namespace Marsyas
00151 
00152 #endif // MARSYAS_THREAD_SYSTEM_INCLUDED