Jack2  1.9.10
JackDriver.h
00001 /*
00002  Copyright (C) 2001 Paul Davis
00003  Copyright (C) 2004-2008 Grame
00004 
00005  This program is free software; you can redistribute it and/or modify
00006  it under the terms of the GNU General Public License as published by
00007  the Free Software Foundation; either version 2 of the License, or
00008  (at your option) any later version.
00009 
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  GNU General Public License for more details.
00014 
00015  You should have received a copy of the GNU General Public License
00016  along with this program; if not, write to the Free Software
00017  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019  */
00020 
00021 #ifndef __JackDriver__
00022 #define __JackDriver__
00023 
00024 #include "types.h"
00025 #include "JackClientInterface.h"
00026 #include "JackConstants.h"
00027 #include "JackPlatformPlug.h"
00028 #include "JackClientControl.h"
00029 #include <list>
00030 
00031 namespace Jack
00032 {
00033 
00034 class JackLockedEngine;
00035 class JackGraphManager;
00036 struct JackEngineControl;
00037 class JackSlaveDriverInterface;
00038 
00043 class SERVER_EXPORT JackDriverInterface
00044 {
00045 
00046     public:
00047 
00048         JackDriverInterface()
00049         {}
00050         virtual ~JackDriverInterface()
00051         {}
00052 
00053         virtual int Open() = 0;
00054 
00055         virtual int Open(jack_nframes_t buffer_size,
00056                          jack_nframes_t samplerate,
00057                          bool capturing,
00058                          bool playing,
00059                          int inchannels,
00060                          int outchannels,
00061                          bool monitor,
00062                          const char* capture_driver_name,
00063                          const char* playback_driver_name,
00064                          jack_nframes_t capture_latency,
00065                          jack_nframes_t playback_latency) = 0;
00066 
00067         virtual int Attach() = 0;
00068         virtual int Detach() = 0;
00069 
00070         virtual int Read() = 0;
00071         virtual int Write() = 0;
00072 
00073         virtual int Start() = 0;
00074         virtual int Stop() = 0;
00075 
00076         virtual bool IsFixedBufferSize() = 0;
00077         virtual int SetBufferSize(jack_nframes_t buffer_size) = 0;
00078         virtual int SetSampleRate(jack_nframes_t sample_rate) = 0;
00079 
00080         virtual int Process() = 0;
00081 
00082         virtual void SetMaster(bool onoff) = 0;
00083         virtual bool GetMaster() = 0;
00084 
00085         virtual void AddSlave(JackDriverInterface* slave) = 0;
00086         virtual void RemoveSlave(JackDriverInterface* slave) = 0;
00087 
00088         virtual std::list<JackDriverInterface*> GetSlaves() = 0;
00089 
00090         // For "master" driver
00091         virtual int ProcessReadSlaves() = 0;
00092         virtual int ProcessWriteSlaves() = 0;
00093 
00094         // For "slave" driver
00095         virtual int ProcessRead() = 0;
00096         virtual int ProcessWrite() = 0;
00097 
00098         // For "slave" driver in "synchronous" mode
00099         virtual int ProcessReadSync() = 0;
00100         virtual int ProcessWriteSync() = 0;
00101 
00102         // For "slave" driver in "asynchronous" mode
00103         virtual int ProcessReadAsync() = 0;
00104         virtual int ProcessWriteAsync() = 0;
00105 
00106         virtual bool IsRealTime() const = 0;
00107         virtual bool IsRunning() const = 0;
00108 };
00109 
00114 class SERVER_EXPORT JackDriverClientInterface : public JackDriverInterface, public JackClientInterface
00115 {};
00116 
00121 #define CaptureDriverFlags  static_cast<JackPortFlags>(JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal)
00122 #define PlaybackDriverFlags static_cast<JackPortFlags>(JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal)
00123 #define MonitorDriverFlags static_cast<JackPortFlags>(JackPortIsOutput)
00124 
00125 typedef std::list<std::pair<std::string, std::pair<std::string, std::string> > > driver_connections_list_t; // [type : (src, dst)]
00126 
00127 class SERVER_EXPORT JackDriver : public JackDriverClientInterface
00128 {
00129 
00130     protected:
00131 
00132         char fCaptureDriverName[JACK_CLIENT_NAME_SIZE + 1];
00133         char fPlaybackDriverName[JACK_CLIENT_NAME_SIZE + 1];
00134 
00135         char fAliasName[JACK_CLIENT_NAME_SIZE + 1];
00136 
00137         jack_nframes_t fCaptureLatency;
00138         jack_nframes_t fPlaybackLatency;
00139 
00140         int fCaptureChannels;
00141         int fPlaybackChannels;
00142 
00143         jack_time_t fBeginDateUst;
00144         jack_time_t fEndDateUst;
00145         float fDelayedUsecs;
00146 
00147         // Pointers to engine state
00148         JackLockedEngine* fEngine;
00149         JackGraphManager* fGraphManager;
00150         JackSynchro* fSynchroTable;
00151         JackEngineControl* fEngineControl;
00152         JackClientControl fClientControl;
00153 
00154         std::list<JackDriverInterface*> fSlaveList;
00155 
00156         bool fIsMaster;
00157         bool fIsRunning;
00158         bool fWithMonitorPorts;
00159 
00160         // Static tables since the actual number of ports may be changed by the real driver
00161         // thus dynamic allocation is more difficult to handle
00162         jack_port_id_t fCapturePortList[DRIVER_PORT_NUM];
00163         jack_port_id_t fPlaybackPortList[DRIVER_PORT_NUM];
00164         jack_port_id_t fMonitorPortList[DRIVER_PORT_NUM];
00165 
00166         driver_connections_list_t fConnections;         // Connections list 
00167 
00168         void CycleIncTime();
00169         void CycleTakeBeginTime();
00170         void CycleTakeEndTime();
00171 
00172         void SetupDriverSync(int ref, bool freewheel);
00173      
00174         void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs);   // XRun notification sent by the driver
00175         void NotifyBufferSize(jack_nframes_t buffer_size);                  // BufferSize notification sent by the driver
00176         void NotifySampleRate(jack_nframes_t sample_rate);                  // SampleRate notification sent by the driver
00177         void NotifyFailure(int code, const char* reason);                   // Failure notification sent by the driver
00178 
00179         virtual void SaveConnections(int alias);
00180         virtual void LoadConnections(int alias, bool full_name = true);
00181         std::string MatchPortName(const char* name, const char** ports, int alias, const std::string& type);
00182 
00183         virtual int StartSlaves();
00184         virtual int StopSlaves();
00185 
00186         virtual int ResumeRefNum();
00187         virtual int SuspendRefNum();
00188 
00189     public:
00190 
00191         JackDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
00192         virtual ~JackDriver();
00193 
00194         void SetMaster(bool onoff);
00195         bool GetMaster();
00196 
00197         void AddSlave(JackDriverInterface* slave);
00198         void RemoveSlave(JackDriverInterface* slave);
00199 
00200         std::list<JackDriverInterface*> GetSlaves()
00201         {
00202             return fSlaveList;
00203         }
00204 
00205         virtual int Open();
00206 
00207         virtual int Open(jack_nframes_t buffer_size,
00208                          jack_nframes_t samplerate,
00209                          bool capturing,
00210                          bool playing,
00211                          int inchannels,
00212                          int outchannels,
00213                          bool monitor,
00214                          const char* capture_driver_name,
00215                          const char* playback_driver_name,
00216                          jack_nframes_t capture_latency,
00217                          jack_nframes_t playback_latency);
00218 
00219         virtual int Close();
00220 
00221         virtual int Process();
00222 
00223         virtual int Attach();
00224         virtual int Detach();
00225 
00226         virtual int Read();
00227         virtual int Write();
00228 
00229         virtual int Start();
00230         virtual int Stop();
00231 
00232         // For "master" driver
00233         int ProcessReadSlaves();
00234         int ProcessWriteSlaves();
00235 
00236         // For "slave" driver with typically decompose a given cycle in separated Read and Write parts.
00237         virtual int ProcessRead();
00238         virtual int ProcessWrite();
00239 
00240         // For "slave" driver in "synchronous" mode
00241         virtual int ProcessReadSync();
00242         virtual int ProcessWriteSync();
00243 
00244         // For "slave" driver in "asynchronous" mode
00245         virtual int ProcessReadAsync();
00246         virtual int ProcessWriteAsync();
00247 
00248         virtual bool IsFixedBufferSize();
00249         virtual int SetBufferSize(jack_nframes_t buffer_size);
00250         virtual int SetSampleRate(jack_nframes_t sample_rate);
00251 
00252         virtual int ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2);
00253         virtual JackClientControl* GetClientControl() const;
00254 
00255         virtual bool IsRealTime() const;
00256         virtual bool IsRunning() const { return fIsRunning; }
00257         virtual bool Initialize();  // To be called by the wrapping thread Init method when the driver is a "blocking" one
00258 
00259 };
00260 
00261 } // end of namespace
00262 
00263 #endif