jamulus/src/channel.cpp

980 lines
36 KiB
C++
Raw Normal View History

/******************************************************************************\
2008-08-02 09:28:21 +02:00
* Copyright (c) 2004-2008
*
* 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() : bWriteStatusHTMLFile ( false )
2006-03-04 12:11:26 +01:00
{
// enable all channels and set server flag
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{
vecChannels[i].SetEnable ( true );
vecChannels[i].SetIsServer ( true );
}
2006-12-09 19:37:40 +01:00
// CODE TAG: MAX_NUM_CHANNELS_TAG
// make sure we have MAX_NUM_CHANNELS connections!!!
// send message
2008-08-02 09:28:21 +02:00
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> ) ) );
// request jitter buffer size
2008-08-05 22:59:01 +02:00
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() ) );
// request connected clients list
2008-08-02 09:28:21 +02: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() ) );
2006-12-10 12:06:14 +01:00
// channel name has changed
2008-08-02 09:28:21 +02:00
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() ) );
// chat text received
QObject::connect ( &vecChannels[0], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh0 ( QString ) ) );
QObject::connect ( &vecChannels[1], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh1 ( QString ) ) );
QObject::connect ( &vecChannels[2], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh2 ( QString ) ) );
QObject::connect ( &vecChannels[3], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh3 ( QString ) ) );
QObject::connect ( &vecChannels[4], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh4 ( QString ) ) );
QObject::connect ( &vecChannels[5], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh5 ( QString ) ) );
// ping message received
2008-08-10 23:56:03 +02:00
QObject::connect ( &vecChannels[0], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh0 ( int ) ) );
QObject::connect ( &vecChannels[1], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh1 ( int ) ) );
QObject::connect ( &vecChannels[2], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh2 ( int ) ) );
QObject::connect ( &vecChannels[3], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh3 ( int ) ) );
QObject::connect ( &vecChannels[4], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh4 ( int ) ) );
QObject::connect ( &vecChannels[5], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh5 ( int ) ) );
2006-03-04 12:12:47 +01:00
}
CVector<CChannelShortInfo> CChannelSet::CreateChannelList()
{
2006-11-27 23:35:22 +01:00
CVector<CChannelShortInfo> vecChanInfo ( 0 );
// look for free channels
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{
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
vecChannels[i].GetAddress().InetAddr.toIPv4Address(), // IP address
2006-11-27 23:35:22 +01:00
vecChannels[i].GetName() /* name */ ) );
}
}
return vecChanInfo;
}
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 );
}
}
2008-08-08 20:51:25 +02:00
// create status HTML file if enabled
if ( bWriteStatusHTMLFile )
{
WriteHTMLChannelList();
}
}
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++ )
{
if ( ( vecChannels[i].IsConnected() ) && ( i != iCurChanID ) )
{
// send message
2006-11-26 22:25:56 +01:00
vecChannels[i].CreateConClientListMes ( vecChanInfo );
}
}
2008-08-08 20:51:25 +02:00
// create status HTML file if enabled
if ( bWriteStatusHTMLFile )
{
WriteHTMLChannelList();
}
}
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 );
}
2008-07-24 19:08:48 +02:00
void CChannelSet::CreateAndSendChatTextForAllConChannels ( const int iCurChanID,
const QString& strChatText )
{
// create message which is sent to all connected clients -------------------
// get client name, if name is empty, use IP address instead
QString ChanName = vecChannels[iCurChanID].GetName();
if ( ChanName.isEmpty() )
{
// convert IP address to text and show it
const QHostAddress addrTest ( vecChannels[iCurChanID].GetAddress().InetAddr );
ChanName = addrTest.toString();
}
// add name of the client at the beginning of the message text
const QString strActualMessageText =
"<" + ChanName + "> " + strChatText;
// send chat text to all connected clients ---------------------------------
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{
if ( vecChannels[i].IsConnected() )
{
// send message
vecChannels[i].CreateChatTextMes ( strActualMessageText );
}
}
}
int CChannelSet::GetFreeChan()
{
// look for a free channel
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{
if ( !vecChannels[i].IsConnected() )
{
return i;
}
}
/* 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 */
if ( InetAddr == Addr )
{
return i;
}
}
}
/* 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,
const CHostAddress& HostAdr )
{
2006-11-27 23:35:22 +01:00
bool bRet = false;
bool bCreateChanList = false;
Mutex.lock();
{
bool bChanOK = true;
// 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();
if ( iCurChanID != INVALID_CHANNEL_ID )
{
// initialize current channel by storing the calling host
// address
vecChannels[iCurChanID].SetAddress ( HostAdr );
// 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 );
}
// 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;
}
else
{
2006-12-06 22:10:24 +01:00
// no free channel available
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
}
}
// put received data in jitter buffer ----------------------------------
if ( bChanOK )
{
// 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 );
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;
}
}
// after data is put in channel buffer, create channel list message if
// requested
if ( bCreateChanList )
{
// 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 );
}
}
Mutex.unlock();
return bRet;
}
void CChannelSet::GetBlockAllConC ( CVector<int>& vecChanID,
CVector<CVector<double> >& vecvecdData,
CVector<CVector<double> >& vecvecdGains )
{
int i, j;
bool bCreateChanList = false;
// init temporal data vector and clear input buffers
CVector<double> vecdData ( MIN_BLOCK_SIZE_SAMPLES );
vecChanID.Init ( 0 );
vecvecdData.Init ( 0 );
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 );
// 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;
}
if ( vecChannels[i].IsConnected() )
{
// 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 ( eGetStat == GS_BUFFER_OK )
{
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_GREEN, i );
}
else
{
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_RED, i );
}
}
}
// 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] );
}
}
// create channel list message if requested
if ( bCreateChanList )
{
// update channel list for all currently connected clients
CreateAndSendChanListForAllConChannels();
}
}
Mutex.unlock(); // release mutex
}
void CChannelSet::GetConCliParam ( CVector<CHostAddress>& vecHostAddresses,
2008-01-22 22:15:04 +01:00
CVector<QString>& vecsName,
CVector<int>& veciJitBufSize,
CVector<int>& veciNetwOutBlSiFact,
CVector<int>& veciNetwInBlSiFact )
{
CHostAddress InetAddr;
// init return values
vecHostAddresses.Init ( MAX_NUM_CHANNELS );
2006-12-29 11:52:25 +01:00
vecsName.Init ( MAX_NUM_CHANNELS );
veciJitBufSize.Init ( MAX_NUM_CHANNELS );
veciNetwOutBlSiFact.Init ( MAX_NUM_CHANNELS );
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();
}
}
}
2008-08-08 20:51:25 +02:00
void CChannelSet::StartStatusHTMLFileWriting ( const QString& strNewFileName,
const QString& strNewServerNameWithPort )
{
// set important parameters
strServerHTMLFileListName = strNewFileName;
strServerNameWithPort = strNewServerNameWithPort;
2008-08-08 20:51:25 +02:00
// set flag
bWriteStatusHTMLFile = true;
2008-08-05 20:55:40 +02:00
2008-08-08 20:51:25 +02:00
// write initial file
WriteHTMLChannelList();
}
void CChannelSet::WriteHTMLChannelList()
{
// create channel list
CVector<CChannelShortInfo> vecChanInfo ( CChannelSet::CreateChannelList() );
// prepare file and stream
QFile serverFileListFile ( strServerHTMLFileListName );
if ( !serverFileListFile.open ( QIODevice::WriteOnly | QIODevice::Text ) )
{
return;
}
QTextStream streamFileOut ( &serverFileListFile );
streamFileOut << strServerNameWithPort << endl << "<ul>" << endl;
// get the number of connected clients
int iNumConnClients = 0;
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{
if ( vecChannels[i].IsConnected() )
{
iNumConnClients++;
}
}
// depending on number of connected clients write list
if ( iNumConnClients == 0 )
{
// no clients are connected -> empty server
2008-08-08 20:51:25 +02:00
streamFileOut << " No client connected" << endl;
}
else
{
// write entry for each connected client
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{
if ( vecChannels[i].IsConnected() )
{
2008-08-08 20:51:25 +02:00
QString strCurChanName = vecChannels[i].GetName();
// if text is empty, show IP address instead
if ( strCurChanName.isEmpty() )
{
// convert IP address to text and show it
const QHostAddress addrTest ( vecChannels[i].GetAddress().InetAddr );
strCurChanName = addrTest.toString();
// remove last digits
strCurChanName = strCurChanName.section ( ".", 0, 2 ) + ".x";
}
streamFileOut << " <li>" << strCurChanName << "</li>" << endl;
}
}
}
// finish list
streamFileOut << "</ul>" << endl;
}
/******************************************************************************\
* CChannel *
\******************************************************************************/
2006-11-26 22:25:56 +01:00
CChannel::CChannel() : sName ( "" ),
vecdGains ( MAX_NUM_CHANNELS, (double) 1.0 ), bIsServer ( false ),
2008-08-05 20:55:40 +02:00
// it is important to give the following parameters meaningful initial
// values because they are dependend on each other
iCurSockBufSize ( DEF_NET_BUF_SIZE_NUM_BL ),
2008-08-05 20:55:40 +02:00
iCurNetwInBlSiFact ( DEF_NET_BLOCK_SIZE_FACTOR )
{
// query all possible network in buffer sizes for determining if an
2008-08-09 18:34:02 +02:00
// audio packet was received (the following code only works if all
// possible network buffer sizes are different!)
const int iNumSupportedAudComprTypes = 3;
vecNetwBufferInProps.Init ( iNumSupportedAudComprTypes *
MAX_NET_BLOCK_SIZE_FACTOR );
for ( int i = 0; i < MAX_NET_BLOCK_SIZE_FACTOR; i++ )
{
const int iNoneIdx = iNumSupportedAudComprTypes * i;
const int iIMAIdx = iNumSupportedAudComprTypes * i + 1;
const int iMSIdx = iNumSupportedAudComprTypes * i + 2;
2008-08-09 18:34:02 +02:00
// network block size factor must start from 1 -> i + 1
const int iCurNetBlockSizeFact = i + 1;
vecNetwBufferInProps[iNoneIdx].iBlockSizeFactor = iCurNetBlockSizeFact;
vecNetwBufferInProps[iIMAIdx].iBlockSizeFactor = iCurNetBlockSizeFact;
vecNetwBufferInProps[iMSIdx].iBlockSizeFactor = iCurNetBlockSizeFact;
// None (no audio compression)
vecNetwBufferInProps[iNoneIdx].eAudComprType = CAudioCompression::CT_NONE;
vecNetwBufferInProps[iNoneIdx].iNetwInBufSize = AudioCompressionIn.Init (
vecNetwBufferInProps[iNoneIdx].iBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES,
vecNetwBufferInProps[iNoneIdx].eAudComprType );
2008-08-09 18:34:02 +02:00
// IMA ADPCM
vecNetwBufferInProps[iIMAIdx].eAudComprType = CAudioCompression::CT_IMAADPCM;
vecNetwBufferInProps[iIMAIdx].iNetwInBufSize = AudioCompressionIn.Init (
vecNetwBufferInProps[iIMAIdx].iBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES,
vecNetwBufferInProps[iIMAIdx].eAudComprType );
// MS ADPCM
vecNetwBufferInProps[iMSIdx].eAudComprType = CAudioCompression::CT_MSADPCM;
vecNetwBufferInProps[iMSIdx].iNetwInBufSize = AudioCompressionIn.Init (
vecNetwBufferInProps[iMSIdx].iBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES,
vecNetwBufferInProps[iMSIdx].eAudComprType );
}
2006-12-06 22:10:24 +01:00
// init the socket buffer
SetSockBufSize ( DEF_NET_BUF_SIZE_NUM_BL );
2006-01-28 12:29:22 +01:00
// set initial input and output block size factors
SetNetwInBlSiFactAndCompr ( DEF_NET_BLOCK_SIZE_FACTOR, CAudioCompression::CT_MSADPCM );
2008-08-09 18:34:02 +02:00
SetNetwBufSizeFactOut ( DEF_NET_BLOCK_SIZE_FACTOR );
// set initial audio compression format for output
SetAudioCompressionOut ( CAudioCompression::CT_MSADPCM );
2006-12-06 22:10:24 +01:00
// init time-out for the buffer with zero -> no connection
iConTimeOut = 0;
2006-02-26 11:50:47 +01:00
2006-12-06 22:10:24 +01:00
// connections -------------------------------------------------------------
QObject::connect ( &Protocol,
SIGNAL ( MessReadyForSending ( CVector<uint8_t> ) ),
this, SLOT ( OnSendProtMessage ( CVector<uint8_t> ) ) );
QObject::connect ( &Protocol,
SIGNAL ( ChangeJittBufSize ( int ) ),
this, SLOT ( OnJittBufSizeChange ( int ) ) );
QObject::connect ( &Protocol,
SIGNAL ( ReqJittBufSize() ),
SIGNAL ( ReqJittBufSize() ) );
QObject::connect ( &Protocol,
SIGNAL ( ReqConnClientsList() ),
SIGNAL ( ReqConnClientsList() ) );
QObject::connect ( &Protocol,
SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ),
2006-11-26 22:25:56 +01:00
SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ) );
QObject::connect ( &Protocol,
SIGNAL ( ChangeNetwBlSiFact ( int ) ),
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
2008-01-22 22:15:04 +01:00
QObject::connect( &Protocol, SIGNAL ( ChangeChanName ( QString ) ),
this, SLOT ( OnChangeChanName ( QString ) ) );
2008-07-24 08:53:07 +02:00
QObject::connect( &Protocol, SIGNAL ( ChatTextReceived ( QString ) ),
this, SIGNAL ( ChatTextReceived ( QString ) ) );
2008-08-02 09:28:21 +02:00
2008-08-10 23:56:03 +02:00
QObject::connect( &Protocol, SIGNAL ( PingReceived ( int ) ),
this, SIGNAL ( PingReceived ( int ) ) );
2006-02-26 11:50:47 +01:00
}
2006-03-04 11:24:40 +01:00
void CChannel::SetEnable ( const bool bNEnStat )
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
// set internal parameter
bIsEnabled = bNEnStat;
// if channel is not enabled, reset time out count
if ( !bNEnStat )
{
iConTimeOut = 0;
}
}
2008-08-09 18:34:02 +02:00
void CChannel::SetNetwInBlSiFactAndCompr ( const int iNewBlockSizeFactor,
const CAudioCompression::EAudComprType eNewAudComprType )
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
// store new value
iCurNetwInBlSiFact = iNewBlockSizeFactor;
2008-08-15 08:56:49 +02:00
// init audio compression unit
AudioCompressionIn.Init ( iNewBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES,
eNewAudComprType );
2008-08-15 08:56:49 +02:00
// initial value for connection time out counter
iConTimeOutStartVal = ( CON_TIME_OUT_SEC_MAX * 1000 ) /
( iNewBlockSizeFactor * MIN_BLOCK_DURATION_MS );
2008-08-15 08:56:49 +02:00
// socket buffer must be adjusted
SetSockBufSizeIntern ( GetSockBufSize() );
}
void CChannel::SetNetwBufSizeFactOut ( const int iNewNetwBlSiFactOut )
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
2008-08-15 08:56:49 +02:00
// store new value
iCurNetwOutBlSiFact = iNewNetwBlSiFactOut;
2008-08-15 08:56:49 +02:00
// init audio compression and get audio compression block size
iAudComprSizeOut = AudioCompressionOut.Init (
iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES, eAudComprTypeOut );
// init conversion buffer
ConvBuf.Init ( iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES );
}
void CChannel::SetAudioCompressionOut ( const CAudioCompression::EAudComprType eNewAudComprTypeOut )
{
// store new value
eAudComprTypeOut = eNewAudComprTypeOut;
// call "set network buffer size factor" function because its initialization
// depends on the audio compression format and implicitely, the audio compression
// is initialized
SetNetwBufSizeFactOut ( iCurNetwOutBlSiFact );
}
void CChannel::SetSockBufSize ( const int iNumBlocks )
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex ); // this opperation must be done with mutex
SetSockBufSizeIntern ( iNumBlocks );
}
void CChannel::SetSockBufSizeIntern ( const int iNumBlocks )
{
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
// the internal block size, which is set with SetSockBufSize
SockBuf.Init ( MIN_BLOCK_SIZE_SAMPLES,
iNumBlocks + iCurNetwInBlSiFact );
}
void CChannel::SetGain ( const int iChanID, const double dNewGain )
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
// set value (make sure channel ID is in range)
if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) )
{
2008-08-15 08:56:49 +02:00
vecdGains[iChanID] = dNewGain;
}
}
double CChannel::GetGain ( const int iChanID )
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
2008-08-15 08:56:49 +02:00
// get value (make sure channel ID is in range)
if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) )
{
2008-08-15 08:56:49 +02:00
return vecdGains[iChanID];
}
else
{
return 0;
}
}
void CChannel::SetName ( const QString strNewName )
{
bool bNameHasChanged = false;
Mutex.lock();
{
// apply value (if different from previous name)
if ( sName.compare ( strNewName ) )
{
sName = strNewName;
bNameHasChanged = true;
}
}
Mutex.unlock();
// fire message that name has changed
if ( bNameHasChanged )
{
// the "emit" has to be done outside the mutexed region
emit NameHasChanged();
}
}
QString CChannel::GetName()
{
// make sure the string is not written at the same time when it is
// read here -> use mutex to secure access
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
2008-08-15 08:56:49 +02:00
return sName;
}
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
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
}
void CChannel::OnNetwBlSiFactChange ( int iNewNetwBlSiFact )
{
SetNetwBufSizeFactOut ( iNewNetwBlSiFact );
}
void CChannel::OnJittBufSizeChange ( int iNewJitBufSize )
{
SetSockBufSize ( iNewJitBufSize );
}
void CChannel::OnChangeChanGain ( int iChanID, double dNewGain )
{
SetGain ( iChanID, dNewGain );
}
2008-01-22 22:15:04 +01:00
void CChannel::OnChangeChanName ( QString strName )
2006-12-10 12:06:14 +01:00
{
SetName ( strName );
2006-12-10 12:06:14 +01:00
}
bool CChannel::GetAddress(CHostAddress& RetAddr)
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
2008-08-15 08:56:49 +02:00
if ( IsConnected() )
{
2008-08-15 08:56:49 +02:00
RetAddr = InetAddr;
return true;
}
else
{
RetAddr = CHostAddress();
return false;
}
}
EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData,
int iNumBytes )
{
EPutDataStat eRet = PS_GEN_ERROR;
// init flags
bool bIsProtocolPacket = false;
bool bIsAudioPacket = false;
bool bNewConnection = false;
if ( bIsEnabled )
{
// first check if this is protocol data
// only use protocol data if channel is connected
if ( IsConnected() )
{
2008-08-05 20:55:40 +02:00
// parse the message assuming this is a protocol message
if ( !Protocol.ParseMessage ( vecbyData, iNumBytes ) )
{
// set status flags
eRet = PS_PROT_OK;
bIsProtocolPacket = true;
// 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
2008-08-09 18:34:02 +02:00
const int iPossNetwSizes = vecNetwBufferInProps.Size();
for ( int i = 0; i < iPossNetwSizes; i++ )
{
2008-08-05 20:55:40 +02:00
// check for low/high quality audio packets and set flags
2008-08-09 18:34:02 +02:00
if ( iNumBytes == vecNetwBufferInProps[i].iNetwInBufSize )
{
2008-08-05 22:59:01 +02:00
bIsAudioPacket = true;
// check if we are correctly initialized
2008-08-09 18:34:02 +02:00
const int iNewNetwInBlSiFact =
vecNetwBufferInProps[i].iBlockSizeFactor;
const CAudioCompression::EAudComprType eNewAudComprType =
vecNetwBufferInProps[i].eAudComprType;
if ( ( iNewNetwInBlSiFact != iCurNetwInBlSiFact ) ||
( eNewAudComprType != AudioCompressionIn.GetType() ) )
2008-08-05 22:59:01 +02:00
{
// re-initialize to new value
2008-08-09 18:34:02 +02:00
SetNetwInBlSiFactAndCompr ( iNewNetwInBlSiFact,
eNewAudComprType );
}
// in case of a server channel, use the same audio
// compression for output as for the input
if ( bIsServer )
{
if ( GetAudioCompressionOut() != vecNetwBufferInProps[i].eAudComprType )
{
SetAudioCompressionOut ( vecNetwBufferInProps[i].eAudComprType );
}
2008-08-05 22:59:01 +02:00
}
}
}
// only process if packet has correct size
if ( bIsAudioPacket )
{
Mutex.lock();
{
// decompress audio
2008-08-05 22:59:01 +02:00
CVector<short> vecsDecomprAudio ( AudioCompressionIn.Decode ( vecbyData ) );
// convert received data from short to double
CVector<double> vecdDecomprAudio ( iCurNetwInBlSiFact * MIN_BLOCK_SIZE_SAMPLES );
for ( int i = 0; i < iCurNetwInBlSiFact * MIN_BLOCK_SIZE_SAMPLES; i++ )
{
vecdDecomprAudio[i] = static_cast<double> ( vecsDecomprAudio[i] );
}
if ( SockBuf.Put ( vecdDecomprAudio ) )
{
eRet = PS_AUDIO_OK;
}
else
{
eRet = PS_AUDIO_ERR;
}
// check if channel was not connected, this is a new connection
bNewConnection = !IsConnected();
// reset time-out counter
iConTimeOut = iConTimeOutStartVal;
}
Mutex.unlock();
}
else
{
// the protocol parsing failed and this was no audio block,
// we treat this as protocol error (unkown packet)
eRet = PS_PROT_ERR;
// create message for protocol status
emit ProtocolStatus ( false );
}
}
// inform other objects that new connection was established
if ( bNewConnection )
{
// log new connection
CHostAddress address ( GetAddress() );
2008-02-24 18:13:59 +01:00
qDebug() << CLogTimeDate::toString() + "Connected with IP " + address.InetAddr.toString();
emit NewConnection();
}
}
return eRet;
}
EGetDataStat CChannel::GetData ( CVector<double>& vecdData )
2006-03-06 18:04:07 +01:00
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
// init with ok flag
EGetDataStat eGetStatus = GS_BUFFER_OK;
2008-08-15 08:56:49 +02:00
if ( !SockBuf.Get ( vecdData ) )
{
2008-08-15 08:56:49 +02:00
// decrease time-out counter
if ( iConTimeOut > 0 )
{
2008-08-15 08:56:49 +02:00
iConTimeOut--;
2008-08-15 08:56:49 +02:00
if ( iConTimeOut == 0 )
{
// channel is just disconnected
eGetStatus = GS_CHAN_NOW_DISCONNECTED;
}
else
{
2008-08-15 08:56:49 +02:00
// channel is not yet disconnected but no data in buffer
eGetStatus = GS_BUFFER_UNDERRUN;
}
}
2008-08-15 08:56:49 +02:00
else
{
// channel is disconnected
eGetStatus = GS_CHAN_NOT_CONNECTED;
}
}
return eGetStatus;
}
CVector<unsigned char> CChannel::PrepSendPacket ( const CVector<short>& vecsNPacket )
2006-01-28 12:29:22 +01:00
{
2008-08-15 08:56:49 +02:00
QMutexLocker locker ( &Mutex );
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
CVector<unsigned char> vecbySendBuf ( 0 );
2008-08-15 08:56:49 +02:00
// use conversion buffer to convert sound card block size in network
// block size
if ( ConvBuf.Put ( vecsNPacket ) )
{
2008-08-15 08:56:49 +02:00
// a packet is ready, compress audio
vecbySendBuf.Init ( iAudComprSizeOut );
vecbySendBuf = AudioCompressionOut.Encode ( ConvBuf.Get() );
}
return vecbySendBuf;
}