diff --git a/linux/Makefile.am b/linux/Makefile.am index 02aa2831..83f73170 100755 --- a/linux/Makefile.am +++ b/linux/Makefile.am @@ -38,7 +38,10 @@ llcon_SOURCES = ../src/buffer.cpp \ # these need to be generated before the rest can be compiled -BUILT_SOURCES=moc/moc_server.cpp \ +BUILT_SOURCES=moc/moc_server.cpp \ + moc/moc_client.cpp \ + moc/moc_protocol.cpp \ + moc/moc_channel.cpp \ moc/moc_socket.cpp \ moc/moc_multicolorled.cpp \ moc/moc_util.cpp \ @@ -55,8 +58,11 @@ dist-hook: mkdir $(distdir)/moc moc/moc_server.cpp: ../src/server.h - $(MOC) ../src/server.h -o moc/moc_server.cpp - + $(MOC) ../src/server.h -o moc/moc_server.cpp + +moc/moc_client.cpp: ../src/client.h + $(MOC) ../src/client.h -o moc/moc_client.cpp + moc/moc_socket.cpp: ../src/socket.h $(MOC) ../src/socket.h -o moc/moc_socket.cpp @@ -66,6 +72,11 @@ moc/moc_multicolorled.cpp: ../src/multicolorled.h moc/moc_util.cpp: ../src/util.h $(MOC) ../src/util.h -o moc/moc_util.cpp +moc/moc_protocol.cpp: ../src/protocol.h + $(MOC) ../src/protocol.h -o moc/moc_protocol.cpp + +moc/moc_channel.cpp: ../src/channel.h + $(MOC) ../src/channel.h -o moc/moc_channel.cpp moc/moc_aboutdlgbase.cpp: moc/aboutdlgbase.h diff --git a/src/channel.cpp b/src/channel.cpp index 66ab7ddb..fb780709 100755 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -198,7 +198,7 @@ for ( int i = 0; i < MAX_NUM_CHANNELS; i++ ) /******************************************************************************\ * CChannel * \******************************************************************************/ -CChannel::CChannel() +CChannel::CChannel () { /* init time stamp activation counter */ iTimeStampActCnt = NUM_BL_TIME_STAMPS; @@ -220,8 +220,14 @@ CChannel::CChannel() iConTimeOut = 0; /* init sample rate offset estimation object */ - SampleOffsetEst.Init(); -} + SampleOffsetEst.Init(); + + + /* connections ---------------------------------------------------------- */ + // just route message through this class + QObject::connect(&Protocol, SIGNAL(MessReadyForSending()), + SIGNAL(MessReadyForSending())); +} void CChannel::SetSockBufSize ( const int iNewBlockSize, const int iNumBlocks ) { @@ -230,7 +236,7 @@ void CChannel::SetSockBufSize ( const int iNewBlockSize, const int iNumBlocks ) SockBuf.Init ( iNewBlockSize, iNumBlocks ); - Mutex.unlock (); + Mutex.unlock (); } bool CChannel::GetAddress(CHostAddress& RetAddr) @@ -292,7 +298,7 @@ for (int i = 0; i < BLOCK_SIZE_SAMPLES; i++) // TODO add protocol parsing here -ClientProtocol.ParseMessage ( vecbyData, iNumBytes ); +Protocol.ParseMessage ( vecbyData, iNumBytes ); @@ -304,27 +310,29 @@ ClientProtocol.ParseMessage ( vecbyData, iNumBytes ); return bRet; } -bool CChannel::GetData(CVector& vecdData) +bool CChannel::GetData ( CVector& vecdData ) { - Mutex.lock(); /* get mutex lock */ + Mutex.lock (); /* get mutex lock */ - const bool bGetOK = SockBuf.Get(vecdData); + const bool bGetOK = SockBuf.Get ( vecdData ); - if (!bGetOK) + if ( !bGetOK ) { /* decrease time-out counter */ - if (iConTimeOut > 0) + if ( iConTimeOut > 0 ) { iConTimeOut--; /* if time out is reached, re-init resample offset estimation module */ - if (iConTimeOut == 0) - SampleOffsetEst.Init(); + if ( iConTimeOut == 0 ) + { + SampleOffsetEst.Init (); + } } } - Mutex.unlock(); /* get mutex unlock */ + Mutex.unlock (); /* get mutex unlock */ return bGetOK; } diff --git a/src/channel.h b/src/channel.h index 4d1afec1..29bb390b 100755 --- a/src/channel.h +++ b/src/channel.h @@ -74,28 +74,40 @@ protected: /* CChannel ----------------------------------------------------------------- */ -class CChannel -{ +class CChannel : public QObject +{ + Q_OBJECT + public: - CChannel(); - virtual ~CChannel() {} + CChannel (); + virtual ~CChannel () {} - bool PutData(const CVector& vecbyData, - int iNumBytes); - bool GetData(CVector& vecdData); + bool PutData ( const CVector& vecbyData, + int iNumBytes ); + bool GetData ( CVector& vecdData ); - CVector PrepSendPacket(const CVector& vecsNPacket); + CVector PrepSendPacket ( const CVector& vecsNPacket ); + CVector GetSendMessage () { return Protocol.GetSendMessage (); } + + bool IsConnected () const { return iConTimeOut > 0; } + + int GetTimeStampIdx (); + int GetComprAudSize () { return iAudComprSize; } + double GetResampleOffset () { return SampleOffsetEst.GetSamRate (); } + + void SetAddress ( const CHostAddress NAddr ) { InetAddr = NAddr; } + bool GetAddress ( CHostAddress& RetAddr ); + CHostAddress GetAddress () { return InetAddr; } - bool GetAddress(CHostAddress& RetAddr); - CHostAddress GetAddress() {return InetAddr;} - int GetTimeStampIdx(); - void SetAddress(const CHostAddress NAddr) {InetAddr = NAddr;} - bool IsConnected() const {return iConTimeOut > 0;} - int GetComprAudSize() {return iAudComprSize;} - double GetResampleOffset() {return SampleOffsetEst.GetSamRate();} void SetSockBufSize ( const int iNewBlockSize, const int iNumBlocks ); - int GetSockBufSize() {return SockBuf.GetSize();} - + int GetSockBufSize () { return SockBuf.GetSize(); } + + // network protocol interface + void CreateJitBufMes ( const int iJitBufSize ) + { + Protocol.CreateJitBufMes ( iJitBufSize ); + } + protected: /* audio compression */ CAudioCompression AudioCompression; @@ -118,10 +130,8 @@ protected: /* network output conversion buffer */ CConvBuf ConvBuf; - -// TEST TODO: better implementation, now this object is created in server AND client which is not good -CClientProtocol ClientProtocol; - + // network protocol + CProtocol Protocol; /* time stamp index counter */ Q_UINT8 byTimeStampIdxCnt; @@ -129,7 +139,10 @@ CClientProtocol ClientProtocol; int iConTimeOut; - QMutex Mutex; + QMutex Mutex; + +signals: + void MessReadyForSending (); }; diff --git a/src/client.cpp b/src/client.cpp index 5c0ec492..37ac08ff 100755 --- a/src/client.cpp +++ b/src/client.cpp @@ -25,7 +25,30 @@ #include "client.h" -/* Implementation *************************************************************/ +/* Implementation *************************************************************/ +CClient::CClient () : bRun ( false ), Socket ( &Channel ), + iAudioInFader ( AUD_FADER_IN_MAX / 2 ), + iReverbLevel ( AUD_REVERB_MAX / 6 ), + bReverbOnLeftChan ( false ) +{ + QObject::connect(&Channel, SIGNAL(MessReadyForSending()), + this, SLOT(OnSendProtMessage())); +} + +void CClient::OnSendProtMessage () +{ + + // the protocol queries me to call the function to send the message + // send it through the network + Socket.SendPacket ( Channel.GetSendMessage (), + Channel.GetAddress(), Channel.GetTimeStampIdx() ); + + + +} + + + bool CClient::SetServerAddr(QString strNAddr) { QHostAddress InetAddr; @@ -88,7 +111,7 @@ void CClient::Init() void CClient::run() { int i, iInCnt; - + /* Set thread priority (The working thread should have a higher priority than the GUI) */ #ifdef _WIN32 @@ -112,10 +135,14 @@ void CClient::run() while (bRun) { /* get audio from sound card (blocking function) */ - if (Sound.Read(vecsAudioSndCrd)) - PostWinMessage(MS_SOUND_IN, MUL_COL_LED_RED); - else - PostWinMessage(MS_SOUND_IN, MUL_COL_LED_GREEN); + if (Sound.Read(vecsAudioSndCrd)) + { + PostWinMessage(MS_SOUND_IN, MUL_COL_LED_RED); + } + else + { + PostWinMessage(MS_SOUND_IN, MUL_COL_LED_GREEN); + } /* copy data from one stereo buffer in two separate buffers */ iInCnt = 0; @@ -181,10 +208,14 @@ void CClient::run() Channel.GetAddress(), Channel.GetTimeStampIdx()); /* receive a new block */ - if (Channel.GetData(vecdNetwData)) - PostWinMessage(MS_JIT_BUF_GET, MUL_COL_LED_GREEN); - else - PostWinMessage(MS_JIT_BUF_GET, MUL_COL_LED_RED); + if (Channel.GetData(vecdNetwData)) + { + PostWinMessage(MS_JIT_BUF_GET, MUL_COL_LED_GREEN); + } + else + { + PostWinMessage(MS_JIT_BUF_GET, MUL_COL_LED_RED); + } #ifdef _DEBUG_ #if 0 @@ -252,14 +283,18 @@ fflush(pFileDelay); if (Channel.IsConnected()) { /* write mono input signal in both sound-card channels */ - for (i = 0; i < iBlockSizeSam; i++) - vecdAudioL[i] = vecdAudioR[i] = vecdNetwData[i]; + for (i = 0; i < iBlockSizeSam; i++) + { + vecdAudioL[i] = vecdAudioR[i] = vecdNetwData[i]; + } } else { /* if not connected, clear data */ - for (i = 0; i < iBlockSizeSam; i++) - vecdAudioL[i] = vecdAudioR[i] = 0.0; + for (i = 0; i < iBlockSizeSam; i++) + { + vecdAudioL[i] = vecdAudioR[i] = 0.0; + } } /* resample data for each channel separately */ @@ -275,10 +310,14 @@ fflush(pFileDelay); } /* play the new block */ - if (Sound.Write(vecsAudioSndCrd)) - PostWinMessage(MS_SOUND_OUT, MUL_COL_LED_RED); - else + if (Sound.Write(vecsAudioSndCrd)) + { + PostWinMessage(MS_SOUND_OUT, MUL_COL_LED_RED); + } + else + { PostWinMessage(MS_SOUND_OUT, MUL_COL_LED_GREEN); + } /* update response time measurement --------------------------------- */ @@ -316,4 +355,4 @@ bool CClient::Stop() /* give thread some time to terminate, return status */ return wait(5000); -} +} diff --git a/src/client.h b/src/client.h index 9ea73a88..bd2704cc 100755 --- a/src/client.h +++ b/src/client.h @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include "global.h" #include "socket.h" #include "resample.h" @@ -51,46 +51,60 @@ /* Classes ********************************************************************/ -class CClient : public QThread -{ +class CClient : public QObject, public QThread +{ + Q_OBJECT + public: - CClient() : bRun ( false ), Socket ( &Channel ), - iAudioInFader ( AUD_FADER_IN_MAX / 2 ), - iReverbLevel ( AUD_REVERB_MAX / 6 ), - bReverbOnLeftChan ( false ) {} + CClient(); virtual ~CClient () {} - void Init(); - bool Stop(); - bool IsRunning() {return bRun;} - bool SetServerAddr(QString strNAddr); - double MicLevelL() {return SignalLevelMeterL.MicLevel();} - double MicLevelR() {return SignalLevelMeterR.MicLevel();} - bool IsConnected() {return Channel.IsConnected();} + void Init (); + bool Stop (); + bool IsRunning () { return bRun; } + bool SetServerAddr ( QString strNAddr ); + double MicLevelL () { return SignalLevelMeterL.MicLevel (); } + double MicLevelR () { return SignalLevelMeterR.MicLevel (); } + bool IsConnected () { return Channel.IsConnected (); } /* we want to return the standard deviation. For that we need to calculate the sqaure root */ - double GetTimingStdDev() {return sqrt(RespTimeMoAvBuf.GetAverage());} + double GetTimingStdDev () { return sqrt ( RespTimeMoAvBuf.GetAverage () ); } - int GetAudioInFader() {return iAudioInFader;} - void SetAudioInFader(const int iNV) {iAudioInFader = iNV;} + int GetAudioInFader () { return iAudioInFader; } + void SetAudioInFader ( const int iNV ) { iAudioInFader = iNV; } - int GetReverbLevel() {return iReverbLevel;} - void SetReverbLevel(const int iNL) {iReverbLevel = iNL;} + int GetReverbLevel () { return iReverbLevel; } + void SetReverbLevel ( const int iNL ) { iReverbLevel = iNL; } + + bool IsReverbOnLeftChan () { return bReverbOnLeftChan; } + void SetReverbOnLeftChan ( const bool bIL ) + { + bReverbOnLeftChan = bIL; + AudioReverb.Clear (); + } + + + void SetSockBufSize ( const int iNewBlockSize, const int iNumBlocks ) + { + // set the new socket size + Channel.SetSockBufSize ( iNewBlockSize, iNumBlocks ); + + // tell the server that size has changed + Channel.CreateJitBufMes ( iNumBlocks ); + } + int GetSockBufSize () { return Channel.GetSockBufSize (); } - bool IsReverbOnLeftChan() {return bReverbOnLeftChan;} - void SetReverbOnLeftChan(const bool bIL) - {bReverbOnLeftChan = bIL; AudioReverb.Clear();} - CSound* GetSndInterface() {return &Sound;} - CChannel* GetChannel() {return &Channel;} + CSound* GetSndInterface () { return &Sound; } + CChannel* GetChannel () { return &Channel; } // settings string strIPAddress; protected: - virtual void run(); + virtual void run (); /* only one channel is needed for client application */ CChannel Channel; @@ -129,6 +143,9 @@ protected: /* debugging, evaluating */ CMovingAv RespTimeMoAvBuf; QTime TimeLastBlock; + +public slots: + void OnSendProtMessage (); }; diff --git a/src/global.h b/src/global.h index 4f445a3c..66516f2e 100755 --- a/src/global.h +++ b/src/global.h @@ -42,7 +42,7 @@ /* version and application name */ #ifndef VERSION -# define VERSION "0.9.3cvs" +# define VERSION "0.9.4cvs" #endif #define APP_NAME "llcon" @@ -125,7 +125,7 @@ typedef unsigned int _MESSAGE_IDENT; #define MS_SOUND_OUT 2 #define MS_JIT_BUF_PUT 3 #define MS_JIT_BUF_GET 4 -#define MS_PACKET_RECEIVED 5 +#define MS_PACKET_RECEIVED 5 #define MUL_COL_LED_RED 0 #define MUL_COL_LED_YELLOW 1 diff --git a/src/llconclientdlg.cpp b/src/llconclientdlg.cpp index eeba740c..1f4f1c21 100755 --- a/src/llconclientdlg.cpp +++ b/src/llconclientdlg.cpp @@ -98,7 +98,7 @@ CLlconClientDlg::CLlconClientDlg ( CClient* pNCliP, QWidget* parent, /* network buffer */ SliderNetBuf->setRange(1, MAX_NET_BUF_SIZE_NUM_BL); - const int iCurNumNetBuf = pClient->GetChannel()->GetSockBufSize(); + const int iCurNumNetBuf = pClient->GetSockBufSize(); SliderNetBuf->setValue(iCurNumNetBuf); TextNetBuf->setText("Size: " + QString().setNum(iCurNumNetBuf)); @@ -240,7 +240,7 @@ void CLlconClientDlg::OnSliderSndBufOutChange(int value) void CLlconClientDlg::OnSliderNetBuf(int value) { - pClient->GetChannel()->SetSockBufSize ( MIN_BLOCK_SIZE_SAMPLES, value ); + pClient->SetSockBufSize ( MIN_BLOCK_SIZE_SAMPLES, value ); TextNetBuf->setText("Size: " + QString().setNum(value)); } diff --git a/src/protocol.cpp b/src/protocol.cpp index bd39e1fa..68e6b8f2 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -58,9 +58,37 @@ MESSAGES /* Implementation *************************************************************/ +CVector CProtocol::GetSendMessage () +{ +// TEST, TODO implement protocol handling here (timers, etc.) -bool CClientProtocol::ParseMessage ( const CVector& vecbyData, - const int iNumBytes ) +// convert unsigned uint8_t in char, TODO convert all buffers in uint8_t +CVector vecbyDataConv ( vecMessage.Size () ); +for ( int i = 0; i < vecMessage.Size (); i++ ) { + vecbyDataConv[i] = static_cast ( vecMessage[i] ); +} + + return vecbyDataConv; +} + + + +void CProtocol::EnqueueMessage ( CVector& vecMessage ) +{ + /* TODO */ + +emit MessReadyForSending (); + +} + + + + + + + +bool CProtocol::ParseMessage ( const CVector& vecbyData, + const int iNumBytes ) { /* return code: true -> ok; false -> error @@ -68,6 +96,11 @@ bool CClientProtocol::ParseMessage ( const CVector& vecbyData, int iRecCounter, iRecID; CVector vecData; + +// TEST +qDebug ( "parser entered" ); + + // convert unsigned char in uint8_t, TODO convert all buffers in uint8_t CVector vecbyDataConv ( vecbyData.Size () ); for ( int i = 0; i < vecbyData.Size (); i++ ) { @@ -95,7 +128,7 @@ for ( int i = 0; i < vecbyData.Size (); i++ ) { } } -void CClientProtocol::CreateJitBufMes ( const int iJitBufSize ) +void CProtocol::CreateJitBufMes ( const int iJitBufSize ) { CVector vecData ( 2 ); unsigned int iPos = 0; @@ -105,6 +138,13 @@ void CClientProtocol::CreateJitBufMes ( const int iJitBufSize ) // build complete message GenMessageFrame ( vecMessage, iCounter, PROTMESSID_JITT_BUF_SIZE, vecData ); + +// increase counter (wraps around automatically) +// TODO: make it thread safe!!!!!!!!!!!! +iCounter++; + + // enqueue message + EnqueueMessage ( vecMessage ); } diff --git a/src/protocol.h b/src/protocol.h index 3d58c174..39adf8bc 100755 --- a/src/protocol.h +++ b/src/protocol.h @@ -26,6 +26,7 @@ #define PROTOCOL_H__3B123453_4344_BB2392354455IUHF1912__INCLUDED_ #include +#include #include "global.h" #include "util.h" @@ -42,13 +43,25 @@ /* Classes ********************************************************************/ -class CProtocol +class CProtocol : public QObject { + Q_OBJECT + public: CProtocol () : iCounter ( 0 ) {} virtual ~CProtocol () {} + void CreateJitBufMes ( const int iJitBufSize ); + + bool ParseMessage ( const CVector& vecbyData, + const int iNumBytes ); + + CVector GetSendMessage (); + protected: + void EnqueueMessage ( CVector& vecMessage ); + + bool ParseMessageFrame ( const CVector& vecIn, int& iCnt, int& iID, @@ -70,42 +83,12 @@ protected: CVector vecMessage; uint8_t iCounter; + + bool bIsClient; + +signals: + void MessReadyForSending (); }; - -class CClientProtocol : public CProtocol -{ -public: - CClientProtocol () {} - virtual ~CClientProtocol () {} - - void CreateJitBufMes ( const int iJitBufSize ); - - bool ParseMessage ( const CVector& vecbyData, - const int iNumBytes ); - -protected: - -}; - - -class CServerProtocol : public CProtocol -{ -public: - CServerProtocol () {} - virtual ~CServerProtocol () {} - - bool ParseMessage ( const CVector& vecbyData, - const int iNumBytes ); - -protected: - -}; - - - - - - #endif /* !defined(PROTOCOL_H__3B123453_4344_BB2392354455IUHF1912__INCLUDED_) */ diff --git a/src/settings.cpp b/src/settings.cpp index 60bfdc76..ae0a7c66 100755 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -79,7 +79,7 @@ void CSettings::ReadIniFile() // network jitter buffer size if ( GetNumericIniSet(ini, "Client", "jitbuf", 0, MAX_NET_BUF_SIZE_NUM_BL, iValue ) == TRUE ) { - pClient->GetChannel()->SetSockBufSize ( MIN_BLOCK_SIZE_SAMPLES, iValue ); + pClient->SetSockBufSize ( MIN_BLOCK_SIZE_SAMPLES, iValue ); } } @@ -106,7 +106,7 @@ void CSettings::WriteIniFile() SetNumericIniSet ( ini, "Client", "audoutbuf", pClient->GetSndInterface()->GetOutNumBuf () ); // network jitter buffer size - SetNumericIniSet ( ini, "Client", "jitbuf", pClient->GetChannel()->GetSockBufSize () ); + SetNumericIniSet ( ini, "Client", "jitbuf", pClient->GetSockBufSize () ); /* Save settings in init-file */ diff --git a/src/socket.cpp b/src/socket.cpp index 1429d85f..bb7db020 100755 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -76,7 +76,7 @@ void CSocket::SendPacket( const CVector& vecbySendBuf, { /* send packet through network */ SocketDevice.writeBlock ( - (const char*) &((CVector) vecbySendBuf)[0], + (const char*) &( (CVector) vecbySendBuf )[0], iVecSizeOut, HostAddr.InetAddr, HostAddr.iPort ); } @@ -93,7 +93,7 @@ void CSocket::OnDataReceived () { /* read block from network interface */ const int iNumBytesRead = SocketDevice.readBlock( (char*) &vecbyRecBuf[0], - MAX_SIZE_BYTES_NETW_BUF); + MAX_SIZE_BYTES_NETW_BUF ); /* check if an error occurred */ if ( iNumBytesRead < 0 ) diff --git a/src/socket.h b/src/socket.h index bbadf920..71e712fc 100755 --- a/src/socket.h +++ b/src/socket.h @@ -69,6 +69,7 @@ protected: CChannel* pChannel; /* for client */ CChannelSet* pChannelSet; /* for server */ + QObject* pServer; bool bIsClient; diff --git a/windows/MocQT.bat b/windows/MocQT.bat index 1ab82b26..d66597a6 100755 --- a/windows/MocQT.bat +++ b/windows/MocQT.bat @@ -30,8 +30,11 @@ rem .h -------------- %qtdir%\bin\moc.exe ..\src\multicolorled.h -o moc\moc_multicolorled.cpp %qtdir%\bin\moc.exe ..\src\llconclientdlg.h -o moc\moc_llconclientdlg.cpp %qtdir%\bin\moc.exe ..\src\llconserverdlg.h -o moc\moc_llconserverdlg.cpp -%qtdir%\bin\moc.exe ..\src\server.h -o moc\moc_server.cpp +%qtdir%\bin\moc.exe ..\src\server.h -o moc\moc_server.cpp +%qtdir%\bin\moc.exe ..\src\client.h -o moc\moc_client.cpp %qtdir%\bin\moc.exe ..\src\socket.h -o moc\moc_socket.cpp +%qtdir%\bin\moc.exe ..\src\protocol.h -o moc\moc_protocol.cpp +%qtdir%\bin\moc.exe ..\src\channel.h -o moc\moc_channel.cpp rem .ui ------------- diff --git a/windows/llcon.dsp b/windows/llcon.dsp index 30af9b4b..a4caff78 100755 --- a/windows/llcon.dsp +++ b/windows/llcon.dsp @@ -113,6 +113,14 @@ SOURCE=.\moc\moc_aboutdlgbase.cpp # End Source File # Begin Source File +SOURCE=.\moc\moc_channel.cpp +# End Source File +# Begin Source File + +SOURCE=.\moc\moc_client.cpp +# End Source File +# Begin Source File + SOURCE=.\moc\moc_llconclientdlg.cpp # End Source File # Begin Source File @@ -133,6 +141,10 @@ SOURCE=.\moc\moc_multicolorled.cpp # End Source File # Begin Source File +SOURCE=.\moc\moc_protocol.cpp +# End Source File +# Begin Source File + SOURCE=.\moc\moc_server.cpp # End Source File # Begin Source File