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 "client.h"
|
|
|
|
|
|
|
|
|
2006-02-26 11:50:47 +01:00
|
|
|
/* Implementation *************************************************************/
|
2009-02-23 00:13:59 +01:00
|
|
|
CClient::CClient ( const quint16 iPortNumber ) :
|
2009-03-01 12:17:35 +01:00
|
|
|
Channel ( false ), /* we need a client channel -> "false" */
|
2009-02-24 23:56:19 +01:00
|
|
|
Sound ( AudioCallback, this ),
|
2008-08-13 21:08:45 +02:00
|
|
|
Socket ( &Channel, iPortNumber ),
|
2009-02-17 13:32:50 +01:00
|
|
|
iAudioInFader ( AUD_FADER_IN_MIDDLE ),
|
2009-02-12 18:15:17 +01:00
|
|
|
iReverbLevel ( 0 ),
|
2008-03-29 08:14:35 +01:00
|
|
|
bReverbOnLeftChan ( false ),
|
2008-07-24 18:20:25 +02:00
|
|
|
strIPAddress ( "" ), strName ( "" ),
|
2009-02-11 19:37:26 +01:00
|
|
|
bOpenChatOnNewMessage ( true ),
|
2009-03-04 08:57:44 +01:00
|
|
|
bDoAutoSockBufSize ( true ),
|
2009-03-05 21:45:36 +01:00
|
|
|
iSndCrdPreferredMonoBlSizeIndex ( CSndCrdBufferSizes::GetDefaultIndex() ),
|
|
|
|
iSndCrdMonoBlockSizeSam ( 0 )
|
2006-02-26 11:50:47 +01:00
|
|
|
{
|
2006-11-25 15:46:57 +01:00
|
|
|
// connection for protocol
|
2006-12-07 19:57:26 +01:00
|
|
|
QObject::connect ( &Channel,
|
|
|
|
SIGNAL ( MessReadyForSending ( CVector<uint8_t> ) ),
|
2006-11-25 15:46:57 +01:00
|
|
|
this, SLOT ( OnSendProtMessage ( CVector<uint8_t> ) ) );
|
2006-03-07 22:26:40 +01:00
|
|
|
|
2006-11-25 15:46:57 +01:00
|
|
|
QObject::connect ( &Channel, SIGNAL ( ReqJittBufSize() ),
|
|
|
|
this, SLOT ( OnReqJittBufSize() ) );
|
2006-11-05 11:09:32 +01:00
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
QObject::connect ( &Channel,
|
|
|
|
SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ),
|
2006-11-26 22:25:56 +01:00
|
|
|
SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ) );
|
2006-12-09 11:04:27 +01:00
|
|
|
|
|
|
|
QObject::connect ( &Channel, SIGNAL ( NewConnection() ),
|
|
|
|
this, SLOT ( OnNewConnection() ) );
|
2008-07-24 18:20:25 +02:00
|
|
|
|
|
|
|
QObject::connect ( &Channel, SIGNAL ( ChatTextReceived ( QString ) ),
|
|
|
|
this, SIGNAL ( ChatTextReceived ( QString ) ) );
|
2008-08-02 15:42:24 +02:00
|
|
|
|
2008-08-10 23:56:03 +02:00
|
|
|
QObject::connect ( &Channel, SIGNAL ( PingReceived ( int ) ),
|
|
|
|
this, SLOT ( OnReceivePingMessage ( int ) ) );
|
2009-03-12 14:07:43 +01:00
|
|
|
|
|
|
|
QObject::connect ( &Sound, SIGNAL ( ReinitRequest() ),
|
|
|
|
this, SLOT ( OnSndCrdReinitRequest() ) );
|
2006-02-26 11:50:47 +01:00
|
|
|
}
|
|
|
|
|
2006-03-01 20:46:44 +01:00
|
|
|
void CClient::OnSendProtMessage ( CVector<uint8_t> vecMessage )
|
2006-02-26 11:50:47 +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
|
2009-03-28 21:02:21 +01:00
|
|
|
Socket.SendPacket ( vecMessage, Channel.GetAddress() );
|
2006-02-26 11:50:47 +01:00
|
|
|
}
|
2006-03-12 13:24:42 +01:00
|
|
|
|
|
|
|
void CClient::OnReqJittBufSize()
|
|
|
|
{
|
2009-03-01 21:53:12 +01:00
|
|
|
// TODO cant we implement this OnReqJjittBufSize inside the channel object?
|
2006-11-25 15:46:57 +01:00
|
|
|
Channel.CreateJitBufMes ( Channel.GetSockBufSize() );
|
2006-03-12 13:24:42 +01:00
|
|
|
}
|
2006-12-10 12:06:14 +01:00
|
|
|
|
|
|
|
void CClient::OnNewConnection()
|
|
|
|
{
|
|
|
|
// a new connection was successfully initiated, send name and request
|
|
|
|
// connected clients list
|
|
|
|
Channel.SetRemoteName ( strName );
|
2008-08-09 09:57:44 +02:00
|
|
|
|
|
|
|
// We have to send a connected clients list request since it can happen
|
|
|
|
// that we just had connected to the server and then disconnected but
|
|
|
|
// the server still thinks that we are connected (the server is still
|
|
|
|
// waiting for the channel time-out). If we now connect again, we would
|
|
|
|
// not get the list because the server does not know about a new connection.
|
2006-12-10 12:06:14 +01:00
|
|
|
Channel.CreateReqConnClientsList();
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-08-10 23:56:03 +02:00
|
|
|
void CClient::OnReceivePingMessage ( int iMs )
|
2008-08-02 09:28:21 +02:00
|
|
|
{
|
2008-08-11 19:21:09 +02:00
|
|
|
// calculate difference between received time in ms and current time in ms,
|
|
|
|
// take care of wrap arounds (if wrapping, do not use result)
|
|
|
|
const int iCurDiff = PreciseTime.elapsed() - iMs;
|
|
|
|
if ( iCurDiff >= 0 )
|
|
|
|
{
|
|
|
|
emit PingTimeReceived ( iCurDiff );
|
|
|
|
}
|
2008-08-02 09:28:21 +02:00
|
|
|
}
|
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
bool CClient::SetServerAddr ( QString strNAddr )
|
|
|
|
{
|
|
|
|
QHostAddress InetAddr;
|
2009-03-10 18:35:39 +01:00
|
|
|
quint16 iNetPort = LLCON_DEFAULT_PORT_NUMBER;
|
2008-07-22 17:35:58 +02:00
|
|
|
|
|
|
|
// parse input address for the type [IP address]:[port number]
|
|
|
|
QString strPort = strNAddr.section ( ":", 1, 1 );
|
|
|
|
if ( !strPort.isEmpty() )
|
|
|
|
{
|
|
|
|
// a colon is present in the address string, try to extract port number
|
|
|
|
iNetPort = strPort.toInt();
|
|
|
|
|
|
|
|
// extract address port before colon (should be actual internet address)
|
|
|
|
strNAddr = strNAddr.section ( ":", 0, 0 );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2007-02-24 10:49:52 +01:00
|
|
|
// first try if this is an IP number an can directly applied to QHostAddress
|
|
|
|
if ( !InetAddr.setAddress ( strNAddr ) )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2007-02-24 10:49:52 +01:00
|
|
|
// it was no vaild IP address, try to get host by name, assuming
|
|
|
|
// that the string contains a valid host name string
|
2008-01-17 19:56:43 +01:00
|
|
|
QHostInfo HostInfo = QHostInfo::fromName ( strNAddr );
|
2007-02-24 10:49:52 +01:00
|
|
|
|
2008-01-17 19:56:43 +01:00
|
|
|
if ( HostInfo.error() == QHostInfo::NoError )
|
2007-02-24 10:49:52 +01:00
|
|
|
{
|
2008-01-17 19:56:43 +01:00
|
|
|
// apply IP address to QT object
|
|
|
|
if ( !HostInfo.addresses().isEmpty() )
|
|
|
|
{
|
|
|
|
// use the first IP address
|
|
|
|
InetAddr = HostInfo.addresses().first();
|
|
|
|
}
|
2007-02-24 10:49:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false; // invalid address
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2007-02-24 10:49:52 +01:00
|
|
|
|
|
|
|
// apply address (the server port is fixed and always the same)
|
2008-07-22 17:35:58 +02:00
|
|
|
Channel.SetAddress ( CHostAddress ( InetAddr, iNetPort ) );
|
2007-02-24 10:49:52 +01:00
|
|
|
|
|
|
|
return true;
|
2006-11-05 11:09:32 +01:00
|
|
|
}
|
|
|
|
|
2009-03-04 08:57:44 +01:00
|
|
|
void CClient::SetSndCrdPreferredMonoBlSizeIndex ( const int iNewIdx )
|
|
|
|
{
|
2009-03-05 21:07:41 +01:00
|
|
|
// right now we simply set the internal value
|
|
|
|
if ( ( iNewIdx >= 0 ) && ( CSndCrdBufferSizes::GetNumOfBufferSizes() ) )
|
|
|
|
{
|
|
|
|
iSndCrdPreferredMonoBlSizeIndex = iNewIdx;
|
|
|
|
}
|
2009-03-04 08:57:44 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
// init with new parameter, if client was running then first
|
|
|
|
// stop it and restart again after new initialization
|
|
|
|
const bool bWasRunning = Sound.IsRunning();
|
|
|
|
if ( bWasRunning )
|
|
|
|
{
|
|
|
|
Sound.Stop();
|
|
|
|
}
|
2009-03-04 08:57:44 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
// init with new block size index parameter
|
|
|
|
Init ( iSndCrdPreferredMonoBlSizeIndex );
|
2009-03-04 08:57:44 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
if ( bWasRunning )
|
|
|
|
{
|
|
|
|
Sound.Start();
|
|
|
|
}
|
2009-03-04 08:57:44 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
// tell the server that audio coding has changed (it
|
|
|
|
// is important to call this function AFTER we have applied
|
|
|
|
// the new setting to the channel!)
|
|
|
|
Channel.CreateNetTranspPropsMessFromCurrentSettings();
|
|
|
|
}
|
2009-03-04 08:57:44 +01:00
|
|
|
|
2009-03-08 18:14:37 +01:00
|
|
|
QString CClient::SetSndCrdDev ( const int iNewDev )
|
2009-03-07 21:45:00 +01:00
|
|
|
{
|
|
|
|
// if client was running then first
|
|
|
|
// stop it and restart again after new initialization
|
|
|
|
const bool bWasRunning = Sound.IsRunning();
|
|
|
|
if ( bWasRunning )
|
|
|
|
{
|
|
|
|
Sound.Stop();
|
|
|
|
}
|
|
|
|
|
2009-03-08 18:14:37 +01:00
|
|
|
const QString strReturn = Sound.SetDev ( iNewDev ).c_str();
|
2009-03-07 21:45:00 +01:00
|
|
|
|
|
|
|
// init again because the sound card actual buffer size might
|
|
|
|
// be changed on new device
|
|
|
|
Init ( iSndCrdPreferredMonoBlSizeIndex );
|
|
|
|
|
|
|
|
if ( bWasRunning )
|
|
|
|
{
|
|
|
|
Sound.Start();
|
|
|
|
}
|
2009-03-08 18:14:37 +01:00
|
|
|
|
|
|
|
return strReturn;
|
2009-03-07 21:45:00 +01:00
|
|
|
}
|
|
|
|
|
2009-03-12 14:07:43 +01:00
|
|
|
void CClient::OnSndCrdReinitRequest()
|
|
|
|
{
|
|
|
|
// if client was running then first
|
|
|
|
// stop it and restart again after new initialization
|
|
|
|
const bool bWasRunning = Sound.IsRunning();
|
|
|
|
if ( bWasRunning )
|
|
|
|
{
|
|
|
|
Sound.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reinit the driver (we use the currently selected driver) and
|
|
|
|
// init client object, too
|
|
|
|
Sound.SetDev ( Sound.GetDev() );
|
|
|
|
Init ( iSndCrdPreferredMonoBlSizeIndex );
|
|
|
|
|
|
|
|
if ( bWasRunning )
|
|
|
|
{
|
|
|
|
Sound.Start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
void CClient::Start()
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
2009-02-21 18:37:15 +01:00
|
|
|
// init object
|
2009-03-05 21:07:41 +01:00
|
|
|
Init ( iSndCrdPreferredMonoBlSizeIndex );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
// enable channel
|
|
|
|
Channel.SetEnable ( true );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2009-02-23 00:13:59 +01:00
|
|
|
// start audio interface
|
|
|
|
Sound.Start();
|
2009-02-21 18:37:15 +01:00
|
|
|
}
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
void CClient::Stop()
|
|
|
|
{
|
2009-02-23 00:13:59 +01:00
|
|
|
// stop audio interface
|
|
|
|
Sound.Stop();
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2009-02-23 21:13:03 +01:00
|
|
|
// disable channel
|
|
|
|
Channel.SetEnable ( false );
|
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
// reset current signal level and LEDs
|
|
|
|
SignalLevelMeter.Reset();
|
|
|
|
PostWinMessage ( MS_RESET_ALL, 0 );
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2009-02-23 00:13:59 +01:00
|
|
|
void CClient::AudioCallback ( CVector<short>& psData, void* arg )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2009-02-23 00:13:59 +01:00
|
|
|
// get the pointer to the object
|
|
|
|
CClient* pMyClientObj = reinterpret_cast<CClient*> ( arg );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2009-02-23 00:13:59 +01:00
|
|
|
// process audio data
|
|
|
|
pMyClientObj->ProcessAudioData ( psData );
|
2009-02-21 18:37:15 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2009-03-05 21:07:41 +01:00
|
|
|
void CClient::Init ( const int iPrefMonoBlockSizeSamIndexAtSndCrdSamRate )
|
2009-02-21 18:37:15 +01:00
|
|
|
{
|
2009-03-05 21:07:41 +01:00
|
|
|
// translate block size index in actual block size
|
|
|
|
const int iPrefMonoBlockSizeSamAtSndCrdSamRate = CSndCrdBufferSizes::
|
|
|
|
GetBufferSizeFromIndex ( iPrefMonoBlockSizeSamIndexAtSndCrdSamRate );
|
|
|
|
|
2009-03-01 23:08:06 +01:00
|
|
|
// get actual sound card buffer size using preferred size
|
|
|
|
iSndCrdMonoBlockSizeSam = Sound.Init ( iPrefMonoBlockSizeSamAtSndCrdSamRate );
|
|
|
|
iSndCrdStereoBlockSizeSam = 2 * iSndCrdMonoBlockSizeSam;
|
2009-03-01 12:17:35 +01:00
|
|
|
|
2009-03-01 23:08:06 +01:00
|
|
|
iMonoBlockSizeSam = iSndCrdMonoBlockSizeSam * SYSTEM_SAMPLE_RATE / SND_CRD_SAMPLE_RATE;
|
2009-02-24 23:56:19 +01:00
|
|
|
iStereoBlockSizeSam = 2 * iMonoBlockSizeSam;
|
|
|
|
|
2009-03-03 19:04:51 +01:00
|
|
|
// the channel works on the same block size as the sound interface
|
|
|
|
Channel.SetNetwBufSizeOut ( iMonoBlockSizeSam );
|
2009-02-21 18:37:15 +01:00
|
|
|
|
|
|
|
vecsAudioSndCrdStereo.Init ( iSndCrdStereoBlockSizeSam );
|
|
|
|
vecdAudioSndCrdMono.Init ( iSndCrdMonoBlockSizeSam );
|
|
|
|
vecdAudioSndCrdStereo.Init ( iSndCrdStereoBlockSizeSam );
|
|
|
|
|
|
|
|
vecdAudioStereo.Init ( iStereoBlockSizeSam );
|
|
|
|
|
|
|
|
// resample objects are always initialized with the input block size
|
|
|
|
// record
|
|
|
|
ResampleObjDown.Init ( iSndCrdMonoBlockSizeSam, SND_CRD_SAMPLE_RATE, SYSTEM_SAMPLE_RATE );
|
|
|
|
|
|
|
|
// playback
|
|
|
|
ResampleObjUp.Init ( iMonoBlockSizeSam, SYSTEM_SAMPLE_RATE, SND_CRD_SAMPLE_RATE );
|
|
|
|
|
|
|
|
// init network buffers
|
|
|
|
vecsNetwork.Init ( iMonoBlockSizeSam );
|
|
|
|
vecdNetwData.Init ( iMonoBlockSizeSam );
|
|
|
|
|
2009-03-07 11:52:06 +01:00
|
|
|
// init response time evaluation
|
2009-03-17 07:50:00 +01:00
|
|
|
CycleTimeVariance.Init ( iMonoBlockSizeSam, TIME_MOV_AV_RESPONSE );
|
2009-03-08 10:01:01 +01:00
|
|
|
|
2009-03-07 11:52:06 +01:00
|
|
|
CycleTimeVariance.Reset();
|
2009-02-21 18:37:15 +01:00
|
|
|
|
|
|
|
AudioReverb.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CClient::ProcessAudioData ( CVector<short>& vecsStereoSndCrd )
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
// convert data from short to double
|
|
|
|
for ( i = 0; i < iSndCrdStereoBlockSizeSam; i++ )
|
|
|
|
{
|
|
|
|
vecdAudioSndCrdStereo[i] = (double) vecsStereoSndCrd[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// resample data for each channel seaparately
|
|
|
|
ResampleObjDown.ResampleStereo ( vecdAudioSndCrdStereo, vecdAudioStereo );
|
|
|
|
|
|
|
|
// update stereo signal level meter
|
|
|
|
SignalLevelMeter.Update ( vecdAudioStereo );
|
|
|
|
|
|
|
|
// add reverberation effect if activated
|
|
|
|
if ( iReverbLevel != 0 )
|
|
|
|
{
|
|
|
|
// calculate attenuation amplification factor
|
|
|
|
const double dRevLev = (double) iReverbLevel / AUD_REVERB_MAX / 2;
|
|
|
|
|
|
|
|
if ( bReverbOnLeftChan )
|
|
|
|
{
|
|
|
|
for ( i = 0; i < iStereoBlockSizeSam; i += 2 )
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
2009-02-21 18:37:15 +01:00
|
|
|
// left channel
|
|
|
|
vecdAudioStereo[i] +=
|
|
|
|
dRevLev * AudioReverb.ProcessSample ( vecdAudioStereo[i] );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
2009-02-21 18:37:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( i = 1; i < iStereoBlockSizeSam; i += 2 )
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
2009-02-21 18:37:15 +01:00
|
|
|
// right channel
|
|
|
|
vecdAudioStereo[i] +=
|
|
|
|
dRevLev * AudioReverb.ProcessSample ( vecdAudioStereo[i] );
|
2006-11-25 15:46:57 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-21 18:37:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mix both signals depending on the fading setting, convert
|
|
|
|
// from double to short
|
|
|
|
if ( iAudioInFader == AUD_FADER_IN_MIDDLE )
|
|
|
|
{
|
|
|
|
// just mix channels together
|
|
|
|
for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 )
|
|
|
|
{
|
|
|
|
vecsNetwork[i] =
|
|
|
|
Double2Short ( vecdAudioStereo[j] + vecdAudioStereo[j + 1] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const double dAttFact =
|
|
|
|
(double) ( AUD_FADER_IN_MIDDLE - abs ( AUD_FADER_IN_MIDDLE - iAudioInFader ) ) /
|
|
|
|
AUD_FADER_IN_MIDDLE;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
if ( iAudioInFader > AUD_FADER_IN_MIDDLE )
|
2006-11-25 15:46:57 +01:00
|
|
|
{
|
2009-02-14 01:46:58 +01:00
|
|
|
for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 )
|
2006-12-07 19:57:26 +01:00
|
|
|
{
|
2009-02-21 18:37:15 +01:00
|
|
|
// attenuation on right channel
|
2009-02-14 01:46:58 +01:00
|
|
|
vecsNetwork[i] =
|
2009-02-21 18:37:15 +01:00
|
|
|
Double2Short ( vecdAudioStereo[j] + dAttFact * vecdAudioStereo[j + 1] );
|
2006-12-07 19:57:26 +01:00
|
|
|
}
|
2009-02-14 01:46:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-21 18:37:15 +01:00
|
|
|
for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 )
|
2009-02-17 13:32:50 +01:00
|
|
|
{
|
2009-02-21 18:37:15 +01:00
|
|
|
// attenuation on left channel
|
|
|
|
vecsNetwork[i] =
|
|
|
|
Double2Short ( vecdAudioStereo[j + 1] + dAttFact * vecdAudioStereo[j] );
|
2006-12-07 19:57:26 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2009-02-21 18:37:15 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
// send it through the network
|
|
|
|
Socket.SendPacket ( Channel.PrepSendPacket ( vecsNetwork ),
|
|
|
|
Channel.GetAddress() );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
// receive a new block
|
|
|
|
if ( Channel.GetData ( vecdNetwData ) == GS_BUFFER_OK )
|
|
|
|
{
|
|
|
|
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_GREEN );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_RED );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2006-01-28 12:29:22 +01:00
|
|
|
/*
|
2009-02-08 23:44:18 +01:00
|
|
|
// TEST
|
2006-01-28 12:29:22 +01:00
|
|
|
// fid=fopen('v.dat','r');x=fread(fid,'int16');fclose(fid);
|
|
|
|
static FILE* pFileDelay = fopen("v.dat", "wb");
|
|
|
|
short sData[2];
|
2009-02-14 01:46:58 +01:00
|
|
|
for (i = 0; i < iMonoBlockSizeSam; i++)
|
2006-01-28 12:29:22 +01:00
|
|
|
{
|
2009-02-21 18:37:15 +01:00
|
|
|
sData[0] = (short) vecdNetwData[i];
|
|
|
|
fwrite(&sData, size_t(2), size_t(1), pFileDelay);
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
|
|
|
fflush(pFileDelay);
|
|
|
|
*/
|
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
// check if channel is connected
|
|
|
|
if ( Channel.IsConnected() )
|
|
|
|
{
|
|
|
|
// resample data
|
|
|
|
ResampleObjUp.ResampleMono ( vecdNetwData, vecdAudioSndCrdMono );
|
2009-02-17 12:58:27 +01:00
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
// convert data from double to short type and copy mono
|
|
|
|
// received data in both sound card channels
|
|
|
|
for ( i = 0, j = 0; i < iSndCrdMonoBlockSizeSam; i++, j += 2 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2009-02-21 18:37:15 +01:00
|
|
|
vecsStereoSndCrd[j] = vecsStereoSndCrd[j + 1] =
|
|
|
|
Double2Short ( vecdAudioSndCrdMono[i] );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2009-02-21 18:37:15 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if not connected, clear data
|
|
|
|
vecsStereoSndCrd.Reset ( 0 );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
2006-12-09 11:04:27 +01:00
|
|
|
|
2009-02-21 18:37:15 +01:00
|
|
|
// update response time measurement and socket buffer size
|
2009-03-07 11:52:06 +01:00
|
|
|
CycleTimeVariance.Update();
|
2009-02-21 18:37:15 +01:00
|
|
|
UpdateSocketBufferSize();
|
2006-02-26 11:50:47 +01:00
|
|
|
}
|
2009-02-11 19:37:26 +01:00
|
|
|
|
|
|
|
void CClient::UpdateSocketBufferSize()
|
2009-02-12 15:48:35 +01:00
|
|
|
{
|
2009-02-11 19:37:26 +01:00
|
|
|
// just update the socket buffer size if auto setting is enabled, otherwise
|
2009-02-11 19:45:22 +01:00
|
|
|
// do nothing
|
|
|
|
if ( bDoAutoSockBufSize )
|
2009-02-11 19:37:26 +01:00
|
|
|
{
|
2009-03-04 22:11:48 +01:00
|
|
|
// We use the time response measurement for the automatic setting.
|
2009-02-11 19:37:26 +01:00
|
|
|
// Assumptions:
|
2009-03-18 08:24:53 +01:00
|
|
|
// - the audio interface/network jitter is assumed to be Gaussian
|
2009-03-19 18:45:43 +01:00
|
|
|
// - the buffer size is set to 3.3 times the standard deviation of
|
|
|
|
// the jitter (~98% of the jitter should be fit in the
|
2009-02-11 19:37:26 +01:00
|
|
|
// buffer)
|
|
|
|
// - introduce a hysteresis to avoid switching the buffer sizes all the
|
|
|
|
// time in case the time response measurement is close to a bound
|
|
|
|
// - only use time response measurement results if averaging buffer is
|
|
|
|
// completely filled
|
|
|
|
const double dHysteresis = 0.3;
|
|
|
|
|
2009-03-10 11:47:55 +01:00
|
|
|
// calculate current buffer setting
|
|
|
|
// Use worst case scenario: We add the block size of input and
|
|
|
|
// output. This is not required if the smaller block size is a
|
|
|
|
// multiple of the bigger size, but in the general case where
|
|
|
|
// the block sizes do not have this relation, we require to have
|
|
|
|
// a minimum buffer size of the sum of both sizes
|
|
|
|
const double dAudioBufferDurationMs =
|
|
|
|
( iMonoBlockSizeSam + Channel.GetAudioBlockSizeIn() ) * 1000 /
|
|
|
|
SYSTEM_SAMPLE_RATE;
|
|
|
|
|
2009-03-17 07:50:00 +01:00
|
|
|
// accumulate the standard deviations of input network stream and
|
|
|
|
// internal timer,
|
2009-03-19 07:53:05 +01:00
|
|
|
// add 0.5 to "round up" -> ceil,
|
2009-03-17 07:50:00 +01:00
|
|
|
// divide by MIN_SERVER_BLOCK_DURATION_MS because this is the size of
|
|
|
|
// one block in the jitter buffer
|
|
|
|
const double dEstCurBufSet = ( dAudioBufferDurationMs +
|
2009-03-19 18:45:43 +01:00
|
|
|
3.3 * ( Channel.GetTimingStdDev() + CycleTimeVariance.GetStdDev() ) ) /
|
2009-03-19 07:53:05 +01:00
|
|
|
MIN_SERVER_BLOCK_DURATION_MS + 0.5;
|
2009-03-10 11:47:55 +01:00
|
|
|
|
|
|
|
// upper/lower hysteresis decision
|
|
|
|
const int iUpperHystDec = LlconMath().round ( dEstCurBufSet - dHysteresis );
|
|
|
|
const int iLowerHystDec = LlconMath().round ( dEstCurBufSet + dHysteresis );
|
|
|
|
|
|
|
|
// if both decisions are equal than use the result
|
|
|
|
if ( iUpperHystDec == iLowerHystDec )
|
|
|
|
{
|
|
|
|
// set the socket buffer via the main window thread since somehow
|
|
|
|
// it gives a protocol deadlock if we call the SetSocketBufSize()
|
|
|
|
// function directly
|
|
|
|
PostWinMessage ( MS_SET_JIT_BUF_SIZE, iUpperHystDec );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we are in the middle of the decision region, use
|
|
|
|
// previous setting for determing the new decision
|
|
|
|
if ( !( ( GetSockBufSize() == iUpperHystDec ) ||
|
|
|
|
( GetSockBufSize() == iLowerHystDec ) ) )
|
2009-02-11 19:45:22 +01:00
|
|
|
{
|
2009-03-10 11:47:55 +01:00
|
|
|
// The old result is not near the new decision,
|
|
|
|
// use per definition the upper decision.
|
|
|
|
// Set the socket buffer via the main window thread since somehow
|
2009-02-13 00:06:41 +01:00
|
|
|
// it gives a protocol deadlock if we call the SetSocketBufSize()
|
2009-03-10 11:47:55 +01:00
|
|
|
// function directly.
|
2009-02-13 00:06:41 +01:00
|
|
|
PostWinMessage ( MS_SET_JIT_BUF_SIZE, iUpperHystDec );
|
2009-02-11 19:37:26 +01:00
|
|
|
}
|
2009-03-10 11:47:55 +01:00
|
|
|
}
|
2009-02-11 19:37:26 +01:00
|
|
|
}
|
|
|
|
}
|