Jack2 1.9.8

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 (bool capturing,
00056                          bool playing,
00057                          int inchannels,
00058                          int outchannels,
00059                          bool monitor,
00060                          const char* capture_driver_name,
00061                          const char* playback_driver_name,
00062                          jack_nframes_t capture_latency,
00063                          jack_nframes_t playback_latency) = 0;
00064 
00065         virtual int Open(jack_nframes_t buffer_size,
00066                          jack_nframes_t samplerate,
00067                          bool capturing,
00068                          bool playing,
00069                          int inchannels,
00070                          int outchannels,
00071                          bool monitor,
00072                          const char* capture_driver_name,
00073                          const char* playback_driver_name,
00074                          jack_nframes_t capture_latency,
00075                          jack_nframes_t playback_latency) = 0;
00076 
00077         virtual int Attach() = 0;
00078         virtual int Detach() = 0;
00079 
00080         virtual int Read() = 0;
00081         virtual int Write() = 0;
00082 
00083         virtual int Start() = 0;
00084         virtual int Stop() = 0;
00085 
00086         virtual bool IsFixedBufferSize() = 0;
00087         virtual int SetBufferSize(jack_nframes_t buffer_size) = 0;
00088         virtual int SetSampleRate(jack_nframes_t sample_rate) = 0;
00089 
00090         virtual int Process() = 0;
00091 
00092         virtual void SetMaster(bool onoff) = 0;
00093         virtual bool GetMaster() = 0;
00094 
00095         virtual void AddSlave(JackDriverInterface* slave) = 0;
00096         virtual void RemoveSlave(JackDriverInterface* slave) = 0;
00097 
00098         virtual std::list<JackDriverInterface*> GetSlaves() = 0;
00099 
00100         // For "master" driver
00101         virtual int ProcessReadSlaves() = 0;
00102         virtual int ProcessWriteSlaves() = 0;
00103 
00104         // For "slave" driver
00105         virtual int ProcessRead() = 0;
00106         virtual int ProcessWrite() = 0;
00107 
00108         virtual int ProcessReadSync() = 0;
00109         virtual int ProcessWriteSync() = 0;
00110 
00111         virtual int ProcessReadAsync() = 0;
00112         virtual int ProcessWriteAsync() = 0;
00113 
00114         virtual bool IsRealTime() const = 0;
00115         virtual bool IsRunning() const = 0;
00116 };
00117 
00122 class SERVER_EXPORT JackDriverClientInterface : public JackDriverInterface, public JackClientInterface
00123 {};
00124 
00129 #define CaptureDriverFlags  static_cast<JackPortFlags>(JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal)
00130 #define PlaybackDriverFlags static_cast<JackPortFlags>(JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal)
00131 #define MonitorDriverFlags static_cast<JackPortFlags>(JackPortIsOutput)
00132 
00133 class SERVER_EXPORT JackDriver : public JackDriverClientInterface
00134 {
00135 
00136     protected:
00137 
00138         char fCaptureDriverName[JACK_CLIENT_NAME_SIZE + 1];
00139         char fPlaybackDriverName[JACK_CLIENT_NAME_SIZE + 1];
00140 
00141         char fAliasName[JACK_CLIENT_NAME_SIZE + 1];
00142 
00143         jack_nframes_t fCaptureLatency;
00144         jack_nframes_t fPlaybackLatency;
00145 
00146         int fCaptureChannels;
00147         int fPlaybackChannels;
00148 
00149         jack_time_t fBeginDateUst;
00150         jack_time_t fEndDateUst;
00151         float fDelayedUsecs;
00152 
00153         // Pointers to engine state
00154         JackLockedEngine* fEngine;
00155         JackGraphManager* fGraphManager;
00156         JackSynchro* fSynchroTable;
00157         JackEngineControl* fEngineControl;
00158         JackClientControl fClientControl;
00159 
00160         std::list<JackDriverInterface*> fSlaveList;
00161 
00162         bool fIsMaster;
00163         bool fIsRunning;
00164         bool fWithMonitorPorts;
00165 
00166         // Static tables since the actual number of ports may be changed by the real driver
00167         // thus dynamic allocation is more difficult to handle
00168         jack_port_id_t fCapturePortList[DRIVER_PORT_NUM];
00169         jack_port_id_t fPlaybackPortList[DRIVER_PORT_NUM];
00170         jack_port_id_t fMonitorPortList[DRIVER_PORT_NUM];
00171 
00172         std::list<std::pair<std::string, std::string> > fConnections;           // Connections list
00173 
00174         void CycleIncTime();
00175         void CycleTakeBeginTime();
00176         void CycleTakeEndTime();
00177 
00178         void SetupDriverSync(int ref, bool freewheel);
00179 
00180         void NotifyXRun(jack_time_t callback_usecs, float delayed_usecs);   // XRun notification sent by the driver
00181         void NotifyBufferSize(jack_nframes_t buffer_size);                  // BufferSize notification sent by the driver
00182         void NotifySampleRate(jack_nframes_t sample_rate);                  // SampleRate notification sent by the driver
00183         void NotifyFailure(int code, const char* reason);                   // Failure notification sent by the driver
00184 
00185         virtual void SaveConnections();
00186         virtual void RestoreConnections();
00187 
00188         virtual int StartSlaves();
00189         virtual int StopSlaves();
00190 
00191         virtual int ResumeRefNum();
00192         virtual int SuspendRefNum();
00193 
00194     public:
00195 
00196         JackDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
00197         JackDriver();
00198         virtual ~JackDriver();
00199 
00200         void SetMaster(bool onoff);
00201         bool GetMaster();
00202 
00203         void AddSlave(JackDriverInterface* slave);
00204         void RemoveSlave(JackDriverInterface* slave);
00205 
00206         std::list<JackDriverInterface*> GetSlaves()
00207         {
00208             return fSlaveList;
00209         }
00210 
00211         virtual int Open();
00212 
00213         virtual int Open(bool capturing,
00214                          bool playing,
00215                          int inchannels,
00216                          int outchannels,
00217                          bool monitor,
00218                          const char* capture_driver_name,
00219                          const char* playback_driver_name,
00220                          jack_nframes_t capture_latency,
00221                          jack_nframes_t playback_latency);
00222 
00223         virtual int Open(jack_nframes_t buffer_size,
00224                          jack_nframes_t samplerate,
00225                          bool capturing,
00226                          bool playing,
00227                          int inchannels,
00228                          int outchannels,
00229                          bool monitor,
00230                          const char* capture_driver_name,
00231                          const char* playback_driver_name,
00232                          jack_nframes_t capture_latency,
00233                          jack_nframes_t playback_latency);
00234 
00235         virtual int Close();
00236 
00237         virtual int Process();
00238 
00239         virtual int Attach();
00240         virtual int Detach();
00241 
00242         virtual int Read();
00243         virtual int Write();
00244 
00245         virtual int Start();
00246         virtual int Stop();
00247 
00248         // For "master" driver
00249         int ProcessReadSlaves();
00250         int ProcessWriteSlaves();
00251 
00252         // For "slave" driver
00253         int ProcessRead();
00254         int ProcessWrite();
00255 
00256         int ProcessReadSync();
00257         int ProcessWriteSync();
00258 
00259         int ProcessReadAsync();
00260         int ProcessWriteAsync();
00261 
00262         virtual bool IsFixedBufferSize();
00263         virtual int SetBufferSize(jack_nframes_t buffer_size);
00264         virtual int SetSampleRate(jack_nframes_t sample_rate);
00265 
00266         virtual int ClientNotify(int refnum, const char* name, int notify, int sync, const char* message, int value1, int value2);
00267         virtual JackClientControl* GetClientControl() const;
00268 
00269         virtual bool IsRealTime() const;
00270         virtual bool IsRunning() const { return fIsRunning; }
00271         virtual bool Initialize();  // To be called by the wrapping thread Init method when the driver is a "blocking" one
00272 
00273 };
00274 
00275 } // end of namespace
00276 
00277 #endif