ITANetAudioStreamingServer.cpp 8.51 KB
Newer Older
Anne's avatar
Anne committed
1 2
#include <ITANetAudioStreamingServer.h>
#include <ITANetAudioServer.h>
3
#include <ITANetAudioMessage.h>
Anne's avatar
Anne committed
4 5

// ITA includes
Anne's avatar
Anne committed
6
#include <ITADataSource.h>
7
#include <ITANetAudioMessage.h>
Anne's avatar
Anne committed
8
#include <ITAException.h>
9
#include <ITAStreamInfo.h>
Anne's avatar
Anne committed
10
#include <ITAClock.h>
Anne's avatar
Anne committed
11 12 13 14 15 16 17

// Vista includes
#include <VistaInterProcComm/Concurrency/VistaThreadLoop.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaInterProcComm/IPNet/VistaTCPSocket.h>
#include <VistaBase/VistaTimeUtils.h>
#include <VistaInterProcComm/IPNet/VistaIPAddress.h>
18
#include <VistaBase/VistaStreamUtils.h>
Anne's avatar
Anne committed
19
#include <ITADataLog.h>
Anne's avatar
Anne committed
20 21 22 23 24

// STL
#include <cmath>
#include <cassert>

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
struct ITAServerLog : public ITALogDataBase
{
	inline static std::ostream& outputDesc( std::ostream& os )
	{
		os << "BlockId";
		os << "\t" << "WorldTimeStamp";
		os << "\t" << "ProtocolStatus";
		os << "\t" << "FreeSamples";
		os << std::endl;
		return os;
	};

	inline std::ostream& outputData( std::ostream& os ) const
	{
		os << uiBlockId;
		os << "\t" << std::setprecision( 12 ) << dWorldTimeStamp;
		os << "\t" << iProtocolStatus;
		os << "\t" << iFreeSamples;
		os << std::endl;
		return os;
	};

	unsigned int uiBlockId; //!< Block identifier (audio streaming)
	double dWorldTimeStamp;
	int iProtocolStatus; //!< ... usw
	int iFreeSamples;
Anne's avatar
Anne committed
51 52 53 54
};

class ITABufferedDataLoggerImplServer : public ITABufferedDataLogger < ITAServerLog > {};

Anne's avatar
Anne committed
55 56 57 58 59
CITANetAudioStreamingServer::CITANetAudioStreamingServer( )
: m_pInputStream( NULL )
, m_iUpdateStrategy( AUTO )
, m_pConnection( NULL )
, m_pNetAudioServer( new CITANetAudioServer( ) )
60
{
Anne's avatar
Anne committed
61
	iServerBlockId = 0;
62
	m_iMaxSendBlocks = 20;
Anne Heimes's avatar
Anne Heimes committed
63
	m_iClientRingBufferFreeSamples = 0;
Anne's avatar
Anne committed
64 65
}

66
bool CITANetAudioStreamingServer::Start( const std::string& sAddress, int iPort )
Anne's avatar
Anne committed
67
{
Anne's avatar
Anne committed
68
	if ( !m_pInputStream )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
69 70
		ITA_EXCEPT1( MODAL_EXCEPTION, "Can not start server without a valid input stream" );

Anne's avatar
Anne committed
71
	if ( !m_pNetAudioServer->Start( sAddress, iPort ) ) // blocking
72 73
		return false;

Anne's avatar
Anne committed
74
	m_pConnection = m_pNetAudioServer->GetConnection( );
75

Anne's avatar
Anne committed
76 77
	m_pMessage = new CITANetAudioMessage( m_pConnection->GetByteorderSwapFlag( ) );
	m_pMessage->ResetMessage( );
78
	m_pMessage->SetConnection( m_pConnection );
79
	while ( !m_pMessage->ReadMessage( 0 ) ); //blocking
80

Anne's avatar
Anne committed
81 82
	assert( m_pMessage->GetMessageType( ) == CITANetAudioProtocol::NP_CLIENT_OPEN );
	CITANetAudioProtocol::StreamingParameters oClientParams = m_pMessage->ReadStreamingParameters( );
83

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
84
	bool bOK = false;
Anne's avatar
Anne committed
85
	m_oServerParams.iRingBufferSize = oClientParams.iRingBufferSize;
Anne's avatar
Anne committed
86
	m_iClientRingBufferFreeSamples = m_oServerParams.iRingBufferSize;
Anne's avatar
Anne committed
87
	if ( m_oServerParams == oClientParams )
Anne's avatar
Anne committed
88
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
89
		bOK = true;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
90
#ifdef NET_AUDIO_SHOW_TRAFFIC
91
		vstr::out() << "[ITANetAudioStreamingServer] Server and client parameters matched. Will resume with streaming" << std::endl;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
92 93 94
	}
	else
	{
95
		vstr::out() << "[ITANetAudioStreamingServer] Server and client parameters mismatch detected. Will notify client and stop." << std::endl;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
96
#endif
Anne's avatar
Anne committed
97
	}
