jamulus/src/channel.cpp

513 lines
14 KiB
C++
Raw Normal View History

2006-01-28 12:29:22 +01:00
/******************************************************************************\
* Copyright (c) 2004-2006
*
* Author(s):
* Volker Fischer
*
******************************************************************************
*
* This program 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, 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 Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\******************************************************************************/
#include "channel.h"
/******************************************************************************\
* CChannelSet *
2006-03-04 12:11:26 +01:00
\******************************************************************************/
CChannelSet::CChannelSet()
{
// make sure we have MAX_NUM_CHANNELS connections!!!
// send message
QObject::connect(&vecChannels[0],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh0(CVector<uint8_t>)));
QObject::connect(&vecChannels[1],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh1(CVector<uint8_t>)));
QObject::connect(&vecChannels[2],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh2(CVector<uint8_t>)));
QObject::connect(&vecChannels[3],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh3(CVector<uint8_t>)));
QObject::connect(&vecChannels[4],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh4(CVector<uint8_t>)));
QObject::connect(&vecChannels[5],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh5(CVector<uint8_t>)));
QObject::connect(&vecChannels[6],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh6(CVector<uint8_t>)));
QObject::connect(&vecChannels[7],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh7(CVector<uint8_t>)));
QObject::connect(&vecChannels[8],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh8(CVector<uint8_t>)));
QObject::connect(&vecChannels[9],SIGNAL(MessReadyForSending(CVector<uint8_t>)),this,SLOT(OnSendProtMessCh9(CVector<uint8_t>)));
2006-03-04 12:11:26 +01:00
// request jitter buffer size
QObject::connect(&vecChannels[0],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh0()));
QObject::connect(&vecChannels[1],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh1()));
QObject::connect(&vecChannels[2],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh2()));
QObject::connect(&vecChannels[3],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh3()));
QObject::connect(&vecChannels[4],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh4()));
QObject::connect(&vecChannels[5],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh5()));
QObject::connect(&vecChannels[6],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh6()));
QObject::connect(&vecChannels[7],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh7()));
QObject::connect(&vecChannels[8],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh8()));
QObject::connect(&vecChannels[9],SIGNAL(NewConnection()),this,SLOT(OnNewConnectionCh9()));
2006-03-04 12:12:47 +01:00
}
2006-03-04 12:11:26 +01:00
2006-01-28 12:29:22 +01:00
int CChannelSet::GetFreeChan()
{
/* look for a free channel */
2006-02-17 22:08:05 +01:00
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
2006-01-28 12:29:22 +01:00
{
2006-02-17 22:08:05 +01:00
if ( !vecChannels[i].IsConnected () )
{
return i;
}
2006-01-28 12:29:22 +01:00
}
/* no free channel found, return invalid ID */
return INVALID_CHANNEL_ID;
}
2006-02-17 22:08:05 +01:00
int CChannelSet::CheckAddr ( const CHostAddress& Addr )
2006-01-28 12:29:22 +01:00
{
CHostAddress InetAddr;
/* Check for all possible channels if IP is already in use */
2006-02-17 22:08:05 +01:00
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
2006-01-28 12:29:22 +01:00
{
2006-02-17 22:08:05 +01:00
if ( vecChannels[i].GetAddress ( InetAddr ) )
2006-01-28 12:29:22 +01:00
{
/* IP found, return channel number */
2006-02-17 22:08:05 +01:00
if ( InetAddr == Addr )
{
return i;
}
2006-01-28 12:29:22 +01:00
}
}
/* IP not found, return invalid ID */
return INVALID_CHANNEL_ID;
}
2006-02-17 22:08:05 +01:00
bool CChannelSet::PutData ( const CVector<unsigned char>& vecbyRecBuf,
const int iNumBytesRead,
const CHostAddress& HostAdr )
2006-01-28 12:29:22 +01:00
{
2006-03-07 23:06:45 +01:00
bool bRet = false;
2006-03-06 18:04:07 +01:00
Mutex.lock ();
2006-01-28 12:29:22 +01:00
{
2006-03-07 23:06:45 +01:00
bool bChanOK = true;
2006-03-06 18:04:07 +01:00
/* get channel ID --------------------------------------------------- */
/* check address */
int iCurChanID = CheckAddr ( HostAdr );
2006-01-28 12:29:22 +01:00
2006-03-06 18:04:07 +01:00
if ( iCurChanID == INVALID_CHANNEL_ID )
{
2006-03-06 18:04:07 +01:00
/* a new client is calling, look for free channel */
iCurChanID = GetFreeChan ();
if ( iCurChanID != INVALID_CHANNEL_ID )
{
vecChannels[iCurChanID].SetAddress ( HostAdr );
}
else
{
/* no free channel available */
2006-03-07 23:06:45 +01:00
bChanOK = false;
2006-03-06 18:04:07 +01:00
}
}
2006-01-28 12:29:22 +01:00
2006-03-06 18:04:07 +01:00
/* put received data in jitter buffer ------------------------------- */
if ( bChanOK )
{
2006-03-06 18:04:07 +01:00
/* put packet in socket buffer */
switch ( vecChannels[iCurChanID].PutData ( vecbyRecBuf, iNumBytesRead ) )
{
case PS_AUDIO_OK:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_GREEN, iCurChanID );
2006-03-07 23:06:45 +01:00
bRet = true; // in case we have an audio packet, return true
2006-03-06 18:04:07 +01:00
break;
case PS_AUDIO_ERR:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_RED, iCurChanID );
2006-03-07 23:06:45 +01:00
bRet = true; // in case we have an audio packet, return true
2006-03-06 18:04:07 +01:00
break;
case PS_PROT_ERR:
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_YELLOW, iCurChanID );
break;
}
}
2006-01-28 12:29:22 +01:00
}
Mutex.unlock ();
2006-01-28 12:29:22 +01:00
2006-03-07 23:06:45 +01:00
return bRet;
2006-01-28 12:29:22 +01:00
}
2006-02-17 22:08:05 +01:00
void CChannelSet::GetBlockAllConC ( CVector<int>& vecChanID,
CVector<CVector<double> >& vecvecdData )
2006-01-28 12:29:22 +01:00
{
/* init temporal data vector and clear input buffers */
2006-02-17 22:08:05 +01:00
CVector<double> vecdData ( MIN_BLOCK_SIZE_SAMPLES );
2006-01-28 12:29:22 +01:00
2006-02-17 22:08:05 +01:00
vecChanID.Init ( 0 );
vecvecdData.Init ( 0 );
2006-01-28 12:29:22 +01:00
/* make put and get calls thread safe. Do not forget to unlock mutex
afterwards! */
2006-02-17 22:08:05 +01:00
Mutex.lock ();
2006-01-28 12:29:22 +01:00
{
2006-03-06 18:04:07 +01:00
/* Check all possible channels */
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
2006-01-28 12:29:22 +01:00
{
2006-03-06 18:04:07 +01:00
/* read out all input buffers to decrease timeout counter on
disconnected channels */
const bool bGetOK = vecChannels[i].GetData ( vecdData );
2006-01-28 12:29:22 +01:00
2006-03-06 18:04:07 +01:00
if ( vecChannels[i].IsConnected () )
2006-02-17 22:08:05 +01:00
{
2006-03-06 18:04:07 +01:00
/* add ID and data */
vecChanID.Add ( i );
const int iOldSize = vecvecdData.Size ();
vecvecdData.Enlarge ( 1 );
vecvecdData[iOldSize].Init ( vecdData.Size () );
vecvecdData[iOldSize] = vecdData;
/* send message for get status (for GUI) */
if ( bGetOK )
{
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_GREEN, i );
}
else
{
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_RED, i );
}
2006-02-17 22:08:05 +01:00
}
2006-01-28 12:29:22 +01:00
}
}
2006-02-17 22:08:05 +01:00
Mutex.unlock (); /* release mutex */
2006-01-28 12:29:22 +01:00
}
2006-02-17 22:08:05 +01:00
void CChannelSet::GetConCliParam ( CVector<CHostAddress>& vecHostAddresses,
CVector<int>& veciJitBufSize )
2006-01-28 12:29:22 +01:00
{
CHostAddress InetAddr;
/* init return values */
2006-02-17 22:08:05 +01:00
vecHostAddresses.Init ( MAX_NUM_CHANNELS );
veciJitBufSize.Init ( MAX_NUM_CHANNELS );
2006-01-28 12:29:22 +01:00
/* Check all possible channels */
2006-02-17 22:08:05 +01:00
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
2006-01-28 12:29:22 +01:00
{
2006-02-17 22:08:05 +01:00
if ( vecChannels[i].GetAddress ( InetAddr ) )
2006-01-28 12:29:22 +01:00
{
/* add new address and sample rate offset to vectors */
vecHostAddresses[i] = InetAddr;
veciJitBufSize[i] = vecChannels[i].GetSockBufSize ();
2006-01-28 12:29:22 +01:00
}
}
}
/******************************************************************************\
* CChannel *
\******************************************************************************/
2006-02-26 11:50:47 +01:00
CChannel::CChannel ()
2006-01-28 12:29:22 +01:00
{
/* init time stamp index counter */
byTimeStampIdxCnt = 0;
/* init the socket buffer */
2006-02-17 22:08:05 +01:00
SetSockBufSize ( MIN_BLOCK_SIZE_SAMPLES, DEF_NET_BUF_SIZE_NUM_BL );
2006-01-28 12:29:22 +01:00
/* init conversion buffer */
ConvBuf.Init ( BLOCK_SIZE_SAMPLES );
/* init audio compression unit */
iAudComprSize = AudioCompression.Init ( BLOCK_SIZE_SAMPLES,
CAudioCompression::CT_IMAADPCM );
/* init time-out for the buffer with zero -> no connection */
iConTimeOut = 0;
2006-02-26 11:50:47 +01:00
/* connections ---------------------------------------------------------- */
2006-03-01 20:46:44 +01:00
QObject::connect ( &Protocol,
SIGNAL ( MessReadyForSending ( CVector<uint8_t> ) ),
2006-03-04 11:24:40 +01:00
this, SLOT ( OnSendProtMessage ( CVector<uint8_t> ) ) );
QObject::connect ( &Protocol, SIGNAL ( ChangeJittBufSize ( int ) ),
this, SLOT ( OnJittBufSizeChange ( int ) ) );
QObject::connect ( &Protocol, SIGNAL ( ReqJittBufSize() ),
SIGNAL ( ReqJittBufSize() ) );
2006-02-26 11:50:47 +01:00
}
2006-03-04 11:24:40 +01:00
void CChannel::OnSendProtMessage ( CVector<uint8_t> vecMessage )
{
// only send messages if we are connected, otherwise delete complete queue
2006-03-04 12:11:26 +01:00
if ( IsConnected () )
{
2006-03-04 11:24:40 +01:00
// emit message to actually send the data
emit MessReadyForSending ( vecMessage );
2006-03-04 12:11:26 +01:00
}
else
{
// delete send message queue
Protocol.DeleteSendMessQueue();
}
2006-03-04 11:24:40 +01:00
}
2006-01-28 12:29:22 +01:00
void CChannel::SetSockBufSize ( const int iNewBlockSize, const int iNumBlocks )
{
2006-02-27 21:27:47 +01:00
/* this opperation must be done with mutex */
2006-01-28 12:29:22 +01:00
Mutex.lock ();
2006-03-06 18:04:07 +01:00
{
SockBuf.Init ( iNewBlockSize, iNumBlocks );
}
2006-02-26 11:50:47 +01:00
Mutex.unlock ();
2006-01-28 12:29:22 +01:00
}
void CChannel::OnJittBufSizeChange ( int iNewJitBufSize )
{
2006-03-01 20:46:44 +01:00
// TEST
qDebug ( "new jitter buffer size: %d", iNewJitBufSize );
SetSockBufSize ( MIN_BLOCK_SIZE_SAMPLES, iNewJitBufSize );
}
2006-01-28 12:29:22 +01:00
bool CChannel::GetAddress(CHostAddress& RetAddr)
{
if (IsConnected())
{
RetAddr = InetAddr;
return true;
}
else
{
RetAddr = CHostAddress();
return false;
}
}
2006-03-06 18:04:07 +01:00
EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData,
int iNumBytes )
2006-01-28 12:29:22 +01:00
{
2006-03-06 18:04:07 +01:00
EPutDataStat eRet = PS_GEN_ERROR;
2006-01-28 12:29:22 +01:00
/* only process if packet has correct size */
if ( iNumBytes == iAudComprSize )
2006-01-28 12:29:22 +01:00
{
/* decompress audio */
CVector<short> vecsDecomprAudio ( BLOCK_SIZE_SAMPLES );
vecsDecomprAudio = AudioCompression.Decode ( vecbyData );
2006-01-28 12:29:22 +01:00
/* do resampling to compensate for sample rate offsets in the
different sound cards of the clients */
/*
for (int i = 0; i < BLOCK_SIZE_SAMPLES; i++)
vecdResInData[i] = (double) vecsData[i];
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];
2006-02-27 21:27:47 +01:00
Mutex.lock (); /* put mutex lock */
2006-03-06 18:04:07 +01:00
{
if ( SockBuf.Put ( vecdResOutData ) )
{
eRet = PS_AUDIO_OK;
}
else
{
eRet = PS_AUDIO_ERR;
}
}
2006-02-27 21:27:47 +01:00
Mutex.unlock (); /* put mutex unlock */
2006-01-28 12:29:22 +01:00
// if channel was not connected, emit signal to inform that new
// connection was established
2006-03-06 18:04:07 +01:00
if ( iConTimeOut == 0 )
{
emit NewConnection();
}
// reset time-out counter
2006-01-28 12:29:22 +01:00
iConTimeOut = CON_TIME_OUT_CNT_MAX;
}
else
{
// this seems not to be an audio block, parse the message
2006-03-06 18:04:07 +01:00
if ( Protocol.ParseMessage ( vecbyData, iNumBytes ) )
{
eRet = PS_PROT_OK;
}
else
{
eRet = PS_PROT_ERR;
}
2006-01-28 12:29:22 +01:00
}
2006-03-06 18:04:07 +01:00
return eRet;
2006-01-28 12:29:22 +01:00
}
2006-02-26 11:50:47 +01:00
bool CChannel::GetData ( CVector<double>& vecdData )
2006-03-06 18:04:07 +01:00
{
bool bGetOK = false;
2006-01-28 12:29:22 +01:00
2006-03-06 18:04:07 +01:00
Mutex.lock (); /* get mutex lock */
2006-01-28 12:29:22 +01:00
{
2006-03-06 18:04:07 +01:00
bGetOK = SockBuf.Get ( vecdData );
if ( !bGetOK )
2006-01-28 12:29:22 +01:00
{
2006-03-06 18:04:07 +01:00
/* decrease time-out counter */
if ( iConTimeOut > 0 )
{
iConTimeOut--;
}
2006-01-28 12:29:22 +01:00
}
}
2006-02-26 11:50:47 +01:00
Mutex.unlock (); /* get mutex unlock */
2006-01-28 12:29:22 +01:00
return bGetOK;
}
CVector<unsigned char> CChannel::PrepSendPacket(const CVector<short>& vecsNPacket)
{
/* if the block is not ready we have to initialize with zero length to
tell the following network send routine that nothing should be sent */
CVector<unsigned char> vecbySendBuf ( 0 );
/* use conversion buffer to convert sound card block size in network
block size */
if ( ConvBuf.Put ( vecsNPacket ) )
{
/* a packet is ready, compress audio */
vecbySendBuf.Init ( iAudComprSize );
vecbySendBuf = AudioCompression.Encode ( ConvBuf.Get () );
}
return vecbySendBuf;
}
2006-03-04 11:24:40 +01:00
2006-01-28 12:29:22 +01:00
/******************************************************************************\
* CSampleOffsetEst *
\******************************************************************************/
void CSampleOffsetEst::Init()
{
/* init sample rate estimation */
dSamRateEst = SAMPLE_RATE;
/* init vectors storing the data */
veciTimeElapsed.Init(VEC_LEN_SAM_OFFS_EST);
veciTiStIdx.Init(VEC_LEN_SAM_OFFS_EST);
/* start reference time (the counter wraps to zero 24 hours after the last
call to start() or restart, but this should not concern us since this
software will most probably not be used that long) */
RefTime.start();
/* init accumulated time stamp variable */
iAccTiStVal = 0;
/* init count (do not ship any result in init phase) */
iInitCnt = VEC_LEN_SAM_OFFS_EST + 1;
}
void CSampleOffsetEst::AddTimeStampIdx(const int iTimeStampIdx)
{
int i;
const int iLastIdx = VEC_LEN_SAM_OFFS_EST - 1;
/* take care of wrap of the time stamp index (byte wrap) */
if (iTimeStampIdx < veciTiStIdx[iLastIdx] - iAccTiStVal)
iAccTiStVal += _MAXBYTE + 1;
/* add new data pair to the FIFO */
for (i = 1; i < VEC_LEN_SAM_OFFS_EST; i++)
{
/* move old data */
veciTimeElapsed[i - 1] = veciTimeElapsed[i];
veciTiStIdx[i - 1] = veciTiStIdx[i];
}
/* add new data */
veciTimeElapsed[iLastIdx] = RefTime.elapsed();
veciTiStIdx[iLastIdx] = iAccTiStVal + iTimeStampIdx;
/*
static FILE* pFile = fopen("v.dat", "w");
for (i = 0; i < VEC_LEN_SAM_OFFS_EST; i++)
fprintf(pFile, "%d\n", veciTimeElapsed[i]);
fflush(pFile);
*/
/* calculate linear regression for sample rate estimation */
/* first, calculate averages */
double dTimeAv = 0;
double dTiStAv = 0;
for (i = 0; i < VEC_LEN_SAM_OFFS_EST; i++)
{
dTimeAv += veciTimeElapsed[i];
dTiStAv += veciTiStIdx[i];
}
dTimeAv /= VEC_LEN_SAM_OFFS_EST;
dTiStAv /= VEC_LEN_SAM_OFFS_EST;
/* calculate gradient */
double dNom = 0;
double dDenom = 0;
for (i = 0; i < VEC_LEN_SAM_OFFS_EST; i++)
{
const double dCurTimeNoAv = veciTimeElapsed[i] - dTimeAv;
dNom += dCurTimeNoAv * (veciTiStIdx[i] - dTiStAv);
dDenom += dCurTimeNoAv * dCurTimeNoAv;
}
/* final sample rate offset estimation calculation */
if (iInitCnt > 0)
iInitCnt--;
else
{
dSamRateEst = dNom / dDenom * NUM_BL_TIME_STAMPS * BLOCK_SIZE_SAMPLES * 1000;
/*
static FILE* pFile = fopen("v.dat", "w");
for (i = 0; i < VEC_LEN_SAM_OFFS_EST; i++)
fprintf(pFile, "%d %d\n", veciTimeElapsed[i], veciTiStIdx[i]);
fflush(pFile);
*/
}
/*
static FILE* pFile = fopen("v.dat", "w");
fprintf(pFile, "%e\n", dSamRateEst);
fflush(pFile);
*/
}