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,
|
2008-08-19 21:59:59 +02:00
|
|
|
const QString& strServerNameForHTMLStatusFile,
|
2009-03-10 13:07:08 +01:00
|
|
|
const int iNewUploadRateLimitKbps ) :
|
2008-08-19 21:59:59 +02:00
|
|
|
Socket ( &ChannelSet, this, iPortNumber ),
|
2009-03-10 13:07:08 +01:00
|
|
|
ChannelSet ( iNewUploadRateLimitKbps )
|
2006-09-03 11:49:15 +02:00
|
|
|
{
|
2009-03-01 12:17:35 +01:00
|
|
|
vecsSendData.Init ( MIN_SERVER_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-03-08 10:01:01 +01:00
|
|
|
CycleTimeVariance.Init ( LEN_MOV_AV_RESPONSE_SERVER );
|
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
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
// connection for protocol
|
|
|
|
QObject::connect ( &ChannelSet,
|
|
|
|
SIGNAL ( MessReadyForSending ( int, CVector<uint8_t> ) ),
|
|
|
|
this, SLOT ( OnSendProtMessage ( int, CVector<uint8_t> ) ) );
|
2006-03-04 12:11:26 +01:00
|
|
|
|
2008-12-02 19:02:40 +01:00
|
|
|
// connection for logging
|
|
|
|
QObject::connect ( &ChannelSet,
|
|
|
|
SIGNAL ( ChannelConnected ( CHostAddress ) ),
|
|
|
|
this, SLOT ( OnNewChannel ( CHostAddress ) ) );
|
|
|
|
|
2006-02-20 22:09:36 +01:00
|
|
|
#ifdef _WIN32
|
2006-11-25 15:46:57 +01:00
|
|
|
// event handling of custom events seems not to work under Windows in this
|
|
|
|
// class, do not use automatic start/stop of server in Windows version
|
|
|
|
Start();
|
2006-12-18 15:39:33 +01:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
// 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)
|
|
|
|
ChannelSet.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
|
|
|
}
|
2006-03-04 12:11:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::OnSendProtMessage ( int iChID, CVector<uint8_t> vecMessage )
|
|
|
|
{
|
|
|
|
|
|
|
|
// convert unsigned uint8_t in char, TODO convert all buffers in uint8_t
|
2006-10-30 21:35:46 +01:00
|
|
|
CVector<unsigned char> vecbyDataConv ( vecMessage.Size() );
|
2006-03-04 12:11:26 +01:00
|
|
|
for ( int i = 0; i < vecMessage.Size (); i++ ) {
|
2006-11-25 15:46:57 +01:00
|
|
|
vecbyDataConv[i] = static_cast<unsigned char> ( vecMessage[i] );
|
2006-03-04 12:11:26 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
Socket.SendPacket ( vecbyDataConv, ChannelSet.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-03-01 12:17:35 +01:00
|
|
|
Timer.start ( MIN_SERVER_BLOCK_DURATION_MS );
|
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-03-07 21:45:00 +01:00
|
|
|
const QString strLogStr = CLogTimeDate::toString() + ",, server stopped "
|
|
|
|
"-------------------------------------";
|
2008-12-02 19:02:40 +01:00
|
|
|
|
2009-03-08 08:26:01 +01:00
|
|
|
QTextStream tsConsoloeStream ( stdout );
|
|
|
|
tsConsoloeStream << strLogStr << endl; // on console
|
|
|
|
Logging << strLogStr; // in log file
|
2008-12-02 19:02:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CServer::OnNewChannel ( CHostAddress ChanAddr )
|
|
|
|
{
|
|
|
|
// logging of new connected channel
|
2009-03-07 21:45:00 +01:00
|
|
|
const QString strLogStr = CLogTimeDate::toString() + ", " +
|
|
|
|
ChanAddr.InetAddr.toString() + ", connected";
|
2007-05-06 14:27:41 +02:00
|
|
|
|
2009-03-08 08:26:01 +01:00
|
|
|
QTextStream tsConsoloeStream ( stdout );
|
|
|
|
tsConsoloeStream << strLogStr << endl; // on console
|
|
|
|
Logging << strLogStr; // in log file
|
2006-02-12 15:26:46 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
void CServer::OnTimer()
|
|
|
|
{
|
|
|
|
CVector<int> vecChanID;
|
2009-03-01 12:17:35 +01:00
|
|
|
CVector<CVector<double> > vecvecdData ( MIN_SERVER_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
|
2006-12-06 21:22:41 +01:00
|
|
|
ChannelSet.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 (
|
2006-12-18 15:39:33 +01:00
|
|
|
ChannelSet.PrepSendPacket ( vecChanID[i], vecsSendData ),
|
|
|
|
ChannelSet.GetAddress ( vecChanID[i] ) );
|
|
|
|
}
|
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.
|
2006-02-20 22:09:36 +01:00
|
|
|
#ifndef _WIN32
|
2006-11-25 15:46:57 +01:00
|
|
|
// event handling of custom events seems not to work under Windows in this
|
|
|
|
// class, do not use automatic start/stop of server in Windows version
|
|
|
|
Stop();
|
2006-02-20 22:09:36 +01:00
|
|
|
#endif
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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-03-01 12:17:35 +01:00
|
|
|
CVector<short> vecsOutData ( MIN_SERVER_BLOCK_SIZE_SAMPLES );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
const int iNumClients = vecvecdData.Size();
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2006-11-27 23:35:22 +01:00
|
|
|
// 3 dB offset to avoid overload if all clients are set to gain 1
|
2006-12-18 15:39:33 +01:00
|
|
|
const double dNorm = (double) 2.0;
|
|
|
|
|
|
|
|
// mix all audio data from all clients together
|
2009-03-01 12:17:35 +01:00
|
|
|
for ( int i = 0; i < MIN_SERVER_BLOCK_SIZE_SAMPLES; i++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
double dMixedData = 0.0;
|
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
for ( int j = 0; j < iNumClients; j++ )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
dMixedData += vecvecdData[j][i] * vecdGains[j];
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// normalization and truncating to short
|
|
|
|
vecsOutData[i] = Double2Short ( dMixedData / dNorm );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|