98 99 100

	std::string paras = std::string( "NetAudioLogServer" ) + std::string( "_BS" ) + std::to_string( m_oServerParams.iBlockSize ) + std::string( "_Ch" ) + std::to_string( m_oServerParams.iChannels ) + std::string( ".txt" );
	m_pServerLogger = new ITABufferedDataLoggerImplServer( );
Anne's avatar
Anne committed
101
	m_pServerLogger->setOutputFile( paras );
Anne's avatar
Anne committed
102

Anne's avatar
Anne committed
103
	m_pMessage->SetMessageType( CITANetAudioProtocol::NP_SERVER_OPEN );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
104
	m_pMessage->WriteBool( bOK );
Anne's avatar
Anne committed
105
	m_pMessage->WriteMessage( );
106

Anne Heimes's avatar
Anne Heimes committed
107 108 109
	m_sfTempTransmitBuffer.init( m_pInputStream->GetNumberOfChannels( ), m_oServerParams.iRingBufferSize, true );


Anne's avatar
Anne committed
110 111
	if ( bOK )
		Run( );
112

Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
113
	return bOK;
Anne's avatar
Anne committed
114 115
}

Anne's avatar
Anne committed
116
bool CITANetAudioStreamingServer::LoopBody( )
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
117
{
118
	bool bAskClient = false;
119
	m_pMessage->ResetMessage( );
Anne's avatar
Anne committed
120 121 122
	ITAServerLog oLog;
	oLog.uiBlockId = ++iServerBlockId;
	int iMsgType;
Anne's avatar
Anne committed
123
	// Sending Samples 
124 125
	unsigned int uiBlockLength = m_pInputStream->GetBlocklength( );
	if ( m_iClientRingBufferFreeSamples >= uiBlockLength )
Anne's avatar
Anne committed
126 127
	{
		// Send Samples
128 129 130 131 132
		// Sende max 10 * Blocklnge aufeinmal
		int iSendBlocks;
		if ( m_iClientRingBufferFreeSamples > m_iMaxSendBlocks * uiBlockLength )
			iSendBlocks = m_iMaxSendBlocks;
		else
Anne's avatar
Anne committed
133
		{
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
			iSendBlocks = m_iClientRingBufferFreeSamples / uiBlockLength;
			bAskClient = true;
		}
		if ( m_sfTempTransmitBuffer.GetLength( ) != iSendBlocks * uiBlockLength )
			m_sfTempTransmitBuffer.init( m_pInputStream->GetNumberOfChannels( ), iSendBlocks * uiBlockLength, false );
		
		for ( int j = 0; j < iSendBlocks; j++ )
		{
			for ( int i = 0; i < int( m_pInputStream->GetNumberOfChannels( ) ); i++ )
			{
				ITAStreamInfo oStreamInfo;
				oStreamInfo.nSamples = uiBlockLength;

				const float* pfData = m_pInputStream->GetBlockPointer( i, &oStreamInfo );
				if ( pfData != 0 )
					m_sfTempTransmitBuffer[ i ].write( pfData, uiBlockLength, j * uiBlockLength );
			}
			m_pInputStream->IncrementBlockPointer( );
Anne Heimes's avatar
Anne Heimes committed
152 153 154 155 156
		}
		iMsgType = CITANetAudioProtocol::NP_SERVER_SENDING_SAMPLES;
		m_pMessage->SetMessageType( iMsgType );
		m_pMessage->WriteSampleFrame( &m_sfTempTransmitBuffer );
		m_pMessage->WriteMessage( );
157
		m_iClientRingBufferFreeSamples -= iSendBlocks * uiBlockLength;
Anne's avatar
Anne committed
158
#ifdef NET_AUDIO_SHOW_TRAFFIC
159
		vstr::out( ) << "[ITANetAudioStreamingServer] Transmitted " << iSendSamples << " samples for "
Anne Heimes's avatar
Anne Heimes committed
160
			<< m_pInputStream->GetNumberOfChannels( ) << " channels" << std::endl;
Anne's avatar
Anne committed
161
#endif
162 163 164 165 166 167 168
		
	}
	else
		bAskClient = true;

	if ( bAskClient )
	{
Anne's avatar
Anne committed
169
		// Waiting for Trigger
170 171 172 173 174 175
#ifdef NET_AUDIO_SHOW_TRAFFIC
		vstr::out( ) << "[ITANetAudioStreamingServer] Not enough free samples in client buffer, requesting a trigger when more free samples available" << std::endl;
#endif
		ITAServerLog oLog;
		oLog.uiBlockId = ++iServerBlockId;
		m_pMessage->ResetMessage( );
Anne's avatar
Anne committed
176 177
		iMsgType = CITANetAudioProtocol::NP_SERVER_GET_RINGBUFFER_FREE_SAMPLES;
		m_pMessage->SetMessageType( iMsgType );
178
		m_pMessage->WriteBool( true );
Anne's avatar
Anne committed
179
		m_pMessage->WriteMessage( );
Anne Heimes's avatar
Anne Heimes committed
180
		oLog.iProtocolStatus = iMsgType;
181 182 183
		oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
		oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
		m_pServerLogger->log( oLog );
Anne's avatar
Anne committed
184
	}
185

Anne's avatar
Anne committed
186 187

	// Try to Empfange Daten
Anne's avatar
Anne committed
188
	m_pMessage->ResetMessage( );
189

190
	if ( m_pMessage->ReadMessage( 1 ) )
191
	{
Anne's avatar
Anne committed
192 193
		ITAServerLog oLog;
		oLog.uiBlockId = ++iServerBlockId;
Anne's avatar
Anne committed
194 195
		int iMsgType = m_pMessage->GetMessageType( );
		switch ( iMsgType )
196
		{
Anne's avatar
Anne committed
197
			case CITANetAudioProtocol::NP_CLIENT_SENDING_RINGBUFFER_FREE_SAMPLES:
198
			{
Anne's avatar
Anne committed
199
				m_iClientRingBufferFreeSamples = m_pMessage->ReadInt( );	
Anne's avatar
Anne committed
200 201 202 203 204
				break;
			}
			case CITANetAudioProtocol::NP_CLIENT_CLOSE:
			{
				StopGently( false );
Anne's avatar
Anne committed
205
				m_pConnection = NULL;
Anne's avatar
Anne committed
206
				Stop( );
Anne's avatar
Anne committed
207
				break;
Anne's avatar
Anne committed
208 209 210 211 212 213
			}
			default:
			{
				vstr::out( ) << "[ITANetAudioStreamingServer] Unkown protocol type : " << iMsgType << std::endl;
				break;
			}
214 215 216
		}
		oLog.iFreeSamples = m_iClientRingBufferFreeSamples;
		oLog.iProtocolStatus = iMsgType;
Anne's avatar
Anne committed
217 218
		oLog.dWorldTimeStamp = ITAClock::getDefaultClock( )->getTime( );
		m_pServerLogger->log( oLog );
219
	}
220 221 222 223
	else
	{
		/*
		// Neue Sample bei ca 1ms (0,9ms) warten
224
		float dSamples = 0.0009 * m_pInputStream->GetSampleRate();
225 226 227 228 229
		int iSamples = ( int ) dSamples;
		int offset = iSamples % m_pInputStream->GetBlocklength( );
		m_iClientRingBufferFreeSamples += iSamples - offset;
		*/
	}
230 231


Anne's avatar
Anne committed
232
	return false;
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
233 234
}

