--- linuxsampler/trunk/src/network/lscpserver.h 2004/03/05 13:46:15 35 +++ linuxsampler/trunk/src/network/lscpserver.h 2005/09/23 06:58:26 778 @@ -2,20 +2,21 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2005 Christian Schoenebeck * * * - * This program is free software; you can redistribute it and/or modify * + * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * - * This program is distributed in the hope that it will be useful, * + * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software * + * along with this library; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * * MA 02111-1307 USA * ***************************************************************************/ @@ -26,93 +27,164 @@ #include #include #include +#include +#include #include #include #include #include +#include + +#include "lscp.h" #include "lscpparser.h" -#include "../thread.h" -#include "../audiothread.h" +#include "lscp.h" +#include "lscpevent.h" +#include "lscpinstrumentloader.h" +#include "../Sampler.h" +#include "../common/Thread.h" +#include "../common/Mutex.h" +#include "../common/Condition.h" /// TCP Port on which the server should listen for connection requests. +#define LSCP_ADDR INADDR_ANY #define LSCP_PORT 8888 -/// Handle for a client connection (FIXME: doesn't work for more than one network connections of course, thus has to be included to the yyparse() parameters instead). -extern int hSession; +/// try up to 3 minutes to bind server socket +#define LSCP_SERVER_BIND_TIMEOUT 180 + +using namespace LinuxSampler; // External references to the main scanner and parser functions extern int yyparse(void* YYPARSE_PARAM); -extern int yylex_init(yyscan_t* scanner); -extern int yylex_destroy(yyscan_t yyscanner); +extern void restart(yyparse_param_t* pparam, int& yychar); /** * Network server for the LinuxSampler Control Protocol (LSCP). */ class LSCPServer : public Thread { public: - LSCPServer(AudioThread* pEngine); + LSCPServer(Sampler* pSampler, long int addr, short int port); + virtual ~LSCPServer(); + int WaitUntilInitialized(long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L); // Methods called by the parser - String LoadInstrument(String Filename, uint Instrument, uint SamplerChannel); - String LoadEngine(String EngineName, uint SamplerChannel); + String DestroyAudioOutputDevice(uint DeviceIndex); + String DestroyMidiInputDevice(uint DeviceIndex); + String LoadInstrument(String Filename, uint uiInstrument, uint uiSamplerChannel, bool bBackground = false); + String SetEngineType(String EngineName, uint uiSamplerChannel); String GetChannels(); + String ListChannels(); String AddChannel(); - String RemoveChannel(uint SamplerChannel); + String RemoveChannel(uint uiSamplerChannel); String GetAvailableEngines(); + String ListAvailableEngines(); String GetEngineInfo(String EngineName); - String GetChannelInfo(uint SamplerChannel); - String GetVoiceCount(uint SamplerChannel); - String GetStreamCount(uint SamplerChannel); - String GetBufferFill(fill_response_t ResponseType, uint SamplerChannel); - String SetAudioOutputType(audio_output_type_t AudioOutputType, uint SamplerChannel); - String SetAudioOutputChannel(uint AudioOutputChannel, uint SamplerChannel); - String SetMIDIInputPort(String MIDIInputPort, uint Samplerchannel); - String SetMIDIInputChannel(uint MIDIChannel, uint SamplerChannel); - String SetVolume(double Volume, uint SamplerChannel); - String ResetChannel(uint SamplerChannel); - String SubscribeNotification(uint UDPPort); - String UnsubscribeNotification(String SessionID); + String GetChannelInfo(uint uiSamplerChannel); + String GetVoiceCount(uint uiSamplerChannel); + String GetStreamCount(uint uiSamplerChannel); + String GetBufferFill(fill_response_t ResponseType, uint uiSamplerChannel); + String GetAvailableAudioOutputDrivers(); + String ListAvailableAudioOutputDrivers(); + String GetAvailableMidiInputDrivers(); + String ListAvailableMidiInputDrivers(); + String GetAudioOutputDriverInfo(String Driver); + String GetMidiInputDriverInfo(String Driver); +#ifdef __GNUC__ + typedef std::map StringMap; // nasty workaround for a GCC bug (see GCC bug #15980, #57) + String GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map DependencyList = StringMap()); + String GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map DependencyList = StringMap()); + String CreateAudioOutputDevice(String Driver, std::map Parameters = StringMap()); + String CreateMidiInputDevice(String Driver, std::map Parameters = StringMap()); +#else + String GetAudioOutputDriverParameterInfo(String Driver, String Parameter, std::map DependencyList = std::map()); + String GetMidiInputDriverParameterInfo(String Driver, String Parameter, std::map DependencyList = std::map()); + String CreateAudioOutputDevice(String Driver, std::map Parameters = std::map()); + String CreateMidiInputDevice(String Driver, std::map Parameters = std::map()); +#endif // __GNUC__ + String GetAudioOutputDeviceCount(); + String GetMidiInputDeviceCount(); + String GetAudioOutputDevices(); + String GetMidiInputDevices(); + String GetAudioOutputDeviceInfo(uint DeviceIndex); + String GetMidiInputDeviceInfo(uint DeviceIndex); + String GetMidiInputPortInfo(uint DeviceIndex, uint PortIndex); + String GetMidiInputPortParameterInfo(uint DeviceId, uint PortId, String ParameterName); + String GetAudioOutputChannelInfo(uint DeviceId, uint ChannelId); + String GetAudioOutputChannelParameterInfo(uint DeviceId, uint ChannelId, String ParameterName); + String SetAudioOutputChannelParameter(uint DeviceId, uint ChannelId, String ParamKey, String ParamVal); + String SetAudioOutputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal); + String SetMidiInputDeviceParameter(uint DeviceIndex, String ParamKey, String ParamVal); + String SetMidiInputPortParameter(uint DeviceIndex, uint PortIndex, String ParamKey, String ParamVal); + String SetAudioOutputChannel(uint ChannelAudioOutputChannel, uint AudioOutputDeviceInputChannel, uint uiSamplerChannel); + String SetAudioOutputDevice(uint AudioDeviceId, uint SamplerChannel); + String SetAudioOutputType(String AudioOutputDriver, uint uiSamplerChannel); + String SetMIDIInputPort(uint MIDIPort, uint uiSamplerChannel); + String SetMIDIInputChannel(uint MIDIChannel, uint uiSamplerChannel); + String SetMIDIInputDevice(uint MIDIDeviceId, uint uiSamplerChannel); + String SetMIDIInputType(String MidiInputDriver, uint uiSamplerChannel); + String SetMIDIInput(uint MIDIDeviceId, uint MIDIPort, uint MIDIChannel, uint uiSamplerChannel); + String SetVolume(double dVolume, uint uiSamplerChannel); + String SetChannelMute(bool bMute, uint uiSamplerChannel); + String SetChannelSolo(bool bSolo, uint uiSamplerChannel); + String ResetChannel(uint uiSamplerChannel); + String ResetSampler(); + String GetServerInfo(); + String GetTotalVoiceCount(); + String GetTotalVoiceCountMax(); + String SubscribeNotification(LSCPEvent::event_t); + String UnsubscribeNotification(LSCPEvent::event_t); + String QueryDatabase(String query); + String SetEcho(yyparse_param_t* pSession, double boolean_value); void AnswerClient(String ReturnMessage); + + static int currentSocket; + static std::map bufferedCommands; + + static void SendLSCPNotify( LSCPEvent Event ); + static int EventSubscribers( std::list events ); + static void LockRTNotify( void ) { RTNotifyMutex.Lock(); } + static void UnlockRTNotify( void ) { RTNotifyMutex.Unlock(); } + protected: - int hSocket; - sockaddr_in SocketAddress; - AudioThread* pEngine; // FIXME: as long as we only have one engine... + int hSocket; + sockaddr_in SocketAddress; + Sampler* pSampler; + Condition Initialized; + LSCPInstrumentLoader InstrumentLoader; ///< thread responsible for loading instruments in the background int Main(); ///< Implementation of virtual method from class Thread + private: + /** - * Converts a result_t structure into a valid LSCP answer message. + * Find a created audio output device index. */ - inline String ConvertResult(result_t result) { - switch (result.type) { - case result_type_success: { - return "OK\r\n"; - } - case result_type_warning: { - std::stringstream ss; - ss << "WRN:" << result.code << ":" << result.message << "\r\n"; - return ss.str(); - } - case result_type_error: { - std::stringstream ss; - ss << "ERR:" << result.code << ":" << result.message << "\r\n"; - return ss.str(); - } - } - } - - inline String ToString(int i) { - std::stringstream ss; - ss << i; - return ss.str(); - } - - inline String ToString(double d) { - std::stringstream ss; - ss << d; - return ss.str(); - } + int GetAudioOutputDeviceIndex (AudioOutputDevice *pDevice); + + /** + * Find a created midi input device index. + */ + int GetMidiInputDeviceIndex (MidiInputDevice *pDevice); + + bool HasSoloChannel(); + void MuteNonSoloChannels(); + void UnmuteChannels(); + + static std::map bufferedNotifies; + static Mutex NotifyMutex; + static Mutex NotifyBufferMutex; + static bool GetLSCPCommand( std::vector::iterator iter ); + static void CloseConnection( std::vector::iterator iter ); + static std::vector Sessions; + static Mutex SubscriptionMutex; + static std::map< LSCPEvent::event_t, std::list > eventSubscriptions; + static fd_set fdSet; + + //Protect main thread that generates real time notify messages + //like voice count, stream count and buffer fill + //from LSCP server removing engines and channels from underneath + static Mutex RTNotifyMutex; }; #endif // __LSCPSERVER_H_