2006-12-18 15:39:33 +01:00
|
|
|
/******************************************************************************\
|
2009-02-22 12:07:18 +01:00
|
|
|
* Copyright (c) 2004-2009
|
2006-12-18 15:39:33 +01:00
|
|
|
*
|
|
|
|
* 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 "server.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Implementation *************************************************************/
|
2008-12-02 19:02:40 +01:00
|
|
|
CServer::CServer ( const QString& strLoggingFileName,
|
|
|
|
const quint16 iPortNumber,
|
2008-08-09 09:55:27 +02:00
|
|
|
const QString& strHTMLStatusFileName,
|
2009-06-06 12:07:01 +02:00
|
|
|
const QString& strHistoryFileName,
|
2009-07-24 16:31:25 +02:00
|
|
|
const QString& strServerNameForHTMLStatusFile ) :
|
2009-07-26 22:12:22 +02:00
|
|
|
Socket ( this, iPortNumber ),
|
|
|
|
bWriteStatusHTMLFile ( false )
|
2006-09-03 11:49:15 +02:00
|
|
|
{
|
2009-07-26 22:12:22 +02:00
|
|
|
// enable all channels (for the server all channel must be enabled the
|
|
|
|
// entire life time of the software
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
vecChannels[i].SetEnable ( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
// define colors for chat window identifiers
|
|
|
|
vstrChatColors.Init ( 6 );
|
|
|
|
vstrChatColors[0] = "mediumblue";
|
|
|
|
vstrChatColors[1] = "red";
|
|
|
|
vstrChatColors[2] = "darkorchid";
|
|
|
|
vstrChatColors[3] = "green";
|
|
|
|
vstrChatColors[4] = "maroon";
|
|
|
|
vstrChatColors[5] = "coral";
|
|
|
|
|
2009-07-24 18:00:47 +02:00
|
|
|
vecsSendData.Init ( SYSTEM_BLOCK_SIZE_SAMPLES );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
// init moving average buffer for response time evaluation
|
2009-07-24 18:00:47 +02:00
|
|
|
CycleTimeVariance.Init ( SYSTEM_BLOCK_SIZE_SAMPLES,
|
2009-07-05 09:07:30 +02:00
|
|
|
SYSTEM_SAMPLE_RATE, TIME_MOV_AV_RESPONSE );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
// connect timer timeout signal
|
2006-11-25 15:46:57 +01:00
|
|
|
QObject::connect ( &Timer, SIGNAL ( timeout() ),
|
|
|
|
this, SLOT ( OnTimer() ) );
|
2006-02-20 22:09:36 +01:00
|
|
|
|
2007-05-06 14:27:41 +02:00
|
|
|
// enable logging (if requested)
|
2008-12-02 19:02:40 +01:00
|
|
|
if ( !strLoggingFileName.isEmpty() )
|
2007-05-06 14:27:41 +02:00
|
|
|
{
|
2008-12-02 19:02:40 +01:00
|
|
|
Logging.Start ( strLoggingFileName );
|
2007-05-06 14:27:41 +02:00
|
|
|
}
|
2008-08-08 20:51:25 +02:00
|
|
|
|
2009-06-06 12:07:01 +02:00
|
|
|
// enable history graph (if requested)
|
|
|
|
if ( !strHistoryFileName.isEmpty() )
|
|
|
|
{
|
|
|
|
Logging.EnableHistory ( strHistoryFileName );
|
|
|
|
}
|
|
|
|
|
2008-08-08 20:51:25 +02:00
|
|
|
// HTML status file writing
|
|
|
|
if ( !strHTMLStatusFileName.isEmpty() )
|
|
|
|
{
|
2008-08-09 09:55:27 +02:00
|
|
|
QString strCurServerNameForHTMLStatusFile = strServerNameForHTMLStatusFile;
|
|
|
|
|
|
|
|
// if server name is empty, substitude a default name
|
|
|
|
if ( strCurServerNameForHTMLStatusFile.isEmpty() )
|
|
|
|
{
|
|
|
|
strCurServerNameForHTMLStatusFile = "[server address]";
|
|
|
|
}
|
|
|
|
|
2008-08-08 20:51:25 +02:00
|
|
|
// (the static cast to integer of the port number is required so that it
|
|
|
|
// works correctly under Linux)
|
2009-07-26 22:12:22 +02:00
|
|
|
StartStatusHTMLFileWriting ( strHTMLStatusFileName,
|
2008-08-09 09:55:27 +02:00
|
|
|
strCurServerNameForHTMLStatusFile + ":" +
|
|
|
|
QString().number( static_cast<int> ( iPortNumber ) ) );
|
2008-08-08 20:51:25 +02:00
|
|
|
}
|
2009-07-26 22:12:22 +02:00
|
|
|
|
|
|
|
// CODE TAG: MAX_NUM_CHANNELS_TAG
|
|
|
|
// 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> ) ) );
|
|
|
|
|
|
|
|
// 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() ) );
|
|
|
|
|
|
|
|
// request connected clients list
|
|
|
|
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() ) );
|
|
|
|
|
|
|
|
// 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() ) );
|
|
|
|
|
|
|
|
// 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 ) ) );
|
|
|
|
QObject::connect ( &vecChannels[6], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh6 ( QString ) ) );
|
|
|
|
QObject::connect ( &vecChannels[7], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh7 ( QString ) ) );
|
|
|
|
QObject::connect ( &vecChannels[8], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh8 ( QString ) ) );
|
|
|
|
QObject::connect ( &vecChannels[9], SIGNAL ( ChatTextReceived ( QString ) ), this, SLOT ( OnChatTextReceivedCh9 ( QString ) ) );
|
|
|
|
|
|
|
|
// ping message received
|
|
|
|
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 ) ) );
|
|
|
|
QObject::connect ( &vecChannels[6], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh6 ( int ) ) );
|
|
|
|
QObject::connect ( &vecChannels[7], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh7 ( int ) ) );
|
|
|
|
QObject::connect ( &vecChannels[8], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh8 ( int ) ) );
|
|
|
|
QObject::connect ( &vecChannels[9], SIGNAL ( PingReceived ( int ) ), this, SLOT ( OnPingReceivedCh9 ( int ) ) );
|
2006-03-04 12:11:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::OnSendProtMessage ( int iChID, CVector<uint8_t> vecMessage )
|
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// the protocol queries me to call the function to send the message
|
|
|
|
// send it through the network
|
2009-07-26 22:12:22 +02:00
|
|
|
Socket.SendPacket ( vecMessage, GetAddress ( iChID ) );
|
2006-03-04 12:11:26 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
void CServer::Start()
|
2006-02-12 15:26:46 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( !IsRunning() )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
// start main timer
|
2009-07-24 22:17:01 +02:00
|
|
|
|
|
|
|
// TEST
|
|
|
|
Timer.start ( static_cast<int> ( ceil ( SYSTEM_BLOCK_DURATION_MS_FLOAT ) ) );
|
2006-11-25 15:46:57 +01:00
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
// init time for response time evaluation
|
2009-03-07 11:52:06 +01:00
|
|
|
CycleTimeVariance.Reset();
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
2006-02-12 15:26:46 +01:00
|
|
|
}
|
|
|
|
|
2006-10-30 21:35:46 +01:00
|
|
|
void CServer::Stop()
|
2006-02-12 15:26:46 +01:00
|
|
|
{
|
2006-12-06 21:22:41 +01:00
|
|
|
// stop main timer
|
2006-11-25 15:46:57 +01:00
|
|
|
Timer.stop();
|
2006-02-18 13:19:27 +01:00
|
|
|
|
2007-05-06 14:27:41 +02:00
|
|
|
// logging
|
2009-05-24 17:25:04 +02:00
|
|
|
Logging.AddServerStopped();
|
2008-12-02 19:02:40 +01:00
|
|
|
}
|
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
void CServer::OnTimer()
|
|
|
|
{
|
|
|
|
CVector<int> vecChanID;
|
2009-07-24 18:00:47 +02:00
|
|
|
CVector<CVector<double> > vecvecdData ( SYSTEM_BLOCK_SIZE_SAMPLES );
|
2006-12-06 21:22:41 +01:00
|
|
|
CVector<CVector<double> > vecvecdGains;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
// get data from all connected clients
|
2009-07-26 22:12:22 +02:00
|
|
|
GetBlockAllConC ( vecChanID, vecvecdData, vecvecdGains );
|
2006-11-25 15:46:57 +01:00
|
|
|
const int iNumClients = vecChanID.Size();
|
2006-02-12 15:26:46 +01:00
|
|
|
|
2006-12-06 21:22:41 +01:00
|
|
|
// Check if at least one client is connected. If not, stop server until
|
|
|
|
// one client is connected
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( iNumClients != 0 )
|
|
|
|
{
|
2006-12-18 15:39:33 +01:00
|
|
|
for ( int i = 0; i < iNumClients; i++ )
|
|
|
|
{
|
2006-12-06 21:22:41 +01:00
|
|
|
// generate a sparate mix for each channel
|
|
|
|
// actual processing of audio data -> mix
|
|
|
|
vecsSendData = ProcessData ( vecvecdData, vecvecdGains[i] );
|
2006-11-27 23:35:22 +01:00
|
|
|
|
|
|
|
// send separate mix to current clients
|
2006-11-25 15:46:57 +01:00
|
|
|
Socket.SendPacket (
|
2009-07-26 22:12:22 +02:00
|
|
|
PrepSendPacket ( vecChanID[i], vecsSendData ),
|
|
|
|
GetAddress ( vecChanID[i] ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Disable server if no clients are connected. In this case the server
|
|
|
|
// does not consume any significant CPU when no client is connected.
|
|
|
|
Stop();
|
|
|
|
}
|
2009-02-08 23:44:18 +01:00
|
|
|
|
2009-03-07 11:52:06 +01:00
|
|
|
// update response time measurement
|
|
|
|
CycleTimeVariance.Update();
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
2009-07-26 22:12:22 +02:00
|
|
|
void CServer::GetBlockAllConC ( CVector<int>& vecChanID,
|
|
|
|
CVector<CVector<double> >& vecvecdData,
|
|
|
|
CVector<CVector<double> >& vecvecdGains )
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
bool bChannelIsNowDisconnected = false;
|
|
|
|
|
|
|
|
// init temporal data vector and clear input buffers
|
|
|
|
CVector<double> vecdData ( SYSTEM_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 < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
// read out all input buffers to decrease timeout counter on
|
|
|
|
// disconnected channels
|
|
|
|
// const EGetDataStat eGetStat = vecChannels[i].GetData ( vecdData );
|
|
|
|
const EGetDataStat eGetStat=GS_BUFFER_OK;//TEST
|
|
|
|
|
|
|
|
// if channel was just disconnected, set flag that connected
|
|
|
|
// client list is sent to all other clients
|
|
|
|
if ( eGetStat == GS_CHAN_NOW_DISCONNECTED )
|
|
|
|
{
|
|
|
|
bChannelIsNowDisconnected = 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
|
|
|
|
vecvecdGains[i][j] =
|
|
|
|
vecChannels[vecChanID[i]].GetGain( vecChanID[j] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// a channel is now disconnected, take action on it
|
|
|
|
if ( bChannelIsNowDisconnected )
|
|
|
|
{
|
|
|
|
// update channel list for all currently connected clients
|
|
|
|
CreateAndSendChanListForAllConChannels();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Mutex.unlock(); // release mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
CVector<CChannelShortInfo> CServer::CreateChannelList()
|
|
|
|
{
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( 0 );
|
|
|
|
|
|
|
|
// look for free channels
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
// append channel ID, IP address and channel name to storing vectors
|
|
|
|
vecChanInfo.Add ( CChannelShortInfo (
|
|
|
|
i, // ID
|
|
|
|
vecChannels[i].GetAddress().InetAddr.toIPv4Address(), // IP address
|
|
|
|
vecChannels[i].GetName() /* name */ ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vecChanInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::CreateAndSendChanListForAllConChannels()
|
|
|
|
{
|
|
|
|
// create channel list
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( CreateChannelList() );
|
|
|
|
|
|
|
|
// now send connected channels list to all connected clients
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
// send message
|
|
|
|
vecChannels[i].CreateConClientListMes ( vecChanInfo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create status HTML file if enabled
|
|
|
|
if ( bWriteStatusHTMLFile )
|
|
|
|
{
|
|
|
|
WriteHTMLChannelList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::CreateAndSendChanListForAllExceptThisChan ( const int iCurChanID )
|
|
|
|
{
|
|
|
|
// create channel list
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( CreateChannelList() );
|
|
|
|
|
|
|
|
// now send connected channels list to all connected clients except for
|
|
|
|
// the channel with the ID "iCurChanID"
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( ( vecChannels[i].IsConnected() ) && ( i != iCurChanID ) )
|
|
|
|
{
|
|
|
|
// send message
|
|
|
|
vecChannels[i].CreateConClientListMes ( vecChanInfo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create status HTML file if enabled
|
|
|
|
if ( bWriteStatusHTMLFile )
|
|
|
|
{
|
|
|
|
WriteHTMLChannelList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::CreateAndSendChanListForThisChan ( const int iCurChanID )
|
|
|
|
{
|
|
|
|
// create channel list
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( CreateChannelList() );
|
|
|
|
|
|
|
|
// now send connected channels list to the channel with the ID "iCurChanID"
|
|
|
|
vecChannels[iCurChanID].CreateConClientListMes ( vecChanInfo );
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::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
|
|
|
|
ChanName =
|
|
|
|
vecChannels[iCurChanID].GetAddress().GetIpAddressStringNoLastByte();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add name of the client at the beginning of the message text and use
|
|
|
|
// different colors, to get correct HTML, the "<" and ">" signs must be
|
|
|
|
// replaced by "<" and ">"
|
|
|
|
QString sCurColor = vstrChatColors[iCurChanID % vstrChatColors.Size()];
|
|
|
|
const QString strActualMessageText =
|
|
|
|
"<font color=""" + sCurColor + """><b><" + ChanName +
|
|
|
|
"></b></font> " + strChatText;
|
|
|
|
|
|
|
|
|
|
|
|
// send chat text to all connected clients ---------------------------------
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
// send message
|
|
|
|
vecChannels[i].CreateChatTextMes ( strActualMessageText );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CServer::GetFreeChan()
|
|
|
|
{
|
|
|
|
// look for a free channel
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( !vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no free channel found, return invalid ID
|
|
|
|
return INVALID_CHANNEL_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CServer::CheckAddr ( const CHostAddress& Addr )
|
|
|
|
{
|
|
|
|
CHostAddress InetAddr;
|
|
|
|
|
|
|
|
// check for all possible channels if IP is already in use
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
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 CServer::PutData ( const CVector<uint8_t>& vecbyRecBuf,
|
|
|
|
const int iNumBytesRead,
|
|
|
|
const CHostAddress& HostAdr )
|
|
|
|
{
|
|
|
|
bool bAudioOK = false;
|
|
|
|
bool bNewChannelReserved = 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 < USED_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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// set flag for new reserved channel
|
|
|
|
bNewChannelReserved = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no free channel available
|
|
|
|
bChanOK = false;
|
|
|
|
|
|
|
|
// create and send "server full" message
|
|
|
|
|
|
|
|
// TODO problem: if channel is not officially connected, we cannot send messages
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
bAudioOK = 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 );
|
|
|
|
bAudioOK = 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 );
|
|
|
|
|
|
|
|
// TEST
|
|
|
|
bAudioOK = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// after data is put in channel buffer, create channel list message if
|
|
|
|
// requested
|
|
|
|
if ( bNewChannelReserved && bAudioOK )
|
|
|
|
{
|
|
|
|
// logging of new connected channel
|
|
|
|
Logging.AddNewConnection ( HostAdr.InetAddr );
|
|
|
|
|
|
|
|
// A new client connected to the server, create and
|
|
|
|
// send all clients the updated channel list (the list has to
|
|
|
|
// be created after the received data has to be put to the
|
|
|
|
// channel first so that this channel is marked as connected)
|
|
|
|
//
|
|
|
|
// 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 bAudioOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::GetConCliParam ( CVector<CHostAddress>& vecHostAddresses,
|
|
|
|
CVector<QString>& vecsName,
|
|
|
|
CVector<int>& veciJitBufSize,
|
|
|
|
CVector<int>& veciNetwOutBlSiFact )
|
|
|
|
{
|
|
|
|
CHostAddress InetAddr;
|
|
|
|
|
|
|
|
// init return values
|
|
|
|
vecHostAddresses.Init ( USED_NUM_CHANNELS );
|
|
|
|
vecsName.Init ( USED_NUM_CHANNELS );
|
|
|
|
veciJitBufSize.Init ( USED_NUM_CHANNELS );
|
|
|
|
veciNetwOutBlSiFact.Init ( USED_NUM_CHANNELS );
|
|
|
|
|
|
|
|
// check all possible channels
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].GetAddress ( InetAddr ) )
|
|
|
|
{
|
|
|
|
// get requested data
|
|
|
|
vecHostAddresses[i] = InetAddr;
|
|
|
|
vecsName[i] = vecChannels[i].GetName();
|
|
|
|
veciJitBufSize[i] = vecChannels[i].GetSockBufSize();
|
|
|
|
veciNetwOutBlSiFact[i] = vecChannels[i].GetNetwBufSizeFactOut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::StartStatusHTMLFileWriting ( const QString& strNewFileName,
|
|
|
|
const QString& strNewServerNameWithPort )
|
|
|
|
{
|
|
|
|
// set important parameters
|
|
|
|
strServerHTMLFileListName = strNewFileName;
|
|
|
|
strServerNameWithPort = strNewServerNameWithPort;
|
|
|
|
|
|
|
|
// set flag
|
|
|
|
bWriteStatusHTMLFile = true;
|
|
|
|
|
|
|
|
// write initial file
|
|
|
|
WriteHTMLChannelList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::WriteHTMLChannelList()
|
|
|
|
{
|
|
|
|
// create channel list
|
|
|
|
CVector<CChannelShortInfo> vecChanInfo ( 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 < USED_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
|
|
|
|
streamFileOut << " No client connected" << endl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// write entry for each connected client
|
|
|
|
for ( int i = 0; i < USED_NUM_CHANNELS; i++ )
|
|
|
|
{
|
|
|
|
if ( vecChannels[i].IsConnected() )
|
|
|
|
{
|
|
|
|
QString strCurChanName = vecChannels[i].GetName();
|
|
|
|
|
|
|
|
// if text is empty, show IP address instead
|
|
|
|
if ( strCurChanName.isEmpty() )
|
|
|
|
{
|
|
|
|
// convert IP address to text and show it, remove last
|
|
|
|
// digits
|
|
|
|
strCurChanName =
|
|
|
|
vecChannels[i].GetAddress().GetIpAddressStringNoLastByte();
|
|
|
|
}
|
|
|
|
|
|
|
|
streamFileOut << " <li>" << strCurChanName << "</li>" << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish list
|
|
|
|
streamFileOut << "</ul>" << endl;
|
|
|
|
}
|
|
|
|
|
2006-11-04 10:57:59 +01:00
|
|
|
CVector<short> CServer::ProcessData ( CVector<CVector<double> >& vecvecdData,
|
2006-12-18 15:39:33 +01:00
|
|
|
CVector<double>& vecdGains )
|
|
|
|
{
|
2009-06-12 18:06:45 +02:00
|
|
|
int i;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2009-06-12 18:06:45 +02:00
|
|
|
// init return vector with zeros since we mix all channels on that vector
|
2009-07-24 18:00:47 +02:00
|
|
|
CVector<short> vecsOutData ( SYSTEM_BLOCK_SIZE_SAMPLES, 0 );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2009-06-12 18:06:45 +02:00
|
|
|
const int iNumClients = vecvecdData.Size();
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
// mix all audio data from all clients together
|
2009-06-12 18:06:45 +02:00
|
|
|
for ( int j = 0; j < iNumClients; j++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2009-06-12 18:06:45 +02:00
|
|
|
// if channel gain is 1, avoid multiplication for speed optimization
|
|
|
|
if ( vecdGains[j] == static_cast<double> ( 1.0 ) )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2009-07-24 18:00:47 +02:00
|
|
|
for ( i = 0; i < SYSTEM_BLOCK_SIZE_SAMPLES; i++ )
|
2009-06-12 18:06:45 +02:00
|
|
|
{
|
|
|
|
vecsOutData[i] =
|
|
|
|
Double2Short ( vecsOutData[i] + vecvecdData[j][i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-24 18:00:47 +02:00
|
|
|
for ( i = 0; i < SYSTEM_BLOCK_SIZE_SAMPLES; i++ )
|
2009-06-12 18:06:45 +02:00
|
|
|
{
|
|
|
|
vecsOutData[i] =
|
|
|
|
Double2Short ( vecsOutData[i] +
|
|
|
|
vecvecdData[j][i] * vecdGains[j] );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vecsOutData;
|
|
|
|
}
|
2006-02-12 15:26:46 +01:00
|
|
|
|
|
|
|
bool CServer::GetTimingStdDev ( double& dCurTiStdDev )
|
|
|
|
{
|
2006-12-06 21:22:41 +01:00
|
|
|
dCurTiStdDev = 0.0; // init return value
|
2006-11-25 15:46:57 +01:00
|
|
|
|
2009-03-07 11:52:06 +01:00
|
|
|
// only return value if server is active and the actual measurement is
|
|
|
|
// updated
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( IsRunning() )
|
|
|
|
{
|
2009-03-07 11:52:06 +01:00
|
|
|
// return the standard deviation
|
|
|
|
dCurTiStdDev = CycleTimeVariance.GetStdDev();
|
2006-11-25 15:46:57 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2006-02-12 15:26:46 +01:00
|
|
|
}
|
2006-02-18 13:01:29 +01:00
|
|
|
|
2008-01-15 21:07:55 +01:00
|
|
|
void CServer::customEvent ( QEvent* Event )
|
2006-02-18 13:01:29 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
if ( Event->type() == QEvent::User + 11 )
|
|
|
|
{
|
|
|
|
const int iMessType = ( ( CLlconEvent* ) Event )->iMessType;
|
|
|
|
|
|
|
|
switch ( iMessType )
|
|
|
|
{
|
|
|
|
case MS_PACKET_RECEIVED:
|
|
|
|
// wake up the server if a packet was received
|
|
|
|
// if the server is still running, the call to Start() will have
|
|
|
|
// no effect
|
|
|
|
Start();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-02-18 13:01:29 +01:00
|
|
|
}
|