Extending ita_whad for ASIO

rebuilding NetAudio protocol and classes for simple send/receive without answer
Improving ITAsioInterface
parent c6835c7b
...@@ -32,7 +32,7 @@ int main( int, char** ) ...@@ -32,7 +32,7 @@ int main( int, char** )
#ifdef ITA_WHAD_WITH_ASIO #ifdef ITA_WHAD_WITH_ASIO
string sASIOFileName = "ita_whad_asio.txt"; string sASIOFileName = "ita_whad_asio.txt";
FILE* PASIOFile = fopen( sASIOFileName.c_str(), "w" ); FILE* pASIOFile = fopen( sASIOFileName.c_str(), "w" );
ITAsioInitializeLibrary(); ITAsioInitializeLibrary();
...@@ -44,13 +44,23 @@ int main( int, char** ) ...@@ -44,13 +44,23 @@ int main( int, char** )
cout << " ### ASIO ### " << endl; cout << " ### ASIO ### " << endl;
for( long i = 0; i < lASIODrivers; i++ ) for( long i = 0; i < lASIODrivers; i++ )
{ {
cout << "[" << i + 1 << "] \"" << ITAsioGetDriverName( i ) << "\"" << endl; std::string sDriverName = ITAsioGetDriverName( i );
ASIOError ae = ITAsioInitializeDriver( i );
long iIn = -1, iOut = -1;
if( ae == ASE_OK )
{
ITAsioGetChannels( &iIn, &iOut );
ITAsioFinalizeDriver();
}
cout << "[" << i + 1 << "] \"" << sDriverName << "\" (" << iIn << " in, " << iOut << " out)" << endl;
} }
cout << endl; cout << endl;
ITAsioFinalizeLibrary(); ITAsioFinalizeLibrary();
fclose( PASIOFile ); fclose( pASIOFile );
#endif // ITA_WHAD_WITH_ASIO #endif // ITA_WHAD_WITH_ASIO
#ifdef ITA_WHAD_WITH_PORTAUDIO #ifdef ITA_WHAD_WITH_PORTAUDIO
...@@ -79,7 +89,7 @@ int main( int, char** ) ...@@ -79,7 +89,7 @@ int main( int, char** )
fclose( pPAFile ); fclose( pPAFile );
#endif // ITA_WHAD_WITH:PORTAUDIO #endif // ITA_WHAD_WITH_PORTAUDIO
return 0; return 0;
} }
...@@ -48,16 +48,13 @@ class VistaConnectionIP; ...@@ -48,16 +48,13 @@ class VistaConnectionIP;
class ITA_DATA_SOURCES_API CITANetAudioMessage class ITA_DATA_SOURCES_API CITANetAudioMessage
{ {
public: public:
CITANetAudioMessage( VistaSerializingToolset::ByteOrderSwapBehavior bSwapBuffers ); CITANetAudioMessage( VistaConnectionIP* );
void SetConnection( VistaConnectionIP* );
VistaConnectionIP* GetConnection() const; VistaConnectionIP* GetConnection() const;
void ClearConnection(); void ClearConnection();
void WriteMessage(); void WriteMessage();
void ReadMessage(); bool TryReadMessage();
void WriteAnswer();
void ReadAnswer();
void ResetMessage(); void ResetMessage();
...@@ -66,10 +63,7 @@ public: ...@@ -66,10 +63,7 @@ public:
bool GetOutgoingMessageHasData() const; bool GetOutgoingMessageHasData() const;
void SetMessageType( int nType ); void SetMessageType( int nType );
void SetAnswerType( int nType );
int GetMessageType() const; int GetMessageType() const;
int GetAnswerType() const;
void WriteInt( const int ); void WriteInt( const int );
void WriteBool( const bool ); void WriteBool( const bool );
...@@ -100,11 +94,11 @@ public: ...@@ -100,11 +94,11 @@ public:
private: private:
int m_nMessageType; int m_nMessageType;
int m_nMessageId; int m_nMessageId;
int m_nAnswerType;
VistaByteBufferSerializer m_oOutgoing; //!< Serialization buffer for messages VistaByteBufferSerializer m_oOutgoing; //!< Serialization buffer for messages
VistaByteBufferDeSerializer m_oIncoming; //!< Deserialization buffer for messages VistaByteBufferDeSerializer m_oIncoming; //!< Deserialization buffer for messages
std::vector< VistaType::byte > m_vecIncomingBuffer; // Net IO buffer std::vector< VistaType::byte > m_vecIncomingBuffer; // Net IO buffer
int m_nTimeoutMilliseconds; //!< Timeout for try-read message
VistaConnectionIP* m_pConnection; VistaConnectionIP* m_pConnection;
}; };
......
...@@ -60,7 +60,6 @@ public: ...@@ -60,7 +60,6 @@ public:
static const int NP_SERVER_OPEN = 201; static const int NP_SERVER_OPEN = 201;
static const int NP_SERVER_GET_RINGBUFFER_SIZE = 210; static const int NP_SERVER_GET_RINGBUFFER_SIZE = 210;
static const int NP_SERVER_GET_RINGBUFFER_FREE = 211; static const int NP_SERVER_GET_RINGBUFFER_FREE = 211;
static const int NP_SERVER_WAITING_FOR_TRIGGER = 221;
static const int NP_SERVER_SEND_SAMPLES = 222; static const int NP_SERVER_SEND_SAMPLES = 222;
inline CITANetAudioProtocol() {}; inline CITANetAudioProtocol() {};
......
...@@ -67,7 +67,9 @@ private: ...@@ -67,7 +67,9 @@ private:
CITANetAudioStream* m_pStream; CITANetAudioStream* m_pStream;
CITANetAudioProtocol* m_pProtocol; CITANetAudioProtocol* m_pProtocol;
CITANetAudioMessage* m_pMessage; CITANetAudioMessage* m_pIncomingMessage;
CITANetAudioMessage* m_pOutgoingMessage;
VistaConnectionIP* m_pConnection; VistaConnectionIP* m_pConnection;
VistaThreadEvent m_oBlockIncrementEvent; VistaThreadEvent m_oBlockIncrementEvent;
......
...@@ -86,7 +86,8 @@ private: ...@@ -86,7 +86,8 @@ private:
VistaConnectionIP* m_pConnection; VistaConnectionIP* m_pConnection;
CITANetAudioProtocol::StreamingParameters m_oServerParams; CITANetAudioProtocol::StreamingParameters m_oServerParams;
CITANetAudioMessage* m_pMessage; CITANetAudioMessage* m_pIncomingMessage;
CITANetAudioMessage* m_pOutgoingMessage;
int m_iUpdateStrategy; int m_iUpdateStrategy;
int m_iClientRingBufferFreeSamples; int m_iClientRingBufferFreeSamples;
......
...@@ -693,8 +693,17 @@ ITASIO_API ASIOError ITAsioInitializeDriver(const char* pszDriverName) { ...@@ -693,8 +693,17 @@ ITASIO_API ASIOError ITAsioInitializeDriver(const char* pszDriverName) {
// Mutex in Besitz bringen // Mutex in Besitz bringen
EnterCriticalSection(&csExternal); EnterCriticalSection(&csExternal);
// Load the driver, this will setup all the necessary internal data structures // Load the driver, this will setup all the necessary internal data structures
asioDrivers->loadDriver((char*) pszDriverName); try
{
bool bLoadSuccess = asioDrivers->loadDriver( ( char* ) pszDriverName );
if( !bLoadSuccess )
return ASE_NotPresent;
}
catch( ... )
{
return ASE_NotPresent;
}
ASIOError aeResult = ASIOInit(&asioDriverInfo.driverInfo); ASIOError aeResult = ASIOInit(&asioDriverInfo.driverInfo);
if (aeResult != ASE_OK) { if (aeResult != ASE_OK) {
......
...@@ -13,13 +13,14 @@ ...@@ -13,13 +13,14 @@
static int S_nMessageIds = 0; static int S_nMessageIds = 0;
CITANetAudioMessage::CITANetAudioMessage( VistaSerializingToolset::ByteOrderSwapBehavior bSwapBuffers ) CITANetAudioMessage::CITANetAudioMessage( VistaConnectionIP* pConnection )
: m_vecIncomingBuffer( 2048 ) : m_vecIncomingBuffer( 2048 )
, m_oOutgoing( 2048 ) , m_oOutgoing( 2048 )
, m_pConnection( NULL ) , m_pConnection( pConnection )
, m_nTimeoutMilliseconds( 1 )
{ {
m_oOutgoing.SetByteorderSwapFlag( bSwapBuffers ); m_oOutgoing.SetByteorderSwapFlag( pConnection->GetByteorderSwapFlag() );
m_oIncoming.SetByteorderSwapFlag( bSwapBuffers ); m_oIncoming.SetByteorderSwapFlag( pConnection->GetByteorderSwapFlag() );
ResetMessage(); ResetMessage();
} }
...@@ -44,20 +45,12 @@ void CITANetAudioMessage::ResetMessage() ...@@ -44,20 +45,12 @@ void CITANetAudioMessage::ResetMessage()
m_oIncoming.SetBuffer( NULL, 0 ); m_oIncoming.SetBuffer( NULL, 0 );
m_nMessageType = CITANetAudioProtocol::NP_INVALID; m_nMessageType = CITANetAudioProtocol::NP_INVALID;
m_nAnswerType = CITANetAudioProtocol::NP_INVALID;
m_pConnection = NULL;
#if NET_AUDIO_SHOW_TRAFFIC #if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "CITANetAudioMessage [Preparing] (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl; vstr::out() << "CITANetAudioMessage [Preparing] (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
#endif #endif
} }
void CITANetAudioMessage::SetConnection( VistaConnectionIP* pConn )
{
m_pConnection = pConn;
}
void CITANetAudioMessage::WriteMessage() void CITANetAudioMessage::WriteMessage()
{ {
VistaType::byte* pBuffer = ( VistaType::byte* ) m_oOutgoing.GetBuffer(); VistaType::byte* pBuffer = ( VistaType::byte* ) m_oOutgoing.GetBuffer();
...@@ -93,6 +86,7 @@ void CITANetAudioMessage::WriteMessage() ...@@ -93,6 +86,7 @@ void CITANetAudioMessage::WriteMessage()
{ {
// It appears safe to send even very big data payload, so we will send at once // It appears safe to send even very big data payload, so we will send at once
int iRawBufferSize = m_oOutgoing.GetBufferSize(); int iRawBufferSize = m_oOutgoing.GetBufferSize();
assert( iRawBufferSize > 4 );
int nRet = m_pConnection->Send( m_oOutgoing.GetBuffer(), iRawBufferSize ); int nRet = m_pConnection->Send( m_oOutgoing.GetBuffer(), iRawBufferSize );
#if NET_AUDIO_SHOW_TRAFFIC #if NET_AUDIO_SHOW_TRAFFIC
...@@ -103,19 +97,26 @@ void CITANetAudioMessage::WriteMessage() ...@@ -103,19 +97,26 @@ void CITANetAudioMessage::WriteMessage()
if( nRet != m_oOutgoing.GetBufferSize() ) if( nRet != m_oOutgoing.GetBufferSize() )
VISTA_THROW( "ITANetAudioMessage: could not send all data from output buffer via network connection", 255 ); VISTA_THROW( "ITANetAudioMessage: could not send all data from output buffer via network connection", 255 );
} }
catch (VistaExceptionBase& ex) catch( VistaExceptionBase& ex )
{ {
ITA_EXCEPT1( NETWORK_ERROR, ex.GetExceptionText() ); ITA_EXCEPT1( NETWORK_ERROR, ex.GetExceptionText() );
} }
} }
bool CITANetAudioMessage::TryReadMessage()
void CITANetAudioMessage::ReadMessage()
{ {
#if NET_AUDIO_SHOW_TRAFFIC #if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "CITANetAudioMessage [ Reading ] Waiting for incoming data" << std::endl; vstr::out() << "CITANetAudioMessage [ TryRead ] Waiting for incoming data for " << m_nTimeoutMilliseconds << std::endl;
#endif #endif
long nIncomingBytes = m_pConnection->WaitForIncomingData( 0 ); long nIncomingBytes = m_pConnection->WaitForIncomingData( m_nTimeoutMilliseconds );
if( nIncomingBytes <= 0 )
{
#if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "CITANetAudioMessage [ TryRead ] nothing incoming" << std::endl;
#endif
return false;
}
assert( nIncomingBytes >= 4 ); // we need at least the size of message assert( nIncomingBytes >= 4 ); // we need at least the size of message
#if NET_AUDIO_SHOW_TRAFFIC #if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "CITANetAudioMessage [ Reading ] " << nIncomingBytes << " bytes incoming" << std::endl; vstr::out() << "CITANetAudioMessage [ Reading ] " << nIncomingBytes << " bytes incoming" << std::endl;
...@@ -128,14 +129,14 @@ void CITANetAudioMessage::ReadMessage() ...@@ -128,14 +129,14 @@ void CITANetAudioMessage::ReadMessage()
vstr::out() << "CITANetAudioMessage [ Reading ] Expecting " << nMessagePayloadSize << " bytes message payload" << std::endl; vstr::out() << "CITANetAudioMessage [ Reading ] Expecting " << nMessagePayloadSize << " bytes message payload" << std::endl;
#endif #endif
// we need at least the two protocol ints // we need at least the two protocol ints
assert( nMessagePayloadSize >= 2 * sizeof( VistaType::sint32 ) ); //assert( nMessagePayloadSize >= 2 * sizeof( VistaType::sint32 ) );
if( nMessagePayloadSize > ( int ) m_vecIncomingBuffer.size() ) if( nMessagePayloadSize > ( int ) m_vecIncomingBuffer.size() )
m_vecIncomingBuffer.resize( nMessagePayloadSize ); m_vecIncomingBuffer.resize( nMessagePayloadSize );
// Receive all incoming data (potentially splitted) // Receive all incoming data (potentially splitted)
int iBytesReceivedTotal = 0; int iBytesReceivedTotal = 0;
while( nMessagePayloadSize != iBytesReceivedTotal ) while( nMessagePayloadSize < iBytesReceivedTotal )
{ {
int iIncommingBytes = m_pConnection->WaitForIncomingData( 0 ); int iIncommingBytes = m_pConnection->WaitForIncomingData( 0 );
int iBytesReceived = m_pConnection->Receive( &m_vecIncomingBuffer[ iBytesReceivedTotal ], iIncommingBytes ); int iBytesReceived = m_pConnection->Receive( &m_vecIncomingBuffer[ iBytesReceivedTotal ], iIncommingBytes );
...@@ -153,93 +154,8 @@ void CITANetAudioMessage::ReadMessage() ...@@ -153,93 +154,8 @@ void CITANetAudioMessage::ReadMessage()
#if NET_AUDIO_SHOW_TRAFFIC #if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "CITANetAudioMessage [ Reading ] Finished receiving " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl; vstr::out() << "CITANetAudioMessage [ Reading ] Finished receiving " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
#endif #endif
}
void CITANetAudioMessage::WriteAnswer()
{
#if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "CITANetAudioMessage [ Answering] to " << m_nMessageType << " with " << m_nAnswerType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
#endif
assert( m_nAnswerType != CITANetAudioProtocol::NP_INVALID );
VistaType::byte* pBuffer = ( VistaType::byte* )m_oOutgoing.GetBuffer();
VistaType::sint32 iSwapDummy;
// rewrite size dummy
iSwapDummy = m_oOutgoing.GetBufferSize() - sizeof( VistaType::sint32 );
if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
pBuffer += sizeof( VistaType::sint32 );
// rewrite type dummy
iSwapDummy = m_nAnswerType;
if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
pBuffer += sizeof( VistaType::sint32 );
// rewrite message dummy
iSwapDummy = m_nMessageId;
if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
int nRet = m_pConnection->Send( m_oOutgoing.GetBuffer(), m_oOutgoing.GetBufferSize() ); return true;
m_pConnection->WaitForSendFinish();
if( nRet != m_oOutgoing.GetBufferSize() )
ITA_EXCEPT1( UNKNOWN, "Could not write the expected number of bytes" );
}
void CITANetAudioMessage::ReadAnswer()
{
#if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "CITANetAudioMessage [ Reading] yet unkown answer from initial message type " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ") OK" << std::endl;
#endif
VistaType::sint32 nMessagePayloadSize;
int nReturn;
nReturn = m_pConnection->ReadInt32( nMessagePayloadSize );
assert( nReturn == sizeof( VistaType::sint32 ) );
#if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "CITANetAudioMessage [ Reading] Answer type " << nReturn << " (id=" << std::setw( 4 ) << m_nMessageId << ") OK" << std::endl;
#endif
// We need at least the message type and message id in payload
assert( nMessagePayloadSize >= 2 * sizeof( VistaType::sint32 ) );
if( nMessagePayloadSize > ( int ) m_vecIncomingBuffer.size() )
m_vecIncomingBuffer.resize( nMessagePayloadSize );
// @todo: read over while( received < total ) loop!!!
int iBytesReceivedTotal = 0;
while( nMessagePayloadSize != iBytesReceivedTotal )
{
int iIncommingBytes = m_pConnection->WaitForIncomingData( 0 );
int iBytesReceived = m_pConnection->Receive( &m_vecIncomingBuffer[ iBytesReceivedTotal ], iIncommingBytes );
iBytesReceivedTotal += iBytesReceived;
#if NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "[ CITANetAudioMessage ] " << std::setw( 3 ) << std::floor( iBytesReceivedTotal / float( nMessagePayloadSize ) * 100.0f ) << "% of answer transmitted" << std::endl;
#endif
}
if( iBytesReceivedTotal != nMessagePayloadSize )
ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected when trying to receive answer" );
// Swap data to deserialization buffer
m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nMessagePayloadSize, false );
// Take out the two protocol variables type and message id from deserialization buffer
m_nAnswerType = ReadInt();
int nMessageID = ReadInt();
assert( nMessageID == m_nMessageId );
} }
int CITANetAudioMessage::GetMessageType() const int CITANetAudioMessage::GetMessageType() const
...@@ -253,17 +169,6 @@ void CITANetAudioMessage::SetMessageType( int nType ) ...@@ -253,17 +169,6 @@ void CITANetAudioMessage::SetMessageType( int nType )
m_nMessageType = nType; m_nMessageType = nType;
} }
void CITANetAudioMessage::SetAnswerType( int nType )
{
assert( m_nAnswerType == CITANetAudioProtocol::NP_INVALID ); // should only be set once
m_nAnswerType = nType;
}
int CITANetAudioMessage::GetAnswerType() const
{
return m_nAnswerType;
}
int CITANetAudioMessage::GetIncomingMessageSize() const int CITANetAudioMessage::GetIncomingMessageSize() const
{ {
return m_oIncoming.GetTailSize(); return m_oIncoming.GetTailSize();
...@@ -463,4 +368,3 @@ void CITANetAudioMessage::WriteSampleFrame( ITASampleFrame* pSamples ) ...@@ -463,4 +368,3 @@ void CITANetAudioMessage::WriteSampleFrame( ITASampleFrame* pSamples )
} }
} }
} }
...@@ -61,25 +61,10 @@ CITANetAudioStreamingClient::CITANetAudioStreamingClient( CITANetAudioStream* pP ...@@ -61,25 +61,10 @@ CITANetAudioStreamingClient::CITANetAudioStreamingClient( CITANetAudioStream* pP
m_pClientLogger = new ITABufferedDataLoggerImplClient( ); m_pClientLogger = new ITABufferedDataLoggerImplClient( );
m_pClientLogger->setOutputFile(paras); m_pClientLogger->setOutputFile(paras);
iStreamingBlockId = 0; iStreamingBlockId = 0;
m_pMessage = new CITANetAudioMessage( VistaSerializingToolset::SWAPS_MULTIBYTE_VALUES );
} }
CITANetAudioStreamingClient::~CITANetAudioStreamingClient() CITANetAudioStreamingClient::~CITANetAudioStreamingClient()
{ {
//try{
if (m_pConnection->GetIsOpen())
{
m_pMessage->ResetMessage();
m_pMessage->SetConnection(m_pConnection);
m_pMessage->SetMessageType(CITANetAudioProtocol::NP_CLIENT_CLOSE);
m_pMessage->WriteMessage();
m_pClient->Disconnect();
//Disconnect();
}
//}
//catch (ITAException e){
// std::cout << e << std::endl;
//}
delete m_pClientLogger; delete m_pClientLogger;
} }
...@@ -93,20 +78,13 @@ bool CITANetAudioStreamingClient::Connect( const std::string& sAddress, int iPor ...@@ -93,20 +78,13 @@ bool CITANetAudioStreamingClient::Connect( const std::string& sAddress, int iPor
m_pConnection = m_pClient->GetConnection(); m_pConnection = m_pClient->GetConnection();
m_pMessage->ResetMessage(); m_pIncomingMessage = new CITANetAudioMessage( m_pConnection );
m_pMessage->SetConnection( m_pConnection ); m_pOutgoingMessage = new CITANetAudioMessage( m_pConnection );
// Validate streaming parameters of server and client // Validate streaming parameters of server and client
m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_OPEN ); m_pOutgoingMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_OPEN );
m_pMessage->WriteStreamingParameters( m_oParams ); m_pOutgoingMessage->WriteStreamingParameters( m_oParams );
m_pMessage->WriteMessage(); m_pOutgoingMessage->WriteMessage();
m_pMessage->ReadAnswer();
assert( m_pMessage->GetAnswerType() == CITANetAudioProtocol::NP_SERVER_OPEN );
bool bOK = m_pMessage->ReadBool();
if( !bOK )
ITA_EXCEPT1( INVALID_PARAMETER, "Streaming server declined connection, detected streaming parameter mismatch." );
Run(); Run();
...@@ -121,21 +99,25 @@ bool CITANetAudioStreamingClient::LoopBody() ...@@ -121,21 +99,25 @@ bool CITANetAudioStreamingClient::LoopBody()
if( m_bStopIndicated ) if( m_bStopIndicated )
return true; return true;
// Send message to server that samples can be received // Send message to server that (and how many) samples can be received
m_pMessage->ResetMessage(); m_pIncomingMessage->ResetMessage();
m_pMessage->SetConnection( m_pConnection ); if( !m_pIncomingMessage->TryReadMessage() )
m_pMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_WAITING_FOR_SAMPLES ); {
int iFreeSamplesUntilAllowedReached = m_pStream->GetAllowedLatencySamples() - m_pStream->GetRingBufferAvailableSamples(); int iFreeSamplesUntilAllowedReached = m_pStream->GetAllowedLatencySamples() - m_pStream->GetRingBufferAvailableSamples();
oLog.iFreeSamples = iFreeSamplesUntilAllowedReached; oLog.iFreeSamples = iFreeSamplesUntilAllowedReached;
if( iFreeSamplesUntilAllowedReached < 0 ) if( iFreeSamplesUntilAllowedReached < 0 )
iFreeSamplesUntilAllowedReached = 0; iFreeSamplesUntilAllowedReached = 0;
m_pMessage->WriteInt( iFreeSamplesUntilAllowedReached );
m_pMessage->WriteMessage(); m_pOutgoingMessage->ResetMessage();
m_pIncomingMessage->SetMessageType( CITANetAudioProtocol::NP_CLIENT_WAITING_FOR_SAMPLES );
// Wait for answer of server m_pOutgoingMessage->WriteInt( iFreeSamplesUntilAllowedReached );
m_pMessage->ReadAnswer(); m_pOutgoingMessage->WriteMessage();
int iAnswerType = m_pMessage->GetAnswerType();
switch( iAnswerType ) return false;
}
int iIncomingMessageType = m_pIncomingMessage->GetMessageType();
switch( iIncomingMessageType )
{ {
case CITANetAudioProtocol::NP_INVALID: case CITANetAudioProtocol::NP_INVALID:
...@@ -147,15 +129,10 @@ bool CITANetAudioStreamingClient::LoopBody() ...@@ -147,15 +129,10 @@ bool CITANetAudioStreamingClient::LoopBody()
Disconnect(); Disconnect();
break; break;
case CITANetAudioProtocol::NP_SERVER_WAITING_FOR_TRIGGER:
// Wait until block increment is triggered by audio context (more free samples in ring buffer)
m_oBlockIncrementEvent.WaitForEvent( true );
break;
case CITANetAudioProtocol::NP_SERVER_SEND_SAMPLES: case CITANetAudioProtocol::NP_SERVER_SEND_SAMPLES:
// Receive samples from net message and forward them to the stream ring buffer // Receive samples from net message and forward them to the stream ring buffer
m_pMessage->ReadSampleFrame( &m_sfReceivingBuffer ); m_pIncomingMessage->ReadSampleFrame( &m_sfReceivingBuffer );
if ( m_pStream->GetRingBufferFreeSamples( ) >= m_sfReceivingBuffer.GetLength( ) ) if ( m_pStream->GetRingBufferFreeSamples( ) >= m_sfReceivingBuffer.GetLength( ) )
m_pStream->Transmit( m_sfReceivingBuffer, m_sfReceivingBuffer.GetLength( ) ); m_pStream->Transmit( m_sfReceivingBuffer, m_sfReceivingBuffer.GetLength( ) );
//else //else
...@@ -166,7 +143,7 @@ bool CITANetAudioStreamingClient::LoopBody() ...@@ -166,7 +143,7 @@ bool CITANetAudioStreamingClient::LoopBody()
break; break;
} }
oLog.iChannel = m_pStream->GetNumberOfChannels(); oLog.iChannel = m_pStream->GetNumberOfChannels();
oLog.iProtocolStatus = iAnswerType; oLog.iProtocolStatus = iIncomingMessageType;
oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( ); oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
m_pClientLogger->log( oLog ); m_pClientLogger->log( oLog );
return true; return true;
...@@ -185,9 +162,10 @@ bool CITANetAudioStreamingClient::GetIsConnected() const ...@@ -185,9 +162,10 @@ bool CITANetAudioStreamingClient::GetIsConnected() const
void CITANetAudioStreamingClient::Disconnect() void CITANetAudioStreamingClient::Disconnect()
{ {
m_bStopIndicated = true; m_bStopIndicated = true;
StopGently( true );
//delete m_pConnection; delete m_pIncomingMessage;
delete m_pOutgoingMessage;
m_pConnection = NULL; m_pConnection = NULL;
m_bStopIndicated = false; m_bStopIndicated = false;
......
...@@ -38,105 +38,86 @@ bool CITANetAudioStreamingServer::Start( const std::string& sAddress, int iPort ...@@ -38,105 +38,86 @@ bool CITANetAudioStreamingServer::Start( const std::string& sAddress, int iPort
m_pConnection = m_pNetAudioServer->GetConnection(); m_pConnection = m_pNetAudioServer->GetConnection();
m_pMessage = new CITANetAudioMessage( m_pConnection->GetByteorderSwapFlag() ); m_pIncomingMessage = new CITANetAudioMessage( m_pConnection );
m_pMessage->ResetMessage(); m_pOutgoingMessage = new CITANetAudioMessage( m_pConnection );
m_pMessage->SetConnection( m_pConnection );
m_pMessage->ReadMessage(); // blocking
assert( m_pMessage->GetMessageType() == CITANetAudioProtocol::NP_CLIENT_OPEN ); Run();
CITANetAudioProtocol::StreamingParameters oClientParams = m_pMessage->ReadStreamingParameters();
bool bOK = false; return true;
if( m_oServerParams == oClientParams )
{
bOK = true;
#ifdef NET_AUDIO_SHOW_TRAFFIC
vstr::out() << "[ITANetAudioStreamingServer] Server and client parameters matched. Will resume with streaming" << std::endl;
}
else
{
vstr::out() << "[ITANetAudioStreamingServer] Server and client parameters mismatch detected. Will notify client and stop." << std::endl;
#endif
}
m_pMessage->SetAnswerType( CITANetAudioProtocol::NP_SERVER_OPEN );
m_pMessage->WriteBool( bOK );
m_pMessage->WriteAnswer();
if( bOK )
Run();
return bOK;
} }
bool CITANetAudioStreamingServer::LoopBody() bool CITANetAudioStreamingServer::LoopBody()
{ {
m_pMessage->ResetMessage(); m_pIncomingMessage->ResetMessage();
m_pMessage->SetConnection( m_pConnection );
m_pMessage->ReadMessage(); // blocking
int iMsgType = m_pMessage->GetMessageType(); if( m_pIncomingMessage->TryReadMessage() )
switch( iMsgType )
{
case CITANetAudioProtocol::NP_CLIENT_WAITING_FOR_SAMPLES:
{ {
int iFreeSamples = m_pMessage->ReadInt(); int iMsgType = m_pIncomingMessage->GetMessageType();