diff --git a/src/audiocompr.cpp b/src/audiocompr.cpp index c90fe72c..2819ac89 100755 --- a/src/audiocompr.cpp +++ b/src/audiocompr.cpp @@ -33,20 +33,24 @@ /* Implementation *************************************************************/ -int CAudioCompression::Init(const int iNewAudioLen, - const EAudComprType eNewAuCoTy) +int CAudioCompression::Init ( const int iNewAudioLen, + const EAudComprType eNewAuCoTy ) { eAudComprType = eNewAuCoTy; - switch (eNewAuCoTy) + switch ( eNewAuCoTy ) { - case CT_NONE: return iCodeSize = 2 * iNewAudioLen; /* short = 2 * byte */ - case CT_IMAADPCM: return ImaAdpcm.Init(iNewAudioLen); + case CT_NONE: + return iCodeSize = 2 * iNewAudioLen; /* short = 2 * byte */ + + case CT_IMAADPCM: + return ImaAdpcm.Init ( iNewAudioLen ); + default: return 0; } } -CVector CAudioCompression::Encode(const CVector& vecsAudio) +CVector CAudioCompression::Encode ( const CVector& vecsAudio ) { if (eAudComprType == CT_NONE) { @@ -63,10 +67,13 @@ CVector CAudioCompression::Encode(const CVector& vecsAudio } else { - switch (eAudComprType) + switch ( eAudComprType ) { - case CT_IMAADPCM: return ImaAdpcm.Encode(vecsAudio); /* IMA-ADPCM */ - default: return CVector(0); + case CT_IMAADPCM: + return ImaAdpcm.Encode ( vecsAudio ); /* IMA-ADPCM */ + + default: + return CVector ( 0 ); } } } @@ -91,10 +98,13 @@ CVector CAudioCompression::Decode(const CVector& vecbyAdpc } else { - switch (eAudComprType) + switch ( eAudComprType ) { - case CT_IMAADPCM: return ImaAdpcm.Decode(vecbyAdpcm); /* IMA-ADPCM */ - default: return CVector(0); + case CT_IMAADPCM: + return ImaAdpcm.Decode ( vecbyAdpcm ); /* IMA-ADPCM */ + + default: + return CVector ( 0 ); } } } diff --git a/src/audiocompr.h b/src/audiocompr.h index 8117c846..81ce88ce 100755 --- a/src/audiocompr.h +++ b/src/audiocompr.h @@ -56,12 +56,12 @@ static int ima_step_size[IMA_STEP_SIZE_TAB_LEN] = class CImaAdpcm { public: - CImaAdpcm() : iStepindEnc(0) {} + CImaAdpcm() : iStepindEnc ( 0 ) {} virtual ~CImaAdpcm() {} - int Init(const int iNewAudioLen); - CVector Encode(const CVector& vecsAudio); - CVector Decode(const CVector& vecbyAdpcm); + int Init ( const int iNewAudioLen ); + CVector Encode ( const CVector& vecsAudio ); + CVector Decode ( const CVector& vecbyAdpcm ); protected: int iAudSize; @@ -69,12 +69,17 @@ protected: int iStepindEnc; /* inline functions must be declared in the header */ - inline int CheckBounds(const int iData, const int iMin, const int iMax) + inline int CheckBounds ( const int iData, const int iMin, const int iMax ) { - if (iData > iMax) - return iMax; - if (iData < iMin) - return iMin; + if ( iData > iMax ) + { + return iMax; + } + + if ( iData < iMin ) + { + return iMin; + } return iData; } @@ -90,9 +95,10 @@ public: CAudioCompression() {} virtual ~CAudioCompression() {} - int Init(const int iNewAudioLen, const EAudComprType eNewAuCoTy); - CVector Encode(const CVector& vecsAudio); - CVector Decode(const CVector& vecbyAdpcm); + int Init ( const int iNewAudioLen, + const EAudComprType eNewAuCoTy ); + CVector Encode ( const CVector& vecsAudio ); + CVector Decode ( const CVector& vecbyAdpcm ); protected: EAudComprType eAudComprType; diff --git a/src/channel.cpp b/src/channel.cpp index 6d5cecfe..f23df02a 100755 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -229,14 +229,14 @@ CChannel::CChannel () SetSockBufSize ( MIN_BLOCK_SIZE_SAMPLES, DEF_NET_BUF_SIZE_NUM_BL ); /* init conversion buffer */ - ConvBuf.Init ( BLOCK_SIZE_SAMPLES ); - - /* init audio compression unit */ - iAudComprSize = AudioCompression.Init ( BLOCK_SIZE_SAMPLES, - CAudioCompression::CT_IMAADPCM ); + ConvBuf.Init ( BLOCK_SIZE_SAMPLES ); + + // set initial input and output block size factors + SetNetwInBlSiFact ( NET_BLOCK_SIZE_FACTOR ); + SetNetwOutBlSiFact ( NET_BLOCK_SIZE_FACTOR ); /* init time-out for the buffer with zero -> no connection */ - iConTimeOut = 0; + iConTimeOut = 0; /* connections ---------------------------------------------------------- */ @@ -251,6 +251,32 @@ CChannel::CChannel () SIGNAL ( ReqJittBufSize() ) ); } +void CChannel::SetNetwInBlSiFact ( const int iNewBlockSizeFactor ) +{ + // store new value + iCurNetwInBlSiFact = iNewBlockSizeFactor; + + /* init audio compression unit */ + iAudComprSizeIn = AudioCompressionIn.Init ( + iNewBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES, + CAudioCompression::CT_IMAADPCM ); + + // initial value for connection time out counter + iConTimeOutStartVal = ( CON_TIME_OUT_SEC_MAX * 1000 ) / + ( iNewBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES ); +} + +void CChannel::SetNetwOutBlSiFact ( const int iNewBlockSizeFactor ) +{ + /* init audio compression unit */ + iAudComprSizeOut = AudioCompressionOut.Init ( + iNewBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES, + CAudioCompression::CT_IMAADPCM ); + + /* init conversion buffer */ + ConvBuf.Init ( iNewBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES ); +} + void CChannel::OnSendProtMessage ( CVector vecMessage ) { // only send messages if we are connected, otherwise delete complete queue @@ -304,11 +330,10 @@ EPutDataStat CChannel::PutData ( const CVector& vecbyData, EPutDataStat eRet = PS_GEN_ERROR; /* only process if packet has correct size */ - if ( iNumBytes == iAudComprSize ) + if ( iNumBytes == iAudComprSizeIn ) { /* decompress audio */ - CVector vecsDecomprAudio ( BLOCK_SIZE_SAMPLES ); - vecsDecomprAudio = AudioCompression.Decode ( vecbyData ); + CVector vecsDecomprAudio ( AudioCompressionIn.Decode ( vecbyData ) ); /* do resampling to compensate for sample rate offsets in the different sound cards of the clients */ @@ -320,9 +345,10 @@ const int iInSize = ResampleObj.Resample(vecdResInData, vecdResOutData, (double) SAMPLE_RATE / (SAMPLE_RATE - dSamRateOffset)); */ -vecdResOutData.Init(BLOCK_SIZE_SAMPLES); -for (int i = 0; i < BLOCK_SIZE_SAMPLES; i++) - vecdResOutData[i] = (double) vecsDecomprAudio[i]; +vecdResOutData.Init ( iCurNetwInBlSiFact * MIN_BLOCK_SIZE_SAMPLES ); +for ( int i = 0; i < iCurNetwInBlSiFact * MIN_BLOCK_SIZE_SAMPLES; i++ ) { + vecdResOutData[i] = (double) vecsDecomprAudio[i]; +} Mutex.lock (); /* put mutex lock */ @@ -342,7 +368,7 @@ for (int i = 0; i < BLOCK_SIZE_SAMPLES; i++) const bool bChanWasNotConnected = !IsConnected(); // reset time-out counter - iConTimeOut = CON_TIME_OUT_CNT_MAX; + iConTimeOut = iConTimeOutStartVal; // if channel was not connected, emit signal to inform that new // connection was established @@ -403,9 +429,9 @@ CVector CChannel::PrepSendPacket(const CVector& vecsNPacke block size */ if ( ConvBuf.Put ( vecsNPacket ) ) { - /* a packet is ready, compress audio */ - vecbySendBuf.Init ( iAudComprSize ); - vecbySendBuf = AudioCompression.Encode ( ConvBuf.Get () ); + /* a packet is ready, compress audio */ + vecbySendBuf.Init ( iAudComprSizeOut ); + vecbySendBuf = AudioCompressionOut.Encode ( ConvBuf.Get () ); } return vecbySendBuf; diff --git a/src/channel.h b/src/channel.h index 8927aa15..cf5a61a2 100755 --- a/src/channel.h +++ b/src/channel.h @@ -40,7 +40,6 @@ connected to not-connected (the actual time depends on the way the error correction is implemented) */ #define CON_TIME_OUT_SEC_MAX 5 // seconds -#define CON_TIME_OUT_CNT_MAX ( ( CON_TIME_OUT_SEC_MAX * 1000 ) / BLOCK_DURATION_MS ) /* maximum number of internet connections (channels) */ // if you want to change this paramter, change the connections in this class, too! @@ -77,8 +76,6 @@ public: bool IsConnected() const { return iConTimeOut > 0; } - int GetComprAudSize() { return iAudComprSize; } - void SetAddress ( const CHostAddress NAddr ) { InetAddr = NAddr; } bool GetAddress ( CHostAddress& RetAddr ); CHostAddress GetAddress () { return InetAddr; } @@ -97,10 +94,15 @@ public: void CreateReqJitBufMes() { Protocol.CreateReqJitBufMes(); } -protected: +protected: + void SetNetwInBlSiFact ( const int iNewBlockSizeFactor ); + void SetNetwOutBlSiFact ( const int iNewBlockSizeFactor ); + /* audio compression */ - CAudioCompression AudioCompression; - int iAudComprSize; + CAudioCompression AudioCompressionIn; + int iAudComprSizeIn; + CAudioCompression AudioCompressionOut; + int iAudComprSizeOut; /* resampling */ CResample ResampleObj; @@ -124,7 +126,10 @@ protected: Q_UINT8 byTimeStampIdxCnt; int iTimeStampActCnt; - int iConTimeOut; + int iConTimeOut; + int iConTimeOutStartVal; + + int iCurNetwInBlSiFact; QMutex Mutex; diff --git a/src/global.h b/src/global.h index 5be00f31..d502e6b6 100755 --- a/src/global.h +++ b/src/global.h @@ -65,11 +65,15 @@ #define MIN_SND_CRD_BLOCK_SIZE_SAMPLES ( MIN_BLOCK_DURATION_MS * SND_CRD_SAMPLE_RATE / 1000 ) /* first tests showed that with 24000 kHz a block time shorter than 5 ms leads to - much higher DSL network latencies. A length of 6 ms seems to be optimal */ + much higher DSL network latencies. A length of 6 ms seems to be optimal */ +#define NET_BLOCK_SIZE_FACTOR 3 // 3 * 2 ms = 6 ms + +// maximum value of factor for network block size +#define NET_BLOCK_SIZE_FACTOR_MAX 10 + #define BLOCK_DURATION_MS 6 /* ms */ #define BLOCK_SIZE_SAMPLES ( BLOCK_DURATION_MS * SAMPLE_RATE / 1000 ) -#define SND_CRD_BLOCK_SIZE_SAMPLES ( BLOCK_DURATION_MS * SND_CRD_SAMPLE_RATE / 1000 ) /* maximum network buffer size (which can be chosen by slider) */ #define MAX_NET_BUF_SIZE_NUM_BL 12 /* number of blocks */ @@ -97,7 +101,7 @@ /* length of the moving average buffer for response time measurement */ #define TIME_MOV_AV_RESPONSE 30 /* seconds */ -#define LEN_MOV_AV_RESPONSE (TIME_MOV_AV_RESPONSE * 1000 / BLOCK_DURATION_MS) +#define LEN_MOV_AV_RESPONSE (TIME_MOV_AV_RESPONSE * 1000 / MIN_BLOCK_DURATION_MS) #define _MAXSHORT 32767 diff --git a/src/socket.h b/src/socket.h index fc099808..943c3c19 100755 --- a/src/socket.h +++ b/src/socket.h @@ -38,7 +38,7 @@ /* Definitions ****************************************************************/ /* maximum block size for network input buffer. Consider two bytes per sample */ -#define MAX_SIZE_BYTES_NETW_BUF (BLOCK_SIZE_SAMPLES * 2) +#define MAX_SIZE_BYTES_NETW_BUF ( NET_BLOCK_SIZE_FACTOR_MAX * MIN_BLOCK_SIZE_SAMPLES * 2 ) /* Classes ********************************************************************/ diff --git a/windows/sound.h b/windows/sound.h index d75a7682..913db6cb 100755 --- a/windows/sound.h +++ b/windows/sound.h @@ -42,11 +42,8 @@ #define MAX_SND_BUF_IN 200 #define MAX_SND_BUF_OUT 200 -#define NUM_SOUND_BUFFERS_IN (70 / BLOCK_DURATION_MS) -#define NUM_SOUND_BUFFERS_OUT (80 / BLOCK_DURATION_MS) - -//#define NUM_SOUND_BUFFERS_IN 7//200 /* Number of sound card buffers */ -//#define NUM_SOUND_BUFFERS_OUT 8//100//15 /* Number of sound card buffers */ +#define NUM_SOUND_BUFFERS_IN (70 / MIN_BLOCK_DURATION_MS) +#define NUM_SOUND_BUFFERS_OUT (80 / MIN_BLOCK_DURATION_MS) /* Maximum number of recognized sound cards installed in the system */ #define MAX_NUMBER_SOUND_CARDS 10