235 236
void CITANetAudioStreamingServer::SetInputStream( ITADatasource* pInStream )
{
Anne's avatar
Anne committed
237
	if ( VistaThreadLoop::IsRunning( ) )
238 239 240
		ITA_EXCEPT1( MODAL_EXCEPTION, "Streaming loop already running, can not change input stream" );

	m_pInputStream = pInStream;
Anne's avatar
Anne committed
241 242 243
	m_oServerParams.dSampleRate = m_pInputStream->GetSampleRate( );
	m_oServerParams.iBlockSize = m_pInputStream->GetBlocklength( );
	m_oServerParams.iChannels = m_pInputStream->GetNumberOfChannels( );
244 245
}

Anne's avatar
Anne committed
246
ITADatasource* CITANetAudioStreamingServer::GetInputStream( ) const
Anne's avatar
Anne committed
247 248 249 250
{
	return m_pInputStream;
}

Anne's avatar
Anne committed
251
int CITANetAudioStreamingServer::GetNetStreamBlocklength( ) const
252
{
Anne's avatar
Anne committed
253
	return m_sfTempTransmitBuffer.GetLength( );
254 255
}

Anne's avatar
Anne committed
256
int CITANetAudioStreamingServer::GetNetStreamNumberOfChannels( ) const
257
{
Anne's avatar
Anne committed
258
	return m_sfTempTransmitBuffer.channels( );
259 260
}

Anne's avatar
Anne committed
261
void CITANetAudioStreamingServer::SetAutomaticUpdateRate( )
262 263 264 265
{
	m_iUpdateStrategy = AUTO;
}

Anne's avatar
Anne committed
266
bool CITANetAudioStreamingServer::IsClientConnected( ) const
267
{
Anne's avatar
Anne committed
268
	return m_pNetAudioServer->IsConnected( );
269 270
}

Anne's avatar
Anne committed
271
std::string CITANetAudioStreamingServer::GetNetworkAddress( ) const
272
{
Anne's avatar
Anne committed
273
	return m_pNetAudioServer->GetServerAddress( );
274 275
}

276 277


Anne's avatar
Anne committed
278
int CITANetAudioStreamingServer::GetNetworkPort( ) const
279
{
Anne's avatar
Anne committed
280
	return m_pNetAudioServer->GetNetworkPort( );
281 282
}

Anne's avatar
Anne committed
283
void CITANetAudioStreamingServer::Stop( )
Anne's avatar
Anne committed
284
{
Anne's avatar
Anne committed
285
	delete m_pServerLogger;
Anne's avatar
Anne committed
286
	m_pNetAudioServer->Stop( );
Anne's avatar
Anne committed
287
}