small changes (mostly in comments)

This commit is contained in:
Volker Fischer 2007-12-31 13:09:12 +00:00
parent a7a90201c2
commit 84d69211c5
7 changed files with 114 additions and 106 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************\ /******************************************************************************\
* Copyright (c) 2004-2006 * Copyright (c) 2004-2008
* *
* Author(s): * Author(s):
* Volker Fischer, Erik de Castro Lopo * Volker Fischer, Erik de Castro Lopo
@ -41,7 +41,7 @@ int CAudioCompression::Init ( const int iNewAudioLen,
switch ( eNewAuCoTy ) switch ( eNewAuCoTy )
{ {
case CT_NONE: case CT_NONE:
return iCodeSize = 2 * iNewAudioLen; /* short = 2 * byte */ return iCodeSize = 2 * iNewAudioLen; // short = 2 * byte
case CT_IMAADPCM: case CT_IMAADPCM:
return ImaAdpcm.Init ( iNewAudioLen ); return ImaAdpcm.Init ( iNewAudioLen );
@ -54,7 +54,7 @@ CVector<unsigned char> CAudioCompression::Encode ( const CVector<short>& vecsAud
{ {
if ( eAudComprType == CT_NONE ) if ( eAudComprType == CT_NONE )
{ {
/* no compression, simply ship pure samples */ // no compression, simply ship pure samples
CVector<unsigned char> vecbyOut ( iCodeSize ); CVector<unsigned char> vecbyOut ( iCodeSize );
const int iAudSize = iCodeSize / 2; const int iAudSize = iCodeSize / 2;
@ -70,7 +70,7 @@ CVector<unsigned char> CAudioCompression::Encode ( const CVector<short>& vecsAud
switch ( eAudComprType ) switch ( eAudComprType )
{ {
case CT_IMAADPCM: case CT_IMAADPCM:
return ImaAdpcm.Encode ( vecsAudio ); /* IMA-ADPCM */ return ImaAdpcm.Encode ( vecsAudio ); // IMA-ADPCM
default: default:
return CVector<unsigned char> ( 0 ); return CVector<unsigned char> ( 0 );
@ -82,7 +82,7 @@ CVector<short> CAudioCompression::Decode ( const CVector<unsigned char>& vecbyAd
{ {
if ( eAudComprType == CT_NONE ) if ( eAudComprType == CT_NONE )
{ {
/* no compression, reassemble pure samples */ // no compression, reassemble pure samples
const int iAudSize = iCodeSize / 2; const int iAudSize = iCodeSize / 2;
CVector<short> vecsOut ( iAudSize ); CVector<short> vecsOut ( iAudSize );
@ -103,7 +103,7 @@ CVector<short> CAudioCompression::Decode ( const CVector<unsigned char>& vecbyAd
switch ( eAudComprType ) switch ( eAudComprType )
{ {
case CT_IMAADPCM: case CT_IMAADPCM:
return ImaAdpcm.Decode ( vecbyAdpcm ); /* IMA-ADPCM */ return ImaAdpcm.Decode ( vecbyAdpcm ); // IMA-ADPCM
default: default:
return CVector<short> ( 0 ); return CVector<short> ( 0 );
@ -115,7 +115,7 @@ CVector<short> CAudioCompression::Decode ( const CVector<unsigned char>& vecbyAd
/* IMA-ADPCM implementation ------------------------------------------------- */ /* IMA-ADPCM implementation ------------------------------------------------- */
int CImaAdpcm::Init ( const int iNewAudioLen ) int CImaAdpcm::Init ( const int iNewAudioLen )
{ {
/* set lengths for audio and compressed data */ // set lengths for audio and compressed data
iAudSize = iNewAudioLen; iAudSize = iNewAudioLen;
iAdpcmSize = 4 /* bytes header */ + (int) ceil ( iAdpcmSize = 4 /* bytes header */ + (int) ceil (
(double) ( iAudSize - 2 /* first two samples are in header */ ) / 2 ); (double) ( iAudSize - 2 /* first two samples are in header */ ) / 2 );
@ -131,7 +131,7 @@ CVector<unsigned char> CImaAdpcm::Encode ( const CVector<short>& vecsAudio )
CVector<unsigned char> vecbyAdpcm; CVector<unsigned char> vecbyAdpcm;
CVector<unsigned char> vecbyAdpcmTemp; CVector<unsigned char> vecbyAdpcmTemp;
/* init size */ // init size
vecbyAdpcm.Init ( iAdpcmSize ); vecbyAdpcm.Init ( iAdpcmSize );
vecbyAdpcmTemp.Init ( iAudSize ); vecbyAdpcmTemp.Init ( iAudSize );
@ -146,8 +146,10 @@ CVector<unsigned char> CImaAdpcm::Encode ( const CVector<short>& vecsAudio )
/* encode the samples as 4 bit ------------------------------------------- */ /* encode the samples as 4 bit ------------------------------------------- */
for ( i = 1; i < iAudSize; i++ ) for ( i = 1; i < iAudSize; i++ )
{ {
/* init diff and step */ // init diff and step
int diff = vecsAudio[i] - iPrevAudio; int diff = vecsAudio[i] - iPrevAudio;
ASSERT ( iStepindEnc < IMA_STEP_SIZE_TAB_LEN );
int step = ima_step_size[iStepindEnc]; int step = ima_step_size[iStepindEnc];
short bytecode = 0; short bytecode = 0;
@ -180,20 +182,21 @@ CVector<unsigned char> CImaAdpcm::Encode ( const CVector<short>& vecsAudio )
iPrevAudio += vpdiff; iPrevAudio += vpdiff;
} }
/* adjust step size */ // adjust step size
ASSERT ( bytecode < IMA_INDX_ADJUST_TAB_LEN );
iStepindEnc += ima_indx_adjust[bytecode]; iStepindEnc += ima_indx_adjust[bytecode];
/* check that values do not exceed the bounds */ // check that values do not exceed the bounds
iPrevAudio = CheckBounds ( iPrevAudio, _MINSHORT, _MAXSHORT ); iPrevAudio = CheckBounds ( iPrevAudio, _MINSHORT, _MAXSHORT );
iStepindEnc = CheckBounds ( iStepindEnc, 0, IMA_STEP_SIZE_TAB_LEN - 1 ); iStepindEnc = CheckBounds ( iStepindEnc, 0, IMA_STEP_SIZE_TAB_LEN - 1 );
/* use the input buffer as an intermediate result buffer */ // use the input buffer as an intermediate result buffer
vecbyAdpcmTemp[i] = bytecode; vecbyAdpcmTemp[i] = bytecode;
} }
/* pack the 4 bit encoded samples ---------------------------------------- */ /* pack the 4 bit encoded samples ---------------------------------------- */
/* The first encoded audio sample is in header */ // The first encoded audio sample is in header
vecbyAdpcm[3] = vecbyAdpcmTemp[1] & 0x0F; vecbyAdpcm[3] = vecbyAdpcmTemp[1] & 0x0F;
for ( i = 4; i < iAdpcmSize; i++ ) for ( i = 4; i < iAdpcmSize; i++ )
@ -220,17 +223,17 @@ CVector<short> CImaAdpcm::Decode ( const CVector<unsigned char>& vecbyAdpcm )
current -= 0x10000; current -= 0x10000;
} }
/* get and bound step index */ // get and bound step index
int iStepindDec = CheckBounds ( vecbyAdpcm[2], 0, IMA_STEP_SIZE_TAB_LEN - 1 ); int iStepindDec = CheckBounds ( vecbyAdpcm[2], 0, IMA_STEP_SIZE_TAB_LEN - 1 );
/* set first sample which was delivered in the header */ // set first sample which was delivered in the header
vecsAudio[0] = current; vecsAudio[0] = current;
/* -------------------------------------------------------------------------- /* --------------------------------------------------------------------------
pull apart the packed 4 bit samples and store them in their correct sample pull apart the packed 4 bit samples and store them in their correct sample
positions */ positions */
/* The first encoded audio sample is in header */ // The first encoded audio sample is in header
vecsAudio[1] = vecbyAdpcm[3] & 0x0F; vecsAudio[1] = vecbyAdpcm[3] & 0x0F;
for ( i = 4; i < iAdpcmSize; i++ ) for ( i = 4; i < iAdpcmSize; i++ )
@ -246,6 +249,8 @@ CVector<short> CImaAdpcm::Decode ( const CVector<unsigned char>& vecbyAdpcm )
{ {
const short bytecode = vecsAudio[i] & 0xF ; const short bytecode = vecsAudio[i] & 0xF ;
ASSERT ( iStepindDec < IMA_STEP_SIZE_TAB_LEN );
short step = ima_step_size[iStepindDec]; short step = ima_step_size[iStepindDec];
int current = vecsAudio[i - 1]; int current = vecsAudio[i - 1];
@ -268,9 +273,11 @@ CVector<short> CImaAdpcm::Decode ( const CVector<unsigned char>& vecbyAdpcm )
} }
current += diff; current += diff;
ASSERT ( bytecode < IMA_INDX_ADJUST_TAB_LEN );
iStepindDec += ima_indx_adjust[bytecode]; iStepindDec += ima_indx_adjust[bytecode];
/* check that values do not exceed the bounds */ // check that values do not exceed the bounds
current = CheckBounds ( current, _MINSHORT, _MAXSHORT ); current = CheckBounds ( current, _MINSHORT, _MAXSHORT );
iStepindDec = CheckBounds ( iStepindDec, 0, IMA_STEP_SIZE_TAB_LEN - 1 ); iStepindDec = CheckBounds ( iStepindDec, 0, IMA_STEP_SIZE_TAB_LEN - 1 );

View file

@ -31,8 +31,9 @@
/* Definitions ****************************************************************/ /* Definitions ****************************************************************/
/* tables */ // tables
static int ima_indx_adjust[16] = #define IMA_INDX_ADJUST_TAB_LEN 16
static int ima_indx_adjust[IMA_INDX_ADJUST_TAB_LEN] =
{ -1, -1, -1, -1, /* +0 - +3, decrease the step size */ { -1, -1, -1, -1, /* +0 - +3, decrease the step size */
2, 4, 6, 8, /* +4 - +7, increase the step size */ 2, 4, 6, 8, /* +4 - +7, increase the step size */
-1, -1, -1, -1, /* -0 - -3, decrease the step size */ -1, -1, -1, -1, /* -0 - -3, decrease the step size */

View file

@ -31,20 +31,20 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks ) void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks )
{ {
/* total size -> size of one block times number of blocks */ // total size -> size of one block times number of blocks
iBlockSize = iNewBlockSize; iBlockSize = iNewBlockSize;
iMemSize = iNewBlockSize * iNewNumBlocks; iMemSize = iNewBlockSize * iNewNumBlocks;
/* fade in first block added to the buffer */ // fade in first block added to the buffer
bFadeInNewPutData = true; bFadeInNewPutData = true;
/* allocate and clear memory for actual data buffer */ // allocate and clear memory for actual data buffer
vecdMemory.Init ( iMemSize ); vecdMemory.Init ( iMemSize );
/* use the "get" flag to make sure the buffer is cleared */ // use the "get" flag to make sure the buffer is cleared
Clear ( CT_GET ); Clear ( CT_GET );
/* initialize number of samples for fading effect */ // initialize number of samples for fading effect
if ( FADE_IN_OUT_NUM_SAM < iBlockSize ) if ( FADE_IN_OUT_NUM_SAM < iBlockSize )
{ {
iNumSamFading = iBlockSize; iNumSamFading = iBlockSize;
@ -63,7 +63,7 @@ void CNetBuf::Init ( const int iNewBlockSize, const int iNewNumBlocks )
iNumSamFadingExtra = FADE_IN_OUT_NUM_SAM; iNumSamFadingExtra = FADE_IN_OUT_NUM_SAM;
} }
/* init variables for extrapolation (in case a fade out is needed) */ // init variables for extrapolation (in case a fade out is needed)
dExPDiff = 0.0; dExPDiff = 0.0;
dExPLastV = 0.0; dExPLastV = 0.0;
} }
@ -78,36 +78,36 @@ fflush(pFileBI);
bool bPutOK = true; bool bPutOK = true;
/* get size of data to be added to the buffer */ // get size of data to be added to the buffer
const int iInSize = vecdData.Size(); const int iInSize = vecdData.Size();
/* Check if there is not enough space available -> correct */ // Check if there is not enough space available -> correct
if ( GetAvailSpace() < iInSize ) if ( GetAvailSpace() < iInSize )
{ {
/* not enough space in buffer for put operation, correct buffer to // not enough space in buffer for put operation, correct buffer to
prepare for new data */ // prepare for new data
Clear ( CT_PUT ); Clear ( CT_PUT );
/* set flag to fade in new block to avoid clicks */ // set flag to fade in new block to avoid clicks
bFadeInNewPutData = true; bFadeInNewPutData = true;
bPutOK = false; /* return error flag */ bPutOK = false; // return error flag
} }
/* fade in new block if required */ // fade in new block if required
if ( bFadeInNewPutData ) if ( bFadeInNewPutData )
{ {
FadeInAudioDataBlock ( vecdData ); FadeInAudioDataBlock ( vecdData );
} }
/* copy new data in internal buffer */ // copy new data in internal buffer
int iCurPos = 0; int iCurPos = 0;
if ( iPutPos + iInSize > iMemSize ) if ( iPutPos + iInSize > iMemSize )
{ {
/* remaining space size for second block */ // remaining space size for second block
const int iRemSpace = iPutPos + iInSize - iMemSize; const int iRemSpace = iPutPos + iInSize - iMemSize;
/* data must be written in two steps because of wrap around */ // data must be written in two steps because of wrap around
while (iPutPos < iMemSize) while (iPutPos < iMemSize)
{ {
vecdMemory[iPutPos++] = vecdData[iCurPos++]; vecdMemory[iPutPos++] = vecdData[iCurPos++];
@ -120,7 +120,7 @@ fflush(pFileBI);
} }
else else
{ {
/* data can be written in one step */ // data can be written in one step
const int iEnd = iPutPos + iInSize; const int iEnd = iPutPos + iInSize;
while ( iPutPos < iEnd ) while ( iPutPos < iEnd )
{ {
@ -128,7 +128,7 @@ fflush(pFileBI);
} }
} }
/* set buffer state flag */ // set buffer state flag
if ( iPutPos == iGetPos ) if ( iPutPos == iGetPos )
{ {
eBufState = CNetBuf::BS_FULL; eBufState = CNetBuf::BS_FULL;
@ -143,35 +143,35 @@ fflush(pFileBI);
bool CNetBuf::Get ( CVector<double>& vecdData ) bool CNetBuf::Get ( CVector<double>& vecdData )
{ {
bool bGetOK = true; /* init return value */ bool bGetOK = true; // init return value
bool bFadeOutExtrap = false; bool bFadeOutExtrap = false;
/* get size of data to be get from the buffer */ // get size of data to be get from the buffer
const int iInSize = vecdData.Size(); const int iInSize = vecdData.Size();
/* Check if there is not enough data available -> correct */ // Check if there is not enough data available -> correct
if ( GetAvailData() < iInSize ) if ( GetAvailData() < iInSize )
{ {
/* not enough data in buffer for get operation, correct buffer to // not enough data in buffer for get operation, correct buffer to
prepare for getting data */ // prepare for getting data
Clear ( CT_GET ); Clear ( CT_GET );
/* set flag to fade in next new block in buffer and fade out last // set flag to fade in next new block in buffer and fade out last
block by extrapolation to avoid clicks */ // block by extrapolation to avoid clicks
bFadeInNewPutData = true; bFadeInNewPutData = true;
bFadeOutExtrap = true; bFadeOutExtrap = true;
bGetOK = false; /* return error flag */ bGetOK = false; // return error flag
} }
/* copy data from internal buffer in output buffer */ // copy data from internal buffer in output buffer
int iCurPos = 0; int iCurPos = 0;
if ( iGetPos + iInSize > iMemSize ) if ( iGetPos + iInSize > iMemSize )
{ {
/* remaining data size for second block */ // remaining data size for second block
const int iRemData = iGetPos + iInSize - iMemSize; const int iRemData = iGetPos + iInSize - iMemSize;
/* data must be read in two steps because of wrap around */ // data must be read in two steps because of wrap around
while ( iGetPos < iMemSize ) while ( iGetPos < iMemSize )
{ {
vecdData[iCurPos++] = vecdMemory[iGetPos++]; vecdData[iCurPos++] = vecdMemory[iGetPos++];
@ -184,7 +184,7 @@ bool CNetBuf::Get ( CVector<double>& vecdData )
} }
else else
{ {
/* data can be read in one step */ // data can be read in one step
const int iEnd = iGetPos + iInSize; const int iEnd = iGetPos + iInSize;
while ( iGetPos < iEnd ) while ( iGetPos < iEnd )
{ {
@ -192,7 +192,7 @@ bool CNetBuf::Get ( CVector<double>& vecdData )
} }
} }
/* set buffer state flag */ // set buffer state flag
if ( iPutPos == iGetPos ) if ( iPutPos == iGetPos )
{ {
eBufState = CNetBuf::BS_EMPTY; eBufState = CNetBuf::BS_EMPTY;
@ -224,13 +224,13 @@ bool CNetBuf::Get ( CVector<double>& vecdData )
int CNetBuf::GetAvailSpace() const int CNetBuf::GetAvailSpace() const
{ {
/* calculate available space in buffer */ // calculate available space in buffer
int iAvSpace = iGetPos - iPutPos; int iAvSpace = iGetPos - iPutPos;
/* check for special case and wrap around */ // check for special case and wrap around
if ( iAvSpace < 0 ) if ( iAvSpace < 0 )
{ {
iAvSpace += iMemSize; /* wrap around */ iAvSpace += iMemSize; // wrap around
} }
else else
{ {
@ -245,13 +245,13 @@ int CNetBuf::GetAvailSpace() const
int CNetBuf::GetAvailData() const int CNetBuf::GetAvailData() const
{ {
/* calculate available data in buffer */ // calculate available data in buffer
int iAvData = iPutPos - iGetPos; int iAvData = iPutPos - iGetPos;
/* check for special case and wrap around */ // check for special case and wrap around
if ( iAvData < 0 ) if ( iAvData < 0 )
{ {
iAvData += iMemSize; /* wrap around */ iAvData += iMemSize; // wrap around
} }
else else
{ {
@ -285,14 +285,14 @@ void CNetBuf::Clear ( const EClearType eClearType )
#endif #endif
/* different behaviour for get and put corrections */ // different behaviour for get and put corrections
if ( eClearType == CT_GET ) if ( eClearType == CT_GET )
{ {
/* clear buffer */ // clear buffer
vecdMemory.Reset ( 0.0 ); vecdMemory.Reset ( 0.0 );
/* correct buffer so that after the current get operation the pointer // correct buffer so that after the current get operation the pointer
are at maximum distance */ // are at maximum distance
iPutPos = 0; iPutPos = 0;
iGetPos = iMiddleOfBuffer; iGetPos = iMiddleOfBuffer;
@ -308,28 +308,28 @@ void CNetBuf::Clear ( const EClearType eClearType )
} }
else else
{ {
/* in case of "put" correction, do not delete old data but only shift // in case of "put" correction, do not delete old data but only shift
the pointers */ // the pointers
iPutPos = iMiddleOfBuffer; iPutPos = iMiddleOfBuffer;
/* adjust put pointer relative to current get pointer, take care of // adjust put pointer relative to current get pointer, take care of
wrap around */ // wrap around
iPutPos += iGetPos; iPutPos += iGetPos;
if ( iPutPos > iMemSize ) if ( iPutPos > iMemSize )
{ {
iPutPos -= iMemSize; iPutPos -= iMemSize;
} }
/* fade out old data right before new put pointer */ // fade out old data right before new put pointer
int iCurPos = iPutPos - iNumSamFading; int iCurPos = iPutPos - iNumSamFading;
int i = iNumSamFading; int i = iNumSamFading;
if ( iCurPos < 0 ) if ( iCurPos < 0 )
{ {
/* wrap around */ // wrap around
iCurPos += iMemSize; iCurPos += iMemSize;
/* data must be processed in two steps because of wrap around */ // data must be processed in two steps because of wrap around
while ( iCurPos < iMemSize ) while ( iCurPos < iMemSize )
{ {
vecdMemory[iCurPos++] *= ( (double) i / iNumSamFading ); vecdMemory[iCurPos++] *= ( (double) i / iNumSamFading );
@ -344,7 +344,7 @@ void CNetBuf::Clear ( const EClearType eClearType )
} }
else else
{ {
/* data can be processed in one step */ // data can be processed in one step
while ( iCurPos < iPutPos ) while ( iCurPos < iPutPos )
{ {
vecdMemory[iCurPos++] *= ( (double) i / iNumSamFading ); vecdMemory[iCurPos++] *= ( (double) i / iNumSamFading );
@ -352,7 +352,7 @@ void CNetBuf::Clear ( const EClearType eClearType )
} }
} }
/* check for special case */ // check for special case
if ( iPutPos == iGetPos ) if ( iPutPos == iGetPos )
{ {
eBufState = CNetBuf::BS_EMPTY; eBufState = CNetBuf::BS_EMPTY;
@ -366,13 +366,13 @@ void CNetBuf::Clear ( const EClearType eClearType )
void CNetBuf::FadeInAudioDataBlock ( CVector<double>& vecdData ) void CNetBuf::FadeInAudioDataBlock ( CVector<double>& vecdData )
{ {
/* apply linear fading */ // apply linear fading
for ( int i = 0; i < iNumSamFading; i++ ) for ( int i = 0; i < iNumSamFading; i++ )
{ {
vecdData[i] *= ( (double) i / iNumSamFading ); vecdData[i] *= ( (double) i / iNumSamFading );
} }
/* reset flag */ // reset flag
bFadeInNewPutData = false; bFadeInNewPutData = false;
} }
@ -380,13 +380,13 @@ void CNetBuf::FadeOutExtrapolateAudioDataBlock ( CVector<double>& vecdData,
const double dExPDiff, const double dExPDiff,
const double dExPLastV ) const double dExPLastV )
{ {
/* apply linear extrapolation and linear fading */ // apply linear extrapolation and linear fading
for ( int i = 0; i < iNumSamFadingExtra; i++ ) for ( int i = 0; i < iNumSamFadingExtra; i++ )
{ {
/* calculate extrapolated value */ // calculate extrapolated value
vecdData[i] = ( ( i + 1 ) * dExPDiff + dExPLastV ); vecdData[i] = ( ( i + 1 ) * dExPDiff + dExPLastV );
/* linear fading */ // linear fading
vecdData[i] *= ( (double) ( iNumSamFadingExtra - i ) / iNumSamFadingExtra ); vecdData[i] *= ( (double) ( iNumSamFadingExtra - i ) / iNumSamFadingExtra );
} }
} }
@ -396,10 +396,10 @@ void CNetBuf::FadeOutExtrapolateAudioDataBlock ( CVector<double>& vecdData,
/* conversion buffer implementation *******************************************/ /* conversion buffer implementation *******************************************/
void CConvBuf::Init ( const int iNewMemSize ) void CConvBuf::Init ( const int iNewMemSize )
{ {
/* set memory size */ // set memory size
iMemSize = iNewMemSize; iMemSize = iNewMemSize;
/* allocate and clear memory for actual data buffer */ // allocate and clear memory for actual data buffer
vecsMemory.Init ( iMemSize ); vecsMemory.Init ( iMemSize );
iPutPos = 0; iPutPos = 0;
@ -409,7 +409,7 @@ bool CConvBuf::Put ( const CVector<short>& vecsData )
{ {
const int iVecSize = vecsData.Size(); const int iVecSize = vecsData.Size();
/* copy new data in internal buffer */ // copy new data in internal buffer
int iCurPos = 0; int iCurPos = 0;
const int iEnd = iPutPos + iVecSize; const int iEnd = iPutPos + iVecSize;
while ( iPutPos < iEnd ) while ( iPutPos < iEnd )

View file

@ -92,7 +92,6 @@ public:
protected: protected:
CVector<short> vecsMemory; CVector<short> vecsMemory;
int iMemSize; int iMemSize;
int iBlockSize;
int iPutPos; int iPutPos;
}; };

View file

@ -42,7 +42,7 @@
// version and application name (always use this version) // version and application name (always use this version)
#undef VERSION #undef VERSION
#define VERSION "1.0cvs" #define VERSION "1.0.1cvs"
#define APP_NAME "llcon" #define APP_NAME "llcon"
// file name for logging file // file name for logging file

View file

@ -1,5 +1,5 @@
/******************************************************************************\ /******************************************************************************\
* Copyright (c) 2004-2006 * Copyright (c) 2004-2008
* *
* Author(s): * Author(s):
* Volker Fischer * Volker Fischer

View file

@ -28,40 +28,41 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
void CSocket::Init() void CSocket::Init()
{ {
/* allocate memory for network receive and send buffer in samples */ // allocate memory for network receive and send buffer in samples
vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF ); vecbyRecBuf.Init ( MAX_SIZE_BYTES_NETW_BUF );
/* initialize the listening socket */ // initialize the listening socket
bool bSuccess = SocketDevice.bind ( bool bSuccess = SocketDevice.bind (
QHostAddress ( (Q_UINT32) 0 ) /* INADDR_ANY */, LLCON_PORT_NUMBER ); QHostAddress ( (Q_UINT32) 0 ), // INADDR_ANY
LLCON_PORT_NUMBER );
if ( bIsClient ) if ( bIsClient )
{ {
/* if no success, try if server is on same machine (only for client) */ // if no success, try if server is on same machine (only for client)
if ( !bSuccess ) if ( !bSuccess )
{ {
/* if server and client is on same machine, decrease port number by // if server and client is on same machine, decrease port number by
one by definition */ // one by definition
bSuccess = SocketDevice.bind ( bSuccess = SocketDevice.bind (
QHostAddress( (Q_UINT32) 0 ) /* INADDR_ANY */, QHostAddress( (Q_UINT32) 0 ), // INADDR_ANY
LLCON_PORT_NUMBER - 1 ); LLCON_PORT_NUMBER - 1 );
} }
} }
if ( !bSuccess ) if ( !bSuccess )
{ {
/* show error message */ // show error message
QMessageBox::critical ( 0, "Network Error", "Cannot bind the socket.", QMessageBox::critical ( 0, "Network Error", "Cannot bind the socket.",
QMessageBox::Ok, QMessageBox::NoButton ); QMessageBox::Ok, QMessageBox::NoButton );
/* exit application */ // exit application
exit ( 1 ); exit ( 1 );
} }
QSocketNotifier* pSocketNotivRead = QSocketNotifier* pSocketNotivRead =
new QSocketNotifier ( SocketDevice.socket (), QSocketNotifier::Read ); new QSocketNotifier ( SocketDevice.socket (), QSocketNotifier::Read );
/* connect the "activated" signal */ // connect the "activated" signal
QObject::connect ( pSocketNotivRead, SIGNAL ( activated ( int ) ), QObject::connect ( pSocketNotivRead, SIGNAL ( activated ( int ) ),
this, SLOT ( OnDataReceived() ) ); this, SLOT ( OnDataReceived() ) );
} }
@ -73,7 +74,7 @@ void CSocket::SendPacket ( const CVector<unsigned char>& vecbySendBuf,
if ( iVecSizeOut != 0 ) if ( iVecSizeOut != 0 )
{ {
/* send packet through network */ // send packet through network
SocketDevice.writeBlock ( SocketDevice.writeBlock (
(const char*) &( (CVector<unsigned char>) vecbySendBuf )[0], (const char*) &( (CVector<unsigned char>) vecbySendBuf )[0],
iVecSizeOut, HostAddr.InetAddr, HostAddr.iPort ); iVecSizeOut, HostAddr.InetAddr, HostAddr.iPort );
@ -82,24 +83,24 @@ void CSocket::SendPacket ( const CVector<unsigned char>& vecbySendBuf,
void CSocket::OnDataReceived() void CSocket::OnDataReceived()
{ {
/* read block from network interface */ // read block from network interface
const int iNumBytesRead = SocketDevice.readBlock ( (char*) &vecbyRecBuf[0], const int iNumBytesRead = SocketDevice.readBlock ( (char*) &vecbyRecBuf[0],
MAX_SIZE_BYTES_NETW_BUF ); MAX_SIZE_BYTES_NETW_BUF );
/* check if an error occurred */ // check if an error occurred
if ( iNumBytesRead < 0 ) if ( iNumBytesRead < 0 )
{ {
return; return;
} }
/* get host address of client */ // get host address of client
CHostAddress RecHostAddr ( SocketDevice.peerAddress(), CHostAddress RecHostAddr ( SocketDevice.peerAddress(),
SocketDevice.peerPort() ); SocketDevice.peerPort() );
if ( bIsClient ) if ( bIsClient )
{ {
/* client */ // client
/* check if packet comes from the server we want to connect */ // check if packet comes from the server we want to connect
if ( ! ( pChannel->GetAddress() == RecHostAddr ) ) if ( ! ( pChannel->GetAddress() == RecHostAddr ) )
{ {
return; return;
@ -123,7 +124,7 @@ void CSocket::OnDataReceived()
} }
else else
{ {
/* server */ // server
if ( pChannelSet->PutData ( vecbyRecBuf, iNumBytesRead, RecHostAddr ) ) if ( pChannelSet->PutData ( vecbyRecBuf, iNumBytesRead, RecHostAddr ) )
{ {
// this was an audio packet, start server // this was an audio packet, start server