2006-12-18 15:39:33 +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()
|
|
|
|
{
|
2006-12-09 11:04:27 +01:00
|
|
|
// enable all channels
|
|
|
|
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
vecChannels[i].SetEnable ( true );
|
|
|
|
}
|
|
|
|
|
2006-12-09 19:37:40 +01:00
|
|
|
// CODE TAG: MAX_NUM_CHANNELS_TAG
|
2006-11-25 15:46:57 +01:00
|
|
|
// 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>)));
|
2006-12-06 21:22:41 +01:00
|
|
|
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-11-25 15:46:57 +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()));
|
2006-12-06 21:22:41 +01:00
|
|
|
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-12-08 19:19:16 +01:00
|
|
|
|
|
|
|
// request connected clients list
|
2006-12-09 11:04:27 +01:00
|
|
|
QObject::connect(&vecChannels[0],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh0()));
|
|
|
|
QObject::connect(&vecChannels[1],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh1()));
|
|
|
|
QObject::connect(&vecChannels[2],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh2()));
|
|
|
|
QObject::connect(&vecChannels[3],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh3()));
|
|
|
|
QObject::connect(&vecChannels[4],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh4()));
|
|
|
|
QObject::connect(&vecChannels[5],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh5()));
|
|
|
|
QObject::connect(&vecChannels[6],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh6()));
|
|
|
|
QObject::connect(&vecChannels[7],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh7()));
|
|
|
|
QObject::connect(&vecChannels[8],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh8()));
|
|
|
|
QObject::connect(&vecChannels[9],SIGNAL(ReqConnClientsList()),this,SLOT(OnReqConnClientsListCh9()));
|
2006-12-10 12:06:14 +01:00
|
|
|
|
|
|
|
// channel name has changed
|
|
|
|
QObject::connect(&vecChannels[0],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh0()));
|
|
|
|
QObject::connect(&vecChannels[1],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh1()));
|
|
|
|
QObject::connect(&vecChannels[2],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh2()));
|
|
|
|
QObject::connect(&vecChannels[3],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh3()));
|
|
|
|
QObject::connect(&vecChannels[4],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh4()));
|
|
|
|
QObject::connect(&vecChannels[5],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh5()));
|
|
|
|
QObject::connect(&vecChannels[6],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh6()));
|
|
|
|
QObject::connect(&vecChannels[7],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh7()));
|
|
|
|
QObject::connect(&vecChannels[8],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh8()));
|
|
|
|
QObject::connect(&vecChannels[9],SIGNAL(NameHasChanged()),this,SLOT(OnNameHasChangedCh9()));
|
2006-03-04 12:12:47 +01:00
|
|
|
}
|
2006-11-04 14:09:42 +01:00
|
|
|
|
2006-12-09 11:04:27 +01:00
|
|
|
CVector<CChannelShortInfo> CChannelSet::CreateChannelList()
|
2006-11-04 14:09:42 +01:00
|
|
|
{
|
2006-11-27 23:35:22 +01:00
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( 0 );
|
2006-11-25 15:46:57 +01:00
|
|
|
|
|
|
|
// look for free channels
|
2006-12-09 11:04:27 +01:00
|
|
|
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
|
|
|
if ( vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
// append channel ID, IP address and channel name to storing vectors
|
2006-11-27 23:35:22 +01:00
|
|
|
vecChanInfo.Add ( CChannelShortInfo (
|
|
|
|
i, // ID
|
2008-01-14 23:14:17 +01:00
|
|
|
vecChannels[i].GetAddress().InetAddr.toIPv4Address(), // IP address
|
2006-11-27 23:35:22 +01:00
|
|
|
vecChannels[i].GetName() /* name */ ) );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-09 11:04:27 +01:00
|
|
|
return vecChanInfo;
|
|
|
|
}
|
|
|
|
|
2006-12-09 11:40:18 +01:00
|
|
|
void CChannelSet::CreateAndSendChanListForAllConChannels()
|
|
|
|
{
|
|
|
|
// create channel list
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( CChannelSet::CreateChannelList() );
|
|
|
|
|
|
|
|
// now send connected channels list to all connected clients
|
|
|
|
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
// send message
|
|
|
|
vecChannels[i].CreateConClientListMes ( vecChanInfo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-09 11:04:27 +01:00
|
|
|
void CChannelSet::CreateAndSendChanListForAllExceptThisChan ( const int iCurChanID )
|
|
|
|
{
|
|
|
|
// create channel list
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( CChannelSet::CreateChannelList() );
|
|
|
|
|
|
|
|
// now send connected channels list to all connected clients except for
|
|
|
|
// the channel with the ID "iCurChanID"
|
|
|
|
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
2006-12-09 11:04:27 +01:00
|
|
|
if ( ( vecChannels[i].IsConnected() ) && ( i != iCurChanID ) )
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
|
|
|
// send message
|
2006-11-26 22:25:56 +01:00
|
|
|
vecChannels[i].CreateConClientListMes ( vecChanInfo );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
2006-11-04 14:09:42 +01:00
|
|
|
}
|
2006-12-09 11:04:27 +01:00
|
|
|
|
|
|
|
void CChannelSet::CreateAndSendChanListForThisChan ( const int iCurChanID )
|
|
|
|
{
|
|
|
|
// create channel list
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( CChannelSet::CreateChannelList() );
|
|
|
|
|
|
|
|
// now send connected channels list to the channel with the ID "iCurChanID"
|
|
|
|
vecChannels[iCurChanID].CreateConClientListMes ( vecChanInfo );
|
|
|
|
}
|
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
int CChannelSet::GetFreeChan()
|
|
|
|
{
|
|
|
|
// look for a free channel
|
|
|
|
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( !vecChannels[i].IsConnected() )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
return i;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no free channel found, return invalid ID */
|
|
|
|
return INVALID_CHANNEL_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CChannelSet::CheckAddr ( const CHostAddress& Addr )
|
|
|
|
{
|
|
|
|
CHostAddress InetAddr;
|
|
|
|
|
|
|
|
/* Check for all possible channels if IP is already in use */
|
|
|
|
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].GetAddress ( InetAddr ) )
|
|
|
|
{
|
|
|
|
/* IP found, return channel number */
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( InetAddr == Addr )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
return i;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IP not found, return invalid ID */
|
|
|
|
return INVALID_CHANNEL_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CChannelSet::PutData ( const CVector<unsigned char>& vecbyRecBuf,
|
2006-12-06 22:10:24 +01:00
|
|
|
const int iNumBytesRead,
|
2006-12-18 15:39:33 +01:00
|
|
|
const CHostAddress& HostAdr )
|
|
|
|
{
|
2006-11-27 23:35:22 +01:00
|
|
|
bool bRet = false;
|
2006-11-25 15:46:57 +01:00
|
|
|
bool bCreateChanList = false;
|
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
Mutex.lock();
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
bool bChanOK = true;
|
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
// get channel ID ------------------------------------------------------
|
|
|
|
// check address
|
|
|
|
int iCurChanID = CheckAddr ( HostAdr );
|
|
|
|
|
|
|
|
if ( iCurChanID == INVALID_CHANNEL_ID )
|
|
|
|
{
|
|
|
|
// a new client is calling, look for free channel
|
|
|
|
iCurChanID = GetFreeChan();
|
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( iCurChanID != INVALID_CHANNEL_ID )
|
2006-12-06 21:22:41 +01:00
|
|
|
{
|
|
|
|
// initialize current channel by storing the calling host
|
2006-12-18 15:39:33 +01:00
|
|
|
// address
|
2006-11-25 15:46:57 +01:00
|
|
|
vecChannels[iCurChanID].SetAddress ( HostAdr );
|
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
// reset the channel gains of current channel, at the same
|
|
|
|
// time reset gains of this channel ID for all other channels
|
|
|
|
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
vecChannels[iCurChanID].SetGain ( i, (double) 1.0 );
|
|
|
|
|
|
|
|
// other channels (we do not distinguish the case if
|
|
|
|
// i == iCurChanID for simplicity)
|
|
|
|
vecChannels[i].SetGain ( iCurChanID, (double) 1.0 );
|
|
|
|
}
|
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// a new client connected to the server, set flag to create and
|
|
|
|
// send all clients the updated channel list, we cannot create
|
|
|
|
// the message here since the received data has to be put to the
|
|
|
|
// channel first so that this channel is marked as connected
|
|
|
|
bCreateChanList = true;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-11-25 15:46:57 +01:00
|
|
|
else
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-06 22:10:24 +01:00
|
|
|
// no free channel available
|
2006-11-25 15:46:57 +01:00
|
|
|
bChanOK = false;
|
2006-11-27 23:35:22 +01:00
|
|
|
|
|
|
|
// create and send "server full" message
|
2006-12-06 22:10:24 +01:00
|
|
|
|
|
|
|
// TODO problem: if channel is not officially connected, we cannot send messages
|
2006-11-27 23:35:22 +01:00
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// put received data in jitter buffer ----------------------------------
|
|
|
|
if ( bChanOK )
|
|
|
|
{
|
|
|
|
// put packet in socket buffer
|
2006-11-25 15:46:57 +01:00
|
|
|
switch ( vecChannels[iCurChanID].PutData ( vecbyRecBuf, iNumBytesRead ) )
|
|
|
|
{
|
|
|
|
case PS_AUDIO_OK:
|
|
|
|
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_GREEN, iCurChanID );
|
|
|
|
bRet = true; // in case we have an audio packet, return true
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_AUDIO_ERR:
|
|
|
|
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_RED, iCurChanID );
|
|
|
|
bRet = true; // in case we have an audio packet, return true
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PS_PROT_ERR:
|
|
|
|
PostWinMessage ( MS_JIT_BUF_PUT, MUL_COL_LED_YELLOW, iCurChanID );
|
|
|
|
break;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// after data is put in channel buffer, create channel list message if
|
|
|
|
// requested
|
|
|
|
if ( bCreateChanList )
|
|
|
|
{
|
2006-12-09 11:04:27 +01:00
|
|
|
// connected clients list is only send for new connected clients after
|
|
|
|
// request, only the already connected clients get the list
|
|
|
|
// automatically, because they don't know when new clients connect
|
|
|
|
CreateAndSendChanListForAllExceptThisChan ( iCurChanID );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
Mutex.unlock();
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CChannelSet::GetBlockAllConC ( CVector<int>& vecChanID,
|
2006-11-25 15:46:57 +01:00
|
|
|
CVector<CVector<double> >& vecvecdData,
|
2006-12-18 15:39:33 +01:00
|
|
|
CVector<CVector<double> >& vecvecdGains )
|
2006-12-06 21:22:41 +01:00
|
|
|
{
|
2006-12-09 11:40:18 +01:00
|
|
|
int i, j;
|
|
|
|
bool bCreateChanList = false;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
// init temporal data vector and clear input buffers
|
|
|
|
CVector<double> vecdData ( MIN_BLOCK_SIZE_SAMPLES );
|
|
|
|
|
|
|
|
vecChanID.Init ( 0 );
|
2006-12-06 21:22:41 +01:00
|
|
|
vecvecdData.Init ( 0 );
|
2006-12-18 15:39:33 +01:00
|
|
|
vecvecdGains.Init ( 0 );
|
|
|
|
|
|
|
|
// make put and get calls thread safe. Do not forget to unlock mutex
|
|
|
|
// afterwards!
|
|
|
|
Mutex.lock();
|
|
|
|
{
|
|
|
|
// check all possible channels
|
|
|
|
for ( i = 0; i < MAX_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
// read out all input buffers to decrease timeout counter on
|
|
|
|
// disconnected channels
|
|
|
|
const EGetDataStat eGetStat = vecChannels[i].GetData ( vecdData );
|
2006-12-09 11:40:18 +01:00
|
|
|
|
|
|
|
// if channel was just disconnected, set flag that connected
|
|
|
|
// client list is sent to all other clients
|
|
|
|
if ( eGetStat == GS_CHAN_NOW_DISCONNECTED )
|
|
|
|
{
|
|
|
|
bCreateChanList = true;
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
if ( vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
// add ID and data
|
2006-11-25 15:46:57 +01:00
|
|
|
vecChanID.Add ( i );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
const int iOldSize = vecvecdData.Size();
|
|
|
|
vecvecdData.Enlarge ( 1 );
|
|
|
|
vecvecdData[iOldSize].Init ( vecdData.Size() );
|
|
|
|
vecvecdData[iOldSize] = vecdData;
|
|
|
|
|
|
|
|
// send message for get status (for GUI)
|
2006-12-09 11:40:18 +01:00
|
|
|
if ( eGetStat == GS_BUFFER_OK )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_GREEN, i );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-11-25 15:46:57 +01:00
|
|
|
else
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_RED, i );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
2006-12-06 21:22:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// now that we know the IDs of the connected clients, get gains
|
|
|
|
const int iNumCurConnChan = vecChanID.Size();
|
|
|
|
vecvecdGains.Init ( iNumCurConnChan );
|
|
|
|
|
|
|
|
for ( i = 0; i < iNumCurConnChan; i++ )
|
|
|
|
{
|
|
|
|
vecvecdGains[i].Init ( iNumCurConnChan );
|
|
|
|
|
|
|
|
for ( j = 0; j < iNumCurConnChan; j++ )
|
|
|
|
{
|
|
|
|
// The second index of "vecvecdGains" does not represent
|
|
|
|
// the channel ID! Therefore we have to use "vecChanID" to
|
|
|
|
// query the IDs of the currently connected channels
|
2006-12-10 10:44:00 +01:00
|
|
|
vecvecdGains[i][j] =
|
|
|
|
vecChannels[ vecChanID[i] ].GetGain( vecChanID[j] );
|
2006-12-06 21:22:41 +01:00
|
|
|
}
|
2006-12-09 11:40:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// create channel list message if requested
|
|
|
|
if ( bCreateChanList )
|
|
|
|
{
|
|
|
|
// update channel list for all currently connected clients
|
|
|
|
CreateAndSendChanListForAllConChannels();
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
Mutex.unlock(); // release mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
void CChannelSet::GetConCliParam ( CVector<CHostAddress>& vecHostAddresses,
|
2006-12-29 11:52:25 +01:00
|
|
|
CVector<std::string>& vecsName,
|
2006-12-06 21:22:41 +01:00
|
|
|
CVector<int>& veciJitBufSize,
|
|
|
|
CVector<int>& veciNetwOutBlSiFact,
|
|
|
|
CVector<int>& veciNetwInBlSiFact )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
CHostAddress InetAddr;
|
|
|
|
|
|
|
|
// init return values
|
|
|
|
vecHostAddresses.Init ( MAX_NUM_CHANNELS );
|
2006-12-29 11:52:25 +01:00
|
|
|
vecsName.Init ( MAX_NUM_CHANNELS );
|
2006-12-06 21:22:41 +01:00
|
|
|
veciJitBufSize.Init ( MAX_NUM_CHANNELS );
|
2006-11-25 15:46:57 +01:00
|
|
|
veciNetwOutBlSiFact.Init ( MAX_NUM_CHANNELS );
|
2006-12-18 15:39:33 +01:00
|
|
|
veciNetwInBlSiFact.Init ( MAX_NUM_CHANNELS );
|
|
|
|
|
|
|
|
// check all possible channels
|
|
|
|
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].GetAddress ( InetAddr ) )
|
|
|
|
{
|
|
|
|
// get requested data
|
|
|
|
vecHostAddresses[i] = InetAddr;
|
2006-12-29 11:52:25 +01:00
|
|
|
vecsName[i] = vecChannels[i].GetName();
|
|
|
|
veciJitBufSize[i] = vecChannels[i].GetSockBufSize();
|
|
|
|
veciNetwOutBlSiFact[i] = vecChannels[i].GetNetwBufSizeFactOut();
|
|
|
|
veciNetwInBlSiFact[i] = vecChannels[i].GetNetwBufSizeFactIn();
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************\
|
|
|
|
* CChannel *
|
|
|
|
\******************************************************************************/
|
2006-11-26 22:25:56 +01:00
|
|
|
CChannel::CChannel() : sName ( "" ),
|
2006-12-18 15:39:33 +01:00
|
|
|
vecdGains ( MAX_NUM_CHANNELS, (double) 1.0 )
|
2006-03-13 20:32:23 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// query all possible network in buffer sizes for determining if an
|
|
|
|
// audio packet was received
|
|
|
|
for ( int i = 0; i < MAX_NET_BLOCK_SIZE_FACTOR; i++ )
|
|
|
|
{
|
|
|
|
// network block size factor must start from 1 -> ( i + 1 )
|
|
|
|
vecNetwInBufSizes[i] = AudioCompressionIn.Init (
|
|
|
|
( i + 1 ) * MIN_BLOCK_SIZE_SAMPLES,
|
|
|
|
CAudioCompression::CT_IMAADPCM );
|
|
|
|
}
|
2006-03-13 20:32:23 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
iCurNetwInBlSiFact = DEF_NET_BLOCK_SIZE_FACTOR;
|
2006-03-13 20:32:23 +01:00
|
|
|
|
2006-12-06 22:10:24 +01:00
|
|
|
// init the socket buffer
|
2006-11-25 15:46:57 +01:00
|
|
|
SetSockBufSize ( DEF_NET_BUF_SIZE_NUM_BL );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// set initial input and output block size factors
|
|
|
|
SetNetwBufSizeFactOut ( iCurNetwInBlSiFact );
|
|
|
|
SetNetwInBlSiFact ( iCurNetwInBlSiFact );
|
2006-03-13 20:32:23 +01:00
|
|
|
|
2006-12-06 22:10:24 +01:00
|
|
|
// init time-out for the buffer with zero -> no connection
|
2006-11-25 15:46:57 +01:00
|
|
|
iConTimeOut = 0;
|
2006-02-26 11:50:47 +01:00
|
|
|
|
|
|
|
|
2006-12-06 22:10:24 +01:00
|
|
|
// connections -------------------------------------------------------------
|
2006-11-25 15:46:57 +01:00
|
|
|
QObject::connect ( &Protocol,
|
|
|
|
SIGNAL ( MessReadyForSending ( CVector<uint8_t> ) ),
|
|
|
|
this, SLOT ( OnSendProtMessage ( CVector<uint8_t> ) ) );
|
2006-02-27 20:45:27 +01:00
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
QObject::connect ( &Protocol,
|
|
|
|
SIGNAL ( ChangeJittBufSize ( int ) ),
|
2006-11-25 15:46:57 +01:00
|
|
|
this, SLOT ( OnJittBufSizeChange ( int ) ) );
|
2006-03-07 22:26:40 +01:00
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
QObject::connect ( &Protocol,
|
|
|
|
SIGNAL ( ReqJittBufSize() ),
|
2006-11-25 15:46:57 +01:00
|
|
|
SIGNAL ( ReqJittBufSize() ) );
|
2006-03-12 12:50:35 +01:00
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
QObject::connect ( &Protocol,
|
2006-12-08 19:19:16 +01:00
|
|
|
SIGNAL ( ReqConnClientsList() ),
|
|
|
|
SIGNAL ( ReqConnClientsList() ) );
|
|
|
|
|
|
|
|
QObject::connect ( &Protocol,
|
2006-12-06 21:22:41 +01:00
|
|
|
SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ),
|
2006-11-26 22:25:56 +01:00
|
|
|
SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ) );
|
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
QObject::connect ( &Protocol,
|
|
|
|
SIGNAL ( ChangeNetwBlSiFact ( int ) ),
|
2006-11-25 15:46:57 +01:00
|
|
|
this, SLOT ( OnNetwBlSiFactChange ( int ) ) );
|
2006-12-09 16:00:24 +01:00
|
|
|
|
|
|
|
QObject::connect( &Protocol, SIGNAL ( ChangeChanGain ( int, double ) ),
|
|
|
|
this, SLOT ( OnChangeChanGain ( int, double ) ) );
|
2006-12-10 12:06:14 +01:00
|
|
|
|
|
|
|
QObject::connect( &Protocol, SIGNAL ( ChangeChanName ( std::string ) ),
|
|
|
|
this, SLOT ( OnChangeChanName ( std::string ) ) );
|
2006-02-26 11:50:47 +01:00
|
|
|
}
|
2006-03-04 11:24:40 +01:00
|
|
|
|
2006-12-09 11:04:27 +01:00
|
|
|
void CChannel::SetEnable ( const bool bNEnStat )
|
|
|
|
{
|
|
|
|
// set internal parameter
|
|
|
|
bIsEnabled = bNEnStat;
|
|
|
|
|
|
|
|
// if channel is not enabled, reset time out count
|
|
|
|
if ( !bNEnStat )
|
|
|
|
{
|
|
|
|
iConTimeOut = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-11 21:35:38 +01:00
|
|
|
void CChannel::SetNetwInBlSiFact ( const int iNewBlockSizeFactor )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// store new value
|
|
|
|
iCurNetwInBlSiFact = iNewBlockSizeFactor;
|
2006-03-11 21:35:38 +01:00
|
|
|
|
2006-12-06 22:10:24 +01:00
|
|
|
// init audio compression unit
|
2006-11-25 15:46:57 +01:00
|
|
|
iAudComprSizeIn = AudioCompressionIn.Init (
|
|
|
|
iNewBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES,
|
|
|
|
CAudioCompression::CT_IMAADPCM );
|
2006-03-11 21:35:38 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// initial value for connection time out counter
|
|
|
|
iConTimeOutStartVal = ( CON_TIME_OUT_SEC_MAX * 1000 ) /
|
|
|
|
( iNewBlockSizeFactor * MIN_BLOCK_DURATION_MS );
|
2006-03-12 13:24:42 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// socket buffer must be adjusted
|
|
|
|
SetSockBufSize ( GetSockBufSize() );
|
2006-03-11 21:35:38 +01:00
|
|
|
}
|
|
|
|
|
2006-03-13 21:23:05 +01:00
|
|
|
void CChannel::SetNetwBufSizeFactOut ( const int iNewNetwBlSiFactOut )
|
2006-03-11 21:35:38 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// store new value
|
|
|
|
iCurNetwOutBlSiFact = iNewNetwBlSiFactOut;
|
2006-03-13 21:23:05 +01:00
|
|
|
|
2006-12-06 22:10:24 +01:00
|
|
|
// init audio compression unit
|
2006-11-25 15:46:57 +01:00
|
|
|
iAudComprSizeOut = AudioCompressionOut.Init (
|
|
|
|
iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES,
|
|
|
|
CAudioCompression::CT_IMAADPCM );
|
2006-03-11 21:35:38 +01:00
|
|
|
|
2006-12-06 22:10:24 +01:00
|
|
|
// init conversion buffer
|
2006-11-25 15:46:57 +01:00
|
|
|
ConvBuf.Init ( iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES );
|
2006-03-13 21:23:05 +01:00
|
|
|
}
|
|
|
|
|
2006-03-04 11:24:40 +01:00
|
|
|
void CChannel::OnSendProtMessage ( CVector<uint8_t> vecMessage )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// only send messages if we are connected, otherwise delete complete queue
|
|
|
|
if ( IsConnected() )
|
|
|
|
{
|
|
|
|
// emit message to actually send the data
|
|
|
|
emit MessReadyForSending ( vecMessage );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// delete send message queue
|
|
|
|
Protocol.DeleteSendMessQueue();
|
|
|
|
}
|
2006-03-04 11:24:40 +01:00
|
|
|
}
|
2006-03-12 12:50:35 +01:00
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
void CChannel::SetSockBufSize ( const int iNumBlocks )
|
|
|
|
{
|
|
|
|
// this opperation must be done with mutex
|
|
|
|
Mutex.lock();
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
|
|
|
iCurSockBufSize = iNumBlocks;
|
|
|
|
|
|
|
|
// the idea of setting the jitter buffer is as follows:
|
|
|
|
// The network block size is a multiple of the internal minimal
|
|
|
|
// block size. Therefore, the minimum jitter buffer size must be
|
|
|
|
// so that it can take one network buffer -> NET_BLOCK_SIZE_FACTOR.
|
|
|
|
// The actual jitter compensation are then the additional blocks of
|
2006-12-18 15:39:33 +01:00
|
|
|
// the internal block size, which is set with SetSockBufSize
|
2006-11-25 15:46:57 +01:00
|
|
|
SockBuf.Init ( MIN_BLOCK_SIZE_SAMPLES,
|
|
|
|
iNumBlocks + iCurNetwInBlSiFact );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-11-25 15:46:57 +01:00
|
|
|
Mutex.unlock();
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-02-27 20:45:27 +01:00
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
void CChannel::OnNetwBlSiFactChange ( int iNewNetwBlSiFact )
|
|
|
|
{
|
|
|
|
// TEST
|
|
|
|
//qDebug ( "new network block size factor: %d", iNewNetwBlSiFact );
|
|
|
|
|
|
|
|
SetNetwBufSizeFactOut ( iNewNetwBlSiFact );
|
|
|
|
}
|
|
|
|
|
2006-02-27 20:45:27 +01:00
|
|
|
void CChannel::OnJittBufSizeChange ( int iNewJitBufSize )
|
|
|
|
{
|
2006-03-01 20:46:44 +01:00
|
|
|
// TEST
|
2006-09-03 11:49:15 +02:00
|
|
|
//qDebug ( "new jitter buffer size: %d", iNewJitBufSize );
|
2006-03-01 20:46:44 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
SetSockBufSize ( iNewJitBufSize );
|
2006-02-27 20:45:27 +01:00
|
|
|
}
|
2006-03-12 12:50:35 +01:00
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
void CChannel::OnChangeChanGain ( int iChanID, double dNewGain )
|
|
|
|
{
|
2008-01-14 23:14:17 +01:00
|
|
|
Q_ASSERT ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) );
|
2006-12-06 21:22:41 +01:00
|
|
|
|
|
|
|
// set value
|
|
|
|
vecdGains[iChanID] = dNewGain;
|
|
|
|
}
|
|
|
|
|
2006-12-10 12:06:14 +01:00
|
|
|
void CChannel::OnChangeChanName ( std::string strName )
|
|
|
|
{
|
|
|
|
// apply value (if different from previous name)
|
|
|
|
if ( sName.compare ( strName ) )
|
|
|
|
{
|
|
|
|
sName = strName;
|
|
|
|
|
|
|
|
// fire message that name has changed
|
|
|
|
emit NameHasChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
bool CChannel::GetAddress(CHostAddress& RetAddr)
|
|
|
|
{
|
|
|
|
if ( IsConnected() )
|
|
|
|
{
|
|
|
|
RetAddr = InetAddr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RetAddr = CHostAddress();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData,
|
|
|
|
int iNumBytes )
|
|
|
|
{
|
2006-12-09 11:04:27 +01:00
|
|
|
EPutDataStat eRet = PS_GEN_ERROR;
|
2006-11-25 15:46:57 +01:00
|
|
|
|
2006-12-10 13:02:28 +01:00
|
|
|
// init flags
|
|
|
|
bool bIsProtocolPacket = false;
|
|
|
|
bool bIsAudioPacket = false;
|
|
|
|
bool bNewConnection = false;
|
|
|
|
|
2006-12-09 11:04:27 +01:00
|
|
|
if ( bIsEnabled )
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
2006-12-10 13:02:28 +01:00
|
|
|
// first check if this is protocol data
|
|
|
|
// only use protocol data if channel is connected
|
|
|
|
if ( IsConnected() )
|
2006-12-09 11:04:27 +01:00
|
|
|
{
|
2006-12-10 13:02:28 +01:00
|
|
|
// this seems not to be an audio block, parse the message
|
|
|
|
if ( Protocol.ParseMessage ( vecbyData, iNumBytes ) )
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
2006-12-10 13:02:28 +01:00
|
|
|
// set status flags
|
|
|
|
eRet = PS_PROT_OK;
|
|
|
|
bIsProtocolPacket = true;
|
2006-12-09 11:04:27 +01:00
|
|
|
|
2006-12-10 13:02:28 +01:00
|
|
|
// create message for protocol status
|
|
|
|
emit ProtocolStatus ( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only try to parse audio if it was not a protocol packet
|
|
|
|
if ( !bIsProtocolPacket )
|
|
|
|
{
|
|
|
|
// check if this is an audio packet by checking all possible lengths
|
|
|
|
for ( int i = 0; i < MAX_NET_BLOCK_SIZE_FACTOR; i++ )
|
|
|
|
{
|
|
|
|
if ( iNumBytes == vecNetwInBufSizes[i] )
|
2006-12-09 11:04:27 +01:00
|
|
|
{
|
2006-12-10 13:02:28 +01:00
|
|
|
bIsAudioPacket = true;
|
|
|
|
|
|
|
|
// check if we are correctly initialized
|
|
|
|
const int iNewNetwInBlSiFact = i + 1;
|
|
|
|
if ( iNewNetwInBlSiFact != iCurNetwInBlSiFact )
|
|
|
|
{
|
|
|
|
// re-initialize to new value
|
|
|
|
SetNetwInBlSiFact ( iNewNetwInBlSiFact );
|
|
|
|
}
|
2006-12-09 11:04:27 +01:00
|
|
|
}
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2006-12-10 13:02:28 +01:00
|
|
|
// only process if packet has correct size
|
2006-12-18 15:39:33 +01:00
|
|
|
if ( bIsAudioPacket )
|
|
|
|
{
|
2006-12-10 13:02:28 +01:00
|
|
|
Mutex.lock();
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
// decompress audio
|
|
|
|
CVector<short> vecsDecomprAudio ( AudioCompressionIn.Decode ( vecbyData ) );
|
|
|
|
|
|
|
|
// do resampling to compensate for sample rate offsets in the
|
2006-12-10 13:02:28 +01:00
|
|
|
// different sound cards of the clients
|
2006-12-18 15:39:33 +01:00
|
|
|
/*
|
|
|
|
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 ( iCurNetwInBlSiFact * MIN_BLOCK_SIZE_SAMPLES );
|
|
|
|
for ( int i = 0; i < iCurNetwInBlSiFact * MIN_BLOCK_SIZE_SAMPLES; i++ ) {
|
2006-11-25 15:46:57 +01:00
|
|
|
vecdResOutData[i] = (double) vecsDecomprAudio[i];
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2006-12-10 13:02:28 +01:00
|
|
|
if ( SockBuf.Put ( vecdResOutData ) )
|
|
|
|
{
|
|
|
|
eRet = PS_AUDIO_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eRet = PS_AUDIO_ERR;
|
|
|
|
}
|
2006-11-25 15:46:57 +01:00
|
|
|
|
2006-12-10 13:02:28 +01:00
|
|
|
// check if channel was not connected, this is a new connection
|
|
|
|
bNewConnection = !IsConnected();
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2006-12-10 13:02:28 +01:00
|
|
|
// reset time-out counter
|
|
|
|
iConTimeOut = iConTimeOutStartVal;
|
|
|
|
}
|
|
|
|
Mutex.unlock();
|
2006-12-09 11:04:27 +01:00
|
|
|
}
|
2006-12-10 13:02:28 +01:00
|
|
|
else
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
2006-12-10 13:02:28 +01:00
|
|
|
// the protocol parsing failed and this was no audio block,
|
|
|
|
// we treat this as protocol error (unkown packet)
|
|
|
|
eRet = PS_PROT_ERR;
|
2006-11-25 15:46:57 +01:00
|
|
|
|
2006-12-10 13:02:28 +01:00
|
|
|
// create message for protocol status
|
|
|
|
emit ProtocolStatus ( false );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-09 11:04:27 +01:00
|
|
|
// inform other objects that new connection was established
|
|
|
|
if ( bNewConnection )
|
|
|
|
{
|
|
|
|
// log new connection
|
|
|
|
CHostAddress address ( GetAddress() );
|
2008-01-14 23:14:17 +01:00
|
|
|
qDebug() << CLogTimeDate::toString() << "Connected with IP " <<
|
|
|
|
address.InetAddr.toString().toLatin1();
|
2006-11-25 15:46:57 +01:00
|
|
|
|
2006-12-09 11:04:27 +01:00
|
|
|
emit NewConnection();
|
|
|
|
}
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
return eRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGetDataStat CChannel::GetData ( CVector<double>& vecdData )
|
2006-03-06 18:04:07 +01:00
|
|
|
{
|
2006-12-09 11:40:18 +01:00
|
|
|
// init with ok flag
|
|
|
|
EGetDataStat eGetStatus = GS_BUFFER_OK;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2006-12-06 22:10:24 +01:00
|
|
|
Mutex.lock(); // get mutex lock
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
if ( !SockBuf.Get ( vecdData ) )
|
|
|
|
{
|
|
|
|
// decrease time-out counter
|
|
|
|
if ( iConTimeOut > 0 )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
iConTimeOut--;
|
2006-12-09 11:40:18 +01:00
|
|
|
|
|
|
|
if ( iConTimeOut == 0 )
|
|
|
|
{
|
|
|
|
// channel is just disconnected
|
|
|
|
eGetStatus = GS_CHAN_NOW_DISCONNECTED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// channel is not yet disconnected but no data in buffer
|
|
|
|
eGetStatus = GS_BUFFER_UNDERRUN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// channel is disconnected
|
|
|
|
eGetStatus = GS_CHAN_NOT_CONNECTED;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-12-09 11:40:18 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
Mutex.unlock(); // get mutex unlock
|
|
|
|
|
|
|
|
return eGetStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
CVector<unsigned char> CChannel::PrepSendPacket ( const CVector<short>& vecsNPacket )
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
2006-12-06 22:10:24 +01:00
|
|
|
// 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
|
2006-11-25 15:46:57 +01:00
|
|
|
CVector<unsigned char> vecbySendBuf ( 0 );
|
|
|
|
|
2006-12-06 22:10:24 +01:00
|
|
|
// use conversion buffer to convert sound card block size in network
|
|
|
|
// block size
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( ConvBuf.Put ( vecsNPacket ) )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-06 22:10:24 +01:00
|
|
|
// a packet is ready, compress audio
|
2006-12-18 15:39:33 +01:00
|
|
|
vecbySendBuf.Init ( iAudComprSizeOut );
|
2006-11-25 15:46:57 +01:00
|
|
|
vecbySendBuf = AudioCompressionOut.Encode ( ConvBuf.Get() );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
return vecbySendBuf;
|
|
|
|
}
|
|
|
|
|
2006-02-12 15:26:46 +01:00
|
|
|
|
2006-03-04 11:24:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-01-28 12:29:22 +01:00
|
|
|
/******************************************************************************\
|
|
|
|
* CSampleOffsetEst *
|
|
|
|
\******************************************************************************/
|
2006-12-18 15:39:33 +01:00
|
|
|
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 * MIN_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);
